Lucky Number Eight dp

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;
}

  

转载于:https://www.cnblogs.com/liuweimingcprogram/p/6337431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值