QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#179114 | #6890. Guess | PPP# | RE | 0ms | 0kb | C++17 | 2.6kb | 2023-09-14 18:13:12 | 2023-09-14 18:13:12 |
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 998244353;
ll mult(ll a, ll b) {
return (1LL * (a % mod) * (b % mod)) % mod;
}
ll sub(ll a, ll b) {
ll s = (a - b) % mod;
if (s < 0) s += mod;
return s;
}
ll sum(ll a, ll b) {
ll s = (a + b) % mod;
if (s < 0) s += mod;
return s;
}
ll pw(ll a, ll b) {
if (b == 0) return 1;
if (b & 1) return mult(a, pw(a, b - 1));
ll res = pw(a, b / 2);
return mult(res, res);
}
ll n;
const ll INF = (ll)2e18 + 10;
ll safe_mult(ll a, ll b) {
if (!a || !b) return 0;
if (a >= (INF + b - 1) / b) return INF;
return a * b;
}
ll safe_pw(ll a, ll b) {
if (b == 0) return 1;
if (b & 1) return mult(safe_pw(a, b - 1), a);
ll res = safe_pw(a, b / 2);
return safe_mult(res, res);
}
typedef long long lint;
namespace miller_rabin{
lint mul(lint x, lint y, lint mod){ return (__int128) x * y % mod; }
lint ipow(lint x, lint y, lint p){
lint ret = 1, piv = x % p;
while(y){
if(y&1) ret = mul(ret, piv, p);
piv = mul(piv, piv, p);
y >>= 1;
}
return ret;
}
bool miller_rabin(lint x, lint a){
if(x % a == 0) return 0;
lint d = x - 1;
while(1){
lint tmp = ipow(a, d, x);
if(d&1) return (tmp != 1 && tmp != x-1);
else if(tmp == x-1) return 0;
d >>= 1;
}
}
bool isprime(lint x){
for(auto &i : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}){
if(x == i) return 1;
if(x > 40 && miller_rabin(x, i)) return 0;
}
if(x <= 40) return 0;
return 1;
}
}
void solve() {
cin >> n;
if (n == 1) {
cout << 1;
return;
}
for (ll k = 63; k >= 1; k--) {
ll L = 1;
ll R = n + 1;
while (R - L > 1) {
ll mid = (L + R) / 2;
if (safe_pw(mid, k) <= n) L = mid;
else R = mid;
}
if (safe_pw(L, k) == n) {
if (miller_rabin::isprime(L)) {
cout << L % mod;
return;
}
cout << 1;
return;
}
}
assert(false);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int tst;
cin >> tst;
for (int i = 1; i <= tst; i++) {
solve();
if (i != tst) cout << " ";
}
return 0;
}
详细
Test #1:
score: 0
Runtime Error
input:
2000 19491001 1 998244353 26952597531 861547697451441000 996491788296388609 981763655235363000 1024000007168 996491787298144256 973929762490885200 891042123129958800 878912224686896400 804111184288011600 766710664088569200 930867627131442000 996491787298144256 812033461965726000 964953451315854000 8...