HDU6156-Palindrome Function

来源:互联网 发布:安全期计算器软件 编辑:程序博客网 时间:2024/05/22 06:12

Palindrome Function

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 431 Accepted Submission(s): 230

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],定义函数f(n,k),若数字nk进制下为回文则函数值为k,否则为1,求Ri=Lrj=lf(i,j)
解题思路:这道题跟LightOJ-1205和SPOJ-MYQ10 - Mirror Number很像,利用数位dp。
dp[i][j][k][r]表示k进制下从i位到j为,状态为r1表示回文)时回文数的个数,注意判断前导0

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN=40;typedef long long LL;int tmp[MAXN],num[MAXN];int dp[MAXN][MAXN][MAXN][2];//dp[i][j][k][r]:k进制下从i位到j为,状态为r(1表示回文)时回文数的个数int dfs(int st,int cur,int pal,int limit,int base){    if(cur<0) return pal;    if(!limit&&dp[st][cur][base][pal]!=-1)        return dp[st][cur][base][pal];    int up=limit?num[cur]:(base-1);    int ans=0;    for(int i=0;i<=up;i++)    {        tmp[cur]=i;        if(st==cur&&i==0)            ans+=dfs(st-1,cur-1,pal,limit&&i==limit,base);        else if(pal&&cur<(st+1)/2)            ans+=dfs(st,cur-1,tmp[st-cur]==i,limit&&i==up,base);        else ans+=dfs(st,cur-1,pal,limit&&i==up,base);    }    if(!limit) dp[st][cur][base][pal]=ans;    return ans;}int calc(LL n,int base){    int len=0;    while(n)    {        num[len++]=n%base;        n/=base;    }    //num[len]=0;    return dfs(len-1,len-1,1,1,base);}int main(){    memset(dp,-1,sizeof(dp));    int T;    scanf("%d",&T);    int cas=0;    while(T--)    {        LL L,R;        scanf("%lld%lld",&L,&R);        if(L>R) swap(L,R);        LL ans=0;        printf("Case %d: %d\n",++cas,calc(R,10)-calc(L-1,10));    }    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 医院药品断货了怎么办 空腹吃菠萝胃疼怎么办 小孩黑户口怎么办上户 别人说名字起大了怎么办? 念佛号时心老是不集中怎么办 扑lv期嗓子痛头痛怎么办 公众号忘记了账号怎么办 公众号账号密码忘记了怎么办 现实生活被小人缠上怎么办 五行缺木和水怎么办 八字火旺的人怎么办 综合旺衰得分负怎么办 妈妈误打死一只黄鼠狼怎么办 油笔画在白墙上怎么办 壁纸上的水彩笔怎么办 隐形拉链头脱了怎么办 拉链的一边掉了怎么办 帝豪gs加了乙醇汽油怎么办 命理五行缺木怎么办 微信改名含有特殊符号怎么办 户口名字打错了怎么办 寻仙会心几率差怎么办 注册商标下来了没收到怎么办 金融公司倒闭欠的钱怎么办 买车贷款被骗了怎么办 定投终止后钱怎么办 受到小贷公司催款威胁怎么办 合同保证金单据丢了怎么办 公司注销期间发现欠税怎么办 公司注销后银行账户怎么办 注销公司营业执照和公章丢失怎么办 工商核名过期了怎么办 核名后的许可没办下来怎么办 重庆公司核名有同名的怎么办 新电视不全屏怎么办左右有黑边 所学类别找不到音乐表演怎么办 公司口头通知不续签合同怎么办 雪纺衬衣皱了怎么办 狗打架受伤怎么办泰迪 大狗打架破了怎么办 舌头上长溃疡怎么办吃什么药