QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#215539 | #4801. The Pool | ucup-team1209 | AC ✓ | 216ms | 32488kb | C++14 | 5.2kb | 2023-10-15 11:34:32 | 2023-10-15 11:34:33 |
Judging History
answer
#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
using ll = long long ;
using i128 = __int128;
using f64 = long double;
cs int Mod = 998244353;
int T;
ll p;
f64 invp;
void setmod(ll x) {
p = x, invp = (f64) 1 / x;
}
ll mul(ll a, ll b) {
ll z = a * invp * b + 0.5;
ll res = a * b - z * p;
return res + (res >> 63 & p);
}
ll pow(ll a, ll x, ll res = 1) {
for(;x;x >>= 1, a = mul(a, a))
if(x & 1) res = mul(res, a);
return res;
}
bool checkprime(ll p) {
if(p == 1) return 0;
setmod(p);
ll d = __builtin_ctzll(p - 1), s = (p - 1) >> d;
for(ll a : {2, 3, 5, 7, 11, 13, 82, 373}) {
if(a % p == 0)
continue;
ll x = pow(a, s), y;
for(int i = 0;i < d;++i, x = y) {
y = mul(x, x);
if(y == 1 && x != 1 && x != p - 1)
return 0;
}
if(x != 1) return 0;
}
return 1;
}
ll rho(ll n) {
if(!(n & 1)) return 2;
static std::mt19937_64 gen((size_t)"hehezhou");
ll c = gen() % (n - 1) + 1, y = gen() % (n - 1) + 1;
auto f = [&](ll o) {
o = mul(o, o) + c;
return o >= n ? o - n : o;
};
setmod(p);
for(int l = 1;;l <<= 1) {
ll x = y, g = 1;
for(int i = 0;i < l;++i) y = f(y);
const int d = 512;
for(int i = 0;i < l;i += d) {
ll sy = y;
for(int j = 0;j < std::min(d, l - i);++j) {
y = f(y), g = mul(g, (y - x + n));
}
g = std::__gcd(n, g);
if(g == 1)
continue;
if(g == n)
for(g = 1, y = sy;g == 1;)
y = f(y), g = std::__gcd(n, y - x + n);
return g;
}
}
}
std::vector<ll> factor(ll x) {
std::queue<ll> q; q.push(x);
std::vector<ll> res;
for(;q.size();) {
ll x = q.front(); q.pop();
if(x == 1) continue;
if(checkprime(x)) {
res.push_back(x);
continue;
}
ll y = rho(x);
q.push(y), q.push(x / y);
}
sort(res.begin(), res.end());
return res;
}
struct cp {
ll x, y;
cp(ll x = 0, ll y = 0) : x(x), y(y) {}
cp operator + (cs cp &b) { return cp(x + b.x, y + b.y); }
cp operator - (cs cp &b) { return cp(x - b.x, y - b.y); }
cp operator * (cs cp &b) { return cp(x * b.x - y * b.y, x * b.y + y * b.x); }
cp conj() { return cp(x, -y); }
i128 norm() { return (i128) x * x + (i128) y * y; }
bool operator < (cs cp & a) cs { return x < a.x || (x == a.x && y < a.y); }
};
mt19937_64 rnd(223);
ll R(f64 x) { return round(x); }
cp operator / (cs cp & a, cs cp & b) {
complex <f64> A(a.x, a.y), B(b.x, b.y), C = A / B;
// cout << C.real() << ' ' << C.imag() << endl;
return cp(R(C.real()), R(C.imag()));
}
cp mod(cp a, cp b) {
cp c = a / b;
return a - b * c;
}
cp gcd(cp a, cp b) {
if(b.norm() == 0) return a;
cp c = mod(a, b);
// cout << (ll) a.norm() << ' ' << (ll) b.norm() << ' ' << (ll) c.norm() << ' ' << a.x << ' ' << a.y << ' ' << b.x << ' ' <<b.y << ' ' << c.x << ' ' << c.y << endl;
assert(c.norm() < b.norm());
return gcd(b, c);
}
ll ksm(ll a, ll b, ll p) {
ll c = 1;
for(; b; b >>= 1, a = (i128) a * a % p)
if(b & 1) c = (i128) c * a % p;
return c;
}
cp get(ll p) {
cp c(1, 0);
while(1) {
ll x = rnd() % p;
ll y = ksm(x, (p - 1) / 4, p);
if(((i128) y * y + 1) % p == 0) {
c = cp(y, 1); break;
}
}
c = gcd(c, cp(p, 0));
return c;
}
ll calc(ll x, ll y, ll n, ll m) {
ll d = __gcd(x, y);
if((i128) d * m % n) return 0;
i128 all = (x + (i128) y * m / n) * (y + (i128) x * m / n);
// cout << (ll) all << endl;
all -= ((i128) x * m / n - y) * ((i128) y * m / n - x);
// cout << (ll) all << endl;
assert(all % 2 == 0);
i128 e = x + y - d;
// cout << (ll) e << endl;
e = e + (i128) e * m / n;
all = all / 2 - e;
// cout << x << ' ' << y << ' ' << n<< ' ' << m << ' ' << (ll)all % Mod << endl;
return all % Mod;
}
void Main() {
ll n, m;
cin >> n >> m;
if(n > m) swap(n, m);
vector <ll> pr = factor (n);
map <ll, int> mp;
for(ll x : pr) ++ mp[x];
vector <cp> ans = {{1, 0}, {0, 1}};
for(auto [x, y] : mp) {
// cout << x << ' ' << y << endl;
if(x % 4 == 1) {
cp ab = get(x), ba = ab.conj();
vector <cp> nxt;
for(int i = 0; i <= y * 2; i++) {
cp cur(1, 0);
for(int j = 0; j < i; j++)
cur = cur * ab;
for(int j = i; j < y * 2; j++)
cur = cur * ba;
// cout << "FFFFGGGG " << cur.x << ' ' << cur.y << endl;
for(auto c : ans)
nxt.pb(c * cur);
}
swap(nxt, ans);
} else {
for(int t = 0; t < y; t++)
for(auto & c : ans)
c = c * x;
}
}
ll sum = (i128) n * m % Mod;
set <cp> st;
for(auto c : ans) {
ll x = c.x, y = c.y;
x = abs(x), y = abs(y), st.insert(cp(x, y));
}
for(auto c : st) {
ll x = c.x, y = c.y;
x = abs(x), y = abs(y);
// cout << "Case " << x << ' ' << y << endl;
if(x && y) sum = (sum + calc(x, y, n, m)) % Mod;
}
if(n != m) sum = sum * 2 % Mod;
cout << sum << '\n';
}
int main() {
#ifdef zqj
freopen("1.in", "r", stdin);
#endif
cin >> T;
while(T--) Main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
input:
5 5 5 2 3 5 10 2197525579 1145141 91 65
output:
51 12 228 438744975 34722
result:
ok 5 number(s): "51 12 228 438744975 34722"
Test #2:
score: 0
Accepted
time: 15ms
memory: 3572kb
input:
10000 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 6...
output:
1 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 1...
result:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 31ms
memory: 3620kb
input:
10000 101 101 102 102 103 103 104 104 105 105 106 106 107 107 108 108 109 109 110 110 111 111 112 112 113 113 114 114 115 115 116 116 117 117 118 118 119 119 120 120 121 121 122 122 123 123 124 124 125 125 126 126 127 127 128 128 129 129 130 130 131 131 132 132 133 133 134 134 135 135 136 136 137 13...
output:
30131 30684 10609 31936 32571 33132 11449 11664 35043 35772 36411 12544 37803 12996 39123 39728 40491 13924 41867 42624 14641 44092 44811 15376 107535 15876 16129 16384 16641 149508 17161 17424 17689 17956 54027 54784 55539 19044 19321 58128 19881 20164 60643 20736 186425 63132 21609 64976 65843 111...
result:
ok 10000 numbers
Test #4:
score: 0
Accepted
time: 42ms
memory: 3784kb
input:
10000 125987 264237 288891 590429 244358 321145 930851 89174 529670 363571 728747 543238 720391 188689 702144 813561 628383 660056 781508 605777 759705 485733 534730 812292 937524 788519 451996 10588 483682 267682 461493 65270 619145 355885 963015 800644 217669 264757 640439 685387 674020 853944 914...
output:
696726540 737924105 224336399 306851550 821227235 158353643 337250782 482409736 993125906 503496788 325000663 241215210 111087119 587268119 400642821 348635040 386306434 775082288 460682271 440343499 174930751 996502167 422683770 659600287 86069361 209009279 507101627 584541018 913347336 675941934 6...
result:
ok 10000 numbers
Test #5:
score: 0
Accepted
time: 71ms
memory: 3656kb
input:
10000 702209411 496813081 673102149 561219907 730593611 814024114 812959730 314305867 469496529 350635050 699021890 342102981 815487777 787982418 857896659 526518374 421876106 438907614 902179526 449645826 783856158 865633510 238642240 774653971 962475573 467098727 196513513 561435449 333165290 9515...
output:
351178772 289556674 72105570 346798714 479068610 455391821 529721669 67036253 120233075 412824751 891775570 274027105 488358696 989777769 632468413 125288048 441195818 825225634 103088806 867822995 27002557 219310127 103910026 974323804 115998163 107479105 755107560 729371131 986823050 899531 151617...
result:
ok 10000 numbers
Test #6:
score: 0
Accepted
time: 68ms
memory: 3636kb
input:
10000 269845965585325539 410993175365329221 287854792412106895 411389931291882089 384766635564718673 573462151358502890 390854186837699009 580380201657489855 909593130690430685 146053862632939232 618473335373282330 859258565398630021 923379609753287868 721664568079866982 791491023603966291 520180074...
output:
272311181 736851553 279132851 917767580 48830627 701895046 50331 338536426 773927550 176573225 16598281 809600629 744451345 136823092 368912797 654495936 400559921 818603087 393670704 641716193 778469045 987482183 923928038 958501248 49862537 961647563 730415377 836947329 3578939 729903478 939893168...
result:
ok 10000 numbers
Test #7:
score: 0
Accepted
time: 8ms
memory: 3764kb
input:
10000 5068651688 2945786612 1148017871 1617060749 1040331078 367483057 1968945278 1542443693 2805708197 2805708197 719944743 626392657 1053532371 1053532371 809997052 3813374244 1229288006 983636222 336026731 336026731 272965248 1226174235 898797134 684828285 1304524709 1075947495 1 1 1 1 1 1 1 1 1 ...
output:
827446201 436955984 462733196 566072075 379696248 430525451 130826981 232679692 232913626 135625116 705635665 445045646 53174892 1 1 1 1 1 1 6 1 1 1 8 1 16 1 1 1 1 1 16 1 1 1 4 1 1 9 1 9 1 1 24 1 4 4 4 1 1 1 1 1 1 110 1 1 16 51 1 24 4 1 1 1 16 1 4 1508 1 1 4 12 108 1 12 1 1 1 1 1 6 9 1 36 36 1 1 1 1...
result:
ok 10000 numbers
Test #8:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
2 499999999999999931 499999999999999931 499999999999999921 499999999999999921
output:
362029648 591642619
result:
ok 2 number(s): "362029648 591642619"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
2 499999999999999931 499999999999999927 499999999999999927 499999999999999921
output:
854509315 681512266
result:
ok 2 number(s): "854509315 681512266"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
4 46856248255981 46856248255981 46856248255981 4840261 9680521 46856248255981 3045656136638765 3982781101758385
output:
838761607 649440823 335968078 260413871
result:
ok 4 number(s): "838761607 649440823 335968078 260413871"
Test #11:
score: 0
Accepted
time: 1ms
memory: 3872kb
input:
1 994651672331116800 994651672331116800
output:
486528138
result:
ok 1 number(s): "486528138"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
1 994651672331116800 918984210614870400
output:
497975135
result:
ok 1 number(s): "497975135"
Test #13:
score: 0
Accepted
time: 2ms
memory: 3636kb
input:
1 998244359987710471 998244361984199177
output:
851348554
result:
ok 1 number(s): "851348554"
Test #14:
score: 0
Accepted
time: 1ms
memory: 3628kb
input:
1 993244859952713971 993244859952713971
output:
631469821
result:
ok 1 number(s): "631469821"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
1 999997299770000621 999990099770002277
output:
128870106
result:
ok 1 number(s): "128870106"
Test #16:
score: 0
Accepted
time: 216ms
memory: 31712kb
input:
1 943738496366824925 921409040647481425
output:
254383460
result:
ok 1 number(s): "254383460"
Test #17:
score: 0
Accepted
time: 197ms
memory: 30924kb
input:
1 739451687713433825 685180004211530425
output:
421381281
result:
ok 1 number(s): "421381281"
Test #18:
score: 0
Accepted
time: 206ms
memory: 32488kb
input:
1 739451687713433825 739451687713433825
output:
965695191
result:
ok 1 number(s): "965695191"
Test #19:
score: 0
Accepted
time: 3ms
memory: 4020kb
input:
1 81410065665563880 81410065665563880
output:
174651048
result:
ok 1 number(s): "174651048"
Test #20:
score: 0
Accepted
time: 3ms
memory: 3952kb
input:
1 81410065665563880 375476088048816300
output:
591446578
result:
ok 1 number(s): "591446578"
Test #21:
score: 0
Accepted
time: 18ms
memory: 6272kb
input:
1 938515983950855310 375476088048816300
output:
613367980
result:
ok 1 number(s): "613367980"
Test #22:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
1 916393354063333407 916393354063333407
output:
167277509
result:
ok 1 number(s): "167277509"
Test #23:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
1 510510000006636630 391391000005088083
output:
527991263
result:
ok 1 number(s): "527991263"
Test #24:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
1 510510000006636630 510510000006636630
output:
892977407
result:
ok 1 number(s): "892977407"
Test #25:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
2 255255000003318315 255255000003318315 255255000003318315 187187000002433431
output:
15591462 225024415
result:
ok 2 number(s): "15591462 225024415"
Test #26:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
1 692172064958990436 631313342544221733
output:
560564029
result:
ok 1 number(s): "560564029"