結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 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 INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) { int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; }

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a)  begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

int Q;
int A, B, C;

int solve(int a, int c)
{
  vector<int> hoge;
  int ans = 0, tmp = 0;
  int cnt = 0;
  while(a) {
    if(a%c != 0) {
      ans++;
    }

    hoge.push_back(a%c);

    tmp++;
    a /= c;
  }

  reverse(begin(hoge), end(hoge));
  int kk = 0;
  if(hoge.size() >= 2 && hoge[0] == 1 && hoge[1] <= c-2 && 1 <= hoge[1]) kk = 1; 
  return (ans + tmp - 1 - kk) * B;
}

signed main() 
{
  cin >> Q;
  REP(q,Q) {
    cin >> A >> B >> C;
    if(C == 1) {
      cout << -1 << endl;
      continue;
    }
    int ans = INF_LL;
    ans = solve(A, C);
    cout << ans << endl;
  }

  return 0;
}
0