F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4862 Accepted Submission(s): 1803
Problem Description
For a decimal number x with n digits (A
nA
n-1A
n-2 ... A
2A
1), we define its weight as F(x) = A
n * 2
n-1 + A
n-1 * 2
n-2 + ... + A
2 * 2 + A
1 * 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 < 10 9)
For each test case, there are two numbers A and B (0 <= A,B < 10 9)
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
题意:我们定义十进制数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)的数。
思路:一开始写了一个很裸的数位dp,看每组数据0-b里面多少个比a少的。。。这样只是这一组记忆化。。超时了,然后我想是不是要记忆所有的fa呢。。然后把dp数组变成三维了。。。mle了。。。后来才知道,直接二维数组,dp[i][j] i表示位数,j表示在i位这里比j小的数量有多少。。。tle代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int r;
int bit[15], p[10] = {0,1, 2, 4, 8, 16, 32, 64, 128,256}, dp[10][2500];
int dfs(int len, int sum, int limit)
{
if(len < 1) return sum < r || sum == r;
if(sum > r) {dp[len][sum] = 0; return 0;}
if(!limit && dp[len][sum] != -1) return dp[len][sum];
int last = limit ? bit[len] : 9;
int ans = 0;
for(int i = 0; i <= last; i++)
{
int tsum = sum + i * p[len];
ans += dfs(len-1, tsum, limit && i == last);
}
if(!limit) dp[len][sum] = ans;
return ans;
}
int cal(int n)
{
int k = 0;
while(n)
{
bit[++k] = n % 10;
n /= 10;
}
return dfs(k, 0, 1);
}
int main()
{
int t, Case = 0;
scanf("%d", &t);
while(t--)
{
memset(dp, -1, sizeof(dp));
int a, b;
scanf("%d%d", &a ,&b);
int k = 1;
r = 0;
while(a)
{
r += a%10*p[k++];
a /= 10;
}
// cout << r << endl;
printf("Case #%d: %d\n", ++Case, cal(b));
}
return 0;
}
mle代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int r;
int bit[15], p[9] = {0,1, 2, 4, 8, 16, 32, 64, 128}, dp[10][3000][3000];
int dfs(int len, int sum, int r, int limit)
{
if(len < 1) return sum < r || sum == r;
if(!limit && dp[len][sum][r] != -1) return dp[len][sum][r];
int last = limit ? bit[len] : 9;
int ans = 0;
for(int i = 0; i <= last; i++)
{
int tsum = sum + i * p[len];
ans += dfs(len-1, tsum, r, limit && i == last);
}
if(!limit) dp[len][sum][r] = ans;
return ans;
}
int cal(int n)
{
int k = 0;
while(n)
{
bit[++k] = n % 10;
n /= 10;
}
return dfs(k, 0, r, 1);
}
int main()
{
int t, Case = 0;
memset(dp, -1, sizeof(dp));
scanf("%d", &t);
while(t--)
{
int a, b;
scanf("%d%d", &a ,&b);
int k = 1;
r = 0;
while(a)
{
r += a%10*p[k++];
a /= 10;
}
// cout << r << endl;
printf("Case #%d: %d\n", ++Case, cal(b));
}
return 0;
}
正解:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int r;
int bit[20], p[10] = {0,1, 2, 4, 8, 16, 32, 64, 128,256}, dp[20][200000];
int dfs(int len, int sum, int limit)
{
if(len < 1) return sum >= 0;
if(sum < 0) return 0;
if(!limit && dp[len][sum] != -1) return dp[len][sum];
int last = limit ? bit[len] : 9;
int ans = 0;
for(int i = 0; i <= last; i++)
{
ans += dfs(len-1, sum - i*p[len], limit && i == last);
}
if(!limit) dp[len][sum] = ans;
return ans;
}
int cal(int n)
{
int k = 0;
while(n)
{
bit[++k] = n % 10;
n /= 10;
}
return dfs(k, r, 1);
}
int main()
{
int t, Case = 0;
scanf("%d", &t);
memset(dp, -1, sizeof(dp));
while(t--)
{
int a, b;
scanf("%d%d", &a ,&b);
int k = 1, len = 0;
r = 0;
while(a)
{
r += a%10*(1<<len);
len++;
a /= 10;
}
// cout << r << endl;
printf("Case #%d: %d\n", ++Case, cal(b));
}
return 0;
}