(1)转自rockZ的博文

  

          UVa 10328 - Coin Toss (递推)

题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种。

原题中问出现连续至少k个H的情况,很难下手。我们可以试着将问题转化一下。

设dp[i][j]表示抛掷i个硬币出现连续至多j个H的情况种数。

实际上原题中的出现连续至少k个H,即出现连续k个H,k+1个H,...n个H的并集,等价于dp[n][n]-dp[n][k-1],即从连续至多n个H的情况(其实这就是所有的抛掷情况种数)减去连续至多(k-1)个H的情况,这保证得到的所有情况一定至少有k个连续的H。

现在问题就变成了怎么求dp[i][j]。

考虑当i<=j的时候,dp[i][j]=dp[i-1][j]*2,即从上一阶段得到的抛掷序列后面增加正和反两种情况,如果出现连续的H个数大于j个,这种情况是非法的,但很显然此时不会出现这种情况。

当i>j时,如果继续用dp[i][j]=dp[i-1][j]*2就不行了。因为如果 从i-j到第i-1全部都是H ,那么这时候在第i个位置再加一个H,就会出现连续的H个数大于j个的非法状态,所以我们需要减掉 从i-j到第i-1全部都是H 的这种情况。那么这种情况有多少种呢。我们考虑该状态是如何转移而来的。试想第i-j-1个位置应该是什么呢。很明显应该是F。如果是H那就会出现非法状态了。那在第i-j-1之前的位置呢。无论H和F都可以,只要不出现连续的H个数大于j的非法状态即可,这就是dp[i-j-2][j]。

那么这样,dp[i][j]=dp[i-1][j]*2-dp[i-j-2][j]。

但这还是不够的。我们之前的推导都是基于第i-j-1个位置一定存在的前提下(i>j不能保证第i-j-1个位置一定存在),那如果第i-j-1个位置不存在,第i-j-2个位置也就不存在,上述方程也就不成立了。但这种情况很好想,此时一定是i==j+1,从第1个位置到第j个位置全部都是H,只有这一种情况,所以方程变成dp[i][j]=dp[i-1][j]*2-1。

综上:

dp[i][j]表示抛掷i个硬币出现连续至多j个H的情况种数

dp[0][j]=1

i<=j:dp[i][j]=dp[i-1][j]*2

i>j :i==j+1:dp[i][j]=dp[i-1][j]*2-1

else: dp[i][j]=dp[i-1][j]*2-dp[i-j-2][j]

ans=dp[n][n]-dp[n][k-1]

需要用到大数。

(2)转accagin的博客:http://blog.csdn.net/cc_again/article/details/24841249

题目链接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170

Attack on Titans


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.

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.

题目意思:

给n个士兵排队,每个士兵三种G、R、P可选,求至少有m个连续G士兵,最多有k个连续R士兵的排列的种数。

解题思路:

dp递推。

先把问题都转化成至多连续的情况:至多k个连续R,至多n个连续G情况 【减去】至多k个连续R,至多(m-1)个连续G情况。

至多的情况比较好考虑,至少的情况比较复杂,比赛的时候一直落在至少的圈子里,没想到用递推。

//dp[i][0]表示第i个为G,至多有u个连续G,至多有v个连续R的个数  //这里的u和v固定

//dp[i][1]表示第i个为R,....

 //d[i][2]表示第i个为P,....

当第i个为P的情况很好考虑不会对连续的R和G产生影响,dp[i][2]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

当第i个为G时

如果i<=u 时 无论怎么放都不会超过u个连续的G这个限制条件 所以dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

如果i=u+1时,要排除前u个都放了G的情况,dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-1;

如果i>u+1时,要排除从i-1到i-u位置都放了G的情况,dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-dp[i-u-1][1]-dp[i-u-1][2];

当第i个为R时

如果i<=v 时 无论怎么放都不会超过u个连续的G这个限制条件 所以dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];

如果i=v+1时,要排除前v个都放了G的情况,dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-1;

如果i>v+1时,要排除从i-1到i-v位置都放了G的情况,dp[i][1]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]-dp[i-v-1][0]-dp[i-v-1][2];

//#include<CSpreadSheet.h>  

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 1100000 LL dp[Maxn][]; //dp[i][0]表示第i个为G,至多有u个连续G,至多有v个连续R的个数
//dp[i][1]表示第i个为R,....
//dp[i][2]表示第i个为P,....
LL n,m,k,u,v; LL Cal()
{
dp[][]=; //初始状态
dp[][]=;
dp[][]=; for(int i=;i<=n;i++)
{
LL sum=(dp[i-][]+dp[i-][]+dp[i-][])%M;
dp[i][]=sum; if(i<=u)
dp[i][]=sum;
else if(i==u+)
dp[i][]=(sum-)%M;
else
dp[i][]=(sum-dp[i-u-][]-dp[i-u-][])%M; if(i<=v)
dp[i][]=sum;
else if(i==v+)
dp[i][]=(sum-)%M;
else
dp[i][]=(sum-dp[i-v-][]-dp[i-v-][])%M; //printf("u:%lld v:%lld i:%d %lld %lld %lld\n",u,v,i,dp[i][0],dp[i][1],dp[i][2]);
//system("pause"); }
return (dp[n][]+dp[n][]+dp[n][])%M;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%lld%lld%lld",&n,&m,&k))
{
LL ans;
u=n,v=k;
ans=Cal(); //printf(":%lld\n",ans);
//system("pause"); u=m-,v=k;
//printf(":%lld\n",Cal());
//system("pause");
ans=((ans-Cal())%M+M)%M;
printf("%lld\n",ans); }
return ;
}

dp的简单递推笔记1的更多相关文章

  1. HDU 2085 核反应堆 --- 简单递推

    HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...

  2. Flags-Ural1225简单递推

    Time limit: 1.0 second Memory limit: 64 MB On the Day of the Flag of Russia a shop-owner decided to ...

  3. 简单递推 HDU-2108

    要成为一个ACMer,就是要不断学习,不断刷题...最近写了一些递推,发现递推规律还是挺明显的,最简单的斐波那契函数(爬楼梯问题),这个大家应该都会,看一点稍微进阶了一点的,不是简单的v[i] = v ...

  4. NBUT 1028 该减肥了(简单递推)

    [1028] 该减肥了 时间限制: 1000 ms 内存限制: 65535 K 问题描述 由于长期缺乏运动,Teacher Xuan发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥.Teach ...

  5. POJ_2478 Farey Sequence 【欧拉函数+简单递推】

    一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...

  6. BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...

  7. bzoj2004公交线路——DP+矩阵加速递推

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 求方案数,想到DP: 因为两个站间距离<=p,所以每p个站中所有车一定都会停靠至 ...

  8. <每日一题> Day5:简单递推两题

    原题链接 参考代码: #include <iostream> using namespace std; typedef long long ll; + ; ll dp[maxn]; int ...

  9. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

随机推荐

  1. WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法

    WPF 在TextBox失去焦点时检测数据,出错重新获得焦点解决办法 在WPF的TextBox的LostFocus事件中直接使用Focus()方法会出现死循环的问题 正确的使用方式有2中方法: 方法一 ...

  2. WebApi(6) 后台C#调用WebApi

    https://www.cnblogs.com/cxd1008/p/6640015.html 今天来写一下后台C#代码如何访问webapi 这里使用HttpClient方法访问webapi也是很常用的 ...

  3. DecimalFormat 四舍五入Float类型的坑

    今天又踩了一个坑,使用DecimalFormat来完毕四舍五入.可是传入的是float类型,几轮測试才发现一个问题,传入的float会被转为double类型.大家都知道float是4位,double是 ...

  4. Foundation框架中的NSNumber对象详解

    到目前为止,我们所讨论过的所有数字数据类型,如int型.float型和long型都是Objective-C语言中的基本数据类型,也就是说,它们都不是对象.例如,不能向它们发送消息.然而,有时需要作为对 ...

  5. java基础---->Java中异常的使用(二)

    这一篇博客用例子讲述一下异常的处理过程.那些 我们一直惴惴不安 又充满好奇的未来 会在心里隐隐约约地觉得它们是明亮的. 异常的执行过程 一.实例一:return语句 public class Exce ...

  6. JDBC通用DAO

    dbcBaseDao接口,内容如下: package com.sun4j.core.jdbc.dao; import java.io.Serializable; import java.util.Li ...

  7. POI导出EXCEL经典实现(转载)

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  8. Linux云服务器下Tomcat部署超详细

    基于阿里云Centos 7服务器的Tomcat 项目部署 工具:一台安装jdk1.8的Centos 6/7.X 云服务器(64位) Putty  ssh远程连接云服务器的软件 FileZillaCli ...

  9. FW:主流RPC框架

    主流RPC框架  2015年10月27日  zman  RPC 介绍目前在互联网公司比较流行的开源的RPC框架. RPC框架比较   语言 协议 ​服务治理 ​社区 机构 Hessian 多语言 he ...

  10. python(五)常用模块学习

    版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. https://blog.csdn.net/fgf00/article/details/52357 ...