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. Java服务器下载速度的限制

    没有取之不尽,用之不竭的资源.server有限的带宽.运营商可以限制一点点.近期使用云存储openstack swift待办事项文件存储下载.如果第一个限速code: private Long wri ...

  2. POJ 2405 Beavergnaw (计算几何-简单的问题)

    Beavergnaw Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6203   Accepted: 4089 Descri ...

  3. [原创].NET 业务框架开发实战之六 DAL的重构

    原文:[原创].NET 业务框架开发实战之六 DAL的重构 .NET 业务框架开发实战之六 DAL的重构 前言:其实这个系列还是之前的".NET 分布式架构开发实战 ",之所以改了 ...

  4. Tomcat—怎样在Tomcat Webserver下部署Web项目

            总结一下怎样在Tomcat Webserver下部署Web项目:

  5. (初稿)SQL Server 复制(Replication)系列(2)——事务复制搭建

    原文:(初稿)SQL Server 复制(Replication)系列(2)--事务复制搭建 本文演示如何搭建最基本的事务复制. 环境准备: 虚拟机2台: 服务器名分别为RepA和RepB,RepA为 ...

  6. AndroidAndroid程序提示和消息button响应事件

    首先,接口XML加入button响应函数 android:onClick="OnMyClick" <Button android:id="@+id/button1& ...

  7. JSP简明教程(四):EL表达式语言、JavaBean、Cookie、Session

    EL表达式语言 EL这是Expression Language.的目的是为了简化JSP句法.来看几个例子来清除. ${test} 它会被翻译成<%=test%> ${test.name} ...

  8. SQL入门学习6-集合运算

    7-1 表的加减法 集合运算 集合运算就是对满足同一规则的记录,进行的加减等四则运算. 1.1 表的加法--UNION 表之间进行并集运算. 语法: SELECT 对应列1,对应列2-- FROM 表 ...

  9. 大约 C++ 几个方面分析--overload, override, overwrite, rewrite

    overload, override, overwrite, rewrite 这几个单词常常出如今 C++ 书中,翻阅一些译版后发现并未对 override, overwrite, rewrite 严 ...

  10. BCP导出导入

    BCP导出导入大容量数据实践   前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储过程,生成所需的数据文件,甚至可以生成包含SQL语句和数据的脚本文件.各有优 ...