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 ...
随机推荐
- C# 申请非托管内存
方式一:使用 stackalloc 关键字 int* block = stackalloc int[100]; 注:此关键字仅在局部变量初始值设定项中有效. 以下代码导致编译器错误. int* blo ...
- jquery实现增删改(伪)-老男孩作业day13
使用jquery进行,文件的编写,实现自增id,删除,添加,编辑模式. jquery放在本地,src="jquery_js.js" 可以改成其他,或者在线的路径 readme &l ...
- ubuntu卸载福昕阅读器
在安装目录找到maintenancetool.sh运行之 ~/opt/foxitsoftware/foxitreader
- vue.js 作一个用户表添加页面----初级
使用vue.js 制作一个用户表添加页面,实际上是把原来需要使用js写的部分,改写成vue.js的格式 首先,想象一下,先做思考,我们要添加用户表,设涉及到哪些数据,一个是用户id,一个是用户名,一个 ...
- 应用DBExportDoc导出mysql库为word07文档
1.相关软件下载 DBExportDoc V1.0 For MySQL 密码:znu3 MySQL Connector/ODBC 2.安装mysql-connector-odbc并配置数据源 安装略. ...
- Oracle数据文件转移操作
由于oracle表空间数据文件规划问题导致当前数据文件所在文件系统空间不足,当其他文件系统空间充足情况下,可将数据文件移动到空间充足的文件系统下.本文主要描述Oracle表空间数据文件移动的操作步骤. ...
- 浅谈MySQL事务及隔离级别
目录 1.什么是事务 2.事务的ACID属性 2-1.原子性(Atomicity) 2-2.一致性(Consistency) 2-3.隔离性(Isolation) 2-4.持久性(Durability ...
- 16.1 用auth0服务 实现用登录和管理 使用auth版本的2个大坑。
这是三周内容,实现用户登录和管理 回到master分支 切换到 han分支 更新一下 然后工作 开始工作写代码了 安装2个angular端的auth0的lib,也可不安装,后边有不安装的做法 不安装的 ...
- arcgis python arcpy add data script添加数据脚本
arcgis python arcpy add data script添加数据脚本mxd = arcpy.mapping.MapDocument("CURRENT")... df ...
- android资讯类软件框架《IT蓝豹》
android资讯类软件框架 android资讯类软件框架,支持侧滑,并且首页viewpager切换tab,tab滑动到最右侧的时候提示滑动结束, 滑动到最左侧的时候切换滑动侧滑menu.左滑和侧滑处 ...