【UVA】【10328】

总结 变量名add

Toss is an important part of any event. When everything becomes equal toss is the ultimate decider.
Normally a fair coin is used for Toss. A coin has two sides head(H) and tail(T). Superstition may work
in case of choosing head or tail. If anyone becomes winner choosing head he always wants to choose
head. Nobody believes that his winning chance is 50-50. However in this problem we will deal with a
fair coin and n times tossing of such a coin. The result of such a tossing can be represented by a string.
Such as if 3 times tossing is used then there are possible 8 outcomes.
HHH HHT HTH HTT THH THT TTH TTT
As the coin is fair we can consider that the probability of each outcome is also equal. For simplicity
we can consider that if the same thing is repeated 8 times we can expect to get each possible sequence
once.
In the above example we see 1 sequence has 3 consecutive H, 3 sequence has 2 consecutive H and 7
sequence has at least single H. You have to generalize it. Suppose a coin is tossed n times. And the
same process is repeated 2n times. How many sequence you will get which contains a sequence of H of
length at least k.
Input
The input will start with two positive integer, n and k (1 ≤ k ≤ n ≤ 100). Input is terminated by
EOF.
Output
For each test case show the result in a line as specified in the problem statement.
Sample Input
4 1
4 2
4 3
4 4
6 2
Sample Output
15
8
3
1
43


-----------------------------------------------

【】【】【】【】【H】...【H】【H】【H】
                             i-k                         i              


然后排出这种情况。 这种情况i-k 到 i都是 H




这个是定了的


所以考虑dp[i-k-1][1] 就可以了


dp[i-k-1][1] 是
【】【】【】【T】          
那么在dp[i-k-1][1] 后面加上k+1个H 的种类数和 dp[i-k-1][1]的种类数一样多
也就是        
【】【】【】【T】   
和   
【】【】【】【T】【H】...【H】【H】【H】
                      i-k-1       i-k                         i     

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;

int n,k;
const int maxn = 110;
int dp[maxn][2][maxn]; //dp[maxn][0][maxn] 正面 dp[maxn][1][maxn] 反面
int res[maxn];         //dp[0][0][] = 1 dp[0][1]
int ans[maxn];
int tmp[maxn];
const int quote = 10000;
// 求正面的数量 <= upp

void add(int left[maxn], int right[maxn])
{
	memset(res,0,sizeof(res));
	int add = 0;
	for(int i=0;i<100;i++)
	{
		res[i] = (left[i] + right[i] + add) % quote;
		add = (left[i] + right[i] + add) / quote;
	}

}

void sub(int large[maxn],int small[maxn])
{
	memset(res,0,sizeof(res));
	int add = 0;
	for(int i=0;i<100;i++)
	{
		if(-add + large[i] - small[i] >= 0)
		{
			res[i] = - add + large[i] - small[i];
			add = 0;
		}
		else
		{
			res[i] = - add + large[i] - small[i] + quote;
			add = 1;
		}
	}
}
void fun(int upp)
{
	memset(dp,0,sizeof(dp));
	dp[0][0][0]  =  0;
	dp[0][1][0]  =  1;
	for(int i=1;i<=n;i++)
	{
		//dp[i][0][] 正面
		if(i <= upp) //dp[i][0]* = dp[i-1][0]* + dp[i-1][1]*;
		{
			memset(res,0,sizeof(res));
			add(dp[i-1][0],dp[i-1][1]);
			memcpy(dp[i][0],res,sizeof(res));
		}
		else// dp[i][0]* = dp[i-1][0]* + dp[i-1][1]* - dp[i-upp-1][1]*;
		{

			memset(res,0,sizeof(res));
			add(dp[i-1][0],dp[i-1][1]);
			memcpy(tmp,res,sizeof(res));

			memset(res,0,sizeof(res));
			sub(tmp,dp[i-upp-1][1]);
			memcpy(dp[i][0],res,sizeof(res));
		}

		//反面
		//dp[i][1]* = dp[i-1][0]* + dp[i-1][1]*;
		memset(res,0,sizeof(res));
		add(dp[i-1][0],dp[i-1][1]);
		memcpy(dp[i][1],res,sizeof(res));
	}
	//dp[n][0]* + dp[n][1]*;
	memset(res,0,sizeof(res));
	add(dp[n][0],dp[n][1]);
}
void print(int res[maxn])
{
	int pos = 0;
	for(int i=maxn-1;i>=0;i--)
	{
		if(res[i] != 0)
		{
			pos = i;
			break;
		}
	}
	printf("%d",res[pos]);
	for(int i=pos-1;i>=0;i--)
	{
		printf("%04d",res[i]);
	}
	printf("\n");

}
int main()
{
	//freopen("D://1.txt","r",stdin);
	while(scanf("%d%d",&n,&k) != EOF)
	{
		memset(dp,0,sizeof(dp));
		memset(res,0,sizeof(res));
		memset(ans,0,sizeof(ans));
		memset(tmp,0,sizeof(tmp));
		//
		ans[0] = 1;
		memset(res,0,sizeof(res));
		for(int i=0;i<n;i++)
		{
			memset(res,0,sizeof(res));
			add(ans,ans);
			memcpy(ans,res,sizeof(res));
		}		
		memcpy(ans,res,sizeof(res));  // This is ok checkpoint

		//print(ans);
		fun(k-1);
		memset(tmp,0,sizeof(tmp));
		memcpy(tmp,res,sizeof(res));

		memset(res,0,sizeof(res));
		sub(ans,tmp);
		print(res);

	}

	//TEST
	//memset(res,0,sizeof(res));
	//res[0] = 1111;
	//res[1] = 223;
	//res[2] = 99;
	//print(res);
//while(1);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值