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. Office WORD如何简繁转换

    选中要转换的文字,工具-语言,中文简繁转换.

  2. kindle】扫描版PDF完美切割六寸

    kindle]扫描版PDF完美切割六寸  半夏 2013-11-05 18:36:01     软件来源记不清了..连使用说明的网址都找不到了,自己写一下使用方法大家凑合一下呗~    软件是大牛写的 ...

  3. 【Mongodb教程 第二课 】 MongoDB 创建数据库 use 命令

    use 命令 MongoDB use DATABASE_NAME 用于创建数据库.该命令将创建一个新的数据库,如果它不存在,否则将返回现有的数据库. 语法: use DATABASE 语句的基本语法如 ...

  4. 安卓版本和Api Level

    Platform Version API Level VERSION_CODE Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 ...

  5. 生成随机string

    转自:http://blog.csdn.net/yaodong_y/article/details/8115250 字母与数字的ASCII码 目 前计算机中用得最广泛的 字符集及其编码,是由美国国家标 ...

  6. Pthon的定时任务APScheduler的启动与关闭

    Pthon的定时任务APScheduler的启动与关闭 安装: sudo pip install apscheduler 使用: 直接运行Python文件即可,如 python XXX.py,XXX. ...

  7. 搭建集群后再namenode上查看进程发现除了三个基本进程还可能有别的进程,例如本来在子节点出现的进程出现在了主节点上

    搭建集群后再namenode上查看进程发现除了三个基本进程还可能有别的进程,例如本来在子节点出现的进程出现在了主节点上,namenode按正常来说是有三个进程的:namenode,resourcema ...

  8. 总结文件操作函数(二)-C语言

    格式化读写: #include <stdio.h> int printf(const char *format, ...);                   //相当于fprintf( ...

  9. 20170212-备份ABAP程序

    把生产机上所有后续开发的CBO程序都备份下来.以备急用! 用过2种方法:1.写BDC程序,模拟 TCODE:SE38 -->Program --> Utilities(M)-->Mo ...

  10. Lightoj 1014 - Ifter Party

    I have an Ifter party at the 5th day of Ramadan for the contestants. For this reason I have invited  ...