代码+注释

  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的更多相关文章

  1. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  2. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

    Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...

  3. SPOJ - BALNUM - Balanced Numbers(数位DP)

    链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...

  4. BALNUM - Balanced Numbers

    BALNUM - Balanced Numbers Time limit:123 ms Memory limit:1572864 kB Balanced numbers have been used ...

  5. spoj Balanced Numbers(数位dp)

    一个数字是Balanced Numbers,当且仅当组成这个数字的数,奇数出现偶数次,偶数出现奇数次 一下子就相到了三进制状压,数组开小了,一直wa,都不报re, 使用记忆化搜索,dp[i][s] 表 ...

  6. SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)

    Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...

  7. Balanced Numbers (数位dp+三进制)

    SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...

  8. Balanced Numbers (数位DP)

    Balanced Numbers https://vjudge.net/contest/287810#problem/K Balanced numbers have been used by math ...

  9. Balanced Numbers(数位dp)

    Description Balanced numbers have been used by mathematicians for centuries. A positive integer is c ...

随机推荐

  1. 【Linux】Linux介绍和安装 - 测试题

    第一部分测试题 Linux介绍和安装 测试题 做点练习题,巩固一下咯~ ~ _ 10 个选择题. 1.让我们选择开机时进哪个操作系统的软件叫什么? A. booter B. bootloader C. ...

  2. 【Python】国内pip节点

    pip在国内使用国内节点: http://pypi.douban.com/simple 现在已经无法使用了,新版的python3需要使用https://pypi.douban.com/simple/ ...

  3. 代码页(CodePage)列表

    代码页编号    国家地区或语言37                 IBM037    IBM EBCDIC (US-Canada)    437                 IBM437    ...

  4. SAP系统跨平台字符编码转换

    SAP系统在进行了夸平台的迁移,可能会遇到操作系统层文件编码不同,导致SAP系统无法识别或者乱码的问题.例如SAP系统从AIX平台迁移到linux平台,SAP应用服务器的编码会发生变化,从4102变化 ...

  5. 利用容器逃逸实现远程登录k8s集群节点

    某天, 某鱼说要吃瞄, 于是...... 李国宝:边缘计算k8s集群SuperEdge初体验 ​ zhuanlan.zhihu.com 图标 照着上一篇文章来说,我这边边缘计算集群有一堆节点. 每个节 ...

  6. 【ELK】elastalert 日志告警

    一.环境 系统:centos7 elk 版本:7.6.2 1.1 ElastAlert 工作原理 周期性的查询Elastsearch并且将数据传递给规则类型,规则类型定义了需要查询哪些数据. 当一个规 ...

  7. JD6621快速充电协议芯片,带有PPS 控制器的USB-PD3.0

    描述 JD6621是高度集成的USB供电(PD)控制器,支持USB PD 3.0 ,该USB PD 3.0 具有针对USBType-C下游接口(源)设计的可编程电源(PPS)规范.它监视CC引脚以检测 ...

  8. MySQL简介及安装 mysql Ver 14.14 Distrib 5.7.28

    1.MySQL简介 1.数据库产品演变 第一代数据库架构: RDBMS 关系型数据库时代 : 合的时代 代表产品 :Oracle .MSSQL .MySQL.SQL server 第二代数据库架构:拆 ...

  9. python的Counter类

    python的Counter类 Counter 集成于 dict 类,因此也可以使用字典的方法,此类返回一个以元素为 key .元素个数为 value 的 Counter 对象集合 from coll ...

  10. 网络基础知识之Cisco

    2021-01-2313:35:21 大家好,我是新手,刚学完Cisco协议,以后,我会每天都会给大家一些学习笔记 和一些学习心得,这方面不会的可以找我.qq2934896930. 网络的定义: 计算 ...