https://www.hackerrank.com/contests/w28/challenges/lucky-number-eight

设dp[i][v]表示前i位数中,得到余数是v的子序列的数目。

那么产生新的状态就是,对于每一个上一次的余数v。新的余数数目就有,(v * 10 + str[i]) % 8

然后输出dp[n][0]即可。

#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 = 2e5 + 20;
int dp[8];
int sec[8];
char str[maxn];
const int MOD = 1e9 + 7;
void work() {
int n;
scanf("%d", &n);
scanf("%s", str + 1);
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= 7; ++j) sec[j] = dp[j];
for (int j = 0; j <= 7; ++j) {
sec[(j * 10 + str[i] - '0') % 8] += dp[j];
if (sec[(j * 10 + str[i] - '0') % 8] >= MOD)
sec[(j * 10 + str[i] - '0') % 8] -= MOD;
}
sec[(str[i] - '0') % 8]++;
for (int j = 0; j <= 7; ++j) dp[j] = sec[j];
}
int ans = dp[0];
// for (int i = 1; i <= 3; ++i) {
// ans += min(dp[i], dp[8 - i]);
// if (ans >= MOD) ans -= MOD;
// }
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return 0;
}

  

Lucky Number Eight dp的更多相关文章

  1. HDU 4323——Magic Number——————【dp求编辑距离】2012——MUT——3

    Magic Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. 枚举 + 进制转换 --- hdu 4937 Lucky Number

    Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  3. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  4. SCU3502 The Almost Lucky Number

    Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...

  5. HDOJ 4937 Lucky Number

    当进制转换后所剩下的为数较少时(2位.3位),相应的base都比較大.能够用数学的方法计算出来. 预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算. . .. Lucky Numb ...

  6. 题目1380:lucky number

    转载请注明文本链接 http://blog.csdn.net/yangnanhai93/article/details/40441709 题目链接地址:http://ac.jobdu.com/prob ...

  7. HDU 3346 Lucky Number

    水题 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> us ...

  8. 九度oj 题目1380:lucky number

    题目描述: 每个人有自己的lucky number,小A也一样.不过他的lucky number定义不一样.他认为一个序列中某些数出现的次数为n的话,都是他的lucky number.但是,现在这个序 ...

  9. 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]- Nearly Lucky Number(Codeforces Beta Round #84 (Div. 2 Only)A. Nearly)

    A. Nearly Lucky Number time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

随机推荐

  1. Python 点滴 I

    [为什么使用Python] 1. 软件质量:   Python更注重软件质量,一致性,可维护性 2. 开发效率:   相比C/C++/Java这些编译/静态语言,无需编译及链接步骤,Python所须要 ...

  2. 怎样免费设置QQ空间背景音乐

    怎样免费设置QQ空间背景音乐 1.打开QQ空间,点击 2. 3. 4.这里它要求我们输入歌曲的在线路径,并且必须是MP3格式的,这就简单了,我们仅仅要去网上找在线的MP3音乐就能够了.可是如今非常多提 ...

  3. Hibernate4之session核心方法

    在学习session的核心方法之前,我们先了解下hibernate中几种对象的状态: 暂时状态:这样的状态就好像咱们公司请的暂时员工一样,他在公司里没有相关的资料和id. 特点:在使用代理主键的情况下 ...

  4. ios开发--NSDate与NSDateFormatter的相关用法【转】

    原文地址:http://blog.sina.com.cn/s/blog_91ff71c0010188u9.html 1.NSDateFormatter配合NSDate与NSString之间的转化  N ...

  5. CASE UPDATE

    https://leetcode-cn.com/problems/swap-salary/description/ Given a table salary, such as the one belo ...

  6. high-level operations on files and collections of files

    11.10. shutil — High-level file operations — Python 3.6.5 documentation https://docs.python.org/3/li ...

  7. 磁盘扩容 磁盘阵列(Redundant Arrays of Independent Disks,RAID)

    磁盘阵列(Redundant Arrays of Independent Disks,RAID) 云 500G  不够用 扩容 方案1  重建分区,由500G到1T,按历史增速,1年后再扩到1.5T, ...

  8. NS3网络仿真(4): DataRate属性

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在first.py中创建了一个点到点的信道,且配置了两个属性: pointToPoint = ns ...

  9. ubuntu12.04安装tftp,配置,修改目录,错误类型

    [前言]学习嵌入式,需要配置tftp服务,在网上搜了搜,很多,但是配置了,我的老是出现Error code 1: File not found错误,经过探索和一个大哥的博客http://blog.cs ...

  10. UVA11183 Teen Girl Squad —— 最小树形图

    题目链接:https://vjudge.net/problem/UVA-11183 You are part of a group of n teenage girls armed with cell ...