The Luckiest number POJ - 3696
题目描述
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
题目大意
给出一个数L,问L整除的仅由8组成的数最少有多少位
解题思路
对题意略作转化就可以变成
当满足
L
∣
8
∗
(
1
0
n
−
1
)
/
9
L\mid 8*(10^n-1)/9
L∣8∗(10n−1)/9时求
m
i
n
(
n
)
min(n)
min(n)
对原式稍作转化就可以转变成 9 L g c d ( n , 8 ) ∣ 8 g c d ( 8 , n ) ( 1 0 n − 1 ) \frac{9L}{gcd(n,8)}\mid \frac{8}{gcd(8,n)}(10^n-1) gcd(n,8)9L∣gcd(8,n)8(10n−1)显然可得 9 L g c d ( n , 8 ) ̸ ∣ 8 g c d ( 8 , n ) \frac{9L}{gcd(n,8)}\not\mid \frac{8}{gcd(8,n)} gcd(n,8)9L̸∣gcd(8,n)8故有
1 0 n ≡ 1 ( m o d 9 L g c d ( n , 8 ) ) 10^n\equiv 1(mod\ \frac{9L}{gcd(n,8)}) 10n≡1(mod gcd(n,8)9L)由此当10与gcd(n,8)不互质时则无解,当有解时,其至大不会超过 ϕ ( 9 L g c d ( n , 8 ) ) \phi(\frac{9L}{gcd(n,8)}) ϕ(gcd(n,8)9L)因而再去遍历n为欧拉函数的因数,若有满足式子的因数则为答案
AC代码
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long LL;
LL mul(LL a, LL b, LL mod){
LL rt=0;
while(b){
if(b&1) rt=(rt+a)%mod;
a=(a<<1)%mod;
b>>=1;
}
return rt;
}
LL euler(LL n)
{
LL ans=n;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
ans-=ans/i;
do
n/=i;
while(n%i==0);
}
}
if(n>1) ans-=ans/n;
return ans;
}
LL quick_pow(LL a,LL b,LL p)
{
LL ans=1;a%=p;
while(b)
{
if(b&1) ans=mul(ans,a,p);
b>>=1;
a=mul(a,a,p);
}
return ans;
}
inline LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
inline bool check(int n,int p)
{
return quick_pow(10,n,p)==1;
}
int main()
{
int kace = 1;
LL n;
while(~scanf("%lld",&n)&&n)
{
LL m=9*n/gcd(n,8);
if(gcd(m,10)>1) printf("Case %d: 0\n",kace++);
else
{
LL phi=euler(m);
LL ans=phi;
bool flag=0;
LL q=sqrt(phi);
for(int i=1;i<=q;i++)
{
if(phi%i==0&&quick_pow(10,i,m)==1){
ans=i;flag=1;
break;
}
}
if(!flag)
for(int i=q;i>=2;i--)
{
if(phi%i==0&&quick_pow(10,phi/i,m)==1){
ans=phi/i;
break;
}
}
printf("Case %d: %I64d\n",kace++,ans);
}
}
}