J- Beautiful Numbers

 

题目描述

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].

示例1

输入

2
10
18

输出

Case 1: 10
Case 2: 12

思路:数位DP,做的太少,码上,还需理解。

#include<bits/stdc++.h>
using namespace std;
long long n;
int a[20],mod;
long long dp[20][200][200];

long long dfs(int pos,int state,int tmod,bool limit){
    if(!pos) return (state==mod)&&(!tmod);
    if(state>mod) return 0;
    if(!limit&&dp[pos][state][tmod]!=-1) return dp[pos][state][tmod];
    int up=limit?a[pos]:9;
    long long cnt=0;
    for(int i=0;i<=up;i++) cnt+=dfs(pos-1,state+i,(tmod*10+i)%mod,limit&&i==a[pos]);
    return limit?cnt:dp[pos][state][tmod]=cnt;
}



long long cal(long long n){
    int len=0;
    long long nn=n;
    while(nn){
        a[++len]=nn%10;
        nn/=10;

    }
    long long ans=0;
    for(int i=1;i<=9*len;i++){
        mod=i;
        memset(dp,-1,sizeof(dp));
        ans+=dfs(len,0,0,true);
    }
    return ans;
}

int main(){
    int t,cas=0;
    scanf("%d",&t);
    while(t--){
        scanf("%lld",&n);
        printf("Case %d: %lld\n",++cas,cal(n));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值