QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#116062#5280. Depot RearrangementLaurie#100 ✓19ms23156kbC++143.3kb2023-06-28 08:45:492024-05-31 14:20:12

Judging History

你现在查看的是最新测评结果

  • [2024-05-31 14:20:12]
  • 评测
  • 测评结果:100
  • 用时:19ms
  • 内存:23156kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-28 08:45:49]
  • 提交

answer

//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
namespace fastIO {
	template<class T>
	inline void read(T &x) {
		x=0;
		bool fu=0;
		char ch=0;
		while(ch>'9'||ch<'0') {
			ch=getchar();
			if(ch=='-')fu=1;
		}
		while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
		if(fu)x=-x;
	}
	inline int read() {
		int x=0;
		bool fu=0;
		char ch=0;
		while(ch>'9'||ch<'0') {
			ch=getchar();
			if(ch=='-')fu=1;
		}
		while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
		return fu?-x:x;
	}
	template<class T,class... Args>
	inline void read(T& t,Args&... args) {
		read(t);
		read(args...);
	}
	char _n_u_m_[40];
	template<class T>
	inline void write(T x ) {
		if(x==0){
			putchar('0');
			return;
		}
		T tmp = x > 0 ? x : -x ;
		if( x < 0 ) putchar('-') ;
		register int cnt = 0 ;
		while( tmp > 0 ) {
			_n_u_m_[ cnt ++ ] = tmp % 10 + '0' ;
			tmp /= 10 ;
		}
		while( cnt > 0 ) putchar(_n_u_m_[ -- cnt ]) ;
	}
	template<class T>
	inline void write(T x ,char ch) {
		write(x);
		putchar(ch);
	}
}
using namespace fastIO;
const int MAXN=1000005,MAXM=4000005;
int head[MAXN],nxt[MAXM],n,m,to[MAXM],x,y,sta[MAXM],cnt,num;
vector<pii >ans;
bool vis[MAXM>>1];
inline void dfs(int pos){
	for(int &e=head[pos];e;e=nxt[e]){
		if(vis[e])continue;
		int temp=e;
		vis[e]=1;
		dfs(to[e]);
		sta[++cnt]=temp;
	}
}
inline void ed(int x,int y){
	to[++num]=y;
	nxt[num]=head[x];
	head[x]=num;
}
vector<int>v[402][402];
vector<int>A;
int main() {
	cin>>n>>m;
	A.push_back(0);
	F(i,1,n){
		F(j,1,m){
			int w=read();A.push_back(w);
			v[i][w].push_back((i-1)*m+j);
		}
		F(j,1,m){
			if(v[i][j].empty())ed(MAXN-j,i);
			if(v[i][j].size()>=2)F(k,1,v[i][j].size()-1)ed(i,MAXN-j);
		}
	}
	A.push_back(0);
	F(i,1,MAXN-1)if(head[i]&&!vis[head[i]]){
		cnt=0;
		dfs(i);
		reverse(sta+1,sta+cnt+1);
		F(j,1,cnt)sta[j]=to[sta[j]];
		sta[0]=sta[cnt],sta[cnt+1]=sta[1];
		vector<int>R;
		F(j,1,cnt){
			if(sta[j]>n){
				R.push_back(v[sta[j-1]][MAXN-sta[j]].back());
				v[sta[j-1]][MAXN-sta[j]].pop_back();
			}
		}
		
		ans.emplace_back(R.back(),n*m+1);
		
		UF(i,R.size()-2,0)ans.emplace_back(R[i],R[i+1]);
		ans.emplace_back(n*m+1,R[0]);
	}
	write(ans.size(),'\n');
	for(auto i:ans){
		write(i.first,' ');write(i.second,'\n');
		A[i.second]=A[i.first];
	}
//	F(i,1,n*m+1)cerr<<A[i]<<" ";
	return 0;
}
/*
5 6
4 1 3 1 6 5 2 3 2 3 5 6 2 1 4 5 6 4 1 3 2 4 5 5 1 2 3 4 6 6
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 3ms
memory: 13380kb

input:

10 3
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3


output:

0

result:

ok both subtasks are correct!

Test #2:

score: 5
Accepted
time: 0ms
memory: 14424kb

input:

5 4
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4


output:

13
10 21
2 10
14 2
7 14
15 7
3 15
18 3
8 18
19 8
12 19
20 12
4 20
21 4

result:

ok both subtasks are correct!

Test #3:

score: 5
Accepted
time: 0ms
memory: 15256kb

input:

10 10
8 10 10 3 7 3 5 6 1 4 3 8 2 9 1 8 4 2 7 3 10 7 9 2 1 10 10 9 1 2 9 7 4 5 2 9 10 5 7 6 6 8 6 8 4 2 9 1 2 8 6 1 4 2 2 1 5 6 3 10 10 7 9 4 8 9 8 2 5 6 4 3 1 6 3 3 10 7 7 5 3 6 8 5 9 4 6 7 9 4 10 5 3 4 5 1 1 7 8 5


output:

32
18 101
38 18
29 38
75 29
30 75
90 30
49 90
39 49
20 39
43 20
6 43
28 6
67 28
56 67
66 56
76 66
36 76
16 36
26 16
87 26
97 87
55 97
44 55
27 44
95 27
58 95
79 58
50 79
100 50
89 100
3 89
101 3

result:

ok both subtasks are correct!

Test #4:

score: 5
Accepted
time: 2ms
memory: 14144kb

input:

100 10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 9 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10...

output:

19
453 1001
184 453
734 184
713 734
819 713
359 819
775 359
175 775
210 175
830 210
670 830
850 670
814 850
949 814
109 949
1001 109
747 1001
707 747
1001 707

result:

ok both subtasks are correct!

Test #5:

score: 5
Accepted
time: 0ms
memory: 15204kb

input:

200 100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

195
13073 20001
12213 13073
10354 12213
8254 10354
16367 8254
13167 16367
6867 13167
5967 6867
7667 5967
2067 7667
16854 2067
7854 16854
4955 7854
3641 4955
3971 3641
3334 3971
5034 3334
17871 5034
459 17871
1541 459
17469 1541
1569 17469
10541 1569
8875 10541
475 8875
12259 475
1488 12259
5159 1488...

result:

ok both subtasks are correct!

Test #6:

score: 5
Accepted
time: 0ms
memory: 15196kb

input:

201 20
20 18 5 5 1 7 8 17 12 10 20 12 13 19 16 2 9 8 20 20 19 10 17 20 9 11 15 17 9 2 3 4 17 10 7 20 7 19 17 11 20 2 1 13 11 9 11 6 10 8 11 3 2 16 9 15 16 12 13 6 5 13 4 13 3 8 20 18 10 3 14 1 11 20 17 17 2 11 20 1 4 10 3 3 9 13 7 10 19 16 14 16 9 19 14 15 12 9 20 12 2 19 18 2 7 7 2 12 10 8 20 18 16...

output:

1401
84 4021
53 84
37 53
80 37
93 80
258 93
29 258
4 29
60 4
169 60
211 169
104 211
331 104
107 331
358 107
34 358
18 34
40 18
12 40
140 12
144 140
416 144
98 416
171 98
70 171
187 70
380 187
88 380
78 88
100 78
499 100
55 499
28 55
178 28
155 178
129 155
47 129
313 47
357 313
436 357
130 436
193 13...

result:

ok both subtasks are correct!

Test #7:

score: 5
Accepted
time: 0ms
memory: 17420kb

input:

300 300
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

205
72662 90001
11820 72662
72720 11820
75733 72720
37143 75733
68130 37143
22642 68130
36376 22642
38476 36376
39588 38476
77388 39588
69294 77388
1916 69294
55164 1916
13556 55164
85710 13556
20910 85710
42644 20910
44971 42644
53371 44971
26373 53371
19174 26373
85356 19174
31656 85356
76686 3165...

result:

ok both subtasks are correct!

Test #8:

score: 5
Accepted
time: 4ms
memory: 15912kb

input:

301 40
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

11624
322 12041
42 322
362 42
82 362
402 82
122 402
442 122
162 442
482 162
202 482
522 202
242 522
562 242
2 562
642 2
602 642
43 602
643 43
83 643
682 83
123 682
722 123
163 722
762 163
203 762
802 203
243 802
842 243
282 842
683 282
323 683
723 323
363 723
763 363
403 763
803 403
443 803
843 443
...

result:

ok both subtasks are correct!

Test #9:

score: 5
Accepted
time: 0ms
memory: 15824kb

input:

400 100
11 65 1 79 15 18 79 46 9 30 71 53 58 55 94 73 39 16 6 91 49 30 23 30 28 81 90 48 97 54 79 30 94 18 42 77 44 36 5 48 55 97 79 36 41 59 79 71 32 59 3 10 63 52 44 41 9 46 31 31 56 87 60 80 12 51 15 78 41 65 95 34 29 83 46 64 37 53 98 17 41 45 36 73 20 53 48 80 57 54 57 72 39 56 98 6 10 78 11 72...

output:

14592
196 40001
242 196
172 242
96 172
489 96
743 489
1181 743
1099 1181
249 1099
1356 249
337 1356
57 337
600 57
480 600
97 480
482 97
1644 482
345 1644
163 345
1571 163
639 1571
1771 639
2166 1771
761 2166
1998 761
571 1998
139 571
2266 139
381 2266
2953 381
2644 2953
783 2644
287 783
158 287
2899...

result:

ok both subtasks are correct!

Test #10:

score: 5
Accepted
time: 0ms
memory: 15184kb

input:

40 160
17 2 3 4 5 6 7 91 9 10 154 12 103 14 15 16 17 25 19 58 21 8 23 24 52 26 27 58 120 105 50 55 104 32 35 36 37 38 45 10 41 42 43 44 45 71 47 48 49 34 140 52 53 54 115 44 28 58 59 60 61 62 63 64 132 66 67 68 69 70 71 69 24 74 75 76 77 133 79 80 81 82 100 84 31 86 87 88 100 90 91 92 93 94 95 96 97...

output:

1316
338 6401
137 338
2009 137
977 2009
806 977
182 806
40 182
353 40
234 353
839 234
1034 839
510 1034
750 510
1119 750
1428 1119
2105 1428
17 2105
757 17
276 757
396 276
1025 396
938 1025
281 938
73 281
192 73
2805 192
1640 2805
765 1640
2337 765
1581 2337
1804 1581
1560 1804
534 1560
3288 534
241...

result:

ok both subtasks are correct!

Test #11:

score: 5
Accepted
time: 0ms
memory: 15560kb

input:

400 100
88 82 9 2 90 1 83 32 32 79 8 79 63 67 85 82 50 63 69 2 7 91 21 90 69 3 39 78 66 83 96 53 24 65 56 63 90 54 35 55 94 22 76 12 54 55 5 49 91 73 8 19 64 54 39 23 13 27 34 4 81 52 13 11 36 45 3 50 82 81 42 50 75 15 99 70 29 26 70 66 34 15 42 83 16 19 19 12 76 1 68 49 7 17 64 37 98 34 99 37 34 64...

output:

14611
495 40001
794 495
170 794
397 170
159 397
982 159
253 982
186 253
1183 186
550 1183
483 550
1362 483
558 1362
1097 558
577 1097
353 577
293 353
1825 293
866 1825
1618 866
892 1618
374 892
592 374
298 592
1860 298
1970 1860
1265 1970
393 1265
300 393
2159 300
2041 2159
90 2041
311 90
195 311
32...

result:

ok both subtasks are correct!

Test #12:

score: 5
Accepted
time: 3ms
memory: 13896kb

input:

301 20
8 1 1 1 1 1 1 17 1 9 1 5 1 1 1 1 13 1 9 1 18 1 1 16 1 15 5 19 1 8 11 10 1 1 1 1 18 4 1 1 1 1 16 1 1 1 12 10 1 1 1 14 11 13 1 1 1 1 1 1 10 1 1 1 1 1 1 19 14 1 1 1 5 1 1 1 1 13 1 18 1 1 4 1 1 1 1 1 1 1 1 1 1 16 16 10 1 14 18 1 1 1 7 1 1 1 1 6 9 1 13 1 1 1 2 1 1 1 1 1 1 10 1 1 1 17 1 10 10 1 12 ...

output:

4260
322 6021
23 322
343 23
42 343
362 42
63 362
383 63
82 383
402 82
123 402
423 123
143 423
445 143
162 445
482 162
203 482
502 203
242 502
524 242
262 524
544 262
283 544
602 283
3 602
622 3
25 622
642 25
44 642
665 44
64 665
723 64
84 723
742 84
102 742
763 102
124 763
784 124
144 784
802 144
16...

result:

ok both subtasks are correct!

Test #13:

score: 5
Accepted
time: 10ms
memory: 19700kb

input:

300 300
215 159 263 206 201 183 286 56 142 10 231 214 34 54 263 250 169 208 239 148 104 22 244 17 74 68 184 52 2 30 42 83 222 106 25 152 37 225 213 213 69 273 91 221 207 48 166 28 221 50 46 64 10 254 207 109 206 144 270 291 195 197 253 235 141 186 102 68 52 24 38 6 181 44 256 200 77 233 285 163 223 ...

output:

32648
1710 90001
597 1710
1962 597
712 1962
2525 712
861 2525
212 861
1590 212
3254 1590
231 3254
1896 231
3211 1896
974 3211
5432 974
1014 5432
2491 1014
6099 2491
1458 6099
53 1458
1243 53
487 1243
660 487
1651 660
100 1651
7588 100
3504 7588
218 3504
667 218
567 667
1094 567
1244 1094
1673 1244
1...

result:

ok both subtasks are correct!

Test #14:

score: 5
Accepted
time: 8ms
memory: 20492kb

input:

201 400
1 1 1 1 1 152 1 1 1 1 1 1 1 33 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 300 154 1 1 147 1 1 1 383 186 1 1 90 256 1 1 1 1 1 1 1 63 1 1 1 1 208 1 1 1 1 31 1 1 1 1 1 1 1 127 1 1 29 216 397 393 1 1 1 1 1 1 279 1 1 1 1 55 1 1 215 249 1 1 1 1 1 1 172 1 1 1 1 1 1 1 1 1 1 1 1 349 1 331 1 1 1 1 1 1 1 34...

output:

63990
404 80401
2 404
802 2
3 802
1602 3
1203 1602
4 1203
2002 4
1204 2002
803 1204
405 803
1603 405
804 1603
1604 804
5 1604
2402 5
1205 2402
406 1205
2003 406
1197 2003
2004 1197
7 2004
3202 7
1206 3202
2005 1206
1605 2005
408 1605
2404 408
8 2404
809 8
2405 809
2006 2405
409 2006
810 409
2802 810...

result:

ok both subtasks are correct!

Test #15:

score: 5
Accepted
time: 3ms
memory: 19228kb

input:

400 400
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 ...

output:

217
118784 160001
784 118784
160001 784
11722 160001
2922 11722
147572 2922
133420 147572
87336 133420
132109 87336
90155 132109
48288 90155
62732 48288
19769 62732
71849 19769
132724 71849
49916 132724
119484 49916
6527 119484
129727 6527
84927 129727
119327 84927
49884 119327
45116 49884
36387 451...

result:

ok both subtasks are correct!

Test #16:

score: 5
Accepted
time: 6ms
memory: 18472kb

input:

301 200
50 129 146 60 183 51 47 77 26 73 1 45 1 44 149 1 81 196 17 16 163 35 159 71 1 94 161 138 138 27 76 1 102 42 5 186 176 1 111 198 37 63 81 155 95 164 132 135 155 194 126 98 31 34 121 19 175 148 33 105 25 122 91 165 1 69 1 197 12 98 1 155 5 53 42 1 60 98 78 61 155 13 1 171 102 152 95 61 87 200 ...

output:

23506
2980 60201
220 2980
621 220
232 621
1234 232
563 1234
1253 563
419 1253
1659 419
1934 1659
1307 1934
2334 1307
3392 2334
13 3392
1827 13
633 1827
4989 633
2164 4989
5357 2164
16 5357
2600 16
458 2600
6254 458
2798 6254
467 2798
3027 467
507 3027
2914 507
637 2914
3041 637
6412 3041
246 6412
69...

result:

ok both subtasks are correct!

Test #17:

score: 5
Accepted
time: 4ms
memory: 19796kb

input:

201 400
1 1 1 1 1 1 1 1 1 1 1 1 1 263 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 246 1 1 1 1 1 1 1 1 1 1 1 1 1 1 107 1 1 1 1 1 1 1 1 57 1 1 1 1 1 1 1 224 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 90 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

77869
802 80401
2 802
1202 2
803 1202
402 803
1203 402
3 1203
1604 3
804 1604
404 804
1605 404
4 1605
2002 4
915 2002
1606 915
1204 1606
405 1204
2003 405
5 2003
2402 5
6 2402
2802 6
7 2802
3202 7
8 3202
3602 8
9 3602
4002 9
10 4002
4402 10
11 4402
4802 11
12 4802
5202 12
13 5202
806 13
2004 806
120...

result:

ok both subtasks are correct!

Test #18:

score: 5
Accepted
time: 10ms
memory: 20972kb

input:

400 300
75 26 289 176 131 196 124 8 230 157 247 265 13 2 210 141 17 200 187 83 21 22 118 144 232 26 284 75 48 30 132 32 65 34 72 36 73 286 164 40 41 261 65 270 221 12 139 48 49 143 91 39 17 258 275 56 151 194 282 55 228 266 296 64 22 232 67 142 69 152 10 102 109 45 75 49 283 112 78 283 81 236 169 22...

output:

43105
392 120001
1692 392
427 1692
690 427
421 690
3291 421
1496 3291
124 1496
775 124
4136 775
745 4136
4124 745
1404 4124
600 1404
4679 600
806 4679
1656 806
1216 1656
253 1216
2642 253
1436 2642
372 1436
2215 372
5160 2215
3713 5160
5553 3713
4322 5553
5682 4322
4607 5682
243 4607
2370 243
8290 2...

result:

ok both subtasks are correct!

Test #19:

score: 5
Accepted
time: 19ms
memory: 23156kb

input:

333 399
1 1 1 1 1 1 1 28 1 1 1 1 1 1 161 1 17 1 1 1 1 262 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 43 1 1 1 1 1 70 1 1 1 142 1 1 1 1 1 1 1 1 1 1 1 1 70 1 1 1 1 1 1 278 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 245 1 1 1 1 1 1 33 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 106 1 1 1 1 268 1 1 1 172 1 1 1 1 1 312 1 286 1 1 1 1 ...

output:

114795
1199 132868
800 1199
401 800
1200 401
404 1200
2 404
1598 2
405 1598
1599 405
801 1599
1600 801
1201 1600
3 1201
1997 3
406 1997
1998 406
802 1998
2386 802
1601 2386
4 1601
2000 4
5 2000
2397 5
407 2397
2001 407
1202 2001
3046 1202
408 3046
2398 408
803 2398
2399 803
1203 2399
2400 1203
6 240...

result:

ok both subtasks are correct!

Test #20:

score: 5
Accepted
time: 16ms
memory: 20480kb

input:

400 400
100 35 353 385 317 228 7 148 113 165 11 306 209 89 21 166 17 2 19 249 27 305 377 22 3 353 38 28 29 96 191 32 33 309 35 308 100 176 152 40 176 42 43 86 45 46 96 48 396 381 218 246 53 54 334 159 243 360 294 60 33 62 185 64 65 66 191 121 351 107 10 343 367 74 75 201 77 247 79 134 304 92 42 126 ...

output:

55816
2388 160001
523 2388
1228 523
569 1228
147 569
546 147
1984 546
4380 1984
1074 4380
1540 1074
1901 1540
2332 1901
5423 2332
2632 5423
11280 2632
2953 11280
1162 2953
690 1162
920 690
11751 920
3820 11751
13326 3820
4724 13326
391 4724
2968 391
2064 2968
1914 2064
14091 1914
2676 14091
15984 26...

result:

ok both subtasks are correct!