转载请注明出处: 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))

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define rep2(X, L, R) for(int X=L;X<=R;X++)
typedef long long ll; int dp[][];
int dp2[];
const int mod = ; class hdu5362 {
public:
void solve(std::istream &in, std::ostream &out) {
int n, m;
in >> n >> m;
dp[][] = ;
rep2(i, , n) {
for (int j = (i & ); j <= m && j <= i; j++) {
if (!j)dp[i][j] = dp[i - ][j + ];
else if (j == i || j == m)dp[i][j] = (ll) dp[i - ][j - ] * (m - j + ) % mod;
else dp[i][j] = ((ll) dp[i - ][j - ] * (m - j + ) + (ll) dp[i - ][j + ] * (j + )) % mod;
}
}
dp2[] = ;
rep2(i, , n) {
dp2[i] = (ll) dp2[i - ] * m % mod;
}
int ans = ;
rep2(i, , n) {
ans = (ans + (ll) dp[i][i & ] * (n - i + ) % mod * dp2[n - i]) % mod;
}
out << ans << endl;
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
hdu5362 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
int n;
in >> n;
for (int i = ; i < n; ++i) {
solver.solve(in, out);
} return ;
}

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

  1. codeforces 710E E. Generate a String(dp)

    题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...

  2. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  3. CodeForces 710E Generate a String (DP)

    题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...

  4. LeetCode-Interleaving String[dp]

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  5. PAT 1040 Longest Symmetric String[dp][难]

    1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...

  6. 87. Scramble String (String; DP)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. 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 ...

  8. 72. Edit Distance (String; DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. 97. Interleaving String (String; DP)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

随机推荐

  1. Css3做的旋转显示文字和角度的变化

    Css: .spinner{ width:245px; height:245px; position:relative;}.coly{ border-radius:130px; font-size:1 ...

  2. 模块SEO优化中{分类名称}分隔符去掉及只调用下级分类方法

    if($catid) { if($CAT['parentid']) { $seo_catname = ''; $tmp = strip_tags(cat_pos($CAT, 'DESTOON')); ...

  3. 修改浏览器的User-Agent来伪装你的浏览器和操作系统

    近期很多文章都提到了User-Agent (UA) 字符串,但大部分网友都不知道这个东西有什么用处.其实简单的说User-Agent就是客户端浏览器等应用程序使用的一种特殊的网络协议,在每次浏览器(邮 ...

  4. C程序设计语言练习题1-14

    练习1-14 编写一个程序,打印输入中各个字符出现频度的直方图. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() // 定义名为main的 ...

  5. android:onclick属性

    android:onclick属性设置点击时从上下文中调用指定的方法,此时给指定一个方法名.例如: xml中: <Button android:layout_width="wrap_c ...

  6. JDBC连接mysql,TOMCAT错误: Cannot convert value '0000-00-00 00:00:00' from column 10 to TIMESTAMP

    解决思路依据如下URL: http://blog.csdn.net/stail111/article/details/5640109 问题:MySQL数据库,如果数据库中日期字段为空为值为'0000- ...

  7. MYSQL如何导出存储过程和触发器?

    今天遇到.. 类似下面的就可以: mysqldump -u root -p -ntd -R  nxsc>nxsc_trigger.sql

  8. QQ登录-第三方SDK的接入总结(搜索 qq互联)

    由于项目的需要,使用了[QQ登录]SDK 的相关功能!   1.集成[QQ登录]SDK   [QQ登录]SDK下载地址: http://wiki.open.qq.com/wiki/website/SD ...

  9. SSL 证书申请(居然还可以在淘宝上购买)

    免费的目前有 2 个国内的:免费SSL证书申请国外的:StartSSL™ Certificates & Public Key Infrastructure 备注:其实,国内的这家的根证书,也是 ...

  10. jmap命令结合mat插件分析内存泄露--OQL

    http://smallnetvisitor.iteye.com/blog/1826434 User.java package gc; import java.util.ArrayList; impo ...