(dp)湘潭市赛,A.Coins

Coins

Accepted : 66 Submit : 167
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[i]表示前i个硬币满足条件的方案数,dp[i] = 2 * dp[i - 1] + (2^(i - m - 1) - dp[i - m - 1]):
	分两种情况:
	①当第i位对约束无关紧要(即前i-1位已经满足约束),那么这种情况对答案的贡献为2 * dp[i - 1];
	②当第i位对约束是关键的(即前i-1位并不满足约束),那么[i-m+1,m]上的硬币一定正面朝上,且第m个硬币一定反面朝上,对答案的贡献为(2^(i - m - 1) - dp[i - m - 1];
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1000010;
const int mod = 1000000007;
long long p2[N], dp[N];

void solve() {
	int n, m;
	scanf("%d%d", &n, &m);
	memset(dp, 0, sizeof(dp));
	dp[m] = 1;
	for (int i = m + 1; i <= n; i++) {
		dp[i] = (2 * dp[i - 1] + p2[i - m - 1] - dp[i - m - 1]) % mod;
		if (dp[i] < 0) dp[i] += mod;
	}
	printf("%d\n", (int)dp[n]);
}

int main()
{
	p2[0] = 1;
	for (int i = 1; i < N; i++) p2[i] = p2[i - 1] * 2 % mod;
	int t;
	scanf("%d", &t);
	while (t--) solve();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值