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

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 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), ..., (pn, qn) and (x1, y1), (x2, y2), ..., (xn, yn) 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
2
1 2
2 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);
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值