BestCoder Round #75 King's Order dp:数位dp

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's Order dp:数位dp

时间: 2024-12-04 23:40:33

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

BestCoder Round #75 - King&#39;s Phone

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

HDU 5642 King&#39;s Order(数位DP)

题目链接:点击打开链接 题意:要求你生成一个合法的字符串, 由小写字母a~z组成, 相同字母相邻出现不能超过3个, 求有多少种组合. 思路:数位DP来做, 用d[i][j][k]表示处理完前i个字母, 第i-1个字母为j,已经连续出现了k次的方法数. 然后每次转移就很简单了, 继续选择字母j(if(k < 3)), 或者换其他的. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #inc

HDOJ 4906 Our happy ending 状压DP(数位DP?)

http://acm.hdu.edu.cn/showproblem.php?pid=4906 题意: N个数的序列,每个数可以选择填0-L,如果一个序列可以选出某些数,他们的和为K,那么这个序列就是”好序列“,给定N<=20,K<=20,0<=L<=10^9,问好序列的个数. 分析: N和K很小,所以要想办法利用这个特性(状压?搜索?).虽然L很大,但实际上一个数大于K的时候,肯定是不能选他组成K的.我们就先考虑L<=K的做法. 然后还是考虑不出来.. 好吧,看题解吧.. 目

BestCoder Round #4 Miaomiao&amp;#39;s Geometry (暴力)

Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T. 2.The le

Educational Codeforces Round 8(D. Magic Numbers(数位DP))

题目链接:点击打开链接 题意:给一个m一个d, 一个字符串a和b, 问在[a,b]范围内, 有多少个可以整除m的魔法数, 魔法数的定义是, 偶数位上都是d, 奇数位上都不是d. 思路:据说是典型的数位DP, 以前没做过数位DP, 感觉和DP差不多? 用d[i][j][p]表示当前到了第i位, 余数为j, p == 1表示目前和b串相等, p == 0表示已经比b串小了.  每次枚举第i位上放的数, 转移就行了. 区间也好弄, 我们可以先处理出小于等于b的所有情况ans1, 小于等于a的所有情况a

数位dp模板 [dp][数位dp]

现在才想到要学数位dp,我是不是很弱 答案是肯定的 以一道自己瞎掰的题为模板 1 //题: 2 //输入数字n 3 //从0枚举到n,计算这n+1个数中含有两位数a的数的个数 4 //如12930含有两位数93 5 #include<cstdio> 6 #include<cstring> 7 #include<iostream> 8 using namespace std; 9 10 int n,m=0,a,g1,g2; 11 int dp[10][10][2],lim

BestCoder Round #75 解题报告

1.King's Cake #include<stdio.h> #include<iostream> using namespace std; int ans; void solve(int l,int w) { if(weight == 0||weight == 0) return 0; int t,tl,tw; t = l/w; ans += t; tw = w; w = l%w; l = tw; solve(l,w); } int main(void) { int lengt

ACM学习历程—BestCoder Round #75

1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include &l