数位dp(Balanced Numbers )
题意:一个数,如果满足奇数的数字出现偶数次,偶数的数字出现奇数次,
就是符合的数,注比如:12313 就满足,因为1 3出现了偶数次。2出现了奇数次
思路,对于这道题,就是状态压缩加dp;
对于一个数字来说,0~9每一位,都只有3种状态量,要么从未出现,要么出现次数为奇
要么为偶,所以可以用3进制来表示,通过3进制表示出其状态,然后到pos-1的时候
判断0~9的各个状态是否符合条件即可
#include<cstdio>
#include<string.h>
using namespace std;
typedef long long ll;
ll dp[][];
ll a[];
int check(int s)
{
for(int i=;i<=;++i){
int k=s%;
s/=;
if(k==) continue;
else if((i&)&&k==) return ; //若为奇数,如果出现次数也为奇,则退出;
else if(!(i&)&&k==) return ; //若为偶数,。。。。。。。。。。。。。。
}
return ;
}
//三进制代码;
int change(int d,int s)
{
int temp[];
for(int i=;i<=;i++){
temp[i]=s%;
s/=;
}
s=;
if(temp[d]==) temp[d]=; //表示其状态的代码
else temp[d]=-temp[d]; //表示其状态的代码
for(int i=;i>=;i--)
s=s*+temp[i];
return s;
} ll dfs(ll pos,ll sum,bool flag,bool limit)
{
//flag是判断前导0用的
if(pos==-) return check(sum);
if(!limit&&dp[pos][sum]!=-) return dp[pos][sum];
int up=limit? a[pos]:;
ll ans=;
for(int i=;i<=up;i++)
//如果之前一直都是0,i==0,则sum的值依旧等于0
ans+=dfs(pos-,(flag==&&i==)? :change(i,sum),flag||i>,limit&&i==a[pos]);
if(!limit) dp[pos][sum]=ans; //如果i已经大于0,则再也没有前导0这一说法
return ans;
}
ll solve(ll x)
{
int pos=;
while(x){
a[pos++]=x%;
x/=;
}
return dfs(pos-,,,true);
}
int main()
{
int T;
scanf("%d",&T);
memset(dp,-,sizeof(dp));
while(T--){
ll l,r;
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-));
}
return ;
}
数位dp(Balanced Numbers )的更多相关文章
- Balanced Numbers (数位dp+三进制)
SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...
- Balanced Numbers (数位DP)
Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
- Balanced Numbers(数位dp)
Description Balanced numbers have been used by mathematicians for centuries. A positive integer is c ...
- spoj Balanced Numbers(数位dp)
一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...
- 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题, ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
随机推荐
- centos 7 安装 Vue
一.安装npmyum install -y npm 二.更新组件yum update openssl 三.安装Vue最新稳定版本npm install vue最新稳定 CSP 兼容版本npm inst ...
- gulp常用插件之gulp-filter使用
更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-filter这是一款可以把stream里的文件根据一定的规则进行筛选过滤. 更多使用文档请点击访问gulp-filter工具官网. 安装 ...
- sublime text3插件使用
sublime text 使用BUG解决 一.安装emmet.Package Control插件ctrl+shift+p -> install package ->emmet(Zen Co ...
- AMC Problems and Solutions
AMC Problems and Solutions:https://artofproblemsolving.com/wiki/index.php/AMC_Problems_and_Solutions ...
- java回文代码
import java.util.*; import java.math.BigInteger; import java.util.Scanner; public class Test{ static ...
- Linux connect: Network is unreachable
在虚拟机中ping,发现网络不通: [root@node01 ~]# ping 114.114.114.114 connect: Network is unreachable 发生此问题时,环境如下: ...
- Mysql注入汇总!!!!!!!!!
师傅tpl!!!!! https://xz.aliyun.com/t/7169[对MYSQL注入相关内容及部分Trick的归类小结] https://www.jianshu.com/p/f261125 ...
- pytest-pytest-html生成HTML测试报告
pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github.com ...
- [CodeIgniter4]讲解-加载静态页
讲解 本教程旨在向您介绍CodeIgniter框架和MVC体系结构的基本原理.它将向您展示如何以逐步的方式构造基本的CodeIgniter应用程序. 在本教程中,您将创建一个基本的新闻应用程序.您将从 ...
- 使用pycharm搜索框和正则表达式匹配内容
使用pycharm搜索框和正则表达式匹配内容原创薯饼__ 最后发布于2019-03-04 19:28:53 阅读数 660 收藏展开有时候我们要在爬虫网站中匹配各种各样的文本块,以获取需要的信息,每一 ...