hdu3709(数位dp)
求区间[l,r]内有多少个数的满足: 选一个位为中点,是的左边的数到该位的距离等于右边的位到该位的距离。
比如4139 选择3位中点, 那么左边的距离是 4 * 2 + 1 * 1 , 右边的距离是9 * 1
想了半天,想到了枚举哪一位作为中点, 然后进行数位dp, 但是样例错了, 忽然想到会重复啊,就百度了一下
原来只有0在枚举中点时会重复,其他的数有且只有一个中点是的左边的距离等于右边的距离
所以只要最后ans-len+1即可
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#pragma warning(disable:4996)
using namespace std;
typedef long long LL;
const int INF = 0x7fffffff;
const int mod = ;
LL dp[][][];
int num[]; LL dfs(int cur, int mid, int diff, bool flag)
{
if (cur == ) return diff == ;
if (!flag && dp[cur][mid][diff] != -)
return dp[cur][mid][diff];
LL ret = ;
int end = flag ? num[cur] : ;
for (int i = ; i <= end; ++i)
{
int newDiff = diff;
if (cur > mid)
newDiff += (cur - mid) * i;
else if (cur < mid)
newDiff -= (mid - cur) * i;
ret += dfs(cur - , mid, newDiff, i == end && flag);
}
if (!flag)
dp[cur][mid][diff] = ret;
return ret;
}
LL calc(LL n)
{
int len = ;
while (n)
{
num[++len] = n % ;
n /= ;
}
LL ret = ; for (int i = ; i <= len; ++i)
ret += dfs(len, i, ,true);
//0每次枚举中点时都被计算进去了,所以要剪掉
return ret - len + ; }
int main()
{
memset(dp, -, sizeof(dp));
int t;
LL l, r;
scanf("%d", &t);
while (t--)
{
scanf("%I64d%I64d", &l, &r);
if (l == )
printf("%I64d\n", calc(r));
else
printf("%I64d\n", calc(r) - calc(l - ));
}
return ;
}
hdu3709(数位dp)的更多相关文章
- hdu3709 数位dp
枚举fix所在的位置loc即可,然后数位dp即可 这题要注意一种特殊情况,就是所有位都是0的时候对于每个fix都是成立的 /* dp[i][j][k]表示前i位确定了平衡点在第j位,前i位和为k fi ...
- HDU3709 Balanced Number (数位dp)
Balanced Number Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- [暑假集训--数位dp]hdu3709 Balanced Number
A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...
- HDU3709 Balanced Number 题解 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...
- 几道数位DP
因为这几天写的几道数位DP大多都太水..而且也确实没什么好讲所以就扔到一起了. [hdu4772]Good Numbers 要求统计区间内 各位数之和能被10整除 的数的个数. 练手,f[i][j][ ...
- 蒟蒻的数位DP专题总结
BZOJ 1026: [SCOI2009]windy数: 题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1026 d ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- bzoj1026数位dp
基础的数位dp 但是ce了一发,(abs难道不是cmath里的吗?改成bits/stdc++.h就过了) #include <bits/stdc++.h> using namespace ...
- uva12063数位dp
辣鸡军训毁我青春!!! 因为在军训,导致很长时间都只能看书yy题目,而不能溜到机房鏼题 于是在猫大的帮助下我发现这道习题是数位dp 然后想起之前讲dp的时候一直在补作业所以没怎么写,然后就试了试 果然 ...
随机推荐
- Swift - 触摸事件(点击,移动,抬起等)说明及用例
在iOS开发中,UIGestureRecognizer可以方便的响应处理手势事件. 而如果要想更精细的处理,我们还需要借助touchesBegan,touchesMoved,touchesEnded等 ...
- win32用GDI+加载png图片作为背景图
#include <windows.h> #include <gdiplus.h> /* GDI+ startup token */ ULONG_PTR gdiplusStar ...
- Android设计中的.9.png与Android Studio中的设置
在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以任意调整大小的一种图片格式“.9.png”.这种图片是用于Android开发的一种特殊的图片格式,它的 ...
- H面试程序(4):翻转句子中单词的顺序 .
题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. 句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. 例如输入“I am a student.”,则输出“stude ...
- cannot run program "git.exe":CreateProcess error=2
在使用android studio从git上check项目的时候报错cannot run program "git.exe":CreateProcess error=2 请检查下面 ...
- RealThinClient学习(一)
服务端代码: unit RtcHttpServer; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- [Word使用笔记]分类简介
什么Vistual Studio , Eclipse , Xcode , 都弱爆了,Word比他们难多了 - -! 此分类用于记录Word的一些使用
- Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计)
Cocos2d-x 3.1.1 Lua实例-AccelerometerTest(重力加速计) 本篇博客介绍Cocos2d-x的第一个实例--重力加速计測试.效果图(注:这里无法模拟重力感应): --[ ...
- Json for Java API学习
首先声明:本文来个非常多网友的博客,我通过參考了他们的博客,大致的了解了一些项目中经常使用的Json in java 类和方法,以及关于json的个人理解 个人对json的一些简单理解 在近期的学习中 ...
- SmartDraw2008破解过程总结
SmartDraw2008破解过程总结作者:chszs 原创转载请保留作者名. 按下列步骤完毕,保证能够支持中文. 一.所需软件:1)SmartDraw2008安装软件:2)SmartDraw200 ...