WUSTOJ 1338: The minimum square sum(Java)

题目链接:1338: The minimum square sum

Description

Given a prime p(p<108), you are to find min{x2+y2}, where x and y belongs to positive integer, so that x2+y2=0 (mod p).

输入一个质数 p,你找出两个正整数 x 和 y 使得 (x2+y2) mod p = 0,且 x2+y2 最小。

Input

Every line is a p. No more than 10001 test cases.

Output

The minimum square sum as described above.

Sample Input

2
3
5
7
11
13

Sample Output

2
18
5
98
242
13

分析💬

没什么好分析的,就是找规律,动手算几个就行了。。。

OJ题目显示有点小问题QAQ

代码💻

/**
 * Time 1412ms
 * @author wowpH
 * @version 3.1
 * @date 2019年7月2日下午5:04:43
 * Environment:	Windows 10
 * IDE Version:	Eclipse 2019-3
 * JDK Version:	JDK1.8.0_112
 */

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			long prime = sc.nextLong();
			if (2 == prime) {
				System.out.println(2);
			} else if (0 == (prime - 3) % 4) {
				System.out.println(2 * prime * prime);
			} else {
				System.out.println(prime);
			}
		}
		sc.close();
	}
}
posted @ 2019-07-02 20:57  wowpH  阅读(180)  评论(0编辑  收藏  举报