HUST 1599 - Multiple(动态规划)

1599 - Multiple

时间限制:2秒 内存限制:64兆
461 次提交 111 次通过
题目描述
Rocket323 loves math very much. One day, Rocket323 got a number string. He could choose some consecutive digits from the string to form a number. Rocket323 loves 64 very much, so he wanted to know how many ways can he choose from the string so the number he got multiples of 64 ?

A number cannot have leading zeros. For example, 0, 64, 6464, 128 are numbers which multiple of 64 , but 12, 064, 00, 1234 are not.
输入
Multiple cases, in each test cases there is only one line consist a number string.
Length of the string is less than 3 * 10^5 .

Huge Input , scanf is recommended.
输出
Print the number of ways Rocket323 can choose some consecutive digits to form a number which multiples of 64.
样例输入
64064
样例输出
5
提示
There are five substrings which multiples of 64.
[64]064
640[64]
64[0]64
[64064]
[640]64
来源
Problem Setter : Yang Xiao

思路:
根据同余定理,每次枚举到一个数,都把前面的存在的余数乘以10加上这个数再对64取余,就产生新的余数,最后统计余数是0的个数有多少

#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
char a[300005];
long long int dp[2][65];
long long int tag[65];
long long int ans;
long long int res;
int now;
int main()
{
    while(scanf("%s",a)!=EOF)
    {
        ans=0;res=0;
        int len=strlen(a);
        memset(dp,0,sizeof(dp));
        now=0;
        for(int i=0;i<len;i++)
        {
            for(int j=63;j>=0;j--)
                dp[now^1][j]=0;
            for(int j=63;j>=0;j--)
            {
                int num=(j*10+a[i]-'0'+64)%64;
                dp[now^1][num]+=dp[now][j];

            }
            if(a[i]!='0')
                dp[now^1][a[i]-'0']++;
            else
                ans++;
            res+=dp[now^1][0];
            now^=1;

        }
        printf("%lld\n",res+ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值