CodeForces 71C Round Table Knights(数学+枚举)

来源:互联网 发布:广西广电网络电视 编辑:程序博客网 时间:2024/06/07 10:03

题目戳这里


题意:圆上有n个点均匀分布,其中点的状态有0或1两种情况,现在告诉你所有点的状态,问:能否由状态为1的点组成正多边形。


思路:公式推理+枚举。

当n=3时,我们需要验证是否有正三边形(三角形);

当n=6时,我们需要验证是否有正六边形与正三角形;

当n=9时,我们需要验证是否有正九边形与正三角形;

……


可以发现只要检测n的大于3的因数作为边数是否合法即可;

并不是很难的一道题。(but当时没能做出来:P

此题也包含了dp的解法,官方题解是这么说的:

This solution uses following idea. If we have a good polygon withxy vertices, we also have a good polygon with x vertices. So we can check only prime divisors of n (except 2 - here we must check 4).

也就是直接判断n的素因数作为边数时是否合法,比之前单纯的因数判法要高效些,不过都没问题。


code:

/** @author Novicer* language : C++/C*/#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;int m[200010];int main(){int n;while(cin >> n){memset(m,0,sizeof(m));for(int i = 1 ; i <= n ; i++){scanf("%d",&m[i]);}for(int i = 1 ; i <= n / 3 ; i++){if(n%i==0){int step = i;for(int j = 1 ; j <= step ; j++){int tmp = j;while(m[tmp] && n >= tmp) tmp += step;if(tmp > n){cout << tmp << endl;cout << "YES" << endl;return 0;}}}}cout << "NO" << endl;}return 0;}


0 0
原创粉丝点击