【洛谷】AT_abc178_d [ABC178D] Redistribution 的题解

发布于:2024-10-09 ⋅ 阅读:(156) ⋅ 点赞:(0)

【洛谷】AT_abc178_d [ABC178D] Redistribution 的题解

洛谷传送门

AT传送门

题解

一个水水的动态规划,阿巴巴巴。

题目大概是这样:

给定一个正整数 S S S,问有多少个数满足以下条件:

  • 序列中不能出现小于 3 3 3 的正整数。

  • 序列中的和必须等于输入的 S S S

这是一道求方案数的题,我们可以用动态规划来做,那么我们就可以定义 d p i dp_i dpi 为和为 i i i
时的方案数,然后我们就可以想到对于每一个 d p i dp_i dpi 它都等于从
i − 3 i−3 i3 3 3 3 的方案数总和加一,最后输出 f n f_n fn 即可。

最后提醒,AtCoder 输出要换行!!!

代码

#include <bits/stdc++.h>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
namespace fastIO {
	inline int read() {
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int n;
ll f[2005];
int main() {
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	n = read();
	for(int i = 3; i <= n; i ++) {
		f[i] = 1;
		for(int j = 3; j <= i - 3; j ++) {
			f[i] = (f[i] + f[j]) % mod;
		}	
	}
	cout << f[n] << endl;
	return 0;
}

网站公告

今日签到

点亮在社区的每一天
去签到