F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 712 Accepted Submission(s): 269
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
liuyiding
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
#define N 100010
#define eps (1e-8)
#define INF 99999999
using namespace std;
int c[10][9*512];//c[i][j]表示有i位且f(x)<=j的x个数;
void init()
{
int i,j,k;
for(i=0;i<10;i++)
c[i][0]=1;//开始忘记了初始化这个了,WA了好久。。。哎
for(i=0;i<9*512;i++)
c[0][i]=1;
for(i=0;i<9*512;i++)
c[1][i]=min(10,i+1);
for(i=2;i<=9;i++)
for(j=1;j<9*512;j++)
{
for(k=0;k<=9 && j-k*(1<<(i-1))>=0;k++)
c[i][j]+=c[i-1][j-k*(1<<(i-1))];
}
}
int f(int x)
{
int ans=0,q=1;
while(x)
{
ans+=q*(x%10);
q*=2;
x/=10;
}
return ans;
}
int solve(int a,int b)
{
int s[11],d=0,ans=0;
if(a==0) return 1;
while(b)
{
s[d++]=b%10;
b/=10;
}
for(d--;d>=0;d--)
{
for(int i=0;i<s[d];i++)
{
if(a-i*(1<<d)<0) return ans;
ans+=c[d][a-i*(1<<d)];
}
a-=s[d]*(1<<d);
if(a<0) return ans;
}
return ans;
}
int main()
{
int t,cas=1;
init();
scanf("%d",&t);
while(t--)
{
int a,b;
scanf("%d %d",&a,&b);
printf("Case #%d: %d\n",cas++,solve(f(a),b+1));
}
return 0;
}
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language |
9214984 | 2013-09-23 15:27:17 | Accepted | 4734 | 31MS | 492K | 1567 B | G++ |