// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i=0;i<(n);++i)
#define FOO(i,a,b) for(int i=(a);i<=int(b);++i)
#define OOF(i,a,b) for(int i=(a);i>=int(b);--i)
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
inline ll cdiv(ll x,ll m){return x/m+(x%m?(x<0)^(m>0):0);}
#ifdef OWO
#define safe cerr<<"\033[1;32m"<<__PRETTY_FUNCTION__<<" - "<<__LINE__<<" JIZZ\033[0m\n"
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
#else
#pragma GCC optimize("Ofast")
#define safe ((void)0)
#define debug(...) ((void)0)
#endif
const ll inf = 1e9, INF = 4e18;
bool check(int n, int m, int len, bool out = false){
vector<int> in(len + 1, 0);
FOO(i,0,len){
if(i + n <= len) ++in[i + n];
if(i - m >= 0) ++in[i - m];
}
vector<int> q;
FOO(i,0,len) if(in[i] == 0) q.pb(i);
if(q.empty()) return false;
FOR(i,q.size()){
int u = q[i];
if(u + n <= len)
if(--in[u + n] == 0) q.pb(u + n);
if(u - m >= 0)
if(--in[u - m] == 0) q.pb(u - m);
}
if(q.size() != len + 1) return false;
if(!out) return true;
vector<int> ans(len + 1);
int val = len + 1;
FOO(i,0,len){
int p = q[i];
ans[p] = --val;
if(q[i] == 0) ans[p] = 0;
val = ans[p];
}
cout << len << '\n';
FOO(i,1,len) cout << ans[i] - ans[i-1] << ' ';
cout << endl;
return true;
}
void solve(){
int n, m; cin >> n >> m;
int l = 16;
while(check(n, m, l << 1)) l <<= 1;
int ans = 0;
for(; l > 0; l >>= 1)
if(check(n, m, ans | l))
ans |= l;
check(n, m, ans, true);
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t; cin >> t;
for(; t; --t) solve();
return 0;
}
Compilation message
sequence.cpp: In function 'bool check(int, int, int, bool)':
sequence.cpp:8:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define FOR(i,n) for(int i=0;i<(n);++i)
| ^
sequence.cpp:42:2: note: in expansion of macro 'FOR'
42 | FOR(i,q.size()){
| ^~~
sequence.cpp:50:14: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | if(q.size() != len + 1) return false;
| ~~~~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
4 ms |
492 KB |
Ok |
7 |
Correct |
19 ms |
844 KB |
Ok |
8 |
Correct |
9 ms |
620 KB |
Ok |
9 |
Correct |
23 ms |
948 KB |
Ok |
10 |
Correct |
16 ms |
620 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
392 KB |
Ok |
5 |
Correct |
1 ms |
384 KB |
Ok |
6 |
Correct |
197 ms |
4876 KB |
Ok |
7 |
Correct |
170 ms |
3964 KB |
Ok |
8 |
Correct |
373 ms |
7124 KB |
Ok |
9 |
Correct |
278 ms |
8124 KB |
Ok |
10 |
Correct |
156 ms |
3112 KB |
Ok |
11 |
Correct |
292 ms |
6896 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
13 |
Correct |
1 ms |
364 KB |
Ok |
14 |
Correct |
1 ms |
364 KB |
Ok |
15 |
Correct |
1 ms |
364 KB |
Ok |
16 |
Correct |
1 ms |
364 KB |
Ok |
17 |
Correct |
1 ms |
364 KB |
Ok |
18 |
Correct |
1 ms |
364 KB |
Ok |
19 |
Correct |
1 ms |
364 KB |
Ok |
20 |
Correct |
1 ms |
364 KB |
Ok |
21 |
Correct |
1 ms |
364 KB |
Ok |
22 |
Correct |
1 ms |
364 KB |
Ok |
23 |
Correct |
1 ms |
364 KB |
Ok |
24 |
Correct |
5 ms |
492 KB |
Ok |
25 |
Correct |
4 ms |
492 KB |
Ok |
26 |
Correct |
4 ms |
512 KB |
Ok |
27 |
Correct |
4 ms |
492 KB |
Ok |
28 |
Correct |
4 ms |
364 KB |
Ok |
29 |
Correct |
3 ms |
364 KB |
Ok |
30 |
Correct |
4 ms |
364 KB |
Ok |
31 |
Correct |
4 ms |
492 KB |
Ok |
32 |
Correct |
5 ms |
492 KB |
Ok |
33 |
Correct |
4 ms |
492 KB |
Ok |
34 |
Correct |
8 ms |
492 KB |
Ok |
35 |
Correct |
7 ms |
620 KB |
Ok |
36 |
Correct |
9 ms |
492 KB |
Ok |
37 |
Correct |
9 ms |
620 KB |
Ok |
38 |
Correct |
7 ms |
492 KB |
Ok |
39 |
Correct |
8 ms |
492 KB |
Ok |
40 |
Correct |
8 ms |
620 KB |
Ok |
41 |
Correct |
9 ms |
620 KB |
Ok |
42 |
Correct |
8 ms |
492 KB |
Ok |
43 |
Correct |
8 ms |
620 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
13 |
Correct |
1 ms |
364 KB |
Ok |
14 |
Correct |
1 ms |
364 KB |
Ok |
15 |
Correct |
1 ms |
364 KB |
Ok |
16 |
Correct |
1 ms |
364 KB |
Ok |
17 |
Correct |
1 ms |
364 KB |
Ok |
18 |
Correct |
4 ms |
492 KB |
Ok |
19 |
Correct |
19 ms |
844 KB |
Ok |
20 |
Correct |
9 ms |
620 KB |
Ok |
21 |
Correct |
23 ms |
948 KB |
Ok |
22 |
Correct |
16 ms |
620 KB |
Ok |
23 |
Correct |
1 ms |
364 KB |
Ok |
24 |
Correct |
1 ms |
364 KB |
Ok |
25 |
Correct |
1 ms |
364 KB |
Ok |
26 |
Correct |
1 ms |
364 KB |
Ok |
27 |
Correct |
1 ms |
364 KB |
Ok |
28 |
Correct |
1 ms |
364 KB |
Ok |
29 |
Correct |
1 ms |
364 KB |
Ok |
30 |
Correct |
1 ms |
364 KB |
Ok |
31 |
Correct |
1 ms |
364 KB |
Ok |
32 |
Correct |
1 ms |
364 KB |
Ok |
33 |
Correct |
1 ms |
364 KB |
Ok |
34 |
Correct |
5 ms |
492 KB |
Ok |
35 |
Correct |
4 ms |
492 KB |
Ok |
36 |
Correct |
4 ms |
512 KB |
Ok |
37 |
Correct |
4 ms |
492 KB |
Ok |
38 |
Correct |
4 ms |
364 KB |
Ok |
39 |
Correct |
3 ms |
364 KB |
Ok |
40 |
Correct |
4 ms |
364 KB |
Ok |
41 |
Correct |
4 ms |
492 KB |
Ok |
42 |
Correct |
5 ms |
492 KB |
Ok |
43 |
Correct |
4 ms |
492 KB |
Ok |
44 |
Correct |
8 ms |
492 KB |
Ok |
45 |
Correct |
7 ms |
620 KB |
Ok |
46 |
Correct |
9 ms |
492 KB |
Ok |
47 |
Correct |
9 ms |
620 KB |
Ok |
48 |
Correct |
7 ms |
492 KB |
Ok |
49 |
Correct |
8 ms |
492 KB |
Ok |
50 |
Correct |
8 ms |
620 KB |
Ok |
51 |
Correct |
9 ms |
620 KB |
Ok |
52 |
Correct |
8 ms |
492 KB |
Ok |
53 |
Correct |
8 ms |
620 KB |
Ok |
54 |
Correct |
194 ms |
3572 KB |
Ok |
55 |
Correct |
245 ms |
4004 KB |
Ok |
56 |
Correct |
231 ms |
3752 KB |
Ok |
57 |
Correct |
169 ms |
3056 KB |
Ok |
58 |
Correct |
220 ms |
3720 KB |
Ok |
59 |
Correct |
203 ms |
3440 KB |
Ok |
60 |
Correct |
197 ms |
3212 KB |
Ok |
61 |
Correct |
169 ms |
3160 KB |
Ok |
62 |
Correct |
251 ms |
4000 KB |
Ok |
63 |
Correct |
185 ms |
3248 KB |
Ok |
64 |
Correct |
239 ms |
3740 KB |
Ok |
65 |
Correct |
235 ms |
3948 KB |
Ok |
66 |
Correct |
199 ms |
3580 KB |
Ok |
67 |
Correct |
175 ms |
3348 KB |
Ok |
68 |
Correct |
222 ms |
3380 KB |
Ok |
69 |
Correct |
272 ms |
7140 KB |
Ok |
70 |
Correct |
237 ms |
8404 KB |
Ok |
71 |
Correct |
311 ms |
6780 KB |
Ok |
72 |
Correct |
260 ms |
7512 KB |
Ok |
73 |
Correct |
259 ms |
7164 KB |
Ok |
74 |
Correct |
254 ms |
6428 KB |
Ok |
75 |
Correct |
267 ms |
6464 KB |
Ok |
76 |
Correct |
247 ms |
8120 KB |
Ok |
77 |
Correct |
266 ms |
6064 KB |
Ok |
78 |
Correct |
243 ms |
7740 KB |
Ok |
79 |
Correct |
269 ms |
7484 KB |
Ok |
80 |
Correct |
270 ms |
7200 KB |
Ok |
81 |
Correct |
259 ms |
7820 KB |
Ok |
82 |
Correct |
262 ms |
7380 KB |
Ok |
83 |
Correct |
277 ms |
6444 KB |
Ok |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Ok |
2 |
Correct |
1 ms |
364 KB |
Ok |
3 |
Correct |
1 ms |
364 KB |
Ok |
4 |
Correct |
1 ms |
364 KB |
Ok |
5 |
Correct |
1 ms |
364 KB |
Ok |
6 |
Correct |
1 ms |
364 KB |
Ok |
7 |
Correct |
1 ms |
364 KB |
Ok |
8 |
Correct |
1 ms |
364 KB |
Ok |
9 |
Correct |
1 ms |
364 KB |
Ok |
10 |
Correct |
1 ms |
364 KB |
Ok |
11 |
Correct |
1 ms |
364 KB |
Ok |
12 |
Correct |
1 ms |
364 KB |
Ok |
13 |
Correct |
1 ms |
364 KB |
Ok |
14 |
Correct |
1 ms |
364 KB |
Ok |
15 |
Correct |
1 ms |
364 KB |
Ok |
16 |
Correct |
1 ms |
364 KB |
Ok |
17 |
Correct |
1 ms |
364 KB |
Ok |
18 |
Correct |
4 ms |
492 KB |
Ok |
19 |
Correct |
19 ms |
844 KB |
Ok |
20 |
Correct |
9 ms |
620 KB |
Ok |
21 |
Correct |
23 ms |
948 KB |
Ok |
22 |
Correct |
16 ms |
620 KB |
Ok |
23 |
Correct |
1 ms |
364 KB |
Ok |
24 |
Correct |
1 ms |
364 KB |
Ok |
25 |
Correct |
1 ms |
364 KB |
Ok |
26 |
Correct |
1 ms |
364 KB |
Ok |
27 |
Correct |
1 ms |
364 KB |
Ok |
28 |
Correct |
1 ms |
364 KB |
Ok |
29 |
Correct |
1 ms |
364 KB |
Ok |
30 |
Correct |
1 ms |
364 KB |
Ok |
31 |
Correct |
1 ms |
364 KB |
Ok |
32 |
Correct |
1 ms |
364 KB |
Ok |
33 |
Correct |
1 ms |
364 KB |
Ok |
34 |
Correct |
1 ms |
364 KB |
Ok |
35 |
Correct |
1 ms |
364 KB |
Ok |
36 |
Correct |
1 ms |
364 KB |
Ok |
37 |
Correct |
1 ms |
392 KB |
Ok |
38 |
Correct |
1 ms |
384 KB |
Ok |
39 |
Correct |
197 ms |
4876 KB |
Ok |
40 |
Correct |
170 ms |
3964 KB |
Ok |
41 |
Correct |
373 ms |
7124 KB |
Ok |
42 |
Correct |
278 ms |
8124 KB |
Ok |
43 |
Correct |
156 ms |
3112 KB |
Ok |
44 |
Correct |
292 ms |
6896 KB |
Ok |
45 |
Correct |
5 ms |
492 KB |
Ok |
46 |
Correct |
4 ms |
492 KB |
Ok |
47 |
Correct |
4 ms |
512 KB |
Ok |
48 |
Correct |
4 ms |
492 KB |
Ok |
49 |
Correct |
4 ms |
364 KB |
Ok |
50 |
Correct |
3 ms |
364 KB |
Ok |
51 |
Correct |
4 ms |
364 KB |
Ok |
52 |
Correct |
4 ms |
492 KB |
Ok |
53 |
Correct |
5 ms |
492 KB |
Ok |
54 |
Correct |
4 ms |
492 KB |
Ok |
55 |
Correct |
8 ms |
492 KB |
Ok |
56 |
Correct |
7 ms |
620 KB |
Ok |
57 |
Correct |
9 ms |
492 KB |
Ok |
58 |
Correct |
9 ms |
620 KB |
Ok |
59 |
Correct |
7 ms |
492 KB |
Ok |
60 |
Correct |
8 ms |
492 KB |
Ok |
61 |
Correct |
8 ms |
620 KB |
Ok |
62 |
Correct |
9 ms |
620 KB |
Ok |
63 |
Correct |
8 ms |
492 KB |
Ok |
64 |
Correct |
8 ms |
620 KB |
Ok |
65 |
Correct |
194 ms |
3572 KB |
Ok |
66 |
Correct |
245 ms |
4004 KB |
Ok |
67 |
Correct |
231 ms |
3752 KB |
Ok |
68 |
Correct |
169 ms |
3056 KB |
Ok |
69 |
Correct |
220 ms |
3720 KB |
Ok |
70 |
Correct |
203 ms |
3440 KB |
Ok |
71 |
Correct |
197 ms |
3212 KB |
Ok |
72 |
Correct |
169 ms |
3160 KB |
Ok |
73 |
Correct |
251 ms |
4000 KB |
Ok |
74 |
Correct |
185 ms |
3248 KB |
Ok |
75 |
Correct |
239 ms |
3740 KB |
Ok |
76 |
Correct |
235 ms |
3948 KB |
Ok |
77 |
Correct |
199 ms |
3580 KB |
Ok |
78 |
Correct |
175 ms |
3348 KB |
Ok |
79 |
Correct |
222 ms |
3380 KB |
Ok |
80 |
Correct |
272 ms |
7140 KB |
Ok |
81 |
Correct |
237 ms |
8404 KB |
Ok |
82 |
Correct |
311 ms |
6780 KB |
Ok |
83 |
Correct |
260 ms |
7512 KB |
Ok |
84 |
Correct |
259 ms |
7164 KB |
Ok |
85 |
Correct |
254 ms |
6428 KB |
Ok |
86 |
Correct |
267 ms |
6464 KB |
Ok |
87 |
Correct |
247 ms |
8120 KB |
Ok |
88 |
Correct |
266 ms |
6064 KB |
Ok |
89 |
Correct |
243 ms |
7740 KB |
Ok |
90 |
Correct |
269 ms |
7484 KB |
Ok |
91 |
Correct |
270 ms |
7200 KB |
Ok |
92 |
Correct |
259 ms |
7820 KB |
Ok |
93 |
Correct |
262 ms |
7380 KB |
Ok |
94 |
Correct |
277 ms |
6444 KB |
Ok |
95 |
Correct |
536 ms |
8076 KB |
Ok |
96 |
Correct |
772 ms |
11928 KB |
Ok |
97 |
Correct |
758 ms |
10612 KB |
Ok |
98 |
Correct |
531 ms |
8648 KB |
Ok |
99 |
Correct |
646 ms |
10032 KB |
Ok |
100 |
Correct |
639 ms |
10104 KB |
Ok |
101 |
Correct |
705 ms |
11452 KB |
Ok |
102 |
Correct |
717 ms |
11220 KB |
Ok |
103 |
Correct |
628 ms |
9780 KB |
Ok |
104 |
Correct |
835 ms |
11140 KB |
Ok |
105 |
Correct |
808 ms |
12292 KB |
Ok |
106 |
Correct |
601 ms |
9460 KB |
Ok |
107 |
Correct |
741 ms |
11384 KB |
Ok |
108 |
Correct |
800 ms |
12296 KB |
Ok |
109 |
Correct |
640 ms |
10300 KB |
Ok |
110 |
Correct |
1160 ms |
25484 KB |
Ok |
111 |
Correct |
1231 ms |
33348 KB |
Ok |
112 |
Correct |
1331 ms |
27552 KB |
Ok |
113 |
Correct |
1195 ms |
30652 KB |
Ok |
114 |
Correct |
1209 ms |
32004 KB |
Ok |
115 |
Correct |
1239 ms |
30940 KB |
Ok |
116 |
Correct |
1247 ms |
31100 KB |
Ok |
117 |
Correct |
1202 ms |
31716 KB |
Ok |
118 |
Correct |
1306 ms |
28092 KB |
Ok |
119 |
Correct |
1265 ms |
32288 KB |
Ok |
120 |
Correct |
1287 ms |
28320 KB |
Ok |
121 |
Correct |
1258 ms |
27248 KB |
Ok |
122 |
Correct |
1384 ms |
29856 KB |
Ok |
123 |
Correct |
1276 ms |
34120 KB |
Ok |
124 |
Correct |
1236 ms |
24824 KB |
Ok |
125 |
Correct |
1202 ms |
15736 KB |
Ok |