Balanced Numbers

https://vjudge.net/contest/287810#problem/K

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 <= 10 19

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 用三进制来计算数位出现的奇偶
 #include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pb push_back
#define eb emplace_back
#define maxn 13000005
#define eps 1e-8
#define pi acos(-1.0)
#define rep(k,i,j) for(int k=i;k<j;k++)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,int>pli;
typedef pair<int,char> pic;
typedef pair<pair<int,string>,pii> ppp;
typedef unsigned long long ull;
const long long MOD=1e9+;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ ll dp[][];
int a[];
int k; bool Check(int x){
int num[];
for(int i=;i<;i++){
num[i]=x%;
x/=;
}
for(int i=;i<;i++){
if(num[i]==&&(i%)){
return false;
}
if(num[i]==&&(i%==)){
return false;
}
}
return true;
} int Change(int x,int v){
int num[];
for(int i=;i<;i++){
num[i]=x%;
x/=;
}
if(num[v]==) num[v]=;
else if(num[v]==||num[v]==) num[v]=;
x=;
for(int i=;i>=;i--){
x=x*+num[i];
}
return x;
} ll dfs(int pos,int st,int lead,int limit){///要去掉前导0
if(pos==-) return Check(st);
if(!limit&&dp[pos][st]!=-) return dp[pos][st];
ll ans=;
int up=limit?a[pos]:;
for(int i=;i<=up;i++){
ans+=dfs(pos-,(lead==&&i==)?:Change(st,i),lead&&i==,limit&&i==a[pos]);
}
if(!limit) dp[pos][st]=ans;
return ans;
} ll solve(ll x){
int pos=;
while(x){
a[pos++]=x%;
x/=;
}
ll ans=dfs(pos-,,,);
return ans;
} int main(){
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
int t;
ll n,m;
memset(dp,-,sizeof(dp));
cin>>t;
for(int _=;_<=t;_++){
cin>>n>>m;
ll ans=solve(m)-solve(n-);
cout<<ans<<endl;
} }
 

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. SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)

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

  3. spoj Balanced Numbers(数位dp)

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

  4. spoj 10606 Balanced Numbers 数位dp

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

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  6. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  7. hdu3709 Balanced Number (数位dp+bfs)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  8. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

随机推荐

  1. Linux查看系统中socket状态

    当我们打开的socket数量很多时,netstat就会变得慢了,有什么办法可以快速查看系统中socket状态? IPv4: $ cat /proc/net/sockstat sockets: used ...

  2. du 统计文件夹大小

    du -h --max-depth=1 |grep [TG] |sort   #查找上G和T的目录并排序 du -sh    #统计当前目录的大小,以直观方式展现 du -h --max-depth= ...

  3. day07-while,for循环

    1.循环语句 Python提供了for循环和while循环while在给定的判断条件为 true 时执行循环体,否则退出循环体.for重复执行语句嵌套循环可以在while循环体中嵌套for.while ...

  4. SRM-相关资料路径

    SRM采购管理平台功能介绍 https://wenku.baidu.com/view/b05cff5930b765ce0508763231126edb6f1a763c.html https://wen ...

  5. mysql 索引,转载

    from:http://blog.csdn.net/zhanglu0223/article/details/8713149 1. 索引建立的原则 用于索引的最好的备选数据列是那些出现在WHERE子句. ...

  6. 1047A_Little C Loves 3 I(构造)

    A. Little C Loves 3 I time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. Spark 调优(转)

    Spark 调优 返回原文英文原文:Tuning Spark Because of the in-memory nature of most Spark computations, Spark pro ...

  8. [ 测试思维 ] 启发式测试策略模型(HTSM)

    什么是HTSM 启发式测试策略模型(Heuristic Test Strategy Model,简称HTSM)是测试专家James Bach提出的一组帮助测试设计的指南(Guide line).HTS ...

  9. .NET 基础知识

    .net程序基本编写.执行流程(c#)       1>编写c#代码,保存为.cs文件.       2>通过csc.exe程序来将.cs文件编译为.net程序集(.exe或.dll).此 ...

  10. VC/VS2010中快捷键

    序号 热键 备注 1 F7 编译 2 Ctrl + F7 链接 3 Ctrl + F5 运行 4 F9 在光标处设置断点 5 F10 单步跟踪 6 F11 进入函数内部跟踪 7 Shift + F11 ...