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 ...
随机推荐
- centos7 更新yum安装源
系统自带的yum安装源有些软件找不到 这里我们使用阿里云的源 1.加源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/re ...
- uva 10739
dp 只有三个操作 当str[i] != str[j] 时 dp(i, j) = min(dp(i+1, j), dp(i+1, j-1), dp(i, j-1)) #include <ios ...
- Python/Ruby/Go/Node 之四国大战
Python Flask vs Ruby Sinatra vs Go Martini vs Node Express 本文授权转载自 zybuluo 博客. 题外话一: 最近一段时间,Cloud In ...
- weblogic集群无法启动,提示java.lang.NumberFormatException
我有两台weblogic9.2做的集群A,B,A是主服务器,B是受管服务器,后来通过脚本启动weblogic服务,A服务启动异常,经查后台的日志文件发现报错消息如下: WebLogic Server ...
- semantic versioning语义化版本号
语义化版本号 是由github创始人 Tom Preston-Werner 发起的一个关于软件版本号的命名规范,关于这个规范详细的说明可以在 官网 查看,也可访问其 GitHub项目页面 ,官网文档: ...
- (转)Decision Tree
Decision Tree:Analysis 大家有没有玩过猜猜看(Twenty Questions)的游戏?我在心里想一件物体,你可以用一些问题来确定我心里想的这个物体:如是不是植物?是否会飞?能游 ...
- CodePage------Encoding 类支持的编码以及与这些编码关联的代码页(CodePage)
Encoding 类 .NET Framework 4 表示字符编码. 继承层次结构 System.Object System.Text.Encoding System.Text.ASCII ...
- ASP.NET多次点击提交按钮以及Session超时和丢失过期问题
1.ASP.NET防止多次点击提交按钮 对于一个按钮,要让变成恢色的,只要this.disabled=true就可以了,可是在.NET里,添加了OnClick事件后,就无法提交信息了.所以要加上以下代 ...
- 【HDOJ】4370 0 or 1
挺有意思的题目.注意等式的条件.等式1实际表示点1的出度为1,等式2实际表示点2的入度为1,等式表示其它点为中间点,入度和出度相等.很容易转换成一条最短路.spfa直接可求,C即为邻接矩阵.同时,可能 ...
- codevs3732 解方程
%%%.设f(x)=a0+a1x+a2x^2+ - + anx^n.求f(x)=0的x. 数据范围很大,高精度只能骗分. 运用类似hash的思想. 如果这个等式mod p 还成立(p为质数)那它很可能 ...