码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-08-04 13:44:41      阅读:438      评论:0      收藏:0      [点我收藏+]

标签:map   reverse   img   class   wap   isp   when   lightoj   math   

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

4

1 10

100 1

1 1000

1 10000

Sample Output

Case 1: 9

Case 2: 18

Case 3: 108

Case 4: 198

 

问 l 到 r 有多少回文

枚举回文数的长度,然后数位dp。记一下当前位置是啥

技术分享
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<LL,LL>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 using namespace std;
18 inline LL read()
19 {
20     LL x=0,f=1;char ch=getchar();
21     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
22     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
23     return x*f;
24 }
25 int len;
26 LL l,r;
27 LL f[20][20][10];
28 int zhan[20];
29 int d[110];
30 
31 inline LL dfs(int now,int p,int dat,int fp)
32 {
33     if (now==(p+2)/2)
34     {
35         if (!fp)return 1;
36         for (int i=now-1;i>=1;i--)
37         {
38             if (d[i]>zhan[p+1-i])return 1;
39             if (d[i]<zhan[p+1-i])return 0;
40         }
41         return 1;
42     }
43     if (!fp&&f[now][p][dat]!=-1)return f[now][p][dat];
44     LL ans=0;
45     int mx=fp?d[now-1]:9;
46     for (int i=0;i<=mx;i++)
47     {
48         zhan[now-1]=i;
49         ans+=dfs(now-1,p,i,fp&&i==mx);
50         zhan[now-1]=-1;
51     }
52     if (!fp)f[now][p][dat]=ans;
53     return ans;
54 }
55 inline LL calc(LL x)
56 {
57     if (x==-1)return 0;
58     if (x==0)return 1;
59     LL xxx=x;
60     len=0;
61     while (xxx)
62     {
63         d[++len]=xxx%10;
64         xxx/=10;
65     }
66     LL sum=1;
67     for (int i=1;i<=len;i++)
68     {
69         for (int j=1;j<=(i==len?d[len]:9);j++)
70         {
71             zhan[i]=j;
72             sum+=dfs(i,i,j,len==i&&j==d[len]);
73             zhan[i]=-1;
74         }
75     }
76     return sum;
77 }
78 int main()
79 {
80     LL T=read();int cnt=0;
81     memset(f,-1,sizeof(f));
82     while (T--)
83     {
84         l=read();
85         r=read();
86         if (r<l)swap(l,r);
87         printf("Case %d: %lld\n",++cnt,calc(r)-calc(l-1));
88     }
89 }
LightOJ 1205

 

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

标签:map   reverse   img   class   wap   isp   when   lightoj   math   

原文地址:http://www.cnblogs.com/zhber/p/7284465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!