XTU1233 Coins

Coins

Accepted : 85
Submit : 202
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

题意:抛n枚银币,连续m次正面朝上的次数为多少。
分析:可以用dp来写,用一个a[n]存储n枚银币的所有可能数;dp[i][1]表示已经有连续m次正面朝上了,dp[i][0]表示没有。从dp[m][ ]开始更新,因为少于m的时候全为不可能;大于m的时候分已存在dp[i-1][1]*2和刚好存在两种情况dp[i-1-m][0];所以可得dp[i][1]=dp[i-1][1]*2+dp[i-1-m][0],dp[i][0]=a[i]-dp[i][1]。

<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define lson (i<<1)
#define rson ((i<<1)|1)
#define MAXN 1000010

int n,m;
ll dp[MAXN][2];
ll a[MAXN];

int main()
{
    a[0] =  1;
    for(int i=1; i<MAXN; i++)
        a[i] = a[i-1]*2%MOD;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        dp[m][1] = 1;
        dp[m][0] = (a[m]-dp[m][1]+MOD)%MOD;
        for(int i=0; i<m; i++)
            dp[i][0] = a[i];
        for(int i=m+1; i<=n; i++)
        {
            dp[i][1] = (dp[i-1][1]*2+dp[i-1-m][0])%MOD;
            dp[i][0] = (a[i]-dp[i][1]+MOD)%MOD;
        }
        printf("%lld\n",dp[n][1]);
    }
    return 0;
}
</span>


你可能感兴趣的:(dp,湘潭)