結果

問題 No.915 Plus Or Multiple Operation
コンテスト
ユーザー tkmst201
提出日時 2019-10-25 22:07:04
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,043 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,381 ms
コンパイル使用メモリ 178,044 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-07 11:44:38
合計ジャッジ時間 2,443 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//



ll solve(ll A, ll B, ll C) {
	if (C == 1) return -1;
	ll cnt = 0;
	bool f = false;
	
	while (A > 0) {
		if (A > C && A <= C*C && A <= 2*(C-1)) {
			f = true;
		}
		if (A % C > 0) {
			A -= A % C; cnt++;
		}
		if (A > 0) { A /= C; cnt++; }
	}
	if (f) cnt--;
	ll res = cnt * B;
	return res;
}

int main() {
	int Q;
	cin >> Q;
	while (Q--) {
		int A, B, C;
		scanf("%d %d %d", &A, &B, &C);
		cout << solve(A, B, C) << endl;
	}
	return 0;
}
0