UVA 10328 Coin Toss
Coin Toss
This problem will be judged on UVA. Original ID: 10328
64-bit integer IO format: %lld Java class name: Main
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.
The Problem
In the above example we see 1 sequnce 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 2^n times. How many sequence you will get which contains a consequnce of H of length at least k.
The Input
The input will start with two positive integer, n and k (1<=k<=n<=100). Input is terminated by EOF.
The 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
解题:解题思路跟zoj 3747 一样
dp[i][0] 表示连续u个正面 且第i个是正面的方案数
需要注意的是 这道题目是需要用大数的,也就是需要高精度
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define MAXN 100
struct HP {
int len,s[MAXN];
HP() {
memset(s,,sizeof(s));
len=;
}
HP operator =(const char *num) { //字符串赋值
len=strlen(num);
for(int i=; i<len; i++) s[i]=num[len-i-]-'';
} HP operator =(int num) { //int 赋值
char s[MAXN];
sprintf(s,"%d",num);
*this=s;
return *this;
} HP(int num) {
*this=num;
} HP(const char*num) {
*this=num;
} string str()const { //转化成string
string res="";
for(int i=; i<len; i++) res=(char)(s[i]+'')+res;
if(res=="") res="";
return res;
} HP operator +(const HP& b) const {
HP c;
c.len=;
for(int i=,g=; g||i<max(len,b.len); i++) {
int x=g;
if(i<len) x+=s[i];
if(i<b.len) x+=b.s[i];
c.s[c.len++]=x%;
g=x/;
}
return c;
}
void clean() {
while(len > && !s[len-]) len--;
} HP operator *(const HP& b) {
HP c;
c.len=len+b.len;
for(int i=; i<len; i++)
for(int j=; j<b.len; j++)
c.s[i+j]+=s[i]*b.s[j];
for(int i=; i<c.len-; i++) {
c.s[i+]+=c.s[i]/;
c.s[i]%=;
}
c.clean();
return c;
} HP operator - (const HP& b) {
HP c;
c.len = ;
for(int i=,g=; i<len; i++) {
int x=s[i]-g;
if(i<b.len) x-=b.s[i];
if(x>=) g=;
else {
g=;
x+=;
}
c.s[c.len++]=x;
}
c.clean();
return c;
}
HP operator / (const HP &b) {
HP c, f = ;
for(int i = len-; i >= ; i--) {
f = f*;
f.s[] = s[i];
while(f>=b) {
f =f-b;
c.s[i]++;
}
}
c.len = len;
c.clean();
return c;
}
HP operator % (const HP &b) {
HP r = *this / b;
r = *this - r*b;
return r;
} HP operator /= (const HP &b) {
*this = *this / b;
return *this;
} HP operator %= (const HP &b) {
*this = *this % b;
return *this;
} bool operator < (const HP& b) const {
if(len != b.len) return len < b.len;
for(int i = len-; i >= ; i--)
if(s[i] != b.s[i]) return s[i] < b.s[i];
return false;
} bool operator > (const HP& b) const {
return b < *this;
} bool operator <= (const HP& b) {
return !(b < *this);
} bool operator == (const HP& b) {
return !(b < *this) && !(*this < b);
}
bool operator != (const HP &b) {
return !(*this == b);
}
HP operator += (const HP& b) {
*this = *this + b;
return *this;
}
bool operator >= (const HP &b) {
return *this > b || *this == b;
} }; istream& operator >>(istream &in, HP& x) {
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator <<(ostream &out, const HP& x) {
out << x.str();
return out;
}
const int maxn = ;
HP dp[maxn][];//dp[i][0]表示第i个正
int n,k;
HP solve(int u){
dp[][] = ;
dp[][] = ;
for(int i = ; i <= n; ++i){
if(i <= u) dp[i][] = dp[i-][] + dp[i-][];
if(i == u + ) dp[i][] = dp[i-][] + dp[i-][] - ;
if(i > u + ) dp[i][] = dp[i-][] + dp[i-][] - dp[i - u - ][];
dp[i][] = dp[i-][] + dp[i-][];
}
return (dp[n][] + dp[n][]);
}
int main(){
while(~scanf("%d%d",&n,&k))
cout<<solve(n) - solve(k-)<<endl;
return ;
}
UVA 10328 Coin Toss的更多相关文章
- UVA 10328 - Coin Toss dp+大数
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- UVa 10328 - Coin Toss (递推)
题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 原题中问出现连续至少k个H的情况,很难下手.我们可以试着将问题转化一下. 设dp[i][j]表示抛掷i个硬币出现连续至多j个H ...
- UVa 10328 Coin Toss(Java大数+递推)
https://vjudge.net/problem/UVA-10328 题意: 有H和T两个字符,现在要排成n位的字符串,求至少有k个字符连续的方案数. 思路:这道题目和ZOJ3747是差不多的,具 ...
- uva 10328 - Coin Toss 投硬币(dp递推,大数)
题意:抛出n次硬币(有顺序),求至少k个以上的连续正面的情况的种数. 思路:转换成求抛n个硬币,至多k-1个连续的情况种数,用所有可能出现的情况种数减去至多k-1个的情况,就得到答案了.此题涉及大数加 ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)
有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...
- UVa 674 Coin Change【记忆化搜索】
题意:给出1,5,10,25,50五种硬币,再给出n,问有多少种不同的方案能够凑齐n 自己写的时候写出来方案数老是更少(用的一维的) 后来搜题解发现,要用二维的来写 http://blog.csdn. ...
- UVA 674 Coin Change (DP)
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make c ...
随机推荐
- RedisUtil: Jedis连接自动释放
package cloud.app.prod.home.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedi ...
- spring拦截器和注解处理日志操作
整体思想:通过拦截器拦截所有的请求,处理含有自定义注解的方法,通过request得到需要的参数. 拦截器代码: package com.zktx.platform.log2; import java. ...
- oc30--id
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject - (void)sleep; @ ...
- 【POJ 1964】 City Game
[题目链接] http://poj.org/problem?id=1964 [算法] 记f[i]表示第i行最多向上延伸的行数 然后,对于每一行,我们用单调栈计算出这一行向上延伸的最大矩形面积,取最大值 ...
- B4010 菜肴制作 拓扑排序(附随机跳题代码)
今天写了一个自己的随机跳题小程序,第一次试发现跳的全是不可做题,但是在周围我一眼看见了这个题,不能说一眼看出来,但是也是比较有思路,所以就做他了! 做得比较顺利,做完之后美滋滋,突然发现样例第三组过不 ...
- leetcode链表相关
目录 2/445两数相加 综合题(328奇偶链表, 206反转链表, 21合并两个有序链表 ) 92反转链表 II 链表排序(148排序链表, 876链表的中间结点) 142环形链表 II 160相交 ...
- C指针基础知识
指针的声明 C语言声明格式:"类型 变量名;" 基本类型:int hoge; 指针类型:int *pointer; 区别在于: 声明 含义 int hoge; 声明整数类型的变量 ...
- ASP.NET MVC + 工厂模式 + 三层 + 缓存
最近将手头的项目总结整理了一下,以方便自己的学习.... 下面直接上图先介绍项目的结构图: 项目是ASP.NET MVC 4.0的应用程序,DBUtility这个类库主要是DbHelper操作数据库的 ...
- 理解list和vector的区别
原文:http://genwoxuevc.blog.51cto.com/1852984/503337 vector和数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随机存取( ...
- dubbo之结果缓存
结果缓存,用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用户加缓存的工作量. lru 基于最近最少使用原则删除多余缓存,保持最热的数据被缓存. threadlocal 当前线程缓存,比如 ...