HDU 6044
题意略。
思路:
I.对于整个区间a1,....,an,必然有一个区间[1,n]与之对应,因为a1,...,an是1,...,n的一个排列,所以在[1,n]中定然有一个最小的数字1,
如果最大的区间[l,r]长度比[1,n]小,那么我们可以知道在[l,r]之外的数字是依然大于1的,这使得1这个数字没有合法的地方可放。
II.起于1左端的区间不可能终于1的右端。
III.数字1左端的部分类似于整体,因为左端也类似地有一个最小的数字。
IV.要想知道整体的方案数有多少,假设可以由f(1,n)算出,那么f(1,n) = f(1,mid - 1) * f(mid + 1,r) * C(n - 1,mid - 1)。
V.这个题递归的顺序可以预先算出来。
#include<bits/stdc++.h>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
typedef long long LL;
const LL mod = 1e9 + ;
const LL maxn = 1e6 + ; inline bool scan_d(int& ret){
char c;int sgn;
if(c = getchar(),c == EOF) return ;
while(c != '-' && (c < '' || c > '')) c = getchar();
sgn = (c == '-') ? - : ;
ret = (c == '-') ? : (c - '');
while(c = getchar(),c >= '' && c <= '') ret = ret * + (c - '');
ret *= sgn;
return ;
} struct node{
int l,r,id;
node(int a = ,int b = ){
l = a,r = b;
}
bool operator<(const node& nd) const{
if(l != nd.l) return l < nd.l;
return r > nd.r;
}
}; int cnt;
LL fac[maxn],inv[maxn];
node store[maxn];
bool flag; LL exgcd(LL a,LL b,LL& x,LL& y){
if(a == && b == ) return -;
if(b == ){
x = ,y = ;
return a;
}
LL d = exgcd(b,a % b,y,x);
y -= a / b * x;
return d;
}
LL rev(LL a,LL n){
LL x,y;
LL d = exgcd(a,n,x,y);
if(d == ) return (x % n + n) % n;
else return -;
}
void init(){
fac[] = ;
for(LL i = ;i < maxn;++i) fac[i] = fac[i - ] * i % mod; inv[maxn - ] = rev(fac[maxn - ],mod);
for(LL i = maxn - ;i >= ;--i){
inv[i] = inv[i + ] * (i + ) % mod;
//printf("inv[%d] == %lld\n",i,inv[i]);
}
}
LL C(int n,int m){
return ((fac[n] * inv[m]) % mod) * inv[n - m] % mod;
}
LL dfs(int l,int r){
//printf("now l == %d r == %d\n",l,r);
if(flag) return ;
if(l > r) return ; if(store[cnt].l != l || store[cnt].r != r){
flag = true;
return ;
}
node cur = store[cnt++];
int mid = cur.id;
LL lft = ,rht = ,c = ;
lft = dfs(l,mid - );
rht = dfs(mid + ,r);
c = C(r - l,mid - l);
//printf("c == %lld\n",c);
//printf("---> %lld\n",(lft * rht % mod) * c % mod);
return (lft * rht % mod) * c % mod;
} int main(){
int cas = ,n;
init();
while(scan_d(n)){
flag = false;
cnt = ;
for(int i = ;i <= n;++i) scan_d(store[i].l);
for(int i = ;i <= n;++i) scan_d(store[i].r),store[i].id = i;
sort(store + ,store + n + );
LL ans = dfs(,n);
printf("Case #%d: %lld\n",cas++,ans);
}
return ;
}
HDU 6044的更多相关文章
- HDU 6044 - Limited Permutation | 2017 Multi-University Training Contest 1
研究一下建树 : /* HDU 6044 - Limited Permutation [ 读入优化,笛卡尔树 ] | 2017 Multi-University Training Contest 1 ...
- HDU 6044 Limited Permutation(搜索+读入优化)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6044 [题目大意] 给出两个序列li,ri,现在要求构造排列p,使得对于区间[li,ri]来说, ...
- HDU 6044 Limited Permutation 读入挂+组合数学
Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...
- hdu 6044 : Limited Permutation (2017 多校第一场 1012) 【输入挂 组合数学】
题目链接 参考博客: http://blog.csdn.net/jinglinxiao/article/details/76165353 http://blog.csdn.net/qq_3175920 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- 实战SpringCloud响应式微服务系列教程(第一章)
前言 在当今互联网飞速发展的时代,业务需求不断的更新和产品的迭代给系统开发过程和编程模式也带来巨大挑战,Spring Cloud微服务也随之应用而生,从springboot1.x到springboot ...
- spring读取xml配置文件(二)
一.当spring解析完配置文件名的占位符后,就开始refresh容器 @Override public void refresh() throws BeansException, IllegalSt ...
- 使用ASM实现动态代理
如果对我这段代码感兴趣,直接拷贝测试debug,要不然你不知道我写的是什么鬼,如果有什么问题,可以告诉我. 一.实现动态代理,首先得考虑有应该定义哪些类,根据JDK的动态代理思想,那么它就应该有一个生 ...
- Angular JS 中的服务注册方法
在Angular JS中创建服务的几种方法 factory() service() constant() value() provider() factory(name,fn(){}) 该服务为单例的 ...
- 【iOS】iOS 调试快速定位程序在哪崩溃
iOS 开发过程中经常遇到程序崩溃.快速定位程序在哪崩溃的步骤如下: 1. 2. 3. 这样设置后,程序崩溃时会定位到崩溃的语句,如下: 原文链接:iOS开发何如在调试的时候轻松找到程序在哪里崩溃
- git项目版本处理--远程分支重新拉取本地代码如何处理
最近在eclipse 上用git拉取分支,提交代码因为提交代码提交了一些配置文件造成 后续同事提交代码一直出现代码冲突问题 项目老大又重新拉取了一条代码 同样的分支名字 当时有点蒙不知道接着怎么弄:场 ...
- WebSocket的实现与应用
WebSocket的实现与应用 前言 说到websocket,就不得不提http协议的连接特点特点与交互模型. 首先,http协议的特点是无状态连接.即http的前一次连接与后一次连接是相互独立的. ...
- 在docker中开启新的container
当你在启动某个容器类型的时候 如果产生了类似的错误: docker: Error: No such image: b27f5dfcfc70c16d7b135889460def6b3f831bcc72 ...
- 如何让传统ASP.NET网站在Docker中运行
本文主要描述如何让传统ASP.NET网站在Docker中运行,侧重Docker image 搭建. 使用条件: Docker for windows 用户切换到Windows 容器模式 Windows ...
- AWS Aurora数据库 Multi-Master 小测
AWS Aurora Mysql终于推出了Multi-Master,直面硬刚Oracle RAC.在多一份数据库产品选择的小兴奋之余,我们也看看新推出的Multi-Master的特点(包括优缺点). ...