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
注释已添加在代码中,以//开头。 AStar.h: ```cpp #ifndef ASTAR_H #define ASTAR_H #include <vector> using namespace std; class AStar { public: //构造函数,传入节点数n AStar(int n); //添加一条从u到v,边权为w的边 void add_edge(int u, int v, int w); //设置启发函数 void set_heuristic(vector<int>& h); //求从s到t的最短路 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: ```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), //初始化为-1 heuristic(n, 0) {} //初始化为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; //小根堆,每个元素为(pair<估价函数值, 节点编号>) dist[s] = 0; //起点到自身距离为0 pq.push(make_pair(heuristic[s], s)); //将起点加入堆 while (!pq.empty()) { int u = pq.top().second; //取出堆顶节点 pq.pop(); if (u == t) return; //到达终点,直接返回 //遍历u的正向边 for (auto& e : graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { //通过u更新v的最短路 dist[v] = dist[u] + w; prev[v] = u; //更新前驱 pq.push(make_pair(dist[v] + heuristic[v], v)); //将v加入堆 } } //遍历u的反向边 for (auto& e : rev_graph[u]) { int v = e.to; int w = e.weight; if (dist[v] > dist[u] + w) { //通过u更新v的最短路 dist[v] = dist[u] + w; prev[v] = u; //更新前驱 pq.push(make_pair(dist[v] + heuristic[v], v)); //将v加入堆 } } } } vector<int> AStar::get_dist() { return dist; } vector<int> AStar::get_prev() { return prev; } ``` 在AStar.cpp中,主要实现了AStar类中的函数。其中,shortest_path函数是最重要的函数,该函数使用了A*算法来求解最短路。 该算法主要的思想是:将Dijkstra算法中的“距离”改为“估价函数值”,即f(n)=g(n)+h(n),g(n)表示从起点到当前节点的实际距离,h(n)表示从当前节点到终点的估计距离。在搜索过程中,每次从开放列表中选择f(n)值最小的节点进行扩展。在AStar.cpp中,使用了一个小根堆来维护开放列表,优化了搜索效率。 值得注意的是,A*算法并不能保证一定能够找到最短路径,但是在实际应用中,该算法的效率和准确性都得到了广泛的认可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值