Submission #170573

# Submission time Handle Problem Language Result Execution time Memory
170573 2019-12-25T16:16:27 Z SamAnd Nice sequence (IZhO18_sequence) C++17
100 / 100
1757 ms 66412 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 400005;

int gcd(int x, int y)
{
    if (x == 0)
        return y;
    return gcd(y % x, x);
}

int ka()
{
    int x = 0;
    while (1)
    {
        char y = getchar();
        if ('0' <= y && y <= '9')
            x = (x * 10) + (y - '0');
        else
            return x;
    }
}

char uu[12];
void tp(int x)
{
    if (x == 0)
    {
        putchar('0');
        return;
    }
    if (x < 0)
    {
        putchar('-');
        x *= (-1);
    }
    int i = 0;
    while (x)
    {
        uu[i++] = (x % 10) + '0';
        x /= 10;
    }
    for (i = i - 1; i >= 0; --i)
        putchar(uu[i]);
}

int n, m;

vector<int> a[N];

int c[N];
bool dfs(int x)
{
    c[x] = 1;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (c[h] == 1)
            return true;
        if (c[h] == 0)
            if (dfs(h))
                return true;
    }
    c[x] = 2;
    return false;
}

bool stg(int x)
{
    for (int i = 0; i <= x; ++i)
        a[i].clear();
    for (int i = 0; i <= x; ++i)
    {
        if (i + m <= x)
            a[i + m].push_back(i);
        if (i + n <= x)
            a[i].push_back(i + n);
    }
    for (int i = 0; i <= x; ++i)
        c[i] = 0;
    for (int i = 0; i <= x; ++i)
    {
        if (dfs(i))
            return false;
    }
    return true;
}

vector<int> v;
void dfs1(int x)
{
    c[x] = 1;
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (c[h])
            continue;
        dfs1(h);
    }
    v.push_back(x);
}

int p[N];
void solv()
{
    //n = ka();
    //m = ka();
    scanf("%d%d", &n, &m);
    int g = gcd(n, m);
    int ans;
    ans = n + m - gcd(n, m) - 1;
    /*int l = min(n, m) - 1, r = N - 1;
    while (l <= r)
    {
        int m = (l + r) / 2;
        if (stg(m))
        {
            ans = m;
            l = m + 1;
        }
        else
            r = m - 1;
    }*/
    tp(ans);
    putchar('\n');
    //printf("%d\n", ans);
    //return;
    for (int i = 0; i <= ans; ++i)
        a[i].clear();
    for (int i = 0; i <= ans; ++i)
    {
        if (i + m <= ans)
            a[i + m].push_back(i);
    }
    for (int i = ans; i >= 0; --i)
    {
        if (i + n <= ans)
            a[i].push_back(i + n);
    }
    v.clear();
    for (int i = 0; i <= ans; ++i)
        c[i] = 0;
    for (int i = 0; i <= ans; ++i)
    {
        if (!c[i])
            dfs1(i);
    }
    for (int i = 0; i < v.size(); ++i)
        p[v[i]] = i;
    for (int i = 1; i <= ans; ++i)
    {
        tp(p[i] - p[i - 1]);
        putchar(' ');
        //printf("%d ", p[i] - p[i - 1]);
    }
    putchar('\n');
    //printf("\n");
}

int main()
{
    int tt;
    //tt = ka();
    scanf("%d", &tt);
    while (tt--)
    {
        solv();
    }
    return 0;
}

Compilation message

sequence.cpp: In function 'bool dfs(int)':
sequence.cpp:56:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
sequence.cpp: In function 'void dfs1(int)':
sequence.cpp:94:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
sequence.cpp: In function 'void solv()':
sequence.cpp:149:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < v.size(); ++i)
                     ~~^~~~~~~~~~
sequence.cpp:110:9: warning: unused variable 'g' [-Wunused-variable]
     int g = gcd(n, m);
         ^
sequence.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:165:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &tt);
     ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9720 KB Ok
2 Correct 10 ms 9720 KB Ok
3 Correct 11 ms 9720 KB Ok
4 Correct 10 ms 9720 KB Ok
5 Correct 10 ms 9720 KB Ok
6 Correct 10 ms 9720 KB Ok
7 Correct 12 ms 9848 KB Ok
8 Correct 11 ms 9720 KB Ok
9 Correct 12 ms 9720 KB Ok
10 Correct 12 ms 9720 KB Ok
11 Correct 12 ms 9848 KB Ok
12 Correct 13 ms 9692 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 12 ms 9720 KB Ok
2 Correct 11 ms 9720 KB Ok
3 Correct 10 ms 9720 KB Ok
4 Correct 10 ms 9720 KB Ok
5 Correct 10 ms 9720 KB Ok
6 Correct 12 ms 9976 KB Ok
7 Correct 22 ms 10744 KB Ok
8 Correct 15 ms 10108 KB Ok
9 Correct 23 ms 10872 KB Ok
10 Correct 18 ms 10360 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9592 KB Ok
2 Correct 13 ms 9848 KB Ok
3 Correct 12 ms 9720 KB Ok
4 Correct 10 ms 9720 KB Ok
5 Correct 10 ms 9720 KB Ok
6 Correct 11 ms 9720 KB Ok
7 Correct 10 ms 9720 KB Ok
8 Correct 10 ms 9720 KB Ok
9 Correct 10 ms 9720 KB Ok
10 Correct 10 ms 9764 KB Ok
11 Correct 10 ms 9720 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9720 KB Ok
2 Correct 10 ms 9720 KB Ok
3 Correct 10 ms 9720 KB Ok
4 Correct 10 ms 9848 KB Ok
5 Correct 10 ms 9720 KB Ok
6 Correct 116 ms 22380 KB Ok
7 Correct 92 ms 26576 KB Ok
8 Correct 179 ms 29552 KB Ok
9 Correct 149 ms 26844 KB Ok
10 Correct 66 ms 18924 KB Ok
11 Correct 126 ms 24824 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9720 KB Ok
2 Correct 10 ms 9720 KB Ok
3 Correct 11 ms 9720 KB Ok
4 Correct 10 ms 9720 KB Ok
5 Correct 10 ms 9720 KB Ok
6 Correct 10 ms 9720 KB Ok
7 Correct 12 ms 9848 KB Ok
8 Correct 11 ms 9720 KB Ok
9 Correct 12 ms 9720 KB Ok
10 Correct 12 ms 9720 KB Ok
11 Correct 12 ms 9848 KB Ok
12 Correct 13 ms 9692 KB Ok
13 Correct 10 ms 9592 KB Ok
14 Correct 13 ms 9848 KB Ok
15 Correct 12 ms 9720 KB Ok
16 Correct 10 ms 9720 KB Ok
17 Correct 10 ms 9720 KB Ok
18 Correct 11 ms 9720 KB Ok
19 Correct 10 ms 9720 KB Ok
20 Correct 10 ms 9720 KB Ok
21 Correct 10 ms 9720 KB Ok
22 Correct 10 ms 9764 KB Ok
23 Correct 10 ms 9720 KB Ok
24 Correct 12 ms 9848 KB Ok
25 Correct 11 ms 9976 KB Ok
26 Correct 11 ms 9848 KB Ok
27 Correct 11 ms 9848 KB Ok
28 Correct 11 ms 9848 KB Ok
29 Correct 12 ms 9848 KB Ok
30 Correct 11 ms 9848 KB Ok
31 Correct 12 ms 9848 KB Ok
32 Correct 12 ms 9848 KB Ok
33 Correct 12 ms 9848 KB Ok
34 Correct 15 ms 10108 KB Ok
35 Correct 15 ms 10104 KB Ok
36 Correct 15 ms 10104 KB Ok
37 Correct 17 ms 10232 KB Ok
38 Correct 17 ms 10104 KB Ok
39 Correct 17 ms 10020 KB Ok
40 Correct 15 ms 10104 KB Ok
41 Correct 16 ms 10104 KB Ok
42 Correct 16 ms 10104 KB Ok
43 Correct 17 ms 10104 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9720 KB Ok
2 Correct 10 ms 9720 KB Ok
3 Correct 11 ms 9720 KB Ok
4 Correct 10 ms 9720 KB Ok
5 Correct 10 ms 9720 KB Ok
6 Correct 10 ms 9720 KB Ok
7 Correct 12 ms 9848 KB Ok
8 Correct 11 ms 9720 KB Ok
9 Correct 12 ms 9720 KB Ok
10 Correct 12 ms 9720 KB Ok
11 Correct 12 ms 9848 KB Ok
12 Correct 13 ms 9692 KB Ok
13 Correct 12 ms 9720 KB Ok
14 Correct 11 ms 9720 KB Ok
15 Correct 10 ms 9720 KB Ok
16 Correct 10 ms 9720 KB Ok
17 Correct 10 ms 9720 KB Ok
18 Correct 12 ms 9976 KB Ok
19 Correct 22 ms 10744 KB Ok
20 Correct 15 ms 10108 KB Ok
21 Correct 23 ms 10872 KB Ok
22 Correct 18 ms 10360 KB Ok
23 Correct 10 ms 9592 KB Ok
24 Correct 13 ms 9848 KB Ok
25 Correct 12 ms 9720 KB Ok
26 Correct 10 ms 9720 KB Ok
27 Correct 10 ms 9720 KB Ok
28 Correct 11 ms 9720 KB Ok
29 Correct 10 ms 9720 KB Ok
30 Correct 10 ms 9720 KB Ok
31 Correct 10 ms 9720 KB Ok
32 Correct 10 ms 9764 KB Ok
33 Correct 10 ms 9720 KB Ok
34 Correct 12 ms 9848 KB Ok
35 Correct 11 ms 9976 KB Ok
36 Correct 11 ms 9848 KB Ok
37 Correct 11 ms 9848 KB Ok
38 Correct 11 ms 9848 KB Ok
39 Correct 12 ms 9848 KB Ok
40 Correct 11 ms 9848 KB Ok
41 Correct 12 ms 9848 KB Ok
42 Correct 12 ms 9848 KB Ok
43 Correct 12 ms 9848 KB Ok
44 Correct 15 ms 10108 KB Ok
45 Correct 15 ms 10104 KB Ok
46 Correct 15 ms 10104 KB Ok
47 Correct 17 ms 10232 KB Ok
48 Correct 17 ms 10104 KB Ok
49 Correct 17 ms 10020 KB Ok
50 Correct 15 ms 10104 KB Ok
51 Correct 16 ms 10104 KB Ok
52 Correct 16 ms 10104 KB Ok
53 Correct 17 ms 10104 KB Ok
54 Correct 72 ms 15420 KB Ok
55 Correct 80 ms 15732 KB Ok
56 Correct 81 ms 15852 KB Ok
57 Correct 56 ms 14704 KB Ok
58 Correct 62 ms 15188 KB Ok
59 Correct 57 ms 14828 KB Ok
60 Correct 51 ms 14448 KB Ok
61 Correct 55 ms 15036 KB Ok
62 Correct 70 ms 15476 KB Ok
63 Correct 60 ms 14832 KB Ok
64 Correct 82 ms 15596 KB Ok
65 Correct 67 ms 15344 KB Ok
66 Correct 70 ms 15088 KB Ok
67 Correct 59 ms 14956 KB Ok
68 Correct 73 ms 15216 KB Ok
69 Correct 216 ms 23156 KB Ok
70 Correct 242 ms 23704 KB Ok
71 Correct 235 ms 23248 KB Ok
72 Correct 232 ms 23188 KB Ok
73 Correct 236 ms 22772 KB Ok
74 Correct 219 ms 22636 KB Ok
75 Correct 211 ms 22520 KB Ok
76 Correct 243 ms 23284 KB Ok
77 Correct 218 ms 22224 KB Ok
78 Correct 222 ms 22548 KB Ok
79 Correct 226 ms 23104 KB Ok
80 Correct 221 ms 23156 KB Ok
81 Correct 245 ms 22644 KB Ok
82 Correct 219 ms 22956 KB Ok
83 Correct 226 ms 22536 KB Ok
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9720 KB Ok
2 Correct 10 ms 9720 KB Ok
3 Correct 11 ms 9720 KB Ok
4 Correct 10 ms 9720 KB Ok
5 Correct 10 ms 9720 KB Ok
6 Correct 10 ms 9720 KB Ok
7 Correct 12 ms 9848 KB Ok
8 Correct 11 ms 9720 KB Ok
9 Correct 12 ms 9720 KB Ok
10 Correct 12 ms 9720 KB Ok
11 Correct 12 ms 9848 KB Ok
12 Correct 13 ms 9692 KB Ok
13 Correct 12 ms 9720 KB Ok
14 Correct 11 ms 9720 KB Ok
15 Correct 10 ms 9720 KB Ok
16 Correct 10 ms 9720 KB Ok
17 Correct 10 ms 9720 KB Ok
18 Correct 12 ms 9976 KB Ok
19 Correct 22 ms 10744 KB Ok
20 Correct 15 ms 10108 KB Ok
21 Correct 23 ms 10872 KB Ok
22 Correct 18 ms 10360 KB Ok
23 Correct 10 ms 9592 KB Ok
24 Correct 13 ms 9848 KB Ok
25 Correct 12 ms 9720 KB Ok
26 Correct 10 ms 9720 KB Ok
27 Correct 10 ms 9720 KB Ok
28 Correct 11 ms 9720 KB Ok
29 Correct 10 ms 9720 KB Ok
30 Correct 10 ms 9720 KB Ok
31 Correct 10 ms 9720 KB Ok
32 Correct 10 ms 9764 KB Ok
33 Correct 10 ms 9720 KB Ok
34 Correct 10 ms 9720 KB Ok
35 Correct 10 ms 9720 KB Ok
36 Correct 10 ms 9720 KB Ok
37 Correct 10 ms 9848 KB Ok
38 Correct 10 ms 9720 KB Ok
39 Correct 116 ms 22380 KB Ok
40 Correct 92 ms 26576 KB Ok
41 Correct 179 ms 29552 KB Ok
42 Correct 149 ms 26844 KB Ok
43 Correct 66 ms 18924 KB Ok
44 Correct 126 ms 24824 KB Ok
45 Correct 12 ms 9848 KB Ok
46 Correct 11 ms 9976 KB Ok
47 Correct 11 ms 9848 KB Ok
48 Correct 11 ms 9848 KB Ok
49 Correct 11 ms 9848 KB Ok
50 Correct 12 ms 9848 KB Ok
51 Correct 11 ms 9848 KB Ok
52 Correct 12 ms 9848 KB Ok
53 Correct 12 ms 9848 KB Ok
54 Correct 12 ms 9848 KB Ok
55 Correct 15 ms 10108 KB Ok
56 Correct 15 ms 10104 KB Ok
57 Correct 15 ms 10104 KB Ok
58 Correct 17 ms 10232 KB Ok
59 Correct 17 ms 10104 KB Ok
60 Correct 17 ms 10020 KB Ok
61 Correct 15 ms 10104 KB Ok
62 Correct 16 ms 10104 KB Ok
63 Correct 16 ms 10104 KB Ok
64 Correct 17 ms 10104 KB Ok
65 Correct 72 ms 15420 KB Ok
66 Correct 80 ms 15732 KB Ok
67 Correct 81 ms 15852 KB Ok
68 Correct 56 ms 14704 KB Ok
69 Correct 62 ms 15188 KB Ok
70 Correct 57 ms 14828 KB Ok
71 Correct 51 ms 14448 KB Ok
72 Correct 55 ms 15036 KB Ok
73 Correct 70 ms 15476 KB Ok
74 Correct 60 ms 14832 KB Ok
75 Correct 82 ms 15596 KB Ok
76 Correct 67 ms 15344 KB Ok
77 Correct 70 ms 15088 KB Ok
78 Correct 59 ms 14956 KB Ok
79 Correct 73 ms 15216 KB Ok
80 Correct 216 ms 23156 KB Ok
81 Correct 242 ms 23704 KB Ok
82 Correct 235 ms 23248 KB Ok
83 Correct 232 ms 23188 KB Ok
84 Correct 236 ms 22772 KB Ok
85 Correct 219 ms 22636 KB Ok
86 Correct 211 ms 22520 KB Ok
87 Correct 243 ms 23284 KB Ok
88 Correct 218 ms 22224 KB Ok
89 Correct 222 ms 22548 KB Ok
90 Correct 226 ms 23104 KB Ok
91 Correct 221 ms 23156 KB Ok
92 Correct 245 ms 22644 KB Ok
93 Correct 219 ms 22956 KB Ok
94 Correct 226 ms 22536 KB Ok
95 Correct 176 ms 24300 KB Ok
96 Correct 241 ms 30820 KB Ok
97 Correct 214 ms 26996 KB Ok
98 Correct 168 ms 27108 KB Ok
99 Correct 181 ms 26688 KB Ok
100 Correct 211 ms 26216 KB Ok
101 Correct 208 ms 29088 KB Ok
102 Correct 209 ms 27236 KB Ok
103 Correct 214 ms 28196 KB Ok
104 Correct 255 ms 29944 KB Ok
105 Correct 241 ms 30312 KB Ok
106 Correct 232 ms 30668 KB Ok
107 Correct 237 ms 29628 KB Ok
108 Correct 324 ms 30308 KB Ok
109 Correct 305 ms 31692 KB Ok
110 Correct 1457 ms 63668 KB Ok
111 Correct 1744 ms 66008 KB Ok
112 Correct 1638 ms 63236 KB Ok
113 Correct 1593 ms 66208 KB Ok
114 Correct 1681 ms 66412 KB Ok
115 Correct 1582 ms 64044 KB Ok
116 Correct 1757 ms 66160 KB Ok
117 Correct 1660 ms 65344 KB Ok
118 Correct 1556 ms 65720 KB Ok
119 Correct 1645 ms 65032 KB Ok
120 Correct 1520 ms 64408 KB Ok
121 Correct 1506 ms 64388 KB Ok
122 Correct 1536 ms 64620 KB Ok
123 Correct 1705 ms 66196 KB Ok
124 Correct 1549 ms 61136 KB Ok
125 Correct 377 ms 49616 KB Ok