CF1829H Don't Blame Me
题解
知识点:线性dp,位运算。
考虑设 表示考虑了前 个数字,与和为 的方案数。转移方程显然。
注意初值为 表示空集,此时注意 时要减去空集这一个方案。
当然也可以选择不加入空集,但dp过程需要特别处理只选自己的方案。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> using namespace std; using ll = long long; const int P = 1e9 + 7; int a[200007]; int f[200007][64]; bool solve() { int n, k; cin >> n >> k; for (int i = 1;i <= n;i++) cin >> a[i]; f[0][63] = 1; for (int i = 1;i <= n;i++) { for (int j = 0;j <= 63;j++) f[i][j] = 0; for (int j = 0;j <= 63;j++) { (f[i][j] += f[i - 1][j]) %= P; (f[i][a[i] & j] += f[i - 1][j]) %= P; } } int ans = 0; for (int i = 0;i <= 63;i++) if (__builtin_popcount(i) == k) (ans += f[n][i]) %= P; cout << ans - (k == 6) << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/17686588.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具