結果

問題 No.1977 Extracting at Constant Intervals
ユーザー magstamagsta
提出日時 2022-06-10 12:44:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 2,384 bytes
コンパイル時間 6,626 ms
コンパイル使用メモリ 294,864 KB
実行使用メモリ 11,076 KB
最終ジャッジ日時 2024-04-17 15:39:20
合計ジャッジ時間 8,436 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 9 ms
5,248 KB
testcase_03 AC 32 ms
6,992 KB
testcase_04 AC 9 ms
5,376 KB
testcase_05 AC 51 ms
8,536 KB
testcase_06 AC 22 ms
6,768 KB
testcase_07 AC 29 ms
8,060 KB
testcase_08 AC 29 ms
6,944 KB
testcase_09 AC 14 ms
5,376 KB
testcase_10 AC 35 ms
6,212 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 8 ms
5,376 KB
testcase_13 AC 41 ms
9,348 KB
testcase_14 AC 73 ms
11,076 KB
testcase_15 AC 26 ms
6,956 KB
testcase_16 AC 57 ms
10,512 KB
testcase_17 AC 52 ms
8,524 KB
testcase_18 AC 23 ms
5,528 KB
testcase_19 AC 15 ms
5,376 KB
testcase_20 AC 55 ms
8,484 KB
testcase_21 AC 49 ms
6,224 KB
testcase_22 AC 61 ms
10,512 KB
testcase_23 AC 10 ms
5,376 KB
testcase_24 AC 44 ms
6,464 KB
testcase_25 AC 5 ms
5,376 KB
testcase_26 AC 8 ms
5,376 KB
testcase_27 AC 28 ms
5,828 KB
testcase_28 AC 12 ms
5,376 KB
testcase_29 AC 32 ms
7,100 KB
testcase_30 AC 56 ms
8,032 KB
testcase_31 AC 18 ms
6,668 KB
testcase_32 AC 30 ms
6,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include "testlib.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() {
    registerValidation();
	long long N, M, L;
	N = inf.readInt(1, 100000, "N");
	inf.readSpace();
	M = inf.readInt(1, 1000000000, "M");
	inf.readSpace();
	L = inf.readLong(1LL, N * M, "L");
	inf.readEoln();
	vector<long long> A(N);
	A[0] = inf.readInt(-10000, 10000, "A");
	for (int i = 1; i < N; i++) {
	    inf.readSpace();
	    A[i] = inf.readInt(-10000, 10000, "A");
	}
	inf.readEoln();
	inf.readEof();

	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