[POJ3696]The Luckiest number

Posted pjykk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ3696]The Luckiest number相关的知识,希望对你有一定的参考价值。

( ext{Description})

  • ( ext{Given a number }L(Lleqslant 2,000,000,000), ext{find a number }x, ext{ so that }L|8 imes(10^x-1)/9.)
  • ( ext{Output the length of }x. ext{If x doesn't exist, output 0.})

( ext{Method})

[L|8 imes(10^x-1)/9]

( ext{Let }M=dfrac{9L}{gcd(L,8)},)

[M|10^x-1]

[10^xequiv 1pmod{M}]

( ext{Use Euler's Theorem}quad gcd(a,m)=1Rightarrow a^{varphi(m)}equiv 1pmod{m},)

( ext{If }gcd(10,M) ext{ equals }1:)

[x|varphi(M)]

( ext{Then count the divisors of }varphi(M), ext{ and find the smallest }x.)

( ext{Else}:)

[x ext{ doesn't exist.}]


( ext{Code})

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define int long long
using namespace std;
int qmul(int a,int b,int mod)
{
    if(a==0||b==0||mod==1ll)return 0;
    if(b==1ll)return a%mod;
    int ans=qmul(a,b/2ll,mod);
    ans+=ans,ans%=mod;
    if(b%2ll)ans+=a,ans%=mod;
    return ans;
}
int qpow(int a,int b,int mod)
{
    if(a==0||mod==1ll)return 0;
    if(b==0)return 1ll;
    int ans=qpow(a,b/2ll,mod);
    ans=qmul(ans,ans,mod),ans%=mod;
    if(b%2ll)ans=qmul(ans,a,mod),ans%=mod;
    return ans;
}
int gcd(int a,int b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int geteuler(int n)
{
    if(n==1ll)return 0;
    int limit=sqrt(n),ans=n;
    for(int i=2ll;i<=limit;i++)
        if(n%i==0)
        {
            ans-=ans/i;
            while(n%i==0)n/=i;
        }
    if(n>1ll)ans-=ans/n;
    return ans;
}
int calc(int l)
{
    int x=l/gcd(l,8ll)*9ll;
    int flag=gcd(10ll,x);
    if(flag!=1ll)return 0;
    int phi=geteuler(x);
    int limit=sqrt(phi),smallest;
    for(int i=1ll;i<=limit;i++)
        if(phi%i==0)
        {
            if(qpow(10,i,x)==1)return i;
            int another=phi/i;
            if(qpow(10,another,x)==1)smallest=another;
        }
    return smallest;
}
int n,cnt;
signed main()
{
    while(~scanf("%lld",&n))
    {
        if(n==0)break;
        cnt++;
        printf("Case %lld: %lld
",cnt,calc(n));
    }
    return 0;
}

以上是关于[POJ3696]The Luckiest number的主要内容,如果未能解决你的问题,请参考以下文章

[POJ3696]The Luckiest number

poj 3696 The Luckiest Number

POJ 3696 The Luckiest number

「POJ3696」The Luckiest number数论,欧拉函数

poj-3696 The Luckiest number

POJ_3696 The Luckiest number 欧拉定理+同余式+对取模的理解