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

【BZOJ4421】[Cerc2015] Digit Division

Description

给出一个数字串,现将其分成一个或多个子串,要求分出来的每个子串能Mod M等于0.

将方案数(mod 10^9+7)

Input

给出N,M,其中1<=N<=300 000,1<=M<=1000 000.

接下来一行,一个数字串,长度为N。

Output

如题

Sample Input

4 2
1246

Sample Output

4

题解:如果一个前缀a%m==0,另一个长一点的前缀b%m==0,那么他们的中间部分(b-a*10^i)%m一定也是0,那就相当于我们每搜到一个前缀为0的位置就可以把这个串从这里切开,那么如果原串有n个前缀%m==0,显然除了最后一个必须切以外,每个前缀我们可以选择切或不切,答案就是2^n

不要比谁的代码长度短了!!!

#include <cstdio>
int n,m,sum,ans,j;
char str[300010];
int main()
{
	scanf("%d%d%s",&n,&m,str),ans=1;
	while(str[j])	sum=(sum*10+str[j]-‘0‘)%m,ans=(!sum&&str[j+1])?(ans*2%1000000007):ans,j++;
	printf("%d",(!sum)?ans:0);
	return 0;
}
时间: 2024-08-07 03:03:27

【BZOJ4421】[Cerc2015] Digit Division 动态规划的相关文章

BZOJ4421 : [Cerc2015] Digit Division

如果两个相邻的串可行,那么它们合并后一定可行,所以求出所有可行的串的个数$t$,则$ans=2^{t-1}$. 注意特判整个串不可行的情况,这个时候答案为0. #include<cstdio> int n,m,i,t,ans;char a[300010]; int main(){ for(scanf("%d%d%s",&n,&m,a);i<n;i++){ t=(t*10+a[i]-'0')%m; if(!t)if(!ans)ans=1;else ans

bzoj 4421: [Cerc2015] Digit Division

4421: [Cerc2015] Digit Division Description 给出一个数字串,现将其分成一个或多个子串,要求分出来的每个子串能Mod M等于0. 将方案数(mod 10^9+7) Input 给出N,M,其中1<=N<=300 000,1<=M<=1000 000. 接下来一行,一个数字串,长度为N. Output 如题 Sample Input 4 2 1246 Sample Output 4 —————以下题解————— 首先,分出来的第一段必须%m=

[CERC2015]Digit Division

题目描述 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 di

BZOJ 4421 Digit Division

随便dp一下就好了... #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 300050 #define maxm 1000050 #define mod 1000000007 using namespace std; long long n,m,t[maxn],sum[maxm],dp[maxn]; char s[maxn]; int

【hihoCoder】1033: 交错和

初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[0, l)满足条件的个数 2. 求解 区间[0, n]满足条件的个数 性质:一个小于n的数m,一定是从高位到低位在某一位小于n的对应位的一个数   数值 百位 十位 个位   n 358 3 5 8   m 350 3 5 0 0<8 m 349 3 4 9 4<5 遍历所有小于n的数:从高位到低

2015-2016 ACM-ICPC, Central Europe Regional Contest (CERC 15)

地址 Rank Solved A B C D E F G H I J K L 62/217 5/12 O O . O . O . . . . O . O: 当场通过 ?: 赛后通过 .: 尚未通过 A ASCII Addition solved by chelly chelly's solution B Book Borders solved by chelly chelly's solution C Cow Confinement unsolved D Digit Division solve

gym101480

A. ASCII Addition 模拟 #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring

【HDU6037】Expectation Division(动态规划,搜索)

[HDU6037]Expectation Division(动态规划,搜索) 题面 Vjudge 你有一个数\(n\),\(n\le 10^{24}\),为了方便会告诉你\(n\)分解之后有\(m\)个不同的质因子,并且把这些质因子给你. 你每次可以把\(n\)变成一个它的约数,求变成\(1\)的期望步数. 题解 首先暴力的转移是: \[f[n]=1+\frac{1}{\sigma(n)}\sum_{d|n}f[d]\] 不难发现这个状态之和每个质因子的出现次数的集合相关,与质因子是什么无关.

BNUOJ 34985 Elegant String 2014北京邀请赛E题 动态规划 矩阵快速幂

Elegant String Time Limit: 1000msMemory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main We define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1,-, k