poj3696 The Luckiest number

来源:互联网 发布:海上风电 知乎 编辑:程序博客网 时间:2024/05/24 07:10
The Luckiest number
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5120 Accepted: 1370

Description

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of only digit '8'.

Input

The input consists of multiple test cases. Each test case contains exactly one line containing L(1 ≤ L ≤ 2,000,000,000).

The last test case is followed by a line containing a zero.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the length of Bob's luckiest number. If Bob can't construct his luckiest number, print a zero.

Sample Input

811160

Sample Output

Case 1: 1Case 2: 2Case 3: 0
//(10^x-1)=L*p*9/8;//所以m=(9*L)/(gcd(L,8))//所以10^x-1=m*p1//所以转化成求同余方程 (10^x=1)modm#include<vector> #include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;vector<LL>mp;bool book[50000];LL p[20000];LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}void prim(){memset(book,false,sizeof(book));book[0]=book[1]=1;int k=0;for(int i=2;i<50000;i++){if(!book[i])p[k++]=i;for(int j=0;j<k&&i*p[j]<50000;j++){book[i*p[j]]=1;if(!(i%p[j]))break;}}}LL mul(LL a,LL b,LL MOD){LL ans=0;while(b){if(b&1)ans+=a,ans%=MOD;b>>=1;a+=a,a%=MOD;}return (ans%MOD+MOD)%MOD;}LL getphi(LL n){LL rea=n;for(int i=0;p[i]*p[i]<=n;i++)if(n%p[i]==0){rea-=rea/p[i];while(n%p[i]==0)n/=p[i];}if(n>1)rea=rea-rea/n;return rea;}LL quickpow(LL n,LL m,LL MOD){LL b=1;while(m){if(m&1)b=mul(b,n,MOD);m>>=1;n=mul(n,n,MOD);}return (b%MOD+MOD)%MOD;}void getfac(LL m){mp.clear();for(int i=0;p[i]*p[i]<=m;i++)if(m%p[i]==0)mp.push_back(p[i]),m/=p[i];if(m>1)mp.push_back(m);  }int main(){LL L;prim();while(scanf("%lld",&L)==1){LL m=(9*L)/(gcd(L,8));LL phi=getphi(m);LL ans=phi;bool flag=true;while(flag){getfac(phi);flag=false;for(int i=0;i<mp.size();i++){if(quickpow(10,phi/mp[i],m)==1){flag=true;if(phi/mp[i]<ans)ans=(phi/mp[i]);} }phi=ans;}if(gcd(10,m)!=1)ans=0;printf("%lld\n",ans);}}

0 0
原创粉丝点击