Description
给一个数n,问至少要用多少个8才能组成n的倍数,无解输出0
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
题解
我的数论是真的烂啊
容易推出式子
8∗(10x+10x−1+...+100)≡0(modn)8∗(10x+10x−1+...+100)≡0(modn)
中间是个等比数列可以写成
8∗(10x−1)/9≡0(modn)8∗(10x−1)/9≡0(modn)
改写成不定方程的形式可以推出
8∗(10x−1)/9=n∗k8∗(10x−1)/9=n∗k
10x−1=n∗k∗9/810x−1=n∗k∗9/8
观察到左边一定是整数,则右边也同为整数
对于n,他能贡献的因子是gcd(n,8)gcd(n,8),则k需要贡献8/gcd(n,8)8/gcd(n,8)这些因子
原式可写为
10x−1=n∗9∗q/gcd(n,8)10x−1=n∗9∗q/gcd(n,8)
设m=9∗n/gcd(n,8)m=9∗n/gcd(n,8),仍可改写为10x−1=q∗m10x−1=q∗m,移项可得10x=1+q∗m10x=1+q∗m,这是个同余方程
10x≡1(modm)10x≡1(modm)
喜欢可以直接BSGS,或者有欧拉定理这个x一定为ϕnϕn的因数
证明不再概述
注意一下爆long long的问题
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long LL;
int tt;
LL L;
LL gcd(LL a,LL b){return a==0?b:gcd(b%a,a);}
inline LL mul(LL a,LL b,LL mod)
{
LL ret=0;
while(b)
{
if(b&1)ret=(ret+a)%mod;
a=(a+a)%mod;b>>=1;
}
return ret;
}
inline LL pow_mod(LL a,LL b,LL mod)
{
LL ret=1;
while(b)
{
if(b&1)ret=mul(ret,a,mod)%mod;
a=mul(a,a,mod)%mod;b>>=1;
}
return ret;
}
int main()
{
while(scanf("%lld",&L)!=EOF)
{
if(!L)break;
LL m=L/gcd(L,8)*9;
tt++;
if(gcd(10,m)!=1){printf("Case %d: 0\n",tt);continue;}
LL mpos=LL(sqrt(double(m+1)));
LL phi=m;
for(int i=2;i<=mpos;i++)
if(m%i==0)
{
while(m%i==0)m/=i;
phi=phi/i*(i-1);
}
if(m!=1)phi=phi/m*(m-1);
m=L/gcd(L,8)*9;
LL ans=(1LL<<63-1);mpos=LL(sqrt(double(phi+1)));
for(LL i=1;i<=mpos;i++)
if(phi%i==0)
{
if(pow_mod(10,i,m)==1)ans=min(ans,i);
if(i!=phi/i)
if(pow_mod(10,phi/i,m)==1)ans=min(ans,phi/i);
}
printf("Case %d: %lld\n",tt,ans);
}
return 0;
}