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. HDU 5285 wyh2000 and pupil(dfs或种类并查集)

    wyh2000 and pupil Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Other ...

  2. Android - 隐藏EditText弹出的软键盘输入(SoftInput)

    隐藏EditText弹出的软键盘输入(SoftInput) 本文地址: http://blog.csdn.net/caroline_wendy 保持界面的整洁, 能够选择在进入界面时, 隐藏EditT ...

  3. 解决运行scrapy是报错No module named cryptography,解决cryptography的安装问题,解决libffi的安装问题

    在linux下配置完运行是出现ImportError: No module named cryptography.hazmat.bindings.openssl.binding的错误.原因是crapt ...

  4. jquery全选,取消全选

    近期项目又用到了这个全选和取消全选的操作. 曾经总是自己写纯JS.如今既然知道怎么写了.那怎样用JQ写得更简洁呢.这样也能学到新的东西.假设乎百度一下果然发现了好东东.感谢OSC的iuhoay. 代码 ...

  5. linux【第六篇】用户和用户管理及定时任务复习

    定时任务复习 1.什么是定时任务? 2.如何编辑查看定时任务(配置文件位置?),语法的特殊字符意义是什么?- * , / 3.书写定时任务有哪些要领? 4.生产如何调试定时任务 5.生产场景配置定时任 ...

  6. 查看和改动MySQL数据库表存储引擎

            要做一名合格的程序猿,除了把代码写的美丽外,熟知数据库方面的知识也是不可或缺的.以下总结一下怎样查看和改动MySQL数据库表存储引擎:        1.查看数据库所能支持的存储引擎: ...

  7. YTU 2437: C++ 习题 比较大小-类模板

    2437: C++ 习题 比较大小-类模板 时间限制: 1 Sec  内存限制: 128 MB 提交: 1144  解决: 805 题目描述 声明一个类模板,利用它分别实现两个整数.浮点数和字符的比较 ...

  8. YTU 2920: Shape系列-7

    2921: Shape系列-7 时间限制: 1 Sec  内存限制: 128 MB 提交: 156  解决: 129 题目描述 小强做的Shape类在本次的测试中出了点状况,发现原来是其中的area函 ...

  9. spring各个jar作用

    spring.jar --->包含完整发布模块的单个jar,但是不包括mock.jar,aspects.jar,spring-porltet.jar,spring-hibernate2.jar ...

  10. I.MX6 PLL5 clock hakcing

    /************************************************************************** * I.MX6 PLL5 clock hakci ...