結果

問題 No.3028 No.9999
ユーザー Today03
提出日時 2025-02-22 18:34:53
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 4,000 ms
コード長 1,415 bytes
コンパイル時間 3,736 ms
コンパイル使用メモリ 283,740 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2025-02-22 18:34:59
合計ジャッジ時間 4,750 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 10;
const ll INFL = 4e18;

// オイラーの totient 関数
// totient(n) : n 以下で n と互いに素な自然数の個数を返す。
// O(sqrt(N))
ll totient(ll n) {
    ll ret = n;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            ret -= ret / i;
            while (n % i == 0) n /= i;
        }
    }
    if (n > 1) ret -= ret / n;
    return ret;
}

// modPow(x, n, m) : x^n (mod m) を返す
// O(log(m))
template <typename T = ll>
T modPow(T x, T n, T mod) {
    ll ret = 1;
    if (typeid(T) == typeid(ll) && mod > INF * 2) return modPow<__int128_t>(x, n, mod);
    while (n > 0) {
        if (n & 1) ret = ret * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return ret;
}

// modInv(x, m) : x*y = 1 (mod m) なる y を返す
// O(log(m))
// ただし、m は素数
ll modInv(ll x, ll mod) { return modPow(x, mod - 2, mod); }

int main() {
    int N;
    cin >> N;

    if (N == 1) {
        cout << 1 << endl;
        return 0;
    }

    int P = totient(N);

    vector<int> div;
    for (int i = 1; i * i <= P; i++) {
        if (P % i == 0) div.push_back(i), div.push_back(P / i);
    }
    ranges::sort(div);

    for (int p : div) {
        if (modPow<ll>(10, p, N) == 1) {
            cout << p << endl;
            return 0;
        }
    }

    assert(false);
}
0