Coin Toss

Time Limit: 3000ms
Memory Limit: 131072KB

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的更多相关文章

  1. UVA 10328 - Coin Toss dp+大数

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  2. UVa 10328 - Coin Toss (递推)

    题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 原题中问出现连续至少k个H的情况,很难下手.我们可以试着将问题转化一下. 设dp[i][j]表示抛掷i个硬币出现连续至多j个H ...

  3. UVa 10328 Coin Toss(Java大数+递推)

    https://vjudge.net/problem/UVA-10328 题意: 有H和T两个字符,现在要排成n位的字符串,求至少有k个字符连续的方案数. 思路:这道题目和ZOJ3747是差不多的,具 ...

  4. uva 10328 - Coin Toss 投硬币(dp递推,大数)

    题意:抛出n次硬币(有顺序),求至少k个以上的连续正面的情况的种数. 思路:转换成求抛n个硬币,至多k-1个连续的情况种数,用所有可能出现的情况种数减去至多k-1个的情况,就得到答案了.此题涉及大数加 ...

  5. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

  6. UVA.674 Coin Change (DP 完全背包)

    UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...

  7. Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)

    有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...

  8. UVa 674 Coin Change【记忆化搜索】

    题意:给出1,5,10,25,50五种硬币,再给出n,问有多少种不同的方案能够凑齐n 自己写的时候写出来方案数老是更少(用的一维的) 后来搜题解发现,要用二维的来写 http://blog.csdn. ...

  9. 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 ...

随机推荐

  1. 2本Hadoop技术内幕电子书百度网盘下载:深入理解MapReduce架构设计与实现原理、深入解析Hadoop Common和HDFS架构设计与实现原理

    这是我收集的两本关于Hadoop的书,高清PDF版,在此和大家分享: 1.<Hadoop技术内幕:深入理解MapReduce架构设计与实现原理>董西成 著  机械工业出版社2013年5月出 ...

  2. Android 6.0 中TimePicker显示为滚动样式的方法

    在Android6.0中,TimePicker控件的默认样式为转盘的样式,就像这个样子: 如果想要显示为之前的滚动样式的话也很简单,只要在布局文件中设置TimePicker的timePickerMod ...

  3. SqlServer 自动备份策略设置

    企业管理器中的Tools,Database Maintenance Planner,可以设置数据库的定期自动备份计划.并通过启动Sql server Agent来自动运行备份计划.具体步骤如下: 1. ...

  4. B1230 [Usaco2008 Nov]lites 开关灯 线段树

    就是线段树维护异或和.之前我线段树区间修改down都是修改当前区间,结果debug出不来,改成每次向下了. 题干: Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶 ...

  5. c++ string 解析ip

    比如输入是192.168.80.12-15,解析成192.168.80.12.192.168.80.13.192.168.80.14.192.168.80.15. #include <iostr ...

  6. 关于类和对象的进一步讨论之析构函数 C++

    析构函数也是一个特殊的成员函数.它的作用与构造函数相反.它的名字是在类名的前面加一个“~”符号.在C++中“~”是位取反运算符.当对象的生命结束时,会自动执行解析函数.以下几种情况会执行析构函数: 1 ...

  7. 如何通过免费开源ERP Odoo实现企业数字化转型深度分析(一)

    本文来自<开源智造企业数字化转型报告白皮书>的精选内容章节.请勿转载.欢迎您反馈阅读意见. 引言 在由消费者驱动的数字经济时代,创新之势锐不可挡.变革步伐从未如此迅速,并且还会越来越快.对 ...

  8. log4net实用配置代码

    log4net实用配置代码 <?xml version="1.0" encoding="utf-8" ?> <configuration> ...

  9. 2-bitmap

    在2.5亿个整数中找出不重复的整数,注,内存不足以容纳这2.5亿个整数. 思路: bitmap用一个bit来代表存在还是不存在,现在我们要判断重不重复,则需要三个状态:不存在,存在一个,存在多个.2b ...

  10. java热部署

    最近使用java做项目,研究了一下热部署,能够提高工作效率. 需要准备的工具: 1.安装文件http://update.zeroturnaround.com/update-site/ 2.破解 下载破 ...