思路:

把0~9的状态用3进制表示,数据量3^10

代码:

#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
const int N = 500+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int dp[22][60000]; //0 没出现,1 奇数,2 偶数
int dig[22];
int check(int sta){
for(int i = 0;i <= 9;i++){
int tmp = sta % 3;
sta /= 3;
if(i % 2 == 1 && tmp == 1) return 0;
else if(i % 2 == 0 && tmp == 2) return 0;
}
return 1;
}
int news(int sta,int x){
int num[10];
for(int i = 0;i <= 9;i++){
num[i] = sta % 3;
sta /= 3;
}
int tmp = 0;
if(num[x] == 0) num[x] = 1;
else num[x] = 3 - num[x];
for(int i = 9;i >= 0;i--){
tmp = tmp*3 + num[i];
}
return tmp;
}
ll dfs(int pos,int sta,bool lead,bool limit){
if(pos == -1) return check(sta);
if(!limit && dp[pos][sta] != -1) return dp[pos][sta];
int top = limit? dig[pos] : 9;
ll ret = 0;
for(int i = 0;i <= top;i++){
int STA = news(sta,i);
if(lead && i == 0) STA = 0;
ret += dfs(pos - 1,STA,lead && i == 0,limit && i == top);
}
if(!limit) dp[pos][sta] = ret;
return ret;
}
ll solve(ll x){
int pos = 0;
if(x == 0) return 1;
while(x){
dig[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1,0,true,true);
}
int main(){
int T;
ll a,b;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
while(T--){
scanf("%lld%lld",&a,&b);
printf("%lld\n",solve(b) - solve(a - 1));
}
return 0;
}

SPOJ BALNUM Balanced Numbers(数位DP+状态压缩)题解的更多相关文章

  1. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  2. SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)

    Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...

  3. spoj 10606 Balanced Numbers 数位dp

    题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...

  4. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. hdu_4352_XHXJ's LIS(数位DP+状态压缩)

    题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...

  6. SPOJ BALNUM Balanced Numbers (数位dp)

    题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...

  7. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

    Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...

  8. SPOJ - BALNUM - Balanced Numbers(数位DP)

    链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...

  9. spoj Balanced Numbers(数位dp)

    一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...

  10. hdu 4352 XHXJ's LIS 数位dp+状态压缩

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others ...

随机推荐

  1. Egret3D学习笔记一 (Unity插件使用)

    一 官方教程: http://developer.egret.com/cn/github/egret-docs/Engine3D/getStarted/getStarted/index.html 大部 ...

  2. Ext学习系列(1)初识Ext

    Ext 基础页设置: <!-- 最好不要设置的文件头,避免IE的问题 --><html> <!-- 每份有效的HTML应符合标准,为此head标签不能少 -->&l ...

  3. VMware Fusion 5虚拟机怎样与MAC共享文件

    刚刚在Macbook Pro中安装了VMware Fusion 5虚拟机,虚拟机里装了Windows7,在虚拟机的设置里也设置了共享MAC的几个文件夹,以便与MAC交换文件,但是在Windows7里怎 ...

  4. 扫描类APP推荐

    扫描全能王 (com.intsig.camscanner) - 5.10.0.20190426 - 应用 - 酷安网 应该是手机上最好的扫描类 APP 了,没有之一.只是因为付费太贵. 感谢酷安评论区 ...

  5. SpringCloud--Ribbon负载均衡

    Ribbon实现客户端负载均衡 负载均衡:是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一. 硬件负载均衡:主要通过在服务器节点之间安装专门用于负载均衡的设备: 软件负载均衡:通过在服务器 ...

  6. CH5E09 能量相连【区间DP】

    5E09 能量项链 0x5E「动态规划」练习 描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且, ...

  7. LINEAR HASH Partitioning

    MySQL :: MySQL 8.0 Reference Manual :: 23.2.4.1 LINEAR HASH Partitioning https://dev.mysql.com/doc/r ...

  8. java-mybaits-010-mybatis-spring-使用 SqlSession、注入映射器

    一. SqlSession概述 在 MyBatis 中,你可以使用 SqlSessionFactory 来创建 SqlSession.一旦你获得一个 session 之后,你可以使用它来执行映射语句, ...

  9. CSLA.Net学习(2)

    采用CSLA.net 2.1.4.0版本的书写方式: using System; using System.ComponentModel; using Csla.Validation; using S ...

  10. 文本IO 二进制IO

    一.文本IO  字符流 使用PrintWriter写入文件后,必须调用close(),否则数据不能正确保存在文件中. Scanner的next()读取一个由分隔符分隔的字符串,nextLine()读取 ...