結果

問題 No.1339 循環小数
ユーザー shun2741shun2741
提出日時 2021-01-16 17:21:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,310 bytes
コンパイル時間 2,064 ms
コンパイル使用メモリ 179,372 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 15:17:20
合計ジャッジ時間 5,476 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第2引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

// 最大公約数
ll gcd(ll a,ll b){
   if (a%b == 0) return(b);
   else return(gcd(b, a%b));
}

// 最小公倍数
ll lcm(ll a, ll b){
    return a/gcd(a, b) * b;
}

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

ll Euler(ll n){
    ll res = n;
    for(auto p : prime_factor(n)) {
        res *= (p.first - 1);
        res /= p.first;
    }
    return res;
}

// 約数の全列挙
vector<ll> divisor(ll n) {
  vector<ll> ret;
  for(ll i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

ll modpow(ll a, ll n, ll mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    cout << fixed << setprecision(15);
    ll T;
    cin >> T;

    REP(t, T){
        ll N;
        cin >> N;

        while(N%2 == 0){
            N /= 2;
        }
        while(N%5 == 0){
            N /= 5;
        }
        ll K = Euler(N);
        // cout << "K:" << K << endl;
        vector<ll>  D = divisor(K);

        if(N == 1 || N == 3 || N == 9){
            cout << 1 << endl;
            continue;
        }
        // cout << "N:" << N << endl;
        for(auto d: D){
            if(d==1) continue;
            // cout << d << endl;
            // cout << "mod:" << modpow(10, d+1 , N)<< endl;
            if(modpow(10, d, N) % N == 1){
                cout << d << endl;
                break;
            }
        }
    }
    return 0;
}
0