POJ3696 The Luckiest Number
Posted songorz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3696 The Luckiest Number相关的知识,希望对你有一定的参考价值。
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
8 11 16 0
Sample Output
Case 1: 1 Case 2: 2 Case 3: 0
题解:这种题目一般都是 转化为: num*(10^x-1)/9= L*k
8*(10^x-1)=9L*k
设 d=gcd(9L,8)=gcd(8,L)
p=8/d,q=9L/d;
则: p*(10^x-1) q*k;
因为q,p互质,则q|(10^x-1) ,p|k
则 10^x-1=0(mod q)
10^x =1(mod q)
10^x=1(mod 9L/d)
当q与10互质时10^(oula(q))=1(mod q)
因此,字需要枚举其因子即可;
参考代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 #define clr(a,val) memset(a,val,sizeof(a)) 7 typedef long long ll; 8 const int INF=0x3f3f3f3f; 9 ll L,fac[1010]={0}; 10 inline ll phi(ll x) 11 { 12 ll p=x,s=x; 13 for(ll i=2;i*i<=s;++i) 14 if(!(x%i)) 15 { 16 p=p/i*(i-1); 17 while(!(x%i)) x/=i; 18 } 19 if(x>1) p=p/x*(x-1); 20 return p; 21 } 22 23 inline void find_factor(ll x) 24 { 25 ll s=x; 26 fac[0]=0; 27 for(ll i=2;i*i<=s;++i) 28 if(!(x%i)) 29 { 30 fac[++fac[0]]=i; 31 while(!(x%i)) x/=i; 32 } 33 if(x>1) fac[++fac[0]]=x; 34 } 35 36 inline ll mult(ll a,ll b,ll mod) 37 { 38 a%=mod; b%=mod; 39 ll s=a,sum=0; 40 while(b) 41 { 42 if(b&1) 43 { 44 sum+=s; 45 if(sum>=mod) sum-=mod; 46 } 47 b>>=1;s<<=1; 48 if(s>=mod) s-=mod; 49 } 50 return sum; 51 } 52 ll power(ll a,ll b,ll mod) 53 { 54 ll s=a,sum=1; 55 while(b) 56 { 57 if(b&1) sum=mult(sum,s,mod); 58 b>>=1;s=mult(s,s,mod); 59 } 60 return sum; 61 } 62 ll gcd(ll a,ll b) {return b==0? a:gcd(b,a%b);} 63 int main() 64 { 65 int t=0; 66 while(~scanf("%lld",&L) && L) 67 { 68 ++t; 69 ll m=L/gcd(L,8)*9,p=phi(m),x=p; 70 if(gcd(m,10)!=1) {printf("Case %d: 0 ",t);continue;} 71 find_factor(p); 72 for(int i=1;i<=fac[0];++i) 73 { 74 while(1) 75 { 76 x/=fac[i]; 77 if(power(10,x,m)!=1) 78 { 79 x*=fac[i]; 80 break; 81 } 82 else if(x%fac[i]) break; 83 } 84 } 85 printf("Case %d: %lld ",t,x); 86 } 87 return 0; 88 }
以上是关于POJ3696 The Luckiest Number的主要内容,如果未能解决你的问题,请参考以下文章