codeforces--507D--The Maths Lecture(數位dp),codeforces
The Maths LectureTime Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64uSubmit StatusAppoint 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 __int64LL 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;}