SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
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 题意:求l-r之间13579是偶数个,24680是奇数个的数的个数 题解:状压压一下每一位是奇是偶,1表示奇,2表示偶,0表示没取
dp[pos][sta]表示第pos位之前sta的数有几个
最基础的数位DP写法
记得去一下前导零 代码如下:
#include<bits/stdc++.h>
using namespace std; int n;
long long l,r;
long long dp[][][],a[],b3[]; int gg(int x,int pos)
{
return (x%b3[pos+])/b3[pos];
} inline int check(int sta)
{
for(int i=;i<=;i+=)
{
if(gg(sta,i)==) return ;
}
for(int i=;i<=;i+=)
{
if(gg(sta,i)==) return ;
}
return ;
} long long dfs(int pos,int sta,int lim,int lim2)
{
if(pos<=) return check(sta);
if(!lim&&dp[pos][sta][lim2]!=-) return dp[pos][sta][lim2];
int up=lim?a[pos]:;
long long res=;
int nextsta;
for(int i=;i<=up;i++)
{
if(!lim2&&i==)
{
res+=dfs(pos-,sta,lim&&i==a[pos],lim2);
}
else
{
if(gg(sta,i)!=) nextsta=sta+b3[i];
else nextsta=sta-b3[i];
res+=dfs(pos-,nextsta,lim&&i==a[pos],lim2|);
}
}
if(!lim) dp[pos][sta][lim2]=res;
return res;
} long long get(long long x)
{
memset(dp,-,sizeof(dp));
int cnt=;
while(x)
{
a[++cnt]=x%;
x/=;
}
return dfs(cnt,,,);
} int main()
{
b3[]=;
for(int i=;i<=;i++) b3[i]=b3[i-]*;
scanf("%d",&n);
while(n--)
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",get(r)-get(l-));
}
}
SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)的更多相关文章
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- 【HDU】4352 XHXJ's LIS(数位dp+状压)
题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...
- spoj Balanced Numbers(数位dp)
一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...
- 【数位dp+状压】XHXJ 's LIS
题目 define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the enti ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)
传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...
- HDU.4352.XHXJ's LIS(数位DP 状压 LIS)
题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...
- Balanced Numbers (数位DP)
Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...
- CodeForces1073E 数位dp+状压dp
http://codeforces.com/problemset/problem/1073/E 题意 给定K,L,R,求L~R之间最多不包含超过K个数码的数的和. 显然这是一道数位dp,在做的过程中会 ...
随机推荐
- 一行转多行 及多行转一行的 hive语句
注意 :|,: 是特殊符号,要用 "\\|", "\\;"来表示. 一行转多行 usertags 里面有很多项,每项之间以逗号分隔 create t ...
- linux 常用find
磁盘查找文件内容: find .|xargs grep x find . -exec grep x{} \; 磁盘查找文件名称: find / -name "httpd.conf" ...
- 关于oracle数据库
Oracle数据库是做什么的? oracle数据库和其他数据库一样,都是保存数据的,同时可以去查询,修改,删除等oracle和其他数据不一样的地方在于,它又复杂的机制可以保证在数据库服务器突然坏了的情 ...
- day1:vcp考试
Q1. An administrator wants to provide users restricted access. The users should only be able to perf ...
- 在Objc项目中调用Swift
之前的文字中记录了在Swift项目中调用OC的相关代码,比较简单直接 传送门 但是在OC中调用swift代码则不是那么的和谐,网络上很多文章业已经有点陈旧.记录步骤如下: 1.创建OC项目 (1)启动 ...
- [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 【转】HttpHandler的认识与加深理解
原文:http://www.cnblogs.com/whtydn/archive/2009/10/19/1585778.html HttpHandler是HTTP请求的处理中心,真正地对客户端请求的服 ...
- win, cmd下安装mysql(win真tm难用)
常用命令: 修改root用户密码 update mysql.user set authentication_string=password('1234qwer') where user='root' ...
- Windows“储存并显示最近在开始菜单和任务栏中打开的项目”显示灰色问题解决
问题截图如下: 解决方法 打开"组策略",依次选择"用户配置"--"管理模板"--"开始菜单和任务栏"--"不 ...
- msys2 显示git branch
在.bashrc或.bash_profile中添加以下内容 function parse_git_branch () { git branch 2> /dev/null | sed -e '/^ ...