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 ...
随机推荐
- Unity ScriptObject
http://godstamps.blogspot.com/2012/02/unity-3d-scriptableobject-assetbundle.html http://ivanozanchet ...
- bzoj 2097: [Usaco2010 Dec]Exercise 奶牛健美操【二分+树形dp】
二分答案,然后dp判断是否合法 具体方法是设f[u]为u点到其子树中的最长链,每次把所有儿子的f值取出来排序,如果某两条能组合出大于mid的链就断掉f较大的一条 a是全局数组!!所以要先dfs完子树才 ...
- bzoj 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏【模拟】
模拟 #include<iostream> #include<cstdio> using namespace std; int n,ans; int main() { scan ...
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】
二分图最大点覆盖模型,因为对于一个点(x,y)显然只要选x或者y就好了,于是连边,跑最大匹配=最大点覆盖(不会证) #include<iostream> #include<cstdi ...
- bzoj 1691: [Usaco2007 Dec]挑剔的美食家【贪心+splay】
高端贪心,好久没写splay调了好久-- 以下v为价格,w为鲜嫩度 把牛和草都按v排升序,扫草,首先把v小于等于当前草的牛都丢进splay,这样一来splay里全是可选的牛了,按w排序,然后贪心的为当 ...
- macbookpro安装Ubuntu16.04.1 LTS爬坑之旅。亲测有效(集众家之长)。安装时间为2017-11-19。
1.格式化U盘 要求:(1)切换分区格式为Mac OS扩展 (日志型):(2)方案(scheme)设置为:GUID Partition Map:如图(使用mac自带磁盘工具) 2.给Ubuntu划分磁 ...
- 如何使用 Idea 远程调试 Java 代码
起因 这几天,我做的项目中需要使用第三方的 API,在第三方的 API 回调时,出现各种错误,需要远程调试.之前做远程调试的时候,我只会在代码中输出日志,记录下来做分析处理,但这样做既麻烦又费时,往往 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Train Seats Reservation
You are given a list of train stations, say from the station 11 to the station 100100. The passenger ...
- 离散化+BFS HDOJ 4444 Walk
题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...
- WebForm vs MVC
What is ASP.NET? ASP.NET is a Microsoft’s Web application framework built on Common language runtime ...