有n张牌,求出至少有k张牌连续是正面的排列的种数。(1=<k<=n<=100)

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
 
/*
思路:
ps(a数组内部的递推计算本来应该也用高精度整数来计算,但是被我省略了,因为太懒了)
(把至少k个正面)转换为(至多n个正面)-(至多k-1个正面)
对于(至多n个正面),它等于2^n,考虑到n比较大,用高精度大整数来计算出2^n 对于(至多k-1个正面)
先根据k-1是否为0定义f[1][1]的初始值
f[1][2]肯定为1
ps.数组的第二维中1代表正,2代表反的总情况数
由于对反面牌没有要求,所以:
第i次为反情况数=第i-1次为正情况数+第i-1次为反情况数 如果i<=k-1,随便放,那么第i次为正情况数=第i-1次为正情况数+第i-1次为反情况数 如果i=(k-1)+1,那么第i次为正情况数=第i-1次为正情况数+第i-1次为反情况数-1
那个-1是减去前面全是正面的情况
如果i>(k-1)+1,那么第i次为正情况数=第i-1次为正情况数+第i-1次为反情况数-第i-(k-1)-1次为反情况数
那个-第i-(k-1)-1次为反情况数 是排除第i-(k-1)-1次为反而且中间全是正的情况的情况
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn=;
ll a[maxn][];
ll big[maxn];
int main()
{
ll n,k;
cin>>n>>k;
big[]=;
big[]=;
for(ll i=;i<=n;i++)
{
for(ll i=;i<=big[];i++)
big[i]*=;
for(ll i=;i<=big[];i++)
{
if(big[i]>=)
{
big[i+]+=big[i]/;
big[i]%=;
}
if(big[big[]+]>)
big[]++;
}
} if(k-==)
{
a[n][]=,a[n][]=;
goto dog;
} a[][]=;a[][]=;
for(ll i=;i<=n;i++)
{
a[i][]=a[i-][]+a[i-][]; if(i<=k-)
a[i][]=a[i-][]+a[i-][];
else if(i==k-+)
a[i][]=a[i-][]+a[i-][]-;
else
a[i][]=a[i-][]+a[i-][]-a[i-(k-)-][];
}
dog:;
big[]-=a[n][]+a[n][];
while(big[]<)
{
big[]+=pow(,big[]-);
big[big[]]--;
if(big[big[]]==)
{
big[]--;
}
}
for(ll i=;i<=big[];i++)
{
if(big[i]>=)
{
big[i+]+=big[i]/;
big[i]%=;
}
if(big[big[]+]>)
big[]++;
}
for(ll i=big[];i>=;i--)
printf("%lld",big[i]);
return ;
}

Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)的更多相关文章

  1. attack on titans(动态规划递推,限制条件,至少转至多方法,进击的巨人)

    题目意思: 给n个士兵排队,每个士兵三种G.R.P可选,求至少有m个连续G士兵,最多有k个连续R士兵的排列的种数. 原题 Attack on Titans Time Limit: 2 Seconds ...

  2. 递推DP(至少和至多之间的转换

    UVa 10328 - Coin Toss 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 转换成抛N次至多连续有N个减去抛N次至多连续有K-1个1的情况 dp[i][k]表 ...

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

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

  4. 最长上升子序列(动态规划递推,LIS)

    1759:最长上升子序列 题目: 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的 ...

  5. 最大子段和(洛谷P1115,动态规划递推)

    洛谷题目链接 题目赋值出来格式有问题,所以我就只放题目链接了 下面为ac代码 #include<bits/stdc++.h> #define ll long long using name ...

  6. UVa 10520【递推 搜索】

    UVa 10520 哇!简直恶心的递推,生推了半天..感觉题不难,但是恶心,不推出来又难受..一不小心还A了[]~( ̄▽ ̄)~*,AC的猝不及防... 先递推求出f[i][1](1<=i< ...

  7. Uva 10446【递推,dp】

    UVa 10446 求(n,bcak)递归次数.自己推出来了一个式子: 其实就是这个式子,但是不知道该怎么写,怕递归写法超时.其实直接递推就好,边界条件易得C(0,back)=1.C(1,back)= ...

  8. UVa 10943 (数学 递推) How do you add?

    将K个不超过N的非负整数加起来,使它们的和为N,一共有多少种方法. 设d(i, j)表示j个不超过i的非负整数之和为i的方法数. d(i, j) = sum{ d(k, j-1) | 0 ≤ k ≤ ...

  9. UVa 557 (概率 递推) Burger

    题意: 有两种汉堡给2n个孩子吃,每个孩子在吃之前要抛硬币决定吃哪一种汉堡.如果只剩一种汉堡,就不用抛硬币了. 求最后两个孩子吃到同一种汉堡的概率. 分析: 可以从反面思考,求最后两个孩子吃到不同汉堡 ...

随机推荐

  1. gradle使用笔记

    1 gradle user home 默认情况下是-/.gradle目录.可以使用gradle -g [directory]修改. 1.1 ./gradle/caches gradle下载的所有的依赖 ...

  2. 深度理解apache 重写模块rewrite_mod,重写不再犯错

    1.RewriteRule ^(com\/.*)$ index.php?do=$1 问:上面的规则匹配表达式 "^(.*)$" 匹配的内容是什么 答:匹配内容是URI站点目录:/d ...

  3. ARM+llinux系统移植3G拨号上网收发短信(一)【转】

    本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17099737 一.      PPP移植 各项工作具体说明 向Linux内核添加3 ...

  4. hdoj--1716--排列2(暴力水题)

     排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  5. [Codeforces 482A] Diverse Permutation

    [题目链接] https://codeforces.com/contest/482/problem/A [算法] 首先构造一个(k + 1)个数的序列 , 满足它们的差为1-k 对于i > k ...

  6. maven pom 详细配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd

    昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...

  8. swoole多进程处理产生的问题

    以前用swoole的时候,没有涉及到数据库连接,碰到问题没有那么多,后来公司业务原生来写swoole多进程,问题出现很多 1.多进程之间会产生进程隔离,global无效,不能共用一个mysql,red ...

  9. java Class.getResource和ClassLoader.getResource

    http://www.cnblogs.com/wang-meng/p/5574071.html http://blog.csdn.net/earbao/article/details/50009241 ...

  10. String field contains invalid UTF-8 data when serializing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.

    [libprotobuf ERROR google/protobuf/wire_format.cc:1053] String field contains invalid UTF-8 data whe ...