QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112241 | #6566. Power of Divisors | ITMO_pengzoo# | AC ✓ | 3ms | 3524kb | C++20 | 5.2kb | 2023-06-10 22:23:41 | 2023-06-10 22:23:44 |
Judging History
answer
// Nikita Golikov, 2023
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
#ifdef GOLIKOV
#include "/Users/golikovnik/contests/debug.h"
#else
#define debug(...) ;
#endif
template <class A, class B>
bool smin(A& x, B&& y) {
if (y < x) {
x = y;
return true;
}
return false;
}
template <class A, class B>
bool smax(A& x, B&& y) {
if (x < y) {
x = y;
return true;
}
return false;
}
#ifndef MODMUL64_H
#define MODMUL64_H
ll modmul64(ll x, ll y, ll p) {
ull q = (long double) x * y / p;
ll res = ll(ull(x) * y - q * p);
if (res >= p) res -= p;
if (res < 0) res += p;
return res;
}
#endif
namespace factorization {
ll pw(ll a, ll n, ll p) {
ll res = 1;
while (n) {
if (n & 1) res = modmul64(res, a, p);
a = modmul64(a, a, p);
n >>= 1;
}
return res;
}
bool check_composite(ll n, int s, ll d, ll a) {
ll x = pw(a, d, n);
if (x == 1 || x == n - 1) return false;
for (int it = 1; it < s; ++it) {
x = modmul64(x, x, n);
if (x == n - 1) return false;
}
return true;
}
bool is_prime(ll n) {
if (n < 4) return n > 1;
if (n % 2 == 0) {
return false;
}
if (n % 3 == 0) {
return false;
}
if (n == 5) {
return true;
}
if (n % 5 == 0) {
return false;
}
int s = 0;
ll d = n - 1;
while (!(d & 1)) {
d >>= 1;
++s;
}
static vector<ll> primes32{2, 7, 61};
static vector<ll> primes64{2, 325, 9375, 28178, 450775, 9780504,
1795265022};
static ll const BOUND = ll(4759123141ll);
for (ll a : (n <= BOUND ? primes32 : primes64)) {
if (n == a) return true;
if (check_composite(n, s, d, a)) return false;
}
return true;
}
ll find_divisor(ll n, int c = 2) {
auto f = [&](ll x) {
auto r = modmul64(x, x, n) + c;
if (r >= n) r -= n;
return r;
};
ll x = c;
ll g = 1;
ll q = 1;
ll xs, y;
int m = 128;
int l = 1;
while (g == 1) {
y = x;
for (int i = 1; i < l; ++i) {
x = f(x);
}
int k = 0;
while (k < l && g == 1) {
xs = x;
for (int i = 0; i < m && i < l - k; ++i) {
x = f(x);
q = modmul64(q, llabs(y - x), n);
}
g = gcd(q, n);
k += m;
}
l *= 2;
}
if (g == n) {
do {
xs = f(xs);
g = gcd(llabs(xs - y), n);
} while (g == 1);
}
return g == n ? find_divisor(n, c + 1) : g;
}
vector<pair<ll, int>> factorize(ll m) {
assert(m > 0);
if (m == 1) {
return {};
}
vector<ll> fac;
auto rec = [&fac](auto&& self, ll x) -> void {
if (is_prime(x)) {
fac.push_back(x);
return;
}
auto d = x % 2 == 0 ? 2 : find_divisor(x);
self(self, d);
self(self, x / d);
};
rec(rec, m);
sort(fac.begin(), fac.end());
vector<pair<ll, int>> ans;
for (auto x : fac) {
if (ans.empty() || ans.back().first != x) {
ans.emplace_back(x, 0);
}
++ans.back().second;
}
return ans;
}
}
using factorization::factorize;
using factorization::is_prime;
vector<ll> find_divisors(vector<pair<ll, int>> const& fac) {
vector<ll> ans;
int cnt = 1;
for (auto it : fac) cnt *= (it.second + 1);
ans.reserve(cnt);
ans.push_back(1);
for (auto[p, d] : fac) {
int sz = int(ans.size());
for (int i = 0; i < sz; ++i) {
ll mul = 1;
for (int j = 1; j <= d; ++j) {
mul *= p;
ans.push_back(ans[i] * mul);
}
}
}
assert(int(ans.size()) == cnt);
return ans;
}
vector<ll> find_divisors(ll x) {
return find_divisors(factorize(x));
}
ll getPhi(ll x, vector<pair<ll, int>> const& fac) {
for (auto it : fac) {
x /= it.first;
x *= it.first - 1;
}
return x;
}
ll getPhi(ll x) {
return getPhi(x, factorize(x));
}
int main() {
#ifdef GOLIKOV
assert(freopen("in", "rt", stdin));
auto _clock_start = chrono::high_resolution_clock::now();
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n; cin >> n;
auto mul = [&](ll x, ll y) {
return x && y > (n + 1) / x ? n + 1 : x * y;
};
auto power = [&](ll x, ll d) {
ll res = 1;
while (d) {
if (d & 1) res = mul(res, x);
x = mul(x, x);
d /= 2;
}
return res;
};
ll ans = n + 1;
for (int d = 1; d <= 60; ++d) {
ll left = 0;
ll right = n;
while (left + 1 != right) {
ll mid = (left + right) / 2;
if (power(mid, d) >= n) {
right = mid;
} else {
left = mid;
}
}
if (power(right, d) == n && int(find_divisors(right).size()) == d) {
smin(ans, right);
}
}
cout << (ans == n + 1 ? -1 : ans) << '\n';
#ifdef GOLIKOV
cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(
chrono::high_resolution_clock::now()
- _clock_start).count() << "ms." << endl;
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3500kb
input:
15625
output:
25
result:
ok single line: '25'
Test #2:
score: 0
Accepted
time: 2ms
memory: 3412kb
input:
64000000
output:
20
result:
ok single line: '20'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3400kb
input:
65536
output:
-1
result:
ok single line: '-1'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
1
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 2ms
memory: 3408kb
input:
10
output:
-1
result:
ok single line: '-1'
Test #6:
score: 0
Accepted
time: 2ms
memory: 3408kb
input:
100
output:
-1
result:
ok single line: '-1'
Test #7:
score: 0
Accepted
time: 2ms
memory: 3468kb
input:
10000
output:
10
result:
ok single line: '10'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
1000000000000000000
output:
100
result:
ok single line: '100'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3464kb
input:
10372926089038969
output:
218089
result:
ok single line: '218089'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3412kb
input:
10642944803293201
output:
10157
result:
ok single line: '10157'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3420kb
input:
10646534823110209
output:
103182047
result:
ok single line: '103182047'
Test #12:
score: 0
Accepted
time: 1ms
memory: 3384kb
input:
1073741824
output:
32
result:
ok single line: '32'
Test #13:
score: 0
Accepted
time: 2ms
memory: 3408kb
input:
121
output:
11
result:
ok single line: '11'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3420kb
input:
1296
output:
6
result:
ok single line: '6'
Test #15:
score: 0
Accepted
time: 1ms
memory: 3396kb
input:
16
output:
-1
result:
ok single line: '-1'
Test #16:
score: 0
Accepted
time: 2ms
memory: 3404kb
input:
16277421889
output:
127583
result:
ok single line: '127583'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
169
output:
13
result:
ok single line: '13'
Test #18:
score: 0
Accepted
time: 2ms
memory: 3400kb
input:
1985984
output:
-1
result:
ok single line: '-1'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3452kb
input:
2
output:
-1
result:
ok single line: '-1'
Test #20:
score: 0
Accepted
time: 2ms
memory: 3408kb
input:
205891132094649
output:
243
result:
ok single line: '243'
Test #21:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
25
output:
5
result:
ok single line: '5'
Test #22:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
2626114239841
output:
1273
result:
ok single line: '1273'
Test #23:
score: 0
Accepted
time: 0ms
memory: 3424kb
input:
26269395104446321
output:
12731
result:
ok single line: '12731'
Test #24:
score: 0
Accepted
time: 2ms
memory: 3488kb
input:
3
output:
-1
result:
ok single line: '-1'
Test #25:
score: 0
Accepted
time: 0ms
memory: 3404kb
input:
3596345248055296
output:
88
result:
ok single line: '88'
Test #26:
score: 0
Accepted
time: 2ms
memory: 3484kb
input:
36
output:
-1
result:
ok single line: '-1'
Test #27:
score: 0
Accepted
time: 1ms
memory: 3460kb
input:
4
output:
2
result:
ok single line: '2'
Test #28:
score: 0
Accepted
time: 2ms
memory: 3504kb
input:
4096
output:
8
result:
ok single line: '8'
Test #29:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
49
output:
7
result:
ok single line: '7'
Test #30:
score: 0
Accepted
time: 2ms
memory: 3488kb
input:
5
output:
-1
result:
ok single line: '-1'
Test #31:
score: 0
Accepted
time: 2ms
memory: 3424kb
input:
576460752303423488
output:
-1
result:
ok single line: '-1'
Test #32:
score: 0
Accepted
time: 2ms
memory: 3420kb
input:
581431415926321
output:
24112889
result:
ok single line: '24112889'
Test #33:
score: 0
Accepted
time: 2ms
memory: 3472kb
input:
6
output:
-1
result:
ok single line: '-1'
Test #34:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
64
output:
4
result:
ok single line: '4'
Test #35:
score: 0
Accepted
time: 1ms
memory: 3408kb
input:
656100000000
output:
30
result:
ok single line: '30'
Test #36:
score: 0
Accepted
time: 2ms
memory: 3404kb
input:
7
output:
-1
result:
ok single line: '-1'
Test #37:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
729
output:
9
result:
ok single line: '9'
Test #38:
score: 0
Accepted
time: 0ms
memory: 3412kb
input:
8
output:
-1
result:
ok single line: '-1'
Test #39:
score: 0
Accepted
time: 2ms
memory: 3412kb
input:
81
output:
-1
result:
ok single line: '-1'
Test #40:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
8527674378686464
output:
452
result:
ok single line: '452'
Test #41:
score: 0
Accepted
time: 2ms
memory: 3404kb
input:
9
output:
3
result:
ok single line: '3'
Test #42:
score: 0
Accepted
time: 2ms
memory: 3432kb
input:
982134461213542729
output:
994009
result:
ok single line: '994009'
Test #43:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
9992002399680016
output:
9998
result:
ok single line: '9998'
Test #44:
score: 0
Accepted
time: 0ms
memory: 3432kb
input:
999269311525198921
output:
-1
result:
ok single line: '-1'
Test #45:
score: 0
Accepted
time: 2ms
memory: 3424kb
input:
999949000866995087
output:
-1
result:
ok single line: '-1'
Test #46:
score: 0
Accepted
time: 0ms
memory: 3392kb
input:
999995482005103081
output:
-1
result:
ok single line: '-1'
Test #47:
score: 0
Accepted
time: 3ms
memory: 3428kb
input:
999999969999997921
output:
-1
result:
ok single line: '-1'
Test #48:
score: 0
Accepted
time: 2ms
memory: 3488kb
input:
999999999999999989
output:
-1
result:
ok single line: '-1'
Test #49:
score: 0
Accepted
time: 2ms
memory: 3524kb
input:
999999999999999999
output:
-1
result:
ok single line: '-1'