//时间限制: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. python -- peewee处理数据库连接

    目前,实现了的Database子类有三个:SqliteDatabase.MySQLDatabase.PostgresqlDatabase class SqliteDatabase(Database) ...

  2. Python 用hashlib。md5加密算法

    廖雪峰的官方网站学习MD5加密,网址:“http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a0 ...

  3. Ubuntu SSH登陆出现Access Denied错误

    在/etc/ssh/sshd_config 中有个 PermitRootLogin, 改成“PermitRootLogin yes”就可以了 重启ssh: /etc/init.d/ssh restar ...

  4. ionic2添加支付宝插件出现问题

    安装本地路径插件正常  编译正常  在打开支付页面时候 就报这个错 在手机app点击无效 错误信息: ERROR Error: Uncaught (in promise): Error: No pro ...

  5. 16JavaScript for循环

    1.JavaScript 循环 如果希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: document.write(cars[0] ...

  6. 解决ios下audio不能正常播放的问题

    解决ios下audio不能正常播放的问题 ios系统下会自动屏蔽audio标签的自动播放,需要使用一个事件来驱动音频播放 this.$refs.startaudio.addEventListener( ...

  7. Role Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using System.Collections.Ge ...

  8. 大数据时代数据库-云HBase架构&生态&实践

    业务的挑战 存储量量/并发计算增大 现如今大量的中小型公司并没有大规模的数据,如果一家公司的数据量超过100T,且能通过数据产生新的价值,基本可以说是大数据公司了 .起初,一个创业公司的基本思路就是首 ...

  9. django查询集-17

    当查询结果是多个的时候,django-ORM会返回一个 查询集(QuerySet) ,表示从数据库中获取对象的 集合 . 查询集可以使用过滤器进行再次处理. 例如查询阅读量大于20且评论数大于30的书 ...

  10. Python学习 :反射 & 单例模式

    反射 什么是反射? - 反射主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省) 面向对象中的反射 - 通过字符串的形式来操作(获取.检查.增加.删除)对象中的成员 - python中的 ...