UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#193699#3404. 挑战数学题heyuzhen1002891ms1200kbC++11973b2023-10-14 11:32:592023-10-14 13:06:30

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll t,n,m,k,a,b,num[2][70],p[2];
ll check(){
	cin >> n >> k >> m >> a >> b;
	if(n % m == 0)return 0;
	if(k == 1)return -1;
	if(a == 0 && b == 0)return 0;
	memset(num,0,sizeof num),p[0] = p[1] = 0;
	ll x = n,y = m,ans,pos = 0,now = n;
	while(x)num[0][++p[0]] = x % k,x /= k;
	reverse(num[0] + 1,num[0] + p[0] + 1);
	while(y)num[1][++p[1]] = y % k,y /= k;
	reverse(num[1] + 1,num[1] + p[1] + 1);
	for(ll i = 1;i <= min(p[0],p[1]);i++)
		if(num[0][i] == num[1][i])pos = i;else break;
	ans = min(p[0] * b,p[0] * b + p[1] * a - pos * a - pos * b);
	for(ll i = 0;i < p[0];i++){
		__int128_t l = now,r = now;
		for(ll j = 0;i * b + j * a < ans;j++){
			if(l / m < r / m || l % m == 0 || r % m == 0){
				ans = min(ans,i * b + j * a);
				break;
			}l *= k;r = (r + 1) * k - 1;
		}now /= k;
	}return ans;
}
int main(){
	cin >> t;
	while(t--)cout << check() << "\n";
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1200kb

input:

1000
8 2 2 59003 81867
3 2 2 89039 80745
1 2 2 53711 65018
2 2 2 32715 17151
10 2 2 84528 34886
8 2 ...

output:

0
89039
53711
0
0
0
0
42487
0
0
50912
12681
28375
34729
5490
0
0
51761
0
0
0
0
37779
0
0
35976
0
0
0...

result:

ok 1000 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 1200kb

input:

1000
2 2 2 37780 86555
7 2 2 89129 7178
2 2 2 17951 52958
10 2 2 26936 42012
3 2 2 60665 93891
6 2 2...

output:

0
21534
0
0
60665
0
58576
2679
2832
0
0
0
0
0
43604
0
0
0
33080
2323
0
8002
0
34150
0
0
1511
29519
3...

result:

ok 1000 lines

Test #3:

score: 10
Accepted
time: 7ms
memory: 1196kb

input:

1000
10 2 2 8045 50026
9 2 2 89220 76715
9 2 2 90704 49411
6 2 2 21157 99577
9 2 2 4097 95999
6 2 2 ...

output:

0
76715
49411
0
4097
0
0
0
17641
16686
27772
2329
67893
0
6300
0
0
0
34980
0
0
0
0
9665
0
0
0
8626
3...

result:

ok 1000 lines

Test #4:

score: 10
Accepted
time: 3ms
memory: 1200kb

input:

1000
3118508115828426 154 868 8132 41884
3931579548240732 941 701 74555 47816
8449377952493847 2 884...

output:

16264
74555
200925
20082
95409
86422
78597
103612
36637
54923
57113
86840
5583
13436
86568
35561
158...

result:

ok 1000 lines

Test #5:

score: 10
Accepted
time: 4ms
memory: 1200kb

input:

1000
3944755588879146 303 819 86909 13867
8763291186392016 129 516 74646 74250
1625936469431466 89 4...

output:

86909
74646
38540
33473
6137
5046
37433
11998
27254
101016
191086
68466
52106
45826
56655
30096
4966...

result:

ok 1000 lines

Test #6:

score: 10
Accepted
time: 3ms
memory: 1196kb

input:

1000
4770994471995274 968 769 65686 10042
3594998529576004 877 457 74736 43787
1430449541658685 176 ...

output:

60252
74736
83510
27694
192224
10112
61677
37598
37436
8312
33973
31002
65925
84934
139692
19224
285...

result:

ok 1000 lines

Test #7:

score: 10
Accepted
time: 337ms
memory: 1196kb

input:

100000
757248424647325 9288055 3367590 725196382 731761592
6919961090641335 53803149 9360478 9641988...

output:

725196382
964198834
1443265902
582242864
963073930
822181745
11039167
506248991
786652862
42778830
2...

result:

ok 100000 lines

Test #8:

score: 10
Accepted
time: 332ms
memory: 1196kb

input:

100000
1583487307763453 50158960 46751588 269675159 461533575
1751668433825323 6733605 70973366 5174...

output:

269675159
103484058
1164455072
295337086
330050067
433397845
103330707
294241887
816252078
170100802...

result:

ok 100000 lines

Test #9:

score: 10
Accepted
time: 1038ms
memory: 1200kb

input:

100000
4716583217277151 4 52747498 12693575 899798681
829175712575305 2 44541602 440174460 495757286...

output:

165016475
11444535960
1730329699
2217034737
1941792230
674845276
6456301760
3564603108
1132871139
84...

result:

ok 100000 lines

Test #10:

score: 10
Accepted
time: 1167ms
memory: 1196kb

input:

100000
3484326877769848 5 1259851 527002834 883146585
5159038285612059 4 6611676 5765801 40208038
59...

output:

3689019838
63423811
12060546023
9233251907
2559958074
6814788860
2053302691
979027344
6094806841
273...

result:

ok 100000 lines

Extra Test:

score: 0
Extra Test Passed