链接:https://ac.nowcoder.com/acm/problem/17385
来源:牛客网

题目描述

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 ≤ 1e12)

输出描述:

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

输入

2
10
18

输出

Case 1: 10
Case 2:
 
题意:给你一个数n让你判断在1-n中有多少个数求余它每个位上的数字之和为0
题解:
由于给的数字较大,暴力跑肯定会超时,就想到用数位dp去做,因为最大范围是1e12,则每个位上的数之和一定不大于12*9=108,则求出范围内各个数上和的最大值x,再从1枚举到x,进行数位dp.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int num[20];
ll dp[15][120][120];
int too;
ll dfs(int pos,int mod,int sum,bool limit)
{
if(pos==-1)
return mod==0&&sum==too;
if(!limit&&dp[pos][mod][sum]!=-1)
return dp[pos][mod][sum];
int mx=limit?num[pos]:9;
ll ans=0;
for(int i = 0;i <= mx;++i)
{
if(sum+i<=too)
ans+=dfs(pos-1,(mod*10+i)%too,sum+i,limit&&i==mx);
}
if(!limit)
dp[pos][mod][sum]=ans;
return ans;
}
ll solve(ll x)
{
int d=0;
int ret=0;
while(x)
{
int temp=x%10;
num[d++]=temp;
x/=10;
ret+=temp;
}
int nx=num[d-1]-1+(d-1)*9;
ret=max(ret,nx);
ll ans=0;
for(int i = 1;i <= ret;++i)
{
memset(dp,-1,sizeof(dp));
too=i;
ans+=dfs(d-1,0,0,1);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
int res=0;
while(t--)
{
ll a;
res++;
scanf("%lld",&a);
ll ans=solve(a);
printf("Case %d: ",res);
printf("%lld\n",ans);
}
return 0;
}

  

Beautiful Numbers(牛客网)的更多相关文章

  1. 牛客网-Beautiful Land 【01背包 + 思维】

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Now HUST got a big land whose capacity is C to p ...

  2. 牛客网多校赛第七场A--Minimum Cost Perfect Matching【位运算】【规律】

    链接:https://www.nowcoder.com/acm/contest/145/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

  3. 2019牛客网暑假多校训练第四场 K —number

    链接:https://ac.nowcoder.com/acm/contest/884/K来源:牛客网 题目描述 300iq loves numbers who are multiple of 300. ...

  4. 数组中出现次数超过一半的数字 牛客网 剑指Offer

    数组中出现次数超过一半的数字 牛客网 剑指Offer 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字 ...

  5. 把数组排成最小的数 牛客网 剑指Offer

    把数组排成最小的数 牛客网 剑指Offer 题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能 ...

  6. 最小的K个数 牛客网 剑指Offer

    最小的K个数 牛客网 剑指Offer 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. class Solution ...

  7. 数组中重复的数字 牛客网 剑指Offer

    数组中重复的数字 牛客网 剑指Offer 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中 ...

  8. 双栈排序 牛客网 程序员面试金典 C++ Python

    双栈排序 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中. ...

  9. [USACO 2009 Mar S]Look Up_via牛客网

    题目 链接:https://ac.nowcoder.com/acm/contest/28537/N 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

随机推荐

  1. multipart/form-data和application/x-www-form-urlencoded区别

    FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型.例如: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码格 ...

  2. Java核心-多线程-并发控制器-CyclicBarrier同步屏障

    1.基本概念 中文译本同步屏障,同样来自jdk并发工具包中一个并发控制器,它的使用和CountDownLatch有点相似,能够完成某些相同并发场景,但是它们却不相同. 2.抽象模型 主要用来实现多个线 ...

  3. docker 命令汇集

    1,查看容器 docker ps [-a] #-a表示全全部否者仅显示运行的 2,登录容器 docker exec -it [containerId] /bin/bashdocker exec -it ...

  4. C# Winform开发程序调用VLC播放器控件播放视频.

    VLC是个好东西,支持的格式多,还无广告,关键还有调用它的播放控件不用安装. 开个文章记录下调用这个控件的流水账,以便以后需要的时候查阅 创建工程 首先新建一个Winform工程. 这里姑且叫做VLC ...

  5. strct配置文件详解

    1,strct配置文件详解 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configurati ...

  6. 团队第九次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-14日晚上9点,以大家在群里讨论为主 召开时长10分钟 任务表格 袁勤 负责协调前后端 https://github.com/buaa-2016/p ...

  7. mysql修改root密码及修改密码过程中报错的解决方案

    参考网站: https://www.linuxidc.com/Linux/2018-05/152586.htmhttps://www.cnblogs.com/wangbaobao/p/7087032. ...

  8. html client websocket

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. python之psutil

    psutil = process and system utilities, psutil是个跨平台库,能够轻松实现获取系统运行的进程和系统利用率,包括CPU.内存.磁盘.网络等信息. Linux系统 ...

  10. 关于python的多行注释,启动新浏览器,循环语句乘法口诀

    1,提问:如何将python写的多行代码改写成注释,进行写下一段代码?这样可以在多个脚本中写东西? 回答:百度了一下,还真有 选中所要注释的代码  CTRL + / 然后所选的代码前面都会出现#,编程 ...