BALNUM - Balanced Numbers
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的更多相关文章
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
- SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)
Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...
- SPOJ - BALNUM - Balanced Numbers(数位DP)
链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...
- SPOJ BALNUM Balanced Numbers (数位dp)
题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...
- BALNUM - Balanced Numbers(数位dp)
题目链接:http://www.spoj.com/problems/BALNUM/en/ 题意:问你在[A,B]的闭区间内有几个满足要求的数,要求为每个出现的奇数个数为偶数个,每个出现的偶数个数为奇数 ...
- SPOJ BALNUM Balanced Numbers(数位DP+状态压缩)题解
思路: 把0~9的状态用3进制表示,数据量3^10 代码: #include<cstdio> #include<map> #include<set> #includ ...
- SPOJ - BALNUM Balanced Numbers
题意: 求出所给范围内满足其数位上的奇数出现偶数次,数位上的偶数出现奇数次(或不出现)的数的个数. 思路: 对于0 ~ 9 每个数有3种情况. 1.没出现过 2.出现奇数次 3.出现偶数次 那么就可以 ...
- SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)
题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...
随机推荐
- 【POJ 1456】 Supermarket
[题目链接] http://poj.org/problem?id=1456 [算法] 贪心 + 堆 [代码] #include <algorithm> #include <bitse ...
- 【POJ 3076】 Sudoku
[题目链接] http://poj.org/problem?id=3076 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 [代码] #include <algorit ...
- bzoj 3743 [ Coci 2015 ] Kamp —— 树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3743 一开始想到了树形DP,处理一下子树中的最小值,向上的最小值,以及子树中的最长路和向上的 ...
- bzoj3957
数学+模拟 细节很多 首先我们发现,如果两个区间已经包含,那么可以输出empty,一个数能通过变换得到另一个区间的数,这个区间的大小必须小于等于终点区间的大小.加法不会改变区间大小,只有乘法会改变,而 ...
- android sdk 更新失败问题解决办法
网上几乎都是这么说的 1.设置tools下的options,然后点击菜单packages下的reload 2.然后就会出现列表在点install进行安装 但是在更新时莫名其妙的也会出现错误 解决类似这 ...
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- 应用JavaScript搭建一个简易页面图片无缝滚动效果
页面图片无缝滚动JavaScript原理:移动的区块包含图片内容,区块相对父级元素进行定位脱离文档流.再令区块的left值每隔固定的时间进行等量减少(或增大)从而实现区块的匀速运动.由于每次间隔移动的 ...
- BZOJ 2431 逆序对数列 DP
2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MB Description 对于一个数列{ai},如果有i< j且ai> ...
- Oracle 生成数据字典
SELECT ROWNUM 序号,A.COLUMN_NAME AS "字段名称",B.comments AS "字段描述", A.DATA_TYPE as 字段 ...
- P1375 嵌套矩形
题目Problem 嵌套矩形 Time Limit: 1000ms Memory Limit: 131072KB 描述Descript. 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形 ...