The Maths Lecture
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status
Appoint description:
System Crawler (2015-01-24)
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?≤?1000, 1?≤?k?≤?100, 1?≤?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; }