Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)
2 seconds
256 megabytes
standard input
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.
A single line with a positive integer kk (1≤k≤100001≤k≤10000).
A single number, denoting the kk-th smallest perfect integer.
1
19
2
28
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)的更多相关文章
- Codeforces Round #460 (Div. 2)-B. Perfect Number
B. Perfect Number time limit per test2 seconds memory limit per test256 megabytes Problem Descriptio ...
- 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 ...
- 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 ...
- Codeforces Round #608 (Div. 2) E - Common Number (二分 思维 树结构)
- Codeforces Round #608 (Div. 2) E. Common Number (二分,构造)
题意:对于一个数\(x\),有函数\(f(x)\),如果它是偶数,则\(x/=2\),否则\(x-=1\),不断重复这个过程,直到\(x-1\),我们记\(x\)到\(1\)的这个过程为\(path( ...
- 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 ...
- Codeforces Round #460 (Div. 2)
A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...
- [Codeforces]Codeforces Round #460 (Div. 2)
Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...
- 【Codeforces Round #460 (Div. 2) B】 Perfect Number
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 直接暴力求出第k个perfect数字就好. 纯模拟. [代码] #include <bits/stdc++.h> #de ...
随机推荐
- c# Winform 调用可执行 exe 文件
c#是一个写windows桌面小工具的好东西,但有个时候,我们需要在 winform 程序中调用其他的 exe 文件,那么该如何实现呢? 如果只是拉起一个 exe 文件,可以参考如下方法实现: str ...
- 基于impi zabbix监控r720 测试过程
1.F2进入服务器bios 修改network 使这台服务器能够被远程访问. 2.在远程的centos 7 服务器上安装 impitool工具包 #ipmitool -I lanplus -H X ...
- [Luogu2600]合并神犇(dp,贪心)
[Luogu2600]合并神犇 题目背景 loidc来到了NOI的赛场上,他在那里看到了好多神犇. 题目描述 神犇们现在正排成一排在刷题.每个神犇都有一个能力值p[i].loidc认为坐在附近的金牌爷 ...
- CSS3 flexbox弹性布局实例
常用例子 1.居中对齐 <!DOCTYPE html> <head> <meta charset="utf-8"> <style type ...
- $[WC2018]$通道(虚树,边分练习)
\([WC2018]\)通道(虚树,边分练习) 感受码题的快感 这段时间真的是忙忙忙忙忙,省选之前还是露个脸,免得以后没机会了. 但是我感觉我的博客真的没啥人看,虽然我挺想要有人看的,但是自己真的没啥 ...
- Windows电脑无法识别USB设备怎么办?
您可能已经注意到,如果您使用USB设备并将其插入计算机,Windows会识别并配置它.然后,如果你拔掉它并将其重新插入另一个USB端口,Windows就会出现一连串的健忘症,并认为这是一个完全不同的设 ...
- error: call of overloaded ‘sqrt(double&)’ is ambiguous
OpenFOAM定义了新的sqrt,当引入新的Library时,必须显式地使用std::sqrt(),否则会报如下错误: error: call of overloaded 'sqrt(double& ...
- Django2 + ORM
创建模型类class UserInfo(models.Model): id = models.IntegerField() username = models.CharField(max_length ...
- jquery ajax请求回调
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Oracle中start with...connect by/start with…connect by prior子句的用法
connect by 是结构化查询中用到的,其基本语法是:select … from tablenamestart with 条件1connect by 条件2where 条件3;例:select * ...