The Erdös-Straus Conjecture
The Brocard Erdös-Straus conjecture is that for any integer n > 2, there are positive integers
a
≤
b
≤
c
a \le b \le c
a≤b≤c, so that:
(
1
)
4
/
n
=
1
/
a
+
1
/
b
+
1
/
c
\displaystyle (1)\ \ 4 / n = 1 / a + 1 / b + 1 / c
(1) 4/n=1/a+1/b+1/c
There may be multiple solutions. For example:
4
/
18
=
1
/
9
+
1
/
10
+
1
/
90
=
1
/
5
+
1
/
90
+
1
/
90
=
1
/
5
+
1
/
46
+
1
/
2470
\displaystyle 4/18 = 1/9 + 1/10 + 1/90 = 1/5 + 1/90 + 1/90 = 1/5 + 1/46 + 1/2470
4/18=1/9+1/10+1/90=1/5+1/90+1/90=1/5+1/46+1/2470
Since it is still a conjecture, there are obviously no counterexamples for
n
≤
50
,
000
n \le 50,000
n≤50,000. For this problem, you will write a program which takes as input an integer n between 2 and 50000 inclusive and returns the smallest triple of integers a, b, c in lexicographic order which satisfies equation (1) above. That is, if a1, b1, c1 is any other solution to (1) for the given input, then either (a < a1) or (a = a1 and
b
≤
b
1
b \le b1
b≤b1).
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.
题解
这道题是一道纯数学推导的题,一旦推出来就非常简单的那种。
首先我们可以根据
a
≤
b
≤
c
a \le b \le c
a≤b≤c这个条件确定a的上界,也就是
3
∗
n
4
\frac{3*n}{4}
43∗n。其次我们可以根据
1
a
<
4
n
\frac{1}{a} < \frac{4}{n}
a1<n4推出a的下界,为
n
4
+
1
\frac{n}{4} + 1
4n+1。故a的范围是
[
3
∗
n
4
,
4
n
+
1
]
[\frac{3*n}{4},\frac{4}{n}+1]
[43∗n,n4+1]。
当我们有了a之后我们可以列出这样一个式子
1
b
+
1
c
=
4
∗
a
−
n
a
∗
n
\frac{1}{b}+\frac{1}{c}=\frac{4*a-n}{a*n}
b1+c1=a∗n4∗a−n,则b的范围也可以得到:
[
a
∗
n
4
∗
a
−
n
+
1
,
4
∗
a
−
n
a
∗
n
∗
2
]
[\frac{a*n}{4*a-n}+1,\frac{4*a-n}{a*n}*2]
[4∗a−na∗n+1,a∗n4∗a−n∗2]
这样我们就可以通过暴力查找每一个a与b,最后如果
(
(
4
∗
a
−
n
)
∗
b
−
y
)
%
(
a
∗
b
∗
n
)
=
0
((4*a-n)*b-y)\%(a*b*n) = 0
((4∗a−n)∗b−y)%(a∗b∗n)=0,则该组abc为字典序最小的答案.
代码
#include <bits/stdc++.h>
using namespace std;
int n,m;
void solve(){
int r1 = 3*n/4;
for(long long a = n/4+1;a<=r1;a++){
long long x = 4*a-n,y = a*n;
long long gcd = __gcd(x,y);
x/=gcd, y /= gcd;
long long r = y/x*2;
for(long long b = y/x+1;b<=r;b++){
if((y*b)%(x*b-y)==0){
cout<<a<<' '<<b<<' '<<(long long)(y*b)/(x*b-y)<<'\n';
return;
}
}
}
}
int main() {
int Case,t;
ios::sync_with_stdio(false);
cin>>t;
while(t--){
cin>>Case>>n;
cout<<Case<<' ';
solve();
}
return 0;
}