Hdu 5642 King ' s Order (digital DP)

Source: Internet
Author: User

Problem Descriptionafter The King ' s speech, everyone is encouraged. But the war isn't over. The king needs to give orders from time to time. But sometimes he can not speak things well. So in his order there is some ones like this: "Let the group-p-p three come to me". As can see letter ' P ' repeats for 3 times. Poor king!
Now, it's war time, because of the spies from enemies, sometimes it's pretty hard-to-do which order s come from the king. But fortunately the general know what the King Speaks:the King never repeats a letter for more than 3 times continually. A nd 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 which has the length of n
To make it simple, only lower case 中文版 Letters can appear in King's order, and please output the answer modulo 1000000007
We regard strings is the same if and only if all charactor is the same place of these, the strings.

Inputthe First line contains a number T(t≤ten) --the number of the testcases.
For each testcase, the first line and the contains a positive number N(n≤).

Outputfor each testcase, print a single number as the answer.

Sample Input224

Sample Output676456950hint:all The order has length 2 is legal. So, the answer is 26*26.for the order which has length 4. The illegal order is: "AAAA", "bbbb" ... " Zzzz "All orders in total. So the answer-n = = 4 is 26^4-26 = 456950

Bestcoder Round #75

Test instructions: A sequence of length n, and a sequence that cannot have consecutive identical characters greater than 3 in length, and how many legal sequences there are.


Idea: Use DP[I][J] to indicate the number of legal sequences ending in J with length I. Let's take a look at how this shifts.
At the end of J, there are three cases, one J ending, two J ending, and three J ending.

If it is three J end, we can be sure, the length of the last three bits of I is J, the 4th digit can not be J, so is ∑dp[i-3][k] (k!=j),

Similarly consider the two J end and a J end, then the transfer equation is: dp[i][j] =∑dp[i-1][k] +∑dp[i-2][k] +∑dp[i-3][k]


AC Code:

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> - using namespacestd; the #definell Long Long - #defineEPS 1e-10 - #defineMOD 1000000007 - #defineN 2006 + #defineINF 1e12 - ll N; +ll dp[n][6]; A voidinit () { atdp[0][1]= -; -      for(LL i=1;i<2005; i++){ -dp[i][2]=dp[i-1][1]%MOD; -dp[i][3]=dp[i-1][2]%MOD; -dp[i][1]= (dp[i-1][1]+dp[i-1][2]+dp[i-1][3])%mod* -; -     } in } - intMain () to { + init (); -     intT; thescanf"%d",&t); *      while(t--){ $scanf"%i64d",&n);Panax Notoginsengprintf"%i64d\n", (dp[n-1][1]+dp[n-1][2]+dp[n-1][3])%MOD); -     } the     return 0; +}

Hdu 5642 King ' s Order (digital DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.