一个数字是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. linux下编译原理分析

    linux下编译hello.c 程序,使用gcc hello.c,然后./a.out就能够执行:在这个简单的命令后面隐藏了很多复杂的过程,这个过程包含了以下的步骤: ================= ...

  2. 开发指南专题八:JEECG微云高速开发平台数据字典

       开发指南专题八:JEECG微云高速开发平台数据字典的使用 1.标签中使用数据字典 数据字典为系统中可能用到的字典类型数据提供了使用的便利性和可维护性.下面拉框标签<t:dictSele ...

  3. ZigBee研究之旅(一)

    *********************************************************************** 以下有引用webee公司的文档的内容,版权属于webee ...

  4. Swift - 几种使用数组的数据存储模型

    在iOS游戏开发中,比如2048游戏.有时会需要存储N×N数组的数据模型(如3×3,4×4等).这里我们演示了三种实现方式,分别是:一维数组.仿二维数组.自定义二维数组(即矩阵结构). 功能是根据传入 ...

  5. 插件 - 提示窗体(ArtDialog)

    效果: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...

  6. Android studio导入Eclipse项目,和一些错误的解决

    Android studio导入Eclipse开发的项目步骤如下 如果已经打开Android studio的话就选择你已打开的项目,关闭然后导入 开始导入 导入完成. 2.项目出错 Error:(13 ...

  7. Java实现定时任务的三种方法(转)

    在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...

  8. leetcode 最长连续序列 longest consecutive sequence

    转载请注明来自souldak,微博:@evagle 题目(来自leetcode): 给你一个n个数的乱序序列,O(N)找出其中最长的连续序列的长度. 例如给你[100, 4, 200, 1, 3, 2 ...

  9. 使用Android简单实现有道电子词典

    前言: 毕业设计的内容,仅仅有Java基础.没学过Android. 本着用到什么学什么.花费了10多个晚上完毕毕业设计. 当然,仅仅是简单的实线了电子词典功能,自始至终没有考虑过性能等问题. 本电子词 ...

  10. [Android学习笔记]获取view的尺寸和坐标

    对于UI方面很多时候需要获取它的很多信息,具体情况见view的文档 View文档 http://developer.android.com/training/index.html 常用方法:获取vie ...