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,在做的过程中会 ...
随机推荐
- 求数组中的逆序对的数量----剑指offer36题
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数: 如数组{7,5,6,4},逆序对总共有5对,{7,5},{7,6},{7, ...
- Retrofit2+Rxjava2 okhttp RxBus 使用记录
学习 博客 http://blog.csdn.net/r17171709/article/details/51149350 @Query 后面跟要添加的字段 @Path 连接url里面{userId} ...
- Hibernate 序列生成主键
@Entity @Table(name="TABLE_NAME") public class TableName implements Serializable { @Id @Ge ...
- POJ1950----DFS
Dessert Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6193 Accepted: 2299 Descripti ...
- 慕课网access_token的获取
access_token的接口是微信公众号一个基础接口,access_token接口微信公众号一个非常重要的接口 access_token是微信公众号的全局唯一票据,公众号的所有接口的调用都需要使用到 ...
- Python issubclass() 函数
Python issubclass() 函数 Python 内置函数 描述 issubclass() 方法用于判断参数 class 是否是类型参数 classinfo 的子类. 语法 以下是 iss ...
- DAO层注入HibernateTemplate的两种方式
-------------------------siwuxie095 DAO 层注入 HibernateTemplat ...
- VMware安装win7:units specified don't exist问题
主要是磁盘接口不匹配,调整CD/DVD和硬件磁盘接口, CD/DVD调整成IDE,硬盘调整成SATA即可. 提示system not found,主分区没有激活,进入disgenius,会提示修正,保 ...
- python之socket运用1
先看下服务端的代码 import socket ip_bind = ("127.0.0.1",3000) sk = socket.socket() sk.bind(ip_bind) ...
- 用SQL2000还原bak文件
1.右击SQL Server 2000实例下的“数据库”文件夹.就是master等数据库上一级的那个图标.选择“所有任务”,“还原数据库” 2.在“还原为数据库”中填上你希望恢 复的数据库名字.这个名 ...