Poj 2282 digital dp

Source: Internet
Author: User

Digit dp, dp [I] [j] indicates the total number of j numbers ranging from 1 to I, and then counts by bit.

 

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int dp[10][10];int pow(int a,int b){    int ret=1;    for(int i=1;i<=b;i++)    ret*=a;    return ret;}long long solve(int a,int tmp){    int s[11],lon=0;    while(a)    {        s[++lon]=a%10;        a/=10;    }    s[0]=0;    long long ans=0;    int cnt=0;    for(int i=lon;i>=1;i--)    {        if(tmp==0&&i==lon)        {            ans+=dp[i-1][tmp]*(s[i]-1);            int ff=1*pow(10,i-1)-1;            ans+=solve(ff,0);        }        else        ans+=dp[i-1][tmp]*s[i];        ans+=cnt*pow(10,i-1)*s[i];        if(tmp)        {            if(s[i]>tmp) ans+=pow(10,i-1);            if(s[i]==tmp) cnt++;        }        else if(i!=lon)        {            if(s[i]>tmp) ans+=pow(10,i-1);            if(s[i]==tmp) cnt++;        }    }    return ans+cnt;}int main(){    memset(dp,0,sizeof(dp));    for(int i=1;i<=9;i++)    {        for(int j=0;j<=9;j++)        dp[i][j]=dp[i-1][j]*10+pow(10,i-1);    }    int a,b;    while(scanf("%d %d",&a,&b),a||b)    {        if(a>b) swap(a,b);        for(int i=0;i<=9;i++)        {            printf("%lld ",solve(b,i)-solve(a-1,i));        }        printf("\n");    }    return 0;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.