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);
}
}

最新文章

  1. runtime-给系统已有类添加属性
  2. Linux命令学习总结:pwd命令
  3. [WPF系列]-DynamicResource与StaticResource的区别
  4. null、undefined、false、0相等性比较
  5. WebAPI IIS PUT和DELETE请求失败
  6. Hadoop 7、MapReduce执行环境配置
  7. &lt;meta http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;&gt;
  8. android屏幕适配方法
  9. 简单C程序,迷宫
  10. csrf
  11. JavaScript原型与原型链
  12. 洗礼灵魂,修炼python(64)--爬虫篇—re模块/正则表达式(2)
  13. 学JS的心路历程 - JS的Class
  14. linux下如何编写shell脚本
  15. SDN期末验收
  16. git的使用与学习
  17. [转]Tomcat的部署
  18. 剑指offer五十三之表示数值的字符串
  19. PHP write byte array to file
  20. 分布式系统的Raft算法学习笔记

热门文章

  1. 使用LVM快照进行数据库备份
  2. 【LuoguP2792 】[JSOI2008]小店购物(最小树形图)
  3. 【python实例】判断质数:for-break-else
  4. qs的两个用途
  5. MCU2FPGA之SPI时序总线
  6. JS FormData 文件异步提交
  7. IIS新建项目
  8. (16)Python3.5+Pyqt5+PyCharm+Opencv3.3+Qtdesigner开发环境配置
  9. org.xml.sax.SAXParseException: 元素类型 &quot;input&quot; 必须由匹配的结束标记 &quot;&lt;/input&gt;&quot; 终止。
  10. characteristics of competent communicators