Language:
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 8 11 16 0 Sample Output Case 1: 1 Case 2: 2 Case 3: 0 Source |
题意:给出一个数N 求一个数M的长度L且M是N的倍数求只有8组成
参考博客:http://blog.csdn.net/huanghongxun/article/details/50966715
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 100010;
long long num[2][60];
long long gcd(long long a,long long b){
return b==0?a:gcd(b,a%b);
}
long long Euler(long long n){
long long ret=1,i;
for(i=2;i*i<=n;i++){
if(n%i==0){
n/=i;ret*=i-1;
while(n%i==0){
n/=i;ret*=i;
}
}
}
if(n>1)ret*=n-1;
return ret;
}
int get_factor(long long n){
int cnt=0;
for(long long i=2;i*i<=n;++i){
if(n%i==0){
num[0][cnt]=i;num[1][cnt]=0;
while(n%i==0){
num[1][cnt]++;n/=i;
}
cnt++;
}
}
if(n>1)num[0][cnt]=n;num[1][cnt++]=1;
return cnt;
}
long long multi(long long a,long long b,long long c) {
long long ans = 0;
while (b) {
if (b & 1) {
ans += a;ans %= c;
}
a += a;a %= c;
b >>= 1;
}
return ans;
}
long long Pow(long long a, long long b, long long c) {
long long ans = 1;
while (b) {
if (b & 1) {
ans = multi(ans, a, c);
}
a = multi(a, a, c);
b >>= 1;
}
return ans;
}
int main()
{
int test=1;
long long n;
while(scanf("%lld",&n),n){
printf("Case %d: ",test++);
long long q=9*n/gcd(n,8);
if(gcd(10,q)!=1){
printf("%d\n",0);
continue;
}
long long phi_q=Euler(q);
int cnt=get_factor(phi_q);
for(int i=0;i<cnt;++i){
for(int j=1;j<=num[1][i];++j){
if(Pow(10,phi_q/num[0][i],q)==1)phi_q/=num[0][i];
}
}
printf("%lld\n",phi_q);
}
return 0;
}