結果

問題 No.3462 Buttons
コンテスト
ユーザー startcpp
提出日時 2026-02-28 16:24:52
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 777 ms / 2,000 ms
コード長 1,836 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,004 ms
コンパイル使用メモリ 81,076 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-02-28 16:25:24
合計ジャッジ時間 14,126 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <string>
#include <atcoder/modint>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;
using namespace atcoder;
using mint = modint998244353;

const int INF = (1LL << 62);

int naive1(int K, int a, int b) {
	int ret = -INF, i, j;
	
	rep(i, (1 << K)) {
		int score = 0;
		rep(j, K) {
			if ((i >> j) % 2 == 0) {
				score += a;
			}
			else {
				score *= b;
			}
		}
		ret = max(ret, score);
	}
	return ret;
}

int naive2(int K, int a, int b) {
	int ret = 0;
	for (int i = 0; i <= K; i++) {
		int score = 0, j;
		rep(j, K) {
			if (j < i) score += a;
			else score *= b;
		}
		ret = max(ret, score);
	}
	return ret;
}

mint solve(int K, int a, int b) {
	if (b == 0 || b == 1) {
		if (a > 0) return K * a;
		return 0;
	}
	if (b >= 2) {
		if (a > 0) return a * mint(b).pow(K - 1);
		return 0;
	}
	if (b == -1) {
		if (a > 0) return K * a;
		return (K - 1) * a * (-1);
	}
	if (b <= -2 && K % 2 == 0) {
		if (a > 0) return 2 * a * mint(b).pow(K - 2);
		return a * mint(b).pow(K - 1);
	}
	if (b <= -2 && K == 1) {
		if (a > 0) return a;
		return 0;
	}

	if (a > 0) return a * mint(b).pow(K - 1);
	return 2 * a * mint(b).pow(K - 2);
}

void test() {
	int mod = 998244353;
	for (int K = 1; K <= 10; K++) {
		for (int a = -50; a <= 50; a++) {
			for (int b = -50; b <= 50; b++) {
				int res1 = naive2(K, a, b);
				int res2 = solve(K, a, b).val();
				if (res1 % mod != res2) {
					cout << "WA" << endl;
					cout << "K: " << K << ", a: " << a << ", b: " << b << endl;
					cout << "naive: " << res1 << ", naiveMod: " << res1 % mod << endl;
					cout << "solve: " << res2 << endl;
					return;
				}
			}
		}
	}

	cout << "AC" << endl;
}

signed main() {
	int T;
	cin >> T;
	while (T--) {
		int a, b, K;
		cin >> a >> b >> K;
		cout << solve(K, a, b).val() << endl;
	}
	return 0;
}
0