hdu 3709 数字dp(小思)
http://acm.hdu.edu.cn/showproblem.php?pid=3709
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].
2
0 9
7604 24324
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(小思)的更多相关文章
- hdu 3709 数位dp
数位dp,有了进一步的了解,模板也可以优化一下了 题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数例如4139,以3为支点4*2 ...
- Balanced Number HDU - 3709 数位dp
题意: 给出范围 算出 满足 选取一个数中任一一个 树作为支点 两边的数分别乘以到中心的距离和 左和等于右和 的数有多少个 数位DP题 状态转移方程为dp[pos][x][state]=dp[ ...
- [zoj 3416/hdu 3709]数位DP
题意:求从区间[L, R]内有多少个数是平衡数,平衡数是指以10进制的某一位为中心轴,左右两边的每一位到中心轴的距离乘上数位上的值的和相等.0<=L<=R<=1e18 思路:由于任何 ...
- 【HDU 3709】 Balanced Number (数位DP)
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- HDU - 4734 F(x) (2013成都网络游戏,数字DP)
意甲冠军:求0-B见面<=F[A]所有可能的 思维:数字DP,内存搜索 #include <iostream> #include <cstring> #include & ...
- hdu 4507 数位dp(求和,求平方和)
http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
- HDU 3555 数位dp入门
开始想用dp[i][j]来记录第i位j开头含有49的数的个数 但是init后并不知道如何进行cal 想了想可以用不要62的思想 当作不要49来做 然后减一下 就好 看网上的代码 不要62和这道题用的d ...
随机推荐
- ubuntu下一个jboss-seam-2.2.2.Final/examples/build.xml:754: warning: 'includeantruntime' was not set
[javac] /home/huihui/app/jboss-seam-2.2.2.Final/examples/build.xml:754: warning: 'includeantruntime' ...
- (五岁以下儿童)NS3样本演示:桥模块演示样品csma-bridge.cc凝视程序
(五岁以下儿童)NS3:桥模块演示样品csma-bridge.cc凝视程序 1.Ns3 bridge模csma-bridge.cc演示示例程序的目光 // Network topology // // ...
- hdu 1298 T9(特里+DFS)
pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...
- Python 统计Facebook用户爱好的个数
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...
- 鸟哥Linux私房菜知识汇总8至9章
一看最近<鸟哥Linux私房菜>. 这是一个基本的书,万丈高楼平地起,学. 这是我整理的一些知识点.尽管非常基础. 希望和大家共同交流. 第8章 Linux磁盘与文件系统管理 一.Linu ...
- ServletContext加入和访问
(1)关于ServletContext认识: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5L2T/f ...
- WPF学习(8)数据绑定
说到数据绑定,其实这并不是一个新的玩意儿.了解asp.net的朋友都知道,在asp.net中已经用到了这个概念,例如Repeater等的数据绑定.那么,在WPF中的数据绑定相比较传统的asp.net中 ...
- HDU 1272 小希迷宫(并检查集合)
意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)
主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...
- 怎么样sourceforge开源项目发现,centos安装-同htop安装案例
一个.htop什么? top是linux下经常使用的监控程序.htop相当于其加强版,颜色显示不同參数.且支持鼠标操作. 详细介绍參看此说明文档. watermark/2/text/aHR0cDovL ...