QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#708058 | #5301. Modulo Ruins the Legend | lonelywolf# | RE | 0ms | 3672kb | C++20 | 1.3kb | 2024-11-03 19:12:17 | 2024-11-03 19:12:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
int g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
bool liEu(int a, int b, int c, int& x, int& y) {
int d = exgcd(a, b, x, y);
if (c % d != 0)
return false;
int k = c / d;
x *= k;
y *= k;
return true;
}
int norm(int x, int m) {
while (x < 0) {
x += m;
}
if (x >= m) {
x %= m;
}
return x;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int a = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
a = (a + x) % m;
}
int b = n % m, c = (n + 1) * n / 2 % m;
int g = gcd(b, c);
g = gcd(g, m);
int t = m / g;
int ans = 1e18;
int k = 0;
int now = 0;
while (now < t) {
int sum = a + now * g;
if (ans > sum % m) {
ans = sum % m;
k = now;
}
if (ans == 0) {
break;
}
int u = (sum + m - 1) / m * m;
int add = (u - sum + g - 1) / g;
now += add;
}
int x, y;
liEu(b, c, k * g, x, y);
cout << ans << "\n";
cout << norm(x, m) << " " << norm(y, m) << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
6 24 1 1 4 5 1 4
output:
1 15 3
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
7 29 1 9 1 9 8 1 0
output:
0 0 0
result:
ok ok
Test #3:
score: -100
Runtime Error
input:
1 1 0