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 ;
}

2015 Multi-University Training Contest 6 hdu 5362 Just A String的更多相关文章

  1. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  2. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  3. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  4. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  8. 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence

    Easy Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2015 Multi-University Training Contest 7 hdu 5378 Leader in Tree Land

    Leader in Tree Land Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

随机推荐

  1. 【剑指Offer学习】【面试题34:丑数】

    题目:我们把仅仅包括因子2.3 和5 的数称作丑数(Ugly Number).求从小到大的顺序的第1500个丑数. 举例说明: 比如6.8 都是丑数.但14 不是.它包括因子7.习惯上我们把1 当做第 ...

  2. 安卓实现序列化之Parcelable接口

    安卓实现序列化之Parcelable接口 1.实现序列化的方法: Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE本身就支持的) .一是实现Parcelabl ...

  3. [Java][Android][Process] Process 创建+控制+分析 经验浅谈

    不管是Android亦或者Java中或多或少须要调用底层的一些命令.运行一些參数: 此时我们须要用到Java的Process来创建一个子进程.之所以是子进程是由于此进程依赖于发起创建请求的进程,假设发 ...

  4. iOS 获取当前时间格式化字符串

    iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...

  5. Spring JDBC数据库开发

    针对数据库操作,Spring框架提供了JdbcTemplate类. 1.Spring JDBC的配置 创建配置文件applicationContext.xml,添加如下代码: <!--配置数据源 ...

  6. 【iOS开发-80】Quartz2D画图简单介绍:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  7. POJ2184 Cow Exhibition 背包

    题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值. 以sum(c[i])(c[i ...

  8. bind(),call(), apply()方法的区别是什么?

    bind(),call(), apply()方法的区别是什么? 共同点:改变this指向,任何调用都不在起作用 bind() 改变this的指向,不会调用函数,返回一个新的函数 var o ={a:' ...

  9. ansible upload

    # 链接地址:https://www.cnblogs.com/xiaoxiaoleo/p/6626299.html # synchronize: 从拉取远程服务器文件,需要加mode: pull # ...

  10. 第5章分布式系统模式 使用客户端激活对象通过 .NET Remoting 实现 Broker

    正在 .NET 中构建一个需要使用分布式对象的应用程序,并且分布式对象的生存期由客户端控制.您的要求包括能够按值或按引用来传递对象,无论这些对象驻留在同一台计算 机上,还是驻留在同一个局域网 (LAN ...