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 ...
随机推荐
- 【学习总结】OS X , IOS , IOS SDK , XCode之间的关系
几个基本的概念 : OS X : 属于桌面PC级别(IMac,MacPro等)对应安装的操作系统 IOS : 属于移动设备级别(Iphone,Ipad等)对应安装的操作系统 XCode: 是一个IDE ...
- DNF技能贴图的研究
一直在猜想DNF的技能贴图怎么贴的,靠在游戏里慢慢移动确定技能的偏移太费时间了.前段发现了“可视坐标生成”这软件,针对DNF改衣服,装备款式的小工具,就自己写了个类似的. 从图上看,技能的域中心点和人 ...
- Angular与React的一些看法
Angular - React之争 Angular和React无疑是目前最受追捧的两个前端框架,谷歌也发现Angular1.x不足的地方,开始开发2.0版本,脸书发现React的组件化和虚拟DOM非常 ...
- 1056: [HAOI2008]排名系统 - BZOJ
Description 排名系统通常要应付三种请求:上传一条新的得分记录.查询某个玩家的当前排名以及返回某个区段内的排名记录.当某个玩家上传自己最新的得分记录时,他原有的得分记录会被删除.为了减轻服务 ...
- centos7 下手动安装MySQL-5.6.32-1.linux_glibc2.5.x86_64.rpm-bundle
由于centos7默认不再是mysql数据库,所以度算手动安装一个. 全程参考http://www.2cto.com/database/201501/371451.html 这里摘抄以下这个链接的内容 ...
- [转载]jquery ajax/post/get 传参数给 mvc的action
jquery ajax/post/get 传参数给 mvc的action 1.ActionResult Test1 2.View Test1.aspx 3.ajax page 4.MetaO ...
- textarea中限制输入字符长度
要在textarea中限制输入字符的长度,比如在twitter中要限制字符为140个,可实现的方法有: 1. <textarea name="A" cols="45 ...
- spoj 42
简单题 水水~~ /************************************************************************* > Author: x ...
- Samza在YARN上的启动过程 =》 之二 submitApplication
首先,来看怎么构造一个org.apache.hadoop.yarn.client.api.YarnClient class ClientHelper(conf: Configuration) exte ...
- Cloud Insight 和 BearyChat 第一次合体,好紧张!
说到 ChatOps 我们可能立刻想到是 Slack(啥?没听过?哦!),但是由于国内网络和语言的问题你可能无法拥有很好的体验了.那就把目光转回国内吧,国内的话就不得不提到 BearyChat 等 C ...