一个数字是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. keil uVision4的安装以及KEIL_Lic.exe的注冊

    1.首先毋庸置疑,在网上下载keil uVision4的EXE可运行文件,可能存在两个版本号.51核的单片机(33.3M)和微控制器开发合集(244M),可依据自己的实际须要选择.没有必要都装 2.依 ...

  2. expect 参数处理之一

    expect 参数处理之一 expect是很强悍的脚本,网上资源虽然很经典,但很少,我把自己的测试脚本贴之,仅作参考 #!/usr/bin/expect -f proc help {} { puts ...

  3. opencv做的美女找茬程序~

    // CMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <cv.h> #include <high ...

  4. Makefile条件推断 ——————————【Badboy】

    使用条件推断,能够让make依据执行时的不同情况选择不同的执行分支. 条件表达式能够是比較变量的值,或是比較变量和常量的值. 一.演示样例 以下的样例,推断$(CC)变量是否"gcc&quo ...

  5. Struts2获取演示示例教程

    回想Struts2的使用过程,网上搜的教程多多少少都会有点问题.又一次记录下创建过程,方便查阅. 1.下载Struts2的jar包 下载地址:http://archive.apache.org/dis ...

  6. Ajax请求URL后加随机数原理

    原文:Ajax请求URL后加随机数原理 例如: $.ajax({             type: "GET",    url: "login.action?ran=& ...

  7. Zeroonepack coming~^.^

    今天抓的四道DP做完了==三道是用背包做的,突然想起来背包知识点总结还没做~反正时间还早..把01背包和完全背包小结了吧~~福利来啦~~噶呜~ 01背包: 基本思路: 01背包问题是最广为人知的动态规 ...

  8. 96 Stocks APIs: Bloomberg, NASDAQ and E*TRADE

      Our API directory now includes 96 stocks APIs. The newest is the Eurex VALUES API. The most popula ...

  9. C++著名类库和C++标准库介绍

    C++著名类库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各 ...

  10. 300M无线路由器 TL-WR842N - TP-LINK官方网站

    300M无线路由器 TL-WR842N - TP-LINK官方网站 300M无线路由器TL-WR842N 11N无线技术.300Mbps无线速率 2x2MIMO架构.CCA技术,提升无线稳定性.扩大无 ...