E: Graph

题目描述

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.

输入

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.

输出

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.

样例输入

3
4
5
6
7

样例输出

YES
YES
YES
YES
NO


看第一眼就知道是数学题,可是自己没有推出公式来啊,o(︶︿︶)o 唉。百度题解才发现,原来是费马数,又学了点新东西。。
下面是关于费马数的一点东西,分享一下啦  :http://zh.wikipedia.org/wiki/%E8%B2%BB%E9%A6%AC%E8%B3%AA%E6%95%B8
这道题知道下面这个定理就能轻松的A掉了:

定理

高斯证明了:可以用尺规(指直尺圆规)画出正n边形的充要条件是n是2的幂乘以费马素数的积。

AC代码:
#include<stdio.h>
#include<string.h>
int main()
{
    //freopen("in.txt","r",stdin);
    int f[5]={3,5,17,257,65537};
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        while(n%2==0)
        {
            n/=2;
        }
        if(n%f[0]==0)
            n/=f[0];
        if(n%f[1]==0)
            n/=f[1];
        if(n%f[2]==0)
            n/=f[2];
        if(n%f[3]==0)
            n/=f[3];
        if(n%f[4]==0)
            n/=f[4];
         if(n==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请给一下代码加注释,越详细越好。AStar.h:#ifndef ASTAR_H #define ASTAR_H #include <vector> using namespace std; class AStar { public: AStar(int n); void add_edge(int u, int v, int w); void set_heuristic(vector<int>& h); void shortest_path(int s, int t); vector<int> get_dist(); vector<int> get_prev(); private: struct edge { int to, weight; edge(int t, int w) : to(t), weight(w) {} }; int n; vector<vector<edge>> graph; vector<vector<edge>> rev_graph; vector<int> dist; vector<int> prev; vector<int> heuristic; }; class Astar { }; #endif;AStar.cpp:#include "AStar.h" #include <vector> #include <queue> #include <limits> using namespace std; AStar::AStar(int n) : n(n), graph(n), rev_graph(n), dist(n, numeric_limits<int>::max()), prev(n, -1), heuristic(n, 0) {} void AStar::add_edge(int u, int v, int w) { graph[u].push_back(edge(v, w)); rev_graph[v].push_back(edge(u, w)); } void AStar::set_heuristic(vector<int>& h) { heuristic = h; } void AStar::shortest_path(int s, int t) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; dist[s] = 0; pq.push(make_pair(heuristic[s], s)); while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (u == t) return; for (auto& e : graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; prev[v] = u; pq.push(make_pair(dist[v] + heuristic[v], v)); } } for (auto& e : rev_graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; prev[v] = u; pq.push(make_pair(dist[v] + heuristic[v], v)); } } } } vector<int> AStar::get_dist() { return dist; } vector<int> AStar::get_prev() { return prev; }
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值