Bestcoder Round #75 King ' s Order DP: Digital DP

Source: Internet
Author: User
Tags hash time limit

King ' s Order accepts:381 submissions:1361 time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) Problem De Scription

After 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 10000 00007

We regard strings is the same if and only if all charactor is the same place of these, the strings. Input

The first line contains a number T (T≤10)--the number of the 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 of that have  length 2 is legal. The answer is 26*26.

For the order, the has length 4. The illegal order is: "AAAA", "bbbb" ... " Zzzz "All orders in total. So the answer-n = = 4 is 26^4-26 = 456950

Source

The question is from BC and Hduoj 5642.


My Solution


Digital DP

Status: D[i][j][k] To process the I character, the end character is ′a′+j, the end part has repeated the number of K-times scheme;

Boundary: d[1][j][1] = 1; (1 <= J <= 26);

State transition equation: Look at the code bar;


#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 <=, J + +) {d[1][j][1] = 1;//d[0][j][2] = 0; D[0][j][3] = 0;//they is not needed.                           D[1][J][2] = 1;
            These, wrong and not needed.                           D[2][J][3] = 1;
        These, wrong and not needed. } for (int i = 1; I <= N, i++) {for (int j = 1; J <=, 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 <=; 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 <=, 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!


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.