链接: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. IDEA 运行spring boot出现端口占用的问题

    Description: The Tomcat connector configured to listen on port 8080 failed to start. The port may al ...

  2. Ubuntu系统配置

    0.基本配置 0.1初始设置 (1)开户root账号并重启系统: 打开gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 添加greeter- ...

  3. memcached—Java操作Memcached实例

    前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...

  4. SpringBoot aop 注解 数据权限校验

    注解类: @Retention(RetentionPolicy.RUNTIME) public @interface DataAuthValid { //位置 public int index() d ...

  5. Shiro简介——《跟我学Shiro》

    地址: http://jinnianshilongnian.iteye.com/blog/2018936

  6. sqlserver查找使用了某个字段的所有存储过程

    当一个系统中使用了很多的表,并且存在大量的存储过程,当数据库中的某个表删除了某个字段,那么相应的存储过程也需要改动,但是我们不知道哪些存储过程使用了该字段,那我们该怎么办?我们可以从之前的文档一个一个 ...

  7. my first note

    please do not laugh It is very glad to be here, I will study hard.

  8. 《python编程快速上手》

    第一部分 编程基础 @表达式 ** % // @ >>> int(3.4) 3 >>>round(3.555,2)3.56 @判断条件时:0和0.0和‘’都是Fal ...

  9. 实现自己的HashMap

    准备工作 ,实现自己的Map.entry.代码如下 : import java.util.Map;public class MapEntry<K,V> implements Map.Ent ...

  10. Echarts属性大全(及时更新最新信息)

    echarts属性的设置(完整大全)   // 全图默认背景  // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87c ...