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)

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

计算满足fa的个数,数位dp,终于搞懂怎么做数位dp,自己写出来了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define sca(x) scanf("%d",&x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1000005;
int p[25];
int tt[15][11];
int dp[11][4605];
void first()
{
p[1]=1;
rep(i,2,22)
p[i]=p[i-1]*2;
rep(i,1,11)
rep(j,0,10)
tt[i][j]=j*p[i];
mm(dp,-1);
}
int calc(int x)
{
int ans=0,pos=1;
while(x)
{
ans+=(x%10)*p[pos++];
x/=10;
}
return ans;
}
int num1[15],num2[15];
int dfs(int pos,int left,bool lead,bool limit)//left是剩下的,lead这里没有用,可舍去,limit是否上限
{
if(!pos) return 1;
int ans=0;
if(!limit&&dp[pos][left]!=-1) return dp[pos][left];
int up=limit?num2[pos]:9;
rep(i,0,up+1)
{
if(tt[pos][i]>left) break;
ans+=dfs(pos-1,left-tt[pos][i],lead&&i==num1[pos],limit&&(i==num2[pos]));
}
if(!limit) dp[pos][left]=ans;
return ans;
}
int solve(int l,int r)
{
int x=l;
mm(num1,0);
int pos1=0;
while(l)
{
num1[++pos1]=l%10;
l/=10;
}
int pos=0;
while(r)
{
num2[++pos]=r%10;
r/=10;
}
return dfs(pos,calc(x),true,true);
}
int main()
{
int re,l,r,cas=1;
first();
sf("%d",&re);
while(re--)
{
sf("%d%d",&l,&r);
pf("Case #%d: %d\n",cas++,solve(l,r));
}
return 0;
}

D - F(x)的更多相关文章

  1. Mysql_以案例为基准之查询

    查询数据操作

  2. 在 C# 里使用 F# 的 option 变量

    在使用 C# 与 F# 混合编程的时候(通常是使用 C# 实现 GUI,F#负责数据处理),经常会遇到要判断一个 option 是 None 还是 Some.虽然 Option module 里有 i ...

  3. 如果你也会C#,那不妨了解下F#(7):面向对象编程之继承、接口和泛型

    前言 面向对象三大基本特性:封装.继承.多态.上一篇中介绍了类的定义,下面就了解下F#中继承和多态的使用吧.

  4. 如果你也会C#,那不妨了解下F#(2):数值运算和流程控制语法

    本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-2.html 一些废话 一门语言火不火,与语言本身并没太大关系,主要看语言的推广. 推广得好,用的 ...

  5. 使用F#开发ASP.NET Core应用程序

    .NET Core 里的F# 在.NET Core刚发布时,就已经添加了对F#的支持.但因为当时F#组件还不完整,而一些依赖包并没有放在Nuget上,而是社区自己放到MyGet上,所以在使用dotne ...

  6. 如果你也会C#,那不妨了解下F#(6):面向对象编程之“类”

    前言 面向对象的思想已经非常成熟,而使用C#的程序员对面向对象也是非常熟悉,所以我就不对面向对象进行介绍了,在这篇文章中将只会介绍面向对象在F#中的使用. F#是支持面向对象的函数式编程语言,所以你用 ...

  7. 如果你也会C#,那不妨了解下F#(5):模块、与C#互相调用

    F# 项目 在之前的几篇文章介绍的代码都在交互窗口(fsi.exe)里运行,但平常开发的软件程序可能含有大类类型和函数定义,代码不可能都在一个文件里.下面我们来看VS里提供的F#项目模板. F#项目模 ...

  8. 如果你也会C#,那不妨了解下F#(4):了解函数及常用函数

    函数式编程其实就是按照数学上的函数运算思想来实现计算机上的运算.虽然我们不需要深入了解数学函数的知识,但应该清楚函数式编程的基础是来自于数学. 例如数学函数\(f(x) = x^2+x\),并没有指定 ...

  9. 如果你也会C#,那不妨了解下F#(3):F#集合类型和其他核心类型

    本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-3.html 在第一篇中,我们介绍了一些基础数据类型,其实那篇标题中不应该含有"F#&q ...

  10. 如果你也会C#,那不妨了解下F#(1):F# 数据类型

    本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-1.html 简单介绍 F#(与C#一样,念作"F Sharp")是一种基于. ...

随机推荐

  1. js扩展运算符(spread)三个点(...)

    常见用法: 1.该运算符主要用于函数调用. function push(array, ...items) { array.push(...items); } function add(x, y) { ...

  2. CSS3-loading动画

    (二) 上次分享了四个CSS3的加载动画,今天继续(标题接上一次). 在线demo:http://liyunpei.xyz/loading.html   (持续更新) 请注意:代码中的关键帧动画有的用 ...

  3. drawRect中抗锯齿

    在开始之前,我们需要创建一个DrawRectView 其初始代码为 // // DrawRectView.h // CGContextSetShouldAntialias // // Created ...

  4. wriesharek同时监听多个端口

    之前的文章<wireshark解析自定义的protobuf协议> ,当时只监听了一个端口,而如果游戏同时有二个 socket 连接,比如一个是网关另外一个是其它的,怎么办呢? for i, ...

  5. Cygwin配置总结

    Cygwin配置总结 Cygwin是 大量GNU和开放源码工具的集合,它们提供了类似于Windows上的Linux发行版的功能 DLL(cygwin1.dll),它提供了大量的POSIX API功能. ...

  6. 【Java】maven多项目资源共享

    方案一: <resources> <resource> <!-- <directory>${project.parent.relativePath}/../. ...

  7. [Vuex] Use Namespaces in Vuex Stores using TypeScript

    Even by using modules, they still share the same namespace. So you couldn’t have the same mutation n ...

  8. C# System.Threading.Timer

    提供以指定的时间间隔对线程池线程执行方法的机制 using System; using System.Threading; class TimerExample { static void Main( ...

  9. JSONObject、JSONArray、Map、JavaBean的相互转换

    1,JSONObject json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value} 2,JSONArray json数组,使用中括号[ ],只不过数组里面的项也是json键 ...

  10. CouchDB 简单HTTP接口使用说明

    目录 1.简介 2.安装 2.HTTP接口简单使用 2.1.认证接口 2.1.1 Basic Authentication 2.1.2 Cookie Authentication 2.2 创建与删除数 ...