exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
4837148d8c5182734f724bf3983248e0
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&10&11\\ 11&12&14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&1\\ 2&3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static void W(Object s){ System.out.print(s.toString()); } static void println(Object s){ System.out.println(s.toString()); } static void inputArr(int[] arr, int n){ for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } } static void printArr(int[] arr){ for(int i=0;i<arr.length;i++){ W(arr[i]+" "); } } static Scanner sc = new Scanner(System.in); static void R(Integer a){ a = sc.nextInt();} static void R(Double a){ a = sc.nextDouble();} static void R(Long a){ a = sc.nextLong();} static void R(String a){ a = sc.nextLine();} static void swap(int a[][], int b[][], int i,int j){ int temp = a[i][j]; a[i][j] = b[i][j]; b[i][j] = temp; } public static void main (String[] args) throws java.lang.Exception { int n=0,h=0,m=0; n = sc.nextInt(); m = sc.nextInt(); int arr[][] = new int[n][m]; int brr[][] = new int[n][m]; for(int i=0;i<n;i++){ for(int j = 0;j<m;j++){ arr[i][j] = sc.nextInt(); } } for(int i=0;i<n;i++){ for(int j = 0;j<m;j++){ brr[i][j] = sc.nextInt(); } } int y = 0; for(int i=0;i<n && y==0;i++){ for(int j = 0;j<m && y==0;j++){ if(i==0 && j==0){ continue; } if(i>0){ int c = arr[i][j]; int d = brr[i][j]; if(c<=arr[i-1][j] || d<=brr[i-1][j]){ swap(arr,brr,i,j); } if(arr[i][j]<=arr[i-1][j] || brr[i][j]<=brr[i-1][j]){ W("Impossible"); y=1; break; } } if(j>0){ int c = arr[i][j]; int d = brr[i][j]; if(c<=arr[i][j-1] || d<=brr[i][j-1]){ swap(arr,brr,i,j); } if(arr[i][j]<=arr[i][j-1] || brr[i][j]<=brr[i][j-1]){ W("Impossible"); y=1; break; } } } } if(y==0) W("Possible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
f8a909133988c4fbe8eb6f8d471c2818
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.lang.*; import java.math.*; import java.util.*; import java.io.*; public class Main { void solve() { int n=ni(),m=ni(); int a[][]=new int[n+1][m+1]; int b[][]=new int[n+1][m+1]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=ni(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ b[i][j]=ni(); if(a[i][j]>b[i][j]){ int tmp=a[i][j]; a[i][j]=b[i][j]; b[i][j]=tmp; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if((a[i][j-1]<a[i][j] && a[i-1][j]<a[i][j] && b[i][j-1]<b[i][j] && b[i-1][j]<b[i][j])){ }else if((a[i][j-1]<b[i][j] && a[i-1][j]<b[i][j] && b[i][j-1]<a[i][j] && b[i-1][j]<a[i][j])){ int tmp=a[i][j]; a[i][j]=b[i][j]; b[i][j]=tmp; }else { pw.println("Impossible"); return; } } } pw.println("Possible"); } long M =(long)1e9+7; InputStream is; PrintWriter pw; String INPUT = ""; void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); pw = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); pw.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o)); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
5cb4f632ca3a225c69c27018af287b27
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.*; import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); long a[][]=new long[n][m]; long b[][]=new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) a[i][j]=sc.nextInt(); } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) b[i][j]=sc.nextInt(); } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[i][j]>b[i][j]) { long c=a[i][j]; a[i][j]=b[i][j]; b[i][j]=c; } } } int c=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((i!=0&&a[i][j]<=a[i-1][j])|| (j!=0&&a[i][j]<=a[i][j-1])|| (i!=0&&b[i][j]<=b[i-1][j])|| (j!=0&&b[i][j]<=b[i][j-1])) { c=1; break; } } } if(c==0) System.out.println("Possible"); else System.out.println("Impossible"); } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
db4a78c0385c34edca508c285f44dabb
train_003.jsonl
1556989500
You are given two $$$n \times m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$\begin{bmatrix} 9&amp;10&amp;11\\ 11&amp;12&amp;14\\ \end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$\begin{bmatrix} 1&amp;1\\ 2&amp;3\\ \end{bmatrix}$$$ is not increasing because the first row is not strictly increasing.Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".
256 megabytes
import java.io.*; import java.util.NoSuchElementException; public class Main_1162B { private static Scanner sc; private static Printer pr; private static void solve() { int n = sc.nextInt(); int m = sc.nextInt(); int[][] a = new int[n][m]; int[][] b = new int[n][m]; for (int i = 0; i < n; i++) { a[i] = sc.nextIntArray(m); } for (int i = 0; i < n; i++) { b[i] = sc.nextIntArray(m); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] > b[i][j]) { int tmp = a[i][j]; a[i][j] = b[i][j]; b[i][j] = tmp; } } } for (int i = 0; i < n; i++) { for (int j = 1; j < m; j++) { if (a[i][j] <= a[i][j - 1] || b[i][j] <= b[i][j - 1]) { pr.println("Impossible"); return; } } } for (int j = 0; j < m; j++) { for (int i = 1; i < n; i++) { if (a[i][j] <= a[i - 1][j] || b[i][j] <= b[i - 1][j]) { pr.println("Impossible"); return; } } } pr.println("Possible"); } // --------------------------------------------------- public static void main(String[] args) { sc = new Scanner(System.in); pr = new Printer(System.out); solve(); pr.close(); sc.close(); } static class Scanner { BufferedReader br; Scanner (InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } private boolean isPrintable(int ch) { return ch >= '!' && ch <= '~'; } private boolean isCRLF(int ch) { return ch == '\n' || ch == '\r' || ch == -1; } private int nextPrintable() { try { int ch; while (!isPrintable(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } return ch; } catch (IOException e) { throw new NoSuchElementException(); } } String next() { try { int ch = nextPrintable(); StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (isPrintable(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } int nextInt() { try { // parseInt from Integer.parseInt() boolean negative = false; int res = 0; int limit = -Integer.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Integer.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } int multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } long nextLong() { try { // parseLong from Long.parseLong() boolean negative = false; long res = 0; long limit = -Long.MAX_VALUE; int radix = 10; int fc = nextPrintable(); if (fc < '0') { if (fc == '-') { negative = true; limit = Long.MIN_VALUE; } else if (fc != '+') { throw new NumberFormatException(); } fc = br.read(); } long multmin = limit / radix; int ch = fc; do { int digit = ch - '0'; if (digit < 0 || digit >= radix) { throw new NumberFormatException(); } if (res < multmin) { throw new NumberFormatException(); } res *= radix; if (res < limit + digit) { throw new NumberFormatException(); } res -= digit; } while (isPrintable(ch = br.read())); return negative ? res : -res; } catch (IOException e) { throw new NoSuchElementException(); } } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { int ch; while (isCRLF(ch = br.read())) { if (ch == -1) { throw new NoSuchElementException(); } } StringBuilder sb = new StringBuilder(); do { sb.appendCodePoint(ch); } while (!isCRLF(ch = br.read())); return sb.toString(); } catch (IOException e) { throw new NoSuchElementException(); } } int[] nextIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = sc.nextInt(); } return ret; } int[][] nextIntArrays(int n, int m) { int[][] ret = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[j][i] = sc.nextInt(); } } return ret; } void close() { try { br.close(); } catch (IOException e) { // throw new NoSuchElementException(); } } } static class Printer extends PrintWriter { Printer(OutputStream out) { super(out); } void printInts(int... a) { StringBuilder sb = new StringBuilder(32); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } void printLongs(long... a) { StringBuilder sb = new StringBuilder(64); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } void printStrings(String... a) { StringBuilder sb = new StringBuilder(32); for (int i = 0, size = a.length; i < size; i++) { if (i > 0) { sb.append(' '); } sb.append(a[i]); } println(sb); } } }
Java
["2 2\n2 10\n11 5\n9 4\n3 12", "2 3\n2 4 5\n4 5 6\n3 6 7\n8 10 11", "3 2\n1 3\n2 4\n5 10\n3 1\n3 6\n4 8"]
1 second
["Possible", "Possible", "Impossible"]
NoteThe first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$\begin{bmatrix} 9&amp;10\\ 11&amp;12\\ \end{bmatrix}$$$ and $$$\begin{bmatrix} 2&amp;4\\ 3&amp;5\\ \end{bmatrix}$$$.In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
Java 8
standard input
[ "greedy", "brute force" ]
53e061fe3fbe3695d69854c621ab6037
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \ldots, a_{im}$$$ ($$$1 \leq a_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, \ldots, b_{im}$$$ ($$$1 \leq b_{ij} \leq 10^9$$$) — the number located in position $$$(i, j)$$$ in the second matrix.
1,400
Print a string "Impossible" or "Possible".
standard output
PASSED
9a6df4807339d1f63035cd8901581978
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.io.*; import java.util.*; public class C { 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 int isPrime(int n){ // for(int i=3;i<=n/2;i+=2){ // if(n%i==0) return 0; // } // return 1; // } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } static int[] findDiv(int n){ int[] ret = new int[2]; ret[0]=0;ret[1]=0; int i; if(isPrime(n/2)){ ret[0]=1; ret[1]=1; } else{ for(i=2;i<32;i++){ if(n==Math.pow(2, i)){ ret[0]=0; ret[1]=2; break; } } } return ret; } public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); String[] name = {"FastestFinger","Ashishgup"}; int n,ans; while(t>0){ ans=0; n=s.nextInt(); if(n==1) ans=0; else if(n%2==1) ans=1; else{ int[] rec = findDiv(n); if(rec[0]==1&&rec[1]==1) ans=0; else if(rec[0]==0&&rec[1]>1) ans=0; else ans=1; } System.out.println(name[ans]); t--; } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
c9e2ee85ae43af796cc85f9261e36406
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.util.*; import java.io.*; import java.lang.Math; public class Main{ static boolean isPrime(int n) { if (n == 1) { return false; } for (int i = 2; i*i <= n; i++) { if (n % i == 0) return false; } return true; } public static void main(String[] args){ FastScanner s = new FastScanner(System.in); int T= s.nextInt(); while(T-->0){ int n= s.nextInt(); int power2= 0; int powerodd=0; while(n%2==0){ power2++; n=n/2; } if(n==1){ powerodd=0; }else if(n>1){ if(isPrime(n)){ powerodd=1; }else powerodd=27; } // System.out.println(k); // System.out.println(power2+" "+powerodd); if(power2==0&&powerodd==0){ System.out.println("FastestFinger"); }else if(power2==0 && powerodd>0){ System.out.println("Ashishgup"); }else if(power2==1 &&( powerodd>1||powerodd==0)){ System.out.println("Ashishgup"); }else if(power2==1 && powerodd==1){ System.out.println("FastestFinger"); }else if(power2>1&&powerodd==0){ System.out.println("FastestFinger"); }else{ System.out.println("Ashishgup"); } } } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { 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()); } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
ddc496f624f781d7f71282efc82d0be6
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { long n = sc.nextLong(); if(n==2) { System.out.println("Ashishgup"); } else if(n==1) { System.out.println("FastestFinger"); } else if(n%2!=0) { System.out.println("Ashishgup"); } else { int count = 0; while(n%2==0) { n/=2; count++; } if(count>1) { if(n!=1) System.out.println("Ashishgup"); else System.out.println("FastestFinger"); } else { if(n!=1) { int flag = 0; for(int i = 3; i<=Math.sqrt(n);i++) { if(n%i==0) { flag = 1; break; } } if(flag == 1) System.out.println("Ashishgup"); else System.out.println("FastestFinger"); } } } } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
7e6844da4f3100280bf2e2961b3ba643
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Hashtable; import java.util.Scanner; import java.util.Set; import java.util.Vector; import javax.swing.plaf.synth.SynthSeparatorUI; public class game { public static int isPrime(int num) { int num2=num; for(int i=3;i<=Math.sqrt(num2);i+=2) { if(num2%i==0) { return 1;}} return -1;} public static void main(String[] args) { String a="Ashishgup"; String f="FastestFinger"; Scanner sc=new Scanner(System.in); int input=sc.nextInt(); Vector<Integer> in=new Vector(); for(int i=0;i<input;i++) { in.add(sc.nextInt()); } boolean moreTwos=false; for(int i=0;i<in.size();i++) { int current=in.get(i); if(current==1) { System.out.println("FastestFinger"); } else if(current==2) { System.out.println(a); } else if(current%2==1) { System.out.println(a);} else { int k=0; while(current%2==0) { current/=2; k++;} if(current==1) System.out.println(f); else { if(k>1) { System.out.println(a);} else if(isPrime(current)==-1) System.out.println(f); else System.out.println(a);}} } }}
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
4330b7346ae89dfc29af8aaf072b7ad3
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.math.BigInteger; import java.util.Scanner; public class Solution { public static void main(String[] args) { /* * 1 11 2 10000001001 */ Solution sl=new Solution(); Scanner scan = new Scanner(System.in); int c =scan.nextInt(); while(c-->0) { int n = scan.nextInt(); //int k = scan.nextInt(); if(n==1) System.out.println("FastestFinger"); else if(n%2==1 || n==2) System.out.println("Ashishgup"); else { if((n & (n-1)) ==0 || (n%4!=0 && isPrime(n/2))) System.out.println("FastestFinger"); else System.out.println("Ashishgup"); } } scan.close(); } static boolean isPrime(int n) { for(int i=2;i<Math.min(50000, n);i++) { if(n%i==0) return false; } return true; } /* * static final Random random=new Random(); * * static void ruffleSort(int[] a) { int n=a.length; //shuffle, then sort for * for(int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; * a[i]=temp; } Arrays.sort(a); } */ }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
999a0393864a1b3944f11f1891cf88b0
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Main { public static void test() { for (int i = 2; i < 100; i+=2) { System.out.println(i + " " + wins(i)); } } public static void main(String[] args) { //test(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { System.out.println(wins(sc.nextInt()) ? "Ashishgup" : "FastestFinger"); } } static HashMap<Integer, Boolean> dp = new HashMap<>(); public static boolean wins(int n) { if (n == 1){ return false; } if (n == 2) { return true; } if (n % 2 == 1) { return true; } if (dp.containsKey(n)) { return dp.get(n); } // win if there exist a factor such that wins(n / f) is false for (int i = 2; i <= Math.sqrt(n) + 1; i++) { if (n % i == 0) { int f1 = i; int f2 = n / i; if ((f1 % 2 == 1 && !wins(f2)) || (f2 % 2 == 1 && !wins(f1))){ dp.put(n, true); return true; } } } dp.put(n, false); return false; } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
110847772a999baea9fa70360b0ebdc9
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. ******************************************************************************/ import java.util.*; import java.io.*; public class Main { static long[] fibo = new long[45]; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter out = new PrintWriter(System.out); int stuff = 1; HashSet<Integer> pow = new HashSet<Integer>(); while(stuff < 1000000000){ pow.add(stuff); stuff *= 2; } int trials = Integer.parseInt(st.nextToken()); for(int trial = 0; trial < trials; trial++){ st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); if(n == 2) out.println("Ashishgup"); else if(pow.contains(n)) out.println("FastestFinger"); else if(n%2 == 1 || n%4 == 0) out.println("Ashishgup"); else{ boolean work = false; for(int i = 3; i <= Math.sqrt(n) && !work; i++) if(n%i == 0){ work = true; out.println("Ashishgup"); } if(!work) out.println("FastestFinger"); } } out.close(); } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
b578a180a5e0bc8ac8eb3afa9014673c
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; 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); TaskA solver = new TaskA(); int t; t = in.nextInt(); while (t > 0) { solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n , a = 0; n = in.nextInt(); if(n>2) { if (n % 2 != 0) { out.println("Ashishgup"); } else { for (int i = 2; i * i <= n; i ++) { if (n % i == 0 && i%2==0) { if ((n / i) % 2 != 0) a = i; } else if(n % i == 0 ){ a = n/i; } if (a > 2) break; } if (a <= 2) { out.println("FastestFinger"); } else { out.println("Ashishgup"); } } } else{ if(n==1) out.println("FastestFinger"); else out.println("Ashishgup"); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong(){ return Long.parseLong(next()); } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
57588447d176745cbeb2406df0a58e25
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class MakingString implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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 String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { 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, nextInt()); 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, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } // if modulo is required set value accordingly public static long[][] matrixMultiply2dL(long t[][],long m[][]) { long res[][]= new long[t.length][m[0].length]; for(int i=0;i<t.length;i++) { for(int j=0;j<m[0].length;j++) { res[i][j]=0; for(int k=0;k<t[0].length;k++) { res[i][j]+=t[i][k]+m[k][j]; } } } return res; } static long combination(long n,long r) { long ans=1; for(long i=0;i<r;i++) { ans=(ans*(n-i))/(i+1); } return ans; } static int mat(String a,String b) { int count=0; for(int i=0;i<a.length();i++) { if(a.charAt(i)!=b.charAt(i)) count++; } return count; } static int accumulate(int arr[], int first, int last) { int init = 0; for (int i = first; i< last; i++) { init = init + arr[i]; } return init; } // Returns true if it is possible to divide // array into two halves of same sum. // This function mainly uses combinationUtil() static Boolean isPossible(int arr[], int n) { // If size of array is not even. if (n % 2 != 0) return false; // If sum of array is not even. int sum = accumulate(arr, 0, n); if (sum % 2 != 0) return false; // A temporary array to store all // combination one by one int k=n/2; int half[] = new int[n/2]; // Print all combination using temporary // array 'half[]' return combinationUtil(arr, half, 0, n - 1, 0, n, sum); } /* arr[] ---> Input Array half[] ---> Temporary array to store current combination of size n/2 start & end ---> Staring and Ending indexes in arr[] index ---> Current index in half[] */ static Boolean combinationUtil(int arr[], int half[], int start, int end, int index, int n, int sum) { // Current combination is ready to // be printed, print it if (index == n / 2) { int curr_sum = accumulate(half, 0 , n/2); return (curr_sum + curr_sum == sum); } // replace index with all possible elements. // The condition "end-i+1 >= n/2-index" makes // sure that including one element at index // will make a combination with remaining // elements at remaining positions for (int i = start; i <= end && end - i + 1 >= n/2 - index; i++) { half[index] = arr[i]; if (combinationUtil(arr, half, i + 1, end, index + 1, n, sum)) return true; } return false; } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void main(String args[]) throws Exception { new Thread(null, new MakingString(),"MakingString",1<<27).start(); } // **just change the name of class from Main to reuquired** public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = sc.nextInt(); for(int j=0;j<n;j++) { int t = sc.nextInt(); if(t==1) { System.out.println("FastestFinger"); } else if(t==2) { System.out.println("Ashishgup"); } else if(t%2!=0) { System.out.println("Ashishgup"); } else { int count=0; while(t%2==0 ) { t/=2; count++; } if(t==1) { System.out.println("FastestFinger"); } else if(isPrime(t)) { if(count==1) System.out.println("FastestFinger"); else System.out.println("Ashishgup"); } else { System.out.println("Ashishgup"); } } } System.out.flush(); w.close(); } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
8eca9377d87120cd7fce1cf2ea613710
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.awt.Point; //ArrayList<Integer> ar=new ArrayList<Integer>(); //HashSet<Integer> h=new HashSet<Integer>(); //HashMap<Integer,Integer> h=new HashMap<Integer, Integer>(); //int b[]=new int[m]; //int a[]=new int[n]; //for(i=0;i<n;i++) //a[i]=sc.ni(); //out.println(); //out.print(+" "); public class Main { //static final long MOD = 998244353L; //static final long INF = 1000000000000000007L; static final long MOD = 1000000007L; static final int INF = 1000000007; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T=sc.ni(); while(T-->0) { int n=sc.ni(); if(n==1){ out.println("FastestFinger"); continue; } else if(n%2==1 || n==2){ out.println("Ashishgup"); continue; } else{ int c=0,temp=n; while(n%2==0 ){ n=n/2; c++; } if(c>=2){ if(n==1){ out.println("FastestFinger");continue;} else{ out.println("Ashishgup"); continue;} } else{ int f=0; for(int i=2;i*i<=n;i++){ if(n%i==0){ out.println("Ashishgup");f=1;break;} } if(f==0) out.println("FastestFinger"); // out.println(c); } } }out.flush(); } public static long dist(long[] p1, long[] p2) { return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1])); } //Find the GCD of two numbers public static long gcd(long a, long b) { if (a < b) return gcd(b,a); if (b == 0) return a; else return gcd(b,a%b); } //Fast exponentiation (x^y mod m) public static long power(long x, long y, long m) { if (y < 0) return 0L; long ans = 1; x %= m; while (y > 0) { if(y % 2 == 1) ans = (ans * x) % m; y /= 2; x = (x * x) % m; } return ans; } public static int[] shuffle(int[] array) { Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static long[] shuffle(long[] array) { Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); long temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static int[][] shuffle(int[][] array) { Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static int[][] sort(int[][] array) { //Sort an array (immune to quicksort TLE) Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { return a[1]-b[1]; //ascending order } }); return array; } public static long[][] sort(long[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); long[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<long[]>() { @Override public int compare(long[] a, long[] b) { if (a[0] < b[0]) return -1; else if (a[0] > b[0]) return 1; else return 0; } }); return array; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
8191e136bdd3912069303ad601492924
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class ProblemC { public static void main(String[] args) throws IOException { InputStream in = System.in; InputReader scan = new InputReader(in); int test = scan.nextInt(); for(int t=0;t<test;t++) { long n = scan.nextLong(); if(n==1) { System.out.println("FastestFinger"); } else if(n%2L!=0L || n==2) { System.out.println("Ashishgup"); } else { int evenDivCount = 0; while(n>1 && n%2==0) { n/=2; evenDivCount++; } int otherDivCount = 0; for(int i=3;i*i<=n;i++) { if(n%i==0) { otherDivCount++; if((n/i)!=1) otherDivCount++; } } if(otherDivCount>1) { System.out.println("Ashishgup"); } else if(evenDivCount==1) { System.out.println("FastestFinger"); } else if(n>1) { System.out.println("Ashishgup"); } else { System.out.println("FastestFinger"); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
c2f268d0cb8b52dcff4302a31f9f5b1a
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class A { public boolean isPrime(int a) { for (int i = 2; i<=Math.sqrt(a); i++) { if (a%i == 0) return false; } return true; } public void run() throws Exception { FastScanner sc = new FastScanner(); int test = sc.nextInt(); for (int q= 0; q<test; q++) { int n = sc.nextInt(); if (n == 1) { System.out.println("FastestFinger"); continue; } else if (n%2 == 1) { System.out.println("Ashishgup"); continue; } Set<Integer> set = new HashSet<Integer>(); int prime = 2; int more1 = 0; int more2 = 0; while(n!=1 ) { if (set.size()>1 && more2>1) { break; } else if (n%prime == 0) { if (prime == 2) { more1++; n = n/prime; set.add(prime); } else { more2++; n = n/prime; set.add(prime); if (n !=1) { set.add(1); more2 = 2; break; } } } else { if (prime == 2) { boolean ok = isPrime(n); prime++; if (ok) { set.add(n); more2 = 1; break; } else { set.add(-1); set.add(1); more2 = 2; break; } } } // System.out.println(n + " " + set); } // System.out.println(set); if (set.size() == 1) { if (set.contains(2)) { if (more1>1)System.out.println("FastestFinger"); else System.out.println("Ashishgup"); } else { System.out.println("Ashishgup"); } } else { if (more1 == 1 && more2 == 1) System.out.println("FastestFinger"); else System.out.println("Ashishgup"); } } } //SSR <3 2020 static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 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()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } } public static void main (String[] args) throws Exception { new A().run(); } public void shuffleArray(long[] arr) { int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
af9e7688e58878e35cf2b226a713421a
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class Solution { static final FS sc = new FS(); static final PrintWriter pw = new PrintWriter(System.out); private static boolean isPrime(int n) { for(int i = 2;i <= Math.sqrt(n);i++) { if (n % i == 0) { return false; } } return true; } public static void main(String[] args) { int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); boolean lose = (n == 1); if(n > 2 && n % 2 == 0){ if((n & (n - 1)) == 0) { lose = true; } else if(n % 4 != 0 && isPrime(n / 2)) { lose = true; } } pw.println(lose ? "FastestFinger" : "Ashishgup"); } pw.flush(); } static class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch(Exception ignored) {} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
a3dd01a2e89de6a06fbeaed5073d3015
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn=new Scanner(System.in); int t =scn.nextInt(); StringBuilder sb=new StringBuilder(); while(t-->0){ int n=scn.nextInt(); int n1=n; if(n==1){ sb.append("FastestFinger"+'\n'); continue; } if(n==2){ sb.append("Ashishgup"+'\n'); continue; } if(n%2==1){ sb.append("Ashishgup"+'\n'); continue; } boolean f=false; int count=0; while(n>0 && (n%2==0)){ n/=2; count++; } //System.out.println(n); boolean ans= isprime(n); //System.out.println(n+"a"+n1); if(n>1 && (ans==false||count>1)){ sb.append("Ashishgup"+'\n'); }else{ sb.append("FastestFinger"+'\n'); } } System.out.println(sb); } public static boolean isprime(int n){ for(int i=2;i*i<=n;i++){ if(n%i==0){ return false; } } return true; } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
b5d5d002a04a3cc722cf13b8720b6b90
train_003.jsonl
1592663700
Ashishgup and FastestFinger play a game. They start with a number $$$n$$$ and play in turns. In each turn, a player can make any one of the following moves: Divide $$$n$$$ by any of its odd divisors greater than $$$1$$$. Subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
256 megabytes
//package learning; import java.util.*; import java.io.*; import java.lang.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Try { static ArrayList<String> s1; static boolean[] prime; static int n = (int)1e7; static void sieve() { Arrays.fill(prime , true); prime[0] = prime[1] = false; for(int i = 2 ; i * i <= n ; ++i) { if(prime[i]) { for(int k = i * i; k<= n ; k+=i) { prime[k] = false; } } } } public static void main(String[] args) { InputReader sc = new InputReader(System.in); n *= 2; prime = new boolean[n + 1]; //sieve(); prime[1] = false; /* int n = sc.ni(); int k = sc.ni(); int []vis = new int[n+1]; int []a = new int[k]; for(int i=0;i<k;i++) { a[i] = i+1; } int j = k+1; int []b = new int[n+1]; for(int i=1;i<=n;i++) { b[i] = -1; } int y = n - k +1; int tr = 0; int y1 = k+1; while(y-- > 0) { System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); b[pos] = a1; for(int i=0;i<k;i++) { if(a[i] == pos) { a[i] = y1; y1++; } } } ArrayList<Integer> a2 = new ArrayList<>(); if(y >= k) { int c = 0; int k1 = 0; for(int i=1;i<=n;i++) { if(b[i] != -1) { c++; a[k1] = i; a2.add(b[i]); k1++; } if(c==k) break; } Collections.sort(a2); System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.println(); System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); int ans = -1; for(int i=0;i<a2.size();i++) { if(a2.get(i) == a1) { ans = i+1; break; } } System.out.println("!" + " " + ans); System.out.flush(); } else { int k1 = 0; a = new int[k]; for(int i=1;i<=n;i++) { if(b[i] != -1) { a[k1] = i; a2.add(b[i]); k1++; } } for(int i=1;i<=n;i++) { if(b[i] == -1) { a[k1] = i; k1++; if(k1==k) break; } } int ans = -1; while(true) { System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.println(); System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); int f = 0; if(b[pos] != -1) { Collections.sort(a2); for(int i=0;i<a2.size();i++) { if(a2.get(i) == a1) { ans = i+1; f = 1; System.out.println("!" + " " + ans); System.out.flush(); break; } } if(f==1) break; } else { b[pos] = a1; a = new int[k]; a2.add(a1); for(int i=1;i<=n;i++) { if(b[i] != -1) { a[k1] = i; a2.add(b[i]); k1++; } } for(int i=1;i<=n;i++) { if(b[i] == -1) { a[k1] = i; k1++; if(k1==k) break; } } } } */ /* int n = sc.ni(); int []a = sc.nia(n); int []b = sc.nia(n); Integer []d = new Integer[n]; int []d1 = new int[n]; for(int i=0;i<n;i++) { d[i] = (a[i] - b[i]); } int l = 0; int r = n-1; Arrays.sort(d); long res = 0; while(l < r) { if(d[l] + d[r] > 0) { res += (long) (r-l); r--; } else l++; } w.println(res); */ /* int n = sc.ni(); ArrayList<Integer> t1 = new ArrayList<>(); int []p1 = new int[n+1]; int []q1 = new int[n-1]; int []q2 = new int[n-1]; for(int i=0;i<n-1;i++) { int p = sc.ni(); int q = sc.ni(); t1.add(q); p1[p]++; q1[i] = p; q2[i] = q; } int res = 0; for(int i=0;i<t1.size();i++) { if(p1[t1.get(i)] == 0) { res = t1.get(i); //break; } } int y = 1; for(int i=0;i<n-1;i++) { if(q2[i] == res) { w.println("0"); } else { w.println(y + " "); y++; } } */ /* int n = sc.ni(); int k = sc.ni(); Integer []a = new Integer[n]; for(int i=0;i<n;i++) { a[i] = sc.ni(); a[i] = a[i]%k; } HashMap<Integer,Integer> hm = new HashMap<>(); for(int i=0;i<n;i++) { if(!hm.containsKey(a[i])) hm.put(a[i], 1); else hm.put(a[i], hm.get(a[i]) + 1); } Arrays.sort(a); int z = 0; long res = 0; HashMap<Integer,Integer> v = new HashMap<>(); for(int i=0;i<n;i++) { if(a[i] == 0) res++; else { if(a[i] == k-a[i] && !v.containsKey(a[i])) { res += (long) hm.get(a[i]) / 2; v.put(a[i],1); } else { if(hm.containsKey(a[i]) && hm.containsKey(k-a[i])) { int temp = a[i]; if(!v.containsKey(a[i]) && !v.containsKey(k-a[i])) { res += (long) Math.min(hm.get(temp), hm.get(k-temp)); v.put(temp,1); v.put(k-temp,1); } } } } } w.println(res); */ /* int []a = new int[4]; //G0 a[0] = 0; a[1] = 1; a[2] = 0; a[3] = 1; int []b = new int[4]; //g1 b[0] = 0; b[1] = 1; b[2] = 1; b[3] = 0; int [][]dp1 = new int[4][4]; int [][]dp2 = new int[4][4]; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { dp1[i][j] = (a[i] | a[j]); } // w.println(); } w.println(); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { dp2[i][j] = (b[i] | b[j]); } //w.println(); } int a1 = 0; int b1 = 0; int c1 = 0; int d1 = 0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(dp1[i][j] == 0 && dp2[i][j] == 0) a1++; else if(dp1[i][j] == 1 && dp2[i][j] == 1) b1++; else if(dp1[i][j] == 0 && dp2[i][j] == 1) c1++; else d1++; } } w.println(a1+ " " + b1 + " " + c1 +" "+ d1); Scanner sc = new Scanner(System.in); int q = sc.nextInt(); while(q-- > 0) { long x = sc.nextLong(); long y = sc.nextLong(); long a = (x - y)/2; long a1 = 0, b1 = 0; int lim = 8; int f = 0; for (int i=0; i<8*lim; i++) { long pi = (y & (1 << i)); long qi = (a & (1 << i)); if (pi == 0 && qi == 0) { } else if (pi == 0 && qi > 0) { a1 = ((1 << i) | a1); b1 = ((1 << i) | b1); } else if (pi > 0 && qi == 0) { a1 = ((1 << i) | a1); } else { f = 1; } } if(a1+b1 != x) f =1; if(f==1) System.out.println("-1"); else { if(a1 > b1) { long temp = a1; a1 = b1; b1 = temp; } System.out.println(a1 + " " + b1); } } */ /* int n = sc.ni(); int k = sc.ni(); int []a = sc.nia(n); int []c = new int[n+1]; int m = n; int f = 0; for(int i=0;i<k;i++) { c[a[i]]++; } HashSet<Integer> temp = new HashSet<>(); for(int i=k;i<n;i++) { if(c[a[i]] == 0) { temp.add(a[i]); } } int re = temp.size(); int st = 0; if(re > 0) { for(int i=k-1;i>=0;i--) { c[a[i]]--; if(c[a[i]] == 0) { f = 1; break; } else { re--; if(re == 0) break; } } st = k-temp.size(); } if(f==1 || st <0) w.println("-1"); else { int div = 10000/k; int mod = 10000%k; Iterator itr = temp.iterator(); while(itr.hasNext()) { Integer e = (Integer) itr.next(); a[st] = e; st++; } w.println("10000"); for(int i=0;i<div;i++) { for(int j=0;j<k;j++) { w.print(a[j] + " "); } } for(int i=0;i<mod;i++) { w.print(a[i] + " "); } w.println(); } */ int t = sc.ni(); while(t-- > 0) { int n = sc.ni(); if(n == 1) w.println("FastestFinger"); else if(n==2) w.println("Ashishgup"); else if(n%2==1) w.println("Ashishgup"); else { int f = 0; ArrayList<Integer> odd = new ArrayList<>(); hm = new HashMap<>(); primeFactors(n); int two = 0; int count = 0; if(hm.containsKey(2)) two = hm.get(2); for(Map.Entry<Integer,Integer> entry : hm.entrySet()) { int key = entry.getKey(); int val = entry.getValue(); if(key %2 ==1) count += val; } if(count==0) { if(two == 1) { w.println("Ashishgup"); } else { w.println("FastestFinger"); } } else { if(two == 1) { if(count == 1) w.println("FastestFinger"); else w.println("Ashishgup"); } else { w.println("Ashishgup"); } } } } w.close(); } static HashMap<Integer,Integer> hm; static class Pair{ int fir; int las; Pair(int fir, int las) { this.fir = fir; this.las = las; } } static int maxSubArraySum(int a[]) { int size = a.length; int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; int max = a[0]; int res = 0; TreeSet<Integer> ts = new TreeSet<>(); for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; ts.add(a[i]); res = Math.max(res,max_ending_here - ts.last()); if (max_so_far < max_ending_here) { max_so_far = max_ending_here; res = Math.max(res,max_so_far - ts.last()); } if (max_ending_here <= 0 || a[i] < 0) { max_ending_here = 0; ts = new TreeSet<>(); } } if(!ts.isEmpty()) res = Math.max(res,max_ending_here-ts.last()); return res; } public static final BigInteger MOD = BigInteger.valueOf(998244353L); static BigInteger fact(int x) { BigInteger res = BigInteger.ONE; for(int i=2;i<=x;i++) { res = res.multiply(BigInteger.valueOf(i)).mod(MOD); } return res; } public static long calc(int x,int y,int z) { long res = ((long) (x-y) * (long) (x-y)) + ((long) (z-y) * (long) (z-y)) + ((long) (x-z) * (long) (x-z)); return res; } public static int upperBound(ArrayList<Integer>arr, int length, int value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; if (value >= arr.get(mid)) { low = mid + 1; } else { high = mid; } } return low; } public static int lowerBound(ArrayList<Integer> arr, int length, int value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; //checks if the value is less than middle element of the array if (value <= arr.get(mid)) { high = mid; } else { low = mid + 1; } } return low; } static HashMap<Long,Integer> map; static class Coin{ long a; long b; Coin(long a, long b) { this.a = a; this.b = b; } } static ArrayList<Long> fac; static HashSet<Long> hs; public static void primeFactors(int n) { // Print the number of 2s that divide n while (n%2==0) { if(!hm.containsKey(2)) hm.put(2,1); else hm.put(2,hm.get(2) + 1); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { if(!hm.containsKey(i)) hm.put(i,1); else hm.put(i,hm.get(i) + 1); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) { if(!hm.containsKey(n)) hm.put(n,1); else hm.put(n,hm.get(n) + 1); } } static int smallestDivisor(int n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } static boolean IsP(String s,int l,int r) { while(l <= r) { if(s.charAt(l) != s.charAt(r)) { return false; } l++; r--; } return true; } static class Student{ int id; int val; Student(int id,int val) { this.id = id; this.val = val; } } static int upperBound(ArrayList<Integer> a, int low, int high, int element){ while(low < high){ int middle = low + (high - low)/2; if(a.get(middle) >= element) high = middle; else low = middle + 1; } return low; } static long func(long t,long e,long h,long a, long b) { if(e*a >= t) return t/a; else { return e + Math.min(h,(t-e*a)/b); } } public static int countSetBits(int number){ int count = 0; while(number>0){ ++count; number &= number-1; } return count; } static long modexp(long x,long n,long M) { long power = n; long result=1; x = x%M; while(power>0) { if(power % 2 ==1) result=(result * x)%M; x=(x*x)%M; power = power/2; } return result; } static long modInverse(long A,long M) { return modexp(A,M-2,M); } static long gcd(long a,long b) { if(a==0) return b; else return gcd(b%a,a); } static class Temp{ int a; int b; int c; int d; Temp(int a,int b,int c,int d) { this.a = a; this.b = b; this.c =c; this.d = d; //this.d = d; } } static long sum1(int t1,int t2,int x,int []t) { int mid = (t2-t1+1)/2; if(t1==t2) return 0; else return sum1(t1,mid-1,x,t) + sum1(mid,t2,x,t); } static String replace(String s,int a,int n) { char []c = s.toCharArray(); for(int i=1;i<n;i+=2) { int num = (int) (c[i] - 48); num += a; num%=10; c[i] = (char) (num+48); } return new String(c); } static String move(String s,int h,int n) { h%=n; char []c = s.toCharArray(); char []temp = new char[n]; for(int i=0;i<n;i++) { temp[(i+h)%n] = c[i]; } return new String(temp); } public static int ip(String s){ return Integer.parseInt(s); } static class multipliers implements Comparator<Long>{ public int compare(Long a,Long b) { if(a<b) return 1; else if(b<a) return -1; else return 0; } } static class multipliers1 implements Comparator<Coin>{ public int compare(Coin a,Coin b) { if(a.a < b.a) return 1; else if(a.a > b.a) return -1; else{ if(a.b < b.b) return 1; else if(a.b > b.b) return -1; else return 0; } } } // Java program to generate power set in // lexicographic order. static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static PrintWriter w = new PrintWriter(System.out); static class Student1 { int id; //int x; int b; //long z; Student1(int id,int b) { this.id = id; //this.x = x; //this.s = s; this.b = b; // this.z = z; } } }
Java
["7\n1\n2\n3\n4\n5\n6\n12"]
2 seconds
["FastestFinger\nAshishgup\nAshishgup\nFastestFinger\nAshishgup\nFastestFinger\nAshishgup"]
NoteIn the first test case, $$$n = 1$$$, Ashishgup cannot make a move. He loses.In the second test case, $$$n = 2$$$, Ashishgup subtracts $$$1$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the third test case, $$$n = 3$$$, Ashishgup divides by $$$3$$$ on the first move. Now $$$n = 1$$$, FastestFinger cannot make a move, so he loses.In the last test case, $$$n = 12$$$, Ashishgup divides it by $$$3$$$. Now $$$n = 4$$$, FastestFinger is forced to subtract $$$1$$$, and Ashishgup gets $$$3$$$, so he wins by dividing it by $$$3$$$.
Java 8
standard input
[ "number theory", "games", "math" ]
b533572dd6d5fe7350589c7f4d5e1c8c
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer  — $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
1,400
For each test case, print "Ashishgup" if he wins, and "FastestFinger" otherwise (without quotes).
standard output
PASSED
6110dffd34f7e42f103bb83586666f9b
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.*; import java.util.*; public class Main { static InputReader in; static OutputWriter out; static final boolean OJ = System.getProperty("ONLINE_JUDGE") != null; public static void main(String[] args) throws IOException { in = new InputReader(OJ ? System.in : new FileInputStream("in.txt")); out = new OutputWriter(OJ ? System.out : new FileOutputStream("out.txt")); run(); out.close(); System.exit(0); } private static void run() throws IOException { String word = in.getString(); String mask = in.getString(); int k = in.getInt(); boolean[] good = new boolean[256]; for (int i = 0; i < 26; i++) if (mask.charAt(i) == '1') good[i + 'a'] = true; Trie dictionary = new Trie(); for (int l = 0; l < word.length(); l++) { int bad = 0; Node current = dictionary.root; for (int r = l + 1; r <= word.length(); r++) { char c = word.charAt(r - 1); if (!good[c]) bad++; if (bad <= k) current = dictionary.insert(c, current); else break; } } out.println(dictionary.words); } static class Node { char content; boolean end; int badChars; Node parent; Node[] child = new Node[27]; public Node(char c, Node pi) { parent = pi; content = c; } public Node subNode(char c) { return child[c - 'a']; } } static class Trie { int words = 0; Node root = new Node('\0', null); public Node insert(char c, Node pi) { Node current = pi; Node sub = current.subNode(c); if (sub != null) current = sub; else { current.child[c - 'a'] = new Node(c, current); current = current.subNode(c); } if (!current.end) words++; current.end = true; return current; } } } class InputReader { private BufferedReader reader; private StringTokenizer st; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 1024); } public String getString() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = reader.readLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } public String getLine() throws IOException { return reader.readLine(); } public int getInt() throws IOException { return Integer.parseInt(getString()); } public long getLong() throws IOException { return Long.parseLong(getString()); } public double getDouble() throws IOException { return Double.parseDouble(getString()); } public boolean ready() throws IOException { return reader.ready(); } public int[][] getIntTable(int rowCount, int columnCount) throws IOException { int[][] table = new int[rowCount][]; for (int i = 0; i < rowCount; i++) table[i] = getIntArray(columnCount); return table; } public int[] getIntArray(int len) throws IOException { int[] table = new int[len]; for (int i = 0; i < len; i++) table[i] = getInt(); return table; } } class OutputWriter { private PrintWriter writer; public OutputWriter(OutputStream stream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream))); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i].toString()); } } public void print(String s, Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(s); writer.print(objects[i].toString()); } } public void println(Object... objects) { print(objects); writer.println(); } public void printf(String format, Object... objects) { writer.printf(format, objects); } public void close() { writer.close(); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
51b72357b722dcdbfb69e91bccc6b010
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class R166D { static String str; static char[] letters; static int k; public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); str = reader.readLine(); letters = reader.readLine().toCharArray(); k = Integer.parseInt(reader.readLine()); Trie trie = new Trie(); for (int i = 0; i < str.length(); i++) { trie.insert(str, i); } System.out.println(trie.count); } public static class Node { Node children[] = new Node[26]; } public static class Trie { Node root = new Node(); int count; public void insert(String str, int start) { int score = 0; Node node = root; int i = start; while (score <= k && i < str.length()) { int key = str.charAt(i) - 'a'; if (letters[key] == '0') { score += 1; } if (score <= k) { if (node.children[key] == null) { node.children[key] = new Node(); count += 1; } node = node.children[key]; } i += 1; } } } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
d634d53a76ebd653074868a94645637e
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.util.Set; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.util.HashSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author sandeepandey */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); GoodSubstring solver = new GoodSubstring(); solver.solve(1, in, out); out.close(); } } class GoodSubstring { private static Set<Long> dict = new HashSet<Long>(); private static int[] array = null; private static int[] cumArray = null; public void solve(int testNumber, InputReader in, OutputWriter out) { String input = in.readString(); String binaryInput = in.readString(); int maxAccecptableBadChars = in.readInt(); cumArray = getBadArray(binaryInput.toCharArray()) ; StringHash hasRef = new ConcreteHasher(input); int lenCount = input.length(); array = new int[lenCount]; array[0] = cumArray[input.charAt(0)-'a']; for(int i = 1;i < lenCount ; i++) { array[i] = array[i-1]+ cumArray[input.charAt(i)-'a']; } for(int i=1;i<=lenCount ; i++) { int k = i; long[] hashes = hasRef.getForwardKSizeHashes(k,lenCount); for(int j = 0;j < hashes.length ; j++) { int badCount = getBadCount(j, j + k-1); // out.printLine("badcount::"+badCount); if(badCount <= maxAccecptableBadChars) { // out.printLine("Item Added for::"+input.substring(j,j+k)); dict.add(hashes[j]); } } } out.printLine(dict.size()); dict.clear(); } private int[] getBadArray(char[] chars) { int[] xx = new int[26]; Arrays.fill(xx,0); int count = chars.length; char[] alpabets = StringUtils.getAlphabetArray(); for(int i = 0; i < alpabets.length ; i++) { if(chars[i] == '0') { xx[i]+=1; } } return xx; } public int getBadCount(int from,int to) { if(from <= 0) { return array[to]; } return array[to]-array[from-1]; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private 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 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(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } } interface StringHash { public long[] getForwardKSizeHashes(int windowSize,int length); } class ConcreteHasher extends SimpleStringHash { public ConcreteHasher(String input) { super(input); } public long hashFunction(long previousHash, char currentChar, int subtractor, long multiplier) { return (previousHash * multiplier + currentChar-subtractor); } public long[] getForwardKSizeHashes(int windowSize,int length) { int hashCount = length-windowSize+1; long[] hashes = new long[hashCount]; for(int i = 0;i <( hashCount ); i++) { hashes[i] = hash(i,i+windowSize-1); } return hashes; } } class StringUtils { private static String DEFAULT_ALPHABEST = "abcdefghijklmnopqrstuvwxyz"; //---------------------------------------------------------------------- //---------------------------------------------------------------------------------- public static char[] getAlphabetArray() { return DEFAULT_ALPHABEST.toCharArray(); } } abstract class SimpleStringHash extends AbstractStringHash { private static long[] hashArray = new long[0]; private static long[] baseArray = new long[0]; private static long[] reverseHashArray = new long[0]; private static int length; public SimpleStringHash(CharSequence input) { super(); length = input.length(); hashArray = new long[length + 1]; baseArray = new long[length + 1]; reverseHashArray = new long[length + 1]; long tmpHash = 0; long tmpMul = 1; for(int i = 0; i < length ; i++) { baseArray[i] = tmpMul; tmpMul = tmpMul * DEFAULT_MAULTIPLIER; } for(int i = 0; i < length ; i++) { tmpHash = hashFunction(tmpHash,input.charAt(i),48,DEFAULT_MAULTIPLIER); hashArray[i] = tmpHash; } tmpHash = 0; for(int i = length-1 ; i >=0 ; i--) { tmpHash = hashFunction(tmpHash,input.charAt(i),48,DEFAULT_MAULTIPLIER); reverseHashArray[i] = tmpHash; } } public abstract long hashFunction(long previousHash,char currentChar,int subtractor, long multiplier); public long hash(int from, int to) { int windowSize = to-from + 1; if(from < 0 || to < 0) { return 0; } return (hashArray[to]-((from <=0) ? 0 : hashArray[from-1] * baseArray[windowSize])); } } abstract class AbstractStringHash implements StringHash { protected final long DEFAULT_MAULTIPLIER ; public AbstractStringHash() { // Random randomBehaviour = new Random(547315431513L + System.currentTimeMillis()) ; // int randomNumber = randomBehaviour.nextInt(Integer.MAX_VALUE); // randomNumber = IntegerUtils.nextPrime(randomNumber); // DEFAULT_MAULTIPLIER = 1000000007; } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
abfeba6465e24dc8576cc013bbdf757e
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; /** * 111118315581 * * -3 3 2 3 2 3 2 3 -3 3 -3 3 -3 3 2 3 * * @author pttrung */ public class C { // public static long x, y, gcd; // public static int Mod = 1000000007; public static PrintWriter out; public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(); out = new PrintWriter(System.out); // System.out.println(Integer.MAX_VALUE); // PrintWriter out = new PrintWriter(new FileOutputStream(new File("output.txt"))); String line = in.next(); String com = in.next(); int k = in.nextInt(); Node root = new Node(); for (int i = 0; i < line.length(); i++) { Node cur = root; int bad = 0; for (int j = i; j < line.length(); j++) { int index = line.charAt(j) - 'a'; if (com.charAt(index) == '0') { bad++; } if (bad <= k) { if (cur.next[index] == null) { cur.next[index] = new Node(); } cur = cur.next[index]; } else { break; } } } int result = root.count() - 1; out.println(result); out.close(); } public static class Node { Node[] next; public Node(){ next = new Node[26]; } public int count() { int result = 1; for (Node node : next) { if (node != null) { result += node.count(); } } return result; } } public static int cross(Point a, Point b) { int val = a.x * b.y - a.y * b.x; return val; } public static long pow(long a, long b) { if (b == 0) { return 1; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val; } else { return val * val * a; } } public static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } public static class Point implements Comparable<Point> { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Point o) { if (x != o.x) { return x - o.x; } else { return y - o.y; } } } // public static void extendEuclid(long a, long b) { // if (b == 0) { // x = 1; // y = 0; // gcd = a; // return; // } // extendEuclid(b, a % b); // long x1 = y; // long y1 = x - (a / b) * y; // x = x1; // y = y1; // // } public static class FT { int[] data; FT(int n) { data = new int[n]; } public void update(int index, int value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public int get(int index) { int result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
eec63110859ac803cbf09034e92ef4f0
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class TC { static String s; static String isBad; static long COUNT=0; static int k; static int[] badCnt; static Trie t; static void init() { badCnt=new int[s.length()]; char first=s.charAt(0); if( isBad.charAt( first-'a' ) =='1') badCnt[0]=0; else badCnt[0]=1; for( int i=1; i<s.length(); i++ ) { char c=s.charAt(i); if( isBad.charAt( c-'a' ) =='1') badCnt[i]=badCnt[i-1]; else badCnt[i]=badCnt[i-1]+1; } } static void add( int start ) { Node curr=t.root; for( int i=start; i<s.length(); i++ ) { char c=s.charAt( i ); curr=curr.children[t.returnIndex(c)]; if( ( isBad( start, i )) || ( curr.isMarked) ) continue; curr.isMarked=true; COUNT++; } } static boolean isBad( int a, int b ) { int cnt=badCnt[b]-badCnt[a]; char ac=s.charAt(a); if( isBad.charAt( ac-'a' ) =='0') cnt++; if( cnt>k ) return true; return false; } static void solve() { init(); t=new Trie(); for( int i=0; i<s.length(); i++ ) t.addWord(s.substring(i)); for( int i=0; i<s.length(); i++ ) add( i ); System.out.println( COUNT ); } public static void main( String[] args ) throws IOException { BufferedReader br=new BufferedReader( new InputStreamReader( System.in ) ); s=br.readLine(); isBad=br.readLine(); k=Integer.parseInt( br.readLine()); solve(); } } class Node { char c; // ROOT = '*' Node[] children; boolean isLast; boolean isMarked; public Node( char _c ) { c = _c; children = new Node[ 26 ]; // To store 26 lowercase children isLast = false; isMarked=false; } } class Trie { Node root; public Trie() { root = new Node('*'); // root.isLast = true; } public void printTrie() { ArrayList<String> words = findWords( root ); for( String s : words ) System.out.println( s ); } private ArrayList<String> findWords( Node n ) { ArrayList<String> res = new ArrayList(); if( n.isLast ) { res.add( "" + n.c ); } for( int i=0; i<26; i++ ) { if( n.children[ i ] != null ) { ArrayList<String> temp = findWords( n.children[ i ]); for( String s : temp ) { res.add( n.c + s); } } } return res; } public int returnIndex( char c ) { return c-'a'; } public void addWord( String s ) { if( s.length() == 0 ) return; // Traverse the trie from the root, add characters at first point of difference //s = s.toUpperCase(); Node curr = root; char c = s.charAt(0); int ind = 0; while( true ) { if( curr.children[ returnIndex( c ) ] == null ) { // Add remaining characters as a linked list for( int i=ind; i<s.length(); i++ ) { Node n = new Node( s.charAt(i)); curr.children[ returnIndex( s.charAt(i) ) ] = n; curr = n; } curr.isLast = true; return; } curr = curr.children[ returnIndex( c ) ]; ind++; if( ind == s.length() ) { curr.isLast = true; return; } c = s.charAt(ind); } } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
41b45be38d3f0ac14a42c426f7f1532d
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.util.*; import java.math.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader c=new BufferedReader(new InputStreamReader(System.in)); char A[]=c.readLine().toCharArray(); int N=A.length; String s=c.readLine(); boolean Bad[]=new boolean[26]; for(int i=0;i<26;i++) { if(s.charAt(i)=='0') Bad[i]=true; } int K=Integer.parseInt(c.readLine()); int ans=0; TrieNode root=new TrieNode('-'); //initialize the root for(int i=0;i<N;i++) { //we count the number of good substrings starting at index i int left=K; TrieNode cur=root; //System.out.println("starting from "+A[i]); for(int j=i;j<N;j++) { if(Bad[A[j]-'a']) left--; if(left>=0) //then check if cur-->A[j] is in the trie { if(cur.children[A[j]-'a']==null) { //System.out.println("\tadding till "+A[j]); ans++; cur.children[A[j]-'a']=new TrieNode(A[j]); cur=cur.children[A[j]-'a']; } else { //System.out.println("\tnot adding at "+A[j]); cur=cur.children[A[j]-'a']; } } else break; } } System.out.println(ans); } } class TrieNode { char a; TrieNode children[]; public TrieNode(char a) { this.a=a; this.children=new TrieNode[26]; } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
95aaa5292e59fe7280607b92563e6883
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
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.HashSet; import java.util.List; import java.util.Set; public class ProblemD { static boolean[] ptable(int max) { boolean[] isprime = new boolean[max]; Arrays.fill(isprime, true); isprime[0] = isprime[1] = false; for (int i = 2 ; i < max ; i++) { if (isprime[i]) { for (int ii = i*2 ; ii < max ; ii += i) { isprime[ii] = false; } } } return isprime; } public static void main(String[] args) throws IOException { // cha(1500); BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); char[] cl = s.readLine().toCharArray(); int len = cl.length; int[] imos = new int[len+1]; char[] bad = s.readLine().toCharArray(); for (int i = 0 ; i < len ; i++) { int d = 0; if (bad[cl[i]-'a'] == '0') { d = 1; } imos[i+1] += imos[i] + d; } boolean[] isp = ptable(1000); List<Integer> primes = new ArrayList<Integer>(); for (int i = 127 ; i < 1000 ; i++) { if (isp[i]) { primes.add(i); } } long p = primes.get((int)(Math.random() * primes.size())); int k = Integer.valueOf(s.readLine()); long ans = 0; Set<Long> hashA = new HashSet<Long>(); for (int l = 0 ; l < len ; l++) { long a = 0; for (int r = l ; r < len ; r++) { a = a*p+cl[r]; if (imos[r+1]-imos[l] <= k) { if (!hashA.contains(a)) { ans++; hashA.add(a); } } else { break; } } } out.println(ans); out.flush(); } public static void cha(int n) { for (int i = 0 ; i < n ; i++) { System.out.print((char)('a' + Math.random() * 26)); } } public static void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
8a7b4ee768f2ffe215e8a34060ac5408
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
//package round166; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D { InputStream is; PrintWriter out; String INPUT = ""; void solve() { char[] s = ns(1501); SuffixAutomaton sa = SuffixAutomaton.buildSuffixAutomaton(s); sa.filter = ns(26); sa.K = ni(); out.println(sa.numberOfDistinctSubstrings()); } public static class SuffixAutomaton { public int size; public int[] len; // initialからの最短経路長 public int[] link; // Failure Link public int[][] next; // つぎ public int[] original; // clonedの場合original public char[] filter; public int K; private SuffixAutomaton(int sz) { size = sz; len = new int[sz]; link = new int[sz]; next = new int[sz][]; original = new int[sz]; } public static int enc(char c) { return c - 'a'; } public static char dec(int n) { return (char)('a'+n); } /** * Suffix Automatonを作成し、トポロジカルソートして返す。 * 1文字加える毎にcloneはたかだか1個しか作成されないので全体で2|a|-1個以下しかノードがない。 * @param a * @return */ public static SuffixAutomaton buildSuffixAutomaton(char[] a) { int n = a.length; int[] len = new int[2*n]; int[] link = new int[2*n]; int[][] next = new int[2*n][26]; int[] original = new int[2*n]; Arrays.fill(link, -1); for(int i = 0;i < 2*n;i++){ Arrays.fill(next[i], -1); } Arrays.fill(original, -1); len[0] = 0; link[0] = -1; int last = 0; int sz = 1; // extend for(char c : a){ int v = enc(c); int cur = sz++; len[cur] = len[last] + 1; int p; for(p = last; p != -1 && next[p][v] == -1; p = link[p]){ next[p][v] = cur; } if(p == -1){ link[cur] = 0; }else{ int q = next[p][v]; if(len[p] + 1 == len[q]){ link[cur] = q; }else{ int clone = sz++; original[clone] = original[q] != -1 ? original[q] : q; len[clone] = len[p]+1; System.arraycopy(next[q], 0, next[clone], 0, next[q].length); link[clone] = link[q]; for(;p != -1 && next[p][v] == q; p = link[p]){ next[p][v] = clone; } link[q] = link[cur] = clone; } } last = cur; } // topological sort int[] nct = new int[sz]; for(int i = 0;i < sz;i++){ for(int e : next[i]){ if(e != -1)nct[e]++; } } int[] ord = new int[sz]; int p = 1; ord[0] = 0; for(int r = 0;r < p;r++){ for(int e : next[ord[r]]){ if(e != -1 && --nct[e] == 0)ord[p++] = e; } } int[] iord = new int[sz]; for(int i = 0;i < sz;i++)iord[ord[i]] = i; SuffixAutomaton sa = new SuffixAutomaton(sz); for(int i = 0;i < sz;i++){ sa.len[i] = len[ord[i]]; sa.link[i] = link[ord[i]] != -1 ? iord[link[ord[i]]] : -1; sa.next[i] = next[ord[i]]; for(int j = 0;j < sa.next[i].length;j++)sa.next[i][j] = sa.next[i][j] != -1 ? iord[sa.next[i][j]] : -1; sa.original[i] = original[ord[i]] != -1 ? iord[original[ord[i]]] : -1; } return sa; } public String toString() { StringBuilder sb = new StringBuilder(); for(int i = 0;i < size;i++){ sb.append("{"); sb.append(i).append("|"); sb.append("len:").append(len[i]).append(", "); sb.append("link:").append(link[i]).append(", "); sb.append("original:").append(original[i]).append(", "); sb.append("next:{"); for(int j = 0;j < 26;j++){ if(next[i][j] != -1){ sb.append(dec(j)).append(":").append(next[i][j]).append(","); } } sb.append("}}\n"); } return sb.toString(); } public int numberOfDistinctSubstrings() { int[][] dp = new int[size][K+1]; for(int i = size-1;i >= 0;i--){ dp[i][0] = 1; for(int u = 0;u < next[i].length;u++){ int e = next[i][u]; if(e != -1){ if(filter[u] == '1'){ for(int j = 0;j <= K;j++){ dp[i][j] += dp[e][j]; } }else{ for(int j = 0;j < K;j++){ dp[i][j+1] += dp[e][j]; } } } } } int ret = -1; for(int i = 0;i <= K;i++){ ret += dp[0][i]; } return ret; } } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new D().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
f2a3de2f5b8da4b9c6949a8590989fa0
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.*; import java.util.*; public class Code implements Runnable { public static void main(String[] args) throws IOException { new Thread(new Code()).start(); } private void solve() throws IOException { String str = nextToken(); String type = nextToken(); int k = nextInt(); int[] prefix = new int[str.length()]; for (int i = 0; i < str.length(); ++i) { if (type.charAt(str.charAt(i) - 'a') == '0') { prefix[i] += 1; } if (i > 0) prefix[i] += prefix[i - 1]; } long[] hashes = new long[str.length()]; long[] pows = new long[str.length() + 1]; long mod = 117; pows[0] = 1; hashes[0] = str.charAt(0) - 'a' + 1; for (int i = 1; i < str.length(); ++i) { pows[i] = pows[i - 1] * mod; hashes[i] = (hashes[i - 1] * mod + str.charAt(i) - 'a' + 1); } pows[pows.length - 1] = pows[pows.length - 2] * mod; long ans = 0; for (int i = 1; i <= str.length(); ++i) { Set<Long> strings = new HashSet<Long>(); for (int j = 0; j + i <= str.length(); ++j) { if (prefix[j + i - 1] - (j > 0 ? prefix[j - 1] : 0) <= k) { long hash = hashes[j + i - 1] - (j > 0 ? hashes[j - 1] : 0) * pows[i]; strings.add(hash); } } ans += strings.size(); } writer.println(ans); } private class Pair<E, V> implements Comparable<Pair<E, V>> { public Pair(E first, V second) { this.first = first; this.second = second; } @Override public int compareTo(Pair<E, V> obj) { if (first.equals(obj.first)) return ((Comparable) second).compareTo(obj.second); return ((Comparable) first).compareTo(obj.first); } @Override public boolean equals(Object obj) { Pair other = (Pair) obj; return first.equals(other.first) && second.equals(other.second); } @Override public String toString() { return first + " " + second; } public E first; public V second; } @Override public void run() { try { if (in.equals("")) reader = new BufferedReader(new InputStreamReader(System.in)); else reader = new BufferedReader(new FileReader(in)); if (out.equals("")) writer = new PrintWriter(System.out, false); else writer = new PrintWriter(new FileWriter(out), false); solve(); } catch (IOException e) { e.printStackTrace(); } finally { //writer.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " KB"); try { reader.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } private float nextFloat() throws IOException { return Float.parseFloat(nextToken()); } private String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(reader.readLine()); return st.nextToken(); } private String in = "", out = ""; private BufferedReader reader; private PrintWriter writer; private StringTokenizer st; }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
0f9275a75bf420d46da6d293e8df7f51
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main{ static class Trie{ Trie next[] = new Trie[26]; public void insert(char c[], int from, int to){ if(from > to) return; if(next[c[from]-'a'] == null) { count++; next[c[from]-'a'] = new Trie(); } next[c[from]-'a'].insert(c, from+1, to); } public void traverse(String cnt){ System.out.println(cnt); for (int i = 0; i < next.length; i++) { if(next[i]!= null) next[i].traverse(cnt+(char)(i+'a')); } } } static int count = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); char c[] = in.next().toCharArray(); int ok[] = new int[26]; char ch[] = in.next().toCharArray(); for (int i = 0; i < ch.length; i++) { if(ch[i] == '1') ok[i] = 1; } int n = in.nextInt(); Trie root = new Trie(); for (int i = 0; i < c.length; i++) { int j = i; int bad = 0; for (; j < c.length; j++) { if(ok[c[j]-'a'] == 0) bad++; if(bad > n) break; } // System.out.println(i+" "+j); root.insert(c, i, j - 1); } // root.traverse(""); System.out.println(count); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
f2dbf27b0e9d3c32611161c5da0199ec
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Properties; /** * Works good for CF * * @author cykeltillsalu */ public class D { // some local config static boolean test = false; static String testDataFile = "testdata.txt"; static String feedFile = "feed.txt"; CompetitionType type = CompetitionType.CF; private static String ENDL = "\n"; static int cnt = 0; class Trie{ HashMap<Integer, Trie> children = new HashMap<Integer, Trie>(); Trie add(int c){ if(!children.containsKey(c)){ children.put(c, new Trie()); cnt ++; // System.out.println(((char)(c)) + " plus one!"); } return children.get(c); } @Override public String toString() { return "Trie [children=" + children + "]"; } } // solution private void solve() throws Throwable { cnt = 0; String wrd = wread(); String tmp = wread(); int maxbad = iread(); boolean[] good = new boolean[26]; for (int i = 0; i < tmp.length(); i++) { good[i] = tmp.charAt(i) == '1'; } Trie trie = new Trie(); int n = wrd.length(); out: for (int i = 0; i < n; i++) { Trie next = trie; int bad = 0; long hash = 0; for (int j = i; j < n; j++) { char c = wrd.charAt(j); if (!good[c - 'a']) { bad++; } if(bad > maxbad){ continue out; } next = next.add(c); } } System.out.println(cnt); } public int iread() throws Exception { return Integer.parseInt(wread()); } public double dread() throws Exception { return Double.parseDouble(wread()); } public long lread() throws Exception { return Long.parseLong(wread()); } public String wread() throws IOException { StringBuilder b = new StringBuilder(); int c; c = in.read(); while (c >= 0 && c <= ' ') c = in.read(); if (c < 0) return ""; while (c > ' ') { b.append((char) c); c = in.read(); } return b.toString(); } public static void main(String[] args) throws Throwable { if (test) { // run all cases from testfile: BufferedReader testdataReader = new BufferedReader(new FileReader(testDataFile)); String readLine = testdataReader.readLine(); int casenr = 0; out: while (true) { BufferedWriter w = new BufferedWriter(new FileWriter(feedFile)); if (!readLine.equalsIgnoreCase("input")) { break; } while (true) { readLine = testdataReader.readLine(); if (readLine.equalsIgnoreCase("output")) { break; } w.write(readLine + "\n"); } w.close(); System.out.println("Answer on case " + (++casenr) + ": "); new D().solve(); System.out.println("Expected answer: "); while (true) { readLine = testdataReader.readLine(); if (readLine == null) { break out; } if (readLine.equalsIgnoreCase("input")) { break; } System.out.println(readLine); } System.out.println("----------------"); } testdataReader.close(); } else { // run on server new D().solve(); } out.close(); } public D() throws Throwable { if (test) { in = new BufferedReader(new FileReader(new File(feedFile))); } } InputStreamReader inp = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(inp); static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); enum CompetitionType { CF, OTHER }; }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
9174d6fa497cb4e90d551e9afa5c4019
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.util.*; import java.io.*; public class D { public void solve() throws IOException { char[] str = nextToken().toCharArray(); String key = nextToken(); int k = nextInt(); boolean[] good = new boolean[26]; for(int i = 0; i < 26; i++){ char x = key.charAt(i); if( x == '1' ){ good[i] = true; } } Set<Long> set = new HashSet<Long>(); int mul = 131; for(int i = 0; i < str.length; i++){ long n = 0; int bad = 0; for(int j = i; j < str.length; j++){ int a = str[j]-'a'; n *= mul; n += str[j]; if( !good[a] ){ bad++; } if( bad > k ){ break; } set.add(n); } } // writer.println(set); writer.println(set.size()); } public static void main(String[] args) throws IOException { new D().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() throws IOException { 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); } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
c5f7a53ac034053692fecb3b6df49bb8
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.*; import java.util.*; import sun.security.provider.certpath.OCSP.RevocationStatus; public class D { String line; StringTokenizer inputParser; BufferedReader is; FileInputStream fstream; DataInputStream in; void openInput(String file) { if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin else { try{ fstream = new FileInputStream(file); in = new DataInputStream(fstream); is = new BufferedReader(new InputStreamReader(in)); }catch(Exception e) { System.err.println(e); } } } void readNextLine() { try { line = is.readLine(); inputParser = new StringTokenizer(line, " "); //System.err.println("Input: " + line); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } } int NextInt() { String n = inputParser.nextToken(); int val = Integer.parseInt(n); //System.out.println("I read this number: " + val); return val; } String NextString() { String n = inputParser.nextToken(); return n; } void closeInput() { try { is.close(); } catch (IOException e) { System.err.println("Unexpected IO ERROR: " + e); } } public static void main(String [] argv) { String filePath=null; if(argv.length>0)filePath=argv[0]; new D(filePath); } public D(String inputFile) { openInput(inputFile); readNextLine(); String s=line; /*s=""; for(int i=0; i<1500; i++) s+=(char)(((int)(Math.random()*26))+'a');*/ readNextLine(); boolean [] g = new boolean[26]; for(int i=0;i<26; i++) g[i]=(line.charAt(i)=='1'); boolean [] gg=test(s, g); readNextLine(); int k=NextInt(); int n=s.length(); int [] [] dp= new int[n][n]; Set <String> [] m = new Set[1501]; for(int i=0; i<m.length; i++) m[i]=new TreeSet <String>(); dp[0][0]=(gg[0])?0:1; int ret=0; if(dp[0][0]<=k)m[1].add(s.substring(0, 1)); for(int i=1; i<n; i++) { dp[i][0]=(gg[i])?0:1; if(dp[i][0]<=k)m[1].add(s.substring(i, i+1)); for(int j=1; j<=i; j++) { dp[i][j]=dp[i-1][j-1]+((gg[i])?0:1); if(dp[i][j]<=k)m[j+1].add(s.substring(i-j, i+1)); } } for(int i=0; i<m.length; i++) { ret+=m[i].size(); } System.out.println(ret); } private boolean [] test(String s, boolean[] g) { boolean [] ret=new boolean[s.length()]; for(int i=0; i<s.length(); i++) ret[i]=g[s.charAt(i)-'a']; return ret; } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
3bb6f15786909932c178c586542f60ff
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.util.HashSet; import java.util.Scanner; public class D { public static long H = 7613529876128795615L; public static long B = 1234782732774L; public static void main(String[] args){ Scanner sc = new Scanner(System.in); String input = sc.next(); char[] array = sc.next().toCharArray(); boolean[] good = new boolean[26]; for(int i = 0; i < 26; i++){ good[i] = array[i] == '1'; } final int k = sc.nextInt(); HashSet<Long> dup = new HashSet<Long>(); final int size = input.length(); long x = 0; char[] str = input.toCharArray(); LOOP: for(int i = 0; i < size; i++){ int bad = 0; long hash = 0; //StringBuilder sb = new StringBuilder(); for(int j = i; j < size; j++){ hash *= B; hash %= H; hash += str[j] * 1000; hash %= H; //hash = sb.append(str[j]).toString().hashCode(); //hash = input.substring(i, j + 1).hashCode(); if(dup.contains(hash)){ if(!(good[str[j] - 'a'])){ bad++; } if(bad > k){ continue LOOP; } continue; }else{ dup.add(hash); } if(!(good[str[j] - 'a'])){ bad++; } if(bad > k){ continue LOOP; }else{ //System.out.println(input.substring(i, j + 1)); x++; } } } System.out.println(x); sc.close(); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
130a790e3018dcad12656f01969f7aaf
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.StreamTokenizer; import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; 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; TokenizerReader in = new TokenizerReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class Node { Node[] children; boolean isFinal = false; } class TaskD { public void solve(@SuppressWarnings("UnusedParameters") int testNumber, TokenizerReader in, PrintWriter out) { char[] s = in.nextString().toCharArray(); int n = s.length; String g = in.nextString(); boolean[] bad = new boolean[256]; for (int i = 0; i < 26; ++i) if (g.charAt(i) == '0') bad[i + 'a'] = true; Node root = new Node(); int res = 0; int k = in.nextInt(); int curBad = 0; int j = -1; for (int i = 0; i < n; ++i) { while (j < n && curBad <= k) { ++j; if (j < n && bad[s[j]]) ++curBad; } Node cur = root; for (int d = i; d < j; ++d) { int idx = s[d] - 'a'; if (cur.children == null) cur.children = new Node[26]; if (cur.children[idx] == null) cur.children[idx] = new Node(); cur = cur.children[idx]; if (!cur.isFinal) { cur.isFinal = true; ++res; } } if (bad[s[i]]) --curBad; } out.println(res); } } class TokenizerReader extends StreamTokenizer { public TokenizerReader(InputStream in) { super(new BufferedReader(new InputStreamReader(in))); defaultConfig(); } public String nextString() { try { nextToken(); } catch (IOException e) { throw new RuntimeException("nextString: exception in line " + lineno(), e); } return sval; } public int nextInt() { return Integer.parseInt(nextString()); } public void defaultConfig() { resetSyntax(); wordChars(33, 126); whitespaceChars(0, ' '); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
39d33bb1d7e363a1ab8a1c7c47d4df9e
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.StreamTokenizer; import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; 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; TokenizerReader in = new TokenizerReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class Node { Node[] children = new Node[26]; boolean isFinal = false; } class TaskD { public void solve(@SuppressWarnings("UnusedParameters") int testNumber, TokenizerReader in, PrintWriter out) { char[] s = in.nextString().toCharArray(); int n = s.length; String g = in.nextString(); boolean[] bad = new boolean[256]; for (int i = 0; i < 26; ++i) if (g.charAt(i) == '0') bad[i + 'a'] = true; Node root = new Node(); int res = 0; int k = in.nextInt(); int curBad = 0; int j = -1; for (int i = 0; i < n; ++i) { while (j < n && curBad <= k) { ++j; if (j < n && bad[s[j]]) ++curBad; } Node cur = root; for (int d = i; d < j; ++d) { int idx = s[d] - 'a'; if (cur.children[idx] == null) cur.children[idx] = new Node(); cur = cur.children[idx]; if (!cur.isFinal) { cur.isFinal = true; ++res; } } if (bad[s[i]]) --curBad; } out.println(res); } } class TokenizerReader extends StreamTokenizer { public TokenizerReader(InputStream in) { super(new BufferedReader(new InputStreamReader(in))); defaultConfig(); } public String nextString() { try { nextToken(); } catch (IOException e) { throw new RuntimeException("nextString: exception in line " + lineno(), e); } return sval; } public int nextInt() { return Integer.parseInt(nextString()); } public void defaultConfig() { resetSyntax(); wordChars(33, 126); whitespaceChars(0, ' '); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
d8df12dac1c52923f84bb05d63b697d7
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) throws IOException { new D().solve(); } void solve() throws IOException { // Scanner sc = new Scanner(new File("input.txt")); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // String[] sp; // sp = in.readLine().split(" "); String str = in.readLine(); boolean[] gb; { gb = new boolean['z' + 1]; String sgb = in.readLine(); for (int i = 0; i < 26; i++) { gb['a' + i] = sgb.charAt(i) == '1'; } } int k = Integer.parseInt(in.readLine()); int n = str.length(); TreeSet<String> set = new TreeSet<String>(); int ret = 0; for (int l = 0; l < n; l++) { int r; { r = l; int count = gb[str.charAt(r)] ? 0 : 1; while (count <= k && r < n) { r++; if (r < n) { count += gb[str.charAt(r)] ? 0 : 1; } } r--; } String sub = str.substring(l, r + 1); int diffIdx = 0; String floor = set.floor(sub); if (floor != null) { int di = 0; while (di < Math.min(sub.length(), floor.length()) && sub.charAt(di) == floor.charAt(di)) { di++; } diffIdx = Math.max(diffIdx, di); if (floor.length() < sub.length() && sub.startsWith(floor)) { set.remove(floor); } } String ceil = set.ceiling(sub); if (ceil != null) { int di = 0; while (di < Math.min(sub.length(), ceil.length()) && sub.charAt(di) == ceil.charAt(di)) { di++; } diffIdx = Math.max(diffIdx, di); if (ceil.length() < sub.length() && sub.startsWith(ceil)) { set.remove(ceil); } } ret += sub.length() - diffIdx; if (sub.length() - diffIdx != 0) { set.add(sub); } } System.out.println(ret); } } // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
18d71ab4ca4fd9319d8d2a2613160e54
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class Solution { BufferedReader in; StringTokenizer st; PrintWriter out; char[] s; boolean[] ok = new boolean[26]; int k; static class Node { Node[] next = new Node[26]; boolean hasNext(char c) { return next[c - 'a'] != null; } Node getNext(char c) { return next[c - 'a']; } void addNext(char c, Node node) { next[c - 'a'] = node; } } void solve() throws IOException { s = ns().toCharArray(); String letters = ns(); for (int i = 0; i < letters.length(); ++i) if (letters.charAt(i) == '1') ok[i] = true; k = ni(); Node root = new Node(); int ret = 0; for (int i = 0; i < s.length; ++i) { Node cur = root; int cnt = 0; for (int j = i; j < s.length; ++j) { char c = s[j]; if (ok[c - 'a'] == false) ++cnt; if (cnt > k) break; if (cur.hasNext(c) == false) { ++ret; cur.addNext(c, new Node()); } cur = cur.getNext(c); } } out.println(ret); } void gen() throws FileNotFoundException { PrintWriter out = new PrintWriter("test.txt"); Random rnd = new Random(); for (int i = 0; i < 1500; ++i) { char c = (char) (rnd.nextInt(26) + 'a'); out.print(c); } out.close(); } public Solution() throws IOException { Locale.setDefault(Locale.US); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); gen(); solve(); in.close(); out.close(); } String ns() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int ni() throws IOException { return Integer.valueOf(ns()); } long nl() throws IOException { return Long.valueOf(ns()); } double nd() throws IOException { return Double.valueOf(ns()); } public static void main(String[] args) throws IOException { new Solution(); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
73238d8394b7c689163243163b3efceb
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import static java.lang.Math.*; import static java.util.Arrays.*; import java.util.*; import java.io.*; public class Main { void run() { Scanner sc = new Scanner(System.in); HashSet<Long> good = new HashSet<Long>(); boolean[] alf = new boolean[26]; String str = sc.next(); String f = sc.next(); int k = sc.nextInt(), n = str.length(); long[] rh = new long[n+1]; long[] pow = new long[n+1]; pow[0] = 1; for(int i=0;i<f.length();i++) alf[i] = f.charAt(i) == '1'; int[] cnt = new int[n+1]; for(int i=1;i<=n;i++) { cnt[i] = cnt[i-1] + (alf[str.charAt(i-1) - 'a']? 0: 1); rh[i] = rh[i-1] + (long)(str.charAt(i-1) - 'a' + 1) * pow[i-1]; pow[i] = pow[i-1] * 27; } for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) { if( cnt[j] - cnt[i-1] <= k ) { // debug((rh[j]-rh[i-1]) * pow[n-i], str.substring(i-1, j)); good.add((rh[j]-rh[i-1]) * pow[n-i]); } } System.out.println(good.size()); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new Main().run(); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
bfcf126392b87b8f95521ad5f2d6431c
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.*; import java.util.*; public class ProblemD { InputReader in; PrintWriter out; final int MAXN = 1505; long[] p29 = new long[MAXN]; class Str implements Comparable<Str> { long h; int len; int from; public int compareTo(Str s2) { if (h < s2.h) return -1; if (h > s2.h) return 1; if (len < s2.len) return -1; if (len > s2.len) return 1; if (from < s2.from) return -1; if (from > s2.from) return 1; return 0; } } void solve() { p29[0] = 1; for (int i = 1; i < MAXN; i++) p29[i] = p29[i - 1] * 29; String s = in.next(); int n = s.length(); long[] h = new long[n]; for (int i = 0; i < n; i++) { h[i] = s.charAt(i) * p29[i]; if (i > 0) h[i] = h[i - 1] + h[i]; } int[] num = new int[256]; String nums = in.next(); for (char i = 'a'; i <= 'z'; i++) if (nums.charAt(i - 'a') == '0') num[i] = 1; else num[i] = 0; int[] sum = new int[n + 1]; sum[0] = 0; for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + num[s.charAt(i - 1)]; // for (int i = 0; i <= n; i++) // out.print(sum[i]); // out.println(); int k = in.nextInt(); int ans = 0; for (int len = 1; len <= n; len++) { Str[] hs = new Str[n - len + 1]; for (int i = 0; i < n - len + 1; i++) hs[i] = new Str(); for (int i = 0; i < n - len + 1; i++) { long curh = h[i + len - 1]; if (i > 0) curh -= h[i - 1]; curh *= p29[n - i - 1]; hs[i].h = curh; hs[i].len = len; hs[i].from = i; } Arrays.sort(hs); // out.println("len == " + len); // for (int i = 0; i < n - len + 1; i++) // out.println(hs[i].h); for (int j = 0; j < n - len + 1; j++) if (j == 0 || hs[j].h != hs[j - 1].h) if (sum[hs[j].from + hs[j].len] - sum[hs[j].from] <= k) { // for (int t = hs[j].from; t < hs[j].from + hs[j].len; t++) // out.print(s.charAt(t)); // out.println(); ans++; } } out.println(ans); } ProblemD(){ boolean oj = System.getProperty("ONLINE_JUDGE") != null; try { if (oj) { in = new InputReader(System.in); out = new PrintWriter(System.out); } else { Writer w = new FileWriter("output.txt"); in = new InputReader(new FileReader("input.txt")); out = new PrintWriter(w); } } catch(Exception e) { throw new RuntimeException(e); } solve(); out.close(); } public static void main(String[] args){ new ProblemD(); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public InputReader(FileReader fr) { reader = new BufferedReader(fr); 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
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
77246d80f6bf7c80e1051a122f9f668c
train_003.jsonl
1360596600
You've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.A substring s[l...r] (1 ≤ l ≤ r ≤ |s|) of string s  =  s1s2...s|s| (where |s| is the length of string s) is string  slsl + 1...sr.The substring s[l...r] is good, if among the letters  sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] ≠ s[p...q].
512 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; import java.awt.*; @SuppressWarnings("unused") public class round166D { static BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer st = new StringTokenizer(""); static int nextInt() throws Exception { return Integer.parseInt(next()); } static String next() throws Exception { while (true) { if (st.hasMoreTokens()) { return st.nextToken(); } String s = br.readLine(); if (s == null) { return null; } st = new StringTokenizer(s); } } public static void main(String[] args)throws Exception { char [] s = next().toCharArray(); HashSet<Character> bad = new HashSet<Character>(); String alpha = next(); for(int i = 0 ; i < 26 ; ++i) if(alpha.charAt(i) == '0') bad.add((char) ('a' + i)); int k = nextInt(); int cumulative [] = new int [s.length]; for(int i = 0 ; i < s.length ; ++i){ if(i == 0) cumulative[0] = bad.contains(s[i]) ? 1 : 0; else cumulative[i] = cumulative[i - 1] + (bad.contains(s[i]) ? 1 : 0); } HashSet<Long> good = new HashSet<Long>(); for(int i = 0 ; i < s.length ; ++i){ long seed = 131; long hash_code = 0; for(int j = i ; j < s.length ; ++j){ if(j == i) hash_code = s[j]; else hash_code = (hash_code * seed) + s[j]; if(cumulative[j] - (i == 0 ? 0 : cumulative[i - 1]) <= k){ good.add(hash_code); } } } System.out.println(good.size()); } }
Java
["ababab\n01000000000000000000000000\n1", "acbacbacaa\n00000000000000000000000000\n2"]
2 seconds
["5", "8"]
NoteIn the first example there are following good substrings: "a", "ab", "b", "ba", "bab".In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
Java 6
standard input
[ "data structures", "strings" ]
c0998741424cd53de41d31f0bbaef9a2
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer k (0 ≤ k ≤ |s|) — the maximum acceptable number of bad characters in a good substring.
1,800
Print a single integer — the number of distinct good substrings of string s.
standard output
PASSED
9de1e5dcfe7ae7ce32f1b877bbf73042
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.*; public class test { public static void main(String[] args) { Scanner in = new Scanner(System.in); int hh = in .nextInt(); int mm = in.nextInt(); int h = in.nextInt(); int d = in.nextInt(); int c = in.nextInt(); int n = in.nextInt(); double ans = 0; if(hh<20){ double ans1 = Math.ceil((double)h/n)*(double)c; int inc = 0; if(mm == 00){ inc = (20-hh)*60*d; } else{ inc = (19-hh)*60*d + (60-mm)*d; } h = h+inc; double ans2 = Math.ceil((double)h/n)*(double)c*0.8; ans = Math.min(ans1,ans2); System.out.println(ans); } else{ double ans2 = Math.ceil((double)h/n)*(double)c*0.8; System.out.println(ans2); } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
e167a5fd1530396565dcbb25f81ea5d3
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import java.util.Arrays; public class Test1{ public static void main(String args[])throws IOException{ Scanner sc=new Scanner(System.in); int hh=sc.nextInt(); int mm=sc.nextInt(); int h=sc.nextInt(); int d=sc.nextInt(); int c=sc.nextInt(); int n=sc.nextInt(); int time=hh*60+mm; double res1=0,res2=0; int buns=0; buns=(int)Math.ceil((double)h/n); res1=buns*c; double res3=res1*0.8; int nh=h+(1200-time)*d; buns=(int)Math.ceil((double)nh/n); res2=(buns*c)*0.8; double res=0.0; if(time>=1200) res=res3; else res=Math.min(res1,res2); System.out.println(res); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
553b0fbb688ffae19cabae8e6dcab46d
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.Scanner; public class main { public static void main(String[] args) { Scanner i = new Scanner(System.in); int hh = i.nextInt(); int mm = i.nextInt(); int H = i.nextInt(); int D = i.nextInt(); int C =i.nextInt(); int N = i.nextInt(); int diffMinitues = getMinDiff(hh, mm); int catHungerPoint = H + diffMinitues * D; int numberOfBunsCase1 = (int) Math.ceil(catHungerPoint / (N*1.0)); double case1 = ( numberOfBunsCase1 * C ) * 0.8; int numberOfBunsCase2 = (int) Math.ceil(H / (N*1.0)); double case2 = numberOfBunsCase2 * C; System.out.println(Math.min(case1, case2)); } public static int getMinDiff(int hh,int mm) { if(hh >= 20 && hh <= 23) { return 0; } else { return 60 - mm + ( 20 - (hh+1) ) * 60; } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
7c84e9fcaf059120efdbb5dd21a74962
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.text.DecimalFormat; import java.util.*; public class Main { public static void main(String[] args) throws Exception { FastReader reader = new FastReader(); int hh = reader.nextInt(), mm = reader.nextInt(); double h, d, c, n; h = reader.nextInt(); d = reader.nextInt(); c = reader.nextInt(); n = reader.nextInt(); double fir = Math.ceil(h / n) * c; double sec; if (hh >= 20) { sec = Math.ceil(h / n) * c; } else { int min = 60 - mm; if (min < 60) { min += (20 - 1 - hh) * 60; } else min += (20 - hh) * 60 - 60; min *= d; sec = Math.ceil((h + min) / n) * c; } System.out.println(new DecimalFormat("#0.0000").format(Math.min(fir, sec * 0.8))); } static class FastReader { BufferedReader br; StringTokenizer st; 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
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
d58eead4ff31b2ed3d065aec06b27638
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Question1 { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner s = new Scanner(System.in); int h = s.nextInt(); int m = s.nextInt(); int H = s.nextInt(); int d = s.nextInt(); int c = s.nextInt(); int n = s.nextInt(); int fal = (H + n - 1) / n; double nor = fal; nor *= c; nor *= 1.0000; int nh = 0; if(h >= 20) { nh = 0; } else nh = (1200) - ((h*60) + m); H += (d*nh); fal = (H + n - 1) / n; double nor1 = fal; nor1 *= c * 1.0000; nor1 *= 0.80000; double ans = nor<nor1 ? nor : nor1; System.out.println(ans); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
54f20cedafa2a0266e739319774b3876
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.Scanner; public class A { public static void main(String[] args) { Scanner in = new Scanner(System.in); double hh = in.nextInt(); double mm = in.nextInt(); double h = in.nextDouble(); double d = in.nextDouble(); double c = in.nextDouble(); double n = in.nextDouble(); if (hh < 20) { double delta = 60 * (20 - hh) - mm; System.out.println(Math.min(c * Math.ceil(h / n), (0.8 * c) * Math.ceil((h + d * delta) / n))); } else { System.out.println((0.8 * c) * Math.ceil(h / n)); } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
dda65e44c5a643477761cb716784610b
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.awt.*; import java.io.*; import java.util.*; import java.util.Objects; public class Main implements Runnable { int maxn = (int)2e3+111; int inf = (int)1e9+111; int n,k,m; int a[] = new int[maxn]; void solve() throws Exception { int hour = in.nextInt(); int minute = in.nextInt(); long cur = in.nextInt(); long add = in.nextInt(); long price = in.nextInt(); long subs = in.nextInt(); if (hour>=20) { long bread = 0; if (cur%subs==0) { bread = cur/subs; } else { bread = cur/subs + 1; } double ans = (double)bread*price*0.8; out.println(ans); } else { long bread = 0; if (cur%subs==0) { bread = cur/subs; } else { bread = cur/subs + 1; } double now = (double)bread*price; long minutes = (20-hour-1)*60 + (60-minute); long newCur = cur + (minutes*add); long newBread = 0; if (newCur%subs==0) { newBread = newCur/subs; } else { newBread = newCur/subs + 1; } double discount = newBread*price*0.8; out.println(Math.min(now, discount)); } } class Pair { int x,y; public Pair(int x, int y) { this.x = x; this.y = y; } } String fileInName = ""; boolean file = false; boolean isAcmp = false; static Throwable throwable; public static void main (String [] args) throws Throwable { Thread thread = new Thread(null, new Main(), "", (1 << 26)); thread.start(); thread.join(); thread.run(); if (throwable != null) throw throwable; } FastReader in; PrintWriter out; public void run() { String fileIn = "absum.in"; String fileOut = "absum.out"; try { if (isAcmp) { if (file) { in = new FastReader(new BufferedReader(new FileReader(fileIn))); out = new PrintWriter (fileOut); } else { in = new FastReader(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); } } else if (file) { in = new FastReader(new BufferedReader(new FileReader(fileInName+".in"))); out = new PrintWriter (fileInName + ".out"); } else { in = new FastReader(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); } solve(); } catch(Exception e) { throwable = e; } finally { out.close(); } } } class FastReader { BufferedReader bf; StringTokenizer tk = null; public FastReader(BufferedReader bf) { this.bf = bf; } public String nextToken () throws Exception { if (tk==null || !tk.hasMoreTokens()) { tk = new StringTokenizer(bf.readLine()); } if (!tk.hasMoreTokens()) return nextToken(); else return tk.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
d46b8a91c7c905c0c743efbe0e9d032d
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Sample { public static void main(String[] args) throws IOException{ BufferedReader read= new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokens=new StringTokenizer(read.readLine()," "); int hour=Integer.parseInt(tokens.nextToken()); int minutes=Integer.parseInt(tokens.nextToken()); tokens=new StringTokenizer(read.readLine()," "); int Hunger=Integer.parseInt(tokens.nextToken()); int Dincrease=Integer.parseInt(tokens.nextToken()); int Cost=Integer.parseInt(tokens.nextToken()); int Ndecrease=Integer.parseInt(tokens.nextToken()); double c=0; double c2=0; if(hour>19){ c=Hunger%Ndecrease==0?(Hunger/Ndecrease)*Cost*.8:((Hunger/Ndecrease)+1)*Cost*.8; }else{ c=Hunger%Ndecrease==0?(Hunger/Ndecrease)*Cost:((Hunger/Ndecrease)+1)*Cost; int hLevel=levelOfHunger(hour,minutes); Hunger+=(hLevel*Dincrease); c2=Hunger%Ndecrease==0?(Hunger/Ndecrease)*Cost*.8:((Hunger/Ndecrease)+1)*Cost*.8; if(c>c2){ c=c2; } } System.out.println(c); } public static int levelOfHunger(int hour,int minutes){ int ans=0; ans+=60-minutes; hour++; ans+=((20-hour)*60); return ans; } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
ee3e337e5b29bb1faef9d18559cb81e4
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.Scanner; public class Sheet3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int hh; hh=in.nextInt(); int mm; mm=in.nextInt(); int h,d,c,n; h=in.nextInt(); d=in.nextInt(); c=in.nextInt(); n=in.nextInt(); double op1; double op2; if(hh<20) { op1 = h / n + ((h % n == 0) ? 0 : 1); op1=op1*c; int hours=20-hh-1; int minutes=60-mm; int t= minutes+60*hours; h= h+d*t; op2 = 0.8*c*(h / n + ((h % n == 0) ? 0 : 1)); if(op1<op2) System.out.println(op1); else System.out.println(op2); } else { op1 =0.8*c*(h / n + ((h % n == 0) ? 0 : 1)); System.out.println(op1); } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
6684ebcdcd96b2f6d4ce3b9980fcdf3d
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math.*; import java.util.Scanner; import javax.swing.plaf.synth.SynthSeparatorUI; public class Class_3 { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); int hh = in.nextInt(); int mm = in.nextInt(); int H = in.nextInt(); int D = in.nextInt(); int C = in.nextInt(); int N = in.nextInt(); double time = hh * 60 + mm; double price_1 = (H + N - 1) / N * C; if (time >= 20 * 60) { price_1 *= 0.8; System.out.println(price_1); System.exit(0); } H += (20 * 60 - time) * D; double price_2 = (H + N - 1) / N * C * 0.8; System.out.println(Math.min(price_1, price_2)); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
b37d4c5470f2666233f7bd4586ef233a
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
//package solve; import java.util.*; import java.math.*; /** * @author 溟檠 * 上午12:04:33 */ public class Main24 { public static void main(String[] args) { Scanner in=new Scanner (System.in); int hh=in.nextInt(),mm=in.nextInt(); int H=in.nextInt(),D=in.nextInt(),C=in.nextInt(),N=in.nextInt(); double cost1=0,cost2=0,cost=0; if(H<=0) { System.out.printf("8.4f",0); } else { if(hh<20) { int time_m=1200-60*hh-mm; cost1=(H%N==0?H/N:(H/N+1))*C; H+=D*time_m; cost2=(H%N==0?H/N:(H/N+1))*C*0.8; cost=cost1<cost2?cost1:cost2; }else { cost=(H%N==0?H/N:(H/N+1))*C*0.8; } System.out.printf("%8.4f",cost); } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
7e2aeaabacc88dd999ba6bdfafa8266f
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class A { static double H; static double D; static double C; static double N; public static void main(String[] args) { FastScanner in = new FastScanner(); int hh = in.nextInt(); int mm = in.nextInt(); H = in.nextDouble(); D = in.nextDouble(); C = in.nextDouble(); N = in.nextDouble(); double best = Double.MAX_VALUE; while(true){ int buy = (int) Math.ceil(H/N); if(hh >= 20) { best = Math.min(best, buy*(C*.8)); } else { best = Math.min(best, buy*C); } mm++; H += D; if(mm == 60) { mm=0; hh++; if(hh==24)break; } } System.out.println(best); } static class FastScanner{ BufferedReader br; StringTokenizer st; public FastScanner(String s) { try{ br = new BufferedReader(new FileReader(s)); } catch(FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(){ br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while(st == null ||!st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine());} catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } String next() { return nextToken(); } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
651bc03d04f1d7b0beb4484f74e2d009
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class A { static double H; static double D; static double C; static double N; public static void main(String[] args) { JS in = new JS(); int hh = in.nextInt(); int mm = in.nextInt(); H = in.nextDouble(); D = in.nextDouble(); C = in.nextDouble(); N = in.nextDouble(); double best = Double.MAX_VALUE; while(true){ int buy = (int) Math.ceil(H/N); if(hh >= 20) { best = Math.min(best, buy*(C*.8)); } else { best = Math.min(best, buy*C); } mm++; H += D; if(mm == 60) { mm=0; hh++; if(hh==24)break; } } System.out.println(best); } static class JS{ 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 JS() { in = new BufferedInputStream(System.in, BS); } public JS(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; } } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
155431b68d60a2551b19e55f701dd494
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
/* * 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. */ import java.io.IOException; import java.util.*; /** * * @author SNEHITH */ public class Main { public void solve(double h, double m, double a, double b, double c, double d){ double mi = h*60 + m; double start = 20*60; double end = 23*60 + 59; if(mi< start){ System.out.println(Math.min( (Math.ceil(a/d)*c), Math.ceil((((start-mi)*b)+a)/d)*0.80*c)); } else if(mi <= end && mi>= start) System.out.println((Math.ceil(a/d))*c*0.8); } public static void main(String args[]) throws IOException{ Main mi = new Main(); Scanner r = new Scanner(System.in); double h = r.nextDouble(); double m = r.nextDouble(); double a = r.nextDouble(); double b = r.nextDouble(); double c = r.nextDouble(); double d = r.nextDouble(); mi.solve(h,m,a,b,c,d); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
04f448ee15268f24d1c354570e96f976
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.Scanner; public class MainA { public static void main(String[] args) { Scanner in = new Scanner(System.in); int hours = in.nextInt(); int minutes = in.nextInt(); int hungerRate = in.nextInt(); int hungerSpeed = in.nextInt(); double bunCost = in.nextDouble(); int bunEfficiency = in.nextInt(); double immediateCost = calcImmediateCost(hungerRate, bunCost, bunEfficiency); if (hours >= 20) { System.out.println(immediateCost * 0.8d); return; } double saleCost = calcSaleCost(hours, minutes, hungerRate, hungerSpeed, bunCost, bunEfficiency); System.out.println(Math.min(immediateCost, saleCost)); } private static double calcSaleCost(int hours, int minutes, int hungerRate, int hungerSpeed, double bunCost, int bunEfficiency) { hungerRate += ((20 - hours) * 60 - minutes) * hungerSpeed; return calcImmediateCost(hungerRate, bunCost, bunEfficiency) * 0.8; } private static double calcImmediateCost(int hungerRate, double bunCost, int bunEfficiency) { return hungerRate / bunEfficiency * bunCost + (hungerRate % bunEfficiency == 0d? 0d : bunCost); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
991baaa5c98f96772b9a782bcd2d1d21
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.Scanner; public class CF955A { public static void main(String[] args) { Scanner input = new Scanner(System.in); int hh = input.nextInt(); int mm = input.nextInt(); int h = input.nextInt(); int d = input.nextInt(); int c = input.nextInt(); int n = input.nextInt(); int s = h/n; if (h % n != 0){ s++; } double cost1 = s * c; int time = 0; if (hh < 20){ time += (20 - hh) * 60; time -= mm; } s = (h + d * time) / n; if ((h + d * time) % n != 0){ s++; } double cost2 = s * c * 0.8; //if (hh >= 20){ // System.out.println(cost1); //}else{ System.out.println(Math.min(cost1, cost2)); //} } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
8a8d4ae1382d7fa2a3c2505be36b1eb9
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.StringTokenizer; public class C_955_A { public static void main(String[] args) throws IOException { FastScanner scanner = new FastScanner(System.in); int hh = scanner.nextInt(); int mm = scanner.nextInt(); int H = scanner.nextInt(); int D = scanner.nextInt(); int C = scanner.nextInt(); int N = scanner.nextInt(); // double res = 0.0; // if (hh >= 20) { // res = (H + N - 1) / N * (C * 0.8); // } else { // double amount = (H + N - 1) / N; // double now = amount * C; // double time = 20*60 - hh*60 - mm; // double amount2 = (time * D + H + N - 1) / N; // double wait = (amount2 * (C * 0.8)); // res = Math.min(now, wait); // } int L = 20*60-(hh*60+mm); double ans = Double.POSITIVE_INFINITY; if(hh < 20){ ans = Math.min(ans, (H+N-1)/N*C); } ans = Math.min(ans, (H+Math.max(L, 0)*D+N-1)/N*C*0.8); System.out.printf("%.4f", ans); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = null; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { if (st == null) return br.readLine(); String line = ""; if (st.hasMoreTokens()) line = st.nextToken(""); st = null; return line; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
adb18ee035f5004eb113da5c6c3607c0
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.*; import java.io.*; public class A955 { public static void main(String[] args) throws Exception { st = new StringTokenizer(in.readLine()); double hh = d(), mm = d(); st = new StringTokenizer(in.readLine()); double H = d(), D = d(), C = d(), N = d(); if (hh >= 20) { out.println(Math.ceil(H / N) * C * 0.8); } else { double nogo = Math.ceil(H / N) * C; double mins = (19 - hh) * 60 + (60 - mm); double go = Math.ceil((H + D * mins) / N) * C * 0.8; out.println(Math.min(go, nogo)); } out.close(); } static BufferedReader in; static StringTokenizer st; static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static { try { in = new BufferedReader(new FileReader("cf.in")); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); } } static int i() {return Integer.parseInt(st.nextToken());} static double d() {return Double.parseDouble(st.nextToken());} static String s() {return st.nextToken();} static long l() {return Long.parseLong(st.nextToken());} }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
9717acf092f7ee76fecb07f3b871e45c
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.util.Scanner; public class FeedACat { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int HH = sc.nextInt(); int mm = sc.nextInt(); double H = sc.nextDouble(); double D = sc.nextDouble(); double C = sc.nextDouble(); double N = sc.nextDouble(); System.out.println(getPrice(HH,mm,H,D,C,N)); } private static double getPrice(int HH, int mm, double H, double D, double C, double N){ double price1=0.00; double price2=0.00; if(HH<20){ price1 = Math.ceil((H+((20-HH)*60-mm)*D)/N)*(C*4/5); price2 = Math.ceil(H/N)*C; return Math.min(price1,price2); } return Math.ceil(H/N)*(C*4/5); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
9928242cb61ee471a1ec5e556bd8b619
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputReader reader = new InputReader(System.in); double number1=reader.nextDouble(); double number2=reader.nextDouble(); double number3=reader.nextDouble(); double number4=reader.nextDouble(); double number5=reader.nextDouble(); double number6=reader.nextDouble(); double doi = (number3 + (20 * 60 - number1 * 60 - number2) * number4); double soluongbanh = Math.ceil((doi / number6)); double soluongbanh2 = Math.ceil((number3 / number6)); if (number1 >= 20) { double temp =(double)Math.round((soluongbanh2 * number5 * 0.8)*1000)/1000; System.out.println(temp); } else { if (soluongbanh * number5 * 0.8 > soluongbanh2 * number5) { System.out.println(soluongbanh2 * number5); } else { System.out.println(soluongbanh* number5 * 0.8); } } } static class InputReader { StringTokenizer tokenizer; BufferedReader reader; String token; String temp; public InputReader(InputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public InputReader(FileInputStream stream) { tokenizer = null; reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() throws IOException { return reader.readLine(); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { if (temp != null) { tokenizer = new StringTokenizer(temp); temp = null; } else { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { } } return tokenizer.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
dd5f570dc0f26c79dd3b175588cd9c1b
train_003.jsonl
1521822900
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; public class Main implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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 String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { 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, nextInt()); 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, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } static long gcd(long a,long b){ if(b==0)return a;return gcd(b,a%b); } static long modPow(long a,long p,long m){ if(a==1)return 1;long ans=1;while (p>0){ if(p%2==1)ans=(ans*a)%m;a=(a*a)%m;p>>=1; }return ans; } static long modInv(long a,long m){return modPow(a,m-2,m);} void reverse(int a[],int i,int j){ int size=j-i+1; int cc=0; for (int k = i; k <=(i+j)/2 ; k++) { int temp=a[k]; a[k]=a[j-cc]; a[j-cc]=temp; cc++; } } String f(String s1,String s2,PrintWriter out){ int i=s1.length()-1; int j=0; Stack<Character> ss1=new Stack<>(); StringBuffer ss2=new StringBuffer(""); Stack<Character> temp=new Stack<>(); int max=0; while(j<s2.length() && i>=0){ ss1.add(s1.charAt(i--)); ss2.append(s2.charAt(j++)); int ff=0,k; int uu=ss1.size(); for (k = 0; k <Math.min(uu,ss2.length()) ; k++) { char tt=ss1.pop(); temp.add(tt); if(ss2.charAt(k)!=tt){ ff=1; break; } } if(ff==0){ max=Math.max(max,Math.min(uu,ss2.length())); } while (!temp.isEmpty()){ ss1.add(temp.pop()); } } return s1+s2.substring(max,s2.length()); } int nextper(int a[],int n,PrintWriter out){ int i; for (i = n-2; i >=0 ;) { if(a[i]>=a[i+1]){ i--; } else{ break; } } if(i==-1){ return -1; } else{ for (int j = n-1; j >i ;) { if(a[i]<a[j]){ int temp=a[j]; a[j]=a[i]; a[i]=temp; reverse(a,i+1,n-1); break; } else if(a[i]==a[j]){ j--; } else{ j--; } } } return 0; } public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int h=sc.nextInt(); int m=sc.nextInt(); int tt=60*h+m; int s=sc.nextInt(); int inc=sc.nextInt(); double cost=sc.nextDouble(); int dec=sc.nextInt(); int tto=20*60; if(tt<tto){ double ans=Math.min(Math.ceil(1.0*s/dec)*cost,((Math.ceil((s+(tto-tt)*inc*1.0)/dec)))*(cost-cost*.2)); out.println(ans); } else{ double ans=Math.min(Double.MAX_VALUE,(Math.ceil(s*1.0/dec))*(cost-cost*.2)); out.println(ans); } out.close(); } }
Java
["19 00\n255 1 100 1", "17 41\n1000 6 15 11"]
1 second
["25200.0000", "1365.0000"]
NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
Java 8
standard input
[ "greedy", "math" ]
937acacc6d5d12d45c08047dde36dcf0
The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).
1,100
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
standard output
PASSED
839e3fa7c78ef3f4119282b16e17606a
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class E { public static void main(String[] args) { // write your code here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int map[][] = new int[n][n]; String input[] = new String[n]; for(int j = 0; j < n; j++) { input[j] = sc.next(); } for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { map[j][k] = Character.getNumericValue(input[j].charAt(k)); } } boolean possible = true; boolean right = true; boolean down = true; for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { right = true; down = true; if(map[j][k] == 1) { if(j + 1 < n) { if(map[j+1][k] != 1) { right = false; } } if(k + 1 < n) { if(map[j][k+1] != 1) { down = false; } } if(!right && !down) { possible = false; } } } } if(possible) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
3d98b6dca8338b00bed90191a1781a4a
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.Scanner; public class Polygon { public static void main(String[] args) { int t; int n; String[] mat2; char[][] mat; boolean ok; Scanner input = new Scanner(System.in); t = input.nextInt(); for(int i = 0; i < t; i++) { ok = true; n = input.nextInt(); mat2 = new String[n]; for(int j = 0; j < n; j++) { mat2[j] = input.next(); } mat = new char[n][n]; for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { mat[j][k] = mat2[j].charAt(k); } } for(int j = 0; j < n - 1; j++) { for(int k = 0; k < n - 1; k++) { if(mat[j][k] == '1' && mat[j + 1][k] == '0' && mat[j][k + 1] == '0') { ok = false; break; } } if(!ok) break; } if(ok) System.out.println("YES"); if(!ok) System.out.println("NO"); } input.close(); } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
662dc27bccc2ad681072b5aa4ceb34af
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; public class Chess { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int k = in.nextInt(); char[][] matrix = new char[k][k]; for(int i=0;i<k;i++) { String str = in.next(); for(int j=0;j<k;j++) { matrix[i][j] = str.charAt(j); } } boolean b = true; for (int i = 0; i < k - 1; i++) { for (int j = 0 ;j < k - 1; j++) { if (matrix[i][j] == '1' && (matrix[i + 1][j] == '0' && matrix[i][j + 1] == '0')) { b = false; } } } String x = b == true ? "YES" : "NO"; System.out.println(x); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
deed0f6c1952c427c599e088d2f0b1a4
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); String[] s=new String[n]; int[][] arr=new int[n][n]; for(int j=0;j<n;j++) { s[j]=sc.next(); for (int k = 0; k < n; k++) arr[j][k] = s[j].charAt(k)-48; } int flag=0; for(int j=0;j<n-1;j++) { for(int k=0;k<n-1;k++) { if(arr[j][k]==1) { if(arr[j+1][k]==0 && arr[j][k+1]==0) { flag=1; break; } } } } if(flag==0) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
fbdf687c9cca188077493c5bc6ca3889
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Map; import java.util.PriorityQueue; import java.util.Set; import javax.lang.model.util.ElementScanner6; import java.util.List; /** * Using FastReader and Output Class * * @author Wallace */ public class Main { static int parent[]=new int[26]; public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); IAMBACK solver = new IAMBACK(); solver.solve(1, in, out); out.close(); } static class IAMBACK { static long mod=1000*1000*1000+7; public void solve(int testNumber, FastReader in, PrintWriter out) { int t=in.nextInt(); while(t!=0){ int n=in.nextInt(); char a[][]=new char[n][n]; for(int i=0;i<n;i++){ String s=in.nextString(); for(int j=0;j<n;j++){ a[i][j]=s.charAt(j); } } int ans=0; for(int i=n-2;i>=0;i--){ for(int j=i;j>=0;j--){ if(a[i][j]=='1'){ if(a[i+1][j]!='1' && a[i][j+1]!='1'){ ans=1; break; } } if(a[j][i]=='1'){ if(a[j+1][i]!='1' && a[j][i+1]!='1'){ ans=1; break; } } } if(ans==1) break; } if(ans==0) out.println("YES"); else out.println("NO"); t--; } } public int helper1(int c,int last,int kitnasum,int l,int target,int dp[][][]){ if(c>l || kitnasum>target) return 0; if(c==l) return dp[c][kitnasum][last]=1; if(dp[c][kitnasum][last]>0) return dp[c][kitnasum][last]; int count=0; for(int i=last+1;i<=52;i++){ count+= helper1(c+1, i, kitnasum+i, l, target,dp); } return dp[c][kitnasum][last]=count; } // public static long helper(long a, long b){ // long ans=1; // while(b!=0) { // if(b%2==1){ // ans=(ans*a)%mod; // } // a=(a*a)%mod; // b=b/2; // } // return ans; // } } // for the fastreader static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { return nextString(); } public String nextString() { int c = pread(); while (isSpaceChar(c)) c = pread(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = pread(); } while (!isSpaceChar(c)); return res.toString(); } public String[] nextStringArray(int n) { String array[]=new String[n]; for(int i=0;i<n;i++) { array[i]=nextString(); } return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } // pcwallace public double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } public long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long nextLong() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } double res = 0.0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
09d1eaf3e3033166c3bf4e16520d6c58
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Q5 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub MScanner s = new MScanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); char[][] arr = new char[n][n]; for (int i = 0; i < n; i++) { String str = s.next(); arr[i] = str.toCharArray(); } boolean res = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] == '1') { boolean r1 = true; if (i + 1 < n) r1 = arr[i + 1][j] == '1'; boolean r2 = true; if (j + 1 < n) r2 = arr[i][j + 1] == '1'; if (r1 == false && r2 == false) { // cout<<r1<<" "<<r2<<endl; res = false; break; } } } if (!res) break; } if (res) System.out.println("YES"); else System.out.println("NO"); } } static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(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 int[] intArr(int n) throws IOException { int[] in = new int[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); return in; } public long[] longArr(int n) throws IOException { long[] in = new long[n]; for (int i = 0; i < n; i++) in[i] = nextLong(); return in; } public int[] intSortedArr(int n) throws IOException { int[] in = new int[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); shuffle(in); Arrays.sort(in); return in; } public long[] longSortedArr(int n) throws IOException { long[] in = new long[n]; for (int i = 0; i < n; i++) in[i] = nextLong(); shuffle(in); Arrays.sort(in); return in; } public Integer[] IntegerArr(int n) throws IOException { Integer[] in = new Integer[n]; for (int i = 0; i < n; i++) in[i] = nextInt(); return in; } public Long[] LongArr(int n) throws IOException { Long[] in = new Long[n]; for (int i = 0; i < n; i++) in[i] = nextLong(); return in; } 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); } } static void shuffle(int[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); int tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } static void shuffle(long[] in) { for (int i = 0; i < in.length; i++) { int idx = (int) (Math.random() * in.length); long tmp = in[i]; in[i] = in[idx]; in[idx] = tmp; } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
eb14b817d22de9dd6bb16487ad20eb17
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; public class S { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String args[]) throws IOException{ S.init(System.in); int t = S.nextInt(); while(t-- > 0) { int n = S.nextInt(); int arr[][] = new int[n][n]; for(int i = 0 ; i < n ; i++) { String s = S.next(); for(int j = 0 ; j < n ; j++) { arr[i][j] = Integer.valueOf(s.charAt(j) + ""); } } int flag = 0; for(int i = 0 ; i < n - 1 ; i++) { int q = 0; for(int j = 0 ; j < n - 1 ; j++) { if(arr[i][j] == 1) { if(arr[i + 1][j] == 0 && arr[i][j + 1] == 0) { flag = 1; break; } } } if(flag == 1) { break; } } if(flag == 1) { System.out.println("NO"); }else { System.out.println("YES"); } } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
9317d53c2050b5856014e4247b2b1a69
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.*; import java.util.*; public class SolutionB { public static void main(String[] args) throws Exception { int tc = scan.nextInt(); for (int i = 0; i < tc; i++) { solve(); } // out.close(); } private static void solve() { int n = scan.nextInt(); char[][] board = new char[n][n]; for (int i = 0; i < n; i++) board[i] = scan.next().toCharArray(); boolean[][] res = new boolean[n][n]; for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { if (board[i][j] == '0') res[i][j] = true; else if (i == n - 1 || j == n - 1) res[i][j] = true; else res[i][j] = board[i][j + 1] == '1' || board[i + 1][j] == '1'; } } for (boolean[] r : res) { for (boolean b : r) { if (!b) { System.out.println("NO"); return; } } } System.out.println("YES"); } private static int gcd(int a, int b) { if (a > b) return gcd(b, a); if (a == 0) return b; return gcd(b, b % a); } //-----------PrintWriter for faster output--------------------------------- // public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static MyScanner scan = new MyScanner(); //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
e1f92c531e26e9100669c932f5087cf3
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.*; import java.util.*; public class SolutionB { public static void main(String[] args) throws Exception { int tc = scan.nextInt(); for (int i = 0; i < tc; i++) { solve(); } // out.close(); } private static void solve() { int n = scan.nextInt(); char[][] board = new char[n][n]; for (int i = 0; i < n; i++) board[i] = scan.next().toCharArray(); boolean[][] res = new boolean[n][n]; for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { if (board[i][j] == '0') res[i][j] = true; else if (i == n - 1 || j == n - 1) res[i][j] = true; else res[i][j] = board[i][j + 1] == '1' || board[i + 1][j] == '1'; } } for (boolean[] r : res) { for (boolean b : r) { if (!b) { System.out.println("NO"); return; } } } System.out.println("YES"); } private static int gcd(int a, int b) { if (a > b) return gcd(b, a); if (a == 0) return b; return gcd(b, b % a); } //-----------PrintWriter for faster output--------------------------------- // public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static MyScanner scan = new MyScanner(); //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
328def6b0770352b0424c03e375c7c70
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class polygon { public static void main(String[] args) { FastReader ob=new FastReader(); int t=ob.nextInt(); while(t-->0) { int n=ob.nextInt(); int a[][]=new int[n+1][n+1]; for(int i=0;i<n;i++) { String s=ob.nextLine(); for(int j=0;j<n;j++) a[i][j]=s.charAt(j)-'0'; } for(int i=0;i<n;i++) { a[i][n]=1; a[n][i]=1; } int c=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(a[i][j]==1 &&!(a[i][j+1]==1 || a[i+1][j]==1)) { c=1; break; } } } if(c==0) System.out.println("YES"); else System.out.println("NO"); } } } 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
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
6dfdb117d5667c52f1466ca10b71f1e3
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author unknown */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); EPolygon solver = new EPolygon(); solver.solve(1, in, out); out.close(); } static class EPolygon { public void solve(int testNumber, InputReader in, OutputWriter out) { int ntc = in.nextInt(); while ((ntc--) > 0) { int n = in.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { char[] chrs = in.nextString().toCharArray(); for (int j = 0; j < n; j++) { arr[i][j] = chrs[j] - '0'; } } boolean ok = true; for (int i = n - 2; i >= 0; i--) { for (int j = i; j >= 0; j--) { if (arr[i][j] == 1) { if (arr[i][j + 1] == 0 && arr[i + 1][j] == 0) { ok = false; break; } } if (arr[j][i] == 1) { if (arr[j][i + 1] == 0 && arr[j + 1][i] == 0) { ok = false; break; } } } if (!ok) { break; } } if (!ok) { out.println("NO"); } else { out.println("YES"); } } } } 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 nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; ++i) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
117fbda03825e25e0759f03f2509831b
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author unknown */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); EPolygon solver = new EPolygon(); solver.solve(1, in, out); out.close(); } static class EPolygon { public void solve(int testNumber, InputReader in, OutputWriter out) { int ntc = in.nextInt(); while ((ntc--) > 0) { int n = in.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { char[] chrs = in.nextString().toCharArray(); for (int j = 0; j < n; j++) { arr[i][j] = chrs[j] - '0'; } } boolean ok = true; // //checking last col // for(int i = n-1-1;i>=0;i--){ // if(arr[i][n-1] == 1){ // if(arr[i+1][n-1]!=1){ // ok = false; // break; // } // } // } // if(!ok){ // out.println("NO"); // continue; // } // // //checking last row // for(int i = n-1-1;i>=0;i--){ // if(arr[n-1][i] == 1){ // if(arr[n-1][i+1]!=1){ // ok = false; // break; // } // } // } // if(!ok){ // out.println("NO"); // continue; // } for (int i = n - 2; i >= 0; i--) { for (int j = i; j >= 0; j--) { if (arr[i][j] == 1) { if (arr[i][j + 1] == 0 && arr[i + 1][j] == 0) { ok = false; break; } } if (arr[j][i] == 1) { if (arr[j][i + 1] == 0 && arr[j + 1][i] == 0) { ok = false; break; } } } if (!ok) { break; } } if (!ok) { out.println("NO"); } else { out.println("YES"); } } } } 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 nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; ++i) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
d63716b6eac8c41a18928fc89b04b9bd
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.Scanner; public class Div3644E { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int n = scanner.nextInt(); int[][] field = new int[n][n]; for (int j = 0; j < n; j++) { String line = scanner.next(); for (int k = 0; k < n; k++) { field[j][k] = Integer.parseInt(line.substring(k, k + 1)); } } System.out.println(solve(field)); } } private static String solve(int[][] field) { for (int i = 0; i < field.length - 1; i++) { for (int j = 0; j < field.length - 1; j++) { if (field[i][j] == 1) { if (field[i][j + 1] == 0 && field[i + 1][j] == 0) { return "NO"; } } } } return "YES"; } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
21e6c114d6fdc16bb435a543ee4efd5e
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.Scanner; import java.util.*; public class Helloworld { public static void main(String[] args) { Scanner s = new Scanner(System.in); int input = s.nextInt(); for (int i1 = 0; i1 < input; i1++) { int a = s.nextInt(); char[][] array = new char[a][a]; for (int i = 0; i < a; i++) { String tmp = s.next(); for (int j = 0; j < a; j++) { array[i][j] = tmp.charAt(j); } } boolean flag = true; for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array[i].length - 1; j++) { if (array[i][j] == '1') { if (array[i][j + 1] == '0' && array[i + 1][j] == '0') { flag = false; } if (!flag) { break; } } } } if (flag) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
b8941708b7776d1f901eb78074860e8d
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; public class cf644div3E { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int k=sc.nextInt(); for(int p=1;p<=k;p++) {int n=sc.nextInt(); int arr[][]=new int[n][n]; for(int i=0;i<n;i++) {String s=sc.next(); for(int j=0;j<n;j++) {arr[i][j]=Integer.parseInt(String.valueOf((s).charAt(j)));}} int f=0; for(int i=0;i<n;i++) {for(int j=0;j<n;j++) {if(arr[i][j]==1) {if((i+1<n && arr[i+1][j]!=1) && (j+1<n && arr[i][j+1]!=1)) {f=1;break;} }}} if(f==1) System.out.println("no"); else System.out.println("yes");} sc.close(); }}
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
f692d49ead6e0c1a7326c1a87205d356
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; import java.math.*; public class S{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int q = in.nextInt(); for(int t = 0; t < q; t++){ int n = in.nextInt(); char arr[][] = new char[n][n]; for(int i = 0; i < n; i++){ String temp = in.next(); arr[i] = temp.toCharArray(); } boolean bool = false; for(int i = 0; i < n - 1; i++){ if(bool){ break; } for(int j = 0; j < n - 1; j++){ if(arr[i][j] == '1'){ if(arr[i + 1][j] != '1' && arr[i][j + 1] != '1'){ bool = true; break; } } } } if(bool){ System.out.println("NO"); }else{ System.out.println("YES"); } } } } // class M implements Comparable<M>{ // private long a, b; // public M(long a, long b){ // this.a = a; // this.b = b; // } // public long getA(){return a;} // public long getB(){return b;} // public int compareTo(M m){ // if(this.a == m.getA()){ // if(this.b <= m.getB()){ // return -1; // } // return 1; // } // if(this.a <= m.getA()){ // return -1; // } // return 1; // } // }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
746a5cd5245c9680e11a87cce32f5c66
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t,i,n,j,k; boolean b; String s; t=sc.nextInt(); for(i=0;i<t;i++){ n=sc.nextInt(); int a[][]=new int[n][n]; for(j=0;j<n;j++){ s=sc.next(); for(k=0;k<n;k++) a[j][k]=Integer.parseInt(Character.toString(s.charAt(k))); s=""; } b=check(a); if(!b) System.out.println("YES"); else System.out.println("NO"); } } public static boolean check(int a[][]){ int n=a.length; int i,j; boolean flag=false; for(i=0;i<n-1;i++){ for(j=0;j<n-1;j++){ if(a[i][j]==1){ if(a[i][j+1]==0&&a[i+1][j]==0){ flag=true; break; } } } } return flag; } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
6fa933a1bc0701bb6c34c71dadeadc88
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.*; import java.util.*; public final class Template { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- != 0) { int n = sc.nextInt(); int a[][] = new int[n][n]; for (int i = 0; i < n; i++) { String str = sc.next(); for (int j = 0; j < n; j++) { a[i][j] = str.charAt(j) - '0'; } } boolean flag = false; for (int i = 0; i < n; i++){ if (!flag) { for (int j = 0; j < n; j++) { if (a[i][j] == 1) { if (! (j >= n-1 || a[i][j+1] == 1 || i >= n-1 || a[i+1][j] == 1) ) { flag = true; break; } } } } else break; } System.out.println(flag ? "NO" : "YES"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
72c8538737e52fab2ba2b89498a080af
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); int a[][]=new int[n][n]; for(int i=0;i<n;i++) { String st1[]=br.readLine().split(""); for(int j=0;j<n;j++) { a[i][j]=Integer.parseInt(st1[j]); }} boolean flag=false; if(n==1) bw.write("YES"+"\n"); else{ for(int i=0;i<n-1;i++) { for(int j=0;j<n-1;j++) { if(a[i][j]==1) { if(a[i+1][j]==1 || a[i][j+1]==1) continue; else { flag=true; break; } } } if(flag) break; } if(!flag) bw.write("YES"+"\n"); else bw.write("NO"+"\n"); }} bw.flush(); }}
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
a228030d0f3faea89631693e15055d45
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); int t=Integer.parseInt(br.readLine()); while(t-->0) { int n=Integer.parseInt(br.readLine()); int a[][]=new int[n][n]; for(int i=0;i<n;i++) { String st1[]=br.readLine().split(""); for(int j=0;j<n;j++) { a[i][j]=Integer.parseInt(st1[j]); }} boolean flag=false; if(n==1) bw.write("YES"+"\n"); else{ for(int i=n-2;i>=0;i--) { for(int j=n-2;j>=0;j--) { if(a[i][j]==1) { if(a[i+1][j]==1 || a[i][j+1]==1) continue; else { flag=true; break; } } } if(flag) break; } if(!flag) bw.write("YES"+"\n"); else bw.write("NO"+"\n"); }} bw.flush(); }}
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
541bbd336c3bc21591207492c6ae95b7
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int t = Integer.parseInt(read.nextLine()); for(int tc = 0; tc < t; tc++) { int n = Integer.parseInt(read.nextLine()); String[][] mat_temp = new String[n][n]; int[][] mat = new int[n][n]; for(int i = 0; i < n; i++) { String[] temp = read.nextLine().split(""); mat_temp[i] = temp; } for(int x = 0; x < n; x++) { for(int y = 0; y < n; y++) { mat[x][y] = Integer.parseInt(mat_temp[x][y]); } } boolean g = true; int[][] temp = new int[n][n]; for(int x = 0; x < n && g; x++) { for(int y = 0; y < n && g; y++) { int score = 0; if(mat[x][y] == 1) { score += checkA(mat, x, y, n); score += checkB(mat, x, y, n); } temp[x][y] = score; if(score < 0) { g = false; } } } if(g) { System.out.println("YES"); } else { System.out.println("NO"); } } } // public static void out(int[][] mat, int n) { // } public static int checkA(int[][] mat, int x, int y, int n) { for(int i = y+1; i < y+2 && i < n; i++) { if(mat[x][i] == 0) { return -1; } } return 1; } public static int checkB(int[][] mat, int x, int y, int n) { for(int i = x+1; i < x+2 && i < n; i++) { if(mat[i][y] == 0) { return -1; } } return 1; } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
9da02e8a2905d3d1857f96e26d193bce
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; public class E1 { public static boolean isRight(String[] grid, int i, int j) { int a = grid.length; if (grid[i].charAt(j) == '0') { return true; } else { if (i == a - 1 && j == a- 1) { return true; } if (i == a - 1) {return true;} if (j == a - 1) {return true;} if (j < a - 1 && grid[i].charAt(j + 1) == '1') { return true; } if (i < a - 1 && grid[i + 1].charAt(j) == '1') { return true; } } return false; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int test = scan.nextInt(); for (int u = 0; u < test; u++) { int a = scan.nextInt(); String[] grid = new String[a]; boolean[][] grid2 = new boolean[a][a]; for (int i = 0 ; i < a; i++) { grid[i] = scan.next(); } boolean flag = true; for (int i = 0; i < a; i++) { for (int j = 0; j < a; j ++) { if (isRight(grid, i, j) == false) { flag = false; break; } } } if (flag == false) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
b10bb745e0ef72dceacdcb0fe32f555b
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.*; import java.util.*; public class E { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); for(int i = 0; i < t; ++i) { int n = sc.nextInt(); int[][] vals = new int[n][n]; for(int j = 0; j < n; j++) { String ln = sc.nextLine(); for(int k = 0; k < n; k++) { vals[j][k] = Integer.parseInt(ln.substring(k, k+1)); } } boolean sol = true; for(int j = n-2; j >= 0; j--) { for(int k = n-2; k >= 0; k--) { if((vals[j][k]==1) && (vals[j+1][k] == 0) && (vals[j][k+1]==0)) sol = false; } } System.out.println(sol ? "YES" : "NO"); } out.close(); } public static PrintWriter out; public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
3c07ee3edf90d0d099bfa0ce97446300
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.* ; import java.lang.Math ; public class Main { public static void main (String [] args){ Scanner in = new Scanner (System.in) ; int t = in.nextInt() ; while (t-- > 0){ int n = in.nextInt(); String [] a = new String[n]; in.nextLine(); for(int i = 0;i<n;i++)a[i] = in.nextLine() ; int fg = 1 ; for(int i = 0 ;i < n-1 ; i++){ for(int j = 0 ;j < n-1 ; j++){ if(a[i].charAt(j) - '0' == 1){ if(a[i].charAt(j + 1) - '0' == 0 && a[i + 1].charAt(j)-'0' == 0) fg = 0; } } } if(fg == 1)System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
970eda3fd234a5e97cead90fc4c00d42
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; public class Main { public static Scanner input = new Scanner(System.in); public static void main(String args[]) { int cases = input.nextInt(); int n; String[] s; while (cases-- > 0) { n = input.nextInt(); s = new String[n]; for (int i = 0; i < n; i++) { s[i] = input.next(); } boolean ans = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (s[i].charAt(j) == '1') { if ((j + 1 < n) && (s[i].charAt(j + 1) != '1') && (i + 1 < n) && (s[i + 1].charAt(j) != '1')) { ans = false; break; } } } if (!ans) break; } if (ans) { System.out.println("YES"); } else { System.out.println("NO"); } } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
3989956e9fb459a8d3e91c8cbb1182c6
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; import java.io.*; public class MyClass { 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 void main(String args[]) { FastReader s=new FastReader();int t=0,n=0;String str="";boolean k=true; //Scanner in=new Scanner(System.in); //if(in.hasNext()) t=s.nextInt(); for(int j1=0;j1<t;j1++) { // if(in.hasNext()) n=s.nextInt(); char mat[][]=new char[n][n]; for(int i=0;i<n;i++) { str=s.next(); char ch[]=str.toCharArray(); mat[i]=ch; } k=true; for(int i=0;i<n-1;i++) { for(int j=0;j<n-1;j++) { if(mat[i][j]=='1') { if(mat[i][j+1]=='1' || mat[i+1][j]=='1') continue; else { k=false;break; } } } if(k==false) break; } System.out.println((k==false)?"NO":"YES"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
6381dcd9a9339989e9eed903b86a75a9
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { private static class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } public static void main(String[] args) { FastScanner sc = new FastScanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); String[] rows = new String[n]; for (int i = 0; i < n; i++) rows[i] = sc.next(); boolean result = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (rows[i].charAt(j) == '0') continue; if (i + 1 == n || j + 1 == n) continue; if ((i + 1 < n && rows[i + 1].charAt(j) == '1') || (j + 1 < n && rows[i].charAt(j + 1) == '1')) { continue; } result = false; break; } if (!result) break; } System.out.println(result ? "YES" : "NO"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
b0c30da4e9dc9f845132569107376c75
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static boolean check(int polygon[][],int n){ for(int i=n-1;i>=0;i--){ boolean flag=false; for(int j=n-1;j>=0;j--){ if(polygon[j][i]==0){ flag=true; }else{ if(flag && i!=n-1 && polygon[j][i+1]==0 && j!=n-1 && polygon[j+1][i]==0){ return false; } } } } return true; } public static void main(String []args)throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); int t=Integer.parseInt(br.readLine()); while(t>0){ int n=Integer.parseInt(br.readLine()); int polygon[][]=new int[n][n]; for(int i=0;i<n;i++){ String str=br.readLine(); for(int j=0;j<n;j++){ polygon[i][j]=str.charAt(j)-'0'; } } if(check(polygon,n)){ bw.write("YES\n"); }else{ bw.write("NO\n"); } t--; } bw.flush(); } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
f079a51375e94124ba5f890fa8d540fc
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } 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 = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0){ int n = in.nextInt(); String[] str = new String[n]; for(int i=0;i<n;i++) str[i]=in.readString(); char[][] a = new char[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++) { a[i][j]=str[i].charAt(j); } } boolean flag = true; for(int i=0;i<n-1;i++){ for(int j=0;j<n-1;j++){ if(i==n-1){ if(a[i][j]=='1' && a[i][j+1]=='0'){ flag = false; break; } }else if(j==n-1){ if(a[i][j]=='1' && a[i+1][j]=='0'){ flag=false; break; } }else{ if(a[i][j]=='1' && (a[i][j+1]=='0' && a[i+1][j]=='0')){ flag=false; break; } } } } if(flag) System.out.println("YES"); else System.out.println("NO"); } w.close(); } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
705f7f178581614437da383df629ea72
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.IOException; import java.io.InputStream; import java.util.*; public class Solution { public static void main(String args[]) throws IOException { FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); int i, j; boolean ok=true; int t=ok?in.nextInt():1; while(t-->0){ int n=in.nextInt(); boolean arr[][]=new boolean[n][n]; for(i=0;i<n;i++){ String s=in.next(); for(j=0;j<n;j++){ if(s.charAt(j)=='1') arr[i][j]=true; } } int f=0; outer: for(i=0;i<n-1;i++){ for(j=0;j<n-1;j++){ if(arr[i][j] && !arr[i+1][j] && !arr[i][j+1]){ f=1; break outer; } } } if(f==0) sb.append("YES\n"); else sb.append("NO\n"); } System.out.print(sb); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
903952801af6df2deac17771e47f5d01
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.Scanner; //1360/E public class Ploygon { public static void main(String[] args) { Scanner val = new Scanner(System.in); int t = val.nextInt(); for (int i = 0; i < t; i++) { int n = val.nextInt(); int a[][] = new int[n + 1][n + 1]; for (int j = 0; j < a.length; j++) { for (int k = 0; k < a.length; k++) { if (j == n) { a[j][k] = -1; } if (k == n) { a[j][k] = -1; } } } for (int j = 0; j < n; j++) { String S=val.next(); char c[]=S.toCharArray(); for (int k = 0; k < c.length; k++) { a[j][k] = Integer.parseInt(String.valueOf(c[k]));; } } // for (int j = 0; j < a.length; j++) { // System.out.println(); // for (int k = 0; k < a.length; k++) { // System.out.print(a[j][k]+" "); // } // } int loopFlag = 0; boolean result = true; for (int j = 0; j < a.length; j++) { for (int k = 0; k < a.length; k++) { if (a[j][k] == 1) { if ((a[j + 1][k] == 1 || a[j + 1][k] == -1) || (a[j][k + 1] == 1 || a[j][k + 1] == -1)) { continue; } else { loopFlag = 1; result = false; break; } } } if (loopFlag == 1) { break; } } if (result) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
4904123b70c3fda9ba643897f87d80e9
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
/* package codechef; // don't place package name! */ import java.lang.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); char ch[][] = new char[n][n]; for(int i=0;i<n;i++) { String s = sc.next(); for(int j=0;j<n;j++) { ch[i][j]=s.charAt(j); } } HashSet<Pair> set = new HashSet<>(); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) if(ch[i][j]=='1') set.add(new Pair(i,j)); } boolean ans=true; for(Pair p:set) { int i=p.x,j=p.y; //out.println(i+" "+j); if(i==n-1 || j==n-1) continue; if(ch[i+1][j]!='1' && ch[i][j+1]!='1' ) {ans=false;break;} } if(ans) out.println("YES"); else out.println("NO"); } out.close(); } } class Pair { int x,y; Pair(int x,int y) { this.x=x; this.y=y; } } 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
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
8a1e8dc2d0433b9905510568d775b8f8
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
//package Code; import java.util.*; import java.math.*; import java.lang.System; import java.nio.CharBuffer; import static java.lang.System.*; import java.io.InputStream; import java.sql.Time; public class Main { public static void main(String[] args) { Integer b, j, k, t; Integer l, r, m, i, n; Scanner in = new Scanner(System.in); t = in .nextInt(); while (t != 0) { t--; n = in .nextInt(); ArrayList < String > s = new ArrayList < String > (); String tmp; for (i = 0; i < n; i++) { tmp = in .next(); s.add(tmp); } Boolean bo = false; for (i = 0; i < n -1; i++) { for (j = 0; j < n - 1; j++) { if (s.get(i).charAt(j) == '1') { if (s.get(i).charAt(j + 1) != '1' && s.get(i + 1).charAt(j) != '1') { bo = true; i = n; break; } } } } if (bo) { System.out.println("NO"); } else { System.out.println("YES"); } } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
e3ccf39b049e13443e78e509dd44bb66
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.Scanner; public class polygon { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (--t >= 0) { int n = sc.nextInt(), matrix[][] = new int[n][n]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < n; j++) matrix[i][j] = s.charAt(j) - '0'; } int flag = 0; for (int i = 0; i < n && flag == 0; i++) { for (int j = 0; j < n && flag == 0; j++) { if (matrix[i][j] == 1) { if (i == n - 1 || j == n - 1) continue; else if (matrix[i][j + 1] == 1 || matrix[i + 1][j] == 1) { continue; } else { flag = 1; System.out.println("NO"); } } } } if (flag == 0) System.out.println("YES"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
1065cc7a0aaaad9598b41f153d8fa630
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; public class canons{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int[][] arr = new int[n][n]; for(int i=0;i<n;i++){ String s = sc.next(); char[] c = s.toCharArray(); for(int j=0;j<n;j++){ arr[i][j]=c[j]-'0'; } } boolean ans = true; for(int i=n-2;i>=0;i--){ for(int j=n-2;j>=0;j--){ if(arr[i][j]==1 && arr[i+1][j]!=1 && arr[i][j+1]!=1){ ans = false; } } } System.out.println(ans? "YES":"NO"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
3b28d44e25c87c2bda7ee89f232a3100
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int zzz = sc.nextInt(); for (int zz = 0; zz < zzz; zz++) { int n = sc.nextInt(); char[][]m = new char[n][n]; for (int i = 0;i < n;i++){ m[i] = sc.next().toCharArray(); } boolean valid = true; for (int i = 0;i < m.length - 1;i++){ for(int j = 0;j < m[0].length - 1;j++){ if(m[i][j] == '1' && (m[i + 1][j] == '0' && m[i][j + 1] == '0')){ valid = false; break; } } } System.out.println(valid ? "YES":"NO"); } } private static int[]readArr(int n){ int[]ans = new int[n]; for(int i = 0;i < n;i++){ ans[i] = sc.nextInt(); } return ans; } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
c0c1315e673143437a22fda4fd390c3b
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.*; import java.io.*; public class polygon { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t != 0) { t--; int n = sc.nextInt(); sc.nextLine(); String[] arr = new String[n]; for (int i = 0; i < arr.length; i++) { String s = sc.nextLine(); arr[i] = s; } // for (int i = 0; i < arr.length; i++) // System.out.println(arr[i]); boolean flag = true; for (int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if (arr[i].charAt(j) == '1') { if (i < n-1 && j < n-1) { if (arr[i+1].charAt(j) == '0' && arr[i].charAt(j+1) == '0') { flag = false; break; } } } } if (!flag) break; } if (flag) System.out.println("YES"); else System.out.println("NO"); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
38e7580734ee328c906eff30b8fdb3fe
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.Reader; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.util.NoSuchElementException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Ruins He */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskE solver = new TaskE(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskE { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); char[][] mp = new char[n][]; for (int i = 0; i < n; i++) { mp[i] = in.nextChars(); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mp[i][j] == '1' && i < n - 1 && mp[i + 1][j] == '0' && j < n - 1 && mp[i][j + 1] == '0') { out.println("NO"); return; } } } out.println("YES"); } } static class InputReader { public final BufferedReader reader; public StringTokenizer tokenizer = null; public InputReader(Reader reader) { this.reader = new BufferedReader(reader, 32767); } public InputReader(InputStream inputStream) { this(new InputStreamReader(inputStream)); } public String next() { if (hasNext()) return tokenizer.nextToken(); else throw new NoSuchElementException(); } public int nextInt() { return Integer.parseInt(next()); } public char[] nextChars() { return next().toCharArray(); } public boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String nextLine = reader.readLine(); if (nextLine == null) return false; tokenizer = new StringTokenizer(nextLine); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.hasMoreTokens(); } } static class OutputWriter { private final PrintWriter writer; private OutputWriter(Writer writer, boolean autoFlush) { this.writer = new PrintWriter(new BufferedWriter(writer, 32767), autoFlush); } public OutputWriter(Writer writer) { this(writer, true); } public OutputWriter(OutputStream outputStream) { this(new OutputStreamWriter(outputStream), false); } public OutputWriter println(String string) { writer.println(string); return this; } public void close() { writer.close(); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output
PASSED
2c173147e85b3c8be584c4e224b970f4
train_003.jsonl
1590327300
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$.                         2. Shoot the cannon in the row $$$2$$$.                         3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int polCount = s.nextInt(); ArrayList<String> answers = new ArrayList(); for (int i = 0; i < polCount; i++) { // int n = s.nextInt(); s.nextLine(); ArrayList<ArrayList<Integer>> rows = new ArrayList<>(); for (int j = 0; j < n; j++) { String stringRow = s.nextLine(); ArrayList newRow = new ArrayList(); for (int k = 0; k < stringRow.length(); k++) { newRow.add(Integer.parseInt(stringRow.substring(k, k+1))); } rows.add(newRow); } boolean found = false; for (int j = 0; j < rows.size(); j++) { ArrayList<Integer> row = rows.get(j); for (int k = 0; k < row.size(); k++) { if (row.get(k) == 1) { //check if not right or last row if (k != row.size() - 1 && j != rows.size() - 1) { if (row.get(k + 1) != 1) { //check if bottom if (rows.get(j+1).get(k) != 1) { found = true; break; } } } } } } if (found) { answers.add("NO"); } else { answers.add("YES"); } } for (String answer : answers) { System.out.println(answer); } } }
Java
["5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111"]
2 seconds
["YES\nNO\nYES\nYES\nNO"]
NoteThe first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
Java 11
standard input
[ "dp", "implementation", "graphs", "shortest paths" ]
eea15ff7e939bfcc7002cacbc8a1a3a5
The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 \le n \le 50$$$) — the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1 — the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$.
1,300
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
standard output