【DP】 HDOJ 5230 ZCC loves hacking

dp[i][j] 代表用了i个数,组成j的方案数,那么dp[i][j]可以由dp[i-1][j-i]递推而来,相当于前面i-1个数加1,最后加个1,也可以由dp[i][j-i]递推而来,相当于i个数每个数加1.。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 40005
#define eps 1e-12
#define mod 998244353
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
//#define ls o<<1
//#define rs o<<1 | 1
//#define lson o<<1, L, mid 
//#define rson o<<1 | 1, mid+1, R
//#pragma comment(linker, "/STACK:102400000,102400000")
#define pii pair<int, int> 
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

LL res[maxn];
LL dp[2][maxn];

void init()
{
	int now = 0, pre;
	dp[now][0] = 1;
	for(int i = 1; i <= 450; i++) {
		now = now ^ 1;
		pre = now ^ 1;
		for(int j = 0; j < maxn; j++) res[j] = (res[j] + dp[pre][j]) % mod;
		for(int j = 0; j < i; j++) dp[now][j] = 0;
		for(int j = i; j < maxn; j++) dp[now][j] = (dp[pre][j - i] + dp[now][j - i]) % mod;
	}
	for(int i = 1; i < maxn; i++) res[i] = (res[i] + res[i-1]) % mod;
}

void work()
{
	int c, l, r, n;
	scanf("%d%d%d%d", &n, &c, &l, &r);
	l -= c, r -= c;
	LL ans = res[r];
	if(l) ans = ((ans - res[l-1]) % mod + mod) % mod;
	printf("%lld\n", ans);
}

int main()
{
	int _;
	init();
	scanf("%d", &_);
	while(_--) work();
	
	return 0;
}


你可能感兴趣的:(dp)