답안 #343989

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343989 2021-01-05T02:19:27 Z Rakhmand Nice sequence (IZhO18_sequence) C++14
100 / 100
1330 ms 54372 KB
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
 
using namespace std;
 
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer {cout << "NO"; exit(0);}
#define FOR(i, start, finish, k) for(llong i = start; i <= finish; i += k)
 
const long long mxn = 4e5 + 110;
const long long mnn = 1e3 + 2;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long OO = 1e8;
 
typedef long long llong;
typedef unsigned long long ullong;
 
int n, m, k;
int pr[mxn], a[mxn];
int used[mxn];
vector<int> top;
 
bool dfs(int x){
    used[x] = 1;
    bool ok = 1;
    if (x + n <= k) {
        if (!used[x + n])
            ok &= dfs(x + n);
        else if (used[x + n] == 1)
            return false;
    }
    if (x - m >= 1) {
        if (!used[x - m])
            ok &= dfs(x - m);
        else if (used[x - m] == 1)
            return false;
    }
    used[x] = 2;
    return ok;
}
 
bool check(int K){
    k = K;
    for(int i = 1; i <= k; i++){
        used[i] = 0;
    }
    return dfs(1);
}
 
void dfss(int x){
    used[x] = 1;
    if (x + n <= k) {
        if (used[x + n] == 0)
            dfss(x + n);
    }
    if (x - m >= 1) {
        if (used[x - m] == 0)
            dfss(x - m);
    }
    top.push_back(x);
}
 
void solve(){
    cin >> n >> m;
    if(n == m){
        cout << n - 1 << nl;
        for(int i = 1; i < n; i++){
            cout << 1 << ' ';
        }
        cout << nl;
        return ;
    }
    if(n == 1 || m % n == 0){
        cout << m - 1 << nl;
        for(int i = 1; i <= m - 1; i++) cout << -1 << ' ';
        cout << nl;
        return ;
    }
    if(m == 1 || n % m == 0){
        cout << n - 1 << nl;
        for(int i = 1; i <= n - 1; i++) cout << 1 << ' ';
        cout << nl;
        return ;
    }
    int l = 0, r = n + m + 1;
    while(r - l > 1){
        int mid = (l + r) / 2;
        if(check(mid)){
            l = mid;
        }else{
            r = mid;
        }
    }
    k = -1;
    if(check(r)) k = r;
    else k = l;
    for(int i = 1; i <= k; i++){
        used[i] = 0;
    }
    top.clear();
    cout << k - 1 << nl;
    for(int i = 1; i <= k; i++){
        if(used[i] == 0){
            dfss(i);
        }
    }
    reverse(top.begin(), top.end());
    for(int i = 0; i < top.size(); i++){
        pr[top[i]] = OO - i;
    }
    for(int i = 2; i <= k; i++){
        pr[i] -= pr[1];
    }
    pr[1] = 0;
    for(int i = 2; i <= k; i++){
        a[i] = pr[i] - pr[i - 1];
    }
    for(int i = 2; i <= k; i++){
        cout << a[i] << ' ';
    }
    cout << nl;
}
 
int main() {
    ios;
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}

Compilation message

sequence.cpp: In function 'void solve()':
sequence.cpp:136:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |     for(int i = 0; i < top.size(); i++){
      |                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 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 1 ms 364 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 3 ms 492 KB Ok
7 Correct 14 ms 1388 KB Ok
8 Correct 6 ms 748 KB Ok
9 Correct 13 ms 1516 KB Ok
10 Correct 8 ms 1004 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 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 189 ms 13672 KB Ok
7 Correct 219 ms 15012 KB Ok
8 Correct 350 ms 17640 KB Ok
9 Correct 241 ms 15720 KB Ok
10 Correct 102 ms 8428 KB Ok
11 Correct 188 ms 16508 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 1 ms 364 KB Ok
13 Correct 1 ms 364 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 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 492 KB Ok
25 Correct 2 ms 492 KB Ok
26 Correct 2 ms 492 KB Ok
27 Correct 5 ms 492 KB Ok
28 Correct 2 ms 492 KB Ok
29 Correct 2 ms 492 KB Ok
30 Correct 2 ms 492 KB Ok
31 Correct 3 ms 492 KB Ok
32 Correct 2 ms 492 KB Ok
33 Correct 2 ms 492 KB Ok
34 Correct 5 ms 748 KB Ok
35 Correct 6 ms 748 KB Ok
36 Correct 6 ms 748 KB Ok
37 Correct 6 ms 748 KB Ok
38 Correct 5 ms 768 KB Ok
39 Correct 5 ms 748 KB Ok
40 Correct 6 ms 748 KB Ok
41 Correct 6 ms 748 KB Ok
42 Correct 5 ms 748 KB Ok
43 Correct 6 ms 748 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 1 ms 364 KB Ok
13 Correct 1 ms 364 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 492 KB Ok
19 Correct 14 ms 1388 KB Ok
20 Correct 6 ms 748 KB Ok
21 Correct 13 ms 1516 KB Ok
22 Correct 8 ms 1004 KB Ok
23 Correct 1 ms 364 KB Ok
24 Correct 1 ms 364 KB Ok
25 Correct 1 ms 364 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 492 KB Ok
35 Correct 2 ms 492 KB Ok
36 Correct 2 ms 492 KB Ok
37 Correct 5 ms 492 KB Ok
38 Correct 2 ms 492 KB Ok
39 Correct 2 ms 492 KB Ok
40 Correct 2 ms 492 KB Ok
41 Correct 3 ms 492 KB Ok
42 Correct 2 ms 492 KB Ok
43 Correct 2 ms 492 KB Ok
44 Correct 5 ms 748 KB Ok
45 Correct 6 ms 748 KB Ok
46 Correct 6 ms 748 KB Ok
47 Correct 6 ms 748 KB Ok
48 Correct 5 ms 768 KB Ok
49 Correct 5 ms 748 KB Ok
50 Correct 6 ms 748 KB Ok
51 Correct 6 ms 748 KB Ok
52 Correct 5 ms 748 KB Ok
53 Correct 6 ms 748 KB Ok
54 Correct 90 ms 3564 KB Ok
55 Correct 81 ms 3980 KB Ok
56 Correct 79 ms 3948 KB Ok
57 Correct 59 ms 3180 KB Ok
58 Correct 79 ms 3436 KB Ok
59 Correct 73 ms 3308 KB Ok
60 Correct 75 ms 2924 KB Ok
61 Correct 61 ms 3180 KB Ok
62 Correct 81 ms 3692 KB Ok
63 Correct 64 ms 3308 KB Ok
64 Correct 81 ms 3948 KB Ok
65 Correct 75 ms 3564 KB Ok
66 Correct 68 ms 3436 KB Ok
67 Correct 59 ms 3308 KB Ok
68 Correct 69 ms 3436 KB Ok
69 Correct 172 ms 12780 KB Ok
70 Correct 190 ms 13292 KB Ok
71 Correct 159 ms 12768 KB Ok
72 Correct 193 ms 12652 KB Ok
73 Correct 175 ms 12780 KB Ok
74 Correct 160 ms 12652 KB Ok
75 Correct 159 ms 12416 KB Ok
76 Correct 192 ms 13036 KB Ok
77 Correct 148 ms 12268 KB Ok
78 Correct 172 ms 12780 KB Ok
79 Correct 167 ms 12908 KB Ok
80 Correct 168 ms 13036 KB Ok
81 Correct 170 ms 12908 KB Ok
82 Correct 165 ms 12908 KB Ok
83 Correct 150 ms 12268 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 1 ms 364 KB Ok
13 Correct 1 ms 364 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 492 KB Ok
19 Correct 14 ms 1388 KB Ok
20 Correct 6 ms 748 KB Ok
21 Correct 13 ms 1516 KB Ok
22 Correct 8 ms 1004 KB Ok
23 Correct 1 ms 364 KB Ok
24 Correct 1 ms 364 KB Ok
25 Correct 1 ms 364 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 1 ms 384 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 189 ms 13672 KB Ok
40 Correct 219 ms 15012 KB Ok
41 Correct 350 ms 17640 KB Ok
42 Correct 241 ms 15720 KB Ok
43 Correct 102 ms 8428 KB Ok
44 Correct 188 ms 16508 KB Ok
45 Correct 2 ms 492 KB Ok
46 Correct 2 ms 492 KB Ok
47 Correct 2 ms 492 KB Ok
48 Correct 5 ms 492 KB Ok
49 Correct 2 ms 492 KB Ok
50 Correct 2 ms 492 KB Ok
51 Correct 2 ms 492 KB Ok
52 Correct 3 ms 492 KB Ok
53 Correct 2 ms 492 KB Ok
54 Correct 2 ms 492 KB Ok
55 Correct 5 ms 748 KB Ok
56 Correct 6 ms 748 KB Ok
57 Correct 6 ms 748 KB Ok
58 Correct 6 ms 748 KB Ok
59 Correct 5 ms 768 KB Ok
60 Correct 5 ms 748 KB Ok
61 Correct 6 ms 748 KB Ok
62 Correct 6 ms 748 KB Ok
63 Correct 5 ms 748 KB Ok
64 Correct 6 ms 748 KB Ok
65 Correct 90 ms 3564 KB Ok
66 Correct 81 ms 3980 KB Ok
67 Correct 79 ms 3948 KB Ok
68 Correct 59 ms 3180 KB Ok
69 Correct 79 ms 3436 KB Ok
70 Correct 73 ms 3308 KB Ok
71 Correct 75 ms 2924 KB Ok
72 Correct 61 ms 3180 KB Ok
73 Correct 81 ms 3692 KB Ok
74 Correct 64 ms 3308 KB Ok
75 Correct 81 ms 3948 KB Ok
76 Correct 75 ms 3564 KB Ok
77 Correct 68 ms 3436 KB Ok
78 Correct 59 ms 3308 KB Ok
79 Correct 69 ms 3436 KB Ok
80 Correct 172 ms 12780 KB Ok
81 Correct 190 ms 13292 KB Ok
82 Correct 159 ms 12768 KB Ok
83 Correct 193 ms 12652 KB Ok
84 Correct 175 ms 12780 KB Ok
85 Correct 160 ms 12652 KB Ok
86 Correct 159 ms 12416 KB Ok
87 Correct 192 ms 13036 KB Ok
88 Correct 148 ms 12268 KB Ok
89 Correct 172 ms 12780 KB Ok
90 Correct 167 ms 12908 KB Ok
91 Correct 168 ms 13036 KB Ok
92 Correct 170 ms 12908 KB Ok
93 Correct 165 ms 12908 KB Ok
94 Correct 150 ms 12268 KB Ok
95 Correct 166 ms 8680 KB Ok
96 Correct 247 ms 12004 KB Ok
97 Correct 254 ms 10468 KB Ok
98 Correct 179 ms 9316 KB Ok
99 Correct 269 ms 10136 KB Ok
100 Correct 247 ms 9572 KB Ok
101 Correct 238 ms 10596 KB Ok
102 Correct 220 ms 9956 KB Ok
103 Correct 223 ms 10468 KB Ok
104 Correct 259 ms 11748 KB Ok
105 Correct 250 ms 11492 KB Ok
106 Correct 210 ms 11620 KB Ok
107 Correct 246 ms 11108 KB Ok
108 Correct 271 ms 12004 KB Ok
109 Correct 233 ms 12132 KB Ok
110 Correct 735 ms 50532 KB Ok
111 Correct 853 ms 53476 KB Ok
112 Correct 754 ms 53092 KB Ok
113 Correct 816 ms 52708 KB Ok
114 Correct 856 ms 54372 KB Ok
115 Correct 854 ms 52196 KB Ok
116 Correct 767 ms 53604 KB Ok
117 Correct 835 ms 52196 KB Ok
118 Correct 778 ms 52196 KB Ok
119 Correct 861 ms 52068 KB Ok
120 Correct 748 ms 51812 KB Ok
121 Correct 787 ms 50660 KB Ok
122 Correct 798 ms 52964 KB Ok
123 Correct 919 ms 53328 KB Ok
124 Correct 699 ms 50660 KB Ok
125 Correct 1330 ms 35640 KB Ok