Palindrome Function

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

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
 
 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define mod 1000000007
typedef long long ll;
int t;
int bit[];
int ans[];
ll dp[][][];
ll dfs(int pos,int zero,int jinzhi,int flag,int beg)
{
if(pos<) return zero==;
if(dp[pos][jinzhi][beg]!=-&&!flag&&!zero)
return dp[pos][jinzhi][beg];
ll sum=;
int up=flag?bit[pos]:jinzhi-;
for(int i=; i<=up; i++){
if(zero&&i==)
sum+=dfs(pos-,zero,jinzhi,flag&&i==up,beg);
else{
if(zero){
ans[pos]=i;
sum+=dfs(pos-,,jinzhi,flag&&i==up,pos);
}
else if(pos<(beg+)/){
if(i==ans[beg-pos])
sum+=dfs(pos-,,jinzhi,flag&&i==up,beg);
}
else{
ans[pos]=i;
sum+=dfs(pos-,,jinzhi,flag&&i==up,beg);
}
}
}
ans[pos]=-;
if(!flag&&!zero)
dp[pos][jinzhi][beg]=sum;
return sum;
}
ll slove (int x,int jinzhi){
int len=;
while(x)
{
bit[len++]=x%jinzhi;
x/=jinzhi;
}
return dfs(len-,,jinzhi,,);
}
int main()
{
scanf("%d",&t);
memset(dp,-,sizeof(dp));
int ce=;
while(t--){
int L,R,l,r;
scanf("%d %d %d %d",&L,&R,&l,&r);
ll ans=;
for(int i=l; i<=r; i++){
ll sum=slove(R,i)-slove(L-,i);
ans=ans+sum*i+(R-L+-sum);
}
printf("Case #%d: %lld\n",ce++,ans);
}
return ;
}

HDU 6156 数位dp的更多相关文章

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...

  2. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  3. hdu 4352 数位dp + 状态压缩

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

  4. hdu:2089 ( 数位dp入门+模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp的模板题,统计一个区间内不含62的数字个数和不含4的数字个数,直接拿数位dp的板子敲就行 ...

  5. HDU 4352 XHXJ's LIS HDU(数位DP)

    HDU 4352 XHXJ's LIS HDU 题目大意 给你L到R区间,和一个数字K,然后让你求L到R区间之内满足最长上升子序列长度为K的数字有多少个 solution 简洁明了的题意总是让人无从下 ...

  6. hdu 3709 数位dp

    数位dp,有了进一步的了解,模板也可以优化一下了 题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数例如4139,以3为支点4*2 ...

  7. HDU 2089 数位dp入门

    开始学习数位dp...一道昨天看过代码思想的题今天打了近两个小时..最后还是看了别人的代码找bug...(丢丢) 传说院赛要取消 ? ... 这么菜不出去丢人也好吧~ #include<stdi ...

  8. HDU 2089 数位dp/字符串处理 两种方法

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDU 5808[数位dp]

    /* 题意: 给你l和r,范围9e18,求l到r闭区间有多少个数字满足,连续的奇数的个数都为偶数,连续的偶数的个数都为奇数. 例如33433符合要求,44不符合要求.不能含有前导零. 思路: 队友说是 ...

随机推荐

  1. 20155339 Exp5 MSF基础应用

    20155339 Exp5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode. 答: exploit是通过自己选择的漏洞和载荷进行攻击的一个过程. pay ...

  2. USART_GetITStatus和USART_GetFlagStatus的区别

    USART_GetITStatus()和USART_GetFlagStatus()的区别 都是访问串口的SR状态寄存器,唯一不同是,USART_GetITStatus()会判断中断是否开启,如果没开启 ...

  3. 前端页面loading效果(CSS实现)

    当首页内容或图片比较多时,加载时间会比较长,此时可能出现页面白屏的情况,用户体验较差.所以,在页面完全加载出来之前,可以考虑加入loading效果,当页面完全加载完后,是loading消失即可. 1. ...

  4. 手撸orm

    ORM简介 ORM即Object Relational Mapping,全称对象关系映射.当我们需要对数据库进行操作时,势必需要通过连接数据.调用sql语句.执行sql语句等操作,ORM将数据库中的表 ...

  5. IT高管和易筋经的故事

    老板是我非常敬重的前领导之一,他的一些管理风格,也影响了后来我对技术团队的管理. 理想企业 什么是程序员理想的IT企业?公司里面有良好的同事关系,合理的产品需求和开发进度,最好老板懂点编程,这样公司更 ...

  6. java实现基于关键字的文件夹(文件)的搜索、文件夹(文件)的复制、删除

    最近在做一个项目,需要实现这几项功能,上网查了很多资料,自己研究了好几天终于实现了,现在与大家分享一下. 一.JAVA实现文件夹的搜索   在百度搜索N个技术文章,从哪些大牛们共享的资料中终于写出了我 ...

  7. 迷你MVVM框架 avalonjs 0.8发布

    本版本最重要的特性是引进了AMD规范的模块加载器,亦即原来mass Framework 的并行加载器, 不同之处,它引进了requirejs的xxx!风格的插件机制,比如要延迟到DOM树建完时触发,是 ...

  8. cocos2dx内存优化

    纹理消耗了大量内存 在大部分情况下,是纹理(textures)消耗了游戏程序大量的内存.因此,纹理是我们首要考虑优化的对象 纹理加载 cocos2d里面纹理加载分为两个阶段:从图片文件中创建一个Ima ...

  9. arduino驱动安装

    方法一:使用官方提供的一键安装程序安装 打开Arduino在你电脑上的位置如果你的电脑是32位系统,就运行dpinst-x86.exe如果是64位系统,就运行dpinst-amd64.exe然后在弹出 ...

  10. ace -- api

    Ace The main class required to set up an Ace instance in the browser. 在浏览器中设置一个ace实例所需要的主要类. Methods ...