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 ...
随机推荐
- [搜片神器]BT管理程序数据库速度调试优化问题
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 谢谢园子 ...
- ASProtect.SKE.2.11 stolen code解密
关于ASProtect.SKE.2.11 stolen code方面的文章已经很多了,今天我想再详细地说说它的细节,献给喜欢破解的兄弟们. stolen code并不可怕! ASProtect.SKE ...
- Kafka server的的停止
这算是CountDownLatch的一个典型使用场景. kafka.Kafka对象的main方法中与此有关的代码为 // attach shutdown handler to catch contro ...
- BindingFlags说明
为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static. 指定 BindingFlags.Public 可在搜索中包含公共成员. 指定 Bin ...
- highcharts 柱状图
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- Oracle10g 回收站及彻底删除table : drop table xx purge
drop后的表被放在回收站(user_recyclebin)里,而不是直接删除掉.这样,回收站里的表信息就可以被恢复,或彻底清除. 1.通过查询回收站user_recyclebin获取被删除的表信息, ...
- Linux信号列表
我们运行如下命令,可看到Linux支持的信号列表: ~$ kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL5) SIGTRAP 6) SIGABRT 7) ...
- html元素elem.style.top.left始终为空
有如下元素: <div id="div1" >div1</div> #div1{ width:100px; height:100px; position ...
- D3D游戏编程系列(一):DXLib的介绍
这篇文章里我准备向大家介绍下我封装的一个基础D3D库:DXLib.有了这样一个类库,可以减少很多无用功以及繁琐的工作,使我们的效率大大提高. DXLib.h #define DIRECTINPUT_V ...
- Git教程(6)常用技巧之远程分支简单示例
基础 1,"master" 与"origin" “master” 是当你运行 git init 时默认的起始分支名字,原因仅仅是它的广泛使用,“origin” ...