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. docker启动遇到的问题

    转:https://blog.csdn.net/w1316022737/article/details/83692701 遇到问题: Job for docker.service failed bec ...

  2. Part-Appium-1

    1.安装node > 下载二进制文件,解压后,配置到环境变量 https://nodejs.org/download/release/latest/ > vim ~/.bashrc,exp ...

  3. python3下scrapy爬虫(第六卷:利用cookie模拟登陆抓取个人中心页面)

    之前我们爬取的都是那些无需登录就要可以使用的网站但是当我们想爬取自己或他人的个人中心时就需要做登录,一般进入登录页面有两种 ,一个是独立页面登陆,另一个是弹窗,我们先不管验证码登陆的问题 ,现在试一下 ...

  4. 安装VSCODE和typora黑屏

    工欲善其事必先利其器,本来是为了学git为了保存代码,然后网上一顿搜索研究之后发现,用git来保存笔记也不错,因为现在用的onenote搜索实在在在在是太不方便了,除了搜索不行,其他方面她还是很好的, ...

  5. Python sorted函数详解(高级篇)

    sorted() 函数对所有可迭代的对象进行排序操作. sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 s ...

  6. python3的数据类型转换问题

    问题描述:在自我学习的过程中,写了个登陆,在input处,希望能够对数据类型进行判断,但是因为python3的输入的数据会被系统默认为字符串,也就是1,1.2,a.都会被系统默认为字符串,这个心塞啊, ...

  7. html和jsp页面中把文本框禁用,只能读不能写的方法

    方法常用有三种: 第一种,使用   onfocus="this.blur()" <input name="deptno" type="text& ...

  8. python中sorted和sorted 、reversed和reverse的使用。

    #encoding = utf-8 list = [1,8,3,6] print(list.sort()) #None print(list) #[1,3,6,8] print(sorted(list ...

  9. JVM之工具分析

    JVM分析工具有很多; jdk自带工具:jconsole.jvisualvm 其他工具:jprofile ,yourkit等 不要在线上用,影响性能,在测试环境中使用. 一.jconsole ——jd ...

  10. Java集合 - 明的博客

    "In this world there are only two tragedies. One is not getting what one wants, and the other i ...