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 |
0.回顾
欧拉函数:φ(n)= n(1 – 1/p1)(1 – 1/p2)…(1 – 1/pm)。
欧拉定理:若(a,p) =1,则a^φ(p)≡ 1 (mod p)。
不大于n(n>=2)且与n互质的所有正整数的和为n*φ(n)/2。
1.阶
满足a^x≡ 1 (mod p)的最小的x称为a关于p的阶,记为δp(a),简写为δ(a)。
性质1:a^x≡ 1 (mod p) ó δp(a) | x。
性质2:δ(a^k)= δ(a) / (δ(a), k)
性质3:(δ(a), δ(b))= 1 ó δ(ab) = δ(a)δ(b)。
【阶的求法】
由于a^φ(p) ≡ 1 (mod p),所以阶一定是φ(p)的约数。
求出φ(p)以及它的所有约数,放到一个数组中排序,从小到大用快速幂依次验证,第一个满足的就是答案。
时间复杂度上界为O(sqrt(p)log(p)),一般情况下远远达不到。
言归正传:
进入列方程Mode:
8/9*(10^m-1)=k*L
8*(10^m-1)=9kL
8*(10^m-1)/gcd(8,9L)=9kL/gcd(8,9L)
8/gcd(8,9L)*(10^m-1)=9L/gcd(8,9L) *k
由定义(a/gcd(a,b),b/gcd(a,b))=1:(8/gcd(8,9L),9L/gcd(8,9L))=1
10^m-1|9L/gcd(8,9L)
10^m ≡1 (mod 9L/gcd(8,9L))
(8,9)=1 ->gcd(8,9L)=gcd(8,L)
->10^m ≡1 (mod 9L/gcd(8,L))
值得称道的是MulMod 写法:
拆成2进制,a*b=a*2^c1+a*2^c2+a*2^c3+...+a82^cn
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMI(a) memset(a,127,sizeof(a))
#define MEMi(a) memset(a,128,sizeof(a))
#define INF (2139062143)
#define MAXL (2000000000)
typedef long long LL;
LL L;
LL F;
LL gcd(LL a,LL b){if (!b) return a;return gcd(b,a%b);}
LL sqrt(LL x){return (LL)sqrt((long double)x);}
LL st[1000000+10];
int size=0;
LL phi(LL x)
{
if (x==1) return 1;
LL ans=1;
Fork(i,2,sqrt(x))
{
if (x%i==0) ans*=i-1,x/=i;
while (x%i==0) ans*=i,x/=i;
}
if (x-1) ans*=x-1;
return ans;
}
LL mul(LL a,LL b)
{
LL ans=0;
while (b)
{
if (b&1) ans=(ans+a)%F;
a=(a<<1)%F;
b>>=1;
}
return ans;
}
LL pow2(LL a,LL b)
{
if (b==0) return 1;
if (b==1) return a%F;
LL c=pow2(a,b/2)%F;
c=mul(c,c)%F;
if (b%2) c=mul(c,a)%F;
return c;
}
int main()
{
// freopen("poj3696.in","r",stdin);
// freopen(".out","w",stdout);
int T=1;
while (scanf("%lld",&L)!=EOF&&L)
{
F=9*L/gcd(8,9*L);
LL m=phi(F);
//if (gcd(10,F)!=1) {printf("Case %d: 0\n",T++);continue;}
size=0;
For(i,sqrt(m))
if (m%i==0)
{
st[++size]=i;
if (i*i<m) st[++size]=m/(LL)i;
}
sort(st+1,st+1+size);
bool bo=0;
For(i,size)
{
if (pow2(10,st[i])%F==1)
{
printf("Case %d: %lld\n",T++,st[i]);
bo=1;break;
}
}
if (!bo) printf("Case %d: 0\n",T++);
}
//printf("Case %d: 0\n",T++);
return 0;
}