forked from likecs/Codechef-June-18-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork_ways.cpp
61 lines (58 loc) · 1.17 KB
/
work_ways.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
int n, c, ptr;
vector<int> facts;
int ans[1000001];
bool check(int idx) {
ptr = n;
int prod = c;
while(ptr > 0) {
while(prod % facts[idx] != 0) {
idx -= 1;
}
prod /= facts[idx];
ans[ptr] = facts[idx] + ptr - 1;
ptr -= 1;
if (idx != (facts.size() - 1) && facts[idx] == (facts[idx + 1] - 1)) {
// greedily chose the largest factor.
idx += 1;
}
if (idx == 0 || prod == 1) {
// string of 1 as factor so prod will remain same.
break;
}
}
return (prod == 1);
}
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while(t--) {
cin >> n >> c;
facts.clear();
int x = sqrt(c);
for(int i = 1; i <= x; ++i) {
if (c % i == 0) {
facts.push_back(i);
if (i * i != c) {
facts.push_back(c / i);
}
}
}
sort(facts.begin(), facts.end());
for(int i = 0; i < facts.size(); ++i) {
if (check(i)) {
break;
}
}
for(int i = 1; i <= ptr; ++i) {
cout << i << " ";
}
for(int i = ptr+1; i <= n; ++i) {
cout << ans[i] << " ";
}
cout << "\n";
}
return 0;
}