This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=5005,M=1e9+7;
int dp[N][N],n,m,a[N],b,fac[N],p2[N],inv[N];
bool vis[N][N];
int C(int n,int k){
if (n<0 || k<0 || n-k<0) return 0;
return fac[n]*inv[k]%M*inv[n-k]%M;
}
int pow(int x,int y){
if (!y) return 1;
int z=pow(x,y/2);
z=z*z%M;
if (y&1) z=z*x%M;
return z;
}
int rec(int i,int j){
if (i==b+1) return (j==0);
if (vis[i][j]) return dp[i][j];
vis[i][j]=1;
for (int k=0;k<=min(a[i]/2,j);k++){
dp[i][j]=(dp[i][j]+rec(i+1,j-k)*C(j,k)%M*C(a[i],2*k)%M*fac[2*k]%M)%M;
}return dp[i][j];
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
fac[0]=1;p2[0]=1;
for (int i=1;i<N;i++) p2[i]=p2[i-1]*2LL%M;
for (int i=1;i<N;i++) fac[i]=fac[i-1]*i%M;
inv[N-1]=pow(fac[N-1],M-2);
for (int i=N-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%M;
cin>>n>>m;
int x=2*n;
for (int i=1;i && x;i++){
b=i;
a[i]=min(x,2*n/m);
x-=a[i];
}
int ans=fac[2*n];
for (int i=1;i<=n;i++){
//cout<<rec(1,i)<<endl;
if (i&1) ans=(ans-rec(1,i)*C(n,i)%M*fac[2*n-2*i]%M+M)%M;
else ans=(ans+rec(1,i)*C(n,i)%M*fac[2*n-2*i]%M)%M;
}cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |