attack on titans(动态规划递推,限制条件,至少转至多方法,进击的巨人)
题目意思:
给n个士兵排队,每个士兵三种G、R、P可选,求至少有m个连续G士兵,最多有k个连续R士兵的排列的种数。
原题
Attack on Titans
Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newfound enemy was overwhelming. Soon, mankind was driven to the brink of extinction. Luckily, the surviving humans managed to build three walls: Wall Maria, Wall Rose and Wall Sina. Owing to the protection of the walls, they lived in peace for more than one hundred years.
But not for long, a colossal Titan appeared out of nowhere. Instantly, the walls were shattered, along with the illusory peace of everyday life. Wall Maria was abandoned and human activity was pushed back to Wall Rose. Then mankind began to realize, hiding
behind the walls equaled to death and they should manage an attack on the Titans.
So, Captain Levi, the strongest ever human being, was ordered to set up a special operation squad of N people, numbered from 1 to N. Each number should be assigned to a soldier. There are three corps that the soldiers come from: the Garrison, the Recon Corp
and the Military Police. While members of the Garrison are stationed at the walls and defend the cities, the Recon Corps put their lives on the line and fight the Titans in their own territory. And Military Police serve the King by controlling the crowds and
protecting order. In order to make the team more powerful, Levi will take advantage of the differences between the corps and some conditions must be met.
The Garrisons are good at team work, so Levi wants there to be at least M Garrison members assigned with continuous numbers. On the other hand, members of the Recon Corp are all elite forces of mankind. There should be no more than K Recon Corp members assigned
with continuous numbers, which is redundant. Assume there is unlimited amount of members in each corp, Levi wants to know how many ways there are to arrange the special operation squad.
Input
There are multiple test cases. For each case, there is a line containing 3 integers N (0 < N < 1000000), M (0 < M < 10000) and K (0 < K < 10000), separated by spaces.
One line for each case, you should output the number of ways mod 1000000007.
Sample Input
3 2 2
5
//首先感谢MartaYang的详细博文让我搞懂这题
//https://blog.csdn.net/MartaYang/article/details/77175488
/*
思路:
1(至少化为至多):至少化为至多,这样可以使问题简单化,
比如说从10个人中至少挑3人=10人至多挑十人(无限制的总方案数)-10人至多挑6人
那么这题同理有:
至少m个连续G士兵=至多n个连续G士兵-至多m-1个连续G士兵 准备一个数组,纵行代表处理到第几位放哪个士兵的方案数,横行代表这三个兵种各自的方案数
先处理一下边缘条件,然后
假设G至多u个连续,R至多v个连续(刚刚已经把至少问题转换为至多问题了)
对于G:
如果现在当前行i<=u,那么可以随便排列
如果当前行i=u+1,加完各种情况后要减去一种情况,就是前面u个全是g的情况,
因为是一种情况,所以减1
如果当前行>u+1,那么加完各种情况后要-a[i-u-2]-a[i-u-3];
即减去第i-u-2位不是G而中间其他位置都是G的情况,这样可以避免第i个放下去后产生u+1个连续
有人会问为什么不是-1-1
因为这其实是-a[i-u-2]*1-a[i-u-3]*1
那个*1其实是*中间全是G,这却时常是1种,所以-a[i-u-2]-a[i-u-3]
用这种方法,i>u+1位以后,就不可能出现连续数超过u,因为通过减方案减去了可能出现问题的地方
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=10e6+;
ll a[maxn][];//定义一个数组,a[i][1]代表第i位选的总方案数G士兵,
//同理a[i][2]代表第i位选R士兵的总方案数,a[i][1]代表第i位选P士兵的总方案数,
ll n,m,k;//最少m个连续G,最多k个连续R
ll mod=;//定义模,题目要求取模
ll solve(ll u,ll v)//准备一个函数算方案
{
if(u==) a[][]=; else a[][]=;//为G士兵赋初始值,要考虑当连续的要求为0的情况
//a[1][1]表示第一位选G的方案数
//a[2][1]表示第二位选G并且包含前面的总方案数
if(v==) a[][]=;else a[][]=;//v和u差不多
a[][]=;//第三种士兵没有限制,直接赋初值为1
for(ll i=;i<=n;i++)//枚举每一个位置,从2到n
{
a[i][]=a[i-][]+a[i-][]+a[i-][];
//第三种士兵的方案数等于前一个位置三种方案数相加
if(i<=u) a[i][]=(a[i-][]+a[i-][]+a[i-][])%mod;
else if(i==u+) a[i][]=(a[i-][]+a[i-][]+a[i-][]-)%mod;
else a[i][]=(a[i-][]+a[i-][]+a[i-][]-a[i-u-][]-a[i-u-][])%mod; if(i<=v) a[i][]=(a[i-][]+a[i-][]+a[i-][])%mod;
else if(i==v+) a[i][]=(a[i-][]+a[i-][]+a[i-][]-)%mod;
else a[i][]=(a[i-][]+a[i-][]+a[i-][]-a[i-u-][]-a[i-u-][])%mod;
}
return (a[n][]+a[n][]+a[n][])%mod;//把方案数return出去 } int main()
{
cin>>n>>m>>k;
//最多n个连续G,最多k个连续R --减去-- 最少m-1个连续G,最多k个连续R
cout<<((solve(n,k)-solve(m-,k))%mod+mod)%mod<<endl;
return ;
}
attack on titans(动态规划递推,限制条件,至少转至多方法,进击的巨人)的更多相关文章
- Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)
有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...
- 递推DP(至少和至多之间的转换
UVa 10328 - Coin Toss 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 转换成抛N次至多连续有N个减去抛N次至多连续有K-1个1的情况 dp[i][k]表 ...
- 最长上升子序列(动态规划递推,LIS)
1759:最长上升子序列 题目: 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的 ...
- 最大子段和(洛谷P1115,动态规划递推)
洛谷题目链接 题目赋值出来格式有问题,所以我就只放题目链接了 下面为ac代码 #include<bits/stdc++.h> #define ll long long using name ...
- 数的计数(noip2001,动态规划递推)
题目链接: 普通版: https://www.luogu.org/problemnew/show/P1028 数据加强版: https://www.luogu.org/problemnew/show/ ...
- P1759 通天之潜水(不详细,勿看)(动态规划递推,组合背包,洛谷)
题目链接:点击进入 题目分析: 简单的组合背包模板题,但是递推的同时要刷新这种情况使用了哪些物品 ac代码: #include<bits/stdc++.h> using namespace ...
- P2347 砝码称重(动态规划递推,背包,洛谷)
题目链接:P2347 砝码称重 参考题解:点击进入 纪念我第一道没理解题意的题 ''但不包括一个砝码也不用的情况'',这句话我看成了每个砝码起码放一个 然后就做不出来了 思路: 1.这题数据很小,10 ...
- NOIP 2008 传纸条(洛谷P1006,动态规划递推,滚动数组)
题目链接:P1006 传纸条 PS:伤心,又想不出来,看了大神的题解 AC代码: #include<bits/stdc++.h> #define ll long long using na ...
- NOIP2000方格取数(洛谷,动态规划递推)
先上题目: P1004 方格取数 下面上ac代码: ///如果先走第一个再走第二个不可控因素太多 #include<bits/stdc++.h> #define ll long long ...
随机推荐
- 怎样处理Gradle中的这个文件下载慢的问题的
如图:在build.gradle中的dependencies中加上要依赖的包后,就点击sync gradle.然后就开始了下载.在此过程中我是FQ了的(在此同时我是可以用chrome进入https:/ ...
- backbone源代码注释(部分)
// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...
- Rails5 radio_button
容易错,集中记下来 首先是radio button的三种形式 函数名 参数意义 radio_button_tag(prop, value [, opts]) prop: radio的属性 v ...
- Linux安装PHP环境
简介: PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,主要 ...
- java io 文件下载
/** * 文件下载 * @param response * @param downloadPath * @param docName */ public void downLoadFile( Htt ...
- 洛谷 P2090 数字对
发现如果给定两个数(a,b),可以用类似辗转相除法在logn的时间内计算出(反向)变到(1,1)的最小步数. 然而并不知道另一个数是多少? 暴力嘛,枚举一下另一个数,反正1000000的nlogn不虚 ...
- 超实用的jQuery代码片段
1.jQuery回到顶部效果 HTML代码:<a href="javascript:;" id="btn" title="回到顶部"& ...
- 每天学点linux命令之nc
nc is NetCat.素以短小精悍著称的网络工具包.主要用来开放的扫描端口(黑客或者OSAdmin的最爱),不同主机之间传输文字 | 文件. http://blog.csdn.net/zhangx ...
- C#基础 结构体 枚举类型
结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 一.定义的例子: struct student { public int nianling; public int fen ...
- Java内存模型原理,你真的理解吗?
[51CTO.com原创稿件]这篇文章主要介绍模型产生的问题背景,解决的问题,处理思路,相关实现规则,环环相扣,希望读者看完这篇文章后能对 Java 内存模型体系产生一个相对清晰的理解,知其然知其所以 ...



