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. python学习笔记(九)之字符串

    定义字符串 >>> mystring = 'Hello Python' >>> name = str('Mountain') >>> mystri ...

  2. Vue前端开发规范(山东数漫江湖)

    一.强制 1. 组件名为多个单词 组件名应该始终是多个单词的,根组件 App 除外. 正例: export default { name: 'TodoItem', // ... } 反例: expor ...

  3. Apache服务器添加网站目录不在根目录的情况

    Apache原本根目录: /var/www 需要添加的新的Apache网站目录 /home/*** 在Apache服务器虚拟配置下添加一个站点 <VirtualHost *:> Serve ...

  4. poj 3104 Drying(二分查找)

    题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  5. 安装Vue.js devtools

    1.下载安装 https://github.com/vuejs/vue-devtools#vue-devtools 通过以上地址下载安装包,解压以后进入文件,按住shift,点击鼠标右键打开命令窗口 ...

  6. Linux内核死锁检测机制【转】

    转自:http://www.oenhan.com/kernel-deadlock-check 死锁就是多个进程(线程)因为等待别的进程已占有的自己所需要的资源而陷入阻塞的一种状态,死锁状态一旦形成,进 ...

  7. sicily 1059. Exocenter of a Trian

    Description Given a triangle ABC, the Extriangles of ABC are constructed as follows: On each side of ...

  8. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph 暴暴暴暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6152 题意:判定一个无向图是否有三个点的团或者三个点的独立集. 解法:Ramsey theorem,n ...

  9. JQUERY 提取多个元素 a img 的 src href

    <div class="abc"><a href="1.html"><img src="1.jpg"/> ...

  10. IE8下面的line-height的bug

    当line-height小于正常值时,超出的部分将被剪裁掉