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 ...
随机推荐
- [POJ 2282] The Counting Problem
[题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...
- NOIP 2012 D1T1 Vigenère密码
嗯嗯 一道找规律的题.... 真佩服那些把表打出来的人 //By SiriusRen #include <cstdio> #include <cstring> using na ...
- Android应用优化之代码检测优化
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- Android之Action Bar
Action Bar在实际应用中,很好地为用户提供了导航,窗口位置标识,操作点击等功能.它出现于Android3.0(API 11)之后的版本中,在2.1之后的版本中也可以使用. 添加与隐藏Actio ...
- Oracle数据库实例
数据库通常由两部分组成:数据库和数据库实例 数据库与实例的关系:数据库指的是:物理数据.数据库管理系统.即物理数据.内存.操作系统.用户访问Oracle都是访问一个实例,实例名指的是用于相应某个数据库 ...
- ubuntu+win10双系统,调整分区大小后进入了emergency mode
问题背景: 装了Ubuntu+win10双系统,在Ubuntu下面挂载了Windows的D盘.后来因为D空间不够,进入Windows压缩C盘分区,扩大了D盘.重启后无法启动Ubuntu,进入了emer ...
- 基于S3C2440数码相框
[参考]韦东山 教学笔记 1. 程序框架1.1 触摸屏: 主按线程,通过socket发给显示进程 --------------------------- 封装事件:ts线程 按键线程 -------- ...
- 【Oracle】DG中 Switchover 主、备切换
操作系统:OEL 5.6 数据库版本:Oracle11gR2 11.2.0.4.0 Switchover切换要求主库和备库在数据同步情况下进行,是主备之间的正常切换,主要用于日常维护.灾备演练等.切 ...
- 写给VC++ Windows开发的初学者 一片不错的博文
不知不觉2010年都过了半年了,想来我学C语言已经12个年头了(从1998年开始),用VC++也有11年了,最早使用Turbo C2.0 ,也学过汇编,后来使用Borland C++3.0 .Micr ...
- mysql的模糊查询
mysql模糊查询like/REGEXP(1)like / not like MySql的like语句中的通配符:百分号.下划线和escape %:表示任意个或多个字符.可匹配任意类型和长度的字符. ...