[HDU3709]Balanced Number

试题描述

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].

输入

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).

输出

For each case, print the number of balanced numbers in the range [x, y] in a line.

输入示例


输出示例


数据规模及约定

见“输入

题解

令 f[k][i][j][s] 表示考虑数的前 i 位,最高位为 j,支点在位置 k,支点右力矩 - 左力矩 = s 的数有多少个。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define LL long long LL read() {
LL x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 20
#define maxs 1800
LL f[maxn][maxn][10][maxs]; int num[maxn];
LL sum(LL x) {
if(!x) return 1;
int cnt = 0; LL tx = x;
while(x) num[++cnt] = x % 10, x /= 10;
LL ans = 0;
for(int i = cnt - 1; i; i--)
for(int k = 1; k <= i; k++)
for(int j = 1; j <= 9; j++) ans += f[k][i][j][0];
for(int i = cnt; i; i--) {
for(int k = cnt; k; k--) {
int s = 0;
for(int x = cnt; x > i; x--) s += (x - k) * num[x];
if(s < 0 || s >= maxs) continue;
for(int j = i < cnt ? 0 : 1; j < num[i]; j++) {
ans += f[k][i][j][s];
// if(!j && !s && i > 1) ans--;
}
}
}
for(int k = 1; k <= cnt; k++) {
int s = 0;
for(int x = 1; x <= cnt; x++) s += (x - k) * num[x];
if(!s){ ans++; break; }
}
ans++;
return ans;
} int main() {
for(int j = 0; j <= 9; j++) f[1][1][j][0] = 1;
for(int k = 2; k < maxn; k++)
for(int j = 0; j <= 9; j++) f[k][1][j][(k-1)*j] = 1;
for(int k = 1; k < maxn; k++)
for(int i = 1; i < maxn - 1; i++)
for(int j = 0; j <= 9; j++)
for(int s = 0; s < maxs; s++) if(f[k][i][j][s]) {
for(int x = 0; x <= 9 && s + (k - i - 1) * x >= 0; x++)
if(s + (k - i - 1) * x < maxs) f[k][i+1][x][s+(k-i-1)*x] += f[k][i][j][s];
// printf("%d %d %d %d: %lld\n", k, i, j, s, f[k][i][j][s]);
}
int T = read();
while(T--) {
LL l = read(), r = read();
LL ans = sum(r); if(l) ans -= sum(l - 1);
printf("%lld\n", ans);
} return 0;
}

[HDU3709]Balanced Number的更多相关文章

  1. HDU3709 Balanced Number —— 数位DP

    题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others)     ...

  2. hdu3709 Balanced Number (数位dp+bfs)

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

  3. HDU3709 Balanced Number (数位dp)

     Balanced Number Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Descript ...

  4. [暑假集训--数位dp]hdu3709 Balanced Number

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  5. hdu3709 Balanced Number 树形dp

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  6. hdu3709 Balanced Number 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...

  7. HDU3709 Balanced Number 题解 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意: 求区间 \([x, y]\) 范围内"平衡数"的数量. 所谓平衡 ...

  8. HDU3709:Balanced Number(数位DP+记忆化DFS)

    Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...

  9. HDU - 3709 - Balanced Number(数位DP)

    链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...

随机推荐

  1. Redis学习-开始

    C:\Program Files\Redis\redis-cli.exe 使用servicestack.redis class Program { static void Main(string[] ...

  2. 对Java垃圾回收最大的误解是什么

    当 我还是小孩的时候,父母常说如果你不好好学习,就只能去扫大街了.但他们不知道的是,清理垃圾实际上是很棒的一件事.可能这也是即使在Java的世界中, 同样有很多开发者对GC算法产生误解的原因--包括它 ...

  3. eclipse里怎么用命令行输入args

    eclipse中给java应用传args参数的方法如下:1.先写好Java代码,比如文件名为IntArrqy.java:2.在工具栏或菜单上点run as下边有个Run Configuration:3 ...

  4. nginx的简单操作

    1.今天在nginx下访问403,发现图片访问出现403,其他没有问题,看来下图片的文件夹没有读写的权限,修改下权限就OK了! 2.重启nginx 在env/nginx/sbin目录下输入:nginx ...

  5. Django笔记-字符编码相关问题整理

    1.添加中文注释后编译出错,提示:Non-ASCII   解决方法: 在Python脚本文件的第一行或第二行添加一句:      #coding:gbk或#coding:utf-8或##-*- cod ...

  6. JS从剪切板里粘贴图片

    功能需求:在网页中,Ctrl+V,把系统剪切板的图片(比如QQ截图)进行粘贴.显示.上传...,提高用户体验. 参考链接:https://ruby-china.org/topics/17266 git ...

  7. sql server 日期相关操作

    ), ): ), ): :57AM ), ): ), ): ), ): ), ): ), ): ), ): ), ): , ), ): :: ), ): :::827AM ), ): ), ): ), ...

  8. SAMBA 共享服务器搭建

    yum install samba service smb start chkconfig smb on 1.给要共享的文件夹赋权限 777 2.修改 smb 的配置文件:/etc/samba/smb ...

  9. flask 知识点总结

    ============================request对象的常用属性============================具体使用方法如下:request.headers, requ ...

  10. iOS: ARC & MRC下string内存管理策略探究

    ARC & MRC下string内存管理策略探究 前两天跟同事争论一个关于NSString执行copy操作以后是否会发生变化,两个人整了半天,最后写代码验证了一下,发现原来NSString操作 ...