F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 382 Accepted Submission(s): 137
Problem Description
For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A).
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
For each test case, there are two numbers A and B (0 <= A,B < 109)
For each test case, there are two numbers A and B (0 <= A,B < 109)
Output
For every case,you should output "Case #t: " at first, without quotes. The t is the case number starting from 1. Then output the answer.
Sample Input
3
0 100
1 10
5 100
Sample Output
Case #1: 1
Case #2: 2
Case #3: 13
Source
Recommend
liuyiding
数位DP的水题
dp[i][j]表示i位值<=j 的总数
/* *********************************************** Author :kuangbin Created Time :2013/9/14 星期六 12:45:42 File Name :2013成都网络赛1007.cpp ************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; int dp[20][200000]; int bit[20]; int dfs(int pos,int num,bool flag) { if(pos == -1)return num >= 0; if(num < 0)return 0; if(!flag && dp[pos][num] != -1) return dp[pos][num]; int ans = 0; int end = flag?bit[pos]:9; for(int i = 0;i <= end;i++) { ans += dfs(pos-1,num - i*(1<<pos),flag && i==end); } if(!flag)dp[pos][num] = ans; return ans; } int F(int x) { int ret = 0; int len = 0; while(x) { ret += (x%10)*(1<<len); len++; x /= 10; } return ret; } int A,B; int calc() { int len = 0; while(B) { bit[len++] = B%10; B/=10; //cout<<bit[len-1]<<endl; } //cout<<F(A)<<endl; return dfs(len-1,F(A),1); } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int iCase = 0; scanf("%d",&T); memset(dp,-1,sizeof(dp)); while(T--) { iCase++; scanf("%d%d",&A,&B); printf("Case #%d: %d ",iCase,calc()); } return 0; }