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
题意:
抛n个硬币,求连续出现m个以上正面朝上的次数
思路:
dp[i][0]表示满足条件的情况数量
dp[i][1]表示不满足条件的情况数量
一开始的想法是组合数,但是无奈数学比较差,老是推不出,后来觉得dp也是可行的
#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-1-m][1],这是对于连续m个面朝上看做一个整体,然后包括剩下不可行的方案数 */ 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; }
时间: 2024-10-10 05:31:36