Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.util.*; public class RoundTable{ private static boolean[] primo = new boolean[100010]; private static void cribaErasthotenes(){ for(long i = 2; i < 100010; i++){ if(!primo[(int)i]) for(long j = i * i; j < 100010; j += i) primo[(int)j] = true; } } public static void main(String[] args){ Scanner myScanner = new Scanner(System.in); int n = myScanner.nextInt(); boolean[] mesa = new boolean[n]; for(int i = 0; i < n; i++) mesa[i] = myScanner.nextInt() == 1 ? true : false; cribaErasthotenes(); primo[4] = false; boolean result = false; for(int i = 3; i <= n; i++){ if( n % i == 0 && !primo[i]){ int t = n / i; for(int j = 0; j < t; j++){ result = true; for(int k = j; k < n; k += t) result &= mesa[k]; if(result) break; } if(result) break; } } System.out.println(result ? "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
import java.util.*; import java.io.*; //code forces 71C round table knights public class Main{ public static void main(String [] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String [] g = in.readLine().split(" "); int limit = (n+1)/2; boolean b = false; ans:for(int i = 1; i<limit; i++){ if(n%i!=0) continue; //System.out.println("test1: "+i); loop:for(int o=0; o<i; o++){ int c = 0; //System.out.println("test2: "+o); while(c!=n){ if(g[(c+o)%n].equals("0")){ continue loop; } //System.out.println((c+o)%n); c+=i; } b = true; break ans; } } if(b) System.out.println("YES"); else System.out.println("NO"); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; /** * Created by Tejas on 21-07-2018. */ public class Main { static int a[]; static ArrayList<Integer>div=new ArrayList<>(); public static void main(String[]args)throws IOException{ BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in)); StringBuilder stringBuilder=new StringBuilder(); String temp[]; int N=Integer.parseInt(bufferedReader.readLine()); a=new int[N]; temp=bufferedReader.readLine().split(" "); for (int i = 0; i < N; i++) a[i]=Integer.parseInt(temp[i]); System.out.println(solve(N)); } private static String solve(int N) { getDivisors(N); for(int x: div){ if(x<=N/3) { for (int i = 0; i < x; i++) { if(a[i]==1){ boolean flag=true; for (int j = i; j <N ; j=j+x) { if(a[j]==0) { flag = false; j=N; } } if(flag) return "YES"; } } } } return "NO"; } public static void getDivisors(int N) { for (int i = 1; i <=Math.sqrt(N) ; i++) { if(N%i==0) { div.add(i); if(i!=N/i) div.add(N/i); } } Collections.sort(div); } }
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 RoundTableKnights { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),temp=n; ArrayList<Integer>primes=new ArrayList<Integer>(); for(int i=3;i<=temp;i++){ if(temp%i==0) primes.add(i); while(temp%i==0) temp/=i; } boolean[][]dp=new boolean[n][primes.size()]; for(int i=0;i<n;i++) if(sc.nextInt()==0) for(int j=0;j<primes.size();j++) dp[i][j]=true; else for(int j=0;j<primes.size();j++) if(i>=n/primes.get(j)) dp[i][j]=dp[i-n/primes.get(j)][j]; sc.close(); boolean lucky=false; for(int j=0;j<primes.size();j++) for(int i=n-n/primes.get(j);i<n;i++) if(!dp[i][j]) lucky=true; System.out.println(lucky?"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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ 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 { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.readInt(); int[] a = in.readIntArray(n); ArrayList<Integer> divisors = new ArrayList<>(); for (int i = 1; i <= n; i++) { if (n % i == 0) divisors.add(i); } for (int i : divisors) { if (n / i < 3) continue; for (int j = 0; j < i; j++) { int sum = 0; for (int k = 0; k < n; k += i) { sum += a[k + j]; } if (sum == n / i) { out.println("YES"); return; } } } out.println("NO"); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] readIntArray(int size) { int[] ans = new int[size]; for (int i = 0; i < size; i++) ans[i] = readInt(); return ans; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
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 CF; import java.io.BufferedReader; import java.io.FileReader; 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.StringTokenizer; public class B { static int [] a; static boolean calc(int div) { boolean flag = false; if(a.length/div < 3) return false; for (int i = 0; i < a.length && !flag; i++) { if(a[i] == 0) continue; boolean tmp = true; for (int j = i, k = 0; k < a.length && tmp; j=(j+div)%a.length, k++) { if(a[j] == 0) tmp = false; } flag |= tmp; } return flag; } public static void main(String[] args) throws Exception { Scanner bf = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = bf.nextInt(); a = new int[n]; for (int i = 0; i < a.length; i++) { a[i] = bf.nextInt(); } boolean ans = false; for (int i = 1; 1L*i*i <= n && !ans; i++) { if(n % i != 0) continue; ans |= calc(i); ans |= calc(n/i); } out.println(ans?"YES":"NO"); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader fileReader) { br = new BufferedReader(fileReader); } 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(); } } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class Solution71C { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE")!=null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException{ if (ONLINE_JUDGE){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); }else{ in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } } String readString() throws IOException{ while(!tok.hasMoreTokens()){ tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } public static void main(String[] args){ new Solution71C().run(); } public void run(){ try{ long t1 = System.currentTimeMillis(); init(); solve(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = "+(t2-t1)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } static class Utils { private Utils() {} public static void mergeSort(int[] a) { mergeSort(a, 0, a.length - 1); } private static void mergeSort(int[] a, int leftIndex, int rightIndex) { final int MAGIC_VALUE = 50; if (leftIndex < rightIndex) { if (rightIndex - leftIndex <= MAGIC_VALUE) { insertionSort(a, leftIndex, rightIndex); } else { int middleIndex = (leftIndex + rightIndex) / 2; mergeSort(a, leftIndex, middleIndex); mergeSort(a, middleIndex + 1, rightIndex); merge(a, leftIndex, middleIndex, rightIndex); } } } private static void merge(int[] a, int leftIndex, int middleIndex, int rightIndex) { int length1 = middleIndex - leftIndex + 1; int length2 = rightIndex - middleIndex; int[] leftArray = new int[length1]; int[] rightArray = new int[length2]; System.arraycopy(a, leftIndex, leftArray, 0, length1); System.arraycopy(a, middleIndex + 1, rightArray, 0, length2); for (int k = leftIndex, i = 0, j = 0; k <= rightIndex; k++) { if (i == length1) { a[k] = rightArray[j++]; } else if (j == length2) { a[k] = leftArray[i++]; } else { a[k] = leftArray[i] <= rightArray[j] ? leftArray[i++] : rightArray[j++]; } } } private static void insertionSort(int[] a, int leftIndex, int rightIndex) { for (int i = leftIndex + 1; i <= rightIndex; i++) { int current = a[i]; int j = i - 1; while (j >= leftIndex && a[j] > current) { a[j + 1] = a[j]; j--; } a[j + 1] = current; } } } void solve() throws IOException{ int n = readInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = readInt(); int max = n / 3; for(int i = 1; i <= max; i++){ if(n % i == 0){ for(int j = 0; j < i; j++){ boolean check = true; for(int k = j; k < n; k += i) if(a[k] == 0) check = false; if(check){ out.println("YES"); return; } } } } out.println("NO"); } static double distance(long x1, long y1, long x2, long y2){ return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } static long gcd(long a, long b){ while(a != b){ if(a < b) a -=b; else b -= a; } return a; } static long lcm(long a, long b){ return a * b /gcd(a, b); } }
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 arr[100001]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } int flag = 0; int cnt; for (int i = 0; i < n; i++) { while (arr[i] != 1) i++; for (int d = 1; d <= n / 2; d++) { if (n % d != 0) continue; cnt = 1; for (int k = i + d;; k = (k + d) % n) { if (arr[k] != 1) break; else if (k == i) { if (cnt > 2) flag = 1; break; } cnt++; } if (flag == 1 && cnt > 2) { break; } } if (flag == 1 && cnt > 2) { break; } } if (flag == 1 && cnt > 2) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> const int md = 1e9 + 7; const long long hs = 199; using namespace std; int n, a[100000]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 3; i <= n; i++) { if (n % i == 0) { for (int j = 0; j < n / i; j++) { bool good = true; for (int k = j; k < n; k += n / i) good = good && (a[k] == 1); if (good) return cout << "YES" << '\n', 0; } } } return cout << "NO" << '\n', 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author dipankar12 */ import java.io.*; import java.util.*; public class r71c { public static void main(String args[]) { fastio in=new fastio(System.in); PrintWriter pw=new PrintWriter(System.out); int n=in.nextInt(); BitSet good=new BitSet(n); for(int i=0;i<n;i++) if(in.nextInt()==1) good.set(i); //pw.println(good); ArrayList<Integer> divs=new ArrayList<Integer>(); for(int i=1;i*i<=n;i++) if(n%i==0) { divs.add(i); if(n/i!=i) divs.add(n/i); } //pw.println(divs); boolean yes=false; for(int d:divs) { if(n/d<3) continue; BitSet g=new BitSet(d); g.set(0,d,true); //pw.println(g+" "+d); for(int i=0;i<n;i++) if(!good.get(i)) g.clear(i%d); //pw.println("g is now "+g); yes|=g.cardinality()>0; } pw.println(yes?"YES":"NO"); pw.close(); } static class fastio { private final InputStream stream; private final byte[] buf = new byte[8192]; private int cchar, snchar; private SpaceCharFilter filter; public fastio(InputStream stream) { this.stream = stream; } public int nxt() { if (snchar == -1) throw new InputMismatchException(); if (cchar >= snchar) { cchar = 0; try { snchar = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snchar <= 0) return -1; } return buf[cchar++]; } public int nextInt() { int c = nxt(); while (isSpaceChar(c)) { c = nxt(); } int sgn = 1; if (c == '-') { sgn = -1; c = nxt(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = nxt(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = nxt(); while (isSpaceChar(c)) { c = nxt(); } int sgn = 1; if (c == '-') { sgn = -1; c = nxt(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = nxt(); } 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 = nxt(); while (isSpaceChar(c)) { c = nxt(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nxt(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = nxt(); while (isSpaceChar(c)) c = nxt(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nxt(); } 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); } } }
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.*; public class C { private void solve() throws IOException { int n = Integer.parseInt(stdin.readLine()); StringTokenizer o = new StringTokenizer(stdin.readLine()); boolean[] b = new boolean[n]; for (int i = 0; i < n; i++) { int r = Integer.parseInt(o.nextToken()); if (r == 1) b[i] = true; } for (int d = 3; d <= n; d++) { if (n%d == 0) { int k = n/d; end : for (int i = 0; i < k; i++) { for (int j = i; j < n; j += k) if (!b[j]) continue end; out.println("YES"); out.flush(); return; } } } out.println("NO"); out.flush(); } public static void main(String[] args) throws IOException { new C().run(); } BufferedReader stdin; PrintWriter out; public void run() throws IOException { stdin = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); stdin.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 MOD = 1000000007; const long long INFLL = 1e18; const int INF = 1e9; const int NMAX = 1000001; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int v[NMAX]; int first; bool check(int n, int x) { int curr = first; int ct = 0; for (int i = 1; i <= x; ++i) { bool flag = true; for (int j = i; j <= n; j += x) { if (v[j] == 0) { flag = false; break; } } if (flag && n / x > 2) return true; } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> v[i]; if (!first) first = i; } for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { if (check(n, i) || check(n, 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; const int N = 2e5 + 10; const int M = 1e9 + 7; const int MOD = 998244353; const double PI = 3.141592653589793238460; long long int power(long long int a, long long int b) { long long int res = 1; if (a == 0) return 0; if (a == 1) return 1; for (; b > 0; b >>= 1) { if (b & 1) res = (res * a); if (res > MOD) res %= MOD; a = (a * a); if (a > MOD) a = a % MOD; } return res; } bool isPrime[10001]; vector<int> prime; void seive() { isPrime[0] = isPrime[1] = 1; for (int i = 2; i * i <= 10000; i++) { if (!isPrime[i]) for (int j = i * i; j <= 10000; j += i) { isPrime[j] = 1; } } for (int i = 2; i <= 10000; i++) if (!isPrime[i]) prime.push_back(i); } int main() { int n; cin >> n; int arr[n]; int flag = 1; for (int i = 0; i < n; i++) { cin >> arr[i]; if (!arr[i]) flag = 0; } if (flag) { cout << "YES"; return 0; } int diff = 2; for (int diff = 2; diff <= n / 3 + 1; diff++) { if (n % diff != 0) continue; for (int j = 0; j < diff; j++) { int cnt = 0, last; flag = 1; for (int i = j; i < n; i += diff) { if (arr[i] == 1) cnt++, last = i; else { flag = 0; break; } } if (n - last + j != diff) flag = 0; if (flag && cnt > 2) { cout << "YES"; return 0; } else if (flag) { cout << "NO"; 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
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long T, n, m, k, x, y, z, l, r, ans, spf[N], a[N], mod = 1e9 + 7; int f() { for (int i = 1; i <= n / 3; i++) { if (n % i == 0) { for (int j = 1; j <= i; j++) { int s = 1; for (int k = j; k <= n; k += i) s &= a[k]; if (s) return 1; } } } return 0; } void solve() { cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; if (count(a + 1, a + n + 1, 1) == n || f()) cout << "YES"; else cout << "NO"; } int main() { solve(); }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.util.*; import java.io.*; import java.math.*; public class JavaApplication48 { static void printDivisors(int n) { // Note that this loop runs till square root for (int i = 1; i <= Math.sqrt(n); i++) { if (n % i == 0) { // If divisors are equal, print only one if (n / i == i) { a.add(i); } else { // Otherwise print both a.add(i); a.add(n/i); } } } } public static ArrayList<Integer> a = new ArrayList<Integer>(); public static void main(String[] args) { Scanner inp = new Scanner(System.in); int times = inp.nextInt(); boolean[] b = new boolean[times]; for (int time = 0; time < times; time++) { if (inp.nextInt() == 1) { b[time] = true; } } printDivisors(times); //System.out.println(a); int les = times/3; for (int v : a){ if (v > les){ continue; } if (yes(b,v,times)){ System.out.println("YES"); return; } } System.out.println("NO"); } public static boolean yes(boolean[] b, int v, int t){ for (int i = 0; i < v; i++) { boolean x = true; for (int j = i; j < t; j = j + v) { if (b[j] == false){ x = false; //System.out.print(b[j] + " " + j); //System.out.println(""); break; } } //System.out.println(""); if (x){ // System.out.println(i + ":" + v + " --> " + b[i+v]); 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; long long n; long long a[100002]; bool chk(long long p, long long d) { long long rq = n / d; if (rq <= 2 || !a[p]) return 0; for (long long i = p + d; i != p; i = (i + d) % n) { if (a[i] != 1) return 0; } return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (long long i = 0; i < n; i++) cin >> a[i]; set<long long> s; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { s.insert(n / i); s.insert(i); } } for (auto it : s) { for (long long i = 0; i < it; i++) { if (chk(i, it)) return cout << "YES", 0; } } cout << "NO"; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.util.*; import java.io.*; public class Main{ BufferedReader in; StringTokenizer str = null; private int nextInt() throws Exception{ if (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return Integer.parseInt(str.nextToken()); } int n, st; int a[]; public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); n = nextInt(); a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); for(int i=3;i<=n;i++){ if (n % i == 0){ boolean ok = true; int step = n/i; for(int j=0;j<step;j++){ ok = true; for(int k=j;k<n;k+=step) if (a[k] == 0){ ok =false; break; } if (ok){ System.out.println("YES"); return; } } } } System.out.println("NO"); } public static void main(String []args) throws Exception{ new Main().run(); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int ar[100010]; int gcd(int a, int b) { if (a > b) return gcd(b, a); if (a == 0) return b; return gcd(b % a, a); } int n; bool isit(int k) { int len = k; int len2 = n / k; for (int t = 0; t < len; t++) { int p = t; int cnt = 0; bool ok = true; while (cnt < len2) { if (!ar[p]) ok = false; cnt++; p = (p + k) % n; } if (ok) return true; } return false; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", ar + i); } int sn = sqrt(n); bool ret = false; if (isit(1)) ret = true; else for (int j = 2; j <= sn; j++) { if (n % j == 0) { if ((n / j) >= 3 && isit(j)) ret = true; j = n / j; if ((n / j) >= 3 && isit(j)) ret = true; j = n / j; } } if (ret) puts("YES"); else puts("NO"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') def get_primes(n: int): from itertools import chain from array import array primes = [3, 4] is_prime = (array('b', (0, 0, 1, 1, 0, 1, 0)) + array('b', (1, 0, 0, 0, 1, 0)) * ((n - 1) // 6)) for i in chain.from_iterable((range(5, n + 1, 6), range(7, n + 1, 6))): if is_prime[i]: primes.append(i) for j in range(i * 3, n + 1, i * 2): is_prime[j] = 0 return is_prime, primes n = int(input()) a = list(map(int, input().split())) if all(a): print('YES') exit() _, primes = get_primes(n) for p in primes: if n % p != 0: continue m = n // p for i in range(m): if all(a[i::m]): 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
from sys import stdin from sys import exit from math import sqrt #parser def parser(): return map(int, stdin.readline().split()) #Comprobar si el número de entrada es primo o es 4 def IsPrime(t): if t<3: return False if t==4: return True for i in range(2,int(sqrt(t))+1): if t%i==0: return False return True #Comprobar existencia de un polígono regular de ''number_of_sides'' lados empezando desde ''begin_pos'' def rp_found_begin_pos(side_length,number_of_sides,begin_pos): pos=begin_pos while number_of_sides!=0: #comprobando si un caballero esta de mal humor if not knights_mood[pos]: return False pos+=side_length number_of_sides-=1 return True #Comprobar existencia de un polígono regular de ''number_of_sides'' lados def rp_found(side_length,number_of_sides): for i in range(side_length): if rp_found_begin_pos(side_length,number_of_sides,i): return True return False #Recibiendo el valor de n n=int(stdin.readline()) #Recibiendo los estados de humor de los caballeros knights_mood=[x for x in parser()] #Lista para guardar los divisores primos de n mayores que 2 y el 4 si este divide a n con sus respectivas parejas divisors=[] #Hallando raíz cuadrada de n n_sqrt=int(sqrt(n)) #Recorriendo los números enteros desde 1 hasta raíz cuadrada de n for i in range(1,n_sqrt+1): if n % i == 0: i_couple=int(n/i) #Comprobar si i y su pareja son primos o 4 y en caso de que alguno lo sea, agregarlo con su respectiva pareja a la lista ''divisors'' if IsPrime(i): divisors.append((i,i_couple)) if IsPrime(i_couple): divisors.append((i_couple,i)) #Recorriendo las parejas de divisores encontradas for pair in divisors: first_divisor=pair[0] second_divisor=pair[1] #Comprobando si existe un polígono regular tal que el número de lados es un número primo o es 4 if 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
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 5; bool m[maxn]; int a[maxn]; int main() { int n, sum = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } if (n == sum) { cout << "YES" << endl; return 0; } bool flag = false; for (int t = 3; t <= n; t++) if (n % t == 0) { memset(m, 0, sizeof(m)); for (int i = 1; i <= n / t; i++) { if (a[i] == 1) { for (int j = i; j <= n; j += n / t) if (a[i] + a[j] != 2) m[i] = 1; } else m[i] = 1; } int cnt = 0; for (int i = 1; i <= n / t; i++) if (!m[i]) cnt++; if (cnt) flag = true; } if (flag) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#!/usr/bin/python import sys import math if __name__=="__main__": data = sys.stdin.readlines() n = int(data[0]) li = [int(v) for v in data[1].split(' ')] for v in li: if v == 0: break else: print "YES" exit() li += li steps = [] for i in range(n-1, 1, -1): if n % i == 0 and n / i > 2: steps.append(i) #n /= i res = False for step in steps: for i in range(0,step): start = i j = i while j < start + n: if li[j] == 0: break j += step else: res = True break if res: 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
n = int(input()) sr = [int(i) for i in input().split()] v = 0 for sh in range(1, int(len(sr) / 3) + 1): if len(sr) % sh == 0: for j in range(1, sh + 1): for i in range(j, len(sr), sh): if sr[i] == 0: break else: v = 1 if v == 1: break if v == 1: print('YES') else: print('NO')
PYTHON3
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int arr[100001]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } int flag = 0; int cnt; for (int i = 0; i <= n / 3; i++) { while (arr[i] != 1) i++; for (int d = 1; d <= n / 3; d++) { if (n % d != 0) continue; cnt = 1; for (int k = i + d;; k = (k + d) % n) { if (arr[k] != 1) break; else if (k == i) { if (cnt > 2) flag = 1; break; } cnt++; } if (flag == 1 && cnt > 2) { break; } } if (flag == 1 && cnt > 2) { break; } } if (flag == 1 && cnt > 2) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int n; const int N = 200001; bool bs[200001]; vector<int> p; vector<int> sol; vector<int> g; bool vis[200001]; void sieve() { memset(bs, true, sizeof bs); bs[0] = bs[1] = false; for (int i = 2; i * i < N; i++) if (bs[i]) for (int j = i * i; j < N; j += i) bs[j] = false; bs[2] = false; bs[4] = true; for (int i = 0; i < N; i++) if (bs[i]) p.push_back(i); } void primeFact(int x) { sol.clear(); int freq; for (int i = 0; i < p.size() && p[i] <= x; i++) { freq = 0; while (x % p[i] == 0) { ++freq; x /= p[i]; } if (freq != 0) sol.push_back(p[i]); } if (x != 1 && x != 2) sol.push_back(x); } vector<int> pos; bool trY(int x) { pos.clear(); int r; for (int i = 0, cnt = 0; i < n && cnt < x; i += n / x, ++cnt) { pos.push_back(i); r = i; vis[r] = true; } while (r < n) { bool flag = true; for (int i = 0; i < pos.size(); ++i) { if (g[pos[i]] == 0) { flag = false; break; } } if (flag) return true; for (int i = 0; i < pos.size(); ++i) { pos[i]++; if (pos[i] == false) return false; r = pos[i]; vis[pos[i]] = false; } } return false; } int main() { sieve(); scanf("%d", &n); for (int i = 0, tmp; i < n; ++i) { scanf("%d", &tmp); g.push_back(tmp); } primeFact(n); for (int i = 0; i < sol.size(); ++i) { memset(vis, false, sizeof vis); if (trY(sol[i])) { puts("YES"); return 0; } } puts("NO"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import math n=input() arr=map(int,raw_input().split()) a=0 s=0 for i in arr: if i==0: s+=1 else: a=max(a,s) s=0 a=max(a,s) p=3 new=[] ver=[] for i in range(1,int(math.sqrt(n))+1): if n%i==0: new.append(i) new.append(n/i) for h in range(len(new)): ans=new[h] #print ans if n/ans<3: continue i=0 for i in range(0,ans): q=True for j in range(i,n,ans): q&=arr[j] if (q): print 'YES' exit() 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 M = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n, i, j, k, ans; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; vector<int> fact; for (i = 1; i * i < n; i++) { if (n % i == 0) { fact.emplace_back(i); fact.emplace_back(n / i); } } if (i * i == n) v.emplace_back(i); sort(fact.begin(), fact.end()); int flag = 0; for (auto it = fact.begin(); it != fact.end(); it++) { if (*it > n / 3) break; k = *it; for (int i = 0; i < k; i++) { int foo = 1; for (j = i; j < n; j += k) { if (!v[j]) { foo = 0; break; } } if (foo) { flag = 1; break; } } if (flag) break; } if (flag) cout << "YES\n"; else cout << "NO\n"; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
//package round65; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static String nextToken() throws IOException{ while (st==null || !st.hasMoreTokens()){ String s = bf.readLine(); if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } static int nextInt() throws IOException{ return Integer.parseInt(nextToken()); } static String nextStr() throws IOException{ return nextToken(); } static double nextDouble() throws IOException{ return Double.parseDouble(nextToken()); } static boolean prime(int n){ for (int i=2; i*i<=n; i++) if (n%i == 0) return false; return true; } public static void main(String[] args) throws IOException{ int n = nextInt(); int s[] = new int[n]; for (int i=0; i<n; i++) s[i] = nextInt(); for (int i=3; i<=n; i++) if (n%i==0){ int p = n/i; int a[] = new int[p]; for (int j=0; j<s.length; j++) if (s[j] == 1) a[j%p]++; for (int j=0; j<p; j++) if (a[j] == i){ out.println("YES"); out.flush(); return; } } out.println("NO"); out.flush(); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
from sys import stdin 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 #Comprobar existencia de un poligono regular 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
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.math.BigInteger; import java.util.Arrays; import java.util.Scanner; /** * * @author dody */ public class ProbC { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); String table = scanner.nextLine(); table = table.replaceAll(" ", ""); int sum = 0; for(int i = 0; i< table.length(); i++){ sum += table.charAt(i)-'0'; } if(sum == table.length()){ System.out.println("YES"); return; } // for(int i =0; i<table.length();i++) boolean [] sol = new boolean[n/3+1]; Arrays.fill(sol, true); sol[0] = false; for(int i = 2; i <= n/3; i++){ if(n % i != 0 || !sol[i]){ sol[i] = false; continue; } sol[i] = check(table,i); if(sol[i]){ System.out.println("YES"); return; } for(int j = 2; j * i < sol.length; j++){ sol[j] = false; } } System.out.println("NO"); } private static boolean check(String table, int i) { for(int start = 0; start < i; start++){ boolean ret = true; for(int j = start; j<table.length(); j+= i){ if(table.charAt(j) == '0') ret = false; } if(ret) 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
def foo(listSize,list): for subSize in range(1,listSize+1): if listSize%subSize==0: parts=listSize/subSize if parts ==1: _sum = sum(list) if (listSize == _sum): return "YES" if parts >2: for i in range(subSize): _sum = 0 for index in range(i,listSize,subSize): _sum =_sum+list[index] if parts == _sum: return "YES" return "NO" if __name__ == '__main__': n = input() n = int(n) l = [int(x) for x in input().split()] print(foo(n,l))
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.*; public class tr { public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(); int []a=new int[n]; int ones=0; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(a[i]==1) ones++; } int g=(int)Math.sqrt(n); TreeSet<Integer>ts=new TreeSet(); for(int i=1;i<=g;i++) { if(n%i==0) { ts.add(i); ts.add(n/i); } } boolean f=false; ALL: for(int k:ts) { if(k>=3 && k<=ones) { boolean []vis=new boolean [n]; for(int i=0;i<n;i++) {int i2=i; int c=0; if(vis[i]) break; if(a[i]==1) { c=1; vis[i]=true;//System.out.println(i); for(int j=0;j<k-1;j++) { i+=n/k; if(i>n-1) { i-=n; } if(a[i]==1) { c++; vis[i]=true; } if(a[i]==0) break; } } if(c==k) { f=true; break ALL; } i=i2; } } } // System.out.println(ts); if(f) out.println("YES"); else out.println("NO"); out.flush(); } static int gcd(int a,int b) { if(a==0) return b; if(b==0) return a; if(a>b) return gcd(b,a%b); return gcd(a,b%a); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
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, os, math def er(k): a = [True for i in range(k + 1)] a[0] = a[1] = False global p p = [2] m = 2 while m < k: for i in range(k // m): a[(i + 1) * m] = False a[m] = True i = m + 1 while (not a[i]) & (i < k): i = i + 1 if i < k: m = i p.append(m) else: m = k + 1 def lucky(string): global p n = len(string) for num in p: if (num > n): return 0 elif (n % num == 0): for i in range(n // num): if sum(list(map(int,string[i::n//num])))==num: return 1 p = [2] er(100000) p.insert(2,4) p.remove(2) n = map(int, input()) st = input().replace(" ", "") if lucky(st): 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.ArrayList; import java.util.Arrays; import java.util.HashSet; /** * Created by IntelliJ IDEA. * User: piyushd * Date: 3/18/11 * Time: 9:28 PM * To change this template use File | Settings | File Templates. */ public class TaskC { void run(){ int n = nextInt(); int[] a = new int[n]; ArrayList<Integer> bad = new ArrayList<Integer>(); for(int i = 0; i < n; i++){ a[i] = nextInt(); if(a[i] == 0) bad.add(i); } ArrayList<Integer> factors = new ArrayList<Integer>(); for(int i = 1; i <= (n / 3); i++) if(n % i == 0) factors.add(i); Integer[] f = new Integer[factors.size()]; factors.toArray(f); HashSet<Integer> set = new HashSet<Integer>(); int low = 0, high = f.length; boolean got = false; while(high - low >= 1){ int mid = low + (high - low) / 2; int fac = f[mid]; set.clear(); for(int x : bad) set.add(x % fac); if(set.size() < fac){ got = true; break; } low = mid + 1; } if (got) { System.out.println("YES"); } else { System.out.println("NO"); } } int nextInt(){ try{ int c = System.in.read(); if(c == -1) return c; while(c != '-' && (c < '0' || '9' < c)){ c = System.in.read(); if(c == -1) return c; } if(c == '-') return -nextInt(); int res = 0; do{ res *= 10; res += c - '0'; c = System.in.read(); }while('0' <= c && c <= '9'); return res; }catch(Exception e){ return -1; } } long nextLong(){ try{ int c = System.in.read(); if(c == -1) return -1; while(c != '-' && (c < '0' || '9' < c)){ c = System.in.read(); if(c == -1) return -1; } if(c == '-') return -nextLong(); long res = 0; do{ res *= 10; res += c-'0'; c = System.in.read(); }while('0' <= c && c <= '9'); return res; }catch(Exception e){ return -1; } } double nextDouble(){ return Double.parseDouble(next()); } String next(){ try{ StringBuilder res = new StringBuilder(""); int c = System.in.read(); while(Character.isWhitespace(c)) c = System.in.read(); do{ res.append((char)c); }while(!Character.isWhitespace(c=System.in.read())); return res.toString(); }catch(Exception e){ return null; } } String nextLine(){ try{ StringBuilder res = new StringBuilder(""); int c = System.in.read(); while(c == '\r' || c == '\n') c = System.in.read(); do{ res.append((char)c); c = System.in.read(); }while(c != '\r' && c != '\n'); return res.toString(); }catch(Exception e){ return null; } } public static void main(String[] args){ new TaskC().run(); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import sys def main(): lines = [i.rstrip() for i in sys.stdin.readlines()] nums = [int(i) for i in lines[1].split(" ")] size = len(nums) for i in range(1, (size + 1) / 2): if (size % i != 0): continue for j in range(i): for k in range(size / i): idx = (j + k * i) % size if (nums[idx] == 0): break else: return "YES" return "NO" if __name__ == '__main__': print 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 java.io.*; import java.util.*; import java.lang.*; public class Rextester{ public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); br.close(); int[] array = new int[n]; for(int i=0;i<n;i++){ array[i]=Integer.parseInt(st.nextToken()); } ArrayList<Integer> divisors = new ArrayList<Integer>(); for(int i=3;i<=n;i++){ if(n%i==0){ divisors.add(i); } } boolean flag = false; outer:for(int i=0;i<divisors.size();i++){ int x = divisors.get(i); int dif = n/x; int[] dp = new int[n]; for(int j=0;j<n;j++){ if(j<dif){ dp[j]=array[j]; } else{ dp[j]=dp[j-dif]+array[j]; if(dp[j]==x){ flag=true; break outer; } } } } if(flag){ System.out.println("YES"); } else{ System.out.println("NO"); } } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> int n, a[100005]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n / 3; i++) { if (n % i == 0) { for (int j = 1; j <= i; j++) { int flag = 1; for (int k = j; k <= n; k += i) if (!a[k]) flag = 0; if (flag) { puts("YES"); return 0; } } } } puts("NO"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
/* * Hello! You are trying to hack my solution, are you? =) * Don't be afraid of the size, it's just a dump of useful methods like gcd, or n-th Fib number. * And I'm just too lazy to create a new .java for every task. * And if you were successful to hack my solution, please, send me this test as a message or to [email protected]. * It can help me improve my skills and i'd be very grateful for that. * Sorry for time you spent reading this message. =) * Good luck, unknown rival. =) * */ import java.io.*; import java.math.*; import java.util.*; public class Abra { // double d = 2.2250738585072012e-308; void solve() throws IOException { int n = nextInt();br.readLine(); boolean[] a = new boolean[n]; String s = br.readLine(); for (int i = 0; i < n; i++) { a[i] = s.charAt(i * 2) == '1' ? true : false; } for (int i = 3; i <= n; i++) { if (n % i != 0) continue; boolean t = true; for (int j = 0; j < n / i; j++) { int c = j; t = true; while (c < n && t) { t &= a[c]; c += n / i; } if (t) break; } if (t) { out.println("YES"); return; } } out.println("NO"); } public static void main(String[] args) throws IOException { new Abra().run(); } StreamTokenizer in; PrintWriter out; boolean oj; BufferedReader br; void init() throws IOException { oj = System.getProperty("ONLINE_JUDGE") != null; Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("input.txt"); Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("output.txt"); br = new BufferedReader(reader); in = new StreamTokenizer(br); out = new PrintWriter(writer); } long beginTime; void run() throws IOException { beginTime = System.currentTimeMillis(); long beginMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); init(); solve(); long endMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long endTime = System.currentTimeMillis(); if (!oj) { System.out.println("Memory used = " + (endMem - beginMem)); System.out.println("Total memory = " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())); System.out.println("Running time = " + (endTime - beginTime)); } out.flush(); } int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } long nextLong() throws IOException { in.nextToken(); return (long) in.nval; } String nextString() throws IOException { in.nextToken(); return in.sval; } double nextDouble() throws IOException { in.nextToken(); return in.nval; } myLib lib = new myLib(); void time() { System.out.print("It's "); System.out.println(System.currentTimeMillis() - beginTime); } static class myLib { long fact(long x) { long a = 1; for (long i = 2; i <= x; i++) { a *= i; } return a; } long digitSum(String x) { long a = 0; for (int i = 0; i < x.length(); i++) { a += x.charAt(i) - '0'; } return a; } long digitSum(long x) { long a = 0; while (x > 0) { a += x % 10; x /= 10; } return a; } long digitMul(long x) { long a = 1; while (x > 0) { a *= x % 10; x /= 10; } return a; } int digitCubesSum(int x) { int a = 0; while (x > 0) { a += (x % 10) * (x % 10) * (x % 10); x /= 10; } return a; } double pif(double ax, double ay, double bx, double by) { return Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)); } double pif3D(double ax, double ay, double az, double bx, double by, double bz) { return Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by) + (az - bz) * (az - bz)); } double pif3D(double[] a, double[] b) { return Math.sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + (a[2] - b[2]) * (a[2] - b[2])); } long gcd(long a, long b) { if (a == 0 || b == 0) return 1; if (a < b) { long c = b; b = a; a = c; } while (a % b != 0) { a = a % b; if (a < b) { long c = b; b = a; a = c; } } return b; } int gcd(int a, int b) { if (a == 0 || b == 0) return 1; if (a < b) { int c = b; b = a; a = c; } while (a % b != 0) { a = a % b; if (a < b) { int c = b; b = a; a = c; } } return b; } long lcm(long a, long b) { return a * b / gcd(a, b); } int lcm(int a, int b) { return a * b / gcd(a, b); } int countOccurences(String x, String y) { int a = 0, i = 0; while (true) { i = y.indexOf(x); if (i == -1) break; a++; y = y.substring(i + 1); } return a; } int[] findPrimes(int x) { boolean[] forErato = new boolean[x - 1]; List<Integer> t = new Vector<Integer>(); int l = 0, j = 0; for (int i = 2; i < x; i++) { if (forErato[i - 2]) continue; t.add(i); l++; j = i * 2; while (j < x) { forErato[j - 2] = true; j += i; } } int[] primes = new int[l]; Iterator<Integer> iterator = t.iterator(); for (int i = 0; iterator.hasNext(); i++) { primes[i] = iterator.next().intValue(); } return primes; } int rev(int x) { int a = 0; while (x > 0) { a = a * 10 + x % 10; x /= 10; } return a; } class myDate { int d, m, y; int[] ml = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public myDate(int da, int ma, int ya) { d = da; m = ma; y = ya; if ((ma > 12 || ma < 1 || da > ml[ma - 1] || da < 1) && !(d == 29 && m == 2 && y % 4 == 0)) { d = 1; m = 1; y = 9999999; } } void incYear(int x) { for (int i = 0; i < x; i++) { y++; if (m == 2 && d == 29) { m = 3; d = 1; return; } if (m == 3 && d == 1) { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) { m = 2; d = 29; } return; } } } boolean less(myDate x) { if (y < x.y) return true; if (y > x.y) return false; if (m < x.m) return true; if (m > x.m) return false; if (d < x.d) return true; if (d > x.d) return false; return true; } void inc() { if ((d == 31) && (m == 12)) { y++; d = 1; m = 1; } else { if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) { ml[1] = 29; } if (d == ml[m - 1]) { m++; d = 1; } else d++; } } } int partition(int n, int l, int m) {// n - sum, l - length, m - every // part // <= m if (n < l) return 0; if (n < l + 2) return 1; if (l == 1) return 1; int c = 0; for (int i = Math.min(n - l + 1, m); i >= (n + l - 1) / l; i--) { c += partition(n - i, l - 1, i); } return c; } int rifmQuality(String a, String b) { if (a.length() > b.length()) { String c = a; a = b; b = c; } int c = 0, d = b.length() - a.length(); for (int i = a.length() - 1; i >= 0; i--) { if (a.charAt(i) == b.charAt(i + d)) c++; else break; } return c; } String numSym = "0123456789ABCDEF"; String ZFromXToYNotation(int x, int y, String z) { if (z.equals("0")) return "0"; String a = ""; // long q = 0, t = 1; BigInteger q = BigInteger.ZERO, t = BigInteger.ONE; for (int i = z.length() - 1; i >= 0; i--) { q = q.add(t.multiply(BigInteger.valueOf(z.charAt(i) - 48))); t = t.multiply(BigInteger.valueOf(x)); } while (q.compareTo(BigInteger.ZERO) == 1) { a = numSym.charAt((int) (q.mod(BigInteger.valueOf(y)).intValue())) + a; q = q.divide(BigInteger.valueOf(y)); } return a; } double angleFromXY(int x, int y) { if ((x == 0) && (y > 0)) return Math.PI / 2; if ((x == 0) && (y < 0)) return -Math.PI / 2; if ((y == 0) && (x > 0)) return 0; if ((y == 0) && (x < 0)) return Math.PI; if (x > 0) return Math.atan((double) y / x); else { if (y > 0) return Math.atan((double) y / x) + Math.PI; else return Math.atan((double) y / x) - Math.PI; } } static boolean isNumber(String x) { try { Integer.parseInt(x); } catch (NumberFormatException ex) { return false; } return true; } static boolean stringContainsOf(String x, String c) { for (int i = 0; i < x.length(); i++) { if (c.indexOf(x.charAt(i)) == -1) return false; } return true; } long pow(long a, long n) { // b > 0 if (n == 0) return 1; long k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } int pow(int a, int n) { // b > 0 if (n == 0) return 1; int k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } double pow(double a, int n) { // b > 0 if (n == 0) return 1; double k = n, b = 1, c = a; while (k != 0) { if (k % 2 == 0) { k /= 2; c *= c; } else { k--; b *= c; } } return b; } double log2(double x) { return Math.log(x) / Math.log(2); } int lpd(int[] primes, int x) {// least prime divisor int i; for (i = 0; primes[i] <= x / 2; i++) { if (x % primes[i] == 0) { return primes[i]; } } ; return x; } int np(int[] primes, int x) {// number of prime number for (int i = 0; true; i++) { if (primes[i] == x) return i; } } int[] dijkstra(int[][] map, int n, int s) { int[] p = new int[n]; boolean[] b = new boolean[n]; Arrays.fill(p, Integer.MAX_VALUE); p[s] = 0; b[s] = true; for (int i = 0; i < n; i++) { if (i != s) p[i] = map[s][i]; } while (true) { int m = Integer.MAX_VALUE, mi = -1; for (int i = 0; i < n; i++) { if (!b[i] && (p[i] < m)) { mi = i; m = p[i]; } } if (mi == -1) break; b[mi] = true; for (int i = 0; i < n; i++) if (p[mi] + map[mi][i] < p[i]) p[i] = p[mi] + map[mi][i]; } return p; } boolean isLatinChar(char x) { if (((x >= 'a') && (x <= 'z')) || ((x >= 'A') && (x <= 'Z'))) return true; else return false; } boolean isBigLatinChar(char x) { if (x >= 'A' && x <= 'Z') return true; else return false; } boolean isSmallLatinChar(char x) { if (x >= 'a' && x <= 'z') return true; else return false; } boolean isDigitChar(char x) { if (x >= '0' && x <= '9') return true; else return false; } class NotANumberException extends Exception { private static final long serialVersionUID = 1L; String mistake; NotANumberException() { mistake = "Unknown."; } NotANumberException(String message) { mistake = message; } } class Real { String num = "0"; long exp = 0; boolean pos = true; long length() { return num.length(); } void check(String x) throws NotANumberException { if (!stringContainsOf(x, "0123456789+-.eE")) throw new NotANumberException("Illegal character."); long j = 0; for (long i = 0; i < x.length(); i++) { if ((x.charAt((int) i) == '-') || (x.charAt((int) i) == '+')) { if (j == 0) j = 1; else if (j == 5) j = 6; else throw new NotANumberException("Unexpected sign."); } else if ("0123456789".indexOf(x.charAt((int) i)) != -1) { if (j == 0) j = 2; else if (j == 1) j = 2; else if (j == 2) ; else if (j == 3) j = 4; else if (j == 4) ; else if (j == 5) j = 6; else if (j == 6) ; else throw new NotANumberException("Unexpected digit."); } else if (x.charAt((int) i) == '.') { if (j == 0) j = 3; else if (j == 1) j = 3; else if (j == 2) j = 3; else throw new NotANumberException("Unexpected dot."); } else if ((x.charAt((int) i) == 'e') || (x.charAt((int) i) == 'E')) { if (j == 2) j = 5; else if (j == 4) j = 5; else throw new NotANumberException("Unexpected exponent."); } else throw new NotANumberException("O_o."); } if ((j == 0) || (j == 1) || (j == 3) || (j == 5)) throw new NotANumberException("Unexpected end."); } public Real(String x) throws NotANumberException { check(x); if (x.charAt(0) == '-') pos = false; long j = 0; String e = ""; boolean epos = true; for (long i = 0; i < x.length(); i++) { if ("0123456789".indexOf(x.charAt((int) i)) != -1) { if (j == 0) num += x.charAt((int) i); if (j == 1) { num += x.charAt((int) i); exp--; } if (j == 2) e += x.charAt((int) i); } if (x.charAt((int) i) == '.') { if (j == 0) j = 1; } if ((x.charAt((int) i) == 'e') || (x.charAt((int) i) == 'E')) { j = 2; if (x.charAt((int) (i + 1)) == '-') epos = false; } } while ((num.length() > 1) && (num.charAt(0) == '0')) num = num.substring(1); while ((num.length() > 1) && (num.charAt(num.length() - 1) == '0')) { num = num.substring(0, num.length() - 1); exp++; } if (num.equals("0")) { exp = 0; pos = true; return; } while ((e.length() > 1) && (e.charAt(0) == '0')) e = e.substring(1); try { if (e != "") if (epos) exp += Long.parseLong(e); else exp -= Long.parseLong(e); } catch (NumberFormatException exc) { if (!epos) { num = "0"; exp = 0; pos = true; } else { throw new NotANumberException("Too long exponent"); } } } public Real() { } String toString(long mantissa) { String a = "", b = ""; if (exp >= 0) { a = num; if (!pos) a = '-' + a; for (long i = 0; i < exp; i++) a += '0'; for (long i = 0; i < mantissa; i++) b += '0'; if (mantissa == 0) return a; else return a + "." + b; } else { if (exp + length() <= 0) { a = "0"; if (mantissa == 0) { return a; } if (mantissa < -(exp + length() - 1)) { for (long i = 0; i < mantissa; i++) b += '0'; return a + "." + b; } else { if (!pos) a = '-' + a; for (long i = 0; i < mantissa; i++) if (i < -(exp + length())) b += '0'; else if (i + exp >= 0) b += '0'; else b += num.charAt((int) (i + exp + length())); return a + "." + b; } } else { if (!pos) a = "-"; for (long i = 0; i < exp + length(); i++) a += num.charAt((int) i); if (mantissa == 0) return a; for (long i = exp + length(); i < exp + length() + mantissa; i++) if (i < length()) b += num.charAt((int) i); else b += '0'; return a + "." + b; } } } } boolean containsRepeats(int... num) { Set<Integer> s = new TreeSet<Integer>(); for (int d : num) if (!s.contains(d)) s.add(d); else return true; return false; } int[] rotateDice(int[] a, int n) { int[] c = new int[6]; if (n == 0) { c[0] = a[1]; c[1] = a[5]; c[2] = a[2]; c[3] = a[0]; c[4] = a[4]; c[5] = a[3]; } if (n == 1) { c[0] = a[2]; c[1] = a[1]; c[2] = a[5]; c[3] = a[3]; c[4] = a[0]; c[5] = a[4]; } if (n == 2) { c[0] = a[3]; c[1] = a[0]; c[2] = a[2]; c[3] = a[5]; c[4] = a[4]; c[5] = a[1]; } if (n == 3) { c[0] = a[4]; c[1] = a[1]; c[2] = a[0]; c[3] = a[3]; c[4] = a[5]; c[5] = a[2]; } if (n == 4) { c[0] = a[0]; c[1] = a[2]; c[2] = a[3]; c[3] = a[4]; c[4] = a[1]; c[5] = a[5]; } if (n == 5) { c[0] = a[0]; c[1] = a[4]; c[2] = a[1]; c[3] = a[2]; c[4] = a[3]; c[5] = a[5]; } return c; } int min(int... a) { int c = Integer.MAX_VALUE; for (int d : a) if (d < c) c = d; return c; } int max(int... a) { int c = Integer.MIN_VALUE; for (int d : a) if (d > c) c = d; return c; } int pos(int x) { if (x > 0) return x; else return 0; } long pos(long x) { if (x > 0) return x; else return 0; } double maxD(double... a) { double c = Double.MIN_VALUE; for (double d : a) if (d > c) c = d; return c; } double minD(double... a) { double c = Double.MAX_VALUE; for (double d : a) if (d < c) c = d; return c; } int[] normalizeDice(int[] a) { int[] c = a.clone(); if (c[0] != 0) if (c[1] == 0) c = rotateDice(c, 0); else if (c[2] == 0) c = rotateDice(c, 1); else if (c[3] == 0) c = rotateDice(c, 2); else if (c[4] == 0) c = rotateDice(c, 3); else if (c[5] == 0) c = rotateDice(rotateDice(c, 0), 0); while (c[1] != min(c[1], c[2], c[3], c[4])) c = rotateDice(c, 4); return c; } boolean sameDice(int[] a, int[] b) { for (int i = 0; i < 6; i++) if (a[i] != b[i]) return false; return true; } final double goldenRatio = (1 + Math.sqrt(5)) / 2; final double aGoldenRatio = (1 - Math.sqrt(5)) / 2; long Fib(int n) { if (n < 0) if (n % 2 == 0) return -Math.round((pow(goldenRatio, -n) - pow(aGoldenRatio, -n)) / Math.sqrt(5)); else return -Math.round((pow(goldenRatio, -n) - pow(aGoldenRatio, -n)) / Math.sqrt(5)); return Math.round((pow(goldenRatio, n) - pow(aGoldenRatio, n)) / Math.sqrt(5)); } class japaneeseComparator implements Comparator<String> { @Override public int compare(String a, String b) { int ai = 0, bi = 0; boolean m = false, ns = false; if (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') { if (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') m = true; else return -1; } if (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') { if (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') m = true; else return 1; } a += "!"; b += "!"; int na = 0, nb = 0; while (true) { if (a.charAt(ai) == '!') { if (b.charAt(bi) == '!') break; return -1; } if (b.charAt(bi) == '!') { return 1; } if (m) { int ab = -1, bb = -1; while (a.charAt(ai) <= '9' && a.charAt(ai) >= '0') { if (ab == -1) ab = ai; ai++; } while (b.charAt(bi) <= '9' && b.charAt(bi) >= '0') { if (bb == -1) bb = bi; bi++; } m = !m; if (ab == -1) { if (bb == -1) continue; else return 1; } if (bb == -1) return -1; while (a.charAt(ab) == '0' && ab + 1 != ai) { ab++; if (!ns) na++; } while (b.charAt(bb) == '0' && bb + 1 != bi) { bb++; if (!ns) nb++; } if (na != nb) ns = true; if (ai - ab < bi - bb) return -1; if (ai - ab > bi - bb) return 1; for (int i = 0; i < ai - ab; i++) { if (a.charAt(ab + i) < b.charAt(bb + i)) return -1; if (a.charAt(ab + i) > b.charAt(bb + i)) return 1; } } else { m = !m; while (true) { if (a.charAt(ai) <= 'z' && a.charAt(ai) >= 'a' && b.charAt(bi) <= 'z' && b.charAt(bi) >= 'a') { if (a.charAt(ai) < b.charAt(bi)) return -1; if (a.charAt(ai) > b.charAt(bi)) return 1; ai++; bi++; } else if (a.charAt(ai) <= 'z' && a.charAt(ai) >= 'a') return 1; else if (b.charAt(bi) <= 'z' && b.charAt(bi) >= 'a') return -1; else break; } } } if (na < nb) return 1; if (na > nb) return -1; return 0; } } Random random = new Random(); } void readIntArray(int[] a) throws IOException { for (int i = 0; i < a.length; i++) a[i] = nextInt(); } String readChars(int l) throws IOException { String r = ""; for (int i = 0; i < l; i++) r += (char) br.read(); return r; } class myFraction { long num = 0, den = 1; void reduce() { long d = lib.gcd(num, den); num /= d; den /= d; } myFraction(long ch, long zn) { num = ch; den = zn; reduce(); } myFraction add(myFraction t) { long nd = lib.lcm(den, t.den); myFraction r = new myFraction(nd / den * num + nd / t.den * t.num, nd); r.reduce(); return r; } public String toString() { return num + "/" + den; } int lev(String s1, String s2) { int[][] distance = new int[s1.length() + 1][s2.length() + 1]; for (int i = 0; i <= s1.length(); i++) distance[i][0] = i; for (int j = 0; j <= s2.length(); j++) distance[0][j] = j; for (int i = 1; i <= s1.length(); i++) for (int j = 1; j <= s2.length(); j++) distance[i][j] = Math.min(Math.min(distance[i - 1][j] + 1, distance[i][j - 1] + 1), distance[i - 1][j - 1] + ((s1.charAt(i - 1) == s2.charAt(j - 1)) ? 0 : 1)); return distance[s1.length()][s2.length()]; } } class myPoint { myPoint(int a, int b) { x = a; y = b; } int x, y; boolean equals(myPoint a) { if (x == a.x && y == a.y) return true; else return false; } public String toString() { return x + ":" + y; } } }
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 dp[100010][320]; int a[100010], p[100]; int n, s = 0; int go(int i, int j, int b, int c) { if (i >= b + n) return c; int& ans = dp[i][j]; if (ans != -1) return ans; ans &= go(i + p[j], j, b, c & a[i]); return ans; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; a[n + i] = a[i]; } memset(dp, -1, sizeof dp); for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { p[s++] = i; if (n / i != i) p[s++] = n / i; } } sort(p, p + s); for (int i = 0; i < n; ++i) for (int j = 0; j < s; ++j) if (2 * p[j] != n && p[j] != n) { if (go(i, j, i, a[i])) { cout << "YES\n"; 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
from sys import stdin from sys import exit from math import sqrt #parser def parser(): return map(int, stdin.readline().split()) def IsPrime(t): if t<3: return False if t==4: return True for i in range(2,int(sqrt(t))+1): if t%i==0: return False return True 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 #Comprobar existencia de un poligono regular 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: i_couple=int(n/i) if IsPrime(i): divisors.append((i,i_couple)) if IsPrime(i_couple): divisors.append((i_couple,i)) for pair in divisors: first_divisor=pair[0] second_divisor=pair[1] if 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.*; public class Main { static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));} String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());} catch (IOException e){e.printStackTrace();}}return st.nextToken();} int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());} String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; } } static FastReader sc = new FastReader(); static List<Integer> g[]; public static void main (String[] args) throws Exception { PrintWriter out = new PrintWriter(System.out); int t = 1; // t = sc.nextInt(); z :while(t-->0) { int n = sc.nextInt(); int a[] = new int[n+1]; for(int i=1;i<=n;i++) a[i] = sc.nextInt(); for(int i=1;i<=n/3;i++) { if(n%i == 0) { for(int j=1;j<=i;j++) { boolean flag = true; for(int k=j;k<=n;k+=i) { if(a[k] == 0) { flag = false; break; } } if(flag) { out.write("YES"); out.close(); return; } } } } out.write("NO"); } out.close(); } static int ceil(int a,int b) { return a/b + (a%b==0?0:1); } } class pair implements Comparable<pair>{ int a; int b; pair(int a,int b){ this.a = a; this.b = b; } @Override public int compareTo(pair p){ return p.b - this.b; } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays; public class Main { private static StreamTokenizer in = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); private static PrintWriter out = new PrintWriter(System.out); private static int nextInt() throws Exception { in.nextToken(); return (int) in.nval; } private static String nextString() throws Exception { in.nextToken(); return in.sval; } public static void main(String[] args) throws Exception { int n = nextInt(), t = n; a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } int[] cdf = new int[n]; for (int i = 3; i <= t; i++) { if (t % i == 0) { while (t % i == 0) { t /= i; } int u = n / i; Arrays.fill(cdf, 1); for (int j=0; j<n; j++) { cdf[j%u] &= a[j]; } for (int j=0; j<u; j++) { if (cdf[j] == 1) { out.println("YES"); out.flush(); return; } } } } out.println("NO"); out.flush(); } private static int[] a; }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.*; import java.util.* ; public class HelloWorld { static int a [] ; public static void main (String args [] ) throws IOException ,InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt() ; a = new int [n] ; for (int i = 0 ; i < n;i ++ ) a[i] = sc.nextInt() ; ArrayList<Integer> z = new ArrayList<Integer>() ; for (int i = 1 ; i*i<=n ; i++ ) { if (n%i==0) { z.add(i) ; z.add(n/i) ; } } for (int i : z ){ for (int j = 0 ; j < i ; j ++ ) { if (goodP(j,i)) { pw.println("YES"); pw.close(); return; } } } pw.println("NO"); pw.close(); } static boolean goodP(int j , int i) { if (a.length/i<3) return false ; for (int l = 0 ; l<a.length/i ; l++ ) { if (a[j]==0) return false; j+=i; if (j>=a.length) j-=a.length; } return true ; } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
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 double PI = 3.14159265358979; const double PI2 = 6.28318530717958; const double PId2 = 1.570796326794895; inline long long pw(long long n, int p) { long long ans = 1; for (int i = 0; i < p; i++) ans *= n; return ans; } inline vector<int> ReadVI(int count) { vector<int> arrayname(count); for (int i = 0; i < int(count); i++) cin >> arrayname[i]; return arrayname; }; inline vector<long long> ReadVlong(int count) { vector<long long> arrayname(count); for (int i = 0; i < int(count); i++) cin >> arrayname[i]; return arrayname; }; inline vector<pair<int, int> > ReadVII(int count) { vector<pair<int, int> > arrayname(count); for (int i = 0; i < int(count); i++) { cin >> arrayname[i].first; arrayname[i].second = i; } return arrayname; }; const int MOD = 100000000; const int MAXVALUE = 101; vector<int> factor(int n) { set<int> res; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { res.insert(i); res.insert(n / i); } } vector<int> r; for (auto i = res.begin(); i != res.end(); i++) if (*i >= 3) r.push_back(*i); return r; } int main() { int n; cin >> n; vector<int> a = ReadVI(n); vector<int> d = factor(n); for (int i = 0; i < d.size(); i++) { int count = n / d[i]; for (int j = 0; j < count; j++) { int s = j; bool flag = true; for (int k = 0; k < d[i]; k++) { if (a[s] == 0) flag = false; s += count; s = s % n; } if (flag) return cout << "YES", 0; } } cout << "NO"; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
def main(): n=int(input()) a=input().split(" ") a=[int(i) for i in a] for k in range(1,int(n/3)+1): if(int(n%k)==0): for p in range(k): sum1=0 for v in range(p,n,k): if(a[v]==1): sum1+=1 if(sum1==int(n/k)): return(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; int main() { int n; cin >> n; vector<int> mood(n, -1); for (int i = 0; i < n; i++) { cin >> mood[i]; } for (int i = 0; i < n; i++) { if (mood[i] == 1) { for (int j = n; j >= 3; j--) { if (n % j != 0) continue; bool found = true; for (int k = 1; k <= j; k++) { int m = i + n / j * k; if (mood[m % n] == 1) { continue; } else { found = false; break; } } if (found) { 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.PrintWriter; import java.util.StringTokenizer; public class RoundTableKnights implements Runnable { private void solve() throws IOException { int n = nextInt(); int[] t = new int[n]; for(int i = 0; i < n; ++i) t[i] = nextInt(); for(int i = 3 ; i <= n; ++i) { if(n%i != 0) continue; int jump = n/i; for(int j = 0; j < n; ++j) { boolean ok = true; int c =0; for(int k = j; k < n ; k += jump) { c++; if(t[k] == 0) { ok = false; break; } } if(ok && c==i) { writer.println("YES"); return; } } } writer.println("NO"); } public static void main(String[] args) { new RoundTableKnights().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
n=int(input()) a=list(map(int,input().split())) l=[] # i vertices m=n for i in range(3,n+1): if m%i==0: l.append(i) while m%i==0: m//=i for p in l: q=n//p cnt=[0]*n for i in range(n): if a[i]==1: cnt[i]=1 for s in range(q): for j in range(s,n+q,q): cnt[(j+q)%n]+=cnt[j%n] for i in range(q): if cnt[i]==p+1: print("YES") exit() print("NO")
PYTHON3
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.BitSet; import java.util.StringTokenizer; public class F { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); BitSet good = new BitSet(n); for(int i = 0; i < n; ++i) if(sc.nextInt() == 1) good.set(i); ArrayList<Integer> divs = new ArrayList<Integer>(); for(int i = 1; i * i <= n; ++i) if(n % i == 0) { divs.add(i); if(n / i != i) divs.add(n / i); } boolean yes = false; for(int d: divs) { if(n / d < 3) continue; BitSet g = new BitSet(d); g.set(0, d, true); for(int i = 0; i < n; ++i) if(!good.get(i)) g.clear(i % d); yes |= g.cardinality() > 0; } out.println(yes ? "YES":"NO"); out.close(); } 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 double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
def getPrimes(n): primes = [] if n % 4 == 0: primes.append(4) while n % 2 == 0: n //= 2 div = 3 while n > 1: if n % div == 0: primes.append(div) while n % div == 0: n //= div div += 1 return primes def islucky(n, knights, primes): lucky = False for p in primes: if lucky: break skip = n // p for start in range(skip): allLucky = True for side in range(p): idx = start + side*skip if knights[idx] == 0: allLucky = False break if allLucky: lucky = True break return lucky n = int(input()) knights = [int(x) for x in input().split()] primes = getPrimes(n) if islucky(n, knights, primes): 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
def f(n): n += 1 t = [1] * n for i in range(3, int(n ** 0.5) + 1, 2): u, v = i * i, 2 * i if t[i]: t[u :: v] = [0] * ((n - u - 1) // v + 1) return [4] + [i for i in range(3, n, 2) if t[i]] n, t = int(input()), list(map(int, input().split())) q = [n // i for i in f(n) if n % i == 0] print('YNEOS'[all(0 in t[j :: i] for i in q for j in range(i)) :: 2])
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 mod = 1e9 + 7; const double error = 1e-8; const double PI = acos(-1); mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); inline long long int MOD(long long int x, long long int m = mod) { long long int y = x % m; return (y >= 0) ? y : y + m; } const int inf = 0x3f3f3f3f; const unsigned long long int infl = 1e18 + 1; const int nmax = 1e5 + 5; int input[nmax]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int n; cin >> n; for (int i = 0; i < n; i++) cin >> input[i]; for (int i = 1; i < (n + 1) / 2; i++) { if (n % i != 0) continue; for (int j = 0; j < i; j++) { bool ok = true; for (int k = j; k < n; k += i) { if (input[k] == 0) ok = false; } if (ok) { printf("YES"); return 0; } } } printf("NO"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.*; import java.math.*; import java.util.*; /** * @author Egor Kulikov ([email protected]) * @author Alexey Safronov ([email protected]) */ public class Round65 { @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) private InputReader in; private PrintWriter out; private void solve() { int N = in.readInt(); int[] A = new int[N]; for(int i = 0; i < N; i++) { A[i] = in.readInt(); } ArrayList<Integer> dels = new ArrayList<Integer>(); for(int i = 3; i <= N; i++) { if(N % i == 0) { boolean ok = true; // for(int del : dels) { // if(N % del == 0) { // // ok = false; // break; /// } //} if(ok) { dels.add(i); } } } for(int del : dels) { for(int i = 0; i < N; i++) { if(A[i] == 0) { continue; } boolean ok = true; int counter = 1; int current = (i + (N / del)) % N; while(counter < del) { if(A[current] == 0) { break; } ++counter; current = (current + (N / del)) % N; } if(counter == del) { out.println("YES"); return; } } } out.println("NO"); } public static void main(String[] args) { new Round65().run(); } private Round65() { @SuppressWarnings({"UnusedDeclaration"}) String id = getClass().getName().toLowerCase(); //noinspection EmptyTryBlock try { // System.setIn(new FileInputStream(id + ".in")); // System.setOut(new PrintStream(new FileOutputStream(id + ".out"))); // System.setIn(new FileInputStream("input.txt")); // System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { throw new RuntimeException(e); } in = new InputReader(System.in); out = new PrintWriter(System.out); } private void run() { //noinspection InfiniteLoopStatement // int testCount = in.readInt(); // for (int i = 0; i < testCount; i++) // while (true) solve(); exit(); } @SuppressWarnings({"UnusedDeclaration"}) private void exit() { out.close(); System.exit(0); } @SuppressWarnings({"UnusedDeclaration"}) private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; 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 BigInteger readBigInteger() { try { return new BigInteger(readString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } 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; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = readInt(); return array; } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) array[i] = readLong(); return array; } public double[] readDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) array[i] = readDouble(); return array; } public String[] readStringArray(int size) { String[] array = new String[size]; for (int i = 0; i < size; i++) array[i] = readString(); return array; } public char[][] readTable(int rowCount, int columnCount) { char[][] table = new char[rowCount][columnCount]; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) table[i][j] = readCharacter(); } return table; } public void readIntArrays(int[]... arrays) { for (int i = 0; i < arrays.length; i++) { for (int j = 0; j < arrays[0].length; j++) arrays[i][j] = readInt(); } } } }
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 mood[100005]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> mood[i]; } for (int i = 1; i < (n + 1) / 2; i++) { if (!(n % i) and n / i != 2) { for (int j = 0; j < i; j++) { bool check = true; for (int k = 0; (k * i + j) < n; k++) { if (mood[k * i + j] == 0) { check = false; } } if (check == true) { cout << "YES"; return 0; } } } } cout << "NO"; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.*; import java.util.*; public class TaskC { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); ArrayList<Integer> div = new ArrayList<>(); int i = 1; while (1l*i*i <= n) { if(n % i == 0) { div.add(i); if(n/i!=i) div.add(n/i); } i++; } int[]arr = new int[n]; for (i = 0 ; i < n ; i++) { arr[i] = sc.nextInt(); } for (int j = 0 ; j < div.size() ; j++) { int steps = div.get(j); if(n/steps < 3) continue; for (int begin = 0 ; begin < steps ; begin++) { int total = n/steps; boolean f = true; for (int idx = begin; total > 0 ; total -- , idx= (idx + steps)%n) { if(arr[idx] == 0) { f = false; break; } } if(f) { pw.print("YES"); pw.close(); return; } } } pw.print("NO"); pw.close(); } private static long gcd(long a, long b) { if( b == 0) return a; return gcd(b , a%b); } static long lcm(int a, int b) { return (a*b)/gcd(a, b); } private static int dis(int xa , int ya , int xb , int yb) { return (xa-xb)*(xa - xb) + (ya- yb)*(ya-yb); } static class Pair1 extends Pair{ int idx; public Pair1(int x, int y , int idx) { super(x, y); this.idx = idx; } public int compareTo(Pair o) { if(y == o.y) return x - o.x; return o.y - y; } public String toString() { return x + " " + y +" "+idx; } } static class Pair implements Comparable<Pair> { int x,y; public Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { if (x == o.x) return y - o.y; return x - o.x; } public double dis(Pair a){ // return 0; return (a.x - x)*(a.x - x) + (a.y-y)*(a.y-y); } public String toString() { return x+" "+ y; } public boolean overlap(Pair a) { if((this.x >= a.x && this.x <= a.y) || (a.x >= this.x && a.x <= this.y)) return true; return false; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public boolean check() { if (!st.hasMoreTokens()) return false; return true; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(e); } } public double nextDouble() { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() { try { return br.ready(); } catch (IOException e) { throw new RuntimeException(e); } } } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.IOException; import java.util.Scanner; /** * Author: dened * Date: 26.03.11 */ public class C { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n = in.nextInt(); boolean[] a = new boolean[n]; for (int i = 0; i < n; ++i) { a[i] = in.nextInt() > 0; } boolean[] b = new boolean[n]; for (int k = 1; 3*k <= n; ++k) { if (n % k == 0) { for (int i = 0; i < k; ++i) { b[i] = a[i]; } for (int i = k; i < n; ++i) { b[i] = a[i] && b[i - k]; if (i >= n - k && b[i]) { System.out.println("YES"); System.exit(0); } } } } System.out.println("NO"); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
m = n = int(raw_input()) a = map(int,raw_input().split()) t=0 for _ in xrange(3, n+1): if (n % _ == 0): s = n / _ else: continue for st in xrange(s): i = st while (i < n and a[i]): i += s if (i >= n): t=1 break if t==0: print 'NO' else: print "YES"
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 Main { 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(); } } static long gcd(long n,long m) { if(n==0&&m==0)return Long.MAX_VALUE; if(n==0)return m; long temp=m%n; return gcd(temp,n); } static long pow(int i,int j) { return ((long)Math.pow(i,j)); } static long pow(long x,long j) { for(int i=0;i<32;i++) { long temp=1; for(int w=0;w<i;w++) { temp*=x; } if(neg&&temp>2147483648l)return 1; else if(!neg&&temp>2147483647)return 1; //pw.println(j); if(neg&&temp==j&&i%2!=0)return i; if(!neg&&temp==j)return i; } return 1; } static trouble ex_gcd(long m,long n) { if(m==0) { if(n!=0) return (new trouble(0,1,n)); return(new trouble(0,1,(long)1e9)); } trouble temp=ex_gcd(n%m,m); long r=temp.x; long m_=temp.y; temp.x=(-1*n/m)*r+m_; temp.y=r; return temp; } static class pair{ long x; long y; pair(long i,long j){ x=i; y=j; } } static String toString(trouble p) { return (p.x)+" "+(p.y)+" "+p.z; } static class trouble{ long x; long y; long z; trouble(long i,long j,long k){ x=i; y=j; z=k; } } static LinkedList<Integer> div(int x){ LinkedList<Integer>arr =new LinkedList<>(); for(int i=1;i*i<=x;i++) { if(x%i==0) {arr.add(i);if(x/i!=i)arr.add(x/i);} } return arr; } static long bin(long i,long[] arr) { long l=0l; long r=Math.min(i+2,arr.length-1); long res=r; while(l<=r) { long mid=l+r>>1; if(arr[(int)mid]>=i) { r=mid-1;res=mid; } else l=mid+1; } return res; } static pair add(long a,long m,long add,long min) { return new pair(a+add,m-min); } static boolean neg; static PrintWriter pw=new PrintWriter(System.out); public static void main(String[] args) throws IOException,Exception { // Thread.sleep(3000); Reader sc=new Reader(); //BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=sc.nextInt(); int[]arr=new int[n]; for(int i=0;i<n;i++){arr[i]=sc.nextInt();} int c=0; LinkedList<Integer> div=div(n); boolean flag=false; for(int i=0;i<div.size();i++){ if(div.get(i)>=3) { for(int j=0;j<(n/div.get(i));j++) { if(arr[j]==1) { boolean f=true; for(int x=j;x<n;x+=(n/div.get(i))) { if(arr[x]!=1)f=false; } if(f) {flag=true;break;} } } } } if(flag)pw.println("YES"); else pw.println("NO"); pw.close(); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; const int N = 100005, oo = 0x3f3f3f3f; int n, v[N], vis[N]; vector<int> primes; void genprimes() { memset(vis, 0, sizeof vis); for (long long i = 3; i < N; i += 2) { if (!vis[i]) { primes.push_back(i); for (long long j = i * i; j < N; j += i) { vis[j] = 1; } } } } bool can(int x) { int c = n / x; for (int i = 0; i < n; i++) { int sum = 0; int j = i; while (j < n) { sum += v[j]; j += c; } if (sum == x) { return true; } } return false; } int main() { genprimes(); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &v[i]); } for (int i = 0; i < primes.size() && n >= primes[i]; i++) { int y = primes[i]; if (n % y == 0 && can(y)) { printf("YES\n"); return 0; } } if (n % 4 == 0 && can(4)) { 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
import math n = int(input()) l = list(map(int,input().split())) factors = [] for i in range(1,int((n+1)/2)): if n%i == 0: factors.append(i) f = 1 for d in factors: for i in range(d): k = 0 while True: if k >= n: f = 0 break elif l[i+k] == 1: k += d continue elif l[i+k] == 0: break if f == 0: break if f == 0: break if f == 0: 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
p=[0]*10000 for i in range(2,10000): if not p[i]: for j in range(i+i,10000,i): p[j]=1 p[4]=0 n=input() a=map(int,raw_input().split()) for i,e in enumerate(p[3:],3): if e: continue if i > n: break if n%i: continue for j in range(n/i): if all(a[j::n/i]): print "YES" exit() print "NO"
PYTHON
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class RoundTableKnights { public static void main(String[] args) { boolean[] sieve = new boolean[100001]; for(int i = 2; i*i <= 100000; i++) { if(!sieve[i]) for(int j = i*i; j <= 100000; j+=i) { sieve[j] = true; } } // System.out.println(Arrays.toString(sieve)); boolean debug = false; FastReader sc = new FastReader(); int n = sc.nextInt(); boolean[] kn = new boolean[n]; boolean allhappy = true; for(int i = 0; i < n; i++) { kn[i] = sc.nextInt() == 1; if(!kn[i])allhappy = false; } if(allhappy) { System.out.println("YES"); return; } HashSet<Integer> div = new HashSet<Integer>(); //find divisors for(int i = 3; i < n; i++) { if(n%i == 0 && (!sieve[i] || i == 4)) { div.add(i); } } // System.out.println(div); // System.out.println(div.size()); for(int kns : div) { if(debug)System.out.println("Divisor " + div); for(int offset = 0; offset < n/kns; offset++) { if(debug)System.out.println(" Offset " + offset); boolean cont = true; for(int knight = 0; knight < n; knight += n/kns) { int actualknight = (knight + offset)%n; if(debug)System.out.println(" knight " + knight + " -> " + actualknight); if(!kn[actualknight])cont = false; } if(cont) { System.out.println("YES"); return; } } } System.out.println("NO"); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static boolean fortunate(int[] v) { int n = v.length, iter = 0; for (int i = 0; i < n / 3; i++) if (v[i] == 1) for (int j = 1; j <= n / 3; j++) { iter = i; do iter = (iter + j) % n; while (i != iter && v[iter] == 1); if (i == iter) return true; } return false; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = ""; StringBuilder sb = new StringBuilder(); d: do { line = br.readLine(); if (line == null || line.length() == 0) break d; int n = Integer.parseInt(line); int[] v = retInts(br.readLine()); if (fortunate(v)) sb.append("YES").append("\n"); else sb.append("NO").append("\n"); } while (line != null && line.length() != 0); System.out.print(sb); } public static int[] retInts(String line) { String[] w = line.split(" "); int[] nums = new int[w.length]; for (int i = 0; i < w.length; i++) nums[i] = Integer.parseInt(w[i]); return nums; } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; const long long MOD = 1000000000 + 9; bool a[maxn * 2]; int n; int at[maxn * 2][300 + 10]; vector<int> d; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; a[i + n] = a[i]; } for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (n / i >= 3) d.push_back(i); if (i * i != n) { if (i >= 3) { d.push_back(n / i); } } } } n = n * 2; bool can = false; for (int i = 0; i < n && !can; i++) { for (int j = 0; j < d.size() && !can; j++) { if (!a[i]) continue; ++at[i][j]; if (i - d[j] >= 0) { at[i][j] += at[i - d[j]][j]; } can |= at[i][j] == n / d[j]; if (can) break; } } if (can) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int arr[100001]; int main() { int n, i, be, len, num, k, t; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (len = n / 3; len > 0; len--) { if (n % len != 0) continue; t = n / len; for (i = 0; i < n; i++) { num = t; k = i; while (num > 0) { if (arr[k] == 1) { k = (k + len) % n; num--; } else break; } if (num == 0) { printf("YES\n"); return 0; } } } printf("NO\n"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long INF = 1e18L; const double eps = 1e-9; const long long mod = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 3; i <= n; i++) { if (n % i == 0) { long long q = n / i; for (long long j = 0; j < q; j++) { long long sum = 0; for (long long k = j; k < n; k += q) { sum += a[k]; } if (sum == i) { cout << "YES\n"; return 0; } } } } cout << "NO\n"; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class C { public static boolean check(boolean[] A, int i) { int n = A.length; int dif = n / i; boolean[] visited = new boolean[n]; for (int j = 0; j < n; j++) { if (!A[j] || visited[j]) continue; int k = (j + dif) % n; boolean can = true; while (k != j) { visited[k] = true; can &= A[k]; k += dif; k %= n; } if (can) return true; } return false; } public static boolean Can(boolean[] A) { int n = A.length; for (int i = 3; i <= n; i++) { if (n % i != 0) continue; boolean can = check(A, i); if (can) return true; } return false; } public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); boolean[] A = new boolean[n]; String[] S = in.readLine().split(" "); for (int i = 0; i < n; i++) if (S[i].equals("1")) A[i] = true; else A[i] = false; if (Can(A)) System.out.println("YES"); else System.out.println("NO"); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.util.*; import javafx.util.Pair; public class practice { 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
import java.io.*; import java.util.*; import java.math.*; public class Main{ static class Solver{ Vector<Integer> div(int x){ Vector<Integer> d = new Vector<Integer>(); for(int i = 1; i * i <= x; ++i){ if(x % i == 0){ d.add(i); if(i != x / i){ d.add(x / i); } } } return d; } void main(){ // freopen("in"); int n = nextInt(); boolean[] mood = new boolean[n]; for(int i = 0; i < n; ++i){ mood[i] = (nextInt() == 1); } for(int i : div(n)){ if(n / i >= 3){ boolean[] good = new boolean[i]; Arrays.fill(good, true); for(int j = 0; j < n; ++j){ good[j % i] &= mood[j]; } for(boolean b : good){ if(b){ printf("YES\n"); return; } } } } printf("NO\n"); } <T extends Comparable<T>> T max(T x, T y){ return (x.compareTo(y) > 0 ? x : y); } <T extends Comparable<T>> T min(T x, T y){ return (x.compareTo(y) < 0 ? x : y); } final double pi = Math.acos(-1.0); final double eps = 1e-9; final long infl = 0x3f3f3f3f3f3f3f3fl; final int inf = 0x3f3f3f3f; final int mod = 1000000007; final int maxn = -1; boolean zero(double x){ return x < eps; } class ii implements Comparable<ii>{ int st; int nd; ii(){ st = nd = 0; } ii(int a, int b){ st = a; nd = b; } public int compareTo(ii p){ return st != p.st ? st - p.st : nd - p.nd; } public String toString(){ return "(" + st + ", " + nd + ")"; } } class tri implements Comparable<tri>{ int st; int nd; int rd; tri(){ st = nd = rd = 0; } tri(int a, int b, int c){ st = a; nd = b; rd = c; } public int compareTo(tri p){ return st != p.st ? st - p.st : nd != p.nd ? nd - p.nd : rd - p.rd; } public String toString(){ return "(" + st + ", " + nd + ", " + rd + ")"; } } /** io **/ PrintWriter out; BufferedReader reader; StringTokenizer tokens; Solver(){ tokens = new StringTokenizer(""); reader = new BufferedReader(new InputStreamReader(System.in), 1 << 15); out = new PrintWriter(System.out); Locale.setDefault(Locale.US); // imprime double com ponto main(); // solução out.close(); // flush output } void freopen(String s){ try{ reader = new BufferedReader(new InputStreamReader(new FileInputStream(s)), 1 << 15); } catch(FileNotFoundException e){ throw new RuntimeException(e); } } /** input -- supõe que não chegou no EOF **/ int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next(){ readTokens(); return tokens.nextToken(); } String nextLine(){ readTokens(); return tokens.nextToken("\n"); } boolean readTokens(){ while(!tokens.hasMoreTokens()){ // lê os dados, ignorando linhas vazias try{ String line = reader.readLine(); if(line == null) return false; // EOF tokens = new StringTokenizer(line); } catch(IOException e){ throw new RuntimeException(e); } } return true; } /** output **/ void printf(String s, Object... o) { out.printf(s, o); } void debug(String s, Object... o) { System.err.printf((char)27 + "[91m" + s + (char)27 + "[0m", o); } } public static void main(String[] args){ new Solver(); } }
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; inline void getarrint(vector<int>& a, int n, istream& in) { for (int i = 0; i <= n - 1; i++) in >> a[i]; } class Solution { public: void solve(std::istream& in, std::ostream& out) { int n; in >> n; vector<int> mood(n); getarrint(mood, n, in); for (int i = 1; i * i < n; i++) { if (n % i == 0) { int div1 = i; int div2 = n / i; bool ok; if (n / div1 >= 3) { for (int knight = 0; knight <= div1 - 1; knight++) { ok = true; for (int j = knight; j <= n - 1; j += div1) { ok &= mood[j]; } if (ok) { out << "YES" << '\n'; return; } } } if (n / div2 >= 3) { for (int knight = 0; knight <= div2 - 1; knight++) { ok = true; for (int j = knight; j <= n - 1; j += div2) { ok &= mood[j]; } if (ok) { out << "YES" << '\n'; return; } } } } } out << "NO" << '\n'; return; } }; void solve(std::istream& in, std::ostream& out) { out << std::setprecision(12); Solution solution; solution.solve(in, out); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; ostream& out = cout; solve(in, out); 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
#some convinient function def make_primelist(size): global prime from math import sqrt _prime=[1 for i in xrange(size+1)] _prime[0]=0 _prime[1]=0 for i in xrange(size): if i*i>size: break if _prime[i]==0: continue for k in xrange(2*i,size+1,i): _prime[k]=0 prime=_prime choice_max=2000 dp_choice=[[None for i in xrange(choice_max+1)] for j in xrange(choice_max+1)] def choice(n,m): #return (n+m)!/(n!m!) if n<0 or m<0: return 0 if dp_choice[n][m]!=None: return dp_choice[n][m] if n==m==0: ret=1 else: ret=choice(n-1,m)+choice(n,m-1) dp_choice[n][m]=ret return ret def inverse(x,M): #1/x mod m x=x%M if x==0 or M<=0: raise a,b,c,d=1,0,0,1 v1,v2=x,M while v2: n=v1/v2 v1,v2=v2,v1-n*v2 a, b, c, d = c, d, a-n*c, b-n*d if v1!=1: raise return a%M def div(a,b,M): return a*inverse(b,M)%M def gcd(x,y): while y: x,y=y,x%y return x """ example choice(0,5)==1 and choice(1,4)==5 and choice(2,3)==10 make_primelist(6); prime==[0,0,1,1,0,1,0] inverse(3,10) == 7 div(7,9,10) == 3 because 27/9==3 gcd(10,15) == 5 """ def readints(): return map(int,raw_input().split()) def main(): #from fractions import Fraction n=input() x=readints() for k in xrange(3,n+1): if n%k:continue m=n/k for i in xrange(m): if all(x[i+t*m] for t in xrange(k)): print "YES" return print "NO" pass 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 java.awt.Point; import java.util.*; import java.io.*; import static java.lang.Math.*; public class PracticeProblem { /* * This FastReader code is taken from GeeksForGeeks.com * https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/ * * The article was written by Rishabh Mahrsee */ public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static FastReader in = new FastReader(); public static PrintWriter out = new PrintWriter(System.out); public static final int MOD = (int)1e9 + 7; public static void main(String[] args) { solve(); out.close(); } public static void solve() { int n = in.nextInt(); boolean[] arr = new boolean[n]; for (int i = 0; i < n; i++) arr[i] = in.nextInt() == 1; int num = n; while (num % 2 == 0) // We don't want 2 num /= 2; // But you have to check 4 if (n % 4 == 0) { if (polygon(arr, n / 4 - 1)) { out.println("YES"); return; } } for (int i = 3; i < sqrt(num); i += 2) { if (num % i == 0) { while (num % i == 0) { num /= i; } if (polygon(arr, n / i - 1)) { out.println("YES"); return; } } } if (num != 1) { if (polygon(arr, n / num - 1)) { out.println("YES"); return; } } out.println("NO"); } private static boolean polygon(boolean[] arr, int bw) { for (int i = 0; i <= bw; i++) { boolean badMood = false; for (int j = i; j < arr.length; j += bw + 1) { if (!arr[j]) badMood = true; } if (!badMood) 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
import sys class pythonin: _data = [] _cur = 0 def __init__(self): while True: try: sm = raw_input().split(" ") except EOFError: break for x in sm : if x != "" and x != "\t" : self._data.append(x) def eof(self) : return self._cur == len(self._data) def nextToken(self) : self._cur += 1 return self._data[self._cur - 1] def nextInt(self) : return (int)(self.nextToken()) #sys.stdin = open("input.txt", "r") #sys.stdout = open("output.txt", "w") # pin = pythonin() n = pin.nextInt() a = [pin.nextInt() for i in xrange(0, n)] for side in xrange(1, n / 3 + 1) : if n % side == 0: for start in xrange(0, side) : good = True for pos in xrange(start, n, side) : if a[pos] == 0 : good = False break if good : print "YES" exit() print "NO" #print ("Press any key to continue") #raw_input()
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; void require(bool cond, const string& message = "Runtime error") { if (!cond) { cerr << message << endl; assert(false); } } void readData() {} bool good(const vector<int>& a, int k) { int n = a.size(); if (n / k < 3) return false; for (int j = 0; j < int(k); ++j) { int c = 1; for (int l = j; l < n; l += k) { c &= a[l]; if (!c) break; } if (c) { return true; } } return false; } void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < int(n); ++i) cin >> a[i]; bool ans = false; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { if (good(a, i) || good(a, n / i)) { ans = true; break; } } } if (ans) cout << "YES" << endl; else cout << "NO" << endl; } int main() { ios_base::sync_with_stdio(false); readData(); solve(); 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; bool f, a[(int)1e6 + 9]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < (n == 3 ? 2 : (n >> 1)); i++) { if (n % i == 0) { for (int j = 0; j < i; j++) { if (!a[j]) continue; int k = j + i; f = 1; while (k != j) { if (!a[k]) { f = 0; break; } k = (k + i) % n; } if (f) break; } if (f) break; } } if (f) printf("YES"); else printf("NO"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.*; import java.util.*; public class A implements Runnable{ public static void main (String[] args) {new Thread(null, new A(), "_cf", 1 << 28).start();} public void run() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println("Go!"); int max = (int)1e5+10; boolean[] prime = new boolean[max]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for(int i = 2; i < max; i++) { if(!prime[i]) continue; for(int j = 2 * i; j < max; j += i) prime[j] = false; } int n = fs.nextInt(); boolean yes = true; int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = fs.nextInt(); yes &= a[i] == 1; } if(yes) { System.out.println("YES"); return; } for(int num = 3; num <= n; num++) { if(n % num != 0) continue; if(!prime[num] && num != 4) continue; int len = n / num; for(int i = 0; i < len; i++) { int at = i, cnt = 0; while(at < n) { if(a[at] != 1) break; cnt++; at += len; } if(cnt == num) { System.out.println("YES"); return; } } } out.println("NO"); out.close(); } void sort (int[] a) { int n = a.length; for(int i = 0; i < 50; i++) { Random r = new Random(); int x = r.nextInt(n), y = r.nextInt(n); int temp = a[x]; a[x] = a[y]; a[y] = temp; } Arrays.sort(a); } class FastScanner { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
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 MAX = 1e5 + 55; const int inf = 1e9 + 77; const int MOD = 1e9 + 7; const double PI = acos(-1.0); const double eps = 1e-7; int n; int a[MAX]; vector<int> v; void GenerateDivisors(int n) { int i; for (i = 1; i * i < n; ++i) { if (n % i == 0) { v.push_back(i); if (i != 1) v.push_back(n / i); } } if (i * i == n) v.push_back(i); } bool check(int st, int hop) { for (int i = st; i < n; i += hop) if (!a[i]) return false; return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); GenerateDivisors(n); sort(((v).begin()), ((v).end()), greater<int>()); for (int i = 1 - (n % 2); i < (int)v.size(); ++i) { int d = v[i]; for (int j = 0; j < d; ++j) { if (check(j, d)) { printf("YES"); return 0; } } } printf("NO"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.util.*; import java.io.*; public class CF_71_C_ROUND_TABLE_KNIGHTS { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean[] a = new boolean[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt() == 1; } ArrayList<Integer> divs = new ArrayList<Integer>(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { int j = n / i; divs.add(i); if (i != j) divs.add(j); } } for (int d : divs) { boolean exist = false; if(n / d < 3) continue; outer: for (int i = 0; i < n; i++) for (int j = (i + d) % n, count = 1; a[i] && a[j] ; j = (j + d) % n, count++) { if (i == j) { exist = count >= 3; break outer; } } if (exist) { System.out.println("YES"); return; } } System.out.println("NO"); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int n, a[100005], b[100005]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int s = 0; for (int i = 0; i < n; i++) s += a[i]; if (s == n) { cout << "YES"; return 0; } for (int j = 2; j * j <= n; j++) if (n % j == 0) { int f = n / j; if (n / f >= 3) { for (int i = 0; i < n; i++) b[i] = a[i]; for (int i = f; i <= n + n; i++) { b[i % n] &= b[(i - f) % n]; } for (int i = 0; i < f; i++) if (b[i]) { cout << "YES"; return 0; } } f = j; if (n / f >= 3) { for (int i = 0; i < n; i++) b[i] = a[i]; for (int i = f; i <= n + n; i++) { b[i % n] &= b[(i - f) % n]; } for (int i = 0; i < f; i++) if (b[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.*; import java.io.*; public class Main { static BufferedReader br; static PrintWriter pw; static int inf = (int) 1e9; static int mod = (int) 1e9 + 7; static long[][][] memo; static int[] a, rArr, gArr, bArr; public static void main(String args[]) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); int n = Integer.parseInt(br.readLine()); int[] a = nxtarr(); boolean f = false; boolean g = true; for (int i = 0; i < a.length; i++) { if (a[i] == 0) g = false; } f |= g; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { if (n / i > 2) f |= check(n / i, a); if (i > 2) f |= check(i, a); } } pw.println(f ? "YES" : "NO"); pw.flush(); } static boolean check(int x, int[] a) { boolean f = false; int loop = a.length / x; for (int i = 0; i < loop; i++) { boolean g = true; for (int j = i; j < a.length; j += loop) { if (a[j] == 0) g = false; } f |= g; } return f; } static int binser1(int k, int n) { int s = 0; int e = a.length - 1; int ans = -1; while (s <= e) { int mid = s + e >> 1; long t = pow(k, mid); if (a[mid] >= k) { ans = mid; e = mid - 1; } else s = mid + 1; } return ans; } static ArrayList<Integer> primes; static int[] isComposite; static void sieve(int N) // O(N log log N) { isComposite = new int[N + 1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); for (int i = 2; i <= N; ++i) // can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) // can loop in 2 and odd integers for slightly better performance { primes.add(i); if (1l * i * i <= N) for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in // modified sieve isComposite[j] = 1; } } static int numDiv(int N) // O(sqrt(N) / ln sqrt(N)) { // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); int ans = 1; while (p * p <= N) { int c = 1; while (N % p == 0) { c++; N /= p; } ans *= c; p = primes.get(++idx); } if (N != 1) // last prime factor may be > sqrt(N) ans *= 2; // for integers whose largest prime factor has a power of 1 return ans; } static ArrayList<Integer> primeFactors(int N) // O(sqrt(N) / ln sqrt(N)) { ArrayList<Integer> factors = new ArrayList<Integer>(); // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while (p * p <= N) { 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 countD(int x) { int c = 0; while (x != 0) { x /= 10; c++; } return c; } static class triple implements Comparable<triple> { int x; int y; int z; public triple(int a, int b, int c) { x = a; y = b; z = c; } public int compareTo(triple o) { return x - o.x; } public String toString() { // TODO Auto-generated method stub return x + " " + y + " " + z; } } static int[] nxtarr() throws IOException { StringTokenizer st = new StringTokenizer(br.readLine()); int[] a = new int[st.countTokens()]; for (int i = 0; i < a.length; i++) { a[i] = Integer.parseInt(st.nextToken()); } return a; } static long pow(long a, long e, int mod) // O(log e) { long res = 1; while (e > 0) { if ((e & 1) == 1) { res *= a; res %= mod; } a *= a; a %= mod; e >>= 1; } return res; } static long pow(long a, long e) // O(log e) { long res = 1; while (e > 0) { if ((e & 1) == 1) { res *= a; } a *= a; e >>= 1; } return res; } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Locale; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { new Thread(null, new Runnable() { public void run() { try { long prevTime = System.currentTimeMillis(); new Main().run(); System.err.println("Total time: " + (System.currentTimeMillis() - prevTime) + " ms"); System.err.println("Memory status: " + memoryStatus()); } catch (IOException e) { e.printStackTrace(); } } }, "1", 1L << 24).start(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); Object o = solve(); if (o != null) out.println(o); out.close(); in.close(); } private Object solve() throws IOException { int n = ni(); int[] arr = nia(n); int nn = n; List<Integer> list = new ArrayList<Integer>(); for(int i =2;i*i<=nn;i++){ if(nn%i==0){ list.add(i); while(nn%i==0) nn/=i; } } if(nn!=1) list.add(nn); if(n%4==0) list.add(4); if(list.get(0)==2) list.remove(0); boolean[][] ok = new boolean[list.size()][]; for(int i =0;i<ok.length;i++){ ok[i]= new boolean[n/list.get(i)]; Arrays.fill(ok[i], true); } for(int i =0;i<n;i++){ if(arr[i]==0){ for(int j =0;j<ok.length;j++){ ok[j][i%(n/list.get(j))]=false; } } } for(boolean[] bs : ok) for(boolean b : bs) if(b) return "YES"; return "NO"; } BufferedReader in; PrintWriter out; StringTokenizer strTok = new StringTokenizer(""); String nextToken() throws IOException { while (!strTok.hasMoreTokens()) strTok = new StringTokenizer(in.readLine()); return strTok.nextToken(); } int ni() throws IOException { return Integer.parseInt(nextToken()); } long nl() throws IOException { return Long.parseLong(nextToken()); } double nd() throws IOException { return Double.parseDouble(nextToken()); } int[] nia(int size) throws IOException { int[] ret = new int[size]; for (int i = 0; i < size; i++) ret[i] = ni(); return ret; } long[] nla(int size) throws IOException { long[] ret = new long[size]; for (int i = 0; i < size; i++) ret[i] = nl(); return ret; } double[] nda(int size) throws IOException { double[] ret = new double[size]; for (int i = 0; i < size; i++) ret[i] = nd(); return ret; } String nextLine() throws IOException { strTok = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!strTok.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; strTok = new StringTokenizer(s); } return false; } void printRepeat(String s, int count) { for (int i = 0; i < count; i++) out.print(s); } void printArray(int[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(long[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array, String spec) { for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.printf(Locale.US, spec, array[i]); } out.println(); } void printArray(Object[] array) { boolean blank = false; for (Object x : array) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } @SuppressWarnings("rawtypes") void printCollection(Collection collection) { boolean blank = false; for (Object x : collection) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } static String memoryStatus() { return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB"; } public void pln() { out.println(); } public void pln(int arg) { out.println(arg); } public void pln(long arg) { out.println(arg); } public void pln(double arg) { out.println(arg); } public void pln(String arg) { out.println(arg); } public void pln(boolean arg) { out.println(arg); } public void pln(char arg) { out.println(arg); } public void pln(float arg) { out.println(arg); } public void pln(Object arg) { out.println(arg); } public void p(int arg) { out.print(arg); } public void p(long arg) { out.print(arg); } public void p(double arg) { out.print(arg); } public void p(String arg) { out.print(arg); } public void p(boolean arg) { out.print(arg); } public void p(char arg) { out.print(arg); } public void p(float arg) { out.print(arg); } public void p(Object arg) { out.print(arg); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int n; int arr[100005]; bool go(int i) { int t = 1; for (int j = 0; j < i; j++) { t = 1; for (int k = j; k < n; k += i) { t &= arr[k]; if (!t) break; } if (t) { return true; } } return false; } int main() { int i, j, k; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &arr[i]); for (i = 1; i * i <= n; i++) { if (n % i == 0 && n / i > 2) { int t = n / i; if (go(i)) return cout << "YES" << endl, 0; if (n / t > 2) { if (go(t)) return cout << "YES" << endl, 0; } } } cout << "NO" << endl; return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.util.*; public class a { public static void main(String[] argrs) { Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<Integer> factors = new ArrayList<Integer>(); for(int i=1; i<=n/3; i++) if(n%i==0) factors.add(i); boolean[] v = new boolean[n]; for(int i=0; i<n; i++) v[i] = (in.nextInt()==1); boolean works = false; //for each factor for(int i=0; i<factors.size(); i++) { int f = factors.get(i); //for each offset for(int j=0; j<f; j++) { boolean good = true; for(int k=0; k<n/f; k++) { if(!v[k*f+j]) { good = false; break; } } if(good) { works = true; break; } } if(works) break; } if(works) System.out.println("YES"); else System.out.println("NO"); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C71 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean[] k = new boolean[n]; int cnt = 0; for(int i = 0;i<n;i++) { k[i] = sc.nextInt()==1?true:false; cnt += (k[i]?1:0); } if(cnt==n) { System.out.println("YES"); return; } for(int i = 2;i<=n/3;i++) if(n%i==0) for(int begin = 0;begin<n;begin++) if(k[begin]) { boolean flag = true; int j = 0; for(j = 0;j<n;j+=i) if(!k[(j+begin)%n]) { flag = false; break; } j -= i; // if(flag) // System.out.println(i); if(flag&n-j==i) { System.out.println("YES"); return; } } System.out.println("NO"); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import math n = int(input()) ar = list(map(int,input().split())) def check(d): if n//d<=2: return 0 for i in range(d): for j in range(i,n,d): if not ar[j]: break else: return 1 return 0 root = int(math.sqrt(n)) for i in range(1,root+1): if n%i==0 and (check(i) or check(n//i) ) : print('YES') break else: print('NO') # N = n+3 # sieve = [0]*N # for i in range(2,N): # if sieve[i]==0: # for j in range(i*i,N,i): # sieve[j] = i # pf = set() # while(sieve[n]): # pf |= {sieve[n]} # n = n//sieve[n] # pf |= {n} # pf -= {2} # if not n%4: # pf |= {4} # for p in pf: # if check(p): # print('YES') # break # else: # print('NO') # C:\Users\Usuario\HOME2\Programacion\ACM
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.*; public class icpc { public static void main(String[] args) throws IOException { Reader in = new Reader(); //BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = in.nextInt(); int[] A = new int[n]; for (int i=0;i<n;i++) A[i] = in.nextInt(); boolean flag = false; for (int i=1;i*i <= n;i++) { if (n % i == 0) { if (check(A, i) || check(A, n / i)) { flag = true; break; } } } if (flag) System.out.println("YES"); else System.out.println("NO"); } public static boolean check(int[] A, int k) { int n = A.length; if (n == k || k * 2 == n) return false; for (int i=0;i<k;i++) { boolean f = true; for (int j=i;j<n;j+=k) { if (A[j] == 0) { f = false; break; } } if (f) return true; } return false; } } class Game implements Comparable<Game> { long x; int index; public Game(long x, int index) { this.x = x; this.index = index; } @Override public int compareTo(Game ob) { if (this.x < ob.x) return -1; else if (this.x > ob.x) return 1; return 0; } } class DSU { int[] parent; int[] size; //Pass number of total nodes as parameter to the constructor DSU(int n) { this.parent = new int[n]; this.size = new int[n]; Arrays.fill(parent, -1); } public void makeSet(int v) { parent[v] = v; size[v] = 1; } public int findSet(int v) { if (v == parent[v]) return v; return parent[v] = findSet(parent[v]); } public void unionSets(int a, int b) { a = findSet(a); b = findSet(b); if (a != b) { if (size[a] < size[b]) { int temp = a; a = b; b = temp; } parent[b] = a; size[a] += size[b]; } } } class FastFourierTransform { private void fft(double[] a, double[] b, boolean invert) { int count = a.length; for (int i = 1, j = 0; i < count; i++) { int bit = count >> 1; for (; j >= bit; bit >>= 1) j -= bit; j += bit; if (i < j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; temp = b[i]; b[i] = b[j]; b[j] = temp; } } for (int len = 2; len <= count; len <<= 1) { int halfLen = len >> 1; double angle = 2 * Math.PI / len; if (invert) angle = -angle; double wLenA = Math.cos(angle); double wLenB = Math.sin(angle); for (int i = 0; i < count; i += len) { double wA = 1; double wB = 0; for (int j = 0; j < halfLen; j++) { double uA = a[i + j]; double uB = b[i + j]; double vA = a[i + j + halfLen] * wA - b[i + j + halfLen] * wB; double vB = a[i + j + halfLen] * wB + b[i + j + halfLen] * wA; a[i + j] = uA + vA; b[i + j] = uB + vB; a[i + j + halfLen] = uA - vA; b[i + j + halfLen] = uB - vB; double nextWA = wA * wLenA - wB * wLenB; wB = wA * wLenB + wB * wLenA; wA = nextWA; } } } if (invert) { for (int i = 0; i < count; i++) { a[i] /= count; b[i] /= count; } } } public long[] multiply(long[] a, long[] b) { int resultSize = Integer.highestOneBit(Math.max(a.length, b.length) - 1) << 2; resultSize = Math.max(resultSize, 1); double[] aReal = new double[resultSize]; double[] aImaginary = new double[resultSize]; double[] bReal = new double[resultSize]; double[] bImaginary = new double[resultSize]; for (int i = 0; i < a.length; i++) aReal[i] = a[i]; for (int i = 0; i < b.length; i++) bReal[i] = b[i]; fft(aReal, aImaginary, false); fft(bReal, bImaginary, false); for (int i = 0; i < resultSize; i++) { double real = aReal[i] * bReal[i] - aImaginary[i] * bImaginary[i]; aImaginary[i] = aImaginary[i] * bReal[i] + bImaginary[i] * aReal[i]; aReal[i] = real; } fft(aReal, aImaginary, true); long[] result = new long[resultSize]; for (int i = 0; i < resultSize; i++) result[i] = Math.round(aReal[i]); return result; } } class NumberTheory { public boolean isPrime(long n) { if(n < 2) return false; for(long x = 2;x * x <= n;x++) { if(n % x == 0) return false; } return true; } public ArrayList<Long> primeFactorisation(long n) { ArrayList<Long> f = new ArrayList<>(); for(long x=2;x * x <= n;x++) { while(n % x == 0) { f.add(x); n /= x; } } if(n > 1) f.add(n); return f; } public int[] sieveOfEratosthenes(int n) { //Returns an array with the smallest prime factor for each number and primes marked as 0 int[] sieve = new int[n + 1]; for(int x=2;x * x <= n;x++) { if(sieve[x] != 0) continue; for(int u=x*x;u<=n;u+=x) { if(sieve[u] == 0) { sieve[u] = x; } } } return sieve; } public long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } public long phi(long n) { double result = n; for(long p=2;p*p<=n;p++) { if(n % p == 0) { while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (double)p)); } } if(n > 1) result *= (1.0 - (1.0 / (double)n)); return (long)result; } public Name extendedEuclid(long a, long b) { if(b == 0) return new Name(a, 1, 0); Name n1 = extendedEuclid(b, a % b); Name n2 = new Name(n1.d, n1.y, n1.x - (long)Math.floor((double)a / b) * n1.y); return n2; } public long modularExponentiation(long a, long b, long n) { long d = 1L; String bString = Long.toBinaryString(b); for(int i=0;i<bString.length();i++) { d = (d * d) % n; if(bString.charAt(i) == '1') d = (d * a) % n; } return d; } } class Name { long d; long x; long y; public Name(long d, long x, long y) { this.d = d; this.x = x; this.y = y; } } class SuffixArray { int ALPHABET_SZ = 256, N; int[] T, lcp, sa, sa2, rank, tmp, c; public SuffixArray(String str) { this(toIntArray(str)); } private static int[] toIntArray(String s) { int[] text = new int[s.length()]; for (int i = 0; i < s.length(); i++) text[i] = s.charAt(i); return text; } public SuffixArray(int[] text) { T = text; N = text.length; sa = new int[N]; sa2 = new int[N]; rank = new int[N]; c = new int[Math.max(ALPHABET_SZ, N)]; construct(); kasai(); } private void construct() { int i, p, r; for (i = 0; i < N; ++i) c[rank[i] = T[i]]++; for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1]; for (i = N - 1; i >= 0; --i) sa[--c[T[i]]] = i; for (p = 1; p < N; p <<= 1) { for (r = 0, i = N - p; i < N; ++i) sa2[r++] = i; for (i = 0; i < N; ++i) if (sa[i] >= p) sa2[r++] = sa[i] - p; Arrays.fill(c, 0, ALPHABET_SZ, 0); for (i = 0; i < N; ++i) c[rank[i]]++; for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1]; for (i = N - 1; i >= 0; --i) sa[--c[rank[sa2[i]]]] = sa2[i]; for (sa2[sa[0]] = r = 0, i = 1; i < N; ++i) { if (!(rank[sa[i - 1]] == rank[sa[i]] && sa[i - 1] + p < N && sa[i] + p < N && rank[sa[i - 1] + p] == rank[sa[i] + p])) r++; sa2[sa[i]] = r; } tmp = rank; rank = sa2; sa2 = tmp; if (r == N - 1) break; ALPHABET_SZ = r + 1; } } private void kasai() { lcp = new int[N]; int[] inv = new int[N]; for (int i = 0; i < N; i++) inv[sa[i]] = i; for (int i = 0, len = 0; i < N; i++) { if (inv[i] > 0) { int k = sa[inv[i] - 1]; while ((i + len < N) && (k + len < N) && T[i + len] == T[k + len]) len++; lcp[inv[i] - 1] = len; if (len > 0) len--; } } } } class ZAlgorithm { public int[] calculateZ(char input[]) { int Z[] = new int[input.length]; int left = 0; int right = 0; for(int k = 1; k < input.length; k++) { if(k > right) { left = right = k; while(right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } else { //we are operating inside box int k1 = k - left; //if value does not stretches till right bound then just copy it. if(Z[k1] < right - k + 1) { Z[k] = Z[k1]; } else { //otherwise try to see if there are more matches. left = k; while(right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } } } return Z; } public ArrayList<Integer> matchPattern(char text[], char pattern[]) { char newString[] = new char[text.length + pattern.length + 1]; int i = 0; for(char ch : pattern) { newString[i] = ch; i++; } newString[i] = '$'; i++; for(char ch : text) { newString[i] = ch; i++; } ArrayList<Integer> result = new ArrayList<>(); int Z[] = calculateZ(newString); for(i = 0; i < Z.length ; i++) { if(Z[i] == pattern.length) { result.add(i - pattern.length - 1); } } return result; } } class KMPAlgorithm { public int[] computeTemporalArray(char[] pattern) { int[] lps = new int[pattern.length]; int index = 0; for(int i=1;i<pattern.length;) { if(pattern[i] == pattern[index]) { lps[i] = index + 1; index++; i++; } else { if(index != 0) { index = lps[index - 1]; } else { lps[i] = 0; i++; } } } return lps; } public ArrayList<Integer> KMPMatcher(char[] text, char[] pattern) { int[] lps = computeTemporalArray(pattern); int j = 0; int i = 0; int n = text.length; int m = pattern.length; ArrayList<Integer> indices = new ArrayList<>(); while(i < n) { if(pattern[j] == text[i]) { i++; j++; } if(j == m) { indices.add(i - j); j = lps[j - 1]; } else if(i < n && pattern[j] != text[i]) { if(j != 0) j = lps[j - 1]; else i = i + 1; } } return indices; } } class Hashing { public long[] computePowers(long p, int n, long m) { long[] powers = new long[n]; powers[0] = 1; for(int i=1;i<n;i++) { powers[i] = (powers[i - 1] * p) % m; } return powers; } public long computeHash(String s) { long p = 31; long m = 1_000_000_009; long hashValue = 0L; long[] powers = computePowers(p, s.length(), m); for(int i=0;i<s.length();i++) { char ch = s.charAt(i); hashValue = (hashValue + (ch - 'a' + 1) * powers[i]) % m; } return hashValue; } } class BasicFunctions { public long min(long[] A) { long min = Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min = Math.min(min, A[i]); } return min; } public long max(long[] A) { long max = Long.MAX_VALUE; for(int i=0;i<A.length;i++) { max = Math.max(max, A[i]); } return max; } } class MergeSortInt { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } } class MergeSortLong { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } } class Node { int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object ob) { if(ob == null) return false; if(!(ob instanceof Node)) return false; if(ob == this) return true; Node obj = (Node)ob; if(this.x == obj.x && this.y == obj.y) return true; return false; } @Override public int hashCode() { return (int)this.x; } } 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(); } } class FenwickTree { public void update(long[] fenwickTree,long delta,int index) { index += 1; while(index < fenwickTree.length) { fenwickTree[index] += delta; index = index + (index & (-index)); } } public long prefixSum(long[] fenwickTree,int index) { long sum = 0L; index += 1; while(index > 0) { sum += fenwickTree[index]; index -= (index & (-index)); } return sum; } } class SegmentTree { public int nextPowerOfTwo(int num) { if(num == 0) return 1; if(num > 0 && (num & (num - 1)) == 0) return num; while((num &(num - 1)) > 0) { num = num & (num - 1); } return num << 1; } public int[] createSegmentTree(int[] input) { int np2 = nextPowerOfTwo(input.length); int[] segmentTree = new int[np2 * 2 - 1]; for(int i=0;i<segmentTree.length;i++) segmentTree[i] = Integer.MIN_VALUE; constructSegmentTree(segmentTree,input,0,input.length-1,0); return segmentTree; } private void constructSegmentTree(int[] segmentTree,int[] input,int low,int high,int pos) { if(low == high) { segmentTree[pos] = input[low]; return; } int mid = (low + high)/ 2; constructSegmentTree(segmentTree,input,low,mid,2*pos + 1); constructSegmentTree(segmentTree,input,mid+1,high,2*pos + 2); segmentTree[pos] = Math.max(segmentTree[2*pos + 1],segmentTree[2*pos + 2]); } public int rangeMinimumQuery(int []segmentTree,int qlow,int qhigh,int len) { return rangeMinimumQuery(segmentTree,0,len-1,qlow,qhigh,0); } private int rangeMinimumQuery(int segmentTree[],int low,int high,int qlow,int qhigh,int pos) { if(qlow <= low && qhigh >= high){ return segmentTree[pos]; } if(qlow > high || qhigh < low){ return Integer.MIN_VALUE; } int mid = (low+high)/2; return Math.max(rangeMinimumQuery(segmentTree, low, mid, qlow, qhigh, 2 * pos + 1), rangeMinimumQuery(segmentTree, mid + 1, high, qlow, qhigh, 2 * pos + 2)); } } class Trie { private class TrieNode { Map<Character, TrieNode> children; boolean endOfWord; public TrieNode() { children = new HashMap<>(); endOfWord = false; } } private final TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = current.children.get(ch); if (node == null) { node = new TrieNode(); current.children.put(ch, node); } current = node; } current.endOfWord = true; } public boolean search(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = current.children.get(ch); if (node == null) { return false; } current = node; } return current.endOfWord; } public void delete(String word) { delete(root, word, 0); } private boolean delete(TrieNode current, String word, int index) { if (index == word.length()) { if (!current.endOfWord) { return false; } current.endOfWord = false; return current.children.size() == 0; } char ch = word.charAt(index); TrieNode node = current.children.get(ch); if (node == null) { return false; } boolean shouldDeleteCurrentNode = delete(node, word, index + 1); if (shouldDeleteCurrentNode) { current.children.remove(ch); return current.children.size() == 0; } return false; } } class SegmentTreeLazy { public int nextPowerOfTwo(int num) { if(num == 0) return 1; if(num > 0 && (num & (num - 1)) == 0) return num; while((num &(num - 1)) > 0) { num = num & (num - 1); } return num << 1; } public int[] createSegmentTree(int input[]) { int nextPowOfTwo = nextPowerOfTwo(input.length); int segmentTree[] = new int[nextPowOfTwo*2 -1]; for(int i=0; i < segmentTree.length; i++){ segmentTree[i] = Integer.MAX_VALUE; } constructMinSegmentTree(segmentTree, input, 0, input.length - 1, 0); return segmentTree; } private void constructMinSegmentTree(int segmentTree[], int input[], int low, int high,int pos) { if(low == high) { segmentTree[pos] = input[low]; return; } int mid = (low + high)/2; constructMinSegmentTree(segmentTree, input, low, mid, 2 * pos + 1); constructMinSegmentTree(segmentTree, input, mid + 1, high, 2 * pos + 2); segmentTree[pos] = Math.min(segmentTree[2*pos+1], segmentTree[2*pos+2]); } public void updateSegmentTreeRangeLazy(int input[], int segmentTree[], int lazy[], int startRange, int endRange, int delta) { updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, 0, input.length - 1, 0); } private void updateSegmentTreeRangeLazy(int segmentTree[], int lazy[], int startRange, int endRange, int delta, int low, int high, int pos) { if(low > high) { return; } if (lazy[pos] != 0) { segmentTree[pos] += lazy[pos]; if (low != high) { lazy[2 * pos + 1] += lazy[pos]; lazy[2 * pos + 2] += lazy[pos]; } lazy[pos] = 0; } if(startRange > high || endRange < low) { return; } if(startRange <= low && endRange >= high) { segmentTree[pos] += delta; if(low != high) { lazy[2*pos + 1] += delta; lazy[2*pos + 2] += delta; } return; } int mid = (low + high)/2; updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, low, mid, 2*pos+1); updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, mid+1, high, 2*pos+2); segmentTree[pos] = Math.min(segmentTree[2*pos + 1], segmentTree[2*pos + 2]); } public int rangeMinimumQueryLazy(int segmentTree[], int lazy[], int qlow, int qhigh, int len) { return rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, 0, len - 1, 0); } private int rangeMinimumQueryLazy(int segmentTree[], int lazy[], int qlow, int qhigh, int low, int high, int pos) { if(low > high) { return Integer.MAX_VALUE; } if (lazy[pos] != 0) { segmentTree[pos] += lazy[pos]; if (low != high) { lazy[2 * pos + 1] += lazy[pos]; lazy[2 * pos + 2] += lazy[pos]; } lazy[pos] = 0; } if(qlow > high || qhigh < low) { return Integer.MAX_VALUE; } if(qlow <= low && qhigh >= high) { return segmentTree[pos]; } int mid = (low+high)/2; return Math.min(rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, low, mid, 2 * pos + 1), rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, mid + 1, high, 2 * pos + 2)); } }
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.*; public class j { public static void main(String aa[])throws IOException { BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); int flag=0,p=0,i=0,n=0,j=0,m=0; String s; m=Integer.parseInt(b.readLine()); int d[]=new int[m]; s=b.readLine(); StringTokenizer z=new StringTokenizer(s); for(i=0;i<m;i++) d[i]=Integer.parseInt(z.nextToken()); for(i=1;m/i>2;i++) { if(m%i==0) { for(j=0;j<i;j++) { for(p=j;p<m;p+=i) if(d[p]==0) { flag=1; break; } if(flag==0) { System.out.print("YES"); System.exit(0); } flag=0; } } } System.out.print("NO"); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
import java.io.*; import java.util.*; public class C { private static int numbers[], n; public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); /* 6 1 0 1 1 1 0 */ n = in.nextInt(); HashSet<Integer> valid = new HashSet<>(); for (int i = 2; i <= n / 3; ++i) { if (n % i == 0) { valid.add(i); } } /// System.out.println(valid) ; numbers = new int[n]; int z = 0; int consequtive = 0; for (int i = 0; i < n; ++i) { int temp = in.nextInt(); numbers[i] = temp; if (temp == 0) { ++z; ++consequtive; } else { consequtive = 0; } if (valid.contains(consequtive)) { valid.remove(consequtive); } } if (z == 0) { out.println("YES"); out.close(); return; } if (n - z < 3) { out.println("NO"); out.close(); return; } Iterator itr = valid.iterator(); out.println(process(itr)); out.close(); } private static String process(Iterator itr) { while (itr.hasNext()) { int vTemp = (int) itr.next(); boolean s = true; for (int x = 0; x < vTemp; ++x) { s = true; for (int z = x ; z < n; z += vTemp) { if (numbers[z] == 0) { s = false; break; } } if (s) { break; } } if (s) { return "YES"; } } return "NO"; } } 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()); } 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
#include <bits/stdc++.h> using namespace std; set<int> primeFactors(int n) { set<int> s; if (n % 4 == 0) { s.insert(4); n /= 4; } while (n % 2 == 0) n /= 2; for (int i = 3; i <= sqrt(n); i = i + 2) while (n % i == 0) { s.insert(i); n = n / i; } if (n > 2) s.insert(n); return s; } int main() { ios::sync_with_stdio(false); int n, m; cin >> n; vector<bool> ms; for (int i = 0, _i = (n); i < _i; ++i) { cin >> m; ms.push_back(m); } set<int> ps = primeFactors(n); bool found = false; for (decltype(ps.begin()) p = ps.begin(); p != ps.end(); p++) { found = false; for (int i = 0, _i = (n / *p); i < _i; ++i) { found = true; for (int j = 0, _j = (*p); j < _j; ++j) { found = found && ms[i + (n / *p) * j]; if (!found) break; } if (found) break; } if (found) break; } cout << (found ? "YES" : "NO"); }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b) { if (b == 0) return 1; long long int p = power(a, b / 2); if (b % 2 == 0) return p * p; else return a * p * p; } long int gcd(long int a, long int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { std::ios_base::sync_with_stdio(false); long long int n, i, j, k, c = 0; cin >> n; int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } vector<long int> fac; for (i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (i > 2) fac.push_back(i); if (n != i * i) fac.push_back(n / i); } } for (i = 0; i < fac.size(); i++) { for (j = 0; j < n / fac[i]; j++) { c = 0; for (k = j; k < n; k += n / fac[i]) { if (a[k] != 1) break; else c++; } if (c == fac[i]) break; } if (j != n / fac[i]) break; } if (i != fac.size()) 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
#include <bits/stdc++.h> using namespace std; int b[1000005], a[1000006], n; bool check(int k) { int i, j, x; for (i = 0; i < n / k; i++) { for (j = 1, x = i; j <= k; j++, x += n / k) if (a[x] == 0) break; if (j > k) return 1; } return 0; } int main() { int i, j; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 4; i <= n; i += 2) b[i] = 1; for (i = 3; i * i <= n; i++) if (b[i] == 0) for (j = i * i; j <= n; j += 2 * i) b[j] = 1; for (i = 3; i <= n; i++) if (n % i == 0 && b[i] == 0) { if (check(i)) { printf("YES\n"); return 0; } } if (n % 4 == 0 && check(4)) { printf("YES\n"); return 0; } printf("NO\n"); return 0; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; const int OO = (int)1e9; int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; long long n, z, cnt, x, y; vector<long long> divisors; vector<int> a; vector<long long> generate_divisors(long long n) { vector<long long> v; long long i; for (i = 1; i * i < n; ++i) if (n % i == 0) v.push_back(i), v.push_back(n / i); if (i * i == n) v.push_back(i); return v; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; a.resize(n); for (int i = 0; i < n; i++) { cin >> a[i]; } divisors = generate_divisors(n); bool e = 1; for (int i = 0; i < ((long long)((divisors).size())) && e; i++) { x = divisors[i]; y = 0; while (e && y < x) { cnt = 0; for (int j = y; j < n; j += x) { if (a[j] == 1) { cnt++; } else { break; } } if ((cnt == (n / x)) && (cnt >= 3)) { e = 0; } y++; } } if (!e) { 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.util.*; public class test { static int moods[]; static int n; static boolean flag=false; public static int solver(int a){ if(a<3) return 0; int cnt=0; for(int i=0;i<n/a;i++){ for(int j=i;j<n;j+=n/a){ if(moods[j]==1) cnt++; } if(cnt==a){ flag=true; //System.out.println(i + " "+n/a); } cnt=0; } return 0; } public static void main(String [] args){ Scanner sc = new Scanner(System.in); n = sc.nextInt(); int cnt=0; moods = new int[n]; for(int i=0;i<n;i++){ moods[i] = sc.nextInt(); } for(int i=1;i<=Math.sqrt(n);i++){ if(i==1||i==2 && n%i==0) cnt=solver(n/i); else if(n%i==0){ cnt=solver(i); cnt=solver(n/i); } } if(flag) System.out.print("YES"); else System.out.print("NO"); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; vector<int> a(n); for (auto& i : a) cin >> i; for (int x = 1; x * x <= n; x++) { if (n % x) continue; for (auto d : {x, n / x}) for (int from = 0; from < d; from++) { bool ok = 1; int sides = 0; for (int i = from; i < n; i += d) { ok &= a[i]; sides++; } if (ok) { if (sides >= 3) { cout << "YES" << endl; return 0; } } } } cout << "NO" << endl; exit(EXIT_SUCCESS); }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int n; int a[100005]; int main() { while (cin >> n) { for (int i = 0; i < n; i++) cin >> a[i]; int x = n; bool res = false; for (int i = 1; i <= n / 3; i++) { if (n % i) continue; for (int j = 0; j < i; j++) { bool find = true; int k = j; while (k < n + i) { int x = k % n; if (!a[x]) { find = false; break; } k += i; } if (find) { res = true; break; } } if (res) break; } if (res) cout << "YES"; else cout << "NO"; cout << endl; } }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; void solve(){}; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; bool a[n]; int cnt = 0; vector<int> ps; for (int i = 0; i < n; ++i) { int b; cin >> b; a[i] = b; if (b) ++cnt; } int num = n; if (num % 4 == 0) ps.push_back(4); while (num % 2 == 0) num /= 2; for (int i = 3; i * i <= num; ++i) { if (num % i == 0) ps.push_back(i); while (num % i == 0) num /= i; } if (num > 1) ps.push_back(num); for (auto& p : ps) { for (int i = 0; i < n / p; ++i) { bool found = 1; for (int j = 0; j < p; ++j) { int idx = i + j * n / p; if (!a[idx]) { found = 0; break; } } if (found) { 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
/** * * @author xerxes */ import java.io.*; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; public class C { StringTokenizer st ; BufferedReader bf; int n; int[] ar; public static void main(String[] args){ C ob = new C(); ob.run(); System.exit(0); } public void doit(){ for(int i=1;i<=n;++i){ if(n%i==0){ int k = n/i; if( k >=3 ){ int end = i+1; // System.out.println(i+" "+k+" "+end); for(int j=1;j<end;++j){ // System.out.print(j+" : "); int tot=0; for(int l=j;l<=n;l+=i){ if(ar[l-1]==1){++tot; } else{ break; } } // System.out.println(tot); if(tot==k){ System.out.println("YES"); return ; } } } } } System.out.println("NO"); } public void read(){ bf = new BufferedReader( new InputStreamReader(System.in)); st = new StringTokenizer(""); try { n = nextInt(); ar = new int[n]; for (int i = 0; i < n; ++i) { ar[i] = nextInt(); } } catch (IOException ex) { Logger.getLogger(C.class.getName()).log(Level.SEVERE, null, ex); } } public void run(){ read(); doit(); } String nextToken() throws IOException{ while(!st.hasMoreTokens() ){ String line = bf.readLine().trim(); if(line==null)line="0"; st = new StringTokenizer(line); } 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collections; import java.util.HashSet; import java.util.StringTokenizer; import java.util.TreeSet; public class zizo{ public static void main (String args[]) throws IOException { Scanner zizo=new Scanner (System.in); PrintWriter wr = new PrintWriter(System.out); int n = zizo.nextInt(); ArrayList<Integer> divs = new ArrayList<Integer>(); for(int i = 1; i * i <= n; ++i) if(n % i == 0) { divs.add(i); if(n / i != i) divs.add(n / i); } HashSet<Integer>set = new HashSet<>(); for(int i = 0;i < n; i++) if(zizo.nextInt() == 1) set.add(i); all : for(int i : divs) { if(n / i>=3) { x : for(int j = 0;j < n; j++) { for(int k = 0;k < n/i;k++) { if(!set.contains((j + i * k) % n)) continue x; } System.out.println("YES"); return; } } }System.err.println(divs.toString()); System.err.println(set.toString()); wr.println("NO"); wr.close(); } } 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 double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } }
JAVA
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> using namespace std; int a[100002]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } int p; for (int i = 1; i <= n / 3; i++) { if (n % i == 0) { for (int j = 1; j <= i; j++) { if (a[j]) { p = 1; for (int k = j; k <= n; k += i) { p &= a[k]; } if (p) { cout << "YES" << endl; return 0; } } } } } cout << "NO" << endl; }
CPP
71_C. Round Table Knights
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. <image> King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not. Input The first line contains number n, which is the number of knights at the round table (3 ≤ n ≤ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood. Output Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO". Examples Input 3 1 1 1 Output YES Input 6 1 0 1 1 1 0 Output YES Input 6 1 0 0 1 0 1 Output NO
2
9
#include <bits/stdc++.h> int a[100001]; int main() { int i, j = 1, k, l, m = 0, n; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (i = 1; n / i >= 3; i++) { if (n % i != 0) continue; for (k = 1; k <= i; k++) { for (j = k; j <= n; j += i) { if (a[j] == 0) break; if (j + i > n) { m = 1; } } if (m == 1) break; } if (m == 1) break; } if (m == 1) printf("YES\n"); else printf("NO\n"); return 0; }
CPP