LightOJ 1205 Palindromic Numbers

数位DP。。。。


Palindromic Numbers

Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

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 (inclusive).

Sample Input

4

1 10

100 1

1 1000

1 10000

Sample Output

Case 1: 9

Case 2: 18

Case 3: 108

Case 4: 198

Source

Problem Setter: Jane Alam Jan

[Submit]   [Go Back]   [Status]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;

int a[70];
LL dp[70][70];

LL dfs(int len,int l,int r,bool limit,bool ok)
{
	if(l<r) return !limit||(limit&&ok);
	if(!limit&&~dp[len][l])
		return dp[len][l];
	LL ret=0;
	int mx=limit?a[l]:9;
	for(int i=0;i<=mx;i++)
	{
		if(l==len-1&&i==0)
			continue;
		int g=ok;
		if(g) g=a[r]>=i;
		else g=a[r]>i;
		ret+=dfs(len,l-1,r+1,limit&&i==mx,g);
	}
	if(!limit)
		dp[len][l]=ret;
	return ret;
}

LL gaoit(LL n)
{
	if(n<0) return 0;
	if(n==0) return 1;
	int len=0;
	while(n){a[len++]=n%10;n/=10;}
	LL ret=1;
	for(int i=len;i>=1;i--)
		ret+=dfs(i,i-1,0,i==len,1);
	return ret;
}

int main()
{
	int T_T,cas=1;
	cin>>T_T;
	memset(dp,-1,sizeof(dp));
	while(T_T--)
	{
		LL x,y;
		cin>>x>>y;
		if(x>y) swap(x,y);
		printf("Case %d: %lld\n",cas++,gaoit(y)-gaoit(x-1));
	}
	return 0;
}

LightOJ 1205 Palindromic Numbers,布布扣,bubuko.com

时间: 2024-08-12 17:34:36

LightOJ 1205 Palindromic Numbers的相关文章

LightOJ 1205 - Palindromic Numbers (数位dp)

LightOJ 1205 - Palindromic Numbers (数位dp) ACM 题目地址:SPOJ MYQ10 Mirror Number 题意: 求[a,b]中回文的个数. 分析: 是SPOJ MYQ01的简单版...其实有非递归方法的. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.net/hcbbt * File: 1205.cpp * Create Date: 2014-08-

lightOJ 1205(Palindromic Numbers数位DP)

Palindromic Numbers Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submit Status Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In

[数位dp] lightoj 1205 Palindromic Numbers

题意:给定范围内是回文数的个数. 思路: dp[site][len]  site位的回文长度是len. 需要一个负责数组 ok存每位放的数. 然后就是dfs了,就是取len 的中间值mid 超过mid的话 就看看ok里面之前对称的那个数 判断是否相等 相等才能放进入下一位. 注意判断前导0,和这个oj必须用long long. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #in

LightOJ - 1205:Palindromic Numbers (数位DP&amp;回文串)

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

[暑假集训--数位dp]LightOj1205 Palindromic Numbers

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

xtu summer individual 1 E - Palindromic Numbers

E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In this

Lightoj1205——Palindromic Numbers(数位dp+回文数)

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

[LightOJ1205]Palindromic Numbers(数位dp)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1205 题意:求[l,r]内回文数的数量. dp(s,l,ok)表示数字以s为开头,长度为l的时是/不是回文数 dp(s,l,ok)可以由dp(s,l-1,ok)更新来,当且仅当接下来插入的一位与s对应的位置相同. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const i

lightoj Beginners Problems

很多以前写的丑代码 1000 - Greetings from LightOJ #include<math.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { int a,b,n,Case; scanf("%d",&n); Case=0; while(Case++,n--) { scanf("%d%d",&