BestCoder Round #75 King's Order dp:数位dp
King's Order
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.
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).
For each test case, print a single number as the answer.
2
2
4
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's Order dp:数位dp的更多相关文章
- BestCoder Round #75 King's Cake 模拟&&优化 || gcd
King's Cake Accepts: 967 Submissions: 1572 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...
- BestCoder Round #75 1001 - King's Cake
Problem Description It is the king's birthday before the military parade . The ministers prepared a ...
- hdu 5642 King's Order(数位dp)
Problem Description After the king's speech , everyone is encouraged. But the war is not over. The k ...
- hdu 5643 BestCoder Round #75
King's Game Accepts: 249 Submissions: 671 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6 ...
- hdu 5641 BestCoder Round #75
King's Phone Accepts: 310 Submissions: 2980 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- BestCoder Round #75 1003 - King's Order
国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...
- BestCoder Round #75
前两题不想写了 数位DP 1003 King's Order 考虑i的后缀有j个连续,转移状态很简单,滚动数组优化(其实不用) #include <bits/stdc++.h> const ...
- 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 ...
- 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 ...
随机推荐
- Truck History --hdoj
Truck History Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tota ...
- Node.js:Strea
ylbtech-Node.js:Stream 1.返回顶部 1. Node.js Stream(流) Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...
- SSH整合报错:找不到元素 'beans' 的声明
转自:https://blog.csdn.net/haozhugogo/article/details/54233608 spring版本问题,将bean.xml中xsd文件定义的版本改为spring ...
- 用 JS + LeanCloud 给网页添加数据库(留言功能)
记录给自己网页添加留言功能的过程. 使用工具:LeanCloud,一个自带数据库和增删改查(CRUD)功能的后台系统. 1 在JS中引入LeanCloud官方库 在LeanCloud注册并添加应用的步 ...
- SqlServer数据库基本用法
. 利用T-SQL语句,创建数据库(工资管理数据库),要求如下: 数据库初始大小:3MB:文件大小按兆字节3MB自动增长,增长限制为:15MB: 数据库日志文件初始大小:1MB: 文件大小按百分比5% ...
- (转载)自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果
自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果 作者 小武站台 关注 2016.02.19 11:34 字数 1244 阅读 3885评论 ...
- hdu1811 Rank of Tetris 拓扑排序+并查集
这道题是拓扑排序和并查集的综合运用. 由于排行榜是一种从高到低的排序.所以在拓扑排序的时候,如果有一次加入的入度为零的点数大于1,就有变得不确定了(UNCERTAIN). 由于只有一棵树,当树的数量大 ...
- mysql5.5和5.6版本更新内容
mysql 5.5,5.6 比5.1改进地方: 1,5.5默认存储引擎为innodb2,5.5增加cpu多核处理能力:innodb_read_io_threads innodb_write_io_th ...
- Firebird Character Sets and Collations
Firebird Character Sets and Collations Every CHAR or VARCHAR field can (or, better: must) have a cha ...
- PostgreSQL 满足条件时插入数据
例如:当表中不存在某记录时,才插入这条记录. INSERT INTO 表名(列名1, 列名2) SELECT '值1', '值2' WHERE NOT EXISTS ( SELECT * FROM 表 ...