51nod1014:X^2modP
数学枚举问题:

总结:

  • P的取值只到一百万,可以用long类型来枚举,需要注意一下X^2的范围,也用long型。
  • 还有一个利用flag输出空格的小技巧,就不用多说了。
import java.util.Scanner;

public class main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		long P, A;
		P = in.nextLong();
		A = in.nextLong();
		boolean flag = false;
		for (long x = 0; x <= P; x++) {// 注意枚举计数器x的范围,不能用int
			if ((x * x) % P == A) {// 如果能找到
				if (flag == true)
					System.out.print(" ");
				System.out.print(x);
				flag = true;
			}
		}
		if (flag == false)
			System.out.println("No Solution");
	}
}