码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #287 Div2 D(The Maths Lecture)

时间:2015-03-06 19:16:53      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:数位dp

Problem

长度为N的数X(十进制),如果X的某一段后缀Y(十进制)可被k整除,则可被统计。问有多少这样的X?(mod m)(不可含前导0)

Limits

TimeLimit(ms):1000

MemoryLimit(MB):256

N:[1,1000]

k[1,100]

m[1,109]

Look up Original Problem From here

Solution

数位dp。设dp[i][j]表示数长度为 i 且数mod kj时, 有多少个。dp[i][0]不要往前转移,其余的往前转移,同时在加上后缀全为0的情况往前转移然后,每次用ans+=9?10i?2?dp[i][0]统计答案。

Complexity

TimeComplexity:O(N?k?10)

MemoryComplexity:O(N?k)

My Code

//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uin;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-9
#define MOD 1000000007
#define MAXN
#define N 1005
#define M 105
int n,k,m;
ll dp[N][M],ten,ans,tenpower[N];
void init(){
    ten=10LL;
    tenpower[0]=1LL;
    rep(i,1,N){
        tenpower[i]=(tenpower[i-1]*ten)%m;
    }
}
int main(){
    scanf("%d %d %d",&n,&k,&m);
    init();
    rep(j,1,10){
        dp[n][j%k]=(dp[n][j%k]+1LL)%m;
    }
    ten=1LL;
    ans=0;
    depin(i,n,1){
        //calulate
        if(i==1){
            ans=(ans+dp[i][0])%m;
            break;
        }
        else ans=(ans+((dp[i][0]*9LL%m)*tenpower[i-2])%m)%m;
        //dp
        ten=(ten*10LL)%k;
        rep(j,1,k){
            if(dp[i][j]==0) continue;
            rep(l,0,10){
                int h=(j+((ll)l*ten%k))%k;
                dp[i-1][h]=(dp[i-1][h]+dp[i][j])%m;
            }
        }
        rep(l,1,10){
            int h=(0+((ll)l*ten%k))%k;
            dp[i-1][h]=(dp[i-1][h]+1LL)%m;
        }
    }
    printf("%lld\n",ans);
}

Codeforces Round #287 Div2 D(The Maths Lecture)

标签:数位dp

原文地址:http://blog.csdn.net/uestc_peterpan/article/details/44102111

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!