QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#708070#5301. Modulo Ruins the Legendlonelywolf#TL 0ms3648kbC++201.2kb2024-11-03 19:19:302024-11-03 19:19:31

Judging History

你现在查看的是最新测评结果

  • [2024-11-03 19:19:31]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3648kb
  • [2024-11-03 19:19:30]
  • 提交

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;
}  
  

Details

Tip: Click on the bar to expand more detailed information

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

output:


result: