HDU 5362 Just A String 2015 multi-school Joint training tournament Dynamic planning

Source: Internet
Author: User
Tags constant

Just A String Time limit:2000/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 221 Accepted Submission (s): 40


Problem Description Soda has a random string of length n which are generated by the following Algorithm:each of N Charac Ters of the string is equiprobably chosen from the alphabet of size M.

For a string s, if we can reorder the letters in string s so as to get a palindrome and then we call S a good string.

Soda wants to know the expected number of good substrings in the random string.

Input There is multiple test cases. The first line of input contains an integer T, indicating the number of the test cases. For each test case:

The first line contains the integers n and M (1≤n,m≤2000).

Output for each case, if the expected number are E, a single integer denotes e⋅mn mod 1000000007.
Sample Input

3 2 2 3 2 10 3
Sample Output
10 40 1908021
Source multi-university Training Contest 6

Bloggers said very excited, incredibly someone private messages I said my key to write good ..... Then I'll write more details later.


Topic:

Give the length n, the number of the alphabet is M. For any string of length n (the character can only be in 1-m), if there is a substring,

The substring is re-arranged as a palindrome string, which is called the substring as a good string. To get the expectation of the number of palindrome strings, multiply m^n%1e10+7

Analytical:

Multiply m^n means that the sum of the number of good substrings is calculated for all possible strings of length n.

If the length is an odd substring, the substring must be an even number of letters except one letter is odd.

If the length is even, then each letter must be an even number. -------------------obviously.


Next define the state:

DP[I][J] indicates that a J letter of length I is the number of odd strings.

Then Dp[i][j] can only be from dp[i-1][j-1]-------------means adding an even-numbered letter of the original alphabetic number, the number of odd-number letters increases,

Then the added letters can only be letters that contain an odd number of digits other than the mother of a m-j+1

Dp[i-1][j+1]-----------means to add an odd number of letters, so the number of odd letters decreases by 1.

Then you can only add the original is the odd letter, there are j+1 kinds of letters

DP[I][J] = dp[i-1][j-1]]* (m-j+1) + dp[i-1][j+1]* (j+1)

====================================================================

The above is over, and then I'll talk

1. Border processing can be 0, and the largest case

2. If I is an odd number, then J is an even number of dp[i][j] is 0, is not required to calculate, reduce the constant complexity of 2

3. You cannot use Memset to initialize the session, only for the first

--------------------why it's useless to say that.

Because the data has 1000 groups, the standard process has two, the standard course 1 oneself T, the standard course 2 also is 500+ms. (g++-handed)

I also t have countless hair. All sorts of things is to not T, an estimate you write the words will be better than me;

#include <cstdio> #define MAXN 2048 #define LL Long long int dp[maxn][maxn],mod=1000000007;
It is said that int storage is faster than Longlong int PO[MAXN];
    int main () {int t,m,n;
    Freopen ("1010.in", "R", stdin);
    Freopen ("10101.out", "w", stdout);
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&m); Dp[1][0] = 0;
        The initial session dp[1], and the subsequent direct calculation of dp[n] are to reduce the computation (after all 1000 case) dp[1][1] = m;
        Po[0] = 1;
        for (int i = 1;i <= n;i++) {Po[i] = (ll) po[i-1]*m%mod;
        } Int J;
            for (int i = 2;i < n; i++) {dp[i][0] = dp[i-1][1];
            Dp[i][i] = (ll) dp[i-1][i-1]* (m-i+1)% MoD;
            if (I & 1) j = 1;  else J = 2; In order to reduce the constant complexity of 2 for (; J < i; j+=2) dp[i][j] = (ll) dp[i-1][j-1]* (m-j+1) + (LL) dp[i-1][j+1]* (j+1)
        )%mod;
        } if (n>1) dp[n][0] = dp[n-1][1];
        if (n>1) dp[n][1] = (LL) dp[n-1][0] * m + (LL) dp[n-1][2] * 2)% MoD;
        int ans = 0; for (iNT i = 1;i <= n;
        i++) {ans = (ans+ (ll) dp[i][i&1]* (n-i+1)%mod*po[n-i])%mod;
    } printf ("%d\n", ans);
} return 0;





 }





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.