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 (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

代码:

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 int a[40],tmp[40];
 5 ll dp[40][100][100];
 6 ll dfs(int start,int pos,int ok,bool limit)
 7 {
 8     if(pos<0)
 9         return ok;
10     if(!limit&&dp[pos][ok][start]!=-1)
11         return dp[pos][ok][start];
12     ll ans=0;
13     int up=limit?a[pos]:9;
14     for(int d=0; d<=up; ++d)
15     {
16         tmp[pos]=d;
17         if(start==pos&&d==0)
18             ans+=dfs(start-1,pos-1,ok,limit&&d==up);
19         else if(ok&&pos<(start+1)/2)
20             ans+=dfs(start,pos-1,tmp[start-pos]==d,limit&&d==up);
21         else
22             ans+=dfs(start,pos-1,ok,limit&&d==up);
23     }
24     if(!limit)
25         dp[pos][ok][start]=ans;
26     return ans;
27 }
28 ll solve(ll x)
29 {
30     memset(a,0,sizeof(a));
31     int cnt=0;
32     while(x!=0)
33     {
34         a[cnt++]=x%10;
35         x/=10;
36     }
37     return dfs(cnt-1,cnt-1,1,1);
38 }
39 int main()
40 {
41     memset(dp,-1,sizeof(dp));
42     int t,cnt=1;
43     scanf("%d",&t);
44     while(t--)
45     {
46         ll x,y;
47         scanf("%lld%lld",&x,&y);
48         if(x>y)
49             swap(x,y);
50         printf("Case %d: %lld\n",cnt++,solve(y)-solve(x-1));
51     }
52     return 0;
53 }
时间: 2024-08-28 04:46:43

Lightoj1205——Palindromic Numbers(数位dp+回文数)的相关文章

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

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

回文数 第N个回文数

判断回文数还是不难,如果能转为字符串就更简单了. 如果是求第N个回文数呢. 12321是一个回文数,这里先考虑一半的情况. 回文数的个数其实是有规律的.如: 1位回文数: 9个 2位回文数: 9个 3位回文数: 90个 4位回文数: 90个 5位回文数: 900个 6位回文数: 900个 … 我们看到9.90.900,是不是很有规律,那是什么原因?很简单,我们把回文数拆开两半 [123321]来看.两半的变化一样的,那我们只算其中一半就行了.首位不能是0,所以左半最小为 100,最大为999,共

[暑假集训--数位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

[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

[Swust OJ 797]--Palindromic Squares(回文数水题)

题目链接:http://acm.swust.edu.cn/problem/797/ Time limit(ms): 1000 Memory limit(kb): 10000 Description Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 <= B <= 20 base 1

2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

欧拉项目004:寻找最大的回文数

Problem 4: Largest palindrome product A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. 寻找有两

[C++]LeetCode: 99 Longest Palindromic Substring (最长回文子串)

题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路:题目要求的s的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判