hdu3709(数位DP)

debug了好久发现是数组开太小了!!!(砸

这样弄。。枚举平衡点位置然后直接数位DP即可,把比平衡点高位记为正,低位记为负,这样只要令最后结果为0即可

然后需要证明一下平衡数的平衡点是唯一的。。(其实是一开始忘了这种情况。。

对任意平衡数,以平衡点左移为例,左边的数的权重会变小(除非本身为0),右边的数的权重会变大(除非本身为0)

由于对正数来说,左边的数的权重是不可能为0的。。所以正数的平衡点最多只有一个。。

然而0就不一样了,左右2边的权重均为0,事实上哪个位置都可以作为他的平衡点,所以需要特判。。

其他细节和其他数位DP没什么两样。。




#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1000000007
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 20
#define nm 10005
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}


int n,_t,b[NM];
ll _x,_y,d[NM][nm];

ll dfs(int i,bool f,int t){
	if(t<0)return 0;
	if(!i)if(t==0)return 1;else return 0;
	if(!f&&d[i][t]>=0)return d[i][t];
	int m=f?b[i]:9;ll ans=0;
	inc(j,0,m)
		ans+=dfs(i-1,f&&j==m,j*(i-_t)+t);
	return d[i][t]=ans;
}

ll solve(ll x){
	ll s=0;n=0;
	for(ll t=x;t;t/=10)b[++n]=t%10;
	for(_t=1;_t<=n;_t++){
		memset(d,-1,sizeof(d));
		s+=dfs(n,1,0);
	}
	return s-n+1;
}

int main(){
	int _=read();
	while(_--){
		scanf("%lld%lld",&_x,&_y);
		printf("%lld\n",solve(_y)-solve(_x-1));
	}
	return 0;
}






Balanced Number

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


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 ≤ 10 18).
 

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 
 

Statistic |  Submit |  Discuss | Note

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值