Balanced Numbers

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为偶数个。
3^10约为60000。
注意判断前导零。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; ll a[],p[],st[][];
ll dp[][]; ll dfs(int pos,int sta,bool lead,bool limit){ if(pos==-){
for(int i=;i<=;i+=){
if(st[sta][i]==) return ;
}
for(int i=;i<=;i+=){
if(st[sta][i]==) return ;
}
return ;
}
if(!lead&&!limit&&dp[pos][sta]>-) return dp[pos][sta];
int up=limit?a[pos]:;
ll cnt=;
for(int i=;i<=up;i++){
if(i==&&lead){
cnt+=dfs(pos-,sta,lead&&i==,limit&&i==a[pos]);
}
else if(st[sta][i]==){
cnt+=dfs(pos-,sta-p[i],lead&&i==,limit&&i==a[pos]);
}
else{
cnt+=dfs(pos-,sta+p[i],lead&&i==,limit&&i==a[pos]);
}
}
if(!lead&&!limit) dp[pos][sta]=cnt;
return cnt;
}
ll solve(ll x){
int pos=;
while(x){
a[pos++]=x%;
x/=;
}
return dfs(pos-,,true,true);
}
int main()
{
int t;
ll l,r;
scanf("%d",&t);
memset(dp,-,sizeof(dp));
p[]=;
for(int i=;i<=;i++){
p[i]=p[i-]*;
}
for(int i=;i<p[];i++){
int ii=i,c=-;
while(ii){
c++;
st[i][c]=ii%;
ii/=;
}
}
while(t--){
scanf("%lld%lld",&l,&r);
printf("%lld\n",(solve(r)-solve(l-)));
}
return ;
}

SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)的更多相关文章

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

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

  2. spoj 10606 Balanced Numbers 数位dp

    题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...

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

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

  4. spoj Balanced Numbers(数位dp)

    一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...

  5. ZRDay6A. 萌新拆塔(三进制状压dp)

    题意 Sol 这好像是我第一次接触三进制状压 首先,每次打完怪之后吃宝石不一定是最优的,因为有模仿怪的存在,可能你吃完宝石和他打就GG了.. 因此我们需要维护的状态有三个 0:没打 1:打了怪物 没吃 ...

  6. HDU 3001 三进制状压DP

    N个城市,M条道路,每条道路有其经过的代价,每一个城市最多能够到达两次,求走全然部城市最小代价,起点随意. 三进制状压.存储每一个状态下每一个城市经过的次数. 转移方程: dp[i+b[k]][k]= ...

  7. 三进制状压 HDOJ 3001 Travelling

    题目传送门 题意:从某个点出发,所有点都走过且最多走两次,问最小花费 分析:数据量这么小应该是状压题,旅行商TSP的变形.dp[st][i]表示状态st,在i点时的最小花费,用三进制状压.以后任意进制 ...

  8. Codeforces Round #297 (Div. 2) [ 折半 + 三进制状压 + map ]

    传送门 E. Anya and Cubes time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. hdu3001(三进制状压)

    题目大意: 现在给你一个有n个点和m条边的图,每一条边都有一个费用,每个点不能经过超过两次,求所有点至少遍历一次的最小费用 其中n<=10 m没有明确限制(肯定不会超过1e5) 一看到这个数据范 ...

随机推荐

  1. tests

    test

  2. LeetCode Longest Uncommon Subsequence II

    原题链接在这里:https://leetcode.com/problems/longest-uncommon-subsequence-ii/#/description 题目: Given a list ...

  3. 如何用nodejs启一个前端服务

    1.新建文件夹,如 notice 2.新建页面和js文件,如 index.html server.js 3.index.html页面内容随你写,如: <!DOCTYPE html> < ...

  4. 关于讯飞语音SDK开发学习

    前奏,浑浑噩噩已经工作一年多,这一年多收获还是挺多的.逛园子应该有两年多了,工作后基本上是天天都会来园子逛逛,园子 里还是有很多牛人写了一些不错的博客,帮我解决很多问题.但是一直没写过博客,归根到底一 ...

  5. python中print的几种用法

    python中的print有几种常用的用法: 1. print("first example") 2. print("second", "exampl ...

  6. Azure上采用Json Template从已有的VHD创建VM

    从已有的VHD创建VM是使用Azure中经常要操作的内容. 本文将介绍如何采用Json Template从已经有的VHD创建VM. 一.准备VHD 在我的Azure账户中选择一台VM,如下图: 查看其 ...

  7. Hibernate基础(一)

    Hibernate是ORM开源组件 源码:http://sourceforge.net/projects/hibernate/ 1.Hibernate的配置文件. 默认配置文件: hibernate. ...

  8. 分布式锁之一:zookeeper分布式锁1

    zookeeper集群的每个节点的数据都是一致的, 那么我们可以通过这些节点来作为锁的标志. 首先给锁设置一下API, 至少要包含, lock(锁住), unlock(解锁), isLocked(是否 ...

  9. Zookeeper学习(八):Zookeeper的数据发布与订阅模式

     http://blog.csdn.net/ZuoAnYinXiang/article/category/6104448 1.发布订阅的基本概念        1.发布订阅模式可以看成一对多的关系:多 ...

  10. python风格指南

    还在让别人一眼就看出你是一只野生程序猿嘛,快来看看谷歌的python风格指南提升逼格吧! http://zh-google-styleguide.readthedocs.io/en/latest/go ...