QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#624584#6694. Math ProblemnihaoakekeTL 0ms0kbC++141.1kb2024-10-09 16:10:352024-10-09 16:10:35

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 16:10:35]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-09 16:10:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve(){

    int T;
    cin>>T;
    while(T--) {
        int n, k, m, a, b;
        cin >> n >> k >> m >> a >> b;

        if (k == 1) {
            if (n % m == 0) {
                cout << "0" << "\n";
            } else {
                cout << "-1" << "\n";
            }
            return;
        }
        int ans = 1000000000000;
        int cnt = 0;
        while (cnt*b<1000000000000) {
            int mod = n - n/m*m;
            int cost = cnt * b;
            int flag = 1;
            int range = 1;
            if (mod == 0) flag = 0;
            while (flag) {
                cost += a;
                mod *= k;
                mod %= m;
                range *= k;
                if (mod == 0 || mod + range - 1 >= m) flag = 0;
            }
            ans = min(ans, cost);
            n = n / k;
            cnt++;
        }
        cout << ans << "\n";
    }
}
signed main()
{
    int T;
    cin>>T;
    while(T--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

4
101 4 207 3 5
8 3 16 100 1
114 514 19 19 810
1 1 3 1 1

output:


result: