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

最新文章

  1. 新的一年快开始了,学点新东西吧,从React开始(一)
  2. SVN 删除误上传到服务器的文件
  3. sql server常用语法点
  4. Javascript模块规范(CommonJS规范&amp;&amp;AMD规范)
  5. [Angular 2] Share a Service Across Angular 2 Components and Modules
  6. C++11实现模板手柄:委托构造函数、defaultkeyword分析
  7. C# out的使用 函数例题
  8. JPA + SpringData 操作数据库原来可以这么简单 ---- 深入了解 JPA - 2
  9. HTTP协议与TCP/IP协议
  10. Bootstrap(9) 巨幕页头缩略图和警告框组件
  11. php-fpm的status可以查看汇总信息和详细信息
  12. .Net 垃圾回收和大对象处理 内存碎片整理
  13. MVC教程八:母版页(布局页)视图
  14. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing ___Error Installing APK
  15. CMD命令利用tasklist与taskkill关闭程序
  16. 不同意义的new和delete
  17. nodejs Async详解之三:集合操作
  18. Using the JDBC Driver
  19. js获取网页上选中的部分,包含html代码
  20. 【BZOJ】2724: [Violet 6]蒲公英

热门文章

  1. Python文件处理之文件读取方式(二)
  2. C#7.0
  3. RazorEngine 学习笔记
  4. ELK( ElasticSearch+ Logstash+ Kibana)分布式日志系统部署文档
  5. Oracle12c中新建用户
  6. MFC基本框架
  7. Android软件的国际化
  8. Android取得电池的电量
  9. CF-Approximating a Constant Range
  10. puppet aix package 之rsync安装