首页 技术 正文
技术 2022年11月17日
0 收藏 656 点赞 2,413 浏览 1282 个字

A palindromic number or numeral palindrome is a ‘symmetrical’ number like 16461 that remains the same when its digits are reversed. In this problem you will be given two integers i j, you have to find the number of palindromic numbers between i and j (inclusive).

Input 
Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing two integers i j (0 ≤ i, j ≤ 1017).

Output 
For each case, print the case number and the total number of palindromic numbers between i and j (inclusive).

Sample Input 

1 10 
100 1 
1 1000 
1 10000 
Output for Sample Input 
Case 1: 9 
Case 2: 18 
Case 3: 108 
Case 4: 198

代码:

 #include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[],tmp[];
ll dp[][][];
ll dfs(int start,int pos,int ok,bool limit)
{
if(pos<)
return ok;
if(!limit&&dp[pos][ok][start]!=-)
return dp[pos][ok][start];
ll ans=;
int up=limit?a[pos]:;
for(int d=; d<=up; ++d)
{
tmp[pos]=d;
if(start==pos&&d==)
ans+=dfs(start-,pos-,ok,limit&&d==up);
else if(ok&&pos<(start+)/)
ans+=dfs(start,pos-,tmp[start-pos]==d,limit&&d==up);
else
ans+=dfs(start,pos-,ok,limit&&d==up);
}
if(!limit)
dp[pos][ok][start]=ans;
return ans;
}
ll solve(ll x)
{
memset(a,,sizeof(a));
int cnt=;
while(x!=)
{
a[cnt++]=x%;
x/=;
}
return dfs(cnt-,cnt-,,);
}
int main()
{
memset(dp,-,sizeof(dp));
int t,cnt=;
scanf("%d",&t);
while(t--)
{
ll x,y;
scanf("%lld%lld",&x,&y);
if(x>y)
swap(x,y);
printf("Case %d: %lld\n",cnt++,solve(y)-solve(x-));
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903