Codefroces 762A k-th divisor 数论
题目大意:
给定两个正整数n,k\((n \le 10^{15},k\leq10^9)\),求n的从小到大的第k个约数,无解输出-1
分析:
我们会自然而然地想到找出n的所有的约数,然后取第k个。
我们发现如果这样的话时间复杂度为\(O(\sqrt{n})\),空间复杂度为\(O(lnn)\)
所以我们暴力上就好了
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline ll cat_max(const ll &a,const ll &b){return a>b ? a:b;}
inline ll cat_min(const ll &a,const ll &b){return a<b ? a:b;}
ll a[2000010],cnt1;
ll b[2000010],cnt2;
int main(){
ll n,k;read(n);read(k);
for(ll i=1;i*i<=n;++i){
if(n % i == 0){
a[++cnt1] = i;
if(i*i != n) b[++cnt2] = n/i;
}
}
if(k > cnt1 + cnt2) puts("-1");
else{
if(k <= cnt1){
printf("%I64d\n",a[k]);
}else{
k -= cnt1;
printf("%I64d\n",b[cnt2-k+1]);
}
}
getchar();getchar();
return 0;
}
Codefroces 762A k-th divisor 数论的更多相关文章
- Codeforces 762A k-th divisor(数论)
题目链接:k-th divisor 求出N的第K大因子,满足N <= 10^15,K <= 10^9 直接暴力…… #include <bits/stdc++.h> using ...
- 牛客练习赛16D K进制 数论(待理解QAQ)
正解:数论 解题报告: 行吧那就让我一点点推出来趴QAQ
- 【codeforces 762A】k-th divisor
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 762A】 k-th divisor
[题目链接] 点击打开链接 [算法] 我们知道,一个数的因子是成对出现的,一半小于等于sqrt(N),一半大于sqrt(N),因此,我们可以从 2..sqrt(N)枚举因子 [代码] #include ...
- UVA 10497 - Sweet Child Makes Trouble 高精度DP
Children are always sweet but they can sometimes make you feel bitter. In this problem, you will see ...
- HDU5568/BestCoder Round #63 (div.2) B.sequence2 dp+高精度
sequence2 Problem Description Given an integer array bi with a length of n, please tell me how many ...
- BZOJ2729:[HNOI2012]排队(组合数学)
Description 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不 ...
- 大整数类BIGN的设计与实现 C++高精度模板
首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...
- gym 100735I
Description standard input/outputStatements You are given three numbers. Is there a way to replace v ...
随机推荐
- eclipse adt开发android ndk没有NDK选项问题的解决方案
原创 2015年01月28日 09:38:40 标签: android ndk / eclipse / adt 15989 今天是2015年1月28号,整理一下昨天使用eclipse adt搭建的an ...
- jQuery 标签切换----之选项卡的实现
这一次,我自己写了代码,先看html部分: <div class="tab"> <div class="tab_menu"> <u ...
- <LeetCode OJ> 121. /122. Best Time to Buy and Sell Stock(I / II)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- android shareSDK 微博分享案例
android shareSDK 微博分享案例 ShareSDK APP_KEY 219b1121fc68 腾讯微博 key 801517904 secret bfba83ae253c8f38dabe ...
- BeeFramework 系列二 UISignal篇下
本文转载至 http://www.apkbus.com/android-126129-1-1.html qihoo2 该用户从未签到 156 主题 156 帖子 1826 积分 Android ...
- PHP购物车模块的实现(php/ajax/session)
购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- runsv
runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...
- 操作符表示指针指向的底层值 切片 nill 清空 按值引用赋值 获取地址赋值
package main import "fmt" var thisVisitedUrls [] string func tf() { p := &thisVisitedU ...
- apache 301重定向到带www的二级域名
Options +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^nlike.cn [nc] rewriterule ^(.*)$ ...
- Shell parameter expansion
使用sh写一些小型的脚本会使工作更加简单.有部分内容可能大家都比較陌生(至少我是这样). 就是变量有关的參数展开,以下就是一些简单的描写叙述和使用方法.能够使代码更加简洁 展开运算符 替换运算 ${v ...