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. B1567 [JSOI2008]Blue Mary的战役地图 二分答案+hash

    一开始以为是dp,后来看了一下标签...二分答案?之前也想过,但是没往下想,然后之后的算法就顺理成章,先求出第一个地图的所有子矩阵的hash值,然后求第二个,在上一个地图例二分查找,然后就没了. 算法 ...

  2. python 飞机大战 实例

    飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...

  3. AJAX异步删除操作

    @Ajax.ActionLink("删除", "Delete", new {id = user.Id}, ajaxOption) @{ var ajaxOpti ...

  4. lua迭代

    迭代 function enum(array) local index = 1 return function() --返回迭代函数 local ret = array[index] index = ...

  5. ffmpeg编码

    1. 注册所有容器格式和CODEC:av_register_all()2. 打开文件:av_open_input_file()3. 从文件中提取流信息:av_find_stream_info()4. ...

  6. 八叉树(Octree)Typescript 实现

    Demo GitHub export class Octree { // 父&子树 private parent_node: any; private children_nodes: Octr ...

  7. ProgressDialog的关键几个函数

    进度条对话框在开发是常见的一种工具,只要注意以下几点,就可以轻松使用. ProgressDialog.setMax(MAX_PROGRESS);  //设置最大值,可以如下定义一个常值 //priva ...

  8. hdu3926 Hand in Hand 判断同构

    因为每个人小朋友只有两只手,所以每个点最多只有2度.图有可能是环.链,以及环和链构成的复杂图. 如何判断两幅图是否相似呢?判断相似是判断两幅图的圈的数量,以及构成圈的点数是否相同.还有判断链的数目和构 ...

  9. Spring aop(实验写法)

    1. 创建通知:定义一个接口 Public interface Sleepable { voidsleep(); }然后写一个Human类,他实现了这个接口 publicHuman implement ...

  10. PostgreSQL 满足条件时插入数据

    例如:当表中不存在某记录时,才插入这条记录. INSERT INTO 表名(列名1, 列名2) SELECT '值1', '值2' WHERE NOT EXISTS ( SELECT * FROM 表 ...