news 2026/7/6 6:04:11

AtCoder Beginner Contest 465 E - Digit Circus

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
AtCoder Beginner Contest 465 E - Digit Circus

E - Digit Circus

思路

看到这道题10的500次方的数据范围以及对于每一位数字、数字和和数字种类的讨论,我们不难想到这是一道数位DP。

那现在的问题就是怎么去完成数位DP的过程。

首先,我们的DP必须要考虑到题目要求的所有目标:3的倍数、含有3、用到3种不同的数字。同时我们也要记录数位DP通用的状态:考虑到第几位、是否触及上限。

于是,我们可以得到一个DP数组,dp[i][j][op][mask] :i 表示考虑到从左到右的第 i 位,j 表示当前余数为 j ,op 表示当前是否含有 3 ,mask 是一个对于用到哪些数字的状态压缩。考虑到这道题目要求仅仅为满足其一,数组存储的是有8个元素的容器,通过状态压缩表示满足当前条件的数字数量。

最后,在处理 DP 的过程上,我使用了记忆化搜索。

正解

#include<bits/stdc++.h> #define int long long #define si int using namespace std; typedef vector<int> arr; const int N = 505; const int mod = 998244353; string s; int n; bool vis[N][3][2][1<<10]; arr dp[N][3][2][1<<10]; inline arr dfs(int x, int tight, int mod3, int has3, si mask) { arr res = arr(8); if(x == n) { int f1 = 0, f2 = 0, f3 = 0; if(mask == 0) return res; if(mod3 == 0) f1 = 1; if(has3 == 1) f2 = 1; if(__builtin_popcount(mask) == 3) f3 = 1; int t = f1 | (f2 << 1) | (f3 << 2); res[t] = 1; return res; } if(!tight && vis[x][mod3][has3][mask]) return dp[x][mod3][has3][mask]; int up = (tight? s[x] - '0' : 9); for(int d = 0; d <= up; d++) { int ntight = tight; int nmod3 = mod3; int nhas3 = has3; si nmask = mask; ntight = tight & (d == up); if(mask == 0) { if(d != 0) { nmod3 = d % 3; nhas3 = (d == 3); nmask = (1 << d); } } else { nmod3 = (mod3 * 10 + d) % 3; nhas3 = has3 | (d == 3); nmask = mask | (1 << d); } auto nxt = dfs(x + 1, ntight, nmod3, nhas3, nmask); for(int i = 0; i < 8; i++) res[i] = (res[i] + nxt[i]) % mod; } if(!tight){ vis[x][mod3][has3][mask] = true; dp[x][mod3][has3][mask] = res; } return res; } signed main() { cin>>s; n = s.size(); arr res = dfs(0,1,0,0,0); int ans = 0; for(si i = 0; i < 8; i++) if(__builtin_popcount(i) == 1) ans = (ans + res[i]) % mod; cout<<ans; return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/6 6:04:05

基于MCP与Playwright的Threads评论数据自动化抓取与分析实战

1. 项目概述与核心价值 最近在做一个挺有意思的Side Project&#xff0c;核心目标是把社交媒体平台Threads上的评论数据自动化地抓取下来&#xff0c;然后做一些初步的分析。这个需求其实挺普遍的&#xff0c;无论是做品牌舆情监控、竞品分析&#xff0c;还是研究社区讨论趋势&…

作者头像 李华
网站建设 2026/7/6 6:03:22

网盘直链解析技术方案:基于浏览器扩展的多平台文件下载架构设计

网盘直链解析技术方案&#xff1a;基于浏览器扩展的多平台文件下载架构设计 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 &#xff0c;支持 百度网盘 / 阿里云盘 / 中国移动云…

作者头像 李华
网站建设 2026/7/6 6:02:10

别再只会聊天了!2026年最值得掌握的10个AI应用场景

目录 一.写作前面 二.10个AI应用场景 2.1 非技术岗的5个场景 2.1.1 场景1&#xff1a;Excel/报表自动化处理 2.1.2 场景2&#xff1a;合同/票据智能审查 2.1.3 场景3&#xff1a;公文/材料/周报写作 2.1.4 场景4&#xff1a;会议纪要待办追踪 2.1.5 场景5&#xff1a;企…

作者头像 李华
网站建设 2026/7/6 6:01:15

Reset Windows Update Tool:终结Windows更新故障的智能解决方案

Reset Windows Update Tool&#xff1a;终结Windows更新故障的智能解决方案 【免费下载链接】Reset-Windows-Update-Tool Troubleshooting Tool with Windows Updates (Developed in Dev-C). 项目地址: https://gitcode.com/gh_mirrors/re/Reset-Windows-Update-Tool Wi…

作者头像 李华
网站建设 2026/7/6 5:58:24

3分钟免费激活Windows系统:KMS_VL_ALL_AIO智能脚本终极指南

3分钟免费激活Windows系统&#xff1a;KMS_VL_ALL_AIO智能脚本终极指南 【免费下载链接】KMS_VL_ALL_AIO Smart Activation Script 项目地址: https://gitcode.com/gh_mirrors/km/KMS_VL_ALL_AIO 还在为Windows激活问题烦恼吗&#xff1f;每次开机看到烦人的激活提醒&am…

作者头像 李华
网站建设 2026/7/6 5:58:13

NVIDIA 驱动 550.54.15 安装:3种方法对比(.run/apt/PPA)与性能实测

NVIDIA 驱动 550.54.15 安装&#xff1a;3种方法对比与性能实测在Linux环境下安装NVIDIA显卡驱动是许多开发者和高级用户必须面对的任务。不同于Windows系统的"一键安装"体验&#xff0c;Linux系统提供了多种驱动安装方式&#xff0c;每种方法都有其独特的优势和适用…

作者头像 李华