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 1010. Given a positive integer kk, your task is to find the kk-th smallest perfect positive integer.

Input

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

Output

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

Examples
input

Copy
1
output

Copy
19
input

Copy
2
output

Copy
28
Note

The first perfect integer is 1919 and the second one is 2828.

题意:求出第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[];
bool check(ll x)
{
ll ans=;
while(x){
ans+=x%;
x/=;
}
return ans==;
}
int main()
{
scanf("%d",&k);
int p=;
for(int i=;i<;i++)
{
if(check(i))
{
a[++p]=i;
}
}
printf("%lld\n",a[k]);
return ;
}

枚举 873 ms 300 KB

#include<bits/stdc++.h>
using namespace std;
int k;
bool check(int x)
{
int ans=;
while(x){
ans+=x%;
x/=;
}
return ans==;
}
int main()
{
scanf("%d",&k);
int p=;
for(int i=;i<;i++)
{
if(check(i))
{
p++;
if(p==k)
{
printf("%d\n",i);
}
}
} return ;
}

枚举 140 ms 0 KB

 
#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 INF 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
int k;
int bit[];
int dp[][];//表示到第i位,数位和为j的个数
int dfs(int pos,int sum,bool limit)
{
if(pos==-) return sum==;
if(sum>)return ;
if(!limit&&dp[pos][sum]!=-)return dp[pos][sum];
int ans=;
int up=limit?bit[pos]:;
for(int i=;i<=up;i++)
{
ans+=dfs(pos-,sum+i,limit&&(i==up));
}
if(!limit) dp[pos][sum]=ans;
return ans;
}
int calc(int x)
{
int len=;
while(x)
{
bit[len++]=x%;
x/=;
}
return dfs(len-,,true);
}
int main()
{
memset(dp,-,sizeof(dp));
while(~scanf("%d",&k))
{
int lb=,ub=INF;
int ans=;
while(ub-lb>){
int mid=(lb+ub)/;
if(calc(mid)<k) lb=mid;
else ans=mid,ub=mid;
}
printf("%d\n",ans);
} return ;
}

二分+数位dp 31 ms 0 KB

Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)的更多相关文章

  1. Codeforces Round #460 (Div. 2)-B. Perfect Number

    B. Perfect Number time limit per test2 seconds memory limit per test256 megabytes Problem Descriptio ...

  2. Codeforces Round #287 (Div. 2) D. The Maths Lecture [数位dp]

    传送门 D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. Codeforces Round #585 (Div. 2) B. The Number of Products(DP)

    链接: https://codeforces.com/contest/1215/problem/B 题意: You are given a sequence a1,a2,-,an consisting ...

  4. Codeforces Round #608 (Div. 2) E - Common Number (二分 思维 树结构)

  5. Codeforces Round #608 (Div. 2) E. Common Number (二分,构造)

    题意:对于一个数\(x\),有函数\(f(x)\),如果它是偶数,则\(x/=2\),否则\(x-=1\),不断重复这个过程,直到\(x-1\),我们记\(x\)到\(1\)的这个过程为\(path( ...

  6. Codeforces Round #267 (Div. 2) C. George and Job(DP)补题

    Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...

  7. Codeforces Round #460 (Div. 2)

    A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...

  8. [Codeforces]Codeforces Round #460 (Div. 2)

    Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...

  9. 【Codeforces Round #460 (Div. 2) B】 Perfect Number

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 直接暴力求出第k个perfect数字就好. 纯模拟. [代码] #include <bits/stdc++.h> #de ...

随机推荐

  1. FMC141-4路 250Msps/16bits ADC, FMC板卡

    FMC141-4路 250Msps/16bits ADC, FMC板卡 一.产品概述: 本板卡基于 FMC 标准板卡,实现 4 路 16-bit/250Msps ADC 功能.遵循 VITA 57 标 ...

  2. nodejs 更新代码自动刷新页面

    安装第三方工具: nodemon npm install --global nodemon 安装完毕后使用: 之前使用: node xxx.js 改成 nodemon xxx.js 只要通过nodem ...

  3. break语句、continue语句、goto语句的用法辨析

    1.break语句 break语句常使用在switch语句.循环体以及if语句中,它的作用是跳出循环,而且只能跳出一层循环. for (i = 0; i < 10; ++j) { for (j ...

  4. c# 反射获取属性值 TypeUtils

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  5. python3-调用函数

    Python内置了很多有用的函数,我们可以直接调用. 要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数.可以直接从Python的官方网站查看文档: http://doc ...

  6. Codeforces Round #595 (Div. 3) 题解

    前言 大家都在洛谷上去找原题吧,洛谷还是不错的qwq A 因为没有重复的数,我们只要将数据排序,比较两两之间有没有\(a_j - a_i == 1 (j > i)\) 的,有则输出 \(2\) ...

  7. php 输出缓冲

    <?php ob_start();//开启php输出缓冲区 echo "A"; //"A"会进入php输出缓冲区 ob_flush();//将php输出缓 ...

  8. 20180826(04)-Java序列化

    Java序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型 将序列化对象写入文件之后,可 ...

  9. PHP 判断是否为手机端访问

    /* * 判断是否为手机端 */function check_wap(){ // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_W ...

  10. [CSP-S模拟测试94]题解

    A.凉宫春日的忧郁 高精硬上似乎跑不过,其实可以都取个$log$.那么只需要比较$y\times log ^x$和$\sum \limits _{i=1}^y log^i$就好了. #include& ...