Just A String

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

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 \leq n,m \leq 2000)$.

Output
For each case, if the expected number is E, a single integer denotes$ E\dot mn mod 1000000007$.

Sample Input
3
2 2
3 2
10 3

Sample Output
10
40
1908021

Author
zimpha@zju

Source
 
解题:动态规划
 
吗各级,T了一下午
 
 dp[i][j] 表示长度为i的有j种字母是奇数个的串的个数
 
dp[i][j]可以有两种方向转移过来
一种是dp[i-1][j-1]选那种个数是偶数的字符 既然有j-1种是奇数,那么剩下的 m - j + 1的种数的个数都是偶数,增加其中一个,就多出一种个数是奇数的种数,偶数的选择方式有m - j + 1种
 
另一种转移方向是 dp[i-1][j+1] 从j + 1这些个数是奇数的种数中选择任一一个,增加这种一个,就会少个奇数个数的种数,可以发现有j + 1种选择方式
 
 #include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = ;
const int mod = ;
long long dp[maxn][maxn],PM[maxn];
int main() {
PM[] = dp[][] = ;
int kase,n,m;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
dp[][] = m;
for(int i = ; i <= n; ++i) PM[i] = PM[i-]*m%mod;
for(int i = ; i <= n; ++i) {
for(int j = , k = min(i,m); j <= k; ++j) {
dp[i][j] = ;
if(j) dp[i][j] += dp[i-][j-]*(m - j + );
if(j + <= min(i - ,k)) dp[i][j] += dp[i-][j+]*(j + );
dp[i][j] %= mod;
}
}
long long ret = ;
for(int i = ; i <= n; ++i)
ret += dp[i][i&]*(n - i + )%mod*PM[n-i]%mod;
printf("%I64d\n",ret%mod);
}
return ;
}

最新文章

  1. linux系统免密码登陆
  2. mongodb版本特性
  3. 【MySQL】MySQL 5.7 sys Schema
  4. Kali Linux Web 渗透测试视频教程— 第四课 google hack 实战
  5. Ubuntu学习笔记-win7&amp;Ubuntu双系统简单搭建系统指南
  6. Oralce新建数据库、新建远程登录用户全过程
  7. iOS 数组字典操作
  8. Cglib及其基本使用
  9. Joone
  10. MyBatis3系列__04CRUD以及参数处理
  11. one-hot编码
  12. Java开发环境配置(Jdk、Tomcat、eclipse)
  13. Django 中间件 请求前
  14. Elasticsearch学习笔记——分词
  15. poj 3320(尺取法)
  16. bzoj5312: 冒险(势能均摊线段树)
  17. 机器学习ML策略
  18. String.format的用法
  19. poj 3278 Catch That Cow(bfs+队列)
  20. 各版本.NET委托的写法回顾(转)

热门文章

  1. Android Studio Mac 快捷键整理分享
  2. iOS开发-自己定义重用机制给ScrollerView加入子视图
  3. POJ 3080 Blue Jeans (后缀数组)
  4. Memcache 和 Radis 比较
  5. SpringBoot之表单验证@Valid
  6. Nginx调优实战
  7. ubantu下NiFi单节点安装部署
  8. TYVJ 1941 BZOJ3038 上帝造题的七分钟2 并查集+树状数组
  9. c# 正则表达式regex心得
  10. Web-数据库-mysql篇