Files
python/TangDou/AcWing_TiGao/T5/TongYu/202_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

68 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
typedef unsigned __int128 INT128;
// 最大公约数
int gcd(int a, int b) {
if (b == 0) return a;
return b ? a : gcd(b, a % b);
}
// 快速幂+ INT128
int qmi(int a, int b, int mod) {
int res = 1;
while (b) {
if (b & 1) res = (INT128)res * a % mod;
a = (INT128)a * a % mod;
b >>= 1;
}
return res;
}
/**
* 功能:求单个数字的欧拉函数值
* @param x
* @return
*/
int phi(int x) {
int res = x;
for (int i = 2; i <= x / i; i++)
if (x % i == 0) {
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
return res;
}
signed main() {
int T = 1; // 准备输出Case i
int L;
while (cin >> L, L) {
int d = gcd(L, 8); // L和8的最大公约数 gcd(8,L)
int mod = 9 * L / d; // 公式推导得到的mod
int p = phi(mod); // 单个数字mod的欧拉函数值φ(mod)
int res = LONG_LONG_MAX; // 预求最小先设最大
if (gcd(mod, 10) > 1) // 判断p和10是否互质,不互质:方程无解,输出0
res = 0;
else {
for (int d = 1; d * d <= p; d++) // 枚举φ(p)的每个小约数,到sqrt(φ(p))
if (p % d == 0) { // 如果d是约数,那么其实我们一次发现了两个约数: d 和 phi/d
// 这里与原来的质因子分解有一点点不同,因为那个只要枚举小于sqrt(n)的,并且 n %d==0,
// 就一定是它的小质数因子,本题不是这个意思。
// 本题的目标是找出所有因子,注意,不是小因子。
// 因为,小因子不见得能是方程的解,大因子不见得不是方程的解!!
// 对于它们,都有机会成为答案,需要全部讨论到!然后PK最小值即可!
// 如果这个约数d,满足 10 ^ d ≡ 1 (mod p) ,那么它有机会成为答案
if (qmi(10, d, mod) == 1) res = min(res, d); // 小约数
// 如果这个约数phi/d,满足 10 ^ (phi/d) ≡ 1 (mod p) ,那么它有机会成为答案
if (qmi(10, p / d, mod) == 1) res = min(res, p / d); // 大约数
}
}
// 输出
printf("Case %d: %lld\n", T++, res);
}
}