HackerRank Super Six Substrings dp
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 ;
}
HackerRank Super Six Substrings dp的更多相关文章
- UVA10269 Adventure of Super Mario(Floyd+DP)
UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...
- *[hackerrank]Sam and sub-strings
https://www.hackerrank.com/contests/w3/challenges/sam-and-substrings DP.注意到以N[i]结尾的每个字符串的子数字和有规律: 53 ...
- ZOJ1232 Adventure of Super Mario(DP+SPFA)
dp[u][t]表示从起点出发,到达i点且用了t次magic boot时的最短时间, 方程如下: dp[v][t]=min(dp[v][t],dp[u][t]+dis[u][v]); dp[v][t] ...
- UVa 10269 Adventure of Super Mario (Floyd + DP + BFS)
题意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用,但在 ...
- HDU 4455 Substrings ( DP好题 )
这个……真心看不出来是个DP,我在树状数组的康庄大道上欢快的奔跑了一下午……看了题解才发现错的有多离谱. 参考:http://www.cnblogs.com/kuangbin/archive/2012 ...
- ZOJ 1232 Adventure of Super Mario (Floyd + DP)
题意:有a个村庄,编号为1到a,有b个城堡,编号为a+1到a+b.现在超级玛丽在a+b处,他的家在1处.每条路是双向的,两端地点的编号以及路的长度都已给出.路的长度和通过所需时间相等.他有一双鞋子,可 ...
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...
- leetcode 动态规划类型题
1,Triangle int mininumTotal(vector<vector<int>>& triangle) { ; i >= ; --i) { ; j ...
- 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)
今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...
随机推荐
- 旋转屏幕导致Activity重建
简单来说,Activity是负责与用户交互的最主要机制,任何“设置”(Configuration)的改变都可能对Activity的界面造成影响,这时系统会销毁并重建Activity以便反映新的Conf ...
- c++再探string之eager-copy、COW和SSO方案
在牛客网上看到一题字符串拷贝相关的题目,深入挖掘了下才发现原来C++中string的实现还是有好几种优化方法的. 原始题目是这样的: 关于代码输出正确的结果是()(Linux g++ 环境下编译运行) ...
- c++之函数值传递和引用传递解析----关键在于理解函数return的实现机制(内存分配)
函数调用过程解析 func里的a存储在调用fun函数时开辟的栈空间里,这块栈只在调用func时对func可用,调用结束后返回的a,其实是暂存在寄存器里的(一般情况下是eax),而返回到main里时,m ...
- SIP协议&开源SIP服务器搭建和客户端安装
1. SIP SIP 是一个应用层的控制协议,可以用来建立,修改,和终止多媒体会话,例如Internet电话 SIP在建立和维持终止多媒体会话协议上,支持五个方面: 1) 用户定位: 检查终端用户 ...
- kafka之五:如何手动更新Kafka中某个Topic的偏移量
本文介绍如何手动跟新zookeeper中的偏移量.我们在使用kafka的过程中,有时候需要通过修改偏移量来进行重新消费.我们都知道offsets是记录在zookeeper中的,所以我们想修改offse ...
- dcos下rexray服务的配置
在dcos环境下,rexray服务的默认配置文件为/opt/mesosphere/etc/rexray.conf,而其服务文件则是 /etc/systemd/system/dcos-rexray.se ...
- ubuntu下安装显卡驱动
前言 以下内容是个人学习之后的感悟,转载请注明出处~ 作者的显卡是GT 730,现以NVIDIA-Linux-x86-384.69为例. 1.打开终端,先删除旧的驱动: sudo ...
- httpclient:实现有验证码的模拟登陆
//1.这种方式是先把验证码的图片下载到本地.并且根据网页解析获得token值//2.手动在控制台输入验证码//3.因为验证码图片已经下载下来,后面就可以使用图像文字识别package DoubanS ...
- g2o使用bug总结
g2o进行3d2d优化的时候,设置优化图的边时,注意setVertex()中顶点的顺序. void setVertex(size_t i, Vertex* v) { assert(i < _ve ...
- atcoder057D(组合数模板)
题目链接:http://abc057.contest.atcoder.jp/tasks/abc057_d 题意:给出n个数,可以选择x~y个数,使其平均值最大,求其最大平均数以及选择方案数. 思路:只 ...