Coins

Accepted : 120   Submit : 305
Time Limit : 1000 MS   Memory Limit : 65536 KB

Coins

Problem Description:

Duoxida buys a bottle of MaiDong from a vending machine and the machine give her n coins back. She places them in a line randomly showing head face or tail face on. And Duoxida wants to know how many situations that m continuous coins head face on among all possible situations. Two situations are considered different if and only if there is at least one position that the coins' faces are different.

Input

The first line contains a integer T(no more than 20) which represents the number of test cases.

In each test case, a line contains two integers n and m.(\\(0 < m \\le n \\le 10^6\\))

Output

For each test case, output the result modulo \\(10^9+7\\) in one line.

Sample Input

2
4 2
5 2

Sample Output

8
19

Source

XTU OnlineJudge 


这题的话dp起来还是比较简单的。

dp[0][i]代表不满足情况下长度为i的硬币串数量。

dp[1][i]代表满足情况下长度为i的硬币串数量。

对于i<m 显然所有状态都不满足连续m个硬币正面朝上,d[0][i]=2^i,dp[1][i]=0;

对于i=m 那么只有一种情况连续m个硬币正面朝上,d[0][i]=2^i-1,dp[1][i]=1;

对于i>m,对于满足的情况,考虑到所有状况并且为了避免状况重复,他可以从i-1长度下满足的情况下,在i位置添加一个任意面得到i长度下满足的情况;也能从i-m-1长度的不满足的情况下,在其之后添加一个反面朝上,然后添加m个正面朝上来得到i长度下满足的情况,注意这样的情况是最长长度为m,并且连续的长度最后一位在i位置,与前一种方式的出来的情况没有重复部分。而且两者包含所有的满足的情况,所以,dp[1][i]=dp[1][i-1]*2+dp[0][i-m-1]。不满足的情况即为所有状态减去满足的情况:dp[0][i]=2^i-dp[1][i]。因此此题只要O(n)即能dp得出答案。

 #include<cstdio>
#include<iostream>
#include<cstring>
#define clr(x) memset(x,0,sizeof(x))
#define LL long long
#define MOD (1000000007)
using namespace std;
LL dp[][];
LL pow[];
int main()
{
int n,m;
int T;
scanf("%d",&T);
pow[]=;
for(int i=;i<=;i++)
{
pow[i]=(pow[i-]*)%MOD;
}
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
dp[][i]=;
dp[][i]=pow[i];
}
dp[][m]=;
dp[][m]=pow[m]-dp[][m];
for(int i=m+;i<=n;i++)
{
dp[][i]=(dp[][i-]*+dp[][i-m-])%MOD;
dp[][i]=((pow[i]-dp[][i])%MOD+MOD)%MOD;
}
printf("%lld\n",dp[][n]);
}
return ;
}

dp

xtuoj 1233 coins(dp)的更多相关文章

  1. XTU 1233 Coins(DP)

    题意: n个硬币摆成一排,问有连续m个正面朝上的硬币的序列种数. 很明显的DP题.定义状态dp[i][1]表示前i个硬币满足条件的序列种数.dp[i][0]表示前i个硬币不满足条件的序列种数. 那么显 ...

  2. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  3. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

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

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

  5. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  6. HDU 1398 Square Coins(DP)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. POJ 1742 Coins DP 01背包

    dp[i][j]表示前i种硬币中取总价值为j时第i种硬币最多剩下多少个,-1表示无法到达该状态. a.当dp[i-1][j]>=0时,dp[i][j]=ci; b.当j-ai>=0& ...

  8. uva--562Dividing coins +dp

    题意: 给定一堆硬币,然后将他们分成两部分,使得两部分的差值最小;输出这个最小的差值. 思路: 想了好久都没想到一个合适的状态转移方程.后面看了别人的题解后,才知道能够转成背包问题求解. 我们将全部的 ...

  9. CodeChef Cards, bags and coins [DP 泛型背包]

    https://www.codechef.com/problems/ANUCBC n个数字,选出其一个子集.求有多少子集满足其中数字之和是m的倍数.n $\le$ 100000,m $\le$ 100 ...

随机推荐

  1. 【BZOJ】1552/3506 [Cerc2007]robotic sort

    [算法]splay [题解] splay维护序列,用权值(离散化)作为编号.每次找第i小的话直接找对应编号splay即可. 但是这样splay没有下传翻转标记?直接暴力找到路径然后从根到改结点push ...

  2. ajax post请求json数据在spring-controller解析

    1.前端post请求数据: userInfo=[{"id":"5","uname":"小李","phone&q ...

  3. spring-retry 重试机制

    业务场景 应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务逻辑包装给处理方法返回处理结果:第二步拿 ...

  4. make command explaination 編譯命令解釋

    Creating .config file make ARCH=arm CROSS_COMPILE=arm-none-eabi- stm32_defconfig 以上命令是 將變數 ARCH=arm, ...

  5. tcp窗口机制(写的最简单精炼的文章)

    tcp窗口机制(写的最简单精炼的文章) http://blog.csdn.net/occupy8/article/details/48468445

  6. sicily 1052. Candy Sharing Game

    Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description A number of students sit in a circle ...

  7. swift 动态获取类, 获取命名空间

    在做swift开发中很多时候会动态加载控制器的类, 可以让app更加灵活显示界面信息 一般情况下都是服务器返回显示的控制器类name然后动态显示, 但是服务器返回的类name是string, 怎么转换 ...

  8. iframe自适应高度的方法

    不带边框的iframe因为能和网页无缝的结合从而不刷新新页面的情况下实现更新页面部分的数据成为可能,可是iframe却不像层那样可以收缩自如,iframe高度需要动态的调整需要JS来配合使用,只能通过 ...

  9. django “如何”系列7:错误汇报

    当你正在运行一个公共的站点的时候,你应该关掉DEBUG设置.这将使你的服务器运行的更快,同时也能预防别有用心的用户从你的错误页面看到你应用的一些详细配置信息.然而,当debug为false的时候,你将 ...

  10. CodeForces 450B

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...