zoukankan      html  css  js  c++  java
  • 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 (1n,m2000).
     


    Output
    For each case, if the expected number is E, a single integer denotes Emn 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 }
  • 相关阅读:
    Linux实时性分析-schedule-调度器
    中断解析
    网络商城-PrestaShop
    和学生的学习互动记录(10嵌)
    QQ记录
    Windows7硬盘安装Fedora16图文教程
    今目标登录时报网络错误E110
    vs环境配置——vs快捷键配置——vs插件配置——vs环境设置
    如何防止app接口被别人调用
    mvc4 找到多个与名为“xx”的控制器匹配的类型
  • 原文地址:https://www.cnblogs.com/fraud/p/4712184.html
Copyright © 2011-2022 走看看