hdu 5642 King's Order(数位dp)
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.
For each testcase, the first line and the only line contains a positive number n(n≤2000).
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
题意:一个长度为n的序列,并且序列中不能出现长度大于3的连续的相同的字符,求一共有多少个合法序列。
思路:用dp[i][j]表示以j结尾,长度为i的合法序列个数。我们考虑一下这个怎么转移。
以j结尾的话就三种情况,一个j结尾,两个j结尾,三个j结尾。
如果是三个j结尾的话我们可以确定下来,长度为i的后三位是j,倒数第4位不可以是j,所以就是∑dp[i-3][k](k!=j),
同理考虑两个j结尾和一个j结尾,那么转移方程就是:dp[i][j] = ∑dp[i-1][k] + ∑dp[i-2][k] + ∑dp[i-3][k]
AC代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 2006
#define inf 1e12
ll n;
ll dp[N][];
void init(){
dp[][]=;
for(ll i=;i<;i++){
dp[i][]=dp[i-][]%MOD;
dp[i][]=dp[i-][]%MOD;
dp[i][]=(dp[i-][]+dp[i-][]+dp[i-][])%MOD*;
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--){
scanf("%I64d",&n);
printf("%I64d\n",(dp[n-][]+dp[n-][]+dp[n-][])%MOD);
}
return ;
}
hdu 5642 King's Order(数位dp)的更多相关文章
- HDU 5642 King's Order dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order Accepts: 381 Submissions: 1361 ...
- HDU 5642 King's Order【数位dp】
题目链接: http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=677&pid=1003 题意: 求长度为n的序列 ...
- HDU 5642 King's Order 动态规划
King's Order 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 Description After the king's speec ...
- hdu-5642 King's Order(数位dp)
题目链接: King's Order Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 4507 (鬼畜级别的数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意:求指定范围内与7不沾边的所有数的平方和.结果要mod 10^9+7(鬼畜の元凶) 解题 ...
- HDU 5787 K-wolf Number (数位DP)
K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...
- 【HDU 3652】 B-number (数位DP)
B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...
- HDU 5787 K-wolf Number(数位DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5787 [题目大意] 求区间[L,R]内十进制数相邻k位之间不相同的数字的个数. [题解] 很显然的 ...
- 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】
Valley Numer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- Adroid APPIUM实例步骤
1.下载eclipse 2.安装java 配置环境变量 3.eclipse 安装adt android development tools 4.android sdk manager 安装tool ...
- js基础例子
创建变量 var obj=value; 其中obj是变量名; value表示可能是数字,数组,函数之类的 多变量进行计算 var a1=200,b1='hello',c1=400; var d1=c1 ...
- C#隐式执行CMD命令
本文实现C#隐式执行CMD功能命令.下图是示例程序的主界面. 在命令文本框输入DOS命令,点击"Run"button.在以下的文本框中输出执行结果. 以下是程序的完整代码. 本程序 ...
- io系统
一.浅谈io系统 io系统的结构化思想是:输入-转换流-装饰器-输出. 对于字节流来说,常见的结构类为: package com.handchina.yunmart.middleware.servic ...
- Java性能漫谈-数组复制之System.arraycopy
当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处. 为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对 ...
- hdu5347 MZL's chemistry(打表)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud MZL's chemistry Time Limit: 2000/1000 MS ...
- HDU 4620 Fruit Ninja Extreme(2013多校第二场 剪枝搜索)
这题官方结题报告一直在强调不难,只要注意剪枝就行. 这题剪枝就是生命....没有最优化剪枝就跪了:如果当前连续切割数加上剩余的所有切割数没有现存的最优解多的话,不需要继续搜索了 #include &l ...
- find the safest road--hdu1596
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- C语言内存分配函数
c语言标准库提供了3个内存分配的函数,都包含在头文件<stdlib.h>中 1.malloc 函数原型: void *malloc( size_t size ); 参数:要分配内存大小的字 ...
- [Codeforces Round #237 (Div. 2)] A. Valera and X
A. Valera and X time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...