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 |
---|---|---|---|---|---|
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | import sys
import math
#to read string
get_string = lambda: sys.stdin.readline().strip()
#to read list of integers
get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) )
#to read integers
get_int = lambda: int(sys.stdin.readline())
sys.setrecursionlimit(10**5)
#--------------------------------WhiteHat010--------------------------------------#
n = get_int()
s = get_string()
lst = get_int_list()
flag = False
m = float('inf')
for i in range(n-1):
if s[i] == 'R' and s[i+1] == 'L':
flag = True
m = min(m, (lst[i+1] - lst[i])//2 )
if flag:
print(m)
else:
print(-1) | PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n = int(input())
arr = [[0, ''] for i in range(n)]
a = input()
b = [int(i) for i in input().split()]
for i in range(len(a)):
arr[i][1] = a[i]
for i in range(len(b)):
arr[i][0] = b[i]
arr = sorted(arr)
res = 2e9
for i in range(n-1):
if arr[i][1] == 'R' and arr[i+1][1] == 'L':
res = min(res, (arr[i+1][0] - arr[i][0]) // 2)
print(-1 if res == 2e9 else res)
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n = int(input())
s = str(input())
index = list(map(int , input().split()))
i =0
ans = 9999999999999
while i<len(s)-1:
if s[i]=='R' and s[i+1]=='L':
ans = min( ans , (( index[i+1] - index[i] )//2 ) )
i = i+1
if ans==9999999999999:
print(-1)
else:
print(ans) | PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | /*
* Code Author: Akshay Miterani
* DA-IICT
*/
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
public class A {
static double eps=(double)1e-15;
static long mod=(int)1e9+7;
public static void main(String args[]){
InputReader in = new InputReader(System.in);
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
//----------My Code----------
int n=in.nextInt();
String s=in.nextLine();
int x[]=new int[n];
for(int i=0;i<n;i++){
x[i]=in.nextInt();
}
int ans=Integer.MAX_VALUE;
for(int i=0;i<n-1;i++){
if(s.charAt(i)=='R' && s.charAt(i+1)=='L'){
ans=Math.min(ans, (x[i+1]-x[i])/2);
}
}
System.out.println(ans!=Integer.MAX_VALUE?ans:-1);
out.close();
//---------------The End------------------
}
static class Pair implements Comparable<Pair> {
int u;
int v;
public Pair(int u, int v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static long modulo(long a,long b,long c) {
long x=1;
long y=a;
while(b > 0){
if(b%2 == 1){
x=(x*y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return x%c;
}
static long gcd(long x, long y)
{
if(x==0)
return y;
if(y==0)
return x;
long r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream inputstream) {
reader = new BufferedReader(new InputStreamReader(inputstream));
tokenizer = null;
}
public String nextLine(){
String fullLine=null;
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
fullLine=reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return fullLine;
}
return fullLine;
}
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 long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n = int(input())
dir = input()
a = list(map(int, input().split()))
ans = 10 ** 20
for i in range(n - 1):
if dir[i] == 'R' and dir[i + 1] == 'L':
ans = min(ans, (a[i + 1] - a[i]) // 2)
if ans == 10 ** 20:
print(-1)
else:
print(ans) | PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long maxint = 1e9 + 7;
long long n;
string st;
void openFile() { freopen("inp.txt", "r", stdin); }
void solve() {
cin >> n;
cin.ignore(1);
cin >> st;
long long px = -1;
long long res = maxint, x;
for (int i = 0; i < n; i++) {
cin >> x;
if (st[i] == 'R') {
px = x;
} else {
if (px >= 0) res = min(res, (x - px) / 2);
}
}
res == maxint ? cout << -1 : cout << res;
}
int main() {
ios::sync_with_stdio(false);
int ntest;
solve();
return 0;
}
| CPP |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n = int(raw_input().strip())
dir = list(raw_input())
pos = map(int,raw_input().split())
minDis = 1000000005
for i in range(1,n):
if dir[i] == 'L' and dir[i-1] == 'R':
minDis = min(minDis, pos[i] - pos[i-1])
if minDis == 1000000005:
print '-1'
else:
print minDis / 2 | PYTHON |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 |
import java.util.Scanner;
public class Ps {
public static void main(String[] args){
Scanner input =new Scanner(System.in);
int n=input.nextInt();
String str=input.next();
long moment=(long) 1e9;
int arr[]=new int[n];
for (int i = 0; i < n; i++) {
arr[i]=input.nextInt();
}
for (int i = 0; i < n-1; i++) {
if(str.charAt(i)=='R' && str.charAt(i+1)=='L'){
moment =Math.min(moment,(arr[i+1]-arr[i])/2);
}
}
if(moment==1e9)
System.out.println("-1");
else
System.out.println(moment);
}
}
| JAVA |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = in.nextInt();
String s = in.next();
int[] x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
}
boolean left = false;
boolean right = false;
int xLeft = 0, xRight = 0;
int ans = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'R') {
xRight = x[i];
right = true;
} else {
xLeft = x[i];
left = true;
}
if (left && right) {
int dist = xLeft - xRight;
if (dist > 0) {
ans = Math.min(ans,dist / 2);
}
}
}
if (ans == Integer.MAX_VALUE) {
ans = -1;
}
out.println(ans);
out.close();
}
}
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());
}
public long nextLong() {
return Long.parseLong(next());
}
}
| JAVA |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n;
struct node {
int d, l;
} a[200005];
int ans = 1000000000;
char ch[200005];
int main() {
scanf("%d", &n);
scanf("%s", ch + 1);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].l);
if (ch[i] == 'L')
a[i].d = -1;
else
a[i].d = 1;
}
for (int i = 1; i < n; i++) {
if ((a[i].d == 1) && (a[i + 1].d == -1))
ans = min(ans, (a[i + 1].l - a[i].l) / 2);
}
if (ans == 1000000000) ans = -1;
printf("%d", ans);
}
| CPP |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | def main():
n = int(input())
directions = input()
x = [int(y) for y in input().split()]
last_r = -1
res = -1
for i in range(0, n):
if directions[i] == 'L':
if last_r != -1:
res = (res >= 0 and min(res, (x[i] - x[last_r]) // 2)) or (x[i] - x[last_r]) // 2
else:
last_r = i
print(res)
main() | PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n = int(input())
c = list(input())
p = list(map(int,input().split()))
f = ["R","L"]
ans=[]
for i in range(n-1):
if [c[i],c[i+1]] == f:
ans.append(p[i+1]-p[i])
if len(ans)==0:
print(-1)
else:
print(min(ans)//2)
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n=int(input())
s=input()
A = input().split()
for i in range(len(A)):
A[i] = int(A[i])
min1=10000000000
for i in range (1,n):
if A[i]-A[i-1]<min1 and s[i]=='L' and s[i-1]=='R':
min1=A[i]-A[i-1]
if min1==10000000000:
print(-1)
else:
print(min1//2)
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, t = -1;
char c;
cin >> n;
vector<char> d;
vector<int> coord;
for (int i = 0; i < n; i++) {
cin >> c;
d.push_back(c);
}
for (int i = 0; i < n; i++) {
cin >> x;
coord.push_back(x);
}
for (int i = 0; i < n - 1; i++) {
if (d[i] == 'R' && d[i + 1] == 'L') {
int time = (coord[i + 1] - coord[i]) / 2;
if (t == -1)
t = time;
else
t = min(t, time);
}
}
cout << t << endl;
}
| CPP |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n = int(input())
moves = input()
positions = list(map(int, input().split(' ')))
ans = 10 ** 9
for i in range(n-1):
if moves[i] == 'R' and moves[i+1] == 'L':
ans = min(ans, (positions[i+1] - positions[i]) // 2)
if ans == 10 ** 9:
print(-1)
else:
print(ans)
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int a;
cin >> a;
long long ar[a + 2];
string s;
cin >> s;
for (int i = 0; i < a; i++) {
cin >> ar[i];
}
long long f = 1, m = INT_MAX;
for (int i = 0; i < a - 1; i++) {
if (s[i] == 'R' && s[i + 1] == 'L') {
long long p = ar[i + 1] - ar[i];
if (p < m) {
m = p;
f = 0;
}
}
}
if (f)
cout << -1 << endl;
else
cout << m / 2 << endl;
return 0;
}
| CPP |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m;
b /= 2;
a = ((a % m) * (a % m)) % m;
}
return ans % m;
}
int main() {
int n;
cin >> n;
string s;
cin >> s;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ans = (long long)5e15;
for (int i = 0; i < n - 1; i++) {
if (s[i] == 'R' && s[i + 1] == 'L') {
long long dist = a[i + 1] - a[i];
long long t = (dist) / 2;
ans = min(ans, t);
}
}
if (ans == (long long)5e15) {
cout << "-1";
return 0;
}
cout << ans;
}
| CPP |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | n=int(input())
napr=input()
stroka=input()
coord=[]
k=''
answer=10**10
for i in range(0,len(stroka)):
if i==len(stroka)-1:
coord.append(int(k+stroka[i]))
elif stroka[i]==' ':
coord.append(int(k))
k=''
else:
k+=stroka[i]
for i in range(1,n):
if (napr[i]=='L')and(napr[i-1]=='R')and((coord[i]-coord[i-1])//2+(coord[i]-coord[i-1])%2<answer):
answer=(coord[i]-coord[i-1])//2+(coord[i]-coord[i-1])%2
if answer==10**10:
answer=-1
print(answer)
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
int a, b = 0;
cin >> n;
cin >> s;
int ans = 2000000000;
for (int i = 0; i < n; i++) {
a = b;
cin >> b;
if (i) {
if (s[i] == 'L' && s[i - 1] == 'R') ans = min(ans, (b - a) / 2);
}
}
if (ans == 2000000000) ans = -1;
cout << ans;
return 0;
}
| CPP |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int d[1234678], a[1234678], ans;
int main() {
int n, cnt = 0;
char c;
scanf("%d\n", &n);
for (int i = 1; i <= n; i++) {
scanf("%c", &c);
d[i] = (c == 'R');
}
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
ans = 1047483647;
for (int i = 2; i <= n; i++)
if (d[i - 1] == 1 && d[i] == 0) ans = min(ans, (a[i] - a[i - 1]) / 2);
if (ans == 1047483647)
puts("-1");
else
printf("%d\n", ans);
}
| CPP |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
public class forces_363A {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
java.io.BufferedReader dd = new BufferedReader (new java.io.InputStreamReader(System.in));
int n = Integer.parseInt(dd.readLine());
String k = dd.readLine();
//StringBuilder hey = new StringBuilder(k);
String num[] = dd.readLine().split(" ");
int val[] = new int[num.length];
for(int i=0;i<n;i++){
val[i] = Integer.parseInt(num[i]);
}int lol = 0;
int min=Integer.MAX_VALUE;
if(k.contains("RL")){
for(int i=1;i<n;i++){
char ch = k.charAt(i-1);
char sh = k.charAt(i);
if(ch=='R' && sh=='L'){
lol=(Math.abs(val[i]-val[i-1]))/2;
if(min>lol){
min=lol;
}
}
}
System.out.println(min);
}
else{
System.out.println(-1);
}
}
}
| JAVA |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | //package credit;
import javax.swing.text.MutableAttributeSet;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.IntrospectionException;
import java.io.*;
import java.net.Inet4Address;
import java.sql.SQLIntegrityConstraintViolationException;
import java.time.temporal.ChronoField;
import java.util.*;
import java.util.List;
public class sas {
static boolean v[];
static int ans[];
int size[];
static int count=0;
static int dsu=0;
static int c=0;
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
boolean aBoolean=true;
PrintWriter printWriter = new PrintWriter(System.out);
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
int parent[];
int rank[];
public static void main(String[] args) throws IOException {
sas g = new sas();
g.go();
}
public void go() throws IOException {
// Reader scanner = new Reader();
Scanner scanner=new Scanner(System.in);
int t =1;
for (int i = 0; i < t; i++) {
int n=scanner.nextInt();
String s=scanner.next();
int arr[]=new int[n];
int r=0;
int l=0;
boolean t1=false;
for (int j = 0; j <n; j++) {
int y=scanner.nextInt();
if(s.charAt(j)=='R'){
r=y;
t1=true;
}else if(t1&&s.charAt(j)=='L'){
int w=(y-r)/2;
if(w<min){
min=w;
}
}
}
if(min==Integer.MAX_VALUE){
printWriter.println(-1);
}else{
printWriter.println(min);
}
}
printWriter.flush();
}
public void parent(int n){
for (int i = 0; i < n; i++) {
parent[i]=i;
rank[i]=1;
size[i]=1;
}
}
public void union(int i,int j){
int root1=find(i);
int root2=find(j);
// if(root1 != root2) {
// parent[root2] = root1;
//// sz[a] += sz[b];
// }
if(root1==root2){
return;
}
if(rank[root1]>rank[root2]){
parent[root2]=root1;
size[root1]+=size[root2];
}
else if(rank[root1]<rank[root2]){
parent[root1]=root2;
size[root2]+=size[root1];
}
else{
parent[root2]=root1;
rank[root1]+=1;
size[root1]+=size[root2];
}
}
public int find(int p){
if(parent[p]!=p){
parent[p]=find(parent[p]);
}
return parent[p];
// if(parent[p]==-1){
// return -1;
// }
// else if(parent[p]==p){
// return p;
// }
// else {
// parent[p]=find(parent[p]);
// return parent[p];
// }
}
public void make(int p){
parent[p]=p;
rank[p]=1;
}
Random rand = new Random();
public void sort(int[] a, int n) {
for (int i = 0; i < n; i++) {
int j = rand.nextInt(i + 1);
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
Arrays.sort(a, 0, n);
}
public long gcd(long a,long b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
private void dfs2(ArrayList<Integer>[]arrayList,int j,int count){
ans[j]=count;
for(int i:arrayList[j]){
if(ans[i]==-1){
dfs2(arrayList,i,count);
}
}
}
private void dfs(ArrayList<Integer>[] arrayList, int j) {
v[j]=true;
count++;
for(int i:arrayList[j]){
if(v[i]==false) {
dfs(arrayList, i);
}
}
}
public long fact(long h){
long sum=1;
while(h>=1){
sum=sum*h;
h--;
}
return sum;
}
public long primef(double r){
long c=0;
while(r%2==0){
c++;
r=r/2;
}
for (int i = 3; i <=Math.sqrt(r) ;i+=2) {
while(r%i==0){
c++;
r=r/3;
}
}
if(r>2){
c++;
}
return c;
}
}
class Pair{
long x;
long y;
public Pair(long x,long y){
this.x=x;
this.y=y;
}
}
class Sorting implements Comparator<Pair> {
public int compare(Pair p1,Pair p2){
return Long.compare(p1.x,p2.x);
}
}
| JAVA |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | x=int(input())
n=input()
m=list(map(int,input().split()))[:x]
ar=[]
s=0
mini=1000000000000000000
for i in n:
ar.append(i)
for j in range(0,x-1):
if(ar[j]=="R" and ar[j+1]=="L"):
s=(m[j+1]-m[j])//2
mini=min(mini,s)
if(s):
print(mini)
else:
print("-1")
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | 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.io.InputStream;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Erasyl Abenov
*
*
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
try (PrintWriter out = new PrintWriter(outputStream)) {
TaskB solver = new TaskB();
solver.solve(1, in, out);
}
}
}
class TaskB {
int MAX = 1000000000;
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
char c[] = in.next().toCharArray();
int a[] = new int[n];
int r = -1;
int time = MAX;
for (int i = 0; i < c.length; i++) {
int x = in.nextInt();
if (c[i] == 'R') r = x;
else if (r > -1)
time = Math.min(time, (x - r + 1)/2);
}
if (time == MAX) time = -1;
out.println(time);
}
}
class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
} | JAVA |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | input()
ds = str.strip(input())
xs = map(int, str.split(input()))
best = None
last_right = None
for d, x in zip(ds, xs):
if d == "R":
last_right = x
elif last_right is not None:
t = (x - last_right) // 2
best = min(best or t, t)
print(best or -1)
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | def solve(pos, dir, n):
ans = 987654321
for idx in xrange(0, n - 1):
if dir[idx] == 'R' and dir[idx+1] == 'L':
ans = min(ans, (pos[idx+1] - pos[idx])/2)
if ans == 987654321:
return -1
return ans
# print solve([2,4,6,10], 'RLRL', 4)
# print solve([40,50,60], 'LLR', 3)
n = input()
dir = raw_input().strip()
pos = map(int, raw_input().split(' '))
print solve(pos, dir, n) | PYTHON |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | import sys
if __name__ == '__main__':
n = list(map(int, sys.stdin.readline().split(" ")))[0]
a = list(sys.stdin.readline())
x = list(map(int, sys.stdin.readline().split(" ")))
m = 100000000000
for i in range(n-1):
if a[i]=='R' and a[i+1]=='L':
m = min(m,(x[i+1]-x[i])/2)
if m == 100000000000 :
m = -1
print(int(m))
| PYTHON3 |
699_A. Launch of Collider | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
Input
The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.
The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
Output
In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen.
Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
char str[1010000];
int b[1010000];
int a[1010000];
int bb[1010000];
int main() {
int n;
int flag = 1;
scanf("%d", &n);
scanf("%s", str);
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
}
int coutt1 = 0;
for (int i = 0; i <= n - 1; i++) {
if (str[i] == 'R') {
coutt1++;
a[coutt1] = b[i + 1];
}
}
int coutt2 = 0;
for (int i = 0; i <= n - 1; i++) {
if (str[i] == 'L') {
coutt2++;
bb[coutt2] = b[i + 1];
}
}
int ans = 1000000005;
int i, j;
for (i = 1, j = 1; i <= coutt1 && j <= coutt2;) {
if (a[i] < bb[j]) {
flag = 0;
ans = min((bb[j] - a[i]) / 2, ans);
i++;
} else {
j++;
}
}
if (flag)
printf("-1");
else
printf("%d", ans);
}
| 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 P071C {
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
int n = inScanner.nextInt();
int[] moods = new int[n];
for (int i = 0; i < n; i++) {
moods[i] = inScanner.nextInt();
}
for (int vertices = 3; vertices <= n; vertices++) {
if (n % vertices != 0)
continue;
int step = n / vertices;
tryLoop: for (int offset = 0; offset < step; offset++) {
for (int vertice = offset; vertice < n; vertice += step)
if (moods[vertice] == 0)
continue tryLoop;
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.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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.StringTokenizer;
import java.util.TreeSet;
import javax.swing.plaf.synth.SynthSeparatorUI;
public class Problem1 {
private static final long INF = (long) 1E15;
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
static final int upperBound = (int)10000002;
static ArrayList<Integer>[] modifiedSieve(int N)
{
ArrayList<Integer>[] pf = new ArrayList[N];
for(int i = 1; i < N; ++i)
pf[i] = new ArrayList<Integer>();
for(int i = 2; i < N; ++i)
if(pf[i].isEmpty())
for(int j = i<<1; j < N; j += i)
pf[j].add(i);
return pf;
}
static int pow(long a, int n)
{
int res = 1;
while(n != 0)
{
if((n & 1) == 1)
res *= a;
a *= a;
n >>= 1;
}
return res;
}
static HashSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N))
{
HashSet<Long> factors = new HashSet
<Long>(); //take abs(N) in case of -ve integers
int idx = 0;
long p = primes.get(idx);
while(p * p <= N)
{if(idx+1==primes.size()) {
break;
}
while(N % p == 0) { factors.add(p); 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[] pf;
static void pf(int N)
{
pf = new int[N];
for(int i = 2; i < N; ++i)
if(pf[i] == 0)
{
for(int j = i; j < N; j += i)
{
int p = 0, k = j;
while(k % i == 0)
{
k /= i;
++p;
}
pf[j] += p;
}
}
}
public static void main(String[] args) throws IOException {
//FileWriter f=new FileWriter("D:\\testout.txt");
//out=new PrintWriter(f);
int n=sc.nextInt();
ArrayList<Integer> l=new ArrayList<>();
int a[]=new int[n];
for (int i = 0; i < a.length; i++) {
a[i]=sc.nextInt();
if(a[i]==1) {
l.add(i);
}
}
TreeSet<Integer> set=new TreeSet<>();
for(int i=1;i<=(int)Math.sqrt(n);i++) {
if(n%i==0) {
set.add(i);
set.add(n/i);
}
}
boolean f=false;
boolean f1=true;
for(int k:set) {
if(k>=3&&k<=l.size()) {
for(int x:l) {
if(x>(n/k)) {
break;
}
int j=x;
for(j=x ; j<n ; j+=(n/k)) {
if(a[j]!=1) {
f1=false;
break;
}
}
if(f1)
f=true;
f1=true;
}
}
}
if(f) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
static ArrayList<Integer> primes;
static boolean[] isComposite;
static void sieve(int N) // O(N log log N)
{
isComposite = new boolean[N+1];
Arrays.fill(isComposite, true);
isComposite[0] = isComposite[1] = false; // 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] == true) //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] = false;
}
}
static class Pair{
int val;
int count;
Pair(int v,int c) {
val=v;
count=c;
}
}
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 boolean ready() throws IOException {return br.ready();}
public void waitForInput() throws InterruptedException {Thread.sleep(1000);}
}
} | 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;
public class Main {
public static void main(String[] args) {
try {
new Main().solve();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
private void solve() throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
boolean[] mood = new boolean[n];
String[] s = in.readLine().split(" ");
for (int i = 0; i < n; i++) {
mood[i] = s[i].equals("1");
}
for (int gap = 1; gap << 1 < n; gap++) {
if (n % gap == 0) {
for (int startPoint = 0; startPoint < gap; startPoint++) {
boolean can = true;
for (int current = startPoint; current < n; current += gap) {
if (!mood[current]) {
can = false;
}
}
if (can) {
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;
int main() {
int n;
cin >> n;
int arr[n + 1];
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
set<int> s;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
s.insert(i);
s.insert(n / i);
}
}
s.erase(1);
s.erase(2);
vector<int> v(s.begin(), s.end());
for (int i = 0; i < v.size(); i++) {
int key = v.at(i);
for (int j = 1; j <= (n / key); j++) {
int k = j;
int count = 0;
while ((k <= n)) {
if (arr[k] == 1) {
k = k + (n / key);
count++;
} else {
break;
}
}
if (count == key) {
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 | import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class C {
private static boolean isPrime(int n) {
if (n <= 2) {
return true;
}
if (n % 2 == 0) {
return false;
}
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
boolean[] a = new boolean[n];
for (int i = 0; i < n; ++i) {
a[i] = (in.nextInt() == 1);
}
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = n / 3; i > 0; --i) {
if (n % i == 0) {
primes.add(i);
}
}
boolean can = false;
for (int prime: primes) {
for (int begin = 0; begin < prime; ++begin) {
boolean flag = true;
for (int it = begin; it < n; it += prime) {
if (!a[it]) {
flag = false;
break;
}
}
if (flag) {
can = true;
break;
}
}
if (can) {
break;
}
}
out.println(can ? "YES" : "NO");
in.close();
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;
const int N = 2e5 + 5;
long long int n;
int a[N];
bool check(long long int k) {
for (long long int i = 0; i < k; i++) {
bool f = 1;
for (long long int j = 0; i + j < n; j += k) {
if (!a[i + j]) {
f = 0;
break;
}
}
if (f) {
return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
bool f = 1;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (!a[i]) {
f = 0;
}
}
if (f) {
cout << "YES";
return 0;
}
for (long long int i = 2; i * i <= n; i++) {
if (i == n / 2) {
continue;
}
if (n % i != 0) {
continue;
}
if (check(i)) {
cout << "YES";
return 0;
}
if (i != 2) {
if (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;
int kt[100001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, num;
cin >> n;
for (long long i = 1; i < n + 1; i++) cin >> kt[i];
for (long long i = 3; i < n + 1; i++) {
if (n % i == 0) {
for (int j = 1; j <= n / i; j++) {
num = 0;
for (int k = j; k <= n; k += (n / i))
if (!kt[k]) {
num = 1;
break;
}
if (!num) return cout << "YES", 0;
}
}
}
return cout << "NO", 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
public class RoundTableKnights_CodeForces {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
String [] temp = br.readLine().split(" ");
int []num = new int[n];
int countOnes=0;
ArrayList<Integer>hs= new ArrayList<Integer>();
for(int i=0;i<n;i++){
num[i]=Integer.parseInt(temp[i]);
if(num[i]==1)
countOnes++;
if(num[i]==1){
hs.add(i);
}
}
if(countOnes==n){
System.out.println("YES");
System.exit(0);
}
//int firstIndex=hs.get(0);
//ArrayList<Integer>sec= new ArrayList<Integer>();
//sec.add(firstIndex);
//hs.remove(0);
// int x=2;
for(int f:hs){
for(int x=2;x<n;x++){
//System.out.println("the x is "+x+" the f is "+f);
if(((n-f)/(x-1))+1<3){
// System.out.println("the x is "+x );
// System.out.println("the f is "+f);
// System.out.println("heree");
break;
}
boolean flag=true;
int target=n/x;
int count=1;
int i=f+x;
for(i=f+x;i<n;i+=x){
if(num[i]!=1){
flag=false;
break;
}
else{
count++;
}
//System.out.println(i);
}
//System.out.println(i);
// if(x==3){
// System.out.println("the count is "+ count);
// System.out.println("the target is "+ target);
// }
if(target>2&&flag && count==target && i-n==f){
//System.out.println(x+" "+f);
System.out.println("YES");
System.exit(0);
}
}
}
// for(int x=2;x<n;x++)
// while(n/x>=3){ // increment x
// boolean flag=true;
// if(n%(x)!=0)
// break;
// int target = n/(x);
// int count=1;
// flag=true;
// for(int i=firstIndex+x;i<n;i+=x){
// if(num[i]!=1){
// flag=false;
// break;
// }
// else
// count++;
// }
// if(flag && count==target){
// System.out.println("YES");
// System.exit(0);
// }
// x++;
// }
//}
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.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//(new FileReader("input.in"));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
//PrintWriter pw = new PrintWriter("output.out", "UTF-8");
int n = parseInt(in.readLine());
int Z = 0,z = Integer.MAX_VALUE;
tk = new StringTokenizer(in.readLine());
int [] a = new int[n];
for(int i=0; i<n; i++)
a[i] = parseInt(tk.nextToken());
int [] dp = new int[n];
for(int i=0; i<n; i++) {
if(a[i]==0)
dp[i] = 1+(i==0 ? 0 : dp[i-1]);
Z = max(Z,dp[i]);
z = min(z,dp[i]);
}
if(dp[n-1]!=0 && dp[0]!=0) {
int x = dp[n-1];
int i;
for(i=0; i<n; i++)
if(dp[i]==0) break;
Z = max(Z,x+dp[i-1]);
z = min(z,x+dp[i-1]);
}
int [] dp2 = new int[n];
for(int i=0; i<n; i++) {
dp2[i] = (i>0 ? dp2[i-1] : 0)+(a[i]==1 ? 1 : 0);
}
boolean flag = false;
int i=0;
for(i=1; i*i<=n; i++) {
if(n%i==0 && n/i>=3) {
int j = n/i;
boolean f = true;
for(int k=0; k<i; k++) {
f = true;
int b = k;
for(int l=0; l<j; l++) {
if(a[b]==0) {
f = false;
break;
}
b += i;
}
if(f) break;
}
if(f) {
flag = true;
break;
}
}
if(n%i==0 && n/i != i && i>=3) {
int j = n/i;
boolean f = true;
for(int k=0; k<j; k++) {
f = true;
int b = k;
for(int l=0; l<i; l++) {
if(a[b]==0) {
f = false;
break;
}
b += j;
}
if(f) break;
}
if(f) {
flag = true;
break;
}
}
}
System.out.println(flag ? "YES" : "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 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)
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 | import java.io.*;
import java.util.StringTokenizer;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws IOException {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(in.nextLine());
StringTokenizer s= new StringTokenizer(in.nextLine());
boolean b[]=new boolean[n+1];
int sum=0;
for(int i=1; i<=n; i++){
if(Integer.parseInt(s.nextToken())==1){
b[i]=true;
sum++;
}
}
if(sum==n) {
out.println("YES");
out.close();
return;
}
boolean found = false;
int q = (int)Math.sqrt(n)+1;
r: for(int i=2; i<=n; i++){
if(n%i==0){
int k = n/i;
if(k<3)
continue;
for(int j=1; j<=i; j++){
sum=0;
for(int h=j; h<=n; h+=i){
if(!b[h])
break;
else
sum++;
}
if(sum==k){
found = true;
break r;
}
}
}
}
out.print(found?"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 | def factors(n):
farr=[]
for i in range(1,n+1):
if n%i==0:
farr+=[i]
return farr
n=int(input())
arr=list(map(int,input().split()))
sets=set()
for i in range(n):
if arr[i]==1:
sets.add(i)
farr=factors(n)
flag=0
flag2=0
for d in farr:
if n//d>2:
for j in range(d):
if arr[j]==1:
for i in range(j,n,d):
if i not in sets:
break
else:
print("YES")
flag2=1
flag=1
break
if flag2:
break
if flag2:
break
if not flag:
print("NO")
#print(sets)
#print(farr)
| 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;
long long n;
bool a[(301 * 1000)];
void check(long long x) {
for (int i = 0; i < x; i++) {
bool mark = 1;
for (int j = i; j < n; j += x)
if (!a[j]) {
mark = 0;
break;
}
if (mark) {
cout << "YES";
exit(0);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i <= sqrt(n); i++)
if (n % i == 0) {
if (i >= 3) check(n / i);
if (n / i >= 3) check(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 | import java.util.*;
public class Fku {
static Scanner in =new Scanner(System.in);
static int n,k[];
public static void main(String[] args) {
n=in.nextInt();
k=new int[n+1];
for(int i=1;i<n+1;i++){k[i]=in.nextInt();}
for(int i=1;i<n+1;i++){
if(n%i==0)
if(can(i)){System.out.println("YES");return;}}
System.out.println("NO");
}
static boolean can(int K){
if(n/K<=2){return false;}
boolean b=false;
for(int i=1;i<=K;i++){
b=true;
for(int j=i;j<=n;j+=K){
if(k[j]==0) b=false;
}
if(b)
{return true;}
}
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 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
const int M = 1e9 + 7;
const double eps = 1e-7;
int a[N], b[N];
int Int() {
int x;
scanf("%d", &x);
return x;
}
long long Lnt() {
long long x;
scanf("%I64d", &x);
return x;
}
long long Bigmod(long long A, long long B, long long C) {
if (B == 0LL) return 1LL;
long long x = Bigmod(A, B / 2LL, C);
x = (x * x) % C;
if (B % 2 == 1) x = (x * A) % C;
return x;
}
struct mar {
int i, j;
} cr[N];
bool cmp(mar a, mar b) {
if (a.i == b.i) return a.j < b.j;
return a.i < b.i;
}
bool Cmp(int a, int b) { return a > b; }
void print(int n) { printf("%d\n", n); }
void Print(long long n) { printf("%I64d\n", n); }
void debug(int n) { printf("%d ", n); }
map<int, int> mp;
int NOD(int x) {
if (mp.find(x) != mp.end()) return mp[x];
int d = 1, xx = x;
for (int i = 2; i * i <= x; i++) {
int p = 0;
while (x % i == 0) {
x /= i;
p++;
}
d *= (p + 1);
}
if (x > 1) d *= 2;
d--;
mp[xx] = d;
return d;
}
bool polygon(int p, int n) {
for (int i = 1; i <= p; i++) {
int u = 1;
for (int j = i; j <= n; j += p) {
u &= a[j];
}
if (u && n / p > 2) return true;
}
return false;
}
bool ispolygon(int n) {
int i, sqr = sqrt(n);
for (i = 1; i <= sqr; i++) {
if (n % i == 0)
if (polygon(i, n) || polygon(n / i, n)) return true;
}
return false;
}
int main() {
int n = Int(), i;
for (i = 1; i <= n; i++) a[i] = Int();
if (ispolygon(n)) return puts("YES"), 0;
return puts("NO"), 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 a[100005], n;
vector<int> d;
void gen() {
int tmp = n;
for (int i = 1; i <= tmp; i++) {
if (tmp % i == 0) d.push_back(i);
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
gen();
int sz = d.size();
bool ok = 0;
for (int i = 0; i < sz; i++) {
int jump_length = d[i];
int cycle_length = n / d[i];
if (cycle_length <= 2) continue;
for (int j = 0; j < d[i]; j++) {
if (a[j] == 0) continue;
int cnt = 0;
for (int k = j; k < n; k += jump_length) {
if (a[k] == 1)
cnt++;
else
break;
}
if (cnt == cycle_length) {
ok = 1;
break;
}
}
if (ok) break;
}
if (ok)
printf("YES\n");
else
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 | #include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const double PI = 3.141592653589793;
int n, n2, l, l2, k, i, j, tab[100005], da;
int main() {
scanf("%d", &n);
n2 = n;
for (i = 0; i < n; i++) scanf("%d", &tab[i]);
k = 2;
while (n != 1) {
if (n % k == 0) {
if (k == 2 && n % 4 == 0)
l = 4;
else
l = k;
if (l > 2) {
l2 = n2 / l;
for (i = 0; i < l2; i++) {
da = 1;
for (j = i; j < n2; j += l2)
if (!tab[j]) da = 0;
if (da) {
printf("YES\n");
return 0;
}
}
}
while (n % k == 0) n /= k;
}
k++;
}
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 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class C71 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static int nextInt() throws Exception {
in.nextToken();
return (int) in.nval;
}
static String nextString() throws Exception {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws Exception {
int n = nextInt();
boolean[] b = new boolean[n];
for (int i = 0; i < n; i++)
b[i] = nextInt() == 1;
for (int d = 3; d <= n; d++) if (n%d == 0) {
int k = n/d;
L : for (int i = 0; i < k; i++) {
for (int j = i; j < n; j += k)
if (!b[j]) continue L;
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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.BitSet;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Nafiur Rahman Khadem Shafin
*/
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 ();
}
static class TaskC {
private static BitSet flg;
private static byte[] arr;
private static boolean sieve (int ne) {
flg = new BitSet (ne+5);
flg.set (2, ne+4);
for (int i = 3; i<=ne; i++) {
if (!flg.get (i)) {
continue;
}
int j = 2;
for (j = 2; j*i<ne; j++) {
flg.clear (i*j);
}
if (j*i == ne) {
flg.clear (ne);
// System.out.println (i);
if (i == 2) {
continue;
}
for (int k = 0; k<j; k++) {
boolean khek = true;
for (int l = k; l<ne; l += j) {
if (arr[l] == 0) {
khek = false;
break;
}
}
if (khek) {
return true;
}
}
}
}
if (flg.get (ne)) {
int j = 1;
// System.out.println ("dhuksi");
for (int k = 0; k<j; k++) {
boolean khek = true;
for (int l = k; l<ne; l += j) {
if (arr[l] == 0) {
khek = false;
break;
}
}
if (khek) {
return true;
}
}
}
return false;
}
public void solve (int testNumber, InputReader in, PrintWriter out) {
/* the main problem here maybe the time limit which is only 0.5 seconds:( Another fucking problem statement. Original problem statement is:
* find a regular polygon(with at least 3 vertices) with all green vertices, ==> n%v==0 && v>=3. If v==6 is valid ans then obviously v==3
* is also a valid ans, ==> v must be a prime divisor of n, so I have simplified the problem to check for only the prime divisors but the
* problem is how to check for every v in less complexity than o((n/v)*(v)), let's try submitting using this complexity only*/
int n = in.nextInt ();
arr = new byte[n+5];
for (int i = 0; i<n; i++) {
arr[i] = Byte.parseByte (in.next ());
}
if (sieve (n)) {
out.println ("YES");
}
else out.println ("NO");
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader (InputStream stream) {
reader = new BufferedReader (new InputStreamReader (stream));
tokenizer = null;
}
public String next () {
while (tokenizer == null || !tokenizer.hasMoreTokens ()) {
try {
String str;
if ((str = reader.readLine ()) != null) tokenizer = new StringTokenizer (str);
else return null;//to detect eof
} 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 | import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class RoundTableKnights {
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> divisors = divisors(n);
for ( int div : divisors){
if(n/div>=3){
for ( int i = 0;i < div;i++){
int sum = 0;
for ( int j = i;j < n;j+=div){
sum+=arr[j];
}
if(sum==n/div){
System.out.println("YES");
return;
}
}
}
}
System.out.println("NO");
}
static ArrayList<Integer> divisors(int x) {
ArrayList<Integer> res = new ArrayList<Integer>();
int i;
for (i = 1; i < x / i; i++) {
if (x % i == 0) {
res.add(i);
res.add(x / i);
}
}
if (i * i == x)
res.add(i);
return res;
}
private static class Scanner{
public BufferedReader reader;
public StringTokenizer st;
public Scanner(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
public String next(){
while(st == null || !st.hasMoreTokens()){
try{
String line = reader.readLine();
if(line == null) return null;
st = new StringTokenizer(line);
}catch (Exception e){
throw (new RuntimeException());
}
}
return st.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(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 | from sys import stdin
from sys import exit
from math import sqrt
#parser
def parser():
return map(int, stdin.readline().split())
def rp_found_begin_pos(side_length,number_of_sides,begin_pos):
pos=begin_pos
while number_of_sides!=0:
if not knights_mood[pos]:
return False
pos+=side_length
number_of_sides-=1
return True
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
n=int(stdin.readline())
knights_mood=[x for x in parser()]
divisors=[]
n_sqrt=int(sqrt(n))
for i in range(1,n_sqrt+1):
if n % i == 0:
pair_divisors=(i,int(n/i))
divisors.append(pair_divisors)
for pair in divisors:
first_divisor=pair[0]
second_divisor=pair[1]
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 | import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) throws IOException {
new C().solve();
}
static final boolean DEBUG = System.getProperty("ONLINE_JUDGE") == null;
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
void solve() throws IOException {
br = new BufferedReader(new InputStreamReader(DEBUG ? new FileInputStream("input.txt") : System.in));
int n = nextInt();
boolean[] ok = new boolean[n];
for (int i = 0; i < n; i++) {
ok[i] = nextInt() == 1;
}
br.close();
boolean res = find(n, ok);
PrintWriter pw = new PrintWriter(System.out);
pw.println(res ? "YES" : "NO");
pw.close();
}
List<Integer> findPrimes(int n) {
boolean[] m = new boolean[n + 1];
int up = Math.min(n, (int) Math.sqrt(n + 1));
for (int i = 2; i <= up; i++) {
if (!m[i]) {
int j = i + i;
while (j <= n) {
m[j] = true;
j += i;
}
}
}
List<Integer> primes = new ArrayList<Integer>();
if (n >= 4) {
m[4] = false;
}
for (int i = 3; i <= n; i++) {
if (!m[i] && n % i == 0) {
primes.add(i);
}
}
return primes;
}
boolean find(int n, boolean[] ok) {
List<Integer> primes = findPrimes(n);
int[] next = new int[n];
int lastGoodInd = n + 1;
for (int i = n - 1; i >= 0; i--) {
if (ok[i]) {
lastGoodInd = i;
}
next[i] = lastGoodInd;
}
if (lastGoodInd == n + 1) {
return false;
}
int[] p = new int[primes.get(primes.size() - 1)];
for (int k : primes) {
final int delta = n / k;
p[0] = 0;
for (int i = 1; i < k; i++) {
p[i] = p[i - 1] + delta;
}
while (p[0] < delta) {
int max = 0;
for (int i = 0; i < k; i++) {
final int nextInd = p[i] + max;
if (nextInd >= n) {
break;
}
if (!ok[nextInd]) {
max = Math.max(max, next[nextInd] - p[i]);
if (max >= delta) {
break;
}
}
}
if (max == 0) {
return true;
}
for (int i = 0; i < k; i++) {
p[i] += max;
}
}
}
return false;
}
String nextToken() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(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 | #include <bits/stdc++.h>
using namespace std;
const long long N = 200005, INF = 2000000000000000000;
long long power(long long a, long long b, long long p) {
if (a == 0) return 0;
long long res = 1;
a %= p;
while (b > 0) {
if (b & 1) res = (res * a) % p;
b >>= 1;
a = (a * a) % p;
}
return res;
}
vector<long long> prime;
bool isprime[N];
void pre() {
for (long long i = 2; i < N; i++) {
if (isprime[i]) {
for (long long j = (i * i); j < N; j += i) isprime[j] = false;
}
prime.push_back(i);
}
return;
}
int32_t main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(std::numeric_limits<double>::max_digits10);
;
memset(isprime, false, sizeof(isprime));
pre();
long long n;
cin >> n;
long long ar[n];
for (long long i = 0; i < n; i++) cin >> ar[i];
long long temp = n;
vector<long long> v;
long long x = prime.size();
if (n % 2 == 0 && n > 4) v.push_back(2);
for (long long i = 1; i < x; i++) {
if (temp % prime[i] == 0) {
v.push_back(n / prime[i]);
temp /= prime[i];
}
if (prime[i] > temp) break;
}
long long n1 = v.size();
vector<long long> vv[n1];
long long f = 0;
for (long long i = 0; i < n; i++) {
if (ar[i] == 0) f = 1;
for (long long j = 0; j < n1; j++) {
if (i < v[j])
vv[j].push_back(ar[i]);
else
vv[j][i % v[j]] = min(vv[j][i % v[j]], ar[i]);
}
}
for (long long i = 0; i < n1; i++) {
for (long long j = 0; j < v[i]; j++) {
if (vv[i][j] == 1 && (n / v[i]) > 2 && v[i] > 1) {
cout << "YES";
return 0;
}
}
}
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 | n = int(input())
a = [0] + [int(x) for x in input().split()]
done = 0
for i in range(1, n + 1):
if n % i or n < 3 * i:
continue
flag = 0
for j in range(1, i + 1):
flag = 1
for k in range(j, n + 1, i):
if a[k] == 0:
flag = 0
break
if flag:
break
if flag:
print("YES")
done = 1
break
if not done:
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 M = 1e9 + 7;
template <class T>
T ABS(const T& x) {
return x > 0 ? x : -x;
}
long long int gcd(long long int n1, long long int n2) {
return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2);
}
long long int lcm(long long int n1, long long int n2) {
return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2);
}
long long int ceil2(long long int a, long long int b) {
return (a + b - 1) / b;
}
int n;
void divisors(vector<int>& d) {
vector<int> v;
for (int i = 1; i <= sqrt(n); ++i) {
if (n % i == 0) {
if (n / i == i) {
d.push_back(i);
} else {
d.push_back(i);
v.push_back(n / i);
}
}
}
for (int i = v.size() - 1; i >= 0; --i) {
d.push_back(v[i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int arr[n];
for (int i = 0; i < (n); ++i) {
cin >> arr[i];
}
vector<int> d;
divisors(d);
bool v[n];
for (int i = d.size() - 1; i >= 0; --i) {
int size = n / d[i];
if (size < 3) continue;
memset(v, false, sizeof(v));
int k = d[i], cnt = 0;
for (int l = 0; l < n; ++l) {
int prev = (l - k + n) % n, next = (l + k) % n;
if (arr[l] && arr[prev] && arr[next]) {
v[l] = true;
cnt++;
} else {
v[l] = false;
}
}
if (cnt >= size) {
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;
int n;
int arr[100100];
int m;
int A[500];
void getDiv() {
int sq = sqrt(n);
A[1] = 1;
m = 1;
for (int i = 2; i <= sq; i++) {
if (n % i == 0) {
m++;
A[m] = i;
if (i * i == n || i == 2) continue;
m++;
A[m] = n / i;
}
}
}
int cal() {
getDiv();
sort(A + 1, A + m + 1);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= A[i]; j++) {
int u = j;
int f = 0;
int sum = 0;
while (u <= n) {
if (arr[u] != 1) {
f = 1;
break;
}
u += A[i];
sum++;
}
if (f == 0 && sum >= 3) return 1;
}
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
int uu = cal();
if (uu == 1)
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 | n = int(input())
arr = list(map(lambda x: int(x), input().split(" ")))
def solution(n: int, arr: [int]) -> str:
for i in range(3, n + 1):
if n % i != 0: continue
for j in range(0, n // i):
for k in range(j, n, n // i):
if arr[k] == 0:
break
else:
return "YES"
return "NO"
print(solution(n, arr)) | 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.util.*;
import java.io.*;
public class temp
{
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt();
int[] p = new int[n];
int and = 1;
boolean flag = false;
for(int i=0;i<n;i++)
{
p[i] = in.nextInt();
and = and & p[i];
}
if(and == 1)
flag = true;
else
{
and = 1;
for(int i=2;i*i<=n;i++)
{
if(n%i == 0)
{
if(n/i != 2)
{
for(int j=0;j<i;j++)
{
for(int k=0;k<n/i;k++)
{
and = and & p[k*i + j];
}
if(and == 1)
{
flag = true;
break;
}
else
and = 1;
}
}
if(i != 2)
{
for(int j=0;j<n/i;j++)
{
for(int k=0;k<i;k++)
{
and = and & p[k*(n/i) + j];
}
if(and == 1)
{
flag = true;
break;
}
else
and = 1;
}
}
}
if(flag)
break;
}
}
if(flag)
pw.println("YES");
else
pw.println("NO");
pw.flush();
pw.close();
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
/*static class CodeX {
public static void sort(long arr[]) {
merge_sort(arr, 0, arr.length - 1);
}
private static void merge_sort(long A[], long start, long end) {
if (start < end) {
long mid = (start + end) / 2;
merge_sort(A, start, mid);
merge_sort(A, mid + 1, end);
merge(A, start, mid, end);
}
}
private static void merge(long A[], long start,long mid, long end) {
long p = start, q = mid + 1;
long Arr[] = new long[(int)(end - start + 1)];
long k = 0;
for (int i = (int)start; i <= end; i++) {
if (p > mid)
Arr[(int)k++] = A[(int)q++];
else if (q > end)
Arr[(int)k++] = A[(int)p++];
else if (A[(int)p] < A[(int)q])
Arr[(int)k++] = A[(int)p++];
else
Arr[(int)k++] = A[(int)q++];
}
for (int i = 0; i < k; i++) {
A[(int)start++] = Arr[i];
}
}
}*/
}
| 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;
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans % m;
}
long long n;
vector<long long> v;
bool func(long long k) {
for (long long i = 0; i < k; i++) {
bool check = true;
for (long long j = 0; i + j < n; j += k) {
if (!v[i + j]) {
check = false;
break;
}
}
if (check) {
return true;
}
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
v = vector<long long>(n);
bool check = true;
for (long long i = 0; i < n; i++) {
cin >> v[i];
if (!v[i]) {
check = false;
}
}
if (check) {
cout << "YES";
return 0;
}
for (long long i = 2; i * i <= n; i++) {
if (i == n / 2) {
continue;
}
bool check = func(i);
if (n % i != 0) {
continue;
}
if (check) {
cout << "YES";
return 0;
}
if (i != 2) {
check = func(n / i);
if (check) {
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 | #------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
def Factors(n):
i=1
ans=[]
while(i<=sqrt(n)):
if(n%i==0):
ans.append(i)
if(n/i!=i):
ans.append(n//i)
i+=1
return ans
def possible(x,vertices):
if(vertices<=2):
return False
weight=n//vertices
w=x
for i in range(vertices):
if(a[w]==0): return False
w=(w+weight)%n
return True
n=Int()
a=array()
sides=sorted(Factors(n))
#print(sides)
for side in sides:
for i in range(n):
if(a[i]==1 and possible(i,side)):
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 | #include <bits/stdc++.h>
using namespace std;
int knight[100000];
int is_possible(int t, int n) {
vector<int> r(t, 1);
for (int i = 0; i < n; i++) r[i % t] &= knight[i];
for (int i = 0; i < r.size(); i++)
if (r[i]) return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> knight[i];
for (int i = 1; i * 3 <= n; i++) {
if (n % i == 0 and is_possible(i, 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class C71 {
static StreamTokenizer sc;
public static void main(String[] args) throws IOException{
sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
int n = nextInt();
int[]a = new int[n+1];
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
}
for (int i = 1; i <= n; i++) {
if (n % i==0) {
for (int j = 1; j <= i; j++) {
boolean f = true;
int cnt = 0;
for (int j2 = j; j2 <= n; j2 += i) {
if (a[j2] != 1) {
f = false;
break;
}
else
cnt++;
}
if (f && cnt >= 3) {
System.out.println("YES");
return;
}
}
}
}
System.out.println("NO");
}
private static int nextInt() throws IOException{
sc.nextToken();
return (int) sc.nval;
}
}
| 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 | # -*- coding: utf-8 -*-
n = input()
status = map(int, raw_input().split())
frt = False
ors = lambda x, y: x or y
ands = lambda x, y: x and y
b = False
for l in range(1, n/3+1):
if n%l!=0: continue
for i in range(l):
b = True
for j in range(i, n, l):
if status[j] == 0:
b = False; break
if b: break
if b: break
print 'YES' if b else '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 | n = int(input())
moods = list(map(int, input().split()))
good = False
for skip in range(n // 3, 0, -1):
if n % skip != 0:
continue
verts = n // skip
for i in range(skip):
if sum(moods[i + j * skip] for j in range(verts)) == verts:
good = True
if good:
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;
vector<int> vprimes;
int primes[100005];
int arr[100005];
void gen_primes() {
for (int i = 2; i < 100005; i++) {
if (!primes[i]) {
vprimes.push_back(i);
for (int j = i + i; j < 100005; j += i) {
primes[j] = 1;
}
}
}
}
int main() {
gen_primes();
int num_primes = vprimes.size();
int n;
cin >> n;
bool all_ones = true;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] == 0) all_ones = false;
}
if (all_ones) {
cout << "YES" << endl;
return 0;
}
for (int i = 2; ceil(n / (i * 1.0)) >= 3; i++) {
int gap = i;
if (n % gap) continue;
for (int j = 0; j < gap; j++) {
int k;
for (k = j; k < n; k += gap) {
if (arr[k] == 0) break;
}
if (k >= 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;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
template <typename T>
T POW(T B, T P) {
if (P == 0) return 1;
if (P & 1)
return B * POW(B, P - 1);
else
return (POW(B, P / 2)) * (POW(B, P / 2));
}
long long powmod(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 100000007;
a = a * a % 100000007;
b >>= 1;
}
return ans;
}
int main() {
fastio();
int n;
cin >> n;
int arr[n];
vector<int> v, divisors;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i]) v.push_back(i + 1);
}
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisors.push_back(i);
divisors.push_back(n / i);
}
}
for (int i = 0; i < divisors.size(); i++) {
int k = divisors[i];
for (int j = 0; j < n; j++) {
bool flag = true;
if (arr[j] == 0) continue;
int cnt = 0;
for (int p = j; p < n; p += k) {
if (arr[p] != 1) {
flag = false;
break;
} else
cnt++;
}
if (cnt == n / k && cnt > 2)
flag = true;
else
flag = false;
if (flag) return cout << "YES" << '\n', 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 | n=int(input())
a=input().split()
for i in range(3,n+1):
if n % i > 0:continue
step,ans=n//i,"YES"
for ofs in range(step):
ans="YES"
for j in range(ofs,n,step):
if a[j]=='0':
ans="NO"
break
if ans=="YES":
print(ans)
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.PrintWriter;
import java.util.Scanner;
import java.util.Vector;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int[] b = new int[n];
for (int i = 0; i < b.length; i++)
b[i] = in.nextInt();
Vector<Integer> pr = new Vector<Integer>();
boolean[] complex = new boolean[n + 1];
for (int i = 3; i * i <= n; i++)
if (!complex[i])
for (int j = i + i; j <= n; j += i)
complex[j] = true;
for (int i = 3; i <= n; i++)
if (!complex[i])
pr.add(i);
pr.add(4);
pr.add(n);
boolean yes = false;
outer: for (int i = 0; i < pr.size(); i++) {
int prime = pr.elementAt(i);
if (n % prime != 0)
continue;
int count = n / prime;
for (int j = 0; j < count; j++) {
boolean good = true;
for (int k = j; k < n; k += count)
if (b[k] == 0) {
good = false;
break;
}
if (good) {
yes = true;
break outer;
}
}
}
out.print(yes ? "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 | //package round65;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class C {
Scanner in;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
int[] a = new int[n];
for(int i = 0;i < n;i++){
a[i] = ni();
}
for(int p = 1;p <= n/3;p++){
if(n % p == 0){
outer:
for(int i = 0;i < p;i++){
for(int j = i;j < n;j += p){
if(a[j] == 0){
continue outer;
}
}
out.println("YES");
return;
}
}
}
out.println("NO");
}
void run() throws Exception
{
// int n = 100000;
// StringBuilder sb = new StringBuilder(n + " ");
// Random r = new Random();
// for(int i = 0;i < n;i++){
// sb.append(r.nextInt(2) + " ");
// }
// INPUT = sb.toString();
in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
out = new PrintWriter(System.out);
solve();
out.flush();
}
public static void main(String[] args) throws Exception
{
new C().run();
}
int ni() { return Integer.parseInt(in.next()); }
void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
| 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.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
BufferedReader in;
StringTokenizer st;
PrintWriter out;
int n;
int[] v;
ArrayList<Integer> divisors(int n) {
ArrayList<Integer> ret = new ArrayList<Integer>();
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.add(i);
if (n / i != i)
ret.add(n / i);
}
}
Collections.sort(ret);
return ret;
}
boolean check(int divisor) {
if (n / divisor < 3)
return false;
int index = 0;
while (index < n) {
if (index >= divisor)
break;
boolean found = true;
for (int i = index; i < n; i += divisor)
if (v[i] == 0) {
found = false;
break;
}
if (found)
return true;
++index;
}
return false;
}
void solve() throws IOException {
n = ni();
v = new int[n];
for (int i = 0; i < n; ++i)
v[i] = ni();
ArrayList<Integer> l = divisors(n);
for (int divisor : l) {
if (check(divisor)) {
out.println("YES");
return;
}
}
out.println("NO");
}
void test() throws FileNotFoundException {
PrintWriter out = new PrintWriter("test");
out.println("100000");
for (int i = 0; i < 100000; ++i)
out.println(new Random().nextInt(2));
out.close();
}
public Main() throws IOException {
// test();
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader("test"));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
String ns() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int ni() throws IOException {
return Integer.valueOf(ns());
}
long nl() throws IOException {
return Long.valueOf(ns());
}
double nd() throws IOException {
return Double.valueOf(ns());
}
public static void main(String[] args) throws IOException {
new Main();
}
}
| 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;
signed main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> pr;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i > 2) {
pr.push_back(i);
}
if (i != n / i && (n / i) > 2) {
pr.push_back(n / i);
}
}
}
bool ch = false;
for (int i = 0; i < pr.size(); i++) {
for (int j = 0; j < n / pr[i]; j++) {
int ss = 0;
for (int k = j; k < n; k += (n / pr[i])) {
ss += a[k];
}
if (ss == pr[i]) {
ch = true;
break;
}
}
if (ch) break;
}
if (ch)
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.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Mahmoud Aladdin
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Scanner jin, PrintWriter jout) {
int n = jin.nextInt();
boolean[] mood = new boolean[n];
for (int i = 0; i < n; i++) {
mood[i] = jin.nextInt() == 1;
}
boolean valid = false;
for (int i = 3; !valid && i <= n; i++) {
if (n % i == 0) {
int skip = n / i;
for (int j = 0; !valid && j < skip; j += 1) {
valid = true;
for (int k = j; valid && k < n; k += skip) {
valid &= mood[k];
}
}
}
}
jout.println(valid ? "YES" : "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 | '''
Created on 17.05.2011
@author: Rodion
'''
from string import find
def main():
n = int(raw_input())
a = map(int, raw_input().split())
i = 1
ok = False
while i * i <= n:
if n % i == 0:
ok = ok or check(n / i, a) or check(i, a)
i += 1
print "YES" if ok else "NO"
def check(x, a):
if len(a) / x < 3:
return False
for i in range(0, x):
p = i
while p < len(a):
if not a[p]:
break
else:
p += x
else:
return True
return False
if __name__ == '__main__':
main() | 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 sys
import string
import math
import heapq
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction
def mi(s):
return map(int, s.strip().split())
def lmi(s):
return list(mi(s))
def tmi(s):
return tuple(mi(s))
def mf(f, s):
return map(f, s)
def lmf(f, s):
return list(mf(f, s))
def js(lst):
return " ".join(str(d) for d in lst)
def jsns(lst):
return "".join(str(d) for d in lst)
def line():
return sys.stdin.readline().strip()
def linesp():
return line().split()
def iline():
return int(line())
def mat(n):
matr = []
for _ in range(n):
matr.append(linesp())
return matr
def matns(n):
mat = []
for _ in range(n):
mat.append([c for c in line()])
return mat
def mati(n):
mat = []
for _ in range(n):
mat.append(lmi(line()))
return mat
def pmat(mat):
for row in mat:
print(js(row))
def factorize(n):
factors = []
i = 2
while i*i <= n:
while n % i == 0:
factors.append(i)
n //= i
i += 1
if n > 1:
factors.append(n)
return factors
def div(n):
ans = []
for i in range(2, n):
if n % i == 0:
ans.append(i)
return ans
def prime_lst(n):
primes = [True for _ in range(n + 1)]
primes[0] = False
primes[1] = False
for i in range(2, n + 1):
if i * i > n:
break
if primes[i]:
for j in range(i * i, n + 1, i):
primes[j] = False
p = []
for e, i in enumerate(primes):
if i:
p.append(e)
return p
def pp(arr, start, n, jump):
while True:
if n == 0:
return True
elif arr[start] == 0:
# Needed this position to be true.
return False
else:
n -= 1
start = (start + jump) % len(arr)
return False
def main():
n = iline()
arr = lmi(line())
primes = prime_lst(n)
for p in primes:
if n % p != 0:
continue
if p == 2:
p *= 2
for i in range(n // p):
if pp(arr, i, p, n // p):
print("YES")
return
print("NO")
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 | #include <bits/stdc++.h>
using namespace std;
const long oo = int(1e9);
const long N = int(1e5) + 5;
long n, a[N];
bool ok(long first) {
for (long i = 1; i <= first; i++) {
bool ok = 1;
for (long j = i; j <= n; j += first)
if (a[j] != 1) ok = 0;
if (ok) return 1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n;
for (long i = 1; i <= n; i++) cin >> a[i];
for (long i = 1; i <= n; i++)
if (n % i == 0) {
if (ok(i) && n / i >= 3) {
cout << "YES\n";
return 0;
}
if (ok(n / i) && i >= 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 | def divisors(n):
i = 2
res = set()
tmp = n
while i * i <= n:
while tmp % i == 0:
tmp //= i
res.add(i)
i += 1
if tmp != 1:
res.add(tmp)
if n % 4 == 0:
res.add(4)
res.discard(2)
return res
def main():
n = int(input())
knights = [int(c) for c in input().split()]
for p in divisors(n):
for i in range(n // p):
if all(knights[i % n] for i in range(i, i + n, n // p)):
print('YES')
return
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 | #include <bits/stdc++.h>
using namespace std;
long long fast_power(long long val, long long deg, long long mod = 1000000007) {
if (!deg) return 1 % mod;
if (deg & 1) return fast_power(val, deg - 1, mod) * val % mod;
long long res = fast_power(val, deg >> 1, mod);
return (res * res) % mod;
}
long long MMI(long long a, long long mm = 1000000007) {
return fast_power(a % mm, mm - 2, mm) % mm;
}
vector<long long> v;
long long visited[100010];
long long n, a[100010];
void divisors() {
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (n / i > 2) v.push_back(i);
if (i > 2) v.push_back(n / i);
}
}
cout << "\n";
return;
}
bool check(long long gap) {
memset(visited, 0, sizeof(visited));
for (long long i = 0; i < n; i++) {
long long cnt = 0;
if (visited[i] == 0)
for (long long j = i, cou = 0; cou < n / gap; j = (j + gap) % n, cou++) {
visited[j] = 1;
if (a[j] == 1) cnt++;
}
if (n / gap == cnt) return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
divisors();
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (auto &i : v) {
if (check(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 | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class C71 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
List<Integer> goods = new ArrayList<>();
for (int i = 0; i < n; ++i) {
if (in.nextInt() == 1) {
goods.add(i);
}
}
List<Integer> factors = new ArrayList<>();
for (int i = 3; i * i <= n; ++i) {
if (n % i != 0) {
continue;
}
int a = n / i;
if (a != i) {
factors.add(a);
}
factors.add(i);
}
if (n % 2 == 0 && n / 2 >=3) {
factors.add(2);
}
factors.add(1);
int length = factors.size();
int[][] count = new int[length][];
for (int i = 0; i < length; ++i) {
count[i] = new int[factors.get(i)];
}
for (int i : goods) {
for (int j = 0; j < length; ++j) {
int f = factors.get(j);
int mod = i % f;
count[j][mod]++;
if (count[j][mod] * f == n) {
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 | x2=[]
x3=[]
x5=False
x=input("")
x1=raw_input("")
for i in range(0,len(x1),2):
x2.append(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]=="1":
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 | #include <bits/stdc++.h>
using namespace std;
const long long eps = 998244353;
const long double inf = 1e-8;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
for (int j = 0; j < i; ++j) {
bool ind = true;
int cnt = 0;
for (int q = j; q < n; q += i) {
ind = ind && a[q];
++cnt;
}
if (ind && cnt >= 3) {
cout << "YES";
return 0;
}
}
}
}
for (int i = 3; i * i <= n; ++i) {
if (n % i == 0) {
int k = n / i;
for (int j = 0; j < k; ++j) {
bool ind = true;
int cnt = 0;
for (int q = j; q < n; q += k) {
ind = ind && a[q];
++cnt;
}
if (ind && cnt >= 3) {
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 sortinrev(pair<long long, long long> x, pair<long long, long long> y) {
return x.first > y.first;
}
void swap(long long* a, long long* b) {
long long temp = *a;
*a = *b;
*b = temp;
}
long long div_ceil(long long a, long long b) { return (a + b - 1) / b; }
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
const int MOD = 1000 * 1000 * 1000 + 7;
const long long INF = 1e18L + 5;
const int nax = 100005;
const int N = int(1e6);
void solve() {
long long n;
cin >> n;
vector<long long> arr(n + 1);
for (int i = 1; i <= n; ++i) cin >> arr[i];
vector<long long> F;
for (long long x = 1; x * x <= n; x++) {
if (n % x == 0) {
if (x != 1 && x != 2) F.push_back(x);
if (x * x != n) F.push_back(n / x);
}
}
for (int i = 0; i < F.size(); ++i) {
long long res = n / F[i];
for (long long j = 1; j <= res; j++) {
long long cnt = 0;
for (long long k = 0; k < n; k += res) {
if (arr[j + k] == 1) cnt++;
}
if (cnt == F[i]) {
cout << "YES\n";
return;
}
}
}
cout << "NO\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\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 i, n, j, k, start;
bool f, ff;
cin >> n;
vector<int> a;
a.resize(2 * n);
for (i = 0; i < n; i++) {
cin >> a[i];
a[i + n] = a[i];
}
a[n] = a[0];
k = n / 3;
i = 0;
f = false;
while (!f && i < k) {
i++;
j = 0;
if (n % i == 0) {
start = 0;
ff = false;
while (start < k && !ff) {
f = true;
j = start;
while (f && j < n + start) {
if (a[j] == 0) f = false;
j += i;
}
if (f) ff = true;
start++;
}
}
}
if (f)
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 sys
from collections import defaultdict
def uniqueFactors(N):
factors = defaultdict(list)
for i in range(3, N + 1):
if i in factors:
numList = factors[i]
for factor in numList:
factors[i + factor].append(factor)
else:
factors[i]
factors[i + i].append(i)
return factors
def solve(k, knights):
breakdown = uniqueFactors(k)
if breakdown[k]:
for factor in breakdown[k]:
for j in range(0, k//factor):
isAnswer = True
for i in range(j, len(knights), k//factor):
if knights[i] == 0:
isAnswer = False
break
if isAnswer:
return "YES"
else:
isAnswer = True
for i in range(len(knights)):
if knights[i] == 0:
isAnswer = False
break
if isAnswer:
return "YES"
return "NO"
def readinput():
n = int(sys.stdin.readline().rstrip())
knights = list(map(int, sys.stdin.readline().rstrip().split(" ")))
print(solve(n, knights))
readinput()
| 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;
long long int fast_exp(long long int base, long long int exp1) {
long long int res = 1;
while (exp1 > 0) {
if (exp1 & 1) res = (res * base) % 1000000007;
base = (base * base) % 1000000007;
exp1 /= 2;
}
return res % 1000000007;
}
long long int pr[1000001] = {0};
void isprime() {
pr[0] = 1;
pr[1] = 1;
for (long long int a = 2; a * a <= 1000001; a++) {
if (!pr[a]) {
for (long long int b = a * a; b < 1000001; b += a) {
pr[b] = 1;
}
}
}
}
long long int comp = 1, tim = 0;
struct vertex {
vector<long long int> adj;
long long int vis = 0;
long long int parent = -1;
long long int dist = 0;
long long int component = 0;
long long int in = 0;
long long int out = 0;
long long int low = 0;
long long int child = 0;
long long int indeg = 0;
long long int outdeg = 0;
bool AP = false;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
vector<long long int> v;
for (long long int a = 1; a <= (long long int)sqrt(n); a++) {
if (n % a == 0) {
v.push_back(a);
if (a * a != n and a != 1) v.push_back(n / a);
}
}
long long int x[n];
for (long long int a = 0; a < n; a++) cin >> x[a];
bool flag = false;
for (auto y : v) {
if (n / y < 3) continue;
for (long long int a = 0; a < n and !flag; a++) {
if (x[a] == 1) {
long long int b = 0;
for (long long int c = a; b < n / y and x[c] == 1; b++) {
c += y;
c %= n;
}
if (b == n / y) flag = true;
}
}
}
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 | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import static java.lang.Double.*;
import static java.math.BigInteger.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class Main implements Runnable {
// private long start;
private void solve() throws IOException {
// start = System.currentTimeMillis();
init();
readData();
processing();
}
private void init() {
}
private List<Integer> getDivisors(int n) {
List<Integer> res = new ArrayList<Integer>();
res.add(n);
if (n%2 == 0 && n > 4) {
res.add(n/2);
}
for (int i = 3; i*i <= n; ++i) {
if (n%i == 0) {
res.add(i);
res.add(n/i);
}
}
return res;
}
private boolean can(int start, int step) {
int memo = start;
if (!isFunny[start]) {
return false;
}
start += step;
while (start != memo) {
if (!isFunny[start]) {
return false;
}
start += step;
if (start >= n) {
start -= n;
}
}
return true;
}
private void processing() {
List<Integer> divs = getDivisors(n);
for (int div: divs) {
int step = n/div;
for (int start = 0; start < step; ++start) {
if (can(start, step)) {
println(YES);
return;
}
}
}
println(NO);
}
private void readData() throws IOException {
n = nextInt();
isFunny = new boolean[n];
for (int i = 0; i < n; ++i) {
int m = nextInt();
isFunny[i] = m == 1;
}
}
private int n;
private boolean[] isFunny;
private static final String YES = "YES";
private static final String NO = "NO";
public static void main(String[] args) throws InterruptedException {
new Thread(null, new Runnable() {
public void run() {
new Main().run();
}
}, "1", 1 << 24).start();
}
// @Override
public void run() {
try {
boolean onlineJudge = System.getProperty("ONLINE_JUDGE") != null;
reader = onlineJudge ? new BufferedReader(new InputStreamReader(
System.in)) : new BufferedReader(
new FileReader("input.txt"));
writer = onlineJudge ? new PrintWriter(new BufferedWriter(
new OutputStreamWriter(System.out))) : new PrintWriter(
new BufferedWriter(new FileWriter("output.txt")));
Locale.setDefault(Locale.US);
tokenizer = null;
solve();
writer.flush();
} catch (Exception error) {
error.printStackTrace();
System.exit(1);
}
}
class InputReader {
public InputReader(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 readInt() {
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 long readLong() {
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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines)
return readLine();
else
return readLine0();
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, readInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
}
private class Parser {
public Parser(InputStream in) {
din = new DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public byte c;
public int nextInt() throws IOException {
int value = 0;
c = read();
while (c <= ' ')
c = read();
boolean neg = c == '-';
if (neg)
c = read();
do {
value = value * 10 + c - 48;
c = read();
} while (c > ' ');
if (neg) {
return -value;
} else {
return value;
}
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
public byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
final private int BUFFER_SIZE = 1 << 16;
}
private BufferedReader reader;
private PrintWriter writer;
private StringTokenizer tokenizer;
private void println() {
writer.println();
}
private void print(Object obj) {
writer.print(obj);
}
private void println(Object obj) {
writer.println(obj);
}
private void printf(String f, Object... obj) {
writer.printf(f, obj);
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.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 | n=input();l=raw_input()[::2];s=set();g=0
for i in range(3,n+1):
if n%i<1 and all(i%j for j in s):s.add(i);g|=i*"1"in{l[j::n/i]for j in range(n/i)}
print"NYOE S"[g::2] | 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 | /**
* https://codeforces.com/problemset/problem/71/C
* #implementation
*/
import java.util.Scanner;
import java.util.HashSet;
import java.util.ArrayList;
public class RoundTableKnights {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> goodMood = new ArrayList<>();
for (int i = 1; i <= n; i++) {
int v = sc.nextInt();
if (v == 1) {
goodMood.add(i);
}
}
// corner case
if (goodMood.size() < 3) {
System.out.println("NO");
return;
}
HashSet<Integer> set = new HashSet<>();
goodMood.forEach(x -> set.add(x));
int maxEdgeLen = n / 3;
for (int edgeLen = 1; edgeLen <= maxEdgeLen; edgeLen++) {
if (n % edgeLen != 0) continue;
int edgeNum = n / edgeLen;
for (int i = 1; i <= edgeLen; i++) {
int cnt = 0;
if (goodMood.size() < i) break;
for (int point = goodMood.get(i - 1); set.contains(point); point += edgeLen) {
cnt++;
}
if (cnt == edgeNum) {
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;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (auto &it : v) {
cin >> it;
}
vector<int> fr;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
int x = n / i;
if (i > 2) {
fr.push_back(i);
}
if (x != i && x > 2) {
fr.push_back(x);
}
}
}
bool flag = false;
for (int i = 0; i < fr.size(); i++) {
int x = n / fr[i];
for (int j = 0; j < x; j++) {
int su = 0;
for (int k = j; k < n; k += x) {
su += v[k];
}
if (su == fr[i]) {
flag = true;
}
}
}
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 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class C071 {
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(r.readLine());
StringTokenizer st = new StringTokenizer(r.readLine());
int[] facs = facs(n);
int[] ar = new int[n];
for(int x =0;x<n;x++)
ar[x]=Integer.parseInt(st.nextToken());
for (int p:facs) {
boolean[] ad = new boolean[p];
for(int x=0;x<n;x++)
if(ar[x]==0) {
ad[x%p] = true;
}
for(int x =0;x<p;x++) {
if(!ad[x]) {
System.out.println("YES");
return;
}
}
}
System.out.println("NO");
}
static int[] facs(int a) {
ArrayList<Integer> ar = new ArrayList<Integer>();
for(int x = 1;x*2<a;x++) {
if(a%x==0) {
ar.add(x);
}
}
int[] i = new int[ar.size()];
for(int x = 0;x<ar.size();x++)
i[x]=ar.get(x);
return i;
}
}
| 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 c71c
{
private final Scanner sc;
private static final boolean debug = true;
static void debug(Object ... objects)
{
if(debug)
System.err.println(Arrays.toString(objects));
}
c71c()
{
sc = new Scanner(System.in);
}
public static void main(String [] args)
{
(new c71c()).solve();
}
void solve()
{
int n = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
boolean [] b = new boolean[n];
for(int i=0;i<s.length();i+=2)
if(s.charAt(i) == '1' || s.charAt(i) == '0')
b[i/2] = (s.charAt(i) == '1');
for(int j=3;j<=n;j++)
{
if(n%j != 0)
continue;
int diff = n/j;
for(int i=0;i<diff;i++)
{
boolean ok = true;
for(int k=i;k<n;k+=diff)
if(!b[k])
{
ok = false;
break;
}
if(ok)
{
debug(j + " " + 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;
int i, j, n, s, k, v, h, curcol = 0, n2, t, c1, fuck;
char a[100500];
bool test(int x) {
int r = n / (n / x);
for (int j = 0; j < r; j++) {
int u = j;
while (u < n && a[u] == 1) u += x;
if (u >= n) return true;
}
return false;
}
int main() {
cin >> n;
c1 = 0;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i]) c1++;
}
fuck = n;
h = (int)sqrt(n + 0.0);
for (i = 1; (n / i) > 2; i++) {
if (fuck % i == 0) {
if (test(i)) {
cout << "YES";
return 0;
} 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 | def heras(n): # retorna una lista X donde X[i] es el mayor primo que divide a i
prime_divisors = [1 for _ in range(n)]
for i in range(2, n):
if prime_divisors[i] == 1:
for j in range(i, n, i):
prime_divisors[j] = i
return prime_divisors
n = int(input())
table = [int(x) for x in input().split()]
max_prime_divisors = heras(n + 1)
sub_n = n
while sub_n > 1:
max_prime_divisor = max_prime_divisors[sub_n] # en vez de revisar todos los numeros desde n a 3, solo se consideran los divisores primos
if max_prime_divisor == 2:
if n % 4 == 0:
max_prime_divisor = 4
else:
break
jump = n // max_prime_divisor # el salto es la menor division por primo
start_positions = range(jump) # probar comenzar a contar desde la posición 0 hasta la jump - 1
for i in start_positions:
can_make_poligon = True
test_positions = range(i, n, jump) # probar cada posición de la mesa, saltando jump
for j in test_positions:
if not table[j]: # si no esta de buen humor, no funciona este caso
can_make_poligon = False
break
if can_make_poligon:
print('YES')
exit(0)
sub_n //= max_prime_divisor # obtener siguiente divisor primo
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.util.*;
public class RoundTableKnights {
/**
* @param args
*/
public static void main(String[] args) {
RoundTableKnights rtk = new RoundTableKnights();
}
RoundTableKnights(){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] knights = new int[n];
for(int i=0; i<n; i++){
knights[i] = sc.nextInt();
}
for(int i=3; i<=n; i++){
if(n % i == 0){ //正i角形が出来る可能性がある
for(int j=0; j<n/i; j++){ //この時正i角形はn/i通り考えられる
int temp=0;
for(int k=j; k<n; k+=(n/i)){
temp += knights[k];
}
if(temp == i){
System.out.println("YES");
System.exit(0);
}
//System.out.println(temp);
}
//System.out.println(i);
}
//System.out.println(i);
}
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 sys
def check(arr, head, dis, n):
if arr[head] != 1:
return 0
pos = head + dis
while pos != head:
if arr[pos] != 1:
return 0
else:
pos += dis
pos %= n
return 1
n = input();
kn = raw_input().split()
for i in range(len(kn)):
kn[i] = int(kn[i])
flag = 0
i = 1
while i * i <= n:
if (n % i == 0):
step = i
if n / step > 2:
for j in range(0, step):
if (check(kn, j, step, n) == 1):
flag = 1
break
step = n / i
if n / step > 2:
for j in range(0, step):
if (check(kn, j, step, n) == 1):
flag = 1
break
if flag == 1:
break
if flag == 1:
break
i += 1
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 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 200;
int a[N];
int main() {
int n;
cin >> n;
vector<int> v;
for (int i = 3; i <= n; i++) {
if (n % i == 0) {
v.push_back(i);
}
}
sort(v.begin(), v.end());
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i < v.size(); i++) {
int d = n / v[i];
for (int j = 1; j <= n / v[i]; j++) {
int ok = 0;
for (int k = j; k <= n; k += d) {
if (a[k] == 0) {
ok = 1;
break;
}
}
if (ok == 0) {
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.util.*;
public class RegularPolygon {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int[] arr=new int[n+1];
for(int i=1;i<=n;i++){
arr[i]=scn.nextInt();
}
int flag=1;
for(int i=1;i<=n/3;i++){
if(n%i==0){
for(int j=1;j<=i;j++){
flag=1;
for(int k=j;k<=n;k+=i){
if(arr[k]==0){
flag=0;break;
}
}
if(flag==1){
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 | num_knights = int(raw_input())
knight_disposition = [int(x) for x in raw_input().split()]
for i in range(1, num_knights):
if(num_knights%i!=0):
continue
for offset in range(i):
isregular=True
numhit=0
for j in range(offset, num_knights, i):
if knight_disposition[j] == 0:
isregular = False
break
numhit+=1
if isregular and numhit>2:
print "YES"
exit(0)
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.util.*;
import java.io.*;
public class beta65c {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = in.nextInt();
}
//int bound = (int) Math.floor(N/2);
double bound = (double)N/2;
//System.out.println(bound);
ArrayList<Integer> list = new ArrayList<>();
for (double i = 1; i < bound; i++) {
if (N % i == 0) list.add((int)(Math.floor(i)));
}
//System.out.println(list);
int bound2 = N/3;
boolean ansExists = false;
for (int i = 0; i <= bound2; i++) {
if (arr[i] == 1) {
for (Integer factor : list) {
//System.out.println(factor);
boolean isAns = true;
int bound3 = N/factor;
for (int j = 0; j < bound3; j++) {
if (arr[i + factor*j] == 0) {
isAns = false;
break;
}
}
if (isAns) {
ansExists = true;
break;
}
}
}
if (ansExists) break;
}
if (ansExists) {
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 | a=int(input(''))
b=list(map(int,input().split()))
f=0
for i in range(3,a+1):
if(a%i==0):
n=a//i
for j in range(n):
flag=True
for il in range(j,a,n):
if(b[il]==0):
flag=False
break
if flag:
f=1
break
if f:
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.util.Scanner;
public class cf_71c {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
boolean[] sides = new boolean[n];
for (int i = 0; i < sides.length; i++) {
sides[i] = in.nextInt()==1;
}
for (int i = 1; i < Math.sqrt(sides.length)+2; i++) {
if(n%i==0){
boolean works = true;
int j = n/i;
if(i>2){
//System.out.println("testing factor: " + i);
for(int k = 0; k<j;k++){
works = true;
for(int k2 = k;k2<n;k2+=j){
works = works && sides[k2];
}
if(works){
System.out.println("YES");
return;
}
}
}
if(j > 2){
//System.out.println("testing factor: " + j);
for(int k = 0; k<i;k++){
works = true;
for(int k2 = k;k2<n;k2+=i){
works = works && sides[k2];
}
if(works){
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.util.*;import java.io.*;import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni(),arr[]=new int[n+1];
for(int i=1;i<=n;i++) arr[i]=ni();
ArrayList<Integer> li = new ArrayList<>();
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i!=0)
continue;
li.add(i);
if(n/i!=i && i!=1)
li.add(n/i);
}
boolean flag=true;
//all 1's
for(int i=1;i<=n;i++){
if(arr[i]==0){
flag=false;
break;
}
}
if(flag){
pn("YES");
return;
}
for(Integer len : li){
if(n/len<=2)
continue;
for(int i=1;i<=n;i++){
if(arr[i]==0)
continue;
int st=i,j=st+len;
if(j>n) j%=n;
flag=true;
while(j!=st){
if(arr[j]==0){
flag=false;
break;
}
j+=len;
if(j>n) j%=n;
}
if(flag){//System.out.println("idx "+i+" len "+len);
pn("YES");
return;
}
}
}
pn("NO");
}
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc=new AnotherReader();
boolean oj = true;
oj = System.getProperty("ONLINE_JUDGE") != null;
if(!oj) sc=new AnotherReader(100);
long s = System.currentTimeMillis();
int t=1;
//t=ni();
while(t-->0)
process();
out.flush();
if(!oj)
System.out.println(System.currentTimeMillis()-s+"ms");
System.out.close();
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);System.out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
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 boolean multipleTC=false;
static long mod=(long)1e9+7l;
static void r_sort(int arr[],int n){
Random r = new Random();
for (int i = n-1; i > 0; i--){
int j = r.nextInt(i+1);
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Arrays.sort(arr);
}
static long mpow(long x, long n) {
if(n == 0)
return 1;
if(n % 2 == 0) {
long root = mpow(x, n / 2);
return root * root % mod;
}else {
return x * mpow(x, n - 1) % mod;
}
}
static long mcomb(long a, long b) {
if(b > a - b)
return mcomb(a, a - b);
long m = 1;
long d = 1;
long i;
for(i = 0; i < b; i++) {
m *= (a - i);
m %= mod;
d *= (i + 1);
d %= mod;
}
long ans = m * mpow(d, mod - 2) % mod;
return ans;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ 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 | a=int(input(''))
b=list(map(int,input().split()))
f=0
for i in range(3,a+1):
if(a%i==0):
n=a//i
for k in range(n):
f=1
for z in range(k,a,n):
if(b[z]==0):
f=0
break
if(f==1):
break
if(f==1):
break
if f:
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.