B. Perfect Number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

We consider a positive integer perfect, if and only if the sum of its digits is exactly k-th smallest perfect positive integer.

Input

A single line with a positive integer 1≤k≤10000).

Output

A single number, denoting the k-th smallest perfect integer.

Examples
input
Copy
1
output
Copy
19
input
Copy
2
output
Copy
28
Note

The first perfect integer is 28.

题意:求出第k个各位数和为10的数

题解:本题k的范围比较小,所以暴力可以解决

但是当范围大的时候,就要用数位dp了

数位dp记录的是小于某个数的符合条件的数有多少个

用二分去枚举

代码:

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
int k;
ll a[10005];
bool check(ll x)
{
    ll ans=0;
    while(x){
       ans+=x%10;
       x/=10;
    }
    return ans==10;
}
int main()
{
    scanf("%d",&k);
    int p=0;
   for(int i=0;i<11000006;i++)
   {
       if(check(i))
          {
              a[++p]=i;
          }
   }
   printf("%lld\n",a[k]);
    return 0;
}
枚举 873 ms 300 KB
#include<bits/stdc++.h>
using namespace std;
int k;
bool check(int x)
{
    int ans=0;
    while(x){
       ans+=x%10;
       x/=10;
    }
    return ans==10;
}
int main()
{
    scanf("%d",&k);
    int p=0;
   for(int i=0;i<11000006;i++)
   {
       if(check(i))
          {
              p++;
              if(p==k)
              {
                    printf("%d\n",i);
              }
          }
   }

    return 0;
}
枚举 140 ms 0 KB

相关文章: