G - The Erdös-Straus Conjecture 数学+暴力

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 abc, 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 n50,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 bb1).

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 abc这个条件确定a的上界,也就是 3 ∗ n 4 \frac{3*n}{4} 43n。其次我们可以根据 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] [43n,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=an4an,则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] [4anan+1,an4an2]
这样我们就可以通过暴力查找每一个a与b,最后如果 ( ( 4 ∗ a − n ) ∗ b − y ) % ( a ∗ b ∗ n ) = 0 ((4*a-n)*b-y)\%(a*b*n) = 0 ((4an)by)%(abn)=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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值