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不分割).


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 = (ansa)%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;

}

UVALive 7327 Digit Division (模拟)的更多相关文章

  1. BZOJ 4421: [Cerc2015] Digit Division

    4421: [Cerc2015] Digit Division Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 348  Solved: 202[Subm ...

  2. BZOJ 4421: [Cerc2015] Digit Division 排列组合

    4421: [Cerc2015] Digit Division 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4421 Descripti ...

  3. 【BZOJ4421】[Cerc2015] Digit Division 动态规划

    [BZOJ4421][Cerc2015] Digit Division Description 给出一个数字串,现将其分成一个或多个子串,要求分出来的每个子串能Mod M等于0. 将方案数(mod 1 ...

  4. Digit Division

    Digit Division Time limit: 1 s Memory limit: 512 MiB We are given a sequence of n decimal digits. Th ...

  5. Digit Division(排列组合+思维)(Gym 101480D )

    题目链接:Central Europe Regional Contest 2015 Zagreb, November 13-15, 2015 D.Digit Division(排列组合+思维) 题解: ...

  6. UVALive 7327【模拟】

    题意: 每次方案一个或多个子序列: 每个子序列要整除m 认为分割不同,子序列边界的不同就是不同: 1246有4个 1246 12 46 124 6 12 4 6 思路: 先从整体考虑,因为取膜适用于加 ...

  7. UVALive - 6269 Digital Clock 模拟

    UVALive - 6269 Digital Clock 题意:时钟坏了,给你一段连续的时间,问你现在可能的时间是多少. 思路:直接模拟,他妈的居然这场就跪在了这题,卧槽,他妈的就在111行,居然多打 ...

  8. 【Miscalculation UVALive - 6833 】【模拟】

    题目分析 题目讲的是给你一个串,里面是加法.乘法混合运算(个人赛中误看成是加减乘除混合运算),有两种算法,一种是乘法优先运算,另一种是依次从左向右运算(不管它是否乘在前还是加在前). 个人赛中试着模拟 ...

  9. UVALive - 7139(差分+模拟)

    题目链接 参考 题意 N*M的网格,一辆车沿着网格线按给定路线走,每个网格里有一个人,人的视线始终看着车,问这些人净转圈数的平方和. 分析 由于车的起点和终点都为左上角,且每个格子里的人永远面对着车, ...

随机推荐

  1. AutoResetEvent

    private static readonly AutoResetEvent autoResetEvent = new AutoResetEvent(false); private static vo ...

  2. zlib代码生成

    1.主页下载zlib-1.2.8的source code的压缩包:F:\Develop Tools\zlib-1.2.8 2.下载安装cmake-2.8.1-win32-x86 3.用cmake生成z ...

  3. Save output to a text file from Mac terminal

      Simply with output redirection: system_profiler > file.txt Basically, this will take the output ...

  4. 函数 page_dir_get_n_heap

    查看某page中含有的记录个数 #define PAGE_N_HEAP 4 /* number of records in the heap, bit =flag: new-style compact ...

  5. HDU 1269 迷宫城堡 (强连通分量,常规)

    题意: 判断所给的有向图是否是一个强连通图. 思路: 如果连通分量大于1则必定No,如果强连通分量大于1也是No.tarjan算法求强连通分量. #include <cstdio> #in ...

  6. <四>面向对象分析之UML核心元素之用例

    一:基本概念        --->用例定义了一组用例实例,其中每个实例都是系统所执行一系列操作,这些操作生成特定主角可以观测的值.        --->所谓用例,就是一件事情,要完成这 ...

  7. Asp.Net MVC4 系列-- 进阶篇之路由(1)【转】

    http://blog.csdn.net/lan_liang/article/details/22993839?utm_source=tuicool

  8. mysql 外连接总结

    内连接: 只连接匹配的行左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边 ...

  9. Linux如何统计进程的CPU利用率

    1.0 概述 在Linux的/proc文件系统,可以看到自启动时候开始,所有CPU消耗的时间片:对于个进程,也可以看到进程消耗的时间片.这是一个累计值,可以"非阻塞"的输出.获得一 ...

  10. information_schema中的三个关于锁的表

    在5.5中,information_schema 库中增加了三个关于锁的表(MEMORY引擎):innodb_trx         ## 当前运行的所有事务innodb_locks       ## ...