| Coins |
| accepted:78 |
|
submit:188 |
| 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 she 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 this m continuous coins head face on among all possible situations. The situations is considered different if and only if there was at least one position, the coins ' faces is different. Input The first line contains an integer T (no more than) which represents the number of test cases. In each test case, the a line contains the integers n and M. () Output For each test case, the output of the result modulo in one line. Sample Input 2 4 2 5 2
Sample Output 8 19
Source XTU Onlinejudge |
Reprint Please specify the Source: Looking for Children & stars
Title Link: http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1233
#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 OFF (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 MO D 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 coins altogether have n^2 states scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); DP[M][0] = 1;//initial value, only m coins must be all face up, only 1 kinds of dp[m][1] = (a[m]-dp[m][0]+mod)%mod;//total scheme minus face up scheme number is not feasible foR (i = 0; i<m; i++)//Less than M, then a full state dp[i][1] = a[i]; for (i = m+1; i<=n; i++) {/* I satisfies the condition first from the I-1 enumeration, the i-1 state satisfies, then adds a coin can be reversed, so dp[i-1][0]*2 */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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Coins (2015 Ka Jie information Cup +dp)