标签:gcd ll number 3696 POJ ans test 欧拉 mod
The Luckiest numberTime Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10897 | Accepted: 2769 |
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
8 11 16 0
Sample Output
Case 1: 1 Case 2: 2 Case 3: 0
Source
2008 Asia Hefei Regional Contest Online by USTC//关键在于欧拉函数a^φ(n)≡1 (mod n)此时不能保证 φ(n)是最小的^x≡1 (mod n) /*证明;x0为a^x≡1 (mod n)的最小值且x0为x的因子; 若不然 设φ(n)=ax0+r;r不为0; a^ax0*a^r≡1(mod n) ==>a^r≡1(mod n) ,与假设x0为最小矛盾,因为r<x0; */ #include<iostream> #include<vector> #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll gcd(ll a,ll b){ return !b?a:gcd(b,a%b); } ll quimul(ll a,ll b,ll mod) { ll ans=0; while(b) { if(b&1)ans=(ans+a)%mod; b>>=1; a=(a+a)%mod; } return ans; } ll quipow(ll a, ll n,ll mod) { ll ans=1%mod; while(n) { if(n&1)ans=quimul(ans,a,mod); n>>=1; a=quimul(a,a,mod); } return ans; } ll euler( ll n) { ll ans=n; for(int i=2;i*i<=n;i++) { if(n%i==0) { ans=1ll*ans/i*(i-1); while(n%i==0)n/=i; } } if(n>1)ans=1ll*ans/n*(n-1); return ans; } int main() { ll L,tot=1; while(~scanf("%lld",&L)&&L) { ll ans=0; ll d=gcd(L,8LL); ll mod=1ll*9*L/d,n; vector<ll>f; ll gd=gcd(10LL,mod); if(gd!=1){ printf("Case %lld: %lld\n",tot++,0ll); continue; } n=euler(mod); for(ll i=1;i*i<=n;i++) { if(n%i==0) { f.push_back(i); if(1ll*i*i!=n)f.push_back(n/i); } } sort(f.begin(),f.end()); for(ll i=0;i<f.size();i++) { if(quipow(10,f[i],mod)==1) { ans=f[i]; break; } } printf("Case %lld: %lld\n",tot++,ans); } return 0; }
标签:gcd,ll,number,3696,POJ,ans,test,欧拉,mod 来源: https://www.cnblogs.com/matt-su/p/15969068.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。