ZOJ 3747 Attack on Titans(DP)
2016-03-09 17:33
399 查看
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 ofN 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 thanK
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.
3 integers N (0 < N < 1000000), M (0 <
M < 10000) and K (0 < K < 10000), separated by spaces.
第一次做这类有限制条件的DP.看了大神的题解才明白;
思路是这样的;
首先因为一个是至少存在问题,一个是至多存在问题.不好找此时的状态,
所以先要把它们同意限制条件.假设事件A={至多n个G&&至多k个R}; 事件B={至多m-1个G&&至多kgeR};
所以事件A-B={至少有m个G};
令dp[i][k]表示第i个位置是k种兵k(0=G,1=R,2=P)
令sum=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];
因为P兵种并无影响,所以dp[i][2]=sum;
对于A事件;
假设 i<=u 这时第i个位置可以随意放入GRP任意一种,即dp[i][0]=sum;
假设i==u+1时这时要排除1~u 全为G的情况 所以此时的dp[i][0]=sum-1;
假设i>u+1 时这时要排除i-u~i-1全为G的情况 这种状态即为i-u~i-1全为G时i-u-1为P或R的情况,即dp[i][0]=sum-dp[i-u-1][1]-dp[i-u-1][2];
类似的可以推到出B事件;
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 ofN 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 thanK
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 containing3 integers N (0 < N < 1000000), M (0 <
M < 10000) and K (0 < K < 10000), separated by spaces.
Output
One line for each case, you should output the number of ways mod 1000000007.Sample Input
3 2 2
Sample Output
5
Hint
Denote the Garrison, the Recon Corp and the Military Police as G, R and P. Reasonable arrangements are: GGG, GGR, GGP, RGG, PGG.第一次做这类有限制条件的DP.看了大神的题解才明白;
思路是这样的;
首先因为一个是至少存在问题,一个是至多存在问题.不好找此时的状态,
所以先要把它们同意限制条件.假设事件A={至多n个G&&至多k个R}; 事件B={至多m-1个G&&至多kgeR};
所以事件A-B={至少有m个G};
令dp[i][k]表示第i个位置是k种兵k(0=G,1=R,2=P)
令sum=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];
因为P兵种并无影响,所以dp[i][2]=sum;
对于A事件;
假设 i<=u 这时第i个位置可以随意放入GRP任意一种,即dp[i][0]=sum;
假设i==u+1时这时要排除1~u 全为G的情况 所以此时的dp[i][0]=sum-1;
假设i>u+1 时这时要排除i-u~i-1全为G的情况 这种状态即为i-u~i-1全为G时i-u-1为P或R的情况,即dp[i][0]=sum-dp[i-u-1][1]-dp[i-u-1][2];
类似的可以推到出B事件;
#define M 1000000007 #include<stdio.h> int n,m,k; long long dp[1000005][3]; long long f(int u,int v) { long long sum=0; dp[0][0]=dp[0][1]; dp[0][2]=1; for (int i=1;i<=n+1;i++) { sum=(dp[i-1][0]+dp[i-1][1]+dp[i-1][2])%M; dp[i][2]=sum; if (i<=u) {dp[i][0]=sum;} if (i==u+1) {dp[i][0]=(sum-1)%M;} if (i>u+1) {dp[i][0]=(sum-(dp[i-u-1][1]+dp[i-u-1][2]))%M;} if (i<=v) {dp[i][1]=sum;} if (i==v+1) {dp[i][1]=(sum-1)%M;} if (i>v+1) {dp[i][1]=(sum-(dp[i-v-1][0]+dp[i-v-1][2]))%M;} } return sum; } int main() { while (scanf("%d%d%d",&n,&m,&k)!=EOF) { long long ans=((f(n,k)-f(m-1,k))%M+M)%M; printf("%lld\n",ans); } }
相关文章推荐
- 华为oj 人民币转换
- 各大学习网址总结
- next_permutation函数
- Process 'command '...\jdk1.8.0_40\bin\java.exe'' finished with non-zero exit value 2
- GDOI模拟 染色配对
- Apache HttpClient 4.3开发指南 实现URL重定向
- Sublime Text 3 配置浏览器预览路径 localhost
- Python多线程学习
- JavaEE_Junit_Junit对Spring注入的Service层的测试
- Sicily 1488 矩阵取数游戏
- CodeForces 630J:Divisibility 【数学】
- FPGA 竞争与冒险
- Lua中的面向对象编程
- Github——创建GitHub技术博客全攻略
- Broadcast receiver 的使用
- 全排列一个数组
- 二维码加logo demo
- WebApi说明文档插件
- vb6 winhttp 上传文件
- Activity的顶层窗体