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. 你自认为理解了JavaScript?【转】

    第一题 if (!("a" in window)) { var a = 1; } alert(a); 第二题 var a = 1, b = function a(x) { x &a ...

  2. poj 1837 Balance(背包)

    题目链接:http://poj.org/problem?id=1837 Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  3. css控制文字换行

    1.word-wrap 设置为break-word时,文本中的长单词或url可以换行 <p style="width:100px;word-wrap:break-word;border ...

  4. javascript中=、==与===的区别

    1.等号 =赋值运算符,给变量赋值 var a="1"; 2.相等和不相等操作符 相等操作符由==表示,若两个操作数相等,则返回true:不相等操作符由!=表示,若两个操作数不相等 ...

  5. JavaWeb使用Session防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 1.什么是表单 ...

  6. 【OneNote】使用线性格式输入数学公式

    在OneNote中按Alt+=,就可以开始输入公式. # 对齐公式数组 可以使用@和&来实现,如 \eqarray(x+1&=2@1+2+3+y&=z@3/x&=6)& ...

  7. x64dbg

    https://x64dbg.com/ https://github.com/x64dbg/x64dbg https://sourceforge.net/projects/x64dbg/files/s ...

  8. 简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环)

    简单的自动化运维工具(shell+except+whiptail+功能模块化函数+循环) http://www.cnblogs.com/M18-BlankBox/p/5881700.html

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

  10. android datepicker timepicker简单用法

    1.效果图 2. xml布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...