UVALive 7327 Digit Division

17 篇文章 0 订阅
6 篇文章 0 订阅
We are given a sequence of n decimal digits. The sequence needs to be partitioned into one or more
contiguous subsequences such that each subsequence, when interpreted as a decimal number, is divisible
by a given integer m.
Find the number of different such partitions modulo 109+7. When determining if two partitions are
different, we only consider the locations of subsequence boundaries rather than the digits themselves,
e.g. partitions 2|22 and 22|2 are considered different.
Input
The input file contains several test cases, each of them as described below.
The first line contains two integers n and m (1 ≤ n ≤ 300000, 1 ≤ m ≤ 1000000) — the length
of the sequence and the divisor respectively. The second line contains a string consisting of exactly n
digits.
Output
For each test case, output a single integer — the number of different partitions modulo 109 + 7 on a
line by itself.
Sample Input
4 2
1246
4 7
2015
Sample Output
4

0

就是计算(a[i-1]*10+a[i])%m是否为零,若为零,则说明可以在这里分则cnt++,需要特判一下最后一位能否整除m若不能,直接输出0;最后总和为2^(cnt-1)

#include<stdio.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
#define F(x,a,b) for (int x=a;x<=b;x++)
#define MOD 1000000007
char a[3000005];
int pow(int x) {int s=1;F(i,1,x) s=(s*2)%MOD;return s;}
int main()
{
    int n,m;
    while (cin>>n>>m)
    {
        cin>>a;
        int k=0;
        int ans=0;
        int flag=1;
        F(i,0,n-1){if (((k*10+a[i]-'0')%m)==0) {ans++;}else{if (i==n-1) {cout<<"0"<<endl;flag=0;break;}} k=(k*10+a[i]-'0')%m;}
        if (flag) cout<<pow(ans-1)<<endl;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值