Balanced Numbers (数位DP)
Balanced Numbers
https://vjudge.net/contest/287810#problem/K
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 <= 10 19
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 用三进制来计算数位出现的奇偶
#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pb push_back
#define eb emplace_back
#define maxn 13000005
#define eps 1e-8
#define pi acos(-1.0)
#define rep(k,i,j) for(int k=i;k<j;k++)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,int>pli;
typedef pair<int,char> pic;
typedef pair<pair<int,string>,pii> ppp;
typedef unsigned long long ull;
const long long MOD=1e9+;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ ll dp[][];
int a[];
int k; bool Check(int x){
int num[];
for(int i=;i<;i++){
num[i]=x%;
x/=;
}
for(int i=;i<;i++){
if(num[i]==&&(i%)){
return false;
}
if(num[i]==&&(i%==)){
return false;
}
}
return true;
} int Change(int x,int v){
int num[];
for(int i=;i<;i++){
num[i]=x%;
x/=;
}
if(num[v]==) num[v]=;
else if(num[v]==||num[v]==) num[v]=;
x=;
for(int i=;i>=;i--){
x=x*+num[i];
}
return x;
} ll dfs(int pos,int st,int lead,int limit){///要去掉前导0
if(pos==-) return Check(st);
if(!limit&&dp[pos][st]!=-) return dp[pos][st];
ll ans=;
int up=limit?a[pos]:;
for(int i=;i<=up;i++){
ans+=dfs(pos-,(lead==&&i==)?:Change(st,i),lead&&i==,limit&&i==a[pos]);
}
if(!limit) dp[pos][st]=ans;
return ans;
} ll solve(ll x){
int pos=;
while(x){
a[pos++]=x%;
x/=;
}
ll ans=dfs(pos-,,,);
return ans;
} int main(){
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
int t;
ll n,m;
memset(dp,-,sizeof(dp));
cin>>t;
for(int _=;_<=t;_++){
cin>>n>>m;
ll ans=solve(m)-solve(n-);
cout<<ans<<endl;
} }
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 ...
- 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 ...
随机推荐
- Oracle查询表结果添加到另一张表中
转自:https://blog.csdn.net/lx870576109/article/details/78336695 把每一个知识点进行积累:Oracle数据库中将查询一张表的结果添加到另一张表 ...
- react-native启动页面设置,react-native-splash-screen
用于解决iOS和Android启动白屏问题及简单的启动页面展示 下载 react-native-splash-screen yarn add react-native-splash-screen re ...
- 20165304学习基础和C语言基础调查
20165304学习基础和C语言基础调查 你有什么技能比大多人(超过90%以上)更好? 我觉得自己应该算是一个比较宅的人,因为在高中的时候我在外边租房子住,我爸妈都去上班,平时就只有我自己,当然只能自 ...
- 机器学习进阶-阈值与平滑-图像平滑操作(去噪操作) 1. cv2.blur(均值滤波) 2.cv2.boxfilter(方框滤波) 3. cv2.Guassiannblur(进行高斯滤波) 4. cv2.medianBlur(进行中值滤波)
1.cv2.blur(img, (3, 3)) 进行均值滤波 参数说明:img表示输入的图片, (3, 3) 表示进行均值滤波的方框大小 2. cv2.boxfilter(img, -1, (3, ...
- day02-数据库操作
一.数据库操作 1.1.创建数据库(增) CREATE DATABASE 也可以使用小写,(注意不要漏掉分号 ;) mysql> create database test; 或 mysql> ...
- day16-小数据池
一,什么是代码块 Python程序是由代码块构造的.块是一个python程序的文本,他是作为一个单元执行的. 代码块:一个模块,一个函数,一个类,一个文件等都是一个代码块. 而作为交互方式输入的每个命 ...
- Java中,&&与&,||与|的区别
在java的逻辑运算符中,有这么四类:&&(短路与),&,|,||(短路或). &&和&都是表示与,区别是&&只要第一个条件不满足,后面 ...
- Delphi中TApplication详解(转仅供自己参考)
转自:http://blog.sina.com.cn/s/blog_4d6f55d90100bmv9.html TApplication是用于Delphi应用程序的类型,该类在单元forms中声明.T ...
- requirejs源码分析
- 16.2 profile 显示或者隐藏页面 修改密码
我们auth在clent端有更加强大的功能 显示或者隐藏component 或者 我们可以阻止或者允许某个用户访问url