The Luckiest number(hdu2462)
2016-10-02 23:49
225 查看
The Luckiest number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1163 Accepted Submission(s): 363
[align=left]Problem Description[/align]
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'.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
8
11
16
0
[align=left]Sample Output[/align]
Case 1: 1
Case 2: 2
Case 3: 0
思路:欧拉函数;
其实这题和hdu3307,基本一样,只不过这个推下。
设f
,表示n位全是8的数,那么f
=10*f[n-1]+8,那么构造等比数列f
+(8/9)=10*(f[n-1]+(8/9));
那么f
= (8+8/9)*(10)^(n-1)-8/9;f
= (8/9)*((10)^n-1);那么就是要求最小的n使f
%L=0;
那么(8/9)*(10^n-1)=k*L;
8/gcd(8,L)*(10^n-1)=9*k*L/(gcd(8,L));
化简为8/gcd(8,L)*(10^n)%(9*L/(gcd(8,L)))=8/gcd(8,L);
8/gcd(8,L)与(9*L/(gcd(8,L))互质可以消去,的10^n%(9*L/(gcd(8,L)))=1;
那么用另模数为m,10^n%(m)=1;
m和10必定互质,否则无解。
于是根据欧拉定理,10^(Euler(m)) = 1(mod m) 。由于题目要求最小的解,解必然是Euler(m)的因子。
需要注意的是,对于10^x,由于m太大,直接快速幂相乘的时候会超long long
这题我开始用baby-step,超时了;
#include<stdio.h> #include<algorithm> #include<iostream> #include<string.h> #include<queue> #include<set> #include<math.h> #include<map> using namespace std; typedef long long LL; pair<LL,LL>exgcd(LL n,LL m); LL gcd(LL n,LL m); LL quick(LL n,LL m,LL mod); LL mul(LL n, LL m,LL p); int slove(LL n); LL phi(LL n); bool prime[1000005]; int ans[1000005]; LL fen[1000005]; int main(void) { LL n; int i,j; int cn = 0; for(i = 2; i <= 1000; i++) { if(!prime[i]) { for(j = i; (i*j) <= 1000000; j++) { prime[i*j] = true; } } } for(i = 2; i <= 1000000; i++) { if(!prime[i]) { ans[cn++] = i; } } //printf("%d\n",cn); int __ca = 0; while(scanf("%lld",&n),n!=0) { LL gc = gcd(8,n); n = 9*n/gc; LL oula = phi(n); LL x = gcd(n,10);//printf("%lld\n",n); //printf("%lld\n",x); printf("Case %d: ",++__ca); if(x!=1) { printf("0\n"); } else { int k = slove(oula); //printf("%d\n",k); for(i = 0;i < k;i++) { LL akk =quick(10,fen[i],n); if(akk==1) { break; } }//printf("%d\n",10); printf("%lld\n",fen[i]); } } return 0; } int slove(LL n) { int cn = 0;int i,j; for(i = 1;i < sqrt(1.0*n);i++) { if(n%i==0) { if(n/i==i) { fen[cn++] = i; } else { fen[cn++] = i; fen[cn++] = n/i; } } } sort(fen,fen+cn); return cn; } LL phi(LL n) { int f = 0; bool flag = false; LL ask =n; while(n>1) { while(n%ans[f]==0) { if(!flag) { flag = true; ask/=ans[f]; ask*=ans[f]-1; } n/=ans[f]; } f++; flag = false; if((LL)ans[f]*(LL)ans[f]>n) { break; } } if(n > 1) { ask/=n; ask*=(n-1); } return ask; } pair<LL,LL>exgcd(LL n,LL m) { if(m==0) return make_pair(1,0); else { pair<LL,LL>ak = exgcd(m,n%m); return make_pair(ak.second,ak.first-(n/m)*ak.second); } } LL gcd(LL n,LL m) { if(m==0) return n; else return gcd(m,n%m); } LL quick(LL n,LL m,LL mod) { LL ak = 1; n %= mod; while(m) { if(m&1) ak =mul(ak,n,mod); n = mul(n,n,mod); m>>=1; } return ak; } LL mul(LL n, LL m,LL p) { n%=p; m%=p; LL ret=0; while(m) { if(m&1) { ret=ret+n; ret%=p; } m>>=1; n<<=1; n%=p; } return ret; }
相关文章推荐
- 【hdu2462】【数论】【欧拉函数+欧拉定理+大数快速幂】The Luckiest number
- [HDU2462] The Luckiest number [2008 Asia Hefei Regional Contest Online G]
- HDU 2462 The Luckiest number
- hdu 2462 The Luckiest number
- POJ 3696 The Luckiest Number 欧拉函数
- poj3696 The Luckiest number By Assassin
- poj3696 The Luckiest number
- POJ3696 The Luckiest number
- HDU 2462 The Luckiest number
- poj 3696 The Luckiest number
- poj 3696 The Luckiest number (数论-快速幂+欧拉定理)
- USTCOJ 1106 The Luckiest number,2008 Asia Regional, Hefei, Preliminary
- POJ 3696 The Luckiest number 欧拉定理+快速幂+GCD *
- The Luckiest number(hdu 2462)
- POJ 3696 The Luckiest number(08合肥 数论)
- POJ The Luckiest number 3696 数论(处理快速幂过程中超int64的情况)
- hdu 2462 The Luckiest number
- POJ 3696 The Luckiest number
- poj 3696 The Luckiest number(欧拉函数)
- POJ 3696 The Luckiest number