Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合
题目链接:
http://codeforces.com/problemset/problem/272/D
D. Dima and Two Sequences
memory limit per test256 megabytes
样例输出
1
题意
给两个长度为n的序列,让你把它们合成2*n的非递减序列,问有多少种可能(两个数完全相同当且仅当他们的值相等,且在a,b中出现的位置相同。
题解
就考虑下相同的数中有多少对数是完全相同的,然后总的阶乘除相同的对数,由于取模m比较特别,所以2不能直接求逆,需要单独把2拉出来考虑下。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=2e5+20;
LL fac[maxn],two[maxn];
int num[maxn];
int n,m;
VPII arr;
void init(){
clr(num,0);
fac[0]=fac[1]=1;
for(int i=2;i<maxn;i++){
int tmp=i;
while(tmp%2==0){
tmp/=2; num[i]++;
}
fac[i]=tmp*fac[i-1]%m;
num[i]+=num[i-1];
}
two[0]=1;
rep(i,1,maxn) two[i]=two[i-1]*2%m;
}
int main() {
scf("%d",&n);
rep(i,0,n){
int x; scf("%d",&x);
arr.pb(mkp(x,i+1));
}
rep(i,0,n){
int x; scf("%d",&x);
arr.pb(mkp(x,i+1));
}
scf("%d",&m);
init();
sort(all(arr));
LL ans=1;
rep(i,0,arr.sz()){
int ed=i;
while(ed<arr.sz()&&arr[ed].X==arr[i].X) ed++; ed--;
int cnt=0;
for(int j=i;j<ed;j++){
if(arr[j]==arr[j+1]) cnt++;
}
///这里有可能re
ans=ans*fac[ed-i+1]%m*two[num[ed-i+1]-cnt]%m;
i=ed;
}
prf("%I64d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------