2019 ICPC徐州区域赛-F题
Alduhmellah and Behlah both like large numbers, lots of numbers and lots of large numbers. They also like to do calculations on those numbers.
One day, Alduhmellah wrote down NN positive integers a_1, a_2,\cdots, a_Na
1
,a
2
,⋯,a
N
. He decided to make them large by applying factorial to each number, so the numbers became a_1!, a_2!,\cdots, a_N!a
1
!,a
2
!,⋯,a
N
!. Finally, he multiplied all NN numbers to get a super-large number Z=a_1!\times a_2!\times\cdots\times a_N!Z=a
1
!×a
2
!×⋯×a
N
!.
A few days later, Behlah met Alduhmellah, and Behlah told Alduhmellah that he came up with another two numbers X,YX,Y. Thus, they started to repeatedly multiply XX to ZZ, generating a sequence b_i=Z\times X^ib
i
=Z×X
i
, to find out the largest value of ii such that b_ib
i
is a factor of Y!Y!.
You, being a friend with Alduhmellah and Behlah, realized that it is such a time-wasting process. Thus, to save time, you decided to write a program to calculate the answer.
Input
The first line contains one positive integer TT (T\leq 8T≤8) — the number of tests.
The description of each test contains two lines: the first line contains three positive integers N, X, YN,X,Y (N\leq 10^5; 2\leq X,Y\leq 10^{18}N≤10
5
;2≤X,Y≤10
18
), and the second line contains NN positive integers a_1, a_2,\cdots a_Na
1
,a
2
,⋯a
N
(a_i \leq 10^{18}; a_1+a_2+\cdots+a_N<Ya
i
≤10
18
;a
1
+a
2
+⋯+a
N
<Y). Their meanings are explained above.
Output
For each test, output an integer in one line, representing the largest value of ii such that b_ib
i
is a factor of Y!Y!.
题意:
寻找Y!除以Z的情况下
X
i
,
i
X^i,i
Xi,i的最大值。顺便偷一下大佬的米勒罗宾的板子!Orz。
根据具体数学中找因子的个数,然后我们再将用Y!除以Z剩下的因子来贪心拼凑成X就行了。
开始理解错题+读错题,被强哥骂成哈皮了(Orz)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10, INF = 0x3f3f3f3f;
int n; ll x, y, a[N], f[N];
mt19937 rd(time(0));
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll mul(ll a, ll b, ll p) {
return (__int128)a * b % p;
}
ll qpow(ll base, ll n, ll p) {
ll res = 1;
base %= p;
while (n) {
if (n & 1) {
res = mul(res, base, p);
}
base = mul(base, base, p);
n >>= 1;
}
return res;
}
struct Mill {
ll n, fac[22000][2], bk[22000]; int tot;
const int C = 2307;
const int S = 8;
bool check(ll a, ll n) {
ll m = n - 1, x, y = 0;
int j = 0;
while (!(m & 1)) {
m >>= 1;
++j;
}
x = qpow(a, m, n);
for (int i = 1; i <= j; x = y, ++i) {
y = mul(x, x, n);
if (y == 1 && x != 1 && x != n - 1) {
return 1;
}
}
return y != 1;
}
bool miller_rabin(ll n) {
if (n < 2) {
return 0;
} else if (n == 2) {
return 1;
} else if (!(n & 1)) {
return 0;
}
for (int i = 0; i < S; ++i) {
if (check(rd() % (n - 1) + 1, n)) {
return 0;
}
}
return 1;
}
ll pollard_rho(ll n, int c) {
ll i = 1, k = 2, x = rd() % n, y = x, d;
while (1) {
++i; x = (mul(x, x, n) + c) % n;
d = gcd(y - x, n);
if (d > 1 && d < n) {
return d;
}
if (y == x) {
return n;
}
if (i == k) {
y = x;
k <<= 1;
}
}
}
void findfac(ll n, int c) {
if (n == 1) {
return;
}
if (miller_rabin(n)) {
bk[++*bk] = n;
return;
}
ll m = n;
while (m == n) {
m = pollard_rho(n, c--);
}
findfac(m, c);
findfac(n / m, c);
}
void gao(ll _n) {
n = _n; *bk = 0;
findfac(n, C);
sort(bk + 1, bk + 1 + *bk);
fac[1][0] = bk[1];
fac[1][1] = 1;
tot = 1;
for (int i = 2; i <= *bk; ++i) {
if (bk[i] == bk[i - 1]) {
++fac[tot][1];
} else {
++tot;
fac[tot][0] = bk[i];
fac[tot][1] = 1;
}
}
}
}mill;
int main() {
int _T; cin >> _T;
while (_T--) {
scanf("%d%lld%lld", &n, &x, &y);
for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
mill.gao(x);
int tot = mill.tot;
for (int i = 1; i <= tot; ++i) f[i] = 0;
ll res =-8e18;
for (int i = 1; i <= tot; ++i) {
ll now = 1;
for (int j = 1; now <= y / mill.fac[i][0]; ++j) {
now *= mill.fac[i][0];
f[i] += (y / now);//一共的。
for (int o = 1; o <= n; ++o) {
f[i] -= (a[o] / now);//已经除掉的减去就是剩余的。
}
}
res = max(1ll * res, f[i] / mill.fac[i][1]);//可以最少拼凑的X的数量
}
printf("%lld\n", res);
}
return 0;
}