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>
#pragma GCC optimize("O3")
using namespace std;
/*ifstream fin("num.in");
ofstream fout("num.out");*/
typedef long long ll;
const ll mod=1e9+7;
ll n,m,dp[2005][2005],ans,fact[10005],invfact[10005],nr[10005];
ll pw(ll a, ll b)
{
ll rez=1;
while(b)
{
if(b&1)
rez=(rez*a)%mod;
b/=2;
a=(a*a)%mod;
}
return rez;
}
ll inv(ll a)
{
return pw(a,mod-2);
}
ll comb(ll a, ll b)
{
if(a<b)
return 0;
ll rez=(fact[a]*invfact[b])%mod;
rez=(rez*invfact[a-b])%mod;
return rez;
}
ll aranj(ll a, ll b)
{
ll rez=comb(a,b);
rez=(rez*fact[b])%mod;
return rez;
}
int main()
{
fact[0]=invfact[0]=1;
for(ll i=1;i<=10000;i++)
{
fact[i]=(fact[i-1]*i)%mod;
invfact[i]=inv(fact[i])%mod;
}
cin>>n>>m;
for(int i=1;i<=2*n;i++)
nr[i%m+1]++;
dp[0][0]=1;
int suma=0;
for(int i=1;i<=m;i++)
{
suma+=nr[i];
for(int cur=0;cur<=nr[i]/2;cur++)
{
ll x=aranj(nr[i],cur*2);
for(int tot=cur;tot<=suma/2;tot++)
{
ll val=dp[i-1][tot-cur];
val=(val*x)%mod;
val=(val*comb(n-tot+cur,cur))%mod;
dp[i][tot]=(dp[i][tot]+val)%mod;
}
}
//cout<<dp[i][0]<<' ';
}
for(int i=0;i<=n;i++)
{
ll val=dp[m][i]%mod;
val=(val*fact[2*n-2*i])%mod;
if(i%2==1)
{
ans-=val;
if(ans<0)
ans+=mod;
}
else
ans=(ans+val)%mod;
}
cout<<ans;
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... |