King's Order
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 829 Accepted Submission(s): 454
Now , it is war time , because of the spies from enemies , sometimes it is pretty hard for the general to tell which orders come from the king. But fortunately the general know how the king speaks: the king never repeats a letter for more than 3 times continually .And only this kind of order is legal. For example , the order: "Let the group-p-p-p three come to me" can never come from the king. While the order:" Let the group-p three come to me" is a legal statement.
The general wants to know how many legal orders that has the length of n
To make it simple , only lower case English Letters can appear in king's order , and please output the answer modulo 1000000007
We regard two strings are the same if and only if each charactor is the same place of these two strings are the same.
For each testcase, the first line and the only line contains a positive number n(n≤2000).
數據預處理處理好,遞推過程就兩行...很好整
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int main()
{
int n,T;
long long dp[2005][5],ans[2005];
dp[1][1]=26;
ans[1]=26;
dp[2][1]=26*25;
dp[2][2]=26;
ans[2]=26*26;
dp[3][1]=26*25*25+26*25;
dp[3][2]=26*25;
dp[3][3]=26;
ans[3]=26*26*26;
for(int i=4;i<=2000;i++)
{
dp[i][1]=((dp[i-1][1]+dp[i-1][2]+dp[i-1][3])*25)%mod;
dp[i][2]=dp[i-1][1];
dp[i][3]=dp[i-1][2];
ans[i]=(dp[i][1]+dp[i][2]+dp[i][3])%mod;
}
cin>>T;
while(T--)
{
cin>>n;
cout<<ans[n]<<endl;
}
return 0;
}