链接:

https://vjudge.net/problem/SPOJ-BALNUM

题意:

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.

思路:

三进制记录每个值用的奇数次还是偶数次。

直接DP即可。

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10; ULL a, b;
ULL F[21][60000];
int dig[21];
ULL m[11]; int Upd(int x, int p)
{
int sum = 0;
for (int i = 0;i < 10;i++)
{
int tmp = x%3;
x /= 3;
if (i == p)
sum += (tmp == 1 ? 2 : 1) * m[i];
else
sum += tmp * m[i];
}
return sum;
} bool Check(int x)
{
int p = 0;
while(x)
{
if (x%3 == 2 && p%2 == 0)
return false;
if (x%3 == 1 && p%2 == 1)
return false;
x /= 3;
p++;
}
return true;
} ULL Dfs(int pos, int sta, bool zer, bool lim)
{
if (pos == -1)
return Check(sta);
if (!lim && F[pos][sta] != -1)
return F[pos][sta];
int up = lim ? dig[pos] : 9;
ULL ans = 0;
for (int i = 0;i <= up;i++)
{
ans += Dfs(pos-1, (zer && i == 0) ? 0 : Upd(sta, i), zer && i == 0, lim && i == up);
}
if (!lim)
F[pos][sta] = ans;
return ans;
} ULL Solve(ULL x)
{
int p = 0;
while(x)
{
dig[p++] = x%10;
x /= 10;
}
return Dfs(p-1, 0, 1, 1);
} int main()
{
// freopen("test.in", "r", stdin);
m[0] = 1;
for (int i = 1;i < 11;i++)
m[i] = m[i-1]*3;
memset(F, -1, sizeof(F));
int t;
scanf("%d", &t);
while(t--)
{
scanf("%llu %llu", &a, &b);
printf("%llu\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. SPOJ BALNUM Balanced Numbers (数位dp)

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

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

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

  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. liunx 定时任务执行java程序配置流程

    java jar包使用build fat jar进行打包 ------------------liunx任务创建--------------------------- 1.查看现有任务计划: cron ...

  2. Hbase面试题

    hbase的特点 )hbase适合存储海量数据,是一个分布式的,基于列式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. )hbase 适合存储半结构化或非结构化的数据,对于 ...

  3. FireDAC 中文字段过滤问题

    当使用 FireDAC Filter  过滤数据的时候,通常这样写: FDMemTable.Filtered := False; FDMemTable1.Filter := '姓名=' + strin ...

  4. 关于fastjson与jackson在反序列化bool型时的区别

    背景 在测试中,两个项目a,b的接口参数用到了Bool类型,当传参"0",项目a通过了,项目b报错了,排查了下,项目b的那个接口,在对传参反序列化时就出现了问题,最后发现两个项目使 ...

  5. WPF 的 Application.Current.Dispatcher 中,为什么 Current 可能为 null

    原文:WPF 的 Application.Current.Dispatcher 中,为什么 Current 可能为 null 在 WPF 程序中,可能会存在 Application.Current.D ...

  6. java之hibernate之单向的一对多关联映射

    这篇主要讲hiberante中的 单向一对多关联映射 1.在应用中,有时候需要从一的一端获取多的一端的数据.比如:查看某个分类下的所有书籍信息:查看某个订单下的所有商品等. 2.在一对多的关联关系中, ...

  7. OpenJML入门

    目录 OpenJML 获取 下载 使用 Linux Windows Parsing and Type-checking Extended Static Checking Runtime Asserti ...

  8. JAVA相关知识

    1.CopyOnWrite (1).在写操作的线程,会将数组复制出来一份进行操作.而原本的数组不会做改变. (2)读线程则不会受到影响,但是可能读到的是一个过期的数据. 在juc(java.util. ...

  9. head引入样式

    引入CSS(base基础样式,index页面样式): <link rel="stylesheet" type="text/css" href=" ...

  10. Oracle 创建与管理用户

    标识用户是 Oracle 数据库管理的基本要求之一,每一个能够连接到数据库的用户都必须是系统的合法用户.用户想要使用 Oracle 的系统资源,就必须拥有相应的权限. 一.身份验证 Oracle 为用 ...