XTU1233 Coins

本文探讨了在给定数量的银币中,如何通过动态规划算法计算出连续出现指定次数正面朝上的可能性。通过分析银币排列组合的特性,作者详细解释了如何使用状态转移方程解决此问题,并提供了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Coins

Accepted : 85
Submit : 202
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来写,用一个a[n]存储n枚银币的所有可能数;dp[i][1]表示已经有连续m次正面朝上了,dp[i][0]表示没有。从dp[m][ ]开始更新,因为少于m的时候全为不可能;大于m的时候分已存在dp[i-1][1]*2和刚好存在两种情况dp[i-1-m][0];所以可得dp[i][1]=dp[i-1][1]*2+dp[i-1-m][0],dp[i][0]=a[i]-dp[i][1]。

<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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值