結果

問題 No.915 Plus Or Multiple Operation
ユーザー chocopuuchocopuu
提出日時 2019-10-25 22:07:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,013 bytes
コンパイル時間 1,568 ms
コンパイル使用メモリ 169,036 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-08-07 00:22:49
合計ジャッジ時間 2,240 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,508 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,n) for(int i = 0;i < (int)(n);i++)
#define RREP(i,n) for(int i = (int)n-1;i >= 0;i--)
#define FOR(i,s,n) for(int i = s;i < (int)n;i++)
#define RFOR(i,s,n) for(int i = (int)n-1;i >= s;i--)
#define ALL(a) a.begin(),a.end()
template<class T>inline bool CHMAX(T&a,T b){if(a<b){a = b;return true;}return false;}
template<class T>inline bool CHMIN(T&a,T b){if(a>b){a = b;return true;}return false;}
constexpr long long INF = 1e18;

signed main(){
	int Q;
	cin>>Q;
	vector<int>ans;
	while(Q--){
		int A,B,C;
		cin>>A>>B>>C;
		if(C==1){
			ans.push_back(-1);
			continue;
		}
		int t = 0;
		vector<int>m;
		while(A){
			m.push_back(A%C);
			A/=C;
		}
		reverse(ALL(m));
		REP(i,m.size()){
			if(i+1==m.size()){
				if(i)t++;
				if(m[i])t++;
			}else if(m[i]&&m[i+1]&&m[i]*C+m[i+1]<=2*(C-1)){
				if(i)t+=2;
				i++;
				t+=2;
			}else{
				if(i)t++;
				if(m[i])t++;
			}
		}
		ans.push_back(t*B);
	}
	for(auto &e:ans)cout << e << endl;
}
0