//时间限制:10000ms
//单点时限:1000ms
//内存限制:256MB
//描述
//给定N个不同的质数P1, P2, … PN。用它们作为分目可以组成(P1-1) + (P2-1) + … (PN-1)个分数:
//
//1/P1, 2/P1, 3/P1, …, P1-1/P1, 1/P2, 2/P2, 3/P2, … P2-1/P2, … 1/PN, 2/PN, … PN-1/PN
//
//请帮助小Ho求出其中第K小的分数。
//
//输入
//第一行包含两个整数N和L。
//
//以下N行每行包含一个质数Pi。
//
//对于70%的数据,1 ≤ N ≤ 100, 1 ≤ K ≤ 1000000, 2 ≤ Pi ≤ 100000
//
//对于100%的数据, 1 ≤ N ≤ 1000, 1 ≤ K ≤ 1000000000, 2 ≤ Pi ≤ 1000000000
//
//输出
//输出一个分数表示答案
//
//样例输入
//3 4
//2
//3
//5
//样例输出
//1/2
//题解:
//由于p是质数,所以每个分数都不可约分,由于对于每一个1/P1, 2/P1, 3/P1, …,
//P1-1/P1序列都是递增,我们可以二分答案,算出小于等于二分答案的数有多少个,
//当个数等于k时即二分答案接近要求的答案,记录最接近的分子和分母,即为答案 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm> using namespace std;
int main() {
int n, k;
while(scanf("%d%d",&n,&k)==){ long long f[];
memset(f,,sizeof(f));
for( int i = ; i <= n; i++ ) {
scanf("%lld", &f[i] );
}
long long zz , ff;
double l = , r = 1.0, mid;
while(true){
mid = (r+l)/2.0;
long long ans = ;
zz = ff = -; for( int i = ; i <= n; i++ ) { long long x = mid*f[i]; ans += x; if( zz == - ) {
zz = x;
ff = f[i];
}
if((long long)f[i]*zz < (long long)x*ff){
zz = x;
ff = f[i];
}
// cout << zz << "/" << ff<<endl;
}
// cout<<"ans = "<<ans<<endl;
if( ans == k ) break; if( ans > k ) r = mid;
else l = mid; }
cout << zz << "/" << ff<<endl;
}
return ;
}

第k小分数(二分值)的更多相关文章

  1. [LeetCode] K-th Smallest Prime Fraction 第K小的质分数

    A sorted list A contains 1, plus some number of primes.  Then, for every p < q in the list, we co ...

  2. [LeetCode] 786. K-th Smallest Prime Fraction 第K小的质分数

    A sorted list A contains 1, plus some number of primes.  Then, for every p < q in the list, we co ...

  3. [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  4. [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  5. 230. 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...

  6. leetcode 二叉搜索树中第K小的元素 python

          二叉搜索树中第K小的元素     给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元 ...

  7. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  8. LeetCode:二叉搜索树中第K小的数【230】

    LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...

  9. The UVALIVE 7716 二维区间第k小

    The UVALIVE 7716 二维区间第k小 /** 题意:给一个n * n的矩阵,有q个查询 每次查询r,c,s,k表示已(r,c)为右上角 大小为s的正方形中 第k小的元素 n <= 2 ...

随机推荐

  1. HomeKit 开发指南(中文版)

    转载自cocoachina 本文由CocoaChina翻译组成员iBenjamin_Go和浅夏@旧时光翻译自苹果开发文档:HomeKit Developer Guide,敬请勘误. 本文档内容包括 第 ...

  2. HDU 1165 公式推导题

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1165 Eddy's research II Time Limit: 4000/2000 MS (Java/Othe ...

  3. HDFS的Write过程

    hadoop中重要的组成部分HDFS,它所发挥的重要作用是进行文件的后端存储.HDFS针对的是低端的服务器,场景为读操作多.写操作少的情况.在分布式存储情况下,比较容易出现的情况是数据的损害,为了保证 ...

  4. 批量kill杀死某些会话session的PL/SQL

    原文:http://blog.itpub.net/9240380/viewspace-666622/ SQL> declare 2 v_sid v$session.sid%type; --定义如 ...

  5. oracle数据库的配置文件

    url=jdbc:oracle:thin:@localhost:1521:orcldriver=oracle.jdbc.OracleDriverusrname=GJQ   (PLSQL Develop ...

  6. (Les16 执行数据库恢复)-表空间恢复

    NOARCHIVELOG模式下丢失了数据文件     数据库处于NOARCHIVELOG模式时,如果丢失任何数据文件,执行以下步骤         1.如果实例尚未关闭,请关闭实例         2 ...

  7. [iOS]一些第三方库

    BHInfiniteScrollView 地址 https://github.com/qylibohao/BHInfiniteScrollView 功能 图片轮播 TZImagePickerContr ...

  8. SQLServer禁用、启用外键约束

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ---启用or禁用指定表所有外键约束  alter table PUB_STRU  NOCHECK constrai ...

  9. MySQL-5.5.32 配置文件优化详解

    目录 MySQL-5.5.32 配置文件优化详解 一.配置文件说明 2.my-medium.cnf 3.my-large.cnf 4.my-huge.cnf 5.my-innodb-heavy-4G. ...

  10. 在CentOS7上安装MySQL5.7-源码包方式

    缺点:后期升级不方便,生产中建议RPM包方式安装 CentOS7默认安装了和MySQL有兼容性的MariaDB数据库,在我们安装MySQL5.7之前为了避免发生冲突首先删除MariaDB. # rpm ...