CodeForces 507D The Maths Lecture 构造恰好n位数且存在一个后缀能被k整除的方法数 dp

82 篇文章 0 订阅

题目链接:点击打开链接

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.

Sample test(s)
input
1 2 1000
output
4
input
2 2 1000
output
45
input
5 3 1103
output
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

求有多少个恰好为n位的整数,且这个整数的某个后缀能整除k,答案模m

思路:

因为是求后缀,所以从低位往高位构造。

dp[i][j][0] 表示长度为i位的整数,模k结果为j,且不存在某个后缀能整除k的个数

dp[i][j][1] 表示长度为i位的整数,模k结果为j,且存在某个后缀能整除k的个数

然后转移就在某个状态前加一个数字,判断一下加上后能否被k整除来决定加上数字后到达的状态

#include <cstdio>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define y1 Y1
typedef long long ll;
const int N = 1005;
ll ten[N], dp[N][100][2];
ll n, k, m;
void add(ll &x, ll y){
	x += y;
	if (x >= m)x -= m;
}
int main(){
	while (cin >> n >> k >> m){
		ten[0] = 1 % m;
		for (int i = 1; i < N; i++)ten[i] = (ten[i - 1] * 10LL) % k;
		memset(dp, 0, sizeof dp);
		dp[0][0][0] = 1;
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < k; j++){
				for (int z = 0; z < 10; z++)
				{
					if (i == n - 1 && z == 0)continue;
					ll now = (z*ten[i] + j) % k;
					if (now == 0 && z){
						add(dp[i + 1][now][1], dp[i][j][0]);
					}
					else
						add(dp[i + 1][now][0], dp[i][j][0]);
					add(dp[i + 1][now][1], dp[i][j][1]);
				}
			}
		}
		ll ans = 0;
		for (int i = 0; i < k; i++)
			ans += dp[n][i][1];
		cout << ans%m << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值