結果

問題 No.3028 No.9999
ユーザー Zhiyuan Chen
提出日時 2025-02-21 21:59:49
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 4,000 ms
コード長 1,568 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 81,880 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2025-02-21 21:59:51
合計ジャッジ時間 2,142 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘ll euler_phi(ll)’:
main.cpp:41:15: warning: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   41 |     for (auto [p, _] : factors) {
      |               ^

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long ll;

// 快速幂取模
ll mod_pow(ll a, ll b, ll mod) {
    ll res = 1;
    while (b > 0) {
        if (b % 2 == 1) res = (res * a) % mod;
        a = (a * a) % mod;
        b /= 2;
    }
    return res;
}

// 质因数分解
vector<pair<ll, int>> factorize(ll n) {
    vector<pair<ll, int>> factors;
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            int cnt = 0;
            while (n % i == 0) {
                n /= i;
                cnt++;
            }
            factors.emplace_back(i, cnt);
        }
    }
    if (n > 1) factors.emplace_back(n, 1);
    return factors;
}

// 计算欧拉函数
ll euler_phi(ll n) {
    auto factors = factorize(n);
    ll res = n;
    for (auto [p, _] : factors) {
        res = res / p * (p - 1);
    }
    return res;
}

// 生成所有约数
vector<ll> get_divisors(ll n) {
    vector<ll> divisors;
    for (ll i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            divisors.push_back(i);
            if (i != n / i) divisors.push_back(n / i);
        }
    }
    sort(divisors.begin(), divisors.end());
    return divisors;
}

int main() {
    ll N;
    cin >> N;
    
    ll phi = euler_phi(N);
    auto divisors = get_divisors(phi);
    
    for (auto d : divisors) {
        if (mod_pow(10, d, N) == 1) {
            cout << d << endl;
            return 0;
        }
    }
    
    // 如果没找到(理论上不可能)
    cout << phi << endl;
    return 0;
}
0