题解:P10496 The Luckiest Number
思路
首先,我们先把 lucky number 表示出来。
那根据题目条件,也就是
因为
所以另
那我们可以有
另
我们有
根据欧拉定理,我们可以知道
我们只需要先求出
CODE
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
const int N=1e6;
int phi(int x){
int ans=x,t=x;
for(int i=2;i*i<=x;i++){
if(t%i!=0)continue;
ans=ans/i*(i-1);
while(t%i==0)t/=i;
}
if(t>1)ans=ans/t*(t-1);
return ans;
}//求欧拉函数
int qw(int a,int b,int md){
if(b==0)return 1;
int res=qw(a,b/2,n);
res=res*res%md;
if(b%2==1)res=res*a%md;
return res;
}
signed main(){
int t=0;
while(scanf("%lld",&n)){
t++;
if(n==0)return 0;
int d=__gcd(8ll,n),ans=1e18;
n=9*n/d;
if(n%2==0||n%5==0){
printf("Case %lld: 0\n",t);
continue;
}//答案不存在
int x=phi(n);
ans=x;
for(int i=1;i*i<=x;i++){
if(x%i!=0)continue;
if(qw(10,i,n)%n==1)ans=min(ans,i);
if(qw(10,x/i,n)%n==1)ans=min(ans,x/i);//枚举phi(x)的因子
}
if(ans==1e18)ans=0;
printf("Case %lld: %lld\n",t,ans);
}
return 0;
}