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],问有多少个平衡数? ...
随机推荐
- Java图形用户界面编程
1.Java图形用户界面编程概述 JavaAPI中提供了两套组件用于支持编写图形用户界面:AWT(抽象窗口包)和Swing 2. 容器(Container):重量级容器和轻量级容器(一个容器可以放置 ...
- 5-7 第五天 微信 JS-SDK-简介
微信的SDK显然呢并不是在这个公众号里面直接使用的,而是在网页里面使用的.什么样的网页呢?就是微信内置的浏览器.你从朋友圈.从好友消息.从群消息,确定是从公众号的回复里面打开一个链接. 便会启动一个浏 ...
- Django day15 (一) cbv装饰器 , 中间件
一: 装饰器 二: 中间件
- Swagger UI改造 增加 Token验证、显示控制器注释、自定义泛型缓存应用、
/// <summary> /// Swagger 增加 Token 选项和控制器描述 /// </summary> public class CustomOperationF ...
- C99新增内容之复合文字(compound literal)
前言: 最近在复习C,发现了一些新东西,例如:变长数组,复合文字,指针的兼容性等.今天先简单谈一下复合文字. 正文: 假如需要向带有一个int参量的函数传递一个值,您可以传递一个int变量,也可以传递 ...
- 带"签名"的请求接口实现
废话少说,直接上代码(⊙﹏⊙) class Program { //签名证书 public static X509Certificate2 cerSigneCert; private static c ...
- Dotnet Core2.1 使用CodeFirst
一.添加Mysql引用 二.添加连接字符串 三.配置startup.cs 三.初始化数据库 Add-Migration init Update-Database 四.数据迁移 user实体添加了pas ...
- 【PostgreSQL-9.6.3】表操作语句
1.创建数据表 create table table_name ( 字段1 数据类型[列级别约束条件][默认值], 字段2 数据类型[列级别约束条件][默认值], 字段3 数据类型[列级别约束条件][ ...
- HDFS 处理命令记录
hdfs dfs -ls hdfs dfs -mkdir hdfs dfs -put hdfs dfs -get hdfs dfs -cat hadoop 执行jar 输出的目录 必须要不存在的 y ...
- C# 跨平台换行符 System.Environment.NewLine
C# 跨平台换行符 System.Environment.NewLine