結果

問題 No.1339 循環小数
ユーザー zlnzln
提出日時 2021-01-15 22:00:53
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,568 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 105,612 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-26 14:40:21
合計ジャッジ時間 2,652 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
template<typename T> using V = std::vector<T>;
template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; }
/*-*/

lnt ipow(lnt v,lnt k,lnt m) { lnt r(1),t(v); while(k){ if(k&1) {r*=t;r%=m;} t*=t; t%=m; k>>=1; } return r%m; }


int64_t euler_phi(int64_t n) {
  int64_t ret = n;
  for(int64_t i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      ret -= ret / i;
      while(n % i == 0) n /= i;
    }
  }
  if(n > 1) ret -= ret / n;
  return ret;
}

int main() {
	int t;
	std::cin >> t;
	rep(tt,t) {
		lnt n;
		std::cin >> n;
		if(10%n==0) {
			print(1);
			continue;
		}
		while(n%2==0) n/=2;
		while(n%5==0) n/=5;
		if(n==1) {
			print(1);
			continue;
		}
		lnt phi = euler_phi(n);
		lnt min = phi;
		for(lnt i=1;i*i<=phi;i++) {
			if(phi%i) continue;
			if(ipow(10,i,n)==1) min=std::min(min,i);
			if(ipow(10,phi/i,n)==1) min=std::min(min,phi/i);
		}
		print(min);
	}
}
0