链接: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. centos7安装rabbitmq3.7.9

    感谢此兄: https://blog.51cto.com/huwei555/2341513?source=dra  (centos7 安装rabbitmq 3.7) 以root用户登录.cd /hom ...

  2. ActiveMQ(为什么要使用消息中间件,JMS传输模型)

    为什么要使用消息中间件:    同步请求:当客户端向服务器发送一条请求的时候,此时服务器由于网络,或者处理一些比较大的数据的时候,可能有延迟,客户端 会处于一直等待的状态.只有等待服务器返回处理结果, ...

  3. [UE4]Spline

    Spline和Spline Mesh的区别: 1.Spline Mesh是有实体表现的,Spline Mesh可以拉伸弯曲实体模型,Spline Mesh是具象. 2.Spline 只有曲线,没有实体 ...

  4. 关于Unity单个对象多个脚本的Update调用的时序问题

    先说几句废话, 最近在研究Unity, 这玩意用起来比较简单, 而且商店里还有各种插件, 初学者也能轻松拼凑出一个像模像样的游戏(顺便说一句,自己做着玩就无所谓了,但随便拼凑个辣鸡丢出来骗钱就不好了) ...

  5. linux 安装中文支持

    下载  fonts-chinese-3.02-12.el5.noarch.rpm fonts-ISO8859-2-75dpi-1.0-17.1.noarch.rpm 安装各种提示的依赖 安装 chkf ...

  6. idea springboot 父子工程 子工程maven不自动import

    父工程删除对spring boot启动项的引用,因为父工程 dependencyManagement,它不会自动加载包,只指定包的版本, 如果在父工程中引用了包,但是没有指定包版本,子工程将不会识别到 ...

  7. Python Excel 多sheet 多条数据 自定义写入

    pip install xlwt python excel 数据写入操作,处理网站数据导出以及不是太多数据的爬虫存储, 用处蛮多的轮子. (150+++++++++++++++++++++++++++ ...

  8. Centos7 系统下怎么更改apache默认网站目录

    当我们在Centos7中配置好Apache时,发现apache默认解析目录是在 /var/www/html,也就是说当访问服务器 IP 或者本地 localhost 时,默认定位到这个目录里的 ind ...

  9. Mui中常见问题记录

    1.mui中A标签如果有这个class="mui-control-item"时,a标签点击链接是没有反应的,那么可以用以下方式实现 : mui.init(); mui('body' ...

  10. request.getRealPath为什么会被代替

    以及前两天在网上看到的“不是工程的物理路径封装在Session里 是工程的路径被封装在了ServletContext中的问题” 很抱歉没有找到答案. 只能怪鄙人才识短浅. 在通过这次学习的过程中使我懂 ...