Coins(2015嘉杰信息杯+dp)

Coins

Accepted : 78   Submit : 188
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.()

Output

For each test case, output the result modulo  in one line.

Sample Input

2
4 2
5 2

Sample Output

8
19


 

Source

XTU OnlineJudge

 

 

转载请注明出处:寻找&星空の孩子

题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1233

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

LL dp[N][2];
LL a[N];
int main()
{
    int t,i,j,n,m;
    a[0] = 1;
    for(i = 1; i<N; i++)
        a[i] = ((LL)2*a[i-1])%MOD;//n个硬币一共有n^2个状态
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        dp[m][0] = 1;//初始值,只有m个硬币呢么必须全部面朝上,只有1种
        dp[m][1] = (a[m]-dp[m][0]+MOD)%MOD;//总方案数减去面朝上的方案数为不可行的
        for(i = 0; i<m; i++)//不足m个,则为全状态
            dp[i][1] = a[i];
        for(i = m+1; i<=n; i++)
        {
            /*
            i个满足的状况首先从i-1个枚举来,i-1的状态满足的话,那么再加一个硬币可正可反,所以dp[i-1][0]*2
            */
            dp[i][0] = ((dp[i-1][0]*2+MOD)+dp[i-1-m][1])%MOD;
            dp[i][1] = (a[i]-dp[i][0]+MOD)%MOD;
        }
        printf("%I64d\n",dp[n][0]);
    }

    return 0;
}


 

 

你可能感兴趣的:(Coins(2015嘉杰信息杯+dp))