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 ...
随机推荐
- MSD6A628开发资料与技术支持
MSD6A628VX/VXM是mstar推出的低成本安卓智能网络电视方案,628分两个版本,一个是内置512MB内存,一个是外挂1G内存, 安卓4.4系统,4核A7处理器,Mali450MP2 GPU ...
- bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会【树形dp】
不能用read会TLE!!不能用read会TLE!!不能用read会TLE!! 一开始以为要维护每个点,线段树写了好长(还T了-- 首先dfs一遍,求出点1为集会地点的答案,处理处val[u]为以1为 ...
- javascript实现页面跳转
这里指的页面跳转是将浏览器标签页转到新的网址. 只需要使用 window.location.href="url" 就行了 示例 <!DOCTYPE html> < ...
- 51NOD 1088 最长回文子串&1089 最长回文子串 V2(Manacher算法)
回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度. Input 输入Str(Str的长度 <= 1000(第二题要 ...
- ASP.NET MVC5 之 分部页
1.分部页 _PartialPage.cshtml @model List<string> <a>完美世界</a> @foreach (var item in Mo ...
- ACM_N皇后问题
N皇后问题 Time Limit: 2000/1000ms (Java/Others) Problem Description: 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不 ...
- 392 Is Subsequence 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...
- 382 Linked List Random Node 链表随机节点
给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样.进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现?示例:// 初始化一个单链表 ...
- 297 Serialize and Deserialize Binary Tree 二叉树的序列化与反序列化
序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据.请设计一个算法来实现二叉树 ...
- sql 所有数据表中 插入字段
declare @tablename varchar(200)declare @sql varchar(2000)declare cur_t cursor forselect name from sy ...