output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include<cstdio>
using namespace std;
#define N 605
#define mod 1000000007
int dp[N][N],n,m,a,b,t[N],s[N],su[N],as;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d",&a,&b),t[a]=b,t[b]=a;
s[0]=1;for(int i=2;i<=n*2;i++)s[i]=1ll*s[i-2]*(i-1)%mod;
for(int i=1;i<=n*2;i++)su[i]=su[i-1]+!t[i];
for(int l=1;l<=n*2;l++)
for(int i=1;i+l<=n*2;i++)
{
int j=i+l;
int fg=1;
for(int k=i;k<=j;k++)if((t[k]<i||t[k]>j)&&t[k])fg=0;
if((su[j]^su[i-1])&1)fg=0;
if(!fg)continue;
int tp=s[su[j]-su[i-1]];
for(int k=i;k<j;k++)tp=(tp-1ll*dp[i][k]*s[su[j]-su[k]]%mod+mod)%mod;
dp[i][j]=tp;as=(as+1ll*tp*s[su[n*2]-su[j]+su[i-1]])%mod;
}
printf("%d\n",as);
}// | ### 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>
using namespace std;
#define N 605
#define mod 1000000007
int dp[N][N],n,m,a,b,t[N],s[N],su[N],as;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d",&a,&b),t[a]=b,t[b]=a;
s[0]=1;for(int i=2;i<=n*2;i++)s[i]=1ll*s[i-2]*(i-1)%mod;
for(int i=1;i<=n*2;i++)su[i]=su[i-1]+!t[i];
for(int l=1;l<=n*2;l++)
for(int i=1;i+l<=n*2;i++)
{
int j=i+l;
int fg=1;
for(int k=i;k<=j;k++)if((t[k]<i||t[k]>j)&&t[k])fg=0;
if((su[j]^su[i-1])&1)fg=0;
if(!fg)continue;
int tp=s[su[j]-su[i-1]];
for(int k=i;k<j;k++)tp=(tp-1ll*dp[i][k]*s[su[j]-su[k]]%mod+mod)%mod;
dp[i][j]=tp;as=(as+1ll*tp*s[su[n*2]-su[j]+su[i-1]])%mod;
}
printf("%d\n",as);
}//
``` |
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cassert>
#define SIZE 605
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
int to[SIZE];
int dp[SIZE][SIZE];
int dp2[SIZE][SIZE];
int fac[SIZE];
void make()
{
fac[0]=1;
for(int i=1;i<SIZE;i++)
{
fac[i]=(ll) fac[i-1]*(ll) (2*i-1)%MOD;
}
}
int main()
{
make();
int n,m;
scanf("%d %d",&n,&m);
memset(to,-1,sizeof(to));
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);a--,b--;
to[a]=b;
to[b]=a;
}
ll ret=0;
for(int L=2;L<=2*n;L+=2)
{
for(int s=0;s+L<=2*n;s++)
{
int l=s,r=s+L-1,cnt=0;
bool up=true;
for(int j=l;j<=r;j++)
{
if(to[j]!=-1)
{
if(to[j]<l||to[j]>r)
{
up=false;
break;
}
}
else cnt++;
}
if(!up) dp[l][r]=dp2[l][r]=0;
else
{
dp2[l][r]=fac[cnt/2];
dp[l][r]=dp2[l][r];
for(int j=l;j<r;j++)
{
dp[l][r]-=(ll) dp2[l][j]*(ll) dp[j+1][r]%MOD;
if(dp[l][r]<0) dp[l][r]+=MOD;
}
ret+=(ll) dp[l][r]*(ll) fac[n-m-cnt/2]%MOD;
if(ret>=MOD) ret-=MOD;
//printf("%d %d: %d\n",l,r,dp[l][r]);
}
}
}
printf("%lld\n",ret);
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 <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <cassert>
#define SIZE 605
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
int to[SIZE];
int dp[SIZE][SIZE];
int dp2[SIZE][SIZE];
int fac[SIZE];
void make()
{
fac[0]=1;
for(int i=1;i<SIZE;i++)
{
fac[i]=(ll) fac[i-1]*(ll) (2*i-1)%MOD;
}
}
int main()
{
make();
int n,m;
scanf("%d %d",&n,&m);
memset(to,-1,sizeof(to));
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);a--,b--;
to[a]=b;
to[b]=a;
}
ll ret=0;
for(int L=2;L<=2*n;L+=2)
{
for(int s=0;s+L<=2*n;s++)
{
int l=s,r=s+L-1,cnt=0;
bool up=true;
for(int j=l;j<=r;j++)
{
if(to[j]!=-1)
{
if(to[j]<l||to[j]>r)
{
up=false;
break;
}
}
else cnt++;
}
if(!up) dp[l][r]=dp2[l][r]=0;
else
{
dp2[l][r]=fac[cnt/2];
dp[l][r]=dp2[l][r];
for(int j=l;j<r;j++)
{
dp[l][r]-=(ll) dp2[l][j]*(ll) dp[j+1][r]%MOD;
if(dp[l][r]<0) dp[l][r]+=MOD;
}
ret+=(ll) dp[l][r]*(ll) fac[n-m-cnt/2]%MOD;
if(ret>=MOD) ret-=MOD;
//printf("%d %d: %d\n",l,r,dp[l][r]);
}
}
}
printf("%lld\n",ret);
return 0;
}
``` |
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> Pii;
typedef vector<int> Vi;
inline void read(int &x) {
x=0; char c=getchar(); int f=1;
while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
inline ui R() {
static ui seed=416;
return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 606, mo = 1e9+7;
int n,p[N],g[N],qz[N],tot,f[N][N];
bool ok[N][N];
bool ck(int l, int r){
if((r-l+1)&1)return 0;
rep(i,l,r)if(p[i]&&(p[i]<l||p[i]>r))return 0;
return 1;
}
int main() {
read(n);n*=2;int k;read(k);while(k--){
int u,v;read(u);read(v);p[u]=v;p[v]=u;qz[u]=qz[v]=1;
}
rep(i,1,n)qz[i]=qz[i-1]+(!qz[i]);tot=qz[n];
rep(i,0,n)if(!(i&1)){g[i]=1;for(int j=i-1;j>1;j-=2)g[i]=1LL*g[i]*j%mo;}
rep(i,1,n)rep(j,i,n)ok[i][j]=ck(i,j);
ll ans=0;
for(int t=2;t<=n;t+=2)rep(i,1,n-t+1)if(ok[i][i+t-1]){
int j=i+t-1;ll res=g[qz[j]-qz[i-1]];
for(register int k=i+1;k<j;k+=2)res-=1LL*f[i][k]*g[qz[j]-qz[k]]%mo;
res=res%mo+mo;f[i][j]=res;ans+=res*g[tot-(qz[j]-qz[i-1])]%mo;
// res=(res%mo+mo)*g[tot-(qz[j]-qz[i-1])]%mo;ans+=res;f[i][j]=res;
// if(res)printf("%d %d:%lld\n",i,j,res*g[tot-(qz[j]-qz[i-1])]%mo);
}
printf("%lld",(ans%mo+mo)%mo);
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 fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> Pii;
typedef vector<int> Vi;
inline void read(int &x) {
x=0; char c=getchar(); int f=1;
while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
inline ui R() {
static ui seed=416;
return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 606, mo = 1e9+7;
int n,p[N],g[N],qz[N],tot,f[N][N];
bool ok[N][N];
bool ck(int l, int r){
if((r-l+1)&1)return 0;
rep(i,l,r)if(p[i]&&(p[i]<l||p[i]>r))return 0;
return 1;
}
int main() {
read(n);n*=2;int k;read(k);while(k--){
int u,v;read(u);read(v);p[u]=v;p[v]=u;qz[u]=qz[v]=1;
}
rep(i,1,n)qz[i]=qz[i-1]+(!qz[i]);tot=qz[n];
rep(i,0,n)if(!(i&1)){g[i]=1;for(int j=i-1;j>1;j-=2)g[i]=1LL*g[i]*j%mo;}
rep(i,1,n)rep(j,i,n)ok[i][j]=ck(i,j);
ll ans=0;
for(int t=2;t<=n;t+=2)rep(i,1,n-t+1)if(ok[i][i+t-1]){
int j=i+t-1;ll res=g[qz[j]-qz[i-1]];
for(register int k=i+1;k<j;k+=2)res-=1LL*f[i][k]*g[qz[j]-qz[k]]%mo;
res=res%mo+mo;f[i][j]=res;ans+=res*g[tot-(qz[j]-qz[i-1])]%mo;
// res=(res%mo+mo)*g[tot-(qz[j]-qz[i-1])]%mo;ans+=res;f[i][j]=res;
// if(res)printf("%d %d:%lld\n",i,j,res*g[tot-(qz[j]-qz[i-1])]%mo);
}
printf("%lld",(ans%mo+mo)%mo);
return 0;
}
``` |
#include<bits/stdc++.h>
typedef long long ll;
int const N = 305 * 2;
int const MOD = 1e9 + 7;
int pa[N];
ll f[N][N];
ll fac[N];
int sum[N];
int n, m;
int main() {
std::ios::sync_with_stdio(0);
std::cin >> n >> m;
fac[0] = 1;
for(int i = 1; i < N; ++i) if((i & 1) == 0) {
fac[i] = fac[i - 2] * (i - 1) % MOD;
}
for(int i = 1; i <= m; ++i) {
int x, y;
std::cin >> x >> y;
pa[x] = y;
pa[y] = x;
}
n <<= 1;
for(int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1];
if(pa[i] == 0)
++sum[i];
}
ll ans = 0;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j + i - 1 <= n; ++j) {
int l = j, r = j + i - 1;
bool flag = (sum[r] - sum[l - 1]) % 2;
for(int k = l; k <= r; ++k) if(pa[k]) {
if(pa[k] < l || pa[k] > r) {
flag = 1;
break;
}
}
if(flag) continue;
f[l][r] = fac[sum[r] - sum[l - 1]];
for(int k = l; k < r; ++k) {
int tmp = f[l][k] * fac[sum[r] - sum[k]] % MOD;
tmp = (MOD - tmp) % MOD;
f[l][r] = (f[l][r] + tmp) % MOD;
}
ans += f[l][r] * fac[n - m - m - (sum[r] - sum[l - 1])] % MOD;
ans %= MOD;
}
}
std::cout << ans << '\n';
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;
int const N = 305 * 2;
int const MOD = 1e9 + 7;
int pa[N];
ll f[N][N];
ll fac[N];
int sum[N];
int n, m;
int main() {
std::ios::sync_with_stdio(0);
std::cin >> n >> m;
fac[0] = 1;
for(int i = 1; i < N; ++i) if((i & 1) == 0) {
fac[i] = fac[i - 2] * (i - 1) % MOD;
}
for(int i = 1; i <= m; ++i) {
int x, y;
std::cin >> x >> y;
pa[x] = y;
pa[y] = x;
}
n <<= 1;
for(int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1];
if(pa[i] == 0)
++sum[i];
}
ll ans = 0;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j + i - 1 <= n; ++j) {
int l = j, r = j + i - 1;
bool flag = (sum[r] - sum[l - 1]) % 2;
for(int k = l; k <= r; ++k) if(pa[k]) {
if(pa[k] < l || pa[k] > r) {
flag = 1;
break;
}
}
if(flag) continue;
f[l][r] = fac[sum[r] - sum[l - 1]];
for(int k = l; k < r; ++k) {
int tmp = f[l][k] * fac[sum[r] - sum[k]] % MOD;
tmp = (MOD - tmp) % MOD;
f[l][r] = (f[l][r] + tmp) % MOD;
}
ans += f[l][r] * fac[n - m - m - (sum[r] - sum[l - 1])] % MOD;
ans %= MOD;
}
}
std::cout << ans << '\n';
return 0;
}
``` |
#include <bits/stdc++.h>
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
inline int read() {
int f=1,sum=0;
char x=getchar();
for(;(x<'0'||x>'9');x=getchar()) if(x=='-') f=-1;
for(;x>='0'&&x<='9';x=getchar()) sum=sum*10+x-'0';
return f*sum;
}
#define M 605
#define mod 1000000007
int n,K;
int pos[M],sum[M];
int g[M],f[M][M],ans;
inline void inc(int &x,int y) {x+=y,x-=x>=mod?mod:0;}
inline void dp(int l,int r) {
if(sum[r]-sum[l-1]&1) return;
for1(l,r,i) if(pos[i]&&pos[i]<l||pos[i]>r) return;
int x=g[sum[r]-sum[l-1]];
for1(l,r-1,i) if(f[l][i]) inc(x,mod-1ll*f[l][i]*g[sum[r]-sum[i]]%mod);
f[l][r]=x;
inc(ans,1ll*x*g[n-2*K-sum[r]+sum[l-1]]%mod);
}
int main () {
//freopen("a.in","r",stdin);
n=read()*2,K=read();
for1(1,K,i) {
int x=read(),y=read();
pos[x]=y,pos[y]=x;
}
g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=1ll*(i-1)*g[i-2]%mod;
for1(1,n,i) sum[i]=sum[i-1]+(!pos[i]);
for1(1,n,i) FOR2(n-i+1,1,j) dp(j,j+i-1);
cout<<ans<<endl;
} | ### 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>
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
inline int read() {
int f=1,sum=0;
char x=getchar();
for(;(x<'0'||x>'9');x=getchar()) if(x=='-') f=-1;
for(;x>='0'&&x<='9';x=getchar()) sum=sum*10+x-'0';
return f*sum;
}
#define M 605
#define mod 1000000007
int n,K;
int pos[M],sum[M];
int g[M],f[M][M],ans;
inline void inc(int &x,int y) {x+=y,x-=x>=mod?mod:0;}
inline void dp(int l,int r) {
if(sum[r]-sum[l-1]&1) return;
for1(l,r,i) if(pos[i]&&pos[i]<l||pos[i]>r) return;
int x=g[sum[r]-sum[l-1]];
for1(l,r-1,i) if(f[l][i]) inc(x,mod-1ll*f[l][i]*g[sum[r]-sum[i]]%mod);
f[l][r]=x;
inc(ans,1ll*x*g[n-2*K-sum[r]+sum[l-1]]%mod);
}
int main () {
//freopen("a.in","r",stdin);
n=read()*2,K=read();
for1(1,K,i) {
int x=read(),y=read();
pos[x]=y,pos[y]=x;
}
g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=1ll*(i-1)*g[i-2]%mod;
for1(1,n,i) sum[i]=sum[i-1]+(!pos[i]);
for1(1,n,i) FOR2(n-i+1,1,j) dp(j,j+i-1);
cout<<ans<<endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll
#define INF 1e16
#define mod 1000000007
ll ff(ll m){
if(m<0||m%2==1)return 0;
m--;
ll res=1;
for(ll i=m;i>0;i-=2)(res*=i)%=mod;
return res;
}
ll f[601];
ll n,m;
ll dp[601][601];
ll c[601];
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
rep(i,601)f[i]=ff(i);
cin>>n>>m;
memset(c,-1,sizeof(c));
rep(i,m){
ll a,b;
cin>>a>>b;
a--; b--;
c[a]=b; c[b]=a;
}
ll res=0;
repl(len,2,2*n+1)rep(l,2*n+1){
int r=l+len;
if(r>2*n)continue;
if((r-l)%2==1)continue;
bool ok=true;
repl(i,l,r){
if(c[i]!=-1){
if(c[i]<l||c[i]>=r)ok=false;
}
}
if(!ok)continue;
int cnt=0;
repl(i,l,r){
if(c[i]!=-1){
cnt++;
}
}
dp[l][r]=f[len-cnt];
repl(i,l,r-1){
if(c[i]!=-1)cnt--;
dp[l][r]=(dp[l][r]-(dp[l][i+1]*f[r-(i+1)-cnt]%mod)+mod)%mod;
}
ll s1=0,s2=0;
rep(i,l){
if(c[i]!=-1)s1++;
}
repl(i,r,2*n){
if(c[i]!=-1)s2++;
}
res+=dp[l][r]*f[2*n-len-s1-s2]%mod;
res%=mod;
}
cout<<res<<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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll
#define INF 1e16
#define mod 1000000007
ll ff(ll m){
if(m<0||m%2==1)return 0;
m--;
ll res=1;
for(ll i=m;i>0;i-=2)(res*=i)%=mod;
return res;
}
ll f[601];
ll n,m;
ll dp[601][601];
ll c[601];
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
rep(i,601)f[i]=ff(i);
cin>>n>>m;
memset(c,-1,sizeof(c));
rep(i,m){
ll a,b;
cin>>a>>b;
a--; b--;
c[a]=b; c[b]=a;
}
ll res=0;
repl(len,2,2*n+1)rep(l,2*n+1){
int r=l+len;
if(r>2*n)continue;
if((r-l)%2==1)continue;
bool ok=true;
repl(i,l,r){
if(c[i]!=-1){
if(c[i]<l||c[i]>=r)ok=false;
}
}
if(!ok)continue;
int cnt=0;
repl(i,l,r){
if(c[i]!=-1){
cnt++;
}
}
dp[l][r]=f[len-cnt];
repl(i,l,r-1){
if(c[i]!=-1)cnt--;
dp[l][r]=(dp[l][r]-(dp[l][i+1]*f[r-(i+1)-cnt]%mod)+mod)%mod;
}
ll s1=0,s2=0;
rep(i,l){
if(c[i]!=-1)s1++;
}
repl(i,r,2*n){
if(c[i]!=-1)s2++;
}
res+=dp[l][r]*f[2*n-len-s1-s2]%mod;
res%=mod;
}
cout<<res<<endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
template <typename T> void chmin(T &x,const T &y)
{
if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
if(x<y)x=y;
}
typedef long long s64;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
const int N=600+5,D=1e9+7;
int lk[N],s[N];
s64 f[N][N],g[N][N],_jie[N];
int main()
{
#ifdef kcz
freopen("1.in","r",stdin);
#endif
int n,k;
cin>>n>>k;
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
lk[a]=b;lk[b]=a;
}
n*=2;
rep(i,1,n)s[i]=s[i-1]+(lk[i]==0);
_jie[0]=1;
rep(i,1,n)_jie[i]=_jie[i-1]*(2*i-1)%D;
s64 Ans=0;
per(l,n,1)
{
int mn=N,mx=0;
rep(r,l,n)
{
if(lk[r])
{
chmin(mn,lk[r]);
chmax(mx,lk[r]);
}
if(!((r-l+1)%2==0&&mn>=l&&mx<=r))continue;
s64 ans=g[l][r]=_jie[(s[r]-s[l-1])/2];
for(int m=l+1;m<r;m+=2)(ans-=f[l][m]*g[m+1][r])%=D;
(Ans+=(f[l][r]=ans)*_jie[(s[l-1]+s[n]-s[r])/2])%=D;
}
}
cout<<(Ans+D)%D;
} | ### 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 chmin(T &x,const T &y)
{
if(x>y)x=y;
}
template <typename T> void chmax(T &x,const T &y)
{
if(x<y)x=y;
}
typedef long long s64;
#define rep(i,l,r) for(int i=l;i<=r;++i)
#define per(i,r,l) for(int i=r;i>=l;--i)
const int N=600+5,D=1e9+7;
int lk[N],s[N];
s64 f[N][N],g[N][N],_jie[N];
int main()
{
#ifdef kcz
freopen("1.in","r",stdin);
#endif
int n,k;
cin>>n>>k;
while(k--)
{
int a,b;
scanf("%d%d",&a,&b);
lk[a]=b;lk[b]=a;
}
n*=2;
rep(i,1,n)s[i]=s[i-1]+(lk[i]==0);
_jie[0]=1;
rep(i,1,n)_jie[i]=_jie[i-1]*(2*i-1)%D;
s64 Ans=0;
per(l,n,1)
{
int mn=N,mx=0;
rep(r,l,n)
{
if(lk[r])
{
chmin(mn,lk[r]);
chmax(mx,lk[r]);
}
if(!((r-l+1)%2==0&&mn>=l&&mx<=r))continue;
s64 ans=g[l][r]=_jie[(s[r]-s[l-1])/2];
for(int m=l+1;m<r;m+=2)(ans-=f[l][m]*g[m+1][r])%=D;
(Ans+=(f[l][r]=ans)*_jie[(s[l-1]+s[n]-s[r])/2])%=D;
}
}
cout<<(Ans+D)%D;
}
``` |
#include <bits/stdc++.h>
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
inline int read() {
int f=1,sum=0;
char x=getchar();
for(;(x<'0'||x>'9');x=getchar()) if(x=='-') f=-1;
for(;x>='0'&&x<='9';x=getchar()) sum=sum*10+x-'0';
return f*sum;
}
#define M 605
#define mod 1000000007
int n,K;
int pos[M],sum[M];
int g[M],f[M][M],ans;
inline void inc(int &x,int y) {x+=y,x-=x>=mod?mod:0;}
inline void dp(int l,int r) {
if(sum[r]-sum[l-1]&1) return;
for1(l,r,i) if(pos[i]&&pos[i]<l||pos[i]>r) return;
int x=g[sum[r]-sum[l-1]];
for1(l,r-1,i) inc(x,mod-1ll*f[l][i]*g[sum[r]-sum[i]]%mod);
f[l][r]=x;
inc(ans,1ll*x*g[n-2*K-sum[r]+sum[l-1]]%mod);
}
int main () {
//freopen("a.in","r",stdin);
n=read()*2,K=read();
for1(1,K,i) {
int x=read(),y=read();
pos[x]=y,pos[y]=x;
}
g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=1ll*(i-1)*g[i-2]%mod;
for1(1,n,i) sum[i]=sum[i-1]+(!pos[i]);
for1(1,n,i) FOR2(n-i+1,1,j) dp(j,j+i-1);
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>
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
inline int read() {
int f=1,sum=0;
char x=getchar();
for(;(x<'0'||x>'9');x=getchar()) if(x=='-') f=-1;
for(;x>='0'&&x<='9';x=getchar()) sum=sum*10+x-'0';
return f*sum;
}
#define M 605
#define mod 1000000007
int n,K;
int pos[M],sum[M];
int g[M],f[M][M],ans;
inline void inc(int &x,int y) {x+=y,x-=x>=mod?mod:0;}
inline void dp(int l,int r) {
if(sum[r]-sum[l-1]&1) return;
for1(l,r,i) if(pos[i]&&pos[i]<l||pos[i]>r) return;
int x=g[sum[r]-sum[l-1]];
for1(l,r-1,i) inc(x,mod-1ll*f[l][i]*g[sum[r]-sum[i]]%mod);
f[l][r]=x;
inc(ans,1ll*x*g[n-2*K-sum[r]+sum[l-1]]%mod);
}
int main () {
//freopen("a.in","r",stdin);
n=read()*2,K=read();
for1(1,K,i) {
int x=read(),y=read();
pos[x]=y,pos[y]=x;
}
g[0]=1;
for(int i=2;i<=n;i+=2) g[i]=1ll*(i-1)*g[i-2]%mod;
for1(1,n,i) sum[i]=sum[i-1]+(!pos[i]);
for1(1,n,i) FOR2(n-i+1,1,j) dp(j,j+i-1);
cout<<ans<<endl;
}
``` |
#include<bits/stdc++.h>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long ll;
const int maxn=605,mo=1e9+7;
int i,j,l,k,n,m,a[maxn>>1],b[maxn>>1],g[maxn>>1];
int f[maxn][maxn],num,F[maxn][maxn];
ll ans;
int pd(int x){return i<=x&&x<=j;}
int main(){
scanf("%d%d",&n,&m);
fo(i,1,m) scanf("%d%d",&a[i],&b[i]);
g[0]=1;
fo(i,1,n) g[i]=(ll)g[i-1]*((i<<1)-1)%mo;
for(l=1;l<2*n;l+=2){
fo(i,1,2*n-l){
j=i+l,num=0;
fo(k,1,m){
int bz=pd(a[k])+pd(b[k]);
if (bz==1) break;
num+=bz>0;
}if (k<=m) continue;
num=((l+1)>>1)-num;
f[i][j]=F[i][j]=g[num];
for(k=i+1;k<j;k+=2){
f[i][j]-=(ll)f[i][k]*F[k+1][j]%mo;
f[i][j]+=f[i][j]<0?mo:0;
}
ans+=(ll)f[i][j]*g[n-m-num]%mo;
}
}
printf("%lld\n",ans%mo);
} | ### 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 fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long ll;
const int maxn=605,mo=1e9+7;
int i,j,l,k,n,m,a[maxn>>1],b[maxn>>1],g[maxn>>1];
int f[maxn][maxn],num,F[maxn][maxn];
ll ans;
int pd(int x){return i<=x&&x<=j;}
int main(){
scanf("%d%d",&n,&m);
fo(i,1,m) scanf("%d%d",&a[i],&b[i]);
g[0]=1;
fo(i,1,n) g[i]=(ll)g[i-1]*((i<<1)-1)%mo;
for(l=1;l<2*n;l+=2){
fo(i,1,2*n-l){
j=i+l,num=0;
fo(k,1,m){
int bz=pd(a[k])+pd(b[k]);
if (bz==1) break;
num+=bz>0;
}if (k<=m) continue;
num=((l+1)>>1)-num;
f[i][j]=F[i][j]=g[num];
for(k=i+1;k<j;k+=2){
f[i][j]-=(ll)f[i][k]*F[k+1][j]%mo;
f[i][j]+=f[i][j]<0?mo:0;
}
ans+=(ll)f[i][j]*g[n-m-num]%mo;
}
}
printf("%lld\n",ans%mo);
}
``` |
#include<cstdio>
#define mod 1000000007
#define N 611
int dp[N][N],A[N],B[N],G[N],cnt[N][N],pd[N],n,k,nn,all,ans;
int main()
{
scanf("%d%d",&n,&k);nn=n<<1;all=(n-k)<<1;
for (int i=1;i<=k;++i) scanf("%d%d",&A[i],&B[i]),pd[A[i]]=B[i],pd[B[i]]=A[i];
G[0]=1;for (int i=2;i<=nn;i+=2) G[i]=1ll*G[i-2]*(i-1)%mod;
for (int i=1;i<=nn;++i)
for (int j=i;j<=nn;++j)
cnt[i][j]=cnt[i][j-1]+(!pd[j]);
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2)
{
for (int k=i;k<=j;++k) if (pd[k]&&(pd[k]>j||pd[k]<i)) goto C;
dp[i][j]=G[cnt[i][j]];
for (int k=i+1;k<j;k+=2)
dp[i][j]=(dp[i][j]-1ll*dp[i][k]*G[cnt[k+1][j]]%mod+mod)%mod;
C:;
}
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2) ans=(ans+1ll*dp[i][j]*G[all-cnt[i][j]]%mod)%mod;
printf("%d",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<cstdio>
#define mod 1000000007
#define N 611
int dp[N][N],A[N],B[N],G[N],cnt[N][N],pd[N],n,k,nn,all,ans;
int main()
{
scanf("%d%d",&n,&k);nn=n<<1;all=(n-k)<<1;
for (int i=1;i<=k;++i) scanf("%d%d",&A[i],&B[i]),pd[A[i]]=B[i],pd[B[i]]=A[i];
G[0]=1;for (int i=2;i<=nn;i+=2) G[i]=1ll*G[i-2]*(i-1)%mod;
for (int i=1;i<=nn;++i)
for (int j=i;j<=nn;++j)
cnt[i][j]=cnt[i][j-1]+(!pd[j]);
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2)
{
for (int k=i;k<=j;++k) if (pd[k]&&(pd[k]>j||pd[k]<i)) goto C;
dp[i][j]=G[cnt[i][j]];
for (int k=i+1;k<j;k+=2)
dp[i][j]=(dp[i][j]-1ll*dp[i][k]*G[cnt[k+1][j]]%mod+mod)%mod;
C:;
}
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2) ans=(ans+1ll*dp[i][j]*G[all-cnt[i][j]]%mod)%mod;
printf("%d",ans);
return 0;
}
``` |
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
long long read() {
long long x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) w = ch == '-' ? -1 : 1, ch = getchar();
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * w;
}
const int Max_n = 605, mod = 1e9 + 7;
int n, K, m, ans;
int to[Max_n], s[Max_n];
int f[Max_n][Max_n], g[Max_n];
int F(int l, int r) {
if (l > r) return 0;
return r - l + 1 - s[r] + s[l - 1];
}
int main() {
n = read() << 1, m = read() << 1, g[0] = 1;
int u, v;
for (int i = 1; i <= (m >> 1); i++) {
u = read(), v = read();
to[u] = v, to[v] = u, s[u] = s[v] = 1;
}
for (int i = 1; i <= n; i++) s[i] += s[i - 1];
for (int i = 2; i <= n; i += 2)
g[i] = 1ll * (i - 1) * g[i - 2] % mod;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
if ((j - i) & 1) {
bool fl = 0;
for (int k = i; k <= j; k++)
if (to[k] && (to[k] < i || to[k] > j)) fl = 1;
if (fl) continue;
f[i][j] = g[F(i, j)];
for (int k = i + 1; k < j; k++)
f[i][j] = (f[i][j] - 1ll * f[i][k] * g[F(k + 1, j)] % mod + mod) % mod;
//cout << i << " " << j << " " << f[i][j] << " " << F(1, i - 1) + F(j + 1, n) << endl;
ans = (ans + 1ll * f[i][j] * g[F(1, i - 1) + F(j + 1, n)] % mod) % mod;
}
cout << ans;
}
| ### 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 <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
long long read() {
long long x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) w = ch == '-' ? -1 : 1, ch = getchar();
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * w;
}
const int Max_n = 605, mod = 1e9 + 7;
int n, K, m, ans;
int to[Max_n], s[Max_n];
int f[Max_n][Max_n], g[Max_n];
int F(int l, int r) {
if (l > r) return 0;
return r - l + 1 - s[r] + s[l - 1];
}
int main() {
n = read() << 1, m = read() << 1, g[0] = 1;
int u, v;
for (int i = 1; i <= (m >> 1); i++) {
u = read(), v = read();
to[u] = v, to[v] = u, s[u] = s[v] = 1;
}
for (int i = 1; i <= n; i++) s[i] += s[i - 1];
for (int i = 2; i <= n; i += 2)
g[i] = 1ll * (i - 1) * g[i - 2] % mod;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++)
if ((j - i) & 1) {
bool fl = 0;
for (int k = i; k <= j; k++)
if (to[k] && (to[k] < i || to[k] > j)) fl = 1;
if (fl) continue;
f[i][j] = g[F(i, j)];
for (int k = i + 1; k < j; k++)
f[i][j] = (f[i][j] - 1ll * f[i][k] * g[F(k + 1, j)] % mod + mod) % mod;
//cout << i << " " << j << " " << f[i][j] << " " << F(1, i - 1) + F(j + 1, n) << endl;
ans = (ans + 1ll * f[i][j] * g[F(1, i - 1) + F(j + 1, n)] % mod) % mod;
}
cout << ans;
}
``` |
#include<bits/stdc++.h>
#define ll long long
#define ljc 1000000007
using namespace std;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10ll+ch-'0';ch=getchar();}
return x*f;
}
inline ll fast_pow(ll a,ll b,ll p){
ll t=1;a%=p;
while (b){
if (b&1) t=t*a%p;
b>>=1;a=a*a%p;
}
return t;
}
ll n,K,g[1010],a[1010],dp[1010][1010],_;
signed main(){
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 (ll i=2;i<=n;i+=2) g[i]=g[i-2]*(i-1)%ljc;
for (int len=2;len<=n;len+=2){
for (int i=1;i+len-1<=n;i++){
int j=i+len-1,col=len;
bool flag=1;
for (int k=i;k<=j;k++){
if (a[k]&&(a[k]<i||a[k]>j)) flag=0;
col-=(a[k]>0);
}
if (!flag) continue;
dp[i][j]=g[col];
for (int k=j,d=0;k>i;k--){
d+=(!a[k]);
dp[i][j]=(dp[i][j]-dp[i][k-1]*g[d]%ljc+ljc)%ljc;
}
_=(_+dp[i][j]*g[n-2*K-col]%ljc)%ljc;
}
}
cout<<_;
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 ll long long
#define ljc 1000000007
using namespace std;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10ll+ch-'0';ch=getchar();}
return x*f;
}
inline ll fast_pow(ll a,ll b,ll p){
ll t=1;a%=p;
while (b){
if (b&1) t=t*a%p;
b>>=1;a=a*a%p;
}
return t;
}
ll n,K,g[1010],a[1010],dp[1010][1010],_;
signed main(){
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 (ll i=2;i<=n;i+=2) g[i]=g[i-2]*(i-1)%ljc;
for (int len=2;len<=n;len+=2){
for (int i=1;i+len-1<=n;i++){
int j=i+len-1,col=len;
bool flag=1;
for (int k=i;k<=j;k++){
if (a[k]&&(a[k]<i||a[k]>j)) flag=0;
col-=(a[k]>0);
}
if (!flag) continue;
dp[i][j]=g[col];
for (int k=j,d=0;k>i;k--){
d+=(!a[k]);
dp[i][j]=(dp[i][j]-dp[i][k-1]*g[d]%ljc+ljc)%ljc;
}
_=(_+dp[i][j]*g[n-2*K-col]%ljc)%ljc;
}
}
cout<<_;
return 0;
}
``` |
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int Q=633,MOD=1e9+7;
int f[Q][Q];
int g[Q];
int ct[Q];
int to[Q];
inline int mul(int a,int b)
{return 1LL*a*b%MOD;}
inline int add(int a,int b)
{a+=b;return a>=MOD?a-MOD:a;}
inline int sub(int a,int b)
{a-=b;return a<0?a+MOD:a;}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1,x,y;i<=k;i++){
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
to[x]=y,to[y]=x;
++ct[x],++ct[y];
}
for(int i=1;i<=(n<<1);i++)
ct[i]=ct[i-1]+(ct[i]==0);
g[0]=1;
int als=0;
for(int i=2;i<=(n<<1);i+=2)
g[i]=mul(g[i-2],i-1);
for(int t=1;t<=(n<<1);t++)
for(int i=1,j;(j=i+t-1)<=(n<<1);i++){
int fl=1;
for(int k=i;k<=j;k++)
if(to[k]&&(to[k]<i||to[k]>j)){
fl=0;
break;
}
if(!fl)continue;
f[i][j]=g[ct[j]-ct[i-1]];
for(int k=i;k<j;k++)
f[i][j]=sub(f[i][j],mul(f[i][k],g[ct[j]-ct[k]]));
als=add(als,mul(f[i][j],g[(n<<1)-(k<<1)-(ct[j]-ct[i-1])]));
}
printf("%d",als);
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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int Q=633,MOD=1e9+7;
int f[Q][Q];
int g[Q];
int ct[Q];
int to[Q];
inline int mul(int a,int b)
{return 1LL*a*b%MOD;}
inline int add(int a,int b)
{a+=b;return a>=MOD?a-MOD:a;}
inline int sub(int a,int b)
{a-=b;return a<0?a+MOD:a;}
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1,x,y;i<=k;i++){
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
to[x]=y,to[y]=x;
++ct[x],++ct[y];
}
for(int i=1;i<=(n<<1);i++)
ct[i]=ct[i-1]+(ct[i]==0);
g[0]=1;
int als=0;
for(int i=2;i<=(n<<1);i+=2)
g[i]=mul(g[i-2],i-1);
for(int t=1;t<=(n<<1);t++)
for(int i=1,j;(j=i+t-1)<=(n<<1);i++){
int fl=1;
for(int k=i;k<=j;k++)
if(to[k]&&(to[k]<i||to[k]>j)){
fl=0;
break;
}
if(!fl)continue;
f[i][j]=g[ct[j]-ct[i-1]];
for(int k=i;k<j;k++)
f[i][j]=sub(f[i][j],mul(f[i][k],g[ct[j]-ct[k]]));
als=add(als,mul(f[i][j],g[(n<<1)-(k<<1)-(ct[j]-ct[i-1])]));
}
printf("%d",als);
return 0;
}
``` |
#include<bits/stdc++.h>
#define mod 1000000007
typedef long long ll;
ll gi(){
ll x=0,f=1;
char ch=getchar();
while(!isdigit(ch))f^=ch=='-',ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f?x:-x;
}
int f[610],s[610],R[610],lnk[610],F[610][610];
int main(){
#ifdef XZZSB
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
int n=gi()*2,k=gi(),A,B;
for(int i=1;i<=n;++i)s[i]=1;
while(k--){
A=gi(),B=gi();if(A>B)std::swap(A,B);
s[A]=s[B]=0;R[A]=B;lnk[A]=B,lnk[B]=A;
}
for(int i=1;i<=n;++i)s[i]+=s[i-1];
int ans=0;
f[0]=1;for(int i=2;i<=n;i+=2)f[i]=1ll*f[i-2]*(i-1)%mod;
for(int sz=2;sz<=n;++sz){
for(int l=1,r=sz;r<=n;++l,++r){
if(R[l]&&R[l]>r)continue;
bool flg=0;
for(int i=l;i<=r;++i)if(lnk[i]&&(lnk[i]<l||lnk[i]>r)){flg=1;break;}
if(flg)continue;
F[l][r]=f[s[r]-s[l-1]];
for(int i=R[l];i<r;++i)F[l][r]=(F[l][r]-1ll*F[l][i]*f[s[r]-s[i]]%mod+mod)%mod;
ans=(ans+1ll*F[l][r]*f[s[n]-s[r]+s[l-1]])%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 mod 1000000007
typedef long long ll;
ll gi(){
ll x=0,f=1;
char ch=getchar();
while(!isdigit(ch))f^=ch=='-',ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f?x:-x;
}
int f[610],s[610],R[610],lnk[610],F[610][610];
int main(){
#ifdef XZZSB
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
int n=gi()*2,k=gi(),A,B;
for(int i=1;i<=n;++i)s[i]=1;
while(k--){
A=gi(),B=gi();if(A>B)std::swap(A,B);
s[A]=s[B]=0;R[A]=B;lnk[A]=B,lnk[B]=A;
}
for(int i=1;i<=n;++i)s[i]+=s[i-1];
int ans=0;
f[0]=1;for(int i=2;i<=n;i+=2)f[i]=1ll*f[i-2]*(i-1)%mod;
for(int sz=2;sz<=n;++sz){
for(int l=1,r=sz;r<=n;++l,++r){
if(R[l]&&R[l]>r)continue;
bool flg=0;
for(int i=l;i<=r;++i)if(lnk[i]&&(lnk[i]<l||lnk[i]>r)){flg=1;break;}
if(flg)continue;
F[l][r]=f[s[r]-s[l-1]];
for(int i=R[l];i<r;++i)F[l][r]=(F[l][r]-1ll*F[l][i]*f[s[r]-s[i]]%mod+mod)%mod;
ans=(ans+1ll*F[l][r]*f[s[n]-s[r]+s[l-1]])%mod;
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=605;
const int MOD=1e9+7;
int add (int x,int y) {x=x+y;return x>=MOD?x-MOD:x;}
int mul (int x,int y) {return (LL)x*y%MOD;}
int dec (int x,int y) {x=x-y;return x<0?x+MOD:x;}
int Pow (int x,int y)
{
if (y==1) return x;
int lalal=Pow(x,y>>1);
lalal=mul(lalal,lalal);
if (y&1) lalal=mul(lalal,x);
return lalal;
}
int n,k;
int pos[N];
int g[N];
int sum[N];
int ans=0;
int f[N][N];
void solve (int l,int r)
{
if (f[l][r]!=-1) return ;
f[l][r]=0;
if ((sum[r]-sum[l-1])&1) return ;
for (int u=l;u<=r;u++)
if (pos[u]!=0&&(pos[u]<l||pos[u]>r)) return ;
int now=g[sum[r]-sum[l-1]];
for (int u=l;u<r;u++)
{
solve(l,u);
now=dec(now,mul(f[l][u],g[sum[r]-sum[u]]));
}
f[l][r]=now;
ans=add(ans,mul(now,g[n-2*k-(sum[r]-sum[l-1])]));
}
int main()
{
memset(f,-1,sizeof(f));
scanf("%d%d",&n,&k);n<<=1;
for (int u=1;u<=k;u++)
{
int x,y;
scanf("%d%d",&x,&y);
pos[x]=y;pos[y]=x;
}
g[0]=1;for (int u=2;u<=n;u+=2) g[u]=mul(u-1,g[u-2]);
for (int u=1;u<=n;u++) sum[u]=sum[u-1]+(pos[u]==0);
for (int u=1;u<=n;u++)
for (int i=u;i<=n;i++)
solve(u,i);
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<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=605;
const int MOD=1e9+7;
int add (int x,int y) {x=x+y;return x>=MOD?x-MOD:x;}
int mul (int x,int y) {return (LL)x*y%MOD;}
int dec (int x,int y) {x=x-y;return x<0?x+MOD:x;}
int Pow (int x,int y)
{
if (y==1) return x;
int lalal=Pow(x,y>>1);
lalal=mul(lalal,lalal);
if (y&1) lalal=mul(lalal,x);
return lalal;
}
int n,k;
int pos[N];
int g[N];
int sum[N];
int ans=0;
int f[N][N];
void solve (int l,int r)
{
if (f[l][r]!=-1) return ;
f[l][r]=0;
if ((sum[r]-sum[l-1])&1) return ;
for (int u=l;u<=r;u++)
if (pos[u]!=0&&(pos[u]<l||pos[u]>r)) return ;
int now=g[sum[r]-sum[l-1]];
for (int u=l;u<r;u++)
{
solve(l,u);
now=dec(now,mul(f[l][u],g[sum[r]-sum[u]]));
}
f[l][r]=now;
ans=add(ans,mul(now,g[n-2*k-(sum[r]-sum[l-1])]));
}
int main()
{
memset(f,-1,sizeof(f));
scanf("%d%d",&n,&k);n<<=1;
for (int u=1;u<=k;u++)
{
int x,y;
scanf("%d%d",&x,&y);
pos[x]=y;pos[y]=x;
}
g[0]=1;for (int u=2;u<=n;u+=2) g[u]=mul(u-1,g[u-2]);
for (int u=1;u<=n;u++) sum[u]=sum[u-1]+(pos[u]==0);
for (int u=1;u<=n;u++)
for (int i=u;i<=n;i++)
solve(u,i);
printf("%d\n",ans);
return 0;
}
``` |
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
#define SIZE 100000
ll kkai[1010];
typedef pair<ll, ll>pii;
vector<pii>dat;
int count(int lb, int ub)
{
int r = 0;
for (int i = 0; i < dat.size(); i++)
{
if (lb <= dat[i].first&&dat[i].second <= ub)r++;
else if (lb <= dat[i].first&&dat[i].first <= ub&&ub < dat[i].second)return -1;
else if (lb <= dat[i].second&&dat[i].second <= ub&&dat[i].first < lb)return -1;
}
return r;
}
ll dp[666][666];
ll zdp[666][666];
int main()
{
int num, way;
scanf("%d%d", &num, &way);
kkai[0] = 1;
for (int i = 1; i < 1010; i++)kkai[i] = kkai[i - 1] * (i * 2 - 1) % mod;
for (int i = 0; i < way; i++)
{
int za, zb;
scanf("%d%d", &za, &zb);
dat.push_back(make_pair(min(za, zb), max(za, zb)));
}
ll ans = 0;
for (int i = num + num; i >= 1; i--)
{
for (int j = i + 1; j <= num + num; j += 2)
{
int c = count(i, j);
if (c < 0)dp[i][j] = 0;
else dp[i][j] = (kkai[(j - i + 1) / 2 - c] + mod - zdp[i][j]) % mod;
//printf("%d %d %lld %lld\n", i, j, dp[i][j], zdp[i][j]);
zdp[i][j] = (zdp[i][j] + dp[i][j]) % mod;
for (int k = j + 2; k <= num + num; k += 2)zdp[i][k] = (zdp[i][k] + zdp[i][j] * dp[j + 1][k]) % mod;
ans += dp[i][j] * kkai[(num + num - (j - i + 1)) / 2 - (way - c)];
ans %= mod;
}
}
printf("%lld\n", ans);
} | ### 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<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mod = 1000000007;
#define SIZE 100000
ll kkai[1010];
typedef pair<ll, ll>pii;
vector<pii>dat;
int count(int lb, int ub)
{
int r = 0;
for (int i = 0; i < dat.size(); i++)
{
if (lb <= dat[i].first&&dat[i].second <= ub)r++;
else if (lb <= dat[i].first&&dat[i].first <= ub&&ub < dat[i].second)return -1;
else if (lb <= dat[i].second&&dat[i].second <= ub&&dat[i].first < lb)return -1;
}
return r;
}
ll dp[666][666];
ll zdp[666][666];
int main()
{
int num, way;
scanf("%d%d", &num, &way);
kkai[0] = 1;
for (int i = 1; i < 1010; i++)kkai[i] = kkai[i - 1] * (i * 2 - 1) % mod;
for (int i = 0; i < way; i++)
{
int za, zb;
scanf("%d%d", &za, &zb);
dat.push_back(make_pair(min(za, zb), max(za, zb)));
}
ll ans = 0;
for (int i = num + num; i >= 1; i--)
{
for (int j = i + 1; j <= num + num; j += 2)
{
int c = count(i, j);
if (c < 0)dp[i][j] = 0;
else dp[i][j] = (kkai[(j - i + 1) / 2 - c] + mod - zdp[i][j]) % mod;
//printf("%d %d %lld %lld\n", i, j, dp[i][j], zdp[i][j]);
zdp[i][j] = (zdp[i][j] + dp[i][j]) % mod;
for (int k = j + 2; k <= num + num; k += 2)zdp[i][k] = (zdp[i][k] + zdp[i][j] * dp[j + 1][k]) % mod;
ans += dp[i][j] * kkai[(num + num - (j - i + 1)) / 2 - (way - c)];
ans %= mod;
}
}
printf("%lld\n", ans);
}
``` |
#include<iostream>
#define MOD 1000000007
using namespace std;
int n,k,i,j,ind,X[305],Y[305],Nr[605],Fact[605],g,Dp[605][605],rez,val;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>k;
for(i=1; i<=k; i++)
{
cin>>X[i]>>Y[i];
if(X[i]>Y[i])
swap(X[i],Y[i]);
Nr[X[i]]=-1;
Nr[Y[i]]=-1;
}
for(i=1; i<=2*n; i++)
Nr[i]=Nr[i]+Nr[i-1]+1;
Fact[0]=1;
Fact[2]=1;
for(i=4; i<=2*n; i+=2)
Fact[i]=(1LL*Fact[i-2]*(i-1))%MOD;
for(i=1; i<=2*n; i++)
{
for(j=i+1; j<=2*n; j+=2)
{
g=1;
for(ind=1; ind<=k; ind++)
if((X[ind]<i && Y[ind]>=i && Y[ind]<=j) || (X[ind]>=i && X[ind]<=j && Y[ind]>j))
{
g=0;
break;
}
if(g==0)
continue;
Dp[i][j]=Fact[Nr[j]-Nr[i-1]];
for(ind=i+1; ind<j; ind+=2)
{
val=(1LL*Dp[i][ind]*Fact[Nr[j]-Nr[ind]])%MOD;
Dp[i][j]=(Dp[i][j]-val+MOD)%MOD;
}
val=(1LL*Dp[i][j]*Fact[Nr[2*n]-Nr[j]+Nr[i-1]])%MOD;
rez=(rez+val)%MOD;
}
}
cout<<rez<<"\n";
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<iostream>
#define MOD 1000000007
using namespace std;
int n,k,i,j,ind,X[305],Y[305],Nr[605],Fact[605],g,Dp[605][605],rez,val;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>k;
for(i=1; i<=k; i++)
{
cin>>X[i]>>Y[i];
if(X[i]>Y[i])
swap(X[i],Y[i]);
Nr[X[i]]=-1;
Nr[Y[i]]=-1;
}
for(i=1; i<=2*n; i++)
Nr[i]=Nr[i]+Nr[i-1]+1;
Fact[0]=1;
Fact[2]=1;
for(i=4; i<=2*n; i+=2)
Fact[i]=(1LL*Fact[i-2]*(i-1))%MOD;
for(i=1; i<=2*n; i++)
{
for(j=i+1; j<=2*n; j+=2)
{
g=1;
for(ind=1; ind<=k; ind++)
if((X[ind]<i && Y[ind]>=i && Y[ind]<=j) || (X[ind]>=i && X[ind]<=j && Y[ind]>j))
{
g=0;
break;
}
if(g==0)
continue;
Dp[i][j]=Fact[Nr[j]-Nr[i-1]];
for(ind=i+1; ind<j; ind+=2)
{
val=(1LL*Dp[i][ind]*Fact[Nr[j]-Nr[ind]])%MOD;
Dp[i][j]=(Dp[i][j]-val+MOD)%MOD;
}
val=(1LL*Dp[i][j]*Fact[Nr[2*n]-Nr[j]+Nr[i-1]])%MOD;
rez=(rez+val)%MOD;
}
}
cout<<rez<<"\n";
return 0;
}
``` |
#include <bits/stdc++.h>
#define For(i, j, k) for (int i = j; i <= k; i++)
using namespace std;
const int N = 610;
const int Mod = 1e9 + 7;
int n, k;
int A[N], B[N];
int dp[N], f[N], cnt[N][N];
int main() {
scanf("%d%d", &n, &k);
For(i, 1, k) {
scanf("%d%d", &A[i], &B[i]);
if (A[i] > B[i]) swap(A[i], B[i]);
}
f[0] = 1;
For(i, 1, n) f[i] = 1ll * f[i - 1] * (2 * i - 1) % Mod;
int ans = 0;
For(i, 0, 2 * n - 2)
for (int j = i + 2; j <= 2 * n; j += 2) {
For(u, 1, k) {
int c = (A[u] <= i || A[u] > j) + (B[u] <= i || B[u] > j);
if (c == 1) { cnt[i][j] = -1; break; }
else if (!c) cnt[i][j]++;
}
}
For(i, 0, 2 * n - 2) {
dp[i] = -1;
for (int j = i + 2; j <= 2 * n; j += 2) {
dp[j] = 0;
for (int l = i; l < j; l += 2)
if (cnt[l][j] != -1) dp[j] = (dp[j] - 1ll * f[(j - l) / 2 - cnt[l][j]] * dp[l]) % Mod;
ans = (ans + 1ll * dp[j] * f[(n - (j - i) / 2) - (k - cnt[i][j])]) % Mod;
}
}
ans = (ans + Mod) % 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, j, k) for (int i = j; i <= k; i++)
using namespace std;
const int N = 610;
const int Mod = 1e9 + 7;
int n, k;
int A[N], B[N];
int dp[N], f[N], cnt[N][N];
int main() {
scanf("%d%d", &n, &k);
For(i, 1, k) {
scanf("%d%d", &A[i], &B[i]);
if (A[i] > B[i]) swap(A[i], B[i]);
}
f[0] = 1;
For(i, 1, n) f[i] = 1ll * f[i - 1] * (2 * i - 1) % Mod;
int ans = 0;
For(i, 0, 2 * n - 2)
for (int j = i + 2; j <= 2 * n; j += 2) {
For(u, 1, k) {
int c = (A[u] <= i || A[u] > j) + (B[u] <= i || B[u] > j);
if (c == 1) { cnt[i][j] = -1; break; }
else if (!c) cnt[i][j]++;
}
}
For(i, 0, 2 * n - 2) {
dp[i] = -1;
for (int j = i + 2; j <= 2 * n; j += 2) {
dp[j] = 0;
for (int l = i; l < j; l += 2)
if (cnt[l][j] != -1) dp[j] = (dp[j] - 1ll * f[(j - l) / 2 - cnt[l][j]] * dp[l]) % Mod;
ans = (ans + 1ll * dp[j] * f[(n - (j - i) / 2) - (k - cnt[i][j])]) % Mod;
}
}
ans = (ans + Mod) % Mod;
printf("%d\n", ans);
return 0;
}
``` |
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "vector"
#include "string"
#include "map"
#include "algorithm"
#include "functional"
#include "set"
#include "numeric"
using namespace std;
const long long int MOD = 1000000007;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
vector<pair<int, int>>v(M);
vector<int>used(N * 2,1);
for (auto &i : v) {
cin >> i.first >> i.second;
i.first--;
i.second--;
if (i.first > i.second)swap(i.first, i.second);
used[i.first]--;
used[i.second]--;
}
for (int i = 1; i < N * 2; i++)used[i] += used[i - 1];
vector<long long int>by(2 * N + 1, 1);
for (int i = 2; i <= N * 2; i += 2) {
by[i] = by[i - 2] * (i - 1);
by[i] %= MOD;
}
vector<vector<long long int>>dp(N * 2, vector<long long int>(N * 2));
for (int i = 1; i < 2 * N; i+=2) {
for (int j = 0; j + i < N * 2; j++) {
bool ed = false;
for (auto k : v) {
if (k.second<j || k.first>j + i || (k.first >= j && k.second <= j + i) || (k.first<j&&k.second >j + i))continue;
ed = true;
}
if (ed)continue;
dp[j][j + i] = by[used[i + j] - (j ? used[j - 1] : 0)];
for (int k = j + i - 2; k > j; k -= 2) {
dp[j][j + i] += MOD;
long long int minus = dp[j][k] * (by[used[i + j] - used[k]]);
minus %= MOD;
dp[j][j + i] -= minus;
}
dp[j][j + i] %= MOD;
}
}
long long int ans = 0;
for (int i = 0; i < N * 2; i++) {
for (int j = i+1; j < N * 2; j++) {
long long int add = dp[i][j];
add *= by[used[2 * N - 1] - (used[j] - (i ? used[i - 1] : 0))];
add %= MOD;
ans += add;
}
}
ans %= MOD;
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 "iostream"
#include "climits"
#include "list"
#include "queue"
#include "vector"
#include "string"
#include "map"
#include "algorithm"
#include "functional"
#include "set"
#include "numeric"
using namespace std;
const long long int MOD = 1000000007;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
vector<pair<int, int>>v(M);
vector<int>used(N * 2,1);
for (auto &i : v) {
cin >> i.first >> i.second;
i.first--;
i.second--;
if (i.first > i.second)swap(i.first, i.second);
used[i.first]--;
used[i.second]--;
}
for (int i = 1; i < N * 2; i++)used[i] += used[i - 1];
vector<long long int>by(2 * N + 1, 1);
for (int i = 2; i <= N * 2; i += 2) {
by[i] = by[i - 2] * (i - 1);
by[i] %= MOD;
}
vector<vector<long long int>>dp(N * 2, vector<long long int>(N * 2));
for (int i = 1; i < 2 * N; i+=2) {
for (int j = 0; j + i < N * 2; j++) {
bool ed = false;
for (auto k : v) {
if (k.second<j || k.first>j + i || (k.first >= j && k.second <= j + i) || (k.first<j&&k.second >j + i))continue;
ed = true;
}
if (ed)continue;
dp[j][j + i] = by[used[i + j] - (j ? used[j - 1] : 0)];
for (int k = j + i - 2; k > j; k -= 2) {
dp[j][j + i] += MOD;
long long int minus = dp[j][k] * (by[used[i + j] - used[k]]);
minus %= MOD;
dp[j][j + i] -= minus;
}
dp[j][j + i] %= MOD;
}
}
long long int ans = 0;
for (int i = 0; i < N * 2; i++) {
for (int j = i+1; j < N * 2; j++) {
long long int add = dp[i][j];
add *= by[used[2 * N - 1] - (used[j] - (i ? used[i - 1] : 0))];
add %= MOD;
ans += add;
}
}
ans %= MOD;
cout << ans << endl;
return 0;
}
``` |
#include <cstdio>
#include <iostream>
using namespace std;
#define debug(...) //fprintf(stderr, __VA_ARGS__)
inline char nc()
{
//return getchar();
static char buf[100000], * l = buf, * r = buf;
if(l == r) r = (l = buf) + fread(buf, 1, 100000, stdin);
if(l == r) return EOF;
return *l++;
}
template<class T> void readin(T & x)
{
x = 0; int f = 1, ch = nc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
x *= f;
}
const int mod = 1e9 + 7;
int n, k, a[605];
int dp[605][605], g[605], c[605][605];
void solve()
{
g[0] = 1;
for(int i = 1; i <= n; i++)
{
if(i & 1) g[i] = 0;
else g[i] = 1ll * g[i - 2] * (i - 1) % mod;
}
debug("%d\n", g[n]);
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
c[i][j] = c[i][j - 1] + int(a[j] == 0);
}
}
for(int len = 1; len <= n; len++)
{
for(int i = 1, j = len; j <= n; i++, j++)
{
int ok = true;
for(int h = i; h <= j; h++)
{
if(a[h] && (a[h] < i || a[h] > j))
{
ok = false;
break;
}
}
if(ok == 0) continue;
int x = c[i][j];
dp[i][j] = g[x];
for(int h = i + 1; h < j; h++)
{
int z = c[h + 1][j];
dp[i][j] -= 1ll * dp[i][h] * g[z] % mod;
dp[i][j] %= mod;
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
debug("%d ", dp[i][j]);
int y = k - c[i][j];
ans += 1ll * dp[i][j] * g[y] % mod;
ans %= mod;
}
debug("\n");
}
ans = (ans + mod) % mod;
cout << ans << endl;
}
int main()
{
readin(n);
readin(k);
n <<= 1;
for(int i = 1; i <= k; i++)
{
int x, y;
readin(x);
readin(y);
a[x] = y;
a[y] = x;
}
k = n - k * 2;
solve();
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 <iostream>
using namespace std;
#define debug(...) //fprintf(stderr, __VA_ARGS__)
inline char nc()
{
//return getchar();
static char buf[100000], * l = buf, * r = buf;
if(l == r) r = (l = buf) + fread(buf, 1, 100000, stdin);
if(l == r) return EOF;
return *l++;
}
template<class T> void readin(T & x)
{
x = 0; int f = 1, ch = nc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=nc();}
while(ch>='0'&&ch<='9'){x=x*10-'0'+ch;ch=nc();}
x *= f;
}
const int mod = 1e9 + 7;
int n, k, a[605];
int dp[605][605], g[605], c[605][605];
void solve()
{
g[0] = 1;
for(int i = 1; i <= n; i++)
{
if(i & 1) g[i] = 0;
else g[i] = 1ll * g[i - 2] * (i - 1) % mod;
}
debug("%d\n", g[n]);
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
c[i][j] = c[i][j - 1] + int(a[j] == 0);
}
}
for(int len = 1; len <= n; len++)
{
for(int i = 1, j = len; j <= n; i++, j++)
{
int ok = true;
for(int h = i; h <= j; h++)
{
if(a[h] && (a[h] < i || a[h] > j))
{
ok = false;
break;
}
}
if(ok == 0) continue;
int x = c[i][j];
dp[i][j] = g[x];
for(int h = i + 1; h < j; h++)
{
int z = c[h + 1][j];
dp[i][j] -= 1ll * dp[i][h] * g[z] % mod;
dp[i][j] %= mod;
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
debug("%d ", dp[i][j]);
int y = k - c[i][j];
ans += 1ll * dp[i][j] * g[y] % mod;
ans %= mod;
}
debug("\n");
}
ans = (ans + mod) % mod;
cout << ans << endl;
}
int main()
{
readin(n);
readin(k);
n <<= 1;
for(int i = 1; i <= k; i++)
{
int x, y;
readin(x);
readin(y);
a[x] = y;
a[y] = x;
}
k = n - k * 2;
solve();
return 0;
}
``` |
//Love and Freedom.
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 20021225
#define N 610
#define mdn 1000000007
using namespace std;
int read()
{
int s=0,t=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') t=-1; ch=getchar();}
while(ch>='0' && ch<='9') s=s*10+ch-'0',ch=getchar();
return s*t;
}
int f[N][N],g[N][N];
int n,a[N],b[N],fac[N],k;
void upd(int &x,int y){x+=x+y>=mdn?y-mdn:y;}
bool in(int x,int l,int r){return x>=l&&x<=r;}
int main()
{
n=read(),k=read(); fac[0]=1; int top=2*n;
for(int i=1;i<=k;i++) a[i]=read(),b[i]=read();
for(int i=2;i<=2*n;i+=2) fac[i]=1ll*fac[i-2]*(i-1)%mdn;
for(int i=1;i<=top;i++) for(int j=i+1;j<=top;j+=2)
{
int flag=1,tot=0;
for(int w=1;w<=k;w++)
flag&=(in(a[w],i,j)==in(b[w],i,j)),tot+=in(a[w],i,j)+in(b[w],i,j);
if(!flag) continue;
g[i][j]=fac[j-i+1-tot]; g[j][i]=fac[top-2*k-j+i-1+tot];
}
int ans=0;
for(int len=2;len<=2*n;len+=2) for(int i=1;i+len-1<=top;i++)
{
int j=i+len-1; f[i][j]=g[i][j];
for(int k=i+1;k<j;k+=2) upd(f[i][j],mdn-1ll*f[i][k]*g[k+1][j]%mdn);
upd(ans,1ll*f[i][j]*g[j][i]%mdn);
}
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
//Love and Freedom.
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 20021225
#define N 610
#define mdn 1000000007
using namespace std;
int read()
{
int s=0,t=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') t=-1; ch=getchar();}
while(ch>='0' && ch<='9') s=s*10+ch-'0',ch=getchar();
return s*t;
}
int f[N][N],g[N][N];
int n,a[N],b[N],fac[N],k;
void upd(int &x,int y){x+=x+y>=mdn?y-mdn:y;}
bool in(int x,int l,int r){return x>=l&&x<=r;}
int main()
{
n=read(),k=read(); fac[0]=1; int top=2*n;
for(int i=1;i<=k;i++) a[i]=read(),b[i]=read();
for(int i=2;i<=2*n;i+=2) fac[i]=1ll*fac[i-2]*(i-1)%mdn;
for(int i=1;i<=top;i++) for(int j=i+1;j<=top;j+=2)
{
int flag=1,tot=0;
for(int w=1;w<=k;w++)
flag&=(in(a[w],i,j)==in(b[w],i,j)),tot+=in(a[w],i,j)+in(b[w],i,j);
if(!flag) continue;
g[i][j]=fac[j-i+1-tot]; g[j][i]=fac[top-2*k-j+i-1+tot];
}
int ans=0;
for(int len=2;len<=2*n;len+=2) for(int i=1;i+len-1<=top;i++)
{
int j=i+len-1; f[i][j]=g[i][j];
for(int k=i+1;k<j;k+=2) upd(f[i][j],mdn-1ll*f[i][k]*g[k+1][j]%mdn);
upd(ans,1ll*f[i][j]*g[j][i]%mdn);
}
printf("%d\n",ans);
return 0;
}
``` |
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void rd(T &x) {
x=0; char c=getchar(); int f=1;
while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=610,mod=1e9+7;
inline void Add(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; }
int f[N][N],g[N][N],h[N][N],fac[N];
int A[N],B[N];
int n,m;
bool In(int x,int l,int r) { return x>=l&&x<=r; }
int main() {
rd(n),rd(m);
for(int i=1;i<=m;++i) rd(A[i]),rd(B[i]);
fac[0]=1;
for(int i=2;i<=2*n;i+=2) fac[i]=fac[i-2]*(ll)(i-1)%mod;
for(int i=1;i<=2*n;++i)
for(int j=i+1;j<=2*n;j+=2) {
int tot=0,flg=1;
for(int k=1;k<=m;++k) {
flg&=In(A[k],i,j)==In(B[k],i,j);
tot+=In(A[k],i,j)+In(B[k],i,j);
}
if(!flg) continue;
g[i][j]=fac[j-i+1-tot];
h[i][j]=fac[2*n-m*2-(j-i+1-tot)];
// cout<<2*n-m*2-(j-i+1-tot)<<' '<<g[i][j]<<' '<<g[j][i]<<endl;
}
int ans=0;
for(int len=2;len<=2*n;len+=2)
for(int i=1;i+len-1<=2*n;++i) {
int j=i+len-1;
f[i][j]=g[i][j];
for(int k=i+1;k<j;k+=2)
Dec(f[i][j],f[i][k]*(ll)g[k+1][j]%mod);
Add(ans,f[i][j]*(ll)h[i][j]%mod);
}
printf("%d",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 <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
using namespace std;
template <class T>
inline void rd(T &x) {
x=0; char c=getchar(); int f=1;
while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=610,mod=1e9+7;
inline void Add(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; }
int f[N][N],g[N][N],h[N][N],fac[N];
int A[N],B[N];
int n,m;
bool In(int x,int l,int r) { return x>=l&&x<=r; }
int main() {
rd(n),rd(m);
for(int i=1;i<=m;++i) rd(A[i]),rd(B[i]);
fac[0]=1;
for(int i=2;i<=2*n;i+=2) fac[i]=fac[i-2]*(ll)(i-1)%mod;
for(int i=1;i<=2*n;++i)
for(int j=i+1;j<=2*n;j+=2) {
int tot=0,flg=1;
for(int k=1;k<=m;++k) {
flg&=In(A[k],i,j)==In(B[k],i,j);
tot+=In(A[k],i,j)+In(B[k],i,j);
}
if(!flg) continue;
g[i][j]=fac[j-i+1-tot];
h[i][j]=fac[2*n-m*2-(j-i+1-tot)];
// cout<<2*n-m*2-(j-i+1-tot)<<' '<<g[i][j]<<' '<<g[j][i]<<endl;
}
int ans=0;
for(int len=2;len<=2*n;len+=2)
for(int i=1;i+len-1<=2*n;++i) {
int j=i+len-1;
f[i][j]=g[i][j];
for(int k=i+1;k<j;k+=2)
Dec(f[i][j],f[i][k]*(ll)g[k+1][j]%mod);
Add(ans,f[i][j]*(ll)h[i][j]%mod);
}
printf("%d",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int mo=1000000007;
const int N=605;
int n,k,S[N];
int x[N],y[N],f[N];
int g[N][N],h[N][N];
int main(){
scanf("%d%d",&n,&k);
n*=2;
For(i,1,n) S[i]=1;
For(i,1,k) scanf("%d%d",&x[i],&y[i]);
For(i,1,n) S[x[i]]=S[y[i]]=0;
For(i,1,n) S[i]+=S[i-1];
f[0]=1;
for (int i=2;i<=n;i+=2)
f[i]=1ll*f[i-2]*(i-1)%mo;
For(i,1,n) For(j,i+1,n){
bool fl=0;
For(p,1,k) fl|=(i<=x[p]&&x[p]<=j)^(i<=y[p]&&y[p]<=j);
if (fl) continue;
int sum=S[j]-S[i-1];
g[i][j]=h[i][j]=f[sum];
}
For(i,1,n) For(j,i+1,n) For(k,j+1,n)
g[i][k]=(g[i][k]+mo-1ll*g[i][j]*h[j+1][k]%mo)%mo;
int ans=f[n-2*k];
For(i,1,n-1) For(j,i+1,n-1){
int res=n-2*k-(S[j]-S[i-1]);
ans=(ans+1ll*f[res]*g[i][j])%mo;
}
printf("%d\n",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<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int mo=1000000007;
const int N=605;
int n,k,S[N];
int x[N],y[N],f[N];
int g[N][N],h[N][N];
int main(){
scanf("%d%d",&n,&k);
n*=2;
For(i,1,n) S[i]=1;
For(i,1,k) scanf("%d%d",&x[i],&y[i]);
For(i,1,n) S[x[i]]=S[y[i]]=0;
For(i,1,n) S[i]+=S[i-1];
f[0]=1;
for (int i=2;i<=n;i+=2)
f[i]=1ll*f[i-2]*(i-1)%mo;
For(i,1,n) For(j,i+1,n){
bool fl=0;
For(p,1,k) fl|=(i<=x[p]&&x[p]<=j)^(i<=y[p]&&y[p]<=j);
if (fl) continue;
int sum=S[j]-S[i-1];
g[i][j]=h[i][j]=f[sum];
}
For(i,1,n) For(j,i+1,n) For(k,j+1,n)
g[i][k]=(g[i][k]+mo-1ll*g[i][j]*h[j+1][k]%mo)%mo;
int ans=f[n-2*k];
For(i,1,n-1) For(j,i+1,n-1){
int res=n-2*k-(S[j]-S[i-1]);
ans=(ans+1ll*f[res]*g[i][j])%mo;
}
printf("%d\n",ans);
}
``` |
#include "bits//stdc++.h"
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define ALL(v) v.begin(), v.end()
typedef pair<int, int> P;
int cnt[600];
ll dp[601][601];
ll g[601];
int main() {
int N, K;
cin >> N >> K;
g[0] = 1;
for (int i = 2; i <= 2 * N; i += 2) {
g[i] = g[i - 2] * (i - 1) % MOD;
}
for (int i = 0; i < 2 * N; i++) cnt[i] = 1;
vector<P> v;
for (int i = 0; i < K; i++) {
int A, B;
cin >> A >> B;
A--; B--;
cnt[A] = cnt[B] = 0;
v.emplace_back(A, B);
}
for (int i = 1; i < 2 * N; i++) cnt[i] += cnt[i - 1];
ll ans = 0;
for (int len = 2; len <= 2 * N; len +=2) {
for (int i = 0; i + len <= 2 * N; i++) {
bool ok = 1;
for (int j = 0; j < K; j++) {
bool b1 = (i <= v[j].first && v[j].first < i + len);
bool b2 = (i <= v[j].second && v[j].second < i + len);
if (b1 != b2) {
ok = 0;
break;
}
}
if (!ok) continue;
int c1 = cnt[i + len - 1];
if (i > 0) c1 -= cnt[i - 1];
ll sum = g[c1];
for (int j = i + 1; j + 1 < i + len; j +=2) {
int c2 = cnt[i + len - 1];
c2 -= cnt[j];
ll t = dp[i][j] * g[c2] % MOD;
(sum += MOD - t) %= MOD;
}
dp[i][i + len - 1] = sum;
int c3 = cnt[2 * N - 1] - cnt[i + len - 1];
if (i > 0) c3 += cnt[i - 1];
(sum *= g[c3]) %= MOD;
(ans += sum) %= 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;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define ALL(v) v.begin(), v.end()
typedef pair<int, int> P;
int cnt[600];
ll dp[601][601];
ll g[601];
int main() {
int N, K;
cin >> N >> K;
g[0] = 1;
for (int i = 2; i <= 2 * N; i += 2) {
g[i] = g[i - 2] * (i - 1) % MOD;
}
for (int i = 0; i < 2 * N; i++) cnt[i] = 1;
vector<P> v;
for (int i = 0; i < K; i++) {
int A, B;
cin >> A >> B;
A--; B--;
cnt[A] = cnt[B] = 0;
v.emplace_back(A, B);
}
for (int i = 1; i < 2 * N; i++) cnt[i] += cnt[i - 1];
ll ans = 0;
for (int len = 2; len <= 2 * N; len +=2) {
for (int i = 0; i + len <= 2 * N; i++) {
bool ok = 1;
for (int j = 0; j < K; j++) {
bool b1 = (i <= v[j].first && v[j].first < i + len);
bool b2 = (i <= v[j].second && v[j].second < i + len);
if (b1 != b2) {
ok = 0;
break;
}
}
if (!ok) continue;
int c1 = cnt[i + len - 1];
if (i > 0) c1 -= cnt[i - 1];
ll sum = g[c1];
for (int j = i + 1; j + 1 < i + len; j +=2) {
int c2 = cnt[i + len - 1];
c2 -= cnt[j];
ll t = dp[i][j] * g[c2] % MOD;
(sum += MOD - t) %= MOD;
}
dp[i][i + len - 1] = sum;
int c3 = cnt[2 * N - 1] - cnt[i + len - 1];
if (i > 0) c3 += cnt[i - 1];
(sum *= g[c3]) %= MOD;
(ans += sum) %= MOD;
}
}
cout << ans << endl;
}
``` |
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int read();
void Add(int &x, int y) { (x += y) >= mod ? x -= mod : x; }
int n, k;
int vis[1003];
int f[1003][1003], g[1003], lim = 1000;
int pre[1003];
int h(int l, int r) { return pre[r] - pre[l - 1]; }
int fsp(int bs, int p) {
int rt = 1;
while (p) {
if (p & 1) rt = 1ll * rt * bs % mod;
bs = 1ll * bs * bs % mod, p >>= 1;
}
return rt;
}
void init() {
g[0] = 1;
for (int i = 1; i <= lim; ++i) g[i] = 1ll * g[i - 1] * ((i << 1) - 1) % mod;
}
int res;
void solve(int l, int r) {
for (int x = l; x <= r; ++x)
if (vis[x] && (vis[x] < l || vis[x] > r)) return;
f[l][r] = g[h(l, r) >> 1];
for (int x = l + 1; x < r; ++x)
Add(f[l][r], mod - 1ll * f[l][x] * g[h(x + 1, r) >> 1] % mod);
Add(res, 1ll * f[l][r] * g[h(1, l - 1) + h(r + 1, n << 1) >> 1] % mod);
}
int main() {
n = read(), k = read(), init();
for (int t = 1, u, v; t <= k; ++t)
u = read(), v = read(), vis[u] = v, vis[v] = u;
for (int i = 1; i <= 2 * n; ++i) pre[i] = pre[i - 1] + (!vis[i]);
for (int l = 1; l <= 2 * n; ++l)
for (int r = l + 1; r <= 2 * n; ++r)
!((h(l, r) & 1) || (r - l + 1 & 1)) ? solve(l, r) : void();
printf("%d\n", res);
return 0;
}
const int _SIZE = 1 << 22;
char ibuf[_SIZE], *iS = ibuf, *iT = ibuf;
#define gc \
(iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)), \
(iS == iT ? EOF : *iS++) : *iS++)
int read() {
int x = 0, f = 1;
char c = gc;
while (!isdigit(c)) f = (c == '-') ? -1 : f, c = gc;
while (isdigit(c)) x = x * 10 + c - '0', c = gc;
return x * f;
} | ### 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;
int read();
void Add(int &x, int y) { (x += y) >= mod ? x -= mod : x; }
int n, k;
int vis[1003];
int f[1003][1003], g[1003], lim = 1000;
int pre[1003];
int h(int l, int r) { return pre[r] - pre[l - 1]; }
int fsp(int bs, int p) {
int rt = 1;
while (p) {
if (p & 1) rt = 1ll * rt * bs % mod;
bs = 1ll * bs * bs % mod, p >>= 1;
}
return rt;
}
void init() {
g[0] = 1;
for (int i = 1; i <= lim; ++i) g[i] = 1ll * g[i - 1] * ((i << 1) - 1) % mod;
}
int res;
void solve(int l, int r) {
for (int x = l; x <= r; ++x)
if (vis[x] && (vis[x] < l || vis[x] > r)) return;
f[l][r] = g[h(l, r) >> 1];
for (int x = l + 1; x < r; ++x)
Add(f[l][r], mod - 1ll * f[l][x] * g[h(x + 1, r) >> 1] % mod);
Add(res, 1ll * f[l][r] * g[h(1, l - 1) + h(r + 1, n << 1) >> 1] % mod);
}
int main() {
n = read(), k = read(), init();
for (int t = 1, u, v; t <= k; ++t)
u = read(), v = read(), vis[u] = v, vis[v] = u;
for (int i = 1; i <= 2 * n; ++i) pre[i] = pre[i - 1] + (!vis[i]);
for (int l = 1; l <= 2 * n; ++l)
for (int r = l + 1; r <= 2 * n; ++r)
!((h(l, r) & 1) || (r - l + 1 & 1)) ? solve(l, r) : void();
printf("%d\n", res);
return 0;
}
const int _SIZE = 1 << 22;
char ibuf[_SIZE], *iS = ibuf, *iT = ibuf;
#define gc \
(iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)), \
(iS == iT ? EOF : *iS++) : *iS++)
int read() {
int x = 0, f = 1;
char c = gc;
while (!isdigit(c)) f = (c == '-') ? -1 : f, c = gc;
while (isdigit(c)) x = x * 10 + c - '0', c = gc;
return x * f;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const long long mod=1000000007ll;
void ad(long long &a,long long b){a+=b;a%=mod;}
void mn(long long &a,long long b){a+=mod-b;a%=mod;}
int main()
{
int n,K;
scanf("%d%d",&n,&K);
vector<int> a(K),b(K);
for(int i=0;i<K;i++){
scanf("%d%d",&a[i],&b[i]);
a[i]--,b[i]--;
}
vector<long long> predp(n+1);
predp[0]=1ll;
for(int i=1;i<=n;i++){
predp[i]=predp[i-1]*(2*i-1)%mod;
}
vector<vector<long long> > dp1(2*n,vector<long long>(2*n));
vector<vector<int> > dc(2*n,vector<int>(2*n));
long long ans=0;
for(int i=0;i<2*n;i++){
for(int t=1;i+t<2*n;t+=2){
int j=i+t;
bool F=true;
int c=(t+1)/2;
for(int x=0;x<K;x++){
bool F1=(i<=a[x]&&a[x]<=j),F2=(i<=b[x]&&b[x]<=j);
if(F1!=F2){
F=false;
break;
}
if(F1&&F2){
c--;
}
}
if(!F){
dp1[i][j]=0ll;
dc[i][j]=-1;
}
else{
dp1[i][j]=predp[c];
dc[i][j]=c;
for(int k=i+1;k<j;k+=2){
if(dc[i][k]!=-1){
mn(dp1[i][j],dp1[i][k]*predp[c-dc[i][k]]%mod);
}
}
ad(ans,dp1[i][j]*predp[n-K-c]%mod);
//printf("%d %d %lld\n",i,j,dp1[i][j]);
}
}
}
printf("%lld\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 long long mod=1000000007ll;
void ad(long long &a,long long b){a+=b;a%=mod;}
void mn(long long &a,long long b){a+=mod-b;a%=mod;}
int main()
{
int n,K;
scanf("%d%d",&n,&K);
vector<int> a(K),b(K);
for(int i=0;i<K;i++){
scanf("%d%d",&a[i],&b[i]);
a[i]--,b[i]--;
}
vector<long long> predp(n+1);
predp[0]=1ll;
for(int i=1;i<=n;i++){
predp[i]=predp[i-1]*(2*i-1)%mod;
}
vector<vector<long long> > dp1(2*n,vector<long long>(2*n));
vector<vector<int> > dc(2*n,vector<int>(2*n));
long long ans=0;
for(int i=0;i<2*n;i++){
for(int t=1;i+t<2*n;t+=2){
int j=i+t;
bool F=true;
int c=(t+1)/2;
for(int x=0;x<K;x++){
bool F1=(i<=a[x]&&a[x]<=j),F2=(i<=b[x]&&b[x]<=j);
if(F1!=F2){
F=false;
break;
}
if(F1&&F2){
c--;
}
}
if(!F){
dp1[i][j]=0ll;
dc[i][j]=-1;
}
else{
dp1[i][j]=predp[c];
dc[i][j]=c;
for(int k=i+1;k<j;k+=2){
if(dc[i][k]!=-1){
mn(dp1[i][j],dp1[i][k]*predp[c-dc[i][k]]%mod);
}
}
ad(ans,dp1[i][j]*predp[n-K-c]%mod);
//printf("%d %d %lld\n",i,j,dp1[i][j]);
}
}
}
printf("%lld\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N=605,mo=1e9+7;
int n,K,a,b,i,j,k,f[N][N],g[N],h[N][N],ans,le[N][N],ri[N][N],s[N][N];
int main(){
scanf("%d%d",&n,&K);
for(i=1;i<=n*2;++i)for(j=i;j<=n*2;++j)le[i][j]=N;
for(i=0;i<K;++i){
scanf("%d%d",&a,&b);if(a>b)swap(a,b);
le[b][b]=a;ri[a][a]=b;s[a][b]=1;
}
for(i=1;i<=n*2;++i)for(j=i;j<n*2;++j)le[i][j+1]=min(le[i][j+1],le[i][j]),
ri[i][j+1]=max(ri[i][j+1],ri[i][j]),s[i][j+1]+=s[i][j];
for(i=n*2;i>1;--i)for(j=i;j<=n*2;++j)le[i-1][j]=min(le[i-1][j],le[i][j]),
ri[i-1][j]=max(ri[i-1][j],ri[i][j]),s[i-1][j]+=s[i][j];
for(i=*g=1;i<N;++i)g[i]=(2ll*i-1)*g[i-1]%mo;
for(i=1;i<n*2;i+=2)for(j=1;j+i<=n*2;++j){
int l=j,r=j+i;
if(le[l][r]>=l && ri[l][r]<=r){
f[l][r]=g[(i+1)/2-s[l][r]];
for(k=1;k<i;k+=2)f[l][r]=(f[l][r]+mo-1ll*f[l][l+k]*h[l+k+1][r]%mo)%mo,
h[l][r]=(h[l][r]+1ll*f[l][l+k]*h[l+k+1][r])%mo;
h[l][r]=(h[l][r]+f[l][r])%mo;
ans=(ans+1ll*f[l][r]*g[n-(i+1)/2-(K-s[l][r])])%mo;
}
}
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;
const int N=605,mo=1e9+7;
int n,K,a,b,i,j,k,f[N][N],g[N],h[N][N],ans,le[N][N],ri[N][N],s[N][N];
int main(){
scanf("%d%d",&n,&K);
for(i=1;i<=n*2;++i)for(j=i;j<=n*2;++j)le[i][j]=N;
for(i=0;i<K;++i){
scanf("%d%d",&a,&b);if(a>b)swap(a,b);
le[b][b]=a;ri[a][a]=b;s[a][b]=1;
}
for(i=1;i<=n*2;++i)for(j=i;j<n*2;++j)le[i][j+1]=min(le[i][j+1],le[i][j]),
ri[i][j+1]=max(ri[i][j+1],ri[i][j]),s[i][j+1]+=s[i][j];
for(i=n*2;i>1;--i)for(j=i;j<=n*2;++j)le[i-1][j]=min(le[i-1][j],le[i][j]),
ri[i-1][j]=max(ri[i-1][j],ri[i][j]),s[i-1][j]+=s[i][j];
for(i=*g=1;i<N;++i)g[i]=(2ll*i-1)*g[i-1]%mo;
for(i=1;i<n*2;i+=2)for(j=1;j+i<=n*2;++j){
int l=j,r=j+i;
if(le[l][r]>=l && ri[l][r]<=r){
f[l][r]=g[(i+1)/2-s[l][r]];
for(k=1;k<i;k+=2)f[l][r]=(f[l][r]+mo-1ll*f[l][l+k]*h[l+k+1][r]%mo)%mo,
h[l][r]=(h[l][r]+1ll*f[l][l+k]*h[l+k+1][r])%mo;
h[l][r]=(h[l][r]+f[l][r])%mo;
ans=(ans+1ll*f[l][r]*g[n-(i+1)/2-(K-s[l][r])])%mo;
}
}
printf("%d\n",ans);
}
``` |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e3,mod=1e9+7;
ll n,m,k,f[maxn][maxn],g[maxn]={1},match[maxn],vis[maxn],pre[maxn],ans,u,v;
signed main(){
scanf("%lld%lld",&n,&m),n<<=1;
for(ll i=1;i<=m;++i){
scanf("%lld%lld",&u,&v);
vis[match[u]=v]=vis[match[v]=u]=1;
}
for(ll i=1;i<=n;++i)pre[i]=pre[i-1]+(!vis[i]);
for(ll i=2;i<=n;i+=2)g[i]=g[i-2]*(i-1)%mod;
for(ll i=2;i<=n;i+=2)for(ll l=1;l<=n-i+1;++l){
ll r=l+i-1;
for(ll j=l;j<=r;++j)if(match[j]&&(match[j]<l||match[j]>r))goto end;
f[l][r]=g[pre[r]-pre[l-1]];
for(ll j=l+1;j<r;j+=2)(f[l][r]+=(mod-f[l][j]*g[pre[r]-pre[j]]%mod))%=mod;
(ans+=g[(pre[n]-pre[r]+pre[l-1])]*f[l][r]%mod)%=mod;
end:;
}
printf("%lld\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;
typedef long long ll;
const int maxn=1e3,mod=1e9+7;
ll n,m,k,f[maxn][maxn],g[maxn]={1},match[maxn],vis[maxn],pre[maxn],ans,u,v;
signed main(){
scanf("%lld%lld",&n,&m),n<<=1;
for(ll i=1;i<=m;++i){
scanf("%lld%lld",&u,&v);
vis[match[u]=v]=vis[match[v]=u]=1;
}
for(ll i=1;i<=n;++i)pre[i]=pre[i-1]+(!vis[i]);
for(ll i=2;i<=n;i+=2)g[i]=g[i-2]*(i-1)%mod;
for(ll i=2;i<=n;i+=2)for(ll l=1;l<=n-i+1;++l){
ll r=l+i-1;
for(ll j=l;j<=r;++j)if(match[j]&&(match[j]<l||match[j]>r))goto end;
f[l][r]=g[pre[r]-pre[l-1]];
for(ll j=l+1;j<r;j+=2)(f[l][r]+=(mod-f[l][j]*g[pre[r]-pre[j]]%mod))%=mod;
(ans+=g[(pre[n]-pre[r]+pre[l-1])]*f[l][r]%mod)%=mod;
end:;
}
printf("%lld\n",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
const int N=605,mu=1000000007;
int s[N],a[N],f[N][N],Ans,ans[N],x,y,n,k;
int c(int x,int y){
if (x>y) return 0;
return ans[s[y]-s[x-1]];
}
int main(){
scanf("%d%d",&n,&k);
n<<=1;
for (int i=1;i<=n;i++) s[i]=1;
for (int i=1;i<=k;i++){
scanf("%d%d",&x,&y);
a[x]=y,a[y]=x,s[x]=0,s[y]=0;
}
for (int i=1;i<=n;i++) s[i]=s[i]+s[i-1];
ans[0]=1;
for (int i=2;i<=n;i+=2)
ans[i]=ans[i-2]*1ll*(i-1)%mu;
for (int l=1;l<=n/2;l++)
for (int i=1;i<=n-2*l+1;i++){
int j=i+2*l-1,ff=0;
for (int k=i;k<=j;k++)
if ((a[k] && a[k]<i) || a[k]>j) ff=1;
if (ff) continue;
f[i][j]=c(i,j);
for (int k=i+1;k<j;k+=2)
f[i][j]=(f[i][j]-1ll*f[i][k]*c(k+1,j)%mu+mu)%mu;
Ans=(Ans+1ll*f[i][j]*ans[s[n]-(s[j]-s[i-1])]%mu)%mu;
}
printf("%d\n",Ans);
} | ### 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>
const int N=605,mu=1000000007;
int s[N],a[N],f[N][N],Ans,ans[N],x,y,n,k;
int c(int x,int y){
if (x>y) return 0;
return ans[s[y]-s[x-1]];
}
int main(){
scanf("%d%d",&n,&k);
n<<=1;
for (int i=1;i<=n;i++) s[i]=1;
for (int i=1;i<=k;i++){
scanf("%d%d",&x,&y);
a[x]=y,a[y]=x,s[x]=0,s[y]=0;
}
for (int i=1;i<=n;i++) s[i]=s[i]+s[i-1];
ans[0]=1;
for (int i=2;i<=n;i+=2)
ans[i]=ans[i-2]*1ll*(i-1)%mu;
for (int l=1;l<=n/2;l++)
for (int i=1;i<=n-2*l+1;i++){
int j=i+2*l-1,ff=0;
for (int k=i;k<=j;k++)
if ((a[k] && a[k]<i) || a[k]>j) ff=1;
if (ff) continue;
f[i][j]=c(i,j);
for (int k=i+1;k<j;k+=2)
f[i][j]=(f[i][j]-1ll*f[i][k]*c(k+1,j)%mu+mu)%mu;
Ans=(Ans+1ll*f[i][j]*ans[s[n]-(s[j]-s[i-1])]%mu)%mu;
}
printf("%d\n",Ans);
}
``` |
#include<bits/stdc++.h>
#define title "chords"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed<<setprecision(x)
#define pii pair<ll,ll>
using namespace std;
void Freopen(){
freopen(title".in","r",stdin);
freopen(title".out","w",stdout);
}
ll read(){
ll g=0,f=1;
char ch=getchar();
while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();}
return g*f;
}
const ll N=605;
const ll mod=1e9+7;
ll cp[N],n,m,vis[N],g[N],dp[N][N],ans;
ll su(ll l,ll r){
return r-l+1-(vis[r]-vis[l-1]);
}
signed main(){
n=read()<<1,m=read();
for(ll i=1;i<=m;i++){
ll x=read(),y=read();
cp[x]=y,cp[y]=x,vis[x]=vis[y]=1;
}
for(ll i=1;i<=n;i++)vis[i]+=vis[i-1];
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++)if((j-i)&1){
bool flag=true;
for(ll k=i;k<=j;k++)if(cp[k]&&(cp[k]<i||cp[k]>j))flag=false;
if(!flag)continue;
dp[i][j]=g[su(i,j)];
for(ll k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-dp[i][k]*g[su(k+1,j)]%mod+mod)%mod;
ans=(ans+dp[i][j]*g[su(1,i-1)+su(j+1,n)]%mod)%mod;
}return cout<<ans,signed();
} | ### 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 title "chords"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed<<setprecision(x)
#define pii pair<ll,ll>
using namespace std;
void Freopen(){
freopen(title".in","r",stdin);
freopen(title".out","w",stdout);
}
ll read(){
ll g=0,f=1;
char ch=getchar();
while(ch<'0'||'9'<ch){if(ch=='-')f=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){g=g*10+ch-'0';ch=getchar();}
return g*f;
}
const ll N=605;
const ll mod=1e9+7;
ll cp[N],n,m,vis[N],g[N],dp[N][N],ans;
ll su(ll l,ll r){
return r-l+1-(vis[r]-vis[l-1]);
}
signed main(){
n=read()<<1,m=read();
for(ll i=1;i<=m;i++){
ll x=read(),y=read();
cp[x]=y,cp[y]=x,vis[x]=vis[y]=1;
}
for(ll i=1;i<=n;i++)vis[i]+=vis[i-1];
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++)if((j-i)&1){
bool flag=true;
for(ll k=i;k<=j;k++)if(cp[k]&&(cp[k]<i||cp[k]>j))flag=false;
if(!flag)continue;
dp[i][j]=g[su(i,j)];
for(ll k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-dp[i][k]*g[su(k+1,j)]%mod+mod)%mod;
ans=(ans+dp[i][j]*g[su(1,i-1)+su(j+1,n)]%mod)%mod;
}return cout<<ans,signed();
}
``` |
#include <bits/stdc++.h>
#define il inline
#define ll long long
const int N=605,P=1e9+7;
int n,K,ans,to[N],s[N],f[N][N],g[N];
il int cal(int l,int r)
{
if ((s[r]-s[l-1]&1)||(r-l+1&1)) return 0; int i;
for (i=l; i<=r; i++) if (to[i]&&(to[i]<l||to[i]>r)) return 0;
f[l][r]=g[s[r]-s[l-1]>>1];
for (i=l+1; i<r; i++) f[l][r]=(f[l][r]-(ll)f[l][i]*g[s[r]-s[i]>>1]%P+P)%P;
return (ll)f[l][r]*g[s[l-1]+s[n]-s[r]>>1]%P;
}
int main()
{
scanf("%d%d",&n,&K),n+=n; int i,j,u,v;
for (i=1; i<=K; i++) scanf("%d%d",&u,&v),to[u]=v,to[v]=u;
for (i=1; i<=n; i++) s[i]=s[i-1]+(!to[i]);
for (i=g[0]=1; i<N; i++) g[i]=(ll)g[i-1]*(i+i-1)%P;
for (i=1; i<=n; i++) for (j=i+1; j<=n; j++) ans=(ans+cal(i,j))%P;
printf("%d",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 il inline
#define ll long long
const int N=605,P=1e9+7;
int n,K,ans,to[N],s[N],f[N][N],g[N];
il int cal(int l,int r)
{
if ((s[r]-s[l-1]&1)||(r-l+1&1)) return 0; int i;
for (i=l; i<=r; i++) if (to[i]&&(to[i]<l||to[i]>r)) return 0;
f[l][r]=g[s[r]-s[l-1]>>1];
for (i=l+1; i<r; i++) f[l][r]=(f[l][r]-(ll)f[l][i]*g[s[r]-s[i]>>1]%P+P)%P;
return (ll)f[l][r]*g[s[l-1]+s[n]-s[r]>>1]%P;
}
int main()
{
scanf("%d%d",&n,&K),n+=n; int i,j,u,v;
for (i=1; i<=K; i++) scanf("%d%d",&u,&v),to[u]=v,to[v]=u;
for (i=1; i<=n; i++) s[i]=s[i-1]+(!to[i]);
for (i=g[0]=1; i<N; i++) g[i]=(ll)g[i-1]*(i+i-1)%P;
for (i=1; i<=n; i++) for (j=i+1; j<=n; j++) ans=(ans+cal(i,j))%P;
printf("%d",ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int N = 605;
int n,k;
int ok[N];
int to[N];
ll mem[N][N];
ll g[N];
int p[N];
int ct = 0;
ll dp(int l, int r){
if (l == r) return 0;
if (mem[l][r] != -1) return mem[l][r];
for (int i = l; i <= r; i++){
if (to[i] != 0 && to[i] < l || to[i] > r) return mem[l][r] = 0;
}
int x = p[r]-p[l-1];
int z = ct-x;
mem[l][r] = g[x];
//printf("x %d g[x] %lld g[z] %lld\n",x,g[x],g[z]);
for (int k = l+1; k < r; k++){
// printf("- %lld*%lld = %lld\n",dp(l,k),g[p[r]-p[k]],(dp(l,k)*g[p[r]-p[k]])%mod);
mem[l][r] -= (dp(l,k)*g[p[r]-p[k]])%mod;
mem[l][r] %= mod;
}
mem[l][r] = (mem[l][r]%mod + mod)%mod;
//printf("%d %d %lld\n",l,r,mem[l][r]);
return mem[l][r];
}
int main(){
scanf("%d%d",&n,&k);
g[0] = 1;
for (int i = 2; i <= 2*n; i+=2){
g[i] = g[i-2]*(i-1)%mod;
}
for (int i = 0,a,b; i < k; i++){
scanf("%d%d",&a,&b);
to[a] = b;
to[b] = a;
ok[a] = ok[b] = 1;
}
for (int i = 1; i <= 2*n; i++){
p[i] += p[i-1]+(1-ok[i]);
}
ct = p[2*n];
ll ans = 0;
memset(mem,-1,sizeof(mem));
for (int i = 1; i <= 2*n; i++){
for (int j = i; j <= 2*n; j++){
int CT = ct-(p[j]-p[i-1]);
ans += dp(i,j)*g[CT];
//printf("%d %d %lld*%lld\n",i,j,dp(i,j),g[CT]);
ans %= mod;
}
}
printf("%lld ",(ans+mod)%mod);
}
| ### 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 N = 605;
int n,k;
int ok[N];
int to[N];
ll mem[N][N];
ll g[N];
int p[N];
int ct = 0;
ll dp(int l, int r){
if (l == r) return 0;
if (mem[l][r] != -1) return mem[l][r];
for (int i = l; i <= r; i++){
if (to[i] != 0 && to[i] < l || to[i] > r) return mem[l][r] = 0;
}
int x = p[r]-p[l-1];
int z = ct-x;
mem[l][r] = g[x];
//printf("x %d g[x] %lld g[z] %lld\n",x,g[x],g[z]);
for (int k = l+1; k < r; k++){
// printf("- %lld*%lld = %lld\n",dp(l,k),g[p[r]-p[k]],(dp(l,k)*g[p[r]-p[k]])%mod);
mem[l][r] -= (dp(l,k)*g[p[r]-p[k]])%mod;
mem[l][r] %= mod;
}
mem[l][r] = (mem[l][r]%mod + mod)%mod;
//printf("%d %d %lld\n",l,r,mem[l][r]);
return mem[l][r];
}
int main(){
scanf("%d%d",&n,&k);
g[0] = 1;
for (int i = 2; i <= 2*n; i+=2){
g[i] = g[i-2]*(i-1)%mod;
}
for (int i = 0,a,b; i < k; i++){
scanf("%d%d",&a,&b);
to[a] = b;
to[b] = a;
ok[a] = ok[b] = 1;
}
for (int i = 1; i <= 2*n; i++){
p[i] += p[i-1]+(1-ok[i]);
}
ct = p[2*n];
ll ans = 0;
memset(mem,-1,sizeof(mem));
for (int i = 1; i <= 2*n; i++){
for (int j = i; j <= 2*n; j++){
int CT = ct-(p[j]-p[i-1]);
ans += dp(i,j)*g[CT];
//printf("%d %d %lld*%lld\n",i,j,dp(i,j),g[CT]);
ans %= mod;
}
}
printf("%lld ",(ans+mod)%mod);
}
``` |
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
const ll MOD=1e9+7;
int main()
{
int n, k; cin>>n>>k;
int a[301], b[301];
for(int i=0; i<k; i++){
cin>>a[i]>>b[i]; a[i]--; b[i]--;
if(a[i]>b[i]) swap(a[i], b[i]);
}
ll f[301]; f[0]=1;
for(ll i=1; i<=n; i++) f[i]=(2*i-1)*f[i-1]%MOD;
ll ct[601][601]={}, ct1[601][601]={}, ct2[601][601]={};
ll ans=0;
for(int i=1; i<2*n; i++){
for(int j=i-1; j>=0; j-=2){
int t=i-j+1, u=2*n-t;
bool dame=0;
for(int l=0; l<k; l++){
if(j<=a[l] && b[l]<=i) t-=2;
else if((j>a[l] && b[l]>i) || i<a[l] || b[l]<j) u-=2;
else{
dame=1;
break;
}
}
if(dame) continue;
ct[j][i]=f[t/2], ct2[j][i]=f[u/2];
ct1[j][i]=ct[j][i];
for(int l=i-1; l>j; l-=2){
ct1[j][i]-=ct[j][l-1]*ct1[l][i]%MOD;
ct1[j][i]=(ct1[j][i]+MOD)%MOD;
}
ans+=ct1[j][i]*ct2[j][i];
//cout<<ct1[j][i]<<" "<<ct2[j][i]<<" "<<j<<" "<<i<<endl;
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 <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
const ll MOD=1e9+7;
int main()
{
int n, k; cin>>n>>k;
int a[301], b[301];
for(int i=0; i<k; i++){
cin>>a[i]>>b[i]; a[i]--; b[i]--;
if(a[i]>b[i]) swap(a[i], b[i]);
}
ll f[301]; f[0]=1;
for(ll i=1; i<=n; i++) f[i]=(2*i-1)*f[i-1]%MOD;
ll ct[601][601]={}, ct1[601][601]={}, ct2[601][601]={};
ll ans=0;
for(int i=1; i<2*n; i++){
for(int j=i-1; j>=0; j-=2){
int t=i-j+1, u=2*n-t;
bool dame=0;
for(int l=0; l<k; l++){
if(j<=a[l] && b[l]<=i) t-=2;
else if((j>a[l] && b[l]>i) || i<a[l] || b[l]<j) u-=2;
else{
dame=1;
break;
}
}
if(dame) continue;
ct[j][i]=f[t/2], ct2[j][i]=f[u/2];
ct1[j][i]=ct[j][i];
for(int l=i-1; l>j; l-=2){
ct1[j][i]-=ct[j][l-1]*ct1[l][i]%MOD;
ct1[j][i]=(ct1[j][i]+MOD)%MOD;
}
ans+=ct1[j][i]*ct2[j][i];
//cout<<ct1[j][i]<<" "<<ct2[j][i]<<" "<<j<<" "<<i<<endl;
ans%=MOD;
}
}
cout<<ans<<endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int f[610][610],g[610][610],n,k,l,r,sum[610],h[610],ans;
bool bo[610][610];
int main()
{
h[0]=1;
for (int i=2; i<=600; i+=2) h[i]=1ll*(i-1)*h[i-2]%mod;
memset(bo,1,sizeof(bo));
scanf("%d%d",&n,&k),sum[0]=0,n<<=1;
for (int i=1; i<=k; i++)
{
scanf("%d%d",&l,&r);
if (l>r) swap(l,r);
sum[l]++,sum[r]++;
for (int j=1; j<=l; j++)
for (int k=l; k<r; k++) bo[j][k]=0;
for (int j=l+1; j<=r; j++)
for (int k=r; k<=n; k++) bo[j][k]=0;
}
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)
if (bo[i][j]) f[i][j]=h[j-i+1-sum[j]+sum[i-1]];
for (int i=1; i<=n; i++)
for (int j=i+1; j<=n; j+=2)
{
g[i][j]=f[i][j];
for (int k=i+1; k<j; k+=2) g[i][j]=(g[i][j]+1ll*(mod-g[i][k])*f[k+1][j])%mod;
ans=(ans+1ll*h[n-(j-i+1-sum[j]+sum[i-1])-2*k]*g[i][j])%mod;
}
return printf("%d\n",ans),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 mod=1000000007;
int f[610][610],g[610][610],n,k,l,r,sum[610],h[610],ans;
bool bo[610][610];
int main()
{
h[0]=1;
for (int i=2; i<=600; i+=2) h[i]=1ll*(i-1)*h[i-2]%mod;
memset(bo,1,sizeof(bo));
scanf("%d%d",&n,&k),sum[0]=0,n<<=1;
for (int i=1; i<=k; i++)
{
scanf("%d%d",&l,&r);
if (l>r) swap(l,r);
sum[l]++,sum[r]++;
for (int j=1; j<=l; j++)
for (int k=l; k<r; k++) bo[j][k]=0;
for (int j=l+1; j<=r; j++)
for (int k=r; k<=n; k++) bo[j][k]=0;
}
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)
if (bo[i][j]) f[i][j]=h[j-i+1-sum[j]+sum[i-1]];
for (int i=1; i<=n; i++)
for (int j=i+1; j<=n; j+=2)
{
g[i][j]=f[i][j];
for (int k=i+1; k<j; k+=2) g[i][j]=(g[i][j]+1ll*(mod-g[i][k])*f[k+1][j])%mod;
ans=(ans+1ll*h[n-(j-i+1-sum[j]+sum[i-1])-2*k]*g[i][j])%mod;
}
return printf("%d\n",ans),0;
}
``` |
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int n,k,cnt[610],p[610],f[610],dp[610][610],ans;
bool check(int x,int y)
{
if((cnt[y]-cnt[x-1])&1)return 0;
for(int i=x;i<=y;++i)if(p[i]&&(p[i]<x||p[i]>y))return 0;
return 1;
}
int main()
{
scanf("%d %d",&n,&k);
int x,y;
for(int i=1;i<=k;++i)
{
scanf("%d %d",&x,&y);
p[x]=y,p[y]=x;
}
for(int i=1;i<=2*n;++i)cnt[i]=cnt[i-1]+(!p[i]);
f[0]=1;
for(int i=2;i<=2*n;++i)f[i]=1ll*f[i-2]*(i-1)%mod;
for(int len=1;len<=2*n;++len)
for(int i=1,j=i+len-1;j<=2*n;++i,++j)
if(check(i,j))
{
dp[i][j]=f[cnt[j]-cnt[i-1]];
for(int k=i;k<j;++k)dp[i][j]=(dp[i][j]+1ll*(mod-dp[i][k])*f[cnt[j]-cnt[k]])%mod;
// printf("%d %d %d\n",i,j,dp[i][j]);
ans=(ans+1ll*dp[i][j]*f[cnt[2*n]-cnt[j]+cnt[i-1]])%mod;
}
printf("%d",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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int n,k,cnt[610],p[610],f[610],dp[610][610],ans;
bool check(int x,int y)
{
if((cnt[y]-cnt[x-1])&1)return 0;
for(int i=x;i<=y;++i)if(p[i]&&(p[i]<x||p[i]>y))return 0;
return 1;
}
int main()
{
scanf("%d %d",&n,&k);
int x,y;
for(int i=1;i<=k;++i)
{
scanf("%d %d",&x,&y);
p[x]=y,p[y]=x;
}
for(int i=1;i<=2*n;++i)cnt[i]=cnt[i-1]+(!p[i]);
f[0]=1;
for(int i=2;i<=2*n;++i)f[i]=1ll*f[i-2]*(i-1)%mod;
for(int len=1;len<=2*n;++len)
for(int i=1,j=i+len-1;j<=2*n;++i,++j)
if(check(i,j))
{
dp[i][j]=f[cnt[j]-cnt[i-1]];
for(int k=i;k<j;++k)dp[i][j]=(dp[i][j]+1ll*(mod-dp[i][k])*f[cnt[j]-cnt[k]])%mod;
// printf("%d %d %d\n",i,j,dp[i][j]);
ans=(ans+1ll*dp[i][j]*f[cnt[2*n]-cnt[j]+cnt[i-1]])%mod;
}
printf("%d",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
#define ll long long
int n, k, u[666], v[666], f[666][666], g[666], usd[666], cc[666][666];
int fix(int x){
return x==-1? 665: x;
}
int main(){
cin>>n>>k;
for (int i=1;i<=k;++i){
cin>>u[i]>>v[i];
usd[u[i]]=v[i]; usd[v[i]]=u[i];
}
g[0]=1;
for (int i=2;i<=n*2;i+=2)
g[i]=(ll)g[i-2]*(i-1)%mod;
int ans=0, all=n*2-k*2;
for (int len=1;len<n*2;++len)
for (int i=1;i+len<=n*2;++i){
int j=i+len;
int fl=1;
for (int k=i;k<=j;++k){
if (usd[k]&&(usd[k]<i||usd[k]>j)) fl=0;
cc[i][j]+=!usd[k];
}
if (cc[i][j]&1) fl=0;
if (!fl){
cc[i][j]=-1;
continue;
}
f[i][j]=g[cc[i][j]];
ll dec=0;
for (int k=i+1;k<j;++k){
dec=(dec+f[i][k]*(ll)g[fix(cc[k+1][j])])%mod;
}
f[i][j]=(f[i][j]-dec+mod)%mod;
ans=(ans+(ll)f[i][j]*g[all-cc[i][j]])%mod;
}
cout<<ans<<endl;
} | ### 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 mod=1e9+7;
#define ll long long
int n, k, u[666], v[666], f[666][666], g[666], usd[666], cc[666][666];
int fix(int x){
return x==-1? 665: x;
}
int main(){
cin>>n>>k;
for (int i=1;i<=k;++i){
cin>>u[i]>>v[i];
usd[u[i]]=v[i]; usd[v[i]]=u[i];
}
g[0]=1;
for (int i=2;i<=n*2;i+=2)
g[i]=(ll)g[i-2]*(i-1)%mod;
int ans=0, all=n*2-k*2;
for (int len=1;len<n*2;++len)
for (int i=1;i+len<=n*2;++i){
int j=i+len;
int fl=1;
for (int k=i;k<=j;++k){
if (usd[k]&&(usd[k]<i||usd[k]>j)) fl=0;
cc[i][j]+=!usd[k];
}
if (cc[i][j]&1) fl=0;
if (!fl){
cc[i][j]=-1;
continue;
}
f[i][j]=g[cc[i][j]];
ll dec=0;
for (int k=i+1;k<j;++k){
dec=(dec+f[i][k]*(ll)g[fix(cc[k+1][j])])%mod;
}
f[i][j]=(f[i][j]-dec+mod)%mod;
ans=(ans+(ll)f[i][j]*g[all-cc[i][j]])%mod;
}
cout<<ans<<endl;
}
``` |
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 666666
const int MOD=1e9+7;
int n,o,g[SZ],f[605][605],e[SZ];
ll w[SZ];
int main()
{
memset(g,-1,sizeof g);
scanf("%d%d",&n,&o); n*=2;
for(int i=1,a,b;i<=o;++i)
scanf("%d%d",&a,&b),g[a]=b,g[b]=a;
w[0]=1;
for(int i=2;i<=n;i+=2)
w[i]=w[i-2]*(i-1)%MOD;
for(int i=1;i<=n;++i)
e[i]=e[i-1]+(g[i]==-1);
ll ans=0;
for(int t=1;t<=n-1;t+=2)
{
for(int l=1;l+t<=n;++l)
{
int r=l+t,ok=1;
for(int s=l;s<=r&&ok;++s)
ok&=g[s]==-1||(g[s]>=l&&g[s]<=r);
if(!ok) continue;
int u=e[r]-e[l-1]; f[l][r]=w[u];
for(int k=l+1;k<r;k+=2) if(f[l][k])
f[l][r]=(f[l][r]-f[l][k]*w[e[r]-e[k]])%MOD;
ans=(ans+f[l][r]*w[e[n]-u])%MOD;
}
}
ans=(ans%MOD+MOD)%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 <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 666666
const int MOD=1e9+7;
int n,o,g[SZ],f[605][605],e[SZ];
ll w[SZ];
int main()
{
memset(g,-1,sizeof g);
scanf("%d%d",&n,&o); n*=2;
for(int i=1,a,b;i<=o;++i)
scanf("%d%d",&a,&b),g[a]=b,g[b]=a;
w[0]=1;
for(int i=2;i<=n;i+=2)
w[i]=w[i-2]*(i-1)%MOD;
for(int i=1;i<=n;++i)
e[i]=e[i-1]+(g[i]==-1);
ll ans=0;
for(int t=1;t<=n-1;t+=2)
{
for(int l=1;l+t<=n;++l)
{
int r=l+t,ok=1;
for(int s=l;s<=r&&ok;++s)
ok&=g[s]==-1||(g[s]>=l&&g[s]<=r);
if(!ok) continue;
int u=e[r]-e[l-1]; f[l][r]=w[u];
for(int k=l+1;k<r;k+=2) if(f[l][k])
f[l][r]=(f[l][r]-f[l][k]*w[e[r]-e[k]])%MOD;
ans=(ans+f[l][r]*w[e[n]-u])%MOD;
}
}
ans=(ans%MOD+MOD)%MOD;
cout<<ans<<"\n";
}
``` |
#include<cstdio>
#define mod 1000000007
#define N 611
int dp[N][N],A[N],B[N],G[N],cnt[N][N],pd[N];
int main()
{
int n,k,nn,all,ans=0;
scanf("%d%d",&n,&k);nn=n<<1;all=(n-k)<<1;
for (int i=1;i<=k;++i) scanf("%d%d",&A[i],&B[i]),pd[A[i]]=B[i],pd[B[i]]=A[i];
G[0]=1;for (int i=2;i<=nn;i+=2) G[i]=1ll*G[i-2]*(i-1)%mod;
// printf("%d\n",pd[1]);
for (int i=1;i<=nn;++i)
for (int j=i;j<=nn;++j)
cnt[i][j]=cnt[i][j-1]+(!pd[j]);
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2)
{
for (int k=i;k<=j;++k) if (pd[k]&&(pd[k]>j||pd[k]<i)) goto C;
dp[i][j]=G[cnt[i][j]];
for (int k=i+1;k<j;k+=2)
dp[i][j]=(dp[i][j]-1ll*dp[i][k]*G[cnt[k+1][j]]%mod+mod)%mod;
C:;
}
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2) ans=(ans+1ll*dp[i][j]*G[all-cnt[i][j]]%mod)%mod;
printf("%d",ans);//,printf("dp[%d][%d]=%d\n",i,j,dp[i][j])
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>
#define mod 1000000007
#define N 611
int dp[N][N],A[N],B[N],G[N],cnt[N][N],pd[N];
int main()
{
int n,k,nn,all,ans=0;
scanf("%d%d",&n,&k);nn=n<<1;all=(n-k)<<1;
for (int i=1;i<=k;++i) scanf("%d%d",&A[i],&B[i]),pd[A[i]]=B[i],pd[B[i]]=A[i];
G[0]=1;for (int i=2;i<=nn;i+=2) G[i]=1ll*G[i-2]*(i-1)%mod;
// printf("%d\n",pd[1]);
for (int i=1;i<=nn;++i)
for (int j=i;j<=nn;++j)
cnt[i][j]=cnt[i][j-1]+(!pd[j]);
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2)
{
for (int k=i;k<=j;++k) if (pd[k]&&(pd[k]>j||pd[k]<i)) goto C;
dp[i][j]=G[cnt[i][j]];
for (int k=i+1;k<j;k+=2)
dp[i][j]=(dp[i][j]-1ll*dp[i][k]*G[cnt[k+1][j]]%mod+mod)%mod;
C:;
}
for (int i=1;i<=nn;++i)
for (int j=i+1;j<=nn;j+=2) ans=(ans+1ll*dp[i][j]*G[all-cnt[i][j]]%mod)%mod;
printf("%d",ans);//,printf("dp[%d][%d]=%d\n",i,j,dp[i][j])
return 0;
}
``` |
#include <iostream>
#include <cstdio>
#define MN 610
#define mod 1000000007
int s[MN], to[MN], f[MN][MN], n;
bool ok[MN][MN];
int fac[MN], inv[MN], mi[MN];
int C(int a, int b) {return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod;}
int calc(int n) {if(n & 1) return 0; n >>= 1; return 1ll * C(2 * n, n) * fac[n] % mod * mi[n] % mod;}
int main()
{
int n, k; scanf("%d%d", &n, &k); n *= 2;
fac[0] = inv[0] = inv[1] = mi[0] = 1;
for(int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
for(int i = 2; i <= n; i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
for(int i = 1; i <= n; i++) inv[i] = 1ll * inv[i - 1] * inv[i] % mod;
for(int i = 1; i <= n; i++) mi[i] = 1ll * mi[i - 1] * (mod + 1 >> 1) % mod;
for(int i = 1; i <= k; i++)
{
int a, b; scanf("%d%d", &a, &b);
to[a] = b; to[b] = a;
}
for(int i = 1; i <= n; i++)
{
if(!to[i]) s[i] = 1;
s[i] += s[i - 1];
}
for(int l = 1; l <= n; l++)
{
int Max = 0;
for(int r = l; r <= n; r++)
{
if(to[r] && to[r] < l) break;
Max = std::max(Max, to[r]);
if(Max <= r) ok[l][r] = 1;
}
}
int ans = 0;
for(int len = 2; len <= n; len++)
for(int l = 1; l + len - 1 <= n; l++)
{
int r = l + len - 1;
if(!ok[l][r]) continue;
f[l][r] = calc(s[r] - s[l - 1]);
if(!f[l][r]) continue;
for(int i = l; i < r; i++)
f[l][r] = (f[l][r] - 1ll * f[l][i] * (ok[i + 1][r] ? calc(s[r] - s[i]) : 0) % mod + mod) % mod;
ans = (ans + 1ll * f[l][r] * calc(s[n] - s[r] + s[l - 1]) % mod) % mod;
}
printf("%d\n", ans);
} | ### 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>
#define MN 610
#define mod 1000000007
int s[MN], to[MN], f[MN][MN], n;
bool ok[MN][MN];
int fac[MN], inv[MN], mi[MN];
int C(int a, int b) {return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod;}
int calc(int n) {if(n & 1) return 0; n >>= 1; return 1ll * C(2 * n, n) * fac[n] % mod * mi[n] % mod;}
int main()
{
int n, k; scanf("%d%d", &n, &k); n *= 2;
fac[0] = inv[0] = inv[1] = mi[0] = 1;
for(int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
for(int i = 2; i <= n; i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
for(int i = 1; i <= n; i++) inv[i] = 1ll * inv[i - 1] * inv[i] % mod;
for(int i = 1; i <= n; i++) mi[i] = 1ll * mi[i - 1] * (mod + 1 >> 1) % mod;
for(int i = 1; i <= k; i++)
{
int a, b; scanf("%d%d", &a, &b);
to[a] = b; to[b] = a;
}
for(int i = 1; i <= n; i++)
{
if(!to[i]) s[i] = 1;
s[i] += s[i - 1];
}
for(int l = 1; l <= n; l++)
{
int Max = 0;
for(int r = l; r <= n; r++)
{
if(to[r] && to[r] < l) break;
Max = std::max(Max, to[r]);
if(Max <= r) ok[l][r] = 1;
}
}
int ans = 0;
for(int len = 2; len <= n; len++)
for(int l = 1; l + len - 1 <= n; l++)
{
int r = l + len - 1;
if(!ok[l][r]) continue;
f[l][r] = calc(s[r] - s[l - 1]);
if(!f[l][r]) continue;
for(int i = l; i < r; i++)
f[l][r] = (f[l][r] - 1ll * f[l][i] * (ok[i + 1][r] ? calc(s[r] - s[i]) : 0) % mod + mod) % mod;
ans = (ans + 1ll * f[l][r] * calc(s[n] - s[r] + s[l - 1]) % mod) % mod;
}
printf("%d\n", ans);
}
``` |
#include <bits/stdc++.h>
using namespace std;
int N,K;
const int fish=1e9+7;
int a[10005],b[10005],pd[5005];
long long g[5005],dp[5005][5005];
int cnt[5005][5005];
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]=1ll*g[i-2]*(i-1)%fish;
for (int i=1;i<=N;i++){
for (int j=i;j<=N;j++)
cnt[i][j]=(cnt[i][j-1]+(!pd[j]))%fish;
}
for (int i=1;i<=N;i++)
for (int j=i;j<=N;j++){
bool pdd=false;
for (int k=i;k<=j;k++){
if (pd[k]&&(pd[k]<i||pd[k]>j)){
pdd=true;
break;
}
}
if (pdd) 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]]%fish)+fish)%fish;
}
long long ans=0;
for (int i=1;i<=N;i++)
for (int j=i;j<=N;j++)
ans=(ans+1ll*dp[i][j]*1ll*(g[(N/2-K)*2-cnt[i][j]])%fish)%fish;
cout<<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;
int N,K;
const int fish=1e9+7;
int a[10005],b[10005],pd[5005];
long long g[5005],dp[5005][5005];
int cnt[5005][5005];
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]=1ll*g[i-2]*(i-1)%fish;
for (int i=1;i<=N;i++){
for (int j=i;j<=N;j++)
cnt[i][j]=(cnt[i][j-1]+(!pd[j]))%fish;
}
for (int i=1;i<=N;i++)
for (int j=i;j<=N;j++){
bool pdd=false;
for (int k=i;k<=j;k++){
if (pd[k]&&(pd[k]<i||pd[k]>j)){
pdd=true;
break;
}
}
if (pdd) 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]]%fish)+fish)%fish;
}
long long ans=0;
for (int i=1;i<=N;i++)
for (int j=i;j<=N;j++)
ans=(ans+1ll*dp[i][j]*1ll*(g[(N/2-K)*2-cnt[i][j]])%fish)%fish;
cout<<ans;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=605;
const int mod=1e9+7;
int n,k,ans;
int f[maxn][maxn],g[maxn][maxn],h[maxn],mat[maxn];
int main(){
scanf("%d%d",&n,&k);
REP(i,k){
int a,b;
scanf("%d%d",&a,&b);
mat[a]=b;
mat[b]=a;
}
h[0]=1;
REP(i,2*n)h[i]=1LL*h[i-2]*(i-1)%mod;
REP(len,2*n)REP(i,2*n){
int j=i+len-1;
if(j>2*n)break;
bool ok=true;
int cnt=0;
for(int k=i;k<=j;k++){
if(mat[k]){
if(mat[k]<i||mat[k]>j)ok=false;
cnt++;
}
}
if(!ok)continue;
f[i][j]=g[i][j]=h[len-cnt];
for(int k=i+1;k<j;k++){
f[i][j]-=1LL*f[i][k]*g[k+1][j]%mod;
if(f[i][j]<0)f[i][j]+=mod;
}
ans+=1LL*f[i][j]*h[2*n-(j-i+1)-(2*k-cnt)]%mod;
if(ans>=mod)ans-=mod;
}
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;
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=605;
const int mod=1e9+7;
int n,k,ans;
int f[maxn][maxn],g[maxn][maxn],h[maxn],mat[maxn];
int main(){
scanf("%d%d",&n,&k);
REP(i,k){
int a,b;
scanf("%d%d",&a,&b);
mat[a]=b;
mat[b]=a;
}
h[0]=1;
REP(i,2*n)h[i]=1LL*h[i-2]*(i-1)%mod;
REP(len,2*n)REP(i,2*n){
int j=i+len-1;
if(j>2*n)break;
bool ok=true;
int cnt=0;
for(int k=i;k<=j;k++){
if(mat[k]){
if(mat[k]<i||mat[k]>j)ok=false;
cnt++;
}
}
if(!ok)continue;
f[i][j]=g[i][j]=h[len-cnt];
for(int k=i+1;k<j;k++){
f[i][j]-=1LL*f[i][k]*g[k+1][j]%mod;
if(f[i][j]<0)f[i][j]+=mod;
}
ans+=1LL*f[i][j]*h[2*n-(j-i+1)-(2*k-cnt)]%mod;
if(ans>=mod)ans-=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 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;
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<bits/stdc++.h>
using namespace std;
const int N=605,mod=1e9+7;
int n,m,dp[N][N],ss[N],f[N],to[N],ans;
inline int mul(int x,int y){return 1ll*x*y%mod;}
inline int dc(int x,int y){x-=y;return x<0?x+mod:x;}
inline int ad(int x,int y){x+=y;return x>=mod?x-mod:x;}
int main(){
int i,j,k,t,x,y,res;
scanf("%d%d",&n,&m);
n<<=1;f[0]=1;
for(i=2;i<=n;i+=2) f[i]=mul(f[i-2],i-1);
for(;m;--m){
scanf("%d%d",&x,&y);
to[x]=y;to[y]=x;
ss[x]=ss[y]=1;
}
for(i=1;i<=n;++i) ss[i]=ss[i-1]+(ss[i]^1);
for(i=1;i<n;++i){
for(j=i+1;j<=n;j+=2){
res=ss[j]-ss[i-1];
if(res&1) continue;
for(t=0,k=i;k<=j;++k)
if(to[k] && (to[k]<i || to[k]>j)) {t=1;break;}
if(t) continue;
res=f[res];x=ss[j];
for(k=i+1;k<j;k+=2){
t=x-ss[k];if(t&1) continue;
res=dc(res,mul(dp[i][k],f[t]));
}
dp[i][j]=res;ans=ad(ans,mul(res,f[ss[i-1]+ss[n]-ss[j]]));
}
}
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 N=605,mod=1e9+7;
int n,m,dp[N][N],ss[N],f[N],to[N],ans;
inline int mul(int x,int y){return 1ll*x*y%mod;}
inline int dc(int x,int y){x-=y;return x<0?x+mod:x;}
inline int ad(int x,int y){x+=y;return x>=mod?x-mod:x;}
int main(){
int i,j,k,t,x,y,res;
scanf("%d%d",&n,&m);
n<<=1;f[0]=1;
for(i=2;i<=n;i+=2) f[i]=mul(f[i-2],i-1);
for(;m;--m){
scanf("%d%d",&x,&y);
to[x]=y;to[y]=x;
ss[x]=ss[y]=1;
}
for(i=1;i<=n;++i) ss[i]=ss[i-1]+(ss[i]^1);
for(i=1;i<n;++i){
for(j=i+1;j<=n;j+=2){
res=ss[j]-ss[i-1];
if(res&1) continue;
for(t=0,k=i;k<=j;++k)
if(to[k] && (to[k]<i || to[k]>j)) {t=1;break;}
if(t) continue;
res=f[res];x=ss[j];
for(k=i+1;k<j;k+=2){
t=x-ss[k];if(t&1) continue;
res=dc(res,mul(dp[i][k],f[t]));
}
dp[i][j]=res;ans=ad(ans,mul(res,f[ss[i-1]+ss[n]-ss[j]]));
}
}
printf("%d\n",ans);
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);
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;
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);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define x first
#define y second
#define pb push_back
#define mp make_pair
template <typename T> void chkmin(T &x,T y){y<x?x=y:T();}
template <typename T> void chkmax(T &x,T y){x<y?x=y:T();}
template <typename T> void readint(T &x)
{
int f=1;char c;x=0;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=x*10+(c-'0');
x*=f;
}
const int MOD=1000000007;
inline int dmy(int x){return x>=MOD?x-MOD:x;}
inline void inc(int &x,int y){x=dmy(x+y);}
int qmi(int x,int y)
{
int ans=1;
for(;y;y>>=1,x=1ll*x*x%MOD)
if(y&1)ans=1ll*ans*x%MOD;
return ans;
}
const int MAXN=605;
int n,m,fac[MAXN],u[MAXN],v[MAXN],f[MAXN][MAXN],g[MAXN][MAXN],cnt[MAXN][MAXN];
bool inside(int x,int l,int r){return x>=l && x<=r;}
int main()
{
#ifdef LOCAL
freopen("code.in","r",stdin);
// freopen("code.out","w",stdout);
#endif
readint(n),readint(m);
fac[0]=1;
for(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*(2*i-1)%MOD;
n<<=1;
for(int i=1;i<=m;++i)readint(u[i]),readint(v[i]);
for(int l=1;l<=n;++l)
for(int r=l+1;r<=n;r+=2)
{
bool flag=1;
cnt[l][r]=0;
for(int i=1;i<=m;++i)
{
if(inside(u[i],l,r)!=inside(v[i],l,r)){flag=0;break;}
if(inside(u[i],l,r))++cnt[l][r];
}
if(!flag)continue;
f[l][r]=fac[(r-l+1)/2-cnt[l][r]];
}
int ans=0;
for(int l=n;l;--l)
for(int r=l+1;r<=n;r+=2)
{
g[l][r]=f[l][r];
for(int i=l+1;i<r;i+=2)
inc(g[l][r],MOD-1ll*f[l][i]*g[i+1][r]%MOD);
inc(ans,1ll*g[l][r]*fac[(n-(r-l+1))/2-(m-cnt[l][r])]%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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define x first
#define y second
#define pb push_back
#define mp make_pair
template <typename T> void chkmin(T &x,T y){y<x?x=y:T();}
template <typename T> void chkmax(T &x,T y){x<y?x=y:T();}
template <typename T> void readint(T &x)
{
int f=1;char c;x=0;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=x*10+(c-'0');
x*=f;
}
const int MOD=1000000007;
inline int dmy(int x){return x>=MOD?x-MOD:x;}
inline void inc(int &x,int y){x=dmy(x+y);}
int qmi(int x,int y)
{
int ans=1;
for(;y;y>>=1,x=1ll*x*x%MOD)
if(y&1)ans=1ll*ans*x%MOD;
return ans;
}
const int MAXN=605;
int n,m,fac[MAXN],u[MAXN],v[MAXN],f[MAXN][MAXN],g[MAXN][MAXN],cnt[MAXN][MAXN];
bool inside(int x,int l,int r){return x>=l && x<=r;}
int main()
{
#ifdef LOCAL
freopen("code.in","r",stdin);
// freopen("code.out","w",stdout);
#endif
readint(n),readint(m);
fac[0]=1;
for(int i=1;i<=n;++i)fac[i]=1ll*fac[i-1]*(2*i-1)%MOD;
n<<=1;
for(int i=1;i<=m;++i)readint(u[i]),readint(v[i]);
for(int l=1;l<=n;++l)
for(int r=l+1;r<=n;r+=2)
{
bool flag=1;
cnt[l][r]=0;
for(int i=1;i<=m;++i)
{
if(inside(u[i],l,r)!=inside(v[i],l,r)){flag=0;break;}
if(inside(u[i],l,r))++cnt[l][r];
}
if(!flag)continue;
f[l][r]=fac[(r-l+1)/2-cnt[l][r]];
}
int ans=0;
for(int l=n;l;--l)
for(int r=l+1;r<=n;r+=2)
{
g[l][r]=f[l][r];
for(int i=l+1;i<r;i+=2)
inc(g[l][r],MOD-1ll*f[l][i]*g[i+1][r]%MOD);
inc(ans,1ll*g[l][r]*fac[(n-(r-l+1))/2-(m-cnt[l][r])]%MOD);
}
printf("%d\n",ans);
return 0;
}
``` |
#include <cstdio>
constexpr int MOD = 1e9 + 7;
int x[605], y[605], pre[605];
long long dp[605][605], fact[605], ans;
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)
{
auto in = [&] (int t) { return l <= t && t < r; };
int cnt = pre[r - 1] - (l ? pre[l - 1] : 0);
bool flg = true;
for (int i = 0; i < k && flg; i++)
flg &= in(x[i]) == in(y[i]);
if (!flg || cnt & 1)
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
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>
constexpr int MOD = 1e9 + 7;
int x[605], y[605], pre[605];
long long dp[605][605], fact[605], ans;
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)
{
auto in = [&] (int t) { return l <= t && t < r; };
int cnt = pre[r - 1] - (l ? pre[l - 1] : 0);
bool flg = true;
for (int i = 0; i < k && flg; i++)
flg &= in(x[i]) == in(y[i]);
if (!flg || cnt & 1)
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 int mod=1000000007;
const int N=700;
int i,j,k,n,m,x,y,t,ans,dp[N][N],cant[N][N],w[N][N],R[N][N],Y[N][N],g[N];
int main(){
scanf("%d%d",&n,&m);
for (k=1;k<=n*2;k+=2)for (i=1;i<=n*2;i++)if (i+k-1<=n*2)cant[i][i+k-1]=1;
for (k=1;k<=m;k++){
scanf("%d%d",&x,&y);
if (x>y)swap(x,y);
for (i=1;i<=x;i++)for (j=x;j<y;j++)cant[i][j]=1;
for (i=x+1;i<=y;i++)for (j=y;j<=n*2;j++)cant[i][j]=1;
for (i=1;i<=x;i++)for (j=y;j<=n*2;j++)w[i][j]++;
}
g[0]=1;for (i=2;i<=n*2;i+=2)g[i]=1ll*(i-1)*g[i-2]%mod;
for (i=1;i<=n*2;i++)
for (j=i+1;j<=n*2;j++)
if (!cant[i][j])
R[i][j]=j-i+1-2*w[i][j],
Y[i][j]=n*2-2*(m-w[i][j])-(j-i+1);
for (t=2;t<=n*2;t+=2)
for (i=1;i<=n*2-t+1;i++){
j=i+t-1;
if (!cant[i][j]){
dp[i][j]=1ll*g[R[i][j]]%mod;
for (k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-1ll*dp[i][k]*g[R[k+1][j]]%mod+mod)%mod;
ans=(ans+1ll*dp[i][j]*g[Y[i][j]]%mod)%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 mod=1000000007;
const int N=700;
int i,j,k,n,m,x,y,t,ans,dp[N][N],cant[N][N],w[N][N],R[N][N],Y[N][N],g[N];
int main(){
scanf("%d%d",&n,&m);
for (k=1;k<=n*2;k+=2)for (i=1;i<=n*2;i++)if (i+k-1<=n*2)cant[i][i+k-1]=1;
for (k=1;k<=m;k++){
scanf("%d%d",&x,&y);
if (x>y)swap(x,y);
for (i=1;i<=x;i++)for (j=x;j<y;j++)cant[i][j]=1;
for (i=x+1;i<=y;i++)for (j=y;j<=n*2;j++)cant[i][j]=1;
for (i=1;i<=x;i++)for (j=y;j<=n*2;j++)w[i][j]++;
}
g[0]=1;for (i=2;i<=n*2;i+=2)g[i]=1ll*(i-1)*g[i-2]%mod;
for (i=1;i<=n*2;i++)
for (j=i+1;j<=n*2;j++)
if (!cant[i][j])
R[i][j]=j-i+1-2*w[i][j],
Y[i][j]=n*2-2*(m-w[i][j])-(j-i+1);
for (t=2;t<=n*2;t+=2)
for (i=1;i<=n*2-t+1;i++){
j=i+t-1;
if (!cant[i][j]){
dp[i][j]=1ll*g[R[i][j]]%mod;
for (k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-1ll*dp[i][k]*g[R[k+1][j]]%mod+mod)%mod;
ans=(ans+1ll*dp[i][j]*g[Y[i][j]]%mod)%mod;
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const ll MOD=1e9+7;
int n,k,match[605];
ll f[605][605],c[605];
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;
}
c[0]=1;
for(int i=1;i<=n;i++)c[i]=c[i-1]*(2*i-1)%MOD;
for(int l=1;l<=2*n;l++)
{
int minn=2*n,maxx=0,num=0;
for(int r=l;r<=2*n;r++)
{
if(match[r])minn=min(minn,match[r]),maxx=max(maxx,match[r]),num--;
num++;
if(minn>=l&&maxx<=r)f[l][r]=c[num>>1];
}
}
ll ans=0;
for(int i=1;i<=2*n;i++)
{
int num=0;
for(int j=i+1;j<=2*n;j+=2)
{
if(match[j-1])num++;
if(match[j])num++;
for(int k=i+1;k<j;k+=2)
f[i][j]=(f[i][j]-f[i][k]*f[k+1][j])%MOD;
ans=(ans+f[i][j]*c[n-(j-i+1)/2-k+num/2])%MOD;
}
}
if(ans<0)ans+=MOD;
printf("%lld\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<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const ll MOD=1e9+7;
int n,k,match[605];
ll f[605][605],c[605];
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;
}
c[0]=1;
for(int i=1;i<=n;i++)c[i]=c[i-1]*(2*i-1)%MOD;
for(int l=1;l<=2*n;l++)
{
int minn=2*n,maxx=0,num=0;
for(int r=l;r<=2*n;r++)
{
if(match[r])minn=min(minn,match[r]),maxx=max(maxx,match[r]),num--;
num++;
if(minn>=l&&maxx<=r)f[l][r]=c[num>>1];
}
}
ll ans=0;
for(int i=1;i<=2*n;i++)
{
int num=0;
for(int j=i+1;j<=2*n;j+=2)
{
if(match[j-1])num++;
if(match[j])num++;
for(int k=i+1;k<j;k+=2)
f[i][j]=(f[i][j]-f[i][k]*f[k+1][j])%MOD;
ans=(ans+f[i][j]*c[n-(j-i+1)/2-k+num/2])%MOD;
}
}
if(ans<0)ans+=MOD;
printf("%lld\n",ans);
return 0;
}
``` |
#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int Mod = 1e9 + 7;
cs int N = 605;
int add(int a, int b){ return a + b >= Mod ? a + b - Mod : a + b; }
int dec(int a, int b){ return a - b < 0 ? a - b + Mod : a - b; }
int mul(int a, int b){ return 1ll * a * b % Mod; }
void Add(int &a, int b){ a = add(a,b); }
void Mul(int &a, int b){ a = mul(a,b); }
void Dec(int &a, int b){ a = dec(a,b); }
int k, n, tr[N];
int dp[N][N], way[N];
int cope(int l, int r, int ct){
int as = way[ct];
for(int i=r,t=0; i>l+1; i--)
t+=!tr[i], Dec(as,mul(dp[l][i-1],way[t]));
return as;
} int chk(int l, int r){
int ct=0;
for(int i=l; i<=r; i++)
if(tr[i]&&(tr[i]<l||tr[i]>r)) return -1;
for(int i=l; i<=r; i++) ct+=!tr[i]; return ct;
}
int main(){
#ifdef FSYolanda
freopen("1.in","r",stdin);
#endif
scanf("%d%d",&n,&k); n<<=1;
for(int i=1,x,y; i<=k; i++)
scanf("%d%d",&x,&y), tr[x]=y, tr[y]=x;
int as=0; way[0]=1;
for(int i=2; i<=n; i+=2) way[i]=mul(way[i-2],i-1);
for(int l=n; l>=1; l--)
for(int r=l+1; r<=n; r+=2){
int ct=chk(l,r); if(ct==-1) continue;
dp[l][r]=cope(l,r,ct);
Add(as,mul(way[n-k-k-ct],dp[l][r]));
}
cout<<as; 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 cs const
#define pb push_back
using namespace std;
cs int Mod = 1e9 + 7;
cs int N = 605;
int add(int a, int b){ return a + b >= Mod ? a + b - Mod : a + b; }
int dec(int a, int b){ return a - b < 0 ? a - b + Mod : a - b; }
int mul(int a, int b){ return 1ll * a * b % Mod; }
void Add(int &a, int b){ a = add(a,b); }
void Mul(int &a, int b){ a = mul(a,b); }
void Dec(int &a, int b){ a = dec(a,b); }
int k, n, tr[N];
int dp[N][N], way[N];
int cope(int l, int r, int ct){
int as = way[ct];
for(int i=r,t=0; i>l+1; i--)
t+=!tr[i], Dec(as,mul(dp[l][i-1],way[t]));
return as;
} int chk(int l, int r){
int ct=0;
for(int i=l; i<=r; i++)
if(tr[i]&&(tr[i]<l||tr[i]>r)) return -1;
for(int i=l; i<=r; i++) ct+=!tr[i]; return ct;
}
int main(){
#ifdef FSYolanda
freopen("1.in","r",stdin);
#endif
scanf("%d%d",&n,&k); n<<=1;
for(int i=1,x,y; i<=k; i++)
scanf("%d%d",&x,&y), tr[x]=y, tr[y]=x;
int as=0; way[0]=1;
for(int i=2; i<=n; i+=2) way[i]=mul(way[i-2],i-1);
for(int l=n; l>=1; l--)
for(int r=l+1; r<=n; r+=2){
int ct=chk(l,r); if(ct==-1) continue;
dp[l][r]=cope(l,r,ct);
Add(as,mul(way[n-k-k-ct],dp[l][r]));
}
cout<<as; 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<vector<int>> dp(2 * n, vector<int>(2 * n));
for (int i = 0; i < 2 * n; ++i) {
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 y = 2 * (n - k) - s[j + 1] + s[i];
if (y % 2 == 1)
continue;
dp[i][j] = g[s[j + 1] - s[i]];
for (int k = i; k < j; ++k)
decrease(dp[i][j], 1LL * dp[i][k] * g[s[j + 1] - s[k + 1]] % P);
increase(ans, 1LL * dp[i][j] * g[y] % P);
}
}
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;
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<vector<int>> dp(2 * n, vector<int>(2 * n));
for (int i = 0; i < 2 * n; ++i) {
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 y = 2 * (n - k) - s[j + 1] + s[i];
if (y % 2 == 1)
continue;
dp[i][j] = g[s[j + 1] - s[i]];
for (int k = i; k < j; ++k)
decrease(dp[i][j], 1LL * dp[i][k] * g[s[j + 1] - s[k + 1]] % P);
increase(ans, 1LL * dp[i][j] * g[y] % P);
}
}
cout << ans << endl;
return 0;
}
``` |
#include<stdio.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int mod = 1e9+7;
int ad(int x,int y) { x+=y; return x>=mod?x-mod:x; }
int sb(int x,int y) { x-=y; return x<0?x+mod:x; }
int mu(int x,int y) { return 1ll*x*y%mod; }
int ksm(int a,int b) {
int ans = 1;
for(;b;b>>=1,a=mu(a,a))
if(b&1) ans = mu(ans,a);
return ans;
}
int n,k;
int mk[605];
int cnt[605][605];
int dp[605][605];
int g[605];
int main() {
scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++) {
int x,y; scanf("%d%d",&x,&y);
mk[x] = y;
mk[y] = x;
}
g[0] = 1;
for(int i=2;i<=2*n;i+=2) {
g[i] = 1;
for(int j=1;j<i;j+=2) {
g[i] = mu(g[i],j);
}
}
for(int i=1;i<=2*n;i++) {
for(int j=i;j<=2*n;j++) {
cnt[i][j] = cnt[i][j-1] + (!mk[j]);
}
}
for(int i=1;i<=2*n;i++) {
for(int j=i;j<=2*n;j++) {
bool fk = 0;
for(int k=i;k<=j;k++) {
if(mk[k]&&(mk[k]>j||mk[k]<i) ) {
fk = 1;
break;
}
}
if(fk) continue;
dp[i][j] = g[cnt[i][j]];
for(int k=i+1;k<=j-1;k++) {
dp[i][j] = sb(dp[i][j],mu(dp[i][k],g[cnt[k+1][j]]));
}
}
}
int ans = 0;
for(int i=1;i<=2*n;i++) {
for(int j=i;j<=2*n;j++) {
ans = ad(ans,mu(dp[i][j],g[ 2*n - 2*k - cnt[i][j] ]));
}
}
printf("%d",ans);
} | ### 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<stdio.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int mod = 1e9+7;
int ad(int x,int y) { x+=y; return x>=mod?x-mod:x; }
int sb(int x,int y) { x-=y; return x<0?x+mod:x; }
int mu(int x,int y) { return 1ll*x*y%mod; }
int ksm(int a,int b) {
int ans = 1;
for(;b;b>>=1,a=mu(a,a))
if(b&1) ans = mu(ans,a);
return ans;
}
int n,k;
int mk[605];
int cnt[605][605];
int dp[605][605];
int g[605];
int main() {
scanf("%d%d",&n,&k);
for(int i=1;i<=k;i++) {
int x,y; scanf("%d%d",&x,&y);
mk[x] = y;
mk[y] = x;
}
g[0] = 1;
for(int i=2;i<=2*n;i+=2) {
g[i] = 1;
for(int j=1;j<i;j+=2) {
g[i] = mu(g[i],j);
}
}
for(int i=1;i<=2*n;i++) {
for(int j=i;j<=2*n;j++) {
cnt[i][j] = cnt[i][j-1] + (!mk[j]);
}
}
for(int i=1;i<=2*n;i++) {
for(int j=i;j<=2*n;j++) {
bool fk = 0;
for(int k=i;k<=j;k++) {
if(mk[k]&&(mk[k]>j||mk[k]<i) ) {
fk = 1;
break;
}
}
if(fk) continue;
dp[i][j] = g[cnt[i][j]];
for(int k=i+1;k<=j-1;k++) {
dp[i][j] = sb(dp[i][j],mu(dp[i][k],g[cnt[k+1][j]]));
}
}
}
int ans = 0;
for(int i=1;i<=2*n;i++) {
for(int j=i;j<=2*n;j++) {
ans = ad(ans,mu(dp[i][j],g[ 2*n - 2*k - cnt[i][j] ]));
}
}
printf("%d",ans);
}
``` |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#define LL long long
#define LD long double
using namespace std;
const int NN=2*300 +117;
const int MM= +117;
int read(){
int fl=1,x;char c;
for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
if(c=='-'){fl=-1;c=getchar();}
for(x=0;c>='0'&&c<='9';c=getchar())
x=(x<<3)+(x<<1)+c-'0';
return x*fl;
}
void open(){
freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
}
void close(){
fclose(stdin);
fclose(stdout);
}
int m,n;
int e[NN][2]={};
LL dp[NN][NN]={};
int s[NN]={};
bool ok[NN][NN]={};
inline int cnt(int l,int r){
return s[r]-s[l-1];
}
bool inside(int x,int l,int r){
return l<=x&&x<=r;
}
LL v[NN]={};
const int mod=1e9+7;
int main(){
//open();
n=read()*2;
m=read();
v[0]=1;
for(int i=2;i<=n;++i){
v[i]=v[i-2]*(i-1)%mod;
}
for(int i=1;i<=m;++i){
e[i][0]=read(),e[i][1]=read();
s[e[i][0]]++;
s[e[i][1]]++;
}
for(int i=1;i<=n;++i){
s[i]+=s[i-1];
}
for(int i=1;i<=n;++i){
for(int j=i+1;j<=n;++j){
ok[i][j]=1;
for(int k=1;k<=m;++k){
if(inside(e[k][0],i,j)^inside(e[k][1],i,j)){
ok[i][j]=0;
break;
}
}
}
}
LL op=0;
for(int len=2;len<=n;len+=2){
for(int i=1;i+len-1<=n;++i){
int j=i+len-1;
if(!ok[i][j])continue;
LL ans=v[len-cnt(i,j)];
for(int c=i+1;c<j;c+=2){
ans-=dp[i][c]*v[j-c-cnt(c+1,j)]%mod;
}
ans%=mod;
dp[i][j]=ans;
ans=ans*v[n-len-2*m+cnt(i,j)]%mod;
op+=ans;
//if(ans)cerr<<i<<" "<<j<<": "<<ans<<endl;
op%=mod;
}
}
op=(op+mod)%mod;
printf("%lld\n",op);
close();
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<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#define LL long long
#define LD long double
using namespace std;
const int NN=2*300 +117;
const int MM= +117;
int read(){
int fl=1,x;char c;
for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
if(c=='-'){fl=-1;c=getchar();}
for(x=0;c>='0'&&c<='9';c=getchar())
x=(x<<3)+(x<<1)+c-'0';
return x*fl;
}
void open(){
freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
}
void close(){
fclose(stdin);
fclose(stdout);
}
int m,n;
int e[NN][2]={};
LL dp[NN][NN]={};
int s[NN]={};
bool ok[NN][NN]={};
inline int cnt(int l,int r){
return s[r]-s[l-1];
}
bool inside(int x,int l,int r){
return l<=x&&x<=r;
}
LL v[NN]={};
const int mod=1e9+7;
int main(){
//open();
n=read()*2;
m=read();
v[0]=1;
for(int i=2;i<=n;++i){
v[i]=v[i-2]*(i-1)%mod;
}
for(int i=1;i<=m;++i){
e[i][0]=read(),e[i][1]=read();
s[e[i][0]]++;
s[e[i][1]]++;
}
for(int i=1;i<=n;++i){
s[i]+=s[i-1];
}
for(int i=1;i<=n;++i){
for(int j=i+1;j<=n;++j){
ok[i][j]=1;
for(int k=1;k<=m;++k){
if(inside(e[k][0],i,j)^inside(e[k][1],i,j)){
ok[i][j]=0;
break;
}
}
}
}
LL op=0;
for(int len=2;len<=n;len+=2){
for(int i=1;i+len-1<=n;++i){
int j=i+len-1;
if(!ok[i][j])continue;
LL ans=v[len-cnt(i,j)];
for(int c=i+1;c<j;c+=2){
ans-=dp[i][c]*v[j-c-cnt(c+1,j)]%mod;
}
ans%=mod;
dp[i][j]=ans;
ans=ans*v[n-len-2*m+cnt(i,j)]%mod;
op+=ans;
//if(ans)cerr<<i<<" "<<j<<": "<<ans<<endl;
op%=mod;
}
}
op=(op+mod)%mod;
printf("%lld\n",op);
close();
return 0;
}
``` |
#include<cstdio>
#include<cstring>
#include<algorithm>
const int mod=1e9+7;
int n,K,link[610];
int cnt[610][610],g[610];
int f[610][610],ans;
void input(){
scanf("%d%d",&n,&K);n<<=1;
for(int i=1,u,v;i<=K;++i){
scanf("%d%d",&u,&v);
link[u]=v;
link[v]=u;
}
return;
}
void work(){
for(int i=1;i<=n;++i)
for(int j=i;j<=n;++j)
cnt[i][j]=cnt[i][j-1]+(!link[j]);
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=i+1;j<=n;j+=2){
bool getOut=0;
for(int k=i;k<=j;++k)
if(link[k]&&(link[k]>j||link[k]<i)){
getOut=1;
break;
}
if(getOut)continue;
f[i][j]=g[cnt[i][j]];
for(int k=i+1;k<=j-1;k+=2)
f[i][j]=(f[i][j]+1ll*(mod-f[i][k])*g[cnt[k+1][j]])%mod;
ans=(ans+1ll*f[i][j]*g[n-2*K-cnt[i][j]])%mod;
}
return;
}
void output(){
printf("%d\n",ans);
return;
}
int main(){
input();
work();
output();
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<cstring>
#include<algorithm>
const int mod=1e9+7;
int n,K,link[610];
int cnt[610][610],g[610];
int f[610][610],ans;
void input(){
scanf("%d%d",&n,&K);n<<=1;
for(int i=1,u,v;i<=K;++i){
scanf("%d%d",&u,&v);
link[u]=v;
link[v]=u;
}
return;
}
void work(){
for(int i=1;i<=n;++i)
for(int j=i;j<=n;++j)
cnt[i][j]=cnt[i][j-1]+(!link[j]);
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=i+1;j<=n;j+=2){
bool getOut=0;
for(int k=i;k<=j;++k)
if(link[k]&&(link[k]>j||link[k]<i)){
getOut=1;
break;
}
if(getOut)continue;
f[i][j]=g[cnt[i][j]];
for(int k=i+1;k<=j-1;k+=2)
f[i][j]=(f[i][j]+1ll*(mod-f[i][k])*g[cnt[k+1][j]])%mod;
ans=(ans+1ll*f[i][j]*g[n-2*K-cnt[i][j]])%mod;
}
return;
}
void output(){
printf("%d\n",ans);
return;
}
int main(){
input();
work();
output();
return 0;
}
``` |
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<cstring>
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
inline char nc(){ return getchar();static char buf[100000] , *p1,*p2; return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,100000,stdin),p1 == p2) ? EOF : *p1++;}
template<class T>inline void rd(T & x){ x = 0;char ch = nc();for(;!isdigit(ch);ch = nc()); for(;isdigit(ch);ch = nc()) x = x * 10 - 48 + ch;}
typedef long long ll;
const int maxn = 600 + 10;
const int mod = 1e9 + 7;
int f[maxn][maxn] , g[maxn][maxn] , h[maxn] , a[maxn] , b[maxn];
template<class T>inline T add(T a,T b){ a += b; if(a > mod) a -= mod; return a;}
template<class T>inline T sub(T a,T b){ a -= b; if(a < 0) a += mod; return a;}
template<class T>inline T mul(T a,T b){ return (ll)a * b % mod;}
int main(){
int n,m;
rd(n); rd(m);
for(int i = 1;i <= m;i++){
rd(a[i]);
rd(b[i]);
}
h[0] = 1;
for(int i = 1;i <= n;i++) h[i] = mul(h[i - 1],(i << 1) - 1);
int ans = 0;
for(int l = n * 2;l;l--){
for(int r = l + 1;r <= n * 2;r += 2){
bool flag = 1;
int in = 0;
for(int i = 1;i <= m;i++){
int t = (a[i] >= l && a[i] <= r) + (b[i] >= l && b[i] <= r);
if(t == 1){
flag = 0;
break;
}
if(t) in++;
}
if(flag){
in = (r - l + 1 >> 1) - in;
int out = n - m - in;
f[l][r] = g[l][r] = h[in];
for(int i = l + 1;i < r;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[out]));
}
}
}
printf("%d\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>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<cstring>
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
inline char nc(){ return getchar();static char buf[100000] , *p1,*p2; return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,100000,stdin),p1 == p2) ? EOF : *p1++;}
template<class T>inline void rd(T & x){ x = 0;char ch = nc();for(;!isdigit(ch);ch = nc()); for(;isdigit(ch);ch = nc()) x = x * 10 - 48 + ch;}
typedef long long ll;
const int maxn = 600 + 10;
const int mod = 1e9 + 7;
int f[maxn][maxn] , g[maxn][maxn] , h[maxn] , a[maxn] , b[maxn];
template<class T>inline T add(T a,T b){ a += b; if(a > mod) a -= mod; return a;}
template<class T>inline T sub(T a,T b){ a -= b; if(a < 0) a += mod; return a;}
template<class T>inline T mul(T a,T b){ return (ll)a * b % mod;}
int main(){
int n,m;
rd(n); rd(m);
for(int i = 1;i <= m;i++){
rd(a[i]);
rd(b[i]);
}
h[0] = 1;
for(int i = 1;i <= n;i++) h[i] = mul(h[i - 1],(i << 1) - 1);
int ans = 0;
for(int l = n * 2;l;l--){
for(int r = l + 1;r <= n * 2;r += 2){
bool flag = 1;
int in = 0;
for(int i = 1;i <= m;i++){
int t = (a[i] >= l && a[i] <= r) + (b[i] >= l && b[i] <= r);
if(t == 1){
flag = 0;
break;
}
if(t) in++;
}
if(flag){
in = (r - l + 1 >> 1) - in;
int out = n - m - in;
f[l][r] = g[l][r] = h[in];
for(int i = l + 1;i < r;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[out]));
}
}
}
printf("%d\n",ans);
}
``` |
#include <cstdio>
constexpr int MOD = 1e9 + 7;
int x[605], y[605], pre[605];
long long dp[605][605], fact[605], ans;
int main()
{
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)
{
auto in = [&] (int t) { return l <= t && t < r; };
int cnt = pre[r - 1] - (l ? pre[l - 1] : 0);
bool flg = true;
for (int i = 0; i < k && flg; i++)
flg &= in(x[i]) == in(y[i]);
if (!flg || cnt & 1)
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
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>
constexpr int MOD = 1e9 + 7;
int x[605], y[605], pre[605];
long long dp[605][605], fact[605], ans;
int main()
{
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)
{
auto in = [&] (int t) { return l <= t && t < r; };
int cnt = pre[r - 1] - (l ? pre[l - 1] : 0);
bool flg = true;
for (int i = 0; i < k && flg; i++)
flg &= in(x[i]) == in(y[i]);
if (!flg || cnt & 1)
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>
#define gc getchar()
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
using namespace std;
const int mod = 1e9+7;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
int f[1000100];
int dp[630][630];
int X[100010],Y[100010];
int n,k;
int vis[1000010];
int lk[1000001];
int main()
{
n=read();k=read();
for(rint i=1;i<=k;++i)
{
X[i]=read(),Y[i]=read();
vis[X[i]]=vis[Y[i]]=1;lk[X[i]]=Y[i];lk[Y[i]]=X[i];
}
for(rint i=1;i<=n<<1;++i) vis[i]+=vis[i-1];
f[0]=1; ll res=0;
for(rint i=2;i<=n<<1;i+=2) f[i]=1LL*f[i-2]*(i-1)%mod;
for(rint l=1;l<=(n<<1);++l)
{
for(rint r=l+1;r<=n<<1;r+=2)
{ int fl=1;
for(rint p=l;p<=r;++p)
{
if(lk[p]&&(lk[p]<l||lk[p]>r)) {fl=0;break;}
}
if(!fl) continue;
dp[l][r]=1ll*f[(r-l+1-vis[r]+vis[l-1])];
for(rint p=l;p<r;++p)
{
dp[l][r]-=1ll*dp[l][p]*f[r-p-vis[r]+vis[p]]%mod;
dp[l][r]+=mod;
dp[l][r]%=mod;
}
res+=1ll*dp[l][r]*f[2*n-(r-l+1)-vis[n*2]+vis[r]-vis[l-1]]%mod;
res%=mod;
}
}cout<<res;
return 0;
}
/*
by was_n
18.10.15
*/
| ### 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 gc getchar()
#define ll long long
#define pb push_back
#define mk make_pair
#define rint register int
using namespace std;
const int mod = 1e9+7;
inline int read(){int w=1,s=0;char ch=getchar();while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}while(isdigit(ch)){s=s*10+ch-'0';ch=getchar();}return w*s;}
int f[1000100];
int dp[630][630];
int X[100010],Y[100010];
int n,k;
int vis[1000010];
int lk[1000001];
int main()
{
n=read();k=read();
for(rint i=1;i<=k;++i)
{
X[i]=read(),Y[i]=read();
vis[X[i]]=vis[Y[i]]=1;lk[X[i]]=Y[i];lk[Y[i]]=X[i];
}
for(rint i=1;i<=n<<1;++i) vis[i]+=vis[i-1];
f[0]=1; ll res=0;
for(rint i=2;i<=n<<1;i+=2) f[i]=1LL*f[i-2]*(i-1)%mod;
for(rint l=1;l<=(n<<1);++l)
{
for(rint r=l+1;r<=n<<1;r+=2)
{ int fl=1;
for(rint p=l;p<=r;++p)
{
if(lk[p]&&(lk[p]<l||lk[p]>r)) {fl=0;break;}
}
if(!fl) continue;
dp[l][r]=1ll*f[(r-l+1-vis[r]+vis[l-1])];
for(rint p=l;p<r;++p)
{
dp[l][r]-=1ll*dp[l][p]*f[r-p-vis[r]+vis[p]]%mod;
dp[l][r]+=mod;
dp[l][r]%=mod;
}
res+=1ll*dp[l][r]*f[2*n-(r-l+1)-vis[n*2]+vis[r]-vis[l-1]]%mod;
res%=mod;
}
}cout<<res;
return 0;
}
/*
by was_n
18.10.15
*/
``` |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=305;
const int M=2*N;
const int mod=1e9+7;
int pow(int x, int k)
{
int ret=1;
for(;k;k>>=1,x=(ll)x*x%mod) if(k&1) ret=(ll)ret*x%mod;
return ret;
}
int dp[M][M],g[M],inv[M],n,m,a[N],b[N];
bool was[M][M],f[M];
bool in(int x, int l, int r){ return x>=l && x<=r;}
int DP(int l, int r)
{
if(was[l][r]) return dp[l][r];
if(l>r) return 0;
was[l][r]=1;
int x=0,y=0,z=0,i;
for(i=1;i<=m;i++) if(in(a[i],l,r)!=in(b[i],l,r)) return 0;
for(i=1;i<l;i++) y+=f[i];
for(i=l;i<=r;i++) x+=f[i];
for(i=r+1;i<=n;i++) y+=f[i];
dp[l][r]=(ll)g[x]*g[y]%mod;
z+=f[r];
for(i=r-1;i>=l;i--)
{
int tmp=DP(l,i);
dp[l][r]-=(ll)tmp*inv[y+z]%mod*g[z]%mod*g[y]%mod;
if(dp[l][r]<0) dp[l][r]+=mod;
z+=f[i];
}
return dp[l][r];
}
int main()
{
int i,j;
scanf("%i %i",&n,&m);n*=2;
for(i=1;i<=n;i++) f[i]=1;
for(i=1;i<=m;i++) scanf("%i %i",&a[i],&b[i]),f[a[i]]=0,f[b[i]]=0;
g[0]=1;for(i=2;i<=n;i+=2) g[i]=(ll)g[i-2]*(i-1)%mod;
for(i=0;i<=n;i++) inv[i]=pow(g[i],mod-2);
int ans=0;
for(i=1;i<=n;i++) for(j=i;j<=n;j++) ans+=DP(i,j),ans=ans>=mod?ans-mod:ans;
printf("%i\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;
#define ll long long
const int N=305;
const int M=2*N;
const int mod=1e9+7;
int pow(int x, int k)
{
int ret=1;
for(;k;k>>=1,x=(ll)x*x%mod) if(k&1) ret=(ll)ret*x%mod;
return ret;
}
int dp[M][M],g[M],inv[M],n,m,a[N],b[N];
bool was[M][M],f[M];
bool in(int x, int l, int r){ return x>=l && x<=r;}
int DP(int l, int r)
{
if(was[l][r]) return dp[l][r];
if(l>r) return 0;
was[l][r]=1;
int x=0,y=0,z=0,i;
for(i=1;i<=m;i++) if(in(a[i],l,r)!=in(b[i],l,r)) return 0;
for(i=1;i<l;i++) y+=f[i];
for(i=l;i<=r;i++) x+=f[i];
for(i=r+1;i<=n;i++) y+=f[i];
dp[l][r]=(ll)g[x]*g[y]%mod;
z+=f[r];
for(i=r-1;i>=l;i--)
{
int tmp=DP(l,i);
dp[l][r]-=(ll)tmp*inv[y+z]%mod*g[z]%mod*g[y]%mod;
if(dp[l][r]<0) dp[l][r]+=mod;
z+=f[i];
}
return dp[l][r];
}
int main()
{
int i,j;
scanf("%i %i",&n,&m);n*=2;
for(i=1;i<=n;i++) f[i]=1;
for(i=1;i<=m;i++) scanf("%i %i",&a[i],&b[i]),f[a[i]]=0,f[b[i]]=0;
g[0]=1;for(i=2;i<=n;i+=2) g[i]=(ll)g[i-2]*(i-1)%mod;
for(i=0;i<=n;i++) inv[i]=pow(g[i],mod-2);
int ans=0;
for(i=1;i<=n;i++) for(j=i;j<=n;j++) ans+=DP(i,j),ans=ans>=mod?ans-mod:ans;
printf("%i\n",ans);
return 0;
}
``` |
#include <cstdio>
typedef long long LL;
const int Mod = 1000000007;
const int MN = 605;
int N, K, J[MN], G[MN], f[MN][MN], Ans;
int main() {
scanf("%d%d", &N, &K), 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, x, y; i <= K; ++i) scanf("%d%d", &x, &y), J[x] = y, J[y] = x;
for (int i = 1; i <= N; ++i)
for (int j = i + 1; j <= N; j += 2) {
int ok = 1, c = j - i + 1;
for (int k = i; k <= j; ++k) if (J[k] && (--c, J[k] < i || J[k] > j)) ok = 0;
if (!ok) continue;
f[i][j] = G[c];
for (int k = j, d = 0; k > i; --k)
d += !J[k], f[i][j] = (f[i][j] - (LL)f[i][k - 1] * G[d]) % Mod;
Ans = (Ans + (LL)f[i][j] * G[N - 2 * K - c]) % Mod;
}
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 <cstdio>
typedef long long LL;
const int Mod = 1000000007;
const int MN = 605;
int N, K, J[MN], G[MN], f[MN][MN], Ans;
int main() {
scanf("%d%d", &N, &K), 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, x, y; i <= K; ++i) scanf("%d%d", &x, &y), J[x] = y, J[y] = x;
for (int i = 1; i <= N; ++i)
for (int j = i + 1; j <= N; j += 2) {
int ok = 1, c = j - i + 1;
for (int k = i; k <= j; ++k) if (J[k] && (--c, J[k] < i || J[k] > j)) ok = 0;
if (!ok) continue;
f[i][j] = G[c];
for (int k = j, d = 0; k > i; --k)
d += !J[k], f[i][j] = (f[i][j] - (LL)f[i][k - 1] * G[d]) % Mod;
Ans = (Ans + (LL)f[i][j] * G[N - 2 * K - c]) % Mod;
}
printf("%d\n", Ans);
return 0;
}
``` |
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ET cout << "\n"
#define MEM(i,j) memset(i,j,sizeof i)
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cerr << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
ll dp[605][605],g[605],match[605],dp2[605][605];
int main()
{jizz
ll n,m,ans=0,a,b,flag;
cin >> n >> m;
while(m--)
cin >> a >> b,match[a]=b,match[b]=a;
g[0]=1,n*=2;
for(int i=2;i<=n;i+=2)
g[i]=g[i-2]*(i-1)%MOD;
for(int i=1;i<n;i+=2)
for(int j=1;j+i<=n;++j)
{
a=0,b=0,flag=0;
for(int k=j;k<=j+i&&!flag;++k)
if(match[k]&&(match[k]<j||match[k]>i+j))
{
flag=1;
break;
}
if(flag) continue;
for(int k=1;k<j;++k)
if(!match[k]) ++b;
for(int k=j+i+1;k<=n;++k)
if(!match[k]) ++b;
for(int k=j+i-1;k>=j;--k)
{
if(!match[k+1]) ++a;
dp[j][j+i]=(dp[j][j+i]-dp2[j][k]*g[a]%MOD*g[b])%MOD;
dp2[j][j+i]=(dp2[j][j+i]-dp2[j][k]*g[a])%MOD;
}
if(!match[j]) ++a;
dp[j][j+i]=((dp[j][j+i]+g[a]*g[b])%MOD+MOD)%MOD,dp2[j][j+i]=(dp2[j][j+i]+g[a])%MOD;
ans=(ans+dp[j][j+i])%MOD;
}
cout << ans << "\n";
} | ### 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
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ET cout << "\n"
#define MEM(i,j) memset(i,j,sizeof i)
#define ALL(v) v.begin(),v.end()
#define DB(a,s,e) {for(int i=s;i<e;++i) cerr << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
ll dp[605][605],g[605],match[605],dp2[605][605];
int main()
{jizz
ll n,m,ans=0,a,b,flag;
cin >> n >> m;
while(m--)
cin >> a >> b,match[a]=b,match[b]=a;
g[0]=1,n*=2;
for(int i=2;i<=n;i+=2)
g[i]=g[i-2]*(i-1)%MOD;
for(int i=1;i<n;i+=2)
for(int j=1;j+i<=n;++j)
{
a=0,b=0,flag=0;
for(int k=j;k<=j+i&&!flag;++k)
if(match[k]&&(match[k]<j||match[k]>i+j))
{
flag=1;
break;
}
if(flag) continue;
for(int k=1;k<j;++k)
if(!match[k]) ++b;
for(int k=j+i+1;k<=n;++k)
if(!match[k]) ++b;
for(int k=j+i-1;k>=j;--k)
{
if(!match[k+1]) ++a;
dp[j][j+i]=(dp[j][j+i]-dp2[j][k]*g[a]%MOD*g[b])%MOD;
dp2[j][j+i]=(dp2[j][j+i]-dp2[j][k]*g[a])%MOD;
}
if(!match[j]) ++a;
dp[j][j+i]=((dp[j][j+i]+g[a]*g[b])%MOD+MOD)%MOD,dp2[j][j+i]=(dp2[j][j+i]+g[a])%MOD;
ans=(ans+dp[j][j+i])%MOD;
}
cout << ans << "\n";
}
``` |
#include<bits/stdc++.h>
using namespace std;
const int N=1005,P=1e9+7;
int n,m,nxt[N],s[N],f[N][N],g[N];
int main()
{
scanf("%d%d",&n,&m);n*=2;
for(int i=1,u,v;i<=m;i++)scanf("%d%d",&u,&v),nxt[u]=v,nxt[v]=u,s[u]=1,s[v]=1;
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]%P;
for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j+=2)
{
int ff=1;
for(int k=i;k<=j;k++)if(nxt[k]&&(nxt[k]<i||nxt[k]>j)){ff=0;break;}
if(!ff)continue;
for(int k=i+1;k<j;k+=2)f[i][j]=(f[i][j]+1ll*f[i][k]*g[j-k-s[j]+s[k]])%P;
f[i][j]=(g[j-i+1-s[j]+s[i-1]]-f[i][j]+P)%P;
}
m*=2;int ans=0;
for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j+=2)ans=(ans+1ll*f[i][j]*g[n-(j-i+1)-(m-s[j]+s[i-1])])%P;
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<bits/stdc++.h>
using namespace std;
const int N=1005,P=1e9+7;
int n,m,nxt[N],s[N],f[N][N],g[N];
int main()
{
scanf("%d%d",&n,&m);n*=2;
for(int i=1,u,v;i<=m;i++)scanf("%d%d",&u,&v),nxt[u]=v,nxt[v]=u,s[u]=1,s[v]=1;
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]%P;
for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j+=2)
{
int ff=1;
for(int k=i;k<=j;k++)if(nxt[k]&&(nxt[k]<i||nxt[k]>j)){ff=0;break;}
if(!ff)continue;
for(int k=i+1;k<j;k+=2)f[i][j]=(f[i][j]+1ll*f[i][k]*g[j-k-s[j]+s[k]])%P;
f[i][j]=(g[j-i+1-s[j]+s[i-1]]-f[i][j]+P)%P;
}
m*=2;int ans=0;
for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j+=2)ans=(ans+1ll*f[i][j]*g[n-(j-i+1)-(m-s[j]+s[i-1])])%P;
printf("%d\n",ans);
return 0;
}
``` |
#include<cstdio>
using namespace std;
#define N 605
#define mod 1000000007
int dp[N][N],n,m,a,b,t[N],s[N],su[N],as;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d",&a,&b),t[a]=b,t[b]=a;
s[0]=1;for(int i=2;i<=n*2;i++)s[i]=1ll*s[i-2]*(i-1)%mod;
for(int i=1;i<=n*2;i++)su[i]=su[i-1]+!t[i];
for(int l=1;l<=n*2;l++)
for(int i=1;i+l<=n*2;i++)
{
int j=i+l;
int fg=1;
for(int k=i;k<=j;k++)if((t[k]<i||t[k]>j)&&t[k])fg=0;
if((su[j]^su[i-1])&1)fg=0;
if(!fg)continue;
int tp=s[su[j]-su[i-1]];
for(int k=i;k<j;k++)tp=(tp-1ll*dp[i][k]*s[su[j]-su[k]]%mod+mod)%mod;
dp[i][j]=tp;as=(as+1ll*tp*s[su[n*2]-su[j]+su[i-1]])%mod;
}
printf("%d\n",as);
} | ### 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>
using namespace std;
#define N 605
#define mod 1000000007
int dp[N][N],n,m,a,b,t[N],s[N],su[N],as;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d",&a,&b),t[a]=b,t[b]=a;
s[0]=1;for(int i=2;i<=n*2;i++)s[i]=1ll*s[i-2]*(i-1)%mod;
for(int i=1;i<=n*2;i++)su[i]=su[i-1]+!t[i];
for(int l=1;l<=n*2;l++)
for(int i=1;i+l<=n*2;i++)
{
int j=i+l;
int fg=1;
for(int k=i;k<=j;k++)if((t[k]<i||t[k]>j)&&t[k])fg=0;
if((su[j]^su[i-1])&1)fg=0;
if(!fg)continue;
int tp=s[su[j]-su[i-1]];
for(int k=i;k<j;k++)tp=(tp-1ll*dp[i][k]*s[su[j]-su[k]]%mod+mod)%mod;
dp[i][j]=tp;as=(as+1ll*tp*s[su[n*2]-su[j]+su[i-1]])%mod;
}
printf("%d\n",as);
}
``` |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int MAXN = 605;
const ll MOD = 1000000007;
int n,m;
int match[MAXN];
ll ans;
ll fac[MAXN];
ll f[MAXN][MAXN];
int main()
{
scanf("%d%d",&n,&m);
n <<= 1;
for (int u,v,i = 1;i <= m;i++)
{
scanf("%d%d",&u,&v);
match[u] = v;
match[v] = u;
}
fac[0] = 1;
for (int i = 2;i <= n;i += 2)
fac[i] = fac[i - 2] * (i - 1) % MOD;
for (int len = 2;len <= n;len += 2)
for (int i = 1,j = len;j <= n;i++,j++)
{
bool ok = 1;
for (int k = i;k <= j;k++)
if (match[k] && (match[k] < i || match[k] > j))
{
ok = 0;
break;
}
if (!ok)
continue;
int cnt = 0;
for (int k = i;k <= j;k++)
{
cnt += !match[k];
if (~cnt & 1)
(f[i][j] -= fac[cnt] * f[k + 1][j]) %= MOD;
}
(f[i][j] += fac[cnt]) %= MOD;
(ans += f[i][j] * fac[n - 2 * m - cnt]) %= MOD;
}
printf("%lld\n",(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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int MAXN = 605;
const ll MOD = 1000000007;
int n,m;
int match[MAXN];
ll ans;
ll fac[MAXN];
ll f[MAXN][MAXN];
int main()
{
scanf("%d%d",&n,&m);
n <<= 1;
for (int u,v,i = 1;i <= m;i++)
{
scanf("%d%d",&u,&v);
match[u] = v;
match[v] = u;
}
fac[0] = 1;
for (int i = 2;i <= n;i += 2)
fac[i] = fac[i - 2] * (i - 1) % MOD;
for (int len = 2;len <= n;len += 2)
for (int i = 1,j = len;j <= n;i++,j++)
{
bool ok = 1;
for (int k = i;k <= j;k++)
if (match[k] && (match[k] < i || match[k] > j))
{
ok = 0;
break;
}
if (!ok)
continue;
int cnt = 0;
for (int k = i;k <= j;k++)
{
cnt += !match[k];
if (~cnt & 1)
(f[i][j] -= fac[cnt] * f[k + 1][j]) %= MOD;
}
(f[i][j] += fac[cnt]) %= MOD;
(ans += f[i][j] * fac[n - 2 * m - cnt]) %= MOD;
}
printf("%lld\n",(ans + MOD) % MOD);
return 0;
}
``` |
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int dp[605][605],c[605][605],ans;
int n,k,a,b;
int go[605],g[605];
int main(){
scanf("%d %d",&n,&k);
for(int i=1;i<=k;i++){
scanf("%d %d",&a,&b);
go[a]=b,go[b]=a;
}
for(int i=0;i<=2*n;i++){
g[i]=1;
for(int j=1;j<i;j+=2) g[i]=1ll*g[i]*j%mod;
}
for(int l=1;l<2*n;l++){
for(int r=l+1;r<=2*n;r+=2){
int tot=0,flag=1;
for(int i=l;i<=r;i++){
if(go[i]&&(go[i]<l||go[i]>r)) flag=0;
else if(!go[i]) tot++;
}
if(flag) c[l][r]=tot;else c[l][r]=-1;
}
}
for(int l=1;l<2*n;l++){
for(int r=l+1;r<=2*n;r+=2){
if(c[l][r]==-1) continue;
dp[l][r]=g[c[l][r]];
for(int i=l;i<r;i++) (dp[l][r]+=mod-1ll*dp[l][i]*g[c[i+1][r]]%mod)%=mod;
(ans+=1ll*dp[l][r]*g[2*n-2*k-c[l][r]]%mod)%=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<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int dp[605][605],c[605][605],ans;
int n,k,a,b;
int go[605],g[605];
int main(){
scanf("%d %d",&n,&k);
for(int i=1;i<=k;i++){
scanf("%d %d",&a,&b);
go[a]=b,go[b]=a;
}
for(int i=0;i<=2*n;i++){
g[i]=1;
for(int j=1;j<i;j+=2) g[i]=1ll*g[i]*j%mod;
}
for(int l=1;l<2*n;l++){
for(int r=l+1;r<=2*n;r+=2){
int tot=0,flag=1;
for(int i=l;i<=r;i++){
if(go[i]&&(go[i]<l||go[i]>r)) flag=0;
else if(!go[i]) tot++;
}
if(flag) c[l][r]=tot;else c[l][r]=-1;
}
}
for(int l=1;l<2*n;l++){
for(int r=l+1;r<=2*n;r+=2){
if(c[l][r]==-1) continue;
dp[l][r]=g[c[l][r]];
for(int i=l;i<r;i++) (dp[l][r]+=mod-1ll*dp[l][i]*g[c[i+1][r]]%mod)%=mod;
(ans+=1ll*dp[l][r]*g[2*n-2*k-c[l][r]]%mod)%=mod;
}
}
printf("%d\n",ans);
return 0;
}
``` |
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const ll N=605,mo=1e9+7;
ll a[N][N],b[N],c[N],ans,n,k;
ll f[N][N];
int main(){
cin>>n>>k;n*=2;
for (int i=1;i<=k;i++){
int x,y;
scanf("%d%d",&x,&y);
a[x][y]++;
a[y][x]++;
b[x]++;
b[y]++;
}
c[0]=1;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
}
b[i]+=b[i-1];
if (i>1)c[i]=c[i-2]*(i-1)%mo;
}
ans=c[n-2*k];
for (int i=1;i<=n;i++){
for (int j=i+1;j<n;j++){
int s1=a[j][j]+a[i-1][i-1]-a[j][i-1]-a[i-1][j],s2=b[j]-b[i-1];
if (s1!=s2)continue;
f[i][j]=c[j-i+1-s1];
for (int l=i+1;l<j;l++)
f[i][j]=(f[i][j]-f[i][l]*c[j-l-b[j]+b[l]])%mo;
(ans+=f[i][j]*c[n-(j-i+1-s1)-2*k])%=mo;
}
}
ans=(ans%mo+mo)%mo;
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<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const ll N=605,mo=1e9+7;
ll a[N][N],b[N],c[N],ans,n,k;
ll f[N][N];
int main(){
cin>>n>>k;n*=2;
for (int i=1;i<=k;i++){
int x,y;
scanf("%d%d",&x,&y);
a[x][y]++;
a[y][x]++;
b[x]++;
b[y]++;
}
c[0]=1;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
}
b[i]+=b[i-1];
if (i>1)c[i]=c[i-2]*(i-1)%mo;
}
ans=c[n-2*k];
for (int i=1;i<=n;i++){
for (int j=i+1;j<n;j++){
int s1=a[j][j]+a[i-1][i-1]-a[j][i-1]-a[i-1][j],s2=b[j]-b[i-1];
if (s1!=s2)continue;
f[i][j]=c[j-i+1-s1];
for (int l=i+1;l<j;l++)
f[i][j]=(f[i][j]-f[i][l]*c[j-l-b[j]+b[l]])%mo;
(ans+=f[i][j]*c[n-(j-i+1-s1)-2*k])%=mo;
}
}
ans=(ans%mo+mo)%mo;
cout<<ans<<endl;
}
``` |
#include<bits/stdc++.h>
#define mo 1000000007
#define ll long long
using namespace std;
struct Po{int x,y;}a[610];
int n,k,c[610];
ll p[610],an[610][610],ansn;
bool be(int x,int y,int z){
if (x<=y&&(z<x||z>y))return false;
if (x>y&&y<z&&z<x)return false;
return true;
}
int co(int x){if (x<=0) x+=n*2;if (x>n*2)x-=n*2;return x;}
int coc(int l,int r){
if (l<=r) return c[r]-c[l-1];
return c[r]+c[n*2]-c[l-1];
}
int main(){
scanf("%d %d",&n,&k);
for (int i=1;i<=n*2;i++)c[i]=1;
for (int i=1;i<=k;i++){cin>>a[i].x>>a[i].y;c[a[i].x]=c[a[i].y]=0;}
for (int i=1;i<=n*2;i++) c[i]+=c[i-1];
p[0]=1;
for (int i=1;i<=n*2;i++)if(i%2==1)p[i]=0;else p[i]=p[i-2]*(i-1)%mo;
for (int i=1;i<=n*2;i++){
for (int j=i+1;j<=n*2;j+=2){
bool c=true;
for (int l=1;l<=k;l++)if ((be(i,j,a[l].x)^be(i,j,a[l].y))==1)c=false;
if (c)an[i][j]=p[coc(i,j)]%mo;
}
}
for (int i=1;i<=n*2;i++)
for (int j=i+1;j<=n*2;j+=2){
if (an[i][j]!=0){
for (int l=i+1;l<j;l+=2)an[i][j]=(an[i][j]-an[i][l]*p[coc(l+1,j)])%mo;
}
}
for (int i=1;i<=n*2;i++)
for (int j=i+1;j<=n*2;j+=2){
ansn=(ansn+an[i][j]*(p[n*2-k*2-coc(i,j)]))%mo;
}
ansn=(ansn+mo)%mo;
cout<<ansn<<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 mo 1000000007
#define ll long long
using namespace std;
struct Po{int x,y;}a[610];
int n,k,c[610];
ll p[610],an[610][610],ansn;
bool be(int x,int y,int z){
if (x<=y&&(z<x||z>y))return false;
if (x>y&&y<z&&z<x)return false;
return true;
}
int co(int x){if (x<=0) x+=n*2;if (x>n*2)x-=n*2;return x;}
int coc(int l,int r){
if (l<=r) return c[r]-c[l-1];
return c[r]+c[n*2]-c[l-1];
}
int main(){
scanf("%d %d",&n,&k);
for (int i=1;i<=n*2;i++)c[i]=1;
for (int i=1;i<=k;i++){cin>>a[i].x>>a[i].y;c[a[i].x]=c[a[i].y]=0;}
for (int i=1;i<=n*2;i++) c[i]+=c[i-1];
p[0]=1;
for (int i=1;i<=n*2;i++)if(i%2==1)p[i]=0;else p[i]=p[i-2]*(i-1)%mo;
for (int i=1;i<=n*2;i++){
for (int j=i+1;j<=n*2;j+=2){
bool c=true;
for (int l=1;l<=k;l++)if ((be(i,j,a[l].x)^be(i,j,a[l].y))==1)c=false;
if (c)an[i][j]=p[coc(i,j)]%mo;
}
}
for (int i=1;i<=n*2;i++)
for (int j=i+1;j<=n*2;j+=2){
if (an[i][j]!=0){
for (int l=i+1;l<j;l+=2)an[i][j]=(an[i][j]-an[i][l]*p[coc(l+1,j)])%mo;
}
}
for (int i=1;i<=n*2;i++)
for (int j=i+1;j<=n*2;j+=2){
ansn=(ansn+an[i][j]*(p[n*2-k*2-coc(i,j)]))%mo;
}
ansn=(ansn+mo)%mo;
cout<<ansn<<endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 603;
const ll MOD = 1000000007;
template <typename T> void cmin(T &x, const T &y)
{
if(y < x) x = y;
}
template <typename T> void cmax(T &x, const T &y)
{
if(y > x) x = y;
}
template <typename T> void read(T &x)
{
x = 0; char c = getchar(); bool f = 0;
while(!isdigit(c) && c!='-') c = getchar();
if(c == '-') f = 1, c = getchar();
while(isdigit(c)) x = x*10+c-'0', c = getchar();
if(f) x = -x;
}
int n, k;
ll f[MX][MX], way[MX];
int mnid[MX][MX], mxid[MX][MX];
int sum[MX];
int main()
{
read(n), read(k);
memset(mnid, 0x3f, sizeof(mnid));
memset(mxid, 0x00, sizeof(mxid));
for(int i=1; i<=k; i++)
{
int a, b;
read(a), read(b);
mnid[a][a] = mxid[a][a] = b;
mnid[b][b] = mxid[b][b] = a;
sum[a]++, sum[b]++;
}
for(int i=1; i<=n*2; i++) sum[i] += sum[i-1];
for(int i=1; i<=n*2; i++)
for(int j=i+1; j<=n*2; j++)
{
mnid[i][j] = min(mnid[i][j-1], mnid[j][j]);
mxid[i][j] = max(mxid[i][j-1], mxid[j][j]);
}
way[0] = 1;
for(int i=2; i<=n*2; i++) way[i] = way[i-2] * (i-1) % MOD;
ll ans = 0;
for(int i=1; i<=n*2; i++)
{
for(int l=1; l+i-1<=n*2; l++)
{
int r = l+i-1;
if(mnid[l][r]<l || mxid[l][r]>r) continue;
int num = (r-l+1) - (sum[r]-sum[l-1]);
if(num & 1) continue;
f[l][r] = way[num];
for(int j=l; j<r; j++)
{
int rem = (r-j) - (sum[r]-sum[j]);
if(rem&1) continue;
else if(mnid[j+1][r]<=j || mxid[j+1][r]>r) continue;
else f[l][r] = (f[l][r] - f[l][j]*way[rem] + MOD*MOD) % MOD;
}
ans = (ans + f[l][r]*way[n*2-k*2-num]) % MOD;
//cout<<l<<" "<<r<<" "<<f[l][r]<<" "<<way[n*2-k*2-num]<<endl;
}
}
printf("%lld\n", ans);
//printf("%lld\n", f[1][n*2]);
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;
typedef long long ll;
const int MX = 603;
const ll MOD = 1000000007;
template <typename T> void cmin(T &x, const T &y)
{
if(y < x) x = y;
}
template <typename T> void cmax(T &x, const T &y)
{
if(y > x) x = y;
}
template <typename T> void read(T &x)
{
x = 0; char c = getchar(); bool f = 0;
while(!isdigit(c) && c!='-') c = getchar();
if(c == '-') f = 1, c = getchar();
while(isdigit(c)) x = x*10+c-'0', c = getchar();
if(f) x = -x;
}
int n, k;
ll f[MX][MX], way[MX];
int mnid[MX][MX], mxid[MX][MX];
int sum[MX];
int main()
{
read(n), read(k);
memset(mnid, 0x3f, sizeof(mnid));
memset(mxid, 0x00, sizeof(mxid));
for(int i=1; i<=k; i++)
{
int a, b;
read(a), read(b);
mnid[a][a] = mxid[a][a] = b;
mnid[b][b] = mxid[b][b] = a;
sum[a]++, sum[b]++;
}
for(int i=1; i<=n*2; i++) sum[i] += sum[i-1];
for(int i=1; i<=n*2; i++)
for(int j=i+1; j<=n*2; j++)
{
mnid[i][j] = min(mnid[i][j-1], mnid[j][j]);
mxid[i][j] = max(mxid[i][j-1], mxid[j][j]);
}
way[0] = 1;
for(int i=2; i<=n*2; i++) way[i] = way[i-2] * (i-1) % MOD;
ll ans = 0;
for(int i=1; i<=n*2; i++)
{
for(int l=1; l+i-1<=n*2; l++)
{
int r = l+i-1;
if(mnid[l][r]<l || mxid[l][r]>r) continue;
int num = (r-l+1) - (sum[r]-sum[l-1]);
if(num & 1) continue;
f[l][r] = way[num];
for(int j=l; j<r; j++)
{
int rem = (r-j) - (sum[r]-sum[j]);
if(rem&1) continue;
else if(mnid[j+1][r]<=j || mxid[j+1][r]>r) continue;
else f[l][r] = (f[l][r] - f[l][j]*way[rem] + MOD*MOD) % MOD;
}
ans = (ans + f[l][r]*way[n*2-k*2-num]) % MOD;
//cout<<l<<" "<<r<<" "<<f[l][r]<<" "<<way[n*2-k*2-num]<<endl;
}
}
printf("%lld\n", ans);
//printf("%lld\n", f[1][n*2]);
return 0;
}
``` |
#include <cstdio>
#include <algorithm>
using namespace std;
const int Max_N(605);
const int MOD(1000000000 + 7);
constexpr int Mult(int a, int b)
{
return a * 1LL * b % MOD;
}
constexpr int Add(int a, int b)
{
return a + b >= MOD ? a + b - MOD : a + b;
}
constexpr int Sub(int a, int b)
{
return a - b < 0 ? a - b + MOD : a - b;
}
inline void upd(int &a, int b)
{
a = Add(a, b);
}
inline void inc(int &a, int b)
{
a = Sub(a, b);
}
int N, K, P[Max_N], F[Max_N][Max_N], H[Max_N], Cnt[Max_N][Max_N];
int main()
{
scanf("%d%d", &N, &K), N <<= 1;
for (int i = 1;i <= N;++i)
Cnt[i][i] = 1;
for (int a, b, i = 1;i <= K;++i)
scanf("%d%d", &a, &b), P[a] = b, P[b] = a;
for (int a = 1;a <= N;++a)
for (int b = a;b <= N;++b)
Cnt[a][b] = Cnt[a][b - 1] + (P[b] == 0);
H[0] = 1;
for (int n = 2;n <= N;n += 2)
H[n] = Mult(n - 1, H[n - 2]);
for (int ad = 1;ad <= N;++ad)
for (int i = 1, j = ad;j <= N;++i, ++j)
{
for (int k = i;k <= j;++k)
if (P[k] && (P[k] < i || P[k] > j))
goto loop;
F[i][j] = H[Cnt[i][j]];
for (int k = i + 1;k <= j - 1;++k)
inc(F[i][j], Mult(F[i][k], H[Cnt[k + 1][j]]));
loop :
;
}
int Ans = 0;
for (int i = 1;i <= N;++i)
for (int j = i + 1;j <= N;++j)
upd(Ans, Mult(F[i][j], H[(N / 2 - K) * 2 - Cnt[i][j]]));
printf("%d", 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 <cstdio>
#include <algorithm>
using namespace std;
const int Max_N(605);
const int MOD(1000000000 + 7);
constexpr int Mult(int a, int b)
{
return a * 1LL * b % MOD;
}
constexpr int Add(int a, int b)
{
return a + b >= MOD ? a + b - MOD : a + b;
}
constexpr int Sub(int a, int b)
{
return a - b < 0 ? a - b + MOD : a - b;
}
inline void upd(int &a, int b)
{
a = Add(a, b);
}
inline void inc(int &a, int b)
{
a = Sub(a, b);
}
int N, K, P[Max_N], F[Max_N][Max_N], H[Max_N], Cnt[Max_N][Max_N];
int main()
{
scanf("%d%d", &N, &K), N <<= 1;
for (int i = 1;i <= N;++i)
Cnt[i][i] = 1;
for (int a, b, i = 1;i <= K;++i)
scanf("%d%d", &a, &b), P[a] = b, P[b] = a;
for (int a = 1;a <= N;++a)
for (int b = a;b <= N;++b)
Cnt[a][b] = Cnt[a][b - 1] + (P[b] == 0);
H[0] = 1;
for (int n = 2;n <= N;n += 2)
H[n] = Mult(n - 1, H[n - 2]);
for (int ad = 1;ad <= N;++ad)
for (int i = 1, j = ad;j <= N;++i, ++j)
{
for (int k = i;k <= j;++k)
if (P[k] && (P[k] < i || P[k] > j))
goto loop;
F[i][j] = H[Cnt[i][j]];
for (int k = i + 1;k <= j - 1;++k)
inc(F[i][j], Mult(F[i][k], H[Cnt[k + 1][j]]));
loop :
;
}
int Ans = 0;
for (int i = 1;i <= N;++i)
for (int j = i + 1;j <= N;++j)
upd(Ans, Mult(F[i][j], H[(N / 2 - K) * 2 - Cnt[i][j]]));
printf("%d", Ans);
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
namespace TYC
{
typedef long long ll;
const int N = 605, mod = 1e9 + 7;
int n, K, A[N], B[N], Mark[N], Cnt[N][N], G[N], F[N][N];
inline bool check(const int l, const int r)
{
for (int i = 1; i <= K; i++)
if ((l <= A[i] && A[i] <= r) != (l <= B[i] && B[i] <= r))
return true;
return false;
}
void work()
{
scanf("%d%d", &n, &K);
n <<= 1;
for (int i = 1; i <= K; i++)
{
scanf("%d%d", &A[i], &B[i]);
if (A[i] > B[i])
swap(A[i], B[i]);
Mark[A[i]] = Mark[B[i]] = 1;
}
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++)
for (int i = l; i <= r; i++)
Cnt[l][r] += !Mark[i];
G[0] = 1;
for (int i = 2; i <= n; i += 2)
G[i] = int((ll)(i - 1) * G[i - 2] % mod);
int ans = 0;
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++)
if (!check(l, r))
{
F[l][r] = G[Cnt[l][r]];
for (int p = l; p < r; p++)
F[l][r] = int((F[l][r] - (ll)F[l][p] * G[Cnt[p + 1][r]] % mod + mod) % mod);
ans = int((ans + (ll)F[l][r] * G[Cnt[1][n] - Cnt[l][r]] % mod) % mod);
}
printf("%d\n", ans);
}
}
int main()
{
TYC::work();
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;
namespace TYC
{
typedef long long ll;
const int N = 605, mod = 1e9 + 7;
int n, K, A[N], B[N], Mark[N], Cnt[N][N], G[N], F[N][N];
inline bool check(const int l, const int r)
{
for (int i = 1; i <= K; i++)
if ((l <= A[i] && A[i] <= r) != (l <= B[i] && B[i] <= r))
return true;
return false;
}
void work()
{
scanf("%d%d", &n, &K);
n <<= 1;
for (int i = 1; i <= K; i++)
{
scanf("%d%d", &A[i], &B[i]);
if (A[i] > B[i])
swap(A[i], B[i]);
Mark[A[i]] = Mark[B[i]] = 1;
}
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++)
for (int i = l; i <= r; i++)
Cnt[l][r] += !Mark[i];
G[0] = 1;
for (int i = 2; i <= n; i += 2)
G[i] = int((ll)(i - 1) * G[i - 2] % mod);
int ans = 0;
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++)
if (!check(l, r))
{
F[l][r] = G[Cnt[l][r]];
for (int p = l; p < r; p++)
F[l][r] = int((F[l][r] - (ll)F[l][p] * G[Cnt[p + 1][r]] % mod + mod) % mod);
ans = int((ans + (ll)F[l][r] * G[Cnt[1][n] - Cnt[l][r]] % mod) % mod);
}
printf("%d\n", ans);
}
}
int main()
{
TYC::work();
return 0;
}
``` |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=(0);i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
template<class T> inline void read(T &x){
int f=0;x=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
if(f)x=-x;
}
const int N=605,mod=1e9+7;
int f[N][N],g[N][N],c[N][N],mn[N][N],mx[N][N],h[N],L[N],R[N];
int n,m,a,b,ans;
int main(){
read(n),read(m),n*=2;
h[0]=1;
rep(i,1,n)h[i]=(ll)h[i-1]*(2*i-1)%mod;
memset(mn,0x3f,sizeof mn);
rep(i,1,m){
read(a),read(b);
if(a>b)swap(a,b);
R[a]=b,L[b]=a;
mx[a][a]=b,mn[b][b]=a;
}
rep(i,1,n+1){
if(!L[i]&&!R[i])c[i][i]=1;
g[i][i-1]=1;
}
per(l,n,1)rep(r,l+1,n){
c[l][r]=c[l][r-1]+c[r][r];
mn[l][r]=min(mn[l][r-1],mn[r][r]);
mx[l][r]=max(mx[l][r-1],mx[r][r]);
if((r-l+1)%2||mn[l][r]<l||mx[l][r]>r)continue;
f[l][r]=g[l][r]=h[c[l][r]/2];
rep(k,l+1,r-1)
f[l][r]=(f[l][r]+mod-(ll)f[l][k]*g[k+1][r]%mod)%mod;
ans=(ans+(ll)f[l][r]*h[(n-2*m-c[l][r])/2])%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>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=(0);i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
template<class T> inline void read(T &x){
int f=0;x=0;char ch=getchar();
for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
if(f)x=-x;
}
const int N=605,mod=1e9+7;
int f[N][N],g[N][N],c[N][N],mn[N][N],mx[N][N],h[N],L[N],R[N];
int n,m,a,b,ans;
int main(){
read(n),read(m),n*=2;
h[0]=1;
rep(i,1,n)h[i]=(ll)h[i-1]*(2*i-1)%mod;
memset(mn,0x3f,sizeof mn);
rep(i,1,m){
read(a),read(b);
if(a>b)swap(a,b);
R[a]=b,L[b]=a;
mx[a][a]=b,mn[b][b]=a;
}
rep(i,1,n+1){
if(!L[i]&&!R[i])c[i][i]=1;
g[i][i-1]=1;
}
per(l,n,1)rep(r,l+1,n){
c[l][r]=c[l][r-1]+c[r][r];
mn[l][r]=min(mn[l][r-1],mn[r][r]);
mx[l][r]=max(mx[l][r-1],mx[r][r]);
if((r-l+1)%2||mn[l][r]<l||mx[l][r]>r)continue;
f[l][r]=g[l][r]=h[c[l][r]/2];
rep(k,l+1,r-1)
f[l][r]=(f[l][r]+mod-(ll)f[l][k]*g[k+1][r]%mod)%mod;
ans=(ans+(ll)f[l][r]*h[(n-2*m-c[l][r])/2])%mod;
}
cout<<ans<<endl;
return 0;
}
``` |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define mod 1000000007
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//-------------------------------------------------------
int Pair[600];
long C[601];
long DP[600][600];
void Calc(){
int N = rei()*2;
int K = rei();
for(int i=0;i<N;i++){
Pair[i] = -1;
}
for(int i=0;i<K;i++){
int f = rei()-1;
int t = rei()-1;
Pair[f] = t;
Pair[t] = f;
}
C[0] = 1;
for(int i=2;i<=N;i++){
C[i] = C[i-2] * (i-1) % mod;
}
long ans = C[N-2*K];
for(int i=1;i<N;i++){
for(int j=i+1;j<N;j++){
bool OK = true;
int c1 = 0;
int c2 = 0;
for(int k=i;k<=j;k++){
if(Pair[k] == -1){
c1++;
}
else{
OK &= i <= Pair[k] && Pair[k] <= j;
}
}
if(!OK){
continue;
}
c2 = N-2*K-c1;
long d = C[c1];
for(int k=i;k<j;k++){
if(Pair[k] == -1){
c1--;
}
d += mod - DP[i][k] * C[c1] % mod;
}
d %= mod;
ans += d * C[c2] % mod;
DP[i][j] = d;
}
}
ans %= mod;
cout << ans << endl;
}
int main(int argc,char** argv){
ios::sync_with_stdio(false), cin.tie(0);
cout.tie(0); Calc(); 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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define mod 1000000007
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//-------------------------------------------------------
int Pair[600];
long C[601];
long DP[600][600];
void Calc(){
int N = rei()*2;
int K = rei();
for(int i=0;i<N;i++){
Pair[i] = -1;
}
for(int i=0;i<K;i++){
int f = rei()-1;
int t = rei()-1;
Pair[f] = t;
Pair[t] = f;
}
C[0] = 1;
for(int i=2;i<=N;i++){
C[i] = C[i-2] * (i-1) % mod;
}
long ans = C[N-2*K];
for(int i=1;i<N;i++){
for(int j=i+1;j<N;j++){
bool OK = true;
int c1 = 0;
int c2 = 0;
for(int k=i;k<=j;k++){
if(Pair[k] == -1){
c1++;
}
else{
OK &= i <= Pair[k] && Pair[k] <= j;
}
}
if(!OK){
continue;
}
c2 = N-2*K-c1;
long d = C[c1];
for(int k=i;k<j;k++){
if(Pair[k] == -1){
c1--;
}
d += mod - DP[i][k] * C[c1] % mod;
}
d %= mod;
ans += d * C[c2] % mod;
DP[i][j] = d;
}
}
ans %= mod;
cout << ans << endl;
}
int main(int argc,char** argv){
ios::sync_with_stdio(false), cin.tie(0);
cout.tie(0); Calc(); return 0;
}
``` |
#include<bits/stdc++.h>
#define maxn 610
#define ll long long
#define mod 1000000007
using namespace std;
int f[maxn][maxn], pr[maxn], tot[maxn], pre[maxn], g[maxn][maxn];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
n *= 2;
for (int i = 1; i <= m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
pr[x] = y; pr[y] = x;
}
for (int i = 1; i <= n; i++)
if (pr[i] == 0) pre[i] = pre[i - 1] + 1;
else pre[i] = pre[i - 1];
for (int i = 1; i <= n - 1; i++)
if ((pr[i] || pr[i + 1]) && pr[i] != i + 1) f[i][i + 1] = 0;
else f[i][i + 1] = g[i][i + 1] = 1;
tot[0] = 1;
for (int i = 1; i <= n; i++)
if (i % 2) tot[i] = 0;
else tot[i] = ((ll)i - 1) * tot[i - 2] % mod;
for (int len = 4; len <= n; len += 2)
for (int i = 1; i <= n - len + 1; i++)
{
int j = i + len - 1, flag = 1;
for (int k = i; k <= j; k++)
if (pr[k] && (pr[k] < i || pr[k] > j)) flag = 0;
if (flag == 0) continue;
f[i][j] = g[i][j] = tot[pre[j] - pre[i - 1]];
for (int k = i + 1; k < j; k += 2)
f[i][j] = (f[i][j] - (ll)f[i][k] * g[k + 1][j] % mod + mod) % mod;
}
int res = 0;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j += 2)
{
if (f[i][j] == 0) continue;
int tmp = pre[n] - (pre[j] - pre[i - 1]);
res = (res + (ll)f[i][j] * tot[tmp] % mod) % mod;
}
printf("%d\n", res);
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 maxn 610
#define ll long long
#define mod 1000000007
using namespace std;
int f[maxn][maxn], pr[maxn], tot[maxn], pre[maxn], g[maxn][maxn];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
n *= 2;
for (int i = 1; i <= m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
pr[x] = y; pr[y] = x;
}
for (int i = 1; i <= n; i++)
if (pr[i] == 0) pre[i] = pre[i - 1] + 1;
else pre[i] = pre[i - 1];
for (int i = 1; i <= n - 1; i++)
if ((pr[i] || pr[i + 1]) && pr[i] != i + 1) f[i][i + 1] = 0;
else f[i][i + 1] = g[i][i + 1] = 1;
tot[0] = 1;
for (int i = 1; i <= n; i++)
if (i % 2) tot[i] = 0;
else tot[i] = ((ll)i - 1) * tot[i - 2] % mod;
for (int len = 4; len <= n; len += 2)
for (int i = 1; i <= n - len + 1; i++)
{
int j = i + len - 1, flag = 1;
for (int k = i; k <= j; k++)
if (pr[k] && (pr[k] < i || pr[k] > j)) flag = 0;
if (flag == 0) continue;
f[i][j] = g[i][j] = tot[pre[j] - pre[i - 1]];
for (int k = i + 1; k < j; k += 2)
f[i][j] = (f[i][j] - (ll)f[i][k] * g[k + 1][j] % mod + mod) % mod;
}
int res = 0;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j += 2)
{
if (f[i][j] == 0) continue;
int tmp = pre[n] - (pre[j] - pre[i - 1]);
res = (res + (ll)f[i][j] * tot[tmp] % mod) % mod;
}
printf("%d\n", res);
return 0;
}
``` |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
Int dp[601][601];
Int rp[601][601];
Int lp[601][601];
Int np[601][601];
Int ff[601];
Int p[601];
Int n, k, a, b, K;
int main(){
ff[0] = ff[1] = 1;
for(int i = 2;i <= 600;i++)ff[i] = (ff[i-2] * (i-1)) % MOD;
cin >> n >> K;
for(int i = 0;i < K;i++){
cin >> a >> b;
p[a] = b;
p[b] = a;
}
for(int i = 1;i <= 2*n;i++){
for(int j = i;j <= 2*n;j++){
if(i == j){
if(p[i] == 0){
rp[i][j] = -INF;
lp[i][j] = INF;
np[i][j] = 1;
}
else{
np[i][j] = 0;
rp[i][j] = lp[i][j] = p[i];
}
}
else{
if(p[j] == 0){
rp[i][j] = rp[i][j-1];
lp[i][j] = lp[i][j-1];
np[i][j] = np[i][j-1]+1;
}
else{
rp[i][j] = max(rp[i][j-1], p[j]);
lp[i][j] = min(lp[i][j-1], p[j]);
np[i][j] = np[i][j-1];
}
}
}
}
Int res=0;
for(int i = 1;i <= 2*n;i++){
for(int j = i;j <= 2*n;j++){
// cout << i << " " << j << " "<< rp[i][j] << " "<<lp[i][j] << " " << np[i][j] << endl;
if(rp[i][j] > j || lp[i][j] < i || np[i][j] %2 == 1){
dp[i][j] = 0;
continue;
}
dp[i][j] = ff[np[i][j]];
for(int k = i;k < j;k++){
dp[i][j] -= dp[i][k] * ff[np[k+1][j]] %MOD;
dp[i][j] %= MOD;
}
// cout << i << " " << j << " " <<dp[i][j] << endl;
//cout << dp[i][j] << endl;
res += dp[i][j] * ff[(n-K)*2-np[i][j]]%MOD;
res %= MOD;
}
}
if(res < 0)res+=MOD;
cout << res << 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<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
Int dp[601][601];
Int rp[601][601];
Int lp[601][601];
Int np[601][601];
Int ff[601];
Int p[601];
Int n, k, a, b, K;
int main(){
ff[0] = ff[1] = 1;
for(int i = 2;i <= 600;i++)ff[i] = (ff[i-2] * (i-1)) % MOD;
cin >> n >> K;
for(int i = 0;i < K;i++){
cin >> a >> b;
p[a] = b;
p[b] = a;
}
for(int i = 1;i <= 2*n;i++){
for(int j = i;j <= 2*n;j++){
if(i == j){
if(p[i] == 0){
rp[i][j] = -INF;
lp[i][j] = INF;
np[i][j] = 1;
}
else{
np[i][j] = 0;
rp[i][j] = lp[i][j] = p[i];
}
}
else{
if(p[j] == 0){
rp[i][j] = rp[i][j-1];
lp[i][j] = lp[i][j-1];
np[i][j] = np[i][j-1]+1;
}
else{
rp[i][j] = max(rp[i][j-1], p[j]);
lp[i][j] = min(lp[i][j-1], p[j]);
np[i][j] = np[i][j-1];
}
}
}
}
Int res=0;
for(int i = 1;i <= 2*n;i++){
for(int j = i;j <= 2*n;j++){
// cout << i << " " << j << " "<< rp[i][j] << " "<<lp[i][j] << " " << np[i][j] << endl;
if(rp[i][j] > j || lp[i][j] < i || np[i][j] %2 == 1){
dp[i][j] = 0;
continue;
}
dp[i][j] = ff[np[i][j]];
for(int k = i;k < j;k++){
dp[i][j] -= dp[i][k] * ff[np[k+1][j]] %MOD;
dp[i][j] %= MOD;
}
// cout << i << " " << j << " " <<dp[i][j] << endl;
//cout << dp[i][j] << endl;
res += dp[i][j] * ff[(n-K)*2-np[i][j]]%MOD;
res %= MOD;
}
}
if(res < 0)res+=MOD;
cout << res << endl;
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
const int md = 1e9 + 7;
int add(int x, int y) {
x += y;
if (x >= md) {
x -= md;
}
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0) {
x += md;
}
return x;
}
int mul(int x, int y) {
return (long long)x * y % md;
}
int main() {
#ifdef wxh010910
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
for (int i = 0; i < m; ++i) {
cin >> a[i] >> b[i];
--a[i];
--b[i];
}
vector<int> ways(n + 1);
ways[0] = 1;
for (int i = 1; i <= n; ++i) {
ways[i] = mul(ways[i - 1], (i << 1) - 1);
}
vector<vector<int>> f(n << 1, vector<int> (n << 1));
vector<vector<int>> g(n << 1, vector<int> (n << 1));
int answer = 0;
for (int l = (n << 1) - 1; ~l; --l) {
for (int r = l + 1; r < n << 1; r += 2) {
bool flag = true;
int inside = 0;
for (int i = 0; i < m; ++i) {
int type = (a[i] >= l && a[i] <= r) + (b[i] >= l && b[i] <= r);
if (type == 1) {
flag = false;
break;
}
if (type) {
++inside;
}
}
if (flag) {
inside = (r - l + 1 >> 1) - inside;
int outside = n - m - inside;
f[l][r] = g[l][r] = ways[inside];
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]));
}
answer = add(answer, mul(f[l][r], ways[outside]));
}
}
}
cout << answer << 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 int md = 1e9 + 7;
int add(int x, int y) {
x += y;
if (x >= md) {
x -= md;
}
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0) {
x += md;
}
return x;
}
int mul(int x, int y) {
return (long long)x * y % md;
}
int main() {
#ifdef wxh010910
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
for (int i = 0; i < m; ++i) {
cin >> a[i] >> b[i];
--a[i];
--b[i];
}
vector<int> ways(n + 1);
ways[0] = 1;
for (int i = 1; i <= n; ++i) {
ways[i] = mul(ways[i - 1], (i << 1) - 1);
}
vector<vector<int>> f(n << 1, vector<int> (n << 1));
vector<vector<int>> g(n << 1, vector<int> (n << 1));
int answer = 0;
for (int l = (n << 1) - 1; ~l; --l) {
for (int r = l + 1; r < n << 1; r += 2) {
bool flag = true;
int inside = 0;
for (int i = 0; i < m; ++i) {
int type = (a[i] >= l && a[i] <= r) + (b[i] >= l && b[i] <= r);
if (type == 1) {
flag = false;
break;
}
if (type) {
++inside;
}
}
if (flag) {
inside = (r - l + 1 >> 1) - inside;
int outside = n - m - inside;
f[l][r] = g[l][r] = ways[inside];
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]));
}
answer = add(answer, mul(f[l][r], ways[outside]));
}
}
}
cout << answer << endl;
return 0;
}
``` |
/****************************************************************
* Author: huhao
* Email: [email protected]
* Create time: 2020-02-17 19:37:35
****************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define fr(i,a,b) for(int i=(a),end_##i=(b);i<=end_##i;i++)
#define fd(i,a,b) for(int i=(a),end_##i=(b);i>=end_##i;i--)
int read()
{
int r=0,t=1,c=getchar();
while(c<'0'||c>'9')
{
t=c=='-'?-1:1;
c=getchar();
}
while(c>='0'&&c<='9')
{
r=(r<<3)+(r<<1)+(c^48);
c=getchar();
}
return r*t;
}
#define i64 long long
const int N=610,mod=1000000007;
i64 n,k,t[N],f[N][N],g[N][N],ans,s[N];
int main()
{
n=read()*2; k=read();
fr(i,1,k){ int a=read(),b=read(); t[a]=b; t[b]=a; }
s[0]=1;
fr(i,1,n/2) s[i*2]=s[(i-1)*2]*(i*2-1)%mod;
fd(i,n,1) fr(j,i+1,n)
{
int l=j-i+1;
fr(k,i,j) if(t[k])
{
if(t[k]<i||t[k]>j) l=-1;
if(l>=0) l--;
}
if(l==-1||l%2) continue;
f[i][j]=s[l]; g[i][j]=l;
fr(k,i,j-1) f[i][j]=(f[i][j]-f[i][k]*s[g[k+1][j]]%mod+mod)%mod;
}
// fr(i,1,n) fr(j,1,n) printf("%d%c",int(f[i][j]),j==n?'\n':' ');
fr(i,1,n) fr(j,i,n) ans+=f[i][j]*s[n-(j-i+1)-(k-(j-i+1-g[i][j])/2)*2]%mod;
printf("%d\n",int(ans%mod));
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
/****************************************************************
* Author: huhao
* Email: [email protected]
* Create time: 2020-02-17 19:37:35
****************************************************************/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define fr(i,a,b) for(int i=(a),end_##i=(b);i<=end_##i;i++)
#define fd(i,a,b) for(int i=(a),end_##i=(b);i>=end_##i;i--)
int read()
{
int r=0,t=1,c=getchar();
while(c<'0'||c>'9')
{
t=c=='-'?-1:1;
c=getchar();
}
while(c>='0'&&c<='9')
{
r=(r<<3)+(r<<1)+(c^48);
c=getchar();
}
return r*t;
}
#define i64 long long
const int N=610,mod=1000000007;
i64 n,k,t[N],f[N][N],g[N][N],ans,s[N];
int main()
{
n=read()*2; k=read();
fr(i,1,k){ int a=read(),b=read(); t[a]=b; t[b]=a; }
s[0]=1;
fr(i,1,n/2) s[i*2]=s[(i-1)*2]*(i*2-1)%mod;
fd(i,n,1) fr(j,i+1,n)
{
int l=j-i+1;
fr(k,i,j) if(t[k])
{
if(t[k]<i||t[k]>j) l=-1;
if(l>=0) l--;
}
if(l==-1||l%2) continue;
f[i][j]=s[l]; g[i][j]=l;
fr(k,i,j-1) f[i][j]=(f[i][j]-f[i][k]*s[g[k+1][j]]%mod+mod)%mod;
}
// fr(i,1,n) fr(j,1,n) printf("%d%c",int(f[i][j]),j==n?'\n':' ');
fr(i,1,n) fr(j,i,n) ans+=f[i][j]*s[n-(j-i+1)-(k-(j-i+1-g[i][j])/2)*2]%mod;
printf("%d\n",int(ans%mod));
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string a;
cin >> a;
sort(a.begin(),a.end());
if(a == "abc")
cout << "Yes" << endl;
else
cout << "No" << endl;
}
| ### Prompt
Construct a CPP code solution to the problem outlined:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string a;
cin >> a;
sort(a.begin(),a.end());
if(a == "abc")
cout << "Yes" << endl;
else
cout << "No" << endl;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
if(s[0]!=s[1]&&s[1]!=s[2]&&s[2]!=s[0]){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
} | ### Prompt
Please create a solution in Cpp to the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
if(s[0]!=s[1]&&s[1]!=s[2]&&s[2]!=s[0]){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a,b,c;
cin>>a>>b>>c;
if(a!=b&&b!=c&&c!=a){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
} | ### Prompt
Create a solution in cpp for the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
char a,b,c;
cin>>a>>b>>c;
if(a!=b&&b!=c&&c!=a){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
sort(s.begin(),s.end());
s == "abc" ? cout << "Yes" << endl : cout << "No" << endl;
}
| ### Prompt
In cpp, your task is to solve the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
sort(s.begin(),s.end());
s == "abc" ? cout << "Yes" << endl : cout << "No" << endl;
}
``` |
#include <stdio.h>
int main()
{
char a,b,c;
scanf("%c%c%c",&a,&b,&c);
if(a+b+c==294)
printf("Yes\n");
else
printf("No\n");
return 0;
}
| ### Prompt
Please formulate a CPP solution to the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <stdio.h>
int main()
{
char a,b,c;
scanf("%c%c%c",&a,&b,&c);
if(a+b+c==294)
printf("Yes\n");
else
printf("No\n");
return 0;
}
``` |
#include<stdio.h>
int main(void)
{
char s[4];
scanf("%s",s);
if((s[0] != s[1]) && (s[1] != s[2]) && (s[0]!= s[2]))
printf("Yes\n");
else printf("No\n");
return 0;
}
| ### Prompt
Please create a solution in cpp to the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<stdio.h>
int main(void)
{
char s[4];
scanf("%s",s);
if((s[0] != s[1]) && (s[1] != s[2]) && (s[0]!= s[2]))
printf("Yes\n");
else printf("No\n");
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N; string s;
cin >> s;
sort(s.begin(), s.end());
if(s=="abc") puts("Yes");
else puts("No");
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N; string s;
cin >> s;
sort(s.begin(), s.end());
if(s=="abc") puts("Yes");
else puts("No");
}
``` |
#include <iostream>
using namespace std;
string s;
int main() {
cin>>s;
cout<<(s[0]==s[1] || s[1]==s[2] || s[2]==s[0] ? "No":"Yes");
return 0;
} | ### Prompt
Create a solution in Cpp for the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <iostream>
using namespace std;
string s;
int main() {
cin>>s;
cout<<(s[0]==s[1] || s[1]==s[2] || s[2]==s[0] ? "No":"Yes");
return 0;
}
``` |
#import<ios>
int main(){printf(getchar()-96^getchar()-96^getchar()-96?"No":"Yes");} | ### Prompt
In CPP, your task is to solve the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#import<ios>
int main(){printf(getchar()-96^getchar()-96^getchar()-96?"No":"Yes");}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
if(s[0]*s[1]*s[2]==97*98*99) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
} | ### Prompt
Please formulate a cpp solution to the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
if(s[0]*s[1]*s[2]==97*98*99) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
``` |
//Created by Young Charlie
#include <bits/stdc++.h>
using namespace std;
int main() {
string a; cin>>a;
sort(a.begin(), a.end());
cout<<(a == "abc" ? "Yes\n" : "No\n");
}
| ### Prompt
Please provide a Cpp coded solution to the problem described below:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
//Created by Young Charlie
#include <bits/stdc++.h>
using namespace std;
int main() {
string a; cin>>a;
sort(a.begin(), a.end());
cout<<(a == "abc" ? "Yes\n" : "No\n");
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int a=s[0],b=s[1],c=s[2];
if(a+b+c==294) cout<<"Yes\n";
else cout<<"No\n";
return 0;
} | ### Prompt
Construct a CPP code solution to the problem outlined:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int a=s[0],b=s[1],c=s[2];
if(a+b+c==294) cout<<"Yes\n";
else cout<<"No\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(){
string S;
cin>>S;
cout << (S[0]!=S[1] && S[1]!=S[2] && S[2]!=S[0]?"Yes":"No");
return 0;
} | ### Prompt
In CPP, your task is to solve the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
string S;
cin>>S;
cout << (S[0]!=S[1] && S[1]!=S[2] && S[2]!=S[0]?"Yes":"No");
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
if(s[0]==s[1]||s[1]==s[2]||s[0]==s[2]){
cout<<"No"<<endl;
return 0;
}
cout<<"Yes"<<endl;
return 0;
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
if(s[0]==s[1]||s[1]==s[2]||s[0]==s[2]){
cout<<"No"<<endl;
return 0;
}
cout<<"Yes"<<endl;
return 0;
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
if(s[0]!=s[1]&&s[1]!=s[2]&&s[2]!=s[0])
puts("Yes");
else
puts("No");
} | ### Prompt
Please provide a CPP coded solution to the problem described below:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
if(s[0]!=s[1]&&s[1]!=s[2]&&s[2]!=s[0])
puts("Yes");
else
puts("No");
}
``` |
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
char c[3];
cin>>c[0]>>c[1]>>c[2];
sort(c,c+3);
if(c[0]=='a'&&c[1]=='b'&&c[2]=='c')
cout<<"Yes";
else
cout<<"No";
} | ### Prompt
Create a solution in Cpp for the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
char c[3];
cin>>c[0]>>c[1]>>c[2];
sort(c,c+3);
if(c[0]=='a'&&c[1]=='b'&&c[2]=='c')
cout<<"Yes";
else
cout<<"No";
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;cin>>s;
if(s[0]>s[1])swap(s[0],s[1]);
if(s=="abc"||s=="acb"||s=="bca")cout<<"Yes\n";
else cout<<"No\n";
return 0;
} | ### Prompt
Please provide a cpp coded solution to the problem described below:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;cin>>s;
if(s[0]>s[1])swap(s[0],s[1]);
if(s=="abc"||s=="acb"||s=="bca")cout<<"Yes\n";
else cout<<"No\n";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin >> s;
if (s[0]!=s[1]&&s[0]!=s[2]&&s[1]!=s[2]) cout << "Yes";
else cout << "No";
}
| ### Prompt
In Cpp, your task is to solve the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin >> s;
if (s[0]!=s[1]&&s[0]!=s[2]&&s[1]!=s[2]) cout << "Yes";
else cout << "No";
}
``` |
#include <iostream>
using namespace std;
int main()
{
string s; cin >> s;
if(s[0] != s[1] && s[0] != s[2] && s[1] != s[2]) cout << "Yes";
else cout << "No";
return 0;
} | ### Prompt
Construct a Cpp code solution to the problem outlined:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <iostream>
using namespace std;
int main()
{
string s; cin >> s;
if(s[0] != s[1] && s[0] != s[2] && s[1] != s[2]) cout << "Yes";
else cout << "No";
return 0;
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s1,s2,s3;
cin >> s1 >> s2 >> s3;
if(s1!=s2&&s1!=s3&&s2!=s3) cout << "Yes" << endl;
else cout << "No" << endl;
} | ### Prompt
Your challenge is to write a CPP solution to the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
char s1,s2,s3;
cin >> s1 >> s2 >> s3;
if(s1!=s2&&s1!=s3&&s2!=s3) cout << "Yes" << endl;
else cout << "No" << endl;
}
``` |
#include<stdio.h>
int main()
{
char s[10];
scanf("%s", s);
if (s[0] != s[1] && s[1] != s[2] && s[2] != s[0])printf("Yes\n");
else printf("No\n");
}
| ### Prompt
Your challenge is to write a CPP solution to the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<stdio.h>
int main()
{
char s[10];
scanf("%s", s);
if (s[0] != s[1] && s[1] != s[2] && s[2] != s[0])printf("Yes\n");
else printf("No\n");
}
``` |
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin>>S;
if(S[1]!=S[2] &&S[2]!=S[0] && S[0]!=S[1]){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
} | ### Prompt
Create a solution in cpp for the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin>>S;
if(S[1]!=S[2] &&S[2]!=S[0] && S[0]!=S[1]){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
``` |
#include<bits/stdc++.h>
using namespace std;
int main(){
string S; cin>>S;
sort(S.begin(),S.end());
string ans="No";
if(S=="abc") ans="Yes";
cout<<ans<<endl;
} | ### Prompt
Develop a solution in Cpp to the problem described below:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
string S; cin>>S;
sort(S.begin(),S.end());
string ans="No";
if(S=="abc") ans="Yes";
cout<<ans<<endl;
}
``` |
#include<iostream>
#include<string>
int main(){
std::string S;
std::cin >> S;
if(S[0] != S[1] && S[2] != S[1] && S[2] != S[0]) std::cout<<"Yes";
else std::cout<<"No";
} | ### Prompt
Please provide a Cpp coded solution to the problem described below:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<iostream>
#include<string>
int main(){
std::string S;
std::cin >> S;
if(S[0] != S[1] && S[2] != S[1] && S[2] != S[0]) std::cout<<"Yes";
else std::cout<<"No";
}
``` |
#include <cstdio>
using namespace std;
int main() {
char a,b,c,d,e;
scanf("%c%c%c", &a, &b, &c);
if(a==b || b==c || c==a){
printf("No");
}
else{
printf("Yes");
}
}
| ### Prompt
Construct a Cpp code solution to the problem outlined:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include <cstdio>
using namespace std;
int main() {
char a,b,c,d,e;
scanf("%c%c%c", &a, &b, &c);
if(a==b || b==c || c==a){
printf("No");
}
else{
printf("Yes");
}
}
``` |
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
char a,b,c;cin>>a>>b>>c;if(a==b || b==c || c==a)cout<<"No"<<endl;else cout<<"Yes"<<endl;
} | ### Prompt
Please formulate a cpp solution to the following problem:
You are given a string S of length 3 consisting of `a`, `b` and `c`. Determine if S can be obtained by permuting `abc`.
Constraints
* |S|=3
* S consists of `a`, `b` and `c`.
Input
Input is given from Standard Input in the following format:
S
Output
If S can be obtained by permuting `abc`, print `Yes`; otherwise, print `No`.
Examples
Input
bac
Output
Yes
Input
bab
Output
No
Input
abc
Output
Yes
Input
aaa
Output
No
### Response
```cpp
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
char a,b,c;cin>>a>>b>>c;if(a==b || b==c || c==a)cout<<"No"<<endl;else cout<<"Yes"<<endl;
}
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.