题目链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1269

10328 - Coin Toss

Time limit: 3.000 seconds
#### 问题描述
> 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.

输入

The input will start with two positive integer, n and k (1 ≤ k ≤ n ≤ 100). Input is terminated by

EOF.

输出

For each test case show the result in a line as specified in the problem statement.

样例输入

4 1

4 2

4 3

4 4

6 2

样例输出

15

8

3

1

43

题意

给你n个硬币,考虑正反的所有排列数:比如n=2:{HH,HT,TH,TT),然后问至少有k个的硬币连续正面朝上的总数。

题解

至少k个=2^n-最多k-1个。

所以我们可以转换成去求最多k个的问题

dp[k][i][0]表示前i个最多k个连续正面朝上,且第i个反面朝上的总数,

dp[k][i][1]表示前i个最多k个连续正面朝上,且第i个反面朝上的总数,

则我们只需要考虑扣掉最后连续k+1个正面朝上这种情况就可以转移了,写完之后上大整数。

代码

c++(没考虑数据溢出):

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=111; LL dp[maxn][maxn][2]; int n,m; void pre(){
clr(dp,0);
for(int k=0;k<maxn;k++){
dp[k][0][0]=1;
for(int i=1;i<maxn;i++){
dp[k][i][1]=dp[k][i][0]=dp[k][i-1][0]+dp[k][i-1][1];
if(i>k) dp[k][i][1]-=dp[k][i-k-1][0];
}
}
} int main() {
pre();
while(scf("%d%d",&n,&m)==2){
LL x=1;
rep(i,0,n) x*=2;
prf("%lld\n",x-dp[m-1][n][0]-dp[m-1][n][1]);
}
return 0;
} //end-----------------------------------------------------------------------

java:

import java.util.*;
import java.math.*; public class Main {
final static int maxn = 111; public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
BigInteger[][][] dp = new BigInteger[maxn][maxn][2]; for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
dp[i][j][1]=dp[i][j][0] = BigInteger.ZERO;
}
} for (int k = 0; k < maxn; k++) {
dp[k][0][0] = BigInteger.ONE;
for (int i = 1; i < maxn; i++) {
dp[k][i][1]=dp[k][i][0] = dp[k][i - 1][0].add(dp[k][i - 1][1]); if (i > k)
dp[k][i][1] = dp[k][i][1].subtract(dp[k][i - k - 1][0]);
}
} BigInteger[] x = new BigInteger[maxn];
x[0] = BigInteger.ONE;
for (int i = 1; i < maxn; i++)
x[i] = x[i - 1].add(x[i - 1]); while (cin.hasNext()) {
int n, m;
n = cin.nextInt();
m = cin.nextInt(); BigInteger ans = x[n].subtract(dp[m - 1][n][0]).subtract(dp[m - 1][n][1]); System.out.println(ans.toString()); }
}
}

UVA 10328 - Coin Toss dp+大数的更多相关文章

  1. UVA 10328 Coin Toss

    Coin Toss Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: ...

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

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

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

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

  4. UVa 10328 - Coin Toss (递推)

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

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

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

  6. UVA 674 Coin Change(dp)

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

  7. UVA 674 Coin Change 换硬币 经典dp入门题

    题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数. 经典的dp题...可以递推也可以记忆化搜索... 我个人比较喜欢记忆化搜索,递推不是很熟练. 记忆化搜索:很白 ...

  8. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  9. uva 674 Coin Change 换钱币【完全背包】

    题目链接:https://vjudge.net/contest/59424#problem/A 题目大意: 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值 ...

随机推荐

  1. 转 Linux会话浅析(写得极好,表述清楚语言不硬)

    说起会话,我们经常登录到linux系统,执行各种各样的程序,这都牵涉到会话.但是,一般情况下我们又很少会去关注到会话的存在,很少会去了解它的来龙去脉.本文就对linux会话相关的信息做一些整理,看看隐 ...

  2. php实现姓名按首字母排序的类与方法

    php将名字按首字母进行排序 <?php public function getFirstChar($s){ $s0 = mb_substr($s,0,3); //获取名字的姓 $s = ico ...

  3. T+API HTTPServer服务端

    该服务端是一个HTTP服务器,这样其他语言调用也方便. 出于某些原因,只支持Post方法,不打算支持其他方法,例如Get. API所接受的参数将以Json传送,回传的数据也是一个Json数据,一切只是 ...

  4. ETL项目1:大数据采集,清洗,处理:使用MapReduce进行离线数据分析完整项目

    ETL项目1:大数据采集,清洗,处理:使用MapReduce进行离线数据分析完整项目 思路分析: 1.1 log日志生成 用curl模拟请求,nginx反向代理80端口来生成日志. #! /bin/b ...

  5. sklearn的train_test_split,果然很好用啊!

    sklearn的train_test_split   train_test_split函数用于将矩阵随机划分为训练子集和测试子集,并返回划分好的训练集测试集样本和训练集测试集标签. 格式: X_tra ...

  6. POJ 3683 Priest John's Busiest Day 【2-Sat】

    这是一道裸的2-Sat,只要考虑矛盾条件的判断就好了. 矛盾判断: 对于婚礼现场 x 和 y,x 的第一段可以和 y 的第一段或者第二段矛盾,同理,x 的第二段可以和 y 的第一段或者第二段矛盾,条件 ...

  7. FlexPaper 里的pdf2json.exe 下载地址

    在使用FlexPaper 做在线阅读,需要使用到pdf2json.exe,将PDF转成JSON或者XML格式,网上很少下载的,现在提供一个下载的地址 http://pan.baidu.com/s/1i ...

  8. 《浅析Entity Framework Core中的并发处理》引起的思考

    看到一篇关于EF并发处理的文章,http://www.cnblogs.com/GuZhenYin/p/7761352.html,突然觉得为什么常见业务中为什么很少做并发方面的考虑.结合过去的项目,这样 ...

  9. 20155224 《实验二 Java面向对象程序设计》实验报告

    实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 没有Linux ...

  10. ruby学习笔记(3)- 新手入门

    这里是一个Ruby开发的快速参考指南: Ruby是什么 ? Ruby是一种纯粹的面向对象编程语言.它由日本松本幸创建于1993年. Ruby是一种通用的解释编程语言如Perl和Python. IRb是 ...