output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=605;
const int mod=1e9+7;
int n,m,mat[N],s[N],g[N],f[N][N],ans;
int main(){
scanf("%d%d",&n,&m);n<<=1;
for(int i=1;i<=n;++i)s[i]=1;
for(int i=1,x,y;i<=m;++i){
scanf("%d%d",&x,&y);
mat[x]=y;mat[y]=x;s[x]=s[y]=0;
}
for(int i=1;i<=n;++i)s[i]+=s[i-1];
g[0]=1;
for(int i=2;i<=n;i+=2)g[i]=1ll*(i-1)*g[i-2]%mod;
for(int i=n;i;--i)
for(int j=i;j<=n;++j)
if(j-i&1){
int fg=1;
for(int k=i;k<=j;++k)
if(mat[k]&&(mat[k]<i||mat[k]>j))
fg=0;
if(!fg)continue;
for(int k=i+1;k<j;++k)
if(k-i&1)
f[i][j]=(f[i][j]+1ll*f[i][k]*g[s[j]-s[k]])%mod;
f[i][j]=(g[s[j]-s[i-1]]+mod-f[i][j])%mod;
ans=(ans+1ll*f[i][j]*g[n-m-m-s[j]+s[i-1]])%mod;
}
printf("%d\n",ans);
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=605;
const int mod=1e9+7;
int n,m,mat[N],s[N],g[N],f[N][N],ans;
int main(){
scanf("%d%d",&n,&m);n<<=1;
for(int i=1;i<=n;++i)s[i]=1;
for(int i=1,x,y;i<=m;++i){
scanf("%d%d",&x,&y);
mat[x]=y;mat[y]=x;s[x]=s[y]=0;
}
for(int i=1;i<=n;++i)s[i]+=s[i-1];
g[0]=1;
for(int i=2;i<=n;i+=2)g[i]=1ll*(i-1)*g[i-2]%mod;
for(int i=n;i;--i)
for(int j=i;j<=n;++j)
if(j-i&1){
int fg=1;
for(int k=i;k<=j;++k)
if(mat[k]&&(mat[k]<i||mat[k]>j))
fg=0;
if(!fg)continue;
for(int k=i+1;k<j;++k)
if(k-i&1)
f[i][j]=(f[i][j]+1ll*f[i][k]*g[s[j]-s[k]])%mod;
f[i][j]=(g[s[j]-s[i-1]]+mod-f[i][j])%mod;
ans=(ans+1ll*f[i][j]*g[n-m-m-s[j]+s[i-1]])%mod;
}
printf("%d\n",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const int maxn = 608;
int a[maxn], b[maxn], c[maxn], n, k;
ll p[maxn], f[maxn][maxn];
bool in(int a, int l, int r) {
return l <= a && a < r;
}
bool chk(int l, int r) {
for (int i = 0; i < k; i++) {
if (in(a[i], l, r) && !in(b[i], l, r)) return true;
if (!in(a[i], l, r) && in(b[i], l, r)) return true;
}
return false;
}
int main() {
//freopen("1.txt","r",stdin) ;
scanf("%d%d",&n,&k);
p[0] = 1;
for (int i = 0; i < n-k; i++) {
p[i+1] = p[i] * (i<<1|1) %MOD;
}
n <<= 1;
for (int i = 0; i < k; i++) {
scanf("%d%d",a+i,b+i);
c[a[i]--] = c[b[i]--] = 1;
}
for (int i = 1; i <= n; i++) {
c[i] = (!c[i]) + c[i-1];
}
ll ans = 0;
for (int l = 0; l < n; l++)
for (int r = l+1; r <=n;r++) {
int x = c[r] - c[l];
if ((x%2!=0) || chk(l, r)) continue;
ll s = p[x>>1];
for (int i = l+1; i<r;i++)
s = (s - f[l][i] * p[(c[r]-c[i])>>1]) %MOD;
f[l][r] = s;
ans = (ans + s * p[((n-x-2*k)>>1)]) %MOD;
}
if (ans < 0) ans += MOD;
printf("%lld\n",ans);
return 0;
}
| ### Prompt
Your challenge is to write a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const int maxn = 608;
int a[maxn], b[maxn], c[maxn], n, k;
ll p[maxn], f[maxn][maxn];
bool in(int a, int l, int r) {
return l <= a && a < r;
}
bool chk(int l, int r) {
for (int i = 0; i < k; i++) {
if (in(a[i], l, r) && !in(b[i], l, r)) return true;
if (!in(a[i], l, r) && in(b[i], l, r)) return true;
}
return false;
}
int main() {
//freopen("1.txt","r",stdin) ;
scanf("%d%d",&n,&k);
p[0] = 1;
for (int i = 0; i < n-k; i++) {
p[i+1] = p[i] * (i<<1|1) %MOD;
}
n <<= 1;
for (int i = 0; i < k; i++) {
scanf("%d%d",a+i,b+i);
c[a[i]--] = c[b[i]--] = 1;
}
for (int i = 1; i <= n; i++) {
c[i] = (!c[i]) + c[i-1];
}
ll ans = 0;
for (int l = 0; l < n; l++)
for (int r = l+1; r <=n;r++) {
int x = c[r] - c[l];
if ((x%2!=0) || chk(l, r)) continue;
ll s = p[x>>1];
for (int i = l+1; i<r;i++)
s = (s - f[l][i] * p[(c[r]-c[i])>>1]) %MOD;
f[l][r] = s;
ans = (ans + s * p[((n-x-2*k)>>1)]) %MOD;
}
if (ans < 0) ans += MOD;
printf("%lld\n",ans);
return 0;
}
``` |
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[605][605];
int a[605],b[605];
int h[605],fc[605];
const int mod=1e9+7;
int main(){
int n,k;
scanf("%d%d",&n,&k);
n*=2;
for(int i=1;i<=k;i++){
scanf("%d%d",&a[i],&b[i]);
h[a[i]]=1;
h[b[i]]=1;
}
fc[0]=1;
for(int i=1;i<=n;i++)fc[i]=1ll*(i-1)*fc[i-2]%mod;
for(int i=1;i<=n;i++)h[i]+=h[i-1];
int ans=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
bool ok=1;
for(int p=1;p<=k;p++)if((i<=a[p]&&a[p]<=j)^(i<=b[p]&&b[p]<=j))ok=0;
if(!ok)continue;
f[i][j]=fc[j-i+1-(h[j]-h[i-1])];
for(int p=i+1;p<j;p++)f[i][j]=(f[i][j]-1ll*f[i][p]*fc[j-p-(h[j]-h[p])]%mod+mod)%mod;
ans=(ans+1ll*f[i][j]*fc[n-2*k-(j-i+1-(h[j]-h[i-1]))])%mod;
// printf("%d %d %d %d\n",i,j,f[i][j],ans);
}
}
printf("%d\n",ans);
return 0;
} | ### Prompt
Please create a solution in cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[605][605];
int a[605],b[605];
int h[605],fc[605];
const int mod=1e9+7;
int main(){
int n,k;
scanf("%d%d",&n,&k);
n*=2;
for(int i=1;i<=k;i++){
scanf("%d%d",&a[i],&b[i]);
h[a[i]]=1;
h[b[i]]=1;
}
fc[0]=1;
for(int i=1;i<=n;i++)fc[i]=1ll*(i-1)*fc[i-2]%mod;
for(int i=1;i<=n;i++)h[i]+=h[i-1];
int ans=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
bool ok=1;
for(int p=1;p<=k;p++)if((i<=a[p]&&a[p]<=j)^(i<=b[p]&&b[p]<=j))ok=0;
if(!ok)continue;
f[i][j]=fc[j-i+1-(h[j]-h[i-1])];
for(int p=i+1;p<j;p++)f[i][j]=(f[i][j]-1ll*f[i][p]*fc[j-p-(h[j]-h[p])]%mod+mod)%mod;
ans=(ans+1ll*f[i][j]*fc[n-2*k-(j-i+1-(h[j]-h[i-1]))])%mod;
// printf("%d %d %d %d\n",i,j,f[i][j],ans);
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
#define mp make_pair
#define fs first
#define sc second
#define pb push_back
#define mod 1000000007
using namespace std;
int n,k;
int match[610];
long long f[610];
long long dp[610][610];
long long dp2[610][610];
long long cnt[610][610];
long long solve(int l, int r){
if(dp[l][r] != -1) return dp[l][r];
if((r - l) % 2 == 0) return 0;
long long ans = 0;
cnt[l][r] = r - l + 1;
for(int i = l; i <= r; i++){
if(match[i] != -1){
int v = match[i];
if(v >= l && v <= r){
cnt[l][r]--;
} else {
return dp[l][r] = 0;
}
}
}
// assert(cnt[l][r] % 2 == 0);
ans = f[cnt[l][r]];
return (dp[l][r] = ans);
}
long long solve2(int l, int r){
if(dp2[l][r] != -1) return dp2[l][r];
if((r - l) % 2 == 0) return 0;
long long ans = solve(l, r);
for(int i = l; i < r; i++){
long long ini = (solve2(l, i) * solve(i + 1, r)) % mod;
ans = (ans + mod - ini) % mod;
}
return (dp2[l][r] = ans);
}
int main(){
cin >> n >> k;
n *= 2;
int aktif = n - 2 * k;
memset(match, -1, sizeof(match));
for(int i = 1; i <= k; i++){
int u,v; cin >> u >> v;
match[u] = v;
match[v] = u;
}
memset(f, 0, sizeof(f));
f[0] = 1;
for(int i = 2; i <= n; i++){
f[i] = (f[i - 2] * (i - 1)) % mod;
}
memset(dp, -1, sizeof(dp));
memset(dp2, -1, sizeof(dp2));
long long ans = 0;
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
if((j + i) % 2 == 0) continue;
solve(i, j);
long long x = (solve2(i, j) * f[aktif - cnt[i][j]]) % mod;
ans = (ans + x) % mod;
}
}
cout << ans << "\n";
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define mp make_pair
#define fs first
#define sc second
#define pb push_back
#define mod 1000000007
using namespace std;
int n,k;
int match[610];
long long f[610];
long long dp[610][610];
long long dp2[610][610];
long long cnt[610][610];
long long solve(int l, int r){
if(dp[l][r] != -1) return dp[l][r];
if((r - l) % 2 == 0) return 0;
long long ans = 0;
cnt[l][r] = r - l + 1;
for(int i = l; i <= r; i++){
if(match[i] != -1){
int v = match[i];
if(v >= l && v <= r){
cnt[l][r]--;
} else {
return dp[l][r] = 0;
}
}
}
// assert(cnt[l][r] % 2 == 0);
ans = f[cnt[l][r]];
return (dp[l][r] = ans);
}
long long solve2(int l, int r){
if(dp2[l][r] != -1) return dp2[l][r];
if((r - l) % 2 == 0) return 0;
long long ans = solve(l, r);
for(int i = l; i < r; i++){
long long ini = (solve2(l, i) * solve(i + 1, r)) % mod;
ans = (ans + mod - ini) % mod;
}
return (dp2[l][r] = ans);
}
int main(){
cin >> n >> k;
n *= 2;
int aktif = n - 2 * k;
memset(match, -1, sizeof(match));
for(int i = 1; i <= k; i++){
int u,v; cin >> u >> v;
match[u] = v;
match[v] = u;
}
memset(f, 0, sizeof(f));
f[0] = 1;
for(int i = 2; i <= n; i++){
f[i] = (f[i - 2] * (i - 1)) % mod;
}
memset(dp, -1, sizeof(dp));
memset(dp2, -1, sizeof(dp2));
long long ans = 0;
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
if((j + i) % 2 == 0) continue;
solve(i, j);
long long x = (solve2(i, j) * f[aktif - cnt[i][j]]) % mod;
ans = (ans + x) % mod;
}
}
cout << ans << "\n";
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 605;
const int MOD = 1e9 + 7;
int to[N], sum[N];
ll dp[N][N], g[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int n, m; cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
to[u] = v; to[v] = u;
sum[u]++; sum[v]++;
}
n *= 2; g[0] = 1;
for (int i = 1; i <= n; i++)
sum[i] += sum[i - 1];
for (int i = 2; i <= n; i += 2)
g[i] = g[i - 2] * (i - 1) % MOD;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j += 2) {
bool flag = 0;
for (int k = i; k <= j; k++)
if (to[k] && (to[k] < i || to[k] > j))
flag = 1;
if (flag) continue;
dp[i][j] = g[j - i + 1 - sum[j] + sum[i - 1]];
for (int k = i + 1; k < j; k += 2)
(dp[i][j] -= dp[i][k] * g[j - k - sum[j] + sum[k]]) %= MOD;
}
m *= 2; ll res = 0;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j += 2)
(res += dp[i][j] * g[n - j + i - 1 - m + sum[j] - sum[i - 1]]) %= MOD;
cout << (res + MOD) % MOD << '\n';
} | ### Prompt
Create a solution in cpp for the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 605;
const int MOD = 1e9 + 7;
int to[N], sum[N];
ll dp[N][N], g[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int n, m; cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
to[u] = v; to[v] = u;
sum[u]++; sum[v]++;
}
n *= 2; g[0] = 1;
for (int i = 1; i <= n; i++)
sum[i] += sum[i - 1];
for (int i = 2; i <= n; i += 2)
g[i] = g[i - 2] * (i - 1) % MOD;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j += 2) {
bool flag = 0;
for (int k = i; k <= j; k++)
if (to[k] && (to[k] < i || to[k] > j))
flag = 1;
if (flag) continue;
dp[i][j] = g[j - i + 1 - sum[j] + sum[i - 1]];
for (int k = i + 1; k < j; k += 2)
(dp[i][j] -= dp[i][k] * g[j - k - sum[j] + sum[k]]) %= MOD;
}
m *= 2; ll res = 0;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j += 2)
(res += dp[i][j] * g[n - j + i - 1 - m + sum[j] - sum[i - 1]]) %= MOD;
cout << (res + MOD) % MOD << '\n';
}
``` |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=605,P=1e9+7;
int n,K,ans,fac2[N],match[N],f[2][N];
int main(){
fac2[0]=1;
for(int i=1;i<N;++i)fac2[i]=1LL*fac2[i-1]*(2*i-1)%P;
scanf("%d%d",&n,&K);
for(int i=1;i<=K;++i){
int k1,k2;
scanf("%d%d",&k1,&k2);
match[k1]=k2,match[k2]=k1;
}
rep(i,1,n*2){
int cur=0;
memset(f[cur],0,sizeof(f[cur]));
f[cur][0]=1;
int sz1=0,sz2=0;
for(int j=i;j<=n*2;++j){
if(match[j]){
if(match[j]>j)++sz1;
else if(match[j]<i)break;
else --sz1;
}else{
++sz2;
cur^=1;
memset(f[cur],0,sizeof(f[cur]));
rep(k,0,min(n,sz2)){
if(f[cur^1][k]){
if(k){
(f[cur][k-1]+=1LL*f[cur^1][k]*k%P)%=P;
}
(f[cur][k+1]+=f[cur^1][k])%=P;
}
}
}
if(!sz1){
(ans+=1LL*f[cur][0]*fac2[n-K-(sz2/2)]%P)%=P;
f[cur][0]=0;
}
}
}
printf("%d\n",ans);
return 0;
} | ### Prompt
Create a solution in Cpp for the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=605,P=1e9+7;
int n,K,ans,fac2[N],match[N],f[2][N];
int main(){
fac2[0]=1;
for(int i=1;i<N;++i)fac2[i]=1LL*fac2[i-1]*(2*i-1)%P;
scanf("%d%d",&n,&K);
for(int i=1;i<=K;++i){
int k1,k2;
scanf("%d%d",&k1,&k2);
match[k1]=k2,match[k2]=k1;
}
rep(i,1,n*2){
int cur=0;
memset(f[cur],0,sizeof(f[cur]));
f[cur][0]=1;
int sz1=0,sz2=0;
for(int j=i;j<=n*2;++j){
if(match[j]){
if(match[j]>j)++sz1;
else if(match[j]<i)break;
else --sz1;
}else{
++sz2;
cur^=1;
memset(f[cur],0,sizeof(f[cur]));
rep(k,0,min(n,sz2)){
if(f[cur^1][k]){
if(k){
(f[cur][k-1]+=1LL*f[cur^1][k]*k%P)%=P;
}
(f[cur][k+1]+=f[cur^1][k])%=P;
}
}
}
if(!sz1){
(ans+=1LL*f[cur][0]*fac2[n-K-(sz2/2)]%P)%=P;
f[cur][0]=0;
}
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
#define M 1000000007
#define maxn 603
using namespace std;
typedef long long ll;
inline ll add(ll a, ll b) {return a + b < M ? a + b : a + b - M;}
inline ll sub(ll a, ll b) {return a - b < 0 ? a - b + M : a - b;}
inline ll mult(ll a, ll b) {return a * b % M;}
int n, k;
int a[maxn], b[maxn];
ll d[maxn][maxn];
int s[maxn][maxn];
ll g[maxn];
int main(){
#define TASK "ABC"
// freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> k;
n *= 2;
for (int i = 0; i < k; ++i) {
int a, b;
cin >> a >> b;
::a[i] = min(a, b);
::b[i] = max(a, b);
s[a][a] = s[b][b] = 1;
}
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = mult(g[i - 2], i - 1);
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
s[i][j] = s[i][j - 1] + s[j][j];
ll ans = 0;
for (int l = 1; l < n; l += 2)
for (int i = 1; i + l <= n; ++i) {
int j = i + l;
bool flag = 0;
for (int ik = 0; ik < k; ++ik)
if ((i <= a[ik] && a[ik] <= j) ^ (i <= b[ik] && b[ik] <= j))
flag = 1;
if (flag) continue;
d[i][j] = g[l + 1 - s[i][j]];
for (int ik = i + 1; ik < j; ik += 2)
d[i][j] = sub(d[i][j], mult(d[i][ik], g[j - ik - s[ik + 1][j]]));
ans = add(ans, mult(d[i][j], g[n - 2 * k + s[i][j] - l - 1]));
}
cout << ans;
return 0;
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define M 1000000007
#define maxn 603
using namespace std;
typedef long long ll;
inline ll add(ll a, ll b) {return a + b < M ? a + b : a + b - M;}
inline ll sub(ll a, ll b) {return a - b < 0 ? a - b + M : a - b;}
inline ll mult(ll a, ll b) {return a * b % M;}
int n, k;
int a[maxn], b[maxn];
ll d[maxn][maxn];
int s[maxn][maxn];
ll g[maxn];
int main(){
#define TASK "ABC"
// freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> k;
n *= 2;
for (int i = 0; i < k; ++i) {
int a, b;
cin >> a >> b;
::a[i] = min(a, b);
::b[i] = max(a, b);
s[a][a] = s[b][b] = 1;
}
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = mult(g[i - 2], i - 1);
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
s[i][j] = s[i][j - 1] + s[j][j];
ll ans = 0;
for (int l = 1; l < n; l += 2)
for (int i = 1; i + l <= n; ++i) {
int j = i + l;
bool flag = 0;
for (int ik = 0; ik < k; ++ik)
if ((i <= a[ik] && a[ik] <= j) ^ (i <= b[ik] && b[ik] <= j))
flag = 1;
if (flag) continue;
d[i][j] = g[l + 1 - s[i][j]];
for (int ik = i + 1; ik < j; ik += 2)
d[i][j] = sub(d[i][j], mult(d[i][ik], g[j - ik - s[ik + 1][j]]));
ans = add(ans, mult(d[i][j], g[n - 2 * k + s[i][j] - l - 1]));
}
cout << ans;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 607, mod = 1000000007;
int n, link, opp[N], g[N], f[N][N], c[N][N];
int main() {
scanf("%d%d", &n, &link);
n <<= 1;
while (link--) {
int x, y;
scanf("%d%d", &x, &y);
opp[x] = y;
opp[y] = x;
}
g[0] = 1;
for (int i = 2; i <= n; i += 2) {
g[i] = (ll) g[i - 2] * (i - 1) % mod;
}
for (int l = 1; l <= n; l++) {
for (int r = l; r <= n; r++) {
c[l][r] = c[l][r - 1] + !opp[r];
}
}
int ans = 0;
for (int len = 2; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1, ok = 1;
for (int i = l; i <= r; i++) {
if (opp[i] && (opp[i] < l || opp[i] > r)) {
ok = 0;
}
}
if (!ok) continue;
f[l][r] = g[c[l][r]];
for (int k = l + 1; k < r; k++) {
f[l][r] = (f[l][r] + mod - (ll) f[l][k] * g[c[k + 1][r]] % mod) % mod;
}
ans = (ans + (ll) f[l][r] * g[c[1][l - 1] + c[r + 1][n]]) % mod;
}
}
printf("%d\n", ans);
}
| ### Prompt
In cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 607, mod = 1000000007;
int n, link, opp[N], g[N], f[N][N], c[N][N];
int main() {
scanf("%d%d", &n, &link);
n <<= 1;
while (link--) {
int x, y;
scanf("%d%d", &x, &y);
opp[x] = y;
opp[y] = x;
}
g[0] = 1;
for (int i = 2; i <= n; i += 2) {
g[i] = (ll) g[i - 2] * (i - 1) % mod;
}
for (int l = 1; l <= n; l++) {
for (int r = l; r <= n; r++) {
c[l][r] = c[l][r - 1] + !opp[r];
}
}
int ans = 0;
for (int len = 2; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1, ok = 1;
for (int i = l; i <= r; i++) {
if (opp[i] && (opp[i] < l || opp[i] > r)) {
ok = 0;
}
}
if (!ok) continue;
f[l][r] = g[c[l][r]];
for (int k = l + 1; k < r; k++) {
f[l][r] = (f[l][r] + mod - (ll) f[l][k] * g[c[k + 1][r]] % mod) % mod;
}
ans = (ans + (ll) f[l][r] * g[c[1][l - 1] + c[r + 1][n]]) % mod;
}
}
printf("%d\n", ans);
}
``` |
#include <iostream>
using namespace std;
using LL = long long;
const int maxN = 605;
const int mod = 1e9 + 7;
int n, K, ans;
int to[maxN], sum[maxN], g[maxN];
int f[maxN][maxN];
bool vis[maxN];
inline int cnt(int i, int j)
{ return sum[j] - sum[i - 1]; }
inline void Mod(int& x)
{ x >= mod ? x -= mod : x < 0 ? x += mod : 0; }
int main()
{
cin >> n >> K;
for (int i = 1; i <= K; ++i)
{
int a, b;
cin >> a >> b;
to[a] = b, to[b] = a;
++sum[a], ++sum[b];
}
n <<= 1;
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = (LL)g[i - 2] * (i - 1) % mod;
for (int i = 1; i <= n; ++i)
sum[i] += sum[i - 1];
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; j += 2)
{
int flag = 0;
for (int k = i; k <= j; ++k)
if (to[k] and (to[k] < i or to[k] > j))
{
flag = 1;
break;
}
if (flag)
continue;
f[i][j] = g[j - i + 1 - cnt(i, j)];
for (int k = i + 1; k < j; k += 2)
Mod(f[i][j] -= (LL)f[i][k] * g[j - k - cnt(k + 1, j)] % mod);
}
}
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; j += 2)
Mod(ans += (LL)f[i][j] * g[n - j + i - 1 - cnt(1, i - 1) - cnt(j + 1, n)] % mod);
cout << ans << endl;
return 0;
} | ### Prompt
In CPP, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <iostream>
using namespace std;
using LL = long long;
const int maxN = 605;
const int mod = 1e9 + 7;
int n, K, ans;
int to[maxN], sum[maxN], g[maxN];
int f[maxN][maxN];
bool vis[maxN];
inline int cnt(int i, int j)
{ return sum[j] - sum[i - 1]; }
inline void Mod(int& x)
{ x >= mod ? x -= mod : x < 0 ? x += mod : 0; }
int main()
{
cin >> n >> K;
for (int i = 1; i <= K; ++i)
{
int a, b;
cin >> a >> b;
to[a] = b, to[b] = a;
++sum[a], ++sum[b];
}
n <<= 1;
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = (LL)g[i - 2] * (i - 1) % mod;
for (int i = 1; i <= n; ++i)
sum[i] += sum[i - 1];
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; j += 2)
{
int flag = 0;
for (int k = i; k <= j; ++k)
if (to[k] and (to[k] < i or to[k] > j))
{
flag = 1;
break;
}
if (flag)
continue;
f[i][j] = g[j - i + 1 - cnt(i, j)];
for (int k = i + 1; k < j; k += 2)
Mod(f[i][j] -= (LL)f[i][k] * g[j - k - cnt(k + 1, j)] % mod);
}
}
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; j += 2)
Mod(ans += (LL)f[i][j] * g[n - j + i - 1 - cnt(1, i - 1) - cnt(j + 1, n)] % mod);
cout << ans << endl;
return 0;
}
``` |
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define MAXN 605
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n,K,a[MAXN],b[MAXN];
int unpaired[MAXN];
int sum[MAXN];
int ffact[MAXN];
int dp[MAXN][MAXN];
void add(int &a,int b) {a+=b; if(a>=MOD) a-=MOD;}
void dec(int &a,int b) {a-=b; if(a<0) a+=MOD;}
int main()
{
scanf("%d%d",&n,&K);
n*=2;
ffact[0]=1;
for(int i=1;i<=600;i++)
{
if(i&1) ffact[i]=1LL*i*ffact[i-1]%MOD;
else ffact[i]=ffact[i-1];
}
for(int i=1;i<=600;i+=2) ffact[i]=0;
for(int i=1;i<=n;i++) unpaired[i]=1;
for(int i=0;i<K;i++)
{
scanf("%d%d",&a[i],&b[i]);
unpaired[a[i]]=unpaired[b[i]]=0;
}
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+unpaired[i];
int ans=0;
for(int len=2;len<=n;len+=2)
{
for(int i=1;i+len-1<=n;i++)
{
int j=i+len-1;
bool f=true;
for(int k=0;k<K;k++)
{
bool f1=(a[k]>=i&&a[k]<=j);
bool f2=(b[k]>=i&&b[k]<=j);
if(f1^f2) f=false;
}
if(!f)
{
dp[i][j]=0;
continue;
}
int x=sum[j]-sum[i-1];
dp[i][j]=ffact[x];
for(int k=i+1;k<j;k+=2)
{
int y=sum[j]-sum[k];
dec(dp[i][j],1LL*dp[i][k]*ffact[y]%MOD);
}
add(ans,1LL*dp[i][j]*ffact[sum[n]-x]%MOD);
}
}
printf("%d\n",ans);
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define MAXN 605
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n,K,a[MAXN],b[MAXN];
int unpaired[MAXN];
int sum[MAXN];
int ffact[MAXN];
int dp[MAXN][MAXN];
void add(int &a,int b) {a+=b; if(a>=MOD) a-=MOD;}
void dec(int &a,int b) {a-=b; if(a<0) a+=MOD;}
int main()
{
scanf("%d%d",&n,&K);
n*=2;
ffact[0]=1;
for(int i=1;i<=600;i++)
{
if(i&1) ffact[i]=1LL*i*ffact[i-1]%MOD;
else ffact[i]=ffact[i-1];
}
for(int i=1;i<=600;i+=2) ffact[i]=0;
for(int i=1;i<=n;i++) unpaired[i]=1;
for(int i=0;i<K;i++)
{
scanf("%d%d",&a[i],&b[i]);
unpaired[a[i]]=unpaired[b[i]]=0;
}
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+unpaired[i];
int ans=0;
for(int len=2;len<=n;len+=2)
{
for(int i=1;i+len-1<=n;i++)
{
int j=i+len-1;
bool f=true;
for(int k=0;k<K;k++)
{
bool f1=(a[k]>=i&&a[k]<=j);
bool f2=(b[k]>=i&&b[k]<=j);
if(f1^f2) f=false;
}
if(!f)
{
dp[i][j]=0;
continue;
}
int x=sum[j]-sum[i-1];
dp[i][j]=ffact[x];
for(int k=i+1;k<j;k+=2)
{
int y=sum[j]-sum[k];
dec(dp[i][j],1LL*dp[i][k]*ffact[y]%MOD);
}
add(ans,1LL*dp[i][j]*ffact[sum[n]-x]%MOD);
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int P = 1000000007;
int n, k, a[305], b[305], cnt[605], sum[605][605], f[605][605];
int calc(int xl, int xr, int yl, int yr) {
return sum[xr][yr] - sum[xr][yl - 1] - sum[xl - 1][yr] + sum[xl - 1][yl - 1];
}
int main() {
scanf("%d%d", &n, &k);
cnt[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
cnt[i] = cnt[i - 2] * (i - 1ll) % P;
for (int i = 1; i <= k; ++i) {
scanf("%d%d", a + i, b + i);
if (a[i] > b[i])
swap(a[i], b[i]);
sum[a[i]][b[i]] = 1;
}
for (int i = 1; i <= 2 * n; ++i)
for (int j = 1; j <= 2 * n; ++j)
sum[i][j] += sum[i][j - 1];
for (int i = 1; i <= 2 * n; ++i)
for (int j = 1; j <= 2 * n; ++j)
sum[i][j] += sum[i - 1][j];
int ans = 0;
for (int l = 2; l <= 2 * n; l += 2)
for (int i = 1; i + l - 1 <= 2 * n; ++i) {
int j = i + l - 1;
if (calc(1, i - 1, i, j) || calc(i, j, j + 1, 2 * n))
continue;
f[i][j] = cnt[j - i + 1 - 2 * calc(i, j, i, j)];
for (int m = i + 1; m < j; m += 2)
if (!calc(i, m, m + 1, j))
f[i][j] = (f[i][j] - 1ll * f[i][m] * cnt[j - m - 2 * calc(m + 1, j, m + 1, j)]) % P;
ans = (ans + 1ll * f[i][j] * cnt[2 * n - (j - i + 1) - 2 * (k - calc(i, j, i, j))]) % P;
}
printf("%d\n", (ans + P) % P);
return 0;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int P = 1000000007;
int n, k, a[305], b[305], cnt[605], sum[605][605], f[605][605];
int calc(int xl, int xr, int yl, int yr) {
return sum[xr][yr] - sum[xr][yl - 1] - sum[xl - 1][yr] + sum[xl - 1][yl - 1];
}
int main() {
scanf("%d%d", &n, &k);
cnt[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
cnt[i] = cnt[i - 2] * (i - 1ll) % P;
for (int i = 1; i <= k; ++i) {
scanf("%d%d", a + i, b + i);
if (a[i] > b[i])
swap(a[i], b[i]);
sum[a[i]][b[i]] = 1;
}
for (int i = 1; i <= 2 * n; ++i)
for (int j = 1; j <= 2 * n; ++j)
sum[i][j] += sum[i][j - 1];
for (int i = 1; i <= 2 * n; ++i)
for (int j = 1; j <= 2 * n; ++j)
sum[i][j] += sum[i - 1][j];
int ans = 0;
for (int l = 2; l <= 2 * n; l += 2)
for (int i = 1; i + l - 1 <= 2 * n; ++i) {
int j = i + l - 1;
if (calc(1, i - 1, i, j) || calc(i, j, j + 1, 2 * n))
continue;
f[i][j] = cnt[j - i + 1 - 2 * calc(i, j, i, j)];
for (int m = i + 1; m < j; m += 2)
if (!calc(i, m, m + 1, j))
f[i][j] = (f[i][j] - 1ll * f[i][m] * cnt[j - m - 2 * calc(m + 1, j, m + 1, j)]) % P;
ans = (ans + 1ll * f[i][j] * cnt[2 * n - (j - i + 1) - 2 * (k - calc(i, j, i, j))]) % P;
}
printf("%d\n", (ans + P) % P);
return 0;
}
``` |
#include<iostream>
#define int long long
using namespace std;
const int N=302;
const int mod=1e9+7;
int n,dp[2*N][2*N],damn[2*N],cnt[2*N],wow[2*N];
int dem(int l,int r){
// if(l==n*2+1||r==0){
// return 0;
// }
return r-l+1-cnt[r]+cnt[l-1];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int i,j,num,k,l,ans=0;
cin>>n>>num;
for(i=1;i<=num;i++){
cin>>j>>k;
damn[j]=k;
damn[k]=j;
cnt[j]++;
cnt[k]++;
}
wow[0]=1;
for(i=2;i<=2*n;i++){
cnt[i]+=cnt[i-1];
wow[i]=wow[i-2]*(i-1);
wow[i]%=mod;
}
bool cac=false;
for(i=1;i<=2*n;i++){
for(j=i+1;j<=2*n;j+=2){
cac=false;
for(k=i;k<=j;k++){
if(damn[k]){
if(damn[k]<i||damn[k]>j){
cac=true;
break;
}
}
}
if(cac){
continue;
}
dp[i][j]=wow[dem(i,j)];
for(k=i+1;k<j;k+=2){
dp[i][j]-=dp[i][k]*wow[dem(k+1,j)];
dp[i][j]%=mod;
if(dp[i][j]<0){
dp[i][j]+=mod;
}
}
ans+=dp[i][j]*wow[dem(j+1,2*n)+dem(1,i-1)];
ans%=mod;
// cout<<dp[i][j]<<' '<<i<<' '<<j<<endl;
}
}
cout<<ans;
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#define int long long
using namespace std;
const int N=302;
const int mod=1e9+7;
int n,dp[2*N][2*N],damn[2*N],cnt[2*N],wow[2*N];
int dem(int l,int r){
// if(l==n*2+1||r==0){
// return 0;
// }
return r-l+1-cnt[r]+cnt[l-1];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int i,j,num,k,l,ans=0;
cin>>n>>num;
for(i=1;i<=num;i++){
cin>>j>>k;
damn[j]=k;
damn[k]=j;
cnt[j]++;
cnt[k]++;
}
wow[0]=1;
for(i=2;i<=2*n;i++){
cnt[i]+=cnt[i-1];
wow[i]=wow[i-2]*(i-1);
wow[i]%=mod;
}
bool cac=false;
for(i=1;i<=2*n;i++){
for(j=i+1;j<=2*n;j+=2){
cac=false;
for(k=i;k<=j;k++){
if(damn[k]){
if(damn[k]<i||damn[k]>j){
cac=true;
break;
}
}
}
if(cac){
continue;
}
dp[i][j]=wow[dem(i,j)];
for(k=i+1;k<j;k+=2){
dp[i][j]-=dp[i][k]*wow[dem(k+1,j)];
dp[i][j]%=mod;
if(dp[i][j]<0){
dp[i][j]+=mod;
}
}
ans+=dp[i][j]*wow[dem(j+1,2*n)+dem(1,i-1)];
ans%=mod;
// cout<<dp[i][j]<<' '<<i<<' '<<j<<endl;
}
}
cout<<ans;
}
``` |
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define Mod 1000000007
using namespace std;
bool vis[610];
int A[310],B[310];
int val[610];
int n,k;
bool check(int a,int b,int x,int y)
{
return (x<=a&&a<=y)^(x<=b&&b<=y);
}
int dp[610][610],Dp[610][610];
int f(int,int);
int F(int,int);
int f(int x,int y)
{
if(y<=x)return 0;
if(dp[x][y]!=-1)return dp[x][y];
dp[x][y]=0;
for(int i=1;i<=k;i++)
if(check(A[i],B[i],x,y))return 0;
int cnt=0;
for(int i=x;i<=y;i++)
if(!vis[i])cnt++;
dp[x][y]=val[cnt];
for(int i=y-1;i>x;i--)
dp[x][y]=(dp[x][y]-(long long)f(i,y)*F(x,i-1)%Mod+Mod)%Mod;
return dp[x][y];
}
int F(int x,int y)
{
if(y<=x)return 0;
if(Dp[x][y]!=-1)return Dp[x][y];
Dp[x][y]=0;int cnt=0;
for(int i=y;i>=x;i--)
{
Dp[x][y]=(Dp[x][y]+(long long)f(x,i)*val[cnt])%Mod;
if(!vis[i])cnt++;
}
return Dp[x][y];
}
int main()
{
scanf("%d %d",&n,&k);
for(int i=1;i<=k;i++)
{
scanf("%d %d",&A[i],&B[i]);
vis[A[i]]=vis[B[i]]=true;
}
val[0]=1;
for(int i=2;i<=n*2;i+=2)
val[i]=(long long)val[i-2]*(i-1)%Mod;
memset(dp,-1,sizeof(dp));
memset(Dp,-1,sizeof(Dp));
int ans=0;
for(int i=1;i<n*2;i++)
for(int j=i+1;j<=n*2;j++)
{
int cnt=0;
for(int k=1;k<i;k++)
if(!vis[k])cnt++;
for(int k=j+1;k<=n*2;k++)
if(!vis[k])cnt++;
ans=(ans+(long long)f(i,j)*val[cnt])%Mod;
}
printf("%d\n",ans);
return 0;
} | ### Prompt
Please create a solution in Cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define Mod 1000000007
using namespace std;
bool vis[610];
int A[310],B[310];
int val[610];
int n,k;
bool check(int a,int b,int x,int y)
{
return (x<=a&&a<=y)^(x<=b&&b<=y);
}
int dp[610][610],Dp[610][610];
int f(int,int);
int F(int,int);
int f(int x,int y)
{
if(y<=x)return 0;
if(dp[x][y]!=-1)return dp[x][y];
dp[x][y]=0;
for(int i=1;i<=k;i++)
if(check(A[i],B[i],x,y))return 0;
int cnt=0;
for(int i=x;i<=y;i++)
if(!vis[i])cnt++;
dp[x][y]=val[cnt];
for(int i=y-1;i>x;i--)
dp[x][y]=(dp[x][y]-(long long)f(i,y)*F(x,i-1)%Mod+Mod)%Mod;
return dp[x][y];
}
int F(int x,int y)
{
if(y<=x)return 0;
if(Dp[x][y]!=-1)return Dp[x][y];
Dp[x][y]=0;int cnt=0;
for(int i=y;i>=x;i--)
{
Dp[x][y]=(Dp[x][y]+(long long)f(x,i)*val[cnt])%Mod;
if(!vis[i])cnt++;
}
return Dp[x][y];
}
int main()
{
scanf("%d %d",&n,&k);
for(int i=1;i<=k;i++)
{
scanf("%d %d",&A[i],&B[i]);
vis[A[i]]=vis[B[i]]=true;
}
val[0]=1;
for(int i=2;i<=n*2;i+=2)
val[i]=(long long)val[i-2]*(i-1)%Mod;
memset(dp,-1,sizeof(dp));
memset(Dp,-1,sizeof(Dp));
int ans=0;
for(int i=1;i<n*2;i++)
for(int j=i+1;j<=n*2;j++)
{
int cnt=0;
for(int k=1;k<i;k++)
if(!vis[k])cnt++;
for(int k=j+1;k<=n*2;k++)
if(!vis[k])cnt++;
ans=(ans+(long long)f(i,j)*val[cnt])%Mod;
}
printf("%d\n",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int n, k, a[610], b[610], pd[610];
long long cnt[610][610], dp[610][610], g[2000], ans;
int main() {
scanf("%d%d", &n, &k);
for(int i = 1; i <= k; ++i) {
scanf("%d%d", &a[i], &b[i]);
pd[a[i]] = b[i];
pd[b[i]] = a[i];
}
n <<= 1;
g[0] = 1;
for(int i = 2; i <= n; i += 2) g[i] = (g[i - 2] * (i - 1)) % mod;
for(int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) cnt[i][j] = cnt[i][j - 1] + (!pd[j]);
}
for(int i = 1; i <= n; ++i) {
for(int j = i; j <= n; ++j) {
int ji = 0;
for(int k = i; k <= j; ++k) {
if (pd[k] && (pd[k] > j || pd[k] < i))
ji = 1;
}
if(ji == 1)
continue;
dp[i][j] = g[cnt[i][j]];
for(int k = i + 1; k <= j - 1; ++k)
dp[i][j] = (dp[i][j] - 1ll * dp[i][k] * g[cnt[k + 1][j]] % mod + mod) % mod;
}
}
for(int i = 1; i <= n; ++i) {
for(int j = i; j <= n; ++j) ans = (ans + dp[i][j] * g[(n / 2 - k) * 2 - cnt[i][j]] % mod) % mod;
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int n, k, a[610], b[610], pd[610];
long long cnt[610][610], dp[610][610], g[2000], ans;
int main() {
scanf("%d%d", &n, &k);
for(int i = 1; i <= k; ++i) {
scanf("%d%d", &a[i], &b[i]);
pd[a[i]] = b[i];
pd[b[i]] = a[i];
}
n <<= 1;
g[0] = 1;
for(int i = 2; i <= n; i += 2) g[i] = (g[i - 2] * (i - 1)) % mod;
for(int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) cnt[i][j] = cnt[i][j - 1] + (!pd[j]);
}
for(int i = 1; i <= n; ++i) {
for(int j = i; j <= n; ++j) {
int ji = 0;
for(int k = i; k <= j; ++k) {
if (pd[k] && (pd[k] > j || pd[k] < i))
ji = 1;
}
if(ji == 1)
continue;
dp[i][j] = g[cnt[i][j]];
for(int k = i + 1; k <= j - 1; ++k)
dp[i][j] = (dp[i][j] - 1ll * dp[i][k] * g[cnt[k + 1][j]] % mod + mod) % mod;
}
}
for(int i = 1; i <= n; ++i) {
for(int j = i; j <= n; ++j) ans = (ans + dp[i][j] * g[(n / 2 - k) * 2 - cnt[i][j]] % mod) % mod;
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 610
#define MOD 1000000007
int n, m, a[MAXN], b[MAXN], f[MAXN][MAXN];
int g[MAXN], s[MAXN], ans, in, out;
inline void up(int &x, int y) {
if ((x += y) >= MOD) x -= MOD;
}
int main() {
scanf("%d%d", &n, &m);
g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2) g[i] = 1LL * g[i - 2] * (i - 1) % MOD;
for (int i = 1; i <= 2 * n; i++) s[i] = 1;
for (int i = 1; i <= m; i++) scanf("%d%d", a + i, b + i), s[a[i]]--, s[b[i]]--;
for (int i = 1; i <= 2 * n; i++) s[i] += s[i - 1];
// for (int i = 1; i <= 2 * n; i++) printf("%d ", s[i]);
// puts("");
for (int i = 2 * n; i >= 1; i--) {
for (int j = i; j <= 2 * n; j++) {
for (int k = 1; k <= m; k++) {
if ((i <= a[k] && a[k] <= j) != (i <= b[k] && b[k] <= j)) goto fail;
}
in = s[j] - s[i - 1];
// printf("in %d out %d\n", in, out);
f[i][j] = 1LL * g[in] * g[out] % MOD;
for (int k = i; k < j; k++) {
int rest = s[j] - s[k];
up(f[i][j], MOD - 1LL * f[i][k] * g[rest] % MOD);
}
// printf("f[%d][%d] = %d\n", i, j, f[i][j]);
fail:;
}
}
for(int i = 1;i <= 2 * n;i++){
for(int j = i;j <= 2 * n;j++){
int out = s[2 * n] - s[j] + s[i - 1];
up(ans, 1LL * f[i][j] * g[out] % MOD);
}
}
printf("%d\n", ans);
return 0;
} | ### Prompt
Please create a solution in Cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 610
#define MOD 1000000007
int n, m, a[MAXN], b[MAXN], f[MAXN][MAXN];
int g[MAXN], s[MAXN], ans, in, out;
inline void up(int &x, int y) {
if ((x += y) >= MOD) x -= MOD;
}
int main() {
scanf("%d%d", &n, &m);
g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2) g[i] = 1LL * g[i - 2] * (i - 1) % MOD;
for (int i = 1; i <= 2 * n; i++) s[i] = 1;
for (int i = 1; i <= m; i++) scanf("%d%d", a + i, b + i), s[a[i]]--, s[b[i]]--;
for (int i = 1; i <= 2 * n; i++) s[i] += s[i - 1];
// for (int i = 1; i <= 2 * n; i++) printf("%d ", s[i]);
// puts("");
for (int i = 2 * n; i >= 1; i--) {
for (int j = i; j <= 2 * n; j++) {
for (int k = 1; k <= m; k++) {
if ((i <= a[k] && a[k] <= j) != (i <= b[k] && b[k] <= j)) goto fail;
}
in = s[j] - s[i - 1];
// printf("in %d out %d\n", in, out);
f[i][j] = 1LL * g[in] * g[out] % MOD;
for (int k = i; k < j; k++) {
int rest = s[j] - s[k];
up(f[i][j], MOD - 1LL * f[i][k] * g[rest] % MOD);
}
// printf("f[%d][%d] = %d\n", i, j, f[i][j]);
fail:;
}
}
for(int i = 1;i <= 2 * n;i++){
for(int j = i;j <= 2 * n;j++){
int out = s[2 * n] - s[j] + s[i - 1];
up(ans, 1LL * f[i][j] * g[out] % MOD);
}
}
printf("%d\n", ans);
return 0;
}
``` |
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 610
#define ll long long
#define mod 1000000007
using namespace std;
int link[N],tot[N][N];
ll f[N][N],h[N<<1];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
n<<=1;
for(int i=1;i<=k;i++)
{
int u,v;
scanf("%d%d",&u,&v);
link[u]=v;
link[v]=u;
}
h[0]=1,h[1]=0;
for(int i=2;i<=n;i+=2) h[i]=h[i-2]*(i-1)%mod;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
tot[i][j]=tot[i][j-1]+(!link[j]);
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
bool have=false;
for(int l=i;l<=j;l++)
if(link[l] && (link[l]>j || link[l]<i)) have=true;
if(have) continue;
f[i][j]=h[tot[i][j]];
for(int l=i+1;l<=j-1;l++)
f[i][j]=(f[i][j]-f[i][l]*h[tot[l+1][j]]%mod+mod)%mod;
}
ll ans=0;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
ans=(ans+f[i][j]*h[n-k*2-tot[i][j]]%mod)%mod;
}
printf("%lld\n",ans);
return 0;
}
| ### Prompt
Please formulate a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 610
#define ll long long
#define mod 1000000007
using namespace std;
int link[N],tot[N][N];
ll f[N][N],h[N<<1];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
n<<=1;
for(int i=1;i<=k;i++)
{
int u,v;
scanf("%d%d",&u,&v);
link[u]=v;
link[v]=u;
}
h[0]=1,h[1]=0;
for(int i=2;i<=n;i+=2) h[i]=h[i-2]*(i-1)%mod;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
tot[i][j]=tot[i][j-1]+(!link[j]);
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
bool have=false;
for(int l=i;l<=j;l++)
if(link[l] && (link[l]>j || link[l]<i)) have=true;
if(have) continue;
f[i][j]=h[tot[i][j]];
for(int l=i+1;l<=j-1;l++)
f[i][j]=(f[i][j]-f[i][l]*h[tot[l+1][j]]%mod+mod)%mod;
}
ll ans=0;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
ans=(ans+f[i][j]*h[n-k*2-tot[i][j]]%mod)%mod;
}
printf("%lld\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
#define mo 1000000007
#define ny 499122177
#define maxn 1000000000000000000LL
#define pi 3.1415926535898
#define eps 1e-9
using namespace std;
long long read(){
long long xx=0,flagg=1;
char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')
ch=getchar();
if(ch=='-'){
flagg=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
xx=xx*10+ch-'0';
ch=getchar();
}
return xx*flagg;
}
void pus(long long xx,long long flagg){
if(xx<0){
putchar('-');
xx=-xx;
}
if(xx>=10)
pus(xx/10,0);
putchar(xx%10+'0');
if(flagg==1)
putchar(' ');
if(flagg==2)
putchar('\n');
return;
}
long long n,m,i,j,k,flag,u,v,ans,book[605],sum[605],f[605][605],g[605];
long long cz(int l,int r){
if(l>r)
return 0;
return r-l+1-sum[r]+sum[l-1];
}
int main(){
n=read()*2;m=read()*2;
g[0]=1;
for(i=1;i<=(m>>1);i++){
u=read();
v=read();
book[u]=v;
book[v]=u;
sum[u]=1;
sum[v]=1;
}
for(i=1;i<=n;i++)
sum[i]+=sum[i-1];
for(i=2;i<=n;i+=2)
g[i]=(i-1)*g[i-2]%mo;
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
if((j-i)&1){
flag=0;
for(k=i;k<=j;k++)
if(book[k]&&(book[k]<i||book[k]>j))
flag=1;
if(flag)
continue;
f[i][j]=g[cz(i,j)];
for(k=i+1;k<j;k++)
f[i][j]=(f[i][j]-f[i][k]*g[cz(k+1,j)]%mo+mo)%mo;
ans=(ans+f[i][j]*g[cz(1,i-1)+cz(j+1,n)])%mo;
}
pus(ans,2);
return 0;
} | ### Prompt
Construct a CPP code solution to the problem outlined:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define mo 1000000007
#define ny 499122177
#define maxn 1000000000000000000LL
#define pi 3.1415926535898
#define eps 1e-9
using namespace std;
long long read(){
long long xx=0,flagg=1;
char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')
ch=getchar();
if(ch=='-'){
flagg=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
xx=xx*10+ch-'0';
ch=getchar();
}
return xx*flagg;
}
void pus(long long xx,long long flagg){
if(xx<0){
putchar('-');
xx=-xx;
}
if(xx>=10)
pus(xx/10,0);
putchar(xx%10+'0');
if(flagg==1)
putchar(' ');
if(flagg==2)
putchar('\n');
return;
}
long long n,m,i,j,k,flag,u,v,ans,book[605],sum[605],f[605][605],g[605];
long long cz(int l,int r){
if(l>r)
return 0;
return r-l+1-sum[r]+sum[l-1];
}
int main(){
n=read()*2;m=read()*2;
g[0]=1;
for(i=1;i<=(m>>1);i++){
u=read();
v=read();
book[u]=v;
book[v]=u;
sum[u]=1;
sum[v]=1;
}
for(i=1;i<=n;i++)
sum[i]+=sum[i-1];
for(i=2;i<=n;i+=2)
g[i]=(i-1)*g[i-2]%mo;
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
if((j-i)&1){
flag=0;
for(k=i;k<=j;k++)
if(book[k]&&(book[k]<i||book[k]>j))
flag=1;
if(flag)
continue;
f[i][j]=g[cz(i,j)];
for(k=i+1;k<j;k++)
f[i][j]=(f[i][j]-f[i][k]*g[cz(k+1,j)]%mo+mo)%mo;
ans=(ans+f[i][j]*g[cz(1,i-1)+cz(j+1,n)])%mo;
}
pus(ans,2);
return 0;
}
``` |
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long LL;
const int maxn=305;
const LL mo=1e9+7;
int n,k,op[2*maxn];
bool bz[2*maxn],vis[2*maxn];
LL f[2*maxn][2*maxn];
LL ffac[2*maxn];
void Pre(int n)
{
ffac[1]=1;
fo(i,3,n) if (i&1) ffac[i]=ffac[i-2]*i%mo;
}
LL s(int x) {return (x==0) ?1 :ffac[x-1];}
priority_queue<int> Q,Q0;
int main()
{
//freopen("d.in","r",stdin);
//freopen("d.out","w",stdout);
Pre(600);
scanf("%d %d",&n,&k);
fo(i,1,k)
{
int x,y;
scanf("%d %d",&x,&y);
bz[x]=bz[y]=1;
op[x]=y, op[y]=x;
}
int allfre=2*(n-k);
LL ans=0;
fo(i,1,2*n)
{
memset(vis,0,sizeof(vis));
vis[i]=1;
int fre=(!bz[i]);
Q=Q0;
if (bz[i]) Q.push(-op[i]);
for(int j=i+1; j<=2*n; j++)
{
vis[j]=1;
fre+=(!bz[j]);
if (bz[j] && !vis[op[j]]) Q.push(-op[j]);
if (!Q.empty() && -Q.top()==j) Q.pop();
if (Q.empty())
{
if (fre%2==0)
{
f[i][j]=s(fre);
for(int k=i+1, nowfre=(!bz[i])+(!bz[k]); k<j; k++, nowfre+=(!bz[k]))
if (nowfre%2==0) (f[i][j]-=f[i][k]*s(fre-nowfre))%=mo;
}
}
(ans+=f[i][j]*s(allfre-fre))%=mo;
}
}
printf("%lld\n",(ans+mo)%mo);
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long LL;
const int maxn=305;
const LL mo=1e9+7;
int n,k,op[2*maxn];
bool bz[2*maxn],vis[2*maxn];
LL f[2*maxn][2*maxn];
LL ffac[2*maxn];
void Pre(int n)
{
ffac[1]=1;
fo(i,3,n) if (i&1) ffac[i]=ffac[i-2]*i%mo;
}
LL s(int x) {return (x==0) ?1 :ffac[x-1];}
priority_queue<int> Q,Q0;
int main()
{
//freopen("d.in","r",stdin);
//freopen("d.out","w",stdout);
Pre(600);
scanf("%d %d",&n,&k);
fo(i,1,k)
{
int x,y;
scanf("%d %d",&x,&y);
bz[x]=bz[y]=1;
op[x]=y, op[y]=x;
}
int allfre=2*(n-k);
LL ans=0;
fo(i,1,2*n)
{
memset(vis,0,sizeof(vis));
vis[i]=1;
int fre=(!bz[i]);
Q=Q0;
if (bz[i]) Q.push(-op[i]);
for(int j=i+1; j<=2*n; j++)
{
vis[j]=1;
fre+=(!bz[j]);
if (bz[j] && !vis[op[j]]) Q.push(-op[j]);
if (!Q.empty() && -Q.top()==j) Q.pop();
if (Q.empty())
{
if (fre%2==0)
{
f[i][j]=s(fre);
for(int k=i+1, nowfre=(!bz[i])+(!bz[k]); k<j; k++, nowfre+=(!bz[k]))
if (nowfre%2==0) (f[i][j]-=f[i][k]*s(fre-nowfre))%=mo;
}
}
(ans+=f[i][j]*s(allfre-fre))%=mo;
}
}
printf("%lld\n",(ans+mo)%mo);
}
``` |
//ΔAGC028D
#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 666;
const int MO = 1e9+7;
int n,m,a[N],b[N],c[N];
LL d[N],f[N],ans;
void solve(int u){
int i,j;
LL x;
for(i=2;i<=u;i=i+2){
f[i]=d[c[i]];
for(j=1;j<=i;j=j+1)
if(b[j]>i)
f[i]=0;
if(!f[i])
continue;
for(j=2;j<i;j=j+2)
f[i]+=MO-f[j]*d[c[i]-c[j]]%MO;
f[i]%=MO;
ans+=f[i]*d[m-c[i]]%MO;
//if(f[i])
// cout<<f[i]<<n-u+1<<n-u+i<<d[m-c[i]]<<endl;
}
}
int main()
{
int i,x,y;
d[0]=1;
for(i=2;i<N;i=i+2)
d[i]=d[i-2]*(i-1)%MO;
scanf("%d%d",&n,&m);
n=n+n;
while(m--){
scanf("%d%d",&x,&y);
a[x]=y,a[y]=x;
}
m=0;
for(i=1;i<=n;i=i+1)
if(!a[i])
m++;
for(i=1;i<=n;i=i+1){
memset(b,0,sizeof(b));
for(x=i;x<=n;x=x+1){
if(a[x]&&a[x]<i)
b[x-i+1]=N;
else{
if(a[x])
b[x-i+1]=a[x]-i+1;
}
}
for(x=1;x<=n;x=x+1)
c[x]=c[x-1]+(b[x]==0);
solve(n-i+1);
}
cout<<ans%MO;
return 0;
} | ### Prompt
Your task is to create a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
//ΔAGC028D
#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 666;
const int MO = 1e9+7;
int n,m,a[N],b[N],c[N];
LL d[N],f[N],ans;
void solve(int u){
int i,j;
LL x;
for(i=2;i<=u;i=i+2){
f[i]=d[c[i]];
for(j=1;j<=i;j=j+1)
if(b[j]>i)
f[i]=0;
if(!f[i])
continue;
for(j=2;j<i;j=j+2)
f[i]+=MO-f[j]*d[c[i]-c[j]]%MO;
f[i]%=MO;
ans+=f[i]*d[m-c[i]]%MO;
//if(f[i])
// cout<<f[i]<<n-u+1<<n-u+i<<d[m-c[i]]<<endl;
}
}
int main()
{
int i,x,y;
d[0]=1;
for(i=2;i<N;i=i+2)
d[i]=d[i-2]*(i-1)%MO;
scanf("%d%d",&n,&m);
n=n+n;
while(m--){
scanf("%d%d",&x,&y);
a[x]=y,a[y]=x;
}
m=0;
for(i=1;i<=n;i=i+1)
if(!a[i])
m++;
for(i=1;i<=n;i=i+1){
memset(b,0,sizeof(b));
for(x=i;x<=n;x=x+1){
if(a[x]&&a[x]<i)
b[x-i+1]=N;
else{
if(a[x])
b[x-i+1]=a[x]-i+1;
}
}
for(x=1;x<=n;x=x+1)
c[x]=c[x-1]+(b[x]==0);
solve(n-i+1);
}
cout<<ans%MO;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<int,int>
#define poly vector<int>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
ll x = 0; char ch = gc(); bool positive = 1;
for (; !isdigit(ch); ch = gc()) if (ch == '-') positive = 0;
for (; isdigit(ch); ch = gc()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(ll a){
if(a<0){
a=-a; putchar('-');
}
if(a>=10)write(a/10);
putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
const int N=305,mod=1e9+7;
int x[N],y[N],s[N<<1];
ll g[N<<1],dp[N<<1][N<<1];
bool in(int x,int l,int r){
return l<=x&&x<=r;
}
int main(){
int n=read()*2,k=read();
For(i,1,k){
x[i]=read(); y[i]=read();
s[x[i]]=1; s[y[i]]=1;
}
For(i,1,n)s[i]=1-s[i]+s[i-1];
g[0]=1; for(int i=2;i<=n;i+=2)g[i]=g[i-2]*(i-1)%mod;
Rep(i,n,1)for(int j=i+1;j<=n;j+=2){
int f=1;
For(o,1,k)if(in(x[o],i,j)!=in(y[o],i,j))f=0;
if(f){
dp[i][j]=g[s[j]-s[i-1]];
for(int o=i+1;o<j;o+=2)dp[i][j]=(dp[i][j]-dp[i][o]*g[s[j]-s[o]])%mod;
}
}
ll ans=0;
For(i,1,n)for(int j=i+1;j<=n;j+=2)if(dp[i][j])ans=(ans+dp[i][j]*g[s[i-1]+s[n]-s[j]])%mod;
cout<<(ans+mod)%mod<<endl;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<int,int>
#define poly vector<int>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
ll x = 0; char ch = gc(); bool positive = 1;
for (; !isdigit(ch); ch = gc()) if (ch == '-') positive = 0;
for (; isdigit(ch); ch = gc()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(ll a){
if(a<0){
a=-a; putchar('-');
}
if(a>=10)write(a/10);
putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
const int N=305,mod=1e9+7;
int x[N],y[N],s[N<<1];
ll g[N<<1],dp[N<<1][N<<1];
bool in(int x,int l,int r){
return l<=x&&x<=r;
}
int main(){
int n=read()*2,k=read();
For(i,1,k){
x[i]=read(); y[i]=read();
s[x[i]]=1; s[y[i]]=1;
}
For(i,1,n)s[i]=1-s[i]+s[i-1];
g[0]=1; for(int i=2;i<=n;i+=2)g[i]=g[i-2]*(i-1)%mod;
Rep(i,n,1)for(int j=i+1;j<=n;j+=2){
int f=1;
For(o,1,k)if(in(x[o],i,j)!=in(y[o],i,j))f=0;
if(f){
dp[i][j]=g[s[j]-s[i-1]];
for(int o=i+1;o<j;o+=2)dp[i][j]=(dp[i][j]-dp[i][o]*g[s[j]-s[o]])%mod;
}
}
ll ans=0;
For(i,1,n)for(int j=i+1;j<=n;j+=2)if(dp[i][j])ans=(ans+dp[i][j]*g[s[i-1]+s[n]-s[j]])%mod;
cout<<(ans+mod)%mod<<endl;
}
``` |
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define LL long long
#define fgx cerr<<"--------------"<<endl;
#define dgx cerr<<"=============="<<endl;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN = 100010;
const int INF = 2147483600;
const LL Mod = 1000000007;
int N,K;
int a[MAXN+1],b[MAXN+1];
int sum[MAXN+1];
LL f[610][610]; LL g[MAXN+1];
inline int S(int l,int r){ return sum[r]-sum[l-1]; }
inline bool In(int x,int l,int r){ return x>=l&&x<=r; }
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
N=read()*2,K=read();
for(int i=1;i<=K;i++){
a[i]=read(),b[i]=read();
sum[a[i]]=sum[b[i]]=1;
} LL ans=0; g[0]=1;
for(int i=1;i<=N;i++){
if(i&1) g[i]=0;
else g[i]=g[i-2]*(i-1)%Mod;
}
for(int i=1;i<=N;i++) sum[i]+=sum[i-1];
for(int len=1;len<=N;len++){
if(len&1) continue;
for(int i=1;i+len-1<=N;i++){
int j=i+len-1; bool flag=0;
for(int k=1;k<=K;k++){
if(In(a[k],i,j)&&!In(b[k],i,j)) {
flag=1; break;
} if(!In(a[k],i,j)&&In(b[k],i,j)){
flag=1; break;
}
} if(flag){
f[i][j]=0; continue;
}
f[i][j]=g[len-S(i,j)];
for(int k=i;k<j;k++)
f[i][j]=(f[i][j]-g[j-k-S(k+1,j)]*f[i][k]%Mod+Mod)%Mod;
(ans+=f[i][j]*g[N-len-2*K+S(i,j)]%Mod)%=Mod;
}
} printf("%lld\n",ans);
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define LL long long
#define fgx cerr<<"--------------"<<endl;
#define dgx cerr<<"=============="<<endl;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN = 100010;
const int INF = 2147483600;
const LL Mod = 1000000007;
int N,K;
int a[MAXN+1],b[MAXN+1];
int sum[MAXN+1];
LL f[610][610]; LL g[MAXN+1];
inline int S(int l,int r){ return sum[r]-sum[l-1]; }
inline bool In(int x,int l,int r){ return x>=l&&x<=r; }
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
N=read()*2,K=read();
for(int i=1;i<=K;i++){
a[i]=read(),b[i]=read();
sum[a[i]]=sum[b[i]]=1;
} LL ans=0; g[0]=1;
for(int i=1;i<=N;i++){
if(i&1) g[i]=0;
else g[i]=g[i-2]*(i-1)%Mod;
}
for(int i=1;i<=N;i++) sum[i]+=sum[i-1];
for(int len=1;len<=N;len++){
if(len&1) continue;
for(int i=1;i+len-1<=N;i++){
int j=i+len-1; bool flag=0;
for(int k=1;k<=K;k++){
if(In(a[k],i,j)&&!In(b[k],i,j)) {
flag=1; break;
} if(!In(a[k],i,j)&&In(b[k],i,j)){
flag=1; break;
}
} if(flag){
f[i][j]=0; continue;
}
f[i][j]=g[len-S(i,j)];
for(int k=i;k<j;k++)
f[i][j]=(f[i][j]-g[j-k-S(k+1,j)]*f[i][k]%Mod+Mod)%Mod;
(ans+=f[i][j]*g[N-len-2*K+S(i,j)]%Mod)%=Mod;
}
} printf("%lld\n",ans);
return 0;
}
``` |
#include<cstdio>
#define RI register int
#define CI const int&
using namespace std;
const int N=605,mod=1e9+7;
int n,k,x,y,f[N][N],p[N],g[N],sum[N],ans;
inline void DP(CI l,CI r)
{
if ((sum[r]-sum[l-1])&1) return; RI i;
for (i=l;i<=r;++i) if (p[i]&&(p[i]<l||p[i]>r)) return;
int ret=g[sum[r]-sum[l-1]]; for (i=l+1;i<=r;++i)
if (f[l][i-1]) ret=(ret-1LL*f[l][i-1]*g[sum[r]-sum[i-1]]%mod+mod)%mod;
f[l][r]=ret; (ans+=1LL*ret*g[n-(k<<1)-(sum[r]-sum[l-1])]%mod)%=mod;
}
int main()
{
RI i,j; for (scanf("%d%d",&n,&k),n<<=1,i=1;i<=k;++i)
scanf("%d%d",&x,&y),p[x]=y,p[y]=x;
for (g[0]=1,i=2;i<=n;i+=2) g[i]=1LL*g[i-2]*(i-1)%mod;
for (i=1;i<=n;++i) sum[i]=sum[i-1]+(!p[i]);
for (j=1;j<=n;++j) for (i=n;i-j+1;--i) DP(i-j+1,i);
return printf("%d",ans),0;
} | ### Prompt
Please formulate a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#define RI register int
#define CI const int&
using namespace std;
const int N=605,mod=1e9+7;
int n,k,x,y,f[N][N],p[N],g[N],sum[N],ans;
inline void DP(CI l,CI r)
{
if ((sum[r]-sum[l-1])&1) return; RI i;
for (i=l;i<=r;++i) if (p[i]&&(p[i]<l||p[i]>r)) return;
int ret=g[sum[r]-sum[l-1]]; for (i=l+1;i<=r;++i)
if (f[l][i-1]) ret=(ret-1LL*f[l][i-1]*g[sum[r]-sum[i-1]]%mod+mod)%mod;
f[l][r]=ret; (ans+=1LL*ret*g[n-(k<<1)-(sum[r]-sum[l-1])]%mod)%=mod;
}
int main()
{
RI i,j; for (scanf("%d%d",&n,&k),n<<=1,i=1;i<=k;++i)
scanf("%d%d",&x,&y),p[x]=y,p[y]=x;
for (g[0]=1,i=2;i<=n;i+=2) g[i]=1LL*g[i-2]*(i-1)%mod;
for (i=1;i<=n;++i) sum[i]=sum[i-1]+(!p[i]);
for (j=1;j<=n;++j) for (i=n;i-j+1;--i) DP(i-j+1,i);
return printf("%d",ans),0;
}
``` |
#include<bits/stdc++.h>
#define For(i,l,r) for(int i = (l),i##end = (r);i <= i##end;i++)
#define Fordown(i,r,l) for(int i = (r),i##end = (l);i >= i##end;i--)
#define debug(x) cout << #x << " = " << x << endl
using namespace std;
typedef long long ll;
template <typename T> inline bool chkmin(T &x,T y) { return y < x ? x = y,1 : 0; }
template <typename T> inline bool chkmax(T &x,T y) { return x < y ? x = y,1 : 0; }
const int INF = 0x3f3f3f3f;
const int N = 6e2 + 10,mod = 1e9 + 7;
int Sum[N],a[N],b[N],val[N],dp[N][N];
int n,m;
inline int read() {
int x = 0,flag = 1;
char ch = getchar();
while(!isdigit(ch) && ch != '-')ch = getchar();
if(ch == '-')flag = -1,ch = getchar();
while(isdigit(ch))x = (x << 3) + (x << 1) + (ch - '0'),ch = getchar();
return x * flag;
}
inline bool in(int p,int l,int r) {
return p >= l && p <= r;
}
inline bool check(int l,int r) {
For(i,1,m) if(in(a[i],l,r) ^ in(b[i],l,r)) return false;
return true;
}
int main() {
n = read() << 1,m = read();
For(i,1,n) Sum[i] = 1;
val[0] = 1;
for(int i = 2;i <= n;i += 2) val[i] = 1ll * val[i - 2] * (i - 1) % mod;
For(i,1,m) {
a[i] = read(),b[i] = read();
Sum[a[i]] = Sum[b[i]] = 0;
}
For(i,1,n) Sum[i] += Sum[i - 1];
int ans = 0;
For(len,1,n) For(i,1,n - len + 1) {
int j = i + len - 1,tot = Sum[j] - Sum[i - 1];
if(!check(i,j) || (tot & 1)) continue;
int cur = val[tot];
For(k,i,j - 1) cur = (cur - 1ll * dp[i][k] * val[Sum[j] - Sum[k]]) % mod;
cur = (cur + mod) % mod;
ans = (ans + 1ll * (dp[i][j] = cur) * val[Sum[n] - tot]) % mod;
}
printf("%d\n",ans);
return 0;
} | ### Prompt
Generate a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define For(i,l,r) for(int i = (l),i##end = (r);i <= i##end;i++)
#define Fordown(i,r,l) for(int i = (r),i##end = (l);i >= i##end;i--)
#define debug(x) cout << #x << " = " << x << endl
using namespace std;
typedef long long ll;
template <typename T> inline bool chkmin(T &x,T y) { return y < x ? x = y,1 : 0; }
template <typename T> inline bool chkmax(T &x,T y) { return x < y ? x = y,1 : 0; }
const int INF = 0x3f3f3f3f;
const int N = 6e2 + 10,mod = 1e9 + 7;
int Sum[N],a[N],b[N],val[N],dp[N][N];
int n,m;
inline int read() {
int x = 0,flag = 1;
char ch = getchar();
while(!isdigit(ch) && ch != '-')ch = getchar();
if(ch == '-')flag = -1,ch = getchar();
while(isdigit(ch))x = (x << 3) + (x << 1) + (ch - '0'),ch = getchar();
return x * flag;
}
inline bool in(int p,int l,int r) {
return p >= l && p <= r;
}
inline bool check(int l,int r) {
For(i,1,m) if(in(a[i],l,r) ^ in(b[i],l,r)) return false;
return true;
}
int main() {
n = read() << 1,m = read();
For(i,1,n) Sum[i] = 1;
val[0] = 1;
for(int i = 2;i <= n;i += 2) val[i] = 1ll * val[i - 2] * (i - 1) % mod;
For(i,1,m) {
a[i] = read(),b[i] = read();
Sum[a[i]] = Sum[b[i]] = 0;
}
For(i,1,n) Sum[i] += Sum[i - 1];
int ans = 0;
For(len,1,n) For(i,1,n - len + 1) {
int j = i + len - 1,tot = Sum[j] - Sum[i - 1];
if(!check(i,j) || (tot & 1)) continue;
int cur = val[tot];
For(k,i,j - 1) cur = (cur - 1ll * dp[i][k] * val[Sum[j] - Sum[k]]) % mod;
cur = (cur + mod) % mod;
ans = (ans + 1ll * (dp[i][j] = cur) * val[Sum[n] - tot]) % mod;
}
printf("%d\n",ans);
return 0;
}
``` |
#include<iostream>
#include<cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOF(i,a,b) for(int i=a;i< b;i++)
using namespace std;
const int N=660,P=1e9+7;
int n,K,m,ans,x,y;
int lk[N],w[N],g[N][N],f[N][N];
bool in(int x,int l,int r){return l<=x && x<=r || l<=x+m && x+m<=r;}
int main(){
//freopen("1.in","r",stdin);
scanf("%d%d",&n,&K);
m=n<<1;
FOF(i,0,m) lk[i]=-1;
FOR(i,1,K){
scanf("%d%d",&x,&y);
x--;y--;lk[x]=y;lk[y]=x;
}
w[0]=1;
FOR(i,1,n) w[i]=1ll*w[i-1]*(2*i-1)%P;
FOR(L,0,n)FOR(i,0,m){
int j=i+2*L-1,ok=1;
FOR(k,i,j)if(~lk[k%m]) ok&=in(lk[k%m],i,j),g[i][j]++;
g[i][j]=ok*w[L-g[i][j]/2];
}
FOR(L,1,n)FOR(i,0,m-2*L){
int j=i+2*L-1;f[i][j]=g[i][j];
for(int k=i+1;k<j;k+=2) (f[i][j]+=P-1ll*f[i][k]*g[k+1][j]%P)%=P;
(ans+=1ll*f[i][j]*g[j+1][i+m-1]%P)%=P;
}
cout<<ans<<'\n';
} | ### Prompt
Create a solution in CPP for the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOF(i,a,b) for(int i=a;i< b;i++)
using namespace std;
const int N=660,P=1e9+7;
int n,K,m,ans,x,y;
int lk[N],w[N],g[N][N],f[N][N];
bool in(int x,int l,int r){return l<=x && x<=r || l<=x+m && x+m<=r;}
int main(){
//freopen("1.in","r",stdin);
scanf("%d%d",&n,&K);
m=n<<1;
FOF(i,0,m) lk[i]=-1;
FOR(i,1,K){
scanf("%d%d",&x,&y);
x--;y--;lk[x]=y;lk[y]=x;
}
w[0]=1;
FOR(i,1,n) w[i]=1ll*w[i-1]*(2*i-1)%P;
FOR(L,0,n)FOR(i,0,m){
int j=i+2*L-1,ok=1;
FOR(k,i,j)if(~lk[k%m]) ok&=in(lk[k%m],i,j),g[i][j]++;
g[i][j]=ok*w[L-g[i][j]/2];
}
FOR(L,1,n)FOR(i,0,m-2*L){
int j=i+2*L-1;f[i][j]=g[i][j];
for(int k=i+1;k<j;k+=2) (f[i][j]+=P-1ll*f[i][k]*g[k+1][j]%P)%=P;
(ans+=1ll*f[i][j]*g[j+1][i+m-1]%P)%=P;
}
cout<<ans<<'\n';
}
``` |
// Version.10.32.47.06.04.20.
#include <bits/stdc++.h>
#define N 666
using namespace std;
const int mod = 1e9 + 7;
int n, m, cnt[N], p[N], ffac[N], f[N][N];
inline int num(int l, int r) { return cnt[r] - cnt[l - 1]; }
int main() {
cin >> n >> m;
int K = (n - m) << 1;
for (int i = 1; i <= n * 2; ++i)
p[i] = i, cnt[i] = 1;
for (int i = 1, u, v; i <= m; ++i) {
scanf("%d%d", &u, &v);
cnt[u] = cnt[v] = 0;
p[u] = v, p[v] = u;
} m = n << 1;
for (int i = ffac[0] = 1; i <= m; ++i) {
cnt[i] += cnt[i - 1];
if (i % 2 == 0)
ffac[i] = 1ll * ffac[i - 2] * (i - 1) % mod;
}
long long ans = 0;
for (int i = 1; i <= m; ++i)
for (int j = i + 1; j <= m; ++j) {
f[i][j] = 0;
int ok = 1, c = num(i, j);
for (int k = i; ok && k <= j; ++k)
if (p[k] < i || p[k] > j)
ok = 0;
if (!ok || !ffac[c]) continue;
f[i][j] = ffac[c];
for (int k = i + 1; k < j; ++k)
f[i][j] = (f[i][j] - 1ll * f[i][k] * ffac[num(k + 1, j)] % mod + mod) % mod;
// f[i][j] = 1ll * f[i][j] * ffac[K - c] % mod;
ans = (ans + 1ll * f[i][j] * ffac[K - c]) % mod;
// cout << i << " " << j << " " << f[i][j] << endl;
}
cout << ans << endl;
return 0;
} | ### Prompt
Generate a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
// Version.10.32.47.06.04.20.
#include <bits/stdc++.h>
#define N 666
using namespace std;
const int mod = 1e9 + 7;
int n, m, cnt[N], p[N], ffac[N], f[N][N];
inline int num(int l, int r) { return cnt[r] - cnt[l - 1]; }
int main() {
cin >> n >> m;
int K = (n - m) << 1;
for (int i = 1; i <= n * 2; ++i)
p[i] = i, cnt[i] = 1;
for (int i = 1, u, v; i <= m; ++i) {
scanf("%d%d", &u, &v);
cnt[u] = cnt[v] = 0;
p[u] = v, p[v] = u;
} m = n << 1;
for (int i = ffac[0] = 1; i <= m; ++i) {
cnt[i] += cnt[i - 1];
if (i % 2 == 0)
ffac[i] = 1ll * ffac[i - 2] * (i - 1) % mod;
}
long long ans = 0;
for (int i = 1; i <= m; ++i)
for (int j = i + 1; j <= m; ++j) {
f[i][j] = 0;
int ok = 1, c = num(i, j);
for (int k = i; ok && k <= j; ++k)
if (p[k] < i || p[k] > j)
ok = 0;
if (!ok || !ffac[c]) continue;
f[i][j] = ffac[c];
for (int k = i + 1; k < j; ++k)
f[i][j] = (f[i][j] - 1ll * f[i][k] * ffac[num(k + 1, j)] % mod + mod) % mod;
// f[i][j] = 1ll * f[i][j] * ffac[K - c] % mod;
ans = (ans + 1ll * f[i][j] * ffac[K - c]) % mod;
// cout << i << " " << j << " " << f[i][j] << endl;
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
typedef long long LL;
const int N = 605, mod = 1000000007;
int n, k, ans, dp[N][N], min[N], max[N], g[N], sum[N];
void down(int &x, int y) { x = std::min(x, y); }
void up(int &x, int y) { x = std::max(x, y); }
void reduce(int &x) { x += x >> 31 & mod; }
int dfs(int l, int r) {
if (~dp[l][r]) return dp[l][r];
int &ret = dp[l][r] = g[sum[r] - sum[l - 1]];
for (int p = l + 1; p < r; p += 2)
ret = (ret + (LL) (mod - dfs(l, p)) * g[sum[r] - sum[p]]) % mod;
return ret;
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::memset(dp, -1, sizeof dp);
std::cin >> n >> k, g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
g[i] = (LL) (i - 1) * g[i - 2] % mod;
for (int i = 1; i <= 2 * n; ++i) min[i] = 2 * n, max[i] = 1;
for (int i = 0; i < k; ++i) {
int x, y; std::cin >> x >> y, sum[x] = sum[y] = 1;
down(min[x], y), down(min[y], x), up(max[x], y), up(max[y], x);
}
for (int i = 1; i <= 2 * n; ++i) sum[i] = sum[i - 1] + !sum[i];
for (int i = 1; i <= 2 * n; ++i) {
int m = i, M = i;
for (int j = i; j <= 2 * n; ++j) {
m = std::min(m, min[j]), M = std::max(M, max[j]);
if (m < i || M > j) dp[i][j] = 0;
}
}
for (int i = 1; i <= 2 * n; ++i)
for (int j = i + 1; j <= 2 * n; j += 2)
ans = (ans + (LL) dfs(i, j) * g[sum[2 * n] - sum[j] + sum[i - 1]]) % mod;
std::cout << ans << '\n';
return 0;
} | ### Prompt
Please create a solution in CPP to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
typedef long long LL;
const int N = 605, mod = 1000000007;
int n, k, ans, dp[N][N], min[N], max[N], g[N], sum[N];
void down(int &x, int y) { x = std::min(x, y); }
void up(int &x, int y) { x = std::max(x, y); }
void reduce(int &x) { x += x >> 31 & mod; }
int dfs(int l, int r) {
if (~dp[l][r]) return dp[l][r];
int &ret = dp[l][r] = g[sum[r] - sum[l - 1]];
for (int p = l + 1; p < r; p += 2)
ret = (ret + (LL) (mod - dfs(l, p)) * g[sum[r] - sum[p]]) % mod;
return ret;
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::memset(dp, -1, sizeof dp);
std::cin >> n >> k, g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
g[i] = (LL) (i - 1) * g[i - 2] % mod;
for (int i = 1; i <= 2 * n; ++i) min[i] = 2 * n, max[i] = 1;
for (int i = 0; i < k; ++i) {
int x, y; std::cin >> x >> y, sum[x] = sum[y] = 1;
down(min[x], y), down(min[y], x), up(max[x], y), up(max[y], x);
}
for (int i = 1; i <= 2 * n; ++i) sum[i] = sum[i - 1] + !sum[i];
for (int i = 1; i <= 2 * n; ++i) {
int m = i, M = i;
for (int j = i; j <= 2 * n; ++j) {
m = std::min(m, min[j]), M = std::max(M, max[j]);
if (m < i || M > j) dp[i][j] = 0;
}
}
for (int i = 1; i <= 2 * n; ++i)
for (int j = i + 1; j <= 2 * n; j += 2)
ans = (ans + (LL) dfs(i, j) * g[sum[2 * n] - sum[j] + sum[i - 1]]) % mod;
std::cout << ans << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
a = (a+b) % MOD;
}
int N, K;
int A[300], B[300];
int64_t dp[600][600];
int freedom[600][600];
int64_t g[601];
int main(){
int i, j, k;
cin >> N >> K;
for(i=0; i<K; i++){
int a, b;
cin >> a >> b;
if(a>b) swap(a, b);
A[i] = a-1; B[i] = b-1;
}
g[0] = 1;
for(i=2; i<=2*N; i++) g[i] = g[i-2] * (i-1) % MOD;
int64_t ans = 0;
for(int d=2; d<=2*N; d+=2){
for(int l=0; l+d-1<2*N; l++){
int r = l+d-1;
int inner = 0;
bool ok = true;
for(int k=0; k<K; k++){
if(l <= A[k] && B[k] <= r){
inner++;
}else if((l <= A[k] && A[k] <= r) || (l <= B[k] && B[k] <= r)){
ok = false;
break;
}
}
if(!ok) continue;
freedom[l][r] = d - 2*inner;
dp[l][r] = g[freedom[l][r]];
for(int r2=l+1; r2<r; r2+=2){
int64_t sub = dp[l][r2] * g[freedom[r2+1][r]] % MOD;
add(dp[l][r], MOD - sub);
}
int outer_freedom = 2*N-d - 2*(K-inner);
add(ans, dp[l][r] * g[outer_freedom] % MOD);
}
}
cout << ans << endl;
return 0;
} | ### Prompt
In Cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
a = (a+b) % MOD;
}
int N, K;
int A[300], B[300];
int64_t dp[600][600];
int freedom[600][600];
int64_t g[601];
int main(){
int i, j, k;
cin >> N >> K;
for(i=0; i<K; i++){
int a, b;
cin >> a >> b;
if(a>b) swap(a, b);
A[i] = a-1; B[i] = b-1;
}
g[0] = 1;
for(i=2; i<=2*N; i++) g[i] = g[i-2] * (i-1) % MOD;
int64_t ans = 0;
for(int d=2; d<=2*N; d+=2){
for(int l=0; l+d-1<2*N; l++){
int r = l+d-1;
int inner = 0;
bool ok = true;
for(int k=0; k<K; k++){
if(l <= A[k] && B[k] <= r){
inner++;
}else if((l <= A[k] && A[k] <= r) || (l <= B[k] && B[k] <= r)){
ok = false;
break;
}
}
if(!ok) continue;
freedom[l][r] = d - 2*inner;
dp[l][r] = g[freedom[l][r]];
for(int r2=l+1; r2<r; r2+=2){
int64_t sub = dp[l][r2] * g[freedom[r2+1][r]] % MOD;
add(dp[l][r], MOD - sub);
}
int outer_freedom = 2*N-d - 2*(K-inner);
add(ans, dp[l][r] * g[outer_freedom] % MOD);
}
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 610;
const int Mod = 1e9 + 7;
int add(int a, int b) {
return (a += b) >= Mod ? a - Mod : a;
}
int sub(int a, int b) {
return (a -= b) < 0 ? a + Mod : a;
}
int mul(int a, int b) {
return 1ll * a * b % Mod;
}
int n, k, a[N], b[N], f[N][N], g[N][N], h[N];
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= k; i++) {
scanf("%d %d", &a[i], &b[i]);
}
h[0] = 1;
for (int i = 1; i <= n; i++) {
h[i] = mul(h[i - 1], i * 2 - 1);
}
int ans = 0;
for (int len = 2; len <= 2 * n; len += 2) {
for (int l = 1; l + len - 1 <= 2 * n; l++) {
int r = l + len - 1, cnt = 0;
bool flag = 1;
for (int i = 1; i <= k; i++) {
bool ina = l <= a[i] && a[i] <= r;
bool inb = l <= b[i] && b[i] <= r;
if (ina && inb) cnt++;
else if (ina || inb) flag = 0;
}
if (!flag) continue;
cnt = len / 2 - cnt;
f[l][r] = g[l][r] = h[cnt];
for (int i = l + 1; i < r - 1; i += 2) {
f[l][r] = sub(f[l][r], mul(f[l][i], g[i + 1][r]));
}
ans = add(ans, mul(f[l][r], h[n - k - cnt]));
}
}
cout << ans << endl;
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 610;
const int Mod = 1e9 + 7;
int add(int a, int b) {
return (a += b) >= Mod ? a - Mod : a;
}
int sub(int a, int b) {
return (a -= b) < 0 ? a + Mod : a;
}
int mul(int a, int b) {
return 1ll * a * b % Mod;
}
int n, k, a[N], b[N], f[N][N], g[N][N], h[N];
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= k; i++) {
scanf("%d %d", &a[i], &b[i]);
}
h[0] = 1;
for (int i = 1; i <= n; i++) {
h[i] = mul(h[i - 1], i * 2 - 1);
}
int ans = 0;
for (int len = 2; len <= 2 * n; len += 2) {
for (int l = 1; l + len - 1 <= 2 * n; l++) {
int r = l + len - 1, cnt = 0;
bool flag = 1;
for (int i = 1; i <= k; i++) {
bool ina = l <= a[i] && a[i] <= r;
bool inb = l <= b[i] && b[i] <= r;
if (ina && inb) cnt++;
else if (ina || inb) flag = 0;
}
if (!flag) continue;
cnt = len / 2 - cnt;
f[l][r] = g[l][r] = h[cnt];
for (int i = l + 1; i < r - 1; i += 2) {
f[l][r] = sub(f[l][r], mul(f[l][i], g[i + 1][r]));
}
ans = add(ans, mul(f[l][r], h[n - k - cnt]));
}
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600, mod = 1e9 + 7;
int n, m, s[maxn + 10][maxn + 10];
int a[maxn + 10], b[maxn + 10];
int f[maxn + 10][maxn + 10], g[maxn + 10];
int ans;
bool in(int p, int l, int r) {
if (l <= r) return p >= l && p <= r;
else return p >= l || p <= r;
}
int rlink(int l, int r) {
if (s[l][r] == -1) return 0;
else return g[s[l][r]];
}
int main() {
scanf("%d%d", &n, &m); n *= 2;
for (int i = 1; i <= m; ++i) scanf("%d%d", &a[i], &b[i]);
g[0] = 1;
for (int i = 2; i <= n; i += 2) g[i] = 1ll * g[i - 2] * (i - 1) % mod;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= n; ++k)
if (in(k, i, j)) ++s[i][j];
for (int k = 1; k <= m; ++k) {
bool d1 = in(a[k], i, j), d2 = in(b[k], i, j);
if (d1 ^ d2) {
s[i][j] = -1; break;
}
if (d1 && d2) s[i][j] -= 2;
}
}
for (int l = 0; l < n; ++l)
for (int i = 1; i <= n; ++i) {
int j = i + l;
if (j > n) j -= n;
f[i][j] = rlink(i, j);
for (int k = i; k != j; k = k == n ? 1 : k + 1) {
f[i][j] -= 1ll * f[i][k] * rlink(k == n ? 1 : k + 1, j) % mod - mod;
f[i][j] %= mod;
}
if (l < n - 1) {
if (i <= j)
(ans += 1ll * f[i][j] * rlink(j == n ? 1 : j + 1, i == 1 ? n : i - 1) % mod) %= mod;
}
else if (i == 1)
(ans += f[i][j]) %= mod;
}
printf("%d", ans);
}
| ### Prompt
In Cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600, mod = 1e9 + 7;
int n, m, s[maxn + 10][maxn + 10];
int a[maxn + 10], b[maxn + 10];
int f[maxn + 10][maxn + 10], g[maxn + 10];
int ans;
bool in(int p, int l, int r) {
if (l <= r) return p >= l && p <= r;
else return p >= l || p <= r;
}
int rlink(int l, int r) {
if (s[l][r] == -1) return 0;
else return g[s[l][r]];
}
int main() {
scanf("%d%d", &n, &m); n *= 2;
for (int i = 1; i <= m; ++i) scanf("%d%d", &a[i], &b[i]);
g[0] = 1;
for (int i = 2; i <= n; i += 2) g[i] = 1ll * g[i - 2] * (i - 1) % mod;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= n; ++k)
if (in(k, i, j)) ++s[i][j];
for (int k = 1; k <= m; ++k) {
bool d1 = in(a[k], i, j), d2 = in(b[k], i, j);
if (d1 ^ d2) {
s[i][j] = -1; break;
}
if (d1 && d2) s[i][j] -= 2;
}
}
for (int l = 0; l < n; ++l)
for (int i = 1; i <= n; ++i) {
int j = i + l;
if (j > n) j -= n;
f[i][j] = rlink(i, j);
for (int k = i; k != j; k = k == n ? 1 : k + 1) {
f[i][j] -= 1ll * f[i][k] * rlink(k == n ? 1 : k + 1, j) % mod - mod;
f[i][j] %= mod;
}
if (l < n - 1) {
if (i <= j)
(ans += 1ll * f[i][j] * rlink(j == n ? 1 : j + 1, i == 1 ? n : i - 1) % mod) %= mod;
}
else if (i == 1)
(ans += f[i][j]) %= mod;
}
printf("%d", ans);
}
``` |
#include <bits/stdc++.h>
#define MOD 1000000007
long long dp[605][605], fact[605], ans;
int x[605], y[605], pre[605];
int main()
{
// freopen("AGC028-D.in", "r", stdin);
int n, k;
scanf("%d%d", &n, &k);
n <<= 1;
fact[0] = 1;
for (int i = 2; i <= n; i++) {
fact[i] = fact[i - 2] * (i - 1) % MOD;
}
for (int i = 0; i < k; i++) {
scanf("%d%d", x + i, y + i);
pre[--x[i]] = pre[--y[i]] = 1;
}
for (int i = 0; i < n; i++) {
pre[i] = !pre[i] + (i ? pre[i - 1] : 0);
}
for (int l = 0; l < n; l++) {
for (int r = l + 2; r <= n; r += 2) {
int cnt = pre[r - 1] - (l ? pre[l - 1] : 0);
if (cnt & 1) {
continue;
}
bool flg = true;
for (int i = 0; i < k; i++) {
flg &= (l <= x[i] && x[i] < r) == (l <= y[i] && y[i] < r);
}
if (!flg) {
continue;
}
dp[l][r] = fact[cnt];
for (int i = l + 2; i < r; i += 2) {
(dp[l][r] -= dp[l][i] * fact[pre[r - 1] - pre[i - 1]]) %= MOD;
}
(ans += dp[l][r] * fact[n - cnt - (k << 1)]) %= MOD;
}
}
printf("%lld\n", (ans + MOD) % MOD);
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
#define MOD 1000000007
long long dp[605][605], fact[605], ans;
int x[605], y[605], pre[605];
int main()
{
// freopen("AGC028-D.in", "r", stdin);
int n, k;
scanf("%d%d", &n, &k);
n <<= 1;
fact[0] = 1;
for (int i = 2; i <= n; i++) {
fact[i] = fact[i - 2] * (i - 1) % MOD;
}
for (int i = 0; i < k; i++) {
scanf("%d%d", x + i, y + i);
pre[--x[i]] = pre[--y[i]] = 1;
}
for (int i = 0; i < n; i++) {
pre[i] = !pre[i] + (i ? pre[i - 1] : 0);
}
for (int l = 0; l < n; l++) {
for (int r = l + 2; r <= n; r += 2) {
int cnt = pre[r - 1] - (l ? pre[l - 1] : 0);
if (cnt & 1) {
continue;
}
bool flg = true;
for (int i = 0; i < k; i++) {
flg &= (l <= x[i] && x[i] < r) == (l <= y[i] && y[i] < r);
}
if (!flg) {
continue;
}
dp[l][r] = fact[cnt];
for (int i = l + 2; i < r; i += 2) {
(dp[l][r] -= dp[l][i] * fact[pre[r - 1] - pre[i - 1]]) %= MOD;
}
(ans += dp[l][r] * fact[n - cnt - (k << 1)]) %= MOD;
}
}
printf("%lld\n", (ans + MOD) % MOD);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int64_t M = 1000000007;
const int MAX_N = 300;
int n, k;
int p[2*MAX_N+1];
int64_t dp[2*MAX_N+1][2*MAX_N+1];
int psum[2*MAX_N+1];
int64_t gpre[2*MAX_N];
int64_t g(int i, int j)
{
int n = psum[j] - psum[i-1];
if (i == j || n % 2 == 1) return 0;
if (n == 0) return 1;
return gpre[n-1];
}
int main()
{
cin >> n >> k;
fill_n(p, 2*n+1, 0);
int a, b;
for (int i = 0; i < k; i++) {
cin >> a >> b;
p[a] = b;
p[b] = a;
}
psum[0] = 0;
for (int i = 1; i <= 2*n; i++) {
psum[i] = psum[i-1] + (p[i] == 0 ? 1 : 0);
}
fill_n(gpre, 2*n, 0);
gpre[1] = 1;
for (int i = 3; i < 2*n; i += 2) {
gpre[i] = gpre[i-2] * i % M;
}
for (int i = 0; i <= 2*n; i++)
for (int j = 0; j <= 2*n; j++)
dp[i][j] = 0;
for (int i = 1; i <= 2*n; i++) {
int max_out = p[i];
if (p[i] > 0 && p[i] < i) continue;
for (int j = i+1; j <= 2*n; j++) {
if (p[j] > 0 && p[j] < i) break;
max_out = max(max_out, p[j]);
if (max_out <= j) {
dp[i][j] = g(i,j);
if (dp[i][j] != 0) {
for (int k = i+1; k < j; k++) {
dp[i][j] = (dp[i][j] - (dp[i][k] * g(k+1, j) % M) + M) % M;
}
}
}
}
}
int64_t r = 0;
for (int i = 1; i <= 2*n; i++) {
for (int j = i+1; j <= 2*n; j++) {
int zeros = 2*n - 2*k - (psum[j] - psum[i-1]);
r = (r + dp[i][j] * (zeros == 0 ? 1LL : gpre[zeros - 1]) % M) % M;
}
}
cout << r << endl;
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int64_t M = 1000000007;
const int MAX_N = 300;
int n, k;
int p[2*MAX_N+1];
int64_t dp[2*MAX_N+1][2*MAX_N+1];
int psum[2*MAX_N+1];
int64_t gpre[2*MAX_N];
int64_t g(int i, int j)
{
int n = psum[j] - psum[i-1];
if (i == j || n % 2 == 1) return 0;
if (n == 0) return 1;
return gpre[n-1];
}
int main()
{
cin >> n >> k;
fill_n(p, 2*n+1, 0);
int a, b;
for (int i = 0; i < k; i++) {
cin >> a >> b;
p[a] = b;
p[b] = a;
}
psum[0] = 0;
for (int i = 1; i <= 2*n; i++) {
psum[i] = psum[i-1] + (p[i] == 0 ? 1 : 0);
}
fill_n(gpre, 2*n, 0);
gpre[1] = 1;
for (int i = 3; i < 2*n; i += 2) {
gpre[i] = gpre[i-2] * i % M;
}
for (int i = 0; i <= 2*n; i++)
for (int j = 0; j <= 2*n; j++)
dp[i][j] = 0;
for (int i = 1; i <= 2*n; i++) {
int max_out = p[i];
if (p[i] > 0 && p[i] < i) continue;
for (int j = i+1; j <= 2*n; j++) {
if (p[j] > 0 && p[j] < i) break;
max_out = max(max_out, p[j]);
if (max_out <= j) {
dp[i][j] = g(i,j);
if (dp[i][j] != 0) {
for (int k = i+1; k < j; k++) {
dp[i][j] = (dp[i][j] - (dp[i][k] * g(k+1, j) % M) + M) % M;
}
}
}
}
}
int64_t r = 0;
for (int i = 1; i <= 2*n; i++) {
for (int j = i+1; j <= 2*n; j++) {
int zeros = 2*n - 2*k - (psum[j] - psum[i-1]);
r = (r + dp[i][j] * (zeros == 0 ? 1LL : gpre[zeros - 1]) % M) % M;
}
}
cout << r << endl;
return 0;
}
``` |
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
#define maxn 610
inline ll read()
{
ll x=0; char c=getchar(),f=1;
for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
return x*f;
}
inline void write(ll x)
{
static char buf[20];
int len=0;
if(x<0)putchar('-'),x=-x;
for(;x;x/=10)buf[len++]=x%10+'0';
if(!len)putchar('0');
else while(len)putchar(buf[--len]);
}
inline void writesp(ll x){write(x); putchar(' ');}
inline void writeln(ll x){write(x); putchar('\n');}
ll f[maxn][maxn],g[maxn];
int id[maxn],sum[maxn];
int n,m;
int main()
{
n=read(); m=read();
for(int i=1;i<=m;i++){
int x=read(),y=read();
id[x]=y; id[y]=x;
}
for(int i=1;i<=2*n;i++)
sum[i]=sum[i-1]+(!id[i]);
g[0]=1;
for(int i=2;i<=2*n;i+=2)
g[i]=g[i-2]*(i-1)%mod;
for(int i=2;i<=2*n;i+=2)
for(int l=1,r=i;r<=2*n;l++,r++){
int flag=0;
for(int k=l;k<=r;k++)
if(id[k]&&(id[k]<l||id[k]>r)){
flag=1; break;
}
if(flag)continue;
f[l][r]=g[sum[r]-sum[l-1]];
for(int k=l;k<r;k++)
f[l][r]=(f[l][r]-f[l][k]*g[sum[r]-sum[k]])%mod;
f[l][r]=(f[l][r]%mod+mod)%mod;
}
ll ans=0;
for(int l=1;l<=2*n;l++)
for(int r=l;r<=2*n;r++)
ans=(ans+f[l][r]*g[sum[2*n]-sum[r]+sum[l-1]])%mod;
writeln(ans%mod);
return 0;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
#define maxn 610
inline ll read()
{
ll x=0; char c=getchar(),f=1;
for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
return x*f;
}
inline void write(ll x)
{
static char buf[20];
int len=0;
if(x<0)putchar('-'),x=-x;
for(;x;x/=10)buf[len++]=x%10+'0';
if(!len)putchar('0');
else while(len)putchar(buf[--len]);
}
inline void writesp(ll x){write(x); putchar(' ');}
inline void writeln(ll x){write(x); putchar('\n');}
ll f[maxn][maxn],g[maxn];
int id[maxn],sum[maxn];
int n,m;
int main()
{
n=read(); m=read();
for(int i=1;i<=m;i++){
int x=read(),y=read();
id[x]=y; id[y]=x;
}
for(int i=1;i<=2*n;i++)
sum[i]=sum[i-1]+(!id[i]);
g[0]=1;
for(int i=2;i<=2*n;i+=2)
g[i]=g[i-2]*(i-1)%mod;
for(int i=2;i<=2*n;i+=2)
for(int l=1,r=i;r<=2*n;l++,r++){
int flag=0;
for(int k=l;k<=r;k++)
if(id[k]&&(id[k]<l||id[k]>r)){
flag=1; break;
}
if(flag)continue;
f[l][r]=g[sum[r]-sum[l-1]];
for(int k=l;k<r;k++)
f[l][r]=(f[l][r]-f[l][k]*g[sum[r]-sum[k]])%mod;
f[l][r]=(f[l][r]%mod+mod)%mod;
}
ll ans=0;
for(int l=1;l<=2*n;l++)
for(int r=l;r<=2*n;r++)
ans=(ans+f[l][r]*g[sum[2*n]-sum[r]+sum[l-1]])%mod;
writeln(ans%mod);
return 0;
}
``` |
#include <bits/stdc++.h>
typedef long long ll;
const int N = 648, mod = 1000000007;
int n;
int a[N], s[N];
int dfact[N], min[N][N], max[N][N], f[N][N];
inline void up(int &x, const int y) {x < y ? x = y : 0;}
inline void down(int &x, const int y) {x > y ? x = y : 0;}
inline int sum(int l, int r) {return s[r] - s[l - 1];}
int main() {
int i, j, k, u, v, ans = 0;
scanf("%d%d", &n, &k), n *= 2;
for (*dfact = i = 1; i < n; i += 2) dfact[i + 1] = dfact[i] = (ll)dfact[i - 1] * i % mod;
for (; k; --k) scanf("%d%d", &u, &v), a[u] = v, a[v] = u;
for (i = 1; i <= n; ++i) {
s[i] = s[i - 1] + !a[i], min[i][i - 1] = INT_MAX, max[i][i - 1] = INT_MIN;
for (j = i; j <= n; ++j) {
min[i][j] = min[i][j - 1], max[i][j] = max[i][j - 1];
if (a[j]) down(min[i][j], a[j]), up(max[i][j], a[j]);
}
}
for (i = 1; i <= n; ++i)
for (j = i + 1; j <= n; j += 2) {
if ((u = sum(i, j)) & 1 || min[i][j] < i || max[i][j] > j) continue;
int &ret = f[i][j]; ret = dfact[u];
for (k = i + 2; k < j; k += 2) if (!(sum(k, j) & 1))
ret = (ret - (ll)f[i][k - 1] * dfact[sum(k, j)]) % mod;
ans = (ans + (ll)ret * dfact[s[n] - u]) % mod;
}
printf("%d\n", ans + (ans >> 31 & mod));
return 0;
} | ### Prompt
Please formulate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
typedef long long ll;
const int N = 648, mod = 1000000007;
int n;
int a[N], s[N];
int dfact[N], min[N][N], max[N][N], f[N][N];
inline void up(int &x, const int y) {x < y ? x = y : 0;}
inline void down(int &x, const int y) {x > y ? x = y : 0;}
inline int sum(int l, int r) {return s[r] - s[l - 1];}
int main() {
int i, j, k, u, v, ans = 0;
scanf("%d%d", &n, &k), n *= 2;
for (*dfact = i = 1; i < n; i += 2) dfact[i + 1] = dfact[i] = (ll)dfact[i - 1] * i % mod;
for (; k; --k) scanf("%d%d", &u, &v), a[u] = v, a[v] = u;
for (i = 1; i <= n; ++i) {
s[i] = s[i - 1] + !a[i], min[i][i - 1] = INT_MAX, max[i][i - 1] = INT_MIN;
for (j = i; j <= n; ++j) {
min[i][j] = min[i][j - 1], max[i][j] = max[i][j - 1];
if (a[j]) down(min[i][j], a[j]), up(max[i][j], a[j]);
}
}
for (i = 1; i <= n; ++i)
for (j = i + 1; j <= n; j += 2) {
if ((u = sum(i, j)) & 1 || min[i][j] < i || max[i][j] > j) continue;
int &ret = f[i][j]; ret = dfact[u];
for (k = i + 2; k < j; k += 2) if (!(sum(k, j) & 1))
ret = (ret - (ll)f[i][k - 1] * dfact[sum(k, j)]) % mod;
ans = (ans + (ll)ret * dfact[s[n] - u]) % mod;
}
printf("%d\n", ans + (ans >> 31 & mod));
return 0;
}
``` |
#include<iostream>
#include<cstdio>
#define p 1000000007
using namespace std;
int n,K,ans,to[1010],f[1010][1010],g[1010],num[1010][1010];
bool check(int x,int y)
{
if((x^y^1)&1) return 0;
for(int i=x;i<=y;i++)
if(to[i]&&(to[i]<x||to[i]>y)) return 0;
return 1;
}
int main()
{
scanf("%d%d",&n,&K);
for(int i=1,x,y;i<=K;i++)
scanf("%d%d",&x,&y),to[x]=y,to[y]=x;
int sum=(n-K)<<1;
n<<=1;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
num[i][j]=num[i][j-1]+(!to[j]);
g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=1LL*g[i-2]*(i-1)%p;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
if(check(i,j))
{
f[i][j]=g[num[i][j]];
for(int k=i+1;k<j;k++)
if(f[i][k]&&g[num[k+1][j]])
(f[i][j]+=p-1LL*f[i][k]*g[num[k+1][j]]%p)%=p;
ans=(ans+1LL*f[i][j]*g[sum-num[i][j]]%p)%p;
}
printf("%d\n",ans);
return 0;
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstdio>
#define p 1000000007
using namespace std;
int n,K,ans,to[1010],f[1010][1010],g[1010],num[1010][1010];
bool check(int x,int y)
{
if((x^y^1)&1) return 0;
for(int i=x;i<=y;i++)
if(to[i]&&(to[i]<x||to[i]>y)) return 0;
return 1;
}
int main()
{
scanf("%d%d",&n,&K);
for(int i=1,x,y;i<=K;i++)
scanf("%d%d",&x,&y),to[x]=y,to[y]=x;
int sum=(n-K)<<1;
n<<=1;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
num[i][j]=num[i][j-1]+(!to[j]);
g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=1LL*g[i-2]*(i-1)%p;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
if(check(i,j))
{
f[i][j]=g[num[i][j]];
for(int k=i+1;k<j;k++)
if(f[i][k]&&g[num[k+1][j]])
(f[i][j]+=p-1LL*f[i][k]*g[num[k+1][j]]%p)%=p;
ans=(ans+1LL*f[i][j]*g[sum-num[i][j]]%p)%p;
}
printf("%d\n",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
typedef long long int int64;
static const int64 mod = 1000000007LL;
int pair[603];
int cnt[603];
int64 g[603];
int64 dp[603][603];
int n,m;
int main()
{
scanf("%d%d",&n,&m);n *= 2;
for(int i = 0;i < n;++i) {
pair[i] = -1;
}
for(int i = 0;i < m;++i) {
int a,b;
scanf("%d%d",&a,&b);--a,--b;
pair[a] = b;pair[b] = a;
}
g[0] = 1;
for(int i = 2;i <= n;i += 2) {
g[i] = g[i-2] * (i-1) % mod;
}
for(int i = 0;i < n;++i) {
cnt[i] = (i == 0 ? 0 : cnt[i-1]) + (pair[i] == -1 ? 1 : 0);
}
int64 ans = 0;
for(int i = 0;i < n;++i) {
int minpair = n;
int maxpair = -1;
for(int j = i;j < n;++j) {
if(pair[j] != -1 && pair[j] < minpair) minpair = pair[j];
if(pair[j] != -1 && pair[j] > maxpair) maxpair = pair[j];
if(minpair < i) break;
if(maxpair > j) continue;
int u = cnt[j]-(i==0?0:cnt[i-1]);
int64 val = g[u];
for(int k = i;k < j;++k) {
int u2 = cnt[j]-cnt[k];
val -= dp[i][k] * g[u2] % mod;
}
val %= mod;
if(val < 0) val += mod;
dp[i][j] = val;
ans += val * g[cnt[n-1]-u] % mod;
}
}
printf("%lld\n", ans % mod);
}
| ### Prompt
Generate a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
typedef long long int int64;
static const int64 mod = 1000000007LL;
int pair[603];
int cnt[603];
int64 g[603];
int64 dp[603][603];
int n,m;
int main()
{
scanf("%d%d",&n,&m);n *= 2;
for(int i = 0;i < n;++i) {
pair[i] = -1;
}
for(int i = 0;i < m;++i) {
int a,b;
scanf("%d%d",&a,&b);--a,--b;
pair[a] = b;pair[b] = a;
}
g[0] = 1;
for(int i = 2;i <= n;i += 2) {
g[i] = g[i-2] * (i-1) % mod;
}
for(int i = 0;i < n;++i) {
cnt[i] = (i == 0 ? 0 : cnt[i-1]) + (pair[i] == -1 ? 1 : 0);
}
int64 ans = 0;
for(int i = 0;i < n;++i) {
int minpair = n;
int maxpair = -1;
for(int j = i;j < n;++j) {
if(pair[j] != -1 && pair[j] < minpair) minpair = pair[j];
if(pair[j] != -1 && pair[j] > maxpair) maxpair = pair[j];
if(minpair < i) break;
if(maxpair > j) continue;
int u = cnt[j]-(i==0?0:cnt[i-1]);
int64 val = g[u];
for(int k = i;k < j;++k) {
int u2 = cnt[j]-cnt[k];
val -= dp[i][k] * g[u2] % mod;
}
val %= mod;
if(val < 0) val += mod;
dp[i][j] = val;
ans += val * g[cnt[n-1]-u] % mod;
}
}
printf("%lld\n", ans % mod);
}
``` |
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=1000000007;
int a[804],b[804],g[804],s[804][804],ss[804][804],f[804][804];
int main()
{
int n,k,i,j,kk,ans=0;
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d",&n,&kk);
n*=2;
for(i=1;i<=kk;i++)
{
scanf("%d%d",&a[i],&b[i]);
if(a[i]>b[i])
swap(a[i],b[i]);
}
g[0]=1;
for(i=2;i<=n;i+=2)
g[i]=(long long)g[i-2]*(i-1)%INF;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
int sum1=0,sum2=0,flag=0;
for(k=1;k<=kk;k++)
{
if(a[k]>=i&&b[k]<=j)
sum1++;
else if(b[k]<i||a[k]>j||a[k]<i&&b[k]>j)
sum2++;
else
flag=1;
}
if(!flag)
{
s[i][j]=g[j-i+1-sum1-sum1];
ss[i][j]=g[n-(j-i+1)-sum2-sum2];
}
else
{
s[i][j]=-1;
ss[i][j]=-1;
}
}
for(j=1;j<=n;j+=2)
{
for(i=1;i+j<=n;i++)
{
if(ss[i][i+j]<0)
continue;
f[i][i+j]=s[i][i+j];
for(k=i+1;k<=i+j;k+=2)
f[i][i+j]=(f[i][i+j]-(long long)f[i][k]*s[k+1][i+j]%INF+INF)%INF;
}
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
ans=(ans+(long long)f[i][j]*ss[i][j]%INF)%INF;
printf("%d\n",ans);
return 0;
}
| ### Prompt
Create a solution in cpp for the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=1000000007;
int a[804],b[804],g[804],s[804][804],ss[804][804],f[804][804];
int main()
{
int n,k,i,j,kk,ans=0;
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d",&n,&kk);
n*=2;
for(i=1;i<=kk;i++)
{
scanf("%d%d",&a[i],&b[i]);
if(a[i]>b[i])
swap(a[i],b[i]);
}
g[0]=1;
for(i=2;i<=n;i+=2)
g[i]=(long long)g[i-2]*(i-1)%INF;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
int sum1=0,sum2=0,flag=0;
for(k=1;k<=kk;k++)
{
if(a[k]>=i&&b[k]<=j)
sum1++;
else if(b[k]<i||a[k]>j||a[k]<i&&b[k]>j)
sum2++;
else
flag=1;
}
if(!flag)
{
s[i][j]=g[j-i+1-sum1-sum1];
ss[i][j]=g[n-(j-i+1)-sum2-sum2];
}
else
{
s[i][j]=-1;
ss[i][j]=-1;
}
}
for(j=1;j<=n;j+=2)
{
for(i=1;i+j<=n;i++)
{
if(ss[i][i+j]<0)
continue;
f[i][i+j]=s[i][i+j];
for(k=i+1;k<=i+j;k+=2)
f[i][i+j]=(f[i][i+j]-(long long)f[i][k]*s[k+1][i+j]%INF+INF)%INF;
}
}
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
ans=(ans+(long long)f[i][j]*ss[i][j]%INF)%INF;
printf("%d\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 605;
const int P = 1e9 + 7;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int s[MAXN], home[MAXN], dp[MAXN][MAXN];
int n, m, p[MAXN], fac[MAXN];
void update(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
int main() {
read(n), read(m), fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * (2ll * i - 1) % P;
for (int i = 1; i <= m; i++) {
int x, y; read(x), read(y);
home[x] = home[y] = i;
}
int ans = 0;
for (int i = 1; i <= n * 2; i++) {
static bool col[MAXN]; int cnt = 0, empty = 0;
memset(col, false, sizeof(col));
for (int j = i; j <= n * 2; j++) {
if (home[j]) {
cnt -= col[home[j]];
col[home[j]] ^= true;
cnt += col[home[j]];
} else empty += 1;
if (cnt == 0 && empty % 2 == 0) {
dp[i][j] = fac[empty / 2];
for (int k = i, cmt = 0; k <= j - 1; k++) {
cmt += !home[k];
if (cmt % 2 == 0) update(dp[i][j], P - 1ll * dp[i][k] * fac[empty / 2 - cmt / 2] % P);
}
update(ans, 1ll * dp[i][j] * fac[n - m - empty / 2] % P);
}
}
}
cout << ans << endl;
return 0;
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 605;
const int P = 1e9 + 7;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int s[MAXN], home[MAXN], dp[MAXN][MAXN];
int n, m, p[MAXN], fac[MAXN];
void update(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
int main() {
read(n), read(m), fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * (2ll * i - 1) % P;
for (int i = 1; i <= m; i++) {
int x, y; read(x), read(y);
home[x] = home[y] = i;
}
int ans = 0;
for (int i = 1; i <= n * 2; i++) {
static bool col[MAXN]; int cnt = 0, empty = 0;
memset(col, false, sizeof(col));
for (int j = i; j <= n * 2; j++) {
if (home[j]) {
cnt -= col[home[j]];
col[home[j]] ^= true;
cnt += col[home[j]];
} else empty += 1;
if (cnt == 0 && empty % 2 == 0) {
dp[i][j] = fac[empty / 2];
for (int k = i, cmt = 0; k <= j - 1; k++) {
cmt += !home[k];
if (cmt % 2 == 0) update(dp[i][j], P - 1ll * dp[i][k] * fac[empty / 2 - cmt / 2] % P);
}
update(ans, 1ll * dp[i][j] * fac[n - m - empty / 2] % P);
}
}
}
cout << ans << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
#define MAX_N 600
#define P 1000000007
using namespace std;
typedef long long lnt;
template <class T> inline void read(T &x) {
x = 0; int c = getchar(), f = 1;
for (; !isdigit(c); c = getchar()) if (c == 45) f = -1;
for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0');
}
int n, m; lnt ans;
int p[MAX_N+5], c[MAX_N+5][MAX_N+5];
lnt f[MAX_N+5][MAX_N+5], g[MAX_N+5];
void DP(int l, int r) {
for (int i = l; i <= r; i++)
if (p[i] && (p[i] < l || p[i] > r)) return;
int x = c[l][r], y = n-(m<<1)-x; f[l][r] = g[x]%P;
for (int i = l+1, z; i <= r-1; i++)
z = c[i+1][r], (f[l][r] += P-f[l][i]*g[z]%P) %= P;
(ans += f[l][r]*g[y]%P) %= P;
}
int main() {
read(n), read(m), n <<= 1, g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = g[i-2]*(i-1)%P;
for (int i = 1, u, v; i <= m; i++)
read(u), read(v), p[p[u] = v] = u;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
c[i][j] = c[i][j-1]+!p[j];
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) DP(i, j);
return printf("%lld\n", ans), 0;
} | ### Prompt
Generate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
#define MAX_N 600
#define P 1000000007
using namespace std;
typedef long long lnt;
template <class T> inline void read(T &x) {
x = 0; int c = getchar(), f = 1;
for (; !isdigit(c); c = getchar()) if (c == 45) f = -1;
for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0');
}
int n, m; lnt ans;
int p[MAX_N+5], c[MAX_N+5][MAX_N+5];
lnt f[MAX_N+5][MAX_N+5], g[MAX_N+5];
void DP(int l, int r) {
for (int i = l; i <= r; i++)
if (p[i] && (p[i] < l || p[i] > r)) return;
int x = c[l][r], y = n-(m<<1)-x; f[l][r] = g[x]%P;
for (int i = l+1, z; i <= r-1; i++)
z = c[i+1][r], (f[l][r] += P-f[l][i]*g[z]%P) %= P;
(ans += f[l][r]*g[y]%P) %= P;
}
int main() {
read(n), read(m), n <<= 1, g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = g[i-2]*(i-1)%P;
for (int i = 1, u, v; i <= m; i++)
read(u), read(v), p[p[u] = v] = u;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
c[i][j] = c[i][j-1]+!p[j];
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) DP(i, j);
return printf("%lld\n", ans), 0;
}
``` |
#include<bits/stdc++.h>
#define cmin(a,b) (a>(b)?a=(b),1:0)
#define cmax(a,b) (a<(b)?a=(b),1:0)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define dmax(a,b) ((a)>(b)?(a):(b))
namespace io
{
int F()
{
int F=1,n=0;
char ch;
while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
ch=='-'?F=0:n=ch-'0';
while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
return F?n:-n;
}
long long G()
{
long long F=1,n=0;
char ch;
while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
ch=='-'?F=0:n=ch-'0';
while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
return F?n:-n;
}
}
int R(int l,int r)
{
return (rand()<<15|rand())%(r-l+1)+l;
}
int a[333],b[333];
int f[666][666];
int vis[666];
int g[666];
int sum[666];
const int M=1000000007;
int main()
{
int n=io::F(),k=io::F();
for(register int i=1;i<=k;++i)a[i]=io::F(),b[i]=io::F(),vis[a[i]]=vis[b[i]]=1;
g[0]=1;
for(register int i=2;i<=n+n;i+=2)
g[i]=1ll*g[i-2]*(i-1)%M;
int cnt=0;
for(register int i=1;i<=n+n;++i)
cnt+=!vis[i];
for(register int i=1;i<=n+n;++i)
sum[i]=sum[i-1]+!vis[i];
int ans=0;
for(register int l=2;l<=n+n;l+=2)
for(register int i=1;i+l<=n+n+1;++i)
{
int fl=0;
for(register int j=1;j<=k;++j)
if((i<=a[j]&&a[j]<i+l)^(i<=b[j]&&b[j]<i+l))
{fl=1;break;}
if(fl)continue;
int c2=0;
for(register int j=i;j<i+l;++j)c2+=!vis[j];
f[i][i+l-1]=g[c2];
for(register int j=i+1;j<i+l-1;j+=2)
f[i][i+l-1]=(f[i][i+l-1]+1ll*(M-f[i][j])*g[sum[i+l-1]-sum[j]])%M;
ans=(ans+1ll*f[i][i+l-1]*g[cnt-c2])%M;
}
printf("%d\n",ans);
return 0;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define cmin(a,b) (a>(b)?a=(b),1:0)
#define cmax(a,b) (a<(b)?a=(b),1:0)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define dmax(a,b) ((a)>(b)?(a):(b))
namespace io
{
int F()
{
int F=1,n=0;
char ch;
while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
ch=='-'?F=0:n=ch-'0';
while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
return F?n:-n;
}
long long G()
{
long long F=1,n=0;
char ch;
while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
ch=='-'?F=0:n=ch-'0';
while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
return F?n:-n;
}
}
int R(int l,int r)
{
return (rand()<<15|rand())%(r-l+1)+l;
}
int a[333],b[333];
int f[666][666];
int vis[666];
int g[666];
int sum[666];
const int M=1000000007;
int main()
{
int n=io::F(),k=io::F();
for(register int i=1;i<=k;++i)a[i]=io::F(),b[i]=io::F(),vis[a[i]]=vis[b[i]]=1;
g[0]=1;
for(register int i=2;i<=n+n;i+=2)
g[i]=1ll*g[i-2]*(i-1)%M;
int cnt=0;
for(register int i=1;i<=n+n;++i)
cnt+=!vis[i];
for(register int i=1;i<=n+n;++i)
sum[i]=sum[i-1]+!vis[i];
int ans=0;
for(register int l=2;l<=n+n;l+=2)
for(register int i=1;i+l<=n+n+1;++i)
{
int fl=0;
for(register int j=1;j<=k;++j)
if((i<=a[j]&&a[j]<i+l)^(i<=b[j]&&b[j]<i+l))
{fl=1;break;}
if(fl)continue;
int c2=0;
for(register int j=i;j<i+l;++j)c2+=!vis[j];
f[i][i+l-1]=g[c2];
for(register int j=i+1;j<i+l-1;j+=2)
f[i][i+l-1]=(f[i][i+l-1]+1ll*(M-f[i][j])*g[sum[i+l-1]-sum[j]])%M;
ans=(ans+1ll*f[i][i+l-1]*g[cnt-c2])%M;
}
printf("%d\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int f[603][603] , N , K , pr[1203] , jc[1203];
int poww(long long a , int b){
int times = 1;
while(b){
if(b & 1) times = times * a % MOD;
a = a * a % MOD; b >>= 1;
}
return times;
}
int main(){
cin >> N >> K; memset(pr , -1 , sizeof(pr));
jc[0] = 1;
for(int i = 1 ; i <= N ; ++i) jc[i] = 1ll * jc[i - 1] * (2 * i - 1) % MOD;
for(int i = 1 ; i <= K ; ++i){
int x , y; cin >> x >> y; --x; --y; if(x > y) swap(x , y);
pr[x] = y; pr[y] = x;
}
for(int i = 0 ; i < 2 * N ; ++i)
for(int j = i + 1 ; j < 2 * N ; j += 2){
int p1 = 0; bool flg = 1;
for(int k = i ; k <= j ; ++k)
if(pr[k] == -1) ++p1;
else if(pr[k] < i || pr[k] > j) flg = 0;
if(!flg) continue;
f[i][j] = jc[p1 / 2];
}
for(int i = 1 ; i < 2 * N ; i += 2)
for(int j = 0 ; j + i < 2 * N ; ++j){
bool flg = 1;
for(int t = (j + i + 1) % (2 * N) ; t != j ; t = (t + 1) % (2 * N))
if(pr[t] >= j && pr[t] <= j + i) flg = 0;
if(!flg) continue;
int mn = j + i , p1 = 0;
for(int t = j + i ; t > j ; --t){
if(pr[t] != -1) mn = min(pr[t] , mn); else ++p1;
if(t > mn || (p1 & 1)) continue;
f[j][j + i] = (f[j][j + i] + MOD - 1ll * f[j][t - 1] * jc[p1 / 2] % MOD) % MOD;
}
}
int sum = 0;
for(int i = 0 ; i < 2 * N ; ++i)
for(int j = i ; j < 2 * N ; ++j)
if(f[i][j]){
int p1 = 0;
for(int t = (j + 1) % (2 * N) ; t != i ; t = (t + 1) % (2 * N))
if(pr[t] == -1) ++p1;
sum = (sum + 1ll * jc[p1 / 2] * f[i][j]) % MOD;
}
cout << sum; return 0;
} | ### Prompt
In Cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int f[603][603] , N , K , pr[1203] , jc[1203];
int poww(long long a , int b){
int times = 1;
while(b){
if(b & 1) times = times * a % MOD;
a = a * a % MOD; b >>= 1;
}
return times;
}
int main(){
cin >> N >> K; memset(pr , -1 , sizeof(pr));
jc[0] = 1;
for(int i = 1 ; i <= N ; ++i) jc[i] = 1ll * jc[i - 1] * (2 * i - 1) % MOD;
for(int i = 1 ; i <= K ; ++i){
int x , y; cin >> x >> y; --x; --y; if(x > y) swap(x , y);
pr[x] = y; pr[y] = x;
}
for(int i = 0 ; i < 2 * N ; ++i)
for(int j = i + 1 ; j < 2 * N ; j += 2){
int p1 = 0; bool flg = 1;
for(int k = i ; k <= j ; ++k)
if(pr[k] == -1) ++p1;
else if(pr[k] < i || pr[k] > j) flg = 0;
if(!flg) continue;
f[i][j] = jc[p1 / 2];
}
for(int i = 1 ; i < 2 * N ; i += 2)
for(int j = 0 ; j + i < 2 * N ; ++j){
bool flg = 1;
for(int t = (j + i + 1) % (2 * N) ; t != j ; t = (t + 1) % (2 * N))
if(pr[t] >= j && pr[t] <= j + i) flg = 0;
if(!flg) continue;
int mn = j + i , p1 = 0;
for(int t = j + i ; t > j ; --t){
if(pr[t] != -1) mn = min(pr[t] , mn); else ++p1;
if(t > mn || (p1 & 1)) continue;
f[j][j + i] = (f[j][j + i] + MOD - 1ll * f[j][t - 1] * jc[p1 / 2] % MOD) % MOD;
}
}
int sum = 0;
for(int i = 0 ; i < 2 * N ; ++i)
for(int j = i ; j < 2 * N ; ++j)
if(f[i][j]){
int p1 = 0;
for(int t = (j + 1) % (2 * N) ; t != i ; t = (t + 1) % (2 * N))
if(pr[t] == -1) ++p1;
sum = (sum + 1ll * jc[p1 / 2] * f[i][j]) % MOD;
}
cout << sum; return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while ('0'>ch||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do {(t*=10)+=ch-'0';ch=getchar();} while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int maxn=610;
const ll mod=(1e9)+7;
int n,K,a[maxn],b[maxn],s[maxn];
ll g[maxn],dp[maxn][maxn],ans;
bool in(int l,int r,int x) {
return l<=x&&x<=r;
}
int main() {
read(n); read(K);
for (int i=1;i<=K;i++) {
read(a[i]),read(b[i]);
s[a[i]]=s[b[i]]=1;
}
g[0]=1; n*=2;
for (int i=1;i<=n;i++) s[i]+=s[i-1];
for (int i=2;i<=n;i+=2) g[i]=g[i-2]*(i-1)%mod;
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++) {
bool flag=1; int cnt=s[j]-s[i-1];
for (int k=1,t1,t2;k<=K;k++) {
t1=in(i,j,a[k]); t2=in(i,j,b[k]);
if (t1+t2==1) { flag=0; break; }
}
if (!flag||(j-i+1-cnt)%2==1) continue;
dp[i][j]=g[j-i+1-cnt];
for (int k=i;k<j;k++)
dp[i][j]+=mod-dp[i][k]*g[j-k-s[j]+s[k]]%mod,dp[i][j]%=mod;
ans+=g[n-(j-i+1)-(K*2-cnt)]*dp[i][j]%mod; ans%=mod;
}
printf("%lld\n",ans);
return 0;
} | ### Prompt
Construct a cpp code solution to the problem outlined:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while ('0'>ch||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do {(t*=10)+=ch-'0';ch=getchar();} while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int maxn=610;
const ll mod=(1e9)+7;
int n,K,a[maxn],b[maxn],s[maxn];
ll g[maxn],dp[maxn][maxn],ans;
bool in(int l,int r,int x) {
return l<=x&&x<=r;
}
int main() {
read(n); read(K);
for (int i=1;i<=K;i++) {
read(a[i]),read(b[i]);
s[a[i]]=s[b[i]]=1;
}
g[0]=1; n*=2;
for (int i=1;i<=n;i++) s[i]+=s[i-1];
for (int i=2;i<=n;i+=2) g[i]=g[i-2]*(i-1)%mod;
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++) {
bool flag=1; int cnt=s[j]-s[i-1];
for (int k=1,t1,t2;k<=K;k++) {
t1=in(i,j,a[k]); t2=in(i,j,b[k]);
if (t1+t2==1) { flag=0; break; }
}
if (!flag||(j-i+1-cnt)%2==1) continue;
dp[i][j]=g[j-i+1-cnt];
for (int k=i;k<j;k++)
dp[i][j]+=mod-dp[i][k]*g[j-k-s[j]+s[k]]%mod,dp[i][j]%=mod;
ans+=g[n-(j-i+1)-(K*2-cnt)]*dp[i][j]%mod; ans%=mod;
}
printf("%lld\n",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef long long ll;
inline void add(int &x,ll y) {
x=(x+y)%MOD;
}
ll facd[305];
void pre(int n) {
facd[0]=1;
for(int i=1;i<=n;i++) facd[i]=facd[i-1]*(2LL*i-1)%MOD;
}
int num[605];
int f[2][305];
int main() {
int n,m;
scanf("%d%d",&n,&m);
pre(n);
for(int i=1;i<=m;i++) {
int x,y;
scanf("%d%d",&x,&y);
num[x]=y;
num[y]=x;
}
int ans=0;
for(int i=1;i<=2*n;i++) {
int cur=0;
memset(f[cur],0,sizeof(f[cur]));
f[cur][0]=1;
int sz1=0,sz2=0;
for(int j=i;j<=2*n;j++) {
if (num[j]) {
if (num[j]>j) sz1++;
else if (num[j]<i) break;
else sz1--;
}
else {
sz2++;
cur^=1;
memset(f[cur],0,sizeof(f[cur]));
for(int k=0;k<=min(n,sz2);k++)
if (f[cur^1][k]) {
if (k) add(f[cur][k-1],(ll)f[cur^1][k]*k);
add(f[cur][k+1],f[cur^1][k]);
}
}
if (!sz1&&f[cur][0]) {
add(ans,(ll)f[cur][0]*facd[n-m-(sz2>>1)]);
f[cur][0]=0;
}
}
}
printf("%d\n",ans);
return 0;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef long long ll;
inline void add(int &x,ll y) {
x=(x+y)%MOD;
}
ll facd[305];
void pre(int n) {
facd[0]=1;
for(int i=1;i<=n;i++) facd[i]=facd[i-1]*(2LL*i-1)%MOD;
}
int num[605];
int f[2][305];
int main() {
int n,m;
scanf("%d%d",&n,&m);
pre(n);
for(int i=1;i<=m;i++) {
int x,y;
scanf("%d%d",&x,&y);
num[x]=y;
num[y]=x;
}
int ans=0;
for(int i=1;i<=2*n;i++) {
int cur=0;
memset(f[cur],0,sizeof(f[cur]));
f[cur][0]=1;
int sz1=0,sz2=0;
for(int j=i;j<=2*n;j++) {
if (num[j]) {
if (num[j]>j) sz1++;
else if (num[j]<i) break;
else sz1--;
}
else {
sz2++;
cur^=1;
memset(f[cur],0,sizeof(f[cur]));
for(int k=0;k<=min(n,sz2);k++)
if (f[cur^1][k]) {
if (k) add(f[cur][k-1],(ll)f[cur^1][k]*k);
add(f[cur][k+1],f[cur^1][k]);
}
}
if (!sz1&&f[cur][0]) {
add(ans,(ll)f[cur][0]*facd[n-m-(sz2>>1)]);
f[cur][0]=0;
}
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int N = 605;
const int MOD = 1e9 + 7;
template <typename T> inline void read(T &AKNOI) {
T x = 0, flag = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') flag = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
AKNOI = flag * x;
}
namespace ModCalculator {
inline void Inc(int &x, int y) {
x += y; if (x >= MOD) x -= MOD;
}
inline void Dec(int &x, int y) {
x -= y; if (x < 0) x += MOD;
}
inline int Add(int x, int y) {
Inc(x, y); return x;
}
inline int Sub(int x, int y) {
Dec(x, y); return x;
}
inline int Mul(int x, int y) {
return 1LL * x * y % MOD;
}
}
using namespace ModCalculator;
int n, m, nn, e[N], cnt[N][N];
int g[N], dp[N][N], ans;
void init() {
read(n); read(m);
nn = 2 * (n - m);
n <<= 1;
for (int i = 1, u, v; i <= m; ++i) {
read(u); read(v);
e[u] = v;
e[v] = u;
}
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
cnt[i][j] = cnt[i][j - 1] + (!e[j]);
}
}
g[0] = 1;
for (int i = 2; i <= n; i += 2) {
g[i] = Mul(g[i - 2], i - 1);
}
}
void solve() {
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
int flag = 1;
for (int k = i; k <= j; ++k) {
if (e[k] && (e[k] < i || e[k] > j)) {
flag = 0; break;
}
}
if (!flag) continue;
dp[i][j] = g[cnt[i][j]];
for (int k = i + 1; k < j; ++k) {
Dec(dp[i][j], Mul(dp[i][k], g[cnt[k + 1][j]]));
}
Inc(ans, Mul(dp[i][j], g[nn - cnt[i][j]]));
}
}
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int N = 605;
const int MOD = 1e9 + 7;
template <typename T> inline void read(T &AKNOI) {
T x = 0, flag = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') flag = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
AKNOI = flag * x;
}
namespace ModCalculator {
inline void Inc(int &x, int y) {
x += y; if (x >= MOD) x -= MOD;
}
inline void Dec(int &x, int y) {
x -= y; if (x < 0) x += MOD;
}
inline int Add(int x, int y) {
Inc(x, y); return x;
}
inline int Sub(int x, int y) {
Dec(x, y); return x;
}
inline int Mul(int x, int y) {
return 1LL * x * y % MOD;
}
}
using namespace ModCalculator;
int n, m, nn, e[N], cnt[N][N];
int g[N], dp[N][N], ans;
void init() {
read(n); read(m);
nn = 2 * (n - m);
n <<= 1;
for (int i = 1, u, v; i <= m; ++i) {
read(u); read(v);
e[u] = v;
e[v] = u;
}
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
cnt[i][j] = cnt[i][j - 1] + (!e[j]);
}
}
g[0] = 1;
for (int i = 2; i <= n; i += 2) {
g[i] = Mul(g[i - 2], i - 1);
}
}
void solve() {
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
int flag = 1;
for (int k = i; k <= j; ++k) {
if (e[k] && (e[k] < i || e[k] > j)) {
flag = 0; break;
}
}
if (!flag) continue;
dp[i][j] = g[cnt[i][j]];
for (int k = i + 1; k < j; ++k) {
Dec(dp[i][j], Mul(dp[i][k], g[cnt[k + 1][j]]));
}
Inc(ans, Mul(dp[i][j], g[nn - cnt[i][j]]));
}
}
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = b-1; i >= a; i--)
#define IN(i,a,b) (a<=i&&i<=b)
const int mod = 1e9+7;
ll modpow(ll a, ll b) {
return b?modpow(a*a,b/2)*(b&1?a:1)%mod:1;
}
void add(ll &a, ll b) {
if (b>=mod) b%=mod;
a += b;
if (a>=mod) a-=mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
clock_t start = clock();
#endif
int n,k; cin>>n>>k;
int a[k+1],b[k+1];
f(i,0,k) cin>>a[i]>>b[i];
f(i,0,k) a[i]--,b[i]--;
ll g[2*n+1];
f(i,0,2*n+1) g[i] = i<2?i:g[i-2]*i%mod;
fr(i,1,2*n+1) g[i] = g[i-1];
g[0] = 1;
ll z[2*n];
fill(z,z+2*n,1);
f(i,0,k) z[a[i]] = z[b[i]] = 0;
f(i,1,2*n) z[i] += z[i-1];
ll dp[2*n][2*n];
f(i,0,2*n) f(j,0,2*n) dp[i][j] = 0;
ll ans = 0;
f(l,0,2*n) f(i,0,2*n-l) {
int j = i+l;
dp[i][j] = 0;
bool found = 0;
f(x,0,k) found |= IN(a[x],i,j)^IN(b[x],i,j);
if (found) continue;
int x = z[j] - (i?z[i-1]:0);
int y = 2*n-2*k-x;
add(dp[i][j],g[x]);
f(kk,i+1,j) {
int zz = x-(z[kk]-(i?z[i-1]:0));
add(dp[i][j],mod-dp[i][kk]*g[zz]%mod);
}
add(ans,dp[i][j]*g[y]);
}
cout << ans << endl;
#ifdef LOCAL
cout << setprecision(12) << (long double)(clock()-start) / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
| ### Prompt
In cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = b-1; i >= a; i--)
#define IN(i,a,b) (a<=i&&i<=b)
const int mod = 1e9+7;
ll modpow(ll a, ll b) {
return b?modpow(a*a,b/2)*(b&1?a:1)%mod:1;
}
void add(ll &a, ll b) {
if (b>=mod) b%=mod;
a += b;
if (a>=mod) a-=mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
clock_t start = clock();
#endif
int n,k; cin>>n>>k;
int a[k+1],b[k+1];
f(i,0,k) cin>>a[i]>>b[i];
f(i,0,k) a[i]--,b[i]--;
ll g[2*n+1];
f(i,0,2*n+1) g[i] = i<2?i:g[i-2]*i%mod;
fr(i,1,2*n+1) g[i] = g[i-1];
g[0] = 1;
ll z[2*n];
fill(z,z+2*n,1);
f(i,0,k) z[a[i]] = z[b[i]] = 0;
f(i,1,2*n) z[i] += z[i-1];
ll dp[2*n][2*n];
f(i,0,2*n) f(j,0,2*n) dp[i][j] = 0;
ll ans = 0;
f(l,0,2*n) f(i,0,2*n-l) {
int j = i+l;
dp[i][j] = 0;
bool found = 0;
f(x,0,k) found |= IN(a[x],i,j)^IN(b[x],i,j);
if (found) continue;
int x = z[j] - (i?z[i-1]:0);
int y = 2*n-2*k-x;
add(dp[i][j],g[x]);
f(kk,i+1,j) {
int zz = x-(z[kk]-(i?z[i-1]:0));
add(dp[i][j],mod-dp[i][kk]*g[zz]%mod);
}
add(ans,dp[i][j]*g[y]);
}
cout << ans << endl;
#ifdef LOCAL
cout << setprecision(12) << (long double)(clock()-start) / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 610;
const int mod = 1e9 + 7;
int n, k, fix[N], f[N][N], mat[N];
long long g[N];
inline void calc(int l, int r) {
for (int i = l; i <= r; i ++)
if (mat[i]) if (mat[i] < l || mat[i] > r) return;
long long ans = g[r - l + 1 - (fix[r] - fix[l - 1])];
for (int i = l + 1; i < r; i ++)
(ans -= f[l][i] * g[r - i - (fix[r] - fix[i])]) %= mod;
f[l][r] = (ans % mod + mod) % mod;
}
int main() {
scanf("%d%d", &n, &k);
n *= 2;
for (int i = 1, a, b; i <= k; i ++) {
scanf("%d%d", &a, &b);
fix[a] = fix[b] = 1;
mat[a] = b; mat[b] = a;
}
g[0] = 1;
for (int i = 1; i <= n; i ++) {
fix[i] += fix[i - 1];
g[i] = ( (i & 1) ? 0 : g[i - 2] * (i - 1) % mod );
}
long long ans = 0;
for (int i = 2; i <= n; i += 2)
for (int j = 1; i + j - 1 <= n; j ++) {
calc(j, i + j - 1);
int cont = 2 * k - (fix[i + j - 1] - fix[j - 1]);
ans += f[j][i + j - 1] * g[n - i - cont] % mod;
}
printf("%lld\n", ans % mod);
return 0;
} | ### Prompt
In cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 610;
const int mod = 1e9 + 7;
int n, k, fix[N], f[N][N], mat[N];
long long g[N];
inline void calc(int l, int r) {
for (int i = l; i <= r; i ++)
if (mat[i]) if (mat[i] < l || mat[i] > r) return;
long long ans = g[r - l + 1 - (fix[r] - fix[l - 1])];
for (int i = l + 1; i < r; i ++)
(ans -= f[l][i] * g[r - i - (fix[r] - fix[i])]) %= mod;
f[l][r] = (ans % mod + mod) % mod;
}
int main() {
scanf("%d%d", &n, &k);
n *= 2;
for (int i = 1, a, b; i <= k; i ++) {
scanf("%d%d", &a, &b);
fix[a] = fix[b] = 1;
mat[a] = b; mat[b] = a;
}
g[0] = 1;
for (int i = 1; i <= n; i ++) {
fix[i] += fix[i - 1];
g[i] = ( (i & 1) ? 0 : g[i - 2] * (i - 1) % mod );
}
long long ans = 0;
for (int i = 2; i <= n; i += 2)
for (int j = 1; i + j - 1 <= n; j ++) {
calc(j, i + j - 1);
int cont = 2 * k - (fix[i + j - 1] - fix[j - 1]);
ans += f[j][i + j - 1] * g[n - i - cont] % mod;
}
printf("%lld\n", ans % mod);
return 0;
}
``` |
#include<stdio.h>
int a[512],b[512];
int dp[700][700];
bool vis[1024];
int f[700][700],g[700][700];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
n<<=1;
for(int i=0;i<k;i++)
{
scanf("%d%d",&a[i],&b[i]);
vis[a[i]]=true;
vis[b[i]]=true;
}
for(int l=1;l<=n;l++)
{
for(int r=l;r<=n;r++)
{
bool ok=true;
for(int i=0;i<k;i++)
{
if((a[i]>=l&&a[i]<=r)^(b[i]>=l&&b[i]<=r))
{
ok=false;
break;
}
}
if(!ok)
{
continue;
}
int num1=0,num2=0;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
continue;
}
if(i>=l&&i<=r)
{
num1++;
}
else
{
num2++;
}
}
if((num1&1)||(num2&1))
{
continue;
}
f[l][r]=1;
for(int i=1;i<=num1;i+=2)
{
f[l][r]=(long long)f[l][r]*i%1000000007;
}
g[l][r]=1;
for(int i=1;i<=num2;i+=2)
{
g[l][r]=(long long)g[l][r]*i%1000000007;
}
}
}
int ans=0;
for(int l=1;l<=n;l++)
{
for(int r=l;r<=n;r++)
{
dp[l][r]=f[l][r];
for(int i=l;i<r;i++)
{
dp[l][r]-=(long long)dp[l][i]*f[i+1][r]%1000000007;
if(dp[l][r]<0)
{
dp[l][r]+=1000000007;
}
}
ans+=(long long)dp[l][r]*g[l][r]%1000000007;
if(ans>=1000000007)
{
ans-=1000000007;
}
}
}
printf("%d\n",ans);
return 0;
} | ### Prompt
Your task is to create a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<stdio.h>
int a[512],b[512];
int dp[700][700];
bool vis[1024];
int f[700][700],g[700][700];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
n<<=1;
for(int i=0;i<k;i++)
{
scanf("%d%d",&a[i],&b[i]);
vis[a[i]]=true;
vis[b[i]]=true;
}
for(int l=1;l<=n;l++)
{
for(int r=l;r<=n;r++)
{
bool ok=true;
for(int i=0;i<k;i++)
{
if((a[i]>=l&&a[i]<=r)^(b[i]>=l&&b[i]<=r))
{
ok=false;
break;
}
}
if(!ok)
{
continue;
}
int num1=0,num2=0;
for(int i=1;i<=n;i++)
{
if(vis[i])
{
continue;
}
if(i>=l&&i<=r)
{
num1++;
}
else
{
num2++;
}
}
if((num1&1)||(num2&1))
{
continue;
}
f[l][r]=1;
for(int i=1;i<=num1;i+=2)
{
f[l][r]=(long long)f[l][r]*i%1000000007;
}
g[l][r]=1;
for(int i=1;i<=num2;i+=2)
{
g[l][r]=(long long)g[l][r]*i%1000000007;
}
}
}
int ans=0;
for(int l=1;l<=n;l++)
{
for(int r=l;r<=n;r++)
{
dp[l][r]=f[l][r];
for(int i=l;i<r;i++)
{
dp[l][r]-=(long long)dp[l][i]*f[i+1][r]%1000000007;
if(dp[l][r]<0)
{
dp[l][r]+=1000000007;
}
}
ans+=(long long)dp[l][r]*g[l][r]%1000000007;
if(ans>=1000000007)
{
ans-=1000000007;
}
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
#define to edge[i].v
#define mp make_pair
#define rint register int
#define debug(x) cerr<<#x<<"="<<x<<endl
#define fgx cerr<<"-------------"<<endl
#define N 666
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int mod=1e9+7;
int go[N],mx[N][N],mn[N][N],s[N],f[N][N],g[N];
int main()
{ int n,k,x,y,ans=0; cin>>n>>k; n*=2;
for(rint i=1;i<=k;i++) scanf("%d%d",&x,&y),go[x]=y,go[y]=x;
for(rint i=1;i<=n;i++) s[i]=s[i-1]+(!go[i]);
for(rint i=1;i<=n;i++)
{ mx[i][i-1]=0; mn[i][i-1]=1e9;
for(rint j=i;j<=n;j++)
{ mx[i][j]=mx[i][j-1]; mn[i][j]=mn[i][j-1];
if(go[j]) mx[i][j]=max(mx[i][j],go[j]),mn[i][j]=min(mn[i][j],go[j]);
}
}
g[0]=1; for(rint i=2;i<=n;i++) g[i]=(ll)g[i-2]*(i-1)%mod;
for(rint i=2;i<=n;i+=2)
{ for(rint l=1;l+i-1<=n;l++)
{ int r=l+i-1;
if(mn[l][r]<l||mx[l][r]>r) continue; f[l][r]=g[s[r]-s[l-1]];
for(rint k=l+1;k<r;k+=2)
f[l][r]=(f[l][r]-(ll)f[l][k]*g[s[r]-s[k]]%mod+mod)%mod;
(ans+=(ll)f[l][r]*g[s[l-1]+s[n]-s[r]]%mod)%=mod;
}
}
cout<<ans;
return 0;
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define to edge[i].v
#define mp make_pair
#define rint register int
#define debug(x) cerr<<#x<<"="<<x<<endl
#define fgx cerr<<"-------------"<<endl
#define N 666
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int mod=1e9+7;
int go[N],mx[N][N],mn[N][N],s[N],f[N][N],g[N];
int main()
{ int n,k,x,y,ans=0; cin>>n>>k; n*=2;
for(rint i=1;i<=k;i++) scanf("%d%d",&x,&y),go[x]=y,go[y]=x;
for(rint i=1;i<=n;i++) s[i]=s[i-1]+(!go[i]);
for(rint i=1;i<=n;i++)
{ mx[i][i-1]=0; mn[i][i-1]=1e9;
for(rint j=i;j<=n;j++)
{ mx[i][j]=mx[i][j-1]; mn[i][j]=mn[i][j-1];
if(go[j]) mx[i][j]=max(mx[i][j],go[j]),mn[i][j]=min(mn[i][j],go[j]);
}
}
g[0]=1; for(rint i=2;i<=n;i++) g[i]=(ll)g[i-2]*(i-1)%mod;
for(rint i=2;i<=n;i+=2)
{ for(rint l=1;l+i-1<=n;l++)
{ int r=l+i-1;
if(mn[l][r]<l||mx[l][r]>r) continue; f[l][r]=g[s[r]-s[l-1]];
for(rint k=l+1;k<r;k+=2)
f[l][r]=(f[l][r]-(ll)f[l][k]*g[s[r]-s[k]]%mod+mod)%mod;
(ans+=(ll)f[l][r]*g[s[l-1]+s[n]-s[r]]%mod)%=mod;
}
}
cout<<ans;
return 0;
}
``` |
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
inline int read()
{
int f = 1, x = 0;
char ch;
do{
ch = getchar();
if (ch == '-')
f = -1;
}while(ch < '0' || ch > '9');
do{
x = x * 10 + ch - '0';
ch = getchar();
}while(ch >= '0' && ch <= '9');
return f * x;
}
const int N = 600;
const int mod = 1e9 + 7;
int n, k;
int to[N + 1];
int f[N + 1][N + 1], g[N + 1];
int ans;
inline void dp()
{
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j += 2) {
int sum1 = j - i + 1, sum2;
bool flag = true;
for (int k = i; k <= j; k++) {
if (to[k]) {
sum1--;
if (to[k] < i || to[k] > j) {
flag = false;
break;
}
}
}
if (flag) {
f[i][j] = g[sum1];
sum2 = !to[j];
for (int k = j - 1; k >= i; k--) {
f[i][j] = (f[i][j] - 1ll * f[i][k] * g[sum2] + 1ll * mod) % mod;
sum2 += !to[k];
}
ans = (ans + 1ll * f[i][j] * g[n - (k << 1) - sum1]) % mod;
}
}
}
return;
}
int main()
{
n = read() << 1;
k = read();
for (int i = 1; i <= k; i++) {
int x = read(), y = read();
to[x] = y;
to[y] = x;
}
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = 1ll * g[i - 2] * (i - 1) % mod;
dp();
printf("%d\n", ans);
return 0;
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
inline int read()
{
int f = 1, x = 0;
char ch;
do{
ch = getchar();
if (ch == '-')
f = -1;
}while(ch < '0' || ch > '9');
do{
x = x * 10 + ch - '0';
ch = getchar();
}while(ch >= '0' && ch <= '9');
return f * x;
}
const int N = 600;
const int mod = 1e9 + 7;
int n, k;
int to[N + 1];
int f[N + 1][N + 1], g[N + 1];
int ans;
inline void dp()
{
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j += 2) {
int sum1 = j - i + 1, sum2;
bool flag = true;
for (int k = i; k <= j; k++) {
if (to[k]) {
sum1--;
if (to[k] < i || to[k] > j) {
flag = false;
break;
}
}
}
if (flag) {
f[i][j] = g[sum1];
sum2 = !to[j];
for (int k = j - 1; k >= i; k--) {
f[i][j] = (f[i][j] - 1ll * f[i][k] * g[sum2] + 1ll * mod) % mod;
sum2 += !to[k];
}
ans = (ans + 1ll * f[i][j] * g[n - (k << 1) - sum1]) % mod;
}
}
}
return;
}
int main()
{
n = read() << 1;
k = read();
for (int i = 1; i <= k; i++) {
int x = read(), y = read();
to[x] = y;
to[y] = x;
}
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = 1ll * g[i - 2] * (i - 1) % mod;
dp();
printf("%d\n", ans);
return 0;
}
``` |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int mod=1e9+7;
int add(int x,int y){return x+y<mod?x+y:x+y-mod;}
void ade(int& x,int y){x+=y;if(x>=mod)x-=mod;}
int qpow(int x,int k)
{
int r=1;
while(k)
{
if(k&1)r=1ll*r*x%mod;
k>>=1;x=1ll*x*x%mod;
}
return r;
}
int T;
int n,m;
int f[660][660];
int ed[660];
int pre[660];
int g[660];
bool check(int l,int r)
{
for(int i=l;i<=r;++i) if(ed[i]&&(ed[i]<l||ed[i]>r))return 0;
return 1;
}
int getr(int l,int r){return r>=l?pre[r]-pre[l-1]:0;}
int calc(int l,int r)
{
int res=g[getr(l,r)];
for(int i=l+1;i<r;i+=2)
ade(res,mod-1ll*f[l][i]*g[getr(i+1,r)]%mod);
return res;
}
int main()
{
g[0]=1;
for(int i=2;i<=600;i+=2)g[i]=1ll*g[i-2]*(i-1)%mod;
memset(f,0,sizeof(f));
memset(ed,0,sizeof(ed));
memset(pre,0,sizeof(pre));
scanf("%d%d",&n,&m);
int x,y;
for(int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
ed[x]=y;
ed[y]=x;
}
for(int i=1;i<=n*2;++i) pre[i]=pre[i-1]+(!ed[i]);
int ans=0;
for(int l=1;l<=n*2;++l)
for(int r=l+1;r<=n*2;r+=2)
if(check(l,r))
{
f[l][r]=calc(l,r);
// cout<<"l: "<<l<<" r: "<<r<<" f: "<<f[l][r]<<" re: "<<getr(1,l-1)+getr(r+1,n*2)<<endl;
ade(ans,1ll*f[l][r]*g[getr(1,l-1)+getr(r+1,n*2)]%mod);
}
printf("%d\n",ans);
return 0;
}
| ### Prompt
Please create a solution in CPP to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int mod=1e9+7;
int add(int x,int y){return x+y<mod?x+y:x+y-mod;}
void ade(int& x,int y){x+=y;if(x>=mod)x-=mod;}
int qpow(int x,int k)
{
int r=1;
while(k)
{
if(k&1)r=1ll*r*x%mod;
k>>=1;x=1ll*x*x%mod;
}
return r;
}
int T;
int n,m;
int f[660][660];
int ed[660];
int pre[660];
int g[660];
bool check(int l,int r)
{
for(int i=l;i<=r;++i) if(ed[i]&&(ed[i]<l||ed[i]>r))return 0;
return 1;
}
int getr(int l,int r){return r>=l?pre[r]-pre[l-1]:0;}
int calc(int l,int r)
{
int res=g[getr(l,r)];
for(int i=l+1;i<r;i+=2)
ade(res,mod-1ll*f[l][i]*g[getr(i+1,r)]%mod);
return res;
}
int main()
{
g[0]=1;
for(int i=2;i<=600;i+=2)g[i]=1ll*g[i-2]*(i-1)%mod;
memset(f,0,sizeof(f));
memset(ed,0,sizeof(ed));
memset(pre,0,sizeof(pre));
scanf("%d%d",&n,&m);
int x,y;
for(int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
ed[x]=y;
ed[y]=x;
}
for(int i=1;i<=n*2;++i) pre[i]=pre[i-1]+(!ed[i]);
int ans=0;
for(int l=1;l<=n*2;++l)
for(int r=l+1;r<=n*2;r+=2)
if(check(l,r))
{
f[l][r]=calc(l,r);
// cout<<"l: "<<l<<" r: "<<r<<" f: "<<f[l][r]<<" re: "<<getr(1,l-1)+getr(r+1,n*2)<<endl;
ade(ans,1ll*f[l][r]*g[getr(1,l-1)+getr(r+1,n*2)]%mod);
}
printf("%d\n",ans);
return 0;
}
``` |
#include <iostream>
using i64 = long long;
const i64 MOD = 1000000007;
const int N = 600 + 7;
int n, k;
int to[N];
int cnt[N];
i64 f[N][N], g[N][N];
i64 dfac_[N], *dfac;
i64 ans;
int main() {
// freopen("code.in", "r", stdin);
// freopen("code.out", "w", stdout);
scanf("%d%d", &n, &k), n <<= 1;
for (int i = 1, x, y; i <= k; ++i)
scanf("%d%d", &x, &y), to[x] = y, to[y] = x;
for (int i = 1; i <= n; ++i)
cnt[i] = cnt[i - 1] + (to[i] > 0);
dfac = dfac_ + 2, dfac[-1] = dfac[0] = 1;
for (int i = 1; i <= n; ++i) dfac[i] = dfac[i - 2] * i % MOD;
for (int l = 2; l <= n; l += 2)
for (int i = 1, j = l; j <= n; ++i, ++j) {
int ok = 1;
for (int k = i; k <= j; ++k) ok &= !to[k] || i <= to[k] && to[k] <= j;
if (!ok) continue;
f[i][j] = g[i][j] = dfac[l - (cnt[j] - cnt[i - 1]) - 1];
for (int k = i + 1; k < j; k += 2)
( f[i][j] -= f[i][k] * g[k + 1][j] ) %= MOD;
( ans += f[i][j] * dfac[n - l - 2 * k + (cnt[j] - cnt[i - 1]) - 1] ) %= MOD;
}
printf("%lld", (ans + MOD) % MOD);
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <iostream>
using i64 = long long;
const i64 MOD = 1000000007;
const int N = 600 + 7;
int n, k;
int to[N];
int cnt[N];
i64 f[N][N], g[N][N];
i64 dfac_[N], *dfac;
i64 ans;
int main() {
// freopen("code.in", "r", stdin);
// freopen("code.out", "w", stdout);
scanf("%d%d", &n, &k), n <<= 1;
for (int i = 1, x, y; i <= k; ++i)
scanf("%d%d", &x, &y), to[x] = y, to[y] = x;
for (int i = 1; i <= n; ++i)
cnt[i] = cnt[i - 1] + (to[i] > 0);
dfac = dfac_ + 2, dfac[-1] = dfac[0] = 1;
for (int i = 1; i <= n; ++i) dfac[i] = dfac[i - 2] * i % MOD;
for (int l = 2; l <= n; l += 2)
for (int i = 1, j = l; j <= n; ++i, ++j) {
int ok = 1;
for (int k = i; k <= j; ++k) ok &= !to[k] || i <= to[k] && to[k] <= j;
if (!ok) continue;
f[i][j] = g[i][j] = dfac[l - (cnt[j] - cnt[i - 1]) - 1];
for (int k = i + 1; k < j; k += 2)
( f[i][j] -= f[i][k] * g[k + 1][j] ) %= MOD;
( ans += f[i][j] * dfac[n - l - 2 * k + (cnt[j] - cnt[i - 1]) - 1] ) %= MOD;
}
printf("%lld", (ans + MOD) % MOD);
return 0;
}
``` |
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const int N = 608;
int a[N], b[N], c[N], n, k;
ll p[N], f[N][N];
inline bool in(int a, int l, int r) {
return l <= a && a < r;
}
bool chk(int l, int r) {
for (int i = 0; i < k; i++)
if (in(a[i], l, r) ^ in(b[i], l, r)) return true;
return false;
}
int main() {
scanf("%d%d",&n,&k);
if (n == 1) {puts("1"); return 0;}
p[0] = 1;
for (int i = 0; i < n-k; i++)
p[i+1] = p[i] * (i<<1|1) %MOD;
n <<= 1;
for (int i = 0; i < k; i++) {
scanf("%d%d",a+i,b+i);
c[a[i]--] = c[b[i]--] = 1;
}
for (int i = 1; i <= n; i++)
c[i] = (!c[i]) + c[i-1];
ll ans = 0;
for (int l = 0; l < n; l++)
for (int r = l; (r += 2) <= n;) {
int x = c[r] - c[l];
if ((x&1) || chk(l, r)) continue;
ll s = p[x>>1];
for (int i = l; (i += 2) < r;)
s = (s - f[l][i] * p[c[r]-c[i]>>1]) %MOD;
f[l][r] = s;
ans = (ans + s * p[(n-x>>1)-k]) %MOD;
}
if (ans < 0) ans += MOD;
printf("%lld\n",ans);
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const int N = 608;
int a[N], b[N], c[N], n, k;
ll p[N], f[N][N];
inline bool in(int a, int l, int r) {
return l <= a && a < r;
}
bool chk(int l, int r) {
for (int i = 0; i < k; i++)
if (in(a[i], l, r) ^ in(b[i], l, r)) return true;
return false;
}
int main() {
scanf("%d%d",&n,&k);
if (n == 1) {puts("1"); return 0;}
p[0] = 1;
for (int i = 0; i < n-k; i++)
p[i+1] = p[i] * (i<<1|1) %MOD;
n <<= 1;
for (int i = 0; i < k; i++) {
scanf("%d%d",a+i,b+i);
c[a[i]--] = c[b[i]--] = 1;
}
for (int i = 1; i <= n; i++)
c[i] = (!c[i]) + c[i-1];
ll ans = 0;
for (int l = 0; l < n; l++)
for (int r = l; (r += 2) <= n;) {
int x = c[r] - c[l];
if ((x&1) || chk(l, r)) continue;
ll s = p[x>>1];
for (int i = l; (i += 2) < r;)
s = (s - f[l][i] * p[c[r]-c[i]>>1]) %MOD;
f[l][r] = s;
ans = (ans + s * p[(n-x>>1)-k]) %MOD;
}
if (ans < 0) ans += MOD;
printf("%lld\n",ans);
return 0;
}
``` |
#include <cstdio>
using namespace std;
#define fr(i, s, t) for (int i = s, i##end = t; i <= i##end; ++ i)
typedef long long LL;
const int N = 610;
const LL mod = 1e9 + 7;
int n, m, s, t;
int p[N], sum[N];
LL fac[N];
LL f[N][N], g[N][N];
int main()
{
scanf("%d %d", &n, &m);
fr (i, 1, m)
{
scanf("%d %d", &s, &t);
p[s] = t, p[t] = s;
}
fr (i, 1, n << 1)
sum[i] = sum[i - 1] + (p[i] == 0);
fac[0] = 1;
for (int i = 2; i <= n << 1; i += 2) fac[i] = fac[i - 2] * (i - 1) % mod;
LL ans = 0;
fr (len, 1, n << 1) fr (i, 1, (n << 1) - len)
{
int j = i + len;
bool flag = j - i + 1 & 1;
fr (k, i, j)
if (p[k] && (p[k] < i || p[k] > j)) flag = 1;
if (flag) continue;
f[i][j] = fac[sum[j] - sum[i - 1]];
fr (k, i + 1, j - 1)
(f[i][j] += - f[i][k] * fac[sum[j] - sum[k]] % mod + mod) %= mod;
(ans += f[i][j] * fac[(n - m << 1) - (sum[j] - sum[i - 1])]) %= mod;
}
printf("%lld\n", ans);
} | ### Prompt
Please formulate a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <cstdio>
using namespace std;
#define fr(i, s, t) for (int i = s, i##end = t; i <= i##end; ++ i)
typedef long long LL;
const int N = 610;
const LL mod = 1e9 + 7;
int n, m, s, t;
int p[N], sum[N];
LL fac[N];
LL f[N][N], g[N][N];
int main()
{
scanf("%d %d", &n, &m);
fr (i, 1, m)
{
scanf("%d %d", &s, &t);
p[s] = t, p[t] = s;
}
fr (i, 1, n << 1)
sum[i] = sum[i - 1] + (p[i] == 0);
fac[0] = 1;
for (int i = 2; i <= n << 1; i += 2) fac[i] = fac[i - 2] * (i - 1) % mod;
LL ans = 0;
fr (len, 1, n << 1) fr (i, 1, (n << 1) - len)
{
int j = i + len;
bool flag = j - i + 1 & 1;
fr (k, i, j)
if (p[k] && (p[k] < i || p[k] > j)) flag = 1;
if (flag) continue;
f[i][j] = fac[sum[j] - sum[i - 1]];
fr (k, i + 1, j - 1)
(f[i][j] += - f[i][k] * fac[sum[j] - sum[k]] % mod + mod) %= mod;
(ans += f[i][j] * fac[(n - m << 1) - (sum[j] - sum[i - 1])]) %= mod;
}
printf("%lld\n", ans);
}
``` |
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
const LL mod=1e9+7;
LL p[610],f[610][610],g[610],n,K,cnt[610][610];
int main()
{
scanf("%lld %lld",&n,&K);
n<<=1;
for(LL i=1;i<=K;i++)
{
LL x,y;scanf("%lld %lld",&x,&y);
p[x]=y;p[y]=x;
}
g[0]=1;
for(LL i=2;i<=n;i+=2) g[i]=(g[i-2]*(i-1))%mod;
for(LL i=1;i<=n;i++)
for(LL j=i;j<=n;j++) cnt[i][j]=cnt[i][j-1]+(!p[j]);
for(LL i=1;i<=n;i++)
for(LL j=i;j<=n;j++)
{
//printf("%lld %lld\n",i,j);
bool flag=false;
for(LL k=i;k<=j;k++)
if(p[k]&&(p[k]<i||p[k]>j))
{
flag=true;
break;
}
if(flag) continue;
f[i][j]=g[cnt[i][j]];
for(LL k=i+1;k<j;k++) (f[i][j]-=f[i][k]*g[cnt[k+1][j]])%=mod;
//printf("%lld\n",f[i][j]);
}
LL ans=0;
for(LL i=1;i<=n;i++)
for(LL j=i;j<=n;j++)
(ans+=f[i][j]*g[(n/2-K)*2-cnt[i][j]]%mod)%=mod;
printf("%lld",(ans+mod)%mod);
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
const LL mod=1e9+7;
LL p[610],f[610][610],g[610],n,K,cnt[610][610];
int main()
{
scanf("%lld %lld",&n,&K);
n<<=1;
for(LL i=1;i<=K;i++)
{
LL x,y;scanf("%lld %lld",&x,&y);
p[x]=y;p[y]=x;
}
g[0]=1;
for(LL i=2;i<=n;i+=2) g[i]=(g[i-2]*(i-1))%mod;
for(LL i=1;i<=n;i++)
for(LL j=i;j<=n;j++) cnt[i][j]=cnt[i][j-1]+(!p[j]);
for(LL i=1;i<=n;i++)
for(LL j=i;j<=n;j++)
{
//printf("%lld %lld\n",i,j);
bool flag=false;
for(LL k=i;k<=j;k++)
if(p[k]&&(p[k]<i||p[k]>j))
{
flag=true;
break;
}
if(flag) continue;
f[i][j]=g[cnt[i][j]];
for(LL k=i+1;k<j;k++) (f[i][j]-=f[i][k]*g[cnt[k+1][j]])%=mod;
//printf("%lld\n",f[i][j]);
}
LL ans=0;
for(LL i=1;i<=n;i++)
for(LL j=i;j<=n;j++)
(ans+=f[i][j]*g[(n/2-K)*2-cnt[i][j]]%mod)%=mod;
printf("%lld",(ans+mod)%mod);
}
``` |
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ans=0,n,i,j,u,k,x,y,p,sum,st,ok=0,pos[605],dp[2][1205][1205],fac[605],inv2[605],invfac[605];
const ll mod=1e9+7;
ll pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
ll col(ll a){return a>2*n?a-2*n:a;}
ll cont(ll a,ll b,ll c){return (c>=a&&c<=b)||(c+2*n>=a&&c+2*n<=b);}
int main()
{
scanf("%lld%lld",&n,&k);
fac[0]=1;dp[0][1][0]=1;
for(i=1;i<=2*n;i++)fac[i]=fac[i-1]*i%mod;
invfac[2*n]=pow(fac[2*n],mod-2);
for(i=2*n-1;i>=0;i--)invfac[i]=invfac[i+1]*(i+1)%mod;
inv2[2*n]=pow(pow(2LL,2*n),mod-2);
for(i=2*n-1;i>=0;i--)inv2[i]=inv2[i+1]*2%mod;
for(i=1;i<=k;i++)
{
scanf("%lld%lld",&x,&y);
pos[x]=y;pos[y]=x;
}
for(i=2;i<=2*n;i+=2)
{
for(j=1;j<=2*n;j++)
{
sum=0;
for(u=j;u<=j+i-1;u++)
{
st=col(u);
if(!pos[st])sum++;
else if(!cont(j,j+i-1,pos[st]))goto tag;
}
dp[0][j][j+i-1]=dp[1][j][j+i-1]=fac[sum]*inv2[sum/2]%mod*invfac[sum/2]%mod;
for(u=j+1;u<j+i-1;u+=2)dp[1][j][j+i-1]=(dp[1][j][j+i-1]-dp[1][j][u]*dp[0][u+1][j+i-1]%mod+mod)%mod;
tag:;
}
}
for(i=1;i<=2*n;i++)
{
for(j=i+1;j<=2*n;j+=2)
{
p=j<2*n?j+1:1;
ans=(ans+dp[1][i][j]*dp[0][p][p+(2*n-(j-i+1)-1)]%mod)%mod;
}
}
printf("%lld\n",ans);
return 0;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll ans=0,n,i,j,u,k,x,y,p,sum,st,ok=0,pos[605],dp[2][1205][1205],fac[605],inv2[605],invfac[605];
const ll mod=1e9+7;
ll pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
ll col(ll a){return a>2*n?a-2*n:a;}
ll cont(ll a,ll b,ll c){return (c>=a&&c<=b)||(c+2*n>=a&&c+2*n<=b);}
int main()
{
scanf("%lld%lld",&n,&k);
fac[0]=1;dp[0][1][0]=1;
for(i=1;i<=2*n;i++)fac[i]=fac[i-1]*i%mod;
invfac[2*n]=pow(fac[2*n],mod-2);
for(i=2*n-1;i>=0;i--)invfac[i]=invfac[i+1]*(i+1)%mod;
inv2[2*n]=pow(pow(2LL,2*n),mod-2);
for(i=2*n-1;i>=0;i--)inv2[i]=inv2[i+1]*2%mod;
for(i=1;i<=k;i++)
{
scanf("%lld%lld",&x,&y);
pos[x]=y;pos[y]=x;
}
for(i=2;i<=2*n;i+=2)
{
for(j=1;j<=2*n;j++)
{
sum=0;
for(u=j;u<=j+i-1;u++)
{
st=col(u);
if(!pos[st])sum++;
else if(!cont(j,j+i-1,pos[st]))goto tag;
}
dp[0][j][j+i-1]=dp[1][j][j+i-1]=fac[sum]*inv2[sum/2]%mod*invfac[sum/2]%mod;
for(u=j+1;u<j+i-1;u+=2)dp[1][j][j+i-1]=(dp[1][j][j+i-1]-dp[1][j][u]*dp[0][u+1][j+i-1]%mod+mod)%mod;
tag:;
}
}
for(i=1;i<=2*n;i++)
{
for(j=i+1;j<=2*n;j+=2)
{
p=j<2*n?j+1:1;
ans=(ans+dp[1][i][j]*dp[0][p][p+(2*n-(j-i+1)-1)]%mod)%mod;
}
}
printf("%lld\n",ans);
return 0;
}
``` |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 610;
const int mod = 1e9 + 7;
long long dp[maxn][maxn] = {0}, g[maxn], h[maxn][maxn];
int ea[maxn], eb[maxn];
int n, m;
bool in(int l, int r, int x)
{
return l <= x && x <= r;
}
int get(int l, int r)
{
int cnt = 0;
for(int i = 1; i <= m; i ++)
{
bool asd1 = in(l, r, ea[i]), asd2 = in(l, r, eb[i]);
if(asd1 ^ asd2)
return -1;
if(asd1)
cnt ++;
}
return cnt * 2;
}
int main()
{
std::ios::sync_with_stdio(false);
cin >> n >> m;
n *= 2;
for(int i = 1; i <= m; i ++)
cin >> ea[i] >> eb[i];
g[0] = 1;
for(int i = 2; i <= n; i += 2)
g[i] = g[i - 2] * (i - 1) % mod;
for(int i = 1; i <= n; i ++)
for(int j = i; j <= n; j ++)
h[i][j] = get(i, j);
long long ans = 0;
for(int l = 2; l <= n; l += 2)
for(int i = 1; i + l - 1 <= n; i ++)
{
int j = i + l - 1;
//cout << i << " " << j << " : " << endl;
int c = h[i][j];
//cout << c << endl;
if(c != -1)
{
long long a = g[n - l - m * 2 + c];
dp[i][j] = g[l - c] % mod;
//cout << g[l - c] << " " << a << endl;
for(int k = i + 1; k < j; k += 2)
if(h[k + 1][j] != -1 && h[i][k] != -1)
{
(dp[i][j] += (mod - dp[i][k] * g[j - k - h[k + 1][j]] % mod)) %= mod;
//cout << k << " " << dp[i][k] << " " << j - k - h[k + 1][j] << " " << g[j - k - 1 - h[k + 1][j]] << endl;
//cout << dp[i][k] * g[j - k - 1 - h[k + 1][j]] % mod * a % mod << endl;
}
(ans += dp[i][j] * a) %= mod;
}
//cout << dp[i][j] << endl;
}
cout << ans << endl;
return 0;
} | ### Prompt
Generate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 610;
const int mod = 1e9 + 7;
long long dp[maxn][maxn] = {0}, g[maxn], h[maxn][maxn];
int ea[maxn], eb[maxn];
int n, m;
bool in(int l, int r, int x)
{
return l <= x && x <= r;
}
int get(int l, int r)
{
int cnt = 0;
for(int i = 1; i <= m; i ++)
{
bool asd1 = in(l, r, ea[i]), asd2 = in(l, r, eb[i]);
if(asd1 ^ asd2)
return -1;
if(asd1)
cnt ++;
}
return cnt * 2;
}
int main()
{
std::ios::sync_with_stdio(false);
cin >> n >> m;
n *= 2;
for(int i = 1; i <= m; i ++)
cin >> ea[i] >> eb[i];
g[0] = 1;
for(int i = 2; i <= n; i += 2)
g[i] = g[i - 2] * (i - 1) % mod;
for(int i = 1; i <= n; i ++)
for(int j = i; j <= n; j ++)
h[i][j] = get(i, j);
long long ans = 0;
for(int l = 2; l <= n; l += 2)
for(int i = 1; i + l - 1 <= n; i ++)
{
int j = i + l - 1;
//cout << i << " " << j << " : " << endl;
int c = h[i][j];
//cout << c << endl;
if(c != -1)
{
long long a = g[n - l - m * 2 + c];
dp[i][j] = g[l - c] % mod;
//cout << g[l - c] << " " << a << endl;
for(int k = i + 1; k < j; k += 2)
if(h[k + 1][j] != -1 && h[i][k] != -1)
{
(dp[i][j] += (mod - dp[i][k] * g[j - k - h[k + 1][j]] % mod)) %= mod;
//cout << k << " " << dp[i][k] << " " << j - k - h[k + 1][j] << " " << g[j - k - 1 - h[k + 1][j]] << endl;
//cout << dp[i][k] * g[j - k - 1 - h[k + 1][j]] % mod * a % mod << endl;
}
(ans += dp[i][j] * a) %= mod;
}
//cout << dp[i][j] << endl;
}
cout << ans << endl;
return 0;
}
``` |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
template<typename tn> void read(tn &a){
tn x=0,f=1; char c=' ';
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
a=x*f;
}
const int mod = 1e9+7;
ll fac[1100],ifac[1100],f[610][610],p[610];
int vis[610][610],n,m,match[610],s[610];
ll calc(int n){return fac[n]*ifac[n/2]%mod*p[n/2]%mod;}
ll fp(ll a,ll k){
ll ans=1;
for(;k;k>>=1,a=a*a%mod)
if(k&1) ans=a*ans%mod;
return ans;
}
ll solve(int l,int r){
if(l>r) return 1;
if(vis[l][r]) return f[l][r];
vis[l][r]=1;
for(int i=l;i<=r;i++)
if(match[i]&&(match[i]<l||match[i]>r)) return f[l][r]=0;
ll now=calc(s[r]-s[l-1]);
for(int i=l+1;i<r;i+=2){
now=(now-solve(l,i)*calc(s[r]-s[i]))%mod;
}
return f[l][r]=(now+mod)%mod;
}
int main(){
read(n);read(m);
for(int i=1;i<=m;i++){
int x,y;read(x);read(y);
match[x]=y;
match[y]=x;
}
for(int i=1;i<=2*n;i++)
s[i]=s[i-1]+(match[i]==0);
fac[0]=1;
for(int i=1;i<=2*n;i++)
fac[i]=fac[i-1]*i%mod;
ifac[2*n]=fp(fac[2*n],mod-2);
for(int i=2*n;i;i--)
ifac[i-1]=ifac[i]*i%mod;
for(int i=0;i<=2*n;i++)
p[i]=fp(fp(2,i),mod-2);
ll ans=0;
for(int i=1;i<=2*n;i++)
for(int j=i+1;j<=2*n;j+=2){
ans=(ans+calc(s[i-1]+s[2*n]-s[j])*solve(i,j))%mod;
}
cout<<ans<<'\n';
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define ll long long
using namespace std;
template<typename tn> void read(tn &a){
tn x=0,f=1; char c=' ';
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
a=x*f;
}
const int mod = 1e9+7;
ll fac[1100],ifac[1100],f[610][610],p[610];
int vis[610][610],n,m,match[610],s[610];
ll calc(int n){return fac[n]*ifac[n/2]%mod*p[n/2]%mod;}
ll fp(ll a,ll k){
ll ans=1;
for(;k;k>>=1,a=a*a%mod)
if(k&1) ans=a*ans%mod;
return ans;
}
ll solve(int l,int r){
if(l>r) return 1;
if(vis[l][r]) return f[l][r];
vis[l][r]=1;
for(int i=l;i<=r;i++)
if(match[i]&&(match[i]<l||match[i]>r)) return f[l][r]=0;
ll now=calc(s[r]-s[l-1]);
for(int i=l+1;i<r;i+=2){
now=(now-solve(l,i)*calc(s[r]-s[i]))%mod;
}
return f[l][r]=(now+mod)%mod;
}
int main(){
read(n);read(m);
for(int i=1;i<=m;i++){
int x,y;read(x);read(y);
match[x]=y;
match[y]=x;
}
for(int i=1;i<=2*n;i++)
s[i]=s[i-1]+(match[i]==0);
fac[0]=1;
for(int i=1;i<=2*n;i++)
fac[i]=fac[i-1]*i%mod;
ifac[2*n]=fp(fac[2*n],mod-2);
for(int i=2*n;i;i--)
ifac[i-1]=ifac[i]*i%mod;
for(int i=0;i<=2*n;i++)
p[i]=fp(fp(2,i),mod-2);
ll ans=0;
for(int i=1;i<=2*n;i++)
for(int j=i+1;j<=2*n;j+=2){
ans=(ans+calc(s[i-1]+s[2*n]-s[j])*solve(i,j))%mod;
}
cout<<ans<<'\n';
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double louble;
template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}
namespace ae86
{
const int bufl = 1<<15;
char buf[bufl],*s=buf,*t=buf;
inline int fetch()
{
if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
return *s++;
}
inline int ty()
{
int a=0,b=1,c=fetch();
while(!isdigit(c))b^=c=='-',c=fetch();
while(isdigit(c))a=a*10+c-48,c=fetch();
return b?a:-a;
}
}
using ae86::ty;
const int _ = 307 , __ = _ << 1 , mo = 1000000007;
int n,nn,m,e[__]={0};
int oc[__]={0};
inline int frees(int l,int r){if(l>r)return 0;return r-l+1-(oc[r]-oc[l-1]);}
lint f[__][__],g[__]={0};
int main()
{
n=ty(),m=ty(),nn=n+n;
for(int i=1,a,b;i<=m;i++)a=ty(),b=ty(),e[a]=b,e[b]=a;
for(int i=1;i<=nn;i++)oc[i]=oc[i-1]+(e[i]>0);
g[0]=1;
for(int i=2;i<=nn;i+=2)g[i]=g[i-2]*(i-1)%mo;
lint ans=0;
for(int i=1;i<=nn;i++)
for(int j=i;j<=nn;j++)
{
int len=j-i+1;
if(len&1)continue;
for(int k=i;k<=j;k++)if(e[k] && (e[k]<i || e[k]>j))goto fail;
f[i][j]=g[frees(i,j)];
for(int k=i+1;k<j;k++)f[i][j]=(f[i][j]-f[i][k]*g[frees(k+1,j)]%mo+mo)%mo;
ans=(ans+g[frees(1,i-1)+frees(j+1,nn)]*f[i][j]%mo)%mo;
fail:;
}
printf("%lld\n",ans);
return 0;
}
| ### Prompt
Please formulate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double louble;
template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}
namespace ae86
{
const int bufl = 1<<15;
char buf[bufl],*s=buf,*t=buf;
inline int fetch()
{
if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
return *s++;
}
inline int ty()
{
int a=0,b=1,c=fetch();
while(!isdigit(c))b^=c=='-',c=fetch();
while(isdigit(c))a=a*10+c-48,c=fetch();
return b?a:-a;
}
}
using ae86::ty;
const int _ = 307 , __ = _ << 1 , mo = 1000000007;
int n,nn,m,e[__]={0};
int oc[__]={0};
inline int frees(int l,int r){if(l>r)return 0;return r-l+1-(oc[r]-oc[l-1]);}
lint f[__][__],g[__]={0};
int main()
{
n=ty(),m=ty(),nn=n+n;
for(int i=1,a,b;i<=m;i++)a=ty(),b=ty(),e[a]=b,e[b]=a;
for(int i=1;i<=nn;i++)oc[i]=oc[i-1]+(e[i]>0);
g[0]=1;
for(int i=2;i<=nn;i+=2)g[i]=g[i-2]*(i-1)%mo;
lint ans=0;
for(int i=1;i<=nn;i++)
for(int j=i;j<=nn;j++)
{
int len=j-i+1;
if(len&1)continue;
for(int k=i;k<=j;k++)if(e[k] && (e[k]<i || e[k]>j))goto fail;
f[i][j]=g[frees(i,j)];
for(int k=i+1;k<j;k++)f[i][j]=(f[i][j]-f[i][k]*g[frees(k+1,j)]%mo+mo)%mo;
ans=(ans+g[frees(1,i-1)+frees(j+1,nn)]*f[i][j]%mo)%mo;
fail:;
}
printf("%lld\n",ans);
return 0;
}
``` |
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 310, P = 1000000007;
int n;
int match[N * 2], pre[N], sum[N * 2];
bool ab[N * 2][N * 2];
int con[N * 2][N * 2], spec[N * 2][N * 2];
int main() {
int k;
scanf("%d%d", &n, &k);
while (k--) {
int a, b;
scanf("%d%d", &a, &b);
match[a] = b;
match[b] = a;
}
pre[0] = 1;
for (int i = 1; i <= n; ++i)
pre[i] = pre[i - 1] * (ll)(i * 2 - 1) % P;
for (int l = 1; l <= n * 2; ++l) {
int myn = n * 2, mex = 1;
for (int r = l; r <= n * 2; ++r) {
if (match[r]) {
myn = min(myn, match[r]);
mex = max(mex, match[r]);
}
ab[l][r] = myn >= l && mex <= r;
}
}
for (int i = 1; i <= n * 2; ++i)
sum[i] = sum[i - 1] + bool(match[i]);
int ans = 0;
for (int len = 2; len <= n * 2; len += 2)
for (int l = 1; l + len - 1 <= n * 2; ++l) {
int r = l + len - 1;
if (!ab[l][r])
continue;
con[l][r] = pre[(len - sum[r] + sum[l - 1]) >> 1];
spec[l][r] = con[l][r];
for (int i = l + 1; i < r; i += 2)
spec[l][r] = (P + spec[l][r] - spec[l][i] * (ll)con[i + 1][r] % P) % P;
ans = (ans + spec[l][r] * (ll)pre[(n * 2 - len - sum[l - 1] - sum[n * 2] + sum[r]) >> 1]) % P;
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Generate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 310, P = 1000000007;
int n;
int match[N * 2], pre[N], sum[N * 2];
bool ab[N * 2][N * 2];
int con[N * 2][N * 2], spec[N * 2][N * 2];
int main() {
int k;
scanf("%d%d", &n, &k);
while (k--) {
int a, b;
scanf("%d%d", &a, &b);
match[a] = b;
match[b] = a;
}
pre[0] = 1;
for (int i = 1; i <= n; ++i)
pre[i] = pre[i - 1] * (ll)(i * 2 - 1) % P;
for (int l = 1; l <= n * 2; ++l) {
int myn = n * 2, mex = 1;
for (int r = l; r <= n * 2; ++r) {
if (match[r]) {
myn = min(myn, match[r]);
mex = max(mex, match[r]);
}
ab[l][r] = myn >= l && mex <= r;
}
}
for (int i = 1; i <= n * 2; ++i)
sum[i] = sum[i - 1] + bool(match[i]);
int ans = 0;
for (int len = 2; len <= n * 2; len += 2)
for (int l = 1; l + len - 1 <= n * 2; ++l) {
int r = l + len - 1;
if (!ab[l][r])
continue;
con[l][r] = pre[(len - sum[r] + sum[l - 1]) >> 1];
spec[l][r] = con[l][r];
for (int i = l + 1; i < r; i += 2)
spec[l][r] = (P + spec[l][r] - spec[l][i] * (ll)con[i + 1][r] % P) % P;
ans = (ans + spec[l][r] * (ll)pre[(n * 2 - len - sum[l - 1] - sum[n * 2] + sum[r]) >> 1]) % P;
}
printf("%d\n", ans);
return 0;
}
``` |
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(ll i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
#define N 610
ll n,k,a[N],b[N];
bool c[N];
ll g[N][N],f[N][N];
ll F(ll x){
if(x%2==1)return 0;
ll res=1;
for(x-=1;x>0;x-=2)res=res*x%mod;
return res;
}
ll G(ll i,ll j,ll x){
if(i<=j)return i<=x&&x<=j;
return x<=j||i<=x;
}
int main(){
cin>>n>>k;n*=2;
lol(i,n)c[i]=0;
lol(i,k){
cin>>a[i]>>b[i];a[i]--,b[i]--;
c[a[i]]=c[b[i]]=1;
}
lol(i,n)lol(j,n){
ll cnt=0;
for(ll r=i;r<=(i<=j?j:n-1);r++)cnt+=not c[r];
for(ll r=0;i>j&&r<=j;r++)cnt+=not c[r];
g[i][j]=F(cnt);
}
ll ans=0;
lol(len,n)lol(i,n-len){
ll j=i+len;
f[i][j]=0;
bool ok=1;
lol(r,k)ok&=not(G(i,j,a[r]) xor G(i,j,b[r]));
if(!ok)continue;
ll res=g[i][j];
for(ll r=i;r<j;r++)res=(res-f[i][r]*g[(r+1)%n][j])%mod;
f[i][j]=res;
if(len==n-1){ans=(ans+f[i][j])%mod;}
else ans=(ans+f[i][j]*g[(j+1)%n][(i-1+n)%n])%mod;
}
if(ans<0)ans+=mod;cout<<ans<<endl;
return 0;
}
| ### Prompt
Develop a solution in cpp to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(ll i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
#define N 610
ll n,k,a[N],b[N];
bool c[N];
ll g[N][N],f[N][N];
ll F(ll x){
if(x%2==1)return 0;
ll res=1;
for(x-=1;x>0;x-=2)res=res*x%mod;
return res;
}
ll G(ll i,ll j,ll x){
if(i<=j)return i<=x&&x<=j;
return x<=j||i<=x;
}
int main(){
cin>>n>>k;n*=2;
lol(i,n)c[i]=0;
lol(i,k){
cin>>a[i]>>b[i];a[i]--,b[i]--;
c[a[i]]=c[b[i]]=1;
}
lol(i,n)lol(j,n){
ll cnt=0;
for(ll r=i;r<=(i<=j?j:n-1);r++)cnt+=not c[r];
for(ll r=0;i>j&&r<=j;r++)cnt+=not c[r];
g[i][j]=F(cnt);
}
ll ans=0;
lol(len,n)lol(i,n-len){
ll j=i+len;
f[i][j]=0;
bool ok=1;
lol(r,k)ok&=not(G(i,j,a[r]) xor G(i,j,b[r]));
if(!ok)continue;
ll res=g[i][j];
for(ll r=i;r<j;r++)res=(res-f[i][r]*g[(r+1)%n][j])%mod;
f[i][j]=res;
if(len==n-1){ans=(ans+f[i][j])%mod;}
else ans=(ans+f[i][j]*g[(j+1)%n][(i-1+n)%n])%mod;
}
if(ans<0)ans+=mod;cout<<ans<<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
constexpr int P = 1000000007;
void increase(int &a, int b) {(a += b) >= P ? a -= P : 0;}
void decrease(int &a, int b) {(a -= b) < 0 ? a += P : 0;}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> match(2 * n, -1), g(2 * n + 1), s(2 * n + 1, 1);
s[0] = 0;
g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
g[i] = 1LL * g[i - 2] * (i - 1) % P;
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
match[x] = y;
match[y] = x;
s[x + 1] = s[y + 1] = 0;
}
for (int i = 1; i <= 2 * n; ++i)
s[i] += s[i - 1];
int ans = 0;
vector<int> dp(2 * n);
for (int i = 0; i < 2 * n; ++i) {
fill(dp.begin(), dp.end(), 0);
for (int j = i, mx = -1; j < 2 * n; ++j) {
if (match[j] != -1) {
if (match[j] < i)
break;
mx = max(mx, match[j]);
}
if (j < mx)
continue;
int x = s[j + 1] - s[i];
int y = 2 * (n - k) - x;
if (x % 2 == 1)
continue;
dp[j] = g[x];
for (int k = i; k < j; ++k)
decrease(dp[j], 1LL * dp[k] * g[s[j + 1] - s[k + 1]] % P);
increase(ans, 1LL * dp[j] * g[y] % P);
}
}
cout << ans << endl;
return 0;
} | ### Prompt
Your task is to create a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int P = 1000000007;
void increase(int &a, int b) {(a += b) >= P ? a -= P : 0;}
void decrease(int &a, int b) {(a -= b) < 0 ? a += P : 0;}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> match(2 * n, -1), g(2 * n + 1), s(2 * n + 1, 1);
s[0] = 0;
g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
g[i] = 1LL * g[i - 2] * (i - 1) % P;
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
match[x] = y;
match[y] = x;
s[x + 1] = s[y + 1] = 0;
}
for (int i = 1; i <= 2 * n; ++i)
s[i] += s[i - 1];
int ans = 0;
vector<int> dp(2 * n);
for (int i = 0; i < 2 * n; ++i) {
fill(dp.begin(), dp.end(), 0);
for (int j = i, mx = -1; j < 2 * n; ++j) {
if (match[j] != -1) {
if (match[j] < i)
break;
mx = max(mx, match[j]);
}
if (j < mx)
continue;
int x = s[j + 1] - s[i];
int y = 2 * (n - k) - x;
if (x % 2 == 1)
continue;
dp[j] = g[x];
for (int k = i; k < j; ++k)
decrease(dp[j], 1LL * dp[k] * g[s[j + 1] - s[k + 1]] % P);
increase(ans, 1LL * dp[j] * g[y] % P);
}
}
cout << ans << endl;
return 0;
}
``` |
#include <algorithm>
#include <cstdio>
#include <cstring>
#define w(i, j) (c[j] - c[(i)-1])
typedef long long L;
const int N = 6e2 + 26, P = 1e9 + 7;
struct mod_t {
static int norm(int x) { return x + (x >> 31 & P); }
static void norm2(int& x) { x += x >> 31 & P; }
int x;
mod_t() {}
mod_t(int v) : x(v) {}
mod_t(long long v) : x(v) {}
mod_t(char v) : x(v) {}
mod_t operator+(const mod_t& rhs) const { return norm(x + rhs.x - P); }
mod_t operator-(const mod_t& rhs) const { return norm(x - rhs.x); }
mod_t operator*(const mod_t& rhs) const { return (L)x * rhs.x % P; }
void operator+=(const mod_t& rhs) { norm2(x += rhs.x - P); }
void operator-=(const mod_t& rhs) { norm2(x -= rhs.x); }
void operator*=(const mod_t& rhs) { x = (L)x * rhs.x % P; }
};
mod_t f[N][N], g[N], ans;
int p[N], c[N];
int n, k;
int h(int l, int r) { return (r - l + 1) - w(l, r); }
int main() {
scanf("%d%d", &n, &k);
for (int i = 1, x, y; i <= k; i++) {
scanf("%d%d", &x, &y);
x[p] = y, y[p] = x, x[c] = y[c] = 1;
}
g[0] = g[1] = 1;
for (int i = 2; i <= 2 * n; i++) g[i] = g[i - 2] * (i - 1);
for (int i = 1; i <= 2 * n; i++) c[i] += c[i - 1];
for (int l = 1; l <= 2 * n; l++)
for (int r = l + 1; r <= 2 * n; r++)
if ((l ^ r) & 1) {
for (int x = l; x <= r; x++)
if (x[p] && (x[p] < l || x[p] > r)) goto end;
f[l][r] = g[h(l, r)];
for (int x = l + 1; x < r; x++) f[l][r] -= f[l][x] * g[h(x + 1, r)];
ans += f[l][r] * g[(n << 1) - (r - l + 1) - ((k << 1) - w(l, r))];
end:;
}
printf("%d", ans.x);
} | ### Prompt
In Cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <algorithm>
#include <cstdio>
#include <cstring>
#define w(i, j) (c[j] - c[(i)-1])
typedef long long L;
const int N = 6e2 + 26, P = 1e9 + 7;
struct mod_t {
static int norm(int x) { return x + (x >> 31 & P); }
static void norm2(int& x) { x += x >> 31 & P; }
int x;
mod_t() {}
mod_t(int v) : x(v) {}
mod_t(long long v) : x(v) {}
mod_t(char v) : x(v) {}
mod_t operator+(const mod_t& rhs) const { return norm(x + rhs.x - P); }
mod_t operator-(const mod_t& rhs) const { return norm(x - rhs.x); }
mod_t operator*(const mod_t& rhs) const { return (L)x * rhs.x % P; }
void operator+=(const mod_t& rhs) { norm2(x += rhs.x - P); }
void operator-=(const mod_t& rhs) { norm2(x -= rhs.x); }
void operator*=(const mod_t& rhs) { x = (L)x * rhs.x % P; }
};
mod_t f[N][N], g[N], ans;
int p[N], c[N];
int n, k;
int h(int l, int r) { return (r - l + 1) - w(l, r); }
int main() {
scanf("%d%d", &n, &k);
for (int i = 1, x, y; i <= k; i++) {
scanf("%d%d", &x, &y);
x[p] = y, y[p] = x, x[c] = y[c] = 1;
}
g[0] = g[1] = 1;
for (int i = 2; i <= 2 * n; i++) g[i] = g[i - 2] * (i - 1);
for (int i = 1; i <= 2 * n; i++) c[i] += c[i - 1];
for (int l = 1; l <= 2 * n; l++)
for (int r = l + 1; r <= 2 * n; r++)
if ((l ^ r) & 1) {
for (int x = l; x <= r; x++)
if (x[p] && (x[p] < l || x[p] > r)) goto end;
f[l][r] = g[h(l, r)];
for (int x = l + 1; x < r; x++) f[l][r] -= f[l][x] * g[h(x + 1, r)];
ans += f[l][r] * g[(n << 1) - (r - l + 1) - ((k << 1) - w(l, r))];
end:;
}
printf("%d", ans.x);
}
``` |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long LL;
using namespace std;
const int maxn = 610, mod = 1e9 + 7;
int read()
{
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) { if(c == '-') f = 0; c = getchar(); }
while(isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
return f ? x : -x;
}
int g[maxn], cnt[maxn][maxn], dp[maxn][maxn], a[maxn];
int main()
{
int n = read() * 2, K = read();
for(int i = 1; i <= K; ++i)
{
int x = read(), y = read();
a[x] = y; a[y] = x;
}
g[0] = 1;
for(int i = 2; i <= n; i += 2) g[i] = (LL)g[i - 2] * (i - 1) % mod;
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j)
cnt[i][j] = cnt[i][j - 1] + (a[j] ? 0 : 1);
int ans = 0;
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j)
{
if((j - i + 1) & 1) continue;
for(int k = i; k <= j; ++k)
if(a[k] && (a[k] < i || a[k] > j)) goto nxt;
dp[i][j] = g[cnt[i][j]];
for(int k = i; k < j; ++k)
(dp[i][j] -= (LL)dp[i][k] * g[cnt[k + 1][j]] % mod) %= mod;
(ans += (LL)dp[i][j] * g[cnt[1][i - 1] + cnt[j + 1][n]] % mod) %= mod;
nxt:;
}
cout << ans << endl;
return 0;
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long LL;
using namespace std;
const int maxn = 610, mod = 1e9 + 7;
int read()
{
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) { if(c == '-') f = 0; c = getchar(); }
while(isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
return f ? x : -x;
}
int g[maxn], cnt[maxn][maxn], dp[maxn][maxn], a[maxn];
int main()
{
int n = read() * 2, K = read();
for(int i = 1; i <= K; ++i)
{
int x = read(), y = read();
a[x] = y; a[y] = x;
}
g[0] = 1;
for(int i = 2; i <= n; i += 2) g[i] = (LL)g[i - 2] * (i - 1) % mod;
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j)
cnt[i][j] = cnt[i][j - 1] + (a[j] ? 0 : 1);
int ans = 0;
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j)
{
if((j - i + 1) & 1) continue;
for(int k = i; k <= j; ++k)
if(a[k] && (a[k] < i || a[k] > j)) goto nxt;
dp[i][j] = g[cnt[i][j]];
for(int k = i; k < j; ++k)
(dp[i][j] -= (LL)dp[i][k] * g[cnt[k + 1][j]] % mod) %= mod;
(ans += (LL)dp[i][j] * g[cnt[1][i - 1] + cnt[j + 1][n]] % mod) %= mod;
nxt:;
}
cout << ans << endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=6e2+10;
const int mod=1e9+7;
int c[maxn][maxn],dp[maxn][maxn],tmp[maxn],g[maxn];
int main() {
int n,K,a,b; scanf("%d%d",&n,&K);
for(int i=1;i<=K;i++) {
scanf("%d%d",&a,&b);
g[a]=b,g[b]=a;
} n<<=1;
for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) c[i][j]=c[i][j-1]+(g[j]==0);
tmp[0]=1;
for(int i=2;i<=n;i+=2) tmp[i]=1ll*tmp[i-2]*(i-1)%mod;
for(int i=1;i<=n;i++) {
for(int j=i;j<=n;j++) {
bool flag=0;
for(int k=i;k<=j;k++) {
flag|=(g[k]&&(g[k]<i||g[k]>j));
if (flag) break;
}
if (flag) continue;
dp[i][j]=tmp[c[i][j]];
for(int k=i+1;k<=j-1;k++) {
(dp[i][j]+=mod-1ll*dp[i][k]*tmp[c[k+1][j]]%mod)%=mod;
}
}
} ll ret=0;
for(int i=1;i<=n;i++) {
for(int j=i;j<=n;j++) {
(ret+=1ll*dp[i][j]*tmp[n-2*K-c[i][j]]%mod)%=mod;
}
}
printf("%lld\n",(ret%mod+mod)%mod);
return 0;
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=6e2+10;
const int mod=1e9+7;
int c[maxn][maxn],dp[maxn][maxn],tmp[maxn],g[maxn];
int main() {
int n,K,a,b; scanf("%d%d",&n,&K);
for(int i=1;i<=K;i++) {
scanf("%d%d",&a,&b);
g[a]=b,g[b]=a;
} n<<=1;
for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) c[i][j]=c[i][j-1]+(g[j]==0);
tmp[0]=1;
for(int i=2;i<=n;i+=2) tmp[i]=1ll*tmp[i-2]*(i-1)%mod;
for(int i=1;i<=n;i++) {
for(int j=i;j<=n;j++) {
bool flag=0;
for(int k=i;k<=j;k++) {
flag|=(g[k]&&(g[k]<i||g[k]>j));
if (flag) break;
}
if (flag) continue;
dp[i][j]=tmp[c[i][j]];
for(int k=i+1;k<=j-1;k++) {
(dp[i][j]+=mod-1ll*dp[i][k]*tmp[c[k+1][j]]%mod)%=mod;
}
}
} ll ret=0;
for(int i=1;i<=n;i++) {
for(int j=i;j<=n;j++) {
(ret+=1ll*dp[i][j]*tmp[n-2*K-c[i][j]]%mod)%=mod;
}
}
printf("%lld\n",(ret%mod+mod)%mod);
return 0;
}
``` |
#include <bits/stdc++.h>
using std::cerr;
using std::endl;
#define int long long
const int N = 650, P = 1e9 + 7;
int n, K, f[N][N], g[N], mat[N], ans;
signed main() {
// freopen("data", "r", stdin);
std::cin >> n >> K, n *= 2;
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = g[i - 2] * (i - 1) % P;
for (int i = 1; i <= K; ++i) {
int x, y; std::cin >> x >> y;
mat[x] = y, mat[y] = x;
}
for (int l = 1; l <= n; ++l) {
for (int r = l + 1; r <= n; r += 2) {
int ok = 1;
for (int i = l; i <= r; ++i)
if (mat[i] && (mat[i] < l || mat[i] > r)) {
ok = 0; break;
}
if (!ok) continue;
int cnt = r - l + 1;
for (int i = l; i <= r; ++i)
cnt -= !!mat[i];
f[l][r] = g[cnt];
for (int i = r, tot = 0; i > l; --i) {
tot += !mat[i];
f[l][r] = (f[l][r] - f[l][i - 1] * g[tot]) % P;
}
ans = (ans + f[l][r] * g[n - K * 2 - cnt]) % P;
}
}
ans = (ans % P + P) % P;
std::cout << ans << std::endl;
return 0;
}
| ### Prompt
Your task is to create a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using std::cerr;
using std::endl;
#define int long long
const int N = 650, P = 1e9 + 7;
int n, K, f[N][N], g[N], mat[N], ans;
signed main() {
// freopen("data", "r", stdin);
std::cin >> n >> K, n *= 2;
g[0] = 1;
for (int i = 2; i <= n; i += 2)
g[i] = g[i - 2] * (i - 1) % P;
for (int i = 1; i <= K; ++i) {
int x, y; std::cin >> x >> y;
mat[x] = y, mat[y] = x;
}
for (int l = 1; l <= n; ++l) {
for (int r = l + 1; r <= n; r += 2) {
int ok = 1;
for (int i = l; i <= r; ++i)
if (mat[i] && (mat[i] < l || mat[i] > r)) {
ok = 0; break;
}
if (!ok) continue;
int cnt = r - l + 1;
for (int i = l; i <= r; ++i)
cnt -= !!mat[i];
f[l][r] = g[cnt];
for (int i = r, tot = 0; i > l; --i) {
tot += !mat[i];
f[l][r] = (f[l][r] - f[l][i - 1] * g[tot]) % P;
}
ans = (ans + f[l][r] * g[n - K * 2 - cnt]) % P;
}
}
ans = (ans % P + P) % P;
std::cout << ans << std::endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define MN 600
#define MOD 1000000007
int n,m,F[MN+5],R[MN+5],p[MN+5],u[MN+5][MN+5],s[MN+5][MN+5],f[MN+5];
inline int inv(int x)
{
int r=1,y=MOD-2;
for(;y;y>>=1,x=1LL*x*x%MOD)if(y&1)r=1LL*r*x%MOD;
return r;
}
inline void rw(int&x,int y){if((x+=y)>=MOD)x-=MOD;}
inline int cal(int x){return x&1?0:1LL*F[x]*R[x/2]%MOD*p[x/2]%MOD;}
inline int cal(int l,int r)
{
if((r+1)%n!=l&&s[l][r]+s[(r+1)%n][(l+n-1)%n]<m)return 0;
return cal((r+n-l)%n+1-2*s[l][r]);
}
inline int C(int n,int m){return 1LL*F[n]*R[m]%MOD*R[n-m]%MOD;}
int main()
{
int i,j,k,l,ans=0;
scanf("%d%d",&n,&m);n*=2;
for(F[0]=p[0]=i=1;i<=n;++i)F[i]=1LL*F[i-1]*i%MOD,p[i]=1LL*p[i-1]*(MOD+1)/2%MOD;
for(R[i=n]=inv(F[n]);i--;)R[i]=1LL*R[i+1]*(i+1)%MOD;
for(i=1;i<=m;++i)scanf("%d%d",&j,&k),--j,--k,u[j][k]=u[k][j]=1;
for(i=0;i<n;++i)for(j=0;j<n;++j)
{
s[i][(i+j)%n]=j?s[i][(i+j-1)%n]:0;
for(k=0;k<j;++k)s[i][(i+j)%n]+=u[(i+j)%n][(i+k)%n];
}
for(i=0;i<n;++i)
{
memset(f,0,sizeof(f));
for(j=0;j<=i;++j)
{
rw(f[j],cal(i,(j+n-1)%n));
for(k=j;k<i;++k)rw(f[k+1],(MOD-1LL*f[j]*cal(j,k)%MOD)%MOD);
}
ans=(ans+f[i])%MOD;
}
printf("%d",(1LL*(n+1)*cal(0,n-1)+MOD-ans)%MOD);
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
#define MN 600
#define MOD 1000000007
int n,m,F[MN+5],R[MN+5],p[MN+5],u[MN+5][MN+5],s[MN+5][MN+5],f[MN+5];
inline int inv(int x)
{
int r=1,y=MOD-2;
for(;y;y>>=1,x=1LL*x*x%MOD)if(y&1)r=1LL*r*x%MOD;
return r;
}
inline void rw(int&x,int y){if((x+=y)>=MOD)x-=MOD;}
inline int cal(int x){return x&1?0:1LL*F[x]*R[x/2]%MOD*p[x/2]%MOD;}
inline int cal(int l,int r)
{
if((r+1)%n!=l&&s[l][r]+s[(r+1)%n][(l+n-1)%n]<m)return 0;
return cal((r+n-l)%n+1-2*s[l][r]);
}
inline int C(int n,int m){return 1LL*F[n]*R[m]%MOD*R[n-m]%MOD;}
int main()
{
int i,j,k,l,ans=0;
scanf("%d%d",&n,&m);n*=2;
for(F[0]=p[0]=i=1;i<=n;++i)F[i]=1LL*F[i-1]*i%MOD,p[i]=1LL*p[i-1]*(MOD+1)/2%MOD;
for(R[i=n]=inv(F[n]);i--;)R[i]=1LL*R[i+1]*(i+1)%MOD;
for(i=1;i<=m;++i)scanf("%d%d",&j,&k),--j,--k,u[j][k]=u[k][j]=1;
for(i=0;i<n;++i)for(j=0;j<n;++j)
{
s[i][(i+j)%n]=j?s[i][(i+j-1)%n]:0;
for(k=0;k<j;++k)s[i][(i+j)%n]+=u[(i+j)%n][(i+k)%n];
}
for(i=0;i<n;++i)
{
memset(f,0,sizeof(f));
for(j=0;j<=i;++j)
{
rw(f[j],cal(i,(j+n-1)%n));
for(k=j;k<i;++k)rw(f[k+1],(MOD-1LL*f[j]*cal(j,k)%MOD)%MOD);
}
ans=(ans+f[i])%MOD;
}
printf("%d",(1LL*(n+1)*cal(0,n-1)+MOD-ans)%MOD);
}
``` |
#include <iostream>
#include <cstdio>
#define maxn 1003
#define mod 1000000007
using namespace std;
int pos[maxn], g[maxn], s[maxn], f[maxn][maxn];
void upd(int &x, int y){
x += y;
if (x >= mod)
x -= mod;
}
int main(){
int n, k;
cin >> n >> k;
for (int i = 1; i <= k; ++ i){
int u, v;
cin >> u >> v;
pos[u] = v;
pos[v] = u;
}
n <<= 1;
for (int i = 1; i <= n; ++ i)
s[i] = s[i - 1] + (pos[i] == 0);
g[0] = 1;
for (int i = 2; i <= n; ++ i)
g[i] = 1LL * g[i - 2] * (i - 1) % mod;
int ans = 0;
for (int i = n; i; -- i)
for (int j = i; j <= n; ++ j){
bool flag = false;
for (int k = i; k <= j; ++ k)
if (pos[k] && (pos[k] < i || pos[k] > j)){
flag = true;
break;
}
if (flag)
continue;
f[i][j] = g[s[j] - s[i - 1]];
for (int k = i; k < j; ++ k)
upd(f[i][j], mod - 1LL * f[i][k] * g[s[j] - s[k]] % mod);
upd(ans, 1LL * f[i][j] * g[s[i - 1] + (s[n] - s[j])] % mod);
}
cout << ans << endl;
}
| ### Prompt
Please create a solution in Cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <iostream>
#include <cstdio>
#define maxn 1003
#define mod 1000000007
using namespace std;
int pos[maxn], g[maxn], s[maxn], f[maxn][maxn];
void upd(int &x, int y){
x += y;
if (x >= mod)
x -= mod;
}
int main(){
int n, k;
cin >> n >> k;
for (int i = 1; i <= k; ++ i){
int u, v;
cin >> u >> v;
pos[u] = v;
pos[v] = u;
}
n <<= 1;
for (int i = 1; i <= n; ++ i)
s[i] = s[i - 1] + (pos[i] == 0);
g[0] = 1;
for (int i = 2; i <= n; ++ i)
g[i] = 1LL * g[i - 2] * (i - 1) % mod;
int ans = 0;
for (int i = n; i; -- i)
for (int j = i; j <= n; ++ j){
bool flag = false;
for (int k = i; k <= j; ++ k)
if (pos[k] && (pos[k] < i || pos[k] > j)){
flag = true;
break;
}
if (flag)
continue;
f[i][j] = g[s[j] - s[i - 1]];
for (int k = i; k < j; ++ k)
upd(f[i][j], mod - 1LL * f[i][k] * g[s[j] - s[k]] % mod);
upd(ans, 1LL * f[i][j] * g[s[i - 1] + (s[n] - s[j])] % mod);
}
cout << ans << endl;
}
``` |
#include<cstdio>
#include<cctype>
const int maxn=610,mod=1000000007;
int n,m;
int r[maxn],s[maxn];
int f[maxn][maxn],g[maxn];
int w;
int In() {
register char c;
for(;c=getchar(),c!='-'&&!isdigit(c););
register bool f=c=='-';
register int s=f?0:c-'0';
for(;c=getchar(),isdigit(c);)s=s*10+c-'0';
return f?-s:s;
}
int main() {
n=In(),m=In();
for(register int i=1,x,y;i<=m;++i)x=In(),y=In(),r[x]=y,r[y]=x;
g[0]=1;
for(register int i=2;i<=n*2;i+=2)g[i]=(i-1ll)*g[i-2]%mod,s[i]=(s[i-1]=s[i-2]+!r[i-1])+!r[i];
for(register int i=1;i<=n*2;++i)for(register int j=i+1;j<=n*2;j+=2) {
register bool tg=0;
for(register int k=1;k<i;++k)if(r[k]>=i&&r[k]<=j) tg=1;
for(register int k=n*2;k>j;--k)if(r[k]>=i&&r[k]<=j) tg=1;
if(!tg) {
f[i][j]=g[s[j]-s[i-1]];
for(register int k=i+1;k<j;k+=2)f[i][j]=(f[i][j]-1ll*f[i][k]*g[s[j]-s[k]])%mod;
w=(w+1ll*f[i][j]*g[s[i-1]+s[n*2]-s[j]])%mod;
}
}
printf("%d\n",(w+mod)%mod);
return 0;
} | ### Prompt
In cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#include<cctype>
const int maxn=610,mod=1000000007;
int n,m;
int r[maxn],s[maxn];
int f[maxn][maxn],g[maxn];
int w;
int In() {
register char c;
for(;c=getchar(),c!='-'&&!isdigit(c););
register bool f=c=='-';
register int s=f?0:c-'0';
for(;c=getchar(),isdigit(c);)s=s*10+c-'0';
return f?-s:s;
}
int main() {
n=In(),m=In();
for(register int i=1,x,y;i<=m;++i)x=In(),y=In(),r[x]=y,r[y]=x;
g[0]=1;
for(register int i=2;i<=n*2;i+=2)g[i]=(i-1ll)*g[i-2]%mod,s[i]=(s[i-1]=s[i-2]+!r[i-1])+!r[i];
for(register int i=1;i<=n*2;++i)for(register int j=i+1;j<=n*2;j+=2) {
register bool tg=0;
for(register int k=1;k<i;++k)if(r[k]>=i&&r[k]<=j) tg=1;
for(register int k=n*2;k>j;--k)if(r[k]>=i&&r[k]<=j) tg=1;
if(!tg) {
f[i][j]=g[s[j]-s[i-1]];
for(register int k=i+1;k<j;k+=2)f[i][j]=(f[i][j]-1ll*f[i][k]*g[s[j]-s[k]])%mod;
w=(w+1ll*f[i][j]*g[s[i-1]+s[n*2]-s[j]])%mod;
}
}
printf("%d\n",(w+mod)%mod);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 607;
const ll MOD = 1e9 + 7;
int N, K, match[MAX_N], match_mn[MAX_N][MAX_N], match_mx[MAX_N][MAX_N], match_cnt[MAX_N];
ll fac2[MAX_N], F[MAX_N][MAX_N], ans;
ll ways(int l, int r) {
if (match_mn[l][r] < l || match_mx[l][r] > r) return 0;
return fac2[r - l + 1 - (match_cnt[r] - match_cnt[l - 1])];
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= K; i++) {
int a, b; scanf("%d%d", &a, &b);
match[a] = b; match[b] = a;
}
for (int i = 1; i <= 2 * N; i++) {
match_mn[i][i - 1] = 2 * N;
match_mx[i][i - 1] = 1;
for (int j = i; j <= 2 * N; j++) {
if (match[j]) {
match_mn[i][j] = min(match_mn[i][j - 1], match[j]);
match_mx[i][j] = max(match_mx[i][j - 1], match[j]);
} else {
match_mn[i][j] = match_mn[i][j - 1];
match_mx[i][j] = match_mx[i][j - 1];
}
}
}
for (int i = 1; i <= 2 * N; i++) match_cnt[i] = match_cnt[i - 1] + (match[i] ? 1 : 0);
fac2[0] = 1;
for (int i = 2; i <= 2 * N; i += 2) fac2[i] = fac2[i - 2] * (i - 1) % MOD;
for (int len = 2; len <= 2 * N; len++) {
for (int i = 1; i + len - 1 <= 2 * N; i++) {
int j = i + len - 1;
F[i][j] = ways(i, j);
for (int k = i + 1; k < j; k += 2) {
(F[i][j] += MOD - F[i][k] * ways(k + 1, j) % MOD) %= MOD;
}
(ans += F[i][j] * fac2[2 * N - (j - i + 1) - (2 * K - (match_cnt[j] - match_cnt[i - 1]))]) %= MOD;
}
}
printf("%lld\n", ans);
}
| ### Prompt
Develop a solution in CPP to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 607;
const ll MOD = 1e9 + 7;
int N, K, match[MAX_N], match_mn[MAX_N][MAX_N], match_mx[MAX_N][MAX_N], match_cnt[MAX_N];
ll fac2[MAX_N], F[MAX_N][MAX_N], ans;
ll ways(int l, int r) {
if (match_mn[l][r] < l || match_mx[l][r] > r) return 0;
return fac2[r - l + 1 - (match_cnt[r] - match_cnt[l - 1])];
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= K; i++) {
int a, b; scanf("%d%d", &a, &b);
match[a] = b; match[b] = a;
}
for (int i = 1; i <= 2 * N; i++) {
match_mn[i][i - 1] = 2 * N;
match_mx[i][i - 1] = 1;
for (int j = i; j <= 2 * N; j++) {
if (match[j]) {
match_mn[i][j] = min(match_mn[i][j - 1], match[j]);
match_mx[i][j] = max(match_mx[i][j - 1], match[j]);
} else {
match_mn[i][j] = match_mn[i][j - 1];
match_mx[i][j] = match_mx[i][j - 1];
}
}
}
for (int i = 1; i <= 2 * N; i++) match_cnt[i] = match_cnt[i - 1] + (match[i] ? 1 : 0);
fac2[0] = 1;
for (int i = 2; i <= 2 * N; i += 2) fac2[i] = fac2[i - 2] * (i - 1) % MOD;
for (int len = 2; len <= 2 * N; len++) {
for (int i = 1; i + len - 1 <= 2 * N; i++) {
int j = i + len - 1;
F[i][j] = ways(i, j);
for (int k = i + 1; k < j; k += 2) {
(F[i][j] += MOD - F[i][k] * ways(k + 1, j) % MOD) %= MOD;
}
(ans += F[i][j] * fac2[2 * N - (j - i + 1) - (2 * K - (match_cnt[j] - match_cnt[i - 1]))]) %= MOD;
}
}
printf("%lld\n", ans);
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 605;
const int P = 1e9 + 7;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int s[MAXN], home[MAXN], dp[MAXN][MAXN];
int n, m, p[MAXN], fac[MAXN];
void update(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
int main() {
read(n), read(m), fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * (2ll * i - 1) % P;
for (int i = 1; i <= m; i++) {
int x, y; read(x), read(y);
home[x] = home[x + n * 2] = i;
home[y] = home[y + n * 2] = i;
}
int ans = 0;
for (int i = 1; i <= n * 2 - 1; i++) {
static bool col[MAXN]; int cnt = 0, empty = 0;
memset(col, false, sizeof(col));
for (int j = i; j <= n * 2 - 1; j++) {
if (home[j]) {
cnt -= col[home[j]];
col[home[j]] ^= true;
cnt += col[home[j]];
} else empty += 1;
if (cnt == 0 && empty % 2 == 0) {
dp[i][j] = fac[empty / 2];
for (int k = i, cmt = 0; k <= j - 1; k++) {
cmt += !home[k];
if (cmt % 2 == 0) update(dp[i][j], P - 1ll * dp[i][k] * fac[empty / 2 - cmt / 2] % P);
}
update(ans, 1ll * dp[i][j] * fac[n - m - empty / 2] % P);
}
}
}
update(ans, fac[n - m]);
cout << ans << endl;
return 0;
} | ### Prompt
Generate a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 605;
const int P = 1e9 + 7;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int s[MAXN], home[MAXN], dp[MAXN][MAXN];
int n, m, p[MAXN], fac[MAXN];
void update(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
int main() {
read(n), read(m), fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * (2ll * i - 1) % P;
for (int i = 1; i <= m; i++) {
int x, y; read(x), read(y);
home[x] = home[x + n * 2] = i;
home[y] = home[y + n * 2] = i;
}
int ans = 0;
for (int i = 1; i <= n * 2 - 1; i++) {
static bool col[MAXN]; int cnt = 0, empty = 0;
memset(col, false, sizeof(col));
for (int j = i; j <= n * 2 - 1; j++) {
if (home[j]) {
cnt -= col[home[j]];
col[home[j]] ^= true;
cnt += col[home[j]];
} else empty += 1;
if (cnt == 0 && empty % 2 == 0) {
dp[i][j] = fac[empty / 2];
for (int k = i, cmt = 0; k <= j - 1; k++) {
cmt += !home[k];
if (cmt % 2 == 0) update(dp[i][j], P - 1ll * dp[i][k] * fac[empty / 2 - cmt / 2] % P);
}
update(ans, 1ll * dp[i][j] * fac[n - m - empty / 2] % P);
}
}
}
update(ans, fac[n - m]);
cout << ans << endl;
return 0;
}
``` |
#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define ll long long
using namespace std;
const int N=605,mod=1e9+7;
int n,m,a,b,f[N][N],cnt[N][N],p[N],g[N],ans;
void upd(int &x,int y){x+=y; x-=x>=mod?mod:0;}
int main(){
scanf("%d%d",&n,&m); n*=2;
rep (i,1,m) scanf("%d%d",&a,&b),p[a]=b,p[b]=a;
rep (i,1,n) rep (j,i,n) cnt[i][j]=cnt[i][j-1]+(!p[j]);//i~j中未连过的点数
g[0]=1; rep (i,1,n) if (!(i&1)) g[i]=(ll)g[i-2]*(i-1)%mod;//两个点可以自成一个匹配或拆散i-2个点中的一个匹配,i-2+1
rep (i,1,n)
rep (j,i,n){
bool flag=1;
rep (k,i,j) if (p[k]&&(p[k]>j||p[k]<i)) flag=0;//不能连到外面
if (!flag) continue;
f[i][j]=g[cnt[i][j]];//容斥
rep (k,i+1,j-1) upd(f[i][j],mod-(ll)f[i][k]*g[cnt[k+1][j]]%mod);
}
rep (i,1,n) rep (j,1,n) upd(ans,(ll)f[i][j]*g[n-2*m-cnt[i][j]]%mod);
printf("%d\n",ans);
return 0;
}
//果然很神啊……我好菜 | ### Prompt
Please formulate a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define ll long long
using namespace std;
const int N=605,mod=1e9+7;
int n,m,a,b,f[N][N],cnt[N][N],p[N],g[N],ans;
void upd(int &x,int y){x+=y; x-=x>=mod?mod:0;}
int main(){
scanf("%d%d",&n,&m); n*=2;
rep (i,1,m) scanf("%d%d",&a,&b),p[a]=b,p[b]=a;
rep (i,1,n) rep (j,i,n) cnt[i][j]=cnt[i][j-1]+(!p[j]);//i~j中未连过的点数
g[0]=1; rep (i,1,n) if (!(i&1)) g[i]=(ll)g[i-2]*(i-1)%mod;//两个点可以自成一个匹配或拆散i-2个点中的一个匹配,i-2+1
rep (i,1,n)
rep (j,i,n){
bool flag=1;
rep (k,i,j) if (p[k]&&(p[k]>j||p[k]<i)) flag=0;//不能连到外面
if (!flag) continue;
f[i][j]=g[cnt[i][j]];//容斥
rep (k,i+1,j-1) upd(f[i][j],mod-(ll)f[i][k]*g[cnt[k+1][j]]%mod);
}
rep (i,1,n) rep (j,1,n) upd(ans,(ll)f[i][j]*g[n-2*m-cnt[i][j]]%mod);
printf("%d\n",ans);
return 0;
}
//果然很神啊……我好菜
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define fi first
#define se second
#define gc getchar
#define pc putchar
inline ll read(){
ll x=0,f=1;char c=gc();
for(;!isdigit(c);c=gc())if(c=='-')f=-1;
for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
return x*f;
}
inline void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
inline void wri(ll x){write(x);pc(' ');}
inline void writeln(ll x){write(x);pc('\n');}
const int maxn = 605;
const int mod = 1e9+7;
int dp[maxn][maxn],g[maxn],h[maxn][maxn];
int n,m,a[maxn],b[maxn];
bool in(int l,int r,int x){
return l<=x&&x<=r;
}
int get(int l,int r){
int cnt = 0;
Rep(i,1,m){
bool f1 = in(l,r,a[i]),f2 = in(l,r,b[i]);
if(f1^f2) return -1;
if(f1)cnt++;
}
return cnt*2;
}
int main(){
n = 2*read(),m = read();
Rep(i,1,m) a[i] = read(),b[i] = read();
g[0] = 1;for(int i=2;i<=n;i+=2) g[i] = 1ll * g[i-2] * (i-1) % mod;
Rep(i,1,n)Rep(j,i,n)h[i][j]=get(i,j);
int ans = 0;
for(int l=2;l<=n;l+=2){
for(int i=1;i+l-1<=n;i++){
int j=i+l-1;
int c=h[i][j];
if(c!=-1){
dp[i][j]=g[l-c];
for(int k=i+1;k<j;k+=2)
if(h[k+1][j]!=-1 && h[i][k]!=-1)
dp[i][j] = (dp[i][j] - 1ll * dp[i][k]*g[j-k-h[k+1][j]]%mod+mod)%mod;
// printf("{%d %d}\n",dp[i][j],g[n-l-(m*2-c)]);
ans = (ans + 1ll * dp[i][j] * g[n-l-(m*2-c)]) % mod;
}
}
}
writeln(ans);
return 0;
} | ### Prompt
In Cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define fi first
#define se second
#define gc getchar
#define pc putchar
inline ll read(){
ll x=0,f=1;char c=gc();
for(;!isdigit(c);c=gc())if(c=='-')f=-1;
for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
return x*f;
}
inline void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
inline void wri(ll x){write(x);pc(' ');}
inline void writeln(ll x){write(x);pc('\n');}
const int maxn = 605;
const int mod = 1e9+7;
int dp[maxn][maxn],g[maxn],h[maxn][maxn];
int n,m,a[maxn],b[maxn];
bool in(int l,int r,int x){
return l<=x&&x<=r;
}
int get(int l,int r){
int cnt = 0;
Rep(i,1,m){
bool f1 = in(l,r,a[i]),f2 = in(l,r,b[i]);
if(f1^f2) return -1;
if(f1)cnt++;
}
return cnt*2;
}
int main(){
n = 2*read(),m = read();
Rep(i,1,m) a[i] = read(),b[i] = read();
g[0] = 1;for(int i=2;i<=n;i+=2) g[i] = 1ll * g[i-2] * (i-1) % mod;
Rep(i,1,n)Rep(j,i,n)h[i][j]=get(i,j);
int ans = 0;
for(int l=2;l<=n;l+=2){
for(int i=1;i+l-1<=n;i++){
int j=i+l-1;
int c=h[i][j];
if(c!=-1){
dp[i][j]=g[l-c];
for(int k=i+1;k<j;k+=2)
if(h[k+1][j]!=-1 && h[i][k]!=-1)
dp[i][j] = (dp[i][j] - 1ll * dp[i][k]*g[j-k-h[k+1][j]]%mod+mod)%mod;
// printf("{%d %d}\n",dp[i][j],g[n-l-(m*2-c)]);
ans = (ans + 1ll * dp[i][j] * g[n-l-(m*2-c)]) % mod;
}
}
}
writeln(ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600, mod = 1e9 + 7;
int n, k, a[maxn + 3], b[maxn + 3], g[maxn + 3];
int dp[maxn + 3][maxn + 3], cnt[maxn + 3][maxn + 3];
bool val[maxn + 3][maxn + 3];
bool in(int x, int l, int r) {
return x >= l && x <= r;
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= k; i++) {
scanf("%d %d", &a[i], &b[i]);
}
g[0] = 1;
for (int i = 1; i <= n * 2; i++) {
g[i] = i & 1 ? 0 : 1ll * g[i - 2] * (i - 1) % mod;
}
for (int i = 1; i <= n * 2; i++) {
for (int j = i + 1; j <= n * 2; j++) {
val[i][j] = true;
for (int t = 1; t <= k; t++) {
if (in(a[t], i, j) ^ in(b[t], i, j)) {
val[i][j] = false;
break;
}
}
if (val[i][j]) {
for (int t = 1; t <= k; t++) {
cnt[i][j] += in(a[t], i, j) + in(b[t], i, j);
}
}
}
}
int ans = 0;
for (int d = 2; d <= n * 2; d += 2) {
for (int i = 1, j = d; j <= n * 2; i++, j++) {
if (!val[i][j]) continue;
dp[i][j] = g[d - cnt[i][j]];
for (int k = i + 1; k < j; k++) {
dp[i][j] = (dp[i][j] + 1ll * (mod - dp[i][k]) * g[j - k - cnt[k + 1][j]]) % mod;
}
ans = (ans + 1ll * dp[i][j] * g[(n - k) * 2 - (d - cnt[i][j])]) % mod;
}
}
printf("%d\n", ans);
return 0;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 600, mod = 1e9 + 7;
int n, k, a[maxn + 3], b[maxn + 3], g[maxn + 3];
int dp[maxn + 3][maxn + 3], cnt[maxn + 3][maxn + 3];
bool val[maxn + 3][maxn + 3];
bool in(int x, int l, int r) {
return x >= l && x <= r;
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= k; i++) {
scanf("%d %d", &a[i], &b[i]);
}
g[0] = 1;
for (int i = 1; i <= n * 2; i++) {
g[i] = i & 1 ? 0 : 1ll * g[i - 2] * (i - 1) % mod;
}
for (int i = 1; i <= n * 2; i++) {
for (int j = i + 1; j <= n * 2; j++) {
val[i][j] = true;
for (int t = 1; t <= k; t++) {
if (in(a[t], i, j) ^ in(b[t], i, j)) {
val[i][j] = false;
break;
}
}
if (val[i][j]) {
for (int t = 1; t <= k; t++) {
cnt[i][j] += in(a[t], i, j) + in(b[t], i, j);
}
}
}
}
int ans = 0;
for (int d = 2; d <= n * 2; d += 2) {
for (int i = 1, j = d; j <= n * 2; i++, j++) {
if (!val[i][j]) continue;
dp[i][j] = g[d - cnt[i][j]];
for (int k = i + 1; k < j; k++) {
dp[i][j] = (dp[i][j] + 1ll * (mod - dp[i][k]) * g[j - k - cnt[k + 1][j]]) % mod;
}
ans = (ans + 1ll * dp[i][j] * g[(n - k) * 2 - (d - cnt[i][j])]) % mod;
}
}
printf("%d\n", ans);
return 0;
}
``` |
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int n, m, to[607], dp[607][607], g[607], res;
int read()
{
int num = 0;
char c = getchar();
while (c < '0' || c>'9')c = getchar();
while (c >= '0' && c <= '9')num = num * 10 + c - '0', c = getchar();
return num;
}
int main()
{
n = read();
m = read();
for (int i = 1; i <= m; i++)
{
int a = read(), b = read();
to[a] = b; to[b] = a;
}
g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
g[i] = 1LL * g[i - 2] * (i - 1) % mod;
for (int i = 1; i <= 2 * n; i++)
for (int j = i + 1; j <= 2 * n; j += 2)
{
int c = j - i + 1;
for (int k = i; k <= j; k++)
if (to[k])
{
c--;
if (to[k]<i || to[k]>j)c = -1;
}
if (c < 0)continue;
dp[i][j] = g[c];
for (int k = j, cc = 0; k > i; k--)
{
cc += (!to[k]);
dp[i][j] = (dp[i][j] - 1LL * dp[i][k - 1] * g[cc] % mod + mod) % mod;
}
res = (res + 1LL * dp[i][j] * g[2 * n - 2 * m - c] % mod) % mod;
}
cout << res << endl;
return 0;
} | ### Prompt
Please formulate a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int n, m, to[607], dp[607][607], g[607], res;
int read()
{
int num = 0;
char c = getchar();
while (c < '0' || c>'9')c = getchar();
while (c >= '0' && c <= '9')num = num * 10 + c - '0', c = getchar();
return num;
}
int main()
{
n = read();
m = read();
for (int i = 1; i <= m; i++)
{
int a = read(), b = read();
to[a] = b; to[b] = a;
}
g[0] = 1;
for (int i = 2; i <= 2 * n; i += 2)
g[i] = 1LL * g[i - 2] * (i - 1) % mod;
for (int i = 1; i <= 2 * n; i++)
for (int j = i + 1; j <= 2 * n; j += 2)
{
int c = j - i + 1;
for (int k = i; k <= j; k++)
if (to[k])
{
c--;
if (to[k]<i || to[k]>j)c = -1;
}
if (c < 0)continue;
dp[i][j] = g[c];
for (int k = j, cc = 0; k > i; k--)
{
cc += (!to[k]);
dp[i][j] = (dp[i][j] - 1LL * dp[i][k - 1] * g[cc] % mod + mod) % mod;
}
res = (res + 1LL * dp[i][j] * g[2 * n - 2 * m - c] % mod) % mod;
}
cout << res << endl;
return 0;
}
``` |
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define fir first
#define sec second
#define Rep(x,y,z) for(int x=y;x<=z;++x)
#define Red(x,y,z) for(int x=y;x>=z;--x)
using namespace std;
const int MAXN=605,Mod=1e9+7;
char buf[1<<12],*p1=buf,*p2=buf,nc;int ny;
inline char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
//inline char gc(){return getchar();}
inline int read(){
int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;if(nc<0)return nc;
x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48);return x*ny;
}int n,k,to[MAXN],cnt[MAXN][MAXN],F[MAXN][MAXN],g[MAXN],ans;
int main(){
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
n=read()*2,k=read();
Rep(i,1,k){int a=read(),b=read();to[a]=b,to[b]=a;}
Rep(i,1,n)Rep(j,i,n)cnt[i][j]=cnt[i][j-1]+(!to[j]);
g[0]=1;for(int i=2;i<=n;i+=2)g[i]=1ll*g[i-2]*(i-1)%Mod;
Rep(i,1,n)Rep(j,i,n){
int flg=0;
Rep(k,i,j)if(to[k]&&(to[k]>j||to[k]<i))flg=1;
if(flg)continue;F[i][j]=g[cnt[i][j]];
Rep(k,i+1,j-1)F[i][j]=(F[i][j]-1ll*F[i][k]*g[cnt[k+1][j]]%Mod+Mod)%Mod;
}Rep(i,1,n)Rep(j,i+1,n)ans=(ans+1ll*F[i][j]*g[n-2*k-cnt[i][j]])%Mod;
cout<<ans<<"\n";
return 0;
}
| ### Prompt
Please provide a cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define fir first
#define sec second
#define Rep(x,y,z) for(int x=y;x<=z;++x)
#define Red(x,y,z) for(int x=y;x>=z;--x)
using namespace std;
const int MAXN=605,Mod=1e9+7;
char buf[1<<12],*p1=buf,*p2=buf,nc;int ny;
inline char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
//inline char gc(){return getchar();}
inline int read(){
int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;if(nc<0)return nc;
x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48);return x*ny;
}int n,k,to[MAXN],cnt[MAXN][MAXN],F[MAXN][MAXN],g[MAXN],ans;
int main(){
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
n=read()*2,k=read();
Rep(i,1,k){int a=read(),b=read();to[a]=b,to[b]=a;}
Rep(i,1,n)Rep(j,i,n)cnt[i][j]=cnt[i][j-1]+(!to[j]);
g[0]=1;for(int i=2;i<=n;i+=2)g[i]=1ll*g[i-2]*(i-1)%Mod;
Rep(i,1,n)Rep(j,i,n){
int flg=0;
Rep(k,i,j)if(to[k]&&(to[k]>j||to[k]<i))flg=1;
if(flg)continue;F[i][j]=g[cnt[i][j]];
Rep(k,i+1,j-1)F[i][j]=(F[i][j]-1ll*F[i][k]*g[cnt[k+1][j]]%Mod+Mod)%Mod;
}Rep(i,1,n)Rep(j,i+1,n)ans=(ans+1ll*F[i][j]*g[n-2*k-cnt[i][j]])%Mod;
cout<<ans<<"\n";
return 0;
}
``` |
#include<stdio.h>
typedef long long ll;
const int mod=1000000007;
int mul(int a,int b){return(ll)a*b%mod;}
int p[610],c[610][610],f[610][610],g[610];
int main(){
int n,m,i,j,k,x,y,t,s;
bool flag;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
p[x]=y;
p[y]=x;
}
n<<=1;
g[0]=1;
for(i=2;i<=n;i+=2)g[i]=mul(g[i-2],i-1);
for(i=1;i<=n;i++){
for(j=i;j<=n;j++)c[i][j]=c[i][j-1]+!p[j];
}
s=0;
for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
flag=0;
for(k=i;k<=j;k++){
if(p[k]&&(p[k]<i||p[k]>j)){
flag=1;
break;
}
}
if(flag)continue;
t=g[c[i][j]];
for(k=i+1;k<j;k++)(t-=mul(f[i][k],g[c[k+1][j]]))%=mod;
f[i][j]=t;
(s+=mul(f[i][j],g[n-m*2-c[i][j]]))%=mod;
}
}
printf("%d",(s+mod)%mod);
} | ### Prompt
Please create a solution in cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<stdio.h>
typedef long long ll;
const int mod=1000000007;
int mul(int a,int b){return(ll)a*b%mod;}
int p[610],c[610][610],f[610][610],g[610];
int main(){
int n,m,i,j,k,x,y,t,s;
bool flag;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
p[x]=y;
p[y]=x;
}
n<<=1;
g[0]=1;
for(i=2;i<=n;i+=2)g[i]=mul(g[i-2],i-1);
for(i=1;i<=n;i++){
for(j=i;j<=n;j++)c[i][j]=c[i][j-1]+!p[j];
}
s=0;
for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
flag=0;
for(k=i;k<=j;k++){
if(p[k]&&(p[k]<i||p[k]>j)){
flag=1;
break;
}
}
if(flag)continue;
t=g[c[i][j]];
for(k=i+1;k<j;k++)(t-=mul(f[i][k],g[c[k+1][j]]))%=mod;
f[i][j]=t;
(s+=mul(f[i][j],g[n-m*2-c[i][j]]))%=mod;
}
}
printf("%d",(s+mod)%mod);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N = 600;
const int mo = 1e9 + 7;
int n, m;
int dp[N + 5][N + 5];
int f[N + 5], lnk[N + 5];
int a[N + 5], b[N + 5], s[N + 5];
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d%d", a + i, b + i);
++ s[a[i]], ++ s[b[i]];
lnk[lnk[b[i]] = a[i]] = b[i];
}
n <<= 1;
for(int i = 1; i <= n; ++i) s[i] += s[i-1];
f[0] = f[2] = 1;
for(int i = 4; i <= N; i += 2) f[i] = 1ll * (i - 1) * f[i - 2] % mo;
int ans = 0;
for(int i = 1; i <= n; ++i) {
for(int j = i + 1; j <= n; j += 2) {
bool flag = true;
for(int k = i; k <= j; ++k) if(lnk[k] && (lnk[k] < i || lnk[k] > j)) {
flag = false;
break;
}
if(!flag) continue;
dp[i][j] = f[j - i + 1 - (s[j] - s[i-1])];
for(int k = i; k < j; ++k) dp[i][j] = (dp[i][j] - 1ll * dp[i][k] * f[j - k - (s[j] - s[k])]) % mo;
ans = (ans + 1ll * dp[i][j] * f[n - (j - i + 1) - (2 * m - (s[j] - s[i-1]))]) % mo;
}
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 600;
const int mo = 1e9 + 7;
int n, m;
int dp[N + 5][N + 5];
int f[N + 5], lnk[N + 5];
int a[N + 5], b[N + 5], s[N + 5];
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d%d", a + i, b + i);
++ s[a[i]], ++ s[b[i]];
lnk[lnk[b[i]] = a[i]] = b[i];
}
n <<= 1;
for(int i = 1; i <= n; ++i) s[i] += s[i-1];
f[0] = f[2] = 1;
for(int i = 4; i <= N; i += 2) f[i] = 1ll * (i - 1) * f[i - 2] % mo;
int ans = 0;
for(int i = 1; i <= n; ++i) {
for(int j = i + 1; j <= n; j += 2) {
bool flag = true;
for(int k = i; k <= j; ++k) if(lnk[k] && (lnk[k] < i || lnk[k] > j)) {
flag = false;
break;
}
if(!flag) continue;
dp[i][j] = f[j - i + 1 - (s[j] - s[i-1])];
for(int k = i; k < j; ++k) dp[i][j] = (dp[i][j] - 1ll * dp[i][k] * f[j - k - (s[j] - s[k])]) % mo;
ans = (ans + 1ll * dp[i][j] * f[n - (j - i + 1) - (2 * m - (s[j] - s[i-1]))]) % mo;
}
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N,K;
int A[660],B[660];
int connected[660];
int f[660][660],g[660][660];
int dp[660][660];
int mod=1e9+7;
signed main() {
cin>>N>>K;
for(int i=0;i<K;i++){
cin>>A[i]>>B[i];
connected[A[i]]=connected[B[i]]=1;
}
for(int i=1;i<2*N;i++){
for(int j=i+1;j<=2*N;j++){
for(int k=i;k<=j;k++)f[i][j]+=1-connected[k];
}
}
for(int i=1;i<2*N;i++){
for(int j=i+1;j<=2*N;j++){
g[i][j]=1;
for(int k=1;k<f[i][j];k+=2)(g[i][j]*=k)%=mod;
}
}
int ans=0;
for(int i=1;i<2*N;i++){
for(int j=i+1;j<=2*N;j++){
if(f[i][j]%2==1)continue;
bool flag=false;
for(int k=0;k<K;k++){
flag|=i<=A[k]&&A[k]<=j&&!(i<=B[k]&&B[k]<=j);
flag|=i<=B[k]&&B[k]<=j&&!(i<=A[k]&&A[k]<=j);
}
if(flag)continue;
dp[i][j]=g[i][j];
for(int k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-dp[i][k]*g[k+1][j]+mod)%mod;
int outer=1;
for(int k=1;k<f[1][2*N]-f[i][j];k+=2)(outer*=k)%=mod;
(ans+=dp[i][j]*outer%mod)%=mod;
}
}
cout<<ans<<endl;
}
| ### Prompt
Your task is to create a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N,K;
int A[660],B[660];
int connected[660];
int f[660][660],g[660][660];
int dp[660][660];
int mod=1e9+7;
signed main() {
cin>>N>>K;
for(int i=0;i<K;i++){
cin>>A[i]>>B[i];
connected[A[i]]=connected[B[i]]=1;
}
for(int i=1;i<2*N;i++){
for(int j=i+1;j<=2*N;j++){
for(int k=i;k<=j;k++)f[i][j]+=1-connected[k];
}
}
for(int i=1;i<2*N;i++){
for(int j=i+1;j<=2*N;j++){
g[i][j]=1;
for(int k=1;k<f[i][j];k+=2)(g[i][j]*=k)%=mod;
}
}
int ans=0;
for(int i=1;i<2*N;i++){
for(int j=i+1;j<=2*N;j++){
if(f[i][j]%2==1)continue;
bool flag=false;
for(int k=0;k<K;k++){
flag|=i<=A[k]&&A[k]<=j&&!(i<=B[k]&&B[k]<=j);
flag|=i<=B[k]&&B[k]<=j&&!(i<=A[k]&&A[k]<=j);
}
if(flag)continue;
dp[i][j]=g[i][j];
for(int k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-dp[i][k]*g[k+1][j]+mod)%mod;
int outer=1;
for(int k=1;k<f[1][2*N]-f[i][j];k+=2)(outer*=k)%=mod;
(ans+=dp[i][j]*outer%mod)%=mod;
}
}
cout<<ans<<endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 610, P = 1000000007;
int n, K, pos[maxn], sum[maxn], f[maxn][maxn], cnt[maxn];
int main() {
scanf("%d %d", &n, &K), n <<= 1;
for (int i = 1, a, b; i <= K; i++) {
scanf("%d %d", &a, &b);
pos[a] = b, pos[b] = a;
}
cnt[0] = 1;
for (int i = 2; i <= n; i += 2) {
cnt[i] = 1LL * (i - 1) * cnt[i - 2] % P;
}
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + !pos[i];
}
int ans = 0;
for (int l = 2; l <= n; l++) {
for (int i = 1, j = i + l - 1; j <= n; i++, j++) {
bool flag = 0;
if ((sum[j] - sum[i - 1]) & 1) flag = 1;
for (int k = i; k <= j && !flag; k++) {
if (pos[k] && (pos[k] < i || pos[k] > j)) flag = 1;
}
if (flag) continue;
f[i][j] = cnt[sum[j] - sum[i - 1]];
for (int k = i; k < j; k++) {
f[i][j] = (f[i][j] - 1LL * f[i][k] * cnt[sum[j] - sum[k]] % P + P) % P;
}
int t = n - K - K - (sum[j] - sum[i - 1]);
ans = (ans + 1LL * (f[i][j] + P) * cnt[t]) % P;
}
}
printf("%d\n", ans);
return 0;
} | ### Prompt
Please create a solution in Cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 610, P = 1000000007;
int n, K, pos[maxn], sum[maxn], f[maxn][maxn], cnt[maxn];
int main() {
scanf("%d %d", &n, &K), n <<= 1;
for (int i = 1, a, b; i <= K; i++) {
scanf("%d %d", &a, &b);
pos[a] = b, pos[b] = a;
}
cnt[0] = 1;
for (int i = 2; i <= n; i += 2) {
cnt[i] = 1LL * (i - 1) * cnt[i - 2] % P;
}
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + !pos[i];
}
int ans = 0;
for (int l = 2; l <= n; l++) {
for (int i = 1, j = i + l - 1; j <= n; i++, j++) {
bool flag = 0;
if ((sum[j] - sum[i - 1]) & 1) flag = 1;
for (int k = i; k <= j && !flag; k++) {
if (pos[k] && (pos[k] < i || pos[k] > j)) flag = 1;
}
if (flag) continue;
f[i][j] = cnt[sum[j] - sum[i - 1]];
for (int k = i; k < j; k++) {
f[i][j] = (f[i][j] - 1LL * f[i][k] * cnt[sum[j] - sum[k]] % P + P) % P;
}
int t = n - K - K - (sum[j] - sum[i - 1]);
ans = (ans + 1LL * (f[i][j] + P) * cnt[t]) % P;
}
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MX = 605;
const int MD = 1000000007;
int mod_mult(int a, int b) {
return (a * 1LL * b) % MD;
}
int dp[MX][MX];
int g[MX];
pair<int, int> p[MX];
bool in(int L, int R, int x) {
return L <= x && x <= R;
}
int cnt[MX][MX];
bool taken[MX];
int main()
{
g[0] = 1;
for (int i = 2; i < MX; i += 2) {
g[i] = mod_mult(g[i - 2], i - 1);
}
int n, k;
ignore = scanf("%d %d", &n, &k);
for (int i = 0; i < k; i++) {
ignore = scanf("%d %d", &p[i].first, &p[i].second);
taken[p[i].first] = true;
taken[p[i].second] = true;
}
for (int i = 1; i <= 2 * n; i++) {
for (int j = i; j <= 2 * n; j++) {
cnt[i][j] = cnt[i][j - 1] + (1 - taken[j]);
}
}
int ans = 0;
for (int l = 1; l <= 2 * n; l++) {
for (int i = 1; i + l <= 2 * n; i++) {
int j = i + l;
//printf("(%d %d):\n", i, j);
bool ok = true;
for (int t = 0; t < k; t++) {
if (in(i, j, p[t].first) != in(i, j, p[t].second)) {
ok = false;
break;
}
}
if (!ok) continue;
int x, y;
x = l + 1;
y = 2 * n - x;
for (int t = 0; t < k; t++) {
if (in(i, j, p[t].first)) {
x--;
}
else {
y--;
}
if (in(i, j, p[t].second)) {
x--;
}
else {
y--;
}
}
//printf("x = %d, y = %d\n", x, y);
int sub = 0;
//printf("before sub: %d\n", g[x]);
for (int t = i + 1; t < j; t++) {
sub = (sub + mod_mult(dp[i][t], g[cnt[t + 1][j]])) % MD;
}
dp[i][j] = (g[x] - sub + MD) % MD;
//printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
ans = (ans + mod_mult(dp[i][j], g[y])) % MD;
}
}
printf("%d", ans);
}
| ### Prompt
Please create a solution in Cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = 605;
const int MD = 1000000007;
int mod_mult(int a, int b) {
return (a * 1LL * b) % MD;
}
int dp[MX][MX];
int g[MX];
pair<int, int> p[MX];
bool in(int L, int R, int x) {
return L <= x && x <= R;
}
int cnt[MX][MX];
bool taken[MX];
int main()
{
g[0] = 1;
for (int i = 2; i < MX; i += 2) {
g[i] = mod_mult(g[i - 2], i - 1);
}
int n, k;
ignore = scanf("%d %d", &n, &k);
for (int i = 0; i < k; i++) {
ignore = scanf("%d %d", &p[i].first, &p[i].second);
taken[p[i].first] = true;
taken[p[i].second] = true;
}
for (int i = 1; i <= 2 * n; i++) {
for (int j = i; j <= 2 * n; j++) {
cnt[i][j] = cnt[i][j - 1] + (1 - taken[j]);
}
}
int ans = 0;
for (int l = 1; l <= 2 * n; l++) {
for (int i = 1; i + l <= 2 * n; i++) {
int j = i + l;
//printf("(%d %d):\n", i, j);
bool ok = true;
for (int t = 0; t < k; t++) {
if (in(i, j, p[t].first) != in(i, j, p[t].second)) {
ok = false;
break;
}
}
if (!ok) continue;
int x, y;
x = l + 1;
y = 2 * n - x;
for (int t = 0; t < k; t++) {
if (in(i, j, p[t].first)) {
x--;
}
else {
y--;
}
if (in(i, j, p[t].second)) {
x--;
}
else {
y--;
}
}
//printf("x = %d, y = %d\n", x, y);
int sub = 0;
//printf("before sub: %d\n", g[x]);
for (int t = i + 1; t < j; t++) {
sub = (sub + mod_mult(dp[i][t], g[cnt[t + 1][j]])) % MD;
}
dp[i][j] = (g[x] - sub + MD) % MD;
//printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);
ans = (ans + mod_mult(dp[i][j], g[y])) % MD;
}
}
printf("%d", ans);
}
``` |
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define int long long
using namespace std;
const int N = 605, mod = 1e9 + 7;
int n, K, f[N][N], g[N], to[N], pre[N], ans;
int C2(int x) {
return 1ll * x * (x - 1) / 2 % mod;
}
int num(int l, int r) {
return pre[r] - pre[l - 1];
}
int solve(int l, int r) {
rep(i, l, r)
if (to[i] && (to[i] < l || to[i] > r)) return 0;
f[l][r] = g[num(l, r) >> 1];
rep(k, l + 1, r - 1) {
f[l][r] = (f[l][r] - f[l][k] * g[num(k + 1, r) >> 1] % mod + mod) % mod;
} return f[l][r];
}
signed main() {
cin >> n >> K;
rep(i, 1, K) {
int x, y; scanf("%lld%lld", &x, &y);
to[x] = y, to[y] = x;
}
rep(i, 1, n << 1) pre[i] = pre[i - 1] + (!to[i]);
g[0] = 1;
rep(i, 1, n) g[i] = g[i - 1] * (2 * i - 1) % mod; // g[i] = g[i - 1] * C(2 * i, 2) / i,除掉旋转同构的
rep(l, 1, n << 1)
rep(r, l + 1, n << 1)
if (num(l, r) % 2 == 0 && (r - l + 1) % 2 == 0)
(ans += solve(l, r) * g[(num(1, l - 1) + num(r + 1, 2 * n)) >> 1] % mod) %= mod;
printf("%lld\n", ans);
return 0;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define int long long
using namespace std;
const int N = 605, mod = 1e9 + 7;
int n, K, f[N][N], g[N], to[N], pre[N], ans;
int C2(int x) {
return 1ll * x * (x - 1) / 2 % mod;
}
int num(int l, int r) {
return pre[r] - pre[l - 1];
}
int solve(int l, int r) {
rep(i, l, r)
if (to[i] && (to[i] < l || to[i] > r)) return 0;
f[l][r] = g[num(l, r) >> 1];
rep(k, l + 1, r - 1) {
f[l][r] = (f[l][r] - f[l][k] * g[num(k + 1, r) >> 1] % mod + mod) % mod;
} return f[l][r];
}
signed main() {
cin >> n >> K;
rep(i, 1, K) {
int x, y; scanf("%lld%lld", &x, &y);
to[x] = y, to[y] = x;
}
rep(i, 1, n << 1) pre[i] = pre[i - 1] + (!to[i]);
g[0] = 1;
rep(i, 1, n) g[i] = g[i - 1] * (2 * i - 1) % mod; // g[i] = g[i - 1] * C(2 * i, 2) / i,除掉旋转同构的
rep(l, 1, n << 1)
rep(r, l + 1, n << 1)
if (num(l, r) % 2 == 0 && (r - l + 1) % 2 == 0)
(ans += solve(l, r) * g[(num(1, l - 1) + num(r + 1, 2 * n)) >> 1] % mod) %= mod;
printf("%lld\n", ans);
return 0;
}
``` |
#include<cstdio>
const int MaxN=310,mod=1000000007;
int N,K,to[MaxN*2],pre[MaxN*2],f[MaxN*2][MaxN*2],fac[MaxN*2];
int main(){
scanf("%d%d",&N,&K);
for(int i=0,a,b;i<K;i++)scanf("%d%d",&a,&b),to[a]=b,to[b]=a;
for(int i=1;i<=N*2;i++)pre[i]=pre[i-1]+!to[i];
fac[0]=1;
for(int i=2;i<=N*2;i++)fac[i]=1ll*(i-1)*fac[i-2]%mod;
f[N*2+1][N*2]=1;
int ans=0;
for(int l=N*2;l;l--){
int mn=1<<30,mx=-1,c=0;
f[l][l-1]=1;
for(int r=l;r<=N*2;r++){
if(!to[r])c++;
else{
if(to[r]<mn)mn=to[r];
if(to[r]>mx)mx=to[r];
}
//printf("l=%d r=%d mn=%d mx=%d c=%d\n",l,r,mn,mx,c);
if((r-l&1)&&mn>=l&&mx<=r&&c%2==0){
f[l][r]=fac[c];
for(int m=l,ml=1<<30,mr=-1,d=0;m<r-1;m++){
if(!to[m])d++;
else{
if(to[m]<ml)ml=to[m];
if(to[m]>mr)mr=to[m];
}
if((m-l&1)&&ml>=l&&mr<=m&&d%2==0)f[l][r]=(f[l][r]+1ll*(mod-f[m+1][r])*fac[d])%mod;
}
ans=(ans+1ll*f[l][r]*fac[pre[N*2]-c])%mod;
//printf("f(%d,%d)=%d\n",l,r,f[l][r]);
}
}
}
printf("%d\n",ans);
} | ### Prompt
In CPP, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
const int MaxN=310,mod=1000000007;
int N,K,to[MaxN*2],pre[MaxN*2],f[MaxN*2][MaxN*2],fac[MaxN*2];
int main(){
scanf("%d%d",&N,&K);
for(int i=0,a,b;i<K;i++)scanf("%d%d",&a,&b),to[a]=b,to[b]=a;
for(int i=1;i<=N*2;i++)pre[i]=pre[i-1]+!to[i];
fac[0]=1;
for(int i=2;i<=N*2;i++)fac[i]=1ll*(i-1)*fac[i-2]%mod;
f[N*2+1][N*2]=1;
int ans=0;
for(int l=N*2;l;l--){
int mn=1<<30,mx=-1,c=0;
f[l][l-1]=1;
for(int r=l;r<=N*2;r++){
if(!to[r])c++;
else{
if(to[r]<mn)mn=to[r];
if(to[r]>mx)mx=to[r];
}
//printf("l=%d r=%d mn=%d mx=%d c=%d\n",l,r,mn,mx,c);
if((r-l&1)&&mn>=l&&mx<=r&&c%2==0){
f[l][r]=fac[c];
for(int m=l,ml=1<<30,mr=-1,d=0;m<r-1;m++){
if(!to[m])d++;
else{
if(to[m]<ml)ml=to[m];
if(to[m]>mr)mr=to[m];
}
if((m-l&1)&&ml>=l&&mr<=m&&d%2==0)f[l][r]=(f[l][r]+1ll*(mod-f[m+1][r])*fac[d])%mod;
}
ans=(ans+1ll*f[l][r]*fac[pre[N*2]-c])%mod;
//printf("f(%d,%d)=%d\n",l,r,f[l][r]);
}
}
}
printf("%d\n",ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
typedef long long ll;
typedef pair<ll,ll> P;
ll n,k;
ll mp[666];
ll dp[666][666];
ll comb[666];
ll imos[666];
void init(){
comb[0] = 1;
REP(i,666){
imos[i]++;
mp[i] = -1;
if(i)comb[i] = comb[i-1] * (2 * i - 1) % MOD;
REP(j, 666){
dp[i][j] = -1;
}
}
}
ll dfs(int l, int r){
if(dp[l][r] != -1)return dp[l][r];
int cou = 0;
for(int i = l;i <= r;i++){
if(mp[i] != -1){
if(mp[i] > r || mp[i] < l){
return dp[l][r] = 0;
}
}
else {
cou++;
}
}
if(cou % 2) return dp[l][r] = 0;
ll ret = comb[cou/2];
for(int i = l;i < r;i++){
int seica = imos[r] - imos[i-1] ;
ret = (ret - dfs(l, i) * comb[seica/2] + MOD) % MOD;
}
return dp[l][r] = ret;
}
int main(){
init();
cin >> n >> k;
REP(i, k){
int a, b;cin >> a >> b;
mp[a] = b;
mp[b] = a;
imos[a]--;
imos[b]--;
}
REP(i,665)imos[i+1] += imos[i];
ll ans = 0;
for(int i = 1;i <= 2 * n;i++){
for(int j = i + 1;j <= 2 * n;j++){
ans = (ans + dfs(i, j) * comb[(2 * (n - k) - (imos[j]-imos[i-1]))/2] % MOD) % MOD;
}
}
cout << ans << endl;
return 0;
}
| ### Prompt
Please provide a CPP coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
typedef long long ll;
typedef pair<ll,ll> P;
ll n,k;
ll mp[666];
ll dp[666][666];
ll comb[666];
ll imos[666];
void init(){
comb[0] = 1;
REP(i,666){
imos[i]++;
mp[i] = -1;
if(i)comb[i] = comb[i-1] * (2 * i - 1) % MOD;
REP(j, 666){
dp[i][j] = -1;
}
}
}
ll dfs(int l, int r){
if(dp[l][r] != -1)return dp[l][r];
int cou = 0;
for(int i = l;i <= r;i++){
if(mp[i] != -1){
if(mp[i] > r || mp[i] < l){
return dp[l][r] = 0;
}
}
else {
cou++;
}
}
if(cou % 2) return dp[l][r] = 0;
ll ret = comb[cou/2];
for(int i = l;i < r;i++){
int seica = imos[r] - imos[i-1] ;
ret = (ret - dfs(l, i) * comb[seica/2] + MOD) % MOD;
}
return dp[l][r] = ret;
}
int main(){
init();
cin >> n >> k;
REP(i, k){
int a, b;cin >> a >> b;
mp[a] = b;
mp[b] = a;
imos[a]--;
imos[b]--;
}
REP(i,665)imos[i+1] += imos[i];
ll ans = 0;
for(int i = 1;i <= 2 * n;i++){
for(int j = i + 1;j <= 2 * n;j++){
ans = (ans + dfs(i, j) * comb[(2 * (n - k) - (imos[j]-imos[i-1]))/2] % MOD) % MOD;
}
}
cout << ans << endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=605,p=1000000007;
int read(){
int f=1,g=0;char ch=getchar();
for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
return f*g;
}
int n,K,ans,a[N],s[N][N],f[N][N],g[N];
void check(int &x,int y){if ((x+=y)>=p) x-=p;}
int main(){
n=read()*2;K=read();
for (int i=1;i<=n;i++) a[i]=i;
for (int i=1;i<=K;i++){
int u=read(),v=read();
a[u]=v;a[v]=u;
}
g[0]=1;
for (int i=2;i<=n;i++) g[i]=(ll)g[i-2]*(i-1)%p;
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++)
s[i][j]=s[i][j-1]+(a[j]==j);
for (int i=n;i;i--)
for (int j=i+1;j<=n;j+=2){
int fl=1;
for (int k=i;k<=j;k++)
fl&=(i<=a[k])&&(a[k]<=j);
if (!fl){f[i][j]=0;continue;}
f[i][j]=g[s[i][j]];
for (int k=i+1;k<j;k+=2)
check(f[i][j],p-(ll)f[i][k]*g[s[k+1][j]]%p);
}
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j+=2)
check(ans,(ll)f[i][j]*g[s[1][i-1]+s[j+1][n]]%p);
printf("%d\n",ans);
return 0;
} | ### Prompt
Generate a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=605,p=1000000007;
int read(){
int f=1,g=0;char ch=getchar();
for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
return f*g;
}
int n,K,ans,a[N],s[N][N],f[N][N],g[N];
void check(int &x,int y){if ((x+=y)>=p) x-=p;}
int main(){
n=read()*2;K=read();
for (int i=1;i<=n;i++) a[i]=i;
for (int i=1;i<=K;i++){
int u=read(),v=read();
a[u]=v;a[v]=u;
}
g[0]=1;
for (int i=2;i<=n;i++) g[i]=(ll)g[i-2]*(i-1)%p;
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++)
s[i][j]=s[i][j-1]+(a[j]==j);
for (int i=n;i;i--)
for (int j=i+1;j<=n;j+=2){
int fl=1;
for (int k=i;k<=j;k++)
fl&=(i<=a[k])&&(a[k]<=j);
if (!fl){f[i][j]=0;continue;}
f[i][j]=g[s[i][j]];
for (int k=i+1;k<j;k+=2)
check(f[i][j],p-(ll)f[i][k]*g[s[k+1][j]]%p);
}
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j+=2)
check(ans,(ll)f[i][j]*g[s[1][i-1]+s[j+1][n]]%p);
printf("%d\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N=605,mod=1e9+7;
typedef long long ll;
ll dp[N][N],F[N];
int sum[N],p[N];
bool vis[N][N];
vector<int>v[N];
ll f(int x)
{
if(x<=1) return 1;
if(F[x]) return F[x];
return F[x]=x*f(x-2)%mod;
}
ll g(int x){if(x&1) return 0;return f(x-1);}
int main()
{
int n,k;scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++)
{
int x,y;scanf("%d%d",&x,&y);
p[x]=y;p[y]=x;
}
n*=2;
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+(!p[i]);
ll ans=0;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
if((sum[j]-sum[i-1])&1) continue;
bool flag=true;
for(int k=i;k<=j;k++)
if(p[k]&&(p[k]<i||p[k]>j)) {flag=false;break;}
if(!flag) continue;
dp[i][j]=g(sum[j]-sum[i-1])%mod;
for(int k=i+1;k<j;k++)
dp[i][j]=(dp[i][j]-dp[i][k]*g(sum[j]-sum[k])%mod)%mod;
ans=(ans+dp[i][j]*g(n-k*2-sum[j]+sum[i-1]))%mod;
}
ans=(ans+mod)%mod;
printf("%lld\n",ans);
}
| ### Prompt
Your challenge is to write a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=605,mod=1e9+7;
typedef long long ll;
ll dp[N][N],F[N];
int sum[N],p[N];
bool vis[N][N];
vector<int>v[N];
ll f(int x)
{
if(x<=1) return 1;
if(F[x]) return F[x];
return F[x]=x*f(x-2)%mod;
}
ll g(int x){if(x&1) return 0;return f(x-1);}
int main()
{
int n,k;scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++)
{
int x,y;scanf("%d%d",&x,&y);
p[x]=y;p[y]=x;
}
n*=2;
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+(!p[i]);
ll ans=0;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
if((sum[j]-sum[i-1])&1) continue;
bool flag=true;
for(int k=i;k<=j;k++)
if(p[k]&&(p[k]<i||p[k]>j)) {flag=false;break;}
if(!flag) continue;
dp[i][j]=g(sum[j]-sum[i-1])%mod;
for(int k=i+1;k<j;k++)
dp[i][j]=(dp[i][j]-dp[i][k]*g(sum[j]-sum[k])%mod)%mod;
ans=(ans+dp[i][j]*g(n-k*2-sum[j]+sum[i-1]))%mod;
}
ans=(ans+mod)%mod;
printf("%lld\n",ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
ll dp[605][605];
int n,k;
int to[605];
bool ok[605][605];
int cnt[605][605];
ll v[605];
ll rec(int l,int r){
if(!ok[l][r]) return dp[l][r] = 0;
if(dp[l][r] >= 0) return dp[l][r];
dp[l][r] = v[cnt[l][r]];
for(int x=l+1;x<=r;x++){
if(!ok[x][r]) continue;
dp[l][r] -= rec(l,x-1)*v[cnt[x][r]]%mod;
}
return dp[l][r] = (dp[l][r]%mod+mod)%mod;
}
int main(){
cin>>n>>k;
rep(i,k){
int a,b; cin>>a>>b;
to[a] = b; to[b] = a;
}
for(int i=1;i<=2*n;i++) for(int j=i;j<=2*n;j++){
for(int x=i;x<=j;x++){
if(to[x] && !(i<=to[x] && to[x]<=j)) goto fail;
if(!to[x]) cnt[i][j]++;
}
if(cnt[i][j]%2 == 0) ok[i][j] = 1;
fail:;
}
for(int i=600;i>=0;i-=2){
v[i] = 1;
for(int j=i-1;j>=0;j-=2){
v[i] = v[i]*1LL*j%mod;
}
}
memset(dp,-1,sizeof(dp));
ll ans = 0;
for(int i=1;i<=2*n;i++) for(int j=i;j<=2*n;j++){
ans += rec(i,j) * v[cnt[1][2*n]-cnt[i][j]] % mod;
ans %= mod;
}
cout << ans << endl;
} | ### Prompt
Please formulate a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
ll dp[605][605];
int n,k;
int to[605];
bool ok[605][605];
int cnt[605][605];
ll v[605];
ll rec(int l,int r){
if(!ok[l][r]) return dp[l][r] = 0;
if(dp[l][r] >= 0) return dp[l][r];
dp[l][r] = v[cnt[l][r]];
for(int x=l+1;x<=r;x++){
if(!ok[x][r]) continue;
dp[l][r] -= rec(l,x-1)*v[cnt[x][r]]%mod;
}
return dp[l][r] = (dp[l][r]%mod+mod)%mod;
}
int main(){
cin>>n>>k;
rep(i,k){
int a,b; cin>>a>>b;
to[a] = b; to[b] = a;
}
for(int i=1;i<=2*n;i++) for(int j=i;j<=2*n;j++){
for(int x=i;x<=j;x++){
if(to[x] && !(i<=to[x] && to[x]<=j)) goto fail;
if(!to[x]) cnt[i][j]++;
}
if(cnt[i][j]%2 == 0) ok[i][j] = 1;
fail:;
}
for(int i=600;i>=0;i-=2){
v[i] = 1;
for(int j=i-1;j>=0;j-=2){
v[i] = v[i]*1LL*j%mod;
}
}
memset(dp,-1,sizeof(dp));
ll ans = 0;
for(int i=1;i<=2*n;i++) for(int j=i;j<=2*n;j++){
ans += rec(i,j) * v[cnt[1][2*n]-cnt[i][j]] % mod;
ans %= mod;
}
cout << ans << endl;
}
``` |
#include <cstdio>
using namespace std;
const int mod = 1000000007;
int p[600];
long long c[601];
long long dp[600][601];
long long calc(int x, int y) {
int cnt = 0, i;
if (dp[x][y] >= 0) return dp[x][y];
if (y <= x) return dp[x][y] = 0;
if ((y - x) % 2 == 1) return dp[x][y] = 0;
for (i = x; i < y; i++) {
if (p[i] == -1) {
cnt++;
} else if (p[i] < x || p[i] >= y) {
return dp[x][y] = 0;
}
}
dp[x][y] = c[cnt];
for (i = x; i < y; i++) {
dp[x][y] -= calc(x, i) * c[cnt] % mod;
if (dp[x][y] < 0) dp[x][y] += mod;
if (p[i] == -1) cnt--;
}
return dp[x][y];
}
int main() {
int n, k, i, j, l;
long long ans = 0;
scanf("%d %d", &n, &k);
n *= 2;
for (i = 0; i < n; i++) p[i] = -1;
for (i = 0; i < k; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
p[a] = b;
p[b] = a;
}
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++) {
dp[i][j] = -1;
}
}
c[0] = 1;
for (i = 2; i <= n; i += 2) c[i] = c[i - 2] * (i - 1) % mod;
for (i = 0; i < n; i++) {
for (j = i + 1; j <= n; j++) {
int cnt = 0;
for (l = 0; l < i; l++) {
if (p[l] == -1) cnt++;
}
for (l = j; l < n; l++) {
if (p[l] == -1) cnt++;
}
ans += calc(i, j) * c[cnt] % mod;
if (ans >= mod) ans -= mod;
}
}
printf("%lld\n", ans);
return 0;
}
| ### Prompt
Develop a solution in Cpp to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <cstdio>
using namespace std;
const int mod = 1000000007;
int p[600];
long long c[601];
long long dp[600][601];
long long calc(int x, int y) {
int cnt = 0, i;
if (dp[x][y] >= 0) return dp[x][y];
if (y <= x) return dp[x][y] = 0;
if ((y - x) % 2 == 1) return dp[x][y] = 0;
for (i = x; i < y; i++) {
if (p[i] == -1) {
cnt++;
} else if (p[i] < x || p[i] >= y) {
return dp[x][y] = 0;
}
}
dp[x][y] = c[cnt];
for (i = x; i < y; i++) {
dp[x][y] -= calc(x, i) * c[cnt] % mod;
if (dp[x][y] < 0) dp[x][y] += mod;
if (p[i] == -1) cnt--;
}
return dp[x][y];
}
int main() {
int n, k, i, j, l;
long long ans = 0;
scanf("%d %d", &n, &k);
n *= 2;
for (i = 0; i < n; i++) p[i] = -1;
for (i = 0; i < k; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--;
b--;
p[a] = b;
p[b] = a;
}
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++) {
dp[i][j] = -1;
}
}
c[0] = 1;
for (i = 2; i <= n; i += 2) c[i] = c[i - 2] * (i - 1) % mod;
for (i = 0; i < n; i++) {
for (j = i + 1; j <= n; j++) {
int cnt = 0;
for (l = 0; l < i; l++) {
if (p[l] == -1) cnt++;
}
for (l = j; l < n; l++) {
if (p[l] == -1) cnt++;
}
ans += calc(i, j) * c[cnt] % mod;
if (ans >= mod) ans -= mod;
}
}
printf("%lld\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int N,K;
int OP[1606];
ll fact2[3030];
ll mo=1000000007;
ll memo[603][603];
ll dp(int L,int R) {
if((R-L)%2==0) return 0;
if(memo[L][R]>=0) return memo[L][R];
int i;
int NF=0;
for(i=L;i<=R;i++) {
if(OP[i]!=-1 && (OP[i]<L || OP[i]>R)) return memo[L][R]=0;
NF+=OP[i]==-1;
}
ll ret=fact2[NF];
int NF2=0;
for(i=L;i<R;i++) {
NF2+=OP[i]==-1;
ret+=mo-dp(L,i)*fact2[NF-NF2]%mo;
}
return memo[L][R]=ret%mo;
}
void solve() {
int i,j,k,l,r,x,y; string s;
fact2[0]=1;
for(i=2;i<=600;i+=2) fact2[i]=fact2[i-2]*(i-1)%mo;
MINUS(OP);
MINUS(memo);
cin>>N>>K;
N*=2;
FOR(i,K) {
cin>>x>>y;
OP[x-1]=y-1;
OP[y-1]=x-1;
}
int NFS=0;
FOR(i,N) NFS+=(OP[i]==-1);
ll ret=0;
FOR(x,N) {
int NF=0;
for(y=x;y<N;y++) {
NF+=OP[y]==-1;
ret+=dp(x,y)*fact2[NFS-NF]%mo;
}
}
cout<<ret%mo<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int N,K;
int OP[1606];
ll fact2[3030];
ll mo=1000000007;
ll memo[603][603];
ll dp(int L,int R) {
if((R-L)%2==0) return 0;
if(memo[L][R]>=0) return memo[L][R];
int i;
int NF=0;
for(i=L;i<=R;i++) {
if(OP[i]!=-1 && (OP[i]<L || OP[i]>R)) return memo[L][R]=0;
NF+=OP[i]==-1;
}
ll ret=fact2[NF];
int NF2=0;
for(i=L;i<R;i++) {
NF2+=OP[i]==-1;
ret+=mo-dp(L,i)*fact2[NF-NF2]%mo;
}
return memo[L][R]=ret%mo;
}
void solve() {
int i,j,k,l,r,x,y; string s;
fact2[0]=1;
for(i=2;i<=600;i+=2) fact2[i]=fact2[i-2]*(i-1)%mo;
MINUS(OP);
MINUS(memo);
cin>>N>>K;
N*=2;
FOR(i,K) {
cin>>x>>y;
OP[x-1]=y-1;
OP[y-1]=x-1;
}
int NFS=0;
FOR(i,N) NFS+=(OP[i]==-1);
ll ret=0;
FOR(x,N) {
int NF=0;
for(y=x;y<N;y++) {
NF+=OP[y]==-1;
ret+=dp(x,y)*fact2[NFS-NF]%mo;
}
}
cout<<ret%mo<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
``` |
#include<bits/stdc++.h>
#define fi first
#define se second
#ifdef CX_TEST
#define Debug printf
#else
#define Debug(...)
#endif
#define LL long long
using namespace std;
typedef pair<int, int> P;
const int maxn = 605;
const int mod = 1e9 + 7;
void add(int &x, int y) {
x += y;
if(x >= mod) x -= mod;
}
int mul(int x, int y) {
LL z = 1LL * x * y;
return z - z / mod * mod;
}
int g[maxn], f[maxn][maxn], d[maxn][maxn], c[maxn];
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n, m, i, j, k, l, w = 0;
scanf("%d%d", &n, &m);
n *= 2;
for(i = 0; i < m; i++) {
scanf("%d%d", &j, &k);
c[j] = k;
c[k] = j;
}
m = n - m * 2;
g[0] = 1;
for(i = 2; i <= n; i += 2) g[i] = mul(g[i - 2], i - 1);
for(i = 1; i <= n; i++) {
for(j = i; j <= n; j++) d[i][j] = d[i][j - 1] + (c[j] == 0);
}
for(l = 2; l <= n; l++) {
for(i = 1; i <= n; i++) {
j = i + l - 1;
if(j > n) break;
for(k = i; k <= j; k++) {
if(c[k] && c[k] < i || c[k] > j) break;
}
if(k <= j || (d[i][j] & 1)) continue;
f[i][j] = g[d[i][j]];
for(k = i + 1; k < j; k++) add(f[i][j], mod - mul(f[i][k], g[d[k + 1][j]]));
add(w, mul(f[i][j], g[m - d[i][j]]));
}
}
cout << w << endl;
return 0;
}
| ### Prompt
Your task is to create a CPP solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
#define fi first
#define se second
#ifdef CX_TEST
#define Debug printf
#else
#define Debug(...)
#endif
#define LL long long
using namespace std;
typedef pair<int, int> P;
const int maxn = 605;
const int mod = 1e9 + 7;
void add(int &x, int y) {
x += y;
if(x >= mod) x -= mod;
}
int mul(int x, int y) {
LL z = 1LL * x * y;
return z - z / mod * mod;
}
int g[maxn], f[maxn][maxn], d[maxn][maxn], c[maxn];
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n, m, i, j, k, l, w = 0;
scanf("%d%d", &n, &m);
n *= 2;
for(i = 0; i < m; i++) {
scanf("%d%d", &j, &k);
c[j] = k;
c[k] = j;
}
m = n - m * 2;
g[0] = 1;
for(i = 2; i <= n; i += 2) g[i] = mul(g[i - 2], i - 1);
for(i = 1; i <= n; i++) {
for(j = i; j <= n; j++) d[i][j] = d[i][j - 1] + (c[j] == 0);
}
for(l = 2; l <= n; l++) {
for(i = 1; i <= n; i++) {
j = i + l - 1;
if(j > n) break;
for(k = i; k <= j; k++) {
if(c[k] && c[k] < i || c[k] > j) break;
}
if(k <= j || (d[i][j] & 1)) continue;
f[i][j] = g[d[i][j]];
for(k = i + 1; k < j; k++) add(f[i][j], mod - mul(f[i][k], g[d[k + 1][j]]));
add(w, mul(f[i][j], g[m - d[i][j]]));
}
}
cout << w << endl;
return 0;
}
``` |
#include<iostream>
#include<cstdio>
using namespace std;const int mod=1000000007;
int pre[605],f[605][605],g[605],a[605];
inline int calc(int l,int r) {return (r-l+1)-(pre[r]-pre[l-1]);}
int main() {int n,kkk,u,v,ans=0;
scanf("%d%d",&n,&kkk),n<<=1;for (int i=1;i<=kkk;++i) {
scanf("%d%d",&u,&v),pre[a[u]=v]=pre[a[v]=u]=1;
}for (int i=1;i<=n;++i) pre[i]+=pre[i-1];
*g=1;for (int i=2;i<=n;i+=2) g[i]=1LL*g[i-2]*(i-1)%mod;
for (int i=1,j,mk;i<=n;++i) for (j=i;j<=n;++j) if (i+j&1) {mk=0;
for (int k=i;k<=j;++k) if (a[k]&&(a[k]<i||a[k]>j)) mk=1;
if (mk) continue;f[i][j]=g[calc(i,j)];
for (int k=i+1;k<j;++k) f[i][j]=(f[i][j]-1LL*f[i][k]*g[calc(k+1,j)]%mod+mod)%mod;
}kkk<<=1;for (int i=1;i<=n;++i) for (int j=i;j<=n;++j) if (f[i][j]) {
ans=(ans+1LL*f[i][j]*g[n-(j-i+1)-(kkk-(pre[j]-pre[i-1]))])%mod;
}printf("%d",ans);return 0;
} | ### Prompt
Your task is to create a Cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstdio>
using namespace std;const int mod=1000000007;
int pre[605],f[605][605],g[605],a[605];
inline int calc(int l,int r) {return (r-l+1)-(pre[r]-pre[l-1]);}
int main() {int n,kkk,u,v,ans=0;
scanf("%d%d",&n,&kkk),n<<=1;for (int i=1;i<=kkk;++i) {
scanf("%d%d",&u,&v),pre[a[u]=v]=pre[a[v]=u]=1;
}for (int i=1;i<=n;++i) pre[i]+=pre[i-1];
*g=1;for (int i=2;i<=n;i+=2) g[i]=1LL*g[i-2]*(i-1)%mod;
for (int i=1,j,mk;i<=n;++i) for (j=i;j<=n;++j) if (i+j&1) {mk=0;
for (int k=i;k<=j;++k) if (a[k]&&(a[k]<i||a[k]>j)) mk=1;
if (mk) continue;f[i][j]=g[calc(i,j)];
for (int k=i+1;k<j;++k) f[i][j]=(f[i][j]-1LL*f[i][k]*g[calc(k+1,j)]%mod+mod)%mod;
}kkk<<=1;for (int i=1;i<=n;++i) for (int j=i;j<=n;++j) if (f[i][j]) {
ans=(ans+1LL*f[i][j]*g[n-(j-i+1)-(kkk-(pre[j]-pre[i-1]))])%mod;
}printf("%d",ans);return 0;
}
``` |
#include <bits/stdc++.h>
#define rep(i,n) for ((i)=1;(i)<=(n);(i)++)
#define foreach(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr)
using namespace std;
const int mod=1e9+7;
int n,m,i,j,lk[605],dp[605][605],f[605],cnt[605],ans;
int dfs(int l,int r){
if((cnt[r]-cnt[l-1])&1) return 0;
int i;
for(i=l;i<=r;i++) if(lk[i]&&(lk[i]<l||lk[i]>r)) return 0;
if(dp[l][r]!=-1) return dp[l][r];
dp[l][r]=f[cnt[r]-cnt[l-1]];
for(i=l;i<r;i++){
dp[l][r]=(dp[l][r]+1ll*(mod-f[cnt[r]-cnt[i]])*dfs(l,i))%mod;
}
return dp[l][r];
}
int main(){
memset(dp,-1,sizeof(dp));
cin>>n>>m;
n*=2;
rep(i,m){
int x,y;
cin>>x>>y;
lk[x]=y;
lk[y]=x;
}
f[0]=1;
rep(i,n/2){
f[i*2]=1ll*f[i*2-2]*(i*2-1)%mod;
}
rep(i,n){
cnt[i]=cnt[i-1]+(!lk[i]);
}
rep(i,n){
for(j=i;j<=n;j++){
ans=(ans+1ll*dfs(i,j)*f[n-m-m-(cnt[j]-cnt[i-1])])%mod;
}
}
cout<<ans<<endl;
return 0;
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
#define rep(i,n) for ((i)=1;(i)<=(n);(i)++)
#define foreach(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr)
using namespace std;
const int mod=1e9+7;
int n,m,i,j,lk[605],dp[605][605],f[605],cnt[605],ans;
int dfs(int l,int r){
if((cnt[r]-cnt[l-1])&1) return 0;
int i;
for(i=l;i<=r;i++) if(lk[i]&&(lk[i]<l||lk[i]>r)) return 0;
if(dp[l][r]!=-1) return dp[l][r];
dp[l][r]=f[cnt[r]-cnt[l-1]];
for(i=l;i<r;i++){
dp[l][r]=(dp[l][r]+1ll*(mod-f[cnt[r]-cnt[i]])*dfs(l,i))%mod;
}
return dp[l][r];
}
int main(){
memset(dp,-1,sizeof(dp));
cin>>n>>m;
n*=2;
rep(i,m){
int x,y;
cin>>x>>y;
lk[x]=y;
lk[y]=x;
}
f[0]=1;
rep(i,n/2){
f[i*2]=1ll*f[i*2-2]*(i*2-1)%mod;
}
rep(i,n){
cnt[i]=cnt[i-1]+(!lk[i]);
}
rep(i,n){
for(j=i;j<=n;j++){
ans=(ans+1ll*dfs(i,j)*f[n-m-m-(cnt[j]-cnt[i-1])])%mod;
}
}
cout<<ans<<endl;
return 0;
}
``` |
#ifndef BZ
#pragma GCC optimize "Ofast"
#endif
#include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using ll = int64_t;
using ld = long double;
const ll MOD = 1e9 + 7;
ll pw(ll a, ll b) {
if (!b) {
return 1;
}
ll v = pw(a, b / 2);
v = (v * v) % MOD;
if (b & 1) {
v = (v * a) % MOD;
}
return v;
}
const int MAXN = 602;
ll f[MAXN];
ll g[MAXN][MAXN];
ll h[MAXN][MAXN];
int a[MAXN], b[MAXN];
int main() {
#ifdef BZ
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20);
int n, k;
cin >> n >> k;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
f[i] = f[i - 1] * (2 * i - 1) % MOD;
}
for (int i = 0; i < k; ++i) {
cin >> a[i] >> b[i];
}
ll ans = f[n - k];
for (int l = 2 * n - 2; l >= 0; --l) {
for (int r = l + 2; r < 2 * n; r += 2) {
int c = (r - l) / 2;
bool bad = false;
for (int j = 0; j < k; ++j) {
bool ga = (l < a[j] && a[j] <= r);
bool gb = (l < b[j] && b[j] <= r);
bad |= (ga != gb);
c -= ga;
}
if (bad) {
continue;
}
ll out = f[n - k - c];
g[l][r] = f[c];
h[l][r] = g[l][r];
for (int q = l + 2; q < r; q += 2) {
h[l][r] = (h[l][r] + MOD * MOD - g[l][q] * h[q][r]) % MOD;
}
ans = (ans + h[l][r] * out) % MOD;
}
}
cout << ans << "\n";
}
| ### Prompt
Please create a solution in cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#ifndef BZ
#pragma GCC optimize "Ofast"
#endif
#include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using ll = int64_t;
using ld = long double;
const ll MOD = 1e9 + 7;
ll pw(ll a, ll b) {
if (!b) {
return 1;
}
ll v = pw(a, b / 2);
v = (v * v) % MOD;
if (b & 1) {
v = (v * a) % MOD;
}
return v;
}
const int MAXN = 602;
ll f[MAXN];
ll g[MAXN][MAXN];
ll h[MAXN][MAXN];
int a[MAXN], b[MAXN];
int main() {
#ifdef BZ
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20);
int n, k;
cin >> n >> k;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
f[i] = f[i - 1] * (2 * i - 1) % MOD;
}
for (int i = 0; i < k; ++i) {
cin >> a[i] >> b[i];
}
ll ans = f[n - k];
for (int l = 2 * n - 2; l >= 0; --l) {
for (int r = l + 2; r < 2 * n; r += 2) {
int c = (r - l) / 2;
bool bad = false;
for (int j = 0; j < k; ++j) {
bool ga = (l < a[j] && a[j] <= r);
bool gb = (l < b[j] && b[j] <= r);
bad |= (ga != gb);
c -= ga;
}
if (bad) {
continue;
}
ll out = f[n - k - c];
g[l][r] = f[c];
h[l][r] = g[l][r];
for (int q = l + 2; q < r; q += 2) {
h[l][r] = (h[l][r] + MOD * MOD - g[l][q] * h[q][r]) % MOD;
}
ans = (ans + h[l][r] * out) % MOD;
}
}
cout << ans << "\n";
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N = 666;
const int mod = 1e9 + 7;
int f[N][N], g[N], s[N];
int n, K, to[N];
inline int A(int x, int y){
return x + y - (x + y >= mod ? mod : 0);
}
inline int M(int x, int y){
return 1LL * x * y % mod;
}
int main(){
cin>>n>>K;
n <<= 1;
int u, v;
for(int i = 1;i <= K; i++){
scanf("%d%d", &u, &v);
to[u] = v;
to[v] = u;
}
g[0] = 1;
for(int i = 1;i <= n; i++){
s[i] = (!to[i]) + s[i-1];
if(i > 1)g[i] = M(i - 1, g[i-2]);
}
for(int l = 1;l <= n; l++){
if(to[l]){
if(to[l] < l)continue;
bool fl = 1;
for(int k = l + 1;k < to[l]; k++)
if(to[k] && (to[k] > to[l] || to[k] < l))fl = 0;
f[l][to[l]] = fl ? g[s[to[l]]-s[l-1]] : 0;
}
for(int r = max(l + 1, to[l] + 1); r <= n; r++){
bool fl = 1;
for(int k = l + 1;k <= r; k++){
if(to[k] && (to[k] > r || to[k] < l))fl = 0;
}
if(!fl)continue;
int F = g[s[r]-s[l-1]];
for(int p = l + 1;p < r; p++){
F = A(F, mod - M(f[l][p], g[s[r]-s[p]]));
}
f[l][r] = F;
}
}
int ans = 0;
for(int i = 1;i <= n; i++){
for(int j = i + 1;j <= n; j++){
ans = A(ans, M(f[i][j], g[s[n]-(s[j]-s[i-1])]));
// printf("f[%d][%d]=%d\n", i, j, f[i][j]);
}
}
cout<<ans<<endl;
return 0;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N = 666;
const int mod = 1e9 + 7;
int f[N][N], g[N], s[N];
int n, K, to[N];
inline int A(int x, int y){
return x + y - (x + y >= mod ? mod : 0);
}
inline int M(int x, int y){
return 1LL * x * y % mod;
}
int main(){
cin>>n>>K;
n <<= 1;
int u, v;
for(int i = 1;i <= K; i++){
scanf("%d%d", &u, &v);
to[u] = v;
to[v] = u;
}
g[0] = 1;
for(int i = 1;i <= n; i++){
s[i] = (!to[i]) + s[i-1];
if(i > 1)g[i] = M(i - 1, g[i-2]);
}
for(int l = 1;l <= n; l++){
if(to[l]){
if(to[l] < l)continue;
bool fl = 1;
for(int k = l + 1;k < to[l]; k++)
if(to[k] && (to[k] > to[l] || to[k] < l))fl = 0;
f[l][to[l]] = fl ? g[s[to[l]]-s[l-1]] : 0;
}
for(int r = max(l + 1, to[l] + 1); r <= n; r++){
bool fl = 1;
for(int k = l + 1;k <= r; k++){
if(to[k] && (to[k] > r || to[k] < l))fl = 0;
}
if(!fl)continue;
int F = g[s[r]-s[l-1]];
for(int p = l + 1;p < r; p++){
F = A(F, mod - M(f[l][p], g[s[r]-s[p]]));
}
f[l][r] = F;
}
}
int ans = 0;
for(int i = 1;i <= n; i++){
for(int j = i + 1;j <= n; j++){
ans = A(ans, M(f[i][j], g[s[n]-(s[j]-s[i-1])]));
// printf("f[%d][%d]=%d\n", i, j, f[i][j]);
}
}
cout<<ans<<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int MX = 300, md = 1000000007;
int a[MX], b[MX], f[2 * MX], dp[2 * MX][2 * MX], freeCnt[2 * MX], n;
bool inside(int l, int r, int x) {
if (l <= r) return l <= x && x <= r;
return l <= x || x <= r;
}
int getFree(int l, int r) {
int h;
if (l <= r) h = freeCnt[r] - (l > 0 ? freeCnt[l - 1] : 0);
else h = freeCnt[r] + freeCnt[2 * n - 1] - freeCnt[l - 1];
return h;
}
int main() {
int k;
ignore = scanf("%d %d", &n, &k);
fill(freeCnt, freeCnt + 2 * n, 1);
for (int i = 0; i < k; i++) {
ignore = scanf("%d %d", a + i, b + i);
a[i]--;
b[i]--;
freeCnt[a[i]] = freeCnt[b[i]] = 0;
}
partial_sum(freeCnt, freeCnt + 2 * n, freeCnt);
f[1] = 1;
for (int i = 3; i < 2 * n; i += 2) f[i] = f[i - 2] * 1ll * i % md;
int ans = 0;
for (int len = 2; len <= 2 * n; len += 2) {
for (int l = 0, r = len - 1; r < 2 * n; l++, r++) {
bool ok = true;
for (int e = 0; e < k && ok; e++) {
ok = inside(l, r, a[e]) == inside(l, r, b[e]);
}
if (ok == false) continue;
int h = getFree(l, r);
dp[l][r] = f[max(h, 2) - 1];
for (int lenp = 2, rp = (l + 1) % (2 * n); lenp < len; lenp += 2, rp = (rp + 2) % (2 * n)) {
int g = getFree((rp + 1) % (2 * n), r);
dp[l][r] = (dp[l][r] - dp[l][rp] * 1ll * f[max(2, g) - 1]) % md;
}
if (dp[l][r] < 0) dp[l][r] += md;
ans = (ans + dp[l][r] * 1ll * f[max(2, 2 * (n - k) - h) - 1]) % md;
}
}
printf("%d\n", ans);
return 0;
}
| ### Prompt
Create a solution in Cpp for the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = 300, md = 1000000007;
int a[MX], b[MX], f[2 * MX], dp[2 * MX][2 * MX], freeCnt[2 * MX], n;
bool inside(int l, int r, int x) {
if (l <= r) return l <= x && x <= r;
return l <= x || x <= r;
}
int getFree(int l, int r) {
int h;
if (l <= r) h = freeCnt[r] - (l > 0 ? freeCnt[l - 1] : 0);
else h = freeCnt[r] + freeCnt[2 * n - 1] - freeCnt[l - 1];
return h;
}
int main() {
int k;
ignore = scanf("%d %d", &n, &k);
fill(freeCnt, freeCnt + 2 * n, 1);
for (int i = 0; i < k; i++) {
ignore = scanf("%d %d", a + i, b + i);
a[i]--;
b[i]--;
freeCnt[a[i]] = freeCnt[b[i]] = 0;
}
partial_sum(freeCnt, freeCnt + 2 * n, freeCnt);
f[1] = 1;
for (int i = 3; i < 2 * n; i += 2) f[i] = f[i - 2] * 1ll * i % md;
int ans = 0;
for (int len = 2; len <= 2 * n; len += 2) {
for (int l = 0, r = len - 1; r < 2 * n; l++, r++) {
bool ok = true;
for (int e = 0; e < k && ok; e++) {
ok = inside(l, r, a[e]) == inside(l, r, b[e]);
}
if (ok == false) continue;
int h = getFree(l, r);
dp[l][r] = f[max(h, 2) - 1];
for (int lenp = 2, rp = (l + 1) % (2 * n); lenp < len; lenp += 2, rp = (rp + 2) % (2 * n)) {
int g = getFree((rp + 1) % (2 * n), r);
dp[l][r] = (dp[l][r] - dp[l][rp] * 1ll * f[max(2, g) - 1]) % md;
}
if (dp[l][r] < 0) dp[l][r] += md;
ans = (ans + dp[l][r] * 1ll * f[max(2, 2 * (n - k) - h) - 1]) % md;
}
}
printf("%d\n", ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int Maxn=605,mod=1e9+7;
int p[Maxn],g[Maxn],f[Maxn][Maxn],cnt[Maxn][Maxn];
int main(){
int n,k,ans=0;cin>>n>>k;
g[0]=1;
for(int i=2;i<=2*n;i+=2)g[i]=1ll*g[i-2]*(i-1)%mod;
for(int i=1;i<=k;i++){
int x,y;cin>>x>>y;
p[x]=y;p[y]=x;
}
for(int i=1;i<=2*n;i++)
for(int j=i;j<=2*n;j++)
cnt[i][j]=cnt[i][j-1]+(!p[j]);
for(int len=1;len<=2*n;len++)
for(int l=1,r=len;r<=2*n;l++,r++){
bool succ=true;
for(int k=l;k<=r;k++)
if(p[k]&&(p[k]<l||p[k]>r))
succ=false;
if(!succ)continue;
f[l][r]=g[cnt[l][r]];
for(int k=l+1;k<r;k++)
f[l][r]=(f[l][r]-1ll*f[l][k]*g[cnt[k+1][r]])%mod;
ans=(ans+1ll*f[l][r]*g[cnt[1][l-1]+cnt[r+1][2*n]])%mod;
}
cout<<(ans+mod)%mod<<"\n";
return 0;
} | ### Prompt
In cpp, your task is to solve the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Maxn=605,mod=1e9+7;
int p[Maxn],g[Maxn],f[Maxn][Maxn],cnt[Maxn][Maxn];
int main(){
int n,k,ans=0;cin>>n>>k;
g[0]=1;
for(int i=2;i<=2*n;i+=2)g[i]=1ll*g[i-2]*(i-1)%mod;
for(int i=1;i<=k;i++){
int x,y;cin>>x>>y;
p[x]=y;p[y]=x;
}
for(int i=1;i<=2*n;i++)
for(int j=i;j<=2*n;j++)
cnt[i][j]=cnt[i][j-1]+(!p[j]);
for(int len=1;len<=2*n;len++)
for(int l=1,r=len;r<=2*n;l++,r++){
bool succ=true;
for(int k=l;k<=r;k++)
if(p[k]&&(p[k]<l||p[k]>r))
succ=false;
if(!succ)continue;
f[l][r]=g[cnt[l][r]];
for(int k=l+1;k<r;k++)
f[l][r]=(f[l][r]-1ll*f[l][k]*g[cnt[k+1][r]])%mod;
ans=(ans+1ll*f[l][r]*g[cnt[1][l-1]+cnt[r+1][2*n]])%mod;
}
cout<<(ans+mod)%mod<<"\n";
return 0;
}
``` |
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
int n,k,h[605],sum[605];
long long g[605],dp[605][605];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
h[x]=y,h[y]=x;
sum[x]=1,sum[y]=1;
}
n<<=1;g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=(i-1)*g[i-2]%mod;
for(int i=2;i<=n;i++) sum[i]+=sum[i-1];
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
if((j-i)&1)
{
bool flag=0;
for(int k=i;k<=j;k++)
{
if(h[k]&&(h[k]<i||h[k]>j))
{
flag=1;
break;
}
}
if(flag) continue;
dp[i][j]=g[j-i+1-sum[j]+sum[i-1]];
for(int k=i+1;k<j;k++) dp[i][j]=(dp[i][j]-dp[i][k]*g[j-k-sum[j]+sum[k]]%mod)%mod;
}
}
k<<=1;
long long ans=0;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
if(dp[i][j]) ans=(ans+dp[i][j]*g[n-(j-i+1)-(k-sum[j]+sum[i-1])])%mod;
printf("%lld\n",ans);
} | ### Prompt
Develop a solution in cpp to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
int n,k,h[605],sum[605];
long long g[605],dp[605][605];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
h[x]=y,h[y]=x;
sum[x]=1,sum[y]=1;
}
n<<=1;g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=(i-1)*g[i-2]%mod;
for(int i=2;i<=n;i++) sum[i]+=sum[i-1];
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
if((j-i)&1)
{
bool flag=0;
for(int k=i;k<=j;k++)
{
if(h[k]&&(h[k]<i||h[k]>j))
{
flag=1;
break;
}
}
if(flag) continue;
dp[i][j]=g[j-i+1-sum[j]+sum[i-1]];
for(int k=i+1;k<j;k++) dp[i][j]=(dp[i][j]-dp[i][k]*g[j-k-sum[j]+sum[k]]%mod)%mod;
}
}
k<<=1;
long long ans=0;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
if(dp[i][j]) ans=(ans+dp[i][j]*g[n-(j-i+1)-(k-sum[j]+sum[i-1])])%mod;
printf("%lld\n",ans);
}
``` |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int dp[606][606];
int a[303],b[303];
int cnt[606];
int h[606];
int main()
{
int n,m,ans=0;
bool ok;
scanf("%d%d",&n,&m);
n<<=1;
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)
cnt[i]=1;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a[i],&b[i]);
cnt[a[i]]--;
cnt[b[i]]--;
}
for(int i=2;i<=n;i++)
cnt[i]+=cnt[i-1];
memset(h,0,sizeof(h));
h[0]=1;
for(int i=2;i<=n;i+=2)
h[i]=(long long)h[i-2]*(i-1)%mod;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
ok=true;
for(int k=1;k<=m;k++)
if((i<=a[k]&&a[k]<=j)!=(i<=b[k]&&b[k]<=j))
ok=false;
if(ok)
{
dp[i][j]=h[cnt[j]-cnt[i-1]];
for(int k=i;k<j;k++)
(dp[i][j]+=mod-(long long)dp[i][k]*h[cnt[j]-cnt[k]]%mod)%=mod;
ans=((long long)dp[i][j]*h[cnt[n]-cnt[j]+cnt[i-1]]+ans)%mod;
}
}
printf("%d",ans);
return 0;
} | ### Prompt
Please create a solution in cpp to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int dp[606][606];
int a[303],b[303];
int cnt[606];
int h[606];
int main()
{
int n,m,ans=0;
bool ok;
scanf("%d%d",&n,&m);
n<<=1;
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)
cnt[i]=1;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a[i],&b[i]);
cnt[a[i]]--;
cnt[b[i]]--;
}
for(int i=2;i<=n;i++)
cnt[i]+=cnt[i-1];
memset(h,0,sizeof(h));
h[0]=1;
for(int i=2;i<=n;i+=2)
h[i]=(long long)h[i-2]*(i-1)%mod;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
{
ok=true;
for(int k=1;k<=m;k++)
if((i<=a[k]&&a[k]<=j)!=(i<=b[k]&&b[k]<=j))
ok=false;
if(ok)
{
dp[i][j]=h[cnt[j]-cnt[i-1]];
for(int k=i;k<j;k++)
(dp[i][j]+=mod-(long long)dp[i][k]*h[cnt[j]-cnt[k]]%mod)%=mod;
ans=((long long)dp[i][j]*h[cnt[n]-cnt[j]+cnt[i-1]]+ans)%mod;
}
}
printf("%d",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
const ll MOD = 1000000007;
const int MN = 610;
int N, K;
ll dp[MN][MN];
int mt[MN];
ll sub[MN];
int in[MN][MN];
int main() {
sub[0] = 1;
for (int i = 2; i < MN; i += 2) {
sub[i] = sub[i-2] * (i-1) % MOD;
}
cin >> N >> K;
memset(mt, -1, sizeof(mt));
rep(i, K) {
int a, b;
cin >> a >> b;
--a; --b;
mt[a] = b; mt[b] = a;
}
N *= 2;
for (int l = 0; l < N; ++l) {
for (int r = l; r < N; ++r) {
bool ok = 1;
int num = 0;
for (int m = l; m <= r; ++m) {
if (mt[m] != -1) {
if (mt[m] < l || mt[m] > r) {
ok = 0;
} else {
num++;
}
}
}
if (ok) {
in[l][r] = num;
} else {
in[l][r] = -1;
}
}
}
for (int len = 2; len <= N; len += 2) {
for (int l = 0; l <= N - len; ++l) {
int r = l + len - 1;
if (in[l][r] == -1) continue;
ll t = sub[len - in[l][r]];
for (int m = l+1; m < r; m += 2) if (in[m+1][r] != -1) {
t = (t - dp[l][m] * sub[r - m - in[m+1][r]]) % MOD;
if (t < 0) t += MOD;
}
dp[l][r] = t;
}
}
ll ret = 0;
for (int i = 0; i < N; ++i) {
for (int j = i; j < N; ++j) {
if (dp[i][j] == 0) continue;
int dec = N - K * 2 - (j - i + 1 - in[i][j]);
ret = (ret + dp[i][j] * sub[dec]) % MOD;
}
}
cout << ret << endl;
return 0;
} | ### Prompt
Generate a cpp solution to the following problem:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
const ll MOD = 1000000007;
const int MN = 610;
int N, K;
ll dp[MN][MN];
int mt[MN];
ll sub[MN];
int in[MN][MN];
int main() {
sub[0] = 1;
for (int i = 2; i < MN; i += 2) {
sub[i] = sub[i-2] * (i-1) % MOD;
}
cin >> N >> K;
memset(mt, -1, sizeof(mt));
rep(i, K) {
int a, b;
cin >> a >> b;
--a; --b;
mt[a] = b; mt[b] = a;
}
N *= 2;
for (int l = 0; l < N; ++l) {
for (int r = l; r < N; ++r) {
bool ok = 1;
int num = 0;
for (int m = l; m <= r; ++m) {
if (mt[m] != -1) {
if (mt[m] < l || mt[m] > r) {
ok = 0;
} else {
num++;
}
}
}
if (ok) {
in[l][r] = num;
} else {
in[l][r] = -1;
}
}
}
for (int len = 2; len <= N; len += 2) {
for (int l = 0; l <= N - len; ++l) {
int r = l + len - 1;
if (in[l][r] == -1) continue;
ll t = sub[len - in[l][r]];
for (int m = l+1; m < r; m += 2) if (in[m+1][r] != -1) {
t = (t - dp[l][m] * sub[r - m - in[m+1][r]]) % MOD;
if (t < 0) t += MOD;
}
dp[l][r] = t;
}
}
ll ret = 0;
for (int i = 0; i < N; ++i) {
for (int j = i; j < N; ++j) {
if (dp[i][j] == 0) continue;
int dec = N - K * 2 - (j - i + 1 - in[i][j]);
ret = (ret + dp[i][j] * sub[dec]) % MOD;
}
}
cout << ret << endl;
return 0;
}
``` |
#include<cstdio>
#define N 606
#define p 1000000007
#define ll long long
int a[N],f[N][N],g[N],h[N],i,j,k,m,n;
int main()
{
scanf("%d%d",&n,&m),n<<=1;
for(*g=i=1;i<=n;i++)h[a[i]=i]=1;
for(i=1;i<=m;i++)scanf("%d%d",&j,&k),h[a[k]=j]=h[a[j]=k]=0;
for(i=2;i<=n;i+=2)g[i]=(ll)(i-1)*g[i-2]%p;
for(i=1;i<=n;i++)h[i]+=h[i-1];
for(i=n;i;i--)for(j=i;j<=n;j++)for(f[k=i][j]=g[h[j]-h[i-1]];k<=j&&f[i][j];k++)if(a[k]<i||j<a[k])f[i][j]=0;
for(i=n;i;i--)for(j=i;j<=n;j++)if(f[i][j])for(k=i;k<j;k++)f[i][j]=(f[i][j]+(ll)(p-f[i][k])*g[h[j]-h[k]])%p;
for(i=n,k=0;i;i--)for(j=i;j<=n;j++)k=(k+(ll)f[i][j]*g[h[n]-h[j]+h[i-1]])%p;
return 0*printf("%d\n",k);
} | ### Prompt
Develop a solution in cpp to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<cstdio>
#define N 606
#define p 1000000007
#define ll long long
int a[N],f[N][N],g[N],h[N],i,j,k,m,n;
int main()
{
scanf("%d%d",&n,&m),n<<=1;
for(*g=i=1;i<=n;i++)h[a[i]=i]=1;
for(i=1;i<=m;i++)scanf("%d%d",&j,&k),h[a[k]=j]=h[a[j]=k]=0;
for(i=2;i<=n;i+=2)g[i]=(ll)(i-1)*g[i-2]%p;
for(i=1;i<=n;i++)h[i]+=h[i-1];
for(i=n;i;i--)for(j=i;j<=n;j++)for(f[k=i][j]=g[h[j]-h[i-1]];k<=j&&f[i][j];k++)if(a[k]<i||j<a[k])f[i][j]=0;
for(i=n;i;i--)for(j=i;j<=n;j++)if(f[i][j])for(k=i;k<j;k++)f[i][j]=(f[i][j]+(ll)(p-f[i][k])*g[h[j]-h[k]])%p;
for(i=n,k=0;i;i--)for(j=i;j<=n;j++)k=(k+(ll)f[i][j]*g[h[n]-h[j]+h[i-1]])%p;
return 0*printf("%d\n",k);
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int N=610;
const int M=1e9+7;
typedef long long ll;
int n,m,op[N],g[N],s[N];
bool empty[N];
int ans,f[N][N];
int main(){
scanf("%d%d",&n,&m);
n*=2;
for (int i=1; i<=n; ++i) empty[i]=1;
for (int i=1,x,y; i<=m; ++i){
scanf("%d%d",&x,&y);
op[x]=y;
op[y]=x;
empty[x]=0;
empty[y]=0;
}
g[0]=1;
for (int i=2; i<=n; i+=2) g[i]=(ll)g[i-2]*(i-1)%M;
for (int i=1; i<=n; ++i) s[i]=s[i-1]+empty[i];
//for (int i=1; i<=n; ++i) cerr<<i<<" "<<s[i]<<endl;
for (int k=1; k<=n; ++k)
for (int i=1; i+k-1<=n; ++i){
int j=i+k-1;
if ((s[j]-s[i-1])&1) continue;
bool fl=0;
for (int l=i; l<=j; ++l) if (!empty[l]&&(op[l]<i||op[l]>j)) fl=1;
if (fl) continue;
int &ret=f[i][j];
ret=g[s[j]-s[i-1]];
for (int l=i; l<j; ++l)
(ret+=M-(ll)f[i][l]*g[s[j]-s[l]]%M)%=M;
(ans+=(ll)ret*g[n-2*m-s[j]+s[i-1]]%M)%=M;
//cerr<<i<<" "<<j<<" "<<f[i][j]<<endl;
}
cout<<ans;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N=610;
const int M=1e9+7;
typedef long long ll;
int n,m,op[N],g[N],s[N];
bool empty[N];
int ans,f[N][N];
int main(){
scanf("%d%d",&n,&m);
n*=2;
for (int i=1; i<=n; ++i) empty[i]=1;
for (int i=1,x,y; i<=m; ++i){
scanf("%d%d",&x,&y);
op[x]=y;
op[y]=x;
empty[x]=0;
empty[y]=0;
}
g[0]=1;
for (int i=2; i<=n; i+=2) g[i]=(ll)g[i-2]*(i-1)%M;
for (int i=1; i<=n; ++i) s[i]=s[i-1]+empty[i];
//for (int i=1; i<=n; ++i) cerr<<i<<" "<<s[i]<<endl;
for (int k=1; k<=n; ++k)
for (int i=1; i+k-1<=n; ++i){
int j=i+k-1;
if ((s[j]-s[i-1])&1) continue;
bool fl=0;
for (int l=i; l<=j; ++l) if (!empty[l]&&(op[l]<i||op[l]>j)) fl=1;
if (fl) continue;
int &ret=f[i][j];
ret=g[s[j]-s[i-1]];
for (int l=i; l<j; ++l)
(ret+=M-(ll)f[i][l]*g[s[j]-s[l]]%M)%=M;
(ans+=(ll)ret*g[n-2*m-s[j]+s[i-1]]%M)%=M;
//cerr<<i<<" "<<j<<" "<<f[i][j]<<endl;
}
cout<<ans;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int mul(int a,int b){
return 1LL * a * b % mod;
}
int n, m, x, y, t, sum;
int p[610], c[610][610] ,f[610][610], g[610];
int main(){
bool flag;
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> x >> y;
p[x] = y;
p[y] = x;
}
n <<= 1;
g[0] = 1;
for(int i = 2; i <= n; i += 2)
g[i] = mul(g[i - 2], i - 1);
for(int i = 1; i <= n;i++){
for(int j = i; j <= n; j++)
c[i][j] = c[i][j - 1] + !p[j];
}
sum = 0;
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
flag = 0;
for(int k = i; k <= j; k++){
if(p[k] && (p[k] < i || p[k] > j)){
flag = 1;
break;
}
}
if(flag)
continue;
int aux = g[c[i][j]];
for(int k = i + 1; k < j; k++)(aux-= mul(f[i][k], g[c[k + 1][j]])) %= mod;
f[i][j] = aux;
(sum += mul(f[i][j], g[n - m * 2 - c[i][j]]))%=mod;
}
}
cout << (sum + mod) % mod;
}
| ### Prompt
Develop a solution in CPP to the problem described below:
There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them.
Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge.
Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i.
He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7.
Constraints
* 1 \leq N \leq 300
* 0 \leq K \leq N
* 1 \leq A_i,B_i \leq 2N
* A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_K B_K
Output
Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs.
Examples
Input
2 0
Output
5
Input
4 2
5 2
6 1
Output
6
Input
20 10
10 18
11 17
14 7
4 6
30 28
19 24
29 22
25 32
38 34
36 39
Output
27087418
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int mul(int a,int b){
return 1LL * a * b % mod;
}
int n, m, x, y, t, sum;
int p[610], c[610][610] ,f[610][610], g[610];
int main(){
bool flag;
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> x >> y;
p[x] = y;
p[y] = x;
}
n <<= 1;
g[0] = 1;
for(int i = 2; i <= n; i += 2)
g[i] = mul(g[i - 2], i - 1);
for(int i = 1; i <= n;i++){
for(int j = i; j <= n; j++)
c[i][j] = c[i][j - 1] + !p[j];
}
sum = 0;
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++){
flag = 0;
for(int k = i; k <= j; k++){
if(p[k] && (p[k] < i || p[k] > j)){
flag = 1;
break;
}
}
if(flag)
continue;
int aux = g[c[i][j]];
for(int k = i + 1; k < j; k++)(aux-= mul(f[i][k], g[c[k + 1][j]])) %= mod;
f[i][j] = aux;
(sum += mul(f[i][j], g[n - m * 2 - c[i][j]]))%=mod;
}
}
cout << (sum + mod) % mod;
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.