hdu5362 Just A String(dp)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Just A String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 320    Accepted Submission(s): 62

Problem Description

soda has a random string of length n which is generated by the following algorithm: each of n characters 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, then we call s a good string.

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

Input

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

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

Output

For each case, if the expected number is E, a single integer denotes E⋅mn mod 1000000007.

Sample Input

3

2 2

3 2

10 3

Sample Output

10

40

1908021

当时状态真是见鬼,其实这题还是比较容易的一个dp

dp[i][j]表示长度为i时,j种字符是奇数个的字符串种数

从而dp[i][j] = dp[i-1][j+1]*(j+1) + dp[i-1][j-1]*(m-j+1)

最后Σdp[i][i&1]*(n-i+1)*(m^(n-i))

 1 /**
 2  * code generated by JHelper
 3  * More info: https://github.com/AlexeyDmitriev/JHelper
 4  * @author xyiyy @https://github.com/xyiyy
 5  */
 6
 7 #include <iostream>
 8 #include <fstream>
 9
10 //#####################
11 //Author:fraud
12 //Blog: http://www.cnblogs.com/fraud/
13 //#####################
14 //#pragma comment(linker, "/STACK:102400000,102400000")
15 #include <iostream>
16 #include <sstream>
17 #include <ios>
18 #include <iomanip>
19 #include <functional>
20 #include <algorithm>
21 #include <vector>
22 #include <string>
23 #include <list>
24 #include <queue>
25 #include <deque>
26 #include <stack>
27 #include <set>
28 #include <map>
29 #include <cstdio>
30 #include <cstdlib>
31 #include <cmath>
32 #include <cstring>
33 #include <climits>
34 #include <cctype>
35
36 using namespace std;
37 #define rep2(X, L, R) for(int X=L;X<=R;X++)
38 typedef long long ll;
39
40 int dp[2010][2010];
41 int dp2[2010];
42 const int mod = 1000000007;
43
44 class hdu5362 {
45 public:
46     void solve(std::istream &in, std::ostream &out) {
47         int n, m;
48         in >> n >> m;
49         dp[0][0] = 1;
50         rep2(i, 1, n) {
51             for (int j = (i & 1); j <= m && j <= i; j++) {
52                 if (!j)dp[i][j] = dp[i - 1][j + 1];
53                 else if (j == i || j == m)dp[i][j] = (ll) dp[i - 1][j - 1] * (m - j + 1) % mod;
54                 else dp[i][j] = ((ll) dp[i - 1][j - 1] * (m - j + 1) + (ll) dp[i - 1][j + 1] * (j + 1)) % mod;
55             }
56         }
57         dp2[0] = 1;
58         rep2(i, 1, n) {
59             dp2[i] = (ll) dp2[i - 1] * m % mod;
60         }
61         int ans = 0;
62         rep2(i, 1, n) {
63             ans = (ans + (ll) dp[i][i & 1] * (n - i + 1) % mod * dp2[n - i]) % mod;
64         }
65         out << ans << endl;
66     }
67 };
68
69 int main() {
70     std::ios::sync_with_stdio(false);
71     std::cin.tie(0);
72     hdu5362 solver;
73     std::istream &in(std::cin);
74     std::ostream &out(std::cout);
75     int n;
76     in >> n;
77     for (int i = 0; i < n; ++i) {
78         solver.solve(in, out);
79     }
80
81     return 0;
82 }
时间: 2024-10-11 12:32:08

hdu5362 Just A String(dp)的相关文章

87. Scramble String (String; DP)

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

CodeForces 710E Generate a String (DP)

题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符串的最少花费,当 i 是偶数时,要么再加一个字符,要么从i/2中复制,如果为奇数,要么再加1个字符, 要么从i/2先加一个,再复制.即: 奇数 : dp[i] = min(dp[i-1]+x, dp[i/2+1]+y+x); 偶数 : dp[i] = min(dp[i-1]+x, dp[i/2]+y

115. Distinct Subsequences (String; DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

72. Edit Distance (String; DP)

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

LeetCode-Interleaving String[dp]

Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false

hdu 5311 Hidden String dp o(n)算法 深搜

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 857    Accepted Submission(s): 322 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets

132. Palindrome Partitioning II (String; DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

38. Count and Say (String; DP)

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an

Codeforces 710 E. Generate a String (dp)

题目链接:http://codeforces.com/problemset/problem/710/E 加或者减一个字符代价为x,字符数量翻倍代价为y,初始空字符,问你到n个字符的最小代价是多少. dp[i]表示i个字符的最小代价. 当i为偶数个的时候,由+1或者*2得到. 当i为奇数个的时候,由+1或者*2-1得到. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm&