F(x) 数位dp

时间:2022-07-01 08:26:46
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 < 10[sup]9[/sup])
 
Output
For every case,you should output "Case #t: " at first, without quotes. The [I]t[/I] 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
 
 
 
其实转化后的数字比原来的要小得多   一开始还纠结开不起数组  
 
把数位的和保存起来  最后读取完的时候再比较即可
 
为了memset优化  dp数组用减法
 
#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
#define N 20 int f(int x)
{
if(!x)return ;
int ans=f(x/);
return ans*+(x%);
} ll dp[][+];
ll a[N];
int all; ll dfs(int pos,int sum,bool lead,bool limit)
{
if(!pos)
{
return sum<=all;
}
if(sum>all)return ; if(!limit&&!lead&&dp[pos][all-sum]!=-)return dp[pos][all-sum];
ll ans=;
int up=limit?a[pos]:;
rep(i,,up)
{
ans+=dfs(pos-, sum+i*(<<pos-) , lead&&i==,limit&&i==a[pos]); } if(!limit&&!lead)dp[pos][all-sum]=ans;
return ans;
}
ll solve(int b)
{
int pos=; while(b)
{
a[++pos]=b%;
b/=;
} return dfs(pos, ,true,true);
}
int main()
{
CLR(dp,-); RI(cas);
int kase=;
while(cas--)
{
int a,b;
cin>>a>>b;
all=f(a);
printf("Case #%d: %lld\n",++kase,solve(b));
}
return ;
}