一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次

一下子就相到了三进制状压,数组开小了,一直wa,都不报re,

使用记忆化搜索,dp[i][s] 表示长度为i,状态为s,时,满足条件的balance number的个数

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
const int INF = <<;
/* */
int num[];
LL dp[][]; int getNum(int sta)
{
int tmp1, tmp2;
for (int i = ; i <= ; ++i)
{
tmp1 = i &;
tmp2 = sta % ;
if (tmp1== && tmp2==)//偶数出现了偶数次
return ;
if (tmp1 == && tmp2 == )//奇数出现了奇数次
return ;
sta /= ;
}
return ;
} int getSta(int x, int sta)
{
int val = ;
int newSta = sta;
for (int i = ; i < x; ++i)
{
val *= ;
sta /= ;
}
int times = sta % ;
if (times == || times == )
return newSta + val;
else
return newSta - val;
}
LL dfs(int pos, int sta, bool zero, bool flag)
{
if (pos == ) return getNum(sta);
if (!flag && dp[pos][sta] != -) return dp[pos][sta];
int end = flag ? num[pos] : ;
LL ans = ;
for (int i = ; i <= end; ++i)
ans += dfs(pos - , (zero&&i==)?:getSta(i, sta),zero&&i== ,flag&&i == end);
if (!flag)
dp[pos][sta] = ans;
return ans;
}
LL calc(LL n)
{
int len = ;
while (n)
{
num[++len] = n % ;
n /= ;
}
return dfs(len, , true, true);
}
int main()
{
memset(dp, -, sizeof(dp));
int t;
LL l, r;
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld", &l, &r);
printf("%lld\n", calc(r) - calc(l - ));
}
return ;
}

spoj 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. Balanced Numbers (数位DP)

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

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

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

  4. spoj 10606 Balanced Numbers 数位dp

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

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  6. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  7. hdu3709 Balanced Number (数位dp+bfs)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  8. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  9. 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 ...

随机推荐

  1. Vue ES6

    Vue ES6 Jade Scss Webpack Gulp   一直以来非常庆幸曾经有翻过<代码大全2>:这使我崎岖编程之路少了很多不必要的坎坷.它在软件工艺的话题中有写到一篇:“首先是 ...

  2. 4种Delphi IDE的调试时查看内存的方法,太酷了!

    1.ctrl+alt+m,可以查看每个函数过程的内存位置 2.Ctrl+Alt+C 查看代码对应的汇编 3.原来用delphi看变量信息一直是简单的用watch看,但是有时候变量值直接用特定类型看总是 ...

  3. Linux网络基础配置

    这是看itercast视频的笔记 Linux网络基础配置 以太网连接 在Linux中,以太网接口被命令为:eth0, eth1等, 0,1代表网卡编号 通过lspci命令可以查看网上硬件信息(如果是u ...

  4. Mac OS升级到Yosemite后一些问题

    苹果"优山美地"採用移动设备平面风格,看起来还是相当清爽. 只是升级完还是有一些程序兼容性问题的. 1. 开发Android的程序猿们,Java se 6 须要升级到2014_00 ...

  5. hadoop format过程

    private static boolean format(Configuration conf, boolean isConfirmationNeeded ) throws IOException ...

  6. 怎样用Java编写一段代码引发内存泄露

    通过下面步骤能够非常easy产生内存泄露(程序代码不能訪问到某些对象,可是它们仍然保存在内存中): 应用程序创建一个长时间执行的线程(或者使用线程池,会更快地发生内存泄露). 线程通过某个类载入器(能 ...

  7. 设置Mysql的连接超时参数

     在Mysql的默认设置中,如果一个数据库连接超过8小时没有使用(闲置8小时,即   28800s),mysql server将主动断开这条连接,后续在该连接上进行的查询操作都将失败,将   出现:e ...

  8. touch修改文件的修改时间和访问时间,ls --full-time显示文件详细,stat命令

    1. 同时修改文件的修改时间和访问时间 touch -d "2010-05-31 08:10:30" test.doc 2. 只修改文件的修改时间 touch -m -d &quo ...

  9. 无状态TCP的ip_conntrack

    Linux的ip_conntrack实现得过于沉重和精细.而实际上有时候,根本不需要在conntrack中对TCP的状态进行跟踪,只把它当UDP好了,我们的需求就是让系统可以将一个数据包和一个五元组标 ...

  10. hdu4513(manacher)

    传送门:吉哥系列故事——完美队形II 题意:求最长回文队伍且队伍由中间向两边递减. 分析:manach算法小应用,在判断回文子串向两边递减时加点限制使回文是由中间向两边递减的. #pragma com ...