답안 #376087

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376087 2021-03-10T20:40:39 Z ijxjdjd Nice sequence (IZhO18_sequence) C++14
100 / 100
481 ms 31432 KB
#include <bits/stdc++.h>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)

using namespace std;

using ll = long long;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getrandom() {
    return uniform_int_distribution<int>(1, 5000)(rng);
}
int main() {
	cin.tie(0);
	cin.sync_with_stdio(0);
	int T;
	cin >> T;
	FR(iter, T) {
        int N, M;
        cin >> N >> M;
        bool swapped = false;
        if (max(N,M)%min(N,M) == 0) {
            if (N >= M) {
                cout << N-1 << '\n';
                for (int i = 0; i < N-1; i++) {
                    cout << 1 << " ";
                }
                cout << '\n';
            }
            else {
                cout << M-1 << '\n';
                for (int i = 0; i < M-1; i++) {
                    cout << -1 << " ";
                }
                cout << '\n';
            }
        }
        else {
            if (N < M) {
                swap(N, M);
                swapped = true;
            }
            int d = N%M;
            int mx = N;
            while (d != 0) {
                mx = max(mx, d+N);
                d += N%M;
                if (d >= M) {
                    d -= M;
                }
                if (d == 0) {
                    break;
                }
            }
            vector<int> cntIn(mx, 0);
            vector<int> pref(mx, 0);
            int lst = 0;
            for (int i = 0; i < mx; i++) {
                if (i+N < mx) {
                    cntIn[i+N]++;
                }
                if (i-M >= 0) {
                   cntIn[i-M]++;
                }
            }
            deque<int> deq;
            for (int i = 0; i < mx; i++) {
                if (cntIn[i] == 0) {
                   deq.push_back(i);
                   pref[i] = lst++;
                }
            }
            while (deq.size()) {
                int i = deq.front();
                deq.pop_front();
                if (i+N<mx) {
                    cntIn[i+N]--;
                    pref[i+N] = lst++;
                    if (cntIn[i+N] == 0) {
                        deq.push_back(i+N);
                    }
                }
                if (i-M>=0) {
                    cntIn[i-M]--;
                    pref[i-M] = lst++;
                    if (cntIn[i-M] == 0) {
                        deq.push_back(i-M);
                    }
                }
            }
            for (int i = mx-1; i >=0; i--) {
                pref[i] -= pref[0];
            }
            if (!swapped) {
                for (int i = 1; i < mx; i++) {
                    pref[i] *= -1;
                }
            }
            cout << mx - 1 << '\n';
            for (int i = 1; i < mx; i++) {
                cout << pref[i] - pref[i-1] << " ";
            }
            cout << '\n';
        }
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 0 ms 364 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 268 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 3 ms 364 KB Ok
7 Correct 11 ms 748 KB Ok
8 Correct 4 ms 492 KB Ok
9 Correct 10 ms 876 KB Ok
10 Correct 6 ms 620 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Ok
2 Correct 0 ms 364 KB Ok
3 Correct 1 ms 384 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 78 ms 4436 KB Ok
7 Correct 74 ms 3616 KB Ok
8 Correct 130 ms 6356 KB Ok
9 Correct 105 ms 6392 KB Ok
10 Correct 58 ms 2700 KB Ok
11 Correct 99 ms 5656 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 0 ms 364 KB Ok
13 Correct 0 ms 364 KB Ok
14 Correct 0 ms 364 KB Ok
15 Correct 1 ms 384 KB Ok
16 Correct 1 ms 364 KB Ok
17 Correct 1 ms 364 KB Ok
18 Correct 1 ms 364 KB Ok
19 Correct 1 ms 364 KB Ok
20 Correct 1 ms 364 KB Ok
21 Correct 1 ms 364 KB Ok
22 Correct 1 ms 364 KB Ok
23 Correct 1 ms 364 KB Ok
24 Correct 2 ms 364 KB Ok
25 Correct 2 ms 364 KB Ok
26 Correct 2 ms 364 KB Ok
27 Correct 2 ms 364 KB Ok
28 Correct 2 ms 364 KB Ok
29 Correct 2 ms 364 KB Ok
30 Correct 2 ms 364 KB Ok
31 Correct 2 ms 364 KB Ok
32 Correct 2 ms 364 KB Ok
33 Correct 2 ms 364 KB Ok
34 Correct 4 ms 492 KB Ok
35 Correct 4 ms 492 KB Ok
36 Correct 5 ms 492 KB Ok
37 Correct 4 ms 492 KB Ok
38 Correct 4 ms 492 KB Ok
39 Correct 5 ms 492 KB Ok
40 Correct 4 ms 492 KB Ok
41 Correct 4 ms 492 KB Ok
42 Correct 4 ms 492 KB Ok
43 Correct 4 ms 492 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 0 ms 364 KB Ok
13 Correct 1 ms 268 KB Ok
14 Correct 1 ms 364 KB Ok
15 Correct 1 ms 364 KB Ok
16 Correct 1 ms 364 KB Ok
17 Correct 1 ms 364 KB Ok
18 Correct 3 ms 364 KB Ok
19 Correct 11 ms 748 KB Ok
20 Correct 4 ms 492 KB Ok
21 Correct 10 ms 876 KB Ok
22 Correct 6 ms 620 KB Ok
23 Correct 0 ms 364 KB Ok
24 Correct 0 ms 364 KB Ok
25 Correct 1 ms 384 KB Ok
26 Correct 1 ms 364 KB Ok
27 Correct 1 ms 364 KB Ok
28 Correct 1 ms 364 KB Ok
29 Correct 1 ms 364 KB Ok
30 Correct 1 ms 364 KB Ok
31 Correct 1 ms 364 KB Ok
32 Correct 1 ms 364 KB Ok
33 Correct 1 ms 364 KB Ok
34 Correct 2 ms 364 KB Ok
35 Correct 2 ms 364 KB Ok
36 Correct 2 ms 364 KB Ok
37 Correct 2 ms 364 KB Ok
38 Correct 2 ms 364 KB Ok
39 Correct 2 ms 364 KB Ok
40 Correct 2 ms 364 KB Ok
41 Correct 2 ms 364 KB Ok
42 Correct 2 ms 364 KB Ok
43 Correct 2 ms 364 KB Ok
44 Correct 4 ms 492 KB Ok
45 Correct 4 ms 492 KB Ok
46 Correct 5 ms 492 KB Ok
47 Correct 4 ms 492 KB Ok
48 Correct 4 ms 492 KB Ok
49 Correct 5 ms 492 KB Ok
50 Correct 4 ms 492 KB Ok
51 Correct 4 ms 492 KB Ok
52 Correct 4 ms 492 KB Ok
53 Correct 4 ms 492 KB Ok
54 Correct 61 ms 2812 KB Ok
55 Correct 71 ms 2972 KB Ok
56 Correct 70 ms 3172 KB Ok
57 Correct 54 ms 2368 KB Ok
58 Correct 65 ms 2716 KB Ok
59 Correct 69 ms 2536 KB Ok
60 Correct 54 ms 2416 KB Ok
61 Correct 53 ms 2508 KB Ok
62 Correct 72 ms 2824 KB Ok
63 Correct 59 ms 2572 KB Ok
64 Correct 72 ms 2868 KB Ok
65 Correct 66 ms 3028 KB Ok
66 Correct 60 ms 2776 KB Ok
67 Correct 51 ms 2396 KB Ok
68 Correct 61 ms 2580 KB Ok
69 Correct 105 ms 6536 KB Ok
70 Correct 109 ms 7688 KB Ok
71 Correct 118 ms 5764 KB Ok
72 Correct 105 ms 6792 KB Ok
73 Correct 104 ms 6284 KB Ok
74 Correct 100 ms 5388 KB Ok
75 Correct 106 ms 5640 KB Ok
76 Correct 128 ms 7440 KB Ok
77 Correct 99 ms 5000 KB Ok
78 Correct 106 ms 7176 KB Ok
79 Correct 105 ms 6660 KB Ok
80 Correct 104 ms 6528 KB Ok
81 Correct 110 ms 6916 KB Ok
82 Correct 107 ms 6520 KB Ok
83 Correct 102 ms 5388 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Ok
2 Correct 1 ms 364 KB Ok
3 Correct 1 ms 364 KB Ok
4 Correct 1 ms 364 KB Ok
5 Correct 1 ms 364 KB Ok
6 Correct 1 ms 364 KB Ok
7 Correct 1 ms 364 KB Ok
8 Correct 1 ms 364 KB Ok
9 Correct 1 ms 364 KB Ok
10 Correct 1 ms 364 KB Ok
11 Correct 1 ms 364 KB Ok
12 Correct 0 ms 364 KB Ok
13 Correct 1 ms 268 KB Ok
14 Correct 1 ms 364 KB Ok
15 Correct 1 ms 364 KB Ok
16 Correct 1 ms 364 KB Ok
17 Correct 1 ms 364 KB Ok
18 Correct 3 ms 364 KB Ok
19 Correct 11 ms 748 KB Ok
20 Correct 4 ms 492 KB Ok
21 Correct 10 ms 876 KB Ok
22 Correct 6 ms 620 KB Ok
23 Correct 0 ms 364 KB Ok
24 Correct 0 ms 364 KB Ok
25 Correct 1 ms 384 KB Ok
26 Correct 1 ms 364 KB Ok
27 Correct 1 ms 364 KB Ok
28 Correct 1 ms 364 KB Ok
29 Correct 1 ms 364 KB Ok
30 Correct 1 ms 364 KB Ok
31 Correct 1 ms 364 KB Ok
32 Correct 1 ms 364 KB Ok
33 Correct 1 ms 364 KB Ok
34 Correct 0 ms 364 KB Ok
35 Correct 1 ms 364 KB Ok
36 Correct 1 ms 364 KB Ok
37 Correct 1 ms 364 KB Ok
38 Correct 1 ms 364 KB Ok
39 Correct 78 ms 4436 KB Ok
40 Correct 74 ms 3616 KB Ok
41 Correct 130 ms 6356 KB Ok
42 Correct 105 ms 6392 KB Ok
43 Correct 58 ms 2700 KB Ok
44 Correct 99 ms 5656 KB Ok
45 Correct 2 ms 364 KB Ok
46 Correct 2 ms 364 KB Ok
47 Correct 2 ms 364 KB Ok
48 Correct 2 ms 364 KB Ok
49 Correct 2 ms 364 KB Ok
50 Correct 2 ms 364 KB Ok
51 Correct 2 ms 364 KB Ok
52 Correct 2 ms 364 KB Ok
53 Correct 2 ms 364 KB Ok
54 Correct 2 ms 364 KB Ok
55 Correct 4 ms 492 KB Ok
56 Correct 4 ms 492 KB Ok
57 Correct 5 ms 492 KB Ok
58 Correct 4 ms 492 KB Ok
59 Correct 4 ms 492 KB Ok
60 Correct 5 ms 492 KB Ok
61 Correct 4 ms 492 KB Ok
62 Correct 4 ms 492 KB Ok
63 Correct 4 ms 492 KB Ok
64 Correct 4 ms 492 KB Ok
65 Correct 61 ms 2812 KB Ok
66 Correct 71 ms 2972 KB Ok
67 Correct 70 ms 3172 KB Ok
68 Correct 54 ms 2368 KB Ok
69 Correct 65 ms 2716 KB Ok
70 Correct 69 ms 2536 KB Ok
71 Correct 54 ms 2416 KB Ok
72 Correct 53 ms 2508 KB Ok
73 Correct 72 ms 2824 KB Ok
74 Correct 59 ms 2572 KB Ok
75 Correct 72 ms 2868 KB Ok
76 Correct 66 ms 3028 KB Ok
77 Correct 60 ms 2776 KB Ok
78 Correct 51 ms 2396 KB Ok
79 Correct 61 ms 2580 KB Ok
80 Correct 105 ms 6536 KB Ok
81 Correct 109 ms 7688 KB Ok
82 Correct 118 ms 5764 KB Ok
83 Correct 105 ms 6792 KB Ok
84 Correct 104 ms 6284 KB Ok
85 Correct 100 ms 5388 KB Ok
86 Correct 106 ms 5640 KB Ok
87 Correct 128 ms 7440 KB Ok
88 Correct 99 ms 5000 KB Ok
89 Correct 106 ms 7176 KB Ok
90 Correct 105 ms 6660 KB Ok
91 Correct 104 ms 6528 KB Ok
92 Correct 110 ms 6916 KB Ok
93 Correct 107 ms 6520 KB Ok
94 Correct 102 ms 5388 KB Ok
95 Correct 146 ms 6048 KB Ok
96 Correct 221 ms 10436 KB Ok
97 Correct 216 ms 9456 KB Ok
98 Correct 159 ms 6376 KB Ok
99 Correct 195 ms 8860 KB Ok
100 Correct 197 ms 7860 KB Ok
101 Correct 202 ms 9952 KB Ok
102 Correct 187 ms 8268 KB Ok
103 Correct 193 ms 7880 KB Ok
104 Correct 226 ms 8444 KB Ok
105 Correct 218 ms 8996 KB Ok
106 Correct 175 ms 8020 KB Ok
107 Correct 210 ms 7484 KB Ok
108 Correct 222 ms 9488 KB Ok
109 Correct 211 ms 9712 KB Ok
110 Correct 438 ms 22704 KB Ok
111 Correct 481 ms 31392 KB Ok
112 Correct 438 ms 24168 KB Ok
113 Correct 453 ms 27676 KB Ok
114 Correct 450 ms 29348 KB Ok
115 Correct 448 ms 28484 KB Ok
116 Correct 453 ms 28588 KB Ok
117 Correct 435 ms 28876 KB Ok
118 Correct 450 ms 24900 KB Ok
119 Correct 467 ms 30220 KB Ok
120 Correct 439 ms 25080 KB Ok
121 Correct 437 ms 24652 KB Ok
122 Correct 440 ms 26960 KB Ok
123 Correct 453 ms 31432 KB Ok
124 Correct 428 ms 22456 KB Ok
125 Correct 394 ms 13520 KB Ok