POJ3696 The Luckiest Number

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

题解:这种题目一般都是 转化为: num*(10^x-1)/9= L*k

8*(10^x-1)=9L*k

设 d=gcd(9L,8)=gcd(8,L)

p=8/d,q=9L/d;

则: p*(10^x-1) q*k;

因为q,p互质,则q|(10^x-1) ,p|k

则     10^x-1=0(mod q)

10^x =1(mod q)

10^x=1(mod 9L/d)

当q与10互质时10^(oula(q))=1(mod  q)

因此,字需要枚举其因子即可;
参考代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 #define clr(a,val) memset(a,val,sizeof(a))
 7 typedef long long ll;
 8 const int INF=0x3f3f3f3f;
 9 ll L,fac[1010]={0};
10 inline ll phi(ll x)
11 {
12     ll p=x,s=x;
13     for(ll i=2;i*i<=s;++i)
14         if(!(x%i))
15         {
16             p=p/i*(i-1);
17             while(!(x%i)) x/=i;
18         }
19     if(x>1) p=p/x*(x-1);
20     return p;
21 }
22
23 inline void find_factor(ll x)
24 {
25     ll s=x;
26     fac[0]=0;
27     for(ll i=2;i*i<=s;++i)
28         if(!(x%i))
29         {
30             fac[++fac[0]]=i;
31             while(!(x%i)) x/=i;
32         }
33     if(x>1) fac[++fac[0]]=x;
34 }
35
36 inline ll mult(ll a,ll b,ll mod)
37 {
38     a%=mod; b%=mod;
39     ll s=a,sum=0;
40     while(b)
41     {
42         if(b&1)
43         {
44             sum+=s;
45             if(sum>=mod) sum-=mod;
46         }
47         b>>=1;s<<=1;
48         if(s>=mod) s-=mod;
49     }
50     return sum;
51 }
52 ll power(ll a,ll b,ll mod)
53 {
54     ll s=a,sum=1;
55     while(b)
56     {
57         if(b&1) sum=mult(sum,s,mod);
58         b>>=1;s=mult(s,s,mod);
59     }
60     return sum;
61 }
62 ll gcd(ll a,ll b) {return b==0? a:gcd(b,a%b);}
63 int main()
64 {
65     int t=0;
66     while(~scanf("%lld",&L) && L)
67     {
68         ++t;
69         ll m=L/gcd(L,8)*9,p=phi(m),x=p;
70         if(gcd(m,10)!=1) {printf("Case %d: 0\n",t);continue;}
71         find_factor(p);
72         for(int  i=1;i<=fac[0];++i)
73         {
74             while(1)
75             {
76                 x/=fac[i];
77                 if(power(10,x,m)!=1)
78                 {
79                     x*=fac[i];
80                     break;
81                 }
82                 else if(x%fac[i]) break;
83             }
84         }
85         printf("Case %d: %lld\n",t,x);
86     }
87     return 0;
88 } 

原文地址:https://www.cnblogs.com/songorz/p/10388823.html

时间: 2024-08-01 04:54:12

POJ3696 The Luckiest Number的相关文章

[POJ3696]The Luckiest number(数论)

题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们假设N是x个8组成的 那么88888...888=kL 提个8出来:8*111..1111=kL ① 因为题目只要求x的值,所以要弄出关于x的方程 11...111可以写成(10^k-1)/9 于是①变成了8(10^x-1)=9kL ② 再来回顾下题目,②式中x和k是变量,且都属于正整数,要根据②式

poj-3696 The Luckiest number

题意: 给出一个数L,求一个最小的x,使长度为x的888...8这个数整除L: 无解输出0,L<=2*10^9: 题解: 即求满足下式的最小x值: 8/9*(10^x-1)==k*L         (k为正整数) 8*(10^x-1)==k*9*L 为继续化简,求出r=gcd(L,8): 8/r *(10^x-1)==k*9*L/r 因为8/r与9*L/r互质,9*L这个因式必在(10^x-1)中,所以原式即为: 10^x-1≡0(mod 9*L/r) 10^x≡1(mod 9*L/r) 设q

POJ3696 The Luckiest Number 欧拉定理

昨天终于把欧拉定理的证明看明白了...于是兴冲冲地写了2道题,发现自己啥都不会qwq 题意:给定一个正整数L<=2E+9,求至少多少个8连在一起组成正整数是L的倍数. 这很有意思么... 首先,连续的8可表示为:8*(10^x-1)/9; 那么就是L|8*(10^x-1)*9 => 9*L|8*(10^x-1) ,求最小的x: 我们设d=gcd(L,8) 则9*L/d | 8/d*(10^x-1),因为此时9*L/d 和 8/d 互质,所以9*L/d | 10^x-1,所以 10^x ≡ 1

The Luckiest number(hdu2462)

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 Problem Description Chinese people think of '8' as the lucky digit. Bob also likes digit '

poj_3696_The Luckiest number

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

POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】

一.题目 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 consi

「POJ3696」The Luckiest number【数论,欧拉函数】

# 题解 一道数论欧拉函数和欧拉定理的入门好题. 虽然我提交的时候POJ炸掉了,但是在hdu里面A掉了,应该是一样的吧. 首先我们需要求的这个数一定可以表示成\(\frac{(10^x-1)}{9}\times 8\). 那么可以列出一个下面的方程 \[\frac{(10^x-1)}{9}\times 8=L\times k\] 设\(d=gcd(9L,8)=gcd(L,8)\) \[\frac89(10^x-1)=Lk\] \[\frac{8(10^x-1)}d=\frac{9Lk}{d}\]

poj The Luckiest number

                                     The Luckiest numbe 题目: 在满足|x| + |y|最小时候,让a*|x| + b*|y|最小.求:|x| + |y|最小值. 算法: 同余式的求解运用. 解体步骤: 1.先用gcd判断是否有解.(c%g == 0有解) 2.对式子求最简式.a' = a/g ; b' = b/g: c' = c/g; 3.运用扩展欧几里得求解x,y的值. 4.判断当x,y分别求得最小正整数解时候的大小. 5.确定最后答案

HDU 2462 The Luckiest number

传送门 题目大意: 定义只含有数字8的数为幸运数 给定正整数L,求L的所有倍数中最小的幸运数 算法思路: 设最终答案为x个8,则x满足(10x-1)*8/9≡0 (mod L) 化简:10x≡1 (mod n),其中n=9L/gcd(9L,8) 这是一个离散对数的问题,求解方法如下: 若gcd(10,n)>1,无解 若gcd(10,n)=1,由欧拉定理:10?(n)≡1 (mod n).可以证明,x为?(n)的约数,从小到大枚举约数即可 10l≡1 (mod n) 则成立的最小 l 是? (n)