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. Hyper-V 替换 vmwp

    要激活 Hyper-V 下的虚机 最简单的方法是用带证书的vmwp替换掉原来的 带证书的vmwp参见:http://bbs.pcbeta.com/viewthread-1408240-1-1.html ...

  2. Git 从 master 分支拉新分支开发

    一. 切换到被copy的分支(master),并且从远端拉取最新版本 $git checkout master $git pull 二.从当前分支拉copy开发分支 $git checkout -b ...

  3. Pytorch学习笔记

    非线性回归问题的参数求解,反向求导基本流程.Variable 计算时, 它在后台一步步默默地搭建着一个庞大的系统, 叫做计算图, computational graph. 这个图将所有的计算步骤 (节 ...

  4. 【bzoj4347】[POI2016]Nim z utrudnieniem dp

    题解: 感觉我简直是个傻逼 把题目数据范围看错了.. 然后觉得这题非常的不可做 sigmaai <1e7.... 这题的dp是非常简单的,注意到d很小 f[i][j][k]表示前i个,%d为j, ...

  5. URL地址编码和解码

    0. 参考 [整理]关于http(GET或POST)请求中的url地址的编码(encode)和解码(decode) python3中的urlopen对于中文url是如何处理的? 中文URL的编码问题 ...

  6. Centos6.5下通过shell脚本快速安装samba服务器

    使用方法如下: 上传脚本到linux服务器授权

  7. Linux CA证书与https讲解

    1.什么是CA证书. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 B 公司的所有人都不认识他,他咋办捏?常用的办法是带公司开的一张介绍信,在 ...

  8. python---初始sqlite3

    ***sqllite不需要单独安装,python2.5以上自带的! ***官方中文文档:https://docs.python.org/2/library/sqlite3.html ***SQLite ...

  9. HDU2449 Gauss Elimination 高斯消元 高精度 (C++ AC代码)

    原文链接https://www.cnblogs.com/zhouzhendong/p/HDU2449.html 题目传送门 - HDU2449 题意 高精度高斯消元. 输入 $n$ 个 $n$ 元方程 ...

  10. day32 process模块用法

    昨日作业: 服务端: 服务端: from socket import * from multiprocessing import Process def server(ip,port): server ...