XTU 1233 Coins(DP)
来源:互联网 发布:如何下载软件 编辑:程序博客网 时间:2024/06/06 05:07
Coins
Accepted : 95 Submit : 243Time Limit : 1000 MS Memory Limit : 65536 KBCoins
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
思路:本来以为是组合问题,可是愣是没想出来公式。 于是想到了DP,可是由于自己写的时候状态写错了TLE,后来看了别人的题解才看懂。
很是巧妙。 定义数组dp[i][2],dp[i][0]表示投掷i次硬币满足连续m次正面向上的情况数,dp[i][1]表示投掷i次硬币不满足连续m次正面向上的情况数,也就是说,dp[i][0]+dp[i][1]等于投掷i次硬币的情况总数。 而我们知道,投i次硬币的情况总数a[i]是2的i次方(每投一次有正和反两个状态)。
当i<m时,不可能有连续m次正面向上的情况,所以dp[i][0]=0,dp[i][1]=a[i]
当i==m时,恰好有一种情况可以满足,dp[i][0]=1,dp[i][1]=a[i]-1
当i>m,状态转移方程就是dp[i][0]=2*dp[i-1][0]+dp[i-m-1][1](i-1时就已经满足有连续m次正面向上的情况,那么第i个硬币可正可反,所以乘以2,还有一种情况是在第i个硬币投出正面从而恰好有连续m次正面向上的情况,那么也就是说从第i-m+1到第i个硬币一共m个硬币都是正面,这个时候必须明确第i-m个硬币必须是反面,想一想,为什么?
假设第i-m个硬币也是正面,那么i-m到i-1这m个硬币都是正面,这种情况是在dp[i-1][0]里面的(i-1时已经满足有连续m次正面向上的情况)
所以投掷第i个硬币刚好满足条件的情况数是dp[i-m-1][1])
dp[i][1]=(a[i]-dp[i][0]+mod)%mod注意必须加上mod,负数取模会出错。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 1000010#define ll long long#define mod 1000000007ll dp[N][2];ll a[N];int main(){ int T; int n,m; a[0]=1; for(int i=1; i<=N; i++) a[i]=(2*a[i-1])%mod; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); memset(dp,0,sizeof(dp)); for(int i=0; i<m; i++) dp[i][1]=a[i]; dp[m][0]=1; dp[m][1]=(a[m]-dp[m][0]+mod)%mod; for(int i=m+1; i<=n; i++) { dp[i][0]=(2*dp[i-1][0]+dp[i-1-m][1])%mod; dp[i][1]=(a[i]-dp[i][0]+mod)%mod; } printf("%lld\n",dp[n][0]); } return 0;}
- XTU 1233 Coins(DP)
- xtu 1233 Coins && zoj 3747 Attack on Titans
- Coins(背包DP)
- hdu2844 Coins(DP)
- Coins-(DP)
- poj 1742 Coins (DP)
- POJ 1742 Coins(dp)
- POJ 1742 Coins (dp)
- XTU 1233 n个硬币连续m个正面个数(dp)
- XTU OJ 1168 Alice and Bob (二维dp)
- XTU 1224 Endless Jump(dp)
- pku 1742 Coins(多重背包dp)
- HDU 2844 Coins(DP 背包)
- HDU3519Lucky Coins Sequence(DP+矩阵加速)
- POJ 1742 - Coins (dp 多重背包)
- Square Coins(dp-母函数)
- Uva 562-Dividing coins(DP)
- Coins (poj 1742 && hdu 2844 DP)
- leetcode 198. House Robber
- QNX Bootloader 启动顺序
- 银联支付-SDK使用、测试流程
- Start application automatically during controller boot-up
- lua中字符串匹配笔记
- XTU 1233 Coins(DP)
- Master-Detail Application的纯代码实现
- Java:重写equals()和hashCode()
- Android状态栏黑色字体
- leetcode 30. Substring with Concatenation of All Words
- Java环境配置问题,Eclipse打不开
- JS原型与原型链终极详解
- MFC中,句柄和指针的关系
- C++ STL