King's Order

Accepts: 381
Submissions: 1361
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem 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≤10)——The
number of the test cases.

For each test case, the first line and the only line contains a positive number n(n≤2000).

Output

For each test case, print a single number as the answer.

Sample Input
Copy

2
2
4
Sample Output
Copy

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

Source

The question is from BC
and hduoj 5642.

My Solution

数位dp

状态: d[i][j][k] 为处理完i 个字符 , 结尾字符为′a′+j , 结尾部分已反复出现了
k 次的方案数;

边界:d[1][j][1] = 1; (1 <= j <= 26 );

状态转移方程:看代码吧;

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 2000+6;
const int HASH = 1000000007;
long long d[maxn][27][4];
int main()
{
int T, n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
memset(d, 0, sizeof(d));
for(int j = 1; j <= 26; j++){
d[1][j][1] = 1; //d[0][j][2] = 0; d[0][j][3] = 0;//they are not needed.
//d[1][j][2] = 1; these two are wrong and not needed.
//d[2][j][3] = 1; these two are wrong and not needed.
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= 26; j++){
d[i][j][2] = (d[i][j][2]+d[i-1][j][1])%HASH; // 2
d[i][j][3] = (d[i][j][3]+d[i-1][j][2])%HASH; // 3
for(int k = 1; k <= 26; k++){
if(j != k) d[i][j][1] = ( ( (d[i][j][1]+d[i-1][k][1])%HASH + d[i-1][k][2])%HASH + d[i-1][k][3])%HASH;
}
}
}
long long ans = 0;
for(int j = 1; j <= 26; j++){
for(int k = 1; k <= 3; k++ ){
ans = (ans + d[n][j][k]) %HASH;
}
}
cout<<ans<<endl;
//printf("%lld\n", ans); this website : %I64d
}
return 0;
}

Thank you!

最新文章

  1. 转载 NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel
  2. python注释、脚本参数、字节码
  3. vs文件属性(生成操作)各选项功能(发布Web项目时使用)
  4. wcf 配置
  5. JS-改变页面的颜色(一)
  6. 360网站卫士推出google字体加速方案
  7. 我的手机华为荣耀7,运行android程序不输出Log
  8. 开发自己的cordova插件
  9. 响应式web前端框架Foundation &amp; Bootstrap 对比
  10. cobbler 奇葩出错
  11. JMM内存管理
  12. 对于反射中的invoke()方法的理解
  13. UTF-8和UTF-8无BOM,一个会导致文件中中文变量无法匹配的bug
  14. javascript requestAnimationFarme
  15. Android实现多条Toast快速显示(强制中止上一条Toast的显示)
  16. 如何 dump jvm 内存及线程栈
  17. 使用 OAuth2-Server-php 搭建 OAuth2 Server
  18. 实用的几个JS新特性(es 2016)
  19. maven的安装及试用
  20. Orcale日期函数to_date(),to_char()

热门文章

  1. cxf调用WebService
  2. Gym-101158J Cover the Polygon with Your Disk 计算几何 求动圆与多边形最大面积交
  3. E - Dividing Orange
  4. asp.net的TextBox回车触发指定的按钮事件
  5. 【华科考研机试题】最长&amp;最短文本
  6. 【Oracle】ORA-01157: cannot identify/lock data file 201 - see DBWR trace file
  7. Spring DATA MongoDB @DBref查询,or和and联合查询
  8. 最简单的一致性Hash算法实现
  9. 目标跟踪ObjectT综述介绍
  10. 01--vim常用快捷键