最幸运的数字(欧拉定理,欧拉函数)
作者:互联网
Code:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int N = 1e5 + 10, M = 1e5;
int c;
int qmul(int a,int p,int mod){
int res=0;
while(p){
if(p&1)res=(res+a)%mod;
a=(a+a)%mod;
p>>=1;
}
return res;
}//慢速乘,用于解决快速幂中的两个1e10相乘爆ll问题
int gcd(int a, int b) {
return b ? gcd(b, a%b) : a;
}
int qmi(int a, int p, int mod) {
int res = 1;
while (p) {
if (p & 1)res = qmul(res,a,mod)%mod;
a = qmul(a,a,mod)%mod;
p >>= 1;
}
return res;
}
bool check(int x) {
if (qmi(10, x, c) == 1)return 1;
return 0;
}
signed main()
{
int L;
int id = 0;
while (cin >> L, L) {
printf("Case %lld: ", ++id);
c = 9 * L / gcd(L, 8);
if (gcd(c, 10) != 1)cout << 0 << endl;
else {
int phi = c;
int tc = c;
for (int i = 2; i <= M; i++) {
int cnt = 0;
while (tc%i == 0) {
cnt++;
tc /= i;
}
if(cnt)phi = phi * (i - 1) / i;
}//求欧拉函数
if(tc!=1)phi = phi /tc *(tc-1);//先除后乘,不然又爆ll了...
int res = phi;
for (int i = 1; i*i<=phi; i++) {
if (phi%i == 0 ){
if(check(i))res=min(res,i);
if(check(phi/i))res=min(res,phi/i); //求约数
}
}
cout<<res<<endl;
}
}
}
有诸多的坑
标签:return,gcd,int,res,定理,qmul,幸运,欧拉,mod 来源: https://www.cnblogs.com/codjjj/p/15868924.html