Beautiful Numbers

PROBLEM

题目描述

NIBGNAUK is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by the sum of its digits.

We will not argue with this and just count the quantity of beautiful numbers from 1 to N.

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.

Each test case contains a line with a positive integer N (1 ≤ N ≤ 1012).

输出描述:

For each test case, print the case number and the quantity of beautiful numbers in [1, N].

输入

2

10

18

输出

Case 1: 10

Case 2: 12

MEANING

t组测试用例,每组测试用例给一个整数n,询问从1到n中有多少数能被其各位数之和整除。

SOLUTION

对于每个整数n,找到从1到n的每个数的各位数和的最大值k。

从1到k枚举各位数之和。

对于每个各位数之和,相当于在1到n中找有多少数能整除这个和,且各位数之和等于这个和。这样就将问题分解成若干子问题。

接下来就是数位dp套板子。具体可以看代码中的注释。

CODE

#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#define IN_TEST() freopen("","r",stdin)
#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_PC() freopen("C:\\Users\\hz\\Desktop\\out.txt","w",stdout)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1010; ll dp[15][100][100];
bool q[15][100][100];
int b[15], tot;
//pos:枚举到哪一位 sum:最高位到当前位的和 remain:当前枚举的数除以各位数和的余数
ll dfs(int pos, int sum, int remain, int meiju, bool limit) {
if(pos == 0) return (remain == 0) ? 1ll : 0;
if(limit && q[pos][sum][remain]) return dp[pos][sum][remain];//记忆化搜索
ll res = 0;
int up = limit?9:b[pos];
for(int i = 0; i <= up; i++) {
if(i + sum <= meiju && i + sum + 9 * (pos - 1) >= meiju)
res += dfs(pos-1, sum+i, (remain*10+i) % meiju, meiju, limit || (i!=b[pos]));
}
if(limit) {
q[pos][sum][remain] = true;
dp[pos][sum][remain] = res;
}
return res;
} ll solve(ll num) {
int sum = 0;
tot = 0;
while(num) {
sum += num % 10;
b[++tot] = num % 10;
num /= 10;
}
int ret;
if(tot == 1)ret = b[tot];
else ret = b[tot] - 1 + (tot - 1) * 9;
sum = max(sum, ret);//找到各位数之和的最大值
ll ans = 0;
for(int i = 1; i <= sum; i++) {//枚举各位数之和,解决子问题
memset(dp, 0, sizeof dp);
memset(q, 0, sizeof q);
ans += dfs(tot, 0, 0, i, false);
}
return ans;
} int main() {
// IN_PC();
int T;
scanf("%d", &T);
for(int ca = 1; ca <= T; ca++) {
ll n;
scanf("%lld", &n);
printf("Case %d: ", ca);
printf("%lld\n", solve(n));
}
return 0;
}

【数位dp】Beautiful Numbers @2018acm上海大都会赛J的更多相关文章

  1. The 2018 ACM-ICPC上海大都会赛 J Beautiful Numbers (数位DP)

    题意:求小于等于N且能被自己所有位上数之和整除的数的个数. 分析:裸的数位dp.用一个三位数组dp[i][j][k]记录:第i位,之前数位之和为j,对某个mod余数为k的状态下满足条件的个数.这里mo ...

  2. 2018ACM上海大都会赛 F Color it【基础的扫描线】

    题目:戳这里 题意:有n*m个点全为白色,q个圆,将q个圆内所有的点都染成黑色,问最后剩下多少白色的点. 解题思路:每一行当做一个扫描线,扫描所有的圆,记录每一行在圆中的点即可,O(n*q). 附ac ...

  3. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  4. 2018 ACM 国际大学生程序设计竞赛上海大都会赛

    传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...

  5. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  6. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  7. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...

  8. 2018 ICPC上海大都会赛重现赛 D Thinking-Bear magic (几何)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 D Thinking-Bear magic (几何) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  9. 牛客 Fruit Ninja 2018 ACM 上海大都会赛 (随机化算法)

    题目链接:Fruit Ninja 比赛链接:2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 题目描述 Fruit Ninja is a juicy action game enjoyed ...

随机推荐

  1. 铺放骨牌 uva11270

    题解: 插头dp裸题 没什么好说的啊就是n个二进制位表示状态 相比原先就是用2n个二进制位表示状态 蓝书上后面几题插头dp都挺烦的啊... 代码:

  2. NEST - 返回部分文档

    Selecting fields to return Version:5.x 英文原文地址:Selecting fields to return 有时候,不需要让 Elasticsearch 返回查询 ...

  3. 如何确定系统上的CPU插槽数量

    环境 Red Hat Enterprise Linux 7 Red Hat Enterprise Linux 6 Red Hat Enterprise Linux 5 Red Hat Enterpri ...

  4. JQ JS复制到剪贴板

    示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  5. c#代码文件上传和下载

    public JsonResult UploadFile(DriverFileManager filem)        {                       var hfc = Syste ...

  6. 基于C语言的Socket网络编程搭建简易的Web服务器(socket实现的内部原理)

    首先编写我们服务器上需要的c文件WebServer.c 涉及到的函数API: int copy(FILE *read_f, FILE * write_f) ----- 文件内容复制的方法 int Do ...

  7. maya shell 和 UV shell 的区别

    maya shell 和 UV shell 的区别 shell 是 maya 模型自身分离的部分 UV shell 是 UV 分离的部分 有多少个shell,就至少有多少个 UV shell,但是一个 ...

  8. (openssl_pkey_get_private 函数不存在)phpstudy开启openssl.dll 时提示httpd.exe 丢失libssl-1_1.dll

    下载libssl-1_1.dll  丢到apache目录下的bin目录下(貌似要32位的)

  9. 001.Linux开机启动过程

    相关Linux启动过程解析,此作为通用启动参考:

  10. Java NIO- 最好文档

    http://www.cnblogs.com/puyangsky/p/5840873.html 1 背景介绍 在上一篇文章中我们介绍了Java基本IO,也就是阻塞式IO(BIO),在JDK1.4版本后 ...