https://www.hackerrank.com/contests/hourrank-18/challenges/super-six-substrings

能被6整除的数有一个特点,就是能同时被3和被2整除

那么也就是能整除3的偶数。

设dp[i][j]表示以第i位结尾的所有子串中,%3的余数是j的方案数,

dp[i + 1][(j * 10 + nowNumber) % 3] = dp[i][j]

注意那个 * 10是必须的,因为从第i项转移过来,位数应该移动一位。就比如,12

1 % 3 = 1, 然后加入一个2,是1 * 10 + 2 = 12 % 3 = 0

但是这里又不是必须的,因为10 % 3 = 1,所以相当于没加。

现在说说那个0, 0是不能做前缀的,怎么破、

对于0,只算一个贡献,不加进去dp统计那里就行。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
LL dp[][];
char str[maxn];
void work() {
cin >> str + ;
int lenstr = strlen(str + );
int now = ;
LL ans = ;
for (int i = ; i <= lenstr; ++i) {
now = !now;
memset(dp[now], , sizeof dp[now]);
int num = str[i] - '';
if (num == ) {
ans += dp[!now][] + ;
memcpy(dp[now], dp[!now], sizeof dp[!now]);
continue;
}
dp[now][num % ] = ;
for (int j = ; j <= ; ++j) {
dp[now][(j * + num) % ] += dp[!now][j];
}
if (num % == ) {
ans += dp[now][];
// if (str[i - 1] == '0') ans--;
}
// cout << ans << endl;
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

最新文章

  1. textarea光标处插入文字
  2. 关于yii2框架活动记录activeRecord添加默认字段的问题
  3. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:create (default-cli) on project standalone-pom: Unable to parse configuration of 3: mojo org.apache.maven.plugins:
  4. 提示此windows副本不是正版的win7系统7601解决方法
  5. Hibernate day02笔记
  6. 如何修改 EM12c 中 SYSMAN 用户的密码?
  7. JAVA 构造代码块
  8. MATLAB中如何使用遗传算法
  9. php判断是移动端还是pc
  10. yield 生成器例子
  11. 关于.net根目录路径的问题
  12. OCA读书笔记(13) - 性能管理
  13. zf-中间库(xzfw_xzjc_jianshi)
  14. html+css底部自动固定底部
  15. Chrome浏览器读写系统剪切板
  16. typescript中的接口
  17. java.util.NoSuchElementException错误原因及解决方案
  18. 利用ApplicationContextAware装配Bean
  19. 异常:No Spring WebApplicationInitializer types detected on classpath
  20. Windows登录类型及安全日志解析

热门文章

  1. Java笔记(四)
  2. spellchecker inspection helps locate typeos and misspelling in your code, comments and literals, and fix them in one click
  3. py-day1简单使用方法及语法使用详解
  4. 利用union判断系统的大小端
  5. Qt5.7不能加载MySql驱动问题.(需要重新编译驱动)
  6. bzoj1598
  7. Ubuntu 安装 texlive
  8. shell的split生成的文件按规律命名及添加扩展名
  9. eclispe的使用
  10. JQuery学习笔记(二)JQuery方法