Codeforces Round #287 (Div. 2) - D. The Maths Lecture (数位DP)

本文介绍了一个使用数位动态规划(数位DP)解决特定数学问题的方法。该问题要求找出有多少个n位正整数,使得这些数的后缀(包括自身)能被k整除,并给出答案对m取模的结果。通过构建状态转移方程,文章提供了一种高效的求解策略。

D. The Maths Lecture
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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

Input consists of three integers n, k, m (1 ≤ n ≤ 10001 ≤ k ≤ 1001 ≤ m ≤ 109).

Output

Print the required number modulo m.

Examples
input
Copy
1 2 1000
output
Copy
4
input
Copy
2 2 1000
output
Copy
45
input
Copy
5 3 1103
output
Copy
590
Note

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位数,他的后缀(包括自身)可以被k整除。答案对m取模


POINT:

数位DP,dp[x][y][z]记录 当前x位数对k取模等于y,z代表有没有后缀已经被k整除了。 即y==0。


#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
const LL maxn = 1234 + 10;
const int inf = 0x3f3f3f3f;

int shi[1234];

int dp[1111][111][2];
int n,k,m;

int dfs(int now,int mod,bool flag)
{
	if(dp[now][mod][flag]!=-1) return dp[now][mod][flag];
	if(now==n+1) return flag;
	int limit=now==n?1:0;
	int ans=0;
	for(int i=limit;i<=9;i++){
		ans+=dfs(now+1,(mod+shi[now-1]*i)%k,flag||(i>0&&(mod+shi[now-1]*i)%k==0));
		ans=ans%m;
	}
	return dp[now][mod][flag]=ans;
}

int main()
{
	memset(dp,-1,sizeof dp);

	scanf("%d%d%d",&n,&k,&m);
	shi[0]=1;
	for(int i=1;i<=1000;i++){
		shi[i]=shi[i-1]*10%k;
	}
	printf("%d\n",dfs(1,0,false)%m);


}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值