HDU 6156 Palindrome Function (数位dp)
来源:互联网 发布:电脑广告制作软件 编辑:程序博客网 时间:2024/05/16 15:03
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
31 1 2 361 982180 10 10496690841 524639270 5 20
Sample Output
Case #1: 665Case #2: 1000000Case #3: 447525746
题意
求 L 到 R 之间所有数在 l 到 r 进制中是否是回文数的贡献和。
思路
回文数的个数其实是有规律的,假设在
…
于是我们可以考虑枚举进制
对于每一个进制
首先可以根据上面的规律求得
假设
可以看出,如果
综上,分别求得区间
AC 代码
#include <bits/stdc++.h>using namespace std;typedef __int64 LL;const int maxn = 40;int num[maxn];LL solve(LL x,int k){ if(x==0)return 0; LL i=x,tot=0; while(i) { num[tot++]=i%k; i/=k; } LL p=0,sum=1,cnt=0,base=k,left; for(int i=1; i<tot; i++) { cnt+=base-sum; if(i%2==0) { sum=base; base*=k; } } for(int i=tot-1; i>=tot/2; i--) p = p * k + num[i]; left = p; for(int i=tot/2+(tot&1); i<tot; i++) p = p * k + num[i]; cnt+=left-sum + (p<=x); return cnt*(k-1)+x;}int main(){ int T; cin>>T; for(int ti=1; ti<=T; ti++) { LL L,R,ans=0; int l,r; cin>>L>>R>>l>>r; for(int i=l; i<=r; i++) ans+=solve(R,i)-solve(L-1,i); cout<<"Case #"<<ti<<": "<<ans<<endl; } return 0;}
- HDU 6156 Palindrome Function(数位DP)
- HDU 6156 Palindrome Function (数位dp)
- hdu 6156 Palindrome Function(数位dp)
- hdu 6156 Palindrome Function 数位DP
- HDU 6156 Palindrome Function 经典数位DP
- HDU 6156 Palindrome Function 数位DP
- Hdu 6156 Palindrome Function【数位Dp】
- HDU-6156 Palindrome Function(数位DP)
- HDU 6156(Palindrome Function-数位dp)
- HDU 6156 Palindrome Function【数位dp】
- hdu 6156 Palindrome Function(数位dp&&其他)
- hdu 6156 palindrome function #数位dp 3
- hdu 6156 Palindrome Function【数位dp求回文数】
- HDU 6156 2016ICPC网络赛 G: Palindrome Function(数位DP)
- hdu6156 Palindrome Function (数位dp)
- HDU6156 Palindrome Function[数位DP]
- hdu6156 Palindrome Function数位dp
- hdu6156 Palindrome Function 数位dp
- 【常见数据类型占多少字节问题】
- SQL语言之delete、truncate和drop命令的区别和总结
- bzoj 4094: [Usaco2013 Dec]Optimal Milking 线段树
- 卓美业带来美业管理全新体验,内测中
- matlab中函数与opencv中对应
- HDU 6156 Palindrome Function (数位dp)
- rgb-d slam(2)笔记
- hdu 6154 CaoHaha's staff【找规律】
- 集合框架(五) java中的泛型
- 日常问题
- Java 枚举7常见种用法
- UVA 10881Piotr's Ants
- c语言中的rand()函数和srand()函数产生随机的整数
- 关于*P双机调试的学习