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 ...
随机推荐
- Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)
D. Dima and Lisa Dima loves representing an odd num ...
- SNAPSHOTTING
/etc/redis.conf ################################ SNAPSHOTTING ################################## Sav ...
- javascript总结02
1 如何打开和关闭一个新的窗口? 2 Window对象的哪个属性能返回上一个浏览页面? 3 一次或多次执行一段程序的函数是什么? 定时函数 4 如何查找并访问节点? 5 给表格新增行和单元格的方法分别 ...
- hdu1814 Peaceful Commission——2-SAT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1814 第一次的2-SAT,推荐博客:https://blog.csdn.net/jarjingx/arti ...
- 18. 视图Ext.Viewport和窗口Ext.Window用法
转自:http://www.cnblogs.com/linjiqin/archive/2011/06/22/2087003.html 视图Ext.Viewport和窗口Ext.Window用法. 1. ...
- mysql百万数据分页查询速度
百万数据测试 ,; 受影响的行: 时间: .080ms ,; 受影响的行: 时间: .291ms ,; 受影响的行: 时间: .557ms ,; 受影响的行: 时间: .821ms ,; 受影响的行: ...
- Lightoj 1020 - A Childhood Game (博弈)
题目链接: 1020 - A Childhood Game 题目描述: Alice和Bob在玩弹珠游戏,两人轮流拿走弹珠,每次只能拿走一个或者两个,当Alice作为先手时谁拿走最后一个就是输家,而Bo ...
- 51nod1265判断四点共面
1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4点共 ...
- 状压DP UVA 10817 Headmaster's Headache
题目传送门 /* 题意:学校有在任的老师和应聘的老师,选择一些应聘老师,使得每门科目至少两个老师教,问最少花费多少 状压DP:一看到数据那么小,肯定是状压了.这个状态不好想,dp[s1][s2]表示s ...
- 409 Longest Palindrome 最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...



