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 ...
随机推荐
- Asp.Netcore使用Filter来实现接口的全局异常拦截,以及前置拦截和后置拦截
原文链接:https://blog.csdn.net/qq_38762313/article/details/85234594 全局异常拦截器: 解决写每个接口都需要去做容错而添加try{ ...
- differential evolution代码实例(DE算法)
DE算法是遗传算法中一种比较流行的算法,这种算法比较简单,速度也比较快,下面给出一份示例代码 clear all; close all; clc 2 %Function to be minimized ...
- vue 防抖节流函数——组件封装
防抖(debounce) 所谓防抖,就是指触发事件后在 n 秒内函数只能执行一次,如果在 n 秒内又触发了事件,则会重新计算函数执行时间. 节流(throttle) 所谓节流,就是指连续触发事件但是在 ...
- turtle库使用
turtle库的使用 绘图窗体布局 turtle.setup(width,height,startx,straty) 用来控制窗体的大小与位置,其中后width与height用来控制窗体的大小,sta ...
- Java-java.lang.NoClassDefFoundError:brave.Span.Kind
今天在升级某个框架时,遇到如标题描述的问题.这个问题应该说还是比较明显的,首先去搜了一下NoClassDefFoundError的问题,参考这篇博客:https://www.cnblogs.com/x ...
- 分布式理论 BASE、CAP、ACID
CAP原理: 在理论计算机科学中,CAP定理(CAP theorem),又被称作布鲁尔定理(Brewer's theorem),它指出对于一个分布式计算系统来说,不可能同时满足以下三点: 一致性(Co ...
- boost Filesystem
The library Boost.Filesystem makes it easy to work with files and directories. Paths Paths can be bu ...
- 每天一个linux命令:less(14)
less less命令的作用与more十分相似,都可以用来浏览文字档案的内容,less 在查看之前不会加载整个文件 .用less命令显示文件时,用PageUp键向上翻页,用PageDown键向下翻页. ...
- 如何在MaxCompute上处理存储在OSS上的开源格式数据
0. 前言 MaxCompute作为使用最广泛的大数据平台,内部存储的数据以EB量级计算.巨大的数据存储量以及大规模计算下高性能数据读写的需求,对于MaxCompute提出了各种高要求及挑战.处在大数 ...
- 为你的AliOS Things应用增加自定义cli命令
摘要: 怎么才能在RTOS系统中,通过 串口shell控制LED的开关. 在日常嵌入式开发中,我们经常会用串口命令来使设备进入某种特定的状态,或执行某个特定的操作.如系统自检,模拟运行,或者进入手动模 ...