标签:
Description
After the king‘s speech , everyone is encouraged. But the war is not over. The king needs to give orders from time to time. But sometimes he can not speak things well. So in his order there are some ones like this: "Let the group-p-p three come to me". As you can see letter ‘p‘ repeats for 3 times. Poor king!
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.Input
The first line contains a number $T(T\le 10)$――The number of the testcases.
For each testcase, the first line and the only line contains a positive number $n(n\le 2000)$.Output
For each testcase, print a single number as the answer.Sample Input
2 2 4Sample Output
676 456950 hint: All the order that has length 2 are legal. So the answer is 26*26. For the order that has length 4. The illegal order are : "aaaa" , "bbbb"…….."zzzz" 26 orders in total. So the answer for n == 4 is 26^4-26 = 456950
dp[n][m]=dp[n-1][m+1]+dp[n-1][m]*25
(a+b) mod c = ((a mod c) + (b mod c)) mod c
(a*b) mod c = ((a mod c) * (b mod c)) mod c
1 /* 2 By:OhYee 3 Github:OhYee 4 Email:oyohyee@oyohyee.com 5 Blog:http://www.cnblogs.com/ohyee/ 6 7 かしこいかわいい? 8 エリーチカ! 9 要写出来Хорошо的代码哦~ 10 */ 11 #include <cstdio> 12 #include <algorithm> 13 #include <cstring> 14 #include <cmath> 15 #include <string> 16 #include <iostream> 17 #include <vector> 18 #include <list> 19 #include <queue> 20 #include <stack> 21 using namespace std; 22 23 //DEBUG MODE 24 #define debug 0 25 26 //循环 27 #define REP(n) for(int o=0;o<n;o++) 28 29 const int mod = 1000000007; 30 const int maxn = 2005; 31 typedef unsigned long long LL; 32 LL dp[maxn][5]; 33 34 //n层数 m连续数目 35 LL DP(int n, int m) { 36 if (n == 0) { 37 if (m == 1) 38 return 0; 39 else 40 return 1; 41 } 42 43 if (m == 4) 44 return 0; 45 46 if (dp[n][m] == 0) { 47 //(a+b) mod c = ((a mod c) + (b mod c)) mod c 48 //(a*b) mod c = ((a mod c) * (b mod c)) mod c 49 return (dp[n][m] = (DP(n - 1, m + 1) % mod) + ((25 * (DP(n - 1, 1) % mod)) % mod)) % mod; 50 } 51 else { 52 return dp[n][m]; 53 } 54 55 } 56 57 void Do() { 58 int n; 59 scanf("%d", &n); 60 LL ans = (DP(n, 1) % mod) * 26; 61 printf("%llu\n", ans % mod); 62 return; 63 } 64 65 66 int main() { 67 memset(dp, 0, sizeof(dp)); 68 int T; 69 scanf("%d", &T); 70 while (T--) { 71 Do(); 72 } 73 return 0; 74 }
标签:
原文地址:http://www.cnblogs.com/ohyee/p/5399925.html