Loading

POJ 3696 The Luckiest number 数论,GCD

发此随笔以警示自己不要乱实现。

 

 

 

 

枚举因子的时候多加留心,实现能力还是太差了。

ll get_phi(ll n) {
    ll m = ll(sqrt(n + 0.5));
    ll ans = n;
    for (int i = 2; i <= m; i++) {
        if (n % i == 0) {
            ans = ans / i * (i - 1);
            while (n % i == 0) n /= i;
        }
    }
    if (n > 1) ans = ans / n * (n - 1);
    return ans;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}


int main() {
    ll n;
    int kase = 1;
    while (n = readll()) {
        if (!n) break;
        printf("Case %d: ", kase++);
        n = 9 * n / gcd(n, 8ll);
        ll res = inf;
        ll phi = get_phi(n);
        for (ll i = 1; i * i <= phi; i++) {
            if (phi % i) continue;
            if (phi % i == 0) if (quickPower(10ll, i, n) == 1ll) res = min(res, i);
            if (i * i != phi) if (quickPower(10ll, phi / i, n) == 1ll) res = min(res, phi / i);
        }
        if (res == inf) puts("0");
        else printf("%d\n", res);
    }
}

 

posted @ 2020-07-31 22:16  MQFLLY  阅读(68)  评论(2编辑  收藏  举报