zoukankan      html  css  js  c++  java
  • Codeforces Round #287 (Div. 2) D. The Maths Lecture [数位dp]

    D. The Maths Lecture
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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

    题解转自:http://blog.csdn.net/xu12110501127/article/details/43118157

    D,数位dp,当时读题的时候读错了。题意是n位的数字,如果存在他的后缀%k=0,就算一种,求出总数来再mod m 就是结果。dp[i][j][k],代表第i位余数为j时他是否已经存在后缀串整除了,0代表不存在,1代表存在。

    自己用dp[i][j]做了半天,一直wa,后来看了题解反应过来了,k标志位很关键,既让思路清晰,又避免了 0xxx但是 %k==0这种特殊情况的遗漏,dp水还是很深,要好好练啊~~

      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<map>
      9 #include<set>
     10 #include<stack>
     11 #include<string>
     12 
     13 #define N 105
     14 #define M 105
     15 #define mod 1000000007
     16 //#define p 10000007
     17 #define mod2 1000000000
     18 #define ll long long
     19 #define LL long long
     20 #define eps 1e-6
     21 #define inf 1000000
     22 #define maxi(a,b) (a)>(b)? (a) : (b)
     23 #define mini(a,b) (a)<(b)? (a) : (b)
     24 
     25 using namespace std;
     26 
     27 ll n,k,m;
     28 ll ans;
     29 ll cnt[N*10][N][5];
     30 
     31 void ini()
     32 {
     33     ans=0;
     34     memset(cnt,0,sizeof(cnt));
     35 }
     36 
     37 ll quickpow(ll x,ll nn)
     38 {
     39     ll re=1;
     40     while(nn)
     41     {
     42         if(nn&1){
     43             re=(re*x)%k;
     44         }
     45         nn/=2;
     46         x=(x*x)%k;
     47     }
     48     return re;
     49 }
     50 
     51 void solve()
     52 {
     53     ll i,j,o;
     54     ll te;
     55     cnt[0][0][0]=1;
     56     ll temp=1;
     57     ll st;
     58     ll s;
     59     for(i=1;i<=n;i++){
     60         for(j=0;j<k;j++){
     61             for(o=0;o<=9;o++){
     62                 te=(temp*o+j)%k;
     63                 for(st=0;st<2;st++){
     64                     s=st;
     65                     if(i==n && o==0) continue;
     66                     if(te==0 && o!=0){
     67                         s=1;
     68                     }
     69                     cnt[i][te][s]=(cnt[i][te][s]+cnt[i-1][j][st])%m;
     70                 }
     71             }
     72         }
     73         temp=(temp*10)%k;
     74     }
     75 }
     76 
     77 void out()
     78 {
     79     ll ans=0;
     80     ll i;
     81     for(i=0;i<k;i++){
     82         ans=(ans+cnt[n][i][1])%m;
     83     }
     84     printf("%I64d
    ",ans);
     85 }
     86 
     87 int main()
     88 {
     89     //freopen("data.in","r",stdin);
     90    // freopen("data.out","w",stdout);
     91     //scanf("%d",&T);
     92     //for(int ccnt=1;ccnt<=T;ccnt++)
     93     //while(T--)
     94     while(scanf("%I64d%I64d%I64d",&n,&k,&m)!=EOF)
     95     {
     96         ini();
     97         solve();
     98         out();
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    Java中String、StringBuffer和StringBuilder的区别
    Map集合学习
    Set集合学习
    List集合学习
    常用集合类简介及线程安全和非线程安全的集合对象
    JQuery入门
    原生Ajax使用
    JavaScript闭包
    Demo:servlet实现图片的上传
    Android Handler 内存泄漏问题
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4249384.html
Copyright © 2011-2022 走看看