Codeforces 272D。Dima and Two Sequences,多重集的全排列
作者:互联网
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
Copy
1 1 2 7
output
Copy
1
input
Copy
2 1 2 2 3 11
output
Copy
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.
这个题一开始没看出来相同的最多只有两个然后就是我直接打表给我坑死了我想后来直接统计每个区间中重复的个数直接给他快速幂一除就好了呢结果是得把阶乘拆开一个个的算我佛了一上午就肛在这个地方了(中间吧map都用了本来以为是计数的问题结果不是)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <bits/stdc++.h>
#define MAXN 20
#define INF 0x3f3f3f3f
using namespace std;
long long m;
long long ji[200100];
void jie(int m)
{
ji[0]=ji[1]=1;
for(int i=2;i<=200000;i++)
ji[i]=i*ji[i-1]%m;
return ;
}
struct ju
{
int l,r;
}a[200220];
bool cmp(ju a,ju b)
{
return a.l<b.l;
}
long long kuais(int a,int b)
{
long long ans=1;
for(;b;b>>=1)
{
if(b&1)
ans=ans*a;
a*=a;
}
return ans;
}
int main()
{
int n;
while(cin>>n)
{
memset(a,0,sizeof(a));
long long x,ans=1;
for(int i=1;i<=n;i++)
{
cin>>x;
a[i].l=x,a[i].r=i;
}
for(int i=1;i<=n;i++)
{
cin>>x;
a[n+i].l=x,a[n+i].r=i;
}
cin>>m;
jie(m);
sort(a+1,a+1+2*n,cmp);
a[2*n+1].l=-1,a[2*n+1].r=-1;
map<int,int>jk;
jk.clear();
for(int i=1;i<=2*n;i++)
{
jk[a[i].r]++;
if(a[i].l!=a[i+1].l)
{
int sam=0,sum=0;
for(map<int,int>::iterator ll=jk.begin();ll!=jk.end();ll++)
{
sum+=ll->second;
//cout<<ll->first<<' '<<ll->second<<' '<<endl;
if(ll->second>=2)
{
sam++;
}
}
queue<int>sb;
for(int j=1;j<=sum;j++)
{
if(j%2==0)
{
if(sam)
{
sam--;
sb.push(j/2);
}
else
sb.push(j);
}
else
sb.push(j);
}
while(!sb.empty())
{
long long gh=sb.front();
sb.pop();
ans=ans*gh%m;
}
jk.clear();
}
}
cout<<(ans)%m<<endl;
}
}
标签:...,Dima,sequence,int,多重集,Codeforces,long,sequences,include 来源: https://blog.csdn.net/u013345179/article/details/100154583