QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#120524 | #5738. Square Sum | UrgantTeam | RE | 38ms | 4308kb | C++23 | 4.3kb | 2023-07-06 20:31:27 | 2023-07-06 20:31:28 |
Judging History
answer
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vl = vector<ll>;
const int M = 1000000009;
int prod(int a, int b, int m) {
return ll(a) * b % m;
}
int binpow(int a, int b, int m) {
int ans = 1;
while (b) {
if (b & 1)
ans = prod(ans, a, m);
a = prod(a, a, m);
b >>= 1;
}
return ans;
}
ll intpow(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1)
ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
int dif(int a, int b, int m) {
if ((a -= b) < 0) a += m;
return a;
}
int sum(int a, int b, int m) {
if ((a += b) >= m) a -= m;
return a;
}
int inv(int a, int m) {
return binpow(a, m - 2, m);
}
vpii factor(int n) {
vpii ans;
for (int i = 2; i * i <= n; ++i)
if (n % i == 0) {
ans.emplace_back(i, 1); n /= i;
while (n % i == 0) {
++ans.back().second;
n /= i;
}
}
if (n >= 2) ans.emplace_back(n, 1);
return ans;
}
int rev(int a, int m) {
if (a == 1) return 1;
return (1 - ll(rev(m % a, a)) * m) / a + m;
}
int crt(const int x, const int y, const int mx, const int my) {
int m = mx * my;
return sum(prod(prod(x, my, m), rev(my % mx, mx), m), prod(prod(y, mx, m), rev(mx % my, my), m), m);
}
int legendre(int a, int p) {
return binpow(a, (p - 1) / 2, p);
}
ll fqa(const int pa, const pii& f, const int z) {
const int p = f.first;
const int t = f.second;
int k = 0;
int resa = z;
if (resa == 0) k = t;
else
while (resa % p == 0) {
resa /= p;
++k;
}
resa %= p;
if (p == 2) {
if (t == 1) {
if (z == 0) return 1;
if (z == 1) return 2;
}
else if (t == 2) {
if (z == 0) return 0;
if (z == 1) return 8;
if (z == 2) return 4;
if (z == 3) return 0;
}
int arrans[] = { 0,4,4,0,0,4,0,0 };
int y = z % 8;
return arrans[z] * intpow(2, t - 1);
}
else if (p % 4 == 3) {
if (k >= 1) return 0;
if (legendre(z, p) == 1) return (p + 1) * intpow(p, t - 1);
return (p + 1) * intpow(p, t - 1);
}
else {
if (k >= 1) return (p - 1) * 2 * intpow(p, t - 1);
if (legendre(z, p) == 1) return (p - 1) * intpow(p, t - 1);
return (p - 1) * intpow(p, t - 1);
}
}
ll find_ans(const int pa, const pii& f, const int z) {
const int p = f.first;
const int t = f.second;
int k = 0;
int resa = z;
if (resa == 0) k = t;
else
while (resa % p == 0) {
resa /= p;
++k;
}
resa %= p;
ll ans = 0;
for (int i = 0; i <= t; ++i) {
ll curans = 0;
int g = min(t, 2 * i);
if (k < g) continue;
int pg = intpow(p, g);
if (g == t) {
curans = intpow(p, 2 * (t - i));
if (i < t) curans -= intpow(p, 2 * (t - i - 1));
} else {
ll pw = intpow(p, g);
curans = pw * fqa(pa / pg, { p, t - g }, (z / pg) % (pa / pg));
}
ans += curans;
}
return ans;
}
ll find_ans(const vi& pa, const vpii& f, const int z) {
ll ans = 1;
const int F = f.size();
for (int i = 0; i < F; ++i)
ans *= find_ans(pa[i], f[i], z % pa[i]);
return ans;
}
vl find_ans(int m, const vi& z) {
vpii f = factor(m);
const int F = f.size();
const int n = z.size();
vi pa(F);
for (int i = 0; i < F; ++i)
pa[i] = intpow(f[i].first, f[i].second);
vl ans(n);
for (int i = 0; i < n; ++i)
ans[i] = find_ans(pa, f, z[i]);
return ans;
}
bool solve_test() {
int m, n;
if (!(cin >> m >> n)) return false;
vi z(n);
for (int i = 0; i < n; ++i)
cin >> z[i];
vl ans = find_ans(m, z);
for (int i = 0; i < n; ++i)
cout << ans[i] << " \n"[i == n - 1];
return true;
}
ll find_slow_ans(int m, int z) {
ll ans = 0;
for (ll x = 0; x < m; ++x)
for (ll y = 0; y < m; ++y)
if ((x * x + y * y) % m == z)
++ans;
return ans;
}
void stress() {
for (int m = 1; m <= 10; ++m) {
for (int i = 0; i < m; ++i) {
vi v = { i };
ll fast_ans = find_ans(m, v)[0];
ll slow_ans = find_slow_ans(m, v[0]);
if (fast_ans != slow_ans) {
cout << m << ' ' << v[0] << '\n' << fast_ans << ' ' << slow_ans << '\n';
return;
}
}
}
}
void solve_tests() {
while (solve_test());
}
int main() {
#ifdef ONPC
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve_tests();
stress();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3396kb
input:
3 3 0 1 2
output:
1 4 4
result:
ok 3 number(s): "1 4 4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3392kb
input:
4 4 0 1 2 3
output:
4 8 4 0
result:
ok 4 number(s): "4 8 4 0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
5 1 3
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 24ms
memory: 4308kb
input:
735134400 100000 4 4 1 2 3 4 4 4 5 4 3 4 1 1 1 1 2 0 1 4 4 5 4 1 0 0 1 3 0 4 0 5 3 0 3 0 5 4 0 0 3 2 5 3 2 4 3 4 2 1 3 3 2 2 2 3 1 0 1 2 3 4 3 5 4 4 0 1 5 2 2 3 3 2 4 3 5 5 1 3 1 1 4 3 4 3 4 5 2 4 1 3 2 0 5 0 0 5 5 1 2 0 3 4 0 4 1 0 1 4 5 5 3 1 3 0 3 5 0 4 2 0 4 0 0 0 4 0 2 2 2 4 5 3 0 2 0 4 1 4 1 2...
output:
1698693120 1698693120 1698693120 1698693120 0 1698693120 1698693120 1698693120 3397386240 1698693120 0 1698693120 1698693120 1698693120 1698693120 1698693120 1698693120 30888000 1698693120 1698693120 1698693120 3397386240 1698693120 1698693120 30888000 30888000 1698693120 0 30888000 1698693120 30888...
result:
ok 100000 numbers
Test #5:
score: 0
Accepted
time: 23ms
memory: 4304kb
input:
735134400 100000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 30888000 308...
result:
ok 100000 numbers
Test #6:
score: 0
Accepted
time: 21ms
memory: 4308kb
input:
536870912 100000 1 0 2 4 3 1 3 5 5 1 0 2 1 1 1 0 4 2 4 1 3 2 1 4 3 2 1 2 4 0 4 2 2 1 5 4 1 5 3 0 2 3 4 4 3 4 2 1 2 5 2 1 5 3 1 3 3 0 0 4 3 4 0 4 2 0 5 2 0 3 1 0 0 1 5 5 5 5 5 1 5 3 1 2 4 1 3 3 2 0 2 0 5 1 0 2 1 2 5 2 5 4 4 3 0 3 3 5 4 3 3 2 0 2 1 2 0 1 4 1 1 4 2 4 1 2 4 4 4 2 3 4 5 5 5 4 3 5 5 2 1 5...
output:
1073741824 536870912 1073741824 1073741824 0 1073741824 0 1073741824 1073741824 1073741824 536870912 1073741824 1073741824 1073741824 1073741824 536870912 1073741824 1073741824 1073741824 1073741824 0 1073741824 1073741824 1073741824 0 1073741824 1073741824 1073741824 1073741824 536870912 1073741824...
result:
ok 100000 numbers
Test #7:
score: 0
Accepted
time: 38ms
memory: 4296kb
input:
536870912 100000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 536870912 ...
result:
ok 100000 numbers
Test #8:
score: -100
Runtime Error
input:
536870912 100000 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268435456 268...