5230. 【NOIP2017模拟A组模拟8.5】队伍统计

(File IO): input:count.in output:count.out

Time Limits: 1500 ms Memory Limits: 524288 KB Detailed Limits

Description

现在有n个人要排成一列,编号为1->n 。但由于一些不明原因的关系,人与人之间可能存在一些矛盾关系,具体有m条矛盾关系(u,v),表示编号为u的人想要排在编号为v的人前面。要使得队伍和谐,最多不能违背k条矛盾关系(即不能有超过k条矛盾关系(u,v),满足最后v排在了u前面)。问有多少合法的排列。答案对10^9+7取模。

Input

输入文件名为count.in。

第一行包括三个整数n,m,k。

接下来m行,每行两个整数u,v,描述一个矛盾关系(u,v)。

保证不存在两对矛盾关系(u,v),(x,y),使得u=x且v=y 。

Output

输出文件名为count.out。

输出包括一行表示合法的排列数。

Sample Input

输入1:

4 2 1

1 3

4 2

输入2:

10 12 3

2 6

6 10

1 7

4 1

6 1

2 4

7 6

1 4

10 4

10 9

5 9

8 10

Sample Output

输出1:

18

输出2:

123120

Data Constraint

对于30%的数据,n<=10

对于60%的数据,n<=15

对应100%的数据,n,k<=20,m<=n*(n-1),保证矛盾关系不重复。

题解

状压dp

用f[s][i]表示选人情况为s时,违反了i个矛盾关系

这样复杂度是O(2nn2k),有点高

加个优化

因为矛盾关系不重复,用a[i]表示log2(i)的矛盾关系

这样就去掉了一个n

最终复杂度是O(2nnk)

代码

#include<cstdio>
#include<cmath>
#define lowbit(a) ((a)&-(a))
#define mo 1000000007
#define N 30 long long f[1<<20][N];
long a[1<<20]; int main()
{ long n,m,l,x,y,s,i,j,k,q;
long long num;
freopen("count.in","r",stdin);
freopen("count.out","w",stdout);
scanf("%ld%ld%ld",&n,&m,&l);
for(i=1;i<=m;i++){
scanf("%ld%ld",&x,&y);
a[1<<(x-1)]|=1<<(y-1);
}
f[0][0]=1;
for(s=0;s<(1<<n);s++)
for(i=0;i<=l;i++)if(f[s][i])
for(q=s^((1<<n)-1);q;q^=lowbit(q)){
num=0;
for(k=s&a[lowbit(q)];k;k^=lowbit(k))
num++;
if(i+num<=l)
f[s|lowbit(q)][i+num]+=f[s][i];
}
num=0;
for(i=0;i<=l;i++)
num=(num+f[(1<<n)-1][i])%mo;
printf("%lld\n",num%mo);
return 0;
}

JZOJ 5230. 【NOIP2017模拟A组模拟8.5】队伍统计的更多相关文章

  1. JZOJ 【NOIP2017提高A组模拟9.14】捕老鼠

    JZOJ [NOIP2017提高A组模拟9.14]捕老鼠 题目 Description 为了加快社会主义现代化,建设新农村,农夫约(Farmer Jo)决定给农庄里的仓库灭灭鼠.于是,猫被农夫约派去捕 ...

  2. [JZOJ 100026] [NOIP2017提高A组模拟7.7] 图 解题报告 (倍增)

    题目链接: http://172.16.0.132/senior/#main/show/100026 题目: 有一个$n$个点$n$条边的有向图,每条边为$<i,f(i),w(i)>$,意 ...

  3. 【NOIP2017提高A组模拟9.7】JZOJ 计数题

    [NOIP2017提高A组模拟9.7]JZOJ 计数题 题目 Description Input Output Sample Input 5 2 2 3 4 5 Sample Output 8 6 D ...

  4. JZOJ 100029. 【NOIP2017提高A组模拟7.8】陪审团

    100029. [NOIP2017提高A组模拟7.8]陪审团 Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limits   Got ...

  5. JZOJ 5328. 【NOIP2017提高A组模拟8.22】世界线

    5328. [NOIP2017提高A组模拟8.22]世界线 (File IO): input:worldline.in output:worldline.out Time Limits: 1500 m ...

  6. JZOJ 5329. 【NOIP2017提高A组模拟8.22】时间机器

    5329. [NOIP2017提高A组模拟8.22]时间机器 (File IO): input:machine.in output:machine.out Time Limits: 2000 ms M ...

  7. JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)

    5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...

  8. JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)

    5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...

  9. JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)

    5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...

随机推荐

  1. vue中v-on支持的事件总结

    资源事件 事件名称 何时触发 error 资源加载失败时. abort 正在加载资源已经被中止时. load 资源及其相关资源已完成加载. beforeunload window,document 及 ...

  2. H5的localStorage使用总结

    一.localstorage 的优缺点 优点: 1.localStorage 的存储大小是5M,而cookie的存储大小只有4K,解决了cookie存储空间不足的问题 2.localStorage 可 ...

  3. python学习笔记(17)urllib.parse模块使用

    url.parse :定义了url的标准接口,实现url的各种抽取 parse模块的使用:url的解析,合并,编码,解码 使用时需导入 from urllib import parse urlpars ...

  4. 吴裕雄--天生自然 Hadoop大数据分布式处理:关闭CenterOS 7防火墙

    使用命令:systemctl status firewalld.service 查看防火墙状态 执行后可以看到绿色字样标注的“active(running)”,说明防火墙是开启状态 使用命令:syst ...

  5. OpenCV 级联分类器

    #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" ...

  6. HTML面试题&知识点汇总

    目录 问题&答案 参考资料 结束语 问题&答案 DOCTYPE作用?标准模式与兼容模式各有什么区别? 声明位于HTML文档中的第一行,处于 html 标签之前.告知浏览器的解析器用什么 ...

  7. 《hdu 免费馅饼》

    题目描述 免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. LG_3459_[POI2007]MEG-Megalopolis

    题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who onc ...

  9. 接口测试-chap3-https请求证书问题

    接口地址如果是HTTPS,需要安装证书,或者在发送请求时传入参数verify=False,表示忽略 res = requests.get(url, verify=False)

  10. Vuex安装使用

    vuex是以插件的方式存在的. 安装:打开项目的根目录,即package.json所在目录,执行以下命令: npm install vuex --save-dev 背景:小型应用里的每个组件维护着自有 ...