HDU 3709 Balanced Number (数位DP)
题意:
找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数。
思路:
一开始以为需要枚举位数,枚举前缀和,枚举后缀和,一旦枚举起来就会MLE。
其实只需要3维 [第几位][和][轴位置],对于轴的位置是需要枚举的,每个位都是有可能的,比如900和7都是一个平衡数。注意这道题的区间下限可能为0,而0也是平衡数,这在拆十进制的时候len=0的,最好将0特处理。
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x7f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=; LL f[N][][N], bit[N];
//[第几位][和][轴] LL dfs(int i,int sum,int mid,bool e)
{
if(i==) return sum==;
if(sum< || sum>=) return ;
if(!e && ~f[i][sum][mid]) return f[i][sum][mid]; LL ans=;
int u= e? bit[i]: ;
for(int d=; d<=u; d++)
{
if(sum== && i==mid && d==) continue; //首位是mid,必须不为0
ans+=dfs(i-, sum+(i-mid)*d, mid, e&&d==u); //注意不能为负
}
return e? ans: f[i][sum][mid]=ans;
} LL cal(LL n)
{
if(n<) return ;
int len=;
while(n) //拆数
{
bit[++len]=n%;
n/=;
}
LL ans=; //dfs是没有统计0的,因为len=0是不会执行dfs的
for(int i=; i<=len; i++)
ans+=dfs(len, , i, true);
return ans;
} int main()
{
//freopen("input.txt","r",stdin);
memset(f,-,sizeof(f));
LL L,R;int t;cin>>t;
while( t-- )
{
cin>>L>>R;
cout<<cal(R)-cal(L-)<<endl;
}
return ;
}
AC代码
HDU 3709 Balanced Number (数位DP)的更多相关文章
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu 3709 Balanced Number(平衡数)--数位dp
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 3709 Balanced Number(数位DP)题解
思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...
- HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)
平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...
- HDU - 3709 - Balanced Number(数位DP)
链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- HDU 5787 K-wolf Number 数位DP
K-wolf Number Problem Description Alice thinks an integer x is a K-wolf number, if every K adjacen ...
- hdu3709 Balanced Number (数位dp+bfs)
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
- HDU3709:Balanced Number(数位DP+记忆化DFS)
Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
随机推荐
- In-App Purchase Configuration Guide for iTunes Connect---(一)----Introduction
Introduction In-App Purchase is an Apple technology that allows your users to purchase content and s ...
- Linux 下安装配置 JDK7(2)
Linux 下安装配置 JDK7 自从从Oracle收购Sun近三年来,已经有很多变化.早在8月,甲骨文将"Operating System Distributor License for ...
- gSoap使用入门(2)----自定义接口头文件
摘自:http://blog.csdn.net/zhuzhihai1988/article/details/8131556 接口头文件的格式在向导中没有看到明确的说明性的内容,但通过看开发包中示例程序 ...
- switch()出现警告warning:enumeration value ‘xxx’ not handled in switch
enumeration value ... not handled in switch 如果在switch中使用使用枚举类型,如:switch(枚举类型) 枚举类型的个数没有全部列出,就会报这个警告
- Laravel框架的一些配置
服务器的配置 1.在apache下的配置 配置httpd-conf:php5_module.rewrite_module.Listen 配置extra/httpd-vhost:端口.站点.域名.默认首 ...
- C/C++ Sleep(0)
Sleep(0) 的意义是放弃当前线程执行的时间片,把自身放到等待队列之中.这时其它的线程就会得到时间片进行程序的程序.Sleep(0)能够降低当前线程的执行速 度,比如:现在系统中有100个线程(先 ...
- CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
- 327. Count of Range Sum(inplace_marge)
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- python创建矩阵
创建二维数组的办法 直接创建(不推荐) 列表生产式法(可以去列表生成式 - 廖雪峰的官方网站学习) 使用模块numpy创建 举个栗子: 创建一个3*3矩阵,并计算主对角线元素之和. import nu ...
- PJzhang:kali linux安装网易云音乐、Visual Studio Code、skype
猫宁!!! 参考链接:https://blog.csdn.net/cloudatlasm/article/details/79183583 https://code.visualstudio.com/ ...