「POJ3696」The Luckiest number
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
1 2 3 4 |
8 11 16 0 |
Sample Output
1 2 3 |
Case 1: 1 Case 2: 2 Case 3: 0 |
题解
discuss求阶,注意求欧拉函数可能会爆long long
可以先把乘9放在一边最后再乘。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#include<iostream> #include<set> #include<map> #include<cstdio> #include<cstring> #include<cstdlib> #include<ctime> #include<vector> #include<queue> #include<algorithm> #include<cmath> #define inf 1000000000 #define pa pair<int,int> #define ll unsigned long long using namespace std; int tst,tot; ll pri[100005]; ll n,p,L,mx; bool mark[100005]; vector<ll> q; void getpri() { for(int i=2;i<=100000;i++) { if(!mark[i])pri[++tot]=i; for(int j=1;j<=tot&&pri[j]*i<=100000;j++) { mark[pri[j]*i]=1; if(i%pri[j]==0)break; } } } ll mul(ll a,ll b) { ll ans=0;a%=p; for(ll i=b;i;i>>=1,a=(a+a)%p) if(i&1)ans=(ans+a)%p; return ans; } ll qpow(ll a,ll b) { ll ans=1;a%=p; for(ll i=b;i;i>>=1,a=mul(a,a)) if(i&1)ans=mul(ans,a)%p; return ans; } ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); } ll phi(ll x) { ll ans=x,tmp=x; for(int i=1;pri[i]*pri[i]<=x;i++) { if(x%pri[i]==0)ans=ans*(pri[i]-1)/pri[i]; while(x%pri[i]==0)x/=pri[i]; } if(x!=1)ans=ans*(x-1)/x; return ans; } void solve() { q.clear(); for(ll i=1;i*i<=mx;i++) if(mx%i==0) q.push_back(i),q.push_back(mx/i); sort(q.begin(),q.end()); for(int i=0;i<q.size();i++) if(qpow(10,q[i])==1){printf("%lld\n",q[i]);return;} puts("0"); } int main() { getpri(); while(scanf("%lld",&L)) { if(!L)break; printf("Case %d: ",++tst); p=L/gcd(L,8); if(gcd(10,p*9)!=1)puts("0"); else { mx=phi(p); if(p%3!=0)mx*=6; else mx*=9; p*=9; solve(); } } return 0; } |