描述

对于一个素数P,我们可以用一系列有理分数(分子、分母都是不大于N的自然数)来逼近sqrt(p),例如P=2,N=5的时候:1/1<5/4<4/3<sqrt(2)<3/2<5/3<2/1。
任 务 :
给定P、N(N>sqrt(p)),求X、Y、U、V,使x/y<sqrt(p)<u/v且x/y与sqrt(p)之间、sqrt(p)与u/v之间都不能再插入满足题意的有理分数。

输入格式

输入文件的第一行为P、N,其中 P、N<30000。

输出格式

输出文件只有一行,格式为“X/Y U/V”。注意,答案必须是既约的,也就是说分子、分母的最大公约数必须等于1。

测试样例1

输入

样例1: 
2 5 
样例2: 
5 100

输出

样例1: 
4/3 3/2

样例2: 
38/17 85/38

代码

 #include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int des_int,P,N,a,b,ans[]={,,,,,,,,,};
double des; int check(int i,int j,double x){
return (fabs( des-double(ans[i])/double(ans[j]) ) > fabs( des-x ));
} void xiao(){//ans1~2
for(int i=des_int-;i<=N;i++){
int flag=;
for(int j=;j<=i;j++){
double x=double(i)/double(j);
if(x>des) continue;
if(check(,,x)){
double k1=(double(ans[])/double(ans[])),k2=x;
bool fl=(k1==k2);
if(fl) continue;
ans[]=i;ans[]=j;
}
else{
flag=;break;
}
}
if(flag==) continue;
}
} void da(){//ans3~4
for(int i=des_int+;i<=N;i++){
int flag=;
for(int j=i-;j>=;j--){
double x=double(i)/double(j);
if(x<des) continue;
if(check(,,x)){
if(fabs( des-double(ans[])/double(ans[]) )== fabs( des-x )) continue;
ans[]=i;ans[]=j;
}
else{
flag=;break;
}
}
if(flag==) continue;
}
}
int main(){
// freopen("01.txt","r",stdin);
scanf("%d %d",&P,&N);
des=sqrt(double(P));
des_int=des;
xiao();
da(); printf("%d/%d %d/%d\n",ans[],ans[],ans[],ans[]);
return ;
}

穷尽今生所学,T掉2个点

上看不懂的别人代码:

 #include <math.h>
#include <stdio.h>
#include <stdlib.h> int main(int argc, char **argv)
{
int i, j;
int p, n;
int a, b, c, d;
int beg, end;
double s, max = , min = , t;
scanf("%d%d", &p, &n);
s = sqrt(p);
for(i = ; i <= n; i++){
beg = i * s - ;
end = beg + ;
if(beg <= ){
beg = ;
}
if(end > n){
end = n;
}
for(j = beg; j <= end; j++){
t = (double)(j) / i;
if(t < s && t > max){
a = j, b = i;
max = t;
}
}
}
for(i = ; i <= n; i++){
beg = i * s;
end = beg + ;
if(beg <= ){
beg = ;
}
if(end > n){
end = n;
}
for(j = beg; j <= end; j++){
t = (double)(j) / i;
if(t > s && min > t){
c = j, d = i;
min = t;
}
}
}
printf("%d/%d %d/%d\n", a, b, c, d);
return ;
}

---------------我是猥琐的分割线---------------

 #include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int des_int,P,N,a,b,ans[]={,,,,,,,,,};
double des; int check(int i,int j,double x){
return (fabs( des-double(ans[i])/double(ans[j]) ) > fabs( des-x ));
} void xiao(){//ans1~2
for(int i=des_int-;i<=N;i++){
int flag=;
for(int j=;j<=i;j++){
double x=double(i)/double(j);
if(x>des) continue;
if(check(,,x)){
double k1=(double(ans[])/double(ans[])),k2=x;
bool fl=(k1==k2);
if(fl) continue;
ans[]=i;ans[]=j;
}
else{
flag=;break;
}
}
if(flag==) continue;
}
} void da(){//ans3~4
for(int i=des_int+;i<=N;i++){
int flag=;
for(int j=des_int/i+;j>=;j--){
double x=double(i)/double(j);
if(x<des) continue;
if(check(,,x)){
if(fabs( des-double(ans[])/double(ans[]) )== fabs( des-x )) continue;
ans[]=i;ans[]=j;
}
else{
flag=;break;
}
}
if(flag==) continue;
}
}
int main(){
// freopen("01.txt","r",stdin);
scanf("%d %d",&P,&N);
des=sqrt(double(P));
des_int=des;
xiao();
da(); printf("%d/%d %d/%d\n",ans[],ans[],ans[],ans[]);
return ;
}

39行调了个阈值成功骗分,主要都是da()函数耗的时间,也就没多想,+500有点危险,+100绝对过不了

想法来源于(能跑这么久的数肯定很大,分母也就没必要那么大了!!!)

TYVJ P1077 有理逼近 Label:坑,tle的好帮手 不懂的更多相关文章

  1. TYVJ P1037 阶乘统计2 Label:坑

    描述 n的阶乘定义为n!=1*2*3*……*n 如3!=6 n!通常最后会有很多0,如5!=120 最后有一个0,现在统计n!去除末尾的0后,最后k位是多少  输入格式  第一行包括两个数n,k  输 ...

  2. TYVJ P1032 零用钱 Label:贪心

    背景 USACO OCT09 7TH 描述 作為创造產奶纪录的回报,Farmer John决定开始每个星期给Bessie一点零花钱. FJ有一些硬币,一共有N (1 <= N <= 20) ...

  3. 1833 深坑 TLE 求解

    题目描述: 大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 ...

  4. TYVJ P1004 滑雪 Label:记忆化搜索

    背景 成成第一次模拟赛 第三道 描述     trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行的路线必须向下倾斜.    ...

  5. TYVJ P1068 STR Label:KMP匹配 不懂

    描述 给你两个串A,B,可以得到从A的任意位开始的子串和B匹配的长度.给定K个询问,对于每个询问给定一个x,求出匹配长度恰为x的位置有多少个.N,M,K<=200000 输入格式 第一行三个数  ...

  6. TYVJ P1070 罗马数字 Label:一定要看的枚举

    描述 一类书的序言是以罗马数字标页码的.传统罗马数字用单个字母表示特定的数值,以下是标准数字表: I 1  L 50  M 1000V 5  C 100X 10 D 500最多3个同样的可以表示为10 ...

  7. TYVJ P1090 母舰 Label:模拟,题目看清就好

    背景 广东汕头聿怀初中 Train#3 Problem 1 描述 在小A的星际大战游戏中,一艘强力的母舰往往决定了一场战争的胜负.一艘母舰的攻击力是普通的MA(Mobile Armor)无法比较的.对 ...

  8. TYVJ P1072 bomb Label:看不懂题意

    描述 一场战争正在A国与B国之间如火如荼的展开.B国凭借其强大的经济实力开发出了无数的远程攻击导弹,B国的领导人希望,通过这些导弹直接毁灭A国的指挥部,从而取得战斗的胜利!当然,A国人民不会允许这样的 ...

  9. TYVJ P1016 装箱问题 Label:01背包 DP

    做题记录:2016-08-15 23:07:04 背景 太原成成中学第2次模拟赛 第三道 描述 有一个箱子容量为v(正整数,o≤v≤20000),同时有n个物品(o≤n≤30),每个物品有一个体积 ( ...

随机推荐

  1. HDOJ 1596

    9899828 2013-12-27 16:42:37 Accepted 1596 3312MS 6668K 711 B C++ 泽泽 floyed暴力 #include<cstdio> ...

  2. (转载)【Android】ViewGroup全面分析

    转载自:http://www.cnblogs.com/lqminn/archive/2013/01/23/2866543.html 一个Viewgroup基本的继承类格式如下: import andr ...

  3. Controller之间传递数据:属性传值

    在项目中,Controller之间传递数据非常之多,这里简单介绍一下属性传值.例如有FirstController 和 SecondController,数据从First传递到Second中,我们如何 ...

  4. .Net Attribute特性

    1.什么是Atrribute 首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标 ...

  5. Android 转载一篇.9图片详解文章

    感谢作者,原文链接为 http://blog.csdn.net/ouyang_peng/article/details/9242889

  6. MongoDB副本集学习(二):基本测试与应用

    简单副本集测试 这一节主要对上一节搭建的副本集做一些简单的测试. 我们首先进入primary节点(37017),并向test.test集合里插入10W条数据: . rs0:PRIMARY> ;i ...

  7. Java for LeetCode 189 Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  8. Java for LeetCode 033 Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. codeforces A. Group of Students 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...

  10. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...