UVALive 7327 Digit Division (模拟)

Digit Division

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127407#problem/D

Description


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


题意:


求有多少种方式把一个数串划分成多段,使得每段组成的数字能被m整除.


题解:


先从整体考虑,如果能被划分成满足条件的若干段,那么整个原串组成的数字必定满足条件.
(若A,B都能被m整除,则AB=A*100...+B一定能被m整除)
再考虑把原串划分成两段,当且仅当某个前缀组成的数能被m整除时,原串才能被分成两段.
划成多段同理.
那么结果就是,求有多少个前缀能恰好被m整除.
若有m个(不包括末尾),结果就是 2^m. (相当于枚举每个位置分割or不分割).


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 1000000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL n,m;

LL quickmod(LL a,LL b,LL m)
{
    LL ans = 1;
    while(b){
        if(b&1){
            ans = (ans*a)%m;
            b--;
        }
        b/=2;
        a = a*a%m;
    }
    return ans;
}

int main(int argc, char const *argv[])
{
    //IN;

    while(scanf("%lld %lld", &n,&m) != EOF)
    {
        char tmp[80]; gets(tmp);
        LL cnt = 0;
        LL cur = 0;
        for(int i=1; i<=n; i++) {
            char c = getchar(); c -= '0';
            cur = (cur*10LL + c) % m;
            if(cur == 0LL) cnt++;
        }

        if(cur) {
            printf("0\n");
            continue;
        }

        LL ans = quickmod(2LL, cnt-1LL, mod);

        printf("%lld\n", ans);
        //cout << ans << endl;
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5758003.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值