HackerRank Super Six Substrings dp
2024-08-25 15:58:11
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 ;
}
最新文章
- textarea光标处插入文字
- 关于yii2框架活动记录activeRecord添加默认字段的问题
- [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:
- 提示此windows副本不是正版的win7系统7601解决方法
- Hibernate day02笔记
- 如何修改 EM12c 中 SYSMAN 用户的密码?
- JAVA 构造代码块
- MATLAB中如何使用遗传算法
- php判断是移动端还是pc
- yield 生成器例子
- 关于.net根目录路径的问题
- OCA读书笔记(13) - 性能管理
- zf-中间库(xzfw_xzjc_jianshi)
- html+css底部自动固定底部
- Chrome浏览器读写系统剪切板
- typescript中的接口
- java.util.NoSuchElementException错误原因及解决方案
- 利用ApplicationContextAware装配Bean
- 异常:No Spring WebApplicationInitializer types detected on classpath
- Windows登录类型及安全日志解析
热门文章
- Java笔记(四)
- spellchecker inspection helps locate typeos and misspelling in your code, comments and literals, and fix them in one click
- py-day1简单使用方法及语法使用详解
- 利用union判断系统的大小端
- Qt5.7不能加载MySql驱动问题.(需要重新编译驱动)
- bzoj1598
- Ubuntu 安装 texlive
- shell的split生成的文件按规律命名及添加扩展名
- eclispe的使用
- JQuery学习笔记(二)JQuery方法