SPOJ - BALNUM Balanced Numbers
题意:
求出所给范围内满足其数位上的奇数出现偶数次,数位上的偶数出现奇数次(或不出现)的数的个数。
思路:
对于0 ~ 9 每个数有3种情况。
1.没出现过 2.出现奇数次 3.出现偶数次
那么就可以用三进制来表示状态。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = ;
int t;
int bits[];
ll l, r;
ll dp[][N];
ll judge(ll x) {
for(int i = ; i <= ; i++, x/=)
if(x% == (i+)%+) return ;
return ;
}
ll change(ll x, int pos) {
int j = pos;
ll res = x;
while(j--) res /= ;
res = res%;
if(res < ) x += (int)pow(, pos);
else x -= (int)pow(, pos);
return x;
}
ll dfs(int pos, int state, bool limit) {
if(pos < ) return judge(state);
if((!limit) && (~dp[pos][state])) return dp[pos][state];
int up = limit?bits[pos]:;
ll res = ;
for(int i = ; i <= up; i++)
res += dfs(pos-, state==&&i==?:change(state, i), limit && i==up);
if(!limit) dp[pos][state] = res;
return res;
}
ll solve(ll x) {
int pos = ;
while(x) {
bits[pos++] = x%;
x /= ;
}
return dfs(pos-, , );
}
int main() {
memset(dp, -, sizeof(dp));
scanf("%d", &t);
while(t--) {
scanf("%lld%lld", &l, &r);
printf("%lld\n", solve(r) - solve(l-));
}
}
SPOJ - BALNUM Balanced Numbers的更多相关文章
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- 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 ...
- SPOJ BALNUM Balanced Numbers (数位dp)
题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...
- SPOJ BALNUM Balanced Numbers(数位DP+状态压缩)题解
思路: 把0~9的状态用3进制表示,数据量3^10 代码: #include<cstdio> #include<map> #include<set> #includ ...
- SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)
题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...
- BALNUM - Balanced Numbers
BALNUM - Balanced Numbers Time limit:123 ms Memory limit:1572864 kB Balanced numbers have been used ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
- BALNUM - Balanced Numbers(数位dp)
题目链接:http://www.spoj.com/problems/BALNUM/en/ 题意:问你在[A,B]的闭区间内有几个满足要求的数,要求为每个出现的奇数个数为偶数个,每个出现的偶数个数为奇数 ...
随机推荐
- jQuery 效果使用
.hide() 隐藏匹配的元素. .hide() 这个方法不接受任何参数. .hide([duration][,complete]) duration 一个字符串或者数字决定动画将运行多久. comp ...
- MappingException:class com.zsn.crm.Model.user not found whie looking for property user id
之前好好地运行 什么东西都没动过 再次运行突然报异常*****MappingException:class com.zsn.crm.Model.user not found whie looking ...
- python 计算提成代码
while True: with open('8564.txt') as f: r = f.readlines() start = input("请输入要查询的日期,例如20180101 : ...
- spring-JDBC Template
JDBC Template概念 为简化持久化操作,spring在JDBC API之上提供JDBC Template组件 提供统一模板: 环境配置 1.创建MySQL数据库 2.搭建maven项目,并引 ...
- JS - 兼容的事件助手
(function () { // 兼容的事件助手 window.CompatibleEventHelper = { addEventListener: function (elem, type, c ...
- django+xadmin在线教育平台(七)
4-3 新建项目 Python2.7 创建虚拟环境. mkvirtualenv mxonline2 安装django pip install django==1.9.8 注意Python2下此处必须用 ...
- 即将开始的python之路
准备开始学py 记录一下 加油
- scrapy--cnblogs
之前一直在学习关于滑块验证码的爬虫知识,最接近的当属于模拟人的行为进行鼠标移动,登录页面之后在获取了,由于一直找不到滑块验证码的原图,无法通过openCV获取当前滑块所需要移动的距离. 1.机智如我开 ...
- dts--tests(一)
cmdline.py """ DPDK Test suite. Test cmdline. """ import utils from te ...
- php-语言参考-基本语法3.1
一,PHP代码的开始和结束标记 1,<?php 和 ?> //重点 2,<script language="php"> 和 </script> ...