Balanced Numbers(数位dp)
Description
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记录进去,例如009,应该是9,0是没有出现过的
真正的题意是每个奇数出现的次数是偶数,每个偶数出现的次数是奇数。。注意每个。。
如123 我们只能说1是奇数出现了1次,2是偶数出现了1次,3是奇数出现了一次,不能说奇数出现了2次,偶数出现了1次
好吧,其实这道题的难点在于三进制记录状态,因为要在数和状态之间进行互相转换,哈希好像做不了。。
#include <stdio.h>
#include <string.h>
long long dp[][];
int bit[];
int updata(int x,int n)
{
int num[];
for(int i=;i<=;i++)
{
num[i]=x%;
x/=;
}
if(num[n]==) num[n]=;
else num[n]=-num[n];
int ans=,xx=;
for(int i=;i<=;i++)
{
ans+=num[i]*xx;
xx*=;
}
return ans;
}
int is_ok(int x)
{
int num[];
for(int i=;i<=;i++)
{
num[i]=x%;
if(num[i]==&&i%==) return ;
if(num[i]==&&i%==) return ;
x/=;
}
return ;
}
long long dfs(int len,int x,int flag,int cc)
{
if(len<=)
{
if(is_ok(x)) return ;
else return ;
}
if(!flag&&dp[len][x]!=-) return dp[len][x];
long long ans=,tmp;
int end=flag?bit[len]:;
for(int i=;i<=end;i++)
{
if(cc!=||len==)
{
tmp=dfs(len-,updata(x,i),flag&&i==end,);
}
else
{
if(i==)
{
tmp=dfs(len-,x,flag&&i==end,);
}
else
{
tmp=dfs(len-,updata(x,i),flag&&i==end,);
}
}
ans+=tmp;
}
if(!flag) dp[len][x]=ans;
return ans;
}
long long cacu(long long n)
{
int pos=;
if(n<) bit[++pos]=n;
else
while(n)
{
bit[++pos]=n%;
n/=;
}
return dfs(pos,,,);
}
int main()
{
memset(dp,-,sizeof(dp));
int T;
for(scanf("%d",&T);T;T--)
{
long long A,B;
scanf("%lld%lld",&A,&B);
// printf("%lld %lld\n",cacu(A-1),cacu(B));
printf("%lld\n",cacu(B)-cacu(A-));
}
return ;
}
Balanced Numbers(数位dp)的更多相关文章
- Balanced Numbers (数位DP)
Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...
- 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 Balanced Numbers(数位dp)
一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...
- spoj 10606 Balanced Numbers 数位dp
题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu3709 Balanced Number (数位dp+bfs)
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
随机推荐
- c#自定义ORM框架---(泛型&反射&实体类扩展属性<附带通用增、删、查、改>)
该教材主要是运用到泛型.反射和实体类扩展属性 步骤一.建立扩展属性类 实体类扩展属性要继承Attribute基类完成 [AttributeUsage(AttributeTargets.Property ...
- c++病毒函数
FreeConsole(); 屏蔽输出. BlockInput(); 阻止键盘和鼠标的工作. 所需头文件: #include <windows.h> #include <Winabl ...
- Linux文件详解
一.Linux文件类型分:普通文件.目录文件.链接文件.设备文件.管道文件. 1.普通文件:由ls -al显示属性时,第一个属性为 [-],例如 [-rwxrwxrwx].包括: 纯文本文件(ASCI ...
- hdu2031
http://acm.hdu.edu.cn/showproblem.php?pid=2031 #include<stdio.h> #include<math.h> #inclu ...
- DHTML_____window对象属性
<html> <head> <meta charset="utf-8"> <title>window对象属性</title&g ...
- HTTP常见面试题
转自:http://www.cnblogs.com/Java3y/p/8444033.html Http与Https的区别: Http与Https的区别: HTTP 的URL 以http:// 开头, ...
- window下编写python脚本在linux下运行出错 usr/bin/python^M: bad interpreter: No such file or directory
今天在windows下使用notepad++写了个python脚本,传到linux服务器执行后提示:-bash: ./logger.py: usr/bin/python^M: bad interpre ...
- Magento 多站点多域名安装教程(可以设置手机模版哟,亲 \(^o^)/)
这篇文章是安装magento子域名的教程,请先进行安装之前,确认以下几点: 1.请先确认子域名是否已经指向你的服务器 2.可以编辑.htaccess文件 3. 熟悉Cpanel操作 我们的目标是建立一 ...
- TF实战:(Mask R-CNN原理介绍与代码实现)-Chapter-8
二值掩膜输出依据种类预测分支(Faster R-CNN部分)预测结果:当前RoI的物体种类为i第i个二值掩膜输出就是该RoI的损失Lmask 对于预测的二值掩膜输出,我们对每个像素点应用sigmoid ...
- SpringBoot+Mybatis 自动创建数据表(适用mysql)
Mybatis用了快两年了,在我手上的发展史大概是这样的 第一个阶段 利用Mybatis-Generator自动生成实体类.DAO接口和Mapping映射文件.那时候觉得这个特别好用,大概的过程是这样 ...