Loading

[POJ3696] The Luckiest number

\(\text{Description}\)

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

\(\text{Method}\)

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

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

\[M|10^x-1 \]

\[10^x\equiv 1\pmod{M} \]

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

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

\[x|\varphi(M) \]

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

\(\text{Else}:\)

\[x \text{ doesn't exist.} \]


\(\text{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\n",cnt,calc(n));
	}
	return 0;
}
posted @ 2019-12-15 11:01  pjykk  阅读(159)  评论(0编辑  收藏  举报