Coins | ||
Accepted : 85 | Submit : 202 | |
Time Limit : 1000 MS | Memory Limit : 65536 KB |
CoinsProblem 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. InputThe 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.() OutputFor each test case, output the result modulo in one line. Sample Input2 Sample Output8 SourceXTU OnlineJudge |
<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>