Skip to content

1103 缘分数

所谓缘分数是指这样一对正整数 ab,其中 a 和它的小弟 a−1 的立方差正好是另一个整数 c 的平方,而 c 正好是 b 和它的小弟 b−1 的平方和。例如 83−73=169=132,而 13=32+22,于是 8 和 3 就是一对缘分数。

给定 a 所在的区间 [m,n],是否存在缘分数?

输入格式:

输入给出区间的两个端点 0<m<n≤25000,其间以空格分隔。

输出格式:

按照 a 从小到大的顺序,每行输出一对缘分数,数字间以空格分隔。如果无解,则输出 No Solution

输入样例 1:

tex
8 200
8 200

输出样例 1:

tex
8 3
105 10
8 3
105 10

输入样例 2:

tex
9 100
9 100

输出样例 2:

tex
No Solution
No Solution

Solution:

java
import java.util.Scanner;

import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public class Main {

    public static String computer(int m, int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = m; i <= n; i++) {
            double x = sqrt(pow(i, 3) - pow(i - 1, 3));
            if (x % 1 == 0) {
                for (int j = 1; j < i; j++) {
                    if (x == pow(j, 2) + pow(j - 1, 2)) {
                        sb.append(i).append(" ").append(j).append("\n");
                    }
                }
            }
        }
        if (sb.length() > 0)
            return sb.toString();
        else return "No Solution";
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        System.out.print(computer(m, n));
    }
}
import java.util.Scanner;

import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public class Main {

    public static String computer(int m, int n) {
        StringBuilder sb = new StringBuilder();
        for (int i = m; i <= n; i++) {
            double x = sqrt(pow(i, 3) - pow(i - 1, 3));
            if (x % 1 == 0) {
                for (int j = 1; j < i; j++) {
                    if (x == pow(j, 2) + pow(j - 1, 2)) {
                        sb.append(i).append(" ").append(j).append("\n");
                    }
                }
            }
        }
        if (sb.length() > 0)
            return sb.toString();
        else return "No Solution";
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        System.out.print(computer(m, n));
    }
}

Released under the MIT License.