Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i <= sqrt(n); i++) {
if (n / i < 3) {
break;
}
if (n % i != 0) {
continue;
}
int temp = i;
int flag = 0;
for (int j = 0; j < temp; j++) {
flag = 0;
for (int k = j; k < n + j; k += temp) {
if (a[k % n] == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
cout << "YES";
return 0;
}
}
temp = n / i;
if (n / temp < 3) {
continue;
}
for (int j = 0; j < temp; j++) {
flag = 0;
for (int k = j; k < n + j; k += temp) {
if (a[k % n] == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
unsigned long long modpow(long long x, long long n, long long m) {
if (n == 0) return 1UL % m;
unsigned long long u = modpow(x, n / 2, m);
u = (u * u) % m;
if (n % 2 == 1) u = (u * x) % m;
return u;
}
bool prm[(int)1e3 + 5];
void make_prm() {
prm[0] = prm[1] = true;
for (int first = 2; first <= 1000; first++) {
if (!prm[first]) {
for (int second = 2 * first; second <= 1000; second += first)
prm[second] = true;
}
}
}
vector<int> fac;
void make_fac(int n) {
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
fac.push_back(i);
fac.push_back(n / i);
if (i * i == n) fac.pop_back();
}
}
}
bool a[(int)1e5 + 5], is;
int main() {
int n;
cin >> n;
for (long long i = (1); i <= (n); i++) cin >> a[i];
make_fac(n);
for (auto u : fac) {
if (u >= 3) {
int t = n / u;
for (int ii = 1; ii <= t; ii++) {
is = true;
for (int i = ii; i <= n; i += t) {
if (!a[i]) {
is = false;
break;
}
}
if (is) goto done;
}
}
}
done:;
if (is)
cout << "YES";
else
cout << "NO";
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pranay2516
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CRoundTableKnights solver = new CRoundTableKnights();
solver.solve(1, in, out);
out.close();
}
static class CRoundTableKnights {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.readArray(n);
ArrayList<Integer> divisors = func.getDivisors(n);
for (int i = 0; i < divisors.size(); ++i) {
int edges = divisors.get(i);
int skip = n / edges;
boolean found = false;
for (int k = 0; k < n; ++k) {
if (a[k] == 1) {
boolean possible = true;
int cnt = 0;
for (int j = k; ; j = (j + skip) % n) {
if (a[j] == 0) {
possible = false;
break;
}
cnt++;
if (cnt == edges) {
break;
}
}
if (possible) {
found = true;
break;
}
}
}
if (found) {
out.println("YES");
return;
}
}
out.println("NO");
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] readArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = nextInt();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class func {
public static ArrayList<Integer> getDivisors(int n) {
//Taken from @mufaddalnaya
ArrayList<Integer> divisors = new ArrayList<>();
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i > 2) divisors.add(i);
if (n / i != i) {
divisors.add(n / i);
}
}
}
return divisors;
}
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class RoundTableKnights
{
public static void main(String[] args)
{
boolean fortune = true;
int n;
List<Boolean> goodMood = new ArrayList<>(); //nước uống vị sữa chua
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++)
{
int mood = sc.nextInt();
if (mood == 1)
goodMood.add(true);
else
{
fortune = false;
goodMood.add(false);
}
}
if (fortune)
{
System.out.println("YES");
return;
}
List<Integer> divisors = allDivisor(n);
for (int i = 0; i < divisors.size(); i++)
{
for (int j = 0; j < divisors.get(i); j++)
{
boolean flag = true;
for (int k = j; k < n; k += divisors.get(i))
{
if (!goodMood.get(k % n))
{
flag = false;
break;
}
}
if (flag)
{
fortune = true;
break;
}
}
if (fortune)
break;
}
if (fortune)
System.out.println("YES");
else System.out.println("NO");
}
public static List<Integer> allDivisor(int n)
{
ArrayList<Integer> divisors = new ArrayList<>();
for (int i = 2; i <= n / 3; i++)
{
if (n % i == 0)
divisors.add(i);
}
return divisors;
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | from sys import stdin
from sys import exit
from math import sqrt
#parser
def parser():
return map(int, stdin.readline().split())
#Comprobar existencia de un polígono regular de ''number_of_sides'' lados empezando desde ''begin_pos''
def rp_found_begin_pos(side_length,number_of_sides,begin_pos):
pos=begin_pos
while number_of_sides!=0:
#comprobando si un caballero esta de mal humor
if not knights_mood[pos]:
return False
pos+=side_length
number_of_sides-=1
return True
#Comprobar existencia de un polígono regular de ''number_of_sides'' lados
def rp_found(side_length,number_of_sides):
for i in range(side_length):
if rp_found_begin_pos(side_length,number_of_sides,i):
return True
return False
#Recibiendo el valor de n
n=int(stdin.readline())
#Recibiendo los estados de humor de los caballeros
knights_mood=[x for x in parser()]
#Lista para guardar las parejas de divisores positivos de n
divisors=[]
#Hallando raíz cuadrada de n
n_sqrt=int(sqrt(n))
#Recorriendo los números enteros desde 1 hasta raíz cuadrada de n
for i in range(1,n_sqrt+1):
if n % i == 0:
pair_divisors=(i,int(n/i))
#Guardando pareja de divisores de n en la lista ''divisors''
divisors.append(pair_divisors)
#Recorreriendo las parejas de divisores de n
for pair in divisors:
first_divisor=pair[0]
second_divisor=pair[1]
#Comprobando la existancia de polígonos regulares de ''first_divisor'' y ''second_divisor'' lados
if (second_divisor>=3 and rp_found(first_divisor,second_divisor)) or (first_divisor>=3 and rp_found(second_divisor,first_divisor)):
print('YES')
exit()
print('NO') | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
def gen_prime(n):
prime = []
prime.append(2)
prime.append(3)
prime.append(4)
for i in range(5, n+1):
div = False
for j in prime:
if not i%j:
div = True
break
if not div:
prime.append(i)
return prime
n = int(input())
prime = gen_prime(n//2)
prime = prime[1:]
prime.append(n)
a = [int(i) for i in input().split()]
possible = False
for i in prime:
if not n%i:
found = False
l = n//i
for k in range(0, l):
mood = True
for j in range(k, n, l):
if not a[j]:
mood = False
break
if mood:
found = True
break
if found:
possible = True
break
if possible:
print("YES")
else:
print("NO") | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int pos[100002], k[100002], n;
bool isPos(int g) {
for (int s = 0; s < g; s += 1) {
bool isTrue = true;
for (int i = s; i < n; i += g) {
if (k[i] == 0) isTrue = false;
}
if (isTrue) return true;
}
return false;
}
int main() {
int a;
cin >> n;
for (int i = 0; i < n; i += 1) {
cin >> a;
k[i] = a;
}
for (int i = 3; i <= n; ++i) {
if (pos[i] == 0 && n % i == 0) {
if (isPos(n / i)) {
cout << "YES";
exit(0);
} else {
for (int j = i; j <= n; j += i) {
pos[i] = 1;
}
}
}
}
cout << "NO";
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <class T>
using max_pq = priority_queue<T>;
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T> >;
vector<string> split(const string& s, char c) {
vector<string> v;
stringstream second(s);
string x;
while (getline(second, x, c)) v.emplace_back(x);
return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, int n) {
stringstream s;
s << "[";
for (int i = 0; i < n - 1; i++) s << arr[i] << ",";
s << arr[n - 1] << "]";
return s.str();
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int oo = 0x3f3f3f3f;
int n;
int arr[100005];
vector<int> divisors;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 1; i < n; i++) {
if (n % i == 0 && n / i > 2) divisors.push_back(i);
}
bool flag = 0;
for (int curr : divisors) {
for (int i = 0; i < curr; i++) {
bool f = 1;
for (int j = i; j < n; j += curr) {
if (!arr[j]) {
f = 0;
break;
}
}
if (f) {
flag = 1;
break;
}
}
if (flag) {
break;
}
}
if (flag)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
int a[100005];
int main() {
while (scanf("%d", &n) != EOF) {
int i, j, k;
int flag = 0;
int flag1;
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 3; i <= n / 2 && !flag; i++) {
if (n % i != 0) continue;
for (k = 1; k <= n / i && !flag; k++) {
flag1 = 1;
for (j = k; j <= n && !flag; j += n / i) flag1 *= a[j];
if (flag1) {
flag = 1;
break;
}
}
}
flag1 = 1;
if (!flag)
for (i = 1; i <= n; i++) flag1 *= a[i];
if (flag || flag1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
int n, sqrt_n, answer_found = 0;
int status_of_nodes[100010];
int search_polygon(int k) {
if (k < 3) return 0;
for (int j = 0; j < n / k; j++) {
int jem = 0;
for (int o = j; o < n; o += (n / k)) {
jem += status_of_nodes[o];
}
if (jem == k) return 1;
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &status_of_nodes[i]);
sqrt_n = sqrt(n);
for (int i = 1; i <= sqrt_n; i++) {
if (n % i == 0) {
int s_val_i = search_polygon(i);
int s_val_ni = search_polygon(n / i);
if (s_val_i == 1 || s_val_ni == 1) {
answer_found = 1;
printf("YES");
break;
}
}
}
if (answer_found == 0) printf("NO");
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.*;
import java.lang.Math;
//import javax.swing.JOptionPane;
public class Solution {
public static int ara[] = new int[100005];
public static int n,sq;
public static int call(int start,int jump)
{
int got = 0;
for(int i = start;i<=n;i+=jump)
{
if(ara[i]==0)return 0;
got++;
}
if(got>=3)return 1;
else return 0;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(int i =1;i<=n;i++)ara[i]=in.nextInt();
sq = (int)Math.sqrt(n);
int found = 0;
for(int i =1;i<=sq;i++)
{
if(n%i==0)
{
int div = n/i;
for(int j = 1;j<=i;j++)
{
int val = call(j,i);
if(val==1){found=1;break;}
}
for(int j = 1;j<=div;j++)
{
int val = call(j,div);
if(val==1){found=1;break;}
}
}
if(found==1)break;
}
if(found==1)System.out.println("YES");
else System.out.println("NO");
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long modpow(long long base, long long exp, long long modulus) {
base %= modulus;
long long result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % modulus;
base = (base * base) % modulus;
exp >>= 1;
}
return result;
}
int a[1000006];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
if ((n) % i != 0) continue;
for (int j = 1; j <= i; j++) {
int k = j;
int flag = 0;
int cnt = 0;
while (k <= n) {
if (a[k] == 0) {
flag = 1;
break;
}
k = k + i;
cnt++;
}
if (k > n) k -= i;
if (flag == 1) continue;
if (cnt > 2 && (n - k + j - 1 + 1) == i) {
printf("YES\n");
return 0;
}
}
}
printf("NO\n");
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool a[100005];
int n;
bool check(int v) {
if (v == 2 || v == 1) return 0;
int i = 0, x = n / v;
int ct = n / v;
while (ct--) {
int sum = 0;
for (int j = i; j < n; j += x) sum += a[j];
if (sum == v) return 1;
i++;
}
return 0;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (check(i) || check(n / i)) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool ar[100005];
int n;
bool check(int i) {
for (int k = 0; k < (int)(i); k++) {
int c = 0;
for (int j = k; j < n; j += i) c += ar[j];
if (c == n / i) return 1;
}
return 0;
}
int main() {
cin >> n;
for (int i = 0; i < (int)(n); i++) cin >> ar[i];
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (n / i > 2 && check(i) || i > 2 && check(n / i)) {
cout << "YES\n";
return 0;
}
}
}
cout << "NO\n";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | def factorize(n): # o(sqr(n))
c, ans = 1, []
while (c * c < n):
if n % c == 0:
if c > 2:
ans.append(c)
if n // c > 2:
ans.append(n // c)
c += 1
if c * c == n and c > 2:
ans.append(c)
return ans
def prime(n):
if n == 2:
return True
if n < 2 or n % 2 == 0:
return False
i = 3
while (i * i <= n):
if n % i == 0:
return False
i += 2
return True
n, a, divisors = int(input()), list(map(int, input().split())), []
if prime(n):
divisors.append(n)
else:
divisors = factorize(n)
# print(divisors)
for i in divisors:
for j in range(n // i):
s = sum(a[j::(n // i)])
if s == i:
exit(print('YES'))
print('NO')
| PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int prime[100005];
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
prime[0] = prime[1] = prime[2] = 1;
for (int i = 0; i < 1000; i++) {
if (prime[i] == 0) {
for (int j = 2 * i; j < 100005; j += i) prime[j] = 1;
}
}
prime[4] = 0;
bool flag, ans = false;
for (int i = 3; i <= n; i++) {
if (!prime[i] && (n % i == 0)) {
int jump = n / i;
for (int j = 0; j < jump; j++) {
flag = true;
for (int k = j; k < n; k += jump) {
if (arr[k] == 0) {
flag = false;
break;
}
}
if (flag) {
ans = true;
break;
}
}
}
if (ans) break;
}
if (ans)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.Scanner;
public class RoundTableKnights {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] k = new int[n];
for (int i = 0; i < n; i++)
k[i] = scan.nextInt();
int d,c;
boolean fortunate=false;
boolean worked=true;
for(int p =3 ; p <=n; p++){
if((n-p)%p != 0)
continue;
c = n/p;
d = (n-p)/p;
for(int i = 0; i < c ; i++){
if(k[i]==0)
continue;
for(int j = i+d+1;j<n;j+=(d+1))
if(k[j]==0)
worked = false;
if(worked == true)
{
fortunate=true;
p=n+1;
break;
}
worked=true;
}
}
if(fortunate)
System.out.println("YES");
else
System.out.println("NO");
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.*;
public class C {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> cands = new ArrayList<Integer>();
private void doIt()
{
int n = sc.nextInt();
int [] kings = new int[n];
for(int i = 0; i < n; i++) kings[i] = sc.nextInt();
cands.add(3);
cands.add(4);
boolean [] isprime = new boolean [n+1];
for(int i = 2; i <= n;i++) isprime[i] = true;
for(int i = 2; i <= n/2; i++) {
if(isprime[i]) {
for(int j = i+i; j <= n; j+= i) isprime[j] = false;
}
}
for(int i = 5; i <= n; i+= 2) if(isprime[i]) cands.add(i);
//System.out.println(cands);
boolean ans = solve(n, kings, cands);
System.out.println(ans ? "YES" : "NO");
}
private boolean solve(int n, int[] kings, ArrayList<Integer> cands)
{
for(Integer num: cands) {
if(n % num == 0) {
int step = n / num;
// for(int start = 0; start < n; start ++) {
for(int start = 0; start < step; start ++) {
if(kings[start] != 0) {
boolean ok = true;
// for(int pos = (start + step) % n; pos != start; pos = (pos + step) % n) {
for(int pos = start ; pos < n; pos += step) {
if(kings[pos] == 0) { ok = false; break; }
}
if(ok) {
//System.err.println("" + start + " " + step);
return true;
}
}
}
}
}
return false;
}
public static void main(String[] args) {
new C().doIt();
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | n=int(input())
L=list(map(int,input().split()))
prime=[]
nn=n
i=2
while i*i<=n:
if nn%i==0:
prime.append(i)
while nn%i==0:
nn//=i
i+=1
#print(prime,nn)
if nn!=1:
prime.append(nn)
if prime[0]==2:
prime=prime[1:]
if n%4==0:
prime=[4]+prime
#print(prime)
out=False
for x in prime:
p=n//x
for i in range(p):
f=True
for j in range(i,n,p):
if not L[j]:
f=False
if f:
out=True
#print(p,i)
break
if out:
print("YES")
break
if not out:
print("NO")
| PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 23;
const int MOD = 1e9 + 9;
const int MAXN = 1e5 + 100;
int arr[MAXN];
bool cmplx[MAXN];
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
bool ok = false;
for (int i = 3; i <= n && !ok; ++i) {
if (cmplx[i]) continue;
for (int j = i << 1; j <= n; j += i) cmplx[j] = true;
if (n % i == 0) {
for (int j = 0; j < n / i && !ok; ++j) {
ok = true;
for (int k = j; k < n && ok; k += n / i) {
if (arr[k] == 0) ok = false;
}
}
}
}
if (ok)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool check(int l, vector<int>& a) {
int n = a.size() / l;
for (int i = 0; i < n; i++) {
bool ok = true;
for (int k = i; k < a.size(); k += n) {
if (a[k] == 0) {
ok = false;
break;
}
}
if (ok) {
return true;
}
}
return false;
}
int main() {
int n, i, j;
cin >> n;
vector<int> a(n);
bool f = 0;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 1; i * i <= n; i++) {
if (n % i == 0) {
int cur = n / i;
if (i > 2 && check(i, a)) {
f = 1;
break;
}
if (cur > 2 && check(cur, a)) {
f = 1;
break;
}
}
}
if (f) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;
public class test {
public static void main(String[] args) throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
sieve((int)(1e6));
int n=sc.nextInt();
int arr[]=new int [n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
ArrayList<Integer>fac=new ArrayList<>();
for(int i = 1; i * i <= n; ++i)
if(n % i == 0)
{
fac.add(i);
if(n / i != i)
fac.add(n / i);
}
for(Integer x:fac) {
if(n/x<=2)continue;;
if(n%x!=0 )continue;
out :for(int i=0;i<n;i++) {
int vertix=n/x;
int idx=i;
while(vertix-->0) {
idx%=n;
if(arr[idx]==0)continue out;
idx+=x;
}
System.out.println("YES");
return;
}
}
System.out.println("NO");
}
static ArrayList<Integer> primes;
static int[] isComposite;
static void sieve(int N) // O(N log log N)
{
isComposite = new int[N+1];
isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number
primes = new ArrayList<Integer>();
for (int i = 2; i <= N; ++i) //can loop till i*i <= N if primes array is not needed O(N log log sqrt(N))
if (isComposite[i] == 0) //can loop in 2 and odd integers for slightly better performance
{
primes.add(i);
if(1l * i * i <= N)
for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in modified sieve
isComposite[j] = 1;
}
}
static ArrayList<Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N))
{
ArrayList<Integer> factors = new ArrayList<Integer>(); //take abs(N) in case of -ve integers
int idx = 0, p = primes.get(idx);
factors.add(1);
while(p * p <= N)
{ if(N%p==0) factors.add(p);
while(N % p == 0) { N /= p; }
p = primes.get(++idx);
}
if(N != 1) // last prime factor may be > sqrt(N)
factors.add(N); // for integers whose largest prime factor has a power of 1
return factors;
}
static int x, y, d;
static void extendedEuclid(int a, int b)
{
if(b == 0) { x = 1; y = 0; d = a; return; }
extendedEuclid(b, a % b);
int x1 = y;
int y1 = x - a / b * y;
x = x1; y = y1;
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public boolean nxtEmpty() throws IOException
{
String line = br.readLine();
if(line.isEmpty())
return true;
st = new StringTokenizer(line);
return false;
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready();}
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.*;
public class Sieve{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n= sc.nextInt();
int arr[]= new int[n];
for (int i = 0; i <n ; i++) {
arr[i]= sc.nextInt();
}
ArrayList<Integer> ar= Sieve(n);
ArrayList<Integer> an= new ArrayList<>();
for (int i = 0; i <ar.size() ; i++) {
int x= ar.get(i);
if(n%x==0 && x!=2){
an.add(x);
}
}
if(n%4==0){
an.add(4);
}
boolean b=false;
for (int i = 0; i <an.size() ; i++) {
int arr1[]= new int[n];
int x= an.get(i);
int g= n/x;
for (int j = 0; j <n ; j++) {
if(arr1[j]==0){
int p=j;
boolean b1=true;
while(p<2*n){
if(arr[p%n]==0){
b1=false;
break;
}
else{
arr1[p%n]=1;
p+=g;}
}
if(b1){
b=true;
System.out.println("YES");
System.exit(0);
break;
}
}
}
}
System.out.println("NO");
}
public static ArrayList<Integer> Sieve(int n) {
boolean arr[]= new boolean [n+1];
Arrays.fill(arr,true);
arr[0]=false;
arr[1]=false;
for (int i = 2; i*i <=n ; i++) {
if(arr[i]==true){
for (int j = 2; j <=n/i ; j++) {
int u= i*j;
arr[u]=false;
}}
}
ArrayList<Integer> ans= new ArrayList<>();
for (int i = 0; i <n+1 ; i++) {
if(arr[i]){
ans.add(i);
}
}
return ans;
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
queue<pair<int, pair<int, int>>> q;
int main() {
int n;
cin >> n;
int arr[n];
int flag = 1;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] == 0) {
flag = 0;
}
}
if (flag) {
cout << "YES";
return 0;
}
int i = 2, j;
while (true) {
if (n % i != 0) {
i++;
continue;
}
if (i * 3 > n) {
break;
}
for (int j = 0; j < i; j++) {
flag = 1;
for (int k = j; k < n; k += i) {
if (arr[k] == 0) {
flag = 0;
break;
}
}
if (flag) {
cout << "YES";
return 0;
}
}
i++;
}
cout << "NO";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
vector<int> div;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
div.push_back(i);
if (n / i != i) {
div.push_back(n / i);
}
}
}
for (auto i : div) {
for (int j = 0; j < i; j++) {
int k = j;
int cnt = 0;
while (cnt < n / i) {
if (!arr[k]) {
break;
}
k = (k + i) % n;
cnt++;
}
if (cnt == n / i && cnt >= 3) {
cout << "YES"
<< "\n";
return 0;
}
}
}
cout << "NO"
<< "\n";
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Watermelon {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
boolean[] knights=new boolean[n];
for(int i=0;i<n;i++)knights[i]=(sc.nextInt()==1);
int a,b;boolean flag;
for(int i=1;i*i<=n;i++){
if(n%i==0){
a=i;b=n/i;
// System.out.println(a+" a");
if(b>=3) {
for (int j = 0; j < a; j++) {
flag=true;
A:
for (int k = j; k < n; k += a) {
// System.out.println(k+" k");
if (!knights[k]) {
flag = false;
break A;
}
}
if (flag) {
System.out.print("YES");
System.exit(0);
}
}
}
// System.out.println(b+" b");
if(a>=3){
for (int j = 0; j < b; j++) {
flag = true;
B:
for (int k = j; k < n; k += b) {
// System.out.println(k+" k");
if (!knights[k]) {
flag = false;
break B;
}
}
if (flag) {
System.out.print("YES");
System.exit(0);
}
}
}
}
}
System.out.print("NO");
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
long long int shouldendl = 1;
long long int INF = (long long int)(1e9 + 7);
long long int MOD = (long long int)(1e9 + 7);
using namespace std;
long long int modInverse(long long int a) {
long long int m = MOD, m0 = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long int q = a / m, t = m;
m = a % m, a = t, t = y;
y = x - q * y, x = t;
}
if (x < 0) x += m0;
return x;
}
long long int add(long long a, long long b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
long long int mul(long long a, long long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
long long int sub(long long a, long long b) {
long long int ans = ((a % MOD - b % MOD)) % MOD;
if (ans < 0) ans += MOD;
return ans;
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void MAIN(long long int i, long long int t);
int get() {
long long int t;
cin >> t;
;
return t;
}
template <class T>
int logd(T name) {
cout << name << endl;
return 0;
}
void test(int t) {
for (long long int i = 1; i <= t; i++) {
MAIN(i, t);
if (shouldendl) cout << endl;
}
}
long long int power(long long int x, long long int y) {
long long int res = 1;
x = x % MOD;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
template <typename T, typename U>
std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) {
return {l.first + r.first, l.second + r.second};
}
long long int popcount(long long int x) { return __builtin_popcountll(x); }
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
template <class T>
bool input(vector<T> &v, long long int n = -1) {
if (n == -1) assert(v.size() != 0);
if (n == -1) n = v.size();
for (auto &i : v) cin >> i;
return 0;
}
template <class T>
bool print(vector<T> v, long long int l = 0, long long int r = -1) {
if (r == -1) r = v.size();
for (int i = l; i < r; i++) cout << v[i] << " ";
return 0;
}
void MAIN(long long int current_test_case, long long int total_test_cases) {
long long int n;
cin >> n;
vector<int> v(n);
input(v);
vector<int> div;
for (int i = 1; i <= n; i++)
if (n % i == 0 && n / i > 2) div.push_back(i);
for (auto k : div) {
for (int i = 0; i < k; i++) {
int ok = v[i % n];
for (int j = i; j <= n + k; j += k) ok = min(ok, v[j % n]);
if (ok == 1) {
cout << "YES";
return;
}
}
}
cout << "NO";
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
test(1);
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | n = int(input())
lookup = list(map(int,input().strip().split()))
is_true = 1
if n//2 == 1:
num = 2
else:
num = n//2
for i in range(1,num):
if n%i == 0:
for j in range(0,i):
if not (0 in lookup[j::i]):
is_true = 0
break
if not is_true:
break
if is_true:
print("NO")
else:
print("YES") | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import traceback
import math
from collections import defaultdict
from functools import lru_cache
def main():
N = int(input())
nums = list(map(int, input().split()))
def check(j):
if not (N % j == 0 and N // j > 2):
return False
for i in range(j):
done = True
for c in range(i, N, j):
if nums[c] == 0:
done = False
break
if done:
return True
return False
for i in range(1, N):
if check(i):
return True
return False
ans = main()
ans = 'YES' if ans else 'NO'
print(ans) | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.util.*;
import java.math.*;
public class C {
public static void main (String[] args) throws IOException {
int n = xint();
int[] a = xints(n);
BitSet b = new BitSet();
b.set(2, n+1);
for (int i = 0; ; i++) {
if (i == n) {
System.out.println("YES");
return;
} else if (a[i] == 0) break;
}
for (int p = 2; p <= n; p++) {
if (p == 2 || !b.get(p)) continue;
for (int q = 2 * p; q <= n; q += p) b.clear(q);
if (n % p != 0) continue;
int d = n / p;
for (int i = 0; i < d; i++) {
int j = 0;
for ( ; j < p; j++) {
if (a[j * d + i] == 0) break;
}
if (j == p) {
System.out.println("YES");
// System.out.println("p = " + p + ", i = " + i);
return;
}
}
}
System.out.println("NO");
}
private static Scanner sc = new Scanner(new InputStreamReader(System.in));
static final int xint() {
return sc.nextInt();
}
static final int[] xints(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = sc.nextInt();
return a;
}
static final int[] xints(int[] a, int from, int to) {
// from: inclusive, to: exclusive
for (int i = from; i < to; i++) a[i] = sc.nextInt();
return a;
}
static final String xline() {
String s = sc.nextLine();
if (s.equals("")) s = sc.nextLine();
return sc.nextLine();
}
static final boolean hasx() {
return sc.hasNext();
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (check(a, i) || check(a, n / i)) {
out.println("YES");
return;
}
}
}
out.println("NO");
}
boolean check(int[] a, int size) {
int good = a.length / size;
if (good < 3) return false;
int[] count = new int[size];
for (int i = 0; i < a.length; i++) {
if (a[i] == 1) count[i % size]++;
}
for (int i = 0; i < count.length; i++) {
if (count[i] == good) return true;
}
return false;
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | n = int(input())
a = list(map(int,input().split()))
fl = 0
for i in range(3,n + 1):
if n%i == 0:
for j in range(n//i):
f = 1
for k in range(j,n,n//i):
#print(k)
if a[k] == 0:
f = 0
break
if f:
fl = 1
break
print('YES' if fl else 'NO')
| PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | n = int(input())
a = list(map(int,input().split()))
fl = 0
for i in range(3,n + 1):
if n%i == 0:
for j in range(n//i):
f = 1
for k in range(j,n,n//i):
#print(k)
if a[k] == 0:
f = 0
break
if f:
fl = 1
break
print('YES' if fl else 'NO')
# Made By Mostafa_Khaled | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
long long fix_mod(long long x, long long y) { return (y + x % y) % y; }
void fast() {
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int vis[(int)1e5 + 10];
int main() {
fast();
int n;
cin >> n;
vector<int> v;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
v.push_back(i);
if (n / i != i) v.push_back(n / i);
}
}
vector<int> vx(n);
for (int i = 0; i < n; i++) {
cin >> vx[i];
}
for (auto b : v) {
for (int i = 0; i < n; i++) {
if (vx[i]) vis[i % b]++;
}
for (int i = 0; i < b; i++) {
if (vis[i] == n / b && n / b > 2)
return cout << "YES"
<< "\n",
0;
;
vis[i] = 0;
}
}
return cout << "NO"
<< "\n",
0;
;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author duc
*/
public class CF65_C {
public static void main(String [] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
StringTokenizer tmp = new StringTokenizer(in.readLine(), " ");
int [] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = Integer.parseInt(tmp.nextToken());
}
int [] f = new int[n];
for (int d = 3; d <= n; ++d) {
if (n % d == 0) {
Arrays.fill(f, 0);
int t = n / d;
for (int i = 0; i < n; ++i) {
f[i] = a[i];
if (a[i] == 1 && i >= t) {
f[i] += f[i-t];
}
if (f[i] == d) {
System.out.println("YES");
System.exit(0);
}
}
}
}
System.out.println("NO");
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | def find_divs(n):
divs=[]
for i in range(3,n+1):
if n%i==0:
divs.append(n//i)
return divs
n=int(input())
a=[int(i) for i in input().split()]
divs=find_divs(n)
for div in divs:
for shift in range(0, div):
if(all([i==1 for i in [a[j] for j in range(shift, n, div)]])):
print('YES')
quit()
print('NO') | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int flag = 0;
int n;
cin >> n;
int num[100005];
int vis[100005];
for (int i = 0; i < n; ++i) {
cin >> num[i];
}
int cnt;
for (cnt = 2; cnt < n; cnt++) {
if (n / cnt >= 3 && !(n % cnt)) {
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; ++i) {
if (!num[i]) {
vis[i % cnt]++;
}
}
for (int j = 0; j < cnt; ++j) {
if (!vis[j]) {
flag = 1;
break;
}
}
}
}
int k;
for (k = 0; k < n; ++k) {
if (!num[k]) break;
}
if (k >= n) flag = 1;
if (flag) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
int main() {
int n, i, j, k;
int a[1000000];
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
for (i = 1; i <= n / 3; i++) {
if (n % i == 0)
for (j = 0; j < i; j++) {
k = j;
while (a[k] && k < n) k = k + i;
if (k >= n) {
printf("YES");
return 0;
}
}
}
printf("NO");
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.util.*;
public class RoundTableKnight implements Runnable {
private void solve() throws IOException {
int n = nextInt();
boolean [] b = new boolean[n];
for(int i=0;i<n;i++){
int x = nextInt();
if(x==1)
b[i]=true;
else
b[i]=false;
}
for(int i=3;i<=n;i++){
if(n%i==0)
{
for(int j=0;j<n/i;j++){
int loc = j;
boolean flag = true;
for(int k=0;k<i;k++)
{
loc += n/i;
loc %=n;
if(!b[loc]){
flag = false;
break;
}
}
if(flag){
writer.println("YES");
return;
}
}
}
}
writer.println("NO");
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
@Override
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
tokenizer = null;
solve();
reader.close();
writer.close();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) {
new RoundTableKnight().run();
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.IOException;
import java.util.InputMismatchException;
public class RoundTableKnights {
public static void main(String[] args) {
FasterScanner sc = new FasterScanner();
int N = sc.nextInt();
int[] A = sc.nextIntArray(N);
for (int p = 1; p <= N; p++) {
if (p > 2 && N % p == 0) {
int step = N / p;
for (int s = 0; s < step; s++) {
boolean fail = false;
for (int i = s; i < N; i += step) {
if (A[i] == 0) {
fail = true;
break;
}
}
if (!fail) {
System.out.println("YES");
return;
}
}
}
}
System.out.println("NO");
}
public static class FasterScanner {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class RoundTableKnights {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean x [] = new boolean [n];
for(int i=0;i<n;i++)
x[i]=sc.nextInt()==1;
for(int i=1;i<n;i++) {
if(n/i>=3&&n%i==0) {
outer: for(int u=0;u<n;u++) {
for(int j = 0;j<n/i;j++) {
if(!x[(u+j*i)%n]) {
continue outer;
}
}
System.out.println("YES");
return;
}
}
}
System.out.println("NO");
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public long nextlong() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
long res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v, dev;
bool check(int number) {
int last = -1e9;
for (int i = 1; i <= n; i++) {
if (v[i] == 0 || i < last || (n - number) % number != 0) continue;
int j, q;
for (j = i, q = 0; q <= number; q++) {
int pos = j % n;
if (j < n) last = max(last, j);
if (pos == 0) pos = n;
if (pos == i && q != 0) return true;
if (v[pos] == 0) break;
j += ((n - number) / number) + 1;
}
}
return false;
}
int main() {
bool flg = 1;
cin >> n;
v.resize(n + 1);
for (int i = 1; i <= n; i++) {
cin >> v[i];
if (v[i] == 0) flg = 0;
}
if (flg) {
cout << "YES";
return 0;
}
for (int i = 1; i * i <= n; ++i)
if (n % i == 0) {
dev.push_back(i);
if (n / i != i) dev.push_back(n / i);
}
for (int i = 0; i < dev.size(); i++)
if (dev[i] != n && dev[i] >= 3 && check(dev[i])) {
cout << "YES";
return 0;
}
cout << "NO";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<int> d;
int A[100100];
int main() {
int n, i, j, k, p, temp;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%d", &A[i]);
for (i = 1; i * i <= n; i++)
if (n % i == 0) {
d.push_back(i);
if (i > 2) d.push_back(n / i);
}
sort((d).begin(), (d).end());
for (i = 0; i < d.size(); i++) {
if (n / d[i] > 2) {
for (j = 0; j < d[i]; j++) {
for (k = j; k < n; k += d[i])
if (A[k] == 0) break;
if (k >= n) {
printf("%s\n", "YES\n");
return 0;
};
}
}
}
printf("NO\n");
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int N, A[MAXN];
bool flag[MAXN];
bool check(int n) {
if (n < 3) return false;
int d = N / n;
for (int i = 0; i < d; ++i) flag[i] = true;
for (int i = 1; i <= N; ++i)
if (!A[i]) flag[i % d] = false;
for (int i = 0; i < d; ++i)
if (flag[i]) return true;
return false;
}
int main() {
cin >> N;
for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
for (int i = 1; i * i <= N; ++i)
if (N % i == 0) {
if (check(i)) {
printf("YES\n");
return 0;
}
if (check(N / i)) {
printf("YES\n");
return 0;
}
}
printf("NO\n");
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
auto Check = [&, n](int x) {
for (int i = 0; i < x; ++i) {
bool ok = true;
int cnt = 0;
for (int j = i; j < n; j += x) {
if (a[j] == 0) {
ok = false;
break;
}
++cnt;
if (j + x >= n) {
if ((j + x) % n != i) {
ok = false;
break;
}
}
}
if (ok && cnt > 2) {
return true;
}
}
return false;
};
auto Bye = [](bool b) {
if (b) {
puts("YES");
exit(0);
}
};
for (long long i = 1; i * i <= n; ++i) {
if (n % i == 0) {
Bye(Check(i));
if (i * i != n) Bye(Check(n / i));
}
}
puts("NO");
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.util.*;
public class roundtableknights {
public static void main (String [] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int numKnights = Integer.parseInt(in.readLine());
boolean [] good = new boolean[numKnights], pass = new boolean[numKnights + 1];
StringTokenizer nums = new StringTokenizer(in.readLine());
for (int count = 0; count < numKnights; count ++)
good[count] = Integer.parseInt(nums.nextToken()) == 1;
for (int count = 3; count <= numKnights; count ++) {
if (pass[count])
continue;
int add = numKnights / count;
for (int count2 = count; count2 <= numKnights; count2 += count)
pass[count2] = true;
if (numKnights % count != 0)
continue;
boolean regular = false;
for (int count2 = 0; count2 < add; count2 ++) {
boolean reg2 = true;
for (int count3 = 0, start = count2; count3 < count; count3 ++, start += add, start %= numKnights) {
//System.out.printf("%b %d\n", good[start], start);
if (!good[start]) {
reg2 = false;
break;
}
}
if (reg2) {
regular = true;
break;
}
}
if (regular) {
System.out.println("YES");
System.exit(0);
}
}
System.out.println("NO");
System.exit(0);
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static boolean fortunate(int[] v) {
int n = v.length, iter = 0;
//Desde la posicion 0 hasta la posicion n/3
//puede comenzar el poligono tal que contenga
//minimo 3 vertices
for (int i = 0; i < n / 3; i++)
if (v[i] == 1) //Punto de inicio valido
//Prueba las diferentes longitudes del poligono
for (int j = n/3; j >= 1; j--) {
iter = i;
do
iter = (iter + j) % n; //Avanza de j espacios
while (i != iter && v[iter] == 1);
//Si volvio a llegar a su posicion
//de inicio entonces hay un poligono
if (i == iter)
return true;
}
return false;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = "";
StringBuilder sb = new StringBuilder();
d: do {
line = br.readLine();
if (line == null || line.length() == 0)
break d;
int n = Integer.parseInt(line);
//Recibe los valores
int[] v = retInts(br.readLine());
if (fortunate(v))
sb.append("YES").append("\n");
else
sb.append("NO").append("\n");
} while (line != null && line.length() != 0);
System.out.print(sb);
}
public static int[] retInts(String line) {
String[] w = line.split(" ");
int[] nums = new int[w.length];
for (int i = 0; i < w.length; i++)
nums[i] = Integer.parseInt(w[i]);
return nums;
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353
# from sys import stdin
# input = stdin.readline
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
try:
# sys.setrecursionlimit(int(pow(10,7)))
sys.stdin = open("input.txt", "r")
# sys.stdout = open("../output.txt", "w")
except:
pass
def pmat(A):
for ele in A:
print(*ele,end="\n")
def seive():
prime=[1 for i in range(10**6+1)]
prime[0]=0
prime[1]=0
for i in range(10**6+1):
if(prime[i]):
for j in range(2*i,10**6+1,i):
prime[j]=0
return prime
n=L()[0]
A=L()
div=set()
for i in range(1,int(n**0.5)+2):
if n%i==0:
div.add(i)
div.add(n//i)
div=sorted(list(div))
for ele in div:
if n//ele<3:
continue
Z=[0 for i in range(ele)]
for i in range(n):
Z[i%ele]+=A[i]
if n//ele in Z:
# print(Z)
print("YES")
exit()
print("NO")
endtime = time.time()
# print(f"Runtime of the program is {endtime - starttime}")
| PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | def polyPossible(k, knights):
n = len(knights)
group = n/k
this_group = True
for i in xrange(n/k):
if knights[i] == 0:
continue
this_group = True
for j in xrange(i, n, group):
if knights[j] == 0:
this_group = False
if this_group:
return True
return False
n = int(raw_input())
knights = list(map(int, raw_input().split()))
flag = "NO"
for i in xrange(3, n+1):
if n == (n/i)*i:
if polyPossible(i, knights):
flag = "YES"
break
print flag
| PYTHON |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.*;
import java.io.*;
public class problem71C {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
//Scanner in = new Scanner(new File("71C.in"));
int n = in.nextInt();
int[] knights = new int[n];
for (int i = 0; i < n; i++) knights[i] = in.nextInt();
boolean[] isPrime = new boolean[n + 1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) {
if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
}
if (n > 4) isPrime[4] = true;
isPrime[2] = false;
List<Integer> numSides = new ArrayList<Integer>();
for (int i = 3; i <= n; i++)
if (isPrime[i] && n%i == 0) numSides.add(i);
numSides.add(4);
for (int ns : numSides) {
int s = n/ns;
boolean found = false;
for (int i = 0; i < s; i++) { // start at vertex i
boolean good = true;
for (int j = 0; j < ns; j++) {
if (knights[i+j*s] == 0) { // not good
good = false;
break;
}
}
if (good) {
found = true;
System.out.println("YES");
System.exit(0);
}
}
}
System.out.println("NO");
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int m = n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> v;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (i > 2) v.push_back(i);
if ((n / i) != i && (n / i) > 2) v.push_back(n / i);
}
}
if (v.size() == 0)
cout << "NO";
else {
int f = 1;
for (int i = 0; i < v.size(); i++) {
int temp = v[i];
int x = m / temp;
int c = 0;
for (int start = 0; start < x; start++) {
for (int j = start; j < m; j += x) {
c += a[j];
}
if (c == temp) {
f = 0;
break;
}
c = 0;
}
if (f == 0) break;
}
if (f == 0)
cout << "YES";
else
cout << "NO";
}
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | from sys import stdin
#x = [ '9', '1 0 0 1 1 1 0 0 1' ]
x = stdin.readlines()
lines = map( lambda x: x.strip(), x )
n = int( lines[0] )
a = map( int, lines[1].split( ' ' ) )
def go(n, a) :
for i in range( 1, n / 3 + 1 ) :
if n % i != 0 :
continue
for p in range( 0, i ) :
#print n, i, p, a[p::i], all( a[p::i] )
if all( a[p::i] ) :
return 1
if go(n, a) :
print 'YES'
else :
print 'NO'
| PYTHON |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | from math import sqrt
#def gcd(a,b):
# while b > 0: a,b = b, a%b
# return a
#def lcm(a, b):
# return a*b/gcd(a,b)
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211,
223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461]
N = input()
#S = raw_input()
table = map(int, raw_input().split())
ra = [ i for i in xrange(1, int(sqrt(N))+1) if N%i == 0]
ra += [ N/i for i in ra if N/i > 2 ]
ra = [ i for i in ra if i > 2 and i in primes or i == 4]
if 1 not in table:
print "NO"
exit()
for i in ra:
for j in xrange(0, N/i):
chk = (range(j, N, N/i) + range(0, j, N/i))[:i]
good = True
for p in chk:
if table[p] == 0:
good = False
if good:
print "YES"
exit()
print "NO"
#data2 = [ map(int, raw_input().split()) for i in xrange(T)]
| PYTHON |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int arr[100000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> arr[i];
for (int i = 1; n / i >= 3; ++i) {
if (n % i != 0) continue;
for (int j = 0; j < i; ++j) {
int Ok = 0;
for (int k = j; k < n; k += i) {
if (arr[k] == 1) ++Ok;
}
if (Ok == n / i) {
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.util.*;
public class RoundTableKnights {
public static boolean constructPolygon(int[] a,int gap)
{
for (int i = 0; i < a.length; i++) {
//System.out.println("i: " + i);
boolean possible=true;
int indx=i;
while(true)
{
//System.out.println("indx: " + indx + " a[indx]: " + a[indx]);
if(a[indx]==0){possible=false;break;}
indx=(indx+gap)%a.length;
if(indx==i)break;
}
if(possible)return true;
}
return false;
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for (int i = 0; i < a.length; i++) {
a[i]=Integer.parseInt(st.nextToken());
}
boolean possible=false;
for (int i = 1; i <= Math.sqrt(n); i++) {
int gap1=i;
int sides1=n/i;
//System.out.println(gap1 + " " + sides1);
//System.out.println("option1: gap== " + gap1);
boolean possible1=sides1>2?constructPolygon(a,gap1):false;
int gap2=n/i;
int sides2=i;
//System.out.println("option2: gap== " + gap2);
boolean possible2=sides2>2?constructPolygon(a,gap2):false;
if(possible1||possible2){possible=true;break;}
}
pw.println(possible?"YES":"NO");
pw.close();
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.net.URL;
import java.util.*;
public class Template implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
try {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} catch (Exception e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine(), " :");
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = readInt();
}
return res;
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
<T> List<T>[] createGraphList(int size) {
List<T>[] list = new List[size];
for (int i = 0; i < size; i++) {
list[i] = new ArrayList<>();
}
return list;
}
public static void main(String[] args) {
new Template().run();
// new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
}
long timeBegin, timeEnd;
void time() {
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
long memoryTotal, memoryFree;
void memory() {
memoryFree = Runtime.getRuntime().freeMemory();
System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
+ " KB");
}
public void run() {
try {
timeBegin = System.currentTimeMillis();
memoryTotal = Runtime.getRuntime().freeMemory();
init();
solve();
out.close();
if (System.getProperty("ONLINE_JUDGE") == null) {
time();
memory();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void solve() throws IOException {
int n = readInt();
int[] a = readIntArray(n);
for (int i = 1; i <= n; i++) {
if (n % i == 0 && n / i >= 3) {
for (int start = 0; start < i; start++) {
boolean ok = true;
for (int it = start; it < n; it += i) {
if (a[it] == 0) {
ok = false;
break;
}
}
if (ok) {
out.println("YES");
return;
}
}
}
}
out.println("NO");
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool chmin(int64_t& a, const int64_t& b) { return b < a ? a = b, 1 : 0; }
bool chmax(int64_t& a, const int64_t& b) { return a < b ? a = b, 1 : 0; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
const int N = 1e6 + 1;
int check(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return i;
}
}
return 0;
}
void run_case() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
set<int> second;
int temp = n;
while (temp % 4 == 0) {
temp /= 4;
second.insert(4);
}
for (int i = 3; i * i <= temp; i++) {
if (temp % i == 0) {
while (temp % i == 0) {
temp /= i;
second.insert(i);
}
}
}
if (temp > 2) {
if (temp % 2 == 0) temp /= 2;
second.insert(temp);
}
for (auto x : second) {
temp = n / x;
for (int i = 0; i < temp; i++) {
bool flag = true;
for (int j = i; j < n; j += temp) {
if (a[j] == 0) {
flag = false;
break;
}
}
if (flag) {
cout << "YES\n";
return;
}
}
}
puts("NO");
}
auto clk = clock();
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
run_case();
}
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.util.*;
public class Main {
// static Scanner in;
static PrintWriter out;
static StreamTokenizer in; static int next() throws Exception {in.nextToken(); return (int) in.nval;}
public static void main(String[] args) throws Exception {
// in = new Scanner(System.in);
out = new PrintWriter(System.out);
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
int n = next();
int[] x = new int[n];
for (int i = 0; i < n; i++) x[i] = next();
boolean flag = false;
for (int d = 1; d <= n/3; d++) if (n % d == 0) {
int[] k = new int[d];
for (int i = 0; i < n; i++) k[i % d] += x[i];
for (int i = 0; i < d; i++) flag |= k[i] == n/d;
}
out.println(flag ? "YES" : "NO");
out.close();
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> valid, in(n, 0);
for (int i = 0; i < n; i++) {
int mood;
scanf("%d", &mood);
if (mood) {
valid.push_back(i);
in[i] = true;
}
}
set<int> jps;
for (int i = 1; i * i <= n; i++) {
if (n % i != 0) continue;
{
int nodes = n / i;
if (nodes >= 3) jps.insert(i);
}
{
int nodes = i;
if (nodes >= 3) jps.insert(n / i);
}
}
vector<int> f(n);
for (auto jump : jps) {
f.assign(n, 0);
for (auto st : valid) {
int initial = st;
if (f[st] == 1) continue;
bool valid = true;
while (true) {
f[st] = 1;
st += jump;
st %= n;
if (st == initial) break;
if (f[st] == 1) {
valid = false;
break;
}
if (!in[st]) {
valid = false;
break;
}
}
if (valid) {
puts("YES");
return 0;
}
}
}
puts("NO");
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
import java.util.*;
public class Main {
public void doIt(){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] num = new int[n];
for(int i=0; i < n; i++){
num[i] = sc.nextInt();
}
boolean flg = false;
//考えられる間隔分ループ
for(int i=1; i <= n/3; i++){
//割り切れないならば正多角形ではない
if( n % i ==0){
for(int j=0; j < i ; j++){
boolean goodmoodFlg = true;
for(int k = 0; k < n; k+=i){
if(num[j+k] == 0){
goodmoodFlg = false;
break;
}
}
if(goodmoodFlg){
flg = true;
break;
}
}
if(flg){
break;
}
}
}
if(flg){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
public static void main(String[] args) {
Main obj = new Main();
obj.doIt();
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[100004];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", a + i);
for (int i = 3; i <= n; i++)
if (n % i == 0) {
int d = n / i;
bool yes;
for (int r = 0; r < d; r++) {
yes = true;
for (int j = 0; j < n; j += d)
if (a[r + j] == 0) {
yes = false;
break;
}
if (yes) {
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[100005];
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int sq = sqrt(n);
vector<int> div;
for (int i = 1; i <= sq; i++)
if (n % i == 0) {
if (n / i > 2) div.push_back(i);
if (i > 2) div.push_back(n / i);
}
bool ok = 0;
for (int i = 0; i < (int)div.size() && !ok; i++) {
for (int j = 0; j < div[i]; j++) {
int s = j;
if (!a[s]) continue;
s += div[i];
while (s != j)
if (a[s] != 1)
break;
else
s = (s + div[i]) % n;
if (s == j) {
ok = 1;
break;
}
}
}
if (ok)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | from math import *
n=input()
k=n
a=map(int, raw_input().split())
dis=[]
if n%4==0:
dis.append(4)
n/=4
for i in xrange(2, int(sqrt(n))+1):
while n%i==0:
n/=i
if i!=2:
dis.append(i)
if(n!=1):
dis.append(n)
n=k
flag=0
for i in xrange(len(dis)):
for j in xrange(n/dis[i]):
for h in xrange(dis[i]):
if(a[(j+h*(int(n/dis[i])))%n]==0):
break
if(h==dis[i]-1):
flag=1
if flag==1 and n/dis[0]!=1:
print "YES"
elif n/dis[0]!=1:
print "NO"
else:
flag=1
for i in xrange(n):
if a[i]==0:
flag=0
break
if flag==1:
print "YES"
else:
print "NO" | PYTHON |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.util.*;
public final class round_table_knights
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static FastScanner sc=new FastScanner(br);
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws Exception
{
int n=sc.nextInt();int[] a=new int[n];List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
list.add(i);
if(i!=n/i)
{
list.add(n/i);
}
}
}
Collections.sort(list);
boolean res=false;
outer:
for(int i=list.size()-2;i>=0;i--)
{
int curr=list.get(i);
if(n/curr>=3)
{
for(int j=0;j<=curr-1;j++)
{
int left=j,now=(n-(j+1))/curr,k=j+(curr*now),right=n-1-k;
if(left+right==curr-1)
{
boolean ans=true;
for(int l=j;l<n;l+=curr)
{
if(a[l]!=1)
{
ans=false;
}
}
if(ans)
{
res=true;
break outer;
}
}
}
}
}
out.println(res?"YES":"NO");
out.close();
}
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public String next() throws Exception {
return nextToken().toString();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
try {
if (new File("input.txt").exists())
System.setIn(new FileInputStream("input.txt"));
} catch (SecurityException e) {
}
new Main().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
private void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
boolean[] prime = new boolean[100001];
fill(prime, true);
for (int i = 3; i < 100001; i++)
if (prime[i])
for (int j = 2 * i; j < 100001; j += i)
prime[j] = false;
int n = nextInt();
boolean[] mood = new boolean[n];
for (int i = 0; i < n; i++) {
if (nextInt() == 1)
mood[i] = true;
else
mood[i] = false;
}
boolean yes = false;
for (int i = 3; i <= n; i++)
if (prime[i] && n % i == 0) {
int up = n / i;
for (int j = 0; j < up; j++) {
boolean f = true;
for (int k = j; k < n; k += up)
if (!mood[k]) {
f = false;
break;
}
if (f) {
yes = true;
break;
}
}
}
if (yes)
out.println("YES");
else
out.println("NO");
in.close();
out.close();
}
void chk(boolean b) {
if (b)
return;
System.out.println(new Error().getStackTrace()[1]);
exit(999);
}
void deb(String fmt, Object... args) {
System.out.printf(Locale.US, fmt + "%n", args);
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class C {
private static StreamTokenizer in;
private static PrintWriter out;
private static int nextInt() throws Exception {
in.nextToken();
return (int) in.nval;
}
private static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
static {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(
System.in)));
out = new PrintWriter(System.out);
}
public static void main(String[] args) throws Exception {
int n = nextInt();
boolean[] mas = new boolean[n];
for (int i = 0; i < n; i++) {
mas[i] = (nextInt() == 0) ? false : true;
}
int n2 = n / 2;
boolean ans = true;
for (int i = 1; i <= n / 2; i++) {
if (n % i != 0)
continue;
if(n % 2 == 0 && i >= n / 2)continue;
int step = i;
//if(step >= n / 2)continue;
for (int k = 0; k < step; k++) {
boolean b = true;
for (int j = k; j < n; j += i) {
if (!mas[j])
b = false;
}
if (b) {
out.println("YES");
out.flush();
return;
}
}
}
out.println("NO");
out.flush();
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | from sys import stdin
def main():
n = int(input())
ar = list(map(int, stdin.readline().split()))
index = 1
divs = []
while index * index <= n:
if n % index == 0:
if index > 2:
divs.append(index)
if n // index != index and n // index > 2:
divs.append(n // index)
index += 1
dp = [0] * n
divs.sort()
for i in range(n):
dp[i] = ar[i]
ok = False
max_val = 10 ** 9
for div in divs:
dist = n // div
for i in range(n):
if ar[i] == 0:
dp[i] = -max_val
else:
dp[i] = dp[(i - dist) % n] + 1
if dp[i] == div:
ok = True
break
if ok:
break
for i in range(n):
dp[i] = ar[i]
if ok:
print("YES")
else:
print("NO")
if __name__ == "__main__":
main()
| PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | ###### ### ####### ####### ## # ##### ### #####
# # # # # # # # # # # # # ###
# # # # # # # # # # # # # ###
###### ######### # # # # # # ######### #
###### ######### # # # # # # ######### #
# # # # # # # # # # #### # # #
# # # # # # # ## # # # # #
###### # # ####### ####### # # ##### # # # #
from __future__ import print_function # for PyPy2
# from itertools import permutations
# from functools import cmp_to_key # for adding custom comparator
# from fractions import Fraction
# from collections import *
from sys import stdin
# from bisect import *
# from heapq import *
from math import log2, ceil, sqrt, gcd, log
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
rr = lambda x : reversed(range(x))
mod = int(1e9)+7
inf = float("inf")
n, = gil()
a = gil()
f = []
for k in range(3, n+1):
if n%k == 0:f.append(n//k)
while f:
d = f.pop()
pts = []
i = 0
while i < n:
pts.append(i)
i += d
for _ in range(d):
sm = 0
for i in range(len(pts)):
sm += a[pts[i]]
pts[i] += 1
if sm == len(pts):
print("YES")
exit()
print('NO') | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class RoundTableKnights implements Closeable {
private InputReader in = new InputReader(System.in);
private PrintWriter out = new PrintWriter(System.out);
public void solve() {
n = in.ni();
x = new int[n + 1];
for (int i = 1; i <= n; i++) {
x[i] = in.ni();
}
int half = (n + 1) / 2;
for (int i = 1; i < half; i++) {
if (n % i == 0 && ok(i)) {
out.println("YES");
return;
}
}
out.println("NO");
}
private int n;
private int[] x;
private boolean ok(int k) {
for (int i = 1; i <= k; i++) {
boolean ok = true;
for (int j = i; j <= n; j += k) {
ok &= x[j] == 1;
}
if (ok) {
return true;
}
}
return false;
}
@Override
public void close() throws IOException {
in.close();
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public void close() throws IOException {
reader.close();
}
}
public static void main(String[] args) throws IOException {
try (RoundTableKnights instance = new RoundTableKnights()) {
instance.solve();
}
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | x2=[]
x3=[]
x5=False
x=input("")
x1=raw_input("")
for i in range(0,len(x1),2):
x2.append(bool(int(x1[i])))
x=int(x)
x3.append(x)
for i in range(2,((x/2)+1)):
if x%i == 0 and (x/i) >= 3 :
x3.append(x/i)
x3.reverse()
c1=[True]*len(x3)
for i in range(len(x3)):
for j in range((i+1),len(x3)):
if x3[j]%x3[i]==0:
c1[j]=False
for i in range(len(c1)-1,-1,-1):
if c1[i]==False:
del x3[i]
k=1
for i in range(len(x3)):
k=x/x3[i]
z=x3[i]
for j in range(x/x3[i]):
z11=0
z15=0
while z11<x:
if x2[j]==True:
if x2[j]==x2[j+z11]:
z15+=1
z11+=k
if z15==x3[i]:
x5=True
break
if x5==True:
break
if x5 == True:
print("YES")
else:
print("NO") | PYTHON |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
private void solve() throws IOException {
int n = nextInt();
int[] knight = nextIntArray(n);
boolean res = false;
for (int x = 3; x <= n; x++) {
if (n % x == 0) {
int limit = n/x, sum = 0;
for (int y = 0; y < limit ; y++) {
sum = 0;
for (int z = y; z < n; z += limit) {
if (knight[z] == 1) {
sum++;
}
}
if (sum == x) {
res= true;
break;
}
}
}
}
out.println(res ? "YES" : "NO");
}
public static void main(String[] args) {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
new Main().solve();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(239);
}
}
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
static int[] nextIntArray(int n) throws IOException {
int[] temp = new int[n];
for (int x = 0; x < n; x++) {
temp[x] = nextInt();
}
return temp;
}
static long[] nextLongArray(int n) throws IOException {
long[] temp = new long[n];
for (int x = 0; x < n; x++) {
temp[x] = nextLong();
}
return temp;
}
static String[] nextArray(int n) throws IOException {
String[] temp = new String[n];
for (int x = 0; x < n; x++) {
temp[x] = nextToken();
}
return temp;
}
static <T> void debug(T x) {
System.out.println("debug x: " + x.toString());
}
static void debugInt(int[] x) {
for (int a = 0; a < x.length; a++) {
System.out.println("debug array no: " + a + " value: " + x[a]);
}
}
static void debugArray(String[] x) {
for (int a = 0; a < x.length; a++) {
System.out.println("debug array no: " + a + " value: " + x[a]);
}
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, tmp, s[100005], pl[7], p = 0;
bool ok = false;
void chk(int ver) {
for (int i = 0; i < n / ver; i++) {
int cnt = 0;
for (int j = i; j < n; j += n / ver) cnt += s[j];
if (cnt == ver) {
ok = true;
return;
}
}
return;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &s[i]);
tmp = n;
if (tmp % 4 == 0) chk(4);
while (tmp % 2 == 0) tmp /= 2;
for (int i = 3; i * i <= tmp; i++) {
if (tmp % i == 0) {
while (tmp % i == 0) tmp /= i;
chk(i);
}
}
if (tmp > 1) chk(tmp);
if (ok)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool read(T &x) {
int c = getchar();
int sgn = 1;
while (~c && c<'0' | c> '9') {
if (c == '-') sgn = -1;
c = getchar();
}
for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= sgn;
return ~c;
}
struct debugger {
template <typename T>
debugger &operator,(const T &v) {
cerr << v << " ";
return *this;
}
} dbg;
template <class T>
void __stl_print__(T &x) {
cerr << "[";
for (__typeof((x).end()) i = (x).begin(); i != (x).end(); ++i)
cerr << (i != x.begin() ? ", " : "") << *i;
cerr << "]" << endl;
}
template <class T, class U>
inline T max(T &a, U &b) {
return a > b ? a : b;
}
template <class T, class U>
inline T min(T &a, U &b) {
return a < b ? a : b;
}
template <class T, class U>
inline T swap(T &a, U &b) {
T tmp = a;
a = b;
b = tmp;
}
const long long INF = (1ll) << 50;
const int mx = 1e5 + 7;
const int mod = 1000000007;
const double pi = 2 * acos(0.0);
int EQ(double d) {
if (fabs(d) < 1e-7) return 0;
return d > 1e-7 ? 1 : -1;
}
int n;
bool arr[mx];
void check(int div) {
for (__typeof((div)-1) i = (0); i <= ((div)-1); ++i) {
bool f = 1;
for (int j = 0; j < n; j += div)
if (!arr[(i + j) % n]) {
f = 0;
break;
}
if (f) {
cout << "YES" << endl;
exit(0);
}
}
}
int main() {
n = ({
int a;
read(a);
a;
});
for (__typeof((n)-1) i = (0); i <= ((n)-1); ++i)
arr[i] = ({
int a;
read(a);
a;
});
int sz = sqrt(n);
for (__typeof(sz) i = (1); i <= (sz); ++i) {
if (n % i == 0) {
int a = i, b = n / i;
if ((n / a) >= 3) check(a);
if ((n / b) >= 3) check(b);
}
}
cout << "NO" << endl;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1e9 + 1;
constexpr long long LLINF = 1e18 + 1;
template <typename T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
const int nax = 1e5 + 10;
int ar[nax], n;
bool poss(int a) {
vector<int> first(a, 1);
;
for (int i = 0; i < n; i++) first[i % a] &= ar[i];
for (int i = 0; i < a; i++)
if (first[i]) return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> ar[i];
for (int i = 1; i <= n; i++)
if (n % i == 0 && n / i > 2 && poss(i)) {
printf("YES\n");
return 0;
}
printf("NO\n");
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jtimv
*/
import java.io.*;
public class Task {
public static void main(String[] args) {
new Task().main();
}
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
int nextInt() {
try {
in.nextToken();
} catch (IOException ex) {
}
return (int) in.nval;
}
boolean checkThisOne(int[] a, int start, int step) {
int vertexes=0;
while (start < a.length) {
if (a[start] == 0) {
return false;
}
start += step;
vertexes++;
}
return (vertexes >= 3);
}
boolean check(int[] a, int step) {
for (int start = 0; start < step; start++) {
if (checkThisOne(a, start, step)) {
//System.out.println(start+" "+step);
return true;
}
}
return false;
}
void main() {
int n = nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
for (int k = 1; k <= n; k++) {
if ((n%k==0)&&check(a, k)) {
System.out.println("YES");
return;
}
}
System.out.println("NO");
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | n=int(input())
b=list(map(int,input().split()))
p=int(n**0.5)+1
j=1
r=n
c=[]
while(j<p):
if n%j==0:
k=j
r=n//j
if k>=3:
c.append(k)
if r>=3 and r!=k:
c.append(r)
j+=1
c=sorted(c)
f=0
for r in c:
k = n // r - 1
dp = [1] * (k + 1)
j = 0
p = 0
while (j < n):
p = (j // (k + 1))
if b[j] == 0:
dp[j - p * (k + 1)] = 0
j += 1
for j in range(k + 1):
if dp[j] == 1:
f = 1
break
if f == 1:
break
if f==1:
print("YES")
else:
print("NO")
| PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class RoundTableKnights {
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
int n = Integer.parseInt(reader.readLine());
String st[] = reader.readLine().split(" ");
boolean b[] = new boolean[n];
for (int i = 0; i < st.length; i++)
b[i] = (st[i].charAt(0) == '1');
ArrayList<Integer> list = new ArrayList<Integer>();
int sqrt = (int) Math.sqrt(n);
for (int i = 3; i <= sqrt; i++) {
if ((n % i) == 0 && (n / i) > 2) {
list.add(i);
list.add(n / i);
}
}
// add with window 1
// add with window 2
if ((n & 1) == 0) {
if ((n / 2) > 2)
list.add(2);
}
list.add(1);
// System.out.println(list);
// System.out.println(Arrays.toString(b));
for (Integer w : list) {
for (int i = 0; i < w; i++) {
boolean found = true;
for (int j = i; j < n; j += w) {
// System.out.println(j);
if (!b[j]) {
found = false;
break;
}
}
if (found) {
// System.out.println(w+" "+i);
System.out.println("YES");
return;
}
}
}
System.out.println("NO");
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int seive[N];
void pre() {
fill(seive + 2, seive + N, 1);
for (int i = 2; i < N; i++) {
if (seive[i]) {
for (int j = 2; j * i <= N; j++) {
seive[i * j] = 0;
}
}
}
}
int n, a[N];
void check(int i) {
if (n / i < 3) return;
for (int j = 0; j < i; j++) {
bool q = 1;
for (int k = j; k < n; k += i) {
q &= a[k];
}
if (q) {
cout << "YES\n";
exit(0);
}
}
}
int32_t main() {
cin >> n;
for (__typeof(n) i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
check(i);
check(n / i);
}
}
cout << "NO";
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int bsearch(int l, int h, int a[], int key) {
int m = (l + h) / 2;
int ans = -1;
while (l <= h) {
if (a[m] == key) {
ans = m;
break;
} else if (a[m] < key)
l = m + 1;
else
h = m - 1;
}
return ans;
}
long long bin(long long a, long long b, long long m) {
a = a % m;
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % m;
a = a * a % m;
b = b >> 1;
}
return res;
}
int ncr(int n, int r) {
long long res = 1;
for (int i = 0; i < r; i++) {
res = res * (n - i);
res = res / (i + 1);
}
return res;
}
int find(int i, vector<int> p) {
while (p[i] != i) i = p[i];
return i;
}
void join(int u, int v, vector<int>& p) {
int x = find(u, p);
int y = find(v, p);
p[x] = p[y];
}
int check(int m, int a[], int n) {
for (int i = 0; i < m; i++) {
bool flag = 1;
for (int k = i; k < n; k += m) {
if (a[k] == 0) {
flag = 0;
}
}
if (flag) return 1;
}
return 0;
}
int32_t main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i <= n / 3; i++) {
if ((n % i == 0) && (n / i >= 3) && check(i, a, n)) {
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long M = 1e6 + 5;
const long long mod = 1e9 + 7;
long long a[N], cnt;
vector<long long> prime;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
long long m = n;
for (long long i = 0; i < n; ++i) cin >> a[i];
while ((m & 1) == 0) m >>= 1, cnt++;
if (cnt >= 2) prime.push_back(4);
for (long long i = 3; i * i <= n; ++i) {
if (m % i == 0) prime.push_back(i);
while (m % i == 0) m = m / i;
}
if (m > 1) prime.push_back(m);
for (long long i : prime) {
long long d = n / i;
for (long long j = 0; j < d; ++j) {
long long s = 0;
for (long long k = j; k < n; k += d) s += a[k];
if (s == i) return cout << "YES", 0;
}
}
cout << "NO";
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n;
int s[MAXN];
bool check(int k) {
for (int i = 0; i < k; i++) {
bool flag = true;
for (int j = i; j < n; j += k) {
if (!s[j]) {
flag = false;
break;
}
}
if (flag) return true;
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (n / i >= 3 && check(i)) {
cout << "YES" << endl;
return 0;
}
if (i * i != n && i >= 3 && check(n / i)) {
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e9;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
long long n, z, cnt, ans;
vector<long long> divisors;
vector<int> a;
vector<long long> generate_divisors(long long n) {
vector<long long> v;
long long i;
for (i = 1; i * i < n; ++i)
if (n % i == 0) v.push_back(i), v.push_back(n / i);
if (i * i == n) v.push_back(i);
return v;
}
bool check(long long x) {
for (int y = 0; y < x; y++) {
cnt = 0;
for (int j = y; j < n; j += x) {
if (a[j] == 1) {
cnt++;
} else {
break;
}
}
if ((cnt == (n / x)) && (cnt >= 3)) {
return 1;
}
}
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
divisors = generate_divisors(n);
for (int i = 0; i < ((long long)((divisors).size())); i++) {
if (check(divisors[i])) {
ans = 1;
break;
}
}
if (ans) {
cout << "YES"
<< "\n";
} else {
cout << "NO"
<< "\n";
}
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = Utils.readIntArray(in, n);
for (int s = 1; s <= n / 3; ++s) {
if (n % s != 0) continue;
int[] v = new int[s];
for (int i = 0; i < s; ++i) v[i] = 1;
for (int i = 0; i < n; ++i) v[i%s] &= a[i];
for (int i = 0; i < s; ++i) if (v[i] == 1) {
out.println("YES");
return;
}
}
out.println("NO");
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine().trim();
} catch (IOException e) {
return null;
}
}
public String nextString() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
}
class Utils {
public static int[] readIntArray(InputReader in, int n) {
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
}
return a;
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n;
bool mark[100000];
int good(int x) {
for (int i = 0; i < x; i++) {
for (int j = i; j <= n - x + i; j += x) {
if (mark[j] == 0) break;
if (j == n - x + i) return 1;
}
}
return 0;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> mark[i];
for (int m = 1; m <= n / 3; m++) {
if (n % m == 0) {
if (good(m) == 1) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.io.*;
import java.util.*;
public class Task7c {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
for (int i = 3; i <= n; i++) {
if (n % i == 0) {
for (int j = 0; j < n; j++) {
int cnt = 0;
for (int k = j; k < j + n; k += n / i) {
int tmp = k;
if (k >= n) {
tmp -= n;
}
if (arr[tmp] == 1) {
cnt++;
} else {
break;
}
}
if (cnt == i) {
System.out.println("YES");
return;
}
}
}
}
System.out.println("NO");
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 |
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class SpecialistC5 {
static PrintWriter out;
static Scanner sc;
private static boolean[] is_composite;
private static ArrayList<Integer> primes;
public static void main(String[]args) throws IOException {
sc=new Scanner(System.in);
out=new PrintWriter(System.out);
//A();
//B();
//C();
//D();
//E();
//F();
//G();
//H();
//I();
//J();
//K();
//L();
//N();
O();
//U();
out.close();
}
private static void A() throws IOException {
int t=ni();
while(t-->0) {
long a=nl();
long b=nl();
if(b%a!=0)ol(-1);
else {
// long mn=Math.min(a, b/a);
// long mx=Math.max(a, b/a);
ol(a+" "+b);
}
}
}
static void B() throws IOException {
int t=3;
while(sc.ready()) {
int step=ni(),mod=ni();
HashSet<Integer>hs=new HashSet<Integer>();
for(int in=0;!hs.contains(in);in=(in+step)%mod) {
hs.add(in);
}
String a1=""+step,a2=""+mod;
while(a1.length()<10)a1=" "+a1;
while(a2.length()<10)a2=" "+a2;
out.print(a1+a2+" ");
if(hs.size()==mod) {
out.print("Good Choice\n");
}else {
out.print("Bad Choice\n");
}
out.println();
}
}
static int gcd (int a, int b) {
return b==0?a:gcd (b, a % b);
}
static long lcm(int a,int b) {
return (a / gcd(a,b))*b*1l;
}
static void C() throws IOException{
int n,c=1;
while((n=ni())!=-1) {
String ss=ns();
String s=ss.substring(2);
int num=Integer.parseInt(s)-(n==s.length()?0:Integer.parseInt(s.substring(0,s.length()-n)));
int den=(int) (Math.pow(10,s.length())-Math.pow(10,s.length()-n));
if(n!=0) {
int g=gcd(num,den);
num/=g;
den/=g;
}else {
// 2/5
int[]pow=new int[10];
pow[0]=1;
for(int i=1;i<pow.length;i++)pow[i]=10*pow[i-1];
den=pow[s.length()];
for(int i=s.length()-1,cur=1;i>=0;i--,cur*=10) {
num+=(s.charAt(i)-'0')*cur;
}
int g=gcd(num,den);
num/=g;
den/=g;
}
ol("Case "+c+++": "+num+"/"+den);
}
}
static void D() throws IOException {
long n;
while((n=ni())!=0) {
ArrayList<Integer>div=new ArrayList<Integer>();
int ans=0;
for(int i=1;i*i*1l<=n;i++) {
if(n%i==0) {
div.add(i);
if(i*i!=n) {
div.add((int) (n/i));
}
}
}
Collections.sort(div);
for(int i=0;i<div.size();i++) {
for(int j=i;j<div.size();j++) {
long num=lcm(div.get(i),div.get(j));
if(num==n) {
ans++;
}
}
}
out.println(n+" "+ans);
}
}
static void E() throws IOException {
int n;
sieve((int)(Math.ceil(Math.sqrt((int)2e9))));
while((n=ni())!=0) {
int ans=gt(n);
if(ans==-1)ans=1;
ol(ans);
}
}
private static int gt(int N) {
int ans = -1, idx = 0, p = primes.get(0);
while(p * p <= N&&ans!=1)
{
int e = 0;
while(N % p == 0) { N /= p; ++e; }
if(ans==-1) {if(e>0)ans=e;}
else ans = gcd(ans,e);
//if(idx<primes.size()-1)
p = primes.get(++idx);
//else p++;
}
if(N != 1)
ans = 1;
return ans;
}
static void sieve(int N)
{
primes = new ArrayList<Integer>();
boolean[] isComposite = new boolean[N];
for(int i = 2; i < N; ++i)
if(!isComposite[i])
{
primes.add(i);
for(int j = i * i; j < N; j += i)
isComposite[j] = true;
}
}
static int numDiv(int N)
{
int ans = 1, idx = 0, p = primes.get(0);
while(p * p <= N)
{
int e = 0;
while(N % p == 0) { N /= p; ++e; }
ans *= (e + 1);
p = primes.get(++idx);
}
if(N != 1)
ans <<= 1;
return ans;
}
static void F() throws IOException {
sieve(10000);
int bound=(int)1e6;
ArrayList<Integer>seq=new ArrayList<Integer>();
seq.add(1);
int[]occ=new int[bound+1];
occ[1]=1;
while(true) {
int cur=seq.get(seq.size()-1);
int div=numDiv(cur);
if(cur+div>bound)break;
//if(seq.size()<=10000)
//out.println(cur+div);
occ[cur+div]=1;
seq.add(cur+div);
}
for(int i=1;i<=bound;i++) {
occ[i]+=occ[i-1];
}
int t=ni();
int c=1;
while(t-->0) {
int a=ni(),b=ni();
ol("Case "+c+++": "+(occ[b]-occ[a-1]));
}
}
static int[] gcdExtended(int p, int q) {
if (q == 0)
return new int[] { p, 1, 0 };
int[] vals = gcdExtended(q, p % q);
int d = vals[0];
int a = vals[2];
int b = vals[1] - (p / q) * vals[2];
return new int[] { d, a, b };
}
static void G() throws IOException {
int t=ni();
while(t-->0) {
int x=ni(),k=ni();
int a=(int)Math.floor(x/(double)k);
int b=(int)Math.ceil(x/(double)k);
int[]v=gcdExtended(a, b);
int g=gcd(a,b);
long v1=v[1]*x*1l/g;
long v2=v[2]*x*1l/g;
// if(v1<0)v1=-v1;
// if(v2<0)v2=-v2;
ol(v1+" "+v2);
}
}
static void H() throws IOException {
int n;
while((n=sc.nextInt())!=0) {
int c1=sc.nextInt(),n1=sc.nextInt(),c2=sc.nextInt()
,n2=sc.nextInt();
boolean swapped = false;
if(c1 < c2)
{
swapped = true;
c1 ^= c2; c2 ^= c1; c1 ^= c2;
n1 ^= n2; n2 ^= n1; n1 ^= n2;
}
int[]p=gcdExtended(n1,n2);
//out.println(Arrays.toString(p));
if(n%p[0]!=0) {
out.println("failed");
}else {
long g=p[0],a=p[1],b=p[2];
a*=(n/g);b*=(n/g);
long x1=(int)Math.ceil(-a/(double)(n2/g));
long x2=(int)Math.floor(b/(double)(n1/g));
if(x1>x2)out.println("failed");
else {
long a1=a+x1*(n2/g),b1=b-x1*(n1/g);
long a2=a+x2*(n2/g),b2=b-x2*(n1/g);
if(a2*c1+b2*c2<a1*c1+b1*c2) {
a1=a2;
b1=b2;
}
if(swapped) {
a1^=b1;b1^=a1;a1^=b1;
}
out.println(a1+" "+b1);
}
}
}
}
static void I() throws IOException {
int t=2;
while(t-->0) {
int a=ni(),b=ni();
int[]p=gcdExtended(a, b);
int x=p[1],y=p[2];
//out.println(p[0]+" "+p[1]+" "+p[2]);
}
}
static void J() throws IOException {
}
static void K() throws IOException {
}
static void L() throws IOException {
}
static void N() throws IOException {
int n=ni();
int[]a=nai(n);
int[]cnt=new int[3];
for(int i=0;i<n;i++) {
int cur=a[i];
int ans=0;
while(cur>0) {
ans+=cur%10;cur/=10;
}
a[i]=ans%3;
cnt[a[i]]++;
}
out.println(cnt[0]/2 + Math.min(cnt[1], cnt[2]));
}
static void O() throws IOException {
int n=ni();
int[]a=nai(n);
ArrayList<Integer>div=new ArrayList<Integer>();
div.add(1);div.add(n);
for(int i=2;i*i<=n;i++) {
if(n%i==0) {
div.add(i);
if(i*i!=n)div.add(n/i);
}
}
boolean f=false;
for(int dv:div) {
if(n/dv<3)continue;
for(int i=0;i<dv&&!f;i++) {
boolean can=true;
int cnt=0;
for(int j=i;j<n;j+=dv) {
if(a[j]==0)can=false;
//cnt++;
}
if(can)f=true;
}
if(f)break;
}
out.println(f?"YES":"NO");
}
static void U() throws IOException {
int t=ni();
while(t-->0) {
int l=ni(),u=ni();
HashMap<Integer,Integer>hm=new HashMap<Integer, Integer>();
int mx=1,num=l;
for(int i=1;i*i<=u;i++) {
for(int j=u-u%i;j>=l;j-=i) {
if(i*i<=j) {
int cnt=1;
if(i != j/i)cnt++;
int v=hm.getOrDefault(j, 0)+cnt;
hm.put(j,v);
if(v>mx) {
mx=v;num=j;
}
}
}
}
ol("Between "+l+" and "+u+", "+num+" has a maximum of "+mx+" divisors.");
}
}
static int ni() throws IOException {
return sc.nextInt();
}
static double nd() throws IOException {
return sc.nextDouble();
}
static long nl() throws IOException {
return sc.nextLong();
}
static String ns() throws IOException {
return sc.next();
}
static int[] nai(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
return a;
}
static long[] nal(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextLong();
return a;
}
static int[][] nmi(int n,int m) throws IOException{
int[][]a=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=sc.nextInt();
}
}
return a;
}
static long[][] nml(int n,int m) throws IOException{
long[][]a=new long[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=sc.nextLong();
}
}
return a;
}
static void o(String x) {
out.print(x);
}
static void ol(String x) {
out.println(x);
}
static void ol(int x) {
out.println(x);
}
static void disp1(int []a) {
for(int i=0;i<a.length;i++) {
out.print(a[i]+" ");
}
out.println();
}
static class Pair implements Comparable<Pair>{
int x,y;
Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair p) {
return x-p.x==0?y-p.y:x-p.x;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair key = (Pair) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public boolean hasNext() {return st.hasMoreTokens();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int T = 1;
while (T--) {
long long int n, i, t, j, k;
cin >> n;
long long int N = n;
bool mood[n + 5];
bool chh = true;
for (i = 0; i < n; i++) {
cin >> mood[i];
chh = chh && mood[i];
}
if (chh) {
cout << "YES";
return 0;
}
vector<long long int> ans;
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
long long int ttt = n / i;
ans.emplace_back(i);
if (i != ttt) ans.emplace_back(ttt);
}
}
n = N;
long long int skip;
skip = 1;
sort((ans).begin(), (ans).end());
for (i = ans.size() - 1; i >= 0; i--) {
if (n / ans[i] >= 3) {
skip = ans[i];
break;
}
}
if (skip == 1) {
cout << "NO";
return 0;
}
for (k = 0; k < ans.size(); k++) {
skip = ans[k];
if (n / skip < 3) continue;
for (i = 0; i < skip; i++) {
bool hoga = true;
for (j = i; j < n; j += (skip)) {
if (mood[j] == false) {
hoga = false;
break;
}
}
if (hoga == true) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
}
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.*;
import java.io.*;
import java.math.*;
public class C
{
public static void process(int test_number)throws IOException
{
int n = ni(), mood[] = new int[n+1];
for(int i = 1; i <= n; i++)
mood[i] = ni();
ArrayList<Integer> div = new ArrayList<>();
for(int i = 2; i <= Math.sqrt(n); i++){
if(n % i != 0 || n / i <= 2)
continue;
int x1 = i, x2 = n/i;
div.add(x1);
if(x2 != x1 && n / x2 > 2)
div.add(x2);
}
div.add(1);
Collections.sort(div);
for(Integer side : div){
boolean flag;
for(int start = 1; start <= side; start++){
flag = true;
for(int j = 1, curr = start; j <= n / start && flag; j++,
curr = (curr + side > n) ? (curr + side) % n : curr + side){
flag = flag && (mood[curr] == 1);
//trace(curr);
}
if(flag){
pn("YES");
return ;
}
}
}
pn("NO");
}
static final long mod = (long)1e9+7l;
static FastReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc = new FastReader();
long s = System.currentTimeMillis();
int t = 1;
//t = ni();
for(int i = 1; i <= t; i++)
process(i);
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
}
static void trace(Object... o){ System.err.println(Arrays.deepToString(o)); };
static void pn(Object o){ out.println(o); }
static void p(Object o){ out.print(o); }
static int ni()throws IOException{ return Integer.parseInt(sc.next()); }
static long nl()throws IOException{ return Long.parseLong(sc.next()); }
static double nd()throws IOException{ return Double.parseDouble(sc.next()); }
static String nln()throws IOException{ return sc.nextLine(); }
static long gcd(long a, long b)throws IOException{ return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{ return (b==0)?a:gcd(b,a%b); }
static int bit(long n)throws IOException{ return (n==0)?0:(1+bit(n&(n-1))); }
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); }
}
return st.nextToken();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
}
| JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool chmin(int64_t& a, const int64_t& b) { return b < a ? a = b, 1 : 0; }
bool chmax(int64_t& a, const int64_t& b) { return a < b ? a = b, 1 : 0; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
const int N = 1e6 + 1;
int check(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return i;
}
}
return 0;
}
void run_case() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
set<int> second;
int temp = n;
while (temp % 4 == 0) {
temp /= 4;
second.insert(4);
}
for (int i = 3; i * i <= temp; i++) {
if (temp % i == 0) {
while (temp % i == 0) {
temp /= i;
second.insert(i);
}
}
}
if (temp > 2) {
if (temp % 2 == 0) temp /= 2;
second.insert(temp);
}
for (auto x : second) {
temp = n / x;
for (int i = 0; i < temp; i++) {
bool flag = true;
int st = (i + temp) % n;
int sides = 1;
while (st != i) {
if (a[st] == 0) {
flag = false;
break;
}
st = (st + temp) % n;
sides++;
}
if (flag && sides > 2 && a[i] == 1) {
cout << "YES\n";
return;
}
}
}
puts("NO");
}
auto clk = clock();
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
run_case();
}
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int k = 1; k <= n / 3; k++) {
if (n % k == 0) {
for (int p = 0; p < k; p++) {
int sum = 0;
for (int v = p; v < n; v += k) {
if (a[v] == 1) sum++;
}
if (sum == n / k) {
cout << "YES\n";
return 0;
}
}
}
}
cout << "NO\n";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846;
long long MOD = 998244353;
const char nl = '\n';
const long long inf = 1e15;
long long power(long long x, long long y) {
long long z = 1;
while (y > 0) {
if (y % 2) z = z * x;
x = x * x;
y /= 2;
}
return z;
}
long long gcd(long long a, long long b) {
if (a < b) return gcd(b, a);
if (b == 0) return a;
return gcd(b, a % b);
}
long long sq(long long a) {
long long ans = (1ll * a * a);
return ans;
}
bool isprime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long digitsum(long long x) {
long long ans = 0;
while (x > 0) {
ans += (x % 10);
x /= 10;
}
return ans;
}
set<int> Divisors(int n) {
set<int> s;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i) {
s.insert(i);
} else {
s.insert(i);
s.insert(n / i);
}
}
}
return s;
}
int n;
int a[100001];
bool solve(int k) {
if (n / k < 3) return false;
for (int i = 0; i < k; ++i) {
bool ok = true;
for (int j = i; j < n; j += k) {
ok &= a[j];
}
if (ok) return true;
}
return false;
}
void solve() {
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0 && (solve(i) || solve(n / i))) {
cout << "YES\n";
return;
}
}
cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long TC = 1;
while (TC--) {
solve();
}
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[(int)1e5 + 1];
int main() {
int n;
scanf("%d", &n);
bool ok = true;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
ok = min((a[i] == 1), ok);
}
if (ok) return printf("YES"), 0;
vector<int> div;
for (int i = 2; i * i <= n; i++) {
if (!(n % i)) {
if (i * i != n) div.emplace_back(i);
div.emplace_back(n / i);
}
}
for (auto i : div) {
if (i <= 2) continue;
for (int k = 0; k < n; k++) {
if (a[k]) {
int j = k, c = 0, dif = n / i;
while (j < n) {
if (!a[j]) break;
c += a[j];
j += dif % n;
if (c == i) return printf("YES"), 0;
}
}
}
}
printf("NO");
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, a[100020], z;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i <= n / 3; i++)
if (n % i == 0)
for (int j = 0; j < i; j++) {
z = 1;
for (int k = j; k < n; k += i) z &= a[k];
if (z) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | def put(): return map(int, input().split())
n = int(input())
l = list(put())
for i in range(1,n):
if n%i==0 and n//i >=3:
for j in range(i):
a = 1
for k in range(j, n, i):
a&= l[k]
if a==1:
#print(j,i)
print('YES')
exit()
print('NO')
| PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import re
n = int(raw_input())
a = map(int, raw_input().split())
d = []
for i in xrange(1, n):
if i * i > n: break
if n % i == 0:
d.append(i)
if (i != n / i):
d.append(n / i)
flag = True
for i in xrange(len(d)):
if n / d[i] >=3:
for k in xrange(0, d[i]):
flag = True
for j in xrange(k, n, d[i]):
if a[j] == 0:
flag = False
break
if flag:
break
if flag:
break
if flag:
print 'YES'
else:
print 'NO' | PYTHON |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | from math import sqrt
def eratosthenes(n):
A = [True]*(n+1)
for i in range(2, int(sqrt(n)) + 1):
if A[i]:
for j in range(i**2, n+1, i):
A[j] = False
return [i for i in range(2, len(A)) if A[i]]
def knights(n, Table):
P = eratosthenes(n)
P[0] = 4
for k in P:
if n % k == 0:
q = n // k
for p in range(q):
if 0 not in Table[p : (n-1) : q]:
print('YES')
return
print('NO')
n = int(input())
Table = [int(i) for i in input().split()]
knights(n, Table) | PYTHON3 |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int k;
vector<int> v;
cin >> n;
int nn = n;
while (nn--) {
cin >> k;
v.push_back(k);
}
for (int x = 1; x * 3 <= n; ++x) {
if (n % x != 0) {
continue;
}
for (int y = 0; y < x; ++y) {
int z;
for (z = y; z < n; z += x) {
if (v[z] == 0) {
break;
}
}
if (z >= n) {
cout << "YES" << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | import java.util.ArrayList;
import java.util.Scanner;
public class Codeforces71C {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean valid = true;
ArrayList<Boolean> list = new ArrayList<Boolean>();
for (int i = 0 ; i < n ; i++){
list.add(sc.nextInt() == 1 ? true:false);
}
sc.close();
for(int i = 3 ; i <= n; i++){
if( n % i == 0){
int k = n/i;
for(int z = 0; z < k ;z++){
valid = true;
for (int j = 0; j < i; j++){
if(list.get( z + j * k) == false){
valid = false;
}
}
if(valid){
System.out.println("YES");
return;
}
}
}
}
System.out.println("NO");
return;
}
} | JAVA |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | #include <bits/stdc++.h>
long long MM = 1000000007;
using namespace std;
long long gcd(long long a, long long b) {
if (a == 0 || b == 0) return abs(a - b);
long long res = a % b;
while (res) {
a = b;
b = res;
res = a % b;
}
return b;
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
long long numberofdiviser(long long a) {
long long ans = 0;
long long i;
for (i = 1; i * i < a; i++) {
if (a % i == 0) ans += 2;
}
if (i * i == a) ans++;
return ans;
}
void fival(long long* a, long long b, long long n) {
for (int i = 0; i < n; i++) {
a[i] = b;
}
}
long long sum, ans, l, r, x;
long long ss(vector<long long> a, vector<long long>& b, int ii) {
for (int i = ii; i < a.size(); i++) {
b.push_back(a[i]);
sum += a[i];
if (b.size() > 1 && sum >= l && sum <= r && b[b.size() - 1] - b[0] >= x)
ans++;
ss(a, b, i + 1);
sum -= b[b.size() - 1];
b.pop_back();
}
return ans;
}
unsigned long long fact(long long n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
unsigned long long Combinations(unsigned long long n, unsigned long long r) {
if (r > n) return 0;
if (r * 2 > n) r = n - r;
if (r == 0) return 1;
unsigned long long res = n;
for (int i = 2; i <= r; ++i) {
res *= (n - i + 1);
res /= i;
}
return res;
}
long long pow(long long a, long long b, long long m) {
if (b == 0) return 1;
if (b == 1) return a;
long long x = pow(a, b / 2, m);
x = (x * x) % m;
if (b % 2) x = (x * a);
return x % m;
}
int solve() {
int n;
cin >> n;
vector<long long> v;
long long x;
for (int i = 0; i < n; i++) {
cin >> x;
v.emplace_back(x);
}
vector<long long> div;
div.emplace_back(1);
if (n % 2 == 0 && n > 4) div.emplace_back(2);
for (int i = 3; i * i <= n; i++) {
if (n % i == 0) {
div.emplace_back(i);
div.emplace_back(n / i);
}
}
for (int i = 0; i < div.size(); i++) {
for (int j = 0; j < div[i]; j++) {
int k;
for (k = j; k < n; k += div[i]) {
if (v[k] == 0) break;
}
if (k >= n) return cout << "YES", 0;
}
}
cout << "NO";
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
}
| CPP |
71_C. Round Table Knights | 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 vertices, 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.
<image>
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 ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "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 | 2 | 9 | N=int(input())
A=input().split()
if(A.count('1')<=2):
print("NO")
else:
done=False
diff=0
while(not done):
diff+=1
if(N//diff<=2):
break
if(N%(diff)!=0):
continue
for start in range(diff):
done=True
for j in range(start,N,diff):
if(A[j]=='0'):
done=False
break
if(done):
break
if(done):
print("YES")
else:
print("NO")
| PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.