답안 #40367

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
40367 2018-01-31T12:05:58 Z model_code Nice sequence (IZhO18_sequence) C++11
100 / 100
733 ms 39572 KB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define vi vector <int>
#define ld long double
#define pii pair<int, int>
#define y1 sda
using namespace std;    
const int N = int(2e6) + 10, mod = int(1e9)  + 7; 

ll a[N], pref[N];
int pr[N], rnk[N], px[N], py[N], c[N];

int get(int v){
	if(pr[v] == v) return v;
	return pr[v] = get(pr[v]);
}

void Merge(int u,int v){
	u = get(u);
	v = get(v);
	if(u == v) return;
	if(rnk[u] > rnk[v]){
		pr[v] = u;
		rnk[u] += rnk[v];
	}
	else{
		pr[u] = v;
		rnk[v] += rnk[u];
	}
}

bool solve(int n,int m){
    bool sw = 0;
    if(n > m) {sw = 1, swap(n,m);}
    int ans = n + m - 1 - __gcd(n,m);
    if(m % n == 0){
        for(int i = 1; i < m; i++){
        	a[i] = -1;
        }
    }
    else{
    	ll ax = 0, bx = 0, ay = 0, by = 0;
    	for(int i = 1; i <= ans; i++){
    		pr[i] = i;
    		rnk[i] = 1;
    	}
    	for(int i = 1; i <= ans; i++){
    		if(i + n <= ans) Merge(i,i + n);
    		if(i + m <= ans) Merge(i,i + m);
    	}
    	int p1 = -1, p2 = -1;
    	int cn = 0;
    	for(int i = 1; i <= ans; i++){
    		get(i);
    		px[i] = py[i] = 0;
    		c[++cn] =  pr[i];
    	}
    	sort(c + 1, c + cn + 1);
    	cn = unique(c + 1, c + cn + 1) - c - 1;
    	for(int i = 1; i <= n; i++){
    		px[pr[i]]++;
    	}
    	for(int i = 1; i <= m; i++){
    		py[pr[i]]++;
    	}
    	ax = px[c[1]], ay = py[c[1]];
    	p1 = c[1];
    	for(int i = 2; i <= cn; i++){
    		if(1ll * ax * py[c[i]] > 1ll * ay * px[c[i]]){
    			ax = px[c[i]];
    			ay = py[c[i]];
    			p1 = c[i];
    		}
    	}
    	bx = n - ax;
    	by = m - ay;
    	ll vx = bx + by;
    	ll vy = -(ax + ay);
    	for(int i = 1; i <= ans; i++){
    		if(pr[i] == p1) a[i] = vx;
    		else a[i] = vy;
    	}

    }
    if(sw){
    	swap(n,m);
    	for(int i = 1; i <= ans; i++){
    		a[i] = -a[i];
    	}
    }
    printf("%d\n", ans);
    for(int i = 1; i <= ans; i++){
    	printf("%d", a[i]);
    	if(i < ans) printf(" ");
    }
    printf("\n");
}

int main () {
	int T,n,m;
	scanf("%d", &T);
	while(T--){
	    scanf("%d%d", &n, &m);
	    solve(n,m);
	}

return 0;
}

Compilation message

sequence.cpp: In function 'bool solve(int, int)':
sequence.cpp:70:19: warning: unused variable 'p2' [-Wunused-variable]
      int p1 = -1, p2 = -1;
                   ^
sequence.cpp:112:23: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
      printf("%d", a[i]);
                       ^
sequence.cpp:116:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
sequence.cpp: In function 'int main()':
sequence.cpp:120:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &T);
                 ^
sequence.cpp:122:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
      scanf("%d%d", &n, &m);
                           ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Ok
2 Correct 1 ms 356 KB Ok
3 Correct 1 ms 412 KB Ok
4 Correct 1 ms 480 KB Ok
5 Correct 1 ms 492 KB Ok
6 Correct 1 ms 552 KB Ok
7 Correct 2 ms 684 KB Ok
8 Correct 1 ms 684 KB Ok
9 Correct 1 ms 684 KB Ok
10 Correct 1 ms 684 KB Ok
11 Correct 1 ms 684 KB Ok
12 Correct 1 ms 684 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 684 KB Ok
2 Correct 2 ms 684 KB Ok
3 Correct 1 ms 684 KB Ok
4 Correct 2 ms 684 KB Ok
5 Correct 1 ms 684 KB Ok
6 Correct 7 ms 808 KB Ok
7 Correct 13 ms 1204 KB Ok
8 Correct 7 ms 1204 KB Ok
9 Correct 14 ms 1376 KB Ok
10 Correct 9 ms 1376 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1376 KB Ok
2 Correct 2 ms 1376 KB Ok
3 Correct 1 ms 1376 KB Ok
4 Correct 1 ms 1376 KB Ok
5 Correct 2 ms 1376 KB Ok
6 Correct 1 ms 1376 KB Ok
7 Correct 2 ms 1376 KB Ok
8 Correct 2 ms 1376 KB Ok
9 Correct 1 ms 1376 KB Ok
10 Correct 1 ms 1376 KB Ok
11 Correct 1 ms 1376 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1376 KB Ok
2 Correct 2 ms 1376 KB Ok
3 Correct 1 ms 1376 KB Ok
4 Correct 2 ms 1376 KB Ok
5 Correct 2 ms 1376 KB Ok
6 Correct 131 ms 9552 KB Ok
7 Correct 106 ms 9552 KB Ok
8 Correct 249 ms 12288 KB Ok
9 Correct 152 ms 12288 KB Ok
10 Correct 94 ms 12288 KB Ok
11 Correct 157 ms 12288 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Ok
2 Correct 1 ms 356 KB Ok
3 Correct 1 ms 412 KB Ok
4 Correct 1 ms 480 KB Ok
5 Correct 1 ms 492 KB Ok
6 Correct 1 ms 552 KB Ok
7 Correct 2 ms 684 KB Ok
8 Correct 1 ms 684 KB Ok
9 Correct 1 ms 684 KB Ok
10 Correct 1 ms 684 KB Ok
11 Correct 1 ms 684 KB Ok
12 Correct 1 ms 684 KB Ok
13 Correct 2 ms 1376 KB Ok
14 Correct 2 ms 1376 KB Ok
15 Correct 1 ms 1376 KB Ok
16 Correct 1 ms 1376 KB Ok
17 Correct 2 ms 1376 KB Ok
18 Correct 1 ms 1376 KB Ok
19 Correct 2 ms 1376 KB Ok
20 Correct 2 ms 1376 KB Ok
21 Correct 1 ms 1376 KB Ok
22 Correct 1 ms 1376 KB Ok
23 Correct 1 ms 1376 KB Ok
24 Correct 8 ms 12288 KB Ok
25 Correct 4 ms 12288 KB Ok
26 Correct 4 ms 12288 KB Ok
27 Correct 4 ms 12288 KB Ok
28 Correct 3 ms 12288 KB Ok
29 Correct 3 ms 12288 KB Ok
30 Correct 3 ms 12288 KB Ok
31 Correct 4 ms 12288 KB Ok
32 Correct 7 ms 12288 KB Ok
33 Correct 4 ms 12288 KB Ok
34 Correct 8 ms 12288 KB Ok
35 Correct 9 ms 12288 KB Ok
36 Correct 7 ms 12288 KB Ok
37 Correct 11 ms 12288 KB Ok
38 Correct 6 ms 12288 KB Ok
39 Correct 7 ms 12288 KB Ok
40 Correct 8 ms 12288 KB Ok
41 Correct 7 ms 12288 KB Ok
42 Correct 7 ms 12288 KB Ok
43 Correct 7 ms 12288 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Ok
2 Correct 1 ms 356 KB Ok
3 Correct 1 ms 412 KB Ok
4 Correct 1 ms 480 KB Ok
5 Correct 1 ms 492 KB Ok
6 Correct 1 ms 552 KB Ok
7 Correct 2 ms 684 KB Ok
8 Correct 1 ms 684 KB Ok
9 Correct 1 ms 684 KB Ok
10 Correct 1 ms 684 KB Ok
11 Correct 1 ms 684 KB Ok
12 Correct 1 ms 684 KB Ok
13 Correct 1 ms 684 KB Ok
14 Correct 2 ms 684 KB Ok
15 Correct 1 ms 684 KB Ok
16 Correct 2 ms 684 KB Ok
17 Correct 1 ms 684 KB Ok
18 Correct 7 ms 808 KB Ok
19 Correct 13 ms 1204 KB Ok
20 Correct 7 ms 1204 KB Ok
21 Correct 14 ms 1376 KB Ok
22 Correct 9 ms 1376 KB Ok
23 Correct 2 ms 1376 KB Ok
24 Correct 2 ms 1376 KB Ok
25 Correct 1 ms 1376 KB Ok
26 Correct 1 ms 1376 KB Ok
27 Correct 2 ms 1376 KB Ok
28 Correct 1 ms 1376 KB Ok
29 Correct 2 ms 1376 KB Ok
30 Correct 2 ms 1376 KB Ok
31 Correct 1 ms 1376 KB Ok
32 Correct 1 ms 1376 KB Ok
33 Correct 1 ms 1376 KB Ok
34 Correct 8 ms 12288 KB Ok
35 Correct 4 ms 12288 KB Ok
36 Correct 4 ms 12288 KB Ok
37 Correct 4 ms 12288 KB Ok
38 Correct 3 ms 12288 KB Ok
39 Correct 3 ms 12288 KB Ok
40 Correct 3 ms 12288 KB Ok
41 Correct 4 ms 12288 KB Ok
42 Correct 7 ms 12288 KB Ok
43 Correct 4 ms 12288 KB Ok
44 Correct 8 ms 12288 KB Ok
45 Correct 9 ms 12288 KB Ok
46 Correct 7 ms 12288 KB Ok
47 Correct 11 ms 12288 KB Ok
48 Correct 6 ms 12288 KB Ok
49 Correct 7 ms 12288 KB Ok
50 Correct 8 ms 12288 KB Ok
51 Correct 7 ms 12288 KB Ok
52 Correct 7 ms 12288 KB Ok
53 Correct 7 ms 12288 KB Ok
54 Correct 106 ms 12288 KB Ok
55 Correct 128 ms 12288 KB Ok
56 Correct 123 ms 12288 KB Ok
57 Correct 96 ms 12288 KB Ok
58 Correct 115 ms 12288 KB Ok
59 Correct 108 ms 12288 KB Ok
60 Correct 95 ms 12288 KB Ok
61 Correct 107 ms 12288 KB Ok
62 Correct 153 ms 12288 KB Ok
63 Correct 101 ms 12288 KB Ok
64 Correct 138 ms 12288 KB Ok
65 Correct 131 ms 12288 KB Ok
66 Correct 117 ms 12288 KB Ok
67 Correct 98 ms 12288 KB Ok
68 Correct 123 ms 12288 KB Ok
69 Correct 177 ms 12288 KB Ok
70 Correct 251 ms 12288 KB Ok
71 Correct 170 ms 12288 KB Ok
72 Correct 174 ms 12288 KB Ok
73 Correct 174 ms 12288 KB Ok
74 Correct 178 ms 12288 KB Ok
75 Correct 171 ms 12288 KB Ok
76 Correct 172 ms 12288 KB Ok
77 Correct 163 ms 12288 KB Ok
78 Correct 163 ms 12288 KB Ok
79 Correct 174 ms 12288 KB Ok
80 Correct 181 ms 12288 KB Ok
81 Correct 174 ms 12288 KB Ok
82 Correct 169 ms 12288 KB Ok
83 Correct 167 ms 12288 KB Ok
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Ok
2 Correct 1 ms 356 KB Ok
3 Correct 1 ms 412 KB Ok
4 Correct 1 ms 480 KB Ok
5 Correct 1 ms 492 KB Ok
6 Correct 1 ms 552 KB Ok
7 Correct 2 ms 684 KB Ok
8 Correct 1 ms 684 KB Ok
9 Correct 1 ms 684 KB Ok
10 Correct 1 ms 684 KB Ok
11 Correct 1 ms 684 KB Ok
12 Correct 1 ms 684 KB Ok
13 Correct 1 ms 684 KB Ok
14 Correct 2 ms 684 KB Ok
15 Correct 1 ms 684 KB Ok
16 Correct 2 ms 684 KB Ok
17 Correct 1 ms 684 KB Ok
18 Correct 7 ms 808 KB Ok
19 Correct 13 ms 1204 KB Ok
20 Correct 7 ms 1204 KB Ok
21 Correct 14 ms 1376 KB Ok
22 Correct 9 ms 1376 KB Ok
23 Correct 2 ms 1376 KB Ok
24 Correct 2 ms 1376 KB Ok
25 Correct 1 ms 1376 KB Ok
26 Correct 1 ms 1376 KB Ok
27 Correct 2 ms 1376 KB Ok
28 Correct 1 ms 1376 KB Ok
29 Correct 2 ms 1376 KB Ok
30 Correct 2 ms 1376 KB Ok
31 Correct 1 ms 1376 KB Ok
32 Correct 1 ms 1376 KB Ok
33 Correct 1 ms 1376 KB Ok
34 Correct 2 ms 1376 KB Ok
35 Correct 2 ms 1376 KB Ok
36 Correct 1 ms 1376 KB Ok
37 Correct 2 ms 1376 KB Ok
38 Correct 2 ms 1376 KB Ok
39 Correct 131 ms 9552 KB Ok
40 Correct 106 ms 9552 KB Ok
41 Correct 249 ms 12288 KB Ok
42 Correct 152 ms 12288 KB Ok
43 Correct 94 ms 12288 KB Ok
44 Correct 157 ms 12288 KB Ok
45 Correct 8 ms 12288 KB Ok
46 Correct 4 ms 12288 KB Ok
47 Correct 4 ms 12288 KB Ok
48 Correct 4 ms 12288 KB Ok
49 Correct 3 ms 12288 KB Ok
50 Correct 3 ms 12288 KB Ok
51 Correct 3 ms 12288 KB Ok
52 Correct 4 ms 12288 KB Ok
53 Correct 7 ms 12288 KB Ok
54 Correct 4 ms 12288 KB Ok
55 Correct 8 ms 12288 KB Ok
56 Correct 9 ms 12288 KB Ok
57 Correct 7 ms 12288 KB Ok
58 Correct 11 ms 12288 KB Ok
59 Correct 6 ms 12288 KB Ok
60 Correct 7 ms 12288 KB Ok
61 Correct 8 ms 12288 KB Ok
62 Correct 7 ms 12288 KB Ok
63 Correct 7 ms 12288 KB Ok
64 Correct 7 ms 12288 KB Ok
65 Correct 106 ms 12288 KB Ok
66 Correct 128 ms 12288 KB Ok
67 Correct 123 ms 12288 KB Ok
68 Correct 96 ms 12288 KB Ok
69 Correct 115 ms 12288 KB Ok
70 Correct 108 ms 12288 KB Ok
71 Correct 95 ms 12288 KB Ok
72 Correct 107 ms 12288 KB Ok
73 Correct 153 ms 12288 KB Ok
74 Correct 101 ms 12288 KB Ok
75 Correct 138 ms 12288 KB Ok
76 Correct 131 ms 12288 KB Ok
77 Correct 117 ms 12288 KB Ok
78 Correct 98 ms 12288 KB Ok
79 Correct 123 ms 12288 KB Ok
80 Correct 177 ms 12288 KB Ok
81 Correct 251 ms 12288 KB Ok
82 Correct 170 ms 12288 KB Ok
83 Correct 174 ms 12288 KB Ok
84 Correct 174 ms 12288 KB Ok
85 Correct 178 ms 12288 KB Ok
86 Correct 171 ms 12288 KB Ok
87 Correct 172 ms 12288 KB Ok
88 Correct 163 ms 12288 KB Ok
89 Correct 163 ms 12288 KB Ok
90 Correct 174 ms 12288 KB Ok
91 Correct 181 ms 12288 KB Ok
92 Correct 174 ms 12288 KB Ok
93 Correct 169 ms 12288 KB Ok
94 Correct 167 ms 12288 KB Ok
95 Correct 238 ms 12324 KB Ok
96 Correct 408 ms 17684 KB Ok
97 Correct 429 ms 17684 KB Ok
98 Correct 312 ms 17684 KB Ok
99 Correct 433 ms 17684 KB Ok
100 Correct 388 ms 17684 KB Ok
101 Correct 401 ms 17684 KB Ok
102 Correct 375 ms 17684 KB Ok
103 Correct 384 ms 17684 KB Ok
104 Correct 415 ms 17684 KB Ok
105 Correct 420 ms 17684 KB Ok
106 Correct 323 ms 17684 KB Ok
107 Correct 403 ms 17684 KB Ok
108 Correct 441 ms 18572 KB Ok
109 Correct 380 ms 18572 KB Ok
110 Correct 678 ms 36924 KB Ok
111 Correct 672 ms 38960 KB Ok
112 Correct 691 ms 39488 KB Ok
113 Correct 699 ms 39488 KB Ok
114 Correct 733 ms 39572 KB Ok
115 Correct 695 ms 39572 KB Ok
116 Correct 692 ms 39572 KB Ok
117 Correct 668 ms 39572 KB Ok
118 Correct 661 ms 39572 KB Ok
119 Correct 689 ms 39572 KB Ok
120 Correct 712 ms 39572 KB Ok
121 Correct 628 ms 39572 KB Ok
122 Correct 642 ms 39572 KB Ok
123 Correct 689 ms 39572 KB Ok
124 Correct 642 ms 39572 KB Ok
125 Correct 594 ms 39572 KB Ok