您的位置:首页 > 产品设计 > UI/UE

Codeforces 272D Dima and Two Sequences【思维+模拟】

2017-09-20 15:04 645 查看
D. Dima and Two Sequences

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input

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 nintegers 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).

Output

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

Examples

input
1
1
2
7


output
1


input
21 22 3
11


output
2


Note

In the first sample you can get only one sequence: (1, 1), (2, 1).

In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2).
Thus, the answer is 2.

题目大意:

给出N个数对(a1,1),(a2,1),(a3,1)....................(an,1);

再给出N个数对(b1,1),(b2,1),(b3,1)....................(bn,1);

我们需要将这2*N个数对,按照第一个元素从小到大排序,问我们有多少种不同的排列方式。结果模m;

思路:

①首先我们将序列按照第一个元素从小到大排序,如果有相同,那么按照第二个元素从小到大排序。

②我们知道,Ans=ans(1,x)+ans(2,x)+ans(3,x)+.............这里ans(1,x)表示数对第一个元素为1的排列方式。

根据高中知识有:ans(1,x)=A(n,n)/Z.这里n表示数对以1作为第一个元素的数对个数。Z=(1<=y<=n)πA(y,y),这里y表示数对(1,y)的个数。

排列组合的知识,并不难。

③但是我们知道这里涉及到的除法,我们不能直接用逆元求,因为m这个数值是不定的。所以我们考虑在式子上进行化简。

我们知道,A(y,y)的值要么是1,要么是2,因为我们知道,以数字y作为第二个元素出现的数对最多只有两个。

所以我们这里就可以暴力判定一下,然后化简式子了。

④口述相对比较简约,具体参考代码理解。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int x,y;
}c[350000];
#define ll __int64
int cmp(node a,node b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
int main()
{
int n,mod;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
int tmp;scanf("%d",&tmp);
c[i].x=tmp,c[i].y=i;
}
for(int i=1;i<=n;i++)
{
int tmp;scanf("%d",&tmp);
c[i+n].x=tmp,c[i+n].y=i;
}
scanf("%d",&mod);
n*=2;
sort(c+1,c+1+n,cmp);
queue<int>s;
for(int i=1;i<=n;i++)
{
int ss=i;
int ee=i;
while(ee<=n&&c[ee].x==c[ss].x)
{
ee++;
}
ee--;
int cnt=0;
for(int j=ss;j<ee;j++)
{
if(c[j].y==c[j+1].y&&c[j].x==c[j+1].x)cnt++;
}
for(int j=1;j<=ee-ss+1;j++)
{
if(j%2==0)
{
if(cnt>=1)
{
s.push(j/2);
cnt--;
}
else s.push(j);
}
else s.push(j);
}
i=ee;
}
ll output=1;
while(!s.empty())
{
ll u=s.front();s.pop();
output=output*u;
output%=mod;
}
printf("%I64d\n",output);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 272D