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)
因此, 字需要枚举其因子即可;
参考代码:
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- using namespace std;
- #define clr(a,val) memset(a,val,sizeof(a))
- typedef long long ll;
- const int INF=0x3f3f3f3f;
- ll L,fac[1010]={0};
- inline ll phi(ll x)
- {
- ll p=x,s=x;
- for(ll i=2;i*i<=s;++i)
- if(!(x%i))
- {
- p=p/i*(i-1);
- while(!(x%i)) x/=i;
- }
- if(x>1) p=p/x*(x-1);
- return p;
- }
- inline void find_factor(ll x)
- {
- ll s=x;
- fac[0]=0;
- for(ll i=2;i*i<=s;++i)
- if(!(x%i))
- {
- fac[++fac[0]]=i;
- while(!(x%i)) x/=i;
- }
- if(x>1) fac[++fac[0]]=x;
- }
- inline ll mult(ll a,ll b,ll mod)
- {
- a%=mod; b%=mod;
- ll s=a,sum=0;
- while(b)
- {
- if(b&1)
- {
- sum+=s;
- if(sum>=mod) sum-=mod;
- }
- b>>=1;s<<=1;
- if(s>=mod) s-=mod;
- }
- return sum;
- }
- ll power(ll a,ll b,ll mod)
- {
- ll s=a,sum=1;
- while(b)
- {
- if(b&1) sum=mult(sum,s,mod);
- b>>=1;s=mult(s,s,mod);
- }
- return sum;
- }
- ll gcd(ll a,ll b) {return b==0? a:gcd(b,a%b);}
- int main()
- {
- int t=0;
- while(~scanf("%lld",&L) && L)
- {
- ++t;
- ll m=L/gcd(L,8)*9,p=phi(m),x=p;
- if(gcd(m,10)!=1) {printf("Case %d: 0\n",t);continue;}
- find_factor(p);
- for(int i=1;i<=fac[0];++i)
- {
- while(1)
- {
- x/=fac[i];
- if(power(10,x,m)!=1)
- {
- x*=fac[i];
- break;
- }
- else if(x%fac[i]) break;
- }
- }
- printf("Case %d: %lld\n",t,x);
- }
- return 0;
- }
- View Code
来源: http://www.bubuko.com/infodetail-2956941.html