题目:http://www.spoj.com/problems/BALNUM/en/

题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数。

分析:

  明显的数位dp题,首先,只有3种状态(0:没出现过, 1:数字出现奇数次, 2:数字出现偶数次),所以, 0~9 出现的次数就可以用3进制表示,最大的数就是 310 ,那么我们就可以把1019 哈希到310 内了。其中,我们可以假设:

(0:30  ,1:31 , 2:32 , .... , 9: 39

  当第一次出现是,就把次数 +1, 否则,奇数变偶数(1-->2),偶数变奇数(2-->1)。因此,每个数字的变化在0~2内,3进制不会有冲突产生。

  设记忆化数组f[20][310], 就是f[i][s] 表示取了前 i 位数字哈希后值为 s 的方法数。

代码:

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
typedef unsigned long long ull;
const int N = ; ull f[N][];
int dg[N]; int check(int s) {
int nu[];
for(int i = ; i < ; ++i) {
int k = s % ;
s /= ;
if(!k) continue;
if((i&) && (k==)) return ;
if(!(i&) && (k==)) return ;
}
return ;
} int new_s(int d, int s) {
int nu[];
for(int i = ; i < ; ++i, s /= ) nu[i] = s % ; if(nu[d] == ) nu[d] = ;
else nu[d] = - nu[d];
for(int i = ; i > -; --i) s = s * + nu[i];
return s;
} ull dfs(int i, int s, bool flag, bool e) {
if(i == -) return check(s);
if(!e && ~f[i][s]) return f[i][s];
int res = ;
int u = e ? dg[i] : ;
for(int d = ; d <= u; ++d) {
res += dfs(i-, (flag==&&d==) ? : new_s(d, s), flag||d>, e&&d==u);
}
return e ? res : f[i][s] = res;
} ull solve(ull x) {
int len = ;
for( ; x; x /= ) dg[len++] = x % ;
return dfs(len-, , , );
} int main()
{
int T;
scanf("%d", &T);
ull a, b;
memset(f, -, sizeof f);
while(T--) {
scanf("%llu %llu", &a, &b);
printf("%llu\n", solve(b)-solve(a-));
}
return ;
}

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. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

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

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

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

  6. spoj Balanced Numbers(数位dp)

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

  7. Balanced Numbers (数位DP)

    Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...

  8. SPOJ BALNUM Balanced Numbers(数位DP+状态压缩)题解

    思路: 把0~9的状态用3进制表示,数据量3^10 代码: #include<cstdio> #include<map> #include<set> #includ ...

  9. SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)

    题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...

随机推荐

  1. 使用socket发送http请求(get/post)

    手动发送http请求 解释说明 https://blog.csdn.net/zhangliang_571/article/details/23508953 http://www.cnblogs.com ...

  2. Grunt 5分钟上手:合并+压缩前端代码

    Grunt 的各种优点这里就不扯了,对于 新手来说 合并(concat) + 压缩(uglify) 前端代码的需求量应该是最大的,这里以这俩种功能为主做一个5分钟的入门吧! 工作环境 $ node - ...

  3. 2017年软件工程作业-“Hello World!”团队互评beta版本

    A.欢迎来怼——博客园安卓APP(测评人:刘淑霞) 博客地址:http://www.cnblogs.com/liusx0303/p/7905928.html B.Thunder——爱阅app(测评人: ...

  4. linux 常用命令-ps命令

    ps(process status):进程状态相关命令 1.

  5. BufferedWriter与BufferedRead --------------------------Test2

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  6. “吃神么,买神么”的第一个Sprint计划(第五天)

    “吃神么,买神么”项目Sprint计划 ——5.25  星期一(第五天)立会内容与进度 摘要:logo2出来了,修改过不一样的风格,组内总体评价可以,但是颜色要改,色调没注意,统一决定改成与背景色一致 ...

  7. keil c51笔记

    第一章 Keil C51开发系统基本知识 第一节 系统概述 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性. ...

  8. *** error 65: access violation at C:0x001B : no 'execute/read' permission

    转自:http://blog.csdn.net/chenqiai0/article/details/7827071 很多人在进行串口调试的时候会遇到这个问题,请大家略看我的代码,解决方法在其中 //实 ...

  9. Robot Framework 教程 (4) - 自定义Library

    RobotFrame Work为我们提供了包括OS.Android.XML.FTP.HTTP.DataBase.Appium.AutoIt.Selenium.Watir等大量的库.在使用过程中,除这些 ...

  10. vue 组件 子向父亲通信用自定义方法用事件监听

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...