Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4894 | Accepted: 1318 |
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <math.h>
#include <map>
#include <stdlib.h>
#include <algorithm>
#define eps 1e-5
#define inf 0x3f3f3f3f
#define Linf 0x3f3f3f3f3f3f3f3f
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
#define lc rt<<1
#define rc rt<<1 | 1
#define getx2(a) ((a)*(a))
#define Pi acos(-1.0)
typedef long long LL;
using namespace std;
#define MM 76543
LL gcd(LL a,LL b)
{
if(a<b)swap(a,b);
while(b)
{
LL t=b;
b=a%b;
a=t;
}
return a;
}
LL getEuler(LL n)
{
LL res=n;
for(int i=2; i*i<n; i++)
{
if(n%i==0)
{
res=res/i*(i-1);
while(n%i==0)n/=i;
}
}
if(n>1)res=res/n*(n-1);
return res;
}
LL MUL_mod(LL a,LL b,LL m)
{
LL res=0;
a=a%m;
while(b)
{
if(b&1)
res=(res+a)%m;
b>>=1;
a=(a+a)%m;
}
return (res+m)%m;
}
LL pow_mod(LL a,LL n,LL m)
{
LL res=1;
a=a%m;
while(n)
{
if(n&1)
res=MUL_mod(res,a,m);
n>>=1;
a=MUL_mod(a,a,m);
}
return (res+m)%m;
}
int main()
{
LL l;
LL cnt=0;
while(~scanf("%I64d",&l)&&l)
{
LL d;
d=gcd(8,l);
LL q=l/d*9;
LL p=8/d;
LL dd=gcd(q,10);
printf("Case %d: ",++cnt);
if(dd!=1)
printf("0\n");
else
{
LL EulerQ=getEuler(q);
LL ans=1;
for(LL i=1; i*i<=EulerQ; i++)
{
if(EulerQ%i==0)
{
LL pp=pow_mod(10,i,q);
if(pp==1)
{
ans=i;
break;
}
else
{
pp=pow_mod(10,EulerQ/i,q);
if(pp==1)
{
ans=EulerQ/i;
}
}
///cout<<EulerQ<<" "<<ans<<" "<<i<<endl;
}
}
printf("%I64d\n",ans);
}
}
return 0;
}