Submission #1035744

#TimeUsernameProblemLanguageResultExecution timeMemory
1035744phoenixNice sequence (IZhO18_sequence)C++17
100 / 100
772 ms72892 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 500500;

int n, m;

vector<int> g[N];
int color[N];
bool cycle;

void dfs(int s) {
    color[s] = 1;
    for (int to : g[s]) {
        if (!color[to]) dfs(to);
        cycle |= (color[to] == 1);
    }
    color[s] = 2;
}

void build(int len) {
    for (int i = 0; i <= len; i++) {
        g[i].clear();
        color[i] = 0;
    }
    for (int i = 0; i <= len - n; i++) {
        g[i].push_back(i + n);
    }
    for (int i = 0; i <= len - m; i++) {
        g[i + m].push_back(i);
    }
}

vector<int> top;
void topsort(int s) {
    color[s] = 1;
    for (int to : g[s]) if (!color[to]) {
        topsort(to);
    }
    top.push_back(s);
}

bool check(int len) {
    build(len);
    cycle = false;
    for (int i = 0; i <= len; i++) {
        if (color[i]) continue;
        dfs(i);
        if (cycle) return true;
    }
    return false;
}

void solve() {
    cin >> n >> m;
    int l = n + m - gcd(n, m) - 1, r = n + m - gcd(n, m);
    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (check(mid))
            r = mid;
        else 
            l = mid;
    }
    int len = l;
    top.clear();
    build(len);
    for (int i = 0; i <= len; i++) if (!color[i])
        topsort(i);
    
    int arr[len + 1] = {};
    for (int i = 0; i <= len; i++) {
        arr[top[i]] = i;
    }
    cout << len << '\n';
    for (int i = 1; i <= len; i++)
        cout << arr[i] - arr[i - 1] << ' ';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t --> 0) {
        solve();
        cout << '\n';
    }
}
#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...