# Lightoj 1205 (palindromic numbers digital DP)

Source: Internet
Author: User

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, remains the same when it digits is Reversed. In this problem you'll be given II integers I J, you has to find the number of palindromic numbers between I and J (in clusive).

Input

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

Each case starts with a line containing the 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
Source
Problem Setter:jane Alam Jan

Test instructions is relatively simple, I directly on the code, some steps I have written in the code;

``/ * ***********************************************author:xdlovecreated time:2015 August 18 Tuesday 13:18 54 seconds file Na Me:xd.cpp ************************************************ * //* * Lightoj 1205 (digital DP) * DP[I][J] indicates the I-digit number beginning with J * Processing comparison annoying ... */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace STD;/******************************** please don ' t hack me!!/(ToT)/~~ __------__/~    ~             |           //^\\//^\|  /~~\  || t| | t|:~ | |6 | |           ___|_|_||:|  \__.   /o \/' |     (O)/~~~~\ ' \ \/| |~~\ |  ) ~------~ '/' |   | |     /____/~~~) (_/' | | |    /' |       ( |     | | |           \/__)/\ \ \ \ \ \ \ \ \|\/| |\___|  \ |           \____/     | |  /^~> \ _/< |         |  \                 | |        \        \                  -^-\  \       | )               `\_______/^\______/************************************/#define CLR (a) memset (A,0,sizeof (a));typedef Long LongLl;ll dp[ -][Ten];LL haha;intTolintbit[ -],nm[ -];BOOLCheck (ll x) {ll n = x; ll res =0; while(n) {res = res *Ten+ n%Ten; N/=Ten; }returnx = = Res;} ll Fuck (ll l,ll R,intT) {//In addition to the first position can not take 0, the other bits may take 0, with T to Markll ans =0;if(L = = r) {LL TP =0; NM[L] = Nm[r] = Bit[r]; for(inti = tol; I >=1; i--) TP = TP *Ten+ Nm[i];if(TP <= haha)//Because I am a one-person OK, so I need to compare with the original number, because this pit for a long time            returnBIT[L] +1;returnBIT[L]; }if(L > R) {LL TP =0; for(inti = tol; I >=1; i--) TP = TP *Ten+ Nm[i];if(TP <= haha)return 1;return 0; } for(inti = Bit[r]-1; I >= t; i--) ans + = dp[r-l +1][i]; NM[L] = Nm[r] = Bit[r];returnAns + fuck (l +1, R-1,0);} ll solve (ll N) {ll ans =1;//0 is also a palindrome numberhaha = n;if(N <0)return 0;if(N <Ten)returnn +1; Tol =0; while(n)//N to be split{Bit[++tol] = n%Ten; N/=Ten; } for(inti = tol-1; I >=1; i--)//Pre-calculate all palindrome numbers when each bit is 0         for(intj =1; J <Ten;    J + +) ans + = dp[i][j]; Ans + = fuck (1, Tol,1);//recursive processing, determining the number of each bit at a time    returnAns;}intMain () {//freopen ("In.txt", "R", stdin);    //freopen ("OUT.txt", "w", stdout);     for(inti =0; I <Ten; i++) dp[i] = dp[i] =1; for(inti =3; I <= -; i++) for(intj =0; J <Ten; J + +) for(intK =0; K <Ten; k++) dp[i][j] + = dp[i-2][K];intt,cnt =0; ll L,r;Cin>>T; while(t--) {Cin>>l>>r;if(L > R) swap (L,R);printf("Case%d:", ++cnt);cout<<solve (R)-Solve (L-1) <<endl; }return 0;}``

Copyright notice: Chase The dream of heart, never give up! By-xdlove

Lightoj 1205 (palindromic numbers digital DP)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.