codeforces--507D--The Maths Lecture(数位dp)

The Maths Lecture
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Appoint description:  

Description

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 Input

Input
1 2 1000
Output
4
Input
2 2 1000
Output
45
Input
5 3 1103
Output
590

Hint

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位数(不含前导0)存在后缀是k的倍数(0不算),并将总数对m取余。

dp[i][j][0]代表i位数时,对k取余为j的,且后缀没有k'的倍数的个数。

dp[i][j][1]代表i位数时,对k取余为j的,且后缀存在k的倍数的个数。

因为没有前导0,所以当i为n的时候,前面不能加0

如果前面加了一个数x得到的余数为j,那么当j==0&&x!= 0 的时候可以归到dp[i][x][1]中,否则为dp[i][x][0]

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
LL dp[1100][110][2] ;
LL Mod(LL a,LL k,LL b,LL m)
{
    int i , j ;
    LL temp ;
    a = a % m ;
    if( k > 9 )
    {
        i = 0 ;
        k -= 9 ;
        while( i < k-9 )
        {
            a = a*1000000000 % m ;
            i += 9 ;
        }
        for( ; i < k ; i++)
            a = a*10 % m ;
        a = (a*(1000000000)+b) % m ;
    }
    else
    {
        for(i = 0 ; i < k ; i++)
            a *= 10 ;
        a = ( a+b ) % m ;

    }
    return a ;
}
void init(int n,int m,int mod)
{
    int i , j , k ;
    LL x ;
    memset(dp,0,sizeof(dp)) ;
    for(i = 0 ; i < 10 ; i++)
    {
        if( i%m == 0 && i != 0 )
            dp[1][0][1] = (dp[1][0][1]+1) % mod ;
        else
            dp[1][i%m][0] = (dp[1][i%m][0]+1) % mod ;
    }
    for(i = 2 ; i <= n ; i++)
    {
        for(j = 0 ; j < 10 ; j++)
        {
            for(k = 0 ; k < m ; k++)
            {
                if( i == n && j == 0 ) continue ;
                x = Mod(j,i-1,k,m) ;
                if( x == 0 && j != 0 )
                    dp[i][x][1] = ( dp[i][x][1] + dp[i-1][k][0] ) % mod ;
                else
                    dp[i][x][0] = ( dp[i][x][0] + dp[i-1][k][0] ) % mod ;
                dp[i][x][1] = (dp[i][x][1] + dp[i-1][k][1] ) % mod ;
            }
        }
    }
    return ;
}
int main()
{
    LL n , m , mod , ans ;
    while( scanf("%I64d %I64d %I64d", &n, &m, &mod) != EOF )
    {
        init(n,m,mod) ;
        ans = 0 ;
        int i ;
        for(i = 0 ; i < m ; i++)
            ans = ( ans + dp[n][i][1] ) % mod ;
        printf("%I64d\n", ans) ;
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值