結果

問題 No.1977 Extracting at Constant Intervals
ユーザー magstamagsta
提出日時 2022-05-14 20:18:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 2,063 bytes
コンパイル時間 1,744 ms
コンパイル使用メモリ 169,464 KB
実行使用メモリ 8,640 KB
最終ジャッジ日時 2024-04-17 15:38:41
合計ジャッジ時間 4,047 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 11 ms
5,376 KB
testcase_03 AC 36 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 56 ms
5,464 KB
testcase_06 AC 23 ms
5,488 KB
testcase_07 AC 31 ms
6,136 KB
testcase_08 AC 34 ms
5,780 KB
testcase_09 AC 15 ms
5,376 KB
testcase_10 AC 40 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 9 ms
5,376 KB
testcase_13 AC 44 ms
7,424 KB
testcase_14 AC 81 ms
8,640 KB
testcase_15 AC 29 ms
5,796 KB
testcase_16 AC 65 ms
8,204 KB
testcase_17 AC 61 ms
5,376 KB
testcase_18 AC 23 ms
5,376 KB
testcase_19 AC 17 ms
5,376 KB
testcase_20 AC 57 ms
6,420 KB
testcase_21 AC 49 ms
5,376 KB
testcase_22 AC 65 ms
8,204 KB
testcase_23 AC 10 ms
5,376 KB
testcase_24 AC 48 ms
5,376 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 9 ms
5,376 KB
testcase_27 AC 29 ms
5,376 KB
testcase_28 AC 13 ms
5,376 KB
testcase_29 AC 33 ms
5,940 KB
testcase_30 AC 61 ms
6,108 KB
testcase_31 AC 20 ms
5,528 KB
testcase_32 AC 33 ms
5,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long gcd(long long x, long long y) {
	if (x < y) swap(x, y);
	while (y > 0) {
		long long r = x % y;
		x = y;
		y = r;
	}
	return x;
}

long long lcm(long long x, long long y) {
	return x * y / gcd(x, y);
}

long long solve(vector<long long> A, long long M, long long L) {
	long long N = A.size();

	vector<long long> rui(2 * N);
	rui[0] = A[0];
	for (int i = 1; i < 2 * N; i++) {
		rui[i] = rui[i - 1] + A[(i * (L % N)) % N];
	}

	if (L <= N) {
		long long sum = 0;
		for (int i = 0; i < N; i++) {
			sum += A[i];
		}
		vector<long long> ans(L, (M / L) * sum);
		M %= L;
		for (int i = 0; i < N; i++) {
			if ((i * L) % N < L && N * M - 1 - (i * L) % N >= 0) {
				ans[(i * L) % N] += rui[i + (N * M - 1 - (i * L) % N) / L] - rui[i] + A[(i * L) % N];
			}
		}
		long long max_ = -1000000000000000000;
		for (int i = 0; i < L; i++) {
			max_ = max(max_, ans[i]);
		}
		return max_;
	}

	else {
		long long sum = 0;
		for (int i = 0; i < N; i++) {
			sum += A[i];
		}
		vector<long long> ans(N, (M / L) * sum), ans2(N, (M / L) * sum);
		M %= L;
		for (int i = 0; i < N; i++) {
			if (N * M - 1 - (i * (L % N)) % N >= 0) {
				ans[(i * (L % N)) % N] += rui[i + (N * M - 1 - (i * (L % N)) % N) / L] - rui[i] + A[(i * (L % N)) % N];
			}
			if (N * (M - (L - (i * (L % N)) % N - 1) / N) - 1 - (i * (L % N)) % N >= 0) {
				ans2[(i * (L % N)) % N] += rui[i + (N * (M - (L - (i * (L % N)) % N - 1) / N) - 1 - (i * (L % N)) % N) / L] - rui[i] + A[(i * (L % N)) % N];
			}
		}
		long long max_ = -1000000000000000000;
		for (int i = 0; i < N; i++) {
			max_ = max(max_, ans[i]);
			max_ = max(max_, ans2[i]);
		}
		return max_;
	}
}

int main() {
	long long N, M, L; cin >> N >> M >> L;
	vector<long long> A(N);
	for (int i = 0; i < N; i++) cin >> A[i];

	long long max_ = -1000000000000000000;
	for (int i = 0; i < gcd(N, L); i++) {
		vector<long long> B;
		for (int j = 0; j < N / gcd(N, L); j++) {
			B.push_back(A[j * gcd(N, L) + i]);
		}
		max_ = max(max_, solve(B, M, L / gcd(N, L)));
	}

	cout << max_ << endl;
}
0