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 ...
随机推荐
- unity2017 光照与渲染(一)
光照&渲染(基于unity2017.2.0) Custom Skybox 天空盒 最丰富的环境光 a. TextureShape 改成 Cube. b. 把图片直接丢给天空,就会自动生成材质. ...
- case_when
select sname,score,case when score>=20 and score<30 then 20when score>=30 and score<40 t ...
- IncDec Sequence (差分)
题目地址 这道题可以用来检测一下你是否学会了差分,或者你可以更加透彻的理解差分 我们把 \(cf[]\) (差分)数组拿出了,就可以发现这道题就是每次可以在 \(cf[]\)中 选两个数,一个+1,一 ...
- $LCT$维护子树信息学习笔记
\(LCT\)维护子树信息学习笔记 昨天\(FDF\)好题分享投了 \([ZJOI2018]\)历史 这题. 然后我顺势学学这个姿势. 结果调了一年...于是写个笔记记录一下. 基本原理 比较显然地, ...
- 转Serial,Parallel,CMS,G1四大GC收集器特点小结
转 https://blog.csdn.net/u013812939/article/details/48782343 1.Serial收集器 一个单线程的收集器,在进行垃圾收集时候,必须暂停其他所有 ...
- nodejs和npm之间的关系
Node.js是JavaScript的一种运行环境,是对Google V8引擎进行的封装.是一个服务器端的javascript的解释器. 包含关系,nodejs中含有npm,比如说你安装好nodejs ...
- mysql 5.6 datetime default now()
CREATE TABLE `test` ( id int, `gmt_create` datetime DEFAULT NOW() not NULL )ENGINE=InnoDB; mysq ...
- django classonlymethod 和 python classmethod的区别
--classmethod可以被一个实例调用,classonlyethod只能被类调用 class Kls(object): no_inst = 0 def __init__(self): Kls.n ...
- python封装成exe
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_32113189/article ...
- Python_006(is和==,小数据池)
一.is和==的区别 1.id()方法,可以查看一个变量的内存地址 lst = ["周杰伦", "燃哥"] lst1 = ["周杰伦", & ...