【数位dp】Beautiful Numbers @2018acm上海大都会赛J

时间:2023-02-25 13:07:42

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 &lpar;数位DP&rpar;

    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 &lpar;线段树,循环节&rpar;

    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 &lpar;扫描线&rpar;

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

  8. 2018 ICPC上海大都会赛重现赛 D Thinking-Bear magic &lpar;几何&rpar;

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

  9. 牛客 Fruit Ninja 2018 ACM 上海大都会赛 &lpar;随机化算法&rpar;

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

随机推荐

  1. Quartz Java resuming a job excecutes it many times--转

    原文地址:http://*.com/questions/1933676/quartz-java-resuming-a-job-excecutes-it-many-times Q ...

  2. String的点点滴滴

    一.String 的 equals()到底比较的是什么?equals() 与 == 的区别? 当使用关系运算符==比较两个对象时,是比较两个对象使用的内存地址和内容是否相同,如果两个对象使用的是同一个 ...

  3. WinForm TreeView 三种状态

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { var node = e.N ...

  4. android ellipsize 属性详解

    TextView中内容过长时添加省略号的属性,即ellipsize 用法如下: 在XML文件中设置: android:ellipsize = "end" //省略号在结尾 andr ...

  5. version &grave;GLIBC&lowbar;2&period;17&&num;39&semi; not found 解决方法

    1.先查看是哪个函数用的是GLIBC_2.17 root@emb-pc:/home/emb/temp# nm lib61850.so | grep GLIBC_2.17 U clock_gettime ...

  6. s11&period;9 sar&colon;收集系统信息

    功能说明: 通过sar命令,可以全面地获取系统的CPU.运行队列.磁盘I/O.分页(交换区).内存.CPU中断和网络等性能数据. 语法格式 sar  option interval count sar ...

  7. linux分享四:cron系统

    cron相关文件: /etc/cron.monthly/ /etc/cron.weekly/ /etc/cron.daily/ /etc/cron.hourly/ /etc/cron.d/ /etc/ ...

  8. OneThink生成分类树方法(list&lowbar;to&lowbar;tree)使用!

    具体方法: Application / Common / Common / function.php 下的 224行: function list_to_tree($list, $pk='id', $ ...

  9. POI2014题解

    POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...

  10. wpf下使用NotifyIcon

    以前在winForm下使用过NotifyIcon,到wpf找不到了,在wpf下还是直接用WinForm里的那个NotifyIcon实现最小到系统托盘 定义一个NotifyIcon成员 : Notify ...