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

D. The Maths Lecture
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ 1000, 1 ≤ k ≤ 100, 1 ≤ 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整除的个数。
用dp做,dp[i][j][0]表示i位数后缀可以被k整除且模j为的个数
dp[i][j][1]表示i位数后缀不可以被k整除且模j为的个数
两者都不包括0 00 000这样的数,但都包括01 001 ,这样的数,也就是说不包括0但包括n位内所有的数!
状态转移方程
dp[i - 1][j][0] 加上任意的0 - 9可推出所有dp[i][t][0](t = (j + s * 10 ^ (i-1)) % k;s取0 - 9)
dp[i - 1][j][0] 加上任意的0 - 9,如果能整除可推出所有dp[i][t][0],不能整除推出所有的dp[i][t][1]
此外,如果s * 10^ (i-1)能整除相应的dp[i][t][0] + 1,否则dp[i][t][1] + 1,也就是对应这样的数如 10 1000 10000 ,这样的数也要加入,因为dp[i][j]都是不包括0的。
最后答案就是所有的dp[n][j][0]之后减去dp[n-1][j][0]。也就是所有的n位以内减去所有的n-1位以内的数就是答案了!
总的复杂度O(10 * n* k)

#define N 1005
#define M 105
#define maxn 205
#define MOD 1000000007
int n,m,k;
ll dp[N][M][2],ten[N];
int main()
{
    while(S2(n,k)!=EOF)
    {
        S(m);
        memset(dp,0,sizeof(dp));
        ten[0] = 1;
        FI(n+1){
            ten[i+1]= ten[i] * 10 % k;
        }
        for(int i=1;i<=9;i++){
            int t = i % k;
            if(t == 0) dp[1][t][0] = (dp[1][t][0] + 1) % m;
            else dp[1][t][1] = (dp[1][t][1] + 1) % m;
        }
        for(int i = 2;i<=n;i++){
                for(int s = 0;s<10;s++){
                    for(int j =0;j<k;j++){
                        int t = (j + s * ten[i-1]) % k;
                        dp[i][t][0] = (dp[i][t][0] + dp[i - 1][j][0])%m;
                        if(t == 0) dp[i][t][0] = (dp[i][t][0] + dp[i - 1][j][1])%m;
                        else dp[i][t][1] = (dp[i][t][1] + dp[i - 1][j][1])%m;
                    }
                    if(s){
                        int t = (s * ten[i-1]) % k;
                        if(t == 0) dp[i][t][0] = (dp[i][t][0] + 1) % m;
                        else  dp[i][t][1] = (dp[i][t][1] + 1) % m;
                    }
                }
        }
        ll ans = 0;
        for(int i = 0;i<k;i++) ans = (ans + dp[n][i][0]) % m;
        {
            for(int j = 0;j<k;j++)
                ans = (ans - dp[n-1][j][0] + m )% m;
        }
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(Codeforces Round #287 (Div. 2) D. The Maths Lecture 数位dp)