Balanced Numbers(数位+状压)
题意:求给定区间,一个数的数位上每个奇数出现偶数次,每个偶数出现奇数次,这样数的个数
分析:先考虑状态,但总是想不全,所以要把状态压缩一下,用三进制,0 该数不放 1 放了奇数次 2放了偶数次
dp[i][j] 长度为i 状态是j的数字个数,需要前导0判断,前导0不能计入偶数出现的次数。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
ll dp[][],a,b;
int bit[],cas[];
//化三进制
void get(int x)
{
for(int i=; i<; ++i)
{
cas[i]=x%;
x/=;
}
}
//状态改变
int change(int x,int b)
{
get(x);
if(cas[b]==)
cas[b]=;
else if(cas[b]==)
cas[b]=;
else cas[b]=;
int s=,tmp=;
for(int i=; i<; ++i)
{
s+=cas[i]*tmp;
tmp*=;
}
return s;
}
//判断符合条件
int judge(int s)
{
get(s);
for(int i=; i<; ++i)
{
if(i%&&cas[i]==)return ;
if(i%==&&cas[i]==)return ;
}
return ;
}
ll dfs(int i,int j,int f,int e)
{
if(i==)return judge(j);
if(!e&&dp[i][j]!=-)return dp[i][j];
int u=e?bit[i]:;
ll num=;
for(int v=; v<=u; ++v)
{
if(f&&v==)
num+=dfs(i-,,,e&&(v==u));
else
{
num+=dfs(i-,change(j,v),,e&&(v==u));
}
}
if(!e)dp[i][j]=num;
return num;
}
ll solve(ll x)
{
int len=;
while(x)
{
bit[++len]=x%;
x/=;
}
return dfs(len,,,);
}
int main()
{
int t;
scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--)
{
scanf("%I64d%I64d",&a,&b);
printf("%I64d\n",solve(b)-solve(a-));
}
return ;
}
Balanced Numbers(数位+状压)的更多相关文章
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- spoj Balanced Numbers(数位dp)
一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...
- Balanced Numbers (数位DP)
Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...
- [Codefroces401D]Roman and Numbers(状压+数位DP)
题意:给定一个数,求将该数重新排列后mod m==0的方案数 重新排列就考虑到用到哪些数,以及此时mod m的值 于是dp[i][j]表示状态i中mod m==j的方案数 注意:转移的时候只要找到一种 ...
- spoj 10606 Balanced Numbers 数位dp
题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...
- 【Codeforces】CF 165 E Compatible Numbers(状压dp)
题目 传送门:QWQ 分析 很难想到方向,但有方向了就很easy了. 我们如何减少不必要的计算? 如果我们知道了$ 100111 $的相容的数,$ 100101 $的相容数和他是完全一样的. 我们就靠 ...
- SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)
Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...
- Codeforces Round #235 (Div. 2) D. Roman and Numbers 状压dp+数位dp
题目链接: http://codeforces.com/problemset/problem/401/D D. Roman and Numbers time limit per test4 secon ...
随机推荐
- (转载)Cocos2dx-OpenGL ES 2.0教程:你的第一个三角形(1)
前言 在本系列教程中,我会以当下最流行的2D引擎Cocos2D-X为基础,介绍OpenGL ES 2.0的一些基本用法.本系列教程的宗旨是OpenGL扫盲,让大家在使用Cocos2D-X过程中,知其然 ...
- 微软职位内部推荐-Principal DEV Manager for Bing Client
微软近期Open的职位: Title: Principal DEV Manager for Bing ClientGroup: Search Technology Center Asia, BingW ...
- 弹窗开关js
// var guanbi = false; // $("#testbtn").click(function(){ // if(guanbi){ // $("#tan&q ...
- spoj 42
简单题 水水~~ /************************************************************************* > Author: x ...
- Eclipse 插件开发 —— 深入理解查找(Search)功能及其扩展点
引言 查找功能是计算机语言开发环境 / 平台的一个非常重要的特性.Eclipse 也不例外,它提供了丰富的查找功能(用户可以输入正则表达式或任意字符串,指定查找范围和匹配选项等等),并且提供了简单易用 ...
- fork与vfork的区别
fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1. fork ():子进程拷贝父进程的数据段,代码段 vfork ( ):子进程与父进程共享数据段 ...
- php的redis 操作类,适用于单台或多台、多组redis服务器操作
redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...
- 【BZOJ 2300】 2300: [HAOI2011]防线修建 (动态凸包+set)
2300: [HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上 ...
- [itint5]最大子矩阵和
http://www.itint5.com/oj/#39 最大子矩阵和,复杂度O(n^3).利用了最大子段和的方法. int maxRectSum(vector<vector<int> ...
- if语句写在while语句外面效率更高
为了排除某些特殊的文件后缀名,一开始我自然而然的这样写,判断每一个文件的后缀名: // 去除后缀名 foreach (const QString &strKey, local_map.keys ...