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)的精彩案例,在这里分享给大家一起来学习学习!同时感谢 ...
 
随机推荐
- CI核心文件分析之基准测试类 (Benchmark.php)
			
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter * * ...
 - C#开发遇到的常见问题及知识点
			
今天遇到的类型初始值设定项引发异常的原因是:类没有添加[Serializable]属性. this.DialogResult = System.Windows.Forms.DialogResult.O ...
 - 小工具之Synergy
			
用于两个主机共享键盘和鼠标的工具: 软件名字:synergy软件主页: http://synergy-foss.org支持平台:linux,mac,windows 通吃 作用:通过网络在多台主机之间共 ...
 - TX2 刷机过程
			
1.拿到板子,上电 (1)输入 ls (2)进入NVIDIA-INSTALLER/ (3)再sudo ./installer.sh 账户和密码都是:nvidia (4)sudo reboot 参考博客 ...
 - C++模板之typename和class关键字的区别
			
我们都知道,在STL中基本上都使用了模板类的声明,即template.在模板类的声明中,我们有两种方式: template <class T> template <typename ...
 - 【重磅推荐】嵌入式Linux经典书单(部分含视频)
			
一直都有人问我要书单,在网上搜索大半天,没找到合适的,他们写的太不负责了,遂决定自己整理. 本书单综合了豆瓣知乎热评,尤其参考了一线开发者韦东山学员群的小伙伴们的意见, 再结合本人多年答疑经验整理而成 ...
 - @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)
			
前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...
 - 修改Linux内核参数提高Nginx服务器在高的时候的性能
			
并发 Linux下高并发的Nginx服务器,当TCP TIME_WAIT套接字数量经常达到两.三万,服务器很容易被拖死.通过修改Linux内核参数,可以减少Nginx服务器的TIME_WAIT套接字数 ...
 - Win 7下破解Loadrunner 11(带中文版下载地址)
			
空间管理您的位置: 51Testing软件测试网 » 测试是一种生活态度 » 日志 与您一起分享在测试过程中的快乐与辛酸... Win 7下破解Loadrunner 11(带中文版下载地址) 上一篇 ...
 - C - Bear and Five Cards
			
Description A little bear Limak plays a game. He has five cards. There is one number written on each ...