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
| #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5,P = 1e9 + 7; typedef pair<int,int> pii; #define FI first #define SE second #define mkp make_pair int SubId; int n,m; int U[N],V[N]; int fac[N],inv[N]; inline void init(int n) { fac[0] = 1; for(int i = 1;i <= n;i++) fac[i] = 1ll * fac[i - 1] * i % P; inv[1] = 1; for(int i = 2;i <= n;i++) inv[i] = 1ll * inv[P % i] * (P - P / i) % P; } struct Polygon { map<pair<int,int>,int> ida,idb,idd; set<int> E[N],Te[N]; inline void adde(int x,int y) { E[x].insert(y);E[y].insert(x);} inline void UpdTriangle(int a,int b,int c) { int t[3] = {a,b,c}; sort(t,t + 3); assert(t[0] != t[1] && t[1] != t[2] && t[0] != t[2]); idd[mkp(t[0],t[1])] = idd[mkp(t[1],t[0])] = t[2]; idb[mkp(t[0],t[2])] = idb[mkp(t[2],t[0])] = t[1]; ida[mkp(t[1],t[2])] = ida[mkp(t[2],t[1])] = t[0]; } inline void Topo() { for(int i = 1;i <= n;i++) Te[i] = E[i]; queue<int> Q; for(int i = 1;i <= n;i++) if(Te[i].size() == 2) Q.push(i); for(int _ = 1;_ <= n - 2;_++) { int x = Q.front();Q.pop(); int t1 = *Te[x].begin(),t2 = *prev(Te[x].end()); UpdTriangle(t1,t2,x); Te[t1].erase(x);Te[t2].erase(x); if(Te[t1].size() == 2) Q.push(t1); if(Te[t2].size() == 2) Q.push(t2); } } inline pii Rotate1(int a,int c) { if(a > c) swap(a,c); int b = idb[mkp(a,c)],d = idd[mkp(a,c)]; E[a].erase(c);E[c].erase(a); E[b].insert(d);E[d].insert(b); UpdTriangle(a,b,d);UpdTriangle(b,c,d); return mkp(b,d); } inline pii Rotate2(int b,int d) { if(b > d) swap(b,d); int a = ida[mkp(b,d)],c = idb[mkp(b,d)]; E[b].erase(d);E[d].erase(b); E[a].insert(c);E[c].insert(a); UpdTriangle(a,b,c);UpdTriangle(a,c,d); return mkp(a,c); } } T; inline int Solve(int l,int r) { if(r - l <= 1) return 1; int ps = T.idb[mkp(l,r)]; return 1ll * Solve(l,ps) * Solve(ps,r) % P * inv[r - l - 1] % P; } inline int GetAns() { vector<int> Es(T.E[n].begin(),T.E[n].end()); int mul = 1; for(int i = 0;i + 1 < Es.size();i++) mul = 1ll * mul * Solve(Es[i],Es[i + 1]) % P; return 1ll * mul * fac[n - 1 - Es.size()] % P; } int main() { cin >> SubId; cin >> n;init(n); for(int i = 1;i <= n - 3;i++) cin >> U[i] >> V[i],T.adde(U[i],V[i]); for(int i = 1;i <= n;i++) T.adde(i,i % n + 1); T.Topo(); int pans = 0,pnum = n - 1 - (int)T.E[n].size(); if(SubId == 0) printf("%d\n",pnum); else printf("%d %d\n",pnum,pans = GetAns()); cin >> m; for(int i = 1;i <= m;i++) { int a,c; cin >> a >> c; int b = T.idb[mkp(a,c)],d = T.idd[mkp(a,c)]; if(SubId == 0) printf("%d\n",pnum - (d == n)); else { int nans = pans; printf("%d ",pnum - (d == n)); if(d != n) nans = 1ll * pans * (c - a - 1) % P * inv[d - b - 1] % P; else nans = 1ll * pans * (c - a - 1) % P * inv[pnum] % P; printf("%d\n",nans); } } return 0; }
|