Palindromic Numbers LightOJ - 1205 数位dp 求回文数

传送门

文章目录

  • 题意:
  • 思路:

题意:

[l,r][l,r][l,r]中有多少个回文数。

思路:

裸的数位dpdpdp啦,记dp[pos][pre][state]dp[pos][pre][state]dp[pos][pre][state]表示到了第pospospos位,回文是从第preprepre位开始的,并且当前是否为回文串statestatestate。这样就可以把要求的数字特征表示出来,让后还要记录一下选的数是多少,在pos<=(pre−1)/2pos<=(pre-1)/2pos<=(pre1)/2的时候就需要判断是否为回文串了。
好长时间没写数位dpdpdp了,solvesolvesolve传值的时候LLLLLL传成了intintint

//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;LL l,r;
int a[N],num[N],tot;
LL f[30][30][2];LL dfs(int pos,int pre,int state,int flag)
{if(pos==-1) return state;if(flag&&f[pos][pre][state]!=-1) return f[pos][pre][state];int x=flag? 9:a[pos];LL ans=0;for(int i=0;i<=x;i++){num[pos]=i;if(i==0&&pos==pre) ans+=dfs(pos-1,pre-1,state,flag||i<x);else if(pos<=(pre-1)/2&&state) ans+=dfs(pos-1,pre,state&&(num[pre-pos]==i),flag||i<x);else ans+=dfs(pos-1,pre,state,flag||i<x);}if(flag) f[pos][pre][state]=ans;return ans;
}LL solve(LL x)
{tot=0;if(x<0) return 0;if(x==0) return 1;while(x){a[tot++]=x%10;x/=10;}return dfs(tot-1,tot-1,1,0);
}int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);memset(f,-1,sizeof(f));int _; scanf("%d",&_);for(int __=1;__<=_;__++){scanf("%lld%lld",&l,&r);if(l>r) swap(l,r);printf("Case %d: %lld\n",__,solve(r)-solve(l-1));}return 0;
}
/**/

带前导零,更好看点的代码:

// Problem: Palindromic Numbers
// Contest: Virtual Judge - LightOJ
// URL: https://vjudge.net/problem/LightOJ-1205
// Memory Limit: 65 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;LL x,y;
int a[300],tot;
int f[30][30][2];
int num[30];LL dp(int pos,int st,int state,int lead,int flag) {if(pos==0) return state;if(f[pos][st][state]!=-1&&lead&&flag) return f[pos][st][state];LL ans=0;int x=flag? 9:a[pos];for(int i=0;i<=x;i++) {num[pos]=i;if(!lead) {ans+=dp(pos-1,i==0? st:pos,state,lead||i>0,flag||i<x);} else {if(pos<=st/2&&state) ans+=dp(pos-1,st,state&&(i==num[st-pos+1]),lead,flag||i<x);else ans+=dp(pos-1,st,state,lead,flag||i<x);}}if(lead&&flag) f[pos][st][state]=ans;return ans;
}LL solve(LL x) {if(x<0) return 0;if(x==0) return 1;tot=0;while(x) a[++tot]=x%10,x/=10;return dp(tot,0,1,0,0);
}int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);memset(f,-1,sizeof(f));int _; scanf("%d",&_);for(int i=1;i<=_;i++) {LL a,b;scanf("%lld%lld",&a,&b); if(a>b) swap(a,b);printf("Case %d: %lld\n",i,solve(b)-solve(a-1));}return 0;
}
/**/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/315418.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#规范整理·语言要素

如有不理解&#xff0c;请留言&#xff0c;开始!1. 正确操作字符串拼接字符串一定要考虑使用 StringBuilder ,默认长度为16,实际看情况设置。StringBuilder本质&#xff1a; 是以非托管方式分配内存。同时StringFormat方法 内部也是使用StringBuilder进行字符串格式化。2. 使用…

Educational Codeforces Round 94 (Rated for Div. 2) D(思维)

题目&#xff1a; You are given an array a1,a2…an. Calculate the number of tuples (i,j,k,l) such that: 1≤i<j<k<l≤n; aiak and ajal; Input The first line contains a single integer t (1≤t≤100) — the number of test cases. The first line of each…

2019 ICPC Asia Nanchang Regional K.Tree 树上启发式合并 + 动态开点线段树

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给你一棵树&#xff0c;每个点都有一个权值valvalval&#xff0c;求满足以下条件 (1)x!yx!yx!y (2)xxx和yyy不互为祖先 (3)val[lca(x,y)]∗2val[x]val[y]val[lca(x,y)]*2val[x]val[y]val[lca(x,y)]∗2val[x]…

NetCore服务虚拟化01(集群组件Sodao.Core.Grpc)

一. 起始去年.NetCore2.0的发布&#xff0c;公司决定新项目采用.NetCore开发&#xff0c;当作试验。但是问题在于当前公司内部使用的RPC服务为Thrift v0.9 zookeeper版本&#xff0c;经过个性化定制&#xff0c;支持了异步&#xff0c;但也因为如此&#xff0c;这么多年来一直…

2019 ICPC Asia Nanchang Regional And and Pair 组合数学

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给一个长度为nnn的二进制&#xff0c;求满足如下条件的j,ij,ij,i对数&#xff1a; (1)0<j<i<n(1)0<j<i<n(1)0<j<i<n (2)i&ni(2)i\And ni(2)i&ni (3)i&j0(3)i\And j0…

Oracle 发布基于 VS Code 的开发者工具,轻松连接 Oracle 数据库

在之前的文章中&#xff0c;我们提到了亚马逊、谷歌、IBM 等大厂都上了 Visual Studio Code 的船。今天&#xff08;北京时间 2019 年 6 月 20 日&#xff09;&#xff0c;甲骨文也上了 VS Code 的船&#xff0c;发布了基于 VS Code 的开发者工具&#xff0c;让开发者能轻松连接…

CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 树启 + 状压

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 据说是树启的压轴题。 先观察题意&#xff0c;字符有1−221-221−22中&#xff0c;为什么不是1−261-261−26个&#xff1f;显然他就是让你状压的。我们考虑将每条路径上字符状压成statest…

线段树——思维(Codeforces 339D Xenia and Bit Operations/Billboard HDU - 2795)

Codeforces 339D Xenia and Bit Operations vj地址 题意&#xff1a;给出2的n次方个数&#xff0c;每次将现在这个序列中相邻的两个数运算后合并为一个数&#xff0c;得到一个新的序列&#xff0c;这个新序列的长度是上一个序列长度-1&#xff0c;当新序列长度为1时停止运算&am…

误删50节点K8s集群为何3小时才能复原?Spotify揭自家事故幕后经验

误删50节点K8s集群为何3小时才能复原&#xff1f;Spotify揭自家事故幕后经验线上音乐串流服务Spotify一位基础架构工程师David Xia&#xff0c;在今年欧洲KubeCon大会上分享了自家Kubernetes集群一次意外事件。拥有上亿用户的Spotify&#xff0c;旗下开发者高达1千人&#xff0…

Codeforces Round #610 (Div. 2) D. Enchanted Artifact 交互 + 思维

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 首先我们发现如果知道了字符串的长度&#xff0c;我们就可以O(n1)O(n1)O(n1)次询问求解出来。比如当前长度为nnn&#xff0c;那么我们就可以构造出一个长度为nnn的全′a′a′a′字符串&…

P1005 矩阵取数游戏(__int128模板/简单dp)

转跳P1005 题目描述 帅帅经常跟同学玩一个矩阵取数游戏&#xff1a;对于一个给定的 n \times mnm 的矩阵&#xff0c;矩阵中的每个元素 a_{i,j}a i,j ​ 均为非负整数。游戏规则如下&#xff1a; 每次取数时须从每行各取走一个元素&#xff0c;共 nn 个。经过 mm 次后取完矩…

Docker+ Kubernetes已成为云计算的主流(二十六)

前言 最近正在抽时间编写k8s的相关教程&#xff0c;很是费时&#xff0c;等相关内容初步完成后&#xff0c;再和大家分享。对于k8s&#xff0c;还是上云更为简单、稳定并且节省成本&#xff0c;因此我们需要对主流云服务的容器服务进行了解&#xff0c;以便更好地…

P1020 导弹拦截(n*log n时间的最长上升子序列思想)

题目描述 某国为了防御敌国的导弹袭击&#xff0c;发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷&#xff1a;虽然它的第一发炮弹能够到达任意的高度&#xff0c;但是以后每一发炮弹都不能高于前一发的高度。某天&#xff0c;雷达捕捉到敌国的导弹来袭。由于该系统还…

P4137 Rmq Problem / mex 主席树求mex

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 按照值建线段树&#xff0c;每个位置维护值出现的最后位置&#xff0c;让后可持久化一下&#xff0c;当查询[l,r][l,r][l,r]的时候&#xff0c;我们只需要在[1,r][1,r][1,r]中找最后出现位…

《刷新》:拥抱同理心,建立成长型思维

“ 不刷新即死亡”2018年&#xff0c;很多朋友包括博客园里的很多园友都在阅读微软第三任CEO萨提亚纳德拉的这本《刷新》并且发布了很多读后感&#xff0c;但我却一直没有来得及阅读。刚好最近订阅了喜马拉雅的VIP会员&#xff0c;每天上下班时间开始了听书之旅&#xff0c;这里…

P1833 樱花——混合背包 二进制优化成01背包

P1833樱花 题目大意&#xff1a;有n颗樱花树&#xff0c;你的总时间为T&#xff0c;现在n课树&#xff0c;每次观看要花费w时间&#xff0c;能获取v点价值&#xff0c;最多能参观s次&#xff0c;如果s等于0&#xff0c;则可以观看无限次&#xff0c;问你在T时间内 获得的最大价…

ICPC Trainings Moscow2020 K. King and Zeroing 树直径 + 思维

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给你一颗树&#xff0c;但是这棵树的边是双向的&#xff0c;且花费为1。对于每个点&#xff0c;可以在连向他的边中选择一条&#xff0c;使由这个点到边的另一个点的有向边花费变成1&#xff0c;对于每个点都…

Consul初探-集成ocelot

前言由于 Consul 的高可用性、丰富的API、友好的 Web 控制台界面等特点&#xff0c;Consul 的发展非常迅猛&#xff0c;得益于 .NETCore 社区的快速发展和社区成员的贡献&#xff0c;我们现在可以非常方便快速的将 Consul 集成到 .NETCore 中&#xff0c;在 Ocelot 的集成方面也…

Codeforces Round #682 (Div. 2)D Powerful Ksenia ///思维

cf地址 题目大意&#xff1a;Ksenia has an array a consisting of n positive integers a1,a2,…,an. In one operation she can do the following: choose three distinct indices i, j, k, and then change all of ai,aj,ak to ai⊕aj⊕ak simultaneously, where ⊕ denot…

2021年广东工业大学第十五届文远知行杯程序设计竞赛(同步赛) H.有多短 思维

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 可以发现树的直径起点和终点一定是两个度数为111的点&#xff0c;所以我们可以把kkk平均的分给所有度数为111的点&#xff0c;这样答案就为2∗kcnt\frac{2*k}{cnt}cnt2∗k​。 证如果分配给…