Submission #686735

# Submission time Handle Problem Language Result Execution time Memory
686735 2023-01-25T21:16:55 Z quinque Nice sequence (IZhO18_sequence) C++17
24 / 100
210 ms 29228 KB
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("O3")
//#pragma GCC target("sse4,avx2,fma,avx")
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define unit unsigned int
#define ld double
#define F first
#define S second
// chrono::system_clock::now().time_since_epoch().count());

using namespace std;
const int N = 4e5 + 100, IINF = 1e7, LOG = 19, K = 1 << 10;
const ll INF = 1e18 + 214;
int usd[N];

bool cycle(int v, int &n, int &m, int &sz){
    if (usd[v] == sz)
        return false;
    usd[v] = sz;
    bool fl = true;
    if (v + m <= sz)
        fl &= cycle(v + m, n, m, sz);
    if (v - n >= 0)
        fl &= cycle(v - n, n, m, sz);
    return fl;
}

bool can(int n, int m, int sz){
    return cycle(0, n, m, sz);
}
vector<int> top;
void dfs(int v, vector<vector<int>> &g){
    if (usd[v])
        return;
    usd[v] = 1;
    for (auto i : g[v])
        dfs(i, g);
    top.push_back(v);
}

int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

void fill(int v, vector<vector<int>> &g, vector<int> &pref){
    if (usd[v])
        return;
    usd[v] = 1;
    for (auto i : g[v]){
        pref[i] = min(pref[i], pref[v] - 1);
        fill(i, g, pref);
    }
}

inline void solve(){
    int n, m;
    cin >> n >> m;
    int start = -1;
    if (n > m)
        swap(n, m), start *= -1;
    int l = n + m - 1 - gcd(n, m);
    cout << l << '\n';
    vector<vector<int>> g(l + 1);
    for (int i = 0; i <= l; i ++){
        if (i + m <= l)
            g[i].push_back(i + m);
        if (i - n >= 0)
            g[i].push_back(i - n);
    }
    for (int i = 0; i <= l; i ++)
        usd[i] = 0;
    top.clear();
    for (int i = 0; i <= l; i ++){
        if (usd[i])
            continue;
        dfs(i, g);
    }
    //cout << "why" << '\n';
    reverse(top.begin(), top.end());
    for (int i = 0; i <= l; i ++)
        usd[i] = 0;
    vector<int> pref(l + 1);
    int nw = 0;
    for (auto i: top){
        if (!usd[i]){
            pref[i] = nw;
            nw += l + l;
            fill(i, g, pref);
        }
    }
    for (int i = 1; i <= l; i ++)
        cout << (pref[i] - pref[i - 1]) * start << ' ';
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T;
    cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Ok
2 Incorrect 1 ms 212 KB All the numbers must be nonzero
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Ok
2 Correct 1 ms 212 KB Ok
3 Correct 1 ms 212 KB Ok
4 Correct 1 ms 212 KB Ok
5 Correct 0 ms 212 KB Ok
6 Correct 3 ms 596 KB Ok
7 Correct 13 ms 1704 KB Ok
8 Correct 6 ms 960 KB Ok
9 Correct 15 ms 1952 KB Ok
10 Correct 8 ms 1272 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Ok
2 Correct 1 ms 212 KB Ok
3 Correct 0 ms 212 KB Ok
4 Incorrect 0 ms 212 KB All the numbers must be nonzero
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Ok
2 Correct 0 ms 212 KB Ok
3 Correct 0 ms 212 KB Ok
4 Correct 0 ms 212 KB Ok
5 Correct 0 ms 212 KB Ok
6 Correct 123 ms 22500 KB Ok
7 Correct 108 ms 26616 KB Ok
8 Correct 210 ms 29228 KB Ok
9 Correct 164 ms 26632 KB Ok
10 Correct 94 ms 15604 KB Ok
11 Correct 152 ms 28716 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Ok
2 Incorrect 1 ms 212 KB All the numbers must be nonzero
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Ok
2 Incorrect 1 ms 212 KB All the numbers must be nonzero
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Ok
2 Incorrect 1 ms 212 KB All the numbers must be nonzero
3 Halted 0 ms 0 KB -