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 ...
随机推荐
- Java基础学习(4)
Java基础学习(四) String类 特点:创建后不可再修改,看起来的修改只是创建了新的对象 常用方法 StringBuilder类 目的:解决String类频繁创建对象的问题 常用方法 特点:非线 ...
- python常用模块学习1
import time time.sleep(1)#暂停时间 time.time()#显示当前系统时间戳 t=time.localtime()#结构化当地时间,可以将结构化时间想象成一个类 print ...
- 利用sql语句建立全国省市区三级数据库
一.创建数据库zone CREATE DATABASE IF ONT EXISTS zone; 二.建立省级表并增加数据 DROP TABLE IF EXISTS `provinces`; CREAT ...
- dialog写进dll调用
#ifdef DLG_WINDOW_API #define DLG_WINDOW_EXPORT __declspec(dllexport) #else #define DLG_WINDOW_EXPOR ...
- dd hdparm 速度不一致
https://www.cnblogs.com/yinzhengjie/p/9935478.html hparm # 它用来在基于Linux的系统上获取或设置硬盘参数,包括测试读性能以及缓存性 ...
- LinkedList与ArrayList的区别(内部实现)
ArrayList的内部实现是基于内部数组Object[],所以从概念上讲,它更像数组: LinkedList的内部实现是基于一组连接的记录,所以,它更像一个链表结构,所以,它们在性能上有很大的差别. ...
- Codeforces 832D: Misha, Grisha and Underground 【LCA模板】
题目链接 模板copy from http://codeforces.com/contest/832/submission/28835143 题意,给出一棵有n个结点的树,再给出其中的三个结点 s,t ...
- 对webpack的初步研究7
Configuration 您可能已经注意到很少有webpack配置看起来完全相同.这是因为webpack的配置文件是导出对象的JavaScript文件.然后,webpack根据其定义的属性处理此对象 ...
- Jenkines邮件中添加图片
1.在Jenkins的邮件插件 Email-ext中的Default Content内容编写html文件,简单模板如下: <html> <head> </head&g ...
- Cenos7下nginx+mysql+php环境的搭建
首先更新系统软件 1 $ yum update 第一步:安装nginx 1.安装nginx源 1 $ yum localinstall http://nginx.org/packages/centos ...