spoj 10606 Balanced Numbers 数位dp
一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数。
用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表示, 这样只需要开一个20*60000的数组。
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int digit[], a[];
ll dp[][];
int judge(int num) {
int cnt = ;
for(int i = ; i<; i++) {
a[i] = num%;
num/=;
}
for(int i = ; i<; i++) {
if(i%==&&a[i]==)
return ;
if(i%==&&a[i]==)
return ;
}
return ;
}
int cal(int num, int tmp) {
int cnt = ;
for(int i = ; i<; i++) {
a[i] = num%;
num/=;
}
a[tmp]++;
if(a[tmp]==)
a[tmp]=;
for(int i = ; i>=; i--) {
num = num*+a[i];
}
return num;
}
ll dfs(int len, int num, int fp, bool first) {
if(!len) {
return judge(num);
}
if(!fp&&dp[len][num]!=-) {
return dp[len][num];
}
ll ret = ;
int maxx = fp?digit[len]:;
for (int i = ; i<=maxx; i++) {
ret += dfs(len-, (first&&i==)?:cal(num, i), fp&&i==maxx, i==&&first);
}
if(!fp)
return dp[len][num] = ret;
return ret;
}
ll cal(ll n) {
int len = ;
while(n) {
digit[++len] = n%;
n/=;
}
return dfs(len, , , true);
}
int main()
{
mem1(dp);
int t;
ll a, b;
cin>>t;
while(t--) {
scanf("%lld%lld", &a, &b); //I64d会超时......
printf("%lld\n", cal(b)-cal(a-));
}
}
spoj 10606 Balanced Numbers 数位dp的更多相关文章
- 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 ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- SPOJ BALNUM Balanced Numbers (数位dp)
题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...
- SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)
Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...
- SPOJ - BALNUM - Balanced Numbers(数位DP)
链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
随机推荐
- How to Send an Email Using UTL_SMTP with Authenticated Mail Server. (文档 ID 885522.1)
APPLIES TO: PL/SQL - Version 9.2.0.1 to 12.1.0.1 [Release 9.2 to 12.1]Information in this document a ...
- 在 PL/SQL Developer 中执行SQL文件的方法
打开 command Window SQL> @'D:\My Documents\Downloads\bde_chk_cbo.sql'; 整个路径及文件两边要有单引号哦!
- statistics specify some columns count
# -k1 follow the first column nr
- mysql 全文检索的匹配问题
开发过程中碰到一个关于mysql 全文检索的问题,控制台打印的SQL语句拿到数据库里执行,结果不对.后来发现原来是少了双引号.下面是网上找到的资料,我是看到这个才意识到自己的问题. 这是之前在数据库执 ...
- Customizing Zend Studio Using the Welcome Page
Customizing Zend Studio Using the Welcome Page Zend Studio enables you to add or remove plugins from ...
- linux系统find命令使用
find命令简介 1.find的作用 find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. 2. find命令的格式 find [-path……]-option ...
- jquery判断图片是否加载完毕
来源: <http://www.2cto.com/kf/201409/331234.html> 利用图片没有加载完成的时候,宽高为0.我们很容易判断图片的一个加载情况.如下: 思路:判断图 ...
- 3D模型选中显示样式改变
osg::ref_ptr<osg::Material> material = new osg::Material(); //模型材质信息 material->setTranspare ...
- substr,substring,slice 的区别
javascript中的三个函数substr,substring,slice都可以用来提取字符串的某一部分(函数名称都是小写,不要写成subStr,subString又或者Substring,记住js ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...