Submission #343761

# Submission time Handle Problem Language Result Execution time Memory
343761 2021-01-04T13:05:51 Z Rakhmand Nice sequence (IZhO18_sequence) C++14
76 / 100
2000 ms 68448 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;
int pr[mxn], a[mxn];
int used[mxn];
vector<int> g[mxn], top;
 
bool dfs(int x){
    used[x] = 1;
    bool ok = true;
    for(auto to : g[x]){
        if(used[to] == 0) ok = (ok & dfs(to));
        else if(used[to] == 1){
            ok = false;
        }else{
            continue;
        }
    }
    used[x] = 2;
    return ok;
    
}
 
bool check(int k){
    for(int i = 1; i <= k; i++){
        used[i] = 0;
        g[i].clear();
    }
    for(int i = 1; i + min(n, m) <= k; i++){
        if(i + n <= k)
            g[i].push_back(i + n);
        if(i + m <= k)
            g[i + m].push_back(i);
    }
    return dfs(1);
}
 
void dfss(int x){
    used[x] = 1;
    for(auto to : g[x]){
        if(used[to] == 0){
            dfss(to);
        }
    }
    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 m = (l + r) / 2;
        if(check(m)){
            l = m;
        }else{
            r = m - 1;
        }
    }
    int ans = -1;
    if(check(r)) ans = r;
    else ans = l;
    for(int i = 1; i <= ans; i++){
        used[i] = 0;
    }
    top.clear();
    cout << ans - 1 << nl;
    for(int i = 1; i <= ans; 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 <= ans; i++){
        pr[i] -= pr[1];
    }
    pr[1] = 0;
    for(int i = 2; i <= ans; i++){
        a[i] = pr[i] - pr[i - 1];
    }
    for(int i = 2; i <= ans; 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++){
      |                    ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Ok
2 Correct 7 ms 9708 KB Ok
3 Correct 8 ms 9708 KB Ok
4 Correct 7 ms 9708 KB Ok
5 Correct 7 ms 9708 KB Ok
6 Correct 7 ms 9708 KB Ok
7 Correct 7 ms 9708 KB Ok
8 Correct 7 ms 9708 KB Ok
9 Correct 7 ms 9708 KB Ok
10 Correct 7 ms 9708 KB Ok
11 Correct 7 ms 9708 KB Ok
12 Correct 7 ms 9708 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Ok
2 Correct 7 ms 9708 KB Ok
3 Correct 7 ms 9708 KB Ok
4 Correct 7 ms 9708 KB Ok
5 Correct 7 ms 9708 KB Ok
6 Correct 11 ms 10092 KB Ok
7 Correct 24 ms 11116 KB Ok
8 Correct 15 ms 10348 KB Ok
9 Correct 29 ms 11372 KB Ok
10 Correct 20 ms 10604 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Ok
2 Correct 7 ms 9708 KB Ok
3 Correct 7 ms 9708 KB Ok
4 Correct 7 ms 9708 KB Ok
5 Correct 7 ms 9708 KB Ok
6 Correct 7 ms 9708 KB Ok
7 Correct 7 ms 9708 KB Ok
8 Correct 7 ms 9708 KB Ok
9 Correct 7 ms 9708 KB Ok
10 Correct 8 ms 9708 KB Ok
11 Correct 7 ms 9708 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9708 KB Ok
2 Correct 7 ms 9708 KB Ok
3 Correct 7 ms 9708 KB Ok
4 Correct 7 ms 9708 KB Ok
5 Correct 7 ms 9708 KB Ok
6 Correct 303 ms 30688 KB Ok
7 Correct 355 ms 33760 KB Ok
8 Correct 684 ms 36008 KB Ok
9 Correct 435 ms 32828 KB Ok
10 Correct 190 ms 22756 KB Ok
11 Correct 330 ms 34912 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Ok
2 Correct 7 ms 9708 KB Ok
3 Correct 8 ms 9708 KB Ok
4 Correct 7 ms 9708 KB Ok
5 Correct 7 ms 9708 KB Ok
6 Correct 7 ms 9708 KB Ok
7 Correct 7 ms 9708 KB Ok
8 Correct 7 ms 9708 KB Ok
9 Correct 7 ms 9708 KB Ok
10 Correct 7 ms 9708 KB Ok
11 Correct 7 ms 9708 KB Ok
12 Correct 7 ms 9708 KB Ok
13 Correct 7 ms 9708 KB Ok
14 Correct 7 ms 9708 KB Ok
15 Correct 7 ms 9708 KB Ok
16 Correct 7 ms 9708 KB Ok
17 Correct 7 ms 9708 KB Ok
18 Correct 7 ms 9708 KB Ok
19 Correct 7 ms 9708 KB Ok
20 Correct 7 ms 9708 KB Ok
21 Correct 7 ms 9708 KB Ok
22 Correct 8 ms 9708 KB Ok
23 Correct 7 ms 9708 KB Ok
24 Correct 10 ms 9964 KB Ok
25 Correct 10 ms 9964 KB Ok
26 Correct 10 ms 9964 KB Ok
27 Correct 9 ms 9964 KB Ok
28 Correct 9 ms 9964 KB Ok
29 Correct 9 ms 9964 KB Ok
30 Correct 9 ms 9836 KB Ok
31 Correct 10 ms 9964 KB Ok
32 Correct 10 ms 9964 KB Ok
33 Correct 11 ms 9964 KB Ok
34 Correct 17 ms 10220 KB Ok
35 Correct 15 ms 10348 KB Ok
36 Correct 15 ms 10348 KB Ok
37 Correct 15 ms 10476 KB Ok
38 Correct 16 ms 10348 KB Ok
39 Correct 14 ms 10348 KB Ok
40 Correct 15 ms 10348 KB Ok
41 Correct 16 ms 10348 KB Ok
42 Correct 15 ms 10348 KB Ok
43 Correct 15 ms 10348 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Ok
2 Correct 7 ms 9708 KB Ok
3 Correct 8 ms 9708 KB Ok
4 Correct 7 ms 9708 KB Ok
5 Correct 7 ms 9708 KB Ok
6 Correct 7 ms 9708 KB Ok
7 Correct 7 ms 9708 KB Ok
8 Correct 7 ms 9708 KB Ok
9 Correct 7 ms 9708 KB Ok
10 Correct 7 ms 9708 KB Ok
11 Correct 7 ms 9708 KB Ok
12 Correct 7 ms 9708 KB Ok
13 Correct 7 ms 9708 KB Ok
14 Correct 7 ms 9708 KB Ok
15 Correct 7 ms 9708 KB Ok
16 Correct 7 ms 9708 KB Ok
17 Correct 7 ms 9708 KB Ok
18 Correct 11 ms 10092 KB Ok
19 Correct 24 ms 11116 KB Ok
20 Correct 15 ms 10348 KB Ok
21 Correct 29 ms 11372 KB Ok
22 Correct 20 ms 10604 KB Ok
23 Correct 7 ms 9708 KB Ok
24 Correct 7 ms 9708 KB Ok
25 Correct 7 ms 9708 KB Ok
26 Correct 7 ms 9708 KB Ok
27 Correct 7 ms 9708 KB Ok
28 Correct 7 ms 9708 KB Ok
29 Correct 7 ms 9708 KB Ok
30 Correct 7 ms 9708 KB Ok
31 Correct 7 ms 9708 KB Ok
32 Correct 8 ms 9708 KB Ok
33 Correct 7 ms 9708 KB Ok
34 Correct 10 ms 9964 KB Ok
35 Correct 10 ms 9964 KB Ok
36 Correct 10 ms 9964 KB Ok
37 Correct 9 ms 9964 KB Ok
38 Correct 9 ms 9964 KB Ok
39 Correct 9 ms 9964 KB Ok
40 Correct 9 ms 9836 KB Ok
41 Correct 10 ms 9964 KB Ok
42 Correct 10 ms 9964 KB Ok
43 Correct 11 ms 9964 KB Ok
44 Correct 17 ms 10220 KB Ok
45 Correct 15 ms 10348 KB Ok
46 Correct 15 ms 10348 KB Ok
47 Correct 15 ms 10476 KB Ok
48 Correct 16 ms 10348 KB Ok
49 Correct 14 ms 10348 KB Ok
50 Correct 15 ms 10348 KB Ok
51 Correct 16 ms 10348 KB Ok
52 Correct 15 ms 10348 KB Ok
53 Correct 15 ms 10348 KB Ok
54 Correct 143 ms 15844 KB Ok
55 Correct 183 ms 16104 KB Ok
56 Correct 169 ms 16228 KB Ok
57 Correct 116 ms 15084 KB Ok
58 Correct 147 ms 15592 KB Ok
59 Correct 172 ms 15336 KB Ok
60 Correct 122 ms 14820 KB Ok
61 Correct 121 ms 15332 KB Ok
62 Correct 182 ms 15844 KB Ok
63 Correct 124 ms 15212 KB Ok
64 Correct 171 ms 16232 KB Ok
65 Correct 163 ms 15844 KB Ok
66 Correct 139 ms 15596 KB Ok
67 Correct 115 ms 15284 KB Ok
68 Correct 144 ms 15588 KB Ok
69 Correct 583 ms 26896 KB Ok
70 Correct 867 ms 27628 KB Ok
71 Correct 538 ms 26864 KB Ok
72 Correct 565 ms 26724 KB Ok
73 Correct 677 ms 26852 KB Ok
74 Correct 503 ms 26708 KB Ok
75 Correct 430 ms 26340 KB Ok
76 Correct 788 ms 27096 KB Ok
77 Correct 454 ms 26316 KB Ok
78 Correct 688 ms 26840 KB Ok
79 Correct 736 ms 27064 KB Ok
80 Correct 541 ms 27112 KB Ok
81 Correct 513 ms 26892 KB Ok
82 Correct 526 ms 26980 KB Ok
83 Correct 493 ms 26340 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Ok
2 Correct 7 ms 9708 KB Ok
3 Correct 8 ms 9708 KB Ok
4 Correct 7 ms 9708 KB Ok
5 Correct 7 ms 9708 KB Ok
6 Correct 7 ms 9708 KB Ok
7 Correct 7 ms 9708 KB Ok
8 Correct 7 ms 9708 KB Ok
9 Correct 7 ms 9708 KB Ok
10 Correct 7 ms 9708 KB Ok
11 Correct 7 ms 9708 KB Ok
12 Correct 7 ms 9708 KB Ok
13 Correct 7 ms 9708 KB Ok
14 Correct 7 ms 9708 KB Ok
15 Correct 7 ms 9708 KB Ok
16 Correct 7 ms 9708 KB Ok
17 Correct 7 ms 9708 KB Ok
18 Correct 11 ms 10092 KB Ok
19 Correct 24 ms 11116 KB Ok
20 Correct 15 ms 10348 KB Ok
21 Correct 29 ms 11372 KB Ok
22 Correct 20 ms 10604 KB Ok
23 Correct 7 ms 9708 KB Ok
24 Correct 7 ms 9708 KB Ok
25 Correct 7 ms 9708 KB Ok
26 Correct 7 ms 9708 KB Ok
27 Correct 7 ms 9708 KB Ok
28 Correct 7 ms 9708 KB Ok
29 Correct 7 ms 9708 KB Ok
30 Correct 7 ms 9708 KB Ok
31 Correct 7 ms 9708 KB Ok
32 Correct 8 ms 9708 KB Ok
33 Correct 7 ms 9708 KB Ok
34 Correct 8 ms 9708 KB Ok
35 Correct 7 ms 9708 KB Ok
36 Correct 7 ms 9708 KB Ok
37 Correct 7 ms 9708 KB Ok
38 Correct 7 ms 9708 KB Ok
39 Correct 303 ms 30688 KB Ok
40 Correct 355 ms 33760 KB Ok
41 Correct 684 ms 36008 KB Ok
42 Correct 435 ms 32828 KB Ok
43 Correct 190 ms 22756 KB Ok
44 Correct 330 ms 34912 KB Ok
45 Correct 10 ms 9964 KB Ok
46 Correct 10 ms 9964 KB Ok
47 Correct 10 ms 9964 KB Ok
48 Correct 9 ms 9964 KB Ok
49 Correct 9 ms 9964 KB Ok
50 Correct 9 ms 9964 KB Ok
51 Correct 9 ms 9836 KB Ok
52 Correct 10 ms 9964 KB Ok
53 Correct 10 ms 9964 KB Ok
54 Correct 11 ms 9964 KB Ok
55 Correct 17 ms 10220 KB Ok
56 Correct 15 ms 10348 KB Ok
57 Correct 15 ms 10348 KB Ok
58 Correct 15 ms 10476 KB Ok
59 Correct 16 ms 10348 KB Ok
60 Correct 14 ms 10348 KB Ok
61 Correct 15 ms 10348 KB Ok
62 Correct 16 ms 10348 KB Ok
63 Correct 15 ms 10348 KB Ok
64 Correct 15 ms 10348 KB Ok
65 Correct 143 ms 15844 KB Ok
66 Correct 183 ms 16104 KB Ok
67 Correct 169 ms 16228 KB Ok
68 Correct 116 ms 15084 KB Ok
69 Correct 147 ms 15592 KB Ok
70 Correct 172 ms 15336 KB Ok
71 Correct 122 ms 14820 KB Ok
72 Correct 121 ms 15332 KB Ok
73 Correct 182 ms 15844 KB Ok
74 Correct 124 ms 15212 KB Ok
75 Correct 171 ms 16232 KB Ok
76 Correct 163 ms 15844 KB Ok
77 Correct 139 ms 15596 KB Ok
78 Correct 115 ms 15284 KB Ok
79 Correct 144 ms 15588 KB Ok
80 Correct 583 ms 26896 KB Ok
81 Correct 867 ms 27628 KB Ok
82 Correct 538 ms 26864 KB Ok
83 Correct 565 ms 26724 KB Ok
84 Correct 677 ms 26852 KB Ok
85 Correct 503 ms 26708 KB Ok
86 Correct 430 ms 26340 KB Ok
87 Correct 788 ms 27096 KB Ok
88 Correct 454 ms 26316 KB Ok
89 Correct 688 ms 26840 KB Ok
90 Correct 736 ms 27064 KB Ok
91 Correct 541 ms 27112 KB Ok
92 Correct 513 ms 26892 KB Ok
93 Correct 526 ms 26980 KB Ok
94 Correct 493 ms 26340 KB Ok
95 Correct 323 ms 25628 KB Ok
96 Correct 586 ms 32604 KB Ok
97 Correct 627 ms 28648 KB Ok
98 Correct 348 ms 28464 KB Ok
99 Correct 485 ms 28452 KB Ok
100 Correct 562 ms 27248 KB Ok
101 Correct 541 ms 30420 KB Ok
102 Correct 503 ms 28468 KB Ok
103 Correct 500 ms 29796 KB Ok
104 Correct 653 ms 31648 KB Ok
105 Correct 605 ms 32008 KB Ok
106 Correct 434 ms 32608 KB Ok
107 Correct 514 ms 30944 KB Ok
108 Correct 642 ms 31972 KB Ok
109 Correct 543 ms 33760 KB Ok
110 Execution timed out 2103 ms 68448 KB Time limit exceeded
111 Halted 0 ms 0 KB -