Codefores 507D The Maths Lecture( 数位DP )
1 second
256 megabytes
standard input
standard output
Amr doesn't like Maths as he finds it really boring, so he usually sleeps in Maths lectures. But one day the teacher suspected that Amr is sleeping and asked him a question to make sure he wasn't.
First he gave Amr two positive integers n and k. Then he asked Amr, how many integer numbers x > 0 exist such that:
- Decimal representation of x (without leading zeroes) consists of exactly n digits;
- There exists some integer y > 0 such that:
;- decimal representation of y is a suffix of decimal representation of x.
As the answer to this question may be pretty huge the teacher asked Amr to output only its remainder modulo a number m.
Can you help Amr escape this embarrassing situation?
Input consists of three integers n, k, m (1 ≤ n ≤ 1000, 1 ≤ k ≤ 100, 1 ≤ m ≤ 109).
Print the required number modulo m.
1 2 1000
4
2 2 1000
45
5 3 1103
590
A suffix of a string S is a non-empty string that can be obtained by removing some number (possibly, zero) of first characters from S.
题意是统计n位数x , 它有一个后缀y,能够满足( y%k==0)的 x 的个数.我做法是开一个3维数组 , dp[i][j][y] 。 表示符合前 i 位 , 余数是j, 是否有前导 0 的数的个数。
首先要预处理了 i*j^10 % k 的结果,( i = 1~9 , j = 1~n )用于对新的状态的转移。
预处理好排列数 10^j % m ,当出现后缀y符合条件且没前导0 ,可进行计算。
剩下就是状态转移了 。
转移的过程中 无前导0 且 余数等于0 的可以进行一次计数。
否则就继续进行转移就OK 。
代码写得比较恶心...然后要注意处理好边界就没什么问题了。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ;
const int M = ;
LL dp[N][M][] , n , m , k , cnt[N] , rest[][N]; void init() {
cnt[] = ;
for( int i = ; i <= n ; ++i ) {
cnt[i] = cnt[i-] * % m ;
}
for( int j = ; j <= n ; ++j ) {
for( int i = ; i < ; ++i ) {
if( j == ) rest[i][j] = i % k ;
else rest[i][j] = ( % k * rest[i][j-] ) % k ;
// cout << i << ' ' << j << ' ' << rest[i][j] << endl ;
}
}
memset( dp , , sizeof dp );
for( int i = ; i < ; ++i ) dp[][i%k][!i]++;
// cout << "Run" << endl ;
} void Run() {
LL ans = ;
if( n == ) {
for( int i = ; i < ; ++i ) if( i % k == ) ans ++ , ans %= m;
}
else {
for( int i = ; i <= n ; ++i ) {
for( int j = ; j < k ; ++j ) {
for( int y = ; y < ; ++y ) {
if( !y && !j ) { // no leading zero and reminder equal zero
if( i < n ) ans = ( ans + * cnt[n-i-] % m * dp[i][j][y] ) % m ;
else ans += dp[i][j][y] , ans %= m ;
}
else { // leading zero or reminder not equal zero
for( int z = ; z < ; ++z ) {
int i1 = i + , j1 = ( rest[z][i1] + j ) % k ;
dp[i1][j1][!z] += dp[i][j][y] , dp[i1][j1][!z] %= m ;
}
}
}
}
}
}
cout << ans << endl ;
}
int main()
{
// freopen("in.txt","r",stdin);
while( cin >> n >> k >> m )
init() , Run();
}
Codefores 507D The Maths Lecture( 数位DP )的更多相关文章
- Codeforces Round #287 (Div. 2) D. The Maths Lecture [数位dp]
传送门 D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CF431D Random Task 二分+数位dp
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- bzoj1026数位dp
基础的数位dp 但是ce了一发,(abs难道不是cmath里的吗?改成bits/stdc++.h就过了) #include <bits/stdc++.h> using namespace ...
- uva12063数位dp
辣鸡军训毁我青春!!! 因为在军训,导致很长时间都只能看书yy题目,而不能溜到机房鏼题 于是在猫大的帮助下我发现这道习题是数位dp 然后想起之前讲dp的时候一直在补作业所以没怎么写,然后就试了试 果然 ...
- HDU2089 不要62[数位DP]
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 数位DP GYM 100827 E Hill Number
题目链接 题意:判断小于n的数字中,数位从高到低成上升再下降的趋势的数字的个数 分析:简单的数位DP,保存前一位的数字,注意临界点的处理,都是套路. #include <bits/stdc++. ...
- 数位dp总结
由简单到稍微难点. 从网上搜了10到数位dp的题目,有几道还是很难想到的,前几道基本都是模板题,供入门用. 点开即可看题解. hdu3555 Bomb hdu3652 B-number hdu2089 ...
- 数位DP入门
HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...
随机推荐
- xcode 5.0 连接svn error -(NSURLErrorDomain error -1012)
xcode 5.0连接 svn server, check out时出现如下error : The operation couldn’t be completed. (NSURLErrorDomain ...
- 20180329-layoutSubviews的调用机制
如果你想强制更新布局,不要直接调用此方法,你可以调用setNeedsLayout方法,如果你想立即显示你的views,你需要调用layoutIfNeed方法 layoutSubviews作用: lay ...
- 基于GPU的图像处理平台
基于GPU的图像处理平台 1. (309)英伟达推Jetson TX1 GPU模块力推人工智能 1.1 产品概述 Jetson TX1 GPU模块,主要针对近年来蓬勃发展的人工智能市场,包括无人机. ...
- poj2689 Prime Distance(素数区间筛法)
题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...
- python模块之numpy,pandas基本用法
numpy: 是 Python 的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库简单来说:就是支持一维数组和多维数组的创建和操作,并有丰富的函数库. 直接看例子 ...
- 人生苦短_我用Python_javascript_var_function_简单笔记_001
<!--Javascript_var_001:--> <html> <head> <meta charset="UTF-8"> &l ...
- ImageField 字段的使用
Django模型中的ImageField和FileField的upload_to选项是必填项,其存储路径是相对于MEIDA_ROOT而来的.
- HTML5浏览器
你可以学会如何使用旧的浏览器正确处理新的HTML5. HTML5 浏览器支持 HTML5 支持所有现代浏览器. 此外,所有的浏览器,旧的和新的,自动处理未被识别的元素作为内联元素. 因为这样,你可以& ...
- 02 spring security 自定义用户认证流程
1. 自定义登录页面 (1)首先在static目录下面创建login.html 注意: springboot项目默认可以访问resources/resources, resources/s ...
- php fmod()函数 语法
php fmod()函数 语法 作用:fmod()函数的作用是两个数值做除法运算后的余数 语法:fmod(X,Y).大理石平台哪家好 参数: 参数 描述 X 必须,X为除数 Y 必须,被除数,如果Y为 ...