hdu5362 Just A String(dp)
转载请注明出处: 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
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.
The first line contains two integers n and m (1≤n,m≤2000).
当时状态真是见鬼,其实这题还是比较容易的一个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 ;
}
hdu5362 Just A String(dp)的更多相关文章
- codeforces 710E E. Generate a String(dp)
题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
- LeetCode-Interleaving String[dp]
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- PAT 1040 Longest Symmetric String[dp][难]
1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...
- 87. Scramble String (String; DP)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 115. Distinct Subsequences (String; DP)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 72. Edit Distance (String; DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 97. Interleaving String (String; DP)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
随机推荐
- python 装饰器简介
24.装饰器 1.@ + 函数名:#1自动执行outer函数并且将下面的函数名f1当做参数传递 #2将outer函数的返回值,重新赋值给f1. def ou ...
- MSP430 中断优先级
MSP430的中断优先级.打开关闭.中断嵌套 优先级顺序从高到低为: PORT2_VECTOR (1 * 2u) /* 0xFFE2 Port 2 */ PORT1_VECTOR (4 * ...
- 转:堆(heap)和栈(stack)有什么区别??
简单的可以理解为: heap:是由malloc之类函数分配的空间所在地.地址是由低向高增长的. stack:是自动分配变量,以及函数调用的时候所使用的一些空间.地址是由高向低减少的. 预备知识—程序的 ...
- 剑指offer之关于整数的处理
首先是整数次方的处理 在这处理的时候有几个细节主义处理 1.当指数是负数的时候 2.当指数式0的时候 3.当不满足条件的时候要抛出异常 再一个就是常用的将一个树化为二进制的形式,或者是求整数的幂或者矩 ...
- JS版本网站资源状态检测
Title:JS版本网站资源状态检测 --2012-08-28 14:08 前几天需要一个网站状态检测的东东,后面写了个蹩脚的JS版本,里面用到了以前没用过的东西,在这里记下来,其实批处理加curl ...
- 导出excel的三种方式
第一种是Response输出,这种方式输出的文件不符合标准的excel格式,在打开的时候会有提示,而且不好控制内容.第一种是Response输出,这种方式输出的文件不符合标准的excel格式,在打开的 ...
- Delphi从内存流中判断图片格式(好多相关文章)
废话不多说了,利用内存流来判断文件的格式,其实判断文件的前几个字节就可以简单的判断这个文件是什么类型的文件,例如jpg文件 是 FFD8 (从低位到高位就要反过来 D8FF 下面都是一样)BMP文件 ...
- 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析
原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...
- 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1
描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8 ...
- HDFS Federation与HDFS High Availability详解
HDFS Federation NameNode在内存中保存文件系统中每个文件和每个数据块的引用关系,这意味着对于一个拥有大量文件的超大集群来说,内存将成为限制系统横向扩展的瓶颈.在2.0发行版本系列 ...