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

  1. Input:
  2. 2
  3. 1 1000
  4. 1 9
  1. Output:
  2. 147
  3. 4
    分析:如何统计0~9是否出现过且是否出现奇偶次是难点;
       正解是三进制压缩,该位置为0代表没出现,1代表出现奇数次,2代表出现偶数次;
       不过一看内存这么大,可以随便做了,dp[i][j][k]分别代表位置,二进制判是否出现,二进制判每个数出现次数奇偶性;
       注意前导0不算;
    代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <bitset>
  11. #include <map>
  12. #include <queue>
  13. #include <stack>
  14. #include <vector>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define mod 1000000007
  17. #define inf 0x3f3f3f3f
  18. #define vi vector<int>
  19. #define pb push_back
  20. #define mp make_pair
  21. #define fi first
  22. #define se second
  23. #define ll long long
  24. #define pi acos(-1.0)
  25. #define pii pair<int,int>
  26. #define sys system("pause")
  27. const int maxn=1e5+;
  28. const int N=5e4+;
  29. const int M=N**;
  30. using namespace std;
  31. inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  32. inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  33. inline void umax(ll &p,ll q){if(p<q)p=q;}
  34. inline void umin(ll &p,ll q){if(p>q)p=q;}
  35. inline ll read()
  36. {
  37. ll x=;int f=;char ch=getchar();
  38. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  39. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  40. return x*f;
  41. }
  42. int n,m,k,t,num[],pos;
  43. ll dp[][<<][<<],p,q;
  44. ll dfs(int pos,int x,int y,int z,int k)
  45. {
  46. if(pos<)
  47. {
  48. int i;
  49. rep(i,,)if(x>>i&&&(y>>i&)^(i&)!=)return ;
  50. return ;
  51. }
  52. if(z&&k&&dp[pos][x][y]!=-)return dp[pos][x][y];
  53. int now=z?:num[pos],i;
  54. ll ret=;
  55. rep(i,,now)
  56. {
  57. ret+=dfs(pos-,!i&&!k?x:x|(<<i),!i&&!k?y:y^(<<i),z||i<num[pos],k||i);
  58. }
  59. return z&&k?dp[pos][x][y]=ret:ret;
  60. }
  61. ll gao(ll x)
  62. {
  63. pos=;
  64. while(x)num[pos++]=x%,x/=;
  65. return dfs(pos-,,,,);
  66. }
  67. int main()
  68. {
  69. int i,j;
  70. memset(dp,-,sizeof(dp));
  71. scanf("%d",&t);
  72. while(t--)
  73. {
  74. scanf("%lld%lld",&p,&q);
  75. printf("%lld\n",gao(q)-gao(p-));
  76. }
  77. return ;
  78. }

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. visual studio2013 C++查看对象布局

    一在visual studio中进行设置,可以方便的查看对象的内存布局 右键所要显示的*.cpp >> 属性 >> 命令行 >> 其它选项 在其他选项中添加: /d ...

  2. Linux gadget驱动分析1------驱动加载过程

    为了解决一个问题,简单看了一遍linux gadget驱动的加载流程.做一下记录. 使用的内核为linux 2.6.35 硬件为芯唐NUC950. gadget是在UDC驱动上面的一层,如果要编写ga ...

  3. 【POJ 1964】 City Game

    [题目链接] http://poj.org/problem?id=1964 [算法] 记f[i]表示第i行最多向上延伸的行数 然后,对于每一行,我们用单调栈计算出这一行向上延伸的最大矩形面积,取最大值 ...

  4. aspectc中this可以获取的东西

    this->kind  操作类型 this->targetName 被调用函数名称 this->funcName 调用函数名称 this->argsCount 参数个数 thi ...

  5. E20170902-hm

    devise v. 设计; 想出; 发明; 策划;   n. 遗赠; 遗赠的财产; 遗赠的条款; device n. 设备  

  6. golang单点推送

    package main import ( "encoding/json" "flag" "fmt" "log" &qu ...

  7. ROS-URDF-Xacro

    前言:Xacro是一种宏语言,允许代码复用,使用Xacro可以减少URDF文件中的代码量. 参考自:http://wiki.ros.org/urdf/Tutorials/Using%20Xacro%2 ...

  8. MySQL Connector for .NET 和 EF版本匹配问题

    以下讨论的都是EF5.0, 版本号:4.4.0.0 如果装了MySQL 5.0.1 , 那么最好用MySQL Connector 6.3.6,但是创建数据库后,生成迁移历史表的时候,会报错,你不管,直 ...

  9. 用 Django2.0 做 简单的BBS(前端用 Bootstrap)

    实现目标: 开发首页显示BBS的标题和摘要,点击BBS的标题可跳转到BBS详细页面进行展示. 开发环境及开发工具: Python 3.6.3 Django 2.0 Pycharm 2017.3 实现过 ...

  10. js视频学习笔记1

    1:数组赋值的个数长度定义无效,第4个存储的数还是能原封不动打印出来. js的数组是内部有一个变量名叫0,它的值是1,有一变量名叫1,它的值是2.是这样表示的 2:js是弱类型语言,没有var标识符, ...