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. 九度OJ 1147:Jugs(罐子) (模拟、游戏)

    时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:243 解决:200 题目描述: In the movie "Die Hard 3", Bruce Willis and ...

  2. vsftp时间差8个小时的解决方法

    $ vi /etc/vsftpd/vsftpd.conf use_localtime=YES ;

  3. Git with SVN

    1)GIT是分布式的,SVN不是: 这 是GIT和其它非分布式的版本控制系统,例如SVN,CVS等,最核心的区别.好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并, ...

  4. python网络爬虫之如何识别验证码

    有些网站的登录方式是验证码登录的方式,比如今天我们要测试的网站专利检索及分析. http://www.pss-system.gov.cn/sipopublicsearch/portal/uilogin ...

  5. 转 Spring 组件 <context:component-scan base-pakage="">用法

    1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入. <!-- 注解注入 --> <co ...

  6. MySQL——事务

    核心知识: 1.什么是事务?一组原子性的SQL查询语句 2.事务的四种属性:ACID 3.四种隔离级别:读取未提交内容.读取提交内容.重复读.串行化. 4.什么是幻读?幻读有那些解决办法?连续读取同一 ...

  7. HDU 2037 今年暑假不AC ( 起始与终止时间 【贪心】)

    今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. JAVA- JSP中解决无法在Cookie当中保存中文字符的问题

    因为cookie的值是ASCII字符,不能直接把自定义cookie的值直接赋值为中文,但是要实现这个功能,还是有方法的. 1.java中已经给我们提供了方法,此时只需要导入该包就行 <%@ pa ...

  9. MyBatis映射文件中用#和$传递参数的特点

    在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...

  10. 使用同一个目的port的p2p协议传输的tcp流特征相似度计算

    结论: (1)使用同一个目的port的p2p协议传输的tcp流特征相似度高达99%.如果他们是cc通信,那么应该都算在一起,反之就都不是cc通信流. (2)使用不同目的端口的p2p协议传输的tcp流相 ...