hdu 3709 数字dp(小思)

时间:2022-09-21 10:22:37

http://acm.hdu.edu.cn/showproblem.php?pid=3709

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
/**
hdu 3709 数位dp(小思维)
解题思路:有一个非常好的转化技巧,不然会超时。搜索的时候初始值定为f(x),然后最后和0比較。不要搜f(i) 和f(x)比較
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL ;
LL dp[25][25][2000],l,r;
int bit[25];
LL dfs(int len,int pos,int sum,int flag)
{
if(len<0)
{
//printf("%d %d>>>>\n",suml,sumr);
return sum==0;
}
if(flag==0&&dp[len][pos][sum]!=-1)
return dp[len][pos][sum];
int end=flag?bit[len]:9;
LL ans=0;
for(int i=0;i<=end;i++)
{
//printf("len-1:%d\n",len-1);
ans+=dfs(len-1,pos,(sum+(len-pos)*i),flag&&i==end);
}
if(flag==0)
{
dp[len][pos][sum]=ans;
}
return ans;
}
LL solve(LL n)
{
if(n==-1)return 0;
int len=0;
while(n)
{
bit[len++]=n%10;
n/=10;
}
LL ans=0;
for(int i=0;i<len;i++)
{
// printf("len-1:%d\n",len-1);
ans+=dfs(len-1,i,0,1);
}
return ans-len+1;
}
int main()
{
int T;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
while(T--)
{
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",solve(r)-solve(l-1));
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

hdu 3709 数字dp(小思)的更多相关文章

  1. hdu 3709 数位dp

    数位dp,有了进一步的了解,模板也可以优化一下了 题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数例如4139,以3为支点4*2 ...

  2. Balanced Number HDU - 3709 数位dp

    题意: 给出范围 算出 满足  选取一个数中任一一个 树作为支点  两边的数分别乘以到中心的距离和 左和等于右和   的数有多少个 数位DP题 状态转移方程为dp[pos][x][state]=dp[ ...

  3. &lbrack;zoj 3416&sol;hdu 3709&rsqb;数位DP

    题意:求从区间[L, R]内有多少个数是平衡数,平衡数是指以10进制的某一位为中心轴,左右两边的每一位到中心轴的距离乘上数位上的值的和相等.0<=L<=R<=1e18 思路:由于任何 ...

  4. 【HDU 3709】 Balanced Number (数位DP)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  5. 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A &plus; B Problem II&comma;数字的转化(反转),大数的加法&period;&period;&period;&period;&period;&period;)

    数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...

  6. HDU - 4734 F&lpar;x&rpar; (2013成都网络游戏,数字DP)

    意甲冠军:求0-B见面<=F[A]所有可能的 思维:数字DP,内存搜索 #include <iostream> #include <cstring> #include & ...

  7. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  8. HDU 2829 区间DP &amp&semi; 前缀和优化 &amp&semi; 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  9. HDU 3555 数位dp入门

    开始想用dp[i][j]来记录第i位j开头含有49的数的个数 但是init后并不知道如何进行cal 想了想可以用不要62的思想 当作不要49来做 然后减一下 就好 看网上的代码 不要62和这道题用的d ...

随机推荐

  1. PHP 知识结构

  2. VIM编辑器之常用命令

     分类: vim   目录(?)[-] 第一级 存活 第二级 感觉良好 第三级  更好更强更快 更好 更强 更快 第四级 Vim 超能力 在当前行上移动光标 0   f F t T 区域选择 acti ...

  3. npm install -g 全局安装总是出现permission权限问题的解决方案

    npm install -g 全局安装总是出现permission权限问题的解决方案 开始使用node的时候,在使用npm安装global packages时,习惯性地使用npm install -g ...

  4. IE input file隐藏不能上传文件解决方法

    当大神们都在探讨更深层次的问题时,我还在这里转载发些肤浅的问题解决方案.罢了,为了和我一样笨的后来人. 问题: 上传文件时,用<input type="file" /> ...

  5. 从3D Studio Max导入物体 Importing Objects From 3D Studio Max

    原地址:http://game.ceeger.com/Manual/HOWTO-ImportObjectMax.html If you make your 3D objects in 3dsMax, ...

  6. ORACLE SEQUENCE跳号总结

      在ORACLE数据库中,序列(SEQUENCE)是使用非常频繁的一个数据库对象,但是有时候会遇到序列(SEQUECNE)跳号(skip sequence numbers)的情形,那么在哪些情形下会 ...

  7. 从0到1上线一个微信小程序

    0.0 前期准备 微信小程序的出现极大地降低了个人开发者微创业的门槛,不需要后端技术,不需要服务器和域名这些乱七八糟的前置操作,只需要懂得前端技术,就能发布一款属于自己的轻量级应用,简直是前端开发者的 ...

  8. CSS之文本溢出隐藏,不定宽高元素垂直水平居中、禁止页面文本复制

    1.如何让不固定元素宽高的元素垂直水平居中 .center { position: absolute; top: 50%; left: 50%; background-color: #000; wid ...

  9. spring boot aop 报错

    1.控制台报错 Pointcut is not well-formed: expecting 'name pattern' at character position 33 execution(com ...

  10. 用php和ajax写一个省市区的三级联动,实现地区的下拉选择

    要实现这个页面的三级联动,我们需要建立三个php文件,第一个php文件我们导入jQuery文件,里面嵌入JavaScript:第二个php文件我们做一个php的处理页面,里面引入我们封装好的数据库类文 ...