2019牛客暑期多校训练营(第四场) - K - number - dp
2024-09-05 21:09:12
https://ac.nowcoder.com/acm/contest/884/K
一开始整了好几个假算法,还好测了一下自己的样例过了。
考虑到300的倍数都是3的倍数+至少两个零(或者单独的0)。
求以第i个位置的数为结尾的前缀和为j的数的方案数。
当遇到至少两个0的时候,ans+=dp[0][i-2]+1。
+1是那两个0的贡献。
这样子算会漏算单独的0的贡献,最后加回去。
还因为忘记mod3段错误好几次。
老了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[100005];
int dp[3][100005];
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
while(~scanf("%s", s)) {
int n = strlen(s);
for(int i = 0; i < n; ++i)
s[i] -= '0';
dp[0][0] = dp[1][0] = dp[2][0] = 0;
++dp[s[0] % 3][0];
int lx0 = (s[0] == 0);
ll ans = 0;
for(int i = 1; i < n; ++i) {
int c = s[i];
dp[c % 3][i] = dp[0][i - 1];
dp[(c + 1) % 3][i] = dp[1][i - 1];
dp[(c + 2) % 3][i] = dp[2][i - 1];
++dp[c % 3][i];
lx0 = (c == 0) ? lx0 + 1 : 0;
if(lx0 >= 2)
ans += dp[0][i - 2] + 1;
}
for(int i = 0; i < n; ++i)
ans += (s[i] == 0);
printf("%lld\n", ans);
}
}
最新文章
- runtime-给系统已有类添加属性
- Linux命令学习总结:pwd命令
- [WPF系列]-DynamicResource与StaticResource的区别
- null、undefined、false、0相等性比较
- WebAPI IIS PUT和DELETE请求失败
- Hadoop 7、MapReduce执行环境配置
- <;meta http-equiv=";Pragma"; content=";no-cache";>;
- android屏幕适配方法
- 简单C程序,迷宫
- csrf
- JavaScript原型与原型链
- 洗礼灵魂,修炼python(64)--爬虫篇—re模块/正则表达式(2)
- 学JS的心路历程 - JS的Class
- linux下如何编写shell脚本
- SDN期末验收
- git的使用与学习
- [转]Tomcat的部署
- 剑指offer五十三之表示数值的字符串
- PHP write byte array to file
- 分布式系统的Raft算法学习笔记
热门文章
- 使用LVM快照进行数据库备份
- 【LuoguP2792 】[JSOI2008]小店购物(最小树形图)
- 【python实例】判断质数:for-break-else
- qs的两个用途
- MCU2FPGA之SPI时序总线
- JS FormData 文件异步提交
- IIS新建项目
- (16)Python3.5+Pyqt5+PyCharm+Opencv3.3+Qtdesigner开发环境配置
- org.xml.sax.SAXParseException: 元素类型 ";input"; 必须由匹配的结束标记 ";<;/input>;"; 终止。
- characteristics of competent communicators