# Palindrome Function

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 863    Accepted Submission(s): 476

Problem Description
As we all know,a palindrome number is the number which reads the same backward as forward,such as 666 or 747.Some numbers are not the palindrome numbers in decimal form,but in other base,they may become the palindrome number.Like 288,it’s not a palindrome number under 10-base.But if we convert it to 17-base number,it’s GG,which becomes a palindrome number.So we define an interesting function f(n,k) as follow:
f(n,k)=k if n is a palindrome number under k-base.
Otherwise f(n,k)=1.
Now given you 4 integers L,R,l,r,you need to caluclate the mathematics expression

Input
The first line consists of an integer T,which denotes the number of test cases.
In the following T lines,each line consists of 4 integers L,R,l,r.
(

Output
For each test case, output the answer in the form of “Case #i: ans” in a seperate line.

Sample Input
3 1 1 2 36 1 982180 10 10 496690841 524639270 5 20

Sample Output
Case #1: 665 Case #2: 1000000 Case #3: 447525746

Source

 1 #include <iostream>
2 #include <cstdio>
3 #include <cmath>
4 #include <cstring>
5 #include <algorithm>
6 #include <set>
7 #include <map>
8 #include <queue>
9 #include <stack>
10 #include <vector>
11 using namespace std;
12 #define mod 1000000007
13 typedef long long ll;
14 int t;
15 int bit[40];
16 int ans[40];
17 ll dp[40][40][40];
18 ll dfs(int pos,int zero,int jinzhi,int flag,int beg)
19 {
20     if(pos<0) return zero==0;
21     if(dp[pos][jinzhi][beg]!=-1&&!flag&&!zero)
22         return dp[pos][jinzhi][beg];
23     ll sum=0;
24     int up=flag?bit[pos]:jinzhi-1;
25     for(int i=0; i<=up; i++){
26         if(zero&&i==0)
27             sum+=dfs(pos-1,zero,jinzhi,flag&&i==up,beg);
28         else{
29             if(zero){
30                 ans[pos]=i;
31                 sum+=dfs(pos-1,0,jinzhi,flag&&i==up,pos);
32             }
33             else if(pos<(beg+1)/2){
34                 if(i==ans[beg-pos])
35                     sum+=dfs(pos-1,0,jinzhi,flag&&i==up,beg);
36             }
37             else{
38                 ans[pos]=i;
39                 sum+=dfs(pos-1,0,jinzhi,flag&&i==up,beg);
40             }
41         }
42     }
43     ans[pos]=-1;
44     if(!flag&&!zero)
45         dp[pos][jinzhi][beg]=sum;
46     return sum;
47 }
48 ll slove (int x,int jinzhi){
49     int  len=0;
50     while(x)
51     {
52         bit[len++]=x%jinzhi;
53         x/=jinzhi;
54     }
55     return dfs(len-1,1,jinzhi,1,39);
56 }
57 int main()
58 {
59     scanf("%d",&t);
60     memset(dp,-1,sizeof(dp));
61     int ce=1;
62     while(t--){
63         int L,R,l,r;
64         scanf("%d %d %d %d",&L,&R,&l,&r);
65         ll ans=0;
66         for(int i=l; i<=r; i++){
67             ll sum=slove(R,i)-slove(L-1,i);
68             ans=ans+sum*i+(R-L+1-sum);
69         }
70         printf("Case #%d: %lld\n",ce++,ans);
71     }
72     return 0;
73 }