QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#735895 | #9705. Multiply | rose_DKY# | TL | 0ms | 3596kb | C++20 | 3.3kb | 2024-11-11 22:27:19 | 2024-11-11 22:27:19 |
Judging History
answer
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define __int128_t ll
#define MAXN 200005
ll a[MAXN];
ll max_factor;
int quick_pow(__int128_t base, ll exponent, ll modulo) {
__int128_t res = 1 % modulo;
base %= modulo;
while (exponent) {
if (exponent & 1)
res = res * base % modulo;
base = base * base % modulo;
exponent >>= 1;
}
return res;
}
// Miller Rabin 素性测试
// 154590409516822759, 2305843009213693951, 4384957924686954497
int test[] = {
2, 3, 5, 7, 11,
13, 17, 19, 23, 29,
31, 37, 41, 43, 47,
79, 83, 89, 97, 233,
}; // 测试集: 优先使用小质数
bool Miller_Rabin(ll n) {
if (n < 2 or n > 2 and n % 2 == 0)
return false;
int s = n - 1;
while (not(s & 1)) s >>= 1;
for (int p : test) {
if (n == p)
return true;
__int128_t t = s, m = quick_pow(p, s, n);
while (t != n - 1 and m != 1 and m != n - 1) {
m = m * m % n;
t <<= 1;
}
if (m != n - 1 and not(t & 1))
return false;
}
return true;
}
bool is_prime(ll x)
{
if (x == 1) return false;
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
mt19937 rnd((unsigned int)chrono::steady_clock::now().time_since_epoch().count());
int randint(int l, int r = 0) {
if (l > r) swap(l, r);
return rnd() % (r - l + 1) + l;
}
// Pollard Rho 因数测试
ll Pollard_Rho(ll n) {
if (n % 2 == 0) // Pollard 不能判 4
return 2;
if (Miller_Rabin(n))
return n;
while (true) {
int c = randint(1, n - 1);
auto nxt = [&](__int128_t x) -> int { return (x * x + c) % n; };
int turtle = 1, rabbit = -1;
__int128_t mul = 1;
while (turtle != rabbit) {
for (int i = 0; i < 128; i++) {
turtle = nxt(turtle);
rabbit = nxt(nxt(rabbit));
__int128_t tmp = mul * abs(turtle - rabbit) % n;
// 龟兔相遇或积将为 0 退出
if (turtle == rabbit or tmp == 0)
break;
mul = tmp;
}
ll g = __gcd((ll)mul, n);
if (g > 1)
return g;
}
}
}
// 分解质因数
ll divisors[128];
ll cnt_[128];
int t = 0;
ll prime_divisors(ll x) {
int cnt = 0, p = 0;
divisors[cnt++] = x;
while (p < cnt) {
ll x = divisors[p];
ll tmp = Pollard_Rho(x);
if (tmp == x) {
p++;
} else {
divisors[p] = tmp;
divisors[cnt++] = x / tmp;
}
}
sort(divisors, divisors + cnt);
t = 0;
for (int i = 0; i < cnt; i++) {
//cout << divisors[i] << endl;
ll s = 1;
while (i + 1 < cnt && divisors[i] == divisors[i + 1]) {
s++;
i++;
}
cnt_[t++] = s;
}
unique(divisors, divisors + cnt) - divisors - 1;
return t;
}
void solve()
{
ll n, x, y;
cin >> n >> x >> y;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
ll cnt = prime_divisors(x);
ll res = 1e18;
for (int i = 0; i < cnt; i++) {
// cout << divisors[i] << endl;
ll p = divisors[i], c = cnt_[i];
ll c1 = 0, c2 = 0;
for (int i = 1; i <= n; i++) {
ll t = a[i];
while (t) {
c1 += t / p;
t /= p;
}
}
ll t = y;
while (t) {
c2 += t / p;
t /= p;
}
res = min(res, (c2 - c1) / c);
}
cout << res << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
/*
2
3 10 10
2 3 4
2 2 10
1 1
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
2 3 10 10 2 3 4 2 2 10 1 1
output:
2 8
result:
ok 2 number(s): "2 8"
Test #2:
score: -100
Time Limit Exceeded
input:
8 929 98210021061137 164832982985885580 43576998167336 157303878397705 212661169553039 169068044677212 17733912750082 101059786177542 56528418806042 170741049344189 128297164019222 208810463591190 96264177952672 70816863413347 116985928070432 56330014332760 10006522486360 110959002803542 15298525649...