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 | 69514596f9713178f76b4eccafbe48c5 | train_002.jsonl | 1508773500 | One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? | 256 megabytes |
import java.io.*;
import java.util.*;
public class NikitaAndString {
public static void main(String[] args) throws Exception {
FastScanner sc = new FastScanner(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
run(sc, out);
out.close();
}
public static void run(FastScanner sc, PrintWriter out) throws Exception {
String line = sc.next();
int N = line.length();
boolean[] arr = new boolean[line.length()];
for (int i = 0; i < line.length(); i++) {
arr[i] = line.charAt(i) == 'b';
}
int[] preA = new int[N];
int[] preB = new int[N];
(arr[0] ? preB : preA)[0] = 1;
for (int i = 1; i < N; i++) {
preA[i] = preA[i - 1];
preB[i] = preB[i - 1];
(arr[i] ? preB : preA)[i]++;
}
int max = 0;
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
int count = preA[i] - (arr[i] ? 0 : 1);
count += preB[j] - preB[i] + (arr[i] ? 1 : 0);
count += preA[N - 1] - preA[j] + (arr[j] ? 0 : 1);
max = Math.max(count, max);
}
}
out.println(max);
}
static class FastScanner {
final private int BUFFER_SIZE = 1 << 20;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastScanner(InputStream stream) {
din = new DataInputStream(stream);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastScanner(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
String nextLine() throws IOException {
int c = read();
while (c != -1 && isEndline(c))
c = read();
if (c == -1) {
return null;
}
StringBuilder res = new StringBuilder();
do {
if (c >= 0) {
res.appendCodePoint(c);
}
c = read();
} while (!isEndline(c));
return res.toString();
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
String next() throws Exception {
int c = readOutSpaces();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
private int readOutSpaces() throws IOException {
while (true) {
if (bufferPointer == bytesRead)
fillBuffer();
int c = buffer[bufferPointer++];
if (!isSpaceChar(c)) {
return c;
}
}
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
} | Java | ["abba", "bab"] | 2 seconds | ["4", "2"] | NoteIt the first sample the string is already beautiful.In the second sample he needs to delete one of "b" to make it beautiful. | Java 8 | standard input | [
"dp",
"brute force"
] | c768f3e52e562ae1fd47502a60dbadfe | The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". | 1,500 | Print a single integer — the maximum possible size of beautiful string Nikita can get. | standard output | |
PASSED | 4858fcafb30d93af2d2fad57eb5fa32e | train_002.jsonl | 1508773500 | One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? | 256 megabytes | import java.io.*;
import java.util.*;
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);
TaskB solver = new TaskB();
solver.solve(in, out);
out.close();
}
public static class TaskB {
public void solve(InputReader in, PrintWriter out) {
String s = in.next();
int n = s.length();
int prefA[] = new int[n];
int prefB[] = new int[n];
prefA[0] = s.charAt(0) == 'a' ? 1 : 0;
prefB[0] = s.charAt(0) == 'b' ? 1 : 0;
for (int i = 1; i < n; i++) {
prefA[i] = prefA[i - 1] + (s.charAt(i) == 'a' ? 1 : 0);
prefB[i] = prefB[i - 1] + (s.charAt(i) == 'b' ? 1 : 0);
}
int max = 0;
for (int i = -1; i < n; i++) {
int amount1 = sum(0, i, prefA);
for (int j = i; j < n; j++) {
int amount2, amount3;
if (j > i) {
amount2 = sum(i + 1, j, prefB);
amount3 = sum(j + 1, n - 1, prefA);
} else {
amount2 = 0;
if (j + 1 == n)
amount3 = 0;
else
amount3 = sum(j + 1, n - 1, prefA);
}
int curAns = amount1 + amount2 + amount3;
max = Math.max(max, curAns);
}
}
out.println(max);
}
private int sum(int l, int r, int ar[]) {
if (l >= ar.length)
return 0;
if (l > r)
return 0;
if (l == 0)
return ar[r];
else
return ar[r] - ar[l - 1];
}
}
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 | ["abba", "bab"] | 2 seconds | ["4", "2"] | NoteIt the first sample the string is already beautiful.In the second sample he needs to delete one of "b" to make it beautiful. | Java 8 | standard input | [
"dp",
"brute force"
] | c768f3e52e562ae1fd47502a60dbadfe | The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b". | 1,500 | Print a single integer — the maximum possible size of beautiful string Nikita can get. | standard output | |
PASSED | 94b63ca1c9da8d741d0a0d599460b434 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.*;
import java.util.*;
public class ORInMatrix {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
int m = in.nextInt();
int n = in.nextInt();
int[][] B = new int[m][n];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
B[i][j] = in.nextInt();
int[][] A = new int[m][n];
boolean valid = true;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
if (B[i][j] == 1) {
boolean validRow = true;
for (int k = 0; k < m; ++k) {
if (B[k][j] != 1)
validRow = false;
}
boolean validCol = true;
for (int k = 0; k < n; ++k) {
if (B[i][k] != 1)
validCol = false;
}
valid &= validRow || validCol;
if (validRow && validCol)
A[i][j] = 1;
}
}
boolean allZerosA = true;
boolean allZerosB = true;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
if (A[i][j] == 1)
allZerosA = false;
if (B[i][j] == 1)
allZerosB = false;
}
if (valid && allZerosA && !allZerosB)
valid = false;
System.out.println(valid ? "YES" : "NO");
if (valid) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (j > 0)
System.out.print(" ");
System.out.print(A[i][j]);
}
System.out.println();
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = new StringTokenizer("");
}
public String next() {
try {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = new StringTokenizer("");
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) return false;
tokenizer = new StringTokenizer(line);
}
return true;
}
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | c8d56fc400f3bec2494bd5bef7ce1d89 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException{
ContestScanner in = new ContestScanner();
int m = in.nextInt();
int n = in.nextInt();
int[][] a = new int[m][n];
int[] aline = new int[n];
int[] acol = new int[m];
long one = 0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
int val = in.nextInt();
acol[i] += val;
aline[j]+= val;
one += val;
}
}
int line1num = 0;
int col1num = 0;
for(int i=0; i<m; i++){
if(acol[i] == n) line1num++;
}
for(int i=0; i<n; i++){
if(aline[i] == m) col1num++;
}
for(int i=0; i<m; i++){
if(acol[i] != n && acol[i] != col1num){
System.out.println("NO");
return;
}
}
for(int i=0; i<n; i++){
if(aline[i] != m && aline[i] != line1num){
System.out.println("NO");
return;
}
}
if(one > 0 && (line1num == 0 || col1num == 0)){
System.out.println("NO");
return;
}
System.out.println("YES");
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(acol[i] == n && aline[j] == m){
System.out.print(1);
a[i][j] = 1;
}else{
System.out.print(0);
a[i][j] = 0;
}
if(j != n-1) System.out.print(" ");
}
System.out.println();
}
}
}
class MyMath{
public static long fact(long n){
long res = 1;
while(n > 0){
res *= n--;
}
return res;
}
public static long[][] pascalT(int n){
long[][] tri = new long[n][];
for(int i=0; i<n; i++){
tri[i] = new long[i+1];
for(int j=0; j<i+1; j++){
if(j == 0 || j == i){
tri[i][j] = 1;
}else{
tri[i][j] = tri[i-1][j-1] + tri[i-1][j];
}
}
}
return tri;
}
// public static long combi(int n, int k){
// if(k > n/2){
// k = n - k;
// }
// long res = 1;
// for(int i=0; i<k; i++){
// res *= n--;
// }
// while(k > 1){
// res /= k--;
// }
// return res;
// }
//
// public static long combiBig(int n, int k){
// if(k > n/2){
// k = n - k;
// }
// int[] a = new int[k];
// int subK = k;
// for(int i=0; i<k; i++){
// a[i] = n--;
// }
// while(k > 1){
// int oldK = k;
// for(int i=0; i<subK; i++){
// if(a[i]%k == 0){
// a[i] /= k--;
// break;
// }
// }
// if(oldK == k){
// int oldK2 = k;
// for(int i=0; i<subK; i++){
// if(oldK%a[i] == 0){
// oldK /= a[i];
// a[i] = 1;
// if(oldK == 1){
// k--;
// break;
// }
// }
// }
// if(oldK2 == k){
// // ���̂�����̓f�o�b�O�s���̂��߁A�������o�͂��o��ۏ��Ȃ�
// k--;
// long res = 1;
// for(int i=0; i<subK; i++){
// res *= a[i];
// }
// res /= oldK;
// while(k > 1){
// res /= k--;
// }
// return res;
// }
// }
// }
// long res = 1;
// for(int i=0; i<subK; i++){
// res *= a[i];
// }
// return res;
// }
}
class MyCompA implements Comparator<int[]>{
public int compare(int[] arg0, int[] arg1) {
return arg0[0] - arg1[0];
}
}
class MyCompB implements Comparator<int[]>{
public int compare(int[] arg0, int[] arg1) {
return arg0[1] - arg1[1];
}
}
class ContestScanner{
BufferedReader reader;
String[] line;
int idx;
public ContestScanner() throws FileNotFoundException{
reader = new BufferedReader(new InputStreamReader(System.in));
}
public String nextToken() throws IOException{
if(line == null || line.length <= idx){
line = reader.readLine().trim().split(" ");
idx = 0;
}
return line[idx++];
}
public long nextLong() throws IOException, NumberFormatException{
return Long.parseLong(nextToken());
}
public int nextInt() throws NumberFormatException, IOException{
return (int)nextLong();
}
public double nextDouble() throws NumberFormatException, IOException{
return Double.parseDouble(nextToken());
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 69e79f6d8e9d1774058d1a890b13e759 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.InputMismatchException;
public class OrInMatrix {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int m = in.readInt();
int n = in.readInt();
int b[][] = new int[m][n];
int a[][] = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = in.readInt();
a[i][j] = 1;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (b[i][j] == 0) {
for (int k = 0; k < n; k++) {
a[i][k] = 0;
}
for (int x = 0; x < m; x++) {
a[x][j] = 0;
}
}
}
}
boolean fail = false;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int val = 0;
for (int k = 0; k < n; k++) {
val |= a[i][k];
}
for (int x = 0; x < m; x++) {
val |= a[x][j];
}
if (b[i][j] != val) {
fail = true;
break;
}
}
}
if (!fail) {
out.print("YES");
out.printLine();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
out.print(a[i][j] + " ");
}
out.printLine();
}
} else {
out.print("NO");
}
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String readLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String 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 long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
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 printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | e360bc03933e189e3b779ce0aaa6a5f5 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class ORinMatrix
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int m = in.nextInt(), n = in.nextInt();
int[][] a = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = in.nextInt();
int[][] b = new int[m][n];
for (int i = 0; i < m; i++)
Arrays.fill(b[i], 1);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (a[i][j] == 0)
{
for (int k = 0; k < n; k++)
b[i][k] = 0;
for (int k = 0; k < m; k++)
b[k][j] = 0;
}
boolean ok = true;
for(int i = 0 ; i < m ; i++)
for(int j =0 ; j < n ; j++)
if(a[i][j] == 1){
boolean kk = false;
for(int k = 0 ; k < n ; k++)
kk |= b[i][k] == 1;
for(int k = 0 ; k < m ; k++)
kk |= b[k][j] == 1;
ok &= kk;
}
if(!ok)
System.out.println("NO");
else{
System.out.println("YES");
for(int i = 0 ;i < m ; i++){
for(int j = 0 ; j < n ; j++)
System.out.print(b[i][j] + " ");
System.out.println();
}
}
in.close();
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | dcc9220852f8a991b5ec2565e5c7508d | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B486 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int col[] = new int[n];
int row[] = new int[m];
Arrays.fill(col, 1);
Arrays.fill(row, 1);
int B[][] = new int[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int temp = in.nextInt();
if (temp == 0) {
row[i] = 0;
col[j] = 0;
}
B[i][j] = temp;
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (B[i][j] != (col[j] | row[i])) {
System.out.println("NO");
return;
}
}
}
int[][] A = new int[m][n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
A[i][j] = 1;
}
}
for (int i = 0; i < m; ++i) {
if (row[i] == 0) {
for (int j = 0; j < n; ++j) {
A[i][j] = 0;
}
}
}
for (int i = 0; i < n; ++i) {
if (col[i] == 0) {
for (int j = 0; j < m; ++j) {
A[j][i] = 0;
}
}
}
int colTemp[] = new int[n];
int rowTemp[] = new int[m];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
colTemp[j] |= A[i][j];
rowTemp[i] |= A[i][j];
}
}
for (int i = 0; i < m; ++i) {
if (rowTemp[i] != row[i]) {
System.out.println("NO");
return;
}
}
for (int j = 0; j < n; ++j) {
if (col[j] != colTemp[j]) {
System.out.println("NO");
return;
}
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
stringBuilder.append(A[i][j]);
if (j != n - 1) {
stringBuilder.append(" ");
}
}
stringBuilder.append("\n");
}
System.out.println("YES");
System.out.print(stringBuilder);
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | d46851dea32b484f8b345d255eaaf755 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String args[]) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
SolverB.InputReader in = new SolverB.InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
SolverB solver = new SolverB();
int testCount = 1;
for (int i = 1; i <= testCount; i++) {
solver.solve(in, out);
}
out.close();
}
}
class SolverB {
public void solve(InputReader in, PrintWriter out) {
int m = in.nextInt();
int n = in.nextInt();
int a[][] = new int[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
a[i][j] = in.nextInt();
}
}
int b[][] = new int[m][n];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
b[i][j] = 1;
}
}
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (a[i][j] == 0) {
for (int k=0; k<n; k++) {
b[i][k] = 0;
}
for (int k=0; k<m; k++) {
b[k][j] = 0;
}
}
}
}
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (a[i][j] == 1) {
boolean ok = false;
for (int k=0; k<n; k++) {
if (b[i][k] == 1) {
ok = true;
break;
}
}
if (!ok) {
for (int k = 0; k < m; k++) {
if (b[k][j] == 1) {
ok = true;
break;
}
}
}
if (!ok) {
out.println("NO");
return;
}
}
}
}
out.println("YES");
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
out.print(b[i][j] + " ");
}
out.println();
}
}
static class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | e508e84f80075034d32c653ed1dd14d4 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.*;
import java.util.*;
public class Task {
public static void main(String[] args) throws IOException {
new Task().solve();
}
PrintWriter out;
int cnt = 0;
int n;
boolean[] used;
int[] use;
ArrayList<Integer>[] rg;
ArrayList<Integer> tsort = new ArrayList<Integer>();
int[] comp;
int[] compsize;
int[] mt;
int[] a;
int k;
boolean[][] g;
int dp[];
int m;
int[] list;
void solve() throws IOException{
Reader in = new Reader("output.txt");
out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) );
//out = new PrintWriter(new FileWriter(new File("output.txt")));
int n = in.nextInt();
int m = in.nextInt();
int[][] a = new int[n][m];
int[] cols = new int[n];
int[] rows = new int[m];
Arrays.fill(cols, 1);
Arrays.fill(rows, 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
a[i][j] = in.nextInt();
cols[i] &= a[i][j];
rows[j] &= a[i][j];
}
int[][] b = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
b[i][j] = 1;
for (int i = 0; i < n; i++)
if (cols[i] == 0)
for (int j = 0; j < m; j++) {
b[i][j] = 0;
}
for (int i = 0; i < m; i++)
if (rows[i] == 0)
for (int j = 0; j < n; j++) {
b[j][i] = 0;
//cols[j] = 0;
}
Arrays.fill(cols, 0);
Arrays.fill(rows, 0);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cols[i] |= b[i][j];
rows[j] |= b[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 1 && (rows[j] == 0 && cols[i] == 0)) {
System.out.println("NO");
return;
}
}
}
out.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
out.print(b[i][j]+" ");
out.println();
}
out.flush();
out.close();
}
long pow(int x, int m) {
if (m == 0)
return 1;
return x*pow(x, m-1);
}
}
class Pair implements Comparable<Pair> {
int v;
int i;
Pair (int v, int i) {
this.v = v;
this.i = i;
}
@Override
public int compareTo(Pair p) {
if (v > p.v)
return 1;
if (v < p.v)
return -1;
return 0;
}
}
class Item implements Comparable<Item> {
int v;
int l;
int z;
Item(int v, int l, int z) {
this.v = v;
this.l = l;
this.z = z;
}
@Override
public int compareTo(Item item) {
if (l > item.l)
return 1;
else
if (l < item.l)
return -1;
else {
if (z > item.z)
return 1;
else
if (z < item.z)
return -1;
return 0;
}
}
}
class Reader {
StringTokenizer token;
BufferedReader in;
public Reader(String file) throws IOException {
//in = new BufferedReader( new FileReader( file ) );
in = new BufferedReader( new InputStreamReader( System.in ) );
}
public byte nextByte() throws IOException {
return Byte.parseByte(Next());
}
public int nextInt() throws IOException {
return Integer.parseInt(Next());
}
public long nextLong() throws IOException {
return Long.parseLong(Next());
}
public String nextString() throws IOException {
return in.readLine();
}
public String Next() throws IOException {
while (token == null || !token.hasMoreTokens()) {
token = new StringTokenizer(in.readLine());
}
return token.nextToken();
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | befd2762b49f5e052bf2e7b7fd259ca6 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class B {
MyScanner sc = new MyScanner();
Scanner sc2 = new Scanner(System.in);
long start = System.currentTimeMillis();
long fin = System.currentTimeMillis();
final int MOD = 1000000007;
int[] dx = { 1, 0, 0, -1 };
int[] dy = { 0, 1, -1, 0 };
void run() {
int m = sc.nextInt();
int n = sc.nextInt();
int[][] B = sc.nextInt2dArray(m, n);
int[][] A = new int[m][n];
for (int i = 0; i < m; i++)
Arrays.fill(A[i], 1);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (B[i][j] == 0) {
for (int k = 0; k < n; k++) {
A[i][k] = 0;
}
for (int k = 0; k < m; k++) {
A[k][j] = 0;
}
}
}
}
int[][] makeB = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (A[i][j] == 1) {
for (int k = 0; k < n; k++) {
makeB[i][k] = 1;
}
for (int k = 0; k < m; k++) {
makeB[k][j] = 1;
}
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (makeB[i][j] != B[i][j]) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(A[i][j] +" ");
}
System.out.println();
}
}
public static void main(String[] args) {
new B().run();
}
void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void debug2(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
boolean inner(int h, int w, int limH, int limW) {
return 0 <= h && h < limH && 0 <= w && w < limW;
}
void swap(int[] x, int a, int b) {
int tmp = x[a];
x[a] = x[b];
x[b] = tmp;
}
// find minimum i (a[i] >= border)
int lower_bound(int a[], int border) {
int l = -1;
int r = a.length;
while (r - l > 1) {
int mid = (l + r) / 2;
if (border <= a[mid]) {
r = mid;
} else {
l = mid;
}
}
// r = l + 1
return r;
}
boolean palindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
class MyScanner {
int nextInt() {
try {
int c = System.in.read();
while (c != '-' && (c < '0' || '9' < c))
c = System.in.read();
if (c == '-')
return -nextInt();
int res = 0;
do {
res *= 10;
res += c - '0';
c = System.in.read();
} while ('0' <= c && c <= '9');
return res;
} catch (Exception e) {
return -1;
}
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String next() {
try {
StringBuilder res = new StringBuilder("");
int c = System.in.read();
while (Character.isWhitespace(c))
c = System.in.read();
do {
res.append((char) c);
} while (!Character.isWhitespace(c = System.in.read()));
return res.toString();
} catch (Exception e) {
return null;
}
}
int[] nextIntArray(int n) {
int[] in = new int[n];
for (int i = 0; i < n; i++) {
in[i] = nextInt();
}
return in;
}
int[][] nextInt2dArray(int n, int m) {
int[][] in = new int[n][m];
for (int i = 0; i < n; i++) {
in[i] = nextIntArray(m);
}
return in;
}
double[] nextDoubleArray(int n) {
double[] in = new double[n];
for (int i = 0; i < n; i++) {
in[i] = nextDouble();
}
return in;
}
long[] nextLongArray(int n) {
long[] in = new long[n];
for (int i = 0; i < n; i++) {
in[i] = nextLong();
}
return in;
}
char[][] nextCharField(int n, int m) {
char[][] in = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
in[i][j] = s.charAt(j);
}
}
return in;
}
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 411973ea89082d278836748359e65302 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class ORMatrix {
public static void main(String[] Args) throws IOException{
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out= new BufferedWriter(new OutputStreamWriter(System.out));
String[] val = in.readLine().split(" ");
int M=Integer.parseInt(val[0]);
int N=Integer.parseInt(val[1]);
boolean[][] OrM=new boolean [M][N];
boolean[][] AndM=new boolean [M][N];
for(int m=0;m<M;++m){
for(int n=0;n<N;++n){
AndM[m][n]=true;
}
}
for(int m=0;m<M;++m){
String[] line = in.readLine().split(" ");
for(int n=0;n<N;++n){
OrM[m][n]=(Integer.parseInt(line[n])==1);
for(int m2=0;m2<M;++m2){
AndM[m2][n]&=OrM[m][n];
}
for(int n2=0;n2<N;++n2){
AndM[m][n2]&=OrM[m][n];
}
}
}
for(int m=0;m<M;++m){
for(int n=0;n<N;++n){
boolean f=false;
for(int m2=0;m2<M;++m2){
f|=AndM[m2][n];
}
for(int n2=0;n2<N;++n2){
f|=AndM[m][n2];
}
if(OrM[m][n]!=f){
out.write("NO");
out.newLine();
out.close();
return;
}
}
}
out.write("YES");
out.newLine();
for(int m=0;m<M;++m){
for(int n=0;n<N;++n){
out.write((AndM[m][n])?"1":"0");
out.write(" ");
}
out.newLine();
}
out.close();
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 3d65f43f8ab992bb6fb2e630b396653a | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
import java.util.concurrent.*;
public class P486B {
@SuppressWarnings("unchecked")
public void run() throws Exception {
int n = nextInt();
int m = nextInt();
int [][] a = new int [n][];
for (int i = 0; i < n; i++) {
a[i] = readInt(m);
}
int [][] b = new int [n][m];
for (int y = 0; y < n; y++) {
lab: for (int x = 0; x < m; x++) {
for (int i = 0; i < n; i++) if (a[i][x] == 0) { continue lab; }
for (int i = 0; i < m; i++) if (a[y][i] == 0) { continue lab; }
b[y][x] = 1;
}
}
int [][] c = new int [n][m];
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (b[y][x] == 1) {
for (int i = 0; i < n; i++) c[i][x] = 1;
for (int i = 0; i < m; i++) c[y][i] = 1;
}
}
}
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (a[y][x] != c[y][x]) {
println("NO");
return;
}
}
}
println("YES");
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
print(b[y][x] + " ");
}
println();
}
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new P486B().run();
br.close();
pw.close();
}
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) { return null; }
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) { print("" + b); }
void print(int i) { print("" + i); }
void print(long l) { print("" + l); }
void print(double d) { print("" + d); }
void print(char c) { print("" + c); }
void print(Object o) {
if (o instanceof int[]) { print(Arrays.toString((int [])o));
} else if (o instanceof long[]) { print(Arrays.toString((long [])o));
} else if (o instanceof char[]) { print(Arrays.toString((char [])o));
} else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
} else if (o instanceof short[]) { print(Arrays.toString((short [])o));
} else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
} else if (o instanceof float[]) { print(Arrays.toString((float [])o));
} else if (o instanceof double[]) { print(Arrays.toString((double [])o));
} else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
} else { println("" + o); }
}
void print(String s) { pw.print(s); }
void println() { println(""); }
void println(byte b) { println("" + b); }
void println(int i) { println("" + i); }
void println(long l) { println("" + l); }
void println(double d) { println("" + d); }
void println(char c) { println("" + c); }
void println(Object o) { print("" + o); }
void println(String s) { pw.println(s); }
int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
long nextLong() throws IOException { return Long.parseLong(nextToken()); }
double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
char nextChar() throws IOException { return (char) (br.read()); }
String next() throws IOException { return nextToken(); }
String nextLine() throws IOException { return br.readLine(); }
int [] readInt(int size) throws IOException {
int [] array = new int [size];
for (int i = 0; i < size; i++) { array[i] = nextInt(); }
return array;
}
long [] readLong(int size) throws IOException {
long [] array = new long [size];
for (int i = 0; i < size; i++) { array[i] = nextLong(); }
return array;
}
double [] readDouble(int size) throws IOException {
double [] array = new double [size];
for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
return array;
}
String [] readLines(int size) throws IOException {
String [] array = new String [size];
for (int i = 0; i < size; i++) { array[i] = nextLine(); }
return array;
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 452a9f5614ea0b9ef2bd950d25b272a2 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] in = br.readLine().split(" ");
int m = Integer.parseInt(in[0]);
int n = Integer.parseInt(in[1]);
int[][] input = new int[m][n];
int[][] output = new int[m][n];
int[][] tempInput = new int[m][n];
for(int i = 0; i < m; i++) {
in = br.readLine().split(" ");
for(int j = 0; j < n; j++) {
input[i][j] = Integer.parseInt(in[j]);
output[i][j] = 1;
}
}
/*for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
boolean flag1 = false;
boolean flag2 = false;
if(input[i][j] == 1) {
for(int a = 0; a < m; a++) {
if(a != i && input[a][j] == 1) {
flag1 = true;
break;
}
}
for(int a = 0; a < n; a++) {
if(a != j && input[i][a] == 1) {
flag2 = true;
break;
}
}
if(!flag1 && !flag2) {
System.out.println("NO");
return;
}
}
}
}*/
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(input[i][j] == 0) {
for(int a = 0; a < m; a++) {
output[a][j] = 0;
}
for(int a = 0; a < n; a++) {
output[i][a] = 0;
}
}
}
}
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(output[i][j] == 1) {
for(int a = 0; a < m; a++) {
tempInput[a][j] = 1;
}
for(int a = 0; a < n; a++) {
tempInput[i][a] = 1;
}
}
}
}
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(input[i][j] != tempInput[i][j]) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
System.out.print(output[i][j]);
if(j < n - 1)
System.out.print(" ");
}
System.out.println();
}
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 2f88f05477a26d0b32b8b6d43c13e579 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
public class SolB {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskSolver solver = new TaskSolver();
solver.solve(1, in, out);
out.close();
}
}
class TaskSolver {
int m;
int n;
int[][] mtx;
boolean[] row;
boolean[] col;
int i,j;
public void solve(int testNumber, InputReader in, OutputWriter out) {
m = in.nextInt();
n = in.nextInt();
mtx = new int[m][n];
row = new boolean[m];
col = new boolean[n];
for (i = 0; i < m; ++i) {
row[i] = true;
for (j = 0; j < n; ++j) {
mtx[i][j] = in.nextInt();
if(mtx[i][j] == 0)
row[i] = false;
}
}
for (j = 0; j < n; ++j) {
col[j] = true;
for (i = 0; i < m; ++i) {
if(mtx[i][j] == 0)
col[j] = false;
}
}
int[][] outMtx = new int[m][n];
boolean nonZero = false;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
//check 4 directions
if(getVal(i,j) == 1){
nonZero = true;
if(row[i] || col[j]){
if(row[i] && col[j]){
outMtx[i][j] = 1;
}
}else{
out.println("NO");
return;
}
}
}
}
boolean hasRow = false;
boolean hasCol = false;
if(nonZero){
for (i = 0; i < m; ++i) {
if(row[i]){
hasRow = true;
break;
}
}
for (i = 0; i < n; ++i) {
if(col[i]){
hasCol = true;
break;
}
}
if(!hasRow || !hasCol){
out.println("NO");
return;
}
}
out.println("YES");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
out.print(outMtx[i][j] + " ");
}
out.println();
}
}
private int getVal(int i, int j){
if(i < 0 || j < 0 || i >= m || j >= n)
return 1;
return mtx[i][j];
}
}
/*
int in.nextInt()
String in.nextString()
out.print(n + " ") //must be " " which is a String object.
out.close()
*/
class InputReader {
InputStream stream;
byte[] buf = new byte[1024];
int curChar, numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() {
if (numChars == -1)
throw new InputMismatchException();
//buffer wraps around here. Hence the buf array size does not matter.
/*
if the buffer size is enough, i.e 1024. InputStream load at most 1024 bytes(if necessary)
into buffer. read() returns 1 byte every time. After the first read, it only needs to
return corresponding byte in the buf every time it is called. it does not have to call
input stream to read again.
Only if the bug has run out does InputReader has to call stream.read(buf) to reload the
buf.
*/
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();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
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);
print("\n");
}
public void close() {
writer.close();
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 76d41e7dc118c2ac3385e2f5bb9ad4e3 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.*;
public class ORinMatrix {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int A1[][] = new int[n][m];
int A2[][] = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
A1[i][j] = in.nextInt();
A2[i][j] = 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (A1[i][j] == 0) {
for (int k = 0; k < n; k++)
A2[k][j] = 0;
for (int k = 0; k < m; k++)
A2[i][k] = 0;
}
}
}
boolean test = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (A1[i][j] == 1) {
int sum = 0;
for (int k = 0; k < n; k++)
sum += A2[k][j];
for (int k = 0; k < m; k++)
sum += A2[i][k];
if (sum == 0)
test = false;
}
}
}
if (test) {
System.out.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
System.out.print(A2[i][j] + " ");
System.out.println();
}
} else {
System.out.println("NO");
}
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | da5a901c8206e2a12fd3a25dc9407c9b | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Rafael Regis
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
FastWriter out = new FastWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, FastReader in, FastWriter out) {
int m = in.nextInt();
int n = in.nextInt();
int[][] b = new int[m][];
int[][] a = new int[m][];
for (int i = 0; i < m; i++) {
b[i] = in.nextIntArray(n);
a[i] = new int[n];
for (int j = 0; j < n; j++) {
a[i][j] = 1;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (b[i][j] == 0) {
for (int k = 0; k < m; k++) {
a[k][j] = 0;
}
for (int k = 0; k < n; k++) {
a[i][k] = 0;
}
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
boolean one = false;
for (int k = 0; k < m; k++) {
one |= a[k][j] == 1;
}
for (int k = 0; k < n; k++) {
one |= a[i][k] == 1;
}
if (!(one && b[i][j] == 1 || !one && b[i][j] == 0)) {
out.println("NO");
return;
}
}
}
out.println("YES");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
out.print(a[i][j] + " ");
}
out.println();
}
}
}
class FastReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public FastReader(InputStream stream) {
this.reader = new BufferedReader(new InputStreamReader(stream));
this.tokenizer = new StringTokenizer("");
}
public String next() {
return ensureTokens() ? tokenizer.nextToken() : null;
}
private boolean ensureTokens() {
while(!tokenizer.hasMoreTokens()){
try{
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e){
return false;
}
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
}
class FastWriter {
private final PrintWriter printWriter;
public FastWriter(OutputStream stream) {
printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
}
public void close() {
printWriter.close();
}
public void print(String element) {
printWriter.print(element);
}
public void println(String element) {
printWriter.println(element);
}
public void println() {
printWriter.println();
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | f4471960d51dc192956f820416dde1fa | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.*;
public class Main {
static int n,m;
static int[][] a,ans;
static boolean[] row,col;
public static void main(String[] args){
Scanner cin=new Scanner(System.in);
n=cin.nextInt(); m=cin.nextInt();
row=new boolean[n]; for (int i=0;i<n;i++) row[i]=true;
col=new boolean[m]; for (int i=0;i<m;i++) col[i]=true;
a=new int[n][m];
ans=new int[n][m];
boolean error=false;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
a[i][j]=cin.nextInt();
if (a[i][j]==0){
row[i]=false; col[j]=false;
}
}
}
paint();
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (a[i][j]==1){
if (!found(i,j)){
error=true;
i=n+1; j=m+1; break;
}
}
}
}
if (error){
System.out.println("NO");
}else{
System.out.println("YES");
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
System.out.print(ans[i][j]+" ");
}
System.out.println();
}
}
cin.close();
}
public static void paint(){
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (!row[i]||!col[j]) ans[i][j]=0;
else ans[i][j]=1;
}
}
}
public static boolean found(int x,int y){
for (int i=0;i<n;i++){
if (ans[i][y]==1) return true;
}
for (int i=0;i<m;i++){
if (ans[x][i]==1) return true;
}
return false;
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 8bed13275eb31605975154066aaada66 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve()
{
int m = ni();
int n = ni();
int[][] b=new int[m][n];
int[][] a=new int[m][n];
for(int i=0;i<m;i++)
{
b[i] = na(n);
}
int row[] = new int[m];
int col[] = new int[n];
int rowc[] = new int[m];
int colc[] = new int[n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
row[i]+=b[i][j];
}
}
for(int j=0;j<n;j++)
{
for(int i=0;i<m;i++)
{
col[j]+=b[i][j];
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]= (row[i]==n && col[j]==m)?1:0;
if(a[i][j]==1)
{
rowc[i]=1;
colc[j]=1;
}
}
}
boolean ans = true;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(b[i][j]==1)
{
if(rowc[i]==1 || colc[j]==1)
{
continue;
}
ans = false;
break;
}
else
{
if(rowc[i]==0 && colc[j]==0)
{
continue;
}
ans = false;
break;
}
}
if(!ans)
break;
}
if(ans==false)
{
out.println("NO");
}
else
{
out.println("YES");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(j!=n-1)
out.print(a[i][j]+" ");
else
out.print(a[i][j]);
}
if(i!=m-1)
out.println();
}
}
}
private static long pow(long base, long exp, long m) {
long result = 1%m;
while(exp!=0)
{
if((exp&1L)==1)
{
result = (result*base)%m;
}
base = (base*base)%m;
exp = exp>>1L;
//if(result+base == 0)
// return 0;
}
return result;
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1 || b=='\n'){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static 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 static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double nd() { return Double.parseDouble(ns()); }
private static char nc() { return (char)skip(); }
private static String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!isSpaceChar(b)){//(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static 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 static 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 static int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private static char[] nac(int n)
{
char[] a = new char[n];
for(int i = 0;i < n;i++)a[i] = nc();
return a;
}
private static long[] nal(int n)
{
long[] a = new long[n];
for(int i = 0;i < n;i++)a[i] = nl();
return a;
}
private static 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 static 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 static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 40cdb34aea43408c2835621c7c25f4fd | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes |
import java.io.*;
import java.io.FileReader;
import java.util.Scanner;
public class Main
{
public static int a[][],row,clm,flag;
public static int b[][];
public static boolean dd;
public static int row_visit(int r)
{
for(int i=1;i<=clm;i++)
{
if(b[r][i]==0)return 0;
}
return 1;
}
public static int clm_visit(int c)
{
for(int i=1;i<=row;i++)
{
if(b[i][c]==0)return 0;
}
return 1;
}
public static void main(String[] args) {
a=new int[150][150];
b=new int[150][150];
Scanner in=new Scanner(System.in);
row=in.nextInt();
clm=in.nextInt();
//System.out.printf("%d %d\n",row,clm);
for(int i=1;i<=row;i++)
{
for(int j=1;j<=clm;j++)
{
b[i][j]=in.nextInt();
// System.out.printf("%d ", b[i][j]);
}
//System.out.printf("\n");
}
dd=true;
flag=1;
for(int i=1;((i<=row)&&dd);i++)
{
for(int j=1;((j<=clm)&&dd);j++)
{
dd=true;
if(b[i][j]==1)
{
if(flag==1)flag=0;
if((row_visit(i)==1)&&(clm_visit(j)==1))
{
a[i][j]=1; flag=3;
}
else if((row_visit(i)==0)&&(clm_visit(j)==0))
{
dd=false;
}
else a[i][j]=0;
}
}
}
if(dd==false||flag==0)
{
System.out.printf("NO\n");
}
else
{
System.out.printf("YES\n");
for(int i=1;i<=row;i++)
{
for(int j=1;j<=clm;j++)
{
System.out.printf("%d ",a[i][j]);
}
System.out.printf("\n");
}
}
in.close();
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 49c5d1e9ca64f67901f24857b08f5945 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes |
//package calculating_function;
import java.io.*;
import java.io.FileReader;
import java.util.Scanner;
public class Main {
public static int a[][],row,clm,flag;
public static int b[][];
public static boolean dd;
public static int row_visit(int r)
{
for(int i=1;i<=clm;i++)
{
if(b[r][i]==0)return 0;
}
return 1;
}
public static int clm_visit(int c)
{
for(int i=1;i<=row;i++)
{
if(b[i][c]==0)return 0;
}
return 1;
}
public static void main(String[] args) {
a=new int[150][150];
b=new int[150][150];
Scanner in=new Scanner(System.in);
row=in.nextInt();
clm=in.nextInt();
// System.out.printf("%d %d\n",row,clm);
for(int i=1;i<=row;i++)
{
for(int j=1;j<=clm;j++)
{
b[i][j]=in.nextInt();
// System.out.printf("%d ", b[i][j]);
}
//System.out.printf("\n");
}
dd=true;
flag=1;
for(int i=1;((i<=row)&&dd);i++)
{
for(int j=1;((j<=clm)&&dd);j++)
{
dd=true;
if(b[i][j]==1)
{
if(flag==1)flag=0;
if((row_visit(i)==1)&&(clm_visit(j)==1))
{
a[i][j]=1; flag=3;
}
else if((row_visit(i)==0)&&(clm_visit(j)==0))
{
dd=false;
}
else a[i][j]=0;
}
}
}
if(dd==false||flag==0)
{
System.out.printf("NO\n");
}
else
{
System.out.printf("YES\n");
for(int i=1;i<=row;i++)
{
for(int j=1;j<=clm;j++)
{
System.out.printf("%d ",a[i][j]);
}
System.out.printf("\n");
}
}
in.close();
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | b6c0f59f245c44d8a3be7ccc67f498c1 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes |
import java.io.*;
import java.io.FileReader;
import java.util.Scanner;
public class Main
{
public static int a[][],row,clm,flag;
public static int b[][];
public static boolean dd;
public static int row_visit(int r)
{
for(int i=1;i<=clm;i++)
{
if(b[r][i]==0)return 0;
}
return 1;
}
public static int clm_visit(int c)
{
for(int i=1;i<=row;i++)
{
if(b[i][c]==0)return 0;
}
return 1;
}
public static void main(String[] args) {
a=new int[150][150];
b=new int[150][150];
Scanner in=new Scanner(System.in);
row=in.nextInt();
clm=in.nextInt();
//System.out.printf("%d %d\n",row,clm);
for(int i=1;i<=row;i++)
{
for(int j=1;j<=clm;j++)
{
b[i][j]=in.nextInt();
// System.out.printf("%d ", b[i][j]);
}
//System.out.printf("\n");
}
dd=true;
flag=1;
for(int i=1;((i<=row)&&dd);i++)
{
for(int j=1;((j<=clm)&&dd);j++)
{
dd=true;
if(b[i][j]==1)
{
if(flag==1)flag=0;
if((row_visit(i)==1)&&(clm_visit(j)==1))
{
a[i][j]=1; flag=3;
}
else if((row_visit(i)==0)&&(clm_visit(j)==0))
{
dd=false;
}
else a[i][j]=0;
}
}
}
if(dd==false||flag==0)
{
System.out.printf("NO\n");
}
else
{
System.out.printf("YES\n");
for(int i=1;i<=row;i++)
{
for(int j=1;j<=clm;j++)
{
System.out.printf("%d ",a[i][j]);
}
System.out.printf("\n");
}
}
in.close();
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 9d015cdca41bd82903255cc856ddd1cf | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int a[][] = new int[m+1][n+1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
a[i][j] = sc.nextInt();
}
}
int b[][]=new int[m+1][n+1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
b[i][j] = 1;
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if(a[i][j]==0){
for(int k=1;k<=n;k++)
b[i][k]=0;
for(int k=1;k<=m;k++)
b[k][j]=0;
}
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if(a[i][j]==1){
int sum=0;
for(int k=1;k<=n;k++)
sum+= b[i][k];
for(int k=1;k<=m;k++)
sum+= b[k][j];
if(sum==0){
System.out.println("NO");
return;
}
}
}
}
System.out.println("YES");
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | b72b93a3cfd16f54e23138e670864874 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.*;
import java.lang.reflect.Array;
import java.math.*;
import java.io.*;
import java.awt.*;
public class Main{
static FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
static final int maxn = (int)1e2 + 11;
static int inf = (1<<30) - 1;
static long n,m;
static int a[][] = new int [maxn][maxn];
static int b[][] = new int [maxn][maxn];
static int c[][] = new int [maxn][maxn];
static int dr[] = new int [maxn];
static int dc[] = new int [maxn];
public static void solve() throws Exception {
n = in.nextLong();
m = in.nextLong();
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
a[i][j] = in.nextInt();
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
dr[i] += a[i][j];
}
}
for (int i=1; i<=m; i++) {
for (int j=1; j<=n; j++) {
dc[i] += a[j][i];
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (dr[i]+dc[j]-1>=n+m-1) {
b[i][j] = 1;
} else {
b[i][j] = 0;
}
}
}
Arrays.fill(dr, 0);
Arrays.fill(dc, 0);
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
dr[i] += b[i][j];
}
}
for (int i=1; i<=m; i++) {
for (int j=1; j<=n; j++) {
dc[i] += b[j][i];
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (dr[i]+dc[j]>0) {
c[i][j] = 1;
} else {
c[i][j] = 0;
}
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (a[i][j]!=c[i][j]) {
out.println("NO");
return;
}
}
}
out.println("YES");
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
out.print(b[i][j] + " ");
}
out.println();
}
}
public static void main(String[] args) throws Exception {
solve();
out.close();
}
}
class Pair implements Comparable<Pair> {
int x,y;
public Pair (int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair p) {
if (this.x<p.x) {
return -1;
} else if (this.x == p.x) {
return 0;
} else {
return 1;
}
}
}
class FastReader {
BufferedReader bf;
StringTokenizer tk = null;
String DELIM = " ";
public FastReader(BufferedReader bf) {
this.bf = bf;
}
public String nextToken() throws Exception {
if(tk==null || !tk.hasMoreTokens()) {
tk = new StringTokenizer(bf.readLine(),DELIM);
}
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 | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 08011e31e3e78248a5bab19885c57916 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class OrMatrix {
public static void printMatrix(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
public static boolean valid(int[] a) {
for (int i = 0; i < a.length; i++)
if (a[i] != 1) return false;
return true;
}
public static boolean validCell(int u, int v, int[][] a) {
boolean validCol = true, validRow = true;
for (int i = 0; i < a.length; i++)
if (a[i][v] == 0) validCol = false;
for (int j = 0; j < a[0].length; j++)
if (a[u][j] == 0) validRow = false;
return (validRow || validCol);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int[][] b = new int[m][n];
int[][] a = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
b[i][j] = in.nextInt();
a[i][j] = 0;
}
int[] row = new int[m];
int[] col = new int[n];
Arrays.fill(row, 0);
Arrays.fill(col, 0);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (b[i][j] == 1)
if ( !validCell(i, j, b)) {
System.out.println("NO");
return;
}
for (int i = 0; i < m; i++)
if (b[i][0] == 1)
if (valid(b[i])) {
row[i] = 1;
}
for (int j = 0; j < n; j++) {
int[] colj = new int[m];
for (int i = 0; i < m; i++) colj[i] = b[i][j];
if (colj[0] == 1)
if (valid(colj)) {
col[j] = 1;
}
}
for (int i = 0; i < m; i++)
if (row[i] == 1) {
boolean flag = false;
for (int j = 0; j < n; j++)
if (col[j] == 1) {
a[i][j] = 1;
flag = true;
}
if (flag == false) {
System.out.println("NO");
return;
}
}
for (int j = 0; j < n; j++)
if (col[j] == 1) {
boolean flag = false;
for (int i = 0; i < m; i++)
if (row[i] == 1) {
a[i][j] = 1;
flag = true;
}
if (flag == false) {
System.out.println("NO");
return;
}
}
System.out.println("YES");
printMatrix(a);
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | e992efba55ea00cac997cfc39856ab54 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.* ;
import java.io.*;
public class main {
static int sol [] [] ;
static void fillit(int r , int c , int x , int sol [][]){
for(int i = 0 ; i < sol.length ; i++)
sol[i][c] = x ;
for(int i = 0 ; i < sol[r].length ; i++)
sol[r][i] = x ;
}
static void print2D(int n , int m , int ar [][] ){
StringBuilder ans = new StringBuilder() ;
for(int i = 0 ; i < n ; i++)
{for(int j = 0 ; j < m ; j++)
ans.append(ar[i][j] + " ") ;
ans.append('\n') ;
}
System.out.println(ans);
}
static boolean cheack(int ar[] [] ,int ar2 [] []){
for(int i = 0 ; i < ar.length ; i++)
for(int j = 0 ; j < ar[i].length ; j++)
if( ar[i][j] != ar2[i][j])
return false ;
return true ;
}
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)) ;
StringTokenizer str = new StringTokenizer(bf.readLine()) ;
int n = Integer.parseInt(str.nextToken()) ;
int m = Integer.parseInt(str.nextToken()) ;
sol = new int[n][m] ;
for(int i = 0 ; i < n ; i++)
Arrays.fill(sol[i], 1);
int ar [][] = new int[n][m] ;
for(int i = 0 ; i < n ; i++){
str = new StringTokenizer(bf.readLine()) ;
for(int j = 0 ; j < m ;j++)
{ ar[i][j] = Integer.parseInt(str.nextToken()) ;
if(ar[i][j] == 0)
fillit(i, j,0,sol) ;
}
}
int rr [] [] = new int [n][m] ;
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m ;j++)
if(sol[i][j] == 1)
fillit(i, j, 1, rr) ;
if(cheack(ar, rr))
{ System.out.println("YES");
print2D(n, m, sol);}
else
System.out.println("NO");
}} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 8cc688f76e042236e2fe74460cc3563c | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.Scanner;
public class CF486B {
static int n;
static int m;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
int[][] b = new int[n][m];
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
b[i][j] = in.nextInt();
a[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (b[i][j] == 0) {
for (int k = 0; k < m; k++)
a[i][k] = 0;
for (int k = 0; k < n; k++)
a[k][j] = 0;
}
}
}
if (canTurn(a, b)) {
System.out.println("YES");
print(a);
} else {
System.out.println("NO");
}
}
public static void print(int[][] grid) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sb.append(grid[i][j]).append(" ");
}
sb.append("\n");
}
System.out.print(sb.toString());
}
public static int or(int[][] a, int r, int c) {
for (int i = 0; i < m; i++) {
if (a[r][i] == 1) return 1;
}
for (int i = 0; i < n; i++) {
if (a[i][c] == 1) return 1;
}
return 0;
}
public static boolean same(int[][] a, int[][] b) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != b[i][j]) return false;
}
}
return true;
}
public static boolean canTurn(int[][] a, int[][] b) {
int[][] test = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
test[i][j] = or(a, i, j);
}
}
return same(b, test);
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | f2105f2eea09c7f77b3129af26bf660b | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) throws Exception {
new Main().solve();
}
void solve() throws Exception {
FastScanner in = new FastScanner(System.in);
int s = in.nextInt();
int t = in.nextInt();
int[][] B = new int[s][t];
int[][] A = new int[s][t];
for (int i = 0; i < B.length; i++) {
for (int j = 0; j < B[i].length; j++) {
B[i][j] = in.nextInt();
A[i][j] = 1;
}
}
for (int i = 0; i < B.length; i++) {
for (int j = 0; j < B[i].length; j++) {
if (B[i][j] == 0) {
for (int j2 = 0; j2 < A.length; j2++) {
A[j2][j] = 0;
}
for (int j2 = 0; j2 < A[i].length; j2++) {
A[i][j2] = 0;
}
}
}
}
for (int i = 0; i < B.length; i++) {
for (int j = 0; j < B[i].length; j++) {
boolean flag = false;
if (B[i][j] == 1) {
for (int j2 = 0; j2 < A.length; j2++) {
if (A[j2][j] == 1) {
flag = true;
break;
}
}
for (int j2 = 0; j2 < A[i].length; j2++) {
if (A[i][j2] == 1 || flag) {
flag = true;
break;
}
}
if (!flag) {
System.out.println("NO");
return;
}
}
}
}
System.out.println("YES");
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[i].length; j++) {
System.out.print(A[i][j] + " ");
}
System.out.print("\n");
}
}
//
//
//
//
//
//
//
//
//
//
//
//
class FastScanner {
private InputStream _stream;
private byte[] _buf = new byte[1024];
private int _curChar;
private int _numChars;
private StringBuilder _sb = new StringBuilder();
FastScanner(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 next() {
int c = read();
while (isWhitespace(c)) {
c = read();
}
_sb.setLength(0);
do {
_sb.appendCodePoint(c);
c = read();
} while (!isWhitespace(c));
return _sb.toString();
}
public int nextInt() {
return (int) nextLong();
}
public long nextLong() {
int c = read();
while (isWhitespace(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 (!isWhitespace(c));
return res * sgn;
}
public boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | f61a8c58b475c9ffa792f3dc5d1040ee | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | // Forza Juve
import java.io.*;
import java.util.*;
import java.awt.Point;
public class Template {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
public void run() throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.flush();
out.close();
in.close();
}
public void solve() throws Exception {
int n = nextInt();
int m = nextInt();
boolean[][] b = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (nextInt() == 1) {
b[i][j] = true;
}
}
}
boolean[][] a = new boolean[n][m];
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
boolean ok = true;
for (int i = 0; i < n; i++) {
if (!b[i][y]) {
ok = false;
}
}
for (int j = 0; j < m; j++) {
if (!b[x][j]) {
ok = false;
}
}
a[x][y] = ok;
}
}
boolean[][] c = new boolean[n][m];
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
boolean ok = false;
for (int i = 0; i < n; i++) {
if (a[i][y]) {
ok = true;
}
}
for (int j = 0; j < m; j++) {
if (a[x][j]) {
ok = true;
}
}
c[x][y] = ok;
}
}
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if (b[x][y] != c[x][y]) {
out.println("NO");
return;
}
}
}
out.println("YES");
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
out.print((a[x][y] ? 1 : 0) + " ");
}
out.println();
}
}
public static void main(String[] args) throws Exception {
new Template().run();
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 4abfc9bc3bff2f291ef365c404d6c1d3 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.Scanner;
public class Problem_486B
{
// B. OR in Matrix
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int n = input.nextInt();
int[][] matrixA = new int[m][n];
int[][] matrixB = new int[m][n];
int[][] matrixC = new int[m][n];
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
matrixB[i][j] = input.nextInt();
matrixA[i][j] = 1; // initialization
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrixB[i][j] == 0)
{
for(int k = 0; k < m; k++)
{
matrixA[k][j] = 0;
}
for(int k = 0; k < n; k++)
{
matrixA[i][k] = 0;
}
}
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrixA[i][j] == 1)
{
for(int k = 0; k < m; k++)
{
matrixC[k][j] = 1;
}
for(int k = 0; k < n; k++)
{
matrixC[i][k] = 1;
}
}
}
}
boolean check = true;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrixB[i][j] != matrixC[i][j])
{
check = false;
}
}
}
/*
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(matrixA[i][j] + " ");
}
System.out.println();
}
*/
/*
boolean everCheck = false;
boolean check = false;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrixB[i][j] == 1)
{
everCheck = true;
if(matrixA[i][j] == -1)
{
check = true;
}
}
if(i == m - 1 && j == n - 1 && everCheck == false)
{
check = true;
}
}
}
for(int i = 0; i < m; i++)
{
boolean hasZero = false;
boolean hasOne = false;
for(int j = 0; j < n; j++)
{
if(matrixB[i][j] == 1)
{
hasOne = true;
}
matrixB[i][j] = input.nextInt();
matrixA[i][j] = -1; // initialization
}
}
for(int i = 0; i < m; i++)
{
if(matrixB[i][7]==0);
}
*/
if(check)
{
System.out.println("YES");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(matrixA[i][j] + " ");
/*
if(matrixA[i][j] == -1)
{
System.out.print("1 ");
}
else // matrixA[i][j] == 0
{
System.out.print("0 ");
}*/
}
System.out.println();
}
}
else
{
System.out.println("NO");
}
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | c4ecb6f91f6bf7b9c86b351da9af12da | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class B {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
// PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
// int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] a = new int[n][m];
int[][] b = new int[n][m];
int[][] c = new int[n][m];
for(int i=0; i<n; i++) {
st = new StringTokenizer(bf.readLine());
for(int j=0; j<m; j++) a[i][j] = Integer.parseInt(st.nextToken());
}
boolean[] full = new boolean[n];
boolean[] full2 = new boolean[m];
Arrays.fill(full, true); Arrays.fill(full2, true);
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(a[i][j] == 0) {
full[i] = false;
full2[j] = false;
}
}
}
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(full[i])
if(full2[j])
b[i][j] = 1;
boolean possible = true;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
for(int k=0; k<n; k++)
c[i][j] |= b[k][j];
for(int k=0; k<m; k++)
c[i][j] |= b[i][k];
if(c[i][j] != a[i][j]) possible = false;
}
}
if(!possible) System.out.println("NO");
else {
System.out.println("YES");
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("\n");
}
}
// int n = scan.nextInt();
// out.close(); System.exit(0);
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | f520fd3a926fcf0097489513c66ef7f8 | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//package orinmatrix;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
*
* @author PrudhviNIT
*/
public class Main{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String mn[] = br.readLine().trim().split(" ");
int m,n,i,j,k,l;
m = Integer.parseInt(mn[0]);
n = Integer.parseInt(mn[1]);
int matb[][]=new int[m+1][n+1];
int or=0;
String str[];
for(i=0;i<m;i++)
{
str = br.readLine().trim().split(" ");
or = 0;
for(j=0;j<n;j++)
{
matb[i+1][j+1] = Integer.parseInt(str[j]);
}
}
int a[][] = new int[m+1][n+1];
for(i=0;i<=m;i++)
{
Arrays.fill(a[i],-1);
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(matb[i][j]==0)
{
//System.out.println("matb["+i+"]["+j+"] is "+matb[i][j]);
for(k=1;k<=n;k++)
{
if(a[i][k]==1)
{
System.out.println("NO");
return ;
}
else
{
a[i][k]=0;
}
}
for(k=1;k<=m;k++)
{
if(a[k][j]==1)
{
System.out.println("NO");
return;
}
else
{
a[k][j]=0;
}
}
}
}
}
//DEBUG
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(a[i][j]==-1)
{
a[i][j]=1;
}
}
}
/*System.out.println("Intermediate a");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println();*/
int c[][] = new int[m+1][n+1];
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=0;
//System.out.println("I AND J ARE "+i+","+j);
for(k=1;k<=n;k++)
{
//System.out.println("a["+i+"]["+k+"] is "+a[i][k]);
c[i][j]=c[i][j]|a[i][k];
}
for(k=1;k<=m;k++)
{
//System.out.println("a["+k+"]["+j+"] is "+a[k][j]);
c[i][j]=c[i][j]|a[k][j];
}
//System.out.println("c["+i+"]["+j+"] is "+c[i][j]);
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
//System.out.println("c["+i+"]["+j+"] "+c[i][j]+" , a["+i+"]["+j+"] "+a[i][j]);
if(c[i][j]!=matb[i][j])
{
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
| Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | fcf1131bac2884ad64340920d4fba28f | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ivan
*/
public class secondProblem {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int m=Reader.nextInt();
int n=Reader.nextInt();
int[][] mat=new int[m][n];
int[][] A=new int[m][n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
mat[i][j]=Reader.nextInt();
A[i][j]=-1;
}
/* for(int i=0;i<m;i++)
{
System.out.println();
for(int j=0;j<n;j++)
System.out.print(A[i][j]+" ");
}*/
boolean possible=true;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(mat[i][j]==0)
{
for(int k=0;k<m;k++)
A[k][j]=0;
for(int k=0;k<n;k++)
A[i][k]=0;
}
}
/* for(int i=0;i<m;i++)
{
System.out.println();
for(int j=0;j<n;j++)
System.out.print(A[i][j]+" ");
}*/
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
// possible=true;
// System.out.println(mat[i][j]);
if(mat[i][j]==1)
{
// System.out.println(i+" "+j);
possible=false;
for(int k=0;k<m;k++)
{
//System.out.print(A[k][j]+" ");
if(Math.abs(A[k][j])==1)
{
possible=true;
break;
}
}
// System.out.println();
if(possible)
continue;
for(int k=0;k<n;k++)
if(Math.abs(A[i][k])==1)
{
possible=true;
break;
}
if(!possible)
{
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
for(int i=0;i<m;i++)
{
System.out.println();
for(int j=0;j<n;j++)
System.out.print(Math.abs(A[i][j])+" ");
}
}
}
class Reader {
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() );
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | caa2d0f1934488fbfb631b1531560ceb | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main implements Runnable {
// leave empty to read from stdin/stdout
private static final String TASK_NAME_FOR_IO = "";
// file names
private static final String FILE_IN = TASK_NAME_FOR_IO + ".in";
private static final String FILE_OUT = TASK_NAME_FOR_IO + ".out";
BufferedReader in;
PrintWriter out;
StringTokenizer tokenizer = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new Main()).start();
}
private void solve() throws IOException {
int n = nextInt(), m = nextInt();
int [][]b = new int[n][m];
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
b[i][j] = nextInt();
int [][]a = new int[n][m];
for(int []i : a) Arrays.fill(i, -1);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if (b[i][j] == 0) {
for(int k = 0; k < n; ++k) {
a[k][j] = 0;
}
for(int k = 0; k < m; ++k) {
a[i][k] = 0;
}
}
}
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if (b[i][j] == 1) {
boolean found = false;
for(int k = 0; k < n; ++k) {
if (a[k][j] == -1) {
found = true;
}
}
for(int k = 0; k < m; ++k) {
if (a[i][k] == -1) {
found = true;
}
}
if (!found) {
out.println("NO");
return;
}
}
}
}
out.println("YES");
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if (a[i][j] == -1) a[i][j] = 1;
out.print(a[i][j] + " ");
}
out.println();
}
}
public void run() {
long timeStart = System.currentTimeMillis();
boolean fileIO = TASK_NAME_FOR_IO.length() > 0;
try {
if (fileIO) {
in = new BufferedReader(new FileReader(FILE_IN));
out = new PrintWriter(new FileWriter(FILE_OUT));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
}
solve();
in.close();
out.close();
} catch (IOException e) {
throw new IllegalStateException(e);
}
long timeEnd = System.currentTimeMillis();
if (fileIO) {
System.out.println("Time spent: " + (timeEnd - timeStart) + " ms");
}
}
private String nextToken() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private BigInteger nextBigInt() throws IOException {
return new BigInteger(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 521f10f028d0f1a8c1c17f2a7aa02fdd | train_002.jsonl | 1415718000 | Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner: where is equal to 1 if some ai = 1, otherwise it is equal to 0.Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:.(Bij is OR of all elements in row i and column j of matrix A)Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner t =new Scanner(System.in);
int m=t.nextInt();
int n=t.nextInt();
boolean[][] b=new boolean[m][n];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
b[i][j]=t.nextInt()==0?false:true;
}
}
StringBuilder str=new StringBuilder();
str.append("YES\n");
boolean[][] a=new boolean[m][n];
boolean[][] c=new boolean[m][n];
int cont=0;
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
a[i][j]=true;
for (int k=0;k<n;k++){
a[i][j]&=b[i][k];
if(!a[i][j]) break;
}
if(!a[i][j]){
if(j<n-1) str.append("0 ");
else str.append("0\n");
continue;
}
for (int k=0;k<m;k++){
a[i][j]&=b[k][j];
if(!a[i][j]) break;
}
if(a[i][j]){
for (int k=0;k<m;k++) c[k][j]=true;
for (int k=0;k<n;k++) c[i][k]=true;
}
if(j<n-1) str.append(a[i][j]?"1 ":"0 ");
else str.append(a[i][j]?"1\n":"0\n");
}
}
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
if(b[i][j]!=c[i][j]){
System.out.println("NO");
return;
}
}
}
System.out.print(str);
}
} | Java | ["2 2\n1 0\n0 0", "2 3\n1 1 1\n1 1 1", "2 3\n0 1 0\n1 1 1"] | 1 second | ["NO", "YES\n1 1 1\n1 1 1", "YES\n0 0 0\n0 1 0"] | null | Java 7 | standard input | [
"implementation",
"hashing",
"greedy"
] | bacd613dc8a91cee8bef87b787e878ca | The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively. The next m lines each contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1). | 1,300 | In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one. | standard output | |
PASSED | 8c9e4d80a2c95b4d02c113d1269b4d56 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.util.*;
public class Contest{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
int n=sc.nextInt();//total cards
int m=sc.nextInt();//joker
int k=sc.nextInt();//players
int c;//cards each player has
c=n/k;//cards each player has
if(c>m)
{
System.out.println(m);
}
else
{
System.out.println((n-m)/(k-1));
}
t--;
}
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 46ed3077fbc4336310b7410cbcd5a0e5 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t>0){
String[] s = br.readLine().trim().split(" ");
int n = Integer.parseInt(s[0]);
int m = Integer.parseInt(s[1]);
int k = Integer.parseInt(s[2]);
int ans= Math.min(m,n/k);
m = m- ans;
if(m==0){
System.out.println(ans);
}else{
if(m>k-1){
ans -= m/(k-1);
if(m%(k-1)!=0){
ans--;
}
}else{
ans -=1;
}
System.out.println(ans);
}
t--;
}
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 6d7243d0bcb0f6629eef3ac7e62480d5 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-- > 0) {
int n = scn.nextInt();
int m = scn.nextInt();
int k = scn.nextInt();
System.out.println(ans(n,m,k));
}
}
public static int ans(int n,int m,int k) {
if(m == 0) return 0;
if(m <= n/k) return m;
// if(m >= (n/k)*2 + (n-2)*(n/k-1)) return 0;
int a = m - n/k;
if(a % (k - 1) == 0) return n/k - (a/(k-1));
else return n/k - (a/(k-1)) - 1;
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | f72764a6d60dfb49ddb78afb42e27a8f | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.util.*;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
Scanner scanner = new Scanner(System.in);
int test = scanner.nextInt();
while(test>0)
{
int n=scanner.nextInt();
int j=scanner.nextInt();
int k=scanner.nextInt();
if(j==0)
System.out.println(j);
else{
if(n/k>j)
System.out.println(j);
else if(n/k<=j){
int z=0;
if((j-n/k)%(k-1)==0)
{
z =(j-n/k)/(k-1);
}
else
{
z =(j-n/k)/(k-1)+1;
}
System.out.println(n/k-z);
}
}
test--;
}
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | e84c37c80700a27ce49a47a4e6bb329b | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes |
import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int tc = sc.nextInt();
int j = sc.nextInt();
int p = sc.nextInt();
if (j==0) {
System.out.println(0);
continue;
}
if (tc==p && j>1) {
System.out.println(0);
continue;
}
if (tc/p >=j) {
System.out.println(j);
continue;
}
if (tc/p<j ) {
int cards = tc/p;
int remCards = j -cards;
int lcards= 0;
while (remCards> 0) {
lcards++;
remCards-=(p-1);
}
System.out.println(cards - lcards);
continue;
}
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | e9d543ce00d668ba6e2ac16e06589f91 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class TaskA {
static Scanner in = new Scanner(System.in);
static PrintWriter w = new PrintWriter(System.out);
static long numb = 1;
public static void main(String[] args) {
int t = Integer.parseInt(in.nextLine());
outer: while (t-- > 0) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
int hand = n / k;
k--;
int mj = 0, ans = 0;
if (m == n || m == 0) {
w.println(0);
continue;
}
if (hand >= m) {
w.println(m);
continue;
} else {
mj = hand;
m -= mj;
}
int minj = 0;
if (m <= k) {
w.println(mj - 1);
} else {
int div = 0;
if(m%k==0)
div = m/k;
else
div = m/k + 1;
w.println(mj - div);
}
}
w.flush();
w.close();
}
private static void shuffleArray(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = rnd.nextInt(n);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
private static 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 | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | af58beb5cde9fed5ee30a83f04a5a605 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes |
import java.util.*;
public class codfor {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt(), m = s.nextInt(), k = s.nextInt();
int ans = 0;
if (n / k >= m) {
ans = m;
} else {
int temp = (m - (n / k)) / (k - 1);
if ((m - (n / k)) % (k - 1) != 0) {
temp++;
}
ans = n / k - temp;
}
System.out.println(ans);
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | a2c36443323fb3fcc4c277727d32e8fb | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class prime{
void pre() throws Exception{}
void solve(int TC) throws Exception{
int t=ni();
while(t--!=0) {
int n=ni();
int m=ni();
int k=ni();
int ab=n/k;
if(n==k) {
if(m==1)pn("1");
else pn("0");
}
else {
int x=Math.min(ab,m);
int y=(m-x)/(k-1);
if(y*(k-1)!=(m-x))y++;
pn(x-y);
}
}
}
static boolean multipleTC = false, memory = false;
FastReader in;PrintWriter out;
void run() throws Exception{
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC)?ni():1;
pre();for(int t = 1; t<= T; t++)solve(t);
out.flush();
out.close();
}
public static void main(String[] args) throws Exception{
if(memory)new Thread(null, new Runnable() {public void run(){try{new prime().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start();
else new prime().run();
}
void p(Object o){out.print(o);}
void pn(Object o){out.println(o);}
void nd(Object o){out.println(o);}
void pni(Object o){out.println(o);out.flush();}
String n()throws Exception{return in.next();}
String nln()throws Exception{return in.nextLine();}
int ni()throws Exception{return Integer.parseInt(in.next());}
long nl()throws Exception{return Long.parseLong(in.next());}
double nd()throws Exception{return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception{
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception{
String str = "";
try{
str = br.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | e59a91bb14c1ca5a04839d88f64b7e35 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br ;
public static void main(String[] args) throws Exception {
// write your code here
Scanner sc = new Scanner(System.in);
int tc =sc.nextInt();
while(tc-->0){
int n =sc.nextInt();
int m =sc.nextInt();
int k = sc.nextInt();
int p = n/k;
if(m==0||m==n){
System.out.println(0);
continue;
}
if(p>=m){
System.out.println(m);
continue;
}
int r = m-p;
int div =r /(k-1);
int m1 = r %(k-1);
if(m1!=0){
div=div+1;
}
// System.out.println(c+ " "+b);
int ans = p-(div);
System.out.println(ans);
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | ea04951f7b19bc782d24da94fc30a6b9 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | //package main;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Submission {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter (System.out);
//BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
int q = Integer.parseInt(reader.readLine());
for (int p=0;p<q;p++) {
StringTokenizer st = new StringTokenizer(reader.readLine()," ");
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
if (m<=n/k) out.println(m);
else {
int prime=n/k;
m-=n/k;
n-=k;
k--;
int x = (int)Math.ceil((double)m/(double)k);
out.println(prime-x);
}
}
reader.close();
out.close();
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 69c0dd7a8790f640a3c2041f667af8f1 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.util.Scanner;
public class A88 {
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int t=obj.nextInt();
while(t-->0)
{
int n=obj.nextInt();
int m=obj.nextInt();
int k=obj.nextInt();
int ca=n/k;
if(ca>=m)
{
int max=m;
System.out.println(m-0);
}
else
{
k=k-1;
m=m-(ca);
n=n-(ca);
int v=(int)Math.ceil(m/(double)(k));
System.out.println(ca-v);
}
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 712978ce4b7f78bd1944b51717c2d94e | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class cdfer88a
{
static void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
static void sort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
}
}
public static int lowerBound(int[] array, 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 <= array[mid])
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
public static int upperBound(int[] array, int length, int value)
{
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= array[mid]) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long n,long m)
{
if(m==0)
return 1;
long ans=1;
while(m>0)
{
ans=ans*n;
m--;
}
return ans;
}
static int BinarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
for(int i=1;i<=t;i++)
{
String s=br.readLine();
String str[]=s.split(" ");
int n=Integer.parseInt(str[0]);
int m=Integer.parseInt(str[1]);
int k=Integer.parseInt(str[2]);
if(m==0)
{
System.out.println("0");
continue;
}
else if(n/k>=m)
{
System.out.println(m);
continue;
}
else
{
n=n/k;
int a=n;
m-=a;
k--;
if(m%k==0)
m=m/k;
else
m=m/k+1;
System.out.println(a-m);
}
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 4fa1404978691c5bb2de10e147e49075 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class cdfer88a
{
static void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
static void sort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
}
}
public static int lowerBound(int[] array, 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 <= array[mid])
{
high = mid;
}
else
{
low = mid + 1;
}
}
return low;
}
public static int upperBound(int[] array, int length, int value)
{
int low = 0;
int high = length;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= array[mid]) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long n,long m)
{
if(m==0)
return 1;
long ans=1;
while(m>0)
{
ans=ans*n;
m--;
}
return ans;
}
static int BinarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
for(int i=1;i<=t;i++)
{
String s=br.readLine();
String str[]=s.split(" ");
int n=Integer.parseInt(str[0]);
int m=Integer.parseInt(str[1]);
int a=Integer.parseInt(str[2]);
int temp=n/a;
//System.out.println(temp+" "+m);
if(m==0)
{
System.out.println("0");
continue;
}
if(temp>=m)
{
System.out.println(m);
continue;
}
m-=temp;
a--;
if(a==0)
{
System.out.println("0");
continue;
}
if(m%a==0)
m=m/a;
else
m=m/a+1;
System.out.println(temp-m);
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 12dab3df4bebc07bc2ce4424a71f650c | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | //ARNAV KUMAR MANDAL//
//XYPHER//
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.io.*;
public class Solution{
//--------------------------MAIN---------------------------------------------------------------------//
public static void main(String[] args) throws IOException {
int tc = ni();
while(tc-->0) {
int n = ni();
int m = ni();
int k = ni();
int max = m;
if(max > n/k)max = n/k;
m = m - max;
int g = k - 1;
// opl(max+" "+g);
int max2 = m/g;
if(m%g>0)max2++;
opl(max - max2);
}
}
//--------------------------FAST IO objects/methods--------------------------------------------------//
static Reader in= new Reader(System.in);
static OutputWriter out = new OutputWriter(System.out);
private static int ni(){int n = in.nextInt();return n;}
private static long nl(){long n = in.nextLong();return n;}
private double nd(){return Double.parseDouble(ns()); }
private static String ns(){String s = in.nextString();return s;}
private static int[] nai(int n){int[] a = new int[n];for(int i = 0;i < n;i++)a[i] = ni();return a;}
private static long[] nal(int n){long[] a = new long[n];for(int i = 0;i < n;i++)a[i] = nl();return a;}
private static StringBuilder sb(String s){StringBuilder S = new StringBuilder(s);return S;}
private static HashSet<Integer> hsi(){HashSet<Integer> s = new HashSet<Integer>();return s;}
private static HashSet<Long> hsl(){HashSet<Long> s = new HashSet<Long>();return s;}
private static HashMap<Integer,Integer> hmii(){HashMap<Integer,Integer> h = new HashMap<Integer,Integer>();return h;}
private static HashMap<Long,Long> hmll(){HashMap<Long,Long> h = new HashMap<Long,Long>();return h;}
private static HashMap<String,Integer> hmsi(){HashMap<String,Integer> h = new HashMap<String,Integer>();return h;}
private static int[][] nmi(int n, int m){int A[][] = new int[n][m];for(int i=0;i<n;i++){for(int j=0;j<m;j++) {A[i][j] = ni();}}return A;}
private static char[][] nmc(int n, int m){char A[][] = new char[n][m];for(int i=0;i<n;i++){StringBuilder s=sb(ns());for(int j=0;j<m;j++){A[i][j]=s.charAt(j);}}return A;}
private static void op(Object o) {out.print(o);}
private static void opl(Object o) {out.println(o);}
private static void opai(int[] o){for(int a:o)op(a+" ");opl("");}
private static void opad(double[] o){for(double a:o)op(a+" ");opl("");}
private static void opal(long[] o){for(long a:o)op(a+" ");opl("");}
private static void opac(char[] o){for(char a:o)op(a);opl("");}
private static void opas(String[] o){for(String a:o)op(a+" ");opl("");}
private static void opmi(int[][] o) {for(int i=0;i<o.length;i++)opai(o[i]);}
private static void opml(long[][] o) {for(int i=0;i<o.length;i++)opal(o[i]);}
private static void opmc(char[][] o) {for(int i=0;i<o.length;i++)opac(o[i]);}
//--------------------------FAST IO------------------------------------------------------------------//
private static class Reader {
private InputStream stream;
private byte[] buf = new byte[4*1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public Reader(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 {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
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 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 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 nextString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
private 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 println(Object... objects) {
print(objects);
writer.println();
writer.flush();
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.flush();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 674c0c4de5d6752983a3f416e7452f37 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | /*
Aman Agarwal
algo.java
*/
import java.util.*;
import java.io.*;
public class A
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
//static int[][] nai2(int n,int m){int a[][] = new int[n][m];for(int i=0;i<n;i++)for(int j=0;j<m;j++)a[i][j] = ni();return a;}
static int[] nai(int N,int start)throws IOException{int[]A=new int[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
static Integer[] naI(int N,int start)throws IOException{Integer[]A=new Integer[N+start];for(int i=start;i!=(N+start);i++){A[i]=ni();}return A;}
static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static void print(int arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
static void print(long arr[]){for(int i=0;i<arr.length;i++)out.print(arr[i]+" ");out.println();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} // return the number of set bits.
static boolean isPrime(int number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}int sqrt = (int) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
static boolean isPrime(long number){if(number==1) return false;if (number == 2 || number == 3){return true;}if (number % 2 == 0) {return false;}long sqrt = (long) Math.sqrt(number) + 1;for(int i = 3; i < sqrt; i += 2){if (number % i == 0){return false;}}return true;}
static int power(int n,int p){if(p==0)return 1;int a = power(n,p/2);a = a*a;int b = p & 1;if(b!=0){a = n*a;}return a;}
static long power(long n,long p){if(p==0)return 1;long a = power(n,p/2);a = a*a;long b = p & 1;if(b!=0){a = n*a;}return a;}
static void reverse(int[] a) {int b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
static void reverse(long[] a) {long b;for (int i = 0, j = a.length - 1; i < j; i++, j--) {b = a[i];a[i] = a[j];a[j] = b;}}
static void swap(int a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];}
static void swap(long a[],int i,int j){a[i] = a[i] ^ a[j];a[j] = a[j] ^ a[i];a[i] = a[i] ^ a[j];}
static int count(int n){int c=0;while(n>0){c++;n = n/10;}return c;}
static int[] prefix_sum(int a[],int n){int s[] = new int[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
static long[] prefix_sum_int(int a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
static long[] prefix_sum_Integer(Integer a[],int n){long s[] = new long[n];s[0] = (long)a[0];for(int i=1;i<n;i++){s[i] = ((long)a[i])+s[i-1];}return s;}
static long[] prefix_sum_long(long a[],int n){long s[] = new long[n];s[0] = a[0];for(int i=1;i<n;i++){s[i] = a[i]+s[i-1];}return s;}
static boolean isPerfectSquare(double x){double sr = Math.sqrt(x);return ((sr - Math.floor(sr)) == 0);}
static ArrayList<Integer> sieve(int n) {int k=0; boolean prime[] = new boolean[n+1];ArrayList<Integer> p_arr = new ArrayList<>();for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ k=p;if(prime[p] == true) { p_arr.add(p);for(int i = p*2; i <= n; i += p) prime[i] = false; } }for(int i = k+1;i<=n;i++){if(prime[i]==true)p_arr.add(i);}return p_arr;}
static boolean[] seive_check(int n) {boolean prime[] = new boolean[n+1];for(int i=0;i<n;i++) prime[i] = true;for(int p = 2; p*p <=n; p++){ if(prime[p] == true) { for(int i = p*2; i <= n; i += p) prime[i] = false; } }prime[1]=false;return prime;}
static int get_bits(int n){int p=0;while(n>0){p++;n = n>>1;}return p;}
static int get_bits(long n){int p=0;while(n>0){p++;n = n>>1;}return p;}
static int get_2_power(int n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
static int get_2_power(long n){if((n & (n-1))==0)return get_bits(n)-1;return -1;}
static void close(){out.flush();}
/*-------------------------Main Code Starts(algo.java)----------------------------------*/
public static void main(String[] args) throws IOException
{
int t = 1;
t = sc.nextInt();
while(t-- > 0)
{
long n = nl();
long m = nl();
long k = nl();
long d = n/k;
if(m==0)
{
out.println(0);
close();
continue;
}
else
{
if (n / k >= m)
{
out.println(m);
close();
continue;
}
else
{
m -= n / k;
k--;
if (m % k == 0)
{
out.println(d - (m / k));
close();
continue;
}
else
{
long s = m % k;
if (m <= k)
{
out.println(d - 1);
close();
continue;
}
else
{
long j = (m / k) + 1;
if (d - j >= 0)
{
out.println(d - j);
close();
continue;
}
else
{
out.println("0");
close();
continue;
}
}
}
}
}
}
close();
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 9edec66c380df9f4a5d0802f052e662e | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class BazingaA {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
PrintWriter pw = new PrintWriter(System.out);
try {
int t = Integer.parseInt(br.readLine());
while(t-->0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int each = n/k;
if(each >= m) {
pw.println(m);
}
else {
m -= each;
int rest = 0;
if(m%(k-1) == 0) {
rest = m/(k-1);
}
else rest = m/(k-1) + 1;
pw.println(each-rest);
}
}
}
finally {
pw.flush();
pw.close();
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 8adbad4605de0a9a8116e0a5ce270cef | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.lang.*;
public class poker {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out,true);
int t= Integer.parseInt(br.readLine());
while(t-- > 0){
StringTokenizer tok = new StringTokenizer(br.readLine()," ");
int cards = Integer.parseInt(tok.nextToken());
int jokers = Integer.parseInt(tok.nextToken());
int players = Integer.parseInt(tok.nextToken());
int eachPlayersCards = cards/players;
int res = 0;
if(jokers <= eachPlayersCards){
res = jokers;
}
else if(jokers > eachPlayersCards){
int firstPlayersJok = eachPlayersCards;
jokers-=eachPlayersCards;
players--;
int arr[] = new int[players];
int i = 0;
int max = 0;
while(true){
if(i == players){
i = 0;
continue;
}
jokers--;
arr[i]++;
max = Math.max(max,arr[i]);
i++;
if(jokers == 0) break;
}
res = firstPlayersJok-max;
}
out.println(res);
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 6efb906dafa4c00387c5c518085fcabc | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class A
{
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
static StringBuilder sb=new StringBuilder();
static long MOD = (long)(998244353);
// Main Class Starts Here
public static long calc(int x) {
return x*1L*(x+1) / 2L;
}
public static void main(String args[])throws IOException
{
// Write your code.
int t=in();
while(t-->0) {
int n=in();
int m=in(); // jokers
int k=in(); // players
if(m==0) {
app(0+"\n");
continue;
}
int each = n/k;
if(each >= m) {
app(m+"\n");
continue;
}
else {
m-=each;
int div = m / (k-1);
int mod = m % (k-1);
if(mod > 0) {
div++;
}
int ans = each - div;
app(ans+"\n");
}
}
out.printLine(sb);
out.close();
}
static class CircLL{
// circular linked list.
Node head;
Node last;
public CircLL(long num) {
head = new Node(num);
// pointing to itself.
last = head;
last.next = head;
}
public void add(long num) {
Node fresh = new Node(num);
last.next = fresh;
last = last.next;
last.next = head;
}
public void print() {
Node temp = head;
while(temp.next!=head) {
temp = temp.next;
}
prln("\n");
}
}
static class Node{
long data;
Node next;
public Node(long data) {
this.data=data;
this.next=null;
}
}
public static int lis(int a[]) {
int n=a.length;
if(n==0 || n==1)
return n;
int dp[] = new int[n];
for(int i=0;i<n;i++) {
dp[i] = 1;
for(int j=0;j<i;j++) {
if(a[j] < a[i]) {
dp[i] = Math.max(dp[i], (dp[j]+1));
}
}
}
Arrays.sort(dp);
return dp[n-1];
}
public static long rev(long n) {
if(n<10L)
return n;
long rev = 0L;
while(n != 0L) {
long a = n % 10L;
rev = a + rev*10L;
n /= 10L;
}
return rev;
}
public static long pow(long a,long b) {
if(b==0)
return 1L;
if(b==1)
return a % MOD;
long f = a * a;
if(f > MOD)
f %= MOD;
long val = pow(f, b/2) % MOD;
if(b % 2 == 0)
return val;
else {
long temp = val * (a % MOD);
if(temp > MOD)
temp = temp % MOD;
return temp;
}
}
public static long lcm(long a,long b) {
long lcm = (a*b)/gcd(a,b);
return lcm;
}
public static long gcd(long a,long b) {
if(b==0)
return a;
return gcd(b, a%b);
}
public static int[] an(int n) {
int ar[]=new int[n];
for(int i=0;i<n;i++)
ar[i]=in();
return ar;
}
public static long[] lan(int n) {
long ar[]=new long[n];
for(int i=0;i<n;i++)
ar[i]=lin();
return ar;
}
public static String atos(Object ar[]) {
return Arrays.toString(ar);
}
public static int in() {
return in.readInt();
}
public static long lin() {
return in.readLong();
}
public static String sn() {
return in.readString();
}
public static void prln(Object o) {
out.printLine(o);
}
public static void prn(Object o) {
out.print(o);
}
public static int getMax(int a[]) {
int n = a.length;
int max = a[0];
for(int i=1; i < n; i++) {
max = Math.max(max, a[i]);
}
return max;
}
public static int getMin(int a[]) {
int n = a.length;
int min = a[0];
for(int i=1; i < n; i++) {
min = Math.min(min, a[i]);
}
return min;
}
public static void display(int a[]) {
out.printLine(Arrays.toString(a));
}
public static HashMap<Integer,Integer> hm(int a[]){
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0; i < a.length;i++) {
int keep = (int)map.getOrDefault(a[i],0);
map.put(a[i],++keep);
}
return map;
}
public static void app(Object o) {
sb.append(o);
}
public static long calcManhattanDist(int x1,int y1,int x2,int y2) {
long xa = Math.abs(x1-x2);
long ya = Math.abs(y1-y2);
return (xa+ya);
}
static 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 long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
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);
}
}
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 printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 934e31d2eea7e61848bf909ddd7cae02 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.util.*;
public class Joker
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
for(int iter1=0;iter1<T;iter1++)
{
int n=sc.nextInt();
int m=sc.nextInt();
int k=sc.nextInt();
if(m==0)
System.out.println(0);
else
{
int cardsPerPlayer=n/k;
if(m<=cardsPerPlayer)
System.out.println(m);
else
{
int nextMax;
m-=cardsPerPlayer;
nextMax=m/(k-1);
if(m%(k-1)!=0)
nextMax+=1;
System.out.println(cardsPerPlayer-nextMax);
}
}
}
sc.close();
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | f64b50e5097b9967067295900e9e5060 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main implements Runnable{
FastScanner sc;
PrintWriter pw;
final class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public long nlo() {
return Long.parseLong(next());
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public String nli() {
String line = "";
if (st.hasMoreTokens()) line = st.nextToken();
else try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (st.hasMoreTokens()) line += " " + st.nextToken();
return line;
}
public double nd() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) throws Exception
{
new Thread(null,new Main(),"codeforces",1<<28).start();
}
public void run()
{
sc=new FastScanner();
pw=new PrintWriter(System.out);
solve();
pw.flush();
pw.close();
}
public long gcd(long a,long b)
{
return b==0L?a:gcd(b,a%b);
}
public long ppow(long a,long b,long mod)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1)
tmp*=a;
a*=a;
a%=mod;
tmp%=mod;
b>>=1;
}
return (tmp*a)%mod;
}
public int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
//////////////////////////////////
///////////// LOGIC ///////////
////////////////////////////////
public void solve()
{
int t=sc.ni();
while(t-->0)
{
int n=sc.ni();
int m=sc.ni();
int k=sc.ni();
int tmp=Math.min(m,n/k);
m-=tmp;
{
int tmp1=tmp;
tmp=m/(k-1);
tmp=tmp+(m%(k-1)==0?0:1);
tmp1-=tmp;
pw.println(tmp1);
}
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 8184fb53d71a735d7d8653b38ad82db6 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t>0){
t--;
float n = s.nextInt();
float m = s.nextInt();
float k = s.nextInt();
if(m == 0 || m == n){
System.out.println(0);
}
else {
if(n/k<=m) {
System.out.println((int)(n/k - Math.ceil((m-n/k)/(k-1)) ));
}
else {
System.out.println((int)m);
}
}
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 7c2d1664548d9bef19368bd3daed8959 | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 256 megabytes |
import java.util.Scanner;
public class BerlandPoker {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
while(t-->0) {
int n=scan.nextInt();
int m=scan.nextInt();
int k=scan.nextInt();
if(n==k) {
if(m==1)System.out.println("1");
else System.out.println("0");
}
else if(n/k>=m) System.out.println(m);
else {
int max;
int rem=m-n/k;
if(rem%(k-1)==0) max=rem/(k-1);
else max=rem/(k-1)+1;
System.out.println(n/k-max);
}
}
}
}
| Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | f1748c812d202e452500b6345e4d84bd | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 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 q1
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t= Integer.parseInt(sc.next());
while(t-->0)
{
int n = Integer.parseInt(sc.next());
int m = Integer.parseInt(sc.next());
int k = Integer.parseInt(sc.next());
int kl=n/k;
int max= Math.min(kl,m);
int r= m-max;
int min= r/(k-1);
k--;
if(r%k!=0)
{
min++;
}
System.out.println(max-min);
}
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | e9a503ed9d9b2381110c41ccba18b89e | train_002.jsonl | 1590676500 | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$).At the beginning of the game, each player takes $$$\frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points.Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. | 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();
while(t-->0){
int n=scn.nextInt();
int m =scn.nextInt();
int k =scn.nextInt();
int val = n/k;
int min = Math.min(val, m);
int rest= (m-min);
int val2= rest/(k-1);
if(rest%(k-1)!=0){
val2++;
}
// System.out.println(min+" "+val2);
// rest= Math.max(rest-1, 0);
System.out.println(min-val2);
}
}
} | Java | ["4\n8 3 2\n4 2 4\n9 6 3\n42 0 7"] | 2 seconds | ["3\n0\n1\n0"] | NoteTest cases of the example are described in the statement. | Java 8 | standard input | [
"greedy",
"math",
"brute force"
] | 6324ca46b6f072f8952d2619cb4f73e6 | The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 50$$$, $$$0 \le m \le n$$$, $$$2 \le k \le n$$$, $$$k$$$ is a divisors of $$$n$$$). | 1,000 | For each test case, print one integer — the maximum number of points a player can get for winning the game. | standard output | |
PASSED | 9b12c4f32e2a459bd87880c6aa94e483 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int k=in.nextInt();
int[] a=new int[n];
int[] l=new int[n];
List<HashMap<Integer,Integer>> r=new ArrayList<>();
for(int i=0;i<11;i++) {
r.add(new HashMap<Integer,Integer>());
}
for(int i=0;i<n;i++) {
a[i]=in.nextInt();
l[i]=(int)(Math.log10(a[i])+1);
a[i]%=k;
if(r.get(l[i]).containsKey(a[i])) {
r.get(l[i]).replace(a[i], r.get(l[i]).get(a[i])+1);
}else {
r.get(l[i]).put(a[i], 1);
}
}
in.close();
long answer=0;
for(int i=0;i<n;i++) {
long m=a[i];
for(int j=1;j<11;j++) {
m=m*10%k;
int rr=(k-(int)m)%k;
if(r.get(j).containsKey(rr)) {
answer+=r.get(j).get(rr);
if(l[i]==j&&a[i]==rr) answer-=1;
}
}
}
System.out.println(answer);
}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | e0c8513490167601e5e770439b6386d4 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import static java.lang.Math.*;
import java.util.concurrent.ThreadLocalRandom;
public class Sol implements Runnable {
long mod = (long) 1e9 + 7;
void solve(InputReader in, PrintWriter w) {
int n = in.nextInt();
long k = in.nextLong();
long a[] = new long[n];
for(int i=0;i<n;i++) a[i] = in.nextLong();
long pow[] = new long[11];
pow[0] = 1;
for(int i=1;i<=10;i++) pow[i] = (pow[i - 1] * 10L) % k;
HashMap<Long, Integer> bitsRem[] = new HashMap[11];
for(int i=0;i<=10;i++) bitsRem[i] = new HashMap<>();
for(int i=0;i<n;i++) {
long x = a[i];
int cnt = 0;
while(x > 0) {
x /= 10;
cnt++;
}
long rem = a[i] % k;
if(bitsRem[cnt].containsKey(rem)) bitsRem[cnt].put(rem, bitsRem[cnt].get(rem) + 1);
else bitsRem[cnt].put(rem, 1);
}
long pairs = 0;
for(int i=0;i<n;i++) {
long x = a[i];
int bitsHere = 0;
while(x > 0) {
x /= 10;
bitsHere++;
}
x = a[i];
for(int j=1;j<=10;j++) {
long mul = pow[j];
long remHere = (x * mul) % k;
long remReq = (k - remHere) % k;
if(bitsRem[j].containsKey(remReq)) {
pairs += bitsRem[j].get(remReq);
//w.println(pairs+" "+i+" "+j+" "+remHere+" "+remReq);
if(remReq == (x % k) && bitsHere == j) pairs--;
}
}
}
w.println(pairs);
}
// ************* Code ends here ***************
void init() throws Exception {
//Scanner in;
InputReader in;
PrintWriter w;
boolean online = false;
String common_in_fileName = "\\in";
String common_out_fileName = "\\out";
int test_files = 0;
for (int file_no = 0; file_no <= test_files; file_no++) {
String x = "" + file_no;
if (x.length() == 1) x = "0" + x;
String in_fileName = common_in_fileName + "" + x;
String out_fileName = common_out_fileName + "" + x;
if (online) {
//in = new Scanner(new File(in_fileName + ".txt"));
in = new InputReader(new FileInputStream(new File(in_fileName + ".txt")));
w = new PrintWriter(new FileWriter(out_fileName + ".txt"));
} else {
//in = new Scanner(System.in);
in = new InputReader(System.in);
w = new PrintWriter(System.out);
}
solve(in, w);
w.close();
}
}
public void run() {
try {
init();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Sol(), "Sol", 1 << 28).start();
}
static 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 String nextLine() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
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);
}
}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 10aa9311b081a5b643271310f99f09cb | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
import static java.util.Arrays.fill;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
import static java.util.Collections.sort;
public class D {
static int mod = 1000000007;
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int n=in.nextInt();
int k=in.nextInt();
int[] a=new int[n];
Map<String, Integer> map=new HashMap<>();
//len mod
int[] len=new int[n];
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
int md=a[i]%k;
int x=a[i];
int l=0;
while(x!=0)
{
l++;
x/=10;
}
len[i]=l;
String t=l+" "+md;
if(map.containsKey(t))
map.put(t, map.get(t)+1);
else
map.put(t, 1);
}
long ans=0;
for(int i=0;i<n;i++)
{
int initmod=a[i]%k;
long rem=initmod;
for(int l=1;l<=10;l++)
{
rem*=10;
rem%=k;
String t=l+" "+(k-rem)%k;
if(map.containsKey(t))
{
ans+=(map.get(t));
}
else
continue;
if(len[i]==l && (initmod==(k-rem)%k))
{
ans--;
}
}
}
out.println(ans);
out.close();
}
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 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;
}
}
private static void tr(Object... o) {
if (!(System.getProperty("ONLINE_JUDGE") != null))
System.out.println(Arrays.deepToString(o));
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 3df45b399843b60a592263e75fbd611f | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Stream;
public class Main implements Runnable {
static final int MOD = (int) 1e9 + 7;
static final int MI = (int) 1e9;
static final long ML = (long) 1e18;
static final Reader in = new Reader();
static final PrintWriter out = new PrintWriter(System.out);
StringBuilder answer = new StringBuilder();
public static void main(String[] args) {
new Thread(null, new Main(), "persefone", 1 << 28).start();
}
@Override
public void run() {
solve();
printf();
flush();
}
void solve() {
int n = in.nextInt();
int k = in.nextInt();
String[] s = new String[n];
for (int i = 0; i < n; i++) s[i] = in.next();
int[][] a = new int[n][2];
for (int i = 0; i < n; i++) a[i] = new int[] { Integer.parseInt(s[i]), s[i].length() };
Map<Integer, Integer>[] remainderGroupByNumberLength = new HashMap[11];
for (int i = 0; i < n; i++) {
int len = a[i][1];
int key = a[i][0] % k;
if (remainderGroupByNumberLength[len] == null) remainderGroupByNumberLength[len] = new HashMap<>();
remainderGroupByNumberLength[len].put(key, remainderGroupByNumberLength[len].getOrDefault(key,0) + 1);
}
long[] product = new long[11];
product[1] = 10;
for (int i = 2; i < 11; i++) product[i] = product[i - 1] * 10;
long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < 11; j++) {
if (remainderGroupByNumberLength[j] != null) {
int r = (int) (product[j] % k * a[i][0] % k);
int v = remainderGroupByNumberLength[j].getOrDefault((k - r) % k, 0);
if (v == 0) continue;
if (a[i][1] == j && (r + a[i][0]) % k == 0) v--;
ans += v;
}
}
}
printf(ans);
}
void printf() {
out.print(answer);
}
void close() {
out.close();
}
void flush() {
out.flush();
}
void printf(Stream<?> str) {
str.forEach(o -> add(o, " "));
add("\n");
}
void printf(Object... obj) {
printf(false, obj);
}
void printfWithDescription(Object... obj) {
printf(true, obj);
}
private void printf(boolean b, Object... obj) {
if (obj.length > 1) {
for (int i = 0; i < obj.length; i++) {
if (b) add(obj[i].getClass().getSimpleName(), " - ");
if (obj[i] instanceof Collection<?>) {
printf((Collection<?>) obj[i]);
} else if (obj[i] instanceof int[][]) {
printf((int[][]) obj[i]);
} else if (obj[i] instanceof long[][]) {
printf((long[][]) obj[i]);
} else if (obj[i] instanceof double[][]) {
printf((double[][]) obj[i]);
} else printf(obj[i]);
}
return;
}
if (b) add(obj[0].getClass().getSimpleName(), " - ");
printf(obj[0]);
}
void printf(Object o) {
if (o instanceof int[])
printf(Arrays.stream((int[]) o).boxed());
else if (o instanceof char[])
printf(new String((char[]) o));
else if (o instanceof long[])
printf(Arrays.stream((long[]) o).boxed());
else if (o instanceof double[])
printf(Arrays.stream((double[]) o).boxed());
else if (o instanceof boolean[]) {
for (boolean b : (boolean[]) o) add(b, " ");
add("\n");
} else
add(o, "\n");
}
void printf(int[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(long[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(double[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(boolean[]... obj) {
for (int i = 0; i < obj.length; i++) printf(obj[i]);
}
void printf(Collection<?> col) {
printf(col.stream());
}
<T, K> void add(T t, K k) {
if (t instanceof Collection<?>) {
((Collection<?>) t).forEach(i -> add(i, " "));
} else if (t instanceof Object[]) {
Arrays.stream((Object[]) t).forEach(i -> add(i, " "));
} else
add(t);
add(k);
}
<T> void add(T t) {
answer.append(t);
}
@SuppressWarnings("unchecked")
<T extends Comparable<? super T>> T min(T... t) {
if (t.length == 0)
return null;
T m = t[0];
for (int i = 1; i < t.length; i++)
if (t[i].compareTo(m) < 0)
m = t[i];
return m;
}
@SuppressWarnings("unchecked")
<T extends Comparable<? super T>> T max(T... t) {
if (t.length == 0)
return null;
T m = t[0];
for (int i = 1; i < t.length; i++)
if (t[i].compareTo(m) > 0)
m = t[i];
return m;
}
static class Pair<K extends Comparable<? super K>, V extends Comparable<? super V>>
implements Comparable<Pair<K, V>> {
private K k;
private V v;
Pair(K k, V v) {
this.k = k;
this.v = v;
}
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || !(o instanceof Pair)) return false;
Pair<K, V> p = (Pair<K, V>) o;
return k.compareTo(p.k) == 0 && v.compareTo(p.v) == 0;
}
@Override
public int hashCode() {
int hash = 31;
hash = hash * 89 + k.hashCode();
hash = hash * 89 + v.hashCode();
return hash;
}
@Override
public int compareTo(Pair<K, V> pair) {
return k.compareTo(pair.k) == 0 ? v.compareTo(pair.v) : k.compareTo(pair.k);
}
@Override
public Pair<K, V> clone() {
return new Pair<K, V>(this.k, this.v);
}
@Override
public String toString() {
return String.valueOf(k).concat(" ").concat(String.valueOf(v)).concat("\n");
}
}
static class Reader {
private BufferedReader br;
private StringTokenizer st;
Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | c314f1bebcd7b4a81dfead3b38a65e7f | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class D {
static int size(int x)
{
int size = 0 ;
for( ; x > 0 ; x /= 10) size ++;
return size ;
}
static int mult (int x , int y , int k)
{
return (int)((1l * x * y) % k );
}
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner();
int n = sc.nextInt() , k = sc.nextInt();
HashMap<Integer,Integer> [] map = new HashMap[11];
for(int i = 0 ; i < 11 ; i++)
map[i] = new HashMap<>();
int [] a = new int [n];
for(int i = 0 ; i < n ;i++)
{
a[i]= sc.nextInt();
int sz = size(a[i]);
map[sz].put(a[i] % k, map[sz].getOrDefault(a[i] % k, 0) + 1) ;
}
long ans = 0 ;
for(int i = 0 ; i < n ;i++)
{
int x = mult(a[i], 10, k);
for(int j = 1 ; j <= 10 ; j++)
{
int search = (k - (x % k)) % k;
ans += map[j].getOrDefault(search, 0) - (search == (a[i] % k) && j == size(a[i]) ? 1 : 0) ;
x = mult(x, 10, k);
}
}
System.out.println(ans);
}
static class Scanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st ;
String next() throws Exception
{
while(st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws Exception
{
return Integer.parseInt(next());
}
long nextLong() throws Exception
{
return Long.parseLong(next());
}
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 30fe2d67501e8f68936ba5948ed2ce40 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.util.HashMap;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//long val1 = 922337204,val = 56950000;
//System.out.println(val1*val);
Scanner s= new Scanner(System.in);
int N = s.nextInt();
int K = s.nextInt();
int[] arr = new int[N];
int[] digits = new int[N];
HashMap<Integer,Integer>[] map = new HashMap[15];
for(int j=0;j<map.length;j++) {
map[j] = new HashMap<>();
}
for(int j=0;j<N;j++) {
int x = s.nextInt();
arr[j] = x%K;
int digit = countDigits(x);
if(!map[digit].containsKey(arr[j]))
map[digit].put(arr[j], 1);
else {
int count = map[digit].get(arr[j]);
map[digit].put(arr[j], count+1);
}
//System.out.println(digit_mod[digit][arr[j]]);
digits[j]= digit;
}
//System.out.println(arr[-1]);
long ans = 0;
//digit_mod[1][0] = 1;
for(int j=0;j<N;j++) {
for(int k=1;k<=10;k++) {
//System.out.println(arr[j]+" "+(long)(Math.pow(10, k)%K));
long val1 = arr[j];
long val2 = (long)(Math.pow(10, k));
//System.out.println("num = "+val1*(val2%K));
int rem = (int)((val1*(val2%K))%K);
int req = (K-rem)%K;
//System.out.println(rem);
//System.out.println(req+" "+k+" "+digit_mod[k][req]);
int count = 0;
if(map[k].containsKey(req))
count = map[k].get(req);
ans+=count;
if(req==arr[j]&&digits[j]==k)
{ans-=1;
//System.out.println("dec");
}
//System.out.println(req+" "+k+" "+" "+ans);
}
}
System.out.println(ans);
}
public static int countDigits(int num) {
int count = 0;
if(num==0)
return 1;
while(num!=0) {
count++;
num/=10;
}
return count;
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | d7b724253afe0dfd4b7da9dd8d447700 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
String v[]=s.readLine().split(" ");
int n=Integer.parseInt(v[0]);
int k=Integer.parseInt(v[1]); long S=0;
long num[]=new long[11]; num[0]=1;
for(int i=1;i<11;i++)
num[i]=(num[i-1]*10)%k;
HashMap hs[]=new HashMap[11];
for(int i=0;i<11;i++)
hs[i]=new HashMap<Integer,Integer>();
long a[]=new long[n];
String ss[]=s.readLine().split(" ");
for(int i=0;i<n;i++)
{ a[i]=Long.parseLong(ss[i]);
for(int j=0;j<11;j++)
{
long c=num[j];
int u=(int)(((a[i]%k)*(c%k))%k);
if(hs[j].containsKey(u))
{
int r=(int)hs[j].get(u);
hs[j].put(u,r+1);
}
else
hs[j].put(u,1);
}
}
for(int i=0;i<n;i++)
{
int d=(int)((k-a[i]%k)%k);
int l=(int)Math.floor(Math.log10(a[i])+1); int r=0;
if(hs[l].containsKey(d))
r=(int)hs[l].get(d);
S+=(long)r;
long c=num[l];
int u=(int)(((a[i]%k)*(c%k))%k);
if(u==d)
S--;
}
System.out.println(S);
}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 2a45c8c54973b18e5f05c6f54b8e54a7 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes |
import java.util.*;
import java.io.*;
public class p1029D {
public static void main(String[] args) {
int n, k;
Scanner input = new Scanner(System.in);
//long tStart = System.currentTimeMillis();
//long tEnd;
String l1 = input.nextLine();
String[] l1L = l1.split(" ");
n = Integer.parseInt(l1L[0]);
k = Integer.parseInt(l1L[1]);
String l2 = input.nextLine();
String[] l2L = l2.split(" "); //a list of numbers as strings
long[] l2I = new long[l2L.length]; //store the numbers
int[] l2Idigit = new int[l2L.length]; //store the digits
long[] l2Im = new long[l2L.length];//store the a%k
@SuppressWarnings("unchecked")
HashMap<Long, Long> hMap[] = new HashMap[11];
for (int i = 1; i < 11; i++) {
hMap[i] = new HashMap<>();
}
for (int i = 0; i <= n-1; i++) {
long val = Long.parseLong(l2L[i]);
l2I[i] = val;
long valM = val %k;
l2Im[i] = valM;
int dig = (int)Math.log10(val)+1;
l2Idigit[i] = dig;
if (hMap[dig].containsKey(valM))
hMap[dig].put(valM, hMap[dig].get(valM)+1);
else
hMap[dig].put(valM, (long) 1);
}
//System.out.println("finish reading input" );
//tEnd = System.currentTimeMillis();
//System.out.println("t1 " + (tEnd - tStart));
long counter = 0;
for (int i = 0; i <= n-1; i++) {
long val = l2I[i];
for (int j=1; j<11; j++) {
val = (val * 10)%k;
long match = (k - val)% k; //a concat b = k*alpha
if (hMap[j].containsKey(match))
counter += hMap[j].get(match);
// if a concat a is a match
if (j == l2Idigit[i] && match == l2Im[i])
counter--;
}
}
input.close();
//System.out.println(counter);
//tEnd = System.currentTimeMillis();
//System.out.println(tEnd - tStart);
System.out.println(counter);
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 5cf8b5740ea285e639ad0896b6321988 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes |
import java.util.*;
import java.io.*;
public class p1029D {
public static void main(String[] args) {
int n, k;
Scanner input = new Scanner(System.in);
String l1 = input.nextLine();
String[] l1L = l1.split(" ");
n = Integer.parseInt(l1L[0]);
k = Integer.parseInt(l1L[1]);
String l2 = input.nextLine();
String[] l2L = l2.split(" "); //a list of numbers as strings
long[] l2I = new long[l2L.length]; //store the numbers
int[] l2Idigit = new int[l2L.length]; //store the digits
long[] l2Im = new long[l2L.length];//store the a%k
@SuppressWarnings("unchecked")
HashMap<Long, Long> hMap[] = new HashMap[11];
for (int i = 1; i < 11; i++) {
hMap[i] = new HashMap<>();
}
for (int i = 0; i <= n-1; i++) {
long val = Long.parseLong(l2L[i]);
l2I[i] = val;
long valM = val %k;
l2Im[i] = valM;
int dig = (int)Math.log10(val)+1;
l2Idigit[i] = dig;
if (hMap[dig].containsKey(valM))
hMap[dig].put(valM, hMap[dig].get(valM)+1);
else
hMap[dig].put(valM, (long) 1);
}
long counter = 0;
for (int i = 0; i <= n-1; i++) {
long val = l2I[i];
for (int j=1; j<11; j++) {
val = (val * 10)%k;
long match = (k - val)% k; //a concat b = k*alpha
if (hMap[j].containsKey(match))
counter += hMap[j].get(match);
// if a concat a is a match
if (j == l2Idigit[i] && match == l2Im[i])
counter--;
}
}
input.close();
System.out.println(counter);
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 66bcbc392da8661c19ef9785f70510cc | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 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.HashMap;
import java.util.InputMismatchException;
import java.util.Locale;
public class D {
public static void main(String[] args) {
Locale.setDefault(Locale.US);
InputStream inputstream = System.in;
OutputStream outputstream = System.out;
FastReader in = new FastReader(inputstream);
PrintWriter out = new PrintWriter(outputstream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
static int[] computePrefixFunction(String s) {
int arr[] = new int[s.length()];
arr[0] = 0;
int border = 0;
for (int i = 1; i < s.length(); i++) {
while (border > 0 && s.charAt(i) != s.charAt(border)) {
border = arr[border - 1];
}
if (s.charAt(i) == s.charAt(border)) {
border++;
}
arr[i] = border;
}
return arr;
}
public void solve(int testnumber, FastReader in, PrintWriter out) {
int n = in.ni();
int k = in.ni();
int arr[] = new int[n];
int len[] = new int[n];
HashMap<Integer, Integer> map[] = new HashMap[11];
long[] ten = new long[11];
ten[0] = 1;
for (int i = 1; i < 11; i++) {
ten[i] = ten[i - 1] * 10 % k;
}
for (int i = 0; i < 11; i++) {
map[i] = new HashMap<>();
}
for (int i = 0; i < n; i++) {
String s = in.ns();
len[i] = s.length();
arr[i] = Integer.parseInt(s);
map[len[i]].put(arr[i] % k,
map[len[i]].getOrDefault(arr[i] % k, 0) + 1);
}
long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < 11; j++) {
int mod = (int)((k - arr[i]*ten[j] % k) % k);
ans += map[j].getOrDefault(mod, 0);
}
if((arr[i] * ten[len[i]] + arr[i]) % k ==0){
ans--;
}
}
out.println(ans);
}
}
class FastReader {
public InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public FastReader() {
}
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 ni() {
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 ns() {
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 isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] iArr(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String next() {
return ns();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 4e52a576ff47846f9c925d71007fc9a4 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.math.BigInteger;
import java.util.*;
import static java.lang.StrictMath.pow;
public class Q506D {
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
Integer itemCount = in.nextInt();
Integer divisor = in.nextInt();
Integer[] intArr = new Integer[itemCount];
for(int i = 0; i < itemCount; i++) {
intArr[i] = in.nextInt();
}
Map<Integer, Map<Integer, Integer>> mapOfDigitSizeToMap = new HashMap<>();
for(int i = 0; i < itemCount; i++) {
Integer number = intArr[i];
Integer numberOfDigitsInNumber = String.valueOf(number).length();
Integer numberMod = number%divisor;
if(mapOfDigitSizeToMap.containsKey(numberOfDigitsInNumber)) {
Map<Integer, Integer> mapOfModToCount = mapOfDigitSizeToMap.get(numberOfDigitsInNumber);
if(mapOfModToCount.containsKey(numberMod)) {
Integer count = mapOfModToCount.get(numberMod);
count++;
mapOfModToCount.put(numberMod, count);
mapOfDigitSizeToMap.put(numberOfDigitsInNumber, mapOfModToCount);
} else {
mapOfModToCount.put(numberMod, 1);
mapOfDigitSizeToMap.put(numberOfDigitsInNumber, mapOfModToCount);
}
} else {
Map<Integer, Integer> mapOfModToCount = new HashMap<>();
mapOfModToCount.put(numberMod, 1);
mapOfDigitSizeToMap.put(numberOfDigitsInNumber, mapOfModToCount);
}
}
Long noOfPairs = Long.valueOf(0);
for(int i = 0; i < itemCount; i++) {
Integer modI = (intArr[i] % divisor);
Integer numberSize = String.valueOf(intArr[i]).length();
for(double j = 1; j<= 10; j++) {
Long tenPowerJ = (long) pow(10, j);
//Integer modFirstNumber = (Math.multiplyExact(intArr[i], tenPowerJ.intValue()) % divisor);
BigInteger tmp = BigInteger.valueOf(intArr[i]).multiply(BigInteger.valueOf(tenPowerJ));
BigInteger modFirstNumberBigInt = tmp.mod(BigInteger.valueOf(divisor));
Long modFirstNumber = modFirstNumberBigInt.longValue();
Integer modSecondNumberShort = divisor - modFirstNumber.intValue();
if(modSecondNumberShort.intValue() == divisor) {
modSecondNumberShort = 0;
}
if(mapOfDigitSizeToMap.containsKey(((int) j))){
Map<Integer, Integer> mapOfModToCount = mapOfDigitSizeToMap.get(((int) j));
if(mapOfModToCount.containsKey(modSecondNumberShort)) {
Integer modCount = mapOfModToCount.get(modSecondNumberShort);
if(
(modI.intValue() == modSecondNumberShort.intValue())
&& (numberSize.intValue() == ((int) j))
) {
noOfPairs = noOfPairs + modCount - 1;
} else {
noOfPairs = noOfPairs + modCount;
}
}
}
}
}
System.out.println(noOfPairs);
}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 17d4acf43a999bc675a4c6fb42007c82 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.util.HashMap;
import java.util.Scanner;
public class ConcatenatedMultiples {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
HashMap<Integer,Integer>[] single = new HashMap[11];
for (int p = 0; p < single.length; p++){
single[p] = new HashMap<Integer, Integer>();
}
int n =sc.nextInt();
if(n==1){
System.out.print(0);
System.exit(0);
}
int[] nums = new int[n];
int mod = sc.nextInt();
for (int k = 0; k < n; k++){
int next = sc.nextInt();
int numdig = (int)(Math.log10(next)+1);
int nextmod = next%mod;
nums[k] =next;
if (!single[numdig].containsKey(nextmod)){
single[numdig].put(nextmod,0);
}
single[numdig].put(nextmod,single[numdig].get(nextmod)+1);
}
long counter = 0;
for (int k = 0; k < n; k++){
int numdig = (int)(Math.log10(nums[k])+1);
for (int p = 1; p < 11; p++) {
long now = nums[k];
now = ((long) ((long)(now % mod) * (long)(Math.pow(10, p) % mod)) % mod);
if (now == 0){
if (single[p].containsKey(0)){
counter += (single[p].get(0));
}
if (p == numdig && nums[k]%mod== 0){
counter--;
}
}else{
if (single[p].containsKey((int)(mod-now))){
counter+=single[p].get((int)(mod-now));
}
if (p == numdig && (nums[k]%mod == mod-now)) {
counter--;
}
}
}
}
System.out.println(counter);
}
}
| Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | e3ead102f6e2f125a69e83cf479f779f | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.awt.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
public class Main implements Runnable {
int maxn = (int)3e5+111;
long n,m,k;
long a[] = new long[maxn];
void solve() throws Exception {
n = in.nextInt();
k = in.nextInt();
HashMap<BigInteger, Long> totalRemMap[] = new HashMap[12];
for (int i=0; i<=11; i++) {
totalRemMap[i] = new HashMap<>();
}
long ten[] = new long[12];
ten[0] = 1L;
for (int i=1; i<=11; i++) {
ten[i] = ten[i-1]*10L;
}
for (int i=1; i<=n; i++) {
a[i] = in.nextLong();
int len = String.valueOf(a[i]).length();
long val = a[i]%k;
totalRemMap[len].put(BigInteger.ONE.valueOf(val), totalRemMap[len].getOrDefault(BigInteger.ONE.valueOf(val), 0L) + 1L);
}
long ans = 0;
for (int i=1; i<=n; i++) {
for (int j=1; j<11; j++) {
BigInteger tempBig = BigInteger.valueOf(a[i]);
tempBig = tempBig.multiply(BigInteger.valueOf(ten[j])).mod(BigInteger.valueOf(k));
tempBig = BigInteger.valueOf(k).subtract(tempBig).mod(BigInteger.valueOf(k));
ans+=totalRemMap[j].getOrDefault(tempBig, 0L);
}
}
for (int i=1; i<=n; i++) {
int len = String.valueOf(a[i]).length();
BigInteger temp = BigInteger.valueOf(a[i]).multiply(BigInteger.valueOf(ten[len])).add(BigInteger.valueOf(a[i]));
if (temp.mod(BigInteger.valueOf(k))==BigInteger.ZERO) {
ans--;
}
}
// for (int i=1; i<=n; i++) {
// int len = String.valueOf(a[i]).length();
// long val = a[i]*ten[len]+a[i];
// if (val%k==0) ans--;
// }
out.println(ans);
}
class Pair {
int x;
int 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 | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 35688b2b5f3980fec194d7a8bc33e17b | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public void sort(long b[]){
Random rd = new Random();
for(int i=1;i<b.length;++i){
int c = rd.nextInt(i);
long v = b[c];
b[c] = b[i];
b[i] = v;
}
Arrays.sort(b);
}
public void sort(int b[]){
Random rd = new Random();
for(int i = 1;i<b.length;++i){
int c = rd.nextInt(i);
int v = b[c];
b[c]= b[i];
b[i] = v;
}
Arrays.sort(b);
}
static int groups = 0;
static int[] fa;
static int[] sz;
static void init(int n) {
groups = n;
fa = new int[n];
for (int i = 1; i < n; ++i) {
fa[i] = i;
}
sz = new int[n];
Arrays.fill(sz, 1);
}
static int root(int p) {
while (p != fa[p]) {
fa[p] = fa[fa[p]];
p = fa[p];
}
return p;
}
static void combine(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) {
return;
}
if (sz[i] < sz[j]) {
fa[i] = j;
sz[j] += sz[i];
} else {
fa[j] = i;
sz[i] += sz[j];
}
groups--;
}
class Pair {
long x;long y;
Pair(long xx,long yy){
x = xx;y= yy;
long g = gcd(Math.abs(x),Math.abs(y));
if(xx*yy>=0){
x = Math.abs(x)/g;
y = Math.abs(y)/g;
}else{
x = -Math.abs(x)/g;
y = Math.abs(y)/g;
}
}
public boolean equals(Object fk){
Pair p = (Pair)fk;
return x == p.x && y== p.y;
}
public int hashCode(){
return (int)(x*37+y);
}
}
void dfs(int g, List<Integer> go[],boolean vis[]){
vis[g] = true;
for(int u: go[g]){
int to = u;
if(!vis[to]) {
dfs(to, go, vis);
}
}
}
public static void main(String[] args) throws Exception {
new Main().run();}
TreeMap<Integer,Integer> tmp = new TreeMap<>();
void remove(int u){
int v = tmp.get(u);
if(v==1){
tmp.remove(u);
}else{
tmp.put(u,v-1);
}
}
private int len(long i) {
int ans = 0;
while (i > 0) {
i /= 10;
ans++;
}
return ans;
}
void solve(){
int n = ni();
long k = nl();
long a[] = nal(n);
long f[] = new long[11];
f[0] =1;
for(int i=1;i<=10;++i){
f[i] = f[i-1]*10L;
f[i] %= k;
}
Map<Long,Long> d[] = new HashMap[11];
for(int i=1;i<=10;++i){
d[i] = new HashMap<>();
}
long all = 0;
int le[] = new int[n];
long ck[] = new long[n];
for(int i=0;i<n;++i){
le[i] = len(a[i]);
ck[i] = (k-(a[i]%k))%k;
a[i] %= k;
for(int j=1;j<=10;++j) {
long c = (f[j] * a[i]) % k;
d[j].put(c,d[j].getOrDefault(c,0L)+1L);
}
}
for(int i=0;i<n;++i){
all += d[le[i]].getOrDefault(ck[i],0L);
}
for(int i=0;i<n;++i){
long v = ((f[le[i]]+1)*a[i])%k;
if(v==0){
all--;
}
}
println(all);
// PriorityQueue<int[]> q= new PriorityQueue<>((x,y)->{return x[0]-y[0];});
//
// q.offer(new int[]{0,0});
// boolean inq[] = new boolean[n];
// while(q.size()>0){
// int c[]= q.poll();
// for(int i=h[c[1]];i!=-1;i=ne[i]){
// if(!inq[to[i]]) {
// q.offer(new int[]{ wt[i], to[i]});
// inq[to[i]] = true;
// }
// }
// }
}
int err = 0;
void go(int rt,int pt,int fa) {
stk[rt] = pt;
for(int i=h[rt];i!=-1;i=ne[i]) {
if(to[i]==pt||to[i]==fa) continue;
go(to[i],pt,rt);
}
}
int ct = 0;
void add(int u,int v){
to[ct] = v;
ne[ct] = h[u];
h[u] = ct++;
//
// to1[ct] = u;
// wt1[ct] = w;
// ne1[ct] = h1[v];
// h1[v] = ct++;
}
int []h,ne,to,wt,qt,stk;
long inv(long a, long MOD) {
//return fpow(a, MOD-2, MOD);
return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD;
}
void inverse(){
int MAXN = 10000;
long inv[] = new long[MAXN+1];
inv[1] = 1; //注意初始化
long mod = 1000000007;
for (int i=2; i<=MAXN; ++i) {
inv[i] = (mod - mod / i) * inv[(int) (mod % i)] % mod;
}
}
// 计算某个特别大的组合数
long C(long n,long m, long MOD) {
if(m+m>n)m=n-m;
long up=1,down=1;
for(long i=0;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+1)%MOD;
}
return up*inv(down, MOD)%MOD;
}
void solve2() {
int n = ni();
String s= ns();
int g[][] = new int[3][3];
for(int i = 0;i<n;++i){
char c = s.charAt(i);
int idx = 0;
if(c=='G'){
idx =1;
}else if(c=='B'){
idx =2;
}
g[c][i%3]++;
}
}
// int []h,to,ne,wt;
long t1[];
// long t2[];
void update(long[] t,int i,long v){
for(;i<t.length;i+=(i&-i)){
t[i] += v;
}
}
long get(long[] t,int i){
long s = 0;
for(;i>0;i-=(i&-i)){
s += t[i];
}
return s;
}
int equal_bigger(long t[],long v){
int s=0,p=0;
for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) {
if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){
v -= t[p+(1<<i)];
p |= 1<<i;
}
}
return p+1;
}
long gcd(long a,long b){ return b==0?a: gcd(b,a%b);}
InputStream is;PrintWriter out;
void run() throws Exception {is = System.in;
// is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in");
out = new PrintWriter(System.out);solve();out.flush();}
private byte[] inbuf = new byte[2];
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 char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;}
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 String nline() {int b = skip();StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;}
private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;}
private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();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 << 3) + (num << 1) + (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();}}
void print(Object obj){out.print(obj);}
void println(Object obj){out.println(obj);}
void println(){out.println();}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 83231fc15806ab2796bc77960c4ed3f0 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes |
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public void sort(long b[]){
Random rd = new Random();
for(int i=1;i<b.length;++i){
int c = rd.nextInt(i);
long v = b[c];
b[c] = b[i];
b[i] = v;
}
Arrays.sort(b);
}
public void sort(int b[]){
Random rd = new Random();
for(int i = 1;i<b.length;++i){
int c = rd.nextInt(i);
int v = b[c];
b[c]= b[i];
b[i] = v;
}
Arrays.sort(b);
}
static int groups = 0;
static int[] fa;
static int[] sz;
static void init(int n) {
groups = n;
fa = new int[n];
for (int i = 1; i < n; ++i) {
fa[i] = i;
}
sz = new int[n];
Arrays.fill(sz, 1);
}
static int root(int p) {
while (p != fa[p]) {
fa[p] = fa[fa[p]];
p = fa[p];
}
return p;
}
static void combine(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) {
return;
}
if (sz[i] < sz[j]) {
fa[i] = j;
sz[j] += sz[i];
} else {
fa[j] = i;
sz[i] += sz[j];
}
groups--;
}
class Pair {
long x;long y;
Pair(long xx,long yy){
x = xx;y= yy;
long g = gcd(Math.abs(x),Math.abs(y));
if(xx*yy>=0){
x = Math.abs(x)/g;
y = Math.abs(y)/g;
}else{
x = -Math.abs(x)/g;
y = Math.abs(y)/g;
}
}
public boolean equals(Object fk){
Pair p = (Pair)fk;
return x == p.x && y== p.y;
}
public int hashCode(){
return (int)(x*37+y);
}
}
void dfs(int g, List<Integer> go[],boolean vis[]){
vis[g] = true;
for(int u: go[g]){
int to = u;
if(!vis[to]) {
dfs(to, go, vis);
}
}
}
public static void main(String[] args) throws Exception {
new Main().run();}
TreeMap<Integer,Integer> tmp = new TreeMap<>();
void remove(int u){
int v = tmp.get(u);
if(v==1){
tmp.remove(u);
}else{
tmp.put(u,v-1);
}
}
void solve(){
int n = ni();
long k = nl();
long a[] = nal(n);
long f[] = new long[11];
f[0] =1;
for(int i=1;i<=10;++i){
f[i] = f[i-1]*10L;
f[i] %= k;
}
Map<Long,Long> d[] = new HashMap[11];
for(int i=1;i<=10;++i){
d[i] = new HashMap<>();
}
long all = 0;
int le[] = new int[n];
long ck[] = new long[n];
for(int i=0;i<n;++i){
String s = ""+a[i];
le[i] = s.length();
ck[i] = (k-(a[i]%k))%k;
a[i] %= k;
for(int j=1;j<=10;++j) {
long c = (f[j] * a[i]) % k;
d[j].put(c,d[j].getOrDefault(c,0L)+1L);
}
}
for(int i=0;i<n;++i){
all += d[le[i]].getOrDefault(ck[i],0L);
}
for(int i=0;i<n;++i){
long v = ((f[le[i]]+1)*a[i])%k;
if(v==0){
all--;
}
}
println(all);
// PriorityQueue<int[]> q= new PriorityQueue<>((x,y)->{return x[0]-y[0];});
//
// q.offer(new int[]{0,0});
// boolean inq[] = new boolean[n];
// while(q.size()>0){
// int c[]= q.poll();
// for(int i=h[c[1]];i!=-1;i=ne[i]){
// if(!inq[to[i]]) {
// q.offer(new int[]{ wt[i], to[i]});
// inq[to[i]] = true;
// }
// }
// }
}
int err = 0;
void go(int rt,int pt,int fa) {
stk[rt] = pt;
for(int i=h[rt];i!=-1;i=ne[i]) {
if(to[i]==pt||to[i]==fa) continue;
go(to[i],pt,rt);
}
}
int ct = 0;
void add(int u,int v){
to[ct] = v;
ne[ct] = h[u];
h[u] = ct++;
//
// to1[ct] = u;
// wt1[ct] = w;
// ne1[ct] = h1[v];
// h1[v] = ct++;
}
int []h,ne,to,wt,qt,stk;
long inv(long a, long MOD) {
//return fpow(a, MOD-2, MOD);
return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD;
}
void inverse(){
int MAXN = 10000;
long inv[] = new long[MAXN+1];
inv[1] = 1; //注意初始化
long mod = 1000000007;
for (int i=2; i<=MAXN; ++i) {
inv[i] = (mod - mod / i) * inv[(int) (mod % i)] % mod;
}
}
// 计算某个特别大的组合数
long C(long n,long m, long MOD) {
if(m+m>n)m=n-m;
long up=1,down=1;
for(long i=0;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+1)%MOD;
}
return up*inv(down, MOD)%MOD;
}
void solve2() {
int n = ni();
String s= ns();
int g[][] = new int[3][3];
for(int i = 0;i<n;++i){
char c = s.charAt(i);
int idx = 0;
if(c=='G'){
idx =1;
}else if(c=='B'){
idx =2;
}
g[c][i%3]++;
}
}
// int []h,to,ne,wt;
long t1[];
// long t2[];
void update(long[] t,int i,long v){
for(;i<t.length;i+=(i&-i)){
t[i] += v;
}
}
long get(long[] t,int i){
long s = 0;
for(;i>0;i-=(i&-i)){
s += t[i];
}
return s;
}
int equal_bigger(long t[],long v){
int s=0,p=0;
for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) {
if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){
v -= t[p+(1<<i)];
p |= 1<<i;
}
}
return p+1;
}
long gcd(long a,long b){ return b==0?a: gcd(b,a%b);}
InputStream is;PrintWriter out;
void run() throws Exception {is = System.in;
// is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in");
out = new PrintWriter(System.out);solve();out.flush();}
private byte[] inbuf = new byte[2];
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 char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;}
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 String nline() {int b = skip();StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;}
private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;}
private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();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 << 3) + (num << 1) + (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();}}
void print(Object obj){out.print(obj);}
void println(Object obj){out.println(obj);}
void println(){out.println();}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | b8c18c6350824d273291a37bf755bc3e | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public void sort(long b[]){
Random rd = new Random();
for(int i=1;i<b.length;++i){
int c = rd.nextInt(i);
long v = b[c];
b[c] = b[i];
b[i] = v;
}
Arrays.sort(b);
}
public void sort(int b[]){
Random rd = new Random();
for(int i = 1;i<b.length;++i){
int c = rd.nextInt(i);
int v = b[c];
b[c]= b[i];
b[i] = v;
}
Arrays.sort(b);
}
static int groups = 0;
static int[] fa;
static int[] sz;
static void init(int n) {
groups = n;
fa = new int[n];
for (int i = 1; i < n; ++i) {
fa[i] = i;
}
sz = new int[n];
Arrays.fill(sz, 1);
}
static int root(int p) {
while (p != fa[p]) {
fa[p] = fa[fa[p]];
p = fa[p];
}
return p;
}
static void combine(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) {
return;
}
if (sz[i] < sz[j]) {
fa[i] = j;
sz[j] += sz[i];
} else {
fa[j] = i;
sz[i] += sz[j];
}
groups--;
}
class Pair {
long x;long y;
Pair(long xx,long yy){
x = xx;y= yy;
long g = gcd(Math.abs(x),Math.abs(y));
if(xx*yy>=0){
x = Math.abs(x)/g;
y = Math.abs(y)/g;
}else{
x = -Math.abs(x)/g;
y = Math.abs(y)/g;
}
}
public boolean equals(Object fk){
Pair p = (Pair)fk;
return x == p.x && y== p.y;
}
public int hashCode(){
return (int)(x*37+y);
}
}
void dfs(int g, List<Integer> go[],boolean vis[]){
vis[g] = true;
for(int u: go[g]){
int to = u;
if(!vis[to]) {
dfs(to, go, vis);
}
}
}
public static void main(String[] args) throws Exception {
new Main().run();}
TreeMap<Integer,Integer> tmp = new TreeMap<>();
void remove(int u){
int v = tmp.get(u);
if(v==1){
tmp.remove(u);
}else{
tmp.put(u,v-1);
}
}
private int len(long i) {
int ans = 0;
while (i > 0) {
i /= 10;
ans++;
}
return ans;
}
void solve(){
int n = ni();
long k = nl();
long a[] = nal(n);
long f[] = new long[11];
f[0] =1;
for(int i=1;i<=10;++i){
f[i] = f[i-1]*10L;
f[i] %= k;
}
Map<Long,Long> d[] = new HashMap[11];
for(int i=1;i<=10;++i){
d[i] = new HashMap<>();
}
long all = 0;
int le[] = new int[n];
long ck[] = new long[n];
for(int i=0;i<n;++i){
le[i] = len(a[i]);
ck[i] = (k-(a[i]%k))%k;
d[le[i]].put(ck[i], d[le[i]].getOrDefault(ck[i],0L)+1);
a[i] %= k;
}
for(int i=0;i<n;++i){
for(int j=1;j<=10;++j) {
long c = (f[j] * a[i]) % k;
all += d[j].getOrDefault(c,0L);
}
}
for(int i=0;i<n;++i){
long v = ((f[le[i]]+1)*a[i])%k;
if(v==0){
all--;
}
}
println(all);
// PriorityQueue<int[]> q= new PriorityQueue<>((x,y)->{return x[0]-y[0];});
//
// q.offer(new int[]{0,0});
// boolean inq[] = new boolean[n];
// while(q.size()>0){
// int c[]= q.poll();
// for(int i=h[c[1]];i!=-1;i=ne[i]){
// if(!inq[to[i]]) {
// q.offer(new int[]{ wt[i], to[i]});
// inq[to[i]] = true;
// }
// }
// }
}
int err = 0;
void go(int rt,int pt,int fa) {
stk[rt] = pt;
for(int i=h[rt];i!=-1;i=ne[i]) {
if(to[i]==pt||to[i]==fa) continue;
go(to[i],pt,rt);
}
}
int ct = 0;
void add(int u,int v){
to[ct] = v;
ne[ct] = h[u];
h[u] = ct++;
//
// to1[ct] = u;
// wt1[ct] = w;
// ne1[ct] = h1[v];
// h1[v] = ct++;
}
int []h,ne,to,wt,qt,stk;
long inv(long a, long MOD) {
//return fpow(a, MOD-2, MOD);
return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD;
}
void inverse(){
int MAXN = 10000;
long inv[] = new long[MAXN+1];
inv[1] = 1; //注意初始化
long mod = 1000000007;
for (int i=2; i<=MAXN; ++i) {
inv[i] = (mod - mod / i) * inv[(int) (mod % i)] % mod;
}
}
// 计算某个特别大的组合数
long C(long n,long m, long MOD) {
if(m+m>n)m=n-m;
long up=1,down=1;
for(long i=0;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+1)%MOD;
}
return up*inv(down, MOD)%MOD;
}
void solve2() {
int n = ni();
String s= ns();
int g[][] = new int[3][3];
for(int i = 0;i<n;++i){
char c = s.charAt(i);
int idx = 0;
if(c=='G'){
idx =1;
}else if(c=='B'){
idx =2;
}
g[c][i%3]++;
}
}
// int []h,to,ne,wt;
long t1[];
// long t2[];
void update(long[] t,int i,long v){
for(;i<t.length;i+=(i&-i)){
t[i] += v;
}
}
long get(long[] t,int i){
long s = 0;
for(;i>0;i-=(i&-i)){
s += t[i];
}
return s;
}
int equal_bigger(long t[],long v){
int s=0,p=0;
for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) {
if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){
v -= t[p+(1<<i)];
p |= 1<<i;
}
}
return p+1;
}
long gcd(long a,long b){ return b==0?a: gcd(b,a%b);}
InputStream is;PrintWriter out;
void run() throws Exception {is = System.in;
// is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in");
out = new PrintWriter(System.out);solve();out.flush();}
private byte[] inbuf = new byte[2];
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 char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;}
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 String nline() {int b = skip();StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;}
private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;}
private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();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 << 3) + (num << 1) + (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();}}
void print(Object obj){out.print(obj);}
void println(Object obj){out.println(obj);}
void println(){out.println();}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 5ef8c8043f1500d427421b155d01cb7d | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public void sort(long b[]){
Random rd = new Random();
for(int i=1;i<b.length;++i){
int c = rd.nextInt(i);
long v = b[c];
b[c] = b[i];
b[i] = v;
}
Arrays.sort(b);
}
public void sort(int b[]){
Random rd = new Random();
for(int i = 1;i<b.length;++i){
int c = rd.nextInt(i);
int v = b[c];
b[c]= b[i];
b[i] = v;
}
Arrays.sort(b);
}
static int groups = 0;
static int[] fa;
static int[] sz;
static void init(int n) {
groups = n;
fa = new int[n];
for (int i = 1; i < n; ++i) {
fa[i] = i;
}
sz = new int[n];
Arrays.fill(sz, 1);
}
static int root(int p) {
while (p != fa[p]) {
fa[p] = fa[fa[p]];
p = fa[p];
}
return p;
}
static void combine(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) {
return;
}
if (sz[i] < sz[j]) {
fa[i] = j;
sz[j] += sz[i];
} else {
fa[j] = i;
sz[i] += sz[j];
}
groups--;
}
class Pair {
long x;long y;
Pair(long xx,long yy){
x = xx;y= yy;
long g = gcd(Math.abs(x),Math.abs(y));
if(xx*yy>=0){
x = Math.abs(x)/g;
y = Math.abs(y)/g;
}else{
x = -Math.abs(x)/g;
y = Math.abs(y)/g;
}
}
public boolean equals(Object fk){
Pair p = (Pair)fk;
return x == p.x && y== p.y;
}
public int hashCode(){
return (int)(x*37+y);
}
}
void dfs(int g, List<Integer> go[],boolean vis[]){
vis[g] = true;
for(int u: go[g]){
int to = u;
if(!vis[to]) {
dfs(to, go, vis);
}
}
}
public static void main(String[] args) throws Exception {
new Main().run();}
TreeMap<Integer,Integer> tmp = new TreeMap<>();
void remove(int u){
int v = tmp.get(u);
if(v==1){
tmp.remove(u);
}else{
tmp.put(u,v-1);
}
}
private int len(long i) {
int ans = 0;
while (i > 0) {
i /= 10;
ans++;
}
return ans;
}
void solve(){
int n = ni();
long k = nl();
long a[] = nal(n);
long f[] = new long[11];
f[0] =1;
for(int i=1;i<=10;++i){
f[i] = f[i-1]*10L;
f[i] %= k;
}
Map<Long,Long> d[] = new HashMap[11];
for(int i=1;i<=10;++i){
d[i] = new HashMap<>();
}
long all = 0;
int le[] = new int[n];
for(int i=0;i<n;++i){
le[i] = len(a[i]);
a[i] %= k;
d[le[i]].merge(a[i], 1L, (prev, one) -> prev + one);
}
for(int i=0;i<n;++i){
for(int j=1;j<=10;++j) {
long c = (f[j] * a[i]) % k;
long c1 = (k-c)%k;
all += d[j].getOrDefault(c1,0L);
if(j==le[i]&&c1==a[i]){
all--;
}
}
}
println(all);
// PriorityQueue<int[]> q= new PriorityQueue<>((x,y)->{return x[0]-y[0];});
//
// q.offer(new int[]{0,0});
// boolean inq[] = new boolean[n];
// while(q.size()>0){
// int c[]= q.poll();
// for(int i=h[c[1]];i!=-1;i=ne[i]){
// if(!inq[to[i]]) {
// q.offer(new int[]{ wt[i], to[i]});
// inq[to[i]] = true;
// }
// }
// }
}
int err = 0;
void go(int rt,int pt,int fa) {
stk[rt] = pt;
for(int i=h[rt];i!=-1;i=ne[i]) {
if(to[i]==pt||to[i]==fa) continue;
go(to[i],pt,rt);
}
}
int ct = 0;
void add(int u,int v){
to[ct] = v;
ne[ct] = h[u];
h[u] = ct++;
//
// to1[ct] = u;
// wt1[ct] = w;
// ne1[ct] = h1[v];
// h1[v] = ct++;
}
int []h,ne,to,wt,qt,stk;
long inv(long a, long MOD) {
//return fpow(a, MOD-2, MOD);
return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD;
}
void inverse(){
int MAXN = 10000;
long inv[] = new long[MAXN+1];
inv[1] = 1; //注意初始化
long mod = 1000000007;
for (int i=2; i<=MAXN; ++i) {
inv[i] = (mod - mod / i) * inv[(int) (mod % i)] % mod;
}
}
// 计算某个特别大的组合数
long C(long n,long m, long MOD) {
if(m+m>n)m=n-m;
long up=1,down=1;
for(long i=0;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+1)%MOD;
}
return up*inv(down, MOD)%MOD;
}
void solve2() {
int n = ni();
String s= ns();
int g[][] = new int[3][3];
for(int i = 0;i<n;++i){
char c = s.charAt(i);
int idx = 0;
if(c=='G'){
idx =1;
}else if(c=='B'){
idx =2;
}
g[c][i%3]++;
}
}
// int []h,to,ne,wt;
long t1[];
// long t2[];
void update(long[] t,int i,long v){
for(;i<t.length;i+=(i&-i)){
t[i] += v;
}
}
long get(long[] t,int i){
long s = 0;
for(;i>0;i-=(i&-i)){
s += t[i];
}
return s;
}
int equal_bigger(long t[],long v){
int s=0,p=0;
for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) {
if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){
v -= t[p+(1<<i)];
p |= 1<<i;
}
}
return p+1;
}
long gcd(long a,long b){ return b==0?a: gcd(b,a%b);}
InputStream is;PrintWriter out;
void run() throws Exception {is = System.in;
// is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in");
out = new PrintWriter(System.out);solve();out.flush();}
private byte[] inbuf = new byte[2];
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 char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;}
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 String nline() {int b = skip();StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;}
private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;}
private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();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 << 3) + (num << 1) + (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();}}
void print(Object obj){out.print(obj);}
void println(Object obj){out.println(obj);}
void println(){out.println();}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | ee21d5ec775bd81885ad7d3600c5765c | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes |
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public void sort(long b[]){
Random rd = new Random();
for(int i=1;i<b.length;++i){
int c = rd.nextInt(i);
long v = b[c];
b[c] = b[i];
b[i] = v;
}
Arrays.sort(b);
}
public void sort(int b[]){
Random rd = new Random();
for(int i = 1;i<b.length;++i){
int c = rd.nextInt(i);
int v = b[c];
b[c]= b[i];
b[i] = v;
}
Arrays.sort(b);
}
static int groups = 0;
static int[] fa;
static int[] sz;
static void init(int n) {
groups = n;
fa = new int[n];
for (int i = 1; i < n; ++i) {
fa[i] = i;
}
sz = new int[n];
Arrays.fill(sz, 1);
}
static int root(int p) {
while (p != fa[p]) {
fa[p] = fa[fa[p]];
p = fa[p];
}
return p;
}
static void combine(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) {
return;
}
if (sz[i] < sz[j]) {
fa[i] = j;
sz[j] += sz[i];
} else {
fa[j] = i;
sz[i] += sz[j];
}
groups--;
}
class Pair {
long x;long y;
Pair(long xx,long yy){
x = xx;y= yy;
long g = gcd(Math.abs(x),Math.abs(y));
if(xx*yy>=0){
x = Math.abs(x)/g;
y = Math.abs(y)/g;
}else{
x = -Math.abs(x)/g;
y = Math.abs(y)/g;
}
}
public boolean equals(Object fk){
Pair p = (Pair)fk;
return x == p.x && y== p.y;
}
public int hashCode(){
return (int)(x*37+y);
}
}
void dfs(int g, List<Integer> go[],boolean vis[]){
vis[g] = true;
for(int u: go[g]){
int to = u;
if(!vis[to]) {
dfs(to, go, vis);
}
}
}
public static void main(String[] args) throws Exception {
new Main().run();}
TreeMap<Integer,Integer> tmp = new TreeMap<>();
void remove(int u){
int v = tmp.get(u);
if(v==1){
tmp.remove(u);
}else{
tmp.put(u,v-1);
}
}
void solve(){
int n = ni();
long k = nl();
long a[] = nal(n);
long f[] = new long[11];
f[0] =1;
for(int i=1;i<=10;++i){
f[i] = f[i-1]*10L;
f[i] %= k;
}
Map<Long,Long> d[] = new HashMap[11];
for(int i=1;i<=10;++i){
d[i] = new HashMap<>();
}
long all = 0;
int le[] = new int[n];
long ck[] = new long[n];
long vv[][] = new long[n][11];
for(int i=0;i<n;++i){
String s = ""+a[i];
int l = s.length();
le[i] = l;
ck[i] = (k-(a[i]%k))%k;
for(int j=1;j<=10;++j) {
vv[i][j] = (f[j] * (a[i]%k)) % k;
d[j].put(vv[i][j],d[j].getOrDefault(vv[i][j],0L)+1L);
}
}
for(int i=0;i<n;++i){
all += d[le[i]].getOrDefault(ck[i],0L);
}
for(int i=0;i<n;++i){
long v = (f[le[i]]*a[i] +a[i])%k;
if(v==0){
all--;
}
}
println(all);
// PriorityQueue<int[]> q= new PriorityQueue<>((x,y)->{return x[0]-y[0];});
//
// q.offer(new int[]{0,0});
// boolean inq[] = new boolean[n];
// while(q.size()>0){
// int c[]= q.poll();
// for(int i=h[c[1]];i!=-1;i=ne[i]){
// if(!inq[to[i]]) {
// q.offer(new int[]{ wt[i], to[i]});
// inq[to[i]] = true;
// }
// }
// }
}
int err = 0;
void go(int rt,int pt,int fa) {
stk[rt] = pt;
for(int i=h[rt];i!=-1;i=ne[i]) {
if(to[i]==pt||to[i]==fa) continue;
go(to[i],pt,rt);
}
}
int ct = 0;
void add(int u,int v){
to[ct] = v;
ne[ct] = h[u];
h[u] = ct++;
//
// to1[ct] = u;
// wt1[ct] = w;
// ne1[ct] = h1[v];
// h1[v] = ct++;
}
int []h,ne,to,wt,qt,stk;
long inv(long a, long MOD) {
//return fpow(a, MOD-2, MOD);
return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD;
}
void inverse(){
int MAXN = 10000;
long inv[] = new long[MAXN+1];
inv[1] = 1; //注意初始化
long mod = 1000000007;
for (int i=2; i<=MAXN; ++i) {
inv[i] = (mod - mod / i) * inv[(int) (mod % i)] % mod;
}
}
// 计算某个特别大的组合数
long C(long n,long m, long MOD) {
if(m+m>n)m=n-m;
long up=1,down=1;
for(long i=0;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+1)%MOD;
}
return up*inv(down, MOD)%MOD;
}
void solve2() {
int n = ni();
String s= ns();
int g[][] = new int[3][3];
for(int i = 0;i<n;++i){
char c = s.charAt(i);
int idx = 0;
if(c=='G'){
idx =1;
}else if(c=='B'){
idx =2;
}
g[c][i%3]++;
}
}
// int []h,to,ne,wt;
long t1[];
// long t2[];
void update(long[] t,int i,long v){
for(;i<t.length;i+=(i&-i)){
t[i] += v;
}
}
long get(long[] t,int i){
long s = 0;
for(;i>0;i-=(i&-i)){
s += t[i];
}
return s;
}
int equal_bigger(long t[],long v){
int s=0,p=0;
for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) {
if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){
v -= t[p+(1<<i)];
p |= 1<<i;
}
}
return p+1;
}
long gcd(long a,long b){ return b==0?a: gcd(b,a%b);}
InputStream is;PrintWriter out;
void run() throws Exception {is = System.in;
// is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in");
out = new PrintWriter(System.out);solve();out.flush();}
private byte[] inbuf = new byte[2];
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 char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;}
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 String nline() {int b = skip();StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;}
private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;}
private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();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 << 3) + (num << 1) + (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();}}
void print(Object obj){out.print(obj);}
void println(Object obj){out.println(obj);}
void println(){out.println();}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 3c77e795749c85f583176f6943148ce8 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public void sort(long b[]){
Random rd = new Random();
for(int i=1;i<b.length;++i){
int c = rd.nextInt(i);
long v = b[c];
b[c] = b[i];
b[i] = v;
}
Arrays.sort(b);
}
public void sort(int b[]){
Random rd = new Random();
for(int i = 1;i<b.length;++i){
int c = rd.nextInt(i);
int v = b[c];
b[c]= b[i];
b[i] = v;
}
Arrays.sort(b);
}
static int groups = 0;
static int[] fa;
static int[] sz;
static void init(int n) {
groups = n;
fa = new int[n];
for (int i = 1; i < n; ++i) {
fa[i] = i;
}
sz = new int[n];
Arrays.fill(sz, 1);
}
static int root(int p) {
while (p != fa[p]) {
fa[p] = fa[fa[p]];
p = fa[p];
}
return p;
}
static void combine(int p, int q) {
int i = root(p);
int j = root(q);
if (i == j) {
return;
}
if (sz[i] < sz[j]) {
fa[i] = j;
sz[j] += sz[i];
} else {
fa[j] = i;
sz[i] += sz[j];
}
groups--;
}
class Pair {
long x;long y;
Pair(long xx,long yy){
x = xx;y= yy;
long g = gcd(Math.abs(x),Math.abs(y));
if(xx*yy>=0){
x = Math.abs(x)/g;
y = Math.abs(y)/g;
}else{
x = -Math.abs(x)/g;
y = Math.abs(y)/g;
}
}
public boolean equals(Object fk){
Pair p = (Pair)fk;
return x == p.x && y== p.y;
}
public int hashCode(){
return (int)(x*37+y);
}
}
void dfs(int g, List<Integer> go[],boolean vis[]){
vis[g] = true;
for(int u: go[g]){
int to = u;
if(!vis[to]) {
dfs(to, go, vis);
}
}
}
public static void main(String[] args) throws Exception {
new Main().run();}
TreeMap<Integer,Integer> tmp = new TreeMap<>();
void remove(int u){
int v = tmp.get(u);
if(v==1){
tmp.remove(u);
}else{
tmp.put(u,v-1);
}
}
private int len(long i) {
int ans = 0;
while (i > 0) {
i /= 10;
ans++;
}
return ans;
}
void solve(){
int n = ni();
long k = nl();
long a[] = nal(n);
long f[] = new long[11];
f[0] =1;
for(int i=1;i<=10;++i){
f[i] = f[i-1]*10L;
f[i] %= k;
}
Map<Long,Long> d[] = new HashMap[11];
for(int i=1;i<=10;++i){
d[i] = new HashMap<>();
}
long all = 0;
int le[] = new int[n];
long ck[] = new long[n];
for(int i=0;i<n;++i){
le[i] = len(a[i]);
a[i] %= k;
d[le[i]].put(a[i], d[le[i]].getOrDefault(a[i],0L)+1);
}
for(int i=0;i<n;++i){
for(int j=1;j<=10;++j) {
long c = (f[j] * a[i]) % k;
long c1 = (k-c)%k;
all += d[j].getOrDefault(c1,0L);
if(j==le[i]&&c1==a[i]){
all--;
}
}
}
println(all);
// PriorityQueue<int[]> q= new PriorityQueue<>((x,y)->{return x[0]-y[0];});
//
// q.offer(new int[]{0,0});
// boolean inq[] = new boolean[n];
// while(q.size()>0){
// int c[]= q.poll();
// for(int i=h[c[1]];i!=-1;i=ne[i]){
// if(!inq[to[i]]) {
// q.offer(new int[]{ wt[i], to[i]});
// inq[to[i]] = true;
// }
// }
// }
}
int err = 0;
void go(int rt,int pt,int fa) {
stk[rt] = pt;
for(int i=h[rt];i!=-1;i=ne[i]) {
if(to[i]==pt||to[i]==fa) continue;
go(to[i],pt,rt);
}
}
int ct = 0;
void add(int u,int v){
to[ct] = v;
ne[ct] = h[u];
h[u] = ct++;
//
// to1[ct] = u;
// wt1[ct] = w;
// ne1[ct] = h1[v];
// h1[v] = ct++;
}
int []h,ne,to,wt,qt,stk;
long inv(long a, long MOD) {
//return fpow(a, MOD-2, MOD);
return a==1?1:(long )(MOD-MOD/a)*inv(MOD%a, MOD)%MOD;
}
void inverse(){
int MAXN = 10000;
long inv[] = new long[MAXN+1];
inv[1] = 1; //注意初始化
long mod = 1000000007;
for (int i=2; i<=MAXN; ++i) {
inv[i] = (mod - mod / i) * inv[(int) (mod % i)] % mod;
}
}
// 计算某个特别大的组合数
long C(long n,long m, long MOD) {
if(m+m>n)m=n-m;
long up=1,down=1;
for(long i=0;i<m;i++)
{
up=up*(n-i)%MOD;
down=down*(i+1)%MOD;
}
return up*inv(down, MOD)%MOD;
}
void solve2() {
int n = ni();
String s= ns();
int g[][] = new int[3][3];
for(int i = 0;i<n;++i){
char c = s.charAt(i);
int idx = 0;
if(c=='G'){
idx =1;
}else if(c=='B'){
idx =2;
}
g[c][i%3]++;
}
}
// int []h,to,ne,wt;
long t1[];
// long t2[];
void update(long[] t,int i,long v){
for(;i<t.length;i+=(i&-i)){
t[i] += v;
}
}
long get(long[] t,int i){
long s = 0;
for(;i>0;i-=(i&-i)){
s += t[i];
}
return s;
}
int equal_bigger(long t[],long v){
int s=0,p=0;
for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) {
if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){
v -= t[p+(1<<i)];
p |= 1<<i;
}
}
return p+1;
}
long gcd(long a,long b){ return b==0?a: gcd(b,a%b);}
InputStream is;PrintWriter out;
void run() throws Exception {is = System.in;
// is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in");
out = new PrintWriter(System.out);solve();out.flush();}
private byte[] inbuf = new byte[2];
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 char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;}
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 String nline() {int b = skip();StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); }
return sb.toString();}
private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;}
private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;}
private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();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 << 3) + (num << 1) + (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();}}
void print(Object obj){out.print(obj);}
void println(Object obj){out.println(obj);}
void println(){out.println();}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 090d43e6efefd704029975b308dd6b20 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
import java.util.Map.Entry;
public class Main
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private PrintWriter pw;
private long mod = 1000000000 + 7;
private StringBuilder ans_sb;
private void soln()
{
int n = nextInt();
long k = nextInt();
HashMap<Long, Integer>[] map = new HashMap[10];
long[] arr = new long[n];
int[] len1 = new int[n];
for (int i = 0; i < 10; i++)
map[i] = new HashMap<>();
for (int i = 0; i < n; i++) {
long x = nextInt();
long xx = x % k;
arr[i] = xx;
int len = 0;
while (x != 0) {
x /= 10;
len++;
}
len1[i] = len;
if (map[len - 1].containsKey(xx))
map[len - 1].put(xx, map[len - 1].get(xx) + 1);
else
map[len - 1].put(xx, 1);
}
// debug(map);
long[] ps = new long[10];
ps[0] = 10;
for (int i = 1; i < 10; i++) {
ps[i] = ((10L) * ps[i - 1]) % k;
}
// debug(ps);
// debug(len1);
long cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 10; j++) {
long vv = (arr[i] * ps[j]) % k;
long mo = (k - vv) % k;
// debug(mo+" "+arr[i]+" "+j);
if (map[j].containsKey(mo)) {
// debug("in");
if (arr[i] == mo && len1[i] == j + 1) {
long cc = map[j].get(mo);
cnt += (cc - 1);
} else
cnt += (long) map[j].get(mo);
}
}
// debug(cnt);
}
pw.println(cnt);
}
private String solveEqn(long a, long b)
{
long x = 0, y = 1, lastx = 1, lasty = 0, temp;
while (b != 0) {
long q = a / b;
long r = a % b;
a = b;
b = r;
temp = x;
x = lastx - q * x;
lastx = temp;
temp = y;
y = lasty - q * y;
lasty = temp;
}
return lastx + " " + lasty;
}
private void debug(Object... o)
{
System.out.println(Arrays.deepToString(o));
}
private long pow(long a, long b, long c)
{
if (b == 0)
return 1;
long p = pow(a, b / 2, c);
p = (p * p) % c;
return (b % 2 == 0) ? p : (a * p) % c;
}
private long gcd(long n, long l)
{
if (l == 0)
return n;
return gcd(l, n % l);
}
public static void main(String[] args) throws Exception
{
new Thread(null, new Runnable()
{
@Override
public void run()
{
new Main().solve();
}
}, "1", 1 << 26).start();
// new Main().solve();
}
public StringBuilder solve()
{
InputReader(System.in);
/*
* try { InputReader(new FileInputStream("C:\\Users\\hardik\\Desktop\\in.txt"));
* } catch(FileNotFoundException e) {}
*/
pw = new PrintWriter(System.out);
// ans_sb = new StringBuilder();
soln();
pw.close();
// System.out.println(ans_sb);
return ans_sb;
}
public void InputReader(InputStream stream1)
{
stream = stream1;
}
private boolean isWhitespace(int c)
{
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
private 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++];
}
private 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;
}
private 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;
}
private String nextToken()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private 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();
}
private int[] nextIntArray(int n)
{
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
private long[] nextLongArray(int n)
{
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private void pArray(int[] arr)
{
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
private void pArray(long[] arr)
{
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
return;
}
private boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
private char nextChar()
{
int c = read();
while (isSpaceChar(c))
c = read();
char c1 = (char) c;
while (!isSpaceChar(c))
c = read();
return c1;
}
private interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | 2558d0f2c9d0aebe7e1de0ddffbc5380 | train_002.jsonl | 1535122200 | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers.Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$.Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigDecimal;
public class R506D {
public static void main (String[] args) throws java.lang.Exception {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = in.nextInt();long k = in.nextInt();
HashMap<Long, Integer>[] mat = new HashMap[11];
for (int i = 0; i < 11; i++)
mat[i] = new HashMap<>();
long[] a = new long[n];
int[] d = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
long temp = a[i];int x = 0;
while (temp > 0) {
temp /= 10;
x++;
}
d[i] = x;
if (!mat[x].containsKey(a[i] % k))
mat[x].put(a[i] % k, 0);
mat[x].put(a[i] % k, mat[x].get(a[i] % k) + 1);
}
long[] pro = new long[11];
pro[1] = 10;
for (int i = 2; i < 11; i++) {
pro[i] = (pro[i - 1] * 10L) % k;
}
long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < 11; j++) {
long have = ((a[i] % k) * (pro[j] % k)) % k;
long req = (k - (have % k)) % k;
if (mat[j].containsKey(req)) {
ans += mat[j].get(req);
if (d[i] == j && a[i] % k == req)
ans--;
}
}
}
w.println(ans);
w.close();
}
static int ceil(int a, int b) {
if (a < 0)
return 0;
return (a - 1 + b) / b;
}
static boolean nextPermutation(int[] a) {
int n = a.length;
int ptr = n - 1;
while (ptr > 0 && a[ptr - 1] >= a[ptr]) {
ptr--;
}
for (int i = ptr, j = n - 1; i < j; i++, j--) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
if (ptr == 0) {
return false;
}
for (int i = ptr;; i++) {
if (a[ptr - 1] < a[i]) {
int tmp = a[ptr - 1];
a[ptr - 1] = a[i];
a[i] = tmp;
return true;
}
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public void skip(int x) {
while (x-- > 0)
read();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextString() {
return next();
}
public String next() {
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 String nextLine() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
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 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 boolean hasNext() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value != -1;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | Java | ["6 11\n45 1 10 12 11 7", "4 2\n2 78 4 10", "5 2\n3 7 19 3 3"] | 2 seconds | ["7", "12", "0"] | NoteIn the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$.In the second example all $$$n(n - 1)$$$ pairs suffice.In the third example no pair is sufficient. | Java 8 | standard input | [
"implementation",
"math"
] | 1eb41e764a4248744edce6a9e7e3517a | The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$2 \le k \le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). | 1,900 | Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i \neq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. | standard output | |
PASSED | e9fc60c048459910cfc20f1388653f0b | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C
{
static ArrayList<Integer> g[];
static int[] vis,size;
public static void main(String[] args)throws IOException
{
FastReader f=new FastReader();
StringBuffer sb=new StringBuffer();
int n=f.nextInt();
g=new ArrayList[n+1];
for(int i=1;i<=n;i++)
g[i]=new ArrayList<>();
vis=new int[n+1];
size=new int[n+1];
for(int i=1;i<n;i++)
{
int x=f.nextInt();
int y=f.nextInt();
g[x].add(y);
g[y].add(x);
}
if(n%2!=0)
{
System.out.println("-1");
return ;
}
dfs(1);
int count=-1;
for(int i=1;i<=n;i++)
{
if(size[i]%2==0)
count++;
// System.out.print(size[i]+" ");
}
System.out.println(count);
}
static void dfs(int n)
{
vis[n]=1;
size[n]=1;
for(int child : g[n])
{
if(vis[child]==0)
{
dfs(child);
size[n]+=size[child];
}
}
}
}
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 | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | a7d8ced30f646665e586b50e6ebc0eb0 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.util.*;
import java.io.*;
public class _982_C_CutemAll {
public static void main(String[] args) throws IOException {
int N = readInt(); if(N%2 == 1) {println(-1); exit();}
ArrayList<Integer> graph[] = new ArrayList[N+1]; for(int i = 1; i<=N; i++) graph[i] = new ArrayList<>();
for(int i = 1; i<N; i++) { int a = readInt(), b = readInt(); graph[a].add(b); graph[b].add(a);}
int sz[] = new int[N+1]; dfs(1, 0, sz, graph);
int cnt = -1; for(int i = 1; i<=N; i++) cnt += (sz[i]%2 == 0)? 1 : 0; println(cnt);
exit();
}
public static void dfs(int n, int p, int sz[], ArrayList<Integer> graph[]) {
sz[n] = 1; for(int e : graph[n]) if (e != p) {dfs(e, n, sz, graph); sz[n] += sz[e];}
}
final private static int BUFFER_SIZE = 1 << 16;
private static DataInputStream din = new DataInputStream(System.in);
private static byte[] buffer = new byte[BUFFER_SIZE];
private static int bufferPointer = 0, bytesRead = 0;
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = Read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public static String read() throws IOException {
byte[] ret = new byte[1024];
int idx = 0;
byte c = Read();
while (c <= ' ') {
c = Read();
}
do {
ret[idx++] = c;
c = Read();
} while (c != -1 && c != ' ' && c != '\n' && c != '\r');
return new String(ret, 0, idx);
}
public static int readInt() throws IOException {
int ret = 0;
byte c = Read();
while (c <= ' ')
c = Read();
boolean neg = (c == '-');
if (neg)
c = Read();
do {
ret = ret * 10 + c - '0';
} while ((c = Read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public static long readLong() throws IOException {
long ret = 0;
byte c = Read();
while (c <= ' ')
c = Read();
boolean neg = (c == '-');
if (neg)
c = Read();
do {
ret = ret * 10 + c - '0';
} while ((c = Read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public static double readDouble() throws IOException {
double ret = 0, div = 1;
byte c = Read();
while (c <= ' ')
c = Read();
boolean neg = (c == '-');
if (neg)
c = Read();
do {
ret = ret * 10 + c - '0';
} while ((c = Read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = Read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private static void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private static byte Read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
static void print(Object o) {
pr.print(o);
}
static void println(Object o) {
pr.println(o);
}
static void flush() {
pr.flush();
}
static void println() {
pr.println();
}
static void exit() throws IOException {
din.close();
pr.close();
System.exit(0);
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | b0920d12cc49d1ad924396b045659b1e | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CutEmAll {
static int[] num;
static int rc;
static List<Integer>[] adj;
public static void main(String[] args) throws IOException{
//IO********************************************************************************
//**********************************************************************************
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String s = br.readLine();
int n = Integer.parseInt(s);
adj = new ArrayList[n];
for(int j = 0; j<n; j++) adj[j] = new ArrayList<>();
for (int i = 0; i<n-1; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
Integer u = Integer.valueOf(st.nextToken());
Integer v = Integer.valueOf(st.nextToken());
adj[u-1].add(v-1);
adj[v-1].add(u-1);
}
//**********************************************************************************
//**********************************************************************************
if (n%2 == 1) System.out.println(-1);
else {
num = new int[n];
rc = 0;
dFS(0, -1);
int res = 0;
for (int i = 0; i<n; i++) {
if (num[i]%2 == 0) res++;
}
System.out.println(res-1);
}
}
static void dFS(Integer u, Integer parent) {
num[u] = rc;
for (Integer nb: adj[u]) {
if (!nb.equals(parent)) {
dFS(nb, u);
}
}
num[u] = rc - num[u] + 1;
rc++;
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | ddab17a0f55e9b35c1e4e6fb6327b5f2 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CutEmAll {
static int[] num;
static int rc;
static List<Integer>[] adj;
//static int[] parents;
public static void main(String[] args) throws IOException{
//IO********************************************************************************
//**********************************************************************************
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String s = br.readLine();
int n = Integer.parseInt(s);
adj = new ArrayList[n];
for(int j = 0; j<n; j++) adj[j] = new ArrayList<>();
for (int i = 0; i<n-1; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
Integer u = Integer.valueOf(st.nextToken());
Integer v = Integer.valueOf(st.nextToken());
adj[u-1].add(v-1);
adj[v-1].add(u-1);
}
//**********************************************************************************
//**********************************************************************************
if (n%2 == 1) System.out.println(-1);
else {
num = new int[n];
rc = 0;
//parents = new int[n];
dFS(0, -1);
int res = 0;
for (int i = 0; i<n; i++) {
if (num[i]%2 == 0) res++;
}
/*
while (true) {
int idx = 0;
int min = Integer.MAX_VALUE;
for (int i = idx; i<n; i++) {
if(num[i] < min && num[i]>0 && num[i]%2 == 0) {
idx = i;
min = num[idx];
}
}
if (min == Integer.MAX_VALUE) break;
num[idx] = 0;
//des.replace(idx, 0);
int temp = parents[idx];
while (temp != -1) {
num[temp] -= min;
temp = parents[temp];
}
res++;
}
*/
System.out.println(res-1);
}
}
static void dFS(Integer u, Integer parent) {
num[u] = rc;
//parents[u] = parent;
for (Integer nb: adj[u]) {
if (!nb.equals(parent)) {
dFS(nb, u);
}
}
num[u] = rc - num[u] + 1;
rc++;
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | 24efda9ce544793b2785cce813408d14 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | //package Mura;
/***** BY MURAD ******/
import com.sun.javafx.collections.MappingChange;
import com.sun.org.apache.xerces.internal.util.SymbolHash;
import javafx.util.Pair;
import java.io.*;
import java.math.BigInteger;
import java.security.PublicKey;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import static java.lang.Integer.min;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.BitSet;
public class Code {
public static boolean []visited=new boolean[100005];
public static int ans=0;
public static LinkedList<Integer>q=new LinkedList<>();
public static long GCD(long x,long y){
if(y==0)
return x;
else
return GCD(y,x%y);
}
public static int max3( int a, int b, int c )
{
int x = a > b ? a : b;
return x > c ? x : c;
}
static class Graph {
private int v;
private ArrayList<Integer> adj[];
Graph(int vv) {
v = vv;
adj = new ArrayList[v];
for (int i = 0; i < v; ++i) {
adj[i] = new ArrayList<>();
}
}
void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
}
int dfs(int u,int parent)
{
visited[u] = true;
int res = 1;
for(Integer v : adj[u])
{
if (!visited[v])
{
res+=dfs(v,u);
}
}
if (res%2 == 0)
ans++;
return res;
}
}
public static void main(String[] args)throws IOException {
//Solution ob=new Solution();
Scanner inp = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
Graph g=new Graph(100005);
for(int i=0;i<n-1;i++)
{
st=new StringTokenizer(br.readLine());
int x=Integer.parseInt(st.nextToken()),y=Integer.parseInt(st.nextToken());
g.addEdge(x,y);
}
if(n%2!=0)
{
System.out.println(-1);
return;
}
g.dfs(1,-1);
System.out.println(ans-1);
}
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());
}
}
static class Friend implements Comparable<Friend>{
int m;
int f;
public Friend(int m, int f){
this.m = m;
this.f = f;
}
@Override
public int compareTo(Friend f) {
return this.m - f.m;
}
}
public static int[] bctsort(int[] array, int bucketCount) {
if (bucketCount <= 0) throw new IllegalArgumentException("Invalid bucket count");
if (array.length <= 1) return array;
int high = array[0];
int low = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > high) high = array[i];
if (array[i] < low) low = array[i];
}
double interval = ((double)(high - low + 1))/bucketCount;
ArrayList<Integer> buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) {
buckets[i] = new ArrayList();
}
for (int i = 0; i < array.length; i++) {
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}
int pointer = 0;
for (int i = 0; i < buckets.length; i++) {
Collections.sort(buckets[i]);
for (int j = 0; j < buckets[i].size(); j++) {
array[pointer] = buckets[i].get(j);
pointer++;
}
}
return array;
}
/*
int maxValueInMap=(Collections.max(mp.values())); // This will return max value in the Hashmap
for (Map.Entry<String, Integer> entry : mp.entrySet()) { // Itrate through hashmap
if (entry.getValue()==maxValueInMap) {
System.out.println(entry.getKey());
break;
}
}
//vector of vector
ArrayList <Integer> al [] = (ArrayList <Integer> []) new ArrayList[100001];
Relatively Prime :- if diffrence between two number is equal to 1
*/
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | bd74b27dabf75294d2b2228704601248 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | //package com.himanshu.practice.july22.hour7;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
/**
* Created by himanshubhardwaj on 22/07/18.
*/
public class CutThemAll {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Tree tree = new Tree(n);
for (int i = 0; i < (n - 1); i++) {
String[] s = br.readLine().split(" ");
tree.insert(Integer.parseInt(s[0]), Integer.parseInt(s[1]));
}
if (n % 2 == 1) {
System.out.println(-1);
return;
}
tree.DFS(0, -1);
System.out.print(tree.deletedEdges);
}
}
class Tree {
LinkedList<Integer> edj[];
int numNodes;
int deletedEdges = 0;
public Tree(int nuNodes) {
edj = new LinkedList[nuNodes];
for (int i = 0; i < nuNodes; i++) {
edj[i] = new LinkedList<>();
}
this.numNodes = nuNodes;
}
public void insert(int source, int destination) {
source--;
destination--;
edj[source].addLast(destination);
edj[destination].addLast(source);
}
public int DFS(int node, int parent) {
int numChild = 1;
for (int neighbour : edj[node]) {
if (neighbour != parent) {
int subRootChildren = DFS(neighbour, node);
if (subRootChildren % 2 == 0) {
deletedEdges++;
}
numChild += subRootChildren;
}
}
return numChild;
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | d19f03dd805469dcba95012ba841c6ae | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigDecimal;
public class R484C {
static ArrayList<Integer>[] graph;
static boolean[] visited;
static int[] child;
static long ans = 0;
public static void main (String[] args) throws java.lang.Exception {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = in.nextInt();
if (n % 2 == 1)
w.println(-1);
else {
graph = new ArrayList[n];
visited = new boolean[n];
child = new int[n];
for (int i = 0; i < n; i++)
graph[i] = new ArrayList<>();
for (int i = 0; i < n - 1; i++) {
int u = in.nextInt() - 1, v = in.nextInt() - 1;
graph[u].add(v);
graph[v].add(u);
}
dfs(0);
int ans = 0;
for (int i = 1; i < n; i++) {
if (child[i] % 2 == 0)
ans++;
}
w.println(ans);
}
w.close();
}
static void dfs(int v) {
boolean found = false;
visited[v] = true;
for (int u : graph[v]) {
if (!visited[u]) {
dfs(u);
child[v] += child[u];
}
}
child[v]++;
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new UnknownError();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1)
return -1;
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0)
return -1;
}
return buf[curChar];
}
public void skip(int x) {
while (x-- > 0)
read();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextString() {
return next();
}
public String next() {
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 String nextLine() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r')
buf.appendCodePoint(c);
c = read();
}
return buf.toString();
}
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 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 boolean hasNext() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value != -1;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | 6b17ba806a3dcd3686ab8d65fdedd144 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.io.*;
import java.util.*;
public class CutEmAll {
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();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
int ans = 0;
// int le[] = new int[100];
public void solve(int testNumber, InputReader in, PrintWriter out) {
while(testNumber-->0){
int n = in.nextInt();
if(n%2==1){
out.println(-1);
return;
}
ArrayList<ArrayList<Integer>> a = new ArrayList<>();
for(int i=0;i<=n;i++)
a.add(new ArrayList<Integer>());
for(int i=0;i<n-1;i++){
int u = in.nextInt();
int v = in.nextInt();
a.get(u).add(v);
a.get(v).add(u);
}
int visited[] = new int[n+1];
dfs(a , visited , 1 , out);
// for(int i=1;i<=n;i++)
// out.print(le[i] + " ");
// out.println();
out.println(ans-1);
}
}
public int dfs(ArrayList<ArrayList<Integer>> a , int visited[] , int index , PrintWriter out){
if(visited[index] == 1)
return -1;
visited[index] = 1;
if(a.get(index).size() == 1 && visited[a.get(index).get(0)] == 1)
return 0;
int count = 0;
int l = a.get(index).size();
for(int i=0;i<l;i++){
count += 1+dfs(a , visited , a.get(index).get(i) , out);
}
if(count%2==1)
ans++;
// le[index] = count;
return count;
}
class Combine{
int value , delete;
Combine(int val , int delete){
this.value = val;
this.delete = delete;
}
}
class Sort2 implements Comparator<Combine>{
public int compare(Combine a , Combine b){
if(a.value > b.value)
return 1;
else if(a.value == b.value && a.delete>b.delete)
return 1;
return -1;
}
}
public int upperBound(ArrayList<Integer> array, int value) {
int low = 0;
int high = array.size();
while (low < high) {
int mid = (low + high) / 2;
if (value >= array.get(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
public int lowerBound(ArrayList<Integer> array, int value) {
int low = 0;
int high = array.size();
while (low < high) {
final int mid = (low + high) / 2;
//checks if the value is less than middle element of the array
if (value <= array.get(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
}
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 | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | 75350ff085097eb8caed73b3aed14e8a | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Practice {
// static int[] cnt;
// static boolean[] visited;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n%2!=0) {
System.out.println("-1");
return;
}
// cnt = new int[n+1];
//visited = new boolean[n+1];
Map<Integer, Vertex> m = new HashMap<Integer, Vertex>();
for(int i=1; i <=n; i++) {
Vertex l = new Vertex();
l.setChildren(new LinkedList<Integer>());
m.put(i, l);
}
for(int i=0; i < n-1; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
m.get(x).getChildren().add(y);
m.get(y).getChildren().add(x);
m.get(y).setParent(x);
m.get(x).setParent(y);
}
for(int i=1; i <=n; i++) {
if(!m.get(i).isVisited()) {
dfs(m, i);
//visited[i] = true;
}
}
int res = 0;
for(int i=1; i <= n; i++) {
if(m.get(i).getCount()%2==0&&m.get(i).getParent()!=0) {
res++;
}
}
// res = res==2?1:res;
System.out.println(res-1);
}
public static int dfs(Map<Integer, Vertex> graph, int vertex) {
graph.get(vertex).setVisited(true);
Iterator itr = graph.get(vertex).getChildren().iterator();
int cnt=0;
while(itr.hasNext()) {
int v = (Integer)itr.next();
if(!graph.get(v).isVisited()) {
dfs(graph, v);
}
cnt+=graph.get(v).getCount();
}
graph.get(vertex).setCount(cnt+1);
return 0;
}
}
class Vertex {
private LinkedList<Integer> children;
private int parent;
private boolean visited;
private int count;
public LinkedList<Integer> getChildren() {
return children;
}
public void setChildren(LinkedList<Integer> children) {
this.children = children;
}
public int getParent() {
return parent;
}
public void setParent(int parent) {
this.parent = parent;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | d9c889f4491d8f32c77e36b33acad9fd | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
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
*/
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);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
int ct = 0;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
HashSet<Integer> g[] = new HashSet[n];
for (int i = 0; i < g.length; i++) {
g[i] = new HashSet<>();
}
for (int i = 0; i < g.length - 1; i++) {
int st = in.nextInt() - 1;
int end = in.nextInt() - 1;
g[st].add(end);
g[end].add(st);
}
int count = maxCount(0, new boolean[n], g);
boolean visited[] = new boolean[n];
for (int i = 0; i < n; i++) {
if (!visited[i]) {
int size = size(i, visited, g);
if (size % 2 != 0) {
out.println(-1);
return;
}
count++;
}
}
out.println(ct);
}
private int size(int root, boolean visited[], HashSet<Integer> g[]) {
if (visited[root]) return 0;
visited[root] = true;
int size = 0;
for (int child : g[root]) {
size += size(child, visited, g);
}
return size + 1;
}
private int maxCount(int root, boolean visited[], HashSet<Integer> g[]) {
if (visited[root]) return 0;
visited[root] = true;
int count = 0;
int children[] = new int[g[root].size()];
int i = 0;
for (int child : g[root]) {
children[i++] = child;
}
for (int child : children) {
int c = maxCount(child, visited, g);
if (c % 2 == 0 && c > 1) {
g[root].remove(child);
g[child].remove(root);
ct++;
}
count += c;
}
return count + 1;
}
}
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 close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int nextInt() {
return readInt();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | ad6b1c8143d2fc74a44e1864d75a49b0 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.text.*;
public class cf1 {
static long mod = (long)1e9 + 7;
static long mod1 = 998244353;
static FastScanner f;
static PrintWriter pw = new PrintWriter(System.out);
static Scanner S = new Scanner(System.in);
static long x0; static long y0;
static int inf = (int)(1e9);
static long iinf = (long)(1e18);
static boolean vis[];
static int sub[];
static HashMap<Integer , ArrayList<Integer>> g;
static void dfs(int node) {
vis[node] = true;
sub[node] = 1;
for(Integer i : g.get(node)) {
if(!vis[i]) {
dfs(i);
sub[node] += sub[i];
}
}
}
static void solve()throws IOException {
int n = f.ni();
if(n == 1) {pn(-1); return;}
g = new HashMap<>();
vis = new boolean[n + 1]; sub = new int[n + 1];
for(int i = 0; i < n - 1; ++i) {
int u = f.ni(); int v = f.ni();
g.putIfAbsent(u , new ArrayList<Integer>());
g.putIfAbsent(v , new ArrayList<Integer>());
g.get(u).add(v); g.get(v).add(u);
}
dfs(1); int ans = 0;
for(int i = 1; i <= n; ++i) {
if(ise(sub[i])) ++ans;
}
pn(!ise(n) ? -1 : ans - 1);
}
public static void main(String[] args)throws IOException {
init();
int t = 1;
while(t --> 0) {solve();}
pw.flush();
pw.close();
}
/******************************END OF MAIN PROGRAM*******************************************/
public static void init()throws IOException{if(System.getProperty("ONLINE_JUDGE")==null){f=new FastScanner("");}else{f=new FastScanner(System.in);}}
public static class FastScanner {
BufferedReader br;StringTokenizer st;
FastScanner(InputStream stream){try{br=new BufferedReader(new InputStreamReader(stream));}catch(Exception e){e.printStackTrace();}}
FastScanner(String str){try{br=new BufferedReader(new FileReader("!a.txt"));}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();}
String nextLine()throws IOException{return br.readLine();}int ni(){return Integer.parseInt(next());}long nl(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}
}
public static void pn(Object o){pw.println(o);}
public static void p(Object o){pw.print(o);}
public static void pni(Object o){pw.println(o);pw.flush();}
static int gcd(int a,int b){if(b==0)return a;else{return gcd(b,a%b);}}
static long gcd(long a,long b){if(b==0l)return a;else{return gcd(b,a%b);}}
static long lcm(long a,long b){return (a*b/gcd(a,b));}
static long exgcd(long a,long b){if(b==0){x0=1;y0=0;return a;}long temp=exgcd(b,a%b);long t=x0;x0=y0;y0=t-a/b*y0;return temp;}
static long pow(long a,long b){long res=1;while(b>0){if((b&1)==1)res=res*a;b>>=1;a=a*a;}return res;}
static long mpow(long a,long b){long res=1;while(b>0l){if((b&1)==1l)res=((res%mod)*(a%mod))%mod;b>>=1l;a=((a%mod)*(a%mod))%mod;}return res;}
static long mul(long a , long b){return ((a%mod)*(b%mod)%mod);}
static long adp(long a , long b){return ((a%mod)+(b%mod)%mod);}
static int log2(int x){return (int)(Math.log(x)/Math.log(2));}
static boolean isPrime(long n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
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 HashSet<Long> factors(long n){HashSet<Long> hs=new HashSet<Long>();for(long i=1;i<=(long)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;}
static HashSet<Integer> factors(int n){HashSet<Integer> hs=new HashSet<Integer>();for(int i=1;i<=(int)Math.sqrt(n);i++){if(n%i==0){hs.add(i);hs.add(n/i);}}return hs;}
static HashSet<Long> pf(long n){HashSet<Long> ff=factors(n);HashSet<Long> ans=new HashSet<Long>();for(Long i:ff)if(isPrime(i))ans.add(i);return ans;}
static HashSet<Integer> pf(int n){HashSet<Integer> ff=factors(n);HashSet<Integer> ans=new HashSet<Integer>();for(Integer i:ff)if(isPrime(i))ans.add(i);return ans;}
static int[] inpint(int n){int arr[]=new int[n];for(int i=0;i<n;i++){arr[i]=f.ni();}return arr;}
static long[] inplong(int n){long arr[] = new long[n];for(int i=0;i<n;i++){arr[i]=f.nl();}return arr;}
static boolean ise(int x){return ((x&1)==0);}static boolean ise(long x){return ((x&1)==0);}
static int gnv(char c){return Character.getNumericValue(c);}//No. of integers less than equal to i in ub
static int log(long x){return x==1?0:(1+log(x/2));} static int log(int x){return x==1?0:(1+log(x/2));}
static int upperbound(int a[],int i){int lo=0,hi=a.length-1,mid=0;int count=0;while(lo<=hi){mid=(lo+hi)/2;if(a[mid]<=i){count=mid+1;lo=mid+1;}else hi=mid-1;}return count;}
static void sort(int[] a){ArrayList<Integer> l=new ArrayList<>();for(int i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);}
static void sort(long[] a){ArrayList<Long> l=new ArrayList<>();for(long i:a)l.add(i);Collections.sort(l);for(int i=0;i<a.length;++i)a[i]=l.get(i);}
static void sort(ArrayList<Integer> a){Collections.sort(a);}//!Precompute fact in ncr()!
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | 4c0b364d6c3d0d442cd294736078ba30 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class main {
static int ans=0;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = in.nextInt();
LinkedList<Integer> adj[] = new LinkedList[n];
for(int i=0;i<n;i++)
adj[i] = new LinkedList();
for(int i=0;i<n-1;i++){
int u = in.nextInt()-1;
int v = in.nextInt()-1;
adj[u].add(v);
adj[v].add(u);
}
maxEdge(adj,n);
}
public static void maxEdge(LinkedList<Integer> adj[],int n){
boolean visited[] = new boolean[n];
if(n%2==1){
System.out.println("-1");
return;
}
dfsUtil(adj,visited,0);
System.out.println(ans);
}
public static int dfsUtil(LinkedList<Integer> adj[],boolean visited[], int v){
visited[v] = true;
int currentComponentNode=0;
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()){
int u = i.next();
if (!visited[u]){
int subTreecount = dfsUtil(adj,visited,u);
if(subTreecount%2==0){
ans++;
}
else
currentComponentNode +=subTreecount;
}
}
return (1 + currentComponentNode);
}
public static void printArray(long[] a){
for(int k=0;k<a.length;k++)
System.out.print(a[k]+" ");
}
// print all prime numbers between l and r
public static void segmentedSieve(int l, int r){
ArrayList<Integer> prime = new ArrayList<Integer>();
int limit = (int) Math.floor(Math.sqrt(r)) +1;
prime = sieveOfEratosthenes(limit, prime);
boolean mark[] = new boolean[r-l+1];
for(int i=0;i<mark.length;i++){
mark[i] = true;
}
for(int i=0;i<prime.size();i++){
int loLim = (int) Math.floor(l/prime.get(i))*(prime.get(i));
if(loLim<l){
loLim+=prime.get(i);
}
for(int j=loLim;j<r;j+=prime.get(i)){
mark[j-l] = false;
}
}
for(int i=l;i<r;i++){
if(mark[i - l]==true){
System.out.print(i+" ");
}
}
}
public static ArrayList<Integer> sieveOfEratosthenes(int limit, ArrayList<Integer> prime){
boolean isPrime[] = new boolean[limit];
for(int i=0;i<limit;i++){
isPrime[i] = true;
}
for(int p=2; p*p<limit; p++){
if(isPrime[p] == true){
for(int i=2*p; i<limit;i+=p){
isPrime[i] = false;
}
}
}
for(int p=2; p<limit; p++){
if(isPrime[p]==true){
prime.add(p);
}
}
return prime;
}
public static int[] sieveOfEratosthenes(int limit, int[] isPrime){
for(int i=0;i<limit;i++){
isPrime[i] = 1;
}
for(int p=2; p*p<limit; p++){
if(isPrime[p] == 1){
for(int i=2*p; i<limit;i+=p){
isPrime[i] = 0;
}
}
}
return isPrime;
}
public static int KMP(String text, String pattern){
int n = text.length();
int m = pattern.length();
if(m==0) return 0;
int fail[] = computeFailArray(pattern);
int i=0;
int j=0;
while(i<n){
if(text.charAt(i)==pattern.charAt(j)){
if(j==m-1)
return i-j;
i++;
j++;
}
else if(j>0)
j=fail[j-1];
else
i++;
}
return -1;
}
public static int[] computeFailArray(String pattern){
int m = pattern.length();
int fail[] = new int[m];
fail[0] = 0;
int j=0;
int i=1;
while(i<m){
if(pattern.charAt(j)==pattern.charAt(i)){
fail[i]=j+1;
j++;
i++;
}
else if(j>0){
j=fail[j-1];
}
else
i++;
}
return fail;
}
public static void reverseArray(int a[], int start, int end){
if(start>=end)
return;
int temp = a[start];
a[start] = a[end];
a[end] = temp;
reverseArray(a, start+1, end-1);
}
public static long fib(long n){
long F[][] = new long[][]{{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
static void multiply(long F[][], long M[][]){
long x = (F[0][0]*M[0][0] + F[0][1]*M[1][0])%1000000007;
long y = (F[0][0]*M[0][1] + F[0][1]*M[1][1])%1000000007;
long z = (F[1][0]*M[0][0] + F[1][1]*M[1][0])%1000000007;
long w = (F[1][0]*M[0][1] + F[1][1]*M[1][1])%1000000007;
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
static void power(long F[][], long n){
if( n == 0 || n == 1)
return;
long M[][] = new long[][]{{1,1},{1,0}};
power(F, n/2);
multiply(F, F);
if (n%2 != 0)
multiply(F, M);
}
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;
}
static class Pair implements Comparable<Pair> {
int u;
int v;
BigInteger bi;
public Pair(int u, int v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
static long modulo(long a,long b,long c) {
long x=1;
long y=a;
while(b > 0){
if(b%2 == 1){
x=(x*y)%c;
}
y = (y*y)%c; // squaring the base
b /= 2;
}
return x%c;
}
public static long getClosestK(long[] a, long x) {
int low = 0;
int high = a.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
// test lower case
if (mid == 0) {
if (a.length == 1) {
return a[0];
}
return Math.min(Math.abs(a[0] - x), Math.abs(a[1] - x)) + x;
}
// test upper case
if (mid == a.length - 1) {
return a[a.length - 1];
}
// test equality
if (a[mid] == x || a[mid + 1] == x) {
return x;
}
// test perfect range.
if (a[mid] < x && a[mid + 1] > x) {
return Math.min(Math.abs(a[mid] - x), Math.abs(a[mid + 1] - x)) + x;
}
// keep searching.
if (a[mid] < x) {
low = mid + 1;
} else {
high = mid;
}
}
throw new IllegalArgumentException("The array cannot be empty");
}
long factorial(long n,long M) {
long ans=1;
while(n>=1)
{
ans=(ans*n)%M;
n--;
}
return ans;
}
static long gcd(long x, long y){
if(x==0)
return y;
if(y==0)
return x;
long r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream inputstream) {
reader = new BufferedReader(new InputStreamReader(inputstream));
tokenizer = null;
}
public int[] readArr(int n) {
int[] input = new int[n];
for (int i = 0; i < n; i++) {
input[i] = nextInt();
}
return input;
}
public String nextLine(){
String fullLine=null;
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
fullLine=reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return fullLine;
}
return fullLine;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | ace80f3e509f7f01eebbbe27a135e1bd | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
public class A_GENERAL {
// for fast output printing : use printwriter or stringbuilder
// remember to close pw using pw.close()
static StringBuilder sb = new StringBuilder();
static long seive_size = (long) 1e6;
static String alpha = "abcdefghijklmnopqrstuvwxyz";
static ArrayList<Integer> primes = new ArrayList<>();
static boolean[] set = new boolean[(int) seive_size+1];
static int n, m;
static ArrayList<Integer>[] adj;
static boolean[] visited;
static ArrayDeque<Integer> q = new ArrayDeque<>();
static final long MOD = 998244353;
static long[][] arr;
static int[] childs;
static int counts = 0;
public static void main(String[] args) {
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
MyScanner sc = new MyScanner();
n = sc.nextInt();
childs = new int[n+1];
init(n);
for(int i = 1; i <= n-1; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
adj[u].add(v);
adj[v].add(u);
}
dfs(1, 1);
out.println(childs[1]%2 == 0 ? counts : -1);
out.close();
}
public static int dfs(int u, int p) {
int cnt = 1;
for(int v : adj[u]) {
if(v != p) {
int val = dfs(v, u);
cnt += val;
if(val%2 == 0) {
counts++;
}
}
}
return (childs[u] = cnt);
}
public static class Pair {
long f;
long s;
Pair(long f, long s) {
this.f = f;
this.s = s;
}
}
public static void init(int n) {
adj = new ArrayList[n+1];
visited = new boolean[n+1];
for(int i = 1; i <= n; i++)
adj[i] = new ArrayList();
}
// print string "s" multiple times
// prefer to use this function for multiple printing
public static String mp(String s, int times) {
return String.valueOf(new char[times]).replace("\0", s);
}
// take log with base 2
public static long log2(long k) {
return 63-Long.numberOfLeadingZeros(k);
}
// using lambda function for sorting
public static void lambdaSort() {
Arrays.sort(arr, (a, b) -> Double.compare(a[0], b[0]));
}
// (n choose k) = (n/k) * ((n-1) choose (k-1))
public static long choose(long n, long k) {
return (k == 0) ? 1 : (n*choose(n-1, k-1))/k;
}
// just for keeping gcd function for other personal purposes
public static long gcd(long a, long b) {
return (a == 0) ? b : gcd(b%a, a);
}
public static long max(long... as) {
long max = Long.MIN_VALUE;
for (long a : as) max = Math.max(a, max);
return max;
}
public static long min(int... as) {
long min = Long.MAX_VALUE;
for (long a : as) min = Math.min(a, min);
return min;
}
public static long modpow(long x, long n, long mod) {
if(n == 0) return 1%mod;
long u = modpow(x, n/2, mod);
u = (u*u)%mod;
if(n%2 == 1) u = (u*x)%mod;
return u;
}
// ======================= binary search (lower and upper bound) =======================
public static int lowerBound(long[] a, int x) {
int lo = 0;
int hi = a.length-1;
int ans = -1;
while(lo <= hi) {
int mid = (lo+hi)/2;
if(x < a[mid]) {
hi = mid-1;
} else if(x > a[mid]) {
lo = mid+1;
} else if(lo != hi) {
hi = mid-1; // for first occurrence
ans = mid;
} else {
return mid;
}
}
return ans;
}
public static int upperBound(long[] a, long x) {
int lo = 0;
int hi = a.length-1;
int ans = -1;
while(lo <= hi) {
int mid = (lo+hi)/2;
if(x < a[mid]) {
hi = mid-1;
} else if(x > a[mid]) {
lo = mid+1;
} else if(lo != hi) {
lo = mid+1; // for last occurrence
ans = mid;
} else {
return mid;
}
}
return ans;
}
// ================================================================
// ================== SEIVE OF ERATOSTHENES =======================
// Complexity : O(N * log(log(N))) ( almost O(N) )
public static void generatePrimes() {
// set.add(0);
// set.add(1);
Arrays.fill(set, true);
set[0] = false;
set[1] = false;
for(int i = 2; i <= seive_size; i++) {
if(set[i]) {
for(long j = (long) i*i; j <= seive_size; j+=i)
set[(int)j] = false;
primes.add(i);
}
}
}
public static boolean isPrime(long N) {
if(N <= seive_size) return set[(int)N];
for (int i = 0; i < (int)primes.size(); i++)
if (N % primes.get(i) == 0) return false;
return true;
}
// ===========================================================
// ================ Permutation of String ====================
public static void permute(String str) {
permute(str, 0, str.length()-1);
}
public static void permute(String str, int l, int r)
{
if (l == r)
System.out.println(str);
else
{
for (int i = l; i <= r; i++)
{
str = swap(str,l,i);
permute(str, l+1, r);
str = swap(str,l,i);
}
}
}
public static String swap(String a, int i, int j)
{
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i] ;
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
// Union-find
void makeSet(int parent[], int rank[]) {
int i;
for(i = 0; i < parent.length; i++) {
parent[i] = i;
rank[i] = 0;
}
}
public static int find(int u, int parent[]) {
if(parent[u] == u) return u;
int v = find(parent[u], parent);
parent[u] = v;
return v;
}
public static boolean connected(int u, int v, int[] parent) {
return find(u, parent) == find(v, parent);
}
public static void Union(int u, int v, int rank[], int parent[]) {
int x = find(u, parent); //root of u
int y = find(v, parent); //root of v
if(x == y) return;
if(rank[x] == rank[y]) {
parent[y] = x;
rank[x]++;
}
else if(rank[x] > rank[y]) {
parent[y] = x;
}
else {
parent[x] = y;
}
}
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;}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[] nextIntArray(int n, int delta) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt() + delta;
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | e7f899ec0c72ac47b84aa326984bd787 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class A {
static List<List<Integer>> adj = new ArrayList<>(100000);
static int cuts = 0;
static int n;
static int[] cnt;
static boolean a = true;
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(in.readLine());
for(int i=0; i<n; i++) {
adj.add(new ArrayList<>());
}
for(int i=0; i<n-1; i++) {
String[] line = in.readLine().split(" ");
int u = Integer.parseInt(line[0])-1;
int v = Integer.parseInt(line[1])-1;
adj.get(u).add(v);
adj.get(v).add(u);
}
cnt = new int[n];
dfs1(0, -1);
dfs2(0, -1);
if(cuts == 0) {
int sum = 1;
for(int x : adj.get(0)) {
sum += cnt[x];
}
if(sum%2==0) {
System.out.println(0);
}
else {
System.out.println(-1);
}
return;
}
System.out.println(cuts);
}
private static void dfs2(int i, int par) {
for(int x : adj.get(i)) {
if(x == par) {
continue;
}
if((cnt[x] % 2 == 0) && ((n-cnt[x]) % 2 == 0)) {
cuts += 1;
}
dfs2(x, i);
}
}
private static void dfs1(int i, int par) {
cnt[i] = 1;
for(int x : adj.get(i)) {
if(x == par) {
continue;
}
dfs1(x, i);
cnt[i] += cnt[x];
}
}
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | 22363f43141e4213acb2377998dad5b6 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.io.*;
import java.util.*;
public class Graph
{
ArrayList<Integer> adj[];
static int V;
Graph(int v)
{
V=v;
adj=new ArrayList[v];
for(int i=0;i<v;i++)
adj[i] = new ArrayList<>();
}
void addEdge(int u,int v)
{
adj[u].add(v);
adj[v].add(u);
}
int DFS(){
boolean visited[]=new boolean[V];
int a[]=new int[1];
DFSUtil(0,visited,a);
return a[0]-1;
}
int DFSUtil(int s,boolean visited[],int a[]){
visited[s]=true;
int ret=0;
for(int i=0;i<adj[s].size();i++){
if(!visited[adj[s].get(i)]){
ret=ret+DFSUtil(adj[s].get(i),visited,a);
}
}
if(ret%2!=0) {
a[0]++;
return 0;
}
return ret+1;
}
public static void main (String[] args)throws java.lang.Exception {
Reader sc=new Reader();
PrintWriter out=new PrintWriter(System.out);
int n=sc.nextInt();
Graph g=new Graph(n);
for(int i=0;i<n-1;i++) {
g.addEdge(sc.nextInt()-1, sc.nextInt()-1);
}
if(n%2!=0)
out.println(-1);
else
out.println(g.DFS());
out.flush();
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | d2ecb67f367b83653d8d85fe9bfab6fb | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;
public class CutEmAll {
static int total = 0;
public static void main(String[] args) {
Kattio kat = new Kattio(System.in,System.out);
int n = kat.getInt();
ArrayList[] adjList = new ArrayList[n];
for(int i = 0;i<n;i++) {
adjList[i] = new ArrayList();
}
for(int i = 0;i<n-1;i++) {
int a = kat.getInt()-1;
int b = kat.getInt()-1;
adjList[a].add(b);
adjList[b].add(a);
}
boolean[] visited = new boolean[n];
dfs(0,adjList,visited);
if(n%2==0) {
System.out.println(total-1);
}else {
System.out.println(-1);
}
}
public static int dfs(int current, ArrayList<Integer>[] adjList, boolean[] visited) {
int under = 1;
visited[current]=true;
for (int i:adjList[current]) {
if(!visited[i]) {
under += dfs(i,adjList,visited);
}
}
if(under%2==0) {
total += 1;
//System.out.println(current+" " + under);
}
return under;
}
}
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int getInt() {
return Integer.parseInt(nextToken());
}
public double getDouble() {
return Double.parseDouble(nextToken());
}
public long getLong() {
return Long.parseLong(nextToken());
}
public String getWord() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
| Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | 60cee5133a3fce216cde8831114eba58 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | // package CF;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class D {
static ArrayList<Integer> [] adj;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
if((n & 1) != 0)
out.println(-1);
else{
adj = new ArrayList[n];
for (int i = 0; i < adj.length; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < n-1; i++) {
int u = sc.nextInt()-1, v = sc.nextInt()-1;
adj[u].add(v);
adj[v].add(u);
}
sz = new int[n];
dfs(0, -1);
int cnt = 0;
for (int i = 0; i < sz.length; i++) {
if((sz[i] & 1) == 0)
cnt++;
}
out.println(cnt-1);
}
out.flush();
out.close();
}
static int [] sz;
static void dfs(int u, int p)
{
sz[u] = 1;
for(int v: adj[u])
{
if(v != p)
{
dfs(v, u);
sz[u] += sz[v];
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader fileReader) {
br = new BufferedReader(fileReader);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | c0ffd190f479533422722856eaab77d6 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Dispatcher {
static final int DIM = 100010;
static List<Integer> g[]=new List[DIM];
static boolean vis[] = new boolean[DIM];
static int res=0;
public static int dfs(int v)
{
vis[v]=true;
int c=0;
for(int k:g[v])
{
if(!vis[k])
{
int cc=dfs(k);
if(cc%2==0)
{
res++;
}
else
{
c+=cc;
}
}
}
return c+1;
}
public static void main(String[] args) throws IOException {
MyScanner sc = new MyScanner(System.in);
PrintWriter wr = new PrintWriter(new OutputStreamWriter(System.out));
int n,v1,v2;
n=sc.nextInt();
for(int i=0;i<=n+1;i++)
{
g[i]=new LinkedList<>();
}
for(int i=1;i<n;i++)
{
v1=sc.nextInt();
v2=sc.nextInt();
g[v1].add(v2);
g[v2].add(v1);
}
dfs(1);
if(n%2==1)System.out.println(-1);
else System.out.println(res);
}
}
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws IOException {
if (st.hasMoreTokens()) {
return st.nextToken();
} else {
st = new StringTokenizer(br.readLine());
}
return next();
}
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 | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | ca631d5d7b3dc29c465a57c480cf2d49 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Dispatcher {
static final int DIM = 100010;
static List<Integer> g[]=new List[DIM];
static boolean vis[] = new boolean[DIM];
static int res=0;
int k=0;
public static int dfs(int v)
{
vis[v]=true;
int c=0;
for(int k:g[v])
{
if(!vis[k])
{
int cc=dfs(k);
if(cc%2==0)
{
res++;
}
else
{
c+=cc;
}
}
}
return c+1;
}
public static void main(String[] args) throws IOException {
MyScanner sc = new MyScanner(System.in);
PrintWriter wr = new PrintWriter(new OutputStreamWriter(System.out));
int n,v1,v2;
n=sc.nextInt();
for(int i=0;i<=n+1;i++)
{
g[i]=new LinkedList<>();
}
for(int i=1;i<n;i++)
{
v1=sc.nextInt();
v2=sc.nextInt();
g[v1].add(v2);
g[v2].add(v1);
}
dfs(1);
if(n%2==1)System.out.println(-1);
else System.out.println(res);
}
}
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws IOException {
if (st.hasMoreTokens()) {
return st.nextToken();
} else {
st = new StringTokenizer(br.readLine());
}
return next();
}
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 | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | b9b1a313d1b6ac1fcc5827efe83f0b39 | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C {
public static void main(String[] args) {
FastScanner scanner = new FastScanner();
int n = scanner.nextInt();
Map<Integer, Set<Integer>> tree = new HashMap<>();
for (int i=0; i < n-1; ++i) {
int a = scanner.nextInt();
int b = scanner.nextInt();
add(tree, a, b);
add(tree, b, a);
}
int total = dfs(tree, 1, -1);
if (N > 0) {
System.out.println(N);
} else {
if (n % 2 == 0) {
System.out.println("0");
} else {
System.out.println("-1");
}
}
}
static int N = 0;
static int dfs(Map<Integer, Set<Integer>> tree, int a, int parent) {
int k = 1;
if (tree.containsKey(a)) {
for (int child : tree.get(a)) {
if (child != parent) {
int subtree = dfs(tree, child, a);
if (subtree % 2 == 0) {
N++;
} else {
k+=subtree;
}
}
}
}
if (parent == -1 && k % 2 != 0) {
N=0;
}
return k;
}
static void add(Map<Integer, Set<Integer>> tree, int a, int b) {
Set<Integer> childs = tree.get(a);
if (childs==null) {
childs = new HashSet<>(1);
tree.put(a, childs);
}
childs.add(b);
}
public 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 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 | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output | |
PASSED | facf1ec57375e8d1fbb7fde16991fe1a | train_002.jsonl | 1526574900 | You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class Solution1 implements Runnable
{
static final long MAX = 1000000007L;
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 Solution1(),"Solution",1<<26).start();
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
int lcm(int a, int b)
{
return (a*b)/gcd(a, b);
}
int level = 20;
int root = 1;
public void run()
{
InputReader sc= new InputReader(System.in);
PrintWriter w= new PrintWriter(System.out);
int n = sc.nextInt();
arr = new ArrayList[n];
ans = new Pair[n];
for(int i = 0;i < n;i++) {
arr[i] = new ArrayList();
ans[i] = new Pair();
}
visited = new boolean[n];
for(int i = 0;i < n-1;i++) {
int a= sc.nextInt()-1;
int b = sc.nextInt()-1;
arr[a].add(b);
arr[b].add(a);
}
if(n % 2 != 0) {
w.println("-1");
}else {
dfs(0);
for(int i =1;i < ans.length;i++) {
long child = ans[i].y - ans[i].x - 1;
if(child/2 % 2 != 0) {
count++;
}
}
w.println(count);
}
w.close();
}
ArrayList<Integer> arr[];
int count = 0;
int start = 0;
Pair[] ans;
boolean[] visited;
void dfs(int a) {
visited[a] = true;
Iterator<Integer> it = arr[a].iterator();
ans[a].x = start;
start++;
while(it.hasNext()) {
int y = it.next();
if(!visited[y]) {
dfs(y);
}
}
ans[a].y = start;
start++;
}
static class Pair implements Comparable<Pair>{
long x;
long y;
Pair(){}
Pair(long x,long y){
this.x = x;
this.y = y;
}
public int compareTo(Pair p){
return Long.compare(this.x,p.x);
}
}
} | Java | ["4\n2 4\n4 1\n3 1", "3\n1 2\n1 3", "10\n7 1\n8 4\n8 10\n4 7\n6 5\n9 3\n3 5\n2 10\n2 5", "2\n1 2"] | 1 second | ["1", "-1", "4", "0"] | NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$. | Java 8 | standard input | [
"dp",
"greedy",
"graphs",
"dfs and similar",
"trees"
] | 711896281f4beff55a8826771eeccb81 | The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree. The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree. | 1,500 | Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.