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;
}


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值