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 ;
}
 
 
 

F(x) 数位dp的更多相关文章

  1. hdu 4389 X mod f(x) 数位DP

    思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...

  2. HDU 4734 F(x) ★(数位DP)

    题意 一个整数 (AnAn-1An-2 ... A2A1), 定义 F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1,求[0..B]内有多少 ...

  3. 【hdu4734】F(x) 数位dp

    题目描述 对于一个非负整数 $x=​​\overline{a_na_{n-1}...a_2a_1}$ ,设 $F(x)=a_n·2^{n-1}+a_{n-1}·2^{n-2}+...+a_2·2^1+ ...

  4. [hdu4734]F(x)数位dp

    题意:求0~f(b)中,有几个小于等于 f(a)的. 解题关键:数位dp #include<bits/stdc++.h> using namespace std; typedef long ...

  5. hdu4734 F(x)(数位dp)

    题目传送门 F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. HDU4389:X mod f(x)(数位DP)

    Problem Description Here is a function f(x): int f ( int x ) { if ( x == 0 ) return 0; return f ( x ...

  7. HDU 4734 - F(x) - [数位DP][memset优化]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 Time Limit: 1000/500 MS (Java/Others) Memory Lim ...

  8. HDU-4734 F(x) 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 注意到F(x)的值比较小,所以可以先预处理所有F(x)的组合个数.f[i][j]表示 i 位数时 ...

  9. bzoj 3131 [Sdoi2013]淘金(数位DP+优先队列)

    Description 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块.    一阵风吹 ...

随机推荐

  1. 【原创】大叔经验分享(30)CM开启kerberos

    kerberos安装详见:https://www.cnblogs.com/barneywill/p/10394164.html 一 为CM创建用户 # kadmin.local -q "ad ...

  2. 新手-ios

    最近突然让我学习一下ios,之前从未接触过(一脸蒙逼).而且我用的电脑也不是ios操作系统.上网查了下 网友说虚拟机也可以,于是本人从此举用上了ios系统. 需要的安装的工具有: 资源共享给大家: h ...

  3. boolalpha的作用

    #include <iostream>using namespace std;int main(){        bool b=true;        cout << &q ...

  4. 使用XIB 或者storyboard 创建imageView 模式 UIViewContentModeScaleAspectFill  图片越界问题

    ImageView UIViewContentModeScaleAspectFill 超出边界的问题 代码如下 [_photoView setClipsToBounds:Yes];       sto ...

  5. ios  调整 label 的字体行间距

     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200) ...

  6. 如何获取STM32 MCU的唯一ID

    前段时间由于应用需要对产品授权进行限制,所以研究了一下有关STM32 MCU的唯一ID的资料,并最终利用它实现了我们的目标. 1.基本描述 在STM32的全系列MCU中均有一个96位的唯一设备标识符. ...

  7. Confluence 6 避免和清理垃圾

    如果你的 Confluence 是允许公众访问的话,你可能会遇到垃圾内容的骚扰. 阻止垃圾发布者 希望阻止垃圾发布者: 启用验证码(Captcha),请参考页面 Configuring Captcha ...

  8. ignitius and princess 2(全排列)

    A - Ignatius and the Princess II Now our hero finds the door to the BEelzebub feng5166. He opens the ...

  9. ERROR 1044 (42000): Access denied for user 'root'@'%' to database 'mysql'

    原因:修改数据库账号时删除了默认的localhost root,  新建了% root 但没有赋予全部权限; 解决方法: 1.关闭数据库# mysqld stop 2.在my.cnf里加入skip-g ...

  10. HTML&javaSkcript&CSS&jQuery&ajax(七)

    ’一.HTML5 实例  <video width="430" controls>   <source src="mov_nnn.mp4" t ...