#数位dp,高精度#洛谷 2235 [HNOI2002]Kathy函数
分析
首先这个\(f\)函数其实求的是二进制下的回文数,简单证明一下
设\(n\)在二进制下的回文数为\(n'\),第一二条显然
第三条\(f(2n)=f(n)\Rightarrow \overline {0} n'=n'\),很好证明
第四条\(f(4n+1)=2f(2n+1)-f(n)\Rightarrow \overline {10}n'=\overline{1}n'\overline{0}-n'=\overline{10}n'\)
第五条
\]
\]
得证,对于位数小于\(n\)的位数答案,易求
对于位数等于\(n\)的位数的答案,特判\(n\)是否为回文数
给出一个伪代码
inline bool mxcheck(lll m,lll len){
rr int j=len/2,i=len-j-1;
for (;i>=0&&j<len;--i,++j){
if (((m>>i)&1)>((m>>j)&1)) return 1;
if (((m>>i)&1)<((m>>j)&1)) return 0;
}
return 1;
}
signed main(){
scanf("%lld",&m);
for (len=1,n=1;n<m;++len,n=n<<1|1)
ans+=1ll<<((len-1)/2);
ans+=(m&(n>>1))>>(len>>1);
if (mxcheck(m,len)) ++ans;
printf("%lld\n",ans);
}
改成高精度就可以A了
代码
#include <cstdio>
#include <cctype>
#define rr register
#define mod 1000000000
using namespace std;
int len,Ans,a[351],ans[351],Q[101],Qlen;
inline void add_one(int *a,int now,int &len){//在二进制的某一位加1
for (;a[now];++now) a[now]=0; a[now]=1;
if (now>=len) len=now+1;
}
inline bool mxcheck(){
rr int j=len/2,i=len-j-1;
for (;i>=0&&j<len;--i,++j)
if (a[i]!=a[j]) return a[i]>a[j];
return 1;
}
inline void innput(){
rr char c=getchar();
for (;!isdigit(c);c=getchar());
for (;isdigit(c);c=getchar()){
rr int w=c^48,t=0;
if (len){
len+=3;
for (rr int j=len-1;~j;--j){
a[j]=0;
if (j>=3&&a[j-3]) add_one(a,j,len);
if (j>=1&&a[j-1]) add_one(a,j,len);
}
}
for (;w;w>>=1,++t) if (w&1) add_one(a,t,len);
}
}
signed main(){
innput();
for (rr int i=0;i<len-1;++i)
add_one(ans,i>>1,Ans);
for (rr int i=len/2,j=0;i<len-1;++i,++j)
if (a[i]) add_one(ans,j,Ans);
if (mxcheck()) add_one(ans,0,Ans);
for (rr int i=Ans-1;~i;--i){//将二进制转换为十进制
rr int g=0;
for (rr int j=0;j<Qlen;++j){
rr int s=Q[j]*2+g;
g=s/mod,Q[j]=s%mod;
}
if (g) Q[Qlen++]=g;
g=ans[i]; rr int t=0;
for (;g;++t) g=(++Q[t])/mod;
if (t>Qlen) Qlen=t;
}
printf("%d",Q[Qlen-1]);
for (rr int i=Qlen-2;~i;--i) printf("%09d",Q[i]);
return 0;
}
#数位dp,高精度#洛谷 2235 [HNOI2002]Kathy函数的更多相关文章
- 洛谷P2235 [HNOI2002]Kathy函数
传送门 题解 // luogu-judger-enable-o2 //minamoto #include<cstdio> #include<cstring> using nam ...
- bzoj 1223: [HNOI2002]Kathy函数 数位DP 高精度
1223: [HNOI2002]Kathy函数 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 207 Solved: 90[Submit][Stat ...
- 【线型DP】洛谷P2066 机器分配
[线型DP]洛谷P2066 机器分配 标签(空格分隔): 线型DP [题目] 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配 ...
- 洛谷 P3742 umi的函数
传送门 思路 \(loceaner\)已经蔡虚鲲到连红题都不会做了 因为有\(special\ judge\)所以我们就可以瞎搞了! 由题目可知,只要有一个\(y[i] > x[i]\)则一定没 ...
- 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...
- Tarjan+树形DP【洛谷P2515】[HAOI2010]软件安装
[洛谷P2515][HAOI2010]软件安装 题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得 ...
- 状压DP 【洛谷P3694】 邦邦的大合唱站队
[洛谷P3694] 邦邦的大合唱站队 题目背景 BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题. 题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶 ...
- DP练习题——洛谷P1970花匠
目录 题目描述: 输入输出格式: 输入格式: 输出格式: 输入输出样例: 输入样例: 输出样例: 题目分析: 解法一: 解法二: 结语: 题目描述: 洛谷\(P1970\) 花匠栋栋种了一排花,每株花 ...
- 【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线
P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另 ...
- 「树形DP」洛谷P2607 [ZJOI2008]骑士
P2607 [ZJOI2008]骑士 题面: 题目描述 Z 国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的 ...
随机推荐
- helloShell
初识SHELL 变量 常规的变量赋值不必多说,shell脚本还可以从命令输出中提取信息,赋值给变量 反引号字符 testing= `date` $( )格式 testing=$(date) #!/bi ...
- collections模块下的defaultdict用法
defaultdict from collections import defaultdict s=[('yellow',1),('blue', 2), ('yellow', 3), ('blue', ...
- 【Azure Function App】解决Function App For Container 遇见ServiceUnavailable的异常
问题描述 在使用Terraform创建Function App 后,部署函数时候遇见 ServiceUnavailable (Bad Request -- Encountered an error ( ...
- 【Azure 媒体服务】记录使用Java调用Media Service API时候遇见的一些问题
问题一:java.lang.IllegalArgumentException: Parameter this.client.subscriptionId() is required and canno ...
- Isito 入门(二):Istio 的部署
本教程已加入 Istio 系列:https://istio.whuanle.cn 目录 2,部署 Istio 安装 Helm 部署 istio-base 部署 istiod 部署 istio-ingr ...
- Java运行时生成类元数据,初始化注解信息的方式
问题前因 在一次技术升级中, 把分布式配置中心组件由百度的Disconf 改成 Nacos , 在对项目进行改造时, 首先将所有Disconf客户端依赖全部移除后, 依赖的封装的jar包中, 所有依赖 ...
- vscode 切换项目快捷键 Alt + Shift + P 插件 Project Manager
vscode 切换项目快捷键 Alt + Shift + P 插件 Project Manager 需求 快速切换同时打开的项目 解决方案 Alt + Shift + P 话说这个插件很早就用了,但是 ...
- linux 系统目录详解
tmpfs 的优势: 1,动态文件系统的大小. 2,tmpfs 的另一个主要的好处是它闪电般的速度.因为典型的 tmpfs 文件系统会完全驻留在 RAM 中,读写几乎可以是瞬间的. 3,tmpfs 数 ...
- LoggerMessageAttribute 高性能的日志记录
.NET 6 引入了 LoggerMessageAttribute 类型. 使用时,它会以source-generators的方式生成高性能的日志记录 API. source-generators可在 ...
- [置顶]
彻底停止运行线程池ThreadPoolExecutor
最近系统开发时遇到这样一个需求: 该功能执行时间很久,如果运行过程出现错误,也无法将其停止,必须眼睁睁的看着它浪费很久时间,除非停止服务器. 于是,我就想着如何给该功能加上一个"停止&quo ...