HDU 5185 Equation (DP)

题目:LINK

题意:求满足题目要求的x序列的种类数。

能够发现符合条件的序列去重后是一个0, 1, ..., k的连续序列(k满足k*(k+1)/2 <= n) ,则这个去重后的序列长度最长为sqrt(n)规模大小。

能够DP。dp[i][j]表示用到1~i的连续数字当前和为j的方法数。不用考虑长度是否满足n个,由于前面能够用0补上去。

dp[i][j] = dp[i][j-i] + dp[i-1][j-i];

ans = sum(dp[i][n]) for i in range(1, k)

/* ***********************************************
 	Author        : Napoleon
 	Mail		  : [email protected]
 	Created Time  : 2015-03-14 10:55:36
	Problem       : BC_32_D.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define INF 1000000000
//typedef __int64 LL;
#define N 50005
int n, t, m, dp[360][N]; 

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
	scanf("%d", &t);
	int ti = 0;
	while(t--) {
		scanf("%d%d", &n, &m);
		int M = 0;
		while(M*(M+1) <= 2*n) M++;
		M--;
		dp[0][0] = 1;
		for(int i = 1;i <= M; i++) {
			for(int j = i; j <= n; j++) {
				dp[i][j] = (dp[i][j-i] + dp[i-1][j-i]) % m;
			}
		}
		int ans = 0;
		for(int i = 1; i <= M; i++) {
			ans = (ans + dp[i][n]) % m;
		}
		printf("Case #%d: %d\n", ++ti, ans);
	}
	return 0;
}
时间: 2024-12-09 13:04:59

HDU 5185 Equation (DP)的相关文章

HDU 5185 Equation (线性dp 完全背包)

Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 64    Accepted Submission(s): 20 Problem Description Gorwin is very interested in equations. Nowadays she gets an equation like this x1+

hdu 5185 Equation(分析+DP)

题意: Gorwin is very interested in equations. Nowadays she gets an equation like thisx1+x2+x3+?+xn=n, and here 0≤xi≤nfor1≤i≤nxi≤xi+1≤xi+1for1≤i≤n−1 For a certain n, Gorwin wants to know how many combinations of xi satisfies above condition.For the answ

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000

hdu 4123 树形DP+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=4123 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,

HDU 3853 概率dp

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2337    Accepted Submission(s): 951 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).Homura wants to help he

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];

HDU 2845 Beans(DP,最大不连续和)

题意    吃豆子游戏    当你吃了一个格子的豆子   该格子左右两个和上下两行就不能吃了    输入每个格子的豆子数    求你最多能吃多少颗豆子 可以先求出每行你最多可以吃多少颗豆子   然后每行就压缩成只有一个格子了   里面的豆子数就是那一行最多可以吃的豆子数   然后问题就变成求一列最多可以吃多少颗豆子了   和处理每一行一样处理   那么问题就简化成求一行数字的最大不连续和问题了 令d[i]表示某一行前i个豆子的最大和  有两种情况  吃第i个格子中的豆子和不吃第i个格子中的豆子