QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#14756#1924. Joining PowersQingyuAC ✓161ms4196kbC++203.2kb2021-10-11 17:26:062022-05-17 00:59:59

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-17 00:59:59]
  • 评测
  • 测评结果:AC
  • 用时:161ms
  • 内存:4196kb
  • [2021-10-11 17:26:06]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>

#pragma warning ( disable : 4996 )

using namespace std;
typedef unsigned long long uLL;
typedef pair<uLL,uLL> pLL;

const uLL _1e17_uLL = 100uLL*1000uLL*1000uLL*1000uLL*1000uLL*1000uLL;
const double _1e17_plus_EPS = 1e17*(1+1e-6);
const double log_1e17_plus_EPS = log(_1e17_plus_EPS);
vector<int> a;

uLL pow_(uLL a, int n, bool do_try_in_double = false) // guaranteed 2\<n\<63
{
	if(do_try_in_double)
		if(n * log((double)a) > log_1e17_plus_EPS)
			return _1e17_uLL + 1000;
	if(n < 15) {
		uLL res = 1uLL;
		for(int i=0; i<n; i++)
			res *= a;
		return res;
	} else {
		uLL res = pow_(a, n/2, false); // try_in_double already done
		res *= res;
		if(n%2!=0)
			res *= a;
		return res;
	}
}

int gcd(int a, int b)
{
	while(a>0 && b>0)
		if(a>b)
			a%=b;
		else
			b%=a;
	return a+b;
}

int lcm(int a, int b)
{
	return (a / gcd(a,b)) * b;
}

int idx_by_value_1(uLL value, int ex_) 
// guaranteed: 2\<ex_\<50;
// guaranteed: result fits int32
{
	int root = (int)(1e-3 + pow((double)value, 1.0/ex_));
	uLL po_;
	while((po_=pow_((uLL)root, ex_)) > value)
		root--;
	return root;
}

/*
pLL idx_by_value_2(uLL num, int p1, int p2)
{
	pLL p1e19_1 = idx_by_value_1(num, p1);
	pLL p1e19_2 = idx_by_value_1(num, p2);
	uLL res = (p1e19_1.first - idx_by_value_1(num, lcm(p1,p2)).first) + p1e19_2.first;
	return make_pair(res, max(p1e19_1.second, p1e19_2.second));
}
*/

int calc_only_subset(uLL num, int curr_pow, size_t idx_in_a)
{
	if(curr_pow > 60 || (1LL << curr_pow) > num)
		return 0;
	int res = idx_by_value_1(num, curr_pow);
	for(size_t j = idx_in_a + 1;  j < a.size(); j++)
		res -= calc_only_subset(num, lcm(curr_pow, a[j]), j);
	return res-1; // "-1" because 1 is calculated separately
}

int calc_idx_by_value(uLL num)
{
	int res = 1; // value 1, common for all possible sequences
	for(size_t i=0; i<a.size(); i++)
		res += calc_only_subset(num, a[i], i);
	return res;
}

int main(int argc, char* argv[])
{
//	freopen("input.txt", "rt", stdin);
//	freopen("output.txt", "wt", stdout);
	int TEST_NUM;
	cin >> TEST_NUM;
	
	for(int the_test=0; the_test<TEST_NUM; the_test++)
	{
		uLL n;
		int k;
		cin >> n >> k;
		a.resize(k);
		for(int i=0; i<k; i++)
			cin >> a[i];

		cerr << "started the_test = " << the_test << " (n = " << n << ", k = " << k << ")\n";

		sort(a.begin(), a.end());
		for(int j=(int)a.size()-1; j>0; j--) {
			for(int i=0; i<j; i++) {
				if(a[j] % a[i] == 0) {
					a.erase(a.begin() + j);
					break; // breaks loop by i, but not loop by j
				}
			}
		}
		if(a[0]==1)
			cout << n << endl;
		else if(a.size()==1)
			cout << pow_((uLL)n, a[0]) << endl;
		else {
			uLL left_value = max(1uLL, pow_(n/a.size(), a[0], true)), 
				right_value = pow_(n, a[0], true);  // dangerous if double overflow
			while(right_value > left_value)
			{
				uLL mid_value = left_value + (right_value - left_value) / 2;
				int mid_res = calc_idx_by_value(mid_value);
				if(mid_res < n)
					left_value = mid_value + 1;
				else
					right_value = mid_value;
			}
			cout << left_value << endl;
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 161ms
memory: 4096kb

input:

987
221590954 7
22 20 44 48 32 37 2
140459093 33
42 45 41 6 3 20 12 23 39 1 30 48 27 34 17 19 46 4 14 44 29 47 38 25 7 26 16 32 21 13 50 37 9
73521691 40
47 12 2 31 20 44 25 34 19 38 23 10 3 24 27 5 36 22 49 30 11 6 37 13 48 18 42 33 29 17 39 40 28 15 43 45 41 8 7 9
319423979 35
5 45 48 6 1 7 38 32 ...

output:

49102550451448209
140459093
5379536846046276
319423979
233361166
36490476889387249
806460091894081
50378461487355024
66667094289899641
26439622160671
85486090129441936
2689388
920388347
236753201
69437672408863521
4655468
314128147
86130810336821121
37257699
16247365229313409
845277938384896
2073322...

result:

ok 987 lines

Test #2:

score: 0
Accepted
time: 123ms
memory: 4172kb

input:

987
444498 31
3 15 19 46 48 17 27 50 8 22 4 30 41 6 16 42 26 7 9 45 31 13 44 43 21 49 47 34 32 25 29
2122 19
13 7 9 50 35 18 30 43 48 45 42 44 22 20 5 23 10 26 14
919603653 42
16 2 3 37 50 21 29 6 1 13 15 9 12 34 47 41 45 25 7 23 30 42 38 28 48 17 39 18 32 19 36 43 10 11 24 33 35 44 27 5 46 22
57438...

output:

78134876284681728
20134152015805343
919603653
574384418
1
29603160117264721
222493608
524288
297802881
28122841266984749
53091807712227216
4398046511104
81272582
21671920
455621014
1036471407164176
116924344228251
289300540
906185732
714633880973769
66394733324780025
4902227890625
8985939039
2187948...

result:

ok 987 lines

Test #3:

score: 0
Accepted
time: 106ms
memory: 4176kb

input:

987
675005279 29
14 10 50 7 44 48 2 23 25 40 24 22 8 39 6 38 1 18 32 19 49 47 33 46 45 16 17 42 27
1 2
23 32
55526954 39
17 12 22 11 16 18 13 5 8 23 9 21 6 49 32 34 14 45 33 20 41 46 29 10 2 31 3 37 35 39 25 4 36 43 28 27 44 24 15
180295732 22
7 26 17 36 45 11 4 44 1 25 24 27 48 50 10 28 35 34 5 33 ...

output:

675005279
1
3067031901024100
180295732
38770487785168441
556527872731375
851037689
95532363386654221
2457371131685757
124298439464241
9600716341538153
629243814
989709794
32531497152010000
156686404293136
508973221
3136285606502500
926222144
786020949
18114433644976384
9507097878905104
3323293056960...

result:

ok 987 lines

Test #4:

score: 0
Accepted
time: 97ms
memory: 3936kb

input:

987
261759292 32
17 23 32 9 36 12 41 45 4 11 13 25 33 21 18 20 39 30 29 34 2 14 50 6 49 10 47 46 5 27 38 43
471973864 28
47 31 25 34 30 26 11 19 17 6 18 36 5 37 13 3 8 48 2 28 38 1 4 43 27 16 42 12
158393 18
47 12 42 13 15 25 11 36 37 32 49 28 27 3 17 5 30 33
313732592 33
40 34 32 31 21 35 28 9 33 4...

output:

68516667368416996
471973864
3872999604918088
313732592
292551423
387420489
299867094522241
6324066576
645184455
33633700716494761
786901854
211135923
629688370
515847422
11948051041316928
144042197
695620483
11770896175925121
712646903
196716042
571356153
670737701
47627513000645796
805542757
662293...

result:

ok 987 lines

Test #5:

score: 0
Accepted
time: 108ms
memory: 4156kb

input:

987
79242748 13
10 25 19 27 3 1 13 20 21 31 16 2 36
183613899 15
2 28 45 6 29 21 19 46 13 32 24 22 4 10 35
237046785 4
2 21 6 17
719991427 34
33 7 14 40 25 37 16 50 22 21 35 23 1 38 47 8 28 18 10 11 44 20 19 41 42 3 34 36 46 17 12 39 43 31
25917 11
45 31 9 23 30 5 3 26 7 15 29
46737352 17
49 1 38 21...

output:

79242748
33714053990832384
56191173537900625
719991427
16400616094143
46737352
594283040
9631103913667081
24103059302560000
12124017585936
425426924
37772498166893361
1126162419264
752111021
517443910
660481991
107213535210701
567880942487693
146389792
454892001
886768610
664444732
441501397
1115771...

result:

ok 987 lines

Test #6:

score: 0
Accepted
time: 158ms
memory: 4196kb

input:

987
8515 12
28 36 21 49 47 4 48 24 20 12 13 32
275333930 15
10 7 2 32 38 37 20 6 40 18 1 13 27 31 24
676790036 39
10 18 32 31 7 36 47 11 29 9 39 19 26 48 41 38 28 44 1 14 30 37 35 25 15 50 16 43 23 40 20 22 33 5 24 17 13 12 45
216340960 39
22 4 37 48 38 8 23 40 33 45 29 42 39 28 5 7 9 25 21 27 36 20...

output:

5207790833250625
275333930
676790036
216340960
357623942
427790297
8549606719794239
680975628
64106059229761536
16983563041
419304906
739592240
41809240444686400
785613813
42796339657508644
96889010407
50926194487314944
25414869994135561
344212812
2334474605592801
541086900
2247065740400625
13914613...

result:

ok 987 lines

Test #7:

score: 0
Accepted
time: 111ms
memory: 4156kb

input:

987
283231275 18
45 17 24 29 49 27 47 25 43 2 19 11 14 40 28 22 39 20
402947065 18
19 33 31 36 21 1 15 14 8 24 27 6 23 26 40 43 10 2
793105568 18
16 17 27 36 43 33 48 9 1 23 2 7 25 29 30 31 19 39
252353 16
17 8 41 31 36 32 21 23 48 4 7 39 3 5 29 25
713701110 28
34 42 40 29 31 11 39 32 6 5 3 8 17 1 1...

output:

80219926248538176
402947065
793105568
13765675817065528
713701110
612036598
398725723
669110528
489441050
282475249
713520525
24065978875738344
11085588423710641
309632607
666551874
61480716492020809
84038753874739456
25765867368081
4686471801137736
40353607
13788974447543296
47853692273697424
54901...

result:

ok 987 lines

Test #8:

score: 0
Accepted
time: 108ms
memory: 4152kb

input:

987
298070554 27
26 18 33 39 40 30 6 4 21 42 19 24 11 15 34 14 43 49 32 45 44 17 37 1 20 10 38
323085288 42
21 26 42 6 5 10 9 37 4 33 8 7 22 16 34 23 13 20 18 11 3 41 40 47 50 32 30 31 25 39 2 1 45 29 27 46 38 35 36 19 43 15
116 10
33 39 40 36 44 18 30 26 23 8
137615562 10
17 14 38 30 40 16 2 36 28 ...

output:

298070554
323085288
8507630225817856
137615562
19012576848982201
2018946271174596
15045919506432
96477318
1
251112661
81046696046463512
1871872278288488
32536830399939856
2311309852775056
278019163
37204588891477921
49341016
1572763671875
58466850702648025
549057842453207
3614037248889693
1072135352...

result:

ok 987 lines

Test #9:

score: 0
Accepted
time: 132ms
memory: 4016kb

input:

987
178062818 18
24 47 8 45 15 7 1 40 36 20 18 33 4 11 9 37 16 6
885264439 14
1 21 10 44 41 25 39 47 20 27 26 12 23 2
588819494 15
6 28 33 37 14 3 8 5 22 1 13 30 38 7 34
338717816 20
42 2 8 30 44 37 48 13 14 36 45 21 38 39 23 40 7 33 46 1
173232 29
28 10 16 34 8 3 14 31 47 22 38 17 32 4 46 45 41 9 3...

output:

178062818
885264439
588819494
338717816
4480210998307864
24137569000000
761387608
1
553668448
3237251426128801
22876792454961
90458382169
127107547
103442541
70368744177664
788713691
709645644680625
680256900610225
960263236
4294967296
8388608
34326857515599936
1125899906842624
78364164096
619971653...

result:

ok 987 lines

Test #10:

score: 0
Accepted
time: 76ms
memory: 4112kb

input:

987
522268805 36
16 47 13 49 26 22 11 2 12 10 43 17 48 37 39 19 30 4 20 31 34 46 18 15 27 1 36 42 5 21 33 40 23 14 8 7
775464451 16
4 19 30 43 11 1 18 25 14 8 39 40 27 28 47 32
26792 12
21 7 12 30 5 48 45 36 6 20 35 3
451 18
37 16 38 48 42 9 45 15 32 6 26 39 43 27 29 47 44 34
2393 21
4 40 14 36 45 4...

output:

522268805
775464451
18145833636952
3107278481449024
30863013591601
17847986870864164
46260774318912400
599077246
158463630
6103972039782241
657799137
3570467226624
4808584372417849
91426586425943616
1891871595954176
47341328
74020969417202500
8602687898456976
86742989447101632
98027829040857444
1983...

result:

ok 987 lines