結果

問題 No.915 Plus Or Multiple Operation
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-10-25 22:27:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,298 bytes
コンパイル時間 1,761 ms
コンパイル使用メモリ 169,224 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 18:44:15
合計ジャッジ時間 2,026 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr ll MOD = 1000000007;
constexpr ll INF = 1LL << 60;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

void solve() {
    ll a, b, c, ans = INF;
    cin >> a >> b >> c;

    if(c == 1) {
        cout << c * b << endl;
        return;
    }

    ll c_pow = 1LL, ci = 0LL;
    for(ci=0;c_pow*c<=a;++ci) {
        c_pow = c_pow * c;
    }

    vector<ll> digits;
    ll a_copy = a, c_pow_copy = c_pow;
    for(ll i=0;i<=ci;++i) {
        digits.push_back(a_copy/c_pow_copy);
        a_copy -= a_copy / c_pow_copy * c_pow_copy;
        c_pow_copy /= c;
    }
    
    vector<vector<ll>> dp(digits.size()+1, vector<ll>(2, INF));
    dp[0][1] = -1;
    for(int i=0;i<digits.size();++i) {
        dp[i+1][0] = dp[i][1];
        dp[i+1][1] = dp[i][1] + (digits[i]?1:0) + 1;
        if(i) {
            dp[i+1][1] = min(dp[i+1][1], dp[i][0] + (digits[i-1] * c + digits[i] + c - 2) / (c - 1) + 1);
        }
    }
    ans = dp[digits.size()][1];

    ans *= b;
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int q;
    cin >> q;
    while(q--) {
        solve();
    }
}
0