zoukankan      html  css  js  c++  java
  • HDU 6156 数位dp

    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 Ri=Lrj=lf(i,j) .
    When representing the k-base(k>10) number,we need to use A to represent 10,B to represent 11,C to repesent 12 and so on.The biggest number is Z(35),so we only discuss about the situation at most 36-base number.
     
    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.
    (1T105,1LR109,2lr36)
     
    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
    ",ce++,ans);
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    24、可靠信号与不可靠信号
    24、sed用法
    26、linux shell常用的几个函数,diff
    21、Posix共享内存
    23、修改vim的tab,使其占4个空格
    25、linux shell常用的几个函数,sort
    Console算法有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
    Console算法[for,if]一水仙花数(Water flower)
    Console算法回文数
    Console算法[for,if]有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数
  • 原文地址:https://www.cnblogs.com/hsd-/p/7410859.html
Copyright © 2011-2022 走看看