题面

Description

The Brocard Erdös-Straus conjecture is that for any integern > 2 , there are positive integersa ≤ b ≤ c,so that :

\[{4\over n}={1\over a}+{1\over b} +{1\over c}
\]

There may be multiple solutions. For example:

\[{4\over 18}={1\over 9}+{1\over 10}+{1\over 90}={1\over 5}+{1\over 90}+{1\over 90}={1\over 5}+{1\over 46}+{1\over 2470}
\]

Input

The first line of input contains a single decimal integer P, (1 ≤ p ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently.

Each data set consists of a single line of input. It contains the data set number,K, followed by a single space, followed by the decimal integer n,(2 ≤ n ≤ 50000)

Output

For each data set there is one line of output. The single output line consists of the data set number, K, followed by a single space followed by the decimal integer values a, b and c in that order, separated by single spaces

Sample Input

5
1 13
2 529
3 49849
4 49850
5 18

Sample Output

1 4 18 468
2 133 23460 71764140
3 12463 207089366 11696183113896622
4 12463 310640276 96497380762715900
5 5 46 2070

题解

对于这个式子

\[\frac{4}{n}=\frac{1}a+\frac1b+\frac1c
\]

不如先解除\(a\)的范围,首先\(a\)必然大于\(\frac{n}4\),因为\(b和c\)项不能为0,其次\(a\)要小于等于\(\frac{3*n}{4}\),因为要满足字典序最小,\(a\)必然最小。然后我们在这个范围枚举\(a\),计算可行的\(b和c\),不如将\(b和c\)看成一个整体,这样可以解出\(\frac{1}{b}+\frac{1}{c}\)

\[\frac{1}b+\frac{1}c=\frac{4*i-n}{n*i}(\frac{n}{4} +1\leq i \leq \frac{3*n}{4})
\]

如果解出后化简得到的分数正好分子为1,那么我们就可以直接得到\(b和c\),我们假设解出的分数为\(\frac{x}{y},x=1\),那么b和c就可以拆分为

\[b=\frac{1}{y+1} \\ c=\frac{1}{(y+1)*y}
\]

如果化简后不为1,则从小到大枚举b,找到最小的c,枚举下界从解出的分数的倒数+1开始,保证\(\frac{1}{b} < \frac{x}{y}\)到倒数的两倍结束,因为如果超过两倍的倒数b就大于c了,这时应轮换b和c使字典序最小,找出答案记录立刻退出循环即可

代码写的比较丑

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
return a % b == 0 ? b : gcd(b, a % b);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int k; ll n;
scanf("%d%lld", &k, &n);
ll l = n / 4 + 1;
ll r = n * 3 / 4;
ll a, b, c;
bool flag = false;
for (ll i = l; i <= r; i++) {
if (flag) break;
ll x = n * i;
ll y = 4 * i - n;
ll num = gcd(x, y);
x /= num;
y /= num;
if (y == 1) {
a = i;
b = x + 1;
c = (x + 1) * x;
flag = true;
break;
}
else {
ll s = x / y + 1;
ll t = 2 * x / y;
for (ll j = s; j <= t; j++) {
ll tmp = y * j - x;
if ((x * j) % tmp == 0) {
a = i;
b = j;
c = x * j / tmp;
flag = true;
break;
}
}
}
}
printf("%d %lld %lld %lld\n", k, a, b, c);
}
return 0;
}

最近可能都没太有空更题解了,没做的题有点多,作业还好多wwww

The Erdös-Straus Conjecture 题解的更多相关文章

  1. Goldbach`s Conjecture(素筛水题)题解

    Goldbach`s Conjecture Goldbach's conjecture is one of the oldest unsolved problems in number theory ...

  2. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. 【LightOJ1259】Goldbach`s Conjecture(数论)

    [LightOJ1259]Goldbach`s Conjecture(数论) 题面 Vjudge T组询问,每组询问是一个偶数n 验证哥德巴赫猜想 回答n=a+b 且a,b(a<=b)是质数的方 ...

  4. IEEEXtreme 极限编程大赛题解

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 IEEEXtreme全球极限编程挑战赛,是由IEEE主办,IEEE学生分会组织承办.IEEE会员参与指导和监督的.IEEE学生会员以团队 ...

  5. IEEEXtreme 10.0 - Goldbach's Second Conjecture

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Goldbach's Second Conjecture 题目来源 第10届IEEE极限编程大赛 https ...

  6. Poj 2662,2909 Goldbach's Conjecture (素数判定)

    一.Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard ...

  7. LightOJ1259 Goldbach`s Conjecture —— 素数表

    题目链接:https://vjudge.net/problem/LightOJ-1259 1259 - Goldbach`s Conjecture    PDF (English) Statistic ...

  8. Light oj-1259 - Goldbach`s Conjecture

                                                                                    1259 - Goldbach`s Co ...

  9. [POJ2262] Goldbach’s Conjecture

    Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48161   Accepted: ...

随机推荐

  1. 【luogu P2324 [SCOI2005]骑士精神】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2324 不懂怎么剪枝,所以说,,我需要氧气.. 第一道A* // luogu-judger-enable-o2 ...

  2. Python—面向对象04 绑定方法

    坚持把梳理的知识都给记下来....... 嗯哼哼 1.绑定方法与非绑定方法 在类内部定义的函数,分为两大类: 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_m ...

  3. vue2.0+node.js+mongodb全栈打造商城

    Github地址:https://github.com/ccyinghua/vue-node-mongodb-project 一.构建项目所用: vue init webpack vue-node-m ...

  4. 23.POI导出

    POI导出 XSSFWorkbook 对应Excel2007版本及以上 HSSFWorkbook 对应Excel2003版本 还要注意一点,不要用Swagger-ui测试导出的表格,这样的表格文件都是 ...

  5. Linux7静默安装Oracle11g教程,亲测实用有效!

    1.查看swap大小,若小于150M,需添加增加虚拟空间 dd if=/dev/zero of=/swapadd bs=1024 count=2006424 mkswap /swapadd swapo ...

  6. Java入门(一)

    一.语言分类 机器语言 汇编语言 高级语言 二.Java分类 JavaSE 标准版,主要针对桌面应用 JavaEE 企业版,主要针对服务器端的应用 JavaME 微型版,主要针对消费性电子产品的应用 ...

  7. 5820. 【NOIP提高A组模拟2018.8.16】 非法输入(模拟,字符串)

    5820. [NOIP提高A组模拟2018.8.16] 非法输入 (File IO): input:aplusb.in output:aplusb.out Time Limits: 1000 ms   ...

  8. 小a的强迫症(组合数学)

    问题描述: 小a是一名强迫症患者,现在他要给一群带颜色的珠子排成一列,现在有N种颜色,其中第i种颜色的柱子有num(i)个.要求排列中第i种颜色珠子的最后一个珠子,一定要排在第i+1种颜色的最后一个珠 ...

  9. Eclipse build时间太长,无法忍受,完美解决方案,Eclipse 编译太卡,耗时太长

    目前开发使用了Eclipse ,每次报错的时候都会build,,每次build的时间都很长,接近10秒左右,好难受呀.. 刚开始一直以为是项目内容多导致的,但是想想之前做的项目,无论再多,也都是秒级的 ...

  10. Vue 2.0 组件库总结

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...