Palindrome Function

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)∑i=LR∑j=lrf(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.

InputThe 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≤361≤T≤105,1≤L≤R≤109,2≤l≤r≤36)OutputFor 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

[l,r]在[kl,kr]进制下回文串个数。


#include<bits/stdc++.h>
#define MAX 100
using namespace std;
typedef long long ll; int a[MAX];
int b[MAX];
ll dp[MAX][MAX][][]; ll dfs(int pos,int pre,bool hw,bool limit,int k){
int i;
if(pos<){
if(hw) return k;
return ;
}
if(!limit&&dp[pos][pre][hw][k]>-) return dp[pos][pre][hw][k];
int up=limit?a[pos]:k-;
ll cnt=;
for(i=;i<=up;i++){
b[pos]=i;
if(pos==pre&&i==){
cnt+=dfs(pos-,pre-,hw,limit&&i==a[pos],k);
}
else if(hw&&pos<=pre/){
cnt+=dfs(pos-,pre,hw&&b[pre-pos]==i,limit&&i==a[pos],k);
}
else{
cnt+=dfs(pos-,pre,hw,limit&&i==a[pos],k);
}
}
if(!limit) dp[pos][pre][hw][k]=cnt;
return cnt;
}
ll solve(ll x,int k){
int pos=;
while(x){
a[pos++]=x%k;
x/=k;
}
return dfs(pos-,pos-,true,true,k);
}
int main()
{
int tt=,t,i;
ll l,r,kl,kr;
scanf("%d",&t);
memset(dp,-,sizeof(dp));
while(t--){
scanf("%lld%lld%lld%lld",&l,&r,&kl,&kr);
ll ans=;
for(i=kl;i<=kr;i++){
ans+=solve(r,i)-solve(l-,i);
}
printf("Case #%d: %lld\n",++tt,ans);
}
return ;
}

HDU - 6156 2017CCPC网络赛 Palindrome Function(数位dp找回文串)的更多相关文章

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

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

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

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

  3. LightOJ - 1205:Palindromic Numbers (数位DP&回文串)

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  4. HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  5. 【HDU 5456】 Matches Puzzle Game (数位DP)

    Matches Puzzle Game Problem Description As an exciting puzzle game for kids and girlfriends, the Mat ...

  6. 【HDU 4352】 XHXJ's LIS (数位DP+状态压缩+LIS)

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

  8. 【HDU】4352 XHXJ's LIS(数位dp+状压)

    题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...

  9. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. windows10怎样关闭,开机启动项中不需要的应用?

    1.点击"Window"键,输入"设置",双击''设置'',进入设置界面. 2.在设置界面中,点击"启动",然后就可以设置,关闭哪些是我们不 ...

  2. ios之编码规范具体说明

    iOS代码规范: 所有代码规范所有遵循苹果sdk的原则,不清楚的请訪问苹果SDK文档或下载官方Demo查看. 1.project部分: 将项目中每一个功能模块相应的源文件放入同一目录下,使用虚拟目录. ...

  3. python 函数中的递归、lambda 、map reduce 等详解

    举例说明 #例1: ###递归函数求和 from traitlets.traitlets import Instance def mysum(L): print(L) if not L: return ...

  4. ubuntu把文件移动到指定的文件夹

    有个文件a.txt在/etc下,想把它移动到/etc/fuck文件夹中 cd /etc/fuck sudo mv a.txt 要移动到的文件夹 没报错就成功了

  5. vue 的基本语法

    一 . Vue 的介绍 1 . 前端的三大框架 (可以去 GitHub 查看三个框架的 star 星) vue   :  作者尤雨溪, 渐进式的JavaScript 框架 react :  Faceb ...

  6. ubuntu 14.04 用 shell 方便安装nginx

    nginx.sh apt-get install -y build-essential gcc g++ make m4 libpcre3 libpcre3-dev libcurl4-gnutls-de ...

  7. IOS 十六进制字符串转换成UIColor

    /** * 十六进制转换成UIColor * * @param stringToConvert 十六进制字符串 * * @return UIColor */ +(UIColor *) hexStrin ...

  8. mybatis中xml字段空判断及模糊查询

    由于业务特殊的查询需求,需要下面的这种查询,一直感觉模糊不清,本地测试一下顺便做个总结 贴一段xml代码,如下: <if test="receivedName != null and ...

  9. react服务端渲染

    一.服务端渲染的好处 1.SEO, 让搜索引擎更容易读取页面内容: 2.首屏渲染速度更快(重点),无需等待JS文件下载执行过程: 3.更易于维护,服务端和客户端可以共享某些代码: 二.实现原理 服务端 ...

  10. smokeping 出现的问题

    Global symbol "%Config" requires explicit package name at /usr/lib64/perl5/lib.pm line 10. ...