一个数字是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. PreferenceActivity使用示例

    MainActivity如下: package cn.testpreferenceactivity; import android.content.SharedPreferences; import ...

  2. 使用elk+redis搭建nginx日志分析平台(转)

    logstash,elasticsearch,kibana 怎么进行nginx的日志分析呢?首先,架构方面,nginx是有日志文件的,它的每个请求的状态等都有日志文件进行记录.其次,需要有个队列,re ...

  3. Effective C++ 24,25

    24.在函数重载和设定參数缺省值间要谨慎选择. 获得一种类型的数据的最小值或最大值,对于c中,一般使用在<linits.h>中定义的各种宏如INT_MIN 来进行表示,可是这样无法进行泛型 ...

  4. !!!!!安卓界面总是显示载入进度条的问题,没事别乱用ListFragment

    近期的安卓应用快完工了.可是个别界面有瑕疵,怎么改动都解决不了.挥之不去的阴影.问题例如以下: 在界面上,数据已经载入完成了.可还是有一股圆形的进度条,感觉它老是在载入什么东西,严重影响界面美观: 1 ...

  5. 超声波模块SRF05

    //////////////////////////////////////////////////////////////////////////////// // //     PIC16F877 ...

  6. [Cocos2d-x]博客推荐

    推荐一下大神们的博客: JackyStudio: http://blog.csdn.net/jackyvincefu/article/category/1591201/3 老G的小屋: http:// ...

  7. Linux与JVM的内存关系分析(转)

    引言 在一些物理内存为8g的服务器上,主要运行一个Java服务,系统内存分配如下:Java服务的JVM堆大小设置为6g,一个监控进程占用大约600m,Linux自身使用大约800m.从表面上,物理内存 ...

  8. bzoj2301(莫比乌斯反演+分块)

    传送门:2301: [HAOI2011]Problem b 题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y ...

  9. Amazon.com : The Odyssey of the Manual Toothbrusher

    Amazon.com : The Odyssey of the Manual Toothbrusher The Odyssey of the Manual Toothbrusher

  10. 金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on Xanga

    金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on X ...