POJ3696 The Luckiest number

The Luckiest number
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5455   Accepted: 1448

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位8可以表示为(10^n - 1)/9 * 8,欲求满足(10^n - 1)/9*8 = kL的n
把式子化一下
(10^n - 1)*8 = kL*9
我们发现8与9互质,如果让10^n - 1与右边除k以外的部分都互质就最好了,
这样能够得出10^n-1 | 右边除k以外的部分。于是我们设
g = gcd(8,L)
两边同除g得
(10^n - 1)*8/g = k*9L/g
不难发现8/g与9*L/g互质
于是有10^n - 1 | 9*L/g
不难得到10^n  ≡ 1 (mod 9*L/g)
欧拉定理得10^phi(9*L/g) ≡ 1 (mod 9*L/g)
算出phi,因式分解即可,从小到大枚举因子
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #define max(a, b) ((a) > (b) ? (a) : (b))
 7 #define min(a, b) ((a) < (b) ? (a) : (b))
 8 
 9 inline void read(long long &x)
10 {
11     x = 0;char ch = getchar(), c = ch;
12     while(ch < '0' || ch > '9')c = ch, ch = getchar();
13     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
14     if(c == '-')x = -x;
15 }
16 
17 const long long MAXN = 15 + 5;
18 const long long INF = 0x3f3f3f3f3f3f3f3f;
19 
20 long long L, T, oula, m, tmp, g, ans, yinzi[200000], tot;
21  
22 long long gcd(long long a, long long b)
23 {
24     return !b ? a : gcd(b, a%b);
25 }
26 
27 long long mul(long long a, long long b, long long k)
28 {
29     long long r = 0,base = a;
30     for(;b;b >>= 1)
31     {
32         if(b & 1)r += base, r %= k;
33         base *= 2, base %= k;
34     }
35     return r;
36 }
37 
38 long long pow(long long a, long long b, long long k)
39 {
40     long long r = 1, base = a % k;
41     for(;b;b >>= 1)
42     {
43         if(b&1)r = mul(r,base,k);
44         base = mul(base,base,k);
45     }
46     return r;
47 } 
48 
49 int main()
50 {
51     read(L);
52     while(L)
53     {
54         ++ T,g = gcd(8, L),oula = 9*(L/g),tmp = 9*(L/g),ans = INF,tot = 0;
55         m = sqrt(9 * (L/g));
56         for(long long i = 2;i <= m;++ i)
57         {
58             if(tmp % i == 0)
59             {
60                 while(tmp % i == 0)tmp /= i;
61                 oula = oula - oula / i;
62             }
63         }
64         if(tmp > 1)oula = oula - oula / tmp;
65         
66         m = 9 * (L/g),L = sqrt(oula); 
67         for(tmp = 1;tmp <= L;++ tmp)
68         {
69             if(oula % tmp) continue;
70             if(pow(10,tmp,m) == 1)
71             {
72                 ans = tmp;
73                 break;
74             }
75         }
76         for(tmp = L;tmp >= 1;-- tmp)
77         {
78             if(oula % tmp) continue;
79             g = oula/tmp;
80             if(pow(10,g,m) == 1)
81             {
82                 ans = g;
83                 break;
84             }
85         }
86         if(ans == INF)ans = 0;
87         printf("Case %lld: %lld\n", T, ans);
88         read(L);
89     }
90     return 0;
91 }
POJ3696

 

posted @ 2017-09-12 15:38  嘒彼小星  阅读(209)  评论(0编辑  收藏  举报