結果

問題 No.1339 循環小数
ユーザー Kite_kumaKite_kuma
提出日時 2021-01-16 00:02:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,620 ms / 2,000 ms
コード長 13,261 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 178,212 KB
実行使用メモリ 5,716 KB
最終ジャッジ日時 2023-08-17 19:18:40
合計ジャッジ時間 18,673 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 5 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 797 ms
5,216 KB
testcase_22 AC 912 ms
5,440 KB
testcase_23 AC 892 ms
5,184 KB
testcase_24 AC 813 ms
5,352 KB
testcase_25 AC 888 ms
5,716 KB
testcase_26 AC 885 ms
5,400 KB
testcase_27 AC 887 ms
5,184 KB
testcase_28 AC 914 ms
5,160 KB
testcase_29 AC 723 ms
5,432 KB
testcase_30 AC 813 ms
5,420 KB
testcase_31 AC 1,405 ms
5,500 KB
testcase_32 AC 1,428 ms
5,320 KB
testcase_33 AC 890 ms
5,508 KB
testcase_34 AC 495 ms
5,432 KB
testcase_35 AC 1,620 ms
5,324 KB
testcase_36 AC 847 ms
5,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*	author:  Kite_kuma
	created: 2021.01.15 21:51:17 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = priority_queue<T, vector<T>, greater<T>>;
template <class T = ll>
using pqdn = priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod = 1000000007;
// const int mod = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T>
inline bool in_range(T x, T lef, T rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { cout << (f ? "Yes" : "No") << '\n'; }
void No() { cout << "No\n"; }
void YES(bool f = 1) { cout << (f ? "YES" : "NO") << '\n'; }
void NO() { cout << "NO\n"; }

template <class T>
void drop(T answer) {
	cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(vector<T> const &v, bool tate = 0) {
	if(tate) {
		for(auto const &a : v) {
			cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		cout << (*it);
		if(it != v.end() - 1) {
			cout << ' ';
		}
	}
	cout << '\n';
	return;
}

inline void print() { cout << '\n'; }
template <class T>
inline void print(T x) {
	cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	cout << H << " ";
	print(T...);
}

template <class T>
void add(vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

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

// return x such that a * x % mod == gcd(a, mod)
// not (mod divides a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		swap(a, b);
		u -= t * v;
		swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

// normal binomial coefficient
vvll comb(100, vll(100, -1));
long long com(long long n, long long k) {
	assert(n < 100 && k < 100);
	if(n < k || k < 0 || n < 0) return 0;
	if(comb[n][k] != -1) return comb[n][k];
	ll res;
	if(n - k < k)
		res = com(n, n - k);
	else if(k == 0)
		res = 1;
	else
		res = com(n - 1, k - 1) + com(n - 1, k);
	comb[n][k] = res;
	return res;
}

const int MAX = 5100000;  // 300ms, when MAX = 3*10^7 : 1900ms
long long fac[MAX], finv[MAX], inv[MAX];

void cominit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}
long long commod(long long n, long long k) {
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
long long pmod(long long n, long long k) {
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * finv[n - k] % mod;
}
// n個の区別しないボールを区別するk個の箱に入れる方法の総数
long long hmod(long long n, long long k) {	// 重複組み合わせ
	return commod(n + k - 1, n);
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)

namespace scanner {
template <class T>
void scan(T &a) {
	cin >> a;
}
template <class T>
void scan(vector<T> &a) {
	for(auto &i : a) scan(i);
}
template <class T, class L>
void scan(pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &...tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		view(x);                                     \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

// a^x ≡ b となる最小の正の整数 x を求める
// mod は素数
long long modlog(long long a, long long b, int mod) {
	a %= mod, b %= mod;
	if(a == 0) return b ? -1LL : 0LL;

	// calc ceil(sqrt{M})
	long long lo = -1, hi = mod;
	while(hi - lo > 1) {
		long long mid = (lo + hi) / 2;
		if(mid * mid >= mod)
			hi = mid;
		else
			lo = mid;
	}
	long long sqrtmod = hi;

	// {a^0, a^1, a^2, ..., a^sqrt(mod)}
	map<long long, long long> apow;
	long long amari = 1;
	for(long long r = 0; r < sqrtmod; ++r) {
		if(apow.count(amari)) break;
		apow[amari] = r;
		(amari *= a) %= mod;
	}

	// check each A^p
	long long A = modpow(modinv(a, mod), sqrtmod, mod);
	amari = b;
	for(long long q = 0; q < sqrtmod; ++q) {
		if(apow.count(amari)) {
			long long res = q * sqrtmod + apow[amari];
			return res;
		}
		(amari *= A) %= mod;
	}

	// no solutions
	return -1;
}

ll solve(ll n) {
	while(n % 2 == 0) n /= 2;
	while(n % 5 == 0) n /= 5;
	// if(n == 1) return 1LL;
	ll ans = 1LL;

	ans = modlog(10, modinv(10, n), n) + 1;
	return ans;

	// auto ok = [](int keta, ll MOD) { return modpow(10, keta, MOD) % MOD == 1 % MOD; };

	// auto search = [&](ll d) {
	// 	ll m = d - 1;
	// 	ll ans = INF;
	// 	rep(i, 1, m + 1) {
	// 		if((ll)i * (ll)i > m) break;
	// 		if(m % i == 0) {
	// 			if(ok(i, d)) chmin(ans, (ll)i);
	// 			if(ok(m / i, d)) chmin(ans, (ll)m / i);
	// 		}
	// 	}
	// 	return ans;
	// };

	// auto f = [&](ll d, ll cnt) -> ll {
	// 	ll ret = INF;
	// 	if(d == 3) {
	// 		return POW(3, max(0LL, cnt - 2));
	// 	}
	// 	ret = search(d);
	// 	ret *= POW(d, cnt - 1);

	// 	return ret;
	// };

	// ll m = n;
	// // debug(m);
	// rep(i, 2, m + 1) {
	// 	if((ll)i * (ll)i > m) {
	// 		break;
	// 	}
	// 	if(m % i == 0) {
	// 		ll ret = i;
	// 		ll cnt = 0;
	// 		while(m % i == 0) {
	// 			// debug(m, i);
	// 			m /= i;
	// 			cnt++;
	// 		}
	// 		ll res = f(ret, cnt);
	// 		ans = ans / __gcd(ans, res) * res;
	// 	}
	// }
	// if(m > 1) {
	// 	ll res = f(m, 1);
	// 	ans /= __gcd(ans, res);
	// 	ans *= res;
	// }
	// return ans;
}

// ll naive(ll n) {
// 	while(n % 2 == 0) n /= 2;
// 	while(n % 5 == 0) n /= 5;
// 	ll pw = 1;
// 	rep(i, 1, n + 20) {
// 		pw *= 10;
// 		pw %= n;
// 		if((pw - 1) % n == 0) return i;
// 	}
// 	return -1ll;
// }

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	srand((unsigned)time(NULL));

	// debug(solve(15625));

	INT(t);
	rep(t) {
		int n;
		cin >> n;
		print(solve(n));
	}

	// debug(solve(1));

	// do {
	// 	ll n = rand() % 50000 + 2;
	// 	if(solve(n) != naive(n)) {
	// 		debug(n);
	// 		debug(solve(n));
	// 		debug(naive(n));
	// 		exit(0);
	// 	}
	// 	debug(n);
	// } while(1);

	return 0;
}
0