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,在做的过程中会 ...
随机推荐
- ubuntu安装openssh-server 报依赖错误的解决过程
ubuntu自带的有openssh-client,所以可以通过 ? 1 ssh username@host 来远程连接linux 可是要想通过ssh被连接,ubuntu系统需要有openssh-ser ...
- like模糊查询
1. ${}获取值,直接连接 name like '%${search_content}%' 2.CONCAT()函数 MySQL的 CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的m ...
- UVa 12100 Printer Queue(queue或者vector模拟队列)
The only printer in the computer science students' union is experiencing an extremely heavy workload ...
- iOS codeview
1.环境配置 oclint:http://oclint.org/ xcpretty:https://github.com/supermarin/xcpretty 使用Mac安装xcpretty过程可能 ...
- 使用web页面实现oracle的安装和测试
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 旋转链表(所有元素往右移) rotate list
[抄题]: 给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数 样例 给出链表1->2->3->4->5->null和k=2 返回4->5-& ...
- Swift4 - 动态计算UITableView中tableHeaderView的高度 - 获取子控件高度和宽度
核心 : /// 获取 子控件高度 func sizeHeaderToFit(view:UIView) { view.setNeedsLayout() view.layoutIfNeeded() le ...
- iOS - OC - 字典快速遍历
1. [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop ...
- [leetcode]333. Largest BST Subtree最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- TokuMX写操作无法加锁的问题
问题: TokuMX删除文档时,符合删除条件的文档很多,删除操作持续时间长,期间,服务写操作OPS大幅下降,同时日志log里发现大量“ Lock not granted. Try restarting ...