Topic Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 1269
Toss is a important part of any event. When everything becomes equal toss is the ultimate decider. Normally a fair coin is used for toss. A Coin has two sides head (H) and tail (T). Superstition may work in case of the choosing head or tail. If anyone becomes winner choosing head him always wants to choose head. Nobody believes that he winning chance is 50-50. However in this problem we are deal with a fair coin and n tossing of such a coin. The result of such a tossing can is represented by a string. Such as if 3 tossing is used then there are possible 8 outcomes.
HHH HHT hth HTT THH THT TTH TTT
As the coin is fair we can consider this probability of each outcome is also equal. For simplicity we can consider that if the same thing are repeated 8 times we can expect to get each possible sequence once . The Problem
In the above example we are 1 Sequnce has 3 consecutive H, 3 sequence has 2 consecutive H and 7 sequence has at least sing Le H. You are have to generalize it. Suppose a coin is tossed n times. And the same process is repeated 2^n. How many sequence your'll get which contains a consequnce of H of length at least K.
The Input
The input would start with two positive integer, N and K (1<=k<=n<=100). The Input is terminated by EOF. The Output
For each of the test case show the result in a line as specified in the problem statement. Sample Input
4 1
4 2
4 3
4 4
6 2
Sample Output
8
3
1
43
topic Meaning:
There are n cards, to find at least K card continuous is the number of positive arrangement. (1=<k<=n<=100)
Ideas for solving problems:
High Precision + recursion
Obviously, the total number of n cards is 2^n, and if you subtract the most (k-1) continuous positive, that's the final result.
DP[I][0]: The number of positive faces with the first I card and a maximum of k continuous
DP[I][1]=DP[I-1][0]+DP[I-1][1];
Dp[i][0] When i<=k, dp[i][0]=dp[i-1][0]+dp[i-1][1]
When I==k+1, dp[i][0]=dp[i-1][0]+dp[i-1][1]-1;
When I>k+1, dp[i][0]=dp[i-1][0]+dp[i-1][1]-dp[i-k-1][1];
Code:
#include <CSpreadSheet.h> #include <iostream> #include <cmath> #include <cstdio> #include <sstream> #include <cstdlib> #include <string> #include <string.h> #include <cstring> # include<algorithm> #include <vector> #include <map> #include <set> #include <stack> # include<list> #include <queue> #include <ctime> #include <bitset> #include <cmath> # Define EPS 1e-6 #define INF 0x3f3f3f3f #define PI acos ( -1.0) #define LL __int64 #define LL long #define Lson l,m, (rt& lt;<1) #define Rson m+1,r, (rt<<1) |1 #define m 1000000007//#pragma comment (linker, "/stack
: 1024000000,1024000000 ") using namespace std;
#define MAXN dp[maxn][2][maxn];//Dp[i][0] denotes the first I is H, at most the continuous K continuous h, the number int n,k;
int TEMP[MAXN];
int ANS[MAXN];
int RES[MAXN];
void Add (int a[],int b[])//addition {int aa=0;
memset (res,0,sizeof (res));
for (int i=100;i>=1;i--) {res[i]= (A[I]+B[I]+AA)%10000; Aa= (A[I]+B[I]+AA)/10000;
} void sub (int a[110],int b[110])//subtraction {int cur=0;
memset (res,0,sizeof (res));
for (int i=100;i>=1;i--) {if (A[i]-cur>=b[i]) {res[i]=a[i]-cur-b[i];
cur=0;
else {res[i]=a[i]+10000-b[i]-cur;
Cur=1;
()} int main () {//freopen ("In.txt", "R", stdin);
Freopen ("OUT.txt", "w", stdout);
while (~SCANF ("%d%d", &n,&k)) {memset (dp,0,sizeof (DP));
memset (ANS) (ans,0,sizeof);
Ans[100]=1;
Dp[0][0][100]=1;
k--;
for (int i=1;i<=n;i++) {Add (dp[i-1][0],dp[i-1][1]);
memcpy (dp[i][1],res,sizeof (res));
if (i<=k) {Add (dp[i-1][0],dp[i-1][1]);
memcpy (dp[i][0],res,sizeof (res));
else if (i==k+1) {memset (temp,0,sizeof (temp));
Temp[100]=1; Add (dp[i-1][0],dp[i-1][1]);
memcpy (dp[i][0],res,sizeof (res));
Sub (dp[i][0],temp);
memcpy (dp[i][0],res,sizeof (res));
else {Add (dp[i-1][0],dp[i-1][1]);
memcpy (dp[i][0],res,sizeof (res));
Sub (dp[i][0],dp[i-k-1][1]);
memcpy (dp[i][0],res,sizeof (res));
Add (Ans,ans);
memcpy (ans,res,sizeof (res));
int j=1;
while (!ans[j]&&j<=100) j + +;
} sub (ans,dp[n][0]);
memcpy (ans,res,sizeof (res));
Sub (ans,dp[n][1]);
memcpy (ans,res,sizeof (res));
int i=1;
while (!ans[i]&&i<=100) i++;
printf ("%d", ans[i++]);
while (i<=100) printf ("%04d", ans[i++]);
Putchar (' \ n ');
return 0;
}