QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#708070 | #5301. Modulo Ruins the Legend | lonelywolf# | TL | 0ms | 3648kb | C++20 | 1.2kb | 2024-11-03 19:19:30 | 2024-11-03 19:19:31 |
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);
if (g == 0) {
cout << a << "\n";
cout << 0 << " " << 0 << "\n";
return 0;
}
g = gcd(g, m);
int t = m / g;
int k = 0;
int ans = 1e18;
for (int i = 0; i < t; i++) {
if (ans > (a + i * g) % m) {
k = i;
ans = (a + i * g) % m;
}
}
int x, y;
liEu(b, c, k * g, x, y);
cout << ans << "\n";
cout << norm(x, m) << " " << norm(y, m) << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
input:
6 24 1 1 4 5 1 4
output:
1 15 3
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
7 29 1 9 1 9 8 1 0
output:
0 0 0
result:
ok ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
1 1 0
output:
0 0 0
result:
ok ok
Test #4:
score: -100
Time Limit Exceeded
input:
1 1000000000 963837005