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<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-----------------------------------------------------------------------
Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合的更多相关文章
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #167 (Div. 2)
C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...
- Codeforces Round #167 (Div. 1 + Div. 2)
C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...
- Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想
D. Dima and Lisa Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...
- Codeforces Round #208 (Div. 2) 358D Dima and Hares
题目链接:http://codeforces.com/problemset/problem/358/D 开始题意理解错,整个就跪了= = 题目大意:从1到n的位置取数,取数的得到值与周围的数有没有取过 ...
- Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #214 (Div. 2) C. Dima and Salad 背包
C. Dima and Salad Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...
- Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)
D. Dima and Lisa Dima loves representing an odd num ...
- Codeforces Round #208 (Div. 2) A.Dima and Continuous Line
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
随机推荐
- 【开学季】自学嵌入式开发|四核开发板|4412开发板|ARM+Android+linux技术
淘宝店铺:迅为开发板http://arm-board.taobao.com 网站:http://www.topeetboard.com QQ咨询:2551456065 电话咨询:010-5895758 ...
- B+树的特点
1.B+树是应文件系统产生的B树的变种.它依然是一颗多路查找树,与B树相比它的不同体现在: (1).如果非叶子节点包含n个关键码,则这个节点有n个子树. (2).非叶子节点仅包含关键码信息,叶子节点包 ...
- Java基本运算符
1.算术运算符 作用是数字的计算,包括:正号+,负号-,乘*,除/,余%,加+,减-,其算法与数学中的运算相同. 算术运算符实例(假设变量A=10,变量B=20): 操作符 描述 例子 + 正号 +A ...
- ADS(一)
LC匹配 已知输入输出阻抗,设计LC匹配电路使输入输出阻抗共轭匹配 Step1 Step2 Result1 Result2 微带线单枝匹配 已知源和负载阻抗,求微带线单枝匹配电路 Step1 ...
- javascript判断回文数
"回文"是指正读反读都能读通的句子,它是古今中外都有的一种修辞方式和文字游戏,如"我为人人,人人为我"等.在数学中也有这样一类数字有这样的特征,成为回文数(pa ...
- 谈谈自己对java的学习看法
从明天起,开始整理java的基础知识,进行巩固学习. 今天呢,谈谈自己的一点想法.很多人不知道java怎么学,学什么,有的是直接在网上找一些视频来看,不懂的地方到处跑群里问,结果效果并不是太好,怎么办 ...
- Java的文件读写操作 <转>
目录: file内存----输入流----程序----输出流----file内存 java中多种方式读文件 判断文件是否存在不存在创建文件 判断文件夹是否存在不存在创建文件夹 java 写文件的三种方 ...
- HTML5和css3的总结四
HTML5的新东西总结四: 1>video和audio 声明方法(可以制作背景音乐) var oV/oA=new Video()/Audio(); oV/oA.src=''; oV/oA.pla ...
- linux下的缓存机制及清理buffer/cache/swap的方法梳理
(1)缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果没有则通过驱动程序直接 ...
- FMDB 使用方法
优秀的第三方库,README 也是很优秀的,理解了 README,会对使用带来很多便利. ARC 和 MRC 项目中使用 ARC 还是 MRC,对使用 FMDB 都没有任何影响,FMDB 会在编译项目 ...