QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#721728 | #9705. Multiply | tarjen# | WA | 2ms | 3920kb | C++20 | 2.4kb | 2024-11-07 16:42:17 | 2024-11-07 16:42:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, bool>P;
mt19937_64 rnd(time(0));
namespace Pollard_Rho
{
#define ldb long double
ll mul(ll x, ll y, ll mod)
{
return ((x * y - (ll)((ldb)x / mod * y) * mod) + mod) % mod;
}
ll gcd(ll a, ll b)
{
return (b == 0 ? a : gcd(b, a % b));
}
ll ksm(ll a, ll b, ll mod)
{
ll ans = 1; a %= mod;
while (b) {if (b & 1)ans = mul(ans, a, mod); b >>= 1; a = mul(a, a, mod);}
return ans;
}
int pr[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
bool Miller_Rabin(ll n)
{
if (n == 2 || n == 3)return 1;
if (n % 2 == 0 || n == 1)return 0;
ll d = n - 1;
int s = 0;
while (d % 2 == 0)s ++, d >>= 1;
for (int i = 0; i <= 11; i ++)
{
if (pr[i] >= n)break;
ll a = pr[i];
ll x = ksm(a, d, n);
ll y = 0;
for (int j = 0; j <= s - 1; j ++)
{
y = mul(x, x, n);
if (y == 1 && x != 1 && x != (n - 1))return 0;
x = y;
}
if (y != 1)return 0;
}
return 1;
}
ll Pollard_Rho(ll n)
{
ll now, pre, g;
while (true)
{
now = pre = rnd() % (n - 1) + 1;
g = 1;
ll c = rnd() % (n - 1) + 1;
for (int i = 1, fst = 1 ;; i ++)
{
now = (mul(now, now, n) + c) % n;
g = mul(g, abs(now - pre), n);
if (now == pre || !g)break;
if (!(i & 127) || i == fst)
{
g = gcd(g, n);
if (g > 1)return g;
if (i == fst)pre = now, fst <<= 1;
}
}
}
}
void Find(ll n)
{
if (n == 1)return ;
if (Miller_Rabin(n))
{
P[n] = 1;
return ;
}
ll p = Pollard_Rho(n);
int c = 0;
while (!(n % p))
{
n /= p, c ++;
}
Find(p);
Find(n);
}
}
void getp(ll x,set<ll> &s){
Pollard_Rho :: Find(x);
for (auto [x, _] : P)s.insert(x);
P.clear();
}
int solve()
{
ll n,X,Y;cin>>n>>X>>Y;
ll ans=6e18;
vector<ll>a(n);
for(int i=0;i<n;i++)cin>>a[i];
auto cal = [&](ll x,ll y){// cal x! y
ll ans=0;
while(x>0){
// cout<<"x="<<x<<" y="<<y<<endl;
ans+=x/y;x/=y;
}
return ans;
};
auto calans = [&](int x){
ll sum=0;
for(auto &it:a)sum+=cal(it,x);
ll ysum=cal(Y,x);
// cout<<"x="<<x<<" sum="<<sum<<" ysum="<<ysum<<endl;
ysum-=sum;
return ysum;
};
set<ll>s;
getp(X,s);
for(auto it:s){ans=min(ans,calans(it));}
return ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;while(T--)cout<<solve()<<"\n";
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3676kb
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
Wrong Answer
time: 2ms
memory: 3920kb
input:
8 929 98210021061137 164832982985885580 43576998167336 157303878397705 212661169553039 169068044677212 17733912750082 101059786177542 56528418806042 170741049344189 128297164019222 208810463591190 96264177952672 70816863413347 116985928070432 56330014332760 10006522486360 110959002803542 15298525649...
output:
47873232 95837140 367139364 424782143 1941221293 200721910 -1164482262 1365122294
result:
wrong answer 1st numbers differ - expected: '1059', found: '47873232'