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. 关于Java特种兵下冊

    <Java特种兵 下冊>这本书一直没出.至上冊出版近1年以来,每周会有非常多小伙伴发私信或邮件问起我下冊出版的事情,纵使眼下有非常多无奈,小胖还是初步决定准备写下冊. 下冊内容(下冊的内容 ...

  2. jQuery实现轮播效果(一) - 基础

    前戏: XXXX年XX月XX日,经理交给我一个站点新闻资讯网页开发的活儿.我一个java程序猿,怎么完毕得了网页设计这样高端的活儿呢! 之前尽管有学过一点HTML.CSS的知识.可是在实际的使用中,把 ...

  3. 【cl】maven新建项目

    http://blog.csdn.net/sushengmiyan/article/details/40142771 使用maven创建一个helloworld 在本地硬盘创建一个文件夹作为maven ...

  4. Memcache 和 Radis 比较

    Memcache 和 Radis 比较 2014-03-28 11:00 2447人阅读 评论(0) 收藏 举报  分类: memcache(6)  Redis(7)  版权声明:本文为博主原创文章, ...

  5. 学习vi和vim编辑器(1):vi文本编辑器

    UNIX系统中有非常多编辑器.能够分为两种类型:行编辑器和全屏编辑器.行编辑器每次仅仅能在屏幕中显示文件的一行,如ed和ex编辑器.全屏编辑器能够在屏幕上显示文件的一部分. vi(读为vee-eye) ...

  6. 搜索分析(DFS、BFS、递归、记忆化搜索)

    搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2 ...

  7. 【POJ 1222】 EXTENDED LIGHTS OUT

    [题目链接] http://poj.org/problem?id=1222 [算法] 列出异或方程组,用高斯消元求解即可 [代码] #include <algorithm> #includ ...

  8. BZOJ 1061费用流

    思路: 我们可以列出几个不等式 用y0带进去变成等式 下-上 可以消好多东西 我们发现 等式左边的加起来=0 可以把每个方程看成一个点 正->负 连边 跑费用流即可 //By SiriusRen ...

  9. 关于iscroll.js插件的使用

    iscroll 作用: 可以让区域滚动效果好看一些 使用: 1. html结构 外面必须包一层盒子,切内部的元素要尽量简单,不然会影响滚动效果 <div id="wrapper&quo ...

  10. Javascript关于JSON集合的几种循环方法

    /** * 根据json数据生成option树形控件 * 如果有children节点则自动生成树形数据 * @param {JSON} data * @param {int} n 节点深度 * @pa ...