zoukankan      html  css  js  c++  java
  • Palindrome Function

     

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


    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

    2017中国大学生程序设计竞赛 - 网络选拔赛

     

     

    //题意:给出四个整数,L,R,l,r, ∑(L<=i<=R)∑(l<=j<=r) f(i,j) , f(i,j)代表 i 在 j 进制下是否为回文数,是为 k,否则值为 1。

     

    //题解:枚举进制,那么就变为 cal(R,k) - cal(L-1,k) { cal(i,j)表1--i,在k进制下的回文数个数*k+其余数的个数 } 的累加了

    计算小于等于一个数的回文数个数并不难,想清楚就好,如果某数前半部分小于该数的前半部分,那一定能构造出来,所以等于时特判一下即可

     1 # include <cstdio>
     2 # include <cstring>
     3 # include <cstdlib>
     4 # include <iostream>
     5 # include <vector>
     6 # include <queue>
     7 # include <stack>
     8 # include <map>
     9 # include <bitset>
    10 # include <sstream>
    11 # include <set>
    12 # include <cmath>
    13 # include <algorithm>
    14 # pragma  comment(linker,"/STACK:102400000,102400000")
    15 using namespace std;
    16 # define LL          long long
    17 # define pr          pair
    18 # define mkp         make_pair
    19 # define lowbit(x)   ((x)&(-x))
    20 # define PI          acos(-1.0)
    21 # define INF         0x3f3f3f3f3f3f3f3f
    22 # define eps         1e-8
    23 # define MOD         1000000007
    24 
    25 inline int scan() {
    26     int x=0,f=1; char ch=getchar();
    27     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
    28     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    29     return x*f;
    30 }
    31 inline void Out(int a) {
    32     if(a<0) {putchar('-'); a=-a;}
    33     if(a>=10) Out(a/10);
    34     putchar(a%10+'0');
    35 }
    36 #define MX 500
    37 /**************************/
    38 
    39 LL cal(LL x,LL k)
    40 {
    41     int len=0;
    42     LL temp=x;
    43     int dat[500];
    44     while (temp)
    45     {
    46         dat[len++]=temp%k;
    47         temp/=k;
    48     }
    49 
    50     LL ans = 0,sum = k-1,base=0;
    51     for (int i=1;i<len;i++)
    52     {
    53         ans+=sum-base;
    54         if (i%2==0)
    55         {
    56             base=sum;
    57             sum=sum*k+k-1;
    58         }
    59     }
    60     LL sb=0;
    61     for (int i=len-1;i>=len/2;i--)
    62         sb = sb*k+dat[i];
    63     LL now=sb;
    64     int wei;
    65     if (len%2) wei=len/2+1;
    66     else wei = len/2;
    67     for (int i=wei;i<len;i++)
    68         sb = sb*k+dat[i];
    69 
    70     if (sb<=x)
    71         ans+=now-base;
    72     else
    73         ans+=now-base-1;
    74     return ans*(k-1)+x;
    75 }
    76 
    77 int main()
    78 {
    79     int T = scan();
    80     for (int cnt=1;cnt<=T;cnt++)
    81     {
    82         int L,R,l,r;
    83         scanf("%d%d%d%d",&L,&R,&l,&r);
    84         LL ans = 0;
    85         for (int i=l;i<=r;i++)
    86         {
    87             ans +=cal(R,i)-cal(L-1,i);
    88         }
    89         printf("Case #%d: %lld
    ",cnt,ans);
    90     }
    91     return 0;
    92 }
    View Code

     

     

  • 相关阅读:
    根据坐标点画图形
    js 解析geojson格式
    devexpress 安装
    DataTable 获取列名
    ADO.NET 注册
    css:outline
    javascript函数sort
    引用类型-2015/10/06
    2015-11-02-js
    jquery
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/7401903.html
Copyright © 2011-2022 走看看