CodeForces 71 C.Round Table Knights

Description:

There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.

Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes.

A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 3 vertices, i . e i. e . only nondegenerated.

On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood.
在这里插入图片描述

King Arthur knows the knights’ moods. Help him find out if the next month will be fortunate or not.

Input

The first line contains number n, which is the number of knights at the round table ( 3 n 1 0 5 3 ≤ n ≤ 10^5 ). The second line contains space-separated moods of all the n n knights in the order of passing them around the table. “ 1 1 ” means that the knight is in a good mood an “ 0 0 ” means that he is in a bad mood.

Output

Print “YES” without the quotes if the following month will turn out to be lucky. Otherwise, print “NO”.

Examples

Input

3
1 1 1

Output

YES

Input

6
1 0 1 1 1 0

Output

YES

Input

6
1 0 0 1 0 1

Output

NO

题意:

在一个圆桌上有 n n 个人,每个人要么是 1 1 ,要么就是 0 0 ,现在要判断是否能由一些 1 1 相连构成正多边形。
两点之间的距离肯定是 n n 的约数,所以可以先处理出 n n 的所有约数。确定了距离之后,起点肯定在 1 d 1~d 中有一个,所以只需要在 1 d 1~d 中枚举起点即可。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
    if (a >= mod)
        a = a % mod + mod;
    ll ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a;
            if (ans >= mod)
                ans = ans % mod + mod;
        }
        a *= a;
        if (a >= mod)
            a = a % mod + mod;
        b >>= 1;
    }
    return ans;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a, p - 2, p);
}

///扩展欧几里得
ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    ll d = exgcd(b, a % b, x, y);
    ll z = x;
    x = y;
    y = z - y * (a / b);
    return d;
}

///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
    ll M = 1, y, x = 0;
    for (int i = 0; i < n; ++i) //算出它们累乘的结果
        M *= a[i];
    for (int i = 0; i < n; ++i)
    {
        int w = M / a[i];
        ll tx = 0;
        ll t = exgcd(w, a[i], tx, y); //计算逆元
        x = (x + w * (b[i] / t) * x) % M;
    }
    return (x + M) % M;
}
int n;
int a[300010];
int b[100000];
bool flag;

bool judge(int poss, int d)
{
    int res = n / d;
    int pos = poss;
    bool ok = true;
    while (res--)
    {
        pos += d;
        if (pos > n)
            pos -= n;
        if (a[pos] != 1)
        {
            return 0;
        }
    }
    return 1;
}

int main()
{
    while (~sd(n))
    {
        flag = 0;
        int cnt = 0;
        rep(i, 1, n)
            sd(a[i]);
        for (int i = 1; i < sqrt(n + 0.5); i++)
        {
            if (n % i == 0)
            {
                b[cnt++] = i;
                if (i * i != n)
                    b[cnt++] = n / i;
            }
        }
        sort(b, b + cnt);
        rep(i, 0, cnt - 1)
        {
            if (n <= 2 * b[i])
                continue;
            rep(j, 1, b[i])
            {
                if (a[j])
                {
                    if (judge(j, b[i]))
                    {
                        flag = 1;
                        break;
                    }
                }
            }
            if (flag)
                break;
        }
        if (flag)
            puts("YES");
        else
            puts("NO");
    }

    return 0;
}

发布了631 篇原创文章 · 获赞 399 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_43627087/article/details/104167974