hdu4734数位dp

定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+…a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字。

题目给出a,b,求出0~b有多少个不大于f(a)的数。

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;

import org.omg.CORBA.Object;




public class Main {


    public static void main(String[] args) throws IOException{

           StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in)); 
           InputReader in = new InputReader(System.in)  ;
           PrintWriter out = new PrintWriter(System.out) ;

           int t = in.nextInt() ;
           for(int i = 1 ; i <= t ; i++){
                out.print("Case #" + i + ": ")  ;
                new Task().solve(in, out)  ;   //out.flush() ;
           }

           out.flush() ; 


    }


}

class Task{

       static int[] bit = new int[32] ;
       int len ;
       int a , b ;
       static int[][] dp = new int[10][1024*9] ;
       static{
              for(int i = 0 ; i <= 9 ; i++) Arrays.fill(dp[i], -1);
       }


       int  dfs(int pos , int val , boolean isend){
            if(pos < 0) return val >= 0 ? 1 : 0 ;
            if(val < 0) return 0 ;
            if(!isend && dp[pos][val] != -1) return dp[pos][val] ;
            int d = isend ? bit[pos] : 9 ;
            int s = 0 ;
            for(int i = 0 ; i <= d ; i++){
                  s += dfs(pos-1 , val - i * (1<<pos) , isend && (i == d) ) ;
            }
            if(!isend) dp[pos][val] = s ;
            return s ;
       }

       int  answer(){
            len = 0 ;
            while(b > 0){
                 bit[len++] = b % 10 ;
                 b /= 10 ;
            }
            return dfs(len-1 ,  f(a) ,  true) ;
       }

       int  f(int x){
            int i = 0  , s = 0 ; 
            while(x > 0){
                 s += ((x % 10 ) * (1<<i)) ;
                 i++ ;
                 x /= 10 ;
            }
            return s ;
       }

       public void solve(InputReader in , PrintWriter out) throws IOException{
              a = in.nextInt() ;
              b = in.nextInt() ;
              out.println(answer()) ;
       }

}


class InputReader{
        public BufferedReader  reader;
        public StringTokenizer  tokenizer;

        public InputReader(InputStream stream){
                    reader = new BufferedReader(new InputStreamReader(stream), 32768) ;
                    tokenizer = null ;
        }

        public String next(){
                    while(tokenizer == null || ! tokenizer.hasMoreTokens()){
                        try{
                                tokenizer = new StringTokenizer(reader.readLine());
                        }catch (IOException e) {
                                throw new RuntimeException(e);
                        }
                    }
                    return tokenizer.nextToken();  
        }

        public int  nextInt() {
                    return Integer.parseInt(next());
        }

        public long nextLong() {
                    return Long.parseLong(next());
        }

}

你可能感兴趣的:(hdu4734数位dp)