[HNOI2002] Kathy 函数
数位 DP 套路题,求二进制下区间内回文串个数。
设 dp[][][] 表示到第几位时,是否为回文数,去掉前导零后共几位。之后到边界时判断是否为回文数计入贡献。
一开始不知道答案统计要高精,于是后来就自闭了。
#include <cmath>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 350;
int n, num[maxn], tmp[maxn]; char str[maxn], che[maxn][2][maxn];
class Big_integer {
private:
int len, a[105];
public:
Big_integer() { memset(a, 0, sizeof a), len = 1; }
~Big_integer() {};
inline bool operator == (const Big_integer &x) const {
if( this->len != x.len ) return false;
for(int i = len; i; --i) if( this->a[i] != x.a[i] ) return false;
return true;
}
inline bool operator < (const Big_integer &x) const {
if( this->len != x.len ) return this->len < x.len;
for(int i = len; i; --i) if( this->a[i] > x.a[i] ) return false;
return (*this == x) == false;
}
inline bool operator > (const Big_integer &x) const {
if( this->len != x.len ) return this->len > x.len;
for(int i = len; i; --i) if( this->a[i] < x.a[i] ) return false;
return (*this == x) == false;
}
inline Big_integer operator = (int x) {
memset(a, 0, sizeof a), len = 0;
while( x ) a[++len] = x % 10, x = x / 10;
return *this;
}
inline Big_integer operator + (const Big_integer &x) const {
Big_integer res;
res.len = max(this->len, x.len) + 1;
for(int i = 1; i <= res.len; ++i) {
res.a[i] = this->a[i] + x.a[i] + res.a[i];
if( res.a[i] > 9 ) res.a[i + 1] = res.a[i] / 10, res.a[i] = res.a[i] % 10;
}
while( res.a[res.len] == 0 && res.len > 1 ) --res.len;
return res;
}
inline Big_integer operator / (const int &x) const {
Big_integer res;
res.len = this->len;
for(int r = 0, i = len; i; --i) res.a[i] = (r * 10 + this->a[i]) / x, r = (r * 10 + this->a[i]) % x;
while( res.a[res.len] == 0 && res.len > 1 ) --res.len;
return res;
}
inline void read() {
scanf("%s", str + 1), len = strlen(str + 1);
for(int i = len; i; --i) a[i] = str[len - i + 1] ^ 48;
}
inline void prin() {
for(int i = len; i; --i) printf("%d", a[i]); printf("\n");
}
inline void Transform(int *arr) {
while( a[len] != 0 ) arr[++n] = a[1] & 1, *this = *this / 2;
}
} a, dp[maxn][2][maxn];
inline Big_integer Deep_fs(int fir, int pos, int tag, int limit) {
Big_integer res;
if( pos < 1 ) return (tag && fir > 0) ? res = 1 : res = 0;
if( limit == 0 && che[pos][tag][fir] ) return dp[pos][tag][fir];
for(int i = 0; i <= (limit ? num[pos] : 1); ++i) {
tmp[pos] = i;
if( fir == pos && i == 0 ) res = res + Deep_fs(fir - 1, pos - 1, tag, limit && i == num[pos]);
else res = res + Deep_fs(fir, pos - 1, (tag && pos <= (fir >> 1)) ? tmp[fir - pos + 1] == i : tag, limit && i == num[pos]);
}
if( limit == 0 ) dp[pos][tag][fir] = res, che[pos][tag][fir] = 1;
return res;
}
int main(int argc, char const *argv[])
{
a.read(), a.Transform(num), Deep_fs(n, n, 1, 1).prin();
return 0;
}
[HNOI2002] Kathy 函数的更多相关文章
- bzoj 1223: [HNOI2002]Kathy函数 数位DP 高精度
1223: [HNOI2002]Kathy函数 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 207 Solved: 90[Submit][Stat ...
- [bzoj1223] [HNOI2002]Kathy函数
首先由题解可得TAT,f(i)=i当且仅当i在二进制下为回文串. 那么问题就变成了1~n中有多少个二进制下的回文串. 把m转成2进制后就是正常的统计了= =. f[i]表示二进制下,有多少个i位的回文 ...
- 洛谷P2235 [HNOI2002]Kathy函数
传送门 题解 // luogu-judger-enable-o2 //minamoto #include<cstdio> #include<cstring> using nam ...
- 【题解】Kathy函数 [BZOJ1223] [P2235] [HNOI2002]
[题解]Kathy函数 [BZOJ1223] [P2235] [HNOI2002] 这几疯狂刷了数位\(dp\)的题,到这道题时被卡了一天,一看大佬的讲解发现居然是求回文数╮(╯_╰)╭ 感觉被大佬狠 ...
- 洛谷$P$2235 $Kathy$函数 $[HNOI2002]$ 数位$dp$
正解:数位$dp$ 解题报告: 传送门$qwq$ $HNOI$的题从02年就这么神了嘛$QAQ$,,, 嗷对了这题如果看出了一个结论就是个数位$dp$板子,,,?但是结论很神我$jio$得挺难看出来的 ...
- 数位DP学习笔记
数位DP学习笔记 什么是数位DP? 数位DP比较经典的题目是在数字Li和Ri之间求有多少个满足X性质的数,显然对于所有的题目都可以这样得到一些暴力的分数 我们称之为朴素算法: for(int i=l_ ...
- Noip前的大抱佛脚----赛前任务
赛前任务 tags:任务清单 前言 现在xzy太弱了,而且他最近越来越弱了,天天被爆踩,天天被爆踩 题单不会在作业部落发布,所以可(yi)能(ding)会不及时更新 省选前的练习莫名其妙地成为了Noi ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- SQL2005四个排名函数(row_number、rank、dense_rank和ntile)的比较
排名函数是SQL Server2005新加的功能.在SQL Server2005中有如下四个排名函数: .row_number .rank .dense_rank .ntile 下面分别介绍一下这四个 ...
随机推荐
- 【笔记】Clean Code(持续更新)
这个暑假出来实习,第一次体会到在一个团队中开发的体验,与网上的网站看到的大为不同,以前看网上说什么程序员写了屎山代码,写了一堆模糊的注释或者说垃圾代码不写注释. 但在我的实习体验中,代码虽然看起来很多 ...
- 【linux】查看GPU使用率
nvidia-smi -l 1 每秒刷新一次
- 响应国家号召 1+X 证书 Web 前端开发考试模拟题
1+x证书Web前端开发初级理论考试样题2019 http://blog.zh66.club/index.php/archives/149/ 1+x证书Web前端开发初级实操考试样题2019 http ...
- matplotlib画预测框以及打标签
https://blog.csdn.net/weixin_43338538/article/details/89003280 https://blog.csdn.net/yjl9122/article ...
- LibLog 类库 分析
前期思考: Microsoft.Logging 是否可用? 是否需要提供默认的 Logger 实现? 不需要.1,用户自己开启日志功能,设置开启属性,即可打印出相应的日志. LibLog 类库分析: ...
- Topshelf + QuartzNet 实现挂载在 WIndows Services 中的定时任务
直接贴代码了: 首先我们可以把所有的 Job 放到一个单独的 DLL 中,好处是可以共享这些业务 Job.比如我们新建一个 QuartzNetDemo.WinService.Jobs 的类库. 然后, ...
- Node.js能解决什么问题?
一.使用Node.js能解决什么问题 对于PHP.JAVA.Python等服务端语言中,为每个客户端连接创建一个新的线程,而每个线程需要大约2M的内存,理论上,具有8GB内存的服务器可以同时连接的最大 ...
- Immediate Window
name="ZFF""ZFF"date=new DateTime(2017,02,03,21,19,45){2/3/2017 21:19:45 PM} Date ...
- Windows Form父子两个窗体之间的传值测试
1:先看测试的效果图: 2:全部的代码 using System; using System.Windows.Forms; namespace WindowsForms { public partia ...
- JPA笔记1 ManyToOne
persistence.xml <?xml version="1.0" encoding="UTF-8"?> <persistence ver ...