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+其余数的个数 } 的累加了

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

 # 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的更多相关文章

  1. HDU - 6156 2017CCPC网络赛 Palindrome Function(数位dp找回文串)

    Palindrome Function As we all know,a palindrome number is the number which reads the same backward a ...

  2. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  3. Palindrome Function HDU - 6156(数位dp)

    要求m-n内在l-r进制下的是回文数的总个数. dp[进制][从第j为开始][目前到达第k位] = 总的方案数 dfs枚举目前的到达的位置,这个数开始的位置,进制,前导零,限制条件,然后枚举的时候如果 ...

  4. HDU 6156 Palindrome Function

    http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:$f(n,k)$表示判断n在k进制下是否是回文串,如果是,则返回k,如果不是,则返回1.现在要计算$ ...

  5. HDU 6156 Palindrome Function(数位DP)题解

    思路: 数位dp的操作是dfs+记忆化,我们dp开四维:位置,长度,进制,是否回文.然后每次暴搜记录下每个位置的数字是什么,搜到对称轴另一边需要检查是否符合回文. 终于把友谊赛的题目都补完了...没做 ...

  6. HDU-6156 Palindrome Function(数位DP)

    一.题目 二.思路 1.这是很明显的数位DP: 2.和以往数位DP不同的是,这里带了个进制进来,而以往做是纯十进制下或者纯二进制下做操作.但是,不管多少进制,原理都是一样的: 3.这里有个小坑,题目中 ...

  7. 【数位DP】HDU 6156 Palindrome Function

    http://acm.hdu.edu.cn/showproblem.php?pid=6156 [AC] #include<bits/stdc++.h> using namespace st ...

  8. 【2017中国大学生程序设计竞赛 - 网络选拔赛】Palindrome Function

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6156 [题意] 已知函数f(x, k),如果10进制数x在k进制下是个回文数,那么f(x, k)值为k, ...

  9. PPAPI插件开发指南

    转载请注明出处:http://www.cnblogs.com/fangkm/p/4401075.html 前言 插件一直是浏览器的重要组成部分,丰富浏览器的运行能力,实现一些HTML+JS实现不了本地 ...

随机推荐

  1. JavaScript操作dom总结

    最近一直忙于新项目,真的挺费心的 从产品原型.ui.接口.真心挺费心的.好多地方都不完善!(i want say F word!!) 基础的东西又需要重新看看了! Node Node.NodeType ...

  2. Datastage装载数据报错 -798 428C9 不能把一个值插入到用GENERATED ALWAYS定义的ROWID列

    使用Datastage装载数据到下表中报错. 表结构 INCREMENT ),cst_name )) 报错 解决办法 新建表T_tmp )) 导入到该表后再使用INSERT INTO ...SELEC ...

  3. 用BSF + Beanshell使Java程序能够运行字符串形式的代码(转载)

    BSF(Bean Scripting Framework)最初是IBM Alpha工作组的项目,后来贡献给了Apache,BSF使Java能够更好的与各种流行脚本语言整合,实现不同语言之间的交互功能. ...

  4. NodeJS on Nginx: 使用nginx反向代理处理静态页面

    最近OurJS后台已经从纯node.js迁移到了Nginx+NodeJS上来了,感觉性能提升了不少,特与大家分享. Nginx ("engine x") 是一个高性能的 HTTP ...

  5. Velocity中文乱码问题解决方法

    http://yonge812.iteye.com/blog/1010290 Velocity中文乱码问题需要注意一下几点: 1.eclipse默认编码方式 2.页面的编码方式 3.volocity模 ...

  6. ASP.NET MVC传递Model到视图的多种方式总结

    ASP.NET MVC传递Model到视图的多种方式总结 有多种方式可以将数据传递到视图,如下所示: ViewData ViewBag PartialView TempData ViewModel T ...

  7. 移动端H5实现图片上传

    移动端H5实现图片上传 https://segmentfault.com/a/1190000010034177

  8. mac下使用QuickTime录屏及上传youku注意事项

    一,解决QuickTime录屏不能带声音的问题: mac下使用QuickTime屏幕 Soundflower->Audio Setup->soundflower(2ch),在其上鼠标右键, ...

  9. Windows下MySQL備份與還原

    方法一 備份: C:\...\MySQL\MySQL Server 5.1\bin\>mysqldump aa -u root -p > d:\aaa.sql.bak 還原: C:\... ...

  10. Spring 常用类

    一.拦截器 public class SysInteceptor implements HandlerInterceptor { /** * 最后执行,可用于释放资源 */ @Override pub ...