AGC017 F - Zigzag
Time limit : 4sec / Memory limit : 256MB
Score : 1600 points
Problem Statement
There are N(N+1)⁄2 dots arranged to form an equilateral triangle whose sides consist of N dots, as shown below. The j-th dot from the left in the i-th row from the top is denoted by (i,j) (1≤i≤N, 1≤j≤i). Also, we will call (i+1,j) immediately lower-left to (i,j), and (i+1,j+1) immediately lower-right to (i,j).

Takahashi is drawing M polygonal lines L1,L2,…,LM by connecting these dots. Each Li starts at (1,1), and visits the dot that is immediately lower-left or lower-right to the current dots N−1 times. More formally, there exist Xi,1,…,Xi,N such that:
- Li connects the N points (1,Xi,1),(2,Xi,2),…,(N,Xi,N), in this order.
- For each j=1,2,…,N−1, either Xi,j+1=Xi,j or Xi,j+1=Xi,j+1 holds.
Takahashi would like to draw these lines so that no part of Li+1 is to the left of Li. That is, for each j=1,2,…,N, X1,j≤X2,j≤…≤XM,j must hold.
Additionally, there are K conditions on the shape of the lines that must be followed. The i-th condition is denoted by (Ai,Bi,Ci), which means:
- If Ci=0, LAi must visit the immediately lower-left dot for the Bi-th move.
- If Ci=1, LAi must visit the immediately lower-right dot for the Bi-th move.
That is, XAi,Bi+1=XAi,Bi+Ci must hold.
In how many ways can Takahashi draw M polygonal lines? Find the count modulo 1000000007.
Notes
Before submission, it is strongly recommended to measure the execution time of your code using "Custom Test".
Constraints
- 1≤N≤20
- 1≤M≤20
- 0≤K≤(N−1)M
- 1≤Ai≤M
- 1≤Bi≤N−1
- Ci=0 or 1
- No pair appears more than once as (Ai,Bi).
Input
Input is given from Standard Input in the following format:
N M K
A1 B1 C1
A2 B2 C2
:
AK BK CK
Output
Print the number of ways for Takahashi to draw M polygonal lines, modulo 1000000007.
Sample Input 1
3 2 1
1 2 0
Sample Output 1
6
There are six ways to draw lines, as shown below. Here, red lines represent L1, and green lines represent L2.
Sample Input 2
3 2 2
1 1 1
2 1 0
Sample Output 2
0
Sample Input 3
5 4 2
1 3 1
4 2 0
Sample Output 3
172
Sample Input 4
20 20 0
Sample Output 4
881396682 题目大意
有一高度为N的三角形,共有M条线从顶部走到底部,要求第L+1条线不能在第L条线的左边,有K个要求,要求第a条线必须在第b层向某方向走(c为一即向左,为二则向右),问共有几种情况
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int dp[1048580];
int need1[21],need2[21];
int put[1048580],go[1048580][21];
//go是记录将一条先向左再向右边翻折后的形状
//put记录自下向上找,第一个向左的边
int n,m,k;
int main()
{ int i,j,p,q,a,b,c;
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=k;i++){
scanf("%d%d%d",&a,&b,&c);
a--,b--;
need1[a]|=(1<<b);
need2[a]|=(1<<b)*c;
//必须走的路径
}
n--;
memset(go,-1,sizeof(go));
memset(put,-1,sizeof(put));
for(i=0;i<(1<<n);i++){
int num=0;
for(j=0;j<n;j++)
if(i&(1<<j)){
if(j>0&&!(i&(1<<(j-1)))){
go[i][num]=i^(1<<j)^(1<<(j-1));
//i指路径,num指是第几个向右的边
}
num++;
}
}
for(i=0;i<(1<<n);i++)
for(j=n-1;j>=0;j--){
if((i&(1<<j)))continue;
put[i]=i^(1<<j);
break;
}
dp[0]=1;
for(i=0;i<m;i++){
for(j=0;j<(1<<n);j++)
if(put[j]){
dp[put[j]]+=dp[j];
dp[put[j]]%=1000000007;
}
for(p=0;p<n;p++)
for(j=(1<<n)-1;j>=0;j--)
if(go[j][p]!=-1){
dp[go[j][p]]+=dp[j],
dp[go[j][p]]%=1000000007;
}
for(j=0;j<(1<<n);j++)
if((j&need1[i])!=need2[i])
dp[j]=0;
}
int ans=0;
for(i=0;i<(1<<n);i++)
ans+=dp[i],
ans%=1000000007;
printf("%d\n",ans%1000000007);
return 0;
}
AGC017 F - Zigzag的更多相关文章
- AtCoder Grand Contest 017 F - Zigzag
题目传送门:https://agc017.contest.atcoder.jp/tasks/agc017_f 题目大意: 找出\(m\)个长度为\(n\)的二进制数,定义两个二进制数的大小关系如下:若 ...
- 【AtCoder】AGC017
在此处输入标题 标签(空格分隔): 未分类 A - Biscuits dp[i][0/1]表示当前和是偶数还是奇数,直接转移即可 #include <bits/stdc++.h> #def ...
- AtCoder Grand Contest 017
noi前橙名计划失败.全程搞C而gg…… A - Biscuits 题意:背包,求价值为奇/偶的方案数. #include<cstdio> #include<queue> #i ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- AtCoder Grand Contest 017 (VP)
contest link Official Editorial 比赛体验--之前做题的时候感觉 AtCoder 挺快的,现在打了VP之后发现还是会挂的--而且不是加载缓慢或者载不出来,直接给你一个无法 ...
- Mysql_以案例为基准之查询
查询数据操作
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- leetcode 6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
随机推荐
- 客户端一致性与多Leader机制------《Designing Data-Intensive Applications》读书笔记7
接着上一篇的内容,我们继续来梳理分布式系统之中的副本机制与副本一致.上文我们聊到了在可用性与一致性之间的一个折中的一致性等级:最终一致性.我们顺着上篇的内容,由用户来分析一致性等级. 1. 客户端的困 ...
- Scrum已经俘获中国开发者的心? ——从《2017年开发者调查报告》看真相!
云栖社区通过为期两个月,对7032份有效调查问卷分析统计,2017年12月发布了首份<2017中国开发者调查报告>.报告显示,37.3%的开发者表示,协作工具主要来自企业内部自研的协作工具 ...
- 分布式监控系统--zabbix
1Zabbix简介 Zabbix 是一个企业级的分布式开源监控方案. 2.监控系统架构 C/S架构 客户端/服务器端,这种架构适合规模较小,处于同一地域的环境 C/P/S 客户端/代理端/服务器端/, ...
- [20160711][VS2012配置OpenCV2.4.9]
相关说明 OpenCV是一套开源机器视觉库,用于简化机器视觉算法的开发与调试. 移植环境 操作系统:Win7 64位 移植软件:Visual Studio 2012 代码下载: https://sou ...
- vue ajax获取数据的时候,如何保证传递参数的安全或者说如何保护api的安全
https://segmentfault.com/q/1010000005618139 vue ajax获取数据的时候,如何保证传递参数的安全或者说如何保护api的安全 点击提交,发送请求.但是api ...
- Dubbo底层采用Socket进行通信详解
由于Dubbo底层采用Socket进行通信,自己对通信理理论也不是很清楚,所以顺便把通信的知识也学习一下. n 通信理论 计算机与外界的信息交换称为通信.基本的通信方法有并行通信和串行通信两种. 1 ...
- Java中泛型数组创建总结
在java中,可以声明一个泛型数组,不能通过直接通过T[] tarr=new T[10]的方式来创建数组,最简单的方式便是通过Array.newInstance(Classtype,int size) ...
- CCF系列之数字排序(201503-2)
问题描述试题编号: 201503-2试题名称: 数字排序时间限制: 1.0s内存限制: 256.0MB问题描述: 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输 ...
- SuperSocket基础一
SuperSocket基础(一)——————基本概念 项目中之前一直使用TCP socket服务框架,但是不利于扩展.最近刚接触到开源的superSocket感觉很不错,特记录一下.官方开源地址:ht ...
- 深入理解HashMap的扩容机制
什么时候扩容: 网上总结的会有很多,但大多都总结的不够完整或者不够准确.大多数可能值说了满足我下面条件一的情况. 扩容必须满足两个条件: 1. 存放新值的时候当前已有元素的个数必须大于等于阈值 2. ...