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!

BestCoder Round #75 King&#39;s Order dp:数位dp的更多相关文章

  1. BestCoder Round #75 King&#39;s Cake 模拟&amp;&amp;优化 || gcd

    King's Cake Accepts: 967 Submissions: 1572 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...

  2. BestCoder Round #75 1001 - King's Cake

    Problem Description It is the king's birthday before the military parade . The ministers prepared a ...

  3. hdu 5642 King's Order(数位dp)

    Problem Description After the king's speech , everyone is encouraged. But the war is not over. The k ...

  4. hdu 5643 BestCoder Round #75

    King's Game  Accepts: 249  Submissions: 671  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...

  5. hdu 5641 BestCoder Round #75

    King's Phone  Accepts: 310  Submissions: 2980  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  6. BestCoder Round #75 1003 - King's Order

    国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...

  7. BestCoder Round #75

    前两题不想写了 数位DP 1003 King's Order 考虑i的后缀有j个连续,转移状态很简单,滚动数组优化(其实不用) #include <bits/stdc++.h> const ...

  8. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  9. Codeforces Beta Round #8 C. Looking for Order 状压dp

    题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...

随机推荐

  1. Linux下开启vim高亮

    默认是不高亮的. [root@local ~]# vi ~/.vimrc 没有则新建这个文件. 或者修改 [root@local vim74]# vi /etc/vimrc 添加一行. syntax ...

  2. mvc 伪静态任意扩展名的实现方法

    比如:要实现 http://localhost:60291/home/geta/1212.html 或者 .abc 任意扩展名 完成两步即可. 第一步修改路由: public static void ...

  3. [hihocoder][Offer收割]编程练习赛43

    版本号排序 不知道什么傻逼原因,就是过不了 #pragma comment(linker, "/STACK:102400000,102400000") #include<st ...

  4. 关于各浏览器下Hack的写法

    下面是我收集有关于各浏览器下Hack的写法: 1.Firefox @-moz-document url-prefix() { .selector { property: value; } } 上面是仅 ...

  5. iOS11关于隐藏导航栏后带有tableView界面出现,下移问题

    //解决iOS11关于隐藏导航栏后带有scrollView界面出现,下移问题 if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdj ...

  6. Linker scripts之SECTIONS

    1 Purpose The linker script describes how the sections in the input files should be mapped into the ...

  7. 配置OpenCV的Qt开发环境

    QT&openCV系列!链接:http://www.cnblogs.com/emouse/category/449213.html 本文链接:http://blog.csdn.net/qiur ...

  8. js 获取 下拉框的值

    //错误 console.log($("#DictID").select.val()); //错误 console.log($("#DictID").selec ...

  9. java 如何将异常Exception的信息转换为String

    一般情况下,我们是通过log4j封装的api将异常打印到日志当中. logger.error("error", e); 如果我们想在程序中获得该异常的详细信息,并输出到数据库中,我 ...

  10. JS 从36个数字里面随机抽取8个

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...