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

最新文章

  1. 学习js回调函数
  2. String All Methods
  3. nginx+php 安装手册
  4. tcpdump 时报ServFail 0/0/1 (97)
  5. 【wikioi】1403 新三国争霸(dp+kruskal)
  6. php file_get_contents与curl性能比较
  7. IREP_SOA Integration程序注释语法Annotations(概念)
  8. 关于SVN更新时文件加锁的小结
  9. ARFF文件格式
  10. github 使用方法总结 还有一部分不太懂
  11. 从C# String类理解Unicode(UTF8/UTF16)
  12. 使用express.js框架一步步实现基本应用以及构建可扩展的web应用
  13. Lightbox 图片展示插件
  14. Android 性能优化概念(1)
  15. Beta Scrum Day 1
  16. [转帖]御界预警:3700余台SQL服务器被入侵挖矿 或导致严重信息泄露事件
  17. 设计模式C++学习笔记之二十(完结篇 &amp; 面向对象原则)设计模式C++实例下载
  18. Win系统的快捷键
  19. RobotFrameWork(十三)RobotFramework与loadrunner性能测试结合(基于Remote库)
  20. win 10 安装visual studio 2013

热门文章

  1. nodejs--Nodejs单元测试小结
  2. hihoCoder 简单计算器
  3. CALayer(一)
  4. S - New Year Transportation
  5. css中标签,类名,id名的命名 语义化命名
  6. MySQL命令学习之技巧(博主推荐)
  7. 树莓派-基于raspivid实现拍视频
  8. 【转载】linux环境下大数据网站搬家
  9. ★Java语法(六)——————————分支语句
  10. texi格式文件的读取