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
题意:求最小的由8组成的数,是L的倍数。
思路:由一系列证明得到,ans=phi(N)的满足题意的最小因子。
关键: 对于X,求其满足题意的最小因子p|X,可以这样求,枚举素因子prime,如果X/prime满足题意,则X=X/prime。
存疑:我感觉复杂度是根号级别的,但是我看到ppt上说是log级别。
#include<cstdio> #include<cstdlib> #include<iostream> using namespace std; #define ll long long ll gcd(ll a,ll b){ if(b==0) return a;return gcd(b,a%b);} ll qmul(ll a,ll x,ll Mod){ll res=0; a%=Mod; while(x){if(x&1) res=(res+a)%Mod;a=(a+a)%Mod;x>>=1;} return res;} ll qpow(ll a,ll x,ll Mod){ll res=1; a%=Mod; while(x){if(x&1LL) res=qmul(res,a,Mod); a=qmul(a,a,Mod); x>>=1;} return res;} ll phi(ll x) { ll tx=x,res=x; for(int i=2;i*i<=tx;i++){ if(tx%i==0){ res-=res/i; while(tx%i==0) tx/=i; } } if(tx>1) res-=res/tx; return res; } ll find(ll Mod,ll py) //得到最小因子满足条件 { ll n=py; ll tp[50][2]; int k=0; for(ll i=2;i*i<=n;i++) //唯一分解 if(n%i==0){ k++; tp[k][0]=i; tp[k][1]=0; while(n%i==0){ n/=i; tp[k][1]++; } } if(n>1) k++, tp[k][0]=n, tp[k][1]=1; for(int i=1;i<=k;i++) for(int j=1;j<=tp[i][1];j++) if(qpow(10,py/tp[i][0],Mod)==1) py/=tp[i][0]; return py; } int main() { ll N,M,Case=0; while(~scanf("%lld",&N)&&N){ printf("Case %lld: ",++Case); N=N*9/gcd(N,8); if(gcd(N,10)!=1) printf("0 "); else printf("%d ",find(N,phi(N))); } return 0; }