hdu5362 Just A String DP
来源:互联网 发布:黑九月 知乎 编辑:程序博客网 时间:2024/06/07 03:38
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 sizem .
For a strings , 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.
For a 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 integersn and m (1≤n,m≤2000) .
The first line contains two integers
Output
For each case, if the expected number is E , a single integer denotes E⋅mn mod 1000000007 .
Sample Input
32 23 210 3
Sample Output
10401908021
如果子串长度为偶数,那么它的个数为奇数的字符种类数为0,如果长度为奇数,那么它的个数为奇数的字符种类数为1.
dp[i][j]表示长度为i,j种字符个数为奇数。
dp[i][j]=dp[i-1][j-1]*(m-j+1)+dp[i-1][j+1]*(j+1)。
最后的答案为子串i是偶数,j=0,i是奇数,j=1补全为n的总数。
#include<iostream>#include<cstdio>#include<set>#define inf 1000000000000000#define ll long longusing namespace std;const ll mod=1000000007;ll dp[2005][2005];ll che[2005];int n,m;int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); che[0]=1; for(int i=1;i<=n;i++) che[i]=che[i-1]*m%mod; dp[1][0]=0; dp[1][1]=m; for(int i=2;i<=n;i++) { int k=min(i,m); for(int j=0;j<=k;j++) { dp[i][j]=0; if(j-1>=0) { dp[i][j]=(dp[i][j]+dp[i-1][j-1]*(m-j+1))%mod; } if(j+1<=min(i-1,k)) { dp[i][j]=(dp[i][j]+dp[i-1][j+1]*(j+1))%mod; } } } ll ans=0; for(int i=1;i<=n;i++) { ans=(ans+dp[i][i&1]*(n-i+1)%mod*che[n-i]%mod)%mod; } printf("%I64d\n",ans); } return 0;}
0 0
- hdu5362 Just A String DP
- 2015 Multi-University Training Contest 6(hdu5362 - Just A String)dp求期望
- 【HDU】5362 Just A String【生成函数dp】
- BNUOJ Just a String
- hdu 3553 Just a String (后缀数组)
- CDOJ 1305 Just a Magic String
- 【HDU】5121 Just A Mistake【容斥+dp】
- 2017北京师范大学ACM校赛 J Just A String (KMP)
- E - Generate a String dp
- codeforces 710E Generate a String(dp)
- CodeForce 710E - Generate a String(dp)
- CodeForces 710E Generate a String (DP)
- CodeForces-710E Generate a String(DP)
- just a test
- Just a test!
- Just A Test
- Just a test
- just a test
- 蓝桥杯2014-7蚂蚁感冒
- TextView属性大全
- Reverse Bits
- 面向对象三大特点:封装,继承,多态
- 黑马程序员——网络编程4:域名解析 & 网络结构
- hdu5362 Just A String DP
- 欢迎使用CSDN-markdown编辑器
- 【Linux】FrameBuffer操作入门
- Android核心基础-8.Android四大组件之Activity-2.关闭、传递数据
- Redis 5种数据类型,2种特殊数据处理策略
- light oj 1017 Brush (III)
- [经典书籍]编程经典书籍
- 字符串的排列和组合代码 C++
- [leetcode] Sudoku Solver