Balanced Numbers SPOJ - BALNUM
代码+注释
1 //我感觉这道题最扯的就是我因为输入而TLE了半天,懵逼死了,想破脑袋也没想到因为输入TLE了半天
2 //题意:求区间内数字满足“奇数各数出现偶数次,偶数各数出现奇数次”的数字的个数。
3 //题解:
4 //dp[x][y]表示长度为x的时候0~9各出现的状态情况,因为可能有未出现的情况,如果这个y用二进制
5 //保存的话那么未出现的偶数可能会因为0而出现误判,所以应该多一个状态,就用三进制。0表示未出现,
6 //1表示出现了奇数次,2表示出现了偶数次。
7 //就是一道状压+数位dp,和XHXJ's LIS HDU - 4352 很相似
8 //XHXJ's LIS HDU - 4352(这个是状压成二进制+dp):https://www.cnblogs.com/kongbursi-2292702937/p/11934243.html
9
10 #include<stdio.h>
11 #include<string.h>
12 #include<algorithm>
13 #include<iostream>
14 using namespace std;
15 const int maxn=20;
16 const int N=1<<10;
17 typedef long long ll;
18 ll v[maxn],dp[maxn][60000],w[15];
19 //ll mul(ll x, ll y) {
20 // ll ret = 1;
21 // while(y) {
22 // if(y & 1) ret *= x;
23 // x *= x;
24 // y >>= 1;
25 // }
26 // return ret;
27 //}
28 //ll update(ll i,ll s) {
29 // ll ts = s, ti = i;
30 // while(ti--) ts /= 3;
31 // ll bit = ts % 3;
32 // if(bit < 2) return s + mul(3, i);
33 // return s - mul(3, i);
34 //}
35 ll update(ll x,ll y) //更新我们压缩的状态
36 {
37 ll ans[maxn],len=0;
38 memset(ans,0,sizeof(ans));
39 while(y)
40 {
41 ans[len]=y%3;
42 len++;
43 y/=3;
44 }
45 if(ans[x]==0)
46 {
47 ans[x]=1;
48 }
49 else if(ans[x]==1)
50 ans[x]=2;
51 else ans[x]=1;
52 len=max(x+1,len);
53
54 y=0;
55 x=1;
56 for(ll i=0; i<len; ++i)
57 {
58 y+=ans[i]*x;
59 x*=3;
60 }
61 return y;
62 }
63 bool get_num(ll x)
64 {
65 ll ans[maxn],len=0;
66 memset(ans,0,sizeof(ans));
67 while(x)
68 {
69 ans[len]=x%3;
70 len++;
71 x/=3;
72 }
73 bool flag=0;
74 for(ll i=0; i<len; ++i)
75 {
76 if(i==0 || i%2==0)
77 {
78 if(ans[i]!=0 && ans[i]!=1)
79 {
80 flag=1;
81 break;
82 }
83
84 }
85 else
86 {
87 if(ans[i]!=0 && ans[i]!=2)
88 {
89 flag=1;
90 break;
91 }
92
93 }
94 }
95 return flag;
96 }
97 ll dfs(ll pos,ll sta,bool limit,bool lead)
98 {
99 if(pos==-1)
100 {
101 if(!get_num(sta))
102 return 1;
103 else return 0;
104 }
105 if(!limit && dp[pos][sta]!=-1) return dp[pos][sta];
106 ll up=limit?v[pos]:9;
107 ll tmp=0;
108 for(ll i=0; i<=up; ++i)
109 {
110 if(lead && i==0)
111 {
112 tmp+=dfs(pos-1,0,limit && i==v[pos],1);
113
114 }
115 else
116 {
117
118 tmp+=dfs(pos-1,update(i,sta),limit && i==v[pos],0);
119
120 }
121 }
122 if(!limit) dp[pos][sta]=tmp;
123 return tmp;
124 }
125 ll solve(ll ans)
126 {
127 ll pos=0;
128 while(ans)
129 {
130 v[pos++]=ans%10;
131 ans/=10;
132 }
133 return dfs(pos-1,0,true,1);
134 }
135 int main()
136 {
137 int t;
138 ll l,r;
139 cin>>t;
140 memset(dp,-1,sizeof(dp));
141 while(t--)
142 {
143 cin >> l >> r;
144 cout << solve(r) - solve(l-1) << endl;
145 }
146 return 0;
147 }
Balanced Numbers SPOJ - BALNUM的更多相关文章
- 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 ...
- BALNUM - Balanced Numbers
BALNUM - Balanced Numbers Time limit:123 ms Memory limit:1572864 kB Balanced numbers have been used ...
- spoj Balanced Numbers(数位dp)
一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...
- SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)
Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
- 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 ...
- Balanced Numbers(数位dp)
Description Balanced numbers have been used by mathematicians for centuries. A positive integer is c ...
随机推荐
- 【Linux】history用法
通过history命令可以查看我们在系统中输入过的命令 history命令的一些常用参数 -c 清空内存中命令历史 -d # 删除指定的历史命令,比如 history -d 100 ,就是删除第1 ...
- 记一次centos7重启后docker无法启动的问题
问题描述 在重新了centos7系统后,docker突然就启动不了了,查看报错信息 [root@localhost ~]# systemctl status docker.service ● dock ...
- 整理目前支持 Vue 3 的 UI 组件库 (2021 年)
最近,让前端圈子振奋的消息莫过于 Vue 3.0 的发布,一个无论是性能还是 API 设计都有了重大升级的新版本.距离 Vue 3.0 正式版发布已经有一段时间了,相信相关生态周边库也正在适配新版本中 ...
- Electron入门Demo之桌面应用计算器笔记(二)
码文不易啊,转载请带上本文链接呀,感谢感谢 https://www.cnblogs.com/echoyya/p/14307996.html 在之前总结了一篇自学笔记,通过之前学习到的方法和知识,完成了 ...
- 入门OJ:八中生成树2
题目描述 八中里面有N个建设物,M条边.对于这种要建最小生成树的问题,你应该很熟练了.现在老大决定降低某条边的费用,然后这条边必须要被选中,因为这条路他每天都要走,自然......问选了这条边后是否可 ...
- uni-app开发经验分享三: Vuex实现登录和用户信息留存
在做用户登录的过程中,其实最重要的是登录成功后的数据要怎么储存,储存到哪里,这里我分享一个利用vuex来实现用户登录和用户数据留存的方法 vuex代码如下: //引入vue和vuex import V ...
- shiro的授权与认证
shiro的授权与认证 package com.cy.pj.common.aspect;import java.lang.reflect.Method;import java.util.Arrays; ...
- 为什么要使用 do while(0)?
两点 避免宏定义的花括号对代码的完整性造成影响 可以在指定的代码块中(do{})使用break提前跳出,避免goto.
- Jenkins部署springboot项目
记录jenkins如何部署springboot项目(jar类型的) 一.首先需要先配置好jenkins的基本配置(jdk.maven--),可在系统管理-->>全局工具配置中进行配置. 配 ...
- 那些我们不知道的 Python 免费学习资料
作者:小R编辑:AI 兔兔 Python 语言因为其易学,以及强大的功能,是很多刚开始学习编程的入门语言的选择之一. Python 语言被列入中小学教材后引起了越来越多人的关注. 希望孩子学习编程的家 ...