XTU 1233 n个硬币连续m个正面个数(dp)
2015-06-14 21:46
399 查看
题面:
all possible situations. Two situations are considered different if and only if there is at least one position that the coins' faces are different.
In each test case, a line contains two integers n and m.()
4 2
5 2
19
题意:
求n个硬币连续m个硬币是正面的情况的个数。
解析:
dp[ i ] [ 1 ] 表示当前 i 个硬币 符合 m 个硬币正面的情况数。
dp[ i ] [ 0 ] 表示当前 i 个硬币 不符合m个硬币正面朝上的情况数。
状态转移方程:
dp[ i ] [ 1 ] = dp[ i - 1 ] [ 1 ] * 2 + dp [ i - 1 - m ] [ 0 ];
dp[ i ] [ 0 ] = (2 ^ i - dp[ i ] [ 1 ] + mod ) % mod;
解释:
i 长度符合m个正面朝上的个数 = i - 1长度正面朝上 * 2(可以随便加正反面硬币) + i - 1 - m 长度下不能的情况(连续加m个正面)。
i 长度不符合m个正面朝上 = 2^i次方 - 符合 + mod(没有这个wa, 因为出现了负数) 最后 % mod。
初始条件:
dp[ m ] [ 1 ] = 1。
dp[ m ] [ 0 ] = 2 ^ m - dp[ m ] [ 1 ] 。
代码:
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 amongall 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
24 2
5 2
Sample Output
819
Source
XTU OnlineJudge题意:
求n个硬币连续m个硬币是正面的情况的个数。
解析:
dp[ i ] [ 1 ] 表示当前 i 个硬币 符合 m 个硬币正面的情况数。
dp[ i ] [ 0 ] 表示当前 i 个硬币 不符合m个硬币正面朝上的情况数。
状态转移方程:
dp[ i ] [ 1 ] = dp[ i - 1 ] [ 1 ] * 2 + dp [ i - 1 - m ] [ 0 ];
dp[ i ] [ 0 ] = (2 ^ i - dp[ i ] [ 1 ] + mod ) % mod;
解释:
i 长度符合m个正面朝上的个数 = i - 1长度正面朝上 * 2(可以随便加正反面硬币) + i - 1 - m 长度下不能的情况(连续加m个正面)。
i 长度不符合m个正面朝上 = 2^i次方 - 符合 + mod(没有这个wa, 因为出现了负数) 最后 % mod。
初始条件:
dp[ m ] [ 1 ] = 1。
dp[ m ] [ 0 ] = 2 ^ m - dp[ m ] [ 1 ] 。
代码:
#include <iostream> #include <cstdio> using namespace std; #define LL long long #define lson lo, mi, rt << 1 #define rson mi + 1, hi, rt << 1 | 1 const int maxn = 1e6 + 10; const int mod = 1e9 + 7; LL dp[maxn][2]; LL two[maxn]; void init() { LL s = 1; for (int i = 0; i < maxn; i++) { two[i] = s % mod; s = (s << 1) % mod; } } int main() { #ifdef LOCAL //freopen("in.txt", "r", stdin); #endif // LOCAL init(); int ncase; scanf("%d", &ncase); while (ncase--) { int n, m; scanf("%d%d", &n, &m); dp[m][1] = 1; dp[m][0] = two[m] - dp[m][1]; for (int i = 0; i < m; i++) dp[i][0] = two[i]; for (int i = m + 1; i <= n; i++) { dp[i][1] = ((dp[i - 1][1] << 1) % mod + dp[i - 1 - m][0] % mod) % mod; dp[i][0] = (two[i] - dp[i][1] + mod) % mod; } printf("%lld\n", dp [1] % mod); } return 0; }
相关文章推荐
- 可执行程序要修改名字才能正常运行!
- IRP和IO_STACK_LOCATION结构的关联
- TCP/IP协议族-----5、IPv4地址
- hackerrank maxsum mod
- virtual详解
- linux环境编程之线程基础知识
- P、*P和&P三者的区别
- Java AIO 入门实例(转)
- The YubiKey -- COMPARISON OF VERSIONS
- Android NDK开发之 NDK 局部 全局引用
- HDU Fire Net (贪心)
- 冒泡排序—do-while语句实现
- The YubiKey NEO -- Smartcard features
- ROS-create and Building a ROS Package+中文翻译博客
- ACM学习历程—Hihocoder 1177 顺子(模拟 && 排序 && gcd)(hihoCoder挑战赛12)
- XML解析类CMarkup的使用方法
- 初识angular.js之爱恨情仇
- HDOJ 1022 Train Problem I
- 6.面向对象程序设计
- 合作之心灵洗礼