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不分割).
##代码:
``` 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 = aa%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 (模拟)的更多相关文章
- BZOJ 4421: [Cerc2015] Digit Division
4421: [Cerc2015] Digit Division Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 348 Solved: 202[Subm ...
- BZOJ 4421: [Cerc2015] Digit Division 排列组合
4421: [Cerc2015] Digit Division 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4421 Descripti ...
- 【BZOJ4421】[Cerc2015] Digit Division 动态规划
[BZOJ4421][Cerc2015] Digit Division Description 给出一个数字串,现将其分成一个或多个子串,要求分出来的每个子串能Mod M等于0. 将方案数(mod 1 ...
- Digit Division
Digit Division Time limit: 1 s Memory limit: 512 MiB We are given a sequence of n decimal digits. Th ...
- Digit Division(排列组合+思维)(Gym 101480D )
题目链接:Central Europe Regional Contest 2015 Zagreb, November 13-15, 2015 D.Digit Division(排列组合+思维) 题解: ...
- UVALive 7327【模拟】
题意: 每次方案一个或多个子序列: 每个子序列要整除m 认为分割不同,子序列边界的不同就是不同: 1246有4个 1246 12 46 124 6 12 4 6 思路: 先从整体考虑,因为取膜适用于加 ...
- UVALive - 6269 Digital Clock 模拟
UVALive - 6269 Digital Clock 题意:时钟坏了,给你一段连续的时间,问你现在可能的时间是多少. 思路:直接模拟,他妈的居然这场就跪在了这题,卧槽,他妈的就在111行,居然多打 ...
- 【Miscalculation UVALive - 6833 】【模拟】
题目分析 题目讲的是给你一个串,里面是加法.乘法混合运算(个人赛中误看成是加减乘除混合运算),有两种算法,一种是乘法优先运算,另一种是依次从左向右运算(不管它是否乘在前还是加在前). 个人赛中试着模拟 ...
- UVALive - 7139(差分+模拟)
题目链接 参考 题意 N*M的网格,一辆车沿着网格线按给定路线走,每个网格里有一个人,人的视线始终看着车,问这些人净转圈数的平方和. 分析 由于车的起点和终点都为左上角,且每个格子里的人永远面对着车, ...
随机推荐
- class_create()
#define class_create(owner, name) \({ \ ...
- chrome下float元素下input选中内容bug
今天在写一个小demo的时候,发现chrome下一个很奇怪的bug. 我的代码如下: <!DOCTYPE html> <html lang="en"> &l ...
- jetty ZipException: invalid entry size
The issue, as I suspected, was due a corrupt JAR file. The solution for me was to clear my local rep ...
- bzoj2829
裸题,直接上凸包,然后加上一个圆周即可 只是在这之前没写过旋转而已 const pi=3.14159265358979323; eps=1e-8; type point=record x,y:doub ...
- bzoj3252
简单题,每次取出最长链,然后对于练上每个点x,终点在其子树内的链都要减去a[x] 这显然可以用dfs序+线段树维护 显然每个点只要删一次即可,复杂度是O(nlogn) type node=record ...
- bzoj2431: [HAOI2009]逆序对数列
dp. f[i][j]表示放置第i个数有j个逆序对的方案数. s[i][j]维护前缀和(f[i][0]~f[i][j]). 状态转移方程 f[i][j]=s[i-1][j]-s[i-1][max(j- ...
- 无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败。
无法加载 DLL“rasapi32.dll”: 动态链接库(DLL)初始化例程失败. 在Asp.Net项目中使用WebClient或HttpWebRequest时出现以上错误 解决方案:把以下代码放在 ...
- 开发ffmpeg/live555常见问题错误及解决方法
#include <iostream>using namespace std;extern "C" {#include <libavcodec/avcodec.h ...
- DrawDib函数组的使用
Microsoft的针对与设备无关位图(DIB位图),在其WIN32 SDK的Multimedia中提供了一组绘制DIB位图的高性能函数组──DrawDib函数组.DrawDib函数组是一组不依赖于图 ...
- 从网页监听Android设备的返回键
最近搞Android项目的时候,遇到一个比较蛋疼的需求,需要从Client App调用系统浏览器打开一个页面,进行杂七杂八的一些交互之后,返回到App.如何打开浏览器和如何返回App这里就不说了,有兴 ...