Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4018 | Accepted: 1059 |
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
//396K 47MS
#include<stdio.h>
#include<string.h>
long long l,s[57][2],len;
long long gcd(long long a,long long b)
{
return b==0?a:gcd(b,a%b);
}
long long eular(long long n)//求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;//返回n的欧拉函数
}
long long get(long long n)
{
len=0;
for(long long i=2;i*i<=n;i++)
{
if(n%i==0)
{
s[len][0]=i;s[len][1]=0;
//printf("ssssss%d\n",s[len][1]);
do{n/=i;s[len][1]++;}while(n%i==0);
len++;
}
}
if(n>1){s[len][0]=n;s[len++][1]=1;}
}
long long multi(long long a,long long b,long long m)//a*b%m
{
long long ret=0;
while(b>0)
{
if(b&1)ret=(ret+a)%m;
b>>=1;
a=(a<<1)%m;
}
return ret;
}
long long quick_mod(long long a,long long b,long long m)//a^b%m
{
long long ans=1;
a%=m;
while(b)
{
if(b&1)
{
ans=multi(ans,a,m);
b--;
}
b/=2;
a=multi(a,a,m);
}
return ans;
}
int main()
{
int cas=1;
while(scanf("%lld",&l),l)
{
printf("Case %d: ",cas++);
long long m=9*l/gcd(8,l);
if(gcd(10,m)!=1){printf("0\n");continue;}
memset(s,0,sizeof(s));
long long phi_sum=eular(m);
get(phi_sum);
for(int i=0;i<len;i++)
for(int j=1;j<=s[i][1];j++)
if(quick_mod(10,phi_sum/s[i][0],m)==1)phi_sum/=s[i][0];
printf("%lld\n",phi_sum);
}
return 0;
}