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的更多相关文章

  1. UVA10269 Adventure of Super Mario(Floyd+DP)

    UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...

  2. *[hackerrank]Sam and sub-strings

    https://www.hackerrank.com/contests/w3/challenges/sam-and-substrings DP.注意到以N[i]结尾的每个字符串的子数字和有规律: 53 ...

  3. 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] ...

  4. UVa 10269 Adventure of Super Mario (Floyd + DP + BFS)

    题意:有A个村庄,B个城市,m条边,从起点到终点,找一条最短路径.但是,有一种工具可以使人不费力的移动L个长度,但始末点必须是城市或村庄.这种工具有k个,每个只能使用一次,并且在城市内部不可使用,但在 ...

  5. HDU 4455 Substrings ( DP好题 )

    这个……真心看不出来是个DP,我在树状数组的康庄大道上欢快的奔跑了一下午……看了题解才发现错的有多离谱. 参考:http://www.cnblogs.com/kuangbin/archive/2012 ...

  6. ZOJ 1232 Adventure of Super Mario (Floyd + DP)

    题意:有a个村庄,编号为1到a,有b个城堡,编号为a+1到a+b.现在超级玛丽在a+b处,他的家在1处.每条路是双向的,两端地点的编号以及路的长度都已给出.路的长度和通过所需时间相等.他有一双鞋子,可 ...

  7. 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 ...

  8. leetcode 动态规划类型题

    1,Triangle int mininumTotal(vector<vector<int>>& triangle) { ; i >= ; --i) { ; j ...

  9. 我的Android进阶之旅------>Android自定义View实现带数字的进度条(NumberProgressBar)

    今天在Github上面看到一个来自于 daimajia所写的关于Android自定义View实现带数字的进度条(NumberProgressBar)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...

随机推荐

  1. complex brain network

    Organization, development and function of complex brain networks The Brain as a Complex System: Usin ...

  2. C#入门---2、C#装备知识(C#创建桌面程序的时候创建的是什么应用程序)

    C#入门---2.C#装备知识(C#创建桌面程序的时候创建的是什么应用程序) 一.总结 一句话总结: WPF应用程序:来替代 WindowsFroms 来创建桌面应用程序 1.什么是控制台程序 Con ...

  3. map的详细用法 (转

    map的详细用法: map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能 力,由于这个特性,它完成有可能在我 ...

  4. HihoCoder 1473 : 小Ho的强迫症( 欧几里得 )

    描述 小Ho在一条笔直的街道上散步.街道上铺着长度为L的石板,所以每隔L距离就有一条石板连接的缝隙,如下图所示. 小Ho在散步的时候有奇怪的强迫症,他不希望脚踩在石板的缝隙上.(如果小Ho一只脚的脚尖 ...

  5. HDU2444(二分图判定+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. PHP程序中的redis一些写法

    <?php /** * 以下均要先链接好redis */ sdk\libs\RedisHelper::connect("s1")->keys('*'); //这个是获取 ...

  7. 微信小程序开发之页面注册

    页面Page是object Page({  data:{    String1  },  onLoad:function(options){    // 生命周期函数--监听页面加载 一个页面只会调用 ...

  8. HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题

    Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...

  9. Unity3D命令行Build

    转自:http://www.cnblogs.com/gameprogram/archive/2012/05/11/2496303.html 本来是没想用这个命令行Build方式,可惜电脑不知道怎么的就 ...

  10. Unity3d与3dmax模型比例问题

    1 把3dmax中1米的物体,在unity中为1厘米,所以unity中需要放大100倍才能跟3dmax中效果相同 2 unity中调整模型->inspector-scale factor可以调整 ...