正睿
DP:
下面均方案的操作顺序若无特殊说明无要求。
例1-1:将一个数
考虑 dp,
那么平凡的转移是
可以把其当成是一个数学问题,有一堆柱子,每次可以加一根柱子,要求最后所有柱子的长度之和为
我们考虑如果加入一个长度为
- 加入一根柱子
- 集体 +1
那么在不同的时刻加入柱子,最后其长度就是定的,所以可以不重不漏的转移,时间复杂度 .
例1-2:将
由于是不同的,加入的数的个数是
例1-3:同 1-2 但是正整数可以相同
根号分治,
例题:[NOI Online #1 入门组] 跑步
考虑根号分治,
Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, B = 320;
typedef long long ll;
typedef pair<int, int> pii;
int mod;
void cmax(int &x, int y) { if (x < y) x = y; }
void cmin(int &x, int y) { if (x > y) x = y; }
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void mul(int &x, int y) { x = 1ll * x * y % mod; }
template<typename T>
void dbg(const T &t) { cerr << t << endl; }
template<typename Type, typename... Types>
void dbg(const Type& arg, const Types&... args) {
#ifdef ONLINE_JUDGE
return ;
#endif
cerr << arg << ' ';
dbg(args...);
}
int n, m, f[N], g[B][N];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> mod;
m = sqrt(n) + 1;
f[0] = 1;
for (int i = 1; i < m; i++) {
for (int j = i; j <= n; j++) {
add(f[j], f[j - i]);
}
}
g[0][0] = 1;
for (int i = 1; i < m; i++) {
for (int j = i; j <= n; j++) {
g[i][j] = g[i][j - i];
if (j >= m) add(g[i][j], g[i - 1][j - m]);
}
}
int ans = 0;
for (int i = 0; i <= n; i++) {
int sum = 0;
for (int j = 0; j < m; j++) add(sum, g[j][n - i]);
add(ans, 1ll * sum * f[i] % mod);
}
cout << ans << '\n';
return 0;
}
trick: 连续段 dp
维护
- 新建一个段
- 将一个段的两边扩张
- 合并两个段
例题:[CEOI 2016] kangaroo
注意到题目中跳跃的序列是一个
那么考虑转移,
Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> pii;
void cmax(int &x, int y) { if (x < y) x = y; }
void cmin(int &x, int y) { if (x > y) x = y; }
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void mul(int &x, int y) { x = 1ll * x * y % mod; }
template<typename T>
void dbg(const T &t) { cerr << t << endl; }
template<typename Type, typename... Types>
void dbg(const Type& arg, const Types&... args) {
#ifdef ONLINE_JUDGE
return ;
#endif
cerr << arg << ' ';
dbg(args...);
}
int n, s, t, dp[N][N];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s >> t;
dp[1][1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (i != s && i != t) dp[i][j] = (1ll * j * dp[i - 1][j + 1] % mod + 1ll * (j - (i > s) - (i > t)) * dp[i - 1][j - 1] % mod) % mod;
else dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j]) % mod;
}
}
cout << dp[n][1] << '\n';
return 0;
}
字符串
周期的单调性:如果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现