QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#619021 | #6694. Math Problem | yqr | WA | 32ms | 4320kb | C++14 | 1.7kb | 2024-10-07 12:35:43 | 2024-10-07 12:35:45 |
Judging History
answer
#include<stdio.h>
#include<ctype.h>
#include<vector>
#include<math.h>
namespace IO {
constexpr int bufsize = 230005;
char buf[bufsize], *f1, *f2;
#define gtchar() (f1 == f2 && (f2 = buf + fread(f1 = buf, 1, bufsize, stdin)) == buf? EOF: *f1++)
template<typename t> void read(t &ret)
{
int f = ret = 0;
char ch = gtchar();
while(!isdigit(ch)) f = ch == '-', ch = gtchar();
while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = gtchar();
if(f) ret = -ret;
}
#undef gtchar
template<typename t, typename ...T> void read(t &a, T &...b) {read(a), read(b...);}
}using IO::read;
typedef long long ll;
int T, k, m, inf;
ll n, A, B;
ll min(ll a, ll b) {return a > b? b: a;}
ll qpow(ll a, int b)
{
a %= m;
ll ret = 1;
while(b)
{
if(b & 1) ret = ret * a % m;
a = a * a % m;
b >>= 1;
}
return ret;
}
bool check(ll n, int p)
{
ll goal = n % m * qpow(k, p) % m;
goal = goal? m - goal: 0;
return !goal || log(goal) < p * log(k);
}
int solve(ll x)
{
int l = 0, r = inf;
while(l < r)
{
int mid = l + r >> 1;
if(check(x, mid)) r = mid;
else l = mid + 1;
}
return l;
}
int main()
{
// freopen("transform.in", "r", stdin);
// freopen("transform.out", "w", stdout);
read(T);
while(T--)
{
read(n, k, m, A, B);
if(k == 1)
{
puts(n == m? "0": "-1");
continue;
}
inf = ceill(log(m) / log(k)) + 5;
std::vector<ll> tmp;
while(n) tmp.push_back(n), n /= k;
ll ans = 1e18;
tmp.push_back(0);
bool f = false;
for(int i = 0; i < tmp.size(); i++)
{
int t = solve(tmp[i]);
if(t != inf) ans = min(ans, i * B + t * A), f = true;
}
printf("%lld\n", f? ans: -1);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3992kb
input:
4 101 4 207 3 5 8 3 16 100 1 114 514 19 19 810 1 1 3 1 1
output:
11 2 0 -1
result:
ok 4 number(s): "11 2 0 -1"
Test #2:
score: -100
Wrong Answer
time: 32ms
memory: 4320kb
input:
100000 9 5 7 7674 78731 4 3 4 58482 93736 1 4 3 42396 22960 6 2 2 4534 73466 5 7 7 56203 19376 1 7 10 77129 84094 8 3 3 72793 89258 10 10 3 94847 42455 7 4 7 79273 90760 2 7 3 78496 99140 4 4 9 47018 14651 3 7 8 60936 4453 8 6 4 57267 6293 8 7 3 81697 99664 2 10 10 3935 30951 8 9 7 91391 70670 5 8 8...
output:
7674 0 22960 0 19376 77129 72793 84910 0 78496 29302 4453 0 81697 3935 70670 36522 21244 0 0 0 100934 30063 0 57852 31894 72016 6193 9486 2516 27536 0 7306 73625 11302 13802 41343 50014 58015 38743 65165 38963 26747 0 42044 45733 63574 69321 34196 1674 27200 8130 0 46609 53621 11696 7808 4630 10051 ...
result:
wrong answer 3696th numbers differ - expected: '0', found: '-1'