链接: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. vim替换的两种方式

    最近操作一个超过30MB的一个文本文件,常用的编辑器打开就死.最后使用Vim,一路非常顺畅.不愧是久经历史考验的编辑器. 如何在Vim中将空格更换为\t 这个使用\s功能.具体命令为:\s\ \/t/ ...

  2. vscode中使用beautify插件格式化vue文件

    1.点击设置,找到beautify.language并在html一栏里加上vue "beautify.language": { "js": { "ty ...

  3. springboot项目如何打包成war包

    一.修改打包形式 在pom.xml里设置 <packaging>war</packaging> 二.移除嵌入式tomcat插件 在pom.xml里找到spring-boot-s ...

  4. TensorFlow实现回归

    数据:fetch_california_housing(加利福尼亚的房价数据) 1.解析解法 import tensorflow as tf import numpy as np from sklea ...

  5. 【转录组入门】3:了解fastq测序数据

    操作:需要用安装好的sratoolkit把sra文件转换为fastq格式的测序文件,并且用fastqc软件测试测序文件的质量 作业:理解测序reads,GC含量,质量值,接头,index,fastqc ...

  6. REST API TO MiniProgram 上线WordPress官方插件库

    全新开发的用于 wordpress微信小程序的插件 REST API TO MiniProgram今天上线WordPress官方插件库.这个插件的上一个版本叫:wp-rest-api-for-app, ...

  7. DevExpress 折线图和柱状图的绘制与数据绑定

    DevExpress 组件是一个非常丰富和强大的组件,适合各种可视化图形的绘制与展示,在数据分析展示中是个很有帮助的,网上也有很多关于这方面的文章,关于折线图或柱状图的画法,以下是自己在工作中接触到的 ...

  8. Oracle不常用SQL

    Oracle 查询最近创建的表 select * from user_objects where object_type='TABLE' order by created desc Oracle 查询 ...

  9. 如何解决fiddler的响应显示乱码问题

    fiddler中Response出现乱码, 这是因为HTML被压缩了, 我们可以通过两种方法去解压.方法1:点击Response Raw上方的"Response is encoded any ...

  10. py3,休息时间玩点小把戏

    100以内奇数: ls = [x for x in range(100) if x % 2 == 1] 100以内偶数: ls = list(x for x in range(100) if x % ...