poj 3286 - 디지털 DP

1244 단어
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
",count(n)-count(m-1)); return 0; }

좋은 웹페이지 즐겨찾기