I think their is some fault with the base case!

This can be solved in a much more simple way, should I tell you that or you want me to debug your code as well (there are many issues with your code (apart from the complex approach)).

yaa yaaa please debug my code !! thank you so much

You are probably messing up 0, 1, 2 for the games. Can you tell me what are you using for which game?

0 for cricket
1 for football
2 for hockey

Corrected code

/*
Prateek Bhaiya decided to organize a sports fest for students of Coding Blocks and he came to know that Cricket, Football and Hockey are popular in the same order. So, he laid down the following rules for the fest :

Only one match can be played in a single day.
Cricket can be played daily.
Football can't be played on two consecutive days.
There must be a gap of at least two days between two hockey matches.
Since Bhaiya is busy enjoying his Maggi, he wants you to write a program that can calculate the number of ways the fest can be organised, given the number of days the fest is scheduled to be organised over.

Input Format
Single integer representing the duration of fest in number of days.

Constraints
1 <= Number of days <= 10^5

Output Format
Print a single integer, the total number of ways the fest can be organised in. As the number of ways can be large, print the answer modulo 109+7.

Sample Input
2
Sample Output
7
Explanation
There are 7 number of ways : CC, FC, HC, CF, HF, FH, CH. C denotes Cricket, F denotes Football, H denotes Hockey.
*/
#include<bits/stdc++.h>
using namespace std;

#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define watch2(x,y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n"
#define watch3(x,y,z) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<" and "<<(#z)<<" is "<<(z)<<"\n"

#define ll long long
#define ff first
#define ss second
#define null NULL
#define all(c) (c).begin(),(c).end()
#define nl "\n"

#define ld long double
#define eb emplace_back
#define pb push_back
#define pf push_front
#define MOD 1000000007

typedef vector<ll> vl;
typedef vector< vl > vvl;
typedef pair< ll, ll> pll;
typedef map< ll, ll> mll;

ll n;
ll dp[100006][5][5];

ll func(ll i, ll f, ll s) {
	if (i == 2) {
		return 1;
	}

	if (dp[i][f][s] != -1) {
		return dp[i][f][s];
	}

	if (f == 0) {
		if (s == 0) {
			dp[i][f][s] = (func(i - 1, 0, 0) + func(i - 1, 0, 1) + func(i - 1, 0, 2)) % MOD;
		}
		else if (s == 1) {
			dp[i][f][s] = (func(i - 1, 1, 0) + func(i - 1, 1, 2)) % MOD;
		}
		else {
			dp[i][f][s] = (func(i - 1, 2, 0) + func(i - 1, 2, 1)) % MOD;
		}
	} else if (f == 1) {
		if (s == 0) {
			dp[i][f][s] = (func(i - 1, 0, 0) + func(i - 1, 0, 1) + func(i - 1, 0, 2)) % MOD;
		}
		else if (s == 1) {
			dp[i][f][s] = 0;
		}
		else {
			dp[i][f][s] = (func(i - 1, 2, 0) + func(i - 1, 2, 1)) % MOD;
		}
	} else if (f == 2) {
		if (s == 0) {
			dp[i][f][s] = (func(i - 1, 0, 0) + func(i - 1, 0, 1)) % MOD;
		}
		else if (s == 1) {
			dp[i][f][s] = func(i - 1, 1, 0);
		}
		else {
			dp[i][f][s] = 0;
		}
	}
	return dp[i][f][s];
}

int main() {

	// Use ctrl+shift+b ( second option )
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> n;

	memset(dp, -1, sizeof(dp));

	if (n == 1) {
		cout << "3" << nl;
	}
	else if (n == 2) {
		cout << "7" << nl;
	}
	else
		cout << (func(n, 0, 0) + func(n, 0, 1) + func(n, 0, 2) +
		         func(n, 1, 0) + func(n, 1, 2) + func(n, 2, 0) + func(n, 2, 1)) % MOD << nl;


	return 0;
}
2 Likes

Thank you sir , thank you so much !!!

1 Like