The Maths Lecture - CodeForces 507 D dp

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

题意:有多少个n位数,它的后缀可以被k整除。

思路:dp[i][j]表示i位数中可以被k整除为j的数有多少(已经去除掉之其中后缀能被k整除的情况)。因为如果i-1位能被k整除的数前面不管加什么都可以。每次转移的时候只需要添加前面的数字即可。注意要特殊处理后面全是0的情况。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll dp[1010][110],MOD,num[1010],ans,pow[1010];
int n,m;
int main()
{
    int i,j,k;
    scanf("%d%d%I64d",&n,&m,&MOD);
    for(i=1;i<=9;i++)
       dp[1][i%m]++;
    num[0]=1;num[1]=9%MOD;pow[1]=10%m;
    for(i=2;i<=n;i++)
       num[i]=num[i-1]*10%MOD;
    for(i=2;i<=n;i++)
       pow[i]=pow[i-1]*10%m;
    for(i=1;i<=n;i++)
    {
        ans=(ans+dp[i][0]*num[n-i]%MOD)%MOD;
        for(j=1;j<m;j++)
          for(k=0;k<=9;k++)
             dp[i+1][(j+k*pow[i]%m)%m]=(dp[i+1][(j+k*pow[i]%m)%m]+dp[i][j])%MOD;
        for(k=1;k<=9;k++)
           dp[i+1][k*pow[i]%m]++;
    }
    printf("%I64d\n",ans);
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值