博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Palindrome Function
阅读量:5134 次
发布时间:2019-06-13

本文共 2611 字,大约阅读时间需要 8 分钟。

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. 
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≤361≤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 
1 1 2 36 
1 982180 10 10 
496690841 524639270 5 20 
Sample Output 
Case #1: 665 
Case #2: 1000000 
Case #3: 447525746

分析:很好的数位dp的练习;

   dp[pos][len][base]表示当前在pos位置,len长度,base进制下的状态;

   dfs过程时只需pos小于长度一半时check一下即可;

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,m,n) for(i=m;i<=(int)n;i++)#define inf 0x3f3f3f3f#define mod 1000000007#define vi vector
#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)#define pii pair
#define sys system("pause")#define ls (rt<<1)#define rs (rt<<1|1)#define all(x) x.begin(),x.end()const int maxn=1e5+10;const int N=1e4+10;using namespace std;ll gcd(ll p,ll q){ return q==0?p:gcd(q,p%q);}ll qmul(ll p,ll q,ll mo){ll f=0;while(q){ if(q&1)f=(f+p)%mo;p=(p+p)%mo;q>>=1;}return f;}ll qpow(ll p,ll q,ll mo){ll f=1;while(q){ if(q&1)f=f*p%mo;p=p*p%mo;q>>=1;}return f;}int n,m,k,t,num[40],tmp[40],cas;ll dp[40][40][40];ll dfs(int pos,int len,int bs,int zero,int lim){ if(pos<0)return zero; if(zero&&lim&&dp[pos][len][bs]!=-1)return dp[pos][len][bs]; ll ret=0; int ma=lim?bs-1:num[pos],i; rep(i,0,ma) { if(!zero&&i==0)ret+=dfs(pos-1,len-1,bs,zero,lim||i
=(len+1)/2) { tmp[pos]=i; ret+=dfs(pos-1,len,bs,zero||i,lim||i

转载于:https://www.cnblogs.com/dyzll/p/7427823.html

你可能感兴趣的文章
找工作之囧
查看>>
JMeter分布式负载测试(吞吐量控制器)
查看>>
java invokelater 以及invokeandwait
查看>>
fasta文件中序列的排序
查看>>
IOS 实现自定义的导航栏背景以及自定义颜色的状态栏(支持7.0以及低版本)
查看>>
Transaction And Lock--已提交读快照
查看>>
『Python CoolBook』Cython_高效数组操作
查看>>
NDK Build 用法(NDK Build)
查看>>
函数__内置函数(reversed,slice,format,ord,chr,ascii,repr)递归,迭代,二分法
查看>>
【数据结构上机练习】5.栈的简单操作(2)
查看>>
洛谷 P2051 [AHOI2009]中国象棋 状态压缩思想DP
查看>>
virtualbox桥接网卡设置
查看>>
基于JavaScript技术完成单击事件完成显示和隐藏
查看>>
番外篇:根据学习程度划分程序员的级别
查看>>
生命的真谛?
查看>>
cocos2d-x 3.1.1 学习笔记[17] 关于这些活动功能
查看>>
LeetCode——Flatten Binary Tree to Linked List
查看>>
后台还原checkboxlit的是否被选中的属性
查看>>
select框的text与value值的获取(实用版)
查看>>
网页书签
查看>>