hdu3709 (平衡数) 数位DP

时间:2022-09-23 09:30:16

Balanced Number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2945    Accepted Submission(s): 1348

Problem Description
A
balanced number is a non-negative integer that can be balanced if a
pivot is placed at some digit. More specifically, imagine each digit as a
box with weight indicated by the digit. When a pivot is placed at some
digit of the number, the distance from a digit to the pivot is the
offset between it and the pivot. Then the torques of left part and right
part can be calculated. It is balanced if they are the same. A balanced
number must be balanced with the pivot at some of its digits. For
example, 4139 is a balanced number with pivot fixed at 3. The torqueses
are 4*2 + 1*1 = 9 and 9*1 = 9, for left part and right part,
respectively. It's your job
to calculate the number of balanced numbers in a given range [x, y].
 
Input
The
input contains multiple test cases. The first line is the total number
of cases T (0 < T ≤ 30). For each case, there are two integers
separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 1018).
 
Output
For each case, print the number of balanced numbers in the range [x, y] in a line.
 
Sample Input
2
0 9
7604 24324
 
Sample Output
10
897
 
Author
GAO, Yuan
 
Source
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3711 3715 3718 3713 3712
枚举中心点,然后按位枚举,长度由len到0,算出当前权值之和pre,然后结束时判断pre值是否为0,为0,返回1,否则返回1。
用dp[len][cen][pre]保存长度为i,中心点为k,之前的权值之和为pre的数字有多少种的状态。
最后需要去除全是0的情况,因为全是0的话,中心点可随意枚举没,所以需要减去(len-1)。
 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
#define maxn 30
LL dp[maxn][maxn][]; // dp[i][k][j]代表长度为i,中心点为k,最高位到与最高位距离为i的位置经过的数字的权值之和j的数字有多少种
LL digit[maxn];
__int64 dfs(int len,int cen,int pre,bool fp) //dfs版本的纯属暴力枚举每一个数字,而递推版本的是考虑了前缀的影响
{
if(len==)
return pre==;
if(pre<)
return ;
if(!fp && dp[len][cen][pre] != -) //
{
return dp[len][cen][pre];
}
LL ret =;
int fpmax = fp ? digit[len] : ;
for(int i=;i<=fpmax;i++) //分别算出以i开头的数的方案数,
{
LL temp=dfs(len-,cen,pre+i*(len-cen),fp && i == fpmax);
ret+=temp;
}
if(!fp)
dp[len][cen][pre]= ret;
return ret;
} LL f(LL n)
{
if(n==-)
return ;
int len=;
while(n)
{
digit[++len] = n % ;
n /= ;
}
LL ans=;
for(int cen=;cen<=len;cen++)
{
ans+=dfs(len,cen,,true);
}
return ans-len+;
}
void init()
{
memset(dp,-,sizeof(dp));
}
int main()
{
// freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
init();
LL n,m;
scanf("%I64d%I64d",&n,&m);
LL ans1=f(m);
// init();
LL ans2=f(n-);
printf("%I64d\n",ans1-ans2);
}
return ;
}

hdu3709 (平衡数) 数位DP的更多相关文章

  1. HDU3709 Balanced Number —— 数位DP

    题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others)     ...

  2. 【BZOJ1662】&lbrack;Usaco2006 Nov&rsqb;Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

  3. 【BZOJ-1026】windy数 数位DP

    1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5230  Solved: 2353[Submit][Sta ...

  4. CCF 201312-4&Tab;有趣的数 &lpar;数位DP&comma; 状压DP&comma; 组合数学&plus;暴力枚举&comma; 推公式&comma; 矩阵快速幂&rpar;

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  5. bzoj 1026 &lbrack;SCOI2009&rsqb;windy数 数位dp

    1026: [SCOI2009]windy数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...

  6. hdu3709 Balanced Number 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...

  7. luogu P2657 &lbrack;SCOI2009&rsqb;windy数 数位dp 记忆化搜索

    题目链接 luogu P2657 [SCOI2009]windy数 题解 我有了一种所有数位dp都能用记忆话搜索水的错觉 代码 #include<cstdio> #include<a ...

  8. 【BZOJ 3326】&lbrack;Scoi2013&rsqb;数数 数位dp&plus;矩阵乘法优化

    挺好的数位dp……先说一下我个人的做法:经过观察,发现这题按照以往的思路从后往前递增,不怎么好推,然后我就大胆猜想,从前往后推,发现很好推啊,维护四个变量,从开始位置到现在有了i个数 f[i]:所有数 ...

  9. 洛谷P2657 &lbrack;SCOI2009&rsqb;windy数 &lbrack;数位DP,记忆化搜索&rsqb;

    题目传送门 windy数 题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个win ...

随机推荐

  1. VS2010项目缺少组件

    遇到的问题是解决方案中部分项目无法加载, 提示需要缺少的web组件才能加载该项目,是否通过WEB安装组件来网络安装, 点击确定以后就什么也没有了. 网上查阅了一番,结合自己的使用情况(在家里用的是vs ...

  2. python走起之第二话

    Python基础 一.整数(int) 如: 18.73.84 整数类的功能方法及举例: 带__的方法代表有多种表达方式 1.__abs__ <==> abs() 求整数的绝对值:(-11) ...

  3. GROUPING SETS、CUBE、ROLLUP

    其实还是写一个Demo 比较好 USE tempdb IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; G ...

  4. git命令详解(一)

    今天我们来详解一下git的各种命令,此为git的第一篇,后续还会有好几篇,希望大家看了能有所进步 第一篇的命令 1.git commit 2.git branch 3.git merge 4.git ...

  5. App Store 审核指南

    App Store 审核指南 https://developer.apple.com/app-store/review/guidelines/cn/ https://developer.apple.c ...

  6. typescript接口扩展、接口的继承

    //接口扩展:接口可以继承接口 // interface Animal{ // eat():void; // } // interface Person extends Animal{ // work ...

  7. MVP与MVC的区别

    MVP的主要思想就是解耦View和Model 先大致从图上看一下MVP和MVC又什么不同: MVC: M : Model 数据模型,就是对数据的封装和保存: V : View 视图界面,相当于布局文件 ...

  8. golang操作mysql数据库

    golang操作mysql数据库 代码: mysql的增.删.改.查 package main import ( "database/sql" "fmt" &q ...

  9. php设计模式五----适配器模式

    1.简介 适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁.这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能. 意图:将一个类的接口转换成客户希望的另外一个接口 ...

  10. 降维工具箱drtool

    工具箱下载:http://leelab.googlecode.com/svn/trunk/apps/drtoolbox/ ——————————————————————————————————————— ...