QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#619741 | #6694. Math Problem | highkj | TL | 923ms | 4044kb | C++11 | 2.0kb | 2024-10-07 15:13:50 | 2024-10-07 15:13:51 |
Judging History
answer
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define int __int128
#define fire signed
#define il inline
template<class T> il void print(T x) {
if(x<0) printf("-"),x=-x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
const int bufsize = 230005;
char buf[bufsize], *f1, *f2;
#define getchar() (f1 == f2 && (f2 = buf + fread(f1 = buf, 1, bufsize, stdin)) == buf? EOF: *f1++)
template<class T> il void in(T &x) {
x = 0; char ch = getchar();
int f = 1;
while (ch < '0' || ch > '9') {if(ch=='-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
x *= f;
}
int T=1;
int n,k,m,a,b,res;
__int128 qmi(__int128 x,int y) {
__int128 res=1;
while(y) {
if(y&1) res=res*x;
x=x*x;
y>>=1;
}return res;
}
bool check(int c,int n,__int128 m) {
__int128 now=(__int128)(qmi(k,c)*n);
m=(m+now-1)/m*m;
m-=now;
rep1(i,c-1,1) {
now=qmi(k,i);
m-=min(k-1,(int)(m/now))*now;
}
return (m<=k-1);
}
int cc(int k,int x,int r) {
r/=x;
int cnt=false;
while(r) r/=k,cnt++;
return cnt;
}
int gets(int x,int y) {
int l=1,r=max((int)8ll,cc(k,x,1000000000000000000)),res=INT_MAX;
rep(i,l,r) if(check(i,x,y)) res=min(res,i);
return res*a;
}
void solve() {
in(n),in(k),in(m),in(a),in(b);
if(n%m==0) {
cout<<"0\n";
return;
}else if(k==1) {
puts("-1");
return;
}
res=LONG_LONG_MAX;
int now=0;
while(n) {
res=min(res,now+gets(n,m));
n/=k;
now+=b;
if(n%m==0) res=min(res,now);
}
res=min(res,now);
print(res);
cout<<endl;
}
fire main() {
// freopen("transform.in","r",stdin);
// freopen("transform.out","w",stdout);
in(T);
while(T--) {
solve();
}
return false;
}
/*
1
2157584874927409 3499113 70686381 77518537 690462406
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3536kb
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: 0
Accepted
time: 923ms
memory: 3792kb
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:
ok 100000 numbers
Test #3:
score: 0
Accepted
time: 324ms
memory: 3756kb
input:
100000 72 56 61 725468539 908406897 78 7 86 485275009 977836982 25 57 29 104624183 334780226 48 74 44 805881518 26082346 87 26 31 224468594 59732782 55 29 99 697738785 876206855 96 85 8 998474252 490312988 11 22 7 298977007 759215221 43 72 22 785798936 836075792 16 16 29 304049814 445858780 86 24 36...
output:
725468539 970550018 104624183 26082346 119465564 1395477570 0 298977007 785798936 304049814 488620044 1112696878 150581202 929490259 258912618 156420265 767475208 623063452 158730933 211070701 576023224 525233 66950902 893398127 1538977472 552877592 365438928 274813702 347240596 413914126 411352478 ...
result:
ok 100000 numbers
Test #4:
score: 0
Accepted
time: 141ms
memory: 3752kb
input:
100000 151 859 413 113296245 501509373 721 797 951 199093180 857704079 864 743 424 944343390 211412511 537 561 510 160063507 371248812 67 101 1000 982073931 983924207 224 620 106 988167382 31695532 388 496 373 179680114 906477657 463 865 318 568083479 906936536 409 549 816 183391318 716459594 871 83...
output:
113296245 199093180 422825022 160063507 983924207 31695532 179680114 568083479 366782636 353991726 424999871 270298556 845830412 171200301 342928747 708513732 1071693358 495915588 91647563 423066800 629029877 532122044 903837970 923069546 30892184 342614734 344281675 92814148 8303547 371747405 58158...
result:
ok 100000 numbers
Test #5:
score: 0
Accepted
time: 92ms
memory: 4012kb
input:
100000 4630 2977 9157 920976948 319551315 2827 5918 444 915741308 428850012 1830 2194 1995 883906440 695946422 3495 431 7000 831459352 331879489 6334 6866 3180 554770772 999524518 4394 865 2622 462975016 317225012 7146 2059 5206 338704464 650075526 7269 9699 7065 508253316 136145466 3717 9832 5501 1...
output:
639102630 428850012 695946422 663758978 554770772 634450024 677408928 136145466 138804545 86246388 654932853 233406504 158977692 141890698 149668965 289533426 536659273 578882913 234405244 160427375 155168698 546646968 5900866 186185047 30363396 93724506 330946208 71156738 151777321 386435297 166234...
result:
ok 100000 numbers
Test #6:
score: 0
Accepted
time: 100ms
memory: 3700kb
input:
100000 73249682 47619910 55116647 128554518 478392703 63789277 1414550 48229272 250747189 997808121 68243990 71103259 14595028 337838604 410310513 43474969 8461967 37722317 943841593 58972601 79052007 22427901 46110814 619079500 877950428 36298374 69387929 88793453 605131681 357216829 87609669 84382...
output:
257109036 501494378 337838604 117945202 1238159000 357216829 370639522 674316525 166433196 141880926 63179079 654049210 156730406 551517649 735314262 181349794 451156077 565079747 625657717 257899254 1205898584 736860320 436887048 555501015 634904039 720592503 12278164 173702793 193363767 979933288 ...
result:
ok 100000 numbers
Test #7:
score: 0
Accepted
time: 158ms
memory: 4044kb
input:
100000 3464647282669819 76909462 18457991 390212875 487173929 4650003301142948 58807432 33266940 719515794 957413208 3623051027558998 54952606 51934480 131325868 905272434 2157584874927409 3499113 70686381 77518537 690462406 33017219084855 33767393 38574619 740268825 282227083 8188451643953574 58134...
output:
390212875 719515794 131325868 155037074 564454166 219227588 43753104 994443 502671816 299710451 565210982 474336918 433975652 48941841 496369573 863101565 245821709 440163633 726366897 267542384 562858097 952398862 586438103 210538560 124486961 766476932 676547307 983844074 853989168 375846543 31868...
result:
ok 100000 numbers
Test #8:
score: -100
Time Limit Exceeded
input:
100000 7275102973097459 8 5 378261522 402575783 4286374497798649 7 6 933502847 46810288 8023805803098978 8 9 35196391 195312588 4051746189241631 3 8 519770155 701983292 4216164515090411 7 9 182157078 199994837 6972811444444614 6 6 304516916 456411186 7951380680901262 9 2 961077532 951204780 47010508...
output:
378261522 140430864 35196391 1039540310 182157078 0 0 444998952 0 0 754484822 0 180947816 0 0 0 0 254006158 1780383378 218139523 785880599 738973224 462923534 0 0 957159359 713596284 0 0 336000605 734376280 367523046 950147662 742867914 367222451 0 470041638 524073733 217959245 303140703 1393714956 ...