QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#211231#3840. Pass the Ball!linninsWA 5ms5852kbC++204.8kb2023-10-12 13:04:002023-10-12 13:04:00

Judging History

This is the latest submission verdict.

  • [2023-10-12 13:04:00]
  • Judged
  • Verdict: WA
  • Time: 5ms
  • Memory: 5852kb
  • [2023-10-12 13:04:00]
  • Submitted

answer

#pragma GCC optimize(3)
#include<cstdio>
#include<vector>
#include<iostream>
#include<map>

#define int __int128

using namespace std;

const int modp = 4179340454199820289;
const int g = 3;
const int gny = 1393113484733273430;

int pos[1000500 + 1000];

int Map[1000500];
int n,m;
int flag;
int check[1000500];
int maxlen, maxbit, inv;

vector< vector<int> > Vec;
vector< vector<int> > Vec2; 
vector< vector<int> > Answer; 
vector< int > Size;

int read_int();

void print_int(int x);

void Swap(int &a,int &b);

void Make_Ring(int t);

int Cal(int p);

void NTT(vector<int> &A,vector<int> &B);

int Cal_Ring(int t,int p);

int pow_k(int a, int b);

void ntt(vector<int> &A, bool flag);

void checkAnswer();

signed main()
{
	n = read_int();
	m = read_int();
	for(int i=1;i<=n;i++){
		int next;
		next = read_int();
		Map[i] = next;
	}
	for(int i=1;i<=n;i++){
		if(check[i] == 0){
			
			Make_Ring(i);//通过flag来表示前方有几个环 
		}
	}
	flag = Vec.size();
	for(int i=0;i<flag;i++){
		vector<int> Mid;
		int p = Vec[i].size();
		for(int j=0;j<=p;j++)
			Mid.push_back(0);
		for(int j=p+1;j<=p*2;j++)
			Mid.push_back(Vec[i][p*2-j]);
		Vec2.push_back(Mid); 
	}
	
	for(int i=0;i<flag;i++)
		NTT(Vec2[i],Vec[i]);
	
	checkAnswer();
	
	for(int i=1;i<=m;i++){
		int p;
		p = read_int();
		print_int(Cal(p));
		putchar('\n'); 
	}
	return 0;
}

void checkAnswer(){
	map<int,int> Check;
	int Point = 1;
	
//	for(int k=0;k<Size.size();k++){
//		print_int(Size[k]);
//		putchar(' ');
//	}
//	printf("\n^^^^^\n");
//	
//	for(int j=0;j<Vec2.size();j++){
//		for(int k=0;k<Vec2[j].size();k++){
//			print_int(Vec2[j][k]);
//			putchar(' ');
//		}
//		printf("\n$$$$$\n");
//	}
	
	for(int i=0;i<Vec2.size();i++){
		if(Check[Size[i]]==0){
//			printf("1*\n");
//			print_int(Size[i]);
//			printf("\n");
//			print_int(i);
//			printf("\n");
//			printf("%%%%%%%%%%%%%%%\n");
			Check[Size[i]] = Point++;
			Answer.push_back(Vec2[i]); 
		}
		else{
//			printf("2*\n");
//			print_int(Size[i]);
//			printf("\n");
//			print_int(i);
//			printf("\n");
//			printf("%%%%%%%%%%%%%%%\n");
			for(int j=0;j<Answer[Check[Size[i]] - 1].size();j++){
				Answer[Check[Size[i]] - 1][j] += Vec2[i][j];
			}
		}
		
//		for(int j=0;j<Answer.size();j++){
//			for(int k=0;k<Answer[j].size();k++){
//				print_int(Answer[j][k]);
//				putchar(' ');
//			}
//			printf("\n*****\n");
//		}
	}
}

void Make_Ring(int t){
	vector <int> Mid;
	while(check[t] == 0){
		Mid.push_back(t); 
		check[t] = 1;
		t = Map[t];
	}
	Vec.push_back(Mid);
	Size.push_back(Mid.size());
	return; 
}

void NTT(vector<int> &A,vector<int> &B){
	
	int n = A.size() - 1;
	int m = B.size() - 1;
	
	maxlen = 1;
	maxbit = 0;
	while(maxlen <= n + m)
	{
		maxlen <<= 1;
		++maxbit;
	}
	
	for(int i = A.size();i<=maxlen;i++){
		A.push_back(0);
	}
	for(int i = B.size();i<=maxlen;i++){
		B.push_back(0);
	}
	
	inv = pow_k(maxlen, modp - 2);
	for(int i = 1; i < maxlen; ++i)
	{
		pos[i] = ((pos[(i >> 1)] >> 1) | ((i & 1) << (maxbit - 1)));
	}

	
	ntt(A, 1);
	ntt(B, 1);
	for(int i = 0; i < maxlen; ++i)
	{
		A[i] = A[i] * B[i] % modp;
	}
	ntt(A, 0);
	
	
	for(int i = 0; i < n + m + 1; ++i)
	{
		A[i] = A[i] * inv % modp;
	}
}

int Cal(int p){
	int Ans = 0;
	for(int i=0;i<Answer.size();i++){
		Ans += Cal_Ring(i,p);
	}
	return Ans;
}

int Cal_Ring(int t,int p){
	int n = Size[t];
	p = p%n;
	return Answer[t][p+2*n] + Answer[t][p+n];
}

int pow_k(int a, int b)
{
	int ans = 1;
	while(b > 0)
	{
		if(b & 1)
			ans = ans * a % modp;
		a = a * a % modp;
		b = (b >> 1);
	}
	return ans;
}

void ntt(vector<int> &A, bool flag)
{
	for(int i = 1; i < maxlen; ++i)
	{
		if(i < pos[i])	Swap(A[pos[i]], A[i]);
	}
	for(int len = 1; len < maxlen; len <<= 1)
	{
		int wn = pow_k(flag ? g : gny, (modp - 1) / (len << 1));
		for(int i = 0; i < maxlen; i += (len << 1))
		{
			int w = 1;
			for(int j = 0; j < len; ++j)
			{
				int temp1 = A[i + j], temp2 = w * A[i + j + len] % modp;
				A[i + j] = (temp1 + temp2) % modp;
				A[i + j + len] = ((temp1 - temp2) % modp + modp) % modp;
				w = w * wn % modp;
			}
		}
	}
	return ;
}

void Swap(int &a,int &b){
	int c;
	c = a;
	a = b;
	b = c;
}

void print_int(int x){
	if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9)
        print_int(x / 10);
    putchar(x % 10 + '0');
}

int read_int(){
	bool flag = 0;
	char c;
	int x = 0;
	while(((c = getchar()) < '0' || c > '9') && c != '-');
	if(c == '-')
		flag = 1;
	else
		x = c - '0';
	while((c = getchar()) >= '0' && c <= '9')
		x = x * 10 + c - '0';
	if(flag)
		return -x;
	else
		return x;
}

/*
4 4
2 4 1 3
1
2
3
4

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


*/




详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5536kb

input:

4 4
2 4 1 3
1
2
3
4

output:

25
20
25
30

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 5532kb

input:

3 6
2 3 1
1
2
3
999999998
999999999
1000000000

output:

11
11
14
11
14
11

result:

ok 6 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 5556kb

input:

3 6
3 1 2
1
2
3
999999998
999999999
1000000000

output:

11
11
14
11
14
11

result:

ok 6 lines

Test #4:

score: 0
Accepted
time: 4ms
memory: 5820kb

input:

1000 10000
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:

333334000
332835500
332338000
331841500
331346000
330851500
330358000
329865500
329374000
328883500
328394000
327905500
327418000
326931500
326446000
325961500
325478000
324995500
324514000
324033500
323554000
323075500
322598000
322121500
321646000
321171500
320698000
320225500
319754000
319283500
...

result:

ok 10000 lines

Test #5:

score: 0
Accepted
time: 4ms
memory: 5772kb

input:

1000 10000
187 493 316 665 124 40 448 649 657 65 438 730 816 107 789 286 309 469 169 216 488 52 212 111 541 83 990 48 282 867 36 220 676 241 959 372 322 244 481 708 595 957 215 223 120 658 291 176 229 158 431 492 221 986 889 861 606 518 106 349 410 765 745 812 563 998 150 392 358 328 747 793 587 507...

output:

250347169
248662078
245260552
253150328
247096579
249698948
249942589
251180693
248589849
253775352
248472247
248369272
249001282
249611561
251718722
248202949
252492155
251442262
255269934
247070745
248898892
250071493
249262069
247714054
248954719
251676093
251650611
249152315
248608212
249678723
...

result:

ok 10000 lines

Test #6:

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

input:

1000 10000
301 793 604 129 545 524 625 540 271 616 710 629 682 190 152 287 40 5 921 699 730 427 833 680 514 782 641 754 15 218 725 862 238 468 917 368 850 35 243 339 688 460 597 979 398 748 552 25 189 68 115 76 888 844 890 102 835 581 266 342 571 749 574 156 717 538 440 764 601 831 130 39 136 842 78...

output:

250889096
249771741
249454079
255348475
249559048
257117687
248782164
250050189
254433188
254774148
251601723
250597849
251570596
249861162
256368189
253409601
254026050
249568513
248807065
253946543
254531155
252620668
255570774
257943265
252823304
255566622
252678751
254417015
252540528
256854774
...

result:

ok 10000 lines

Test #7:

score: 0
Accepted
time: 4ms
memory: 5852kb

input:

1000 10000
163 149 53 93 995 744 452 73 683 474 213 597 903 190 842 487 894 256 339 588 902 930 870 632 992 561 455 248 961 788 188 436 421 887 847 361 311 192 706 636 352 233 987 376 323 32 120 565 546 730 191 587 404 22 817 906 585 420 621 167 132 816 431 200 952 180 402 838 794 284 349 94 427 482...

output:

250865862
251948566
251553098
253385993
250129972
254017302
254493541
251442447
247937015
250833800
249523549
249193386
252059812
249370594
254109165
249397918
247138568
248934855
249807415
251196617
250357758
249395769
248148350
247009170
252163335
249850249
250639740
251827755
247795165
253868762
...

result:

ok 10000 lines

Test #8:

score: 0
Accepted
time: 4ms
memory: 5656kb

input:

1000 10000
384 881 331 471 166 902 45 90 353 415 895 74 693 69 887 878 807 436 538 328 892 750 294 891 238 527 406 464 23 259 200 731 336 62 30 157 457 243 956 485 670 37 405 16 14 162 484 875 64 38 305 177 391 882 440 649 296 702 990 953 468 109 915 316 622 81 863 286 412 584 659 271 282 984 490 97...

output:

251829371
249579880
249800743
251369774
250567175
248916841
252235114
251856809
249117279
247474406
250612288
247256785
251394491
250004784
252462213
249291071
251938054
251833233
254868301
251496842
242701038
251189092
250051802
248910394
249438626
245833186
250955694
251605815
251188358
253696421
...

result:

ok 10000 lines

Test #9:

score: -100
Wrong Answer
time: 4ms
memory: 5728kb

input:

1000 10000
791 811 116 51 267 840 647 573 765 859 154 984 879 576 938 308 808 301 287 158 43 453 284 243 945 585 156 648 447 331 930 296 657 820 991 457 969 237 644 784 514 685 487 506 353 854 336 7 111 324 593 4 234 548 680 923 501 186 201 918 821 968 549 236 171 173 553 476 882 569 74 841 910 689 ...

output:

247599884
238746470
242796419
244318925
252878426
245507806
241783105
252663768
233045804
242417652
241117315
246056103
248659990
247183709
253824471
250773749
238719015
246324171
246349662
248746365
238023109
244743133
252078945
236292727
245786511
257892006
245842562
249994979
245358527
244752741
...

result:

wrong answer 1st lines differ - expected: '260869097', found: '247599884'