QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#780519 | #6179. 最大平方问题 | le0n | 95 | 1913ms | 19252kb | C++14 | 5.0kb | 2024-11-25 11:20:45 | 2024-11-25 11:20:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 i128;
const int N = 1e6 + 5, mod = 998244353;
ll p;
mt19937_64 rng(1293128);
map<ll, int> mp;
set<ll> qwq;
int qpow(int x, int y)
{
int ans = 1;
while(y)
{
if(y & 1)
ans = (ll)ans * x % mod;
x = (ll)x * x % mod;
y >>= 1;
}
return ans;
}
ll qpow_p(ll x, ll y)
{
ll ans = 1;
while(y)
{
if(y & 1)
ans = (i128)ans * x % p;
x = (i128)x * x % p;
y >>= 1;
}
return ans;
}
void mul_f(vector<ll> &A, vector<ll> B)
{
int i, j, n, m;
if(A.empty() || B.empty())
{
A.clear();
return ;
}
n = (int)A.size() - 1;
m = (int)B.size() - 1;
A.resize(n + m + 1);
for(i = n + 1; i <= n + m; i++)
A[i] = 0;
for(i = n; i >= 0; i--)
{
for(j = 1; j <= m; j++)
A[i + j] = (A[i + j] + (i128)A[i] * B[j]) % p;
A[i] = (i128)A[i] * B[0] % p;
}
}
void mod_f(vector<ll> &A, vector<ll> B)
{
int i, j, n, m;
ll q;
while(!B.empty() && !B.back())
B.pop_back();
n = (int)A.size() - 1;
m = (int)B.size() - 1;
if(n < m)
return ;
q = qpow_p(B.back(), p - 2);
for(auto &o: B)
o = (i128)o * q % p;
for(i = n; i >= m; i--)
{
q = p - A[i];
for(j = 1; j <= m; j++)
A[i - j] = (A[i - j] + (i128)q * B[m - j]) % p;
A[i] = 0;
}
while(!A.empty() && !A.back())
A.pop_back();
}
vector<ll> qpow_f(vector<ll> A, vector<ll> B, ll y)
{
vector<ll> ans = {1};
while(y)
{
if(y & 1)
{
mul_f(ans, A);
mod_f(ans, B);
}
mul_f(A, A);
mod_f(A, B);
y >>= 1;
}
return ans;
}
vector<ll> gcd_f(vector<ll> A, vector<ll> B)
{
ll q;
while(!A.empty() && !A.back())
A.pop_back();
while(!B.empty() && !B.back())
B.pop_back();
while(!A.empty())
{
B.swap(A);
if(A.empty())
break;
mod_f(A, B);
}
q = qpow_p(B.back(), p - 2);
for(auto &o: B)
o = (i128)o * q % p;
return B;
}
vector<ll> facto(vector<ll> A) // A.back() = 1
{
vector<ll> ans, B, C;
int i, j, n, m;
if(A.size() == 1)
return {};
if(A.size() == 2)
return {(p - A[0]) % p};
do
{
B.resize(A.size() - 1);
for(auto &o: B)
o = rng() % p;
B = qpow_f(B, A, (p - 1) / 2);
while(B.empty())
B.emplace_back(0);
B[0] = (B[0] + p - 1) % p;
B = gcd_f(A, B);
}
while(B.size() == 1 || B.size() == A.size());
n = (int)A.size() - 1;
m = (int)B.size() - 1;
C.resize(n - m + 1);
for(i = n - m; i >= 0; i--)
{
C[i] = A[i + m];
for(j = 0; j < m; j++)
A[i + j] = (A[i + j] + (i128)(p - C[i]) * B[j]) % p;
A[i + m] = 0;
}
ans = facto(B);
C = facto(C);
for(auto o: C)
ans.emplace_back(o);
return ans;
}
vector<ll> fact(vector<ll> A)
{
while(!A.empty() && !A.back())
A.pop_back();
if(A.size() <= 1)
return {};
vector<ll> B = {0, 1};
B = qpow_f(B, A, p);
while(B.size() < 2)
B.emplace_back(0);
B[1] = (B[1] + p - 1) % p;
return facto(gcd_f(A, B));
}
bool ntp[N];
ll prime[N];
int cnt;
ll res[N], val[N], awa[N];
void sieve(int n)
{
int i, j;
for(i = 2; i <= n; i++)
{
if(!ntp[i])
prime[++cnt] = i;
for(j = 1; j <= cnt && i * prime[j] <= n; j++)
{
ntp[i * prime[j]] = 1;
if(!(i % prime[j]))
break;
}
}
}
int main()
{
// freopen("sqr.in", "r", stdin);
// freopen("sqr.out", "w", stdout);
int n, a, b, c, i, j, S, ans = 1;
ll x, k;
vector<ll> tmp;
scanf("%d%d%d%d", &a, &b, &c, &n);
x = (ll)a * n * n + (ll)b * n + c;
S = n;
while((ll)(S + 1) * (S + 1) * (S + 1) <= x)
S++;
while((ll)(S + 1) * (S + 1) <= 2ll * n * a + b)
S++;
sieve(S);
for(i = 0; i <= 2 * n; i++)
awa[i] = (ll)a * i + b;
for(i = 0; i <= n; i++)
val[i] = res[i] = (ll)a * i * i + (ll)b * i + c;
for(i = 1; i <= cnt; i++)
{
p = prime[i];
if(!(a % p))
{
if(b % p)
continue;
for(j = 1; j <= 2 * n; j++)
while(!(awa[j] % p))
awa[j] /= p;
continue;
}
j = (ll)qpow_p(a % p, p - 2) * (p - b % p) % p;
for(k = (j ? j : p); k <= 2 * n; k += p)
while(!(awa[k] % p))
awa[k] /= p;
}
for(i = 1; i <= 2 * n; i++)
if(awa[i] > 1)
qwq.insert(awa[i]);
for(auto o: qwq)
prime[++cnt] = o;
// printf("EE%d\n", cnt);
for(i = 1; i <= cnt; i++)
{
p = prime[i];
// if(i > 24300)
// printf("!!%d %lld\n", i, p);
if(p > 10)
{
tmp = fact({c % p, b % p, a % p});
for(auto o: tmp)
for(k = (o ? o : p); k <= n; k += p)
while(!(res[k] % p))
{
res[k] /= p;
mp[p]++;
}
}
else
for(j = 0; j < p; j++)
if(!(val[j] % p))
for(k = (j ? j : p); k <= n; k += p)
while(!(res[k] % p))
{
res[k] /= p;
mp[p]++;
}
}
for(i = 1; i <= n; i++)
if(res[i] > 1)
{
S = sqrt(res[i]);
while((ll)(S + 1) * (S + 1) <= res[i])
S++;
while((ll)S * S > res[i])
S--;
if((ll)S * S == res[i])
mp[S] += 2;
else
mp[res[i]]++;
}
for(auto o: mp)
ans = (ll)ans * qpow(o.first % mod, o.second / 2) % mod;
ans = (ll)ans * ans % mod;
printf("%d\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 1ms
memory: 8156kb
input:
17 3 17 14
output:
123798013
result:
ok 1 number(s): "123798013"
Test #2:
score: 5
Accepted
time: 1ms
memory: 7812kb
input:
1 18 0 6
output:
2073600
result:
ok 1 number(s): "2073600"
Test #3:
score: 5
Accepted
time: 1ms
memory: 8004kb
input:
20 6 20 14
output:
315851899
result:
ok 1 number(s): "315851899"
Test #4:
score: 5
Accepted
time: 2ms
memory: 8024kb
input:
81 190 40 125
output:
924770602
result:
ok 1 number(s): "924770602"
Test #5:
score: 5
Accepted
time: 3ms
memory: 8016kb
input:
108 31 110 122
output:
319532761
result:
ok 1 number(s): "319532761"
Test #6:
score: 5
Accepted
time: 15ms
memory: 8068kb
input:
988 194 1412 934
output:
871618879
result:
ok 1 number(s): "871618879"
Test #7:
score: 5
Accepted
time: 17ms
memory: 8304kb
input:
1956 1305 92 1061
output:
681879967
result:
ok 1 number(s): "681879967"
Test #8:
score: 5
Accepted
time: 172ms
memory: 8932kb
input:
75955 165029 149078 5607
output:
739160804
result:
ok 1 number(s): "739160804"
Test #9:
score: 5
Accepted
time: 83ms
memory: 8640kb
input:
3223 4143 3383 4499
output:
139873119
result:
ok 1 number(s): "139873119"
Test #10:
score: 5
Accepted
time: 208ms
memory: 11616kb
input:
9257 3632 1736 9497
output:
158679485
result:
ok 1 number(s): "158679485"
Test #11:
score: 5
Accepted
time: 411ms
memory: 10616kb
input:
13657 8517 9780 16829
output:
499148359
result:
ok 1 number(s): "499148359"
Test #12:
score: 5
Accepted
time: 251ms
memory: 9420kb
input:
152794 105986 145639 8507
output:
432311896
result:
ok 1 number(s): "432311896"
Test #13:
score: 5
Accepted
time: 5ms
memory: 7972kb
input:
2209 13866 42748 227
output:
576767941
result:
ok 1 number(s): "576767941"
Test #14:
score: 5
Accepted
time: 50ms
memory: 8352kb
input:
14279 7290 25915 2265
output:
173729704
result:
ok 1 number(s): "173729704"
Test #15:
score: 5
Accepted
time: 700ms
memory: 14136kb
input:
24703 6569 26356 26534
output:
108700579
result:
ok 1 number(s): "108700579"
Test #16:
score: 5
Accepted
time: 480ms
memory: 10284kb
input:
187348 185285 76358 13718
output:
425402711
result:
ok 1 number(s): "425402711"
Test #17:
score: 5
Accepted
time: 463ms
memory: 12184kb
input:
189507 133399 143282 13930
output:
608644351
result:
ok 1 number(s): "608644351"
Test #18:
score: 0
Time Limit Exceeded
input:
102698 162019 98595 137546
output:
result:
Test #19:
score: 5
Accepted
time: 484ms
memory: 10548kb
input:
152381 90362 151048 15578
output:
413943175
result:
ok 1 number(s): "413943175"
Test #20:
score: 5
Accepted
time: 1913ms
memory: 19252kb
input:
34402 49259 74598 64198
output:
733372582
result:
ok 1 number(s): "733372582"