結果

問題 No.2785 四乗足す四の末尾の0
ユーザー Tatsu_mr
提出日時 2024-06-14 22:35:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,166 bytes
コンパイル時間 2,251 ms
コンパイル使用メモリ 198,480 KB
最終ジャッジ日時 2025-02-21 22:13:17
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <class T>
T modpow(T a, T b, T mod) {
    T cur = a % mod, res = 1 % mod;
    while (b) {
        if (b & 1) {
            res = (res * cur) % mod;
        }
        cur = (cur * cur) % mod;
        b >>= 1;
    }
    return res;
}

bool MillerRabin(long long n) {
    if (n <= 1) {
        return false;
    }
    if (n == 2 || n == 7 || n == 61) {
        return true;
    }
    if (n % 2 == 0) {
        return false;
    }
    
    vector<long long> A;
    if (n < 4759123141) {
        A = {2, 7, 61};
    } else {
        A = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    }
    long long s = 0, d = n - 1;
    while (d % 2 == 0) {
        s++;
        d >>= 1;
    }
    for (auto a : A) {
        if (a % n == 0) {
            return true;
        }
        long long x = modpow<__int128_t>(a, d, n);
        if (x == 1) {
            continue;
        }
        bool ok = false;
        for (int i = 0; i < s; i++) {
            if (x == n - 1) {
                ok = true;
                break;
            }
            x = (__int128_t)x * x % n;
        }
        if (!ok) {
            return false;
        }
    }
    return true;
}

long long gcd(long long x, long long y) {
    if (y == 0) {
        return x;
    }
    return gcd(y, x % y);
}

unsigned int xorshift() {
    static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned int t = (x ^ (x << 11));
    x = y;
    y = z;
    z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

long long Pollard(long long n) {
    if (n % 2 == 0) {
        return 2LL;
    }
    if (MillerRabin(n)) {
        return n;
    }
    
    long long i = 0;
    while (true) {
        i++;
        long long r = xorshift();
        auto f = [&](long long x) {
            return (__int128_t(x) * x + r) % n;
        };
        long long x = i, y = f(x);
        while (true) {
            long long p = gcd(abs(y - x + n), n);
            if (p == 0 || p == n) {
                break;
            }
            if (p != 1) {
                return p;
            }
            x = f(x);
            y = f(f(y));
        }
    }
}

pair<long long, long long> prime_factorize(long long n) {
    if (n == 1) {
        return {0, 0};
    }
    long long p = Pollard(n);
    if (p == n) {
        if (p == 2) {
            return {1, 0};
        } else if (p == 5) {
            return {0, 1};
        } else {
            return {0, 0};
        }
    }
    auto [twoL, fiveL] = prime_factorize(p);
    auto [twoR, fiveR] = prime_factorize(n / p);
    return {twoL + twoR, fiveL + fiveR};
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        long long n;
        cin >> n;
        long long x = n * n + 2LL * n + 2LL, y = n * n - 2LL * n + 2LL;
        if ((x == 1LL && MillerRabin(y) == true) || (MillerRabin(x) == true && y == 1LL)) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
        auto [twoX, fiveX] = prime_factorize(x);
        auto [twoY, fiveY] = prime_factorize(y);
        cout << min(twoX + twoY, fiveX + fiveY) << endl;
    }
}
0