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)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...
随机推荐
- html5--3.19 新增的progress/meter元素
html5--3.19 新增的progress/meter元素 学习要点 了解progress/meter元素的用法 progress元素 是HTML5中新增的元素,用来建立一个进度条 通常与Java ...
- hdu 1391 Number Steps(规律)
题意:找规律 思路:找规律 #include<iostream> #include<stdio.h> using namespace std; int main(){ int ...
- Jquery 获取所有对象的第一个子元素
转自:http://blog.sina.com.cn/s/blog_5fdbd0410100pmnn.html <ul> <li>John</li> <l ...
- Redis GEO 特性在 LBS 中的应用总结
什么是LBS LBS(Location Based Service),基于位置的服务. Redis和GEO Redis 是最热门的 nosql 数据库之一,它的最大特点就是快.所以在 LBS 这种需要 ...
- 动态规划专题 01背包问题详解 HDU 2546 饭卡
我以此题为例,详细分析01背包问题,希望该题能够为大家对01背包问题的理解有所帮助,对这篇博文有什么问题可以向我提问,一同进步^_^ 饭卡 Time Limit: 5000/1000 MS (Java ...
- Beyond Compare 简体版+注册码
Beyond Compare 3.3.4.14431 官方简体版+注册码 查阅全文 ›
- Unity4.0配置
关于Unity4.0的使用: 一 安装Unity 在程序包管理器控制台输入命令:Istall-Pckage unity.mvc安装后会在App_Start中生成UnityConfig.cs 和Unit ...
- Prime Independence
题意: 对于给定集合,求解最大的子集合,使得集合内两两之商不为质数. 解法: 考虑对于每一个数字分解质因数可以得到 $O(nloglogNUM)$ 条两个数字不可以出现在同一集合的信息. 同时发现一条 ...
- linux内核中ip,tcp等头的定义(转)
一.MAC帧头定义 /*数据帧定义,头14个字节,尾4个字节*/typedef struct _MAC_FRAME_HEADER{ char m_cDstMacAddress[6]; //目的m ...
- Tomcat自定义classLoader加密解密
class很好反编译,所以需要对class文件先进行加密,然后使用自己的classloader进行解密并加载. [步骤] 大概分两步: 1.对class文件进行加密 2.写解密class文件并加载的c ...