Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合

题目链接:

http://codeforces.com/problemset/problem/272/D

D. Dima and Two Sequences


time limit per test2 secondsmemory limit per test256 megabytes

问题描述

Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n).

Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence.

Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi).

As the answer can be rather large, print the remainder from dividing the answer by number m.

输入

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces.

The last line contains integer m (2 ≤ m ≤ 109 + 7).

输出

In the single line print the remainder after dividing the answer to the problem by number m.

样例输入

1
1
2
7

样例输出

1

题意

给两个长度为n的序列,让你把它们合成2*n的非递减序列,问有多少种可能(两个数完全相同当且仅当他们的值相等,且在a,b中出现的位置相同。

题解

就考虑下相同的数中有多少对数是完全相同的,然后总的阶乘除相同的对数,由于取模m比较特别,所以2不能直接求逆,需要单独把2拉出来考虑下。

代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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<<" = "< VI;
typedef pair PII;
typedef vector > 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

转载于:https://www.cnblogs.com/fenice/p/6018100.html

你可能感兴趣的:(数据结构与算法)