結果

問題 No.1339 循環小数
ユーザー merom686
提出日時 2021-01-16 00:51:12
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 820 ms
コンパイル使用メモリ 89,520 KB
最終ジャッジ日時 2025-01-17 21:03:24
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;

ll powmod(ll n, ll k, int m) {
    ll r = 1, t = n % m;
    for (; k != 0; k /= 2) {
        if (k & 1) r = r * t % m;
        t = t * t % m;
    }
    return r;
}

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

    while (t--) {
        int n;
        cin >> n;

        while (n % 2 == 0) n /= 2;
        while (n % 5 == 0) n /= 5;

        int x = n, t = n;
        for (int i = 2; i * i <= t; i++) {
            if (t % i == 0) {
                do t /= i; while (t % i == 0);
                x = x / i * (i - 1);
            }
        }
        if (t > 1) x = x / t * (t - 1);

        int r = n;
        for (int i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                int j = x / i;
                if (powmod(10, i, n) == 1) r = min(r, i);
                if (powmod(10, j, n) == 1) r = min(r, j);
            }
        }

        cout << r << '\n';
    }

    return 0;
}
0