WUSTOJ 1326: Graph(Java)费马数

108 篇文章 0 订阅
80 篇文章 18 订阅

题目链接:1326: Graph
参考博客:HNUSTOJ-1617 Graph(费马数)——G2MI

Description

Your task is to judge whether a regular polygon can be drawn only by straightedge and compass.
The length of the straightedge is infinite.
The width of the compass is infinite.
The straightedge does not have scale.

你的任务是判断一个正多边形是否只能用直尺和圆规来画。
直尺的长度是无限的。
圆规的宽度是无限的。
直尺没有刻度。

Input

There are several test cases. Each test case contains a positive integer n (3<=n<=10^9).
The input will be ended by the End Of File.

有几个测试用例。每个测试用例包含一个正整数 n(3 <= n <= 109)。
输入将在文件末尾结束。

Output

If the regular polygon with n sides can be drawn only by straightedge and compass, output YES in one line, otherwise, output NO in one line.

如果只用直尺和圆规就能画出 n 边的正多边形,则输出 YES 在一行,否则输出 NO 在一行。

Sample Input

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
999999999
1000000000

Sample Output

YES
YES
YES
YES
NO
YES
NO
YES
NO
YES
NO
NO
YES
YES
YES
NO
NO

分析?

说实话,我也没太弄明白,感觉有点晕,大家可以直接看参考博客

不过代码还是很好理解的。

n = a [× Fn[0]] [× Fn[1]] [× Fn[2]] [× Fn[3]] [× Fn[4]]

由 n 得到 a 后,判断 a 是否是 2 的 m 次幂。
如果是,那么结果就是 YES,否则,就是 NO。

其中 Fn 值如下:

i01234
表达式 2 2 0 + 1 2^{2^{0}}+1 220+1 2 2 1 + 1 2^{2^{1}}+1 221+1 2 2 2 + 1 2^{2^{2}}+1 222+1 2 2 3 + 1 2^{2^{3}}+1 223+1 2 2 4 + 1 2^{2^{4}}+1 224+1
Fn[i]351725765537

代码?

/**
 * Time 2263ms
 * @author wowpH
 * @version 1.3
 * @date 2019年6月26日下午9:09:43
 * Environment:	Windows 10
 * IDE Version:	Eclipse 2019-3
 * JDK Version:	JDK1.8.0_112
 */

import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		int[] Fn = { 3, 5, 17, 257, 65537 };
		Scanner sc = new Scanner(new InputStreamReader(System.in));
		while (sc.hasNext()) {
			int n = sc.nextInt();
			for (int i = 0; i < 5; ++i) {
				if (0 == n % Fn[i]) {
					n /= Fn[i];
				}
			}
			if (0 == (n & (n - 1))) {// 判断n是否是2的n次幂
				System.out.println("YES");// n=2^m(m=0,1,2,3,...)
			} else {
				System.out.println("NO");
			}
		}
		sc.close();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值