【hdu4285】 circuits
http://acm.hdu.edu.cn/showproblem.php?pid=4285 (题目链接)
题意
求不不能嵌套的回路个数为K的路径方案数。
Solution
插头dp,时限卡得太紧了,写的我蛋都要碎了T_T。
插头记录连通情况,对于嵌套,我们在合并连通块的时候,判断一下两侧的插头个数的奇偶。如果是奇数,那么合并后一定会出现嵌套;如果是偶数,那么合并后可能会出现嵌套。
细节
卡常经验传授:换行不用for一遍;hash不要开小了
代码
- // hdu4285
- #include<algorithm>
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #define LL long long
- #define HAS 300007
- #define MOD 1000000007
- #define inf 2147483640
- #define Pi acos(-1.0)
- #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
- using namespace std;
- const int maxh=300010,maxs=1000010,maxd=15;
- int a[maxd][maxd],code[maxd],cnts[maxd],n,m,K;
- int size[2],tot[2][maxs],num;
- int nxt[maxs],head[maxh];
- LL s[2][maxs];
- char ch[maxd];
- void decode(LL st) {
- num=st&63,st>>=6;
- for (int i=m;i>=0;i--) code[i]=st&7,st>>=3;
- }
- LL encode(int op,int m) {
- int cnt=0;LL st=0;
- if (op) {
- memset(cnts,-1,sizeof(cnts));cnts[0]=0;
- for (int i=0;i<=m;i++) {
- if (cnts[code[i]]==-1) cnts[code[i]]=++cnt;
- code[i]=cnts[code[i]];
- }
- }
- for (int i=0;i<=m;i++) st=st<<3|code[i];
- return st<<6|num;
- }
- void add(LL tmp,int p,int num) {
- int id=tmp%HAS;
- for (int i=head[id];i;i=nxt[i])
- if (s[p][i]==tmp) {(tot[p][i]+=num)%=MOD;return;}
- s[p][++size[p]]=tmp;tot[p][size[p]]=num;
- nxt[size[p]]=head[id];head[id]=size[p];
- }
- void shift() {
- for (int i=m;i;i--) code[i]=code[i-1];code[0]=0;
- }
- int main() {
- int T;scanf("%d",&T);
- while (T--) {
- memset(a,0,sizeof(a));
- scanf("%d%d%d",&n,&m,&K);
- for (int i=1;i<=n;i++) {
- scanf("%s",ch+1);
- for (int j=1;j<=m;j++) a[i][j]=ch[j]=='.';
- }
- int p=0;
- size[p]=1;tot[p][1]=1;s[p][1]=0;
- for (int i=1;i<=n;i++)
- for (int j=1;j<=m;j++) {
- size[p^=1]=0;
- memset(head,0,sizeof(head));
- for (int k=1;k<=size[p^1];k++) {
- decode(s[p^1][k]);
- int left=code[j-1],up=code[j];
- if (!a[i][j]) {
- code[j-1]=code[j]=0;
- //if (j==m) shift();
- add(encode(0,j==m ? m-1 : m),p,tot[p^1][k]);
- continue;
- }
- if (left && up) {
- if (left==up) {
- if (num>=K) continue;
- int tmp=0;
- for (int l=j+1;l<=m;l++) if (code[l]) tmp++;
- if (tmp&1) continue;
- num++;code[j-1]=code[j]=0;
- //if (j==m) shift();
- add(encode(0,j==m ? m-1 : m),p,tot[p^1][k]);
- }
- else {
- for (int l=0;l<=m;l++) if (code[l]==left) code[l]=up;
- code[j-1]=code[j]=0;
- //if (j==m) shift();
- add(encode(1,j==m ? m-1 : m),p,tot[p^1][k]);
- }
- }
- else if (left || up) {
- int tmp=left ? left : up;
- if (a[i][j+1]) {
- code[j-1]=0,code[j]=tmp;
- add(encode(0,j==m ? m-1 : m),p,tot[p^1][k]);
- }
- if (a[i+1][j]) {
- code[j-1]=tmp,code[j]=0;
- //if (j==m) shift();
- add(encode(0,j==m ? m-1 : m),p,tot[p^1][k]);
- }
- }
- else {
- if (a[i][j+1] && a[i+1][j]) {
- code[j-1]=code[j]=13;
- add(encode(1,j==m ? m-1 : m),p,tot[p^1][k]);
- }
- }
- }
- }
- int ans=0;
- for (int i=1;i<=size[p];i++)
- if ((s[p][i]&63)==K) (ans+=tot[p][i])%=MOD;
- printf("%d\n",ans);
- }
- return 0;
- }
【hdu4285】 circuits的更多相关文章
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- SCI&EI 英文PAPER投稿经验【转】
英文投稿的一点经验[转载] From: http://chl033.woku.com/article/2893317.html 1. 首先一定要注意杂志的发表范围, 超出范围的千万别投,要不就是浪费时 ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- 【原】谈谈对Objective-C中代理模式的误解
[原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...
- 【原】FMDB源码阅读(三)
[原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【调侃】IOC前世今生
前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...
- Python高手之路【三】python基础之函数
基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...
随机推荐
- 4. 为HelloWorld添加日志
回顾 通过上篇内容,我们已经使用flask编写了我们的第一个接口或者说是html页面.我们可以看到,使用flask来编写接口/页面是十分简单的.那么接下来,我们丰富一下上面的例子. 需求 现在的需求来 ...
- C++STL 中的容器整体/逐元素操作方法 少写80%for循环
本文中示例代码默认已引用 std 命名空间 累加 (std::accumulate) accumulate(begin, end, init, op) 返回给定区间内元素的累加值与给定初值的和,初值不 ...
- PHP XXE漏洞
PHP xml 外部实体注入漏洞(XXE) 1.环境 PHP 7.0.30Libxml 2.8.0Libxml2.9.0 以后 ,默认不解析外部实体,对于PHP版本不影响XXE的利用 2.原理介绍 X ...
- JDK8 metaspace调优
从JDK8开始,永久代(PermGen)的概念被废弃掉了,取而代之的是一个称为Metaspace的存储空间.Metaspace使用的是本地内存,而不是堆内存,也就是说在默认情况下Metaspace的大 ...
- Leetcode题库——5.最长回文子串
@author: ZZQ @software: PyCharm @file: longestPalindrome.py @time: 2018/9/18 20:06 要求:给定一个字符串 s,找到 s ...
- 【搜索】POJ-3009 DFS+回溯
一.题目 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. B ...
- mvc拦截请求IHttpModule
代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- Ubuntu16.04 下虚拟环境的创建与使用
1. 虚拟环境 虚拟环境(virtual environment),顾名思义是虚拟出来的环境,通俗来讲,可以借助虚拟机,docker来理解虚拟环境,就是把一部分内容独立出来,我们把这部分独立出来的 ...
- typedef struct bit0 : 1
这句话定义了一个位域,bit0是该位域的域名,而且bit0只占用一个位.位域是指信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.为了节省存储空间,并使处理简便,C语言提供了一种 ...
- Beta阶段团队项目开发篇章4
例会时间 2016.12.11 例会照片 个人工作 上阶段任务验收 删除robot功能较为简单,基本实现,但是一直Question版块任务量过大,考虑到最近要进行编译课设第二次测试,为该任务再分配一段 ...