hdu 6156 Palindrome Function 数位DP
来源:互联网 发布:hive sql minus用法 编辑:程序博客网 时间:2024/06/05 20:24
Palindrome Function
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)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.
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
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 )
In the following T lines,each line consists of 4 integers L,R,l,r.
(
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
Source
2017中国大学生程序设计竞赛 - 网络选拔赛
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.Arrays;import java.util.StringTokenizer;public class Main { public static void main(String[] args) { new Task().solve(); }}class Task { InputReader in = new InputReader(System.in) ; PrintWriter out = new PrintWriter(System.out) ; int[] dight = new int[32] ; int[] tmp = new int[32] ; int[][][][] dp = new int[32][2][32][37] ; int dfs(int start,int pos, int s , boolean isEnd , int k){ if(pos < 0){ return s ; } if(!isEnd && dp[pos][s][start][k] !=-1){ return dp[pos][s][start][k] ; } int ret=0; int end = isEnd ? dight[pos] : k-1 ; for(int d=0; d<=end; d++){ tmp[pos]=d ; if(start==pos && d==0){ ret += dfs(start-1 , pos-1 , s , isEnd&&d==end , k); } else if(s==1 && pos<(start+1)/2) ret+=dfs(start,pos-1, tmp[start-pos]==d ? 1 : 0 , isEnd&&d==end , k); else ret+=dfs(start,pos-1, s , isEnd&&d==end , k); } if(!isEnd) dp[pos][s][start][k] = ret ; return ret; } int solve(int a , int k){ int cnt =0 ; while(a > 0){ dight[cnt++] = a % k ; a /= k ; } return dfs(cnt-1 , cnt-1 , 1 , true , k) ; } long sum(int L , int R , int l , int r){ long ans = 0 ; for(int k = l ; k <= r ; k++){ int cnt = solve(R , k ) - solve(L-1 , k) ; long t = (long)k ; ans += t * cnt ; ans += (R - L + 1) - cnt ; } return ans ; } void solve(){ for(int i = 0 ; i < 32 ; i++){ for(int j = 0 ; j < 2 ; j++){ for(int k = 0 ; k < 32 ; k++){ Arrays.fill(dp[i][j][k] , -1) ; } } } int cas = 1 ; int t = in.nextInt() ; while(t-- > 0){ out.print("Case #" + (cas++) + ": ") ; out.println(sum(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt())) ; } out.flush() ; }}class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = new StringTokenizer(""); } private void eat(String s) { tokenizer = new StringTokenizer(s); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextInts(int n) { int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = nextInt(); } return nums; } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } }
阅读全文
2 2
- 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求回文数】
- HDU6156 Palindrome Function[数位DP]
- hdu6156 Palindrome Function数位dp
- hdu6156 Palindrome Function 数位dp
- HDU 6156 2016ICPC网络赛 G: Palindrome Function(数位DP)
- 【HDU6156】Palindrome Function(数位DP+回文串)
- Android中WebView点击事件的拦截跳转到原生的界面
- pd.value_counts(db)
- 事件对象
- ${sessionScope.user}的使用方法
- 编译多核大内存openwrt版本
- hdu 6156 Palindrome Function 数位DP
- OCLint的部分规则(Redundant 部分)
- CSS的position属性:relative和absolute
- 面向对象的特点
- 集成学习
- 爬虫框架--Scrapy学习笔记一
- jQuery DOM对象区别与联系
- OCLint的部分规则(Naming 部分)
- 问题: 当开启plsql 12工具 时, sublimi3爆卡