# XTU 1233 Coins（DP）

313人阅读 评论(0)

## Coins

Accepted : 95   Submit : 243
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.

2
4 2
5 2

### Sample Output

8
19

dp[i][1]=(a[i]-dp[i][0]+mod)%mod注意必须加上mod，负数取模会出错。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 1000010
#define ll long long
#define mod 1000000007
ll dp[N][2];
ll a[N];
int main()
{
int T;
int n,m;
a[0]=1;
for(int i=1; i<=N; i++)
a[i]=(2*a[i-1])%mod;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
memset(dp,0,sizeof(dp));
for(int i=0; i<m; i++)
dp[i][1]=a[i];
dp[m][0]=1;
dp[m][1]=(a[m]-dp[m][0]+mod)%mod;
for(int i=m+1; i<=n; i++)
{
dp[i][0]=(2*dp[i-1][0]+dp[i-1-m][1])%mod;
dp[i][1]=(a[i]-dp[i][0]+mod)%mod;
}
printf("%lld\n",dp[n][0]);
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：129710次
• 积分：5103
• 等级：
• 排名：第6313名
• 原创：396篇
• 转载：11篇
• 译文：0篇
• 评论：21条
最新评论