poj 3286-数位DP

How many 0‘s?

数位DP,统计两个数之间0的个数--

代码:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring> 
#include<cmath>
#include<queue>
using namespace std;
#define sf scanf
#define pf printf
#define INF 1<<29
#define lint __int64
#define clr(x) memset(x,0,sizeof(x))
#define Clr(x) memset(x,-1,sizeof(x))
//数位DP
lint b[12]={1,10,100,1000,10000,100000,1000000,
10000000,100000000,1000000000,10000000000,100000000000};
lint count(lint n){
    lint left,m,sum=0;
    for(int i=1;i<12;i++){
        left=n/b[i]-1;
        sum+=left*b[i-1];
        m=(n%b[i]-n%b[i-1])/b[i-1]; //求出从低到高的第i位上的具体数字
        if(m>0) 
			sum+=b[i-1];
        else if(m==0) 
			sum+=n%b[i-1]+1;
        if(n<b[i]) 
			break;
    }
    return sum;
}
int main(){
    lint m,n;
    while(sf("%I64d%I64d",&m,&n)&&(n>=0))
        pf("%I64d\n",count(n)-count(m-1));
    return 0;
}


 

你可能感兴趣的:(poj 3286-数位DP)