結果

問題 No.1339 循環小数
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-08-24 23:06:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 212,496 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 23:06:46
合計ジャッジ時間 4,691 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 35 ms
4,376 KB
testcase_22 AC 38 ms
4,380 KB
testcase_23 AC 36 ms
4,380 KB
testcase_24 AC 34 ms
4,384 KB
testcase_25 AC 37 ms
4,380 KB
testcase_26 AC 36 ms
4,376 KB
testcase_27 AC 37 ms
4,376 KB
testcase_28 AC 37 ms
4,376 KB
testcase_29 AC 33 ms
4,376 KB
testcase_30 AC 34 ms
4,380 KB
testcase_31 AC 54 ms
4,380 KB
testcase_32 AC 55 ms
4,380 KB
testcase_33 AC 40 ms
4,376 KB
testcase_34 AC 19 ms
4,380 KB
testcase_35 AC 49 ms
4,376 KB
testcase_36 AC 37 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

vector<ll> factor;
map<ll, ll> prime;

void all_factor(ll n){
    factor.clear();
    for (ll i = 1; i*i <= n; i++){
        if (n % i == 0){
            factor.push_back(i);
            if (i*i != n) factor.push_back(n / i);
        }
    }
    sort(factor.begin(), factor.end());
}

void prime_factor(ll n){
    prime.clear();
    ll m = n;

    if (n % 2 == 0){
        while(n % 2 == 0){
            prime[2]++;
            n /= 2;
        }
    }

    for (ll i = 3; i*i <= m; i+=2){
        if (n % i == 0){
            while(n % i == 0){
                prime[i]++;
                n /= i;
            }
        }
    }

    if (n != 1){
        prime[n]++;
    }
}

ll mod_exp(ll b, ll e, ll m){
    if (e > 0 && b == 0) return 0;
    b %= m;
    ll ans = 1;

    while (e > 0){
        if ((e & 1LL)) ans = (ans * b) % m;
        e = e >> 1LL;
        b = (b*b) % m;
    }

    return ans;
}

ll totient(ll N){
    prime_factor(N);
    ll res=1;
    for (auto [p, e] : prime){
        res *= mod_exp(p, e-1, 9e18) * (p-1);
    }
    return res;
}

void solve(){
    ll N;
    cin >> N;
    while(N % 2 == 0) N /= 2;
    while(N % 5 == 0) N /= 5;
    ll t = totient(N);
    all_factor(t);

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

    for (auto x : factor){
        if (mod_exp(10, x, N) == 1){
            cout << x << endl;
            return;
        }
    }
}

int main(){
    
    int T;
    cin >> T;
    while(T){
        T--;
        solve();
    }

    return 0;
}
0