2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP
题目链接: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的更多相关文章
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph(暴力搜索)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6152 Problem Description It is well known that small ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6150 Vertex Cover 二分图,构造
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6150 题意:"最小点覆盖集"是个NP完全问题 有一个近似算法是说—每次选取度数最大 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff 思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为 n 的简单多边形,每次只能画一条边或者一个格子的对角 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6152 Friend-Graph 暴暴暴暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6152 题意:判定一个无向图是否有三个点的团或者三个点的独立集. 解法:Ramsey theorem,n ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff(几何找规律)
Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in ...
- 【2017中国大学生程序设计竞赛 - 网络选拔赛 && hdu 6154】CaoHaha's staff
[链接]点击打开链接 [题意] 给你一个面积,让你求围成这个面积最少需要几条边,其中边的连线只能是在坐标轴上边长为1的的线或者是两个边长为1 的线的对角线. [题解] 找规律题 考虑s[i]表示i条边 ...
- 【2017中国大学生程序设计竞赛 - 网络选拔赛 hdu 6150】Vertex Cover
[链接]点击打开链接 [题意] 有人写了一个最小点覆盖的贪心算法,然后,让你去hack它. 并且,要求这个算法得到的错误答案,是正确答案的三倍. 让你任意输出hack数据,点数<=500 [题解 ...
随机推荐
- html页面导入文件 使用include后多出一空白行的解决
用include引入的footer和header文件都在上面多出一空白行,是Unicode签名(bom)引起的. “标题/编码”,把 包括unicode签名(bom) 的勾取消就好了.
- P1483 序列变换
题目描述 给定一个由n个整数构成的序列,你需要对它进行如下操作: 操作1:输入格式“1 x y”,表示把所有a[kx](k为正整数,kx<=n)都加上y. 操作2:输入格式“2 j”,表示输出a ...
- [CF1095F]Make It Connected
题目大意:给你$n(n\leqslant2\times10^5)$个点和$m(m\leqslant2\times10^5)$条边,第$i$个点点权为$a_i$.连接$u,v$两个点的代价为$a_u+a ...
- BZOJ1008:[HNOI2008]越狱——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1008 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中 ...
- BZOJ3998:[TJOI2015]弦论——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3998 https://www.luogu.org/problemnew/show/P3975 对于 ...
- BZOJ1086:[SCOI2005]王室联邦——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1086 题面源于洛谷. 题目描述 “余”人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每 ...
- HDU3949:XOR——题解
http://acm.hdu.edu.cn/showproblem.php?pid=3949 求n个数的异或和第k小. 参考:https://blog.sengxian.com/algorithms/ ...
- [Leetcode] Maximum depth of binary tree二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- oracle中varchar2字段存入blob字段及blob转成varchar2
CREATE OR REPLACE FUNCTION C2B (b IN CLOB default empty_clob()) RETURN BLOB -- typecasts BLOB to CLO ...