Submission #173791

# Submission time Handle Problem Language Result Execution time Memory
173791 2020-01-05T12:24:26 Z davitmarg Nice sequence (IZhO18_sequence) C++17
0 / 100
2 ms 376 KB
/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 200005;

int gcd(int a, int b)
{
    if (!a || !b)
        return a + b;
    return gcd(b, a % b);
}

LL lca(LL a, LL b)
{
    return a * b / gcd(a, b);
}

int q, n, m, k;
vector<int> t, a, used, tin;
vector<vector<int>> g;

void dfs(int v)
{
    vector<int> s;
    s.PB(v);
    while (!s.empty())
    {
        v = s.back();
        s.pop_back();
        used[v] = 1;
        for (int i = 0; i < g[v].size(); i++)
        {
            int to = g[v][i];
            if (used[to])
                continue;
            s.PB(to);
        }
        tin[v] = -t.size();
        t.PB(v);
    }
}

bool check(int x)
{
    t.clear();
    a.resize(x + 5);
    used.resize(x + 5);
    tin.resize(x + 5);
    g.resize(x + 5);

    for (int i = 0; i <= x; i++)
    {
        g[i].clear();
        used[i] = 0;
        a[i] = i;
    }

    for (int i = 0; i <= x; i++)
    {
        if (i - m >= 0)
            g[i - m].PB(i);
        if (i + n <= x)
            g[i + n].PB(i);
    }
    for (int i = 0; i <= x; i++)
        if (!used[i])
            dfs(i);
    reverse(all(t));
    //cout << "!!!!!" << x << endl;
    for (int i = 0; i < t.size(); i++)
    {
        int v = t[i];
        for (int j = 0; j < g[v].size(); j++)
        {
            int to = g[v][j];
            if (tin[to] <= tin[v])
                return 0;
            //cout << v << " : " << to << endl;
            a[to] = max(a[to], a[v] + 1);
        }
    }
    return 1;
}

int main()
{
    cin >> q;
    while (q--)
    {
        k = 0;
        cin >> n >> m;
        LL l, r, mid;
        l = 1;
        r = min(lca(n, m) - 1ll, 1000000ll);
        while (l <= r)
        {
            mid = (l + r) / 2;
            if (check(mid))
            {
                k = mid;
                l = mid + 1;
            }
            else
                r = mid - 1;
        }
        check(k);
        cout << k << endl;
        for (int i = 1; i <= k; i++)
            printf("%d ", a[i] - a[i - 1]);
        cout << endl;
    }
    return 0;
}

/*

1
1997 1341

 
*/

Compilation message

sequence.cpp: In function 'void dfs(int)':
sequence.cpp:53:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < g[v].size(); i++)
                         ~~^~~~~~~~~~~~~
sequence.cpp: In function 'bool check(int)':
sequence.cpp:92:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < t.size(); i++)
                     ~~^~~~~~~~~~
sequence.cpp:95:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < g[v].size(); j++)
                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Jury has the better answer : jans = 5, pans = 0
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 252 KB Jury has the better answer : jans = 3, pans = 1
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Jury has the better answer : jans = 2, pans = 0
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 252 KB Jury has the better answer : jans = 3, pans = 2
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Jury has the better answer : jans = 5, pans = 0
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Jury has the better answer : jans = 5, pans = 0
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB Jury has the better answer : jans = 5, pans = 0
2 Halted 0 ms 0 KB -