Codeforces Round #652 (Div. 2) D题

原题链接:https://codeforces.com/contest/1369/problem/D
思路:树形DP
源码:

#include 
#include 
using namespace std;
const int mod = 1e9+7;
const int maxn = 2000010;
long long dp[maxn][2] = {0};

void init(){
	for(long long i = 3;i<maxn;i++){
		dp[i][0] = (max(dp[i-2][0],dp[i-2][1])*2+max(dp[i-1][0],dp[i-1][1]))%mod;
		dp[i][1] = (dp[i-2][0]*2+dp[i-1][0]+4)%mod;
	}
}

int main(){
	long long t;
	cin >> t;
	init();
	while(t--){
		long long n;
		cin >> n;
		cout << max(dp[n][0],dp[n][1]) << endl;
	}
	return 0;
} 

你可能感兴趣的:(ACM-ICPC算法)