結果

問題 No.1339 循環小数
ユーザー KKT89KKT89
提出日時 2021-01-28 19:41:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,317 bytes
コンパイル時間 2,190 ms
コンパイル使用メモリ 202,928 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-08 10:50:42
合計ジャッジ時間 4,382 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
 
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

// オイラーのトーシェント関数
// yukicoder No.1339
template<typename T>
T euler_phi(T n){
    T res=n;
    for(T i=2;i*i<=n;++i){
        if(n%i==0){
            res-=res/i;
            while(n%i==0)n/=i;
        }
    }
    if(n>1)res-=res/n;
    return res;
}

// 約数はソートしていないことに注意
template<typename T> vector<T> divisor(T n){
    vector<T> res;
    for(T i=1;i*i<=n;++i){
        if(n%i==0){
            res.emplace_back(i);
            if(i!=n/i)res.emplace_back(n/i);
        }
    }
    return res;
}

ll mod_pow(ll x,ll n,ll Mod){
    x%=Mod;
    ll res=1;
    while(n>0){
        if(n&1LL)res=res*x%Mod;
        x=x*x%Mod; n>>=1LL;
    }
    return res;
}

int solve(int n){
    while(n%2==0)n/=2;
    while(n%5==0)n/=5;
    int res=n;
    for(int d:divisor(euler_phi(n))){
        if(mod_pow(10,d,n)==1){
            res=min(res,d);
        }
    }
    return res;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        int n; cin >> n;
        cout << solve(n) << "\n";
    }
}
0