結果

問題 No.1339 循環小数
ユーザー DaYuanChiDaYuanChi
提出日時 2021-01-20 00:07:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,170 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 168,344 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 19:42:05
合計ジャッジ時間 3,767 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

//10^xをnで割った余り
ll f(ll x, ll n){
  ll ret = 1;
  ll p = 10%n;
  while(x>0){
    if(x&1>0) ret = (ret*p)%n;
    p = (p*p)%n;
    x >>= 1;
  }
  return ret%n;
}

vector<ll> p;

//nの素因数をベクトルpに格納
void prime(int n){
  int num = n;
  for(int i = 2; i*i <=n ;i++){
     int x = 0;
     while(num%i ==0){
           num  /=  i;
           x++;
    }
    if(x >=1) p.push_back(i);
  }
   if(num!=1) p.push_back(num);
}

//オイラー関数(xと互いに素なx以下の自然数の個数)
int Euler(int x){
  prime(x);
  int euler = x;
  for(int i = 0; i < p.size(); i++) euler = euler/p[i]*(p[i]-1);
  return euler;
}


int main(){
  int t; cin >> t;
  for(int i = 0; i < t; i++){
    int n; cin >> n;
    while(n%2==0) n /= 2;
    while(n%5==0) n /= 5;
    if(n==1){
      cout << 1 << endl;
      continue;
    }
    p.clear();
    int m = Euler(n);
    int ans = m;
    for(int i = 1; i*i <= m; i++){
      if(m%i==0){
        if(f(i, n)==1) ans = min(ans, i);
        if(f(m/i, n)==1) ans = min(ans, m/i);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
0