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 ...
随机推荐
- Linux系统下 为命令配置别名
1.什么是别名 在管理和维护Linux系统的过程中,将会使用到大量命令,有一些很长的命令或用法经常被用到,重复而频繁的输入某个很长命令或用法是不可取的.这时可以使用 别名 功能将这个过程简单化. Li ...
- [转]T4系列文章之3:T4语法的介绍
本文转自:http://www.cnblogs.com/damonlan/archive/2012/03/06/2382724.html 因为这段时间一直都没空,我也不知道有没有对人T4感兴趣,但不管 ...
- 预采订单管理接收来源App数据
- CF864D Make a Permutation!
思路: 贪心,构造,模拟. 实现: #include <bits/stdc++.h> using namespace std; ], a[], vis[], n; int main() { ...
- poj2502 Subway
思路: 需要注意的地方:一条地铁线路并不一定和样例描述的那样是直的:同一条线路上的两个站点步行可能更快. 实现: #include <iostream> #include <cstd ...
- sql Server与ORACLE的语法区别 自用整理!
/*整理背景201403订单中心数据库迁移(整理Oracle与SQL的差异)整理规则第一句为SQL Server 第二句为Oracle*/--数据类型int integervarchar varcha ...
- ubuntu系统中查看本机cpu和内存信息的命令和用法
https://zhidao.baidu.com/question/192966322.html 写出ubuntu linux系统中查看本机cpu和内存信息的命令和用法,以及如何解读这些命令 ubun ...
- UUID 生成32位随机串
java通过jdk自带的UUID,生成32位的随机串 private static String generate_UUID() { UUID uuid=UUID.randomUUID(); Stri ...
- http链接中请求进行编码,Http请求
如果参数中含有特殊字符&,则强制URL编码<br> http协议中参数的传输是"key=value"这种简直对形式的,如果要传多个参数就需要用“&”符号 ...
- Angular——自定义服务
基本介绍 之前我们介绍了angular内置的几种服务,这里我们介绍如何自己定义自己的服务,主要是通过三个方法:factory.service.value 基本使用 factory:可以返回对象,也可以 ...