/* upsolve with help from PurpleCrayon */
#include <stdio.h>
#define A 38
#define U (1 << A / 2)
int gcd[A + 1][A + 1], mx[U], bb[A / 2], ban_odd[U], kk_even[A / 2 + 1][U]; char good_odd[U], good_even[U];
void init() {
int a, b, c, i, j, k, u, u_, v, v_;
for (a = 0; a <= A; a++)
for (b = 0; b <= A; b++)
gcd[a][b] = a > b ? gcd[b][a] : a == 0 ? b : gcd[b % a][a];
for (u = 1, i = 0; u < U; u++) {
while (1 << i + 1 <= u)
i++;
mx[u] = i;
}
good_odd[0] = 1, ban_odd[0] = 0;
for (j = 0; j < A / 2; j++) {
b = j * 2 + 1;
good_odd[1 << j] = 1;
for (u = 1; u < 1 << j; u++) {
u_ = u | 1 << j, i = mx[u], a = i * 2 + 1;
good_odd[u_] = good_odd[u_ ^ 1 << i] && (u & 1 << (gcd[a][b] - 1) / 2) != 0;
}
for (i = 0; i < A / 2; i++) {
a = i * 2 + 1;
bb[i] = 0;
for (k = 0; k < A / 2; k++) {
c = k * 2 + 2;
if (gcd[b][c] == a)
bb[i] |= 1 << k;
}
}
for (u = (1 << j) - 2; u >= 0; u--) {
u_ = u | 1 << j, i = mx[(1 << j) - 1 ^ u];
ban_odd[u_] = ban_odd[u_ ^ 1 << i] | bb[i];
}
for (u = 0; u < 1 << j; u++) {
u_ = u | 1 << j;
good_odd[u_] &= good_odd[u], ban_odd[u_] |= ban_odd[u];
}
}
good_even[0] = 1;
for (j = 0; j < A / 2; j++) {
b = j * 2 + 2;
good_even[1 << j] = 1;
for (v = 1; v < 1 << j; v++) {
v_ = v | 1 << j, i = mx[v], a = i * 2 + 2;
good_even[v_] = good_even[v_ ^ 1 << i] && (v & 1 << (gcd[a][b] - 1) / 2) != 0;
}
for (v = 0; v < 1 << j; v++) {
v_ = v | 1 << j;
good_even[v_] &= good_even[v];
}
}
for (v = 0; v < 1 << A / 2; v++)
kk_even[0][v] = good_even[v];
for (j = 0; j < A / 2; j++)
for (v = 0; v < 1 << A / 2; v++) {
kk_even[j + 1][v] = kk_even[j][v];
if ((v & 1 << j) != 0)
kk_even[j + 1][v] += kk_even[j][v ^ 1 << j];
}
}
int main() {
int t;
init();
scanf("%d", &t);
while (t--) {
int k, i, u, u_, u0, u1, v_, v0, v1, l, k00, k01, k10, k11;
scanf("%d", &k);
u_ = 0, v_ = 0, l = 0;
for (i = A / 2 - 1; i >= 0; i--) {
u0 = u_, u1 = u_ | 1 << i, v0 = v_, v1 = v_ | 1 << i;
k00 = k01 = k10 = k11 = 0;
for (u = 0; u < 1 << i; u++) {
if (good_odd[u0 | u] && (ban_odd[u0 | u] & v0) == 0)
k00 += kk_even[i][v0 | (1 << i) - 1 & ~ban_odd[u0 | u]];
if (good_odd[u1 | u] && (ban_odd[u1 | u] & v0) == 0)
k01 += kk_even[i][v0 | (1 << i) - 1 & ~ban_odd[u1 | u]];
if (good_odd[u0 | u] && (ban_odd[u0 | u] & v1) == 0)
k10 += kk_even[i][v1 | (1 << i) - 1 & ~ban_odd[u0 | u]];
if (good_odd[u1 | u] && (ban_odd[u1 | u] & v1) == 0)
k11 += kk_even[i][v1 | (1 << i) - 1 & ~ban_odd[u1 | u]];
}
if (k < k00) {
u_ = u0, v_ = v0, l += 0;
continue;
}
k -= k00;
if (k < k01) {
u_ = u1, v_ = v0, l += 1;
continue;
}
k -= k01;
if (k < k10) {
u_ = u0, v_ = v1, l += 1;
continue;
}
k -= k10;
u_ = u1, v_ = v1, l += 2;
}
printf("%d", l);
for (i = 0; i < A / 2; i++) {
if ((u_ & 1 << i) != 0)
printf(" %d", i * 2 + 1);
if ((v_ & 1 << i) != 0)
printf(" %d", i * 2 + 2);
}
printf("\n");
}
return 0;
}
Compilation message
Main.c: In function 'init':
Main.c:16:17: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
16 | while (1 << i + 1 <= u)
| ~~^~~
Main.c:38:37: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
38 | u_ = u | 1 << j, i = mx[(1 << j) - 1 ^ u];
| ~~~~~~~~~^~~
Main.c: In function 'main':
Main.c:84:38: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
84 | k00 += kk_even[i][v0 | (1 << i) - 1 & ~ban_odd[u0 | u]];
| ~~~~~~~~~^~~
Main.c:84:42: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
84 | k00 += kk_even[i][v0 | (1 << i) - 1 & ~ban_odd[u0 | u]];
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
Main.c:86:38: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
86 | k01 += kk_even[i][v0 | (1 << i) - 1 & ~ban_odd[u1 | u]];
| ~~~~~~~~~^~~
Main.c:86:42: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
86 | k01 += kk_even[i][v0 | (1 << i) - 1 & ~ban_odd[u1 | u]];
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
Main.c:88:38: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
88 | k10 += kk_even[i][v1 | (1 << i) - 1 & ~ban_odd[u0 | u]];
| ~~~~~~~~~^~~
Main.c:88:42: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
88 | k10 += kk_even[i][v1 | (1 << i) - 1 & ~ban_odd[u0 | u]];
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
Main.c:90:38: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
90 | k11 += kk_even[i][v1 | (1 << i) - 1 & ~ban_odd[u1 | u]];
| ~~~~~~~~~^~~
Main.c:90:42: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
90 | k11 += kk_even[i][v1 | (1 << i) - 1 & ~ban_odd[u1 | u]];
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
Main.c:73:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", &t);
| ^~~~~~~~~~~~~~~
Main.c:77:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d", &k);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
46412 KB |
Output is correct |
2 |
Correct |
35 ms |
46420 KB |
Output is correct |
3 |
Correct |
34 ms |
46412 KB |
Output is correct |
4 |
Correct |
35 ms |
46412 KB |
Output is correct |
5 |
Correct |
34 ms |
46416 KB |
Output is correct |
6 |
Correct |
40 ms |
46412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
46412 KB |
Output is correct |
2 |
Correct |
35 ms |
46420 KB |
Output is correct |
3 |
Correct |
34 ms |
46412 KB |
Output is correct |
4 |
Correct |
35 ms |
46412 KB |
Output is correct |
5 |
Correct |
34 ms |
46416 KB |
Output is correct |
6 |
Correct |
40 ms |
46412 KB |
Output is correct |
7 |
Correct |
40 ms |
46396 KB |
Output is correct |
8 |
Correct |
34 ms |
46444 KB |
Output is correct |
9 |
Correct |
34 ms |
46420 KB |
Output is correct |
10 |
Correct |
34 ms |
46428 KB |
Output is correct |
11 |
Correct |
34 ms |
46412 KB |
Output is correct |
12 |
Correct |
34 ms |
46436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
46412 KB |
Output is correct |
2 |
Correct |
35 ms |
46420 KB |
Output is correct |
3 |
Correct |
34 ms |
46412 KB |
Output is correct |
4 |
Correct |
35 ms |
46412 KB |
Output is correct |
5 |
Correct |
34 ms |
46416 KB |
Output is correct |
6 |
Correct |
40 ms |
46412 KB |
Output is correct |
7 |
Correct |
40 ms |
46396 KB |
Output is correct |
8 |
Correct |
34 ms |
46444 KB |
Output is correct |
9 |
Correct |
34 ms |
46420 KB |
Output is correct |
10 |
Correct |
34 ms |
46428 KB |
Output is correct |
11 |
Correct |
34 ms |
46412 KB |
Output is correct |
12 |
Correct |
34 ms |
46436 KB |
Output is correct |
13 |
Correct |
33 ms |
46392 KB |
Output is correct |
14 |
Correct |
32 ms |
46336 KB |
Output is correct |
15 |
Correct |
34 ms |
46380 KB |
Output is correct |
16 |
Correct |
34 ms |
46412 KB |
Output is correct |
17 |
Correct |
34 ms |
46388 KB |
Output is correct |
18 |
Correct |
35 ms |
46432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
46412 KB |
Output is correct |
2 |
Correct |
35 ms |
46420 KB |
Output is correct |
3 |
Correct |
34 ms |
46412 KB |
Output is correct |
4 |
Correct |
35 ms |
46412 KB |
Output is correct |
5 |
Correct |
34 ms |
46416 KB |
Output is correct |
6 |
Correct |
40 ms |
46412 KB |
Output is correct |
7 |
Correct |
40 ms |
46396 KB |
Output is correct |
8 |
Correct |
34 ms |
46444 KB |
Output is correct |
9 |
Correct |
34 ms |
46420 KB |
Output is correct |
10 |
Correct |
34 ms |
46428 KB |
Output is correct |
11 |
Correct |
34 ms |
46412 KB |
Output is correct |
12 |
Correct |
34 ms |
46436 KB |
Output is correct |
13 |
Correct |
33 ms |
46392 KB |
Output is correct |
14 |
Correct |
32 ms |
46336 KB |
Output is correct |
15 |
Correct |
34 ms |
46380 KB |
Output is correct |
16 |
Correct |
34 ms |
46412 KB |
Output is correct |
17 |
Correct |
34 ms |
46388 KB |
Output is correct |
18 |
Correct |
35 ms |
46432 KB |
Output is correct |
19 |
Correct |
35 ms |
46416 KB |
Output is correct |
20 |
Correct |
40 ms |
46396 KB |
Output is correct |
21 |
Correct |
35 ms |
46336 KB |
Output is correct |
22 |
Correct |
36 ms |
46404 KB |
Output is correct |
23 |
Correct |
34 ms |
46348 KB |
Output is correct |
24 |
Correct |
38 ms |
46456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
46412 KB |
Output is correct |
2 |
Correct |
35 ms |
46420 KB |
Output is correct |
3 |
Correct |
34 ms |
46412 KB |
Output is correct |
4 |
Correct |
35 ms |
46412 KB |
Output is correct |
5 |
Correct |
34 ms |
46416 KB |
Output is correct |
6 |
Correct |
40 ms |
46412 KB |
Output is correct |
7 |
Correct |
40 ms |
46396 KB |
Output is correct |
8 |
Correct |
34 ms |
46444 KB |
Output is correct |
9 |
Correct |
34 ms |
46420 KB |
Output is correct |
10 |
Correct |
34 ms |
46428 KB |
Output is correct |
11 |
Correct |
34 ms |
46412 KB |
Output is correct |
12 |
Correct |
34 ms |
46436 KB |
Output is correct |
13 |
Correct |
33 ms |
46392 KB |
Output is correct |
14 |
Correct |
32 ms |
46336 KB |
Output is correct |
15 |
Correct |
34 ms |
46380 KB |
Output is correct |
16 |
Correct |
34 ms |
46412 KB |
Output is correct |
17 |
Correct |
34 ms |
46388 KB |
Output is correct |
18 |
Correct |
35 ms |
46432 KB |
Output is correct |
19 |
Correct |
35 ms |
46416 KB |
Output is correct |
20 |
Correct |
40 ms |
46396 KB |
Output is correct |
21 |
Correct |
35 ms |
46336 KB |
Output is correct |
22 |
Correct |
36 ms |
46404 KB |
Output is correct |
23 |
Correct |
34 ms |
46348 KB |
Output is correct |
24 |
Correct |
38 ms |
46456 KB |
Output is correct |
25 |
Correct |
38 ms |
46432 KB |
Output is correct |
26 |
Correct |
36 ms |
46416 KB |
Output is correct |
27 |
Correct |
36 ms |
46328 KB |
Output is correct |
28 |
Correct |
36 ms |
46420 KB |
Output is correct |
29 |
Correct |
36 ms |
46336 KB |
Output is correct |
30 |
Correct |
35 ms |
46412 KB |
Output is correct |