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
Sample Output
Source
//题意:给出四个整数,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+其余数的个数 } 的累加了
计算小于等于一个数的回文数个数并不难,想清楚就好,如果某数前半部分小于该数的前半部分,那一定能构造出来,所以等于时特判一下即可
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
#define MX 500
/**************************/ LL cal(LL x,LL k)
{
int len=;
LL temp=x;
int dat[];
while (temp)
{
dat[len++]=temp%k;
temp/=k;
} LL ans = ,sum = k-,base=;
for (int i=;i<len;i++)
{
ans+=sum-base;
if (i%==)
{
base=sum;
sum=sum*k+k-;
}
}
LL sb=;
for (int i=len-;i>=len/;i--)
sb = sb*k+dat[i];
LL now=sb;
int wei;
if (len%) wei=len/+;
else wei = len/;
for (int i=wei;i<len;i++)
sb = sb*k+dat[i]; if (sb<=x)
ans+=now-base;
else
ans+=now-base-;
return ans*(k-)+x;
} int main()
{
int T = scan();
for (int cnt=;cnt<=T;cnt++)
{
int L,R,l,r;
scanf("%d%d%d%d",&L,&R,&l,&r);
LL ans = ;
for (int i=l;i<=r;i++)
{
ans +=cal(R,i)-cal(L-,i);
}
printf("Case #%d: %lld\n",cnt,ans);
}
return ;
}
Palindrome Function的更多相关文章
- HDU - 6156 2017CCPC网络赛 Palindrome Function(数位dp找回文串)
Palindrome Function As we all know,a palindrome number is the number which reads the same backward a ...
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...
- Palindrome Function HDU - 6156(数位dp)
要求m-n内在l-r进制下的是回文数的总个数. dp[进制][从第j为开始][目前到达第k位] = 总的方案数 dfs枚举目前的到达的位置,这个数开始的位置,进制,前导零,限制条件,然后枚举的时候如果 ...
- HDU 6156 Palindrome Function
http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:$f(n,k)$表示判断n在k进制下是否是回文串,如果是,则返回k,如果不是,则返回1.现在要计算$ ...
- HDU 6156 Palindrome Function(数位DP)题解
思路: 数位dp的操作是dfs+记忆化,我们dp开四维:位置,长度,进制,是否回文.然后每次暴搜记录下每个位置的数字是什么,搜到对称轴另一边需要检查是否符合回文. 终于把友谊赛的题目都补完了...没做 ...
- HDU-6156 Palindrome Function(数位DP)
一.题目 二.思路 1.这是很明显的数位DP: 2.和以往数位DP不同的是,这里带了个进制进来,而以往做是纯十进制下或者纯二进制下做操作.但是,不管多少进制,原理都是一样的: 3.这里有个小坑,题目中 ...
- 【数位DP】HDU 6156 Palindrome Function
http://acm.hdu.edu.cn/showproblem.php?pid=6156 [AC] #include<bits/stdc++.h> using namespace st ...
- 【2017中国大学生程序设计竞赛 - 网络选拔赛】Palindrome Function
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6156 [题意] 已知函数f(x, k),如果10进制数x在k进制下是个回文数,那么f(x, k)值为k, ...
- PPAPI插件开发指南
转载请注明出处:http://www.cnblogs.com/fangkm/p/4401075.html 前言 插件一直是浏览器的重要组成部分,丰富浏览器的运行能力,实现一些HTML+JS实现不了本地 ...
随机推荐
- CentOS系统使用yum安装配置MariaDB数据库
http://www.server110.com/mariadb/201310/2670.html 1.在 /etc/yum.repos.d/ 下建立 MariaDB.repo,内容如下:[azure ...
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- (一)EasyUI 使用——基本概念
1. EasyUI是什么 EasyUI是一种第三方组织开发的一款基于jQuery的,简单易用的,功能强大的WEB[后台前端]JavaScript现成的组件库. 2. JavaScript,AJAX, ...
- EF中多表公共字段,以及设置EntityBase使所有实体类继承自定义类
使用EF框架访问数据库时,如果某些表具有公共字段,例如在审核流程中,对于各类申请单资料的创建人.创建时间.修改人.修改时间,这些可能多表都需要的字段,如果在每个实体中进行赋值操作显然是类似和重复的,下 ...
- sql DATEPART() MONTH() convert() cast() dateadd() DATEDIFF() with(nolock)
DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 DATEPART(datepart,date) date 参数是合法的日期表达式.datepart 参数 ...
- 8.1.1 Service的生命周期
2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...
- CREATE SEQUENCE添加自增序列及NEXT VALUE FOR返回序列号
From :https://msdn.microsoft.com/zh-cn/library/ff878091.aspx 语法: CREATE SEQUENCE [schema_name . ] se ...
- MVC之ActionFilterAttribute自定义属性
ActionFilterAttribute里有OnActionExecuting方法,跟Controller一样, 同是抽象实现了IActionFilter接口. // 登录认证特性 public c ...
- unity, iOS集成微信
将微信sdk直接拖进xcode会导致Library Search Paths是错的,需要手动改成如下样子(蓝色选中部分)才能通过编译:
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...