QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#295536 | #2678. 多边形 | yhk1001 | 100 ✓ | 163ms | 46756kb | C++14 | 3.5kb | 2023-12-31 10:57:03 | 2023-12-31 10:57:03 |
Judging History
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();
int cur = 0;
for (auto it = e[n].begin(), nxt = next(it); nxt != e[n].end(); it++, nxt++)
{
int l = *it, r = *nxt;
build(l, r, 0);
int p = id[make_pair(l, r)];
cur += sz[p];
ans = ans * C(cur, sz[p]) % P;//different tree
}
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)
{
nw_cnt++;
int szl = sz[ ch[p][0] ];
int szr = sz[ ch[p][1] ];
nw_ans = nw_ans * invC(sz[p] - 1, szl) % P;
nw_ans = nw_ans * invC(cur, sz[p]) % P;//only edges that not connect to n, which is cur, not n - 3 !
nw_ans = nw_ans * C(cur - sz[p] + szl, szl) % P;
nw_ans = nw_ans * C(cur - 1, szr) % P;//cur - sz[p] + szl + szr = cur - 1
}
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: 5
Accepted
time: 3ms
memory: 8340kb
input:
1 9 1 6 1 4 2 4 4 6 6 9 6 8 0
output:
5 15
result:
ok 2 number(s): "5 15"
Test #2:
score: 5
Accepted
time: 2ms
memory: 8360kb
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: 5
Accepted
time: 0ms
memory: 8352kb
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 15 4 12 4 3 4 3 4 3 4 3 4 3 4 3 5 10 4 12 4 3 5 10
result:
ok 24 numbers
Test #4:
score: 5
Accepted
time: 2ms
memory: 8344kb
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: 8344kb
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: 5
Accepted
time: 0ms
memory: 8568kb
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 1890 10 1890 10 2835 10 1890 10 1890 10 1890 10 7560 9 1512 10 1890 10 1890 10 945 10 7560 9 1512 10 1890 10 1890
result:
ok 30 numbers
Test #7:
score: 5
Accepted
time: 0ms
memory: 8436kb
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: 0ms
memory: 8368kb
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: 5
Accepted
time: 3ms
memory: 8360kb
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 199865574
result:
ok 2 number(s): "96 199865574"
Test #10:
score: 5
Accepted
time: 3ms
memory: 8372kb
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: 34ms
memory: 12164kb
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: 39ms
memory: 12096kb
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: 5
Accepted
time: 7ms
memory: 12236kb
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 618240676 9996 78827115 9995 802652618 9996 104350952 9996 927361014 9996 321033864 9996 618240676 9996 868527075 9996 618240676 9996 618240676 9996 618240676 9996 618240676 9996 618240676 9996 233865248 9996 618240676 9996 618240676 9996 539413561 9996 753351125 9996 539413561 9996 309120338 9...
result:
ok 2002 numbers
Test #14:
score: 5
Accepted
time: 7ms
memory: 12112kb
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: 87ms
memory: 46520kb
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: 156ms
memory: 46544kb
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: 161ms
memory: 46644kb
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: 5
Accepted
time: 143ms
memory: 46620kb
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 745361137 99994 745361137 99994 872680572 99994 490722267 99994 84675932 99994 745361137 99994 470979652 99994 181701416 99994 745361137 99994 745361137 99994 745361137 99994 706671509 99994 577810856 99993 881270336 99994 511935884 99994 745361137 99994 797334315 99994 36626873 99994 58178704...
result:
ok 200002 numbers
Test #19:
score: 5
Accepted
time: 163ms
memory: 46552kb
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 694231833 99995 694231833 99995 694231833 99995 694231833 99995 82695485 99995 595296260 99995 949454647 99995 107072901 99995 787752201 99995 987245967 99995 694231833 99995 388463659 99995 694231833 99995 694231833 99995 616226252 99995 694231833 99995 76899199 99995 288287780 99995 69423183...
result:
ok 200002 numbers
Test #20:
score: 5
Accepted
time: 148ms
memory: 46756kb
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 582102809 99996 582102809 99996 862280762 99996 370338886 99996 582102809 99996 186577105 99996 582102809 99996 186577105 99996 211645172 99996 582102809 99996 582102809 99996 390548303 99996 582102809 99996 845338016 99996 582102809 99996 896624983 99996 164205611 99996 514211117 99996 420622...
result:
ok 200002 numbers