QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#295529#2678. 多边形yhk100160 144ms46844kbC++143.0kb2023-12-31 10:35:532023-12-31 10:35:54

Judging History

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

  • [2023-12-31 10:35:54]
  • 评测
  • 测评结果:60
  • 用时:144ms
  • 内存:46844kb
  • [2023-12-31 10:35:53]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;

// #define Debug

const int N = 1e5;
const long long P = 1e9 + 7;

int W, n, m;
int cnt = 2;//1-n, (n - 1)-n
long long ans = 1;

set<int> e[N + 5];

map<pair<int, int>, int> id;

long long fac[N + 5], inv[N + 5];
long long ksm(long long d, long long u)
{
	long long res = 1;
	while (u)
	{
		if (u & 1)
			res = res * d % P;
		u >>= 1;
		d = d * d % P;
	}
	return res;
}
void init()
{
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = fac[i - 1] * i % P;
	inv[n] = ksm(fac[n], P - 2);
	for (int i = n - 1; i >= 0; i--)
		inv[i] = inv[i + 1] * (i + 1) % P;
	return ;
}
long long C(int x, int y)//C_x^y
{
	if (x < y)
		return 0ll;
	return fac[x] * inv[y] % P * inv[x - y] % P;
}
long long invC(int x, int y)
{
	if (x < y)
		return 0ll;
	return inv[x] * fac[y] % P * fac[x - y] % P;
}

int ch[N + 5][2], fa[N + 5];
int sz[N + 5];
int build(int l, int r, int father)//l <= r
{
	if (l + 1 == r)
		return 0;

	int p = id[make_pair(l, r)];
	fa[p] = father;
	
	int split = *prev(e[l].find(r));
	ch[p][0] = build(l, split, p);
	ch[p][1] = build(split, r, p);

	sz[p] = sz[ch[p][0]] + 1 + sz[ch[p][1]];
	ans = ans * C(sz[p] - 1, sz[ch[p][0]]) % P;//C(sz(ls) + sz(rs), sz(ls))
	return p;
}
int sontype(int p)
{
	return p == ch[fa[p]][1];
}

void print(int ans1, long long ans2)
{
	if (W)
		printf("%d %lld\n", ans1, ans2);
	else
		printf("%d\n", ans1);
	return ;
}

int main()
{
	// freopen("polygon.in", "r", stdin);
	// freopen("polygon.out", "w", stdout);

	scanf("%d%d", &W, &n);
	for (int i = 1, x, y; i <= n - 3; i++)
	{
		scanf("%d%d", &x, &y);
		e[x].insert(y), e[y].insert(x);
		id[make_pair(x, y)] = id[make_pair(y, x)] = i;
		cnt += (x == n || y == n);
	}

	for (int i = 1; i < n; i++)
		e[i].insert(i + 1), e[i + 1].insert(i);
	e[n].insert(1), e[1].insert(n);

	init();
	for (auto it = e[n].begin(), nxt = next(it); nxt != e[n].end(); it++, nxt++)
	{
		int l = *it, r = *nxt;
		build(l, r, 0);
	}

	print(n - 1 - cnt, ans);
	scanf("%d", &m);
	for (int i = 1, x, y; i <= m; i++)
	{
		scanf("%d%d", &x, &y);
		if (x > y)
			swap(x, y);

		int nw_cnt = cnt;
		long long nw_ans = ans;
		int p = id[make_pair(x, y)];

		if (fa[p])
		{
			int type = sontype(p), bro = ch[ fa[p] ][type ^ 1];
			int szA = sz[ ch[p][type] ];
			int szB = sz[ ch[p][type ^ 1] ];
			int szC = sz[ bro ];

			nw_ans = nw_ans * invC(sz[p] - 1, szA) % P;
			nw_ans = nw_ans * invC(sz[fa[p]] - 1, szC) % P;

			nw_ans = nw_ans * C(szB + szC, szC) % P;
			nw_ans = nw_ans * C(sz[fa[p]] - 1, szA) % P;
		}
		else if (y < n)//(x, y) -> (?, n), ans won't change !
			nw_cnt++;
		else//(x, n) -> (?, ?)
		{
			nw_cnt--;
			
			auto it = e[n].find(x);
			int l = *prev(it), r = *next(it);
			int ls = id[make_pair(l, x)];
			int rs = id[make_pair(x, r)];
			nw_ans = nw_ans * C(sz[ls] + sz[rs], sz[ls]) % P;
		}

		print(n - 1 - nw_cnt, nw_ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 9832kb

input:

1
9
1 6
1 4
2 4
4 6
6 9
6 8
0

output:

5 3

result:

wrong answer 2nd numbers differ - expected: '15', found: '3'

Test #2:

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

input:

1
10
1 8
1 7
1 4
2 4
4 7
5 7
8 10
0

output:

6 6

result:

ok 2 number(s): "6 6"

Test #3:

score: 0
Wrong Answer
time: 2ms
memory: 10496kb

input:

1
11
1 6
1 4
2 4
4 6
6 11
7 11
7 9
9 11
11
1 6
7 9
7 9
7 9
7 9
7 9
7 9
1 4
1 6
7 9
1 4

output:

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

result:

wrong answer 2nd numbers differ - expected: '15', found: '3'

Test #4:

score: 5
Accepted
time: 1ms
memory: 10544kb

input:

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

output:

7 1
7 6
6 1
6 1
7 6
7 6
7 1
7 5
7 5
7 4
7 1
7 1
7 5

result:

ok 26 numbers

Test #5:

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

input:

1
13
1 10
1 9
1 5
1 3
3 5
5 9
6 9
6 8
10 13
11 13
13
6 8
1 3
1 9
6 8
6 8
6 8
1 9
6 8
6 8
6 8
6 8
6 8
6 8

output:

8 40
8 40
8 20
8 70
8 40
8 40
8 40
8 70
8 40
8 40
8 40
8 40
8 40
8 40

result:

ok 28 numbers

Test #6:

score: 0
Wrong Answer
time: 1ms
memory: 9944kb

input:

1
14
1 10
1 7
1 6
1 5
1 3
3 5
7 10
8 10
10 14
10 13
10 12
14
10 12
1 5
10 12
10 12
10 12
1 6
1 10
10 12
10 12
1 3
1 6
1 10
10 12
10 12

output:

10 42
10 42
10 63
10 42
10 42
10 42
10 168
9 42
10 42
10 42
10 21
10 168
9 42
10 42
10 42

result:

wrong answer 2nd numbers differ - expected: '1890', found: '42'

Test #7:

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

input:

1
100
1 98
1 96
1 95
1 94
1 91
1 89
1 79
1 78
1 77
1 76
1 75
1 74
1 73
1 70
1 68
1 67
1 64
1 60
1 58
1 57
1 56
1 55
1 54
1 53
1 51
1 49
1 45
1 44
1 42
1 38
1 32
1 30
1 29
1 28
1 27
1 26
1 22
1 21
1 19
1 18
1 16
1 15
1 13
1 11
1 10
1 6
1 5
1 4
1 3
6 10
6 9
7 9
11 13
13 15
16 18
19 21
22 26
23 26
24 2...

output:

96 671291031
96 332953896
96 342582055
96 249715422
96 346826975
96 895054708
96 671291031
96 936673945
96 671291031
96 671291031
96 544638907
96 671291031
96 398074371
96 887220962
96 205973725
96 895054708
96 686225340
96 835645519
96 586258144
96 223763677
96 223763677
96 917822763
96 466378536
9...

result:

ok 202 numbers

Test #8:

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

input:

1
100
1 98
1 97
1 96
1 94
1 93
1 91
1 89
1 88
1 87
1 86
1 84
1 82
1 80
1 72
1 71
1 70
1 68
1 64
1 62
1 61
1 58
1 56
1 53
1 49
1 46
1 45
1 44
1 43
1 42
1 41
1 40
1 34
1 28
1 27
1 26
1 25
1 24
1 23
1 21
1 19
1 17
1 16
1 13
1 11
1 10
1 8
1 7
1 5
1 4
1 3
5 7
8 10
11 13
13 16
14 16
17 19
19 21
21 23
28 3...

output:

96 906354055
96 73575642
96 906354055
96 906354055
96 906354055
96 953177031
96 538429361
96 107010664
96 906354055
96 906354055
96 859531079
96 953177031
96 953177031
96 658851221
96 906354055
96 555170114
96 953177031
96 625416199
96 204009415
96 953177031
96 906354055
96 906354055
96 302687
96 83...

result:

ok 202 numbers

Test #9:

score: 0
Wrong Answer
time: 2ms
memory: 10908kb

input:

1
100
1 96
1 95
1 92
1 90
1 89
1 88
1 87
1 85
1 84
1 82
1 81
1 78
1 77
1 73
1 68
1 66
1 64
1 63
1 60
1 59
1 58
1 57
1 54
1 51
1 46
1 45
1 39
1 36
1 35
1 34
1 33
1 26
1 25
1 23
1 19
1 18
1 17
1 8
1 5
1 3
3 5
5 8
5 7
8 17
8 13
8 11
9 11
11 13
13 17
13 15
15 17
19 23
19 22
19 21
23 25
26 33
26 30
26 28...

output:

96 359254359

result:

wrong answer 2nd numbers differ - expected: '199865574', found: '359254359'

Test #10:

score: 5
Accepted
time: 1ms
memory: 10548kb

input:

1
100
1 98
1 96
1 95
1 90
1 88
1 87
1 86
1 85
1 84
1 81
1 76
1 74
1 73
1 72
1 71
1 69
1 68
1 63
1 61
1 60
1 58
1 57
1 56
1 55
1 52
1 50
1 49
1 44
1 43
1 39
1 38
1 36
1 34
1 32
1 30
1 28
1 25
1 24
1 23
1 19
1 17
1 16
1 15
1 14
1 13
1 11
1 10
1 4
1 3
4 10
4 6
6 10
7 10
8 10
11 13
17 19
19 23
20 23
21 ...

output:

96 982685481

result:

ok 2 number(s): "96 982685481"

Test #11:

score: 5
Accepted
time: 39ms
memory: 15044kb

input:

0
10000
1 9997
1 9996
1 9992
1 9991
1 9990
1 9989
1 9975
1 9974
1 9973
1 9968
1 9966
1 9964
1 9963
1 9957
1 9955
1 9954
1 9950
1 9947
1 9945
1 9944
1 9942
1 9939
1 9936
1 9932
1 9925
1 9923
1 9921
1 9919
1 9916
1 9915
1 9910
1 9908
1 9907
1 9906
1 9903
1 9902
1 9900
1 9896
1 9893
1 9892
1 9891
1 989...

output:

9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9994
9995
9995
9995
9995
9995
9995
9994
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
...

result:

ok 100001 numbers

Test #12:

score: 5
Accepted
time: 35ms
memory: 13952kb

input:

0
10000
1 9995
1 9994
1 9993
1 9991
1 9990
1 9987
1 9986
1 9985
1 9983
1 9975
1 9974
1 9970
1 9969
1 9966
1 9965
1 9964
1 9963
1 9961
1 9957
1 9955
1 9954
1 9953
1 9952
1 9947
1 9946
1 9944
1 9941
1 9940
1 9939
1 9936
1 9934
1 9933
1 9932
1 9926
1 9925
1 9924
1 9923
1 9922
1 9917
1 9915
1 9914
1 991...

output:

9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9994
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
9995
...

result:

ok 100001 numbers

Test #13:

score: 0
Wrong Answer
time: 6ms
memory: 13396kb

input:

1
10000
1 9997
1 9996
1 9995
1 9992
1 9991
1 9990
1 9989
1 9987
1 9985
1 9984
1 9982
1 9981
1 9979
1 9978
1 9977
1 9975
1 9973
1 9972
1 9969
1 9966
1 9964
1 9963
1 9961
1 9958
1 9956
1 9955
1 9954
1 9953
1 9952
1 9947
1 9945
1 9943
1 9941
1 9940
1 9939
1 9936
1 9935
1 9931
1 9928
1 9925
1 9920
1 991...

output:

9996 815588065
9996 210392041
9995 815588065
9996 489406201
9996 723382094
9996 417799226
9996 815588065
9996 218874404
9996 815588065
9996 815588065
9996 815588065
9996 815588065
9996 815588065
9996 958806916
9996 815588065
9996 815588065
9996 605196024
9996 645233433
9996 605196024
9996 907794036
...

result:

wrong answer 2nd numbers differ - expected: '618240676', found: '815588065'

Test #14:

score: 5
Accepted
time: 9ms
memory: 14332kb

input:

1
10000
1 9998
1 9995
1 9993
1 9991
1 9989
1 9987
1 9985
1 9975
1 9973
1 9972
1 9971
1 9969
1 9967
1 9964
1 9962
1 9960
1 9958
1 9955
1 9954
1 9949
1 9948
1 9945
1 9943
1 9940
1 9939
1 9938
1 9935
1 9934
1 9932
1 9931
1 9930
1 9928
1 9927
1 9921
1 9920
1 9919
1 9918
1 9917
1 9916
1 9915
1 9914
1 991...

output:

9996 776488706
9996 838290966
9996 776488706
9995 776488706
9996 752405372
9996 776488706
9995 776488706
9996 776488706
9996 48661017
9996 776488706
9996 776488706
9996 388244353
9996 776488706
9996 651178911
9996 776488706
9996 125254238
9996 776488706
9996 211799436
9996 665893225
9996 962610175
9...

result:

ok 2002 numbers

Test #15:

score: 5
Accepted
time: 82ms
memory: 46656kb

input:

1
100000
1 99994
1 99992
1 99991
1 99990
1 99987
1 99986
1 99985
1 99982
1 99979
1 99978
1 99973
1 99971
1 99970
1 99969
1 99968
1 99966
1 99964
1 99959
1 99954
1 99953
1 99952
1 99950
1 99947
1 99946
1 99945
1 99944
1 99943
1 99942
1 99939
1 99938
1 99934
1 99933
1 99932
1 99931
1 99930
1 99927
1 9...

output:

99992 989014950

result:

ok 2 number(s): "99992 989014950"

Test #16:

score: 5
Accepted
time: 133ms
memory: 46676kb

input:

1
100000
1 99998
1 99996
1 99994
1 99993
1 99989
1 99988
1 99987
1 99985
1 99980
1 99978
1 99977
1 99970
1 99969
1 99966
1 99965
1 99963
1 99962
1 99961
1 99960
1 99959
1 99954
1 99953
1 99952
1 99950
1 99945
1 99944
1 99942
1 99940
1 99939
1 99937
1 99934
1 99933
1 99932
1 99931
1 99930
1 99929
1 9...

output:

99996 546482694
99996 546482694
99996 546482694
99996 546482694
99996 273241347
99996 546482694
99996 92965381
99996 546482694
99996 83877054
99996 273241347
99996 273241347
99996 552801627
99996 185930762
99996 546482694
99996 150734663
99996 441698118
99996 46013942
99996 546482694
99996 92965381
...

result:

ok 200002 numbers

Test #17:

score: 5
Accepted
time: 135ms
memory: 46844kb

input:

1
100000
1 99998
1 99994
1 99992
1 99991
1 99988
1 99987
1 99985
1 99984
1 99982
1 99981
1 99979
1 99978
1 99976
1 99972
1 99970
1 99969
1 99963
1 99961
1 99959
1 99958
1 99957
1 99956
1 99955
1 99951
1 99950
1 99949
1 99947
1 99945
1 99944
1 99943
1 99941
1 99940
1 99938
1 99936
1 99932
1 99931
1 9...

output:

99996 592833083
99996 592833083
99996 513261872
99996 898208276
99996 412564599
99996 592833083
99996 592833083
99996 469937914
99996 78209004
99995 592833083
99996 592833083
99996 796416545
99996 718584671
99996 796416545
99996 318566618
99996 88596616
99996 659559772
99996 318566618
99996 59283308...

result:

ok 200002 numbers

Test #18:

score: 0
Wrong Answer
time: 144ms
memory: 46792kb

input:

1
100000
1 99995
1 99993
1 99991
1 99990
1 99989
1 99987
1 99985
1 99984
1 99983
1 99982
1 99981
1 99980
1 99976
1 99975
1 99974
1 99970
1 99969
1 99967
1 99965
1 99964
1 99962
1 99960
1 99958
1 99957
1 99956
1 99955
1 99953
1 99949
1 99947
1 99946
1 99945
1 99943
1 99939
1 99938
1 99937
1 99933
1 9...

output:

99994 881270336
99994 881270336
99994 440635168
99994 762540665
99994 319800037
99994 881270336
99994 615181625
99994 101587913
99994 881270336
99994 881270336
99994 881270336
99994 24618544
99994 32527730
99993 881270336
99994 182456068
99994 881270336
99994 847028801
99994 10310985
99994 960423450...

result:

wrong answer 2nd numbers differ - expected: '745361137', found: '881270336'

Test #19:

score: 0
Wrong Answer
time: 139ms
memory: 46840kb

input:

1
100000
1 99996
1 99995
1 99994
1 99990
1 99988
1 99987
1 99984
1 99983
1 99982
1 99980
1 99979
1 99978
1 99974
1 99971
1 99970
1 99965
1 99961
1 99956
1 99955
1 99954
1 99953
1 99950
1 99948
1 99946
1 99944
1 99942
1 99941
1 99937
1 99929
1 99928
1 99924
1 99923
1 99920
1 99918
1 99916
1 99914
1 9...

output:

99995 666800287
99995 666800287
99995 666800287
99995 666800287
99995 400847
99995 235767743
99995 313565175
99995 443943271
99995 587807272
99995 689134334
99995 666800287
99995 333600567
99995 666800287
99995 666800287
99995 676780006
99995 666800287
99995 786270088
99995 471376455
99995 666800287...

result:

wrong answer 2nd numbers differ - expected: '694231833', found: '666800287'

Test #20:

score: 0
Wrong Answer
time: 140ms
memory: 46788kb

input:

1
100000
1 99997
1 99995
1 99991
1 99987
1 99985
1 99980
1 99979
1 99978
1 99975
1 99974
1 99971
1 99970
1 99969
1 99968
1 99967
1 99965
1 99963
1 99962
1 99960
1 99956
1 99953
1 99952
1 99950
1 99945
1 99944
1 99942
1 99940
1 99939
1 99937
1 99936
1 99933
1 99928
1 99923
1 99921
1 99918
1 99915
1 9...

output:

99996 830129032
99996 830129032
99996 540410243
99996 30584927
99996 830129032
99996 622596774
99996 830129032
99996 622596774
99996 449260090
99996 830129032
99996 830129032
99996 111718375
99996 830129032
99996 730437676
99996 830129032
99996 464137535
99996 660258057
99996 358509485
99996 8148268...

result:

wrong answer 2nd numbers differ - expected: '582102809', found: '830129032'