Submission #91969

#TimeUsernameProblemLanguageResultExecution timeMemory
91969popovicirobertNice sequence (IZhO18_sequence)C++14
100 / 100
1223 ms57896 KiB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define ld long double
// 217
// 44

using namespace std;

const int INF = 5e8;
const int MAXN = (int) 2e5;

int n, m;
char vis[2 * MAXN + 1];

void dfs(int nod, bool &ok, int len) {
    vis[nod] = 1;
    if(nod + n <= len) {
        if(vis[nod + n] == 0) {
            dfs(nod + n, ok, len);
        }
        else if(vis[nod + n] == 1) {
            ok = 0;
        }
    }
    if(ok && nod - m >= 0) {
        if(vis[nod - m] == 0) {
            dfs(nod - m, ok, len);
        }
        else if(vis[nod - m] == 1) {
            ok = 0;
        }
    }
    vis[nod] = 2;
}

inline bool check(int len) {
    int i;
    for(i = 0; i <= len; i++) {
        vis[i] = 0;
    }
    bool ok = 1;
    for(i = 0; i <= len && ok; i++) {
        if(vis[i] == 0) {
            dfs(i, ok, len);
        }
    }
    return ok;
}

inline int get() {
    int res = 0;
    for(int step = 1 << 18; step; step >>= 1) {
        if(res + step <= n + m && check(res + step)) {
            res += step;
        }
    }
    return res;
}

vector <int> ord;
int sp[2 * MAXN + 1];

void dfs1(int nod, int sz) {
    vis[nod] = 1;
    if(nod + n <= sz && vis[nod + n] == 0) {
        dfs1(nod + n, sz);
    }
    if(nod - m >= 0 && vis[nod - m] == 0) {
        dfs1(nod - m, sz);
    }
    ord.push_back(nod);
}

inline void solve(int sz) {
    int i;
    for(i = 0; i <= sz; i++) {
        vis[i] = 0;
    }
    ord.clear();
    for(i = 0; i <= sz; i++) {
        if(vis[i] == 0) {
            dfs1(i, sz);
        }
    }
    int val = -INF;
    for(auto it : ord) {
        if(it == 0) {
            val = max(val, 0);
            sp[it] = 0;
            val++;
            continue;
        }
        if(it + n <= sz) {
            val = max(val, sp[it + n] + 1);
        }
        if(it - m >= 0) {
            val = max(val, sp[it - m]);
        }
        sp[it] = val;
        val++;
    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int t, i, j;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> t;
    while(t > 0) {
        t--;
        cin >> n >> m;
        int mn = min(n, m), mx = max(n, m);
        if(mx % mn == 0) {
            cout << mx - 1 << "\n";
            int sign = 1;
            if(mn == n) {
                sign = -1;
            }
            for(i = 1; i < mx; i++) {
                cout << sign << " ";
            }
            if(mx > 1) {
                cout << "\n";
            }
            continue;
        }
        int sz = get();
        solve(sz);
        cout << sz << "\n";
        for(i = 1; i <= sz; i++) {
            cout << sp[i] - sp[i - 1] << " ";
        }
        cout << "\n";
    }
    //cin.close();
    //cout.close();
    return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:109:15: warning: unused variable 'j' [-Wunused-variable]
     int t, i, j;
               ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...