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 ...
随机推荐
- Java游戏之碰撞检测
在进行Java游戏开发时,我们经常会遇到碰撞检测的问题.如坦克大战中,炮弹与坦克相遇发生爆炸:守卫者游戏中,守卫者发射的箭与怪物相遇使怪物失血:打飞机游戏中,飞机发送的子弹与敌机相遇干掉敌机.这些都需 ...
- 【172】outlook邮箱设置
参考:outlook 2013设置 参考:Outlook设置hotmail邮箱POP3和SMTP服务器 注意
- hdu 1043 Eight
欸我一直以为双向bfs是搜完一半再搜另一半呢,妹想到是两个一起搜 然后队列里放的结构体里不能直接存答案,所以做一个邻接表一样的东西,直接指向需要的字符即可 记录状态用康托展开来hash 以及居然是多组 ...
- [App Store Connect帮助]六、测试 Beta 版本(3.3)管理测试员:查看测试员信息
如果您使用“TestFlight Beta 版测试”,您可以查看关于测试员的信息,并衡量测试员的参与度. 必要职能:“帐户持有人”职能.“管理”职能.“App 管理”职能.“开发者”职能或“营销”职能 ...
- 【css】回想下经典的布局
看到这张图相信大多数人都很熟悉,这曾经是一种经典的布局方式,一道经典的面试题,一般形如"实现一个布局,左右固定宽度,中间自适应".随着岁月的流转,时光的交替(颇有一种“天下风云出我 ...
- 数据传递--------博客-----------springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换
参考来源:http://blog.csdn.net/qq924862077/article/details/54286976?utm_source=gold_browser_extension Req ...
- Mui使用jquery并且使用点击跳转新窗口
网上好多朋友是这样做的: 全局插入了js代码 mui('body').on('tap', 'a', function () { document.location.href = this.href; ...
- 2017-12-01HTML块及引用
HTML块1.HTML块元素 快元素在显示时,通常会以新行开始 例如:<h1>.<p>.<ul>2.HTML内联元素 内联元素通常不会以新行开始 例如:<b& ...
- 配置服务器 Ubuntu 记录+踩坑
从零开始配置服务器用于ss+站点 1. SS 首先安装pyenv,安装pyenv之前先安装必要环境,具体命令行请见: https://github.com/pyenv/pyenv/wiki/Commo ...
- 北大ACM(POJ1017-Packets)
Question:http://poj.org/problem?id=1017 问题点:贪心. Memory: 224K Time: 32MS Language: C++ Result: Accept ...