QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#215533 | #4801. The Pool | ucup-team1209 | RE | 55ms | 3544kb | C++14 | 5.1kb | 2023-10-15 11:32:53 | 2023-10-15 11:32:54 |
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 x * x + 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: 3424kb
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: 3424kb
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: 34ms
memory: 3544kb
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: 47ms
memory: 3528kb
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: 55ms
memory: 3488kb
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: -100
Runtime Error
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