BALNUM - Balanced Numbers

Time limit:123 ms

Memory limit:1572864 kB

Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a balanced number if:

1)      Every even digit appears an odd number of times in its decimal representation

2)      Every odd digit appears an even number of times in its decimal representation

For example, 77, 211, 6222 and 112334445555677 are balanced numbers while 351, 21, and 662 are not.

Given an interval [A, B], your task is to find the amount of balanced numbers in [A, B] where both A and B are included.

Input

The first line contains an integer T representing the number of test cases.

A test case consists of two numbers A and B separated by a single space representing the interval. You may assume that 1 <= A <= B <= 1019

Output

For each test case, you need to write a number in a single line: the amount of balanced numbers in the corresponding interval

Example

Input:
2
1 1000
1 9
Output:
147
4
分析:如何统计0~9是否出现过且是否出现奇偶次是难点;
   正解是三进制压缩,该位置为0代表没出现,1代表出现奇数次,2代表出现偶数次;
   不过一看内存这么大,可以随便做了,dp[i][j][k]分别代表位置,二进制判是否出现,二进制判每个数出现次数奇偶性;
   注意前导0不算;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
const int N=5e4+;
const int M=N**;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,num[],pos;
ll dp[][<<][<<],p,q;
ll dfs(int pos,int x,int y,int z,int k)
{
if(pos<)
{
int i;
rep(i,,)if(x>>i&&&(y>>i&)^(i&)!=)return ;
return ;
}
if(z&&k&&dp[pos][x][y]!=-)return dp[pos][x][y];
int now=z?:num[pos],i;
ll ret=;
rep(i,,now)
{
ret+=dfs(pos-,!i&&!k?x:x|(<<i),!i&&!k?y:y^(<<i),z||i<num[pos],k||i);
}
return z&&k?dp[pos][x][y]=ret:ret;
}
ll gao(ll x)
{
pos=;
while(x)num[pos++]=x%,x/=;
return dfs(pos-,,,,);
}
int main()
{
int i,j;
memset(dp,-,sizeof(dp));
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&p,&q);
printf("%lld\n",gao(q)-gao(p-));
}
return ;
}

BALNUM - Balanced Numbers的更多相关文章

  1. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  2. SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)

    Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...

  3. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

    Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...

  4. SPOJ - BALNUM - Balanced Numbers(数位DP)

    链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...

  5. SPOJ BALNUM Balanced Numbers (数位dp)

    题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...

  6. BALNUM - Balanced Numbers(数位dp)

    题目链接:http://www.spoj.com/problems/BALNUM/en/ 题意:问你在[A,B]的闭区间内有几个满足要求的数,要求为每个出现的奇数个数为偶数个,每个出现的偶数个数为奇数 ...

  7. SPOJ BALNUM Balanced Numbers(数位DP+状态压缩)题解

    思路: 把0~9的状态用3进制表示,数据量3^10 代码: #include<cstdio> #include<map> #include<set> #includ ...

  8. SPOJ - BALNUM Balanced Numbers

    题意: 求出所给范围内满足其数位上的奇数出现偶数次,数位上的偶数出现奇数次(或不出现)的数的个数. 思路: 对于0 ~ 9 每个数有3种情况. 1.没出现过 2.出现奇数次 3.出现偶数次 那么就可以 ...

  9. SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)

    题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...

随机推荐

  1. 10.0arcmap切片生成ptk步骤

    注意:在制作之前需要点将图放到原本大小.并且保存一下不然容易造成数据丢失. 1.制作mxd 我们将待发布的数据,鼠标选中,拖入到ArcMap中间区域,单击保存. 可以对layers下面的图层进行改名. ...

  2. oc58--Category注意事项

    // // main.m // Category注意事项 #import <Foundation/Foundation.h> #import "Person+NJ.h" ...

  3. OSGI依赖问题处理

    用osgi实现java的模块化和热插拔时要考虑好两个问题,不同bundle间如何通信?依赖怎么处理? OSGi的一个标准就是各个bundle之间是相互隔离的,每个bundle都有自己的classloa ...

  4. EOJ 3023 字符组合

    3.30更新 #include <iostream> #include <stdio.h> #include <algorithm> #include <se ...

  5. hdu 3037Saving Beans(卢卡斯定理)

    Saving Beans Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. [Apple开发者帐户帮助]二、管理你的团队(6)找到您的团队ID

    该组ID是已分配给您的团队苹果产生了独特的10个字符的字符串.您需要为某些活动提供您的团队ID, 例如将您的应用程序转移到App Store Connect中的其他开发人员. 在您的开发者帐户中,单击 ...

  7. Django day04 路由控制

    Django请求的整个的生命周期 Django中路由控制的作用: 一: 简单配置 url 是一个函数 -第一个参数是正则表达式(如果要精确匹配:'^publish'/$ 以^开头,以$结尾) -第二个 ...

  8. python修改植物僵尸

    import win32process#进程模块 import win32con#系统定义 import win32api#调用系统模块 import ctypes#C语言类型 import win3 ...

  9. MySQL安装for windows

    ======MySQL安装 for windows====== 版本5.7.X MySQL服务器帮助我们来管理文件的操作 MySQL软件 - 服务器端软件 - 服务端程序 - 解析指令 - 对文件的操 ...

  10. ASP.NET访问网络驱动器(映射磁盘)

    也许很多朋友在做WEB项目的时候都会碰到这样一个需求: 当用户上传文件时,需要将上传的文件保存到另外一台专门的文件服务器. 要实现这样一个功能,有两种解决方案: 方案一.在文件服务器上新建一站点,用来 ...