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=L∑rj=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.
(1≤T≤105,1≤L≤R≤109,2≤l≤r≤36)

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\n",cnt,ans);
90     }
91     return 0;
92 }

时间: 2024-08-08 21:51:52

Palindrome Function的相关文章

hdu 6156 Palindrome Function(回文数位dp)

题目链接:hdu 6156 Palindrome Function 题意: 给你一个L,R,l,r,问你在[L,R]内在[l,r]进制下有多少数是回文数,然后算一算贡献. 题解: 由于答案和该回文数的最高位有关(因为前导0不算). 考虑dp[i][j][k],表示在i进制下,当前考虑到第j位,该数字的起始点在第k位. 然后开一个数组记录一下前面的数字,做一下记忆化搜索就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) memset(a,b,siz

HDU 6156 Palindrome Function 数位DP

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6156 题目描述: 求L~R所有的数的l~r进制的f(x), f(x) = 当前进制 如果回文串, f(x) = 1 其他情况 解题思路: 数位DP, 统计个数 , 需要作出改变的就是进制和回文 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #inclu

【数位DP】HDU 6156 Palindrome Function

http://acm.hdu.edu.cn/showproblem.php?pid=6156 [AC] 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll mod=1e9+7; 5 int num[50]; 6 int L,R,l,r; 7 ll query(int x,int k) 8 { 9 if(x==0) return 0; 10 int cnt=0; 11 int cp

[HDOJ6156] Palindrome Function(数位dp, 枚举)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:算十进制下数字在[L,R]内用[l,r]进制表述下的数字贡献. 贡献有两种:回文数,贡献是进制k:不是回文数,贡献是1. 由于进制只有36个,枚举进制分别做统计回文数的数位dp即可,贡献按要求. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int maxn = 1

hdu 6156 Palindrome Function

数据好像极限,按理来说二分是可以过得,就是被卡主 #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<cmath> #include<set> #

HDU6156 Palindrome Function

题意:输入L,R,l,r求[L,R]范围内在[l, r]进制下的回文数(2<=l<=r<=26, L<=R<1e9) 题解:主要是求回文数,枚举每个进制,求ans = R前面的回文数-L前面的回文数,可以发现每个进制下小于1e9的回文数比较少,直接把所有的回文数预处理排序二分就可以 #include <bits/stdc++.h> #define ll long long #define maxn 100100 #define BUG(x, n) for(ll i

CCPC for UESTC

1001. Vertex Cover 题意:有一个贪心算法求最小顶点覆盖是每次选出度数最大的点然后删去,输出一个图使得这个算法跑出来的答案是你给出的答案的三倍及以上. 题解: 构造一个二分图,设左边有 nn 个点,标号是 1 ~ n1 n.对于每个 i \in [1, n]i∈[1,n],都在右边新建 \lfloor \frac{n}{i} \rfloor??i??n??? 个点,每个点都选择左边的 ii个点连 1 条边,使得左边每个点最多只被多加了一条边.这样构造完成后可以发现贪心的做法会把右

HDU 6156 回文 数位DP(2017CCPC)

Palindrome Function Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 559    Accepted Submission(s): 299 Problem Description As we all know,a palindrome number is the number which reads the same

通过百度echarts实现数据图表展示功能

现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解echarts是个怎样技术的开发者来说,可以到echarts官网进行学习了解,官网有详细的API文档和实例供大家参考学习. 2.以下是我在工作中实现整理出来的实例源码: 公用的支持js文件 echarts.js.echarts.min.js,还有其他的图表需要支持的js文件也可以到官网下载 echa