HDU 6044 Limited Permutation 读入挂+组合数学
Limited Permutation
Given the positive integers n, (li,ri) (1≤i≤n), you are asked to calculate the number of possible permutations p1,p2,⋯,pn from 1 to n, meeting the above condition.
The answer may be very large, so you only need to give the value of answer modulo 109+7.
For each test case:
The first line contains one positive integer n, satisfying 1≤n≤106.
The second line contains n positive integers l1,l2,⋯,ln, satisfying 1≤li≤i for each 1≤i≤n.
The third line contains n positive integers r1,r2,⋯,rn, satisfying i≤ri≤n for each 1≤i≤n.
It's guaranteed that the sum of n in all test cases is not larger than 3⋅106.
Warm Tips for C/C++: input data is so large (about 38 MiB) that we recommend to use fread() for buffering friendly.
size_t fread(void *buffer, size_t size, size_t count, FILE *stream); // reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by buffer; the total number of elements successfully read is returned.
Case #2: 3
题解:
看懂题意
每个pi掌管 L ,R,题意是指超过这段范围就有比pi还要小的值
所有必然有一个pi 值掌管 1,n的,推出 必有 pj,pk分别 掌管 (1,i - 1), (i+1,n)
dfs下去计算方案
还有就是必须用读入挂才能过
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e6+, M = 1e3+,inf = 2e9,mod = 1e9 + ;
namespace IO {
const int MX = 4e7; //1e7占用内存11000kb
char buf[MX]; int c, sz;
void begin() {
c = ;
sz = fread(buf, , MX, stdin);
}
inline bool read(int &t) {
while(c < sz && buf[c] != '-' && (buf[c] < '' || buf[c] > '')) c++;
if(c >= sz) return false;
bool flag = ; if(buf[c] == '-') flag = , c++;
for(t = ; c < sz && '' <= buf[c] && buf[c] <= ''; c++) t = t * + buf[c] - '';
if(flag) t = -t;
return true;
}
} const int MOD = (int)1e9 + ;
int F[N], Finv[N], inv[N];//F是阶乘,Finv是逆元的阶乘
void init(){
inv[] = ;
for(int i = ; i < N; i ++){
inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
}
F[] = Finv[] = ;
for(int i = ; i < N; i ++){
F[i] = F[i-] * 1ll * i % MOD;
Finv[i] = Finv[i-] * 1ll * inv[i] % MOD;
}
}
inline LL C(int n, int m){//comb(n, m)就是C(n, m)
if(m < || m > n) return ;
return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
} struct ss{
int l,r,id;
bool operator<(const ss& x) const{
if(l == x.l) return r > x.r;
else return l < x.l;
}
}a[N];
int now,ok;
inline LL dfs(int ll,int rr) {
if(!ok) return ;
if(ll > rr) return 1LL;
if(a[now].l != ll || a[now].r != rr) {
ok = ;
return ;
}
int ids = a[now++].id;
return dfs(ll,ids-) * dfs(ids+,rr) % mod * C(rr-ll,ids-ll) % mod;
}
int main() {
init();
int cas = ,n;
IO::begin();
while(IO::read(n)) {
for(int i = ; i <= n; ++i) IO::read(a[i].l);
for(int i = ; i <= n; ++i) IO::read(a[i].r),a[i].id = i;
now = , ok = ;
sort(a+,a+n+);
printf("Case #%d: %lld\n",cas++,dfs(,n));
}
return ;
}
最新文章
- PHP与MYSQL事务处理
- Win10 UI入门 RenderTransform属性分析之Translate 平移变形
- iOS开发工程师面试题(一)
- 调整 ANTD 组件菜单的字体大小。
- 全站 HTTPS 来了
- android学习之EdieText组件的使用
- oreData的学习记录
- JS论坛地址备忘
- 从零开始学java(小游戏 石头剪刀布)
- iOS -不同模拟器字体适配
- Oracle execute and call
- 含服务端,客户端,数据库的注册/登录/聊天/在线/离线查看的聊天demo
- es6的几种写法
- 第九节 JS运动应用
- codevs 1862 最长公共子序列(求最长公共子序列长度并统计最长公共子序列的个数)
- Mysql 5.7 报错 3534 错误
- Solr相似度算法二:BM25Similarity
- noip 模拟赛 After 17(递推+特殊的技巧)
- 如何编译部署 UIKit 离线文档?
- skynet中动态库的处理
热门文章
- .NET重构(一):抽象工厂模式实现登录
- spring配置mybatis3
- Oracle 数据库有五个必需的后台进程,DBWR,LGWR,CKPT,SMON,PMON
- mysql查询死锁,执行语句,服务器状态等语句集合
- maven配置中国下载源【转:http://www.cnblogs.com/libingbin/p/5949483.html】
- 【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题
- C/C++怎样传递二维数组,转载自CSDN
- Netty和Akka有什么不同?
- 快速掌握RabbitMQ(三)——消息确认、持久化、优先级的C#实现
- BZOJ——1607: [Usaco2008 Dec]Patting Heads 轻拍牛头