XTU1233:Coins

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-08-11 03:37:05

XTU1233:Coins的相关文章

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【背包专题】 D - Coins hdu2844【多重背包】

Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he kn

【动态规划】Gym - 101102A - Coins

Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of N coins and Bahosain has a set of M coins. The video game costs W JDs. Find the number of ways in which they can pay exactly W JDs such that the diff

H - Coins

H - Coins Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to

poj 1742 Coins(dp之多重背包+多次优化)

Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact pri

F - Coins

Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1742 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and fou

uva562 - Dividing coins(01背包)

题目:uva562 - Dividing coins(01背包) 题目大意:给出N个硬币,每个硬币有对应的面值.要求将这些硬币分成两部分,求这两部分最小的差值. 解题思路:先求这些硬币能够凑出的钱(0, 1背包),然后再从sum(这些硬币的总和)/2开始往下找这个值能否由这些硬币凑出.要注意的是,可以由前n个硬币组成那样也是可以组成的面值. 代码: #include <cstdio> #include <cstring> const int N = 105; const int m

Coins POJ - 1742

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without c