题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156

题意:如题。

解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做一个数位DP就好了。dp[jz][start][cur][state]表示jz进制下以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL dp[40][40][40][2];
LL temp[100], num[100];
//dp[start][cur][state]表示以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数
LL dfs(int jz, int start, int cur, bool state, bool jud){
if(cur<0) return state;
if(!jud&&dp[jz][start][cur][state]!=-1) return dp[jz][start][cur][state];
int mx = jud?num[cur]:jz-1;
LL ret=0;
for(int i=0; i<=mx; i++){
temp[cur]=i;//枚举该位的值
if(start==cur&&i==0)//前导0
ret += dfs(jz,start-1,cur-1,state,jud&&i==mx);
else if(state&&cur<(start+1)/2)//已经构成回文串
ret+=dfs(jz,start,cur-1,temp[start-cur]==i,jud&&i==mx);
else//尚未构成回文串
ret+=dfs(jz,start,cur-1,state,jud&&i==mx);
}
if(!jud) dp[jz][start][cur][state]=ret;
return ret;
}
LL f(LL n, LL jz){
int len = 0;
while(n){
num[len++]=n%jz;
n/=jz;
}
num[len]=0;
return dfs(jz,len-1,len-1,1,1);
}
LL L,R,l,r;
int main()
{
int T,ks=0;
scanf("%d", &T);
memset(dp,-1,sizeof(dp));
while(T--)
{
LL ans = 0;
scanf("%lld%lld%lld%lld",&L,&R,&l,&r);
for(LL i=l; i<=r; i++){
if(L>R) swap(L,R);
LL ret = f(R,i)-f(L-1,i);
ans += ret*i+(R-L+1-ret);
}
printf("Case #%d: %lld\n", ++ks, ans);
}
return 0;
}

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP的更多相关文章

  1. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...

  2. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph(暴力搜索)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6152 Problem Description It is well known that small ...

  3. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6150 Vertex Cover 二分图,构造

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6150 题意:"最小点覆盖集"是个NP完全问题 有一个近似算法是说—每次选取度数最大 ...

  4. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff 思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角 ...

  5. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph 暴暴暴暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6152 题意:判定一个无向图是否有三个点的团或者三个点的独立集. 解法:Ramsey theorem,n ...

  6. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...

  7. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff(几何找规律)

    Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in ...

  8. 【2017中国大学生程序设计竞赛 - 网络选拔赛 && hdu 6154】CaoHaha's staff

    [链接]点击打开链接 [题意] 给你一个面积,让你求围成这个面积最少需要几条边,其中边的连线只能是在坐标轴上边长为1的的线或者是两个边长为1 的线的对角线. [题解] 找规律题 考虑s[i]表示i条边 ...

  9. 【2017中国大学生程序设计竞赛 - 网络选拔赛 hdu 6150】Vertex Cover

    [链接]点击打开链接 [题意] 有人写了一个最小点覆盖的贪心算法,然后,让你去hack它. 并且,要求这个算法得到的错误答案,是正确答案的三倍. 让你任意输出hack数据,点数<=500 [题解 ...

随机推荐

  1. Django Models相关

    Models的相关知识 1. AutoField:自增整数类型.根据 ID 自增长的 Int字段 2. IntegerField:整数类型 3. BigIntegerField:大整数类型.用于数值较 ...

  2. JUC包中的分而治之策略-为提高性能而生

    一.前言 本次分享我们来共同探讨JUC包中一些有意思的类,包含AtomicLong & LongAdder,ThreadLocalRandom原理. 二.AtomicLong & Lo ...

  3. BZOJ4945 & 洛谷3825 & UOJ317:[NOI2017]游戏——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4945 https://www.luogu.org/problemnew/show/P3825 ht ...

  4. 2017 ccpc哈尔滨 A题 Palindrome

    2017 ccpc哈尔滨 A题 Palindrome 题意: 给一个串\(T\),计算存在多少子串S满足\(S[i]=S[2n−i]=S[2n+i−2](1≤i≤n)\) 思路: 很明显这里的回文串长 ...

  5. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  6. bzoj4822: [Cqoi2017]老C的任务(扫描线+BIT/线段树)

    裸题... 依旧是写了BIT和线段树两种(才不是写完线段树后才想起来可以写BIT呢 怎么卡常数都挺大...QAQ ccz和yy的写法好快哇%%% BIT: #include<iostream&g ...

  7. 修改tomcat编码格式 & tomcat发布WEB项目供外网访问

    1.修改tomcat默认编码格式: 修改tomcat下的conf/server.xml文件,找到如下代码:       <Connector port="8080" prot ...

  8. Codeforces Round #402 (Div. 2) A B C sort D二分 (水)

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. URAL - 1627:Join (生成树计数)

    Join 题目链接:https://vjudge.net/problem/URAL-1627 Description: Businessman Petya recently bought a new ...

  10. [技巧篇]08.Struts2拦截器中获取Servlet API方法

    讲课中遇到的解决Session拦截器的后腿问题,还有如何在拦截器中获取Servlet API,这里留一个备注,方便学生查找