結果

問題 No.1339 循環小数
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-08-24 23:03:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,559 bytes
コンパイル時間 2,540 ms
コンパイル使用メモリ 212,548 KB
実行使用メモリ 8,764 KB
最終ジャッジ日時 2023-08-24 23:03:19
合計ジャッジ時間 7,003 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,764 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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){
    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