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. [zoj 3416/hdu 3709]数位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 + B Problem II,数字的转化(反转),大数的加法......)

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

  6. HDU - 4734 F(x) (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 & 前缀和优化 & 四边形不等式优化

    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. 杭电1171 Big Event in HDU(母函数+多重背包解法)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. Unity3D 表对象分类中的实现(C#)

    // Sort by distance in descending order private void SortTargetsByDistance () { targets.Sort(delegat ...

  3. 求Sn=a+aa+aaa+…+aa…aaa(有n个a)…

    时间限制: 1 Sec  内存限制: 128 MB 提交: 352  解决: 174 [提交][状态][讨论版] 题目描述 求Sn=a+aa+aaa+-+aa-aaa(有n个a)之值,其中a是一个数字 ...

  4. 伪异步IO理解

    伪异步IO实在堵塞IO的基础上将每个client发送过来的请求由新创建的线程来处理改进为用线程池来处理.因此避免了为每个client请求创建一个新线程造成的资源耗尽问题. 来看一下伪异步IO的服务端代 ...

  5. iPhone发展【一】从HelloWorld开始

    转载请注明出处.原文网址:http://blog.csdn.net/m_changgong/article/details/8013553 作者:张燕广 从经典的HelloWorld開始踏入iPhon ...

  6. S性能 Sigmoid Function or Logistic Function

    S性能 Sigmoid Function or Logistic Function octave码 x = -10:0.1:10; y = zeros(length(x), 1); for i = 1 ...

  7. net大型分布式电子商务架构

    net大型分布式电子商务架构 背景 构建具备高可用,高扩展性,高性能,能承载高并发,大流量的分布式电子商务平台,支持用户,订单,采购,物流,配送,财务等多个项目的协作,便于后续运营报表,分析,便于运维 ...

  8. Android开发学习总结——Android开发的一些相关概念(转)

    一.什么是3G.4G 1995年问世的第一代模拟制式手机(1G)只能进行语音通话. 1996到1997年出现的第二代GSM.CDMA等数字制式手机(2G)便增加了接收数据的功能 Ÿ 3G指的是第三代移 ...

  9. HTML5中类jQuery选择器querySelector的高级使用 document.querySelectorAll.bind(document);

    基本用法 querySelector 该方法返回满足条件的单个元素.按照深度优先和先序遍历的原则使用参数提供的CSS选择器在DOM进行查找,返回第一个满足条件的元素. ----> querySe ...

  10. cocos2dx 3.x Value、Vector和Map意识

    1. Value cocos2d::Value 这包括一个非常大的数字原生类型(int,float,double,bool,unsigned char,char* 和 std::string)外 加s ...