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 | cd8094399d010bfb8979ebd54af84e41 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.util.*;
import java.math.*;
//import java.io.*;
public class memoziation {
static Scanner in=new Scanner(System.in);
static int global=0;
// static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static class Pair implements Comparable<Pair>{
String s;char ch;int swap;
Pair(String s,char ch,int swap){
this.s=s;
this.ch=ch;
this.swap=swap;
}
public int compareTo(Pair o){
if(swap%2==0)
return o.ch-this.ch;
else
return this.ch-o.ch;
// Sort return this.first-o.first; pair on the basis of first parameter in ascending order
// return o.first-this.first to sort on basis of first parameter in descending order
// return this.second -o.second to Sort pair on the basis of second parameter in ascending order
//return o.second-this.second to sort on basis of second parameter in descending order
}
}
public static void sort(int ar[],int l,int r) {
if(l<r) {
int m=l+(r-l)/2;
sort(ar,l,m);
sort(ar,m+1,r);
merge(ar,l,m,r);
}
}
public static int hcf(int x,int y) {
if(y==0)
return x;
return hcf(y,x%y);
}
public static void merge(int ar[],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]=ar[l+i];
for(int i=0;i<n2;i++)
R[i]=ar[m+i+1];
int i=0;int j=0;int k=l;
while(i<n1 && j<n2) {
if(L[i]<=R[j]) {
ar[k++]=L[i++];
}else {
ar[k++]=R[j++];
}
}
while(i<n1)
ar[k++]=L[i++];
while(j<n2)
ar[k++]=R[j++];
}
public static int[] sort(int ar[]) {
sort(ar,0,ar.length-1);
//Array.sort uses O(n^2) in its worst case ,so better use merge sort
return ar;
}
public static boolean rec() {
return false;
}
public static void func() {
int n=in.nextInt();int k=in.nextInt();
// int total=n*k;
//
// int even=0;int odd=0;
//
// for(int i=1;i<=total;i++) {
// if(i%2==0)even++;
// else
// odd++;}
//
// int oddrow=0;int evenrow=0;
//
// if(n%2==0) {
// oddrow=n/2;evenrow=n/2;}
// else {
//
// oddrow=(n+1)/2;evenrow=n-oddrow;
//
// }
if(n%2!=0 && k!=1) {
System.out.println("NO");return;
}
else if(k==1) {
System.out.println("YES");
for(int i=1;i<=n;i++)
System.out.println(i);
}else {
System.out.println("YES");int odd=1;int even=2;
for(int i=1;i<=n;i++) {
if((i&1)==0) {
for(int j=1;j<=k;j++) {
System.out.print(even+" ");even+=2;}
System.out.println();
}else {
for(int j=1;j<=k;j++) {
System.out.print(odd+" ");odd+=2;}
System.out.println();
}
}
}
//System.out.println();
}
public static void main(String[] args) {
int t=in.nextInt();
while(t-->0) {
func();
global=0;
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 83bef5397e70e9985e986b2bccd63ab6 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.util.*;
import java.math.*;
//import java.io.*;
public class Experiment {
static Scanner in=new Scanner(System.in);
static int global=0;
// static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static class Pair implements Comparable<Pair>{
String s;char ch;int swap;
Pair(String s,char ch,int swap){
this.s=s;
this.ch=ch;
this.swap=swap;
}
public int compareTo(Pair o){
if(swap%2==0)
return o.ch-this.ch;
else
return this.ch-o.ch;
// Sort return this.first-o.first; pair on the basis of first parameter in ascending order
// return o.first-this.first to sort on basis of first parameter in descending order
// return this.second -o.second to Sort pair on the basis of second parameter in ascending order
//return o.second-this.second to sort on basis of second parameter in descending order
}
}
public static void sort(int ar[],int l,int r) {
if(l<r) {
int m=l+(r-l)/2;
sort(ar,l,m);
sort(ar,m+1,r);
merge(ar,l,m,r);
}
}
public static int hcf(int x,int y) {
if(y==0)
return x;
return hcf(y,x%y);
}
public static void merge(int ar[],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]=ar[l+i];
for(int i=0;i<n2;i++)
R[i]=ar[m+i+1];
int i=0;int j=0;int k=l;
while(i<n1 && j<n2) {
if(L[i]<=R[j]) {
ar[k++]=L[i++];
}else {
ar[k++]=R[j++];
}
}
while(i<n1)
ar[k++]=L[i++];
while(j<n2)
ar[k++]=R[j++];
}
public static int[] sort(int ar[]) {
sort(ar,0,ar.length-1);
//Array.sort uses O(n^2) in its worst case ,so better use merge sort
return ar;
}
public static boolean rec() {
return false;
}
public static void func() {
int n=in.nextInt();int k=in.nextInt();
// int total=n*k;
//
// int even=0;int odd=0;
//
// for(int i=1;i<=total;i++) {
// if(i%2==0)even++;
// else
// odd++;}
//
// int oddrow=0;int evenrow=0;
//
// if(n%2==0) {
// oddrow=n/2;evenrow=n/2;}
// else {
//
// oddrow=(n+1)/2;evenrow=n-oddrow;
//
// }
if(n%2!=0 && k!=1) {
System.out.println("NO");return;
}
else if(k==1) {
System.out.println("YES");
for(int i=1;i<=n;i++)
System.out.println(i);
}else {
System.out.println("YES");int odd=1;int even=2;
for(int i=1;i<=n;i++) {
if(i%2==0) {
for(int j=1;j<=k;j++) {
System.out.print(even+" ");even+=2;}
System.out.println();
}else {
for(int j=1;j<=k;j++) {
System.out.print(odd+" ");odd+=2;}
System.out.println();
}
}
}
//System.out.println();
}
public static void main(String[] args) {
int t=in.nextInt();
while(t-->0) {
func();
global=0;
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 3c7b2763ac39b31b3e49758ef8304889 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class OKEA {
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt(), K = in.nextInt();
int[][] mat = new int[N][K];
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
mat[i][j] = j * N + i;
}
}
int sum = 0;
boolean valid = true;
for (int i = 0; i < K; i++) {
sum += mat[0][i];
if (sum % (i + 1) > 0) {
valid = false;
break;
}
}
if (valid) {
out.println("YES");
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
out.print((mat[i][j] + 1) + (j == K - 1 ? "\n" : " "));
}
}
} else {
out.println("NO");
}
}
out.close();
}
public static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static class Reader {
BufferedReader in;
StringTokenizer st;
public Reader() {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 57db0d24d618b06ab6353b8df3762e78 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | //import java.lang.reflect.Array;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import javax.print.DocFlavor.STRING;
import javax.swing.Popup;
import javax.swing.plaf.synth.SynthStyleFactory;
public class Simple{
public static Deque<Integer> possible(int a[],int n){
boolean bool = true;
Deque<Integer> deq = new LinkedList<>();
int i=0;
int j=n-1;
int max = n;
int min = 1;
while(i<=j){
if(a[i]== min){
deq.addFirst(a[i]);
i++;min++;
continue;
}
if(a[j]== min){
deq.addLast(a[j]);
j--;
min++;
continue;
}
if(a[i]==max){
deq.addFirst(a[i]);
i++;
max--;
continue;
}
if(a[j]==max){
deq.addLast(a[j]);
j--;
max--;
continue;
}
Deque<Integer> d = new LinkedList<>();
d.add(-1);
return d;
}
return deq;
}
public static class Pair implements Comparable<Pair>{
int val;
int freq = 0;
Pair prev ;
Pair next;
boolean bool = false;
public Pair(int val,int freq){
this.val = val;
this.freq= freq;
}
public int compareTo(Pair p){
// if(p.freq == this.freq){
// return this.val - p.freq;
// };
// if(this.freq > p.freq)return -1;
// return 1;
return (p.freq-p.val) - (this.freq - this.val);
}
}
public static long factorial(long n)
{
// single line to find factorial
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
static long m = 998244353;
// Function to return the GCD of given numbers
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Recursive function to return (x ^ n) % m
static long modexp(long x, long n)
{
if (n == 0) {
return 1;
}
else if (n % 2 == 0) {
return modexp((x * x) % m, n / 2);
}
else {
return (x * modexp((x * x) % m, (n - 1) / 2) % m);
}
}
// Function to return the fraction modulo mod
static long getFractionModulo(long a, long b)
{
long c = gcd(a, b);
a = a / c;
b = b / c;
// (b ^ m-2) % m
long d = modexp(b, m - 2);
// Final answer
long ans = ((a % m) * (d % m)) % m;
return ans;
}
// public static long bs(long lo,long hi,long fact,long num){
// long help = num/fact;
// long ans = hi;
// while(lo<=hi){
// long mid = (lo+hi)/2;
// if(mid/)
// }
// }
public static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
public static int countDigit(long n)
{
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
return count;
}
static ArrayList<Long> al ;
static boolean checkperfectsquare(long n)
{
// If ceil and floor are equal
// the number is a perfect
// square
if (Math.ceil((double)Math.sqrt(n)) ==
Math.floor((double)Math.sqrt(n)))
{
return true;
}
else
{
return false;
}
}
public static void decToBinary(int n,int arr[],int j)
{
// Size of an integer is assumed to be 32 bits
for (int i = 31; i >= 0; i--) {
int k = n >> i;
if ((k & 1) > 0){
arr[j]++;
}
j++;
}
}
public static long[] helper(long n){
long arr[] = new long[33];
//ong temp = 2;
for (long i = 1; i <= 32; i++) {
long k = (long)Math.pow(2,i);
if (k<=n){
arr[(int)i-1]+= n/k ;
arr[(int)i-1]+= (n) % k;
}
}
return arr;
}
public static int countAp(int i,int j,int arr[],int n,int k){
if(k==n)return 0;
if(arr[k]-arr[j]==arr[j]-arr[i]){
return countAp(i,j,arr,n,k+1)+1;
}
return countAp(i,j,arr,n,k+1);
}
// public static String reverse(String str){
// String ans ="";
// for(int i=0;i<str.length();i++){
// ans+=str.charAt(n-i-1);
// }
// return ans;
// }
// public static int search(ArrayList<Integer> al,int i,int j,int find){
// int mid = (i+j)/2;
// if(al.get(mid)==find)
// }
static int MAX = 100000;
static int factor[];
// function to generate all prime
// factors of numbers from 1 to 10^6
static void generatePrimeFactors()
{
factor = new int[MAX+1];
factor[1] = 1;
// Initializes all the positions with
// their value.
for (int i = 2; i < MAX; i++)
factor[i] = i;
// Initializes all multiples of 2 with 2
for (int i = 4; i < MAX; i += 2)
factor[i] = 2;
// A modified version of Sieve of
// Eratosthenes to store the
// smallest prime factor that
// divides every number.
for (int i = 3; i * i < MAX; i++) {
// check if it has no prime factor.
if (factor[i] == i) {
// Initializes of j starting from i*i
for (int j = i * i; j < MAX; j += i) {
// if it has no prime factor
// before, then stores the
// smallest prime divisor
if (factor[j] == j)
factor[j] = i;
}
}
}
}
// function to calculate number of factors
static int calculateNoOFactors(int n)
{
if (n == 1)
return 1;
int ans = 1;
// stores the smallest prime number
// that divides n
int dup = factor[n];
// stores the count of number of times
// a prime number divides n.
int c = 1;
// reduces to the next number after prime
// factorization of n
int j = n / factor[n];
// false when prime factorization is done
while (j != 1) {
// if the same prime number is dividing n,
// then we increase the count
if (factor[j] == dup)
c += 1;
/* if its a new prime factor that is
factorizing n, then we again set c=1
and change dup to the new prime factor,
and apply the formula explained
above. */
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
// prime factorizes a number
j = j / factor[j];
}
// for the last prime factor
ans = ans * (c + 1);
return ans;
}
public static int maxPrimefactorNum(int N)
{
if (N < 2)
return 0;
// Based on Sieve of Eratosthenes
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/
boolean[] arr = new boolean[N + 1];
int prod = 1, res = 0;
for (int p = 2; p * p <= N; p++)
{
// If p is prime
if (arr[p] == false)
{
for (int i = p * 2;
i <= N; i += p)
arr[i] = true;
// We simply multiply first set
// of prime numbers while the
// product is smaller than N.
prod *= p;
if (prod > N)
return res;
res++;
}
}
return res;
}
public static class Tuple{
int l,r,u,d;
public Tuple(int l,int r,int u,int d){
this.l=l;
this.r=r;
this.u=u;
this.d=d;
}
}
// public static boolean dfs(ArrayList<ArrayList<Node>> adj,boolean vis[],int node,int k,int count,int n){
// vis[node]=true;
// count++;
// if(count==n)return true;
// for(Node temp : adj.get(node)){
// if(!vis[node] && !isKthBitSet(temp.w, k)&& isEdgeValid){
// return dfs(adj, vis, temp, k,count,n);
// }
// }
// return false;
// }
public static class Node{
//int u;
long v;
long w;
public Node(long v,long w){
//this.u = u;
this.v=v;
this.w=w;
}
}
public static boolean isKthBitSet(int n,int k){
if ((n & (1 << (k - 1))) > 0)
return true;
return false;
}
public static class Edge{
int v1 =-1;
int v2 = -1;
int e1=-1;
int e2=-1;
}
// public static long helper(String a,String b,int i,int j,int lasti,int lastj,long dp[][]){
// if(i==0 && j==0){
// return 0;
// }
// if(dp[i][j]!=-1){
// return dp[i][j];
// }
// long ans1 = Long.MAX_VALUE;
// long ans2 =Long.MAX_VALUE;
// if(i>0){
// if(a.charAt(i-1)=='1'){
// ans1 = helper(a, b, i-1, j, i, lastj, dp)+dp[lasti][lastj]+lasti-i+1;
// }
// else{
// ans1 = helper(a, b, i-1, j, lasti, lastj, dp);
// }
// }
// if(j>0){
// if(b.charAt(j-1)=='1'){
// ans2 = helper(a, b, i, j-1, lasti, j, dp)+dp[lasti][lastj] + lastj-j+1;
// }
// else{
// ans2 = helper(a, b, i, j-1, lasti, lastj, dp);
// }
// }
// dp[i][j] = Math.min(ans1,ans2);
// return dp[i][j];
// }
public static boolean[] sieve(int n){
boolean isPrime[] = new boolean[n+1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<=n;i++){
if(!isPrime[i])continue;
for(int j= i*2;j<=n;j=j+i){
isPrime[j] = false;
}
}
return isPrime;
}
public static void swap(int a[][],int i1,int i2,int j1,int j2){
int temp = a[i1][i2];
a[i1][i2] = a[j1][j2];
a[j1][j2] = temp;
}
public static long helper1(long n){
if(n==0 || n==1||n<0){
return 0;
}
long pow = (long)(Math.log(n)/Math.log(2));//2
long num = (long)(Math.pow(2, pow));//4
long greater = n - num + 1;
long add = num*2 - 1;
if(greater>n/2){
long ans = (n-1)*add;
return ans;
}
long ans = greater*2*add;
return ans + helper1(n-greater*2);
}
static int power(int x, int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more
// than or equal to p
while (y > 0)
{
// If y is odd, multiply
// x with result
if ((y & 1) > 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to find modular
// inverse of a under modulo p
// using Fermat's method.
// Assumption: p is prime
static int modInverse(int a, int p)
{
return power(a, p - 2, p);
}
// Returns n! % p using
// Wilson's Theorem
static int modFact(int n,
int p)
{
if (n >= p)
return 0;
int result = 1;
for (int i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
public static void bfs(ArrayList<ArrayList<Integer>> adj,boolean vis[],int dis[]){
vis[0] = true;
Queue<Integer> q = new LinkedList<>();
q.add(0);
int count = 0;
while(!q.isEmpty()){
int size = q.size();
for(int j = 0;j<size;j++){
int poll = q.poll();
dis[poll] = count;
vis[poll] = true;
for(Integer x : adj.get(poll)){
if(!vis[x]){
q.add(x);
}
}
}
count++;
}
}
public static int op(int n){
if(n==1)return 0;
int pow = (int)(Math.log(n)/Math.log(2));
int num = (int)(Math.pow(2,pow));
int something = n - num;
if(something==0){
return pow;
}
if(something<=num/2){
return pow+1;
}
return pow+2;
}
//
public static void main(String args[]){
//System.out.println("Hello Java");
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int t1 = 1;t1<=t;t1++){
int n = s.nextInt();
int k =s.nextInt();
if(k==1){
System.out.println("YES");
for(int i=1;i<=n;i++){
System.out.println(i);
}
continue;
}
if(n==1){
System.out.println("NO");
continue;
}
// int temp = n;
// n = k;
// k = temp;
if(n%2==1){
System.out.println("NO");
continue;
}
System.out.println("YES");
if(n%2==0){
for(int i =1;i<=n;i++){
for(int j=i;j<=n*k;j=j+n){
System.out.print(j+" ");
}
System.out.println();
}
}
else{
for(int i=1;i<=n*k ;i=i+2*k){
for(int j=0;j<k;j++){
int sum = i + j*2;
System.out.print(sum+" ");
}
System.out.println();
for(int j=0;j<k;j++){
int sum = i + j*2+1;
System.out.print(sum+" ");
}
System.out.println();
}
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | a28d6b185ba023fff473db74831fc05c | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class okea {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
Reader.init(System.in);
int t=Reader.nextint();
while(t-->0) {
int n=Reader.nextint();
int k=Reader.nextint();
if(k==1) {
System.out.println("YES");
for(int i=1;i<=n;i++) {
System.out.println(i);
}
}else if(n%2==1) {
System.out.println("NO");
}else {
// int arr[][]=new int[n][k];
// boolean flag=true;
// for(int i=1;i<=n;i++) {
//// double mean=0;
//// int temp=0;
// for(int j=0;j<k;j++) {
// arr[i-1][j]=i+j*k;
//// mean+=arr[i-1][j];
//// temp++;
//// if((mean/temp)%1!=0) {
//// flag=false;
//// break;
//// }
// }
// if(!flag) {
// break;
// }
// }
// if(flag) {
// System.out.println("YES");
// for(int i=0;i<n;i++) {
// for(int j=0;j<k;j++) {
// System.out.print(arr[i][j]+" ");
// }
// System.out.println();
// }
// }else {
// System.out.println("NO");
// }
System.out.println("YES");
// for(int i=0;i<n;i++) {
// for(int j=1;j<=k;j++) {
// System.out.print((j+i*n)+" ");
// }
// System.out.println();
// }
// for(int i=1;i<=k;i++) {
// for(int j=0;j<n;j++) {
// System.out.print((i+j*k)+" ");
// }
// System.out.println();
// }
for(int i=1;i<=n;i++) {
for(int j=0;j<k;j++) {
System.out.print((i+j*n)+" ");
}
System.out.println();
}
}
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
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 long nextlong() throws IOException {
return Long.parseLong( next() );
}
static double nextdouble() throws IOException {
return Double.parseDouble( next() );
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | d3b2e612f14793a2450dce91b3d6617a | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
// import java.io.*;
// import java.util.*;
// public class Main{
// static class FastReader {
// BufferedReader br;
// StringTokenizer st;
// public FastReader()
// {
// br = new BufferedReader(
// new InputStreamReader(System.in));
// }
// String next()
// {
// while (st == null || !st.hasMoreElements()) {
// try {
// st = new StringTokenizer(br.readLine());
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// }
// return st.nextToken();
// }
// int nextInt() { return Integer.parseInt(next()); }
// long nextLong() { return Long.parseLong(next()); }
// double nextDouble()
// {
// return Double.parseDouble(next());
// }
// String nextLine()
// {
// String str = "";
// try {
// str = br.readLine();
// }
// catch (IOException e) {
// e.printStackTrace();
// }
// return str;
// }
// }
// public static class Pair implements Comparable<Pair>
// {
// double cpu;
// int tu;
// int op;
// int cost;
// Pair(double cpu,int tu,int op,int cost)
// {
// this.cpu=cpu;
// this.tu=tu;
// this.op=op;
// this.cost=cost;
// }
// public int compareTo(Pair o)
// {
// return (int)(o.cpu-this.cpu);
// }
// }
// public static void main(String[] args) throws Exception {
// FastReader sc = new FastReader();
// int t=sc.nextInt();
// while(t-->0)
// {
// int n=sc.nextInt();
// int k=sc.nextInt();
// int[] val=new int[n];
// int[] coins=new int[n];
// for(int i=0;i<n;i++)
// {
// val[i]=sc.nextInt();
// }
// for(int i=0;i<n;i++)
// {
// coins[i]=sc.nextInt();
// }
// Pair cost[]=new Pair[n];
// for(int i=0;i<n;i++)
// {
// double sum=val[i]/(i+1)+1;
// int op=Math.log(10)/Math.sum;
// if(val[i]%(i+1)!=0)
// {
// op++;
// }
// int tu=val[i];
// double cpu=coins[i]/val[i];
// cost[i]=new Pair(cpu,tu,op,coins[i]);
// }
// Arrays.sort(cost);
// for( Pair i:cost)
// {
// System.out.print(i.cpu+","+i.op+"-"+i.cost+"**");
// }
// System.out.println();
// long ans=0;
// for(int i=0;i<n;i++)
// {
// long amt=cost[i].cost;
// if(cost[i].op<=k)
// {
// k-=cost[i].op;
// ans+=amt;
// System.out.print(cost[i].op+" ");
// }
// }
// System.out.println(ans);
// }
// }
// }
import java.io.*;
import java.util.*;
import javax.lang.model.element.Element;
public class Main{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static class Pair implements Comparable<Pair>
{
int i;
int j;
Pair(int i,int j)
{
this.i=i;
this.j=j;
}
public int compareTo(Pair o)
{
return this.i-o.i;
}
}
public static void main(String[] args) throws Exception
{
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
if(n==1&&k==1)
{
System.out.println("YES");
System.out.println(1);
}
else if(n==1&&k!=1)
System.out.println("NO");
else if(k==1)
{
System.out.println("YES");
for(int i=1;i<=n;i++)
{
System.out.println(i);
}
}
else
{
if(((n*k+1)/2)%k!=0)
System.out.println("NO");
else
{
System.out.println("YES");
int odd=1;
int even=0;
for(int i=0;i<n/2;i++)
{
for(int j=0;j<k;j++)
{
System.out.print(odd+" ");
odd+=2;
}
System.out.println();
for(int j=0;j<k;j++)
{
even+=2;
System.out.print(even+" ");
}
System.out.println();
}
}
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | aacf85f2cbead3a76445d4137407face | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* @author Peter Li
* @version 02/06/2022
*/
public class COKEA {
static boolean solve() {
int n = ni(), k = ni();
if (k >= 2 && n % 2 == 1) {
return false;
}
System.out.println("YES");
for (int i = 0; i < n; i++) {
int start = (i - i % 2) * k + 1 + i % 2;
for (int j = 1; j < k; j++) {
System.out.print(start + 2 * (j - 1) + " ");
}
System.out.println(start + 2 * (k-1));
}
return true;
}
public static void main(String[] args) throws IOException {
int t = ni();
while (t-- > 0) {
if (!solve()) {
System.out.println("NO");
}
}
}
private static final String DELIMINATOR = " \n\r\f\t";
private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1 << 15);
private static StringTokenizer st = new StringTokenizer("", DELIMINATOR);
private static boolean hasNext() {
makeStringTokenizerReady();
return st.hasMoreTokens();
}
private static void makeStringTokenizerReady() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine(), DELIMINATOR);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String ns() {
makeStringTokenizerReady();
return st.nextToken();
}
private static long nl() {
return Long.parseLong(ns());
}
private static int ni() {
return (int) nl();
}
private static double nd() {
return Double.parseDouble(ns());
}
private static int[] na(int n) {
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = ni();
}
return ans;
}
private static String[] nsa(int n) {
String[] ans = new String[n];
for (int i = 0; i < n; i++) {
ans[i] = ns();
}
return ans;
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | b74018643e965307b577d31af5a4608b | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.InputMismatchException;
public class E1634C {
public static void main(String[] args) {
FastIO io = new FastIO();
int t = io.nextInt();
while (t-- > 0) {
int n = io.nextInt();
int k = io.nextInt();
int[][] grid = new int[n][k];
boolean ans = true;
int curEven = 2;
int curOdd = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (i % 2 == 0) {
grid[i][j] = curOdd;
curOdd += 2;
} else {
grid[i][j] = curEven;
curEven += 2;
}
}
}
if (grid[n - 1][k - 1] == n * k) {
io.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
io.print(grid[i][j] + " ");
}
io.println();
}
} else io.println("NO");
}
io.close();
}
private static class FastIO extends PrintWriter {
private final InputStream stream;
private final byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 072709ea122e085c2f668897ca7c3f74 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.max;
import static java.lang.System.exit;
import static java.util.Arrays.fill;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class cf {
static long mod = (long) (1e9 + 7);
static void solve() throws Exception {
int tests = scanInt();
// int tests = 1;
for (int test = 0; test < tests; test++) {
// int n = scanInt(), k = scanInt();
// String l = scanString();
// out.println();
// continue test;
int n = scanInt();
int k = scanInt();
if(n % 2 == 0){
out.println("YES");
for(int i = 1; i <= n; i++){
int count = 1;
for(int j = i; j <= n*k && count <= k; j = j + n){
out.print(j + " ");
count++;
}
out.println();
}
}
else{
if(k == 1){
out.println("YES");
for(int i = 1; i <= n; i++){
out.println(i);
}
}
else{
out.println("NO");
}
}
}
}
static class pair {
long x, y;
pair(long ar, long ar2) {
x = ar;
y = ar2;
}
}
static class pairChar {
Character key;
int val;
pairChar(Character key, int val) {
this.key = key;
this.val = val;
}
}
static int[] arrI(int N) throws Exception {
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = scanInt();
}
return A;
}
static long[] arrL(int N) throws Exception {
long A[] = new long[N];
for (int i = 0; i < A.length; i++)
A[i] = scanLong();
return A;
}
static void sort(long[] a) // check for long
{
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 sortReverse(long[] a) // check for long
{
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
int n = a.length;
for (int i = 0; i < n; i++)
a[n - i - 1] = l.get(i);
}
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 sortReverse(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
int n = a.length;
for (int i = 0; i < n; i++)
a[n - i - 1] = l.get(i);
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static class DescendingComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return b - a;
}
}
static class AscendingComparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return a - b;
}
}
static boolean isPalindrome(char X[]) {
int l = 0, r = X.length - 1;
while (l <= r) {
if (X[l] != X[r])
return false;
l++;
r--;
}
return true;
}
static long fact(long N) {
long num = 1L;
while (N >= 1) {
num = ((num % mod) * (N % mod)) % mod;
N--;
}
return num;
}
static long pow(long a, long b) {
long mod = 1000000007;
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0)
pow = (pow * x) % mod;
x = (x * x) % mod;
b /= 2;
}
return pow;
}
static long toggleBits(long x)// one's complement || Toggle bits
{
int n = (int) (Math.floor(Math.log(x) / Math.log(2))) + 1;
return ((1 << n) - 1) ^ x;
}
static int countBits(long a) {
return (int) (Math.log(a) / Math.log(2) + 1);
}
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 (int i = 5; i * i <= N; i = i + 6)
if (N % i == 0 || N % (i + 2) == 0)
return false;
return true;
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else
return GCD(b, a % b);
}
static HashMap<Integer, Integer> getHashMap(int A[]) {
HashMap<Integer, Integer> mp = new HashMap<>();
for (int a : A) {
int f = mp.getOrDefault(a, 0) + 1;
mp.put(a, f);
}
return mp;
}
public static Map<Character, Integer> mapSortByValue(Map<Character, Integer> hm) {
// Create a list from elements of HashMap
List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o1.getValue() - o2.getValue();
}
});
// put data from sorted list to hashmap
Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>();
for (Map.Entry<Character, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
public static Map<Character, Integer> mapSortByKey(Map<Character, Integer> hm) {
// Create a list from elements of HashMap
List<Map.Entry<Character, Integer>> list = new LinkedList<Map.Entry<Character, Integer>>(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o1.getKey() - o2.getKey();
}
});
// put data from sorted list to hashmap
Map<Character, Integer> temp = new LinkedHashMap<Character, Integer>();
for (Map.Entry<Character, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static String scanString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | b1e731d4d5c0bcc54f10445a74575ba6 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static final long mod=1000000007;
public static void Solve() throws IOException{
st=new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken()),k=Integer.parseInt(st.nextToken());
if(n%2!=0 && k>1){
bw.write("NO\n"); return ;
}
int[][] ar=new int[n][k];
int oc=1,ec=2;
for(int i=0;i<n;i++){
if(i%2==0) for(int j=0;j<k;j++,oc+=2) ar[i][j]=oc;
else for(int j=0;j<k;j++,ec+=2) ar[i][j]=ec;
}
bw.write("YES\n");
for(int[] i:ar){
for(int j:i) bw.write(j+" ");
bw.newLine();
}
}
/** Main Method**/
public static void main(String[] YDSV) throws IOException{
//int t=1;
int t=Integer.parseInt(br.readLine());
while(t-->0) Solve();
bw.flush();
}
/** Helpers**/
private static char[] getStr()throws IOException{
return br.readLine().toCharArray();
}
private static int Gcd(int a,int b){
if(b==0) return a;
return Gcd(b,a%b);
}
private static long Gcd(long a,long b){
if(b==0) return a;
return Gcd(b,a%b);
}
private static int[] getArrIn(int n) throws IOException{
st=new StringTokenizer(br.readLine());
int[] ar=new int[n];
for(int i=0;i<n;i++) ar[i]=Integer.parseInt(st.nextToken());
return ar;
}
private static long[] getArrLo(int n) throws IOException{
st=new StringTokenizer(br.readLine());
long[] ar=new long[n];
for(int i=0;i<n;i++) ar[i]=Long.parseLong(st.nextToken());
return ar;
}
private static List<Integer> getListIn(int n) throws IOException{
st=new StringTokenizer(br.readLine());
List<Integer> al=new ArrayList<>();
for(int i=0;i<n;i++) al.add(Integer.parseInt(st.nextToken()));
return al;
}
private static List<Long> getListLo(int n) throws IOException{
st=new StringTokenizer(br.readLine());
List<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) al.add(Long.parseLong(st.nextToken()));
return al;
}
private static long pow_mod(long a,long b) {
long result=1;
while(b!=0){
if((b&1)!=0) result=(result*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return result;
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 7fb41c9de7dcda8f4c938db3f225af2d | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
// when can't think of anything -->>
// 1. In sorting questions try to think about all possibilities like sorting from start, end, middle.
// 2. Two pointers, brute force.
// 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse.
// 4. If order does not matter then just sort the data if constraints allow. It'll never harm.
// 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with.
// 6. Think like a robot, just consider all the possibilities not probabilities if you still can't solve.
// 7. Try to solve it from back or reversely.
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int k = sc.nextInt();
if(k==1) {
writer.println("YES");
for(int i =1; i <= n; i++)writer.println(i);
} else if(n%2==1)writer.println("NO");
else {
writer.println("YES");
int c = 1;
int arr[][] = new int[n][k];
for(int j = 0; j < k; j++) {
for(int i = 0; i < n; i++) {
arr[i][j] = c;
c++;
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < k; j++) {
writer.print(arr[i][j] + " ");
}
writer.println();
}
}
}
writer.flush();
writer.close();
}
private static boolean isPrime(int c) {
for (int i = 2; i*i <= c; i++) {
if(c%i==0)return false;
}
return true;
}
private static int find(int a , int arr[]) {
if(arr[a] != a) return arr[a] = find(arr[a], arr);
return arr[a];
}
private static void union(int a, int b, int arr[]) {
int aa = find(a,arr);
int bb = find(b, arr);
arr[aa] = bb;
}
private static int gcd(int a, int b) {
if(a==0)return b;
return gcd(b%a, a);
}
public static int[] readIntArray(int size, FastReader s) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
return array;
}
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;
}
}
}
class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || obj.getClass()!= this.getClass()) return false;
Pair pair = (Pair) obj;
return (pair.a == this.a && pair.b == this.b);
}
@Override
public int hashCode()
{
return Objects.hash(a,b);
}
@Override
public int compareTo(Pair o) {
if(this.a == o.a) {
return Integer.compare(this.b, o.b);
}else {
return Integer.compare(this.a, o.a);
}
}
@Override
public String toString() {
return this.a + " " + this.b;
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 7e70f7cb6e046de5bd08cc9a0d251ac3 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class CodeForce
{
public static void main(String args[])
{
long n,k;
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0)
{
long init =0;
n=s.nextLong();
k = s.nextLong();
//long a[][] = new long[n+1][k+1];
if(k==1)
{
System.out.println("YES");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
System.out.print(i);
}
System.out.println();
}
}
else if(n==1 && k==1)
{
System.out.println("YES");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
System.out.print(1);
}
System.out.println();
}
}
else if(n==1)
{
System.out.println("NO");
}
else if(n%2==0)
{
System.out.println("YES");
for(int i=1;i<=n;i++)
{
if(i%2!=0)
init = 1 + (i-1)*k;
else
init = 2 + (i-2)*k;
for(int j=1;j<=k;j++)
{
System.out.print(init + " ");
init = init +2;
}
System.out.println();
}
}
else
{
System.out.println("NO");
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 226399b4f336e66636bad6f3d088c180 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int T = in.nextInt();
while (T-- > 0) {
solveOne(in, out);
}
}
private void solveOne(Scanner in, PrintWriter out) {
int rows = in.nextInt();
int cols = in.nextInt();
int G[][] = new int[rows][cols];
int odd = 1;
int even = 2;
for (int i = 0; i < rows && odd <= (rows * cols); i += 2) {
for (int j = 0; j < cols && odd <= (rows * cols); j++) {
G[i][j] = odd;
odd += 2;
}
}
for (int i = 1; i < rows && even <= (rows * cols); i += 2) {
for (int j = 0; j < cols && even <= (rows * cols); j++) {
G[i][j] = even;
even += 2;
}
}
boolean isOk = true;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (G[i][j] == 0) isOk = false;
}
}
if (!isOk) {
out.println(L.NO);
return;
}
out.println(L.YES);
for (int i = 0; i < rows; i++) {
L.printIntArray(G[i], out);
out.println();
}
}
}
static class L {
public static String YES = "YES";
public static String NO = "NO";
public static void printIntArray(int[] array, PrintWriter out) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < array.length; i++) {
stringBuilder.append(array[i] + " ");
}
out.print(stringBuilder.toString());
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 2d2210f244ac8266a9d969bf61c4635d | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class solution{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
if(k==1 || (n&1)==0){
sb.append("YES\n");
for(int i=1;i<=n;i++){
for(int j=0;j<k;j++){
sb.append(j*n+i+" ");
}
sb.append("\n");
}
}
else{
sb.append("NO\n");
}
}
System.out.println(sb);
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 724ada1eded2323307b71077d41a3869 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class solution{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
if(k==1 || (n&1)==0){
sb.append("YES\n");
for(int i=1;i<=n;i++){
for(int j=0;j<k;j++){
sb.append(j*n+i+" ");
}
sb.append("\n");
}
sb.append("\n");
}
else{
sb.append("NO\n");
}
}
System.out.println(sb);
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 4777c06a20fffa3d6678bb0804444c68 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
O : while(t-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
if(n*m==1)
{
printY();
System.out.println("1");
continue;
}
if((n%2!=0) && m!=1)
{
printN();
continue;
}
printY();
int ocount=1,ecount=2;
for(int i=0;ocount<=n*m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(ocount+" ");
ocount+=2;
}
System.out.println();
}
for(int i=0;ecount<=n*m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(ecount+" ");
ecount+=2;
}
System.out.println();
}
}
out.flush();
}
static void printN()
{
System.out.println("NO");
}
static void printY()
{
System.out.println("YES");
}
static int findfrequencies(int a[],int n)
{
int count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==n)
{
count++;
}
}
return count;
}
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());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int a[])
{
ArrayList<Integer> list=new ArrayList<>();
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
if(a[i]%2!=0)
{
list.add(a[i]);
}
}
for(int i=0;i<a.length;i++)
{
a[i]=list.get(i);
}
return a;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int DigitSum(int n)
{
int r=0,sum=0;
while(n>=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
return sum;
}
static boolean checkPerfectSquare(int number)
{
double sqrt=Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isPrime2(int n)
{
if (n <= 1)
{
return false;
}
if (n == 2)
{
return true;
}
if (n % 2 == 0)
{
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
static String minLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for(int i=0;i<n;i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[0];
}
static String maxLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[arr.length-1];
}
static class P implements Comparable<P> {
int i, j;
public P(int i, int j) {
this.i=i;
this.j=j;
}
public int compareTo(P o) {
return Integer.compare(i, o.i);
}
}
static int binary_search(int a[],int value)
{
int start=0;
int end=a.length-1;
int mid=start+(end-start)/2;
while(start<=end)
{
if(a[mid]==value)
{
return mid;
}
if(a[mid]>value)
{
end=mid-1;
}
else
{
start=mid+1;
}
mid=start+(end-start)/2;
}
return -1;
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | c74b3eed71ad732bf5bc18995a65c555 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | // JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.io.*;
public class Eshan {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.0000000");
final static int mod = (int) (1e9 + 7);
final static int MAX = Integer.MAX_VALUE;
final static int MIN = Integer.MIN_VALUE;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
int t = readInt();
// int t = 1;
preprocess();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt(), k = readInt();
if (k == 1) {
out.println("YES");
for (int i = 1; i <= n; i++)
out.println(i);
return;
}
if ((n & 1) == 1 && (k & 1) == 1) {
out.println("NO");
return;
}
if (((n * k) / 2) % k != 0) {
out.println("NO");
return;
}
out.println("YES");
int x = 1;
while (x < n * k) {
for (int i = 1; i <= k; i++) {
out.print(x + " ");
x += 2;
}
out.println(" ");
}
x = 2;
while (x < n * k) {
for (int i = 1; i <= k; i++) {
out.print(x + " ");
x += 2;
}
out.println(" ");
}
}
private static void preprocess() {
}
// ======================= FOR INPUT ==================================
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return br.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray(int n) throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray(m);
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static long power(long a, long b) {
if (b == 0)
return 1L;
long ans = power(a, b >> 1);
ans *= ans;
if ((b & 1) == 1)
ans *= a;
return ans;
}
private static int mod_power(int a, int b) {
if (b == 0)
return 1;
int temp = mod_power(a, b / 2);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static int multiply(int a, int b) {
return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod);
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
// ==================== Primes using Seive =====================
private static List<Integer> SeivePrime(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return true;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return false;
}
// ==================== SEGMENT TREE (RANGE SUM) =====================
public static class SegmentTree {
int n;
int[] arr, tree, lazy;
SegmentTree(int arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new int[(n << 2)];
this.lazy = new int[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, int val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, int val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
int query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
public void insert(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
}
curr.isEnd = true;
}
public boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ==================== FENWICK TREE ================================
static class FT {
long[] tree;
int n;
FT(int[] arr, int n) {
this.n = n;
this.tree = new long[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
long query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
long getSum(int idx) {
long ans = 0L;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int[][] mat, int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
update(i, j, mat[i - 1][j - 1]);
}
}
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return ans;
}
}
// ==================== OTHER CLASSES ================================
static class Pair implements Comparable<Pair> {
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair o) {
return this.first - o.first;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | a94030156f813e587e3738ce40f512e7 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
public class codeforces {
static int mod = 1000000007;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
int cnt = 1;
int cnt1 = 2;
int a[][] = new int[n][m];
boolean flag = true;
StringBuilder ss = new StringBuilder();
for(int i = 0 ; i<n;i++) {
for(int j = 0 ; j <m;j++) {
if(i%2 == 0) {
a[i][j] = cnt;
cnt+=2;
}
else {
a[i][j] = cnt1;
cnt1+=2;
}
if(a[i][j] > n*m) {
flag = false;
break;
}
else {
ss.append(a[i][j] +" ");
}
}
ss.append("\n");
}
if(flag) {
System.out.println("YES");
System.out.println(ss);
}
else {
System.out.println("NO");
}
}
}
}
static long pow(long a, long b) {
long ans = 1;
long temp = a;
while(b>0) {
if((b&1) == 1) {
ans*=temp;
}
temp = temp*temp;
b = b>>1;
}
return ans;
}
static long ncr(int n, int r) {
return fact(n) / (fact(r) *
fact(n - r));
}
static long fact(long n)
{
long res = 1;
for (int i = 2; i <= n; i++) {
res = (res * i);
}
return res;
}
static long gcd(long a, long b) {
if(b == 0) {
return a;
}
return gcd(b , a%b);
}
static ArrayList<Integer> factor(long n) {
ArrayList<Integer> al = new ArrayList<>();
for(int i = 1 ; i*i<=n;i++) {
if(n%i == 0) {
if(n/i == i) {
al.add(i);
}
else {
al.add(i);
al.add((int) (n/i));
}
}
}
return al;
}
static class Pair implements Comparable<Pair>{
int a;
int b ;
int c;
int d;
Pair(int a, int b, int c, int d){
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
@Override
public int compareTo(Pair o) {
// TODO Auto-generated method stub
int temp = (this.b - o.b);
if(temp == 0) {
int temp1 = this.a - o.a;
if(temp1 == 0) {
return this.c - o.c;
}
return temp1;
}
return temp;
}
}
static ArrayList<Integer> sieve(int n) {
boolean a[] = new boolean[n+1];
Arrays.fill(a, false);
for(int i = 2 ; i*i <=n ; i++) {
if(!a[i]) {
for(int j = 2*i ; j<=n ; j+=i) {
a[j] = true;
}
}
}
ArrayList<Integer> al = new ArrayList<>();
for(int i = 2 ; i <=n;i++) {
if(!a[i]) {
al.add(i);
}
}
return al;
}
static ArrayList<Long> pf(long n) {
ArrayList<Long> al = new ArrayList<>();
while(n%2 == 0) {
al.add(2l);
n = n/2;
}
for(long i = 3 ; i*i<=n ; i+=2) {
while(n%i == 0) {
al.add(i);
n = n/i;
}
}
if(n>2) {
al.add( n);
}
return al;
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | d33b0f634f8eda7b4a4c191b90aed343 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.Scanner;
public class test2{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] arr = new int[1010][1010];
int cnt = sc.nextInt();
while(cnt-- > 0)
{
int n = sc.nextInt();
int k = sc.nextInt();
int odd = 1;
int even = 2;
boolean flag = false;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < k; ++j)
{
if(i % 2 == 0)
{
arr[i][j] = odd;
if(odd >n*k) flag = true;
odd +=2;
}
else
{
arr[i][j] = even;
if(even > n*k)
{
flag=true;
}
even+=2;
}
}
}
if(flag)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < k; ++j)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | dc2c0c649fb28b100463315a136c4222 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int tc=sc.nextInt();
while(tc>0)
{
int n=sc.nextInt();
int k=sc.nextInt();
int i,j;
if(k==1)
{
System.out.println("YES");
for(i=1;i<=n;i++)
System.out.println(i);
}
else if(n % 2==1)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
int e=1,c1=1,c2=2;
for(i=0;i<n;i++)
{
if(e==1)
e=0;
else
e=1;
for(j=0;j<k;j++)
{
if(e==1)
{
System.out.print(c2+" ");
c2+=2;
}
else
{
System.out.print(c1+" ");
c1+=2;
}
}
System.out.print("\n");
}
}
--tc;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 67588187df57ae5550e629c4f8f5ed4a | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class C_Okea {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tasks = in.nextInt();
while (tasks-- > 0) {
int shelfCount = in.nextInt();
int shelfLength = in.nextInt();
if (shelfLength == 1) {
System.out.println("YES");
for (int i = 0; i < shelfCount; i++) {
System.out.println(i + 1);
}
continue;
}
if (shelfCount % 2 == 1) {
System.out.println("NO");
continue;
}
System.out.println("YES");
for (int i = 0; i < shelfCount; i++) {
for (int j = 0; j < shelfLength; j++) {
System.out.print(i + 1 + j * shelfCount + " ");
}
System.out.println();
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 5abd01516e09941a338f38ac534d33bf | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.util.*;
import java.io.*;
public class codeforceb
{
//ArrayList<Integer> arr2=new ArrayList<Integer>();
static ArrayList<Long> arr1=new ArrayList<Long>();
//Map<Integer,Integer> hm=new HashMap<Integer,Integer>();
//static Set<Integer> st1=new HashSet<Integer>();
static Set<Integer> prime=new HashSet<Integer>();
static int inf = Integer.MAX_VALUE ;
static long infL = Long.MAX_VALUE ;
static int mod=1000000007;
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
// seive to find prime for a given range except 1;
public static void main (String[] args) throws java.lang.Exception
{
/*Arrays.sort(time,new Comparator<Pair>(){
public int compare(Pair p1,Pair p2){
if(p1.a==p2.a)
return p1.b-p2.b;
return p1.a-p2.a;
}
});*/
//int r[][]= {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}
// };
int t=sc.nextInt();
//seive(1000000);
// seive to find prime for a given range except 1;
while(t-->0)
{
solve();
}
out.close();
}
public static void solve() {
int n=sc.nextInt();
int k=sc.nextInt();
if(k==1) {
out.println("Yes");
for(int i=1;i<=n;i++) {
out.println(i);
}
}
else if(n%2==1 || (n*k)%2!=0) {
out.println("No");
}
else {
int o=1,e=2;
out.print("Yes");
for(int i=1;i<=n;i++) {
if(i%2==1) {
out.println();
for(int j=1;j<=k;j++) {
out.print(o+" ");
o+=2;
}
}
else {
out.println();
for(int j=1;j<=k;j++) {
out.print(e+" ");
e+=2;
}
}
}
out.println();
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
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 class Pair{
int a;
int b;
Pair(int x, int y)
{
this.a = x;
this.b = y;
}
}
static boolean isPrime(long x)
{
if(x==1)
return false;
if(x<=3)
return true;
if(x%2==0 || x%3==0)
return false;
for(int i=5;i<=Math.sqrt(x);i+=2)
if(x%i==0)
return false;
return true;
}
static long gcd(long a,long b)
{
return (b==0)?a:gcd(b,a%b);
}
static int gcd(int a,int b)
{
return (b==0)?a:gcd(b,a%b);
}
static int abs(int n) {
if(n<0) return n*-1;
else return n;
}
static long abs(long n) {
if(n<0) return n*-1;
else return n;
}
public static long myPow(long x, long n) {
if(x==0)
return 0;
if(n==0)
return 1;
if(n==1)
return x;
long calc=myPow((long)x,n/2);
//System.out.println(calc+" "+n/2);
if(Math.abs(n)%2==0)
return (calc*calc);
else
return (x*calc*calc);
}
public static long lcm(long a , long b){
return a * (b/gcd(a,b));
}
public static int lcm(int a , int b){
return (a * b)/gcd(a,b);
}
public static void swap(int[] arr, int left , int right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
public static void swap(char[] arr, int left , int right){
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
public static void reverse(int[] arr){
int left = 0;
int right = arr.length-1;
while(left <= right){
swap(arr, left,right);
left++;
right--;
}
}
public static void seive(int val) {
int arr[]=new int[val+1];
for(int i=2;i<=val;i++) {
if(arr[i]==0) {
prime.add(i);
for(int j=i;j<=val;j+=i) arr[j]=1;
}
else continue;
}
}
public static long maxL(long a,long b) {
return (long)Math.max(a,b);
}
public static int max(int a,int b) {
return (int)Math.max(a,b);
}
public static int max(int a,int b,int c) {
return max(a,max(b,c));
}
public static int min(int a,int b) {
return (int)Math.min(a,b);
}
public static long min(long a,long b) {
return (long)Math.min(a,b);
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 2d799ffcc26ebe0b86473b3d7dadfe99 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | // package div_2_770;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C{
public static void main(String[] args){
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int k=sc.nextInt();
if(n==1 && k==1) {
System.out.println("yes");
System.out.println(1);
}
else {
if(k==1) {
System.out.println("yes");
for(int i=1;i<=n;i++)System.out.println(i);
}
else {
if((n&1)==1)System.out.println("no");
else {
System.out.println("yes");
for(int i=1,j=1;i<=n*k;i+=2,j++) {
System.out.print(i+" ");
if(j==k)System.out.println();
}
for(int i=2,j=1;i<=n*k;i+=2,j++) {
System.out.print(i+" ");
if(j==k)System.out.println();
}
}
}
}
}
}
static int pow(int a,int b) {
if(b==0)return 1;
if(b==1)return a;
return a*pow(a,b-1);
}
static class pair {
int x;int y;
pair(int x,int y){
this.x=x;
this.y=y;
}
}
static ArrayList<Integer> primeFac(int n){
ArrayList<Integer>ans = new ArrayList<Integer>();
int lp[]=new int [n+1];
Arrays.fill(lp, 0); //0-prime
for(int i=2;i<=n;i++) {
if(lp[i]==0) {
for(int j=i;j<=n;j+=i) {
if(lp[j]==0) lp[j]=i;
}
}
}
int fac=n;
while(fac>1) {
ans.add(lp[fac]);
fac=fac/lp[fac];
}
print(ans);
return ans;
}
static ArrayList<Long> prime_in_given_range(long l,long r){
ArrayList<Long> ans= new ArrayList<>();
int n=(int)Math.sqrt(r)+1;
int prime[]=sieve_of_Eratosthenes(n);
long res[]=new long [(int)(r-l)+1];
for(int i=0;i<=r-l;i++) {
res[i]=i+l;
}
for(int i=0;i<prime.length;i++) {
if(prime[i]==1) {
System.out.println(2);
for(int j=Math.max((int)i*i, (int)(l+i-1)/i*i);j<=r;j+=i) {
res[j-(int)l]=0;
}
}
}
for(long i:res) if(i!=0)ans.add(i);
return ans;
}
static int [] sieve_of_Eratosthenes(int n) {
int prime[]=new int [n];
Arrays.fill(prime, 1); // 1-prime | 0-not prime
prime[0]=prime[1]=0;
for(int i=2;i<n;i++) {
if(prime[i]==1) {
for(int j=i*i;j<n;j+=i) {
prime[j]=0;
}
}
}
return prime;
}
static long binpow(long a,long b) {
long res=1;
if(b==0)return 1;
if(a==0)return 0;
while(b>0) {
if((b&1)==1) {
res*=a;
}
a*=a;
b>>=1;
}
return res;
}
static void print(int a[]) {
System.out.println(a.length);
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
System.out.println(a.length);
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static long rolling_hashcode(String s ,int st,int end,long Hashcode,int n) {
if(end>=s.length()) return -1;
int mod=1000000007;
Hashcode=Hashcode-(s.charAt(st-1)*(long)Math.pow(27,n-1));
Hashcode*=10;
Hashcode=(Hashcode+(long)s.charAt(end))%mod;
return Hashcode;
}
static long hashcode(String s,int n) {
long code=0;
for(int i=0;i<n;i++) {
code+=((long)s.charAt(i)*(long)Math.pow(27, n-i-1)%1000000007);
}
return code;
}
static void print(ArrayList<Integer> a) {
System.out.println(a.size());
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
int [] fastArray(int n) {
int a[]=new int [n];
for(int i=0;i<n;i++) {
a[i]=nextInt();
}
return a;
}
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\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 098eba841b0171e2ecfb1cbbcc6fc824 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int k=sc.nextInt();
if(k==1){
System.out.println("YES");
for(int i=1;i<=n;i++){
System.out.println(i);
}
continue;
}
if(((n*k)%2)==0){
if(n%2==0){
int val1=1;
int val2=2;
System.out.println("YES");
while(n!=0){
int tempk1=k;
int val=val1;
while(tempk1!=0){
System.out.print(val1+" ");
val1=val1+2;
tempk1--;
}
System.out.println();
n--;
tempk1=k;
val=val2;
while(tempk1!=0){
System.out.print(val2+" ");
val2=val2+2;
tempk1--;
}
System.out.println();
n--;
}
}else{
System.out.println("NO");
}
}else{
System.out.println("NO");
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 989fd157e2b93e3f8cebbd7cf4547df3 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | //package com.company;
import java.io.*;
import java.util.*;
public class Main{
static boolean[] primecheck = new boolean[1000002];
static ArrayList<Integer>[] adj;
static int[] vis;
static int mod = (int)1e9 + 7;
public static void main(String[] args) {
OutputStream outputStream = System.out;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t = 1;
t = in.ni();
for (int i = 0; i < t; i++) {
solver.solve(in, out);
}
out.close();
}
static class PROBLEM {
public void solve(FastReader in, PrintWriter out) {
int n = in.ni(), k = in.ni();
if((n*k & 1) == 1){
if(k != 1) out.println("NO");
else{
out.println("YES");
for (int i = 0; i < n * k; i++) {
out.println(i+1 + " ");
}
}
}else{
if((n & 1) == 1) out.println("NO");
else {
out.println("YES");
int c = 1, r = 0;
for (int i = 0; i < (n * k) / 2; i++) {
out.print(c + " ");
c += 2;
r++;
if (r == k){
out.println();
r = 0;
}
}
c = 2;
r = 0;
for (int i = 0; i < (n * k) / 2; i++) {
out.print(c + " ");
c += 2;
r++;
if (r == k){
out.println();
r = 0;
}
}
}
}
// int n = in.ni(), a[] = in.ra(n);
// boolean pos = false;
//
// ArrayList<Integer> oddid = new ArrayList<>();
// Map<Integer, Integer> mp = new HashMap<>();
// ArrayList<Pair<Integer, Integer>> p = new ArrayList<>();
//
// for (int i = 0; i < n - 1; i++) {
// if((a[i]&1) == 1){
// pos = true;
// oddid.add(a[i]);
// mp.put(a[i], i);
// p.add(new Pair<>(a[i], i));
// }
// }
//
// if(p){
//
//
//
// }else out.println(-1);
}
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static boolean isPoT(int n){
double p = Math.log((double)n)/Math.log(2D);
return (p == (int)p);
}
static void dfs(int i){
vis[i] = 1;
for(int j: adj[i]){
if(vis[j] == 0) {
dfs(j);
}
}
}
static long sigmaK(long k){
return (k*(k+1))/2;
}
static void swap(int[] a, int l, int r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
static int binarySearch(int[] a, int l, int r, int x){
if(r>=l){
int mid = l + (r-l)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) return binarySearch(a, l, mid-1, x);
else return binarySearch(a,mid+1, r, x);
}
return -1;
}
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 / gcd(a, b)) * b;
}
static boolean isSquare(double a) {
boolean isSq = false;
double b = Math.sqrt(a);
double c = Math.sqrt(a) - Math.floor(b);
if (c == 0) isSq = true;
return isSq;
}
static long fast_pow(long a, long b) { //Jeel bhai OP
if(b == 0)
return 1L;
long val = fast_pow(a, b / 2);
if(b % 2 == 0)
return val * val % mod;
else
return val * val % mod * a % mod;
}
static int exponentMod(int A, int B, int C) {
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (int) ((y + C) % C);
}
// static class Pair implements Comparable<Pair>{
//
// int x;
// int y;
//
// Pair(int x, int y){
// this.x = x;
// this.y = y;
// }
//
// public int compareTo(Pair o){
//
// int ans = Integer.compare(x, o.x);
// if(o.x == x) ans = Integer.compare(y, o.y);
//
// return ans;
//
//// int ans = Integer.compare(y, o.y);
//// if(o.y == y) ans = Integer.compare(x, o.x);
////
//// return ans;
// }
// }
static class Tuple implements Comparable<Tuple>{
int x, y, id;
Tuple(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
public int compareTo(Tuple o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public int compareToY(Pair<U, V> b) {
int cmpU = y.compareTo(b.y);
return cmpU != 0 ? cmpU : x.compareTo(b.x);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
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 ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
char nc() {
return next().charAt(0);
}
boolean nb() {
return !(ni() == 0);
}
// boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nli() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] ra(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = ni();
return array;
}
}
private static int[] mergeSort(int[] array) {
//array.length replaced with ctr
int ctr = array.length;
if (ctr <= 1) {
return array;
}
int midpoint = ctr / 2;
int[] left = new int[midpoint];
int[] right;
if (ctr % 2 == 0) {
right = new int[midpoint];
} else {
right = new int[midpoint + 1];
}
for (int i = 0; i < left.length; i++) {
left[i] = array[i];
}
for (int i = 0; i < right.length; i++) {
right[i] = array[i + midpoint];
}
left = mergeSort(left);
right = mergeSort(right);
int[] result = merge(left, right);
return result;
}
private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length || rightPointer < right.length) {
if (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
} else if (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
return result;
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 5ca7f4e63f2e9ff15aa52f84b0a4b890 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class CF15 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc>0){
tc--;
int n = sc.nextInt();
int k = sc.nextInt();
if(k==1){
System.out.println("Yes");
for(int i=0;i<n;i++){
System.out.println(i+1);
}
continue;
}
if(n%2==1){
System.out.println("No");
}else{
System.out.println("Yes");
for(int i=1;i<=n;i++) {
for(int j=0;j<k;j++) {
System.out.print((i+j*n)+" ");
}
System.out.println();
}
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 1eaebf8a088bb5b26b81f40fc5f1e514 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class pariNa {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long t=sc.nextLong();
//finalAnswer.append().append('\n');
outer:
while(t-->0) {
int a=sc.nextInt();
int b=sc.nextInt();
StringBuilder finalAnswer=new StringBuilder();
if(a==1 && b==1) {
System.out.println("YES");
System.out.println(1);
}
else if(b==1) {
System.out.println("YES");
for(int i=0;i<a;i++) {
System.out.println(i+1);
}
}
else{
finalAnswer.append("YES").append('\n');
// System.out.println("YES");
int e=2,o=1,count=0;;
for(int i=0;;i++) {
for(int j=0;j<b;j++) {
finalAnswer.append(o+" ");
// System.out.print(o+" ");
o+=2;
count++;
if(o>((a*b)+2)) {
System.out.println("NO");
continue outer;
}
}
// System.out.println();
finalAnswer.append('\n');
for(int j=0;j<b;j++) {
finalAnswer.append(e+" ");
// System.out.print(e+" ");
e+=2;
count++;
if(e>((a*b)+2)) {
System.out.println("NO");
continue outer;
}
}
finalAnswer.append('\n');
// System.out.println();
if(count==(a*b)){
System.out.println(finalAnswer);
continue outer;
}
}
}
}
//System.out.println(finalAnswer);
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | a358aca9d0b09c36255c0e2083f28693 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
public class Test
{
final static FastReader fr = new FastReader();
final static PrintWriter out = new PrintWriter(System.out) ;
static long mod = (long)1e9 + 7 ;
static int dp[] ;
static void solve()
{
int n = fr.nextInt(), k = fr.nextInt();
int prod = n * k ;
if (k == 1)
{
out.println("YES");
for (int i = 0; i < n ; i++)
{
out.println(i+1);
}
return;
}
if (n%2 != 0){
out.println("NO");
return;
}
out.println("YES");
int odd = 1, even = 2 ;
for (int i = 1 ; i <= n ; i++)
{
if (i%2 == 0){
for (int j = 0 ; j < k ; j++)
{
out.print(even+" ");
even += 2 ;
}
}
else {
for (int j = 0 ; j < k ; j++)
{
out.print(odd+" ");
odd += 2 ;
}
}
out.println();
}
}
public static void main(String[] args)
{
int t = 1 ;
t = fr.nextInt() ;
while (t-- > 0)
{
solve() ;
}
out.close() ;
}
static long getMax(long ... a)
{
long max = Long.MIN_VALUE ;
for (long x : a) max = Math.max(x, max) ;
return max ;
}
static long getMin(long ... a)
{
long max = Long.MAX_VALUE ;
for (long x : a) max = Math.min(x, max) ;
return max ;
}
static long fastPower(double a, long b)
{
double ans = 1 ;
while (b > 0)
{
if ((b & 1) != 0) ans *= a ;
a *= a ;
b >>= 1 ;
}
return (long)(ans + 0.5) ;
}
static long fastPower(long a, long b, long mod)
{
long ans = 1 ;
while (b > 0)
{
if ((b&1) != 0) ans = (ans%mod * a%mod) %mod;
b >>= 1 ;
a = (a%mod * a%mod)%mod ;
}
return ans ;
}
static int lower_bound(List<Integer> arr, int key)
{
int pos = Collections.binarySearch(arr, key) ;
if (pos < 0)
{
pos = - (pos + 1) ;
}
return pos ;
}
static int upper_bound(List<Integer> arr, int key)
{
int pos = Collections.binarySearch(arr, key);
pos++ ;
if (pos < 0)
{
pos = -(pos) ;
}
return pos ;
}
static int upper_bound(int arr[], int key)
{
int start = 0 , end = arr.length - 1 ;
int ans = -1 ;
while (start <= end)
{
int mid = start + ((end - start) >> 1) ;
if (arr[mid] == key) ans = mid ;
if (arr[mid] <= key) start = mid + 1 ;
else end = mid-1 ;
}
return ans ;
}
static int lower_bound(int arr[], int key)
{
int start = 0 , end = arr.length -1;
int ans = -1 ;
while (start <= end)
{
int mid = start + ((end - start )>>1) ;
if (arr[mid] == key) {
ans = mid ;
}
if (arr[mid] >= key){
end = mid - 1 ;
}
else start = mid + 1 ;
}
return ans ;
}
static class Pair{
long x;
long y ;
Pair(long x, long y){
this.x = x ;
this.y= y ;
}
}
static long gcd(long a, long b)
{
if (b == 0) return a ;
return gcd(b, a%b) ;
}
static long lcm(long a, long b)
{
long lcm = (a * b)/gcd(a, b) ;
return lcm ;
}
static List<Long> seive(int n)
{
// all are false by default
// false -> prime, true -> composite
int nums[] = new int[n+1] ;
for (int i = 2 ; i <= Math.sqrt(n); i++)
{
if (nums[i] == 0)
{
for (int j = i*i ; j <= n ; j += i)
{
nums[j] = 1 ;
}
}
}
long freq[] = new long[1000001] ;
freq[2] = 1 ;
for (int i = 2 ; i <= n ; i++)
{
if (nums[i] == 0) freq[i] = freq[i-1]+1 ;
else freq[i] = freq[i-1] ;
}
ArrayList<Long>primes = new ArrayList<>() ;
for (int i = 2 ; i <=n ; i++)
{
if (nums[i] == 0) primes.add(i*1l) ;
}
return primes ;
}
static boolean isVowel(char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return true ;
return false ;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 675b40e3d15e07115d3668117eca149b | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.awt.*;
import java.io.*;
import java.util.*;
public class Main {
static int N = 100001;
// Array to store inverse of 1 to N
static long[] factorialNumInverse = new long[N + 1];
// Array to precompute inverse of 1! to N!
static long[] naturalNumInverse = new long[N + 1];
// Array to store factorial of first N numbers
static long[] fact = new long[N + 1];
// Function to precompute inverse of numbers
public static void InverseofNumber(int p)
{
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for(int i = 2; i <= N; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p;
}
public static void InverseofFactorial(int p)
{
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// Precompute inverse of natural numbers
for(int i = 2; i <= N; i++)
factorialNumInverse[i] = (naturalNumInverse[i] *
factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to N
public static void factorial(int p)
{
fact[0] = 1;
// Precompute factorials
for(int i = 1; i <= N; i++)
{
fact[i] = (fact[i - 1] * (long)i) % p;
}
}
public static long ncp(int N, int R, int p)
{
// n C r = n!*inverse(r!)*inverse((n-r)!)
long ans = ((fact[N] * factorialNumInverse[R]) %
p * factorialNumInverse[N - R]) % p;
return ans;
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Reader.nextInt();
while ( t > 0){
int n = Reader.nextInt();
int k = Reader.nextInt();
if ( k!=1){
int v = n*k;
if ( v%2== 1){
output.write("NO\n");
}
else {
int odd = (v-1)/2;
odd++;
if (odd%k != 0) {
output.write("NO\n");
}
else {
output.write("YES\n");
int i = 1;
int m = v / 2;
int c = 0;
while (m > 0) {
output.write(i + " ");
i += 2;
c++;
if (c == k) {
output.write("\n");
c = 0;
}
m--;
}
i = 2;
m = v / 2;
c = 0;
while (m > 0) {
output.write(i + " ");
i += 2;
c++;
if (c == k) {
output.write("\n");
c = 0;
}
m--;
}
}
}
}
else{
int i = 1;
output.write("YES\n");
while ( i <= n){
output.write(i+"\n");
i++;
}
}
t--;
}
output.flush();
}
private static long helper(int[] weight,int[] amount,int n,int totalAllowed){
long dp[][] = new long[2][totalAllowed+1];
int i = 0;
while ( i<= totalAllowed){
if ( weight[0]>i){
dp[0][i] = 0;
}
else{
dp[0][i] = amount[0];
}
i++;
}
int current = 1;
int previous = 0;
i = 1;
while ( i < n){
int j = 0;
while ( j <= totalAllowed){
if ( weight[i] <= j){
dp[current][j] = max(dp[previous][j],dp[previous][(j-weight[i])]+amount[i]);
}
else{
dp[current][j] = dp[previous][j];
}
j++;
}
if ( current == 1){
previous = 1;
current = 0;
}
else{
previous =0;
current =1;
}
i++;
}
return dp[previous][totalAllowed];
}
public static int log2(int N)
{
// calculate log2 N indirectly
// using log() method
int result = (int)(Math.log(N) / Math.log(2));
return result;
}
private static int bs(int low,int high,ArrayList<Integer> array,int find){
if ( low <= high ){
int mid = low + (high-low)/2;
if ( array.get(mid) > find){
high = mid -1;
return bs(low,high,array,find);
}
else if ( array.get(mid) < find){
low = mid+1;
return bs(low,high,array,find);
}
return mid;
}
return -1;
}
private static long max(long a, long b) {
return Math.max(a,b);
}
private static long min(long a,long b){
return Math.min(a,b);
}
public static long modularExponentiation(long a,long b,long mod){
if ( b == 1){
return a;
}
else{
long ans = modularExponentiation(a,b/2,mod)%mod;
if ( b%2 == 1){
return (a*((ans*ans)%mod))%mod;
}
return ((ans*ans)%mod);
}
}
public static long sum(long n){
return (n*(n+1))/2;
}
public static long abs(long a){
return a < 0 ? (-1*a) : a;
}
public static long gcd(long a,long b){
if ( a == 0){
return b;
}
else{
return gcd(b%a,a);
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
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() );
}
static long nextLong() throws IOException{
return Long.parseLong(next());
}
}
class NComparator implements Comparator<Node>{
@Override
public int compare(Node o1, Node o2) {
if ( o2.b > o1.b){
return 1;
}
else if ( o2.b < o1.b){
return -1;
}
else {
return 0;
}
}
}
class DComparator implements Comparator<Long>{
@Override
public int compare(Long o1, Long o2) {
if ( o2 > o1){
return 1;
}
else if ( o2 < o1){
return -1;
}
else{
return 0;
}
}
}
class Node{
long a;
long b;
Node(long A,long B){
a = A;
b = B;
}
}
/*
4 4
bbaa
abba
abaa
aaaa
*/ | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 6ef13415336311f2643cb2a69f6485bf | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.math.*;
public class Solution {
static int mod=1000000007;
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.out.println();
Scanner sc = new Scanner(System.in);
int tt=1;
//
tt = sc.nextInt();
//
while (tt-- > 0) {
int n=sc.nextInt();
int k=sc.nextInt();
if(k==1) {
System.out.println("YES");
for(int i=1;i<=n;i++) {
System.out.println(i);
}
continue;
}
int pr=n*k;
if(pr%2==1) {
System.out.println("NO");
continue;
}
pr=pr/2;
if(pr%k!=0) {
System.out.println("NO");
continue;
}
pr=2*pr;
System.out.println("YES");
int idx=0;
for(int i=0;i<n/2;i++) {
for(int j=0;j<k;j++) {
int val=2*idx+1;
System.out.print(val+" ");
idx++;
}
System.out.println();
}
idx=1;
for(int i=0;i<n/2;i++) {
for(int j=0;j<k;j++) {
int val=2*idx;
System.out.print(val+" ");
idx++;
}
System.out.println();
}
}
sc.close();
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 2c8b2ac386a1b9ef67faec00bcd8b083 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
private static final boolean OFFLINE_WITHOUT_FILES = false;
public void solve() throws Exception {
int t = readInt();
while (--t >= 0) {
int n = readInt(), k = readInt();
if (k == 1) {
out.println("YES");
for (int i = 1; i <= n; ++i) {
out.println(i);
}
continue;
}
int res = n * k;
if (n % 2 == 1) {
out.println("NO");
} else {
boolean f = true;
int ch = 2, nCh = 1;
out.println("YES");
for (int i = 0; i < n; ++i) {
f = !f;
for (int j = 0; j < k; ++j) {
if (f) {
out.print(nCh);
nCh+=2;
} else {
out.print(ch);
ch+=2;
}
if (j != k - 1) {
out.print(" ");
}
}
out.println();
}
}
}
}
public static void main(String[] args) {
new Main();
}
BufferedReader in;
PrintWriter out;
StringTokenizer tok;
Main() {
try {
long timeStart = System.currentTimeMillis();
if (ONLINE_JUDGE || OFFLINE_WITHOUT_FILES) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
tok = new StringTokenizer("");
solve();
out.close();
long timeEnd = System.currentTimeMillis();
System.err.println("time = " + (timeEnd - timeStart) + " compiled");
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
String readLine() throws IOException {
return in.readLine();
}
String delimiter = " ";
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
String nextLine = readLine();
if (nextLine == null)
return null;
tok = new StringTokenizer(nextLine);
}
return tok.nextToken(delimiter);
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
int[] sortArrayInt(int[] a) {
Integer[] arr = new Integer[a.length];
for (int i = 0; i < a.length; i++) {
arr[i] = a[i];
}
Arrays.sort(arr);
for (int i = 0; i < a.length; i++) {
a[i] = arr[i];
}
return a;
}
void sortArrayLong(long[] a) {
Long[] arr = new Long[a.length];
for (int i = 0; i < a.length; i++) {
arr[i] = a[i];
}
Arrays.sort(arr);
for (int i = 0; i < a.length; i++) {
a[i] = arr[i];
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 7272318d42c8848fc980070e51c43954 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class JaiShreeRam{
static Scanner in=new Scanner();
static long mod = 998244353;
static List<List<Integer>> adj;
static int seive[]=new int[1000001];
static int dp[]=new int[1001];
public static void main(String[] args) throws Exception{
int z=in.readInt();
while(z-->0) {
solve();
}
}
static void solve() {
int n=in.readInt();
int k=in.readInt();
if(k==1) {
print("YES");
for(int i=1;i<=n;i++) {
print(i);
}
return;
}
if(n%2!=0) {//||(n*k)%4!=0) {
print("NO");
}
else {
int s1=1;
int s2=2;
print("YES");
int ans[][]=new int[n][k];
for(int i=0;i<n;i+=2) {
for(int j=0;j<k;j++) {
ans[i][j]=s1;
ans[i+1][j]=s2;
s1+=2;
s2+=2;
}
print(ans[i]);
print(ans[i+1]);
}
}
}
static boolean isPalin(String s) {
int i=0,j=s.length()-1;
while(i<=j) {
if(s.charAt(i)!=s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
static int knapsack(int W, int wt[],int val[], int n){
int []dp = new int[W + 1];
for (int i = 1; i < n + 1; i++) {
for (int w = W; w >= 0; w--) {
if (wt[i - 1] <= w) {
dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]);
}
}
}
return dp[W];
}
static void seive() {
Arrays.fill(seive, 1);
seive[0]=0;
seive[1]=0;
for(int i=2;i*i<1000001;i++) {
if(seive[i]==1) {
for(int j=i*i;j<1000001;j+=i) {
if(seive[j]==1) {
seive[j]=0;
}
}
}
}
}
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 int[] nia(int n){
int[] arr= new int[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static long[] nla(int n){
long[] arr= new long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static int[] nia1(int n){
int[] arr= new int[n+1];
int i=1;
while(i<=n){
arr[i++]=in.readInt();
}
return arr;
}
static Integer[] nIa(int n){
Integer[] arr= new Integer[n];
int i=0;
while(i<n){
arr[i++]=in.readInt();
}
return arr;
}
static Long[] nLa(int n){
Long[] arr= new Long[n];
int i=0;
while(i<n){
arr[i++]=in.readLong();
}
return arr;
}
static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b, a%b);
}
static void print(long i) {
System.out.println(i);
}
static void print(Object o) {
System.out.println(o);
}
static void print(int a[]) {
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Long> a) {
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(Object a[]) {
for(Object i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static class Scanner{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String readString() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
double readDouble() {
return Double.parseDouble(readString());
}
int readInt() {
return Integer.parseInt(readString());
}
long readLong() {
return Long.parseLong(readString());
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 07b2b46723f65098b18a123b7e9875d5 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static FastScanner sc = new FastScanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder();
static long mod = (long) 1e9 + 7;
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
for(int i = 0; i < t; i++) solve();
pw.flush();
}
public static void solve() {
int n = sc.nextInt();
int k = sc.nextInt();
if(k == 1){
pw.println("YES");
for(int i = 1; i <= n*k; i++){
pw.println(i);
}
}else if(n % 2 == 0){
pw.println("YES");
Deque<Integer> odd = new ArrayDeque<Integer>();
Deque<Integer> even = new ArrayDeque<Integer>();
for(int i = 1; i <= n*k; i++){
if(i % 2 == 0){
even.add(i);
}else{
odd.add(i);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < k; j++){
if(i % 2 == 0){
sb.append(even.poll()).append(" ");
}else{
sb.append(odd.poll()).append(" ");
}
}
pw.println(sb.toString().trim());
sb.setLength(0);
}
}else{
pw.println("NO");
}
}
static class GeekInteger {
public static void save_sort(int[] array) {
shuffle(array);
Arrays.sort(array);
}
public static int[] shuffle(int[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
int randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
public static void save_sort(long[] array) {
shuffle(array);
Arrays.sort(array);
}
public static long[] shuffle(long[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
long randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
}
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public FastScanner(FileReader in) {
reader = new BufferedReader(in);
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String[] nextArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
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;
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 2a50fa50916ff3024fffc33dc7fe477e | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main (String[] args) throws IOException {
Kattio io = new Kattio();
int t = io.nextInt();
for (int ii=0; ii<t; ii++) {
int n = io.nextInt();
int k = io.nextInt();
if (k == 1) {
System.out.println("YES");
for (int i=1; i<=n; i++) {
System.out.println(i);
}
continue;
}
if (n == 1) {
System.out.println("NO");
continue;
}
int oddrow = 0;
int evenrow = 0;
if (n%2 == 0) {
oddrow = n/2;
evenrow = n/2;
} else {
oddrow = n/2 + 1;
evenrow = n/2;
}
int evens = (n * k)/2;
int odds = (n * k + 1)/2;
//System.out.println(oddrow + " " + evenrow);
if ((evens % evenrow == 0 && odds % oddrow == 0) && (evens/evenrow == odds/oddrow)) {
System.out.println("YES");
int curodd = 1;
for (int i=0; i<oddrow; i++) {
for (int j=0; j<k; j++) {
System.out.print(curodd + " ");
curodd += 2;
}
System.out.println();
}
int cureven = 2;
for (int i=0; i<evenrow; i++) {
for (int j=0; j<k; j++) {
System.out.print(cureven + " ");
cureven += 2;
}
System.out.println();
}
} else {
System.out.println("NO");
}
}
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 7325c3a670b95af7c65caa90ad19827e | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
while(tes-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
int i,j;
if(k==1)
{
System.out.println("YES");
for(i=1;i<=n;i++)
System.out.println(i);
continue;
}
if(n%2==1)
{
System.out.println("NO");
continue;
}
System.out.println("YES");
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
System.out.print((j*n)+i+1+" ");
System.out.println();
}
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Long> arr, int low, int high, long x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
//Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Long> ar,int lo , int hi, long k)
{
//Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
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 (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | b59d77cf836e63b44e9a65300bf70734 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
/* Name of the class has to be "Main" only if the class is public. */
public class Okea
{
static FastScanner sc= new FastScanner();
public static void main (String[] args) throws Exception
{
// your code goes here
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
int arr[][] = new int[n][k];
int val = 1;
for(int i=0;i<k;i++){
for(int j=0;j<n;j++){
arr[j][i]=val;
val++;
}
}
if(k==1||n%2==0){
System.out.println("YES");
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}else{
System.out.println("NO");
}
}
System.out.println(sb);
}
public static boolean isPrime(long n)
{
if(n < 2) return false;
if(n == 2 || n == 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
long sqrtN = (long)Math.sqrt(n)+1;
for(long i = 6L; i <= sqrtN; i += 6) {
if(n%(i-1) == 0 || n%(i+1) == 0) return false;
}
return true;
}
public static long gcd(long a, long b)
{
if(a > b)
a = (a+b)-(b=a);
if(a == 0L)
return b;
return gcd(b%a, a);
}
public static ArrayList<Integer> findDiv(int N)
{
//gens all divisors of N
ArrayList<Integer> ls1 = new ArrayList<Integer>();
ArrayList<Integer> ls2 = new ArrayList<Integer>();
for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++)
if(N%i == 0)
{
ls1.add(i);
ls2.add(N/i);
}
Collections.reverse(ls2);
for(int b: ls2)
if(b != ls1.get(ls1.size()-1))
ls1.add(b);
return ls1;
}
public static void sort(int[] arr)
{
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long power(long x, long y, long p)
{
//0^0 = 1
long res = 1L;
x = x%p;
while(y > 0)
{
if((y&1)==1)
res = (res*x)%p;
y >>= 1;
x = (x*x)%p;
}
return res;
}
static class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | e2cd43172120da945720c275dd5e9f74 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
public class MainClass {
static FastScanner fs;
static FastWriter fw;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
private static final int[][] kdir = new int[][]{{-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}};
private static final int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
private static final int iMax = (int) (1e9 + 100), iMin = (int) (-1e9 - 100);
private static final long lMax = (long) (1e18 + 100), lMin = (long) (-1e18 - 100);
private static final int mod1 = (int) (1e9 + 7);
private static final int mod2 = 998244353;
public static void main(String[] args) throws IOException {
fs = new FastScanner();
fw = new FastWriter();
int t = 1;
t = fs.nextInt();
while (t-- > 0) {
solve();
}
fw.out.close();
}
private static class myPair{
int a; int b;
myPair(int a, int b){
this.a = a;
this.b = b;
}
}
private static void solve() {
int n = fs.nextInt();
int k = fs.nextInt();
if(k == 1) {
fw.out.println("YES");
int j = 1;
for (int i = 0; i < n; i++) {
fw.out.println(j++);
}
}else if(n % 2 != 0){
fw.out.println("NO");
}else{
fw.out.println("YES");
int num = 1;
for(int i = 1; i<=n; i++){
for(int j = 1; j<=k; j++){
fw.out.print(num + n*(j-1) + " ");
}
fw.out.println();
num++;
}
}
}
private static class UnionFind {
private final int[] parent;
private final int[] rank;
UnionFind(int n) {
parent = new int[n + 5];
rank = new int[n + 5];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
private int find(int i) {
if (parent[i] == i)
return i;
return parent[i] = find(parent[i]);
}
private void union(int a, int b) {
a = find(a);
b = find(b);
if (a != b) {
if (rank[a] < rank[b]) {
int temp = a;
a = b;
b = temp;
}
parent[b] = a;
if (rank[a] == rank[b])
rank[a]++;
}
}
}
private static class Calc_nCr {
private final long[] fact;
private final long[] invfact;
private final int p;
Calc_nCr(int n, int prime) {
fact = new long[n + 5];
invfact = new long[n + 5];
p = prime;
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (i * fact[i - 1]) % p;
}
invfact[n] = pow(fact[n], p - 2, p);
for (int i = n - 1; i >= 0; i--) {
invfact[i] = (invfact[i + 1] * (i + 1)) % p;
}
}
private long nCr(int n, int r) {
if (r > n || n < 0 || r < 0) return 0;
return (((fact[n] * invfact[r]) % p) * invfact[n - r]) % p;
}
}
private static long gcd(long a, long b) {
return (b == 0 ? a : gcd(b, a % b));
}
private static long lcm(long a, long b) {
return ((a * b) / gcd(a, b));
}
private static long pow(long a, long b, int mod) {
long result = 1;
while (b > 0) {
if ((b & 1L) == 1) {
result = (result * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long ceilDiv(long a, long b) {
return ((a + b - 1) / b);
}
private static long getMin(long... args) {
long min = lMax;
for (long arg : args)
min = Math.min(min, arg);
return min;
}
private static long getMax(long... args) {
long max = lMin;
for (long arg : args)
max = Math.max(max, arg);
return max;
}
private static boolean isPalindrome(String s, int l, int r) {
int i = l, j = r;
while (j - i >= 1) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
private static List<Integer> primes(int n) {
boolean[] primeArr = new boolean[n + 5];
Arrays.fill(primeArr, true);
for (int i = 2; (i * i) <= n; i++) {
if (primeArr[i]) {
for (int j = i * i; j <= n; j += i) {
primeArr[j] = false;
}
}
}
List<Integer> primeList = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (primeArr[i])
primeList.add(i);
}
return primeList;
}
private static int noOfSetBits(long x) {
int cnt = 0;
while (x != 0) {
x = x & (x - 1);
cnt++;
}
return cnt;
}
private static boolean isPerfectSquare(long num) {
long sqrt = (long) Math.sqrt(num);
return ((sqrt * sqrt) == num);
}
private static class Pair<U, V> {
private final U first;
private final V second;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return first.equals(pair.first) && second.equals(pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
private Pair(U ff, V ss) {
this.first = ff;
this.second = ss;
}
}
private static void randomizeIntArr(int[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInIntArr(arr, i, j);
}
}
private static void randomizeLongArr(long[] arr, int n) {
Random r = new Random();
for (int i = (n - 1); i > 0; i--) {
int j = r.nextInt(i + 1);
swapInLongArr(arr, i, j);
}
}
private static void swapInIntArr(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static void swapInLongArr(long[] arr, int a, int b) {
long temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
private static int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextInt();
return arr;
}
private static long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = fs.nextLong();
return arr;
}
private static List<Integer> readIntList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextInt());
return list;
}
private static List<Long> readLongList(int n) {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(fs.nextLong());
return list;
}
private static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws IOException {
if (checkOnlineJudge)
this.br = new BufferedReader(new FileReader("src/input.txt"));
else
this.br = new BufferedReader(new InputStreamReader(System.in));
this.st = new StringTokenizer("");
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException err) {
err.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st.hasMoreTokens()) {
return st.nextToken("").trim();
}
try {
return br.readLine().trim();
} catch (IOException err) {
err.printStackTrace();
}
return "";
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
private static class FastWriter {
PrintWriter out;
FastWriter() throws IOException {
if (checkOnlineJudge)
out = new PrintWriter(new FileWriter("src/output.txt"));
else
out = new PrintWriter(System.out);
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 6b2eefe13836143af67ec93afcc21533 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class geeks {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
while(x>0){
int n=sc.nextInt();
int k=sc.nextInt();
int u=1;
if(n%2==0||k==1){
System.out.println("YES");
for (int i=1;i<=n;i++){
u=i;
for (int j=0;j<k;j++){
System.out.print(u+" ");
u=u+n;
}
System.out.println();
}
}else{
System.out.println("NO");
}
x--;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 8ed1c576b2532d66063086505e31ad90 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
public class Main {
static long M = (long) (1e9 + 7);
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 {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void swap(int[] a, int i, int j) {
int temp = (int) a[i];
a[i] = a[j];
a[j] = temp;
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int lcm(int a, int b) {
return (a * b / gcd(a, b));
}
public static String sortString(String inputString) {
// Converting input string to character array
char[] tempArray = inputString.toCharArray();
// Sorting temp array using
Arrays.sort(tempArray);
// Returning new sorted string
return new String(tempArray);
}
static boolean isSquare(int n) {
int v = (int) Math.sqrt(n);
return v * v == n;
}
static boolean PowerOfTwo(int n) {
if (n == 0) return false;
return (int) (Math.ceil((Math.log(n) / Math.log(2)))) ==
(int) (Math.floor(((Math.log(n) / Math.log(2)))));
}
static int power(long a, long b) {
long res = 1;
while (b > 0) {
if (b % 2 == 1) {
res = (res * a) % M;
}
a = ((a * a) % M);
b = b / 2;
}
return (int) res;
}
public static boolean isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) {
return false;
}
return true;
}
static int pown(long n) {
if (n == 0)
return 1;
if (n == 1)
return 1;
if ((n & (~(n - 1))) == n)
return 0;
return 1;
}
static long computeXOR(long n) {
// If n is a multiple of 4
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
static long binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
long result = 0;
for (int i = numbers.length - 1; i >= 0; i--)
if (numbers[i] == '1')
result += Math.pow(2, (numbers.length - i - 1));
return result;
}
static String reverseString(String str) {
char ch[] = str.toCharArray();
String rev = "";
for (int i = ch.length - 1; i >= 0; i--) {
rev += ch[i];
}
return rev;
}
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- != 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int count = 1;
int[][] arr = new int[m][n];
if (m==1 || n%2==0)
System.out.println("YES");
else
System.out.println("NO");
if (m==1 || n%2==0) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = count;
count++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(arr[j][i] + " ");
}
System.out.println();
}
}}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 9abe6b1210f3f874691a050afac05c21 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n, k;
public static void main(String[] args) throws IOException {
t = in.iscan();
while (t-- > 0) {
n = in.iscan(); k = in.iscan();
if (n % 2 == 0){
out.println("YES");
int nxtOdd = 1, nxtEven = 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (i % 2 == 0) {
out.print(nxtOdd + " ");
nxtOdd += 2;
}
else {
out.print(nxtEven + " ");
nxtEven += 2;
}
}
out.println();
}
}
else {
if (k == 1) {
out.println("YES");
for (int i = 0; i < n; i++) {
out.println(i+1);
}
}
else {
out.println("NO");
}
}
}
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | bb85a87c7d73d3fb800e0823a00d2d7d | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.Scanner;
public class sol{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int m=sc.nextInt();
if(m==1){
System.out.println("YES");
for(int i=1;i<=n;i++){
System.out.println(i);
}
}
else if(n%2==1){
System.out.println("NO");
}
else{
System.out.println("YES");
for(int i=1;i<=n;i++){
for(int j=0;j<m;j+=1){
System.out.print(i+(j*n)+" ");
}
System.out.println();
}
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | f46611d080d09748fadcaba45193e467 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | public class OKEA {
public static void main(String[] args) {
ContestScanner scanner = new ContestScanner();
int tests = scanner.nextInt();
for (int i = 0; i < tests; i++) {
int numberOfShelves = scanner.nextInt();
int lengthOfShelve = scanner.nextInt();
solve(numberOfShelves, lengthOfShelve);
}
}
private static void solve(int numberOfShelves, int lengthOfShelve) {
if (lengthOfShelve != 1 && numberOfShelves % 2 != 0) {
System.out.println("NO");
return;
}
System.out.println("YES");
int evenIndex = 2;
int oddIndex = 1;
boolean odd = true;
StringBuilder builder = new StringBuilder();
while (numberOfShelves-- > 0) {
int temp = lengthOfShelve;
while (temp-- > 0) {
if (odd) {
builder.append(oddIndex);
oddIndex += 2;
} else {
builder.append(evenIndex);
evenIndex += 2;
}
if (temp == 0) {
builder.append(System.lineSeparator());
} else {
builder.append(" ");
}
}
odd = !odd;
}
System.out.print(builder.toString());
}
private static class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int bufferLength = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in) {
this.in = in;
}
public ContestScanner(java.io.File file) throws java.io.FileNotFoundException {
this(new java.io.BufferedInputStream(new java.io.FileInputStream(file)));
}
public ContestScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < bufferLength) {
return true;
} else {
ptr = 0;
try {
bufferLength = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (bufferLength <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new java.util.NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)
);
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = this.nextInt();
return array;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 9d84e9b8addedb9750cd22b4b39594f8 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
public class Round12 {
public static void main(String[] args) {
FastReader fastReader = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = fastReader.nextInt();
while (t-- > 0) {
int n = fastReader.nextInt();
int k = fastReader.nextInt();
int tot = n * k;
int oddreq = ((n + 1) / 2) * k;
int oddhave = (tot + 1) / 2;
if (oddhave != oddreq) {
out.println("No");
} else {
out.println("Yes");
int odd = 1;
int even = 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (i % 2 == 0) {
out.print(odd + " ");
odd += 2;
} else {
out.print(even + " ");
even += 2;
}
}
out.println();
}
}
}
out.close();
}
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final long LMAX = 9223372036854775807L;
static Random __r = new Random();
// math util
static int minof(int a, int b, int c) {
return min(a, min(b, c));
}
static int minof(int... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return min(x[0], x[1]);
if (x.length == 3) return min(x[0], min(x[1], x[2]));
int min = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i];
return min;
}
static long minof(long a, long b, long c) {
return min(a, min(b, c));
}
static long minof(long... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return min(x[0], x[1]);
if (x.length == 3) return min(x[0], min(x[1], x[2]));
long min = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i];
return min;
}
static int maxof(int a, int b, int c) {
return max(a, max(b, c));
}
static int maxof(int... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return max(x[0], x[1]);
if (x.length == 3) return max(x[0], max(x[1], x[2]));
int max = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i];
return max;
}
static long maxof(long a, long b, long c) {
return max(a, max(b, c));
}
static long maxof(long... x) {
if (x.length == 1) return x[0];
if (x.length == 2) return max(x[0], x[1]);
if (x.length == 3) return max(x[0], max(x[1], x[2]));
long max = x[0];
for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i];
return max;
}
static int powi(int a, int b) {
if (a == 0) return 0;
int ans = 1;
while (b > 0) {
if ((b & 1) > 0) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static long powl(long a, int b) {
if (a == 0) return 0;
long ans = 1;
while (b > 0) {
if ((b & 1) > 0) ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static int fli(double d) {
return (int) d;
}
static int cei(double d) {
return (int) ceil(d);
}
static long fll(double d) {
return (long) d;
}
static long cel(double d) {
return (long) ceil(d);
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static int[] exgcd(int a, int b) {
if (b == 0) return new int[]{1, 0};
int[] y = exgcd(b, a % b);
return new int[]{y[1], y[0] - y[1] * (a / b)};
}
static long[] exgcd(long a, long b) {
if (b == 0) return new long[]{1, 0};
long[] y = exgcd(b, a % b);
return new long[]{y[1], y[0] - y[1] * (a / b)};
}
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static long mix(long x) {
x += 0x9e3779b97f4a7c15L;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebL;
return x ^ (x >> 31);
}
public static boolean[] findPrimes(int limit) {
assert limit >= 2;
final boolean[] nonPrimes = new boolean[limit];
nonPrimes[0] = true;
nonPrimes[1] = true;
int sqrt = (int) Math.sqrt(limit);
for (int i = 2; i <= sqrt; i++) {
if (nonPrimes[i]) continue;
for (int j = i; j < limit; j += i) {
if (!nonPrimes[j] && i != j) nonPrimes[j] = true;
}
}
return nonPrimes;
}
// array util
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
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());
}
int[] ria(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next());
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] rla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = Long.parseLong(next());
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 3618a920960999d5368d311af572a0ec | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.List;
import java.util.Scanner;
public class Okea {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int t = Integer.valueOf(scanner.nextLine());
String[] lines = new String[t];
for (int i = 0; i < t; i++) {
lines[i] = scanner.nextLine();
}
for (int i = 0; i < t; i++) {
String[] p = lines[i].split(" ");
int n = Integer.valueOf(p[0]);
int k = Integer.valueOf(p[1]);
check(n, k);
}
}
}
private static void check(int n, int k) {
if (k > 1 && n % 2 > 0) {
System.out.println("NO");
return;
}
System.out.println("YES");
StringBuilder sb = new StringBuilder("");
for (int initial : List.of(1, 2)) {
int start = initial;
int count = 0;
while (start <= n*k) {
sb.append(start).append(" ");
count++;
start += 2;
if (count % k == 0) {
sb.append('\n');
}
}
}
System.out.println(sb.replace(sb.length() - 1, sb.length(), "").toString());
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 1d0d871e5cc820c865e109f234049f8a | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
StringTokenizer tokenizer = new StringTokenizer(br.readLine());
int n = Integer.parseInt(tokenizer.nextToken());
int k = Integer.parseInt(tokenizer.nextToken());
if (k == 1) {
pw.println("YES");
for (int i = 0; i < n; i++) {
pw.println(i + 1);
}
} else {
if (n % 2 == 0) {
pw.println("YES");
for (int i = 1; i <= n / 2; i++) {
int start = (2 * k) * (i - 1) + 1;
for (int j = 0; j < k; j++) {
if (j > 0)
pw.print(" ");
pw.print(start + (j * 2));
}
pw.println();
for (int j = 0; j < k; j++) {
if (j > 0)
pw.print(" ");
pw.print(start + 1 + (j * 2));
}
pw.println();
}
} else {
pw.println("NO");
}
}
}
pw.flush();
pw.close();
br.close();
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 29135c9795e56af4bfbd183dee2d2ad6 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Okea{
public static void main(String args[])throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder result = new StringBuilder();
int testCases = Integer.parseInt(br.readLine());
StringTokenizer str;
while(testCases-- > 0){
str = new StringTokenizer(br.readLine());
int n = Integer.parseInt(str.nextToken());
int k = Integer.parseInt(str.nextToken());
StringBuilder answer = new StringBuilder();
if(k == 1){
for(int i = 1; i <= n; i++){
answer.append(i + "\n");
}
result.append("YES\n" + answer);
}else if(n % 2 == 0){
for(int i = 1; i <= n; i++){
for(int j = i; j <= (k*n); j += n){
answer.append(j + " ");
}
answer.append("\n");
}
result.append("YES\n" + answer);
}else{
result.append("NO\n");
}
}
System.out.println(result);
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 311bbaa4775281f2599a3578eb951fcf | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.*;
//
public class Main{
public static void main(String[] args) {
TaskA solver = new TaskA();
// boolean[]prime=seive(3*100001);
int t = in.nextInt();
for (int i = 1; i <= t ; i++) {
solver.solve(1, in, out);
}
// solver.solve(1, in, out);
out.flush();
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n=in.nextInt();int k=in.nextInt();
if(k==1) {
println("YES");
for(int i=0;i<n;i++) {
println(i+1);
}
}
else if(n%2==0) {
println("YES");
int odd=1;int even=2;
for(int i=1;i<=n;i++) {
String s="";
for(int j=1;j<=k;j++) {
if(i%2==0) {
s+=even;even+=2;s+=" ";
}
else {
s+=odd;odd+=2;s+=" ";
}
}
println(s);
}
}
else {
println("NO");
}
}
}
private static String rev(String s) {
String st="";
for(int i=s.length()-1;i>=0;i--) {
st+=s.charAt(i);
}
return st;
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
System .out.print(arr[i] + " ");
System.out.println(" ");
}
static int dis(int a,int b,int c,int d) {
return Math.abs(c-a)+Math.abs(d-b);
}
static long ceil(long a,long b) {
return (a/b + Math.min(a%b, 1));
}
static char[] rev(char[]ans,int n) {
for(int i=ans.length-1;i>=n;i--) {
ans[i]=ans[ans.length-i-1];
}
return ans;
}
static int countStep(int[]arr,long sum,int k,int count1) {
int count=count1;
int index=arr.length-1;
while(sum>k&&index>0) {
sum-=(arr[index]-arr[0]);
count++;
if(sum<=k) {
break;
}
index--;
// println("c");
}
if(sum<=k) {
return count;
}
else {
return Integer.MAX_VALUE;
}
}
static long pow(long b, long e) {
long ans = 1;
while (e > 0) {
if (e % 2 == 1)
ans = ans * b % mod;
e >>= 1;
b = b * b % mod;
}
return ans;
}
static void sortDiff(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{ if(p1.first==p2.first) {
return (p1.second-p1.first)-(p2.second-p1.first);
}
return (p1.second-p1.first)-(p2.second-p2.first);
}
});
}
static void sortF(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{ if(p1.first==p2.first) {
return p1.second-p2.second;
}
return p1.first - p2.first;
}
});
}
static int[] shift (int[]inp,int i,int j){
int[]arr=new int[j-i+1];
int n=j-i+1;
for(int k=i;k<=j;k++) {
arr[(k-i+1)%n]=inp[k];
}
for(int k=0;k<n;k++ ) {
inp[k+i]=arr[k];
}
return inp;
}
static long[] fac;
static long mod = (long) 1000000007;
static void initFac(long n) {
fac = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = (fac[i - 1] * i) % mod;
}
}
static long nck(int n, int k) {
if (n < k)
return 0;
long den = inv((int) (fac[k] * fac[n - k] % mod));
return fac[n] * den % mod;
}
static long inv(long x) {
return pow(x, mod - 2);
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) {
Queue<Integer>q=new LinkedList<>();
q.add(source);
visited[source]=true;
int distance=0;
while(!q.isEmpty()) {
int curr=q.poll();
distance++;
for(int neighbour:a[curr]) {
if(!visited[neighbour]) {
visited[neighbour]=true;
q.add(neighbour);
}
}
}
return distance;
}
public static Set<Integer>factors(int n){
Set<Integer>ans=new HashSet<>();
ans.add(1);
for(int i=2;i*i<=n;i++) {
if(n%i==0) {
ans.add(i);
ans.add(n/i);
}
}
return ans;
}
public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) {
boolean[]visited=new boolean[a.length];
int[]parent=new int[a.length];
Queue<Integer>q=new LinkedList<>();
int distance=0;
q.add(source);
visited[source]=true;
parent[source]=-1;
while(!q.isEmpty()) {
int curr=q.poll();
if(curr==destination) {
break;
}
for(int neighbour:a[curr]) {
if(neighbour!=-1&&!visited[neighbour]) {
visited[neighbour]=true;
q.add(neighbour);
parent[neighbour]=curr;
}
}
}
int cur=destination;
while(parent[cur]!=-1) {
distance++;
cur=parent[cur];
}
return distance;
}
static int bs(int size,int[]arr) {
int x = -1;
for (int b = size/2; b >= 1; b /= 2) {
while (!ok(arr));
}
int k = x+1;
return k;
}
static boolean ok(int[]x) {
return false;
}
public static int solve1(ArrayList<Integer> A) {
long[]arr =new long[A.size()+1];
int n=A.size();
for(int i=1;i<=A.size();i++) {
arr[i]=((i%2)*((n-i+1)%2))%2;
arr[i]%=2;
}
int ans=0;
for(int i=0;i<A.size();i++) {
if(arr[i+1]==1) {
ans^=A.get(i);
}
}
return ans;
}
public static String printBinary(long a) {
String str="";
for(int i=31;i>=0;i--) {
if((a&(1<<i))!=0) {
str+=1;
}
if((a&(1<<i))==0 && !str.isEmpty()) {
str+=0;
}
}
return str;
}
public static String reverse(long a) {
long rev=0;
String str="";
int x=(int)(Math.log(a)/Math.log(2))+1;
for(int i=0;i<32;i++) {
rev<<=1;
if((a&(1<<i))!=0) {
rev|=1;
str+=1;
}
else {
str+=0;
}
}
return str;
}
////////////////////////////////////////////////////////
static void sortS(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p1.second - p2.second;
}
});
}
static class Pair implements Comparable<Pair>
{
int first ;int second ;
public Pair(int x, int y)
{
this.first = x ;this.second = y ;
}
@Override
public boolean equals(Object obj)
{
if(obj == this)return true ;
if(obj == null)return false ;
if(this.getClass() != obj.getClass()) return false ;
Pair other = (Pair)(obj) ;
if(this.first != other.first)return false ;
if(this.second != other.second)return false ;
return true ;
}
@Override
public int hashCode()
{
return this.first^this.second ;
}
@Override
public String toString() {
String ans = "" ;ans += this.first ; ans += " "; ans += this.second ;
return ans ;
}
@Override
public int compareTo(Main.Pair o) {
{ if(this.first==o.first) {
return this.second-o.second;
}
return this.first - o.first;
}
}
}
//////////////////////////////////////////////////////////////////////////
static int nD(long num) {
String s=String.valueOf(num);
return s.length();
}
static int CommonDigits(int x,int y) {
String s=String.valueOf(x);
String s2=String.valueOf(y);
return 0;
}
static int lcs(String str1, String str2, int m, int n)
{
int L[][] = new int[m + 1][n + 1];
int i, j;
// Following steps build L[m+1][n+1] in
// bottom up fashion. Note that L[i][j]
// contains length of LCS of str1[0..i-1]
// and str2[0..j-1]
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (str1.charAt(i - 1)
== str2.charAt(j - 1))
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = Math.max(L[i - 1][j],
L[i][j - 1]);
}
}
// L[m][n] contains length of LCS
// for X[0..n-1] and Y[0..m-1]
return L[m][n];
}
/////////////////////////////////
boolean IsPowerOfTwo(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
////////////////////////////////
static long power(long a,long b,long m ) {
long ans=1;
while(b>0) {
if(b%2==1) {
ans=((ans%m)*(a%m))%m; b--;
}
else {
a=(a*a)%m;b/=2;
}
}
return ans%m;
}
///////////////////////////////
public static boolean repeatedSubString(String string) {
return ((string + string).indexOf(string, 1) != string.length());
}
static int search(char[]c,int start,int end,char x) {
for(int i=start;i<end;i++) {
if(c[i]==x) {return i;}
}
return -2;
}
////////////////////////////////
static long gcd(long a, long b) {
while (b != 0) {
long t = b;
b = a % b;
a = t;
}
return a;
}
static long fac(long a)
{
if(a== 0L || a==1L)return 1L ;
return a*fac(a-1L) ;
}
static ArrayList al() {
ArrayList<Integer>a =new ArrayList<>();
return a;
}
static HashSet h() {
return new HashSet<Integer>();
}
static void debug(long[][]a) {
int n= a.length;
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[0].length;j++) {
out.print(a[i][j]+" ");
}
out.print("\n");
}
}
static void debug(int[]a) {
out.println(Arrays.toString(a));
}
static void debug(ArrayList<Integer>a) {
out.println(a.toString());
}
static boolean[]seive(int n){
boolean[]b=new boolean[n+1];
for (int i = 2; i <= n; i++)
b[i] = true;
for(int i=2;i*i<=n;i++) {
if(b[i]) {
for(int j=i*i;j<=n;j+=i) {
b[j]=false;
}
}
}
return b;
}
static int longestIncreasingSubseq(int[]arr) {
int[]sizes=new int[arr.length];
Arrays.fill(sizes, 1);
int max=1;
for(int i=1;i<arr.length;i++) {
for(int j=0;j<i;j++) {
if(arr[j]<arr[i]) {
sizes[i]=Math.max(sizes[i],sizes[j]+1);
max=Math.max(max, sizes[i]);
}
}
}
return max;
}
public static ArrayList primeFactors(long n)
{
ArrayList<Long>h= new ArrayList<>();
// Print the number of 2s that divide n
if(n%2 ==0) {h.add(2L);}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (long i = 3; i <= Math.sqrt(n); i+= 2)
{
if(n%i==0) {h.add(i);}
}
if (n > 2)
h.add(n);
return h;
}
static boolean Divisors(long n){
if(n%2==1) {
return true;
}
for (long i=2; i<=Math.sqrt(n); i++){
if (n%i==0 && i%2==1){
return true;
}
}
return false;
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
public static void superSet(int[]a,ArrayList<String>al,int i,String s) {
if(i==a.length) {
al.add(s);
return;
}
superSet(a,al,i+1,s);
superSet(a,al,i+1,s+a[i]+" ");
}
public static long[] makeArr() {
long size=in.nextInt();
long []arr=new long[(int)size];
for(int i=0;i<size;i++) {
arr[i]=in.nextInt();
}
return arr;
}
public static long[] arr(int n) {
long []arr=new long[n+1];
for(int i=1;i<n+1;i++) {
arr[i]=in.nextLong();
}
return arr;
}
public static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void println(long x) {
out.println(x);
}
static void print(long c) {
out.print(c);
}
static void print(int c) {
out.print(c);
}
static void println(int x) {
out.println(x);
}
static void print(String s) {
out.print(s);
}
static void println(String s) {
out.println(s);
}
public static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
//Copy data to temp arrays
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
public static void reverse(int[] array)
{
int n = array.length;
for (int i = 0; i < n / 2; i++) {
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
public static long nCr(int n,int k)
{
long ans=1L;
k=k>n-k?n-k:k;
for( int j=1;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else
if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
static int searchMax(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]>inp[index]) {
index+=1;
}
return index;
}
static int searchMin(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]<inp[index]) {
index+=1;
}
return index;
}
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
static class Pairr implements Comparable<Pairr>{
private int index;
private int cumsum;
private ArrayList<Integer>indices;
public Pairr(int index,int cumsum) {
this.index=index;
this.cumsum=cumsum;
indices=new ArrayList<Integer>();
}
public int compareTo(Pairr other) {
return Integer.compare(cumsum, other.cumsum);
}
}
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\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | c2ee0d4b7919e0bf3156e32c402c4604 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | // In The Name Of Allah
// Mohammad Hosseini
// Hello MF_AS AP
import java.util.*;
import java.io.*;
public class hello {
public static Scanner sc = new Scanner(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
static void solve(){
int n = sc.nextInt(), k = sc.nextInt();
if(k == 1 || n % 2 == 0) {
pw.println("YES");
if(k == 1) {
for(int i = 1; i <= n; i++)
pw.println(i);
}
else {
for(int i = 1; i <= n; i++) {
int h = 2 * k * ((i - 1) / 2) + (i % 2 == 1 ? 1 : 2);
for(int j = 0; j < k; j++, h += 2)
pw.printf("%d ", h);
pw.println();
}
}
}
else {
pw.println("NO");
}
pw.flush();
}
public static void main(String[] args) {
int t = sc.nextInt();
while( t > 0 ) {
solve();
t--;
}
sc.close();
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 353e1ba0fd61ac43d715d5a05ffcea6c | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
public static FastScanner s = new FastScanner();
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int lines = s.nextInt();
for (int i = 0; i < lines; i += 1) {
solve(s.nextInt(), s.nextInt());
}
out.close();
}
public static void solve(int s, int l) {
if (s % 2 == 0 || l == 1) out.println("YES"); else {
out.println("NO");
return;
}
if (l == 1) {
for (int i = 0; i < s; i++) out.println(i + 1);
} else {
for (int i = 0; i < s; i++) {
for (int j = 0; j < l; j++) {
out.print(1 + j * 2 + i % 2 + i / 2 * 2 * l + " ");
}
out.println();
}
}
}
static class FastScanner {//copied from secondthread
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 7fe8af1ae332b201513b305264aaf424 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
private static final FastScanner fs = new FastScanner();
public static void main(String[] args) {
int testCase = fs.nextInt();
for (int i = 1; i <= testCase; i++) {
solve();
}
}
public static void solve() {
int n = fs.nextInt(); //number of shelves
int k = fs.nextInt(); //length of each shelves
int total = n * k;
int totalEven = total / 2;
int totalOdd = total / 2;
if (total % 2 != 0) totalOdd++;
int currentHeight = 0;
int currentOddPlaced = 0;
int currentOdd = 1;
StringBuilder ans = new StringBuilder();
while (currentOddPlaced < totalOdd) {
ans.append(currentOdd).append(" ");
currentOddPlaced++;
currentHeight++;
if (currentHeight == k) {
currentHeight = 0;
ans.append("\n");
}
currentOdd += 2;
}
if (currentHeight != 0) {
System.out.println("NO");
return;
}
int currentEven = 2;
int currentEvenPlaced = 0;
while (currentEvenPlaced < totalEven) {
ans.append(currentEven).append(" ");
currentEven += 2;
currentEvenPlaced++;
currentHeight++;
if (currentHeight == k) {
currentHeight = 0;
ans.append("\n");
}
}
System.out.println("YES");
ans.deleteCharAt(ans.length() - 1);
System.out.println(ans);
}
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 8e1cb3f3317dba18e3ef594c738b0edf | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main {
static FastReader sc=new FastReader();
public static void main(String args[]) {
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt(),k=sc.nextInt();
if(k==1){
System.out.println("YES");
for(int i=0;i<n;i++){
System.out.println(i+1);
}
continue;
}
if(n%2!=0){
System.out.println("NO");
continue;
}
System.out.println("YES");
int[][]arr=new int[n][k];
int even=2;
int odd=1;
for(int i=0;i<n;i+=2){
for(int j=0;j<k;j++){
arr[i][j]=even;
even+=2;
}
}
for(int i=1;i<n;i+=2){
for(int j=0;j<k;j++){
arr[i][j]=odd;
odd+=2;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
static int[] readArray(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) sc.nextInt();
}
return res;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 49f0e3bea3d9b2c1e53aaa8d55cf3a32 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
public class Main {
// static final File ip = new File("input.txt");
// static final File op = new File("output.txt");
// static {
// try {
// System.setOut(new PrintStream(op));
// System.setIn(new FileInputStream(ip));
// } catch (Exception e) {
// }
// }
private static final PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
FastReader sc = new FastReader();
int test = 1;
test = sc.nextInt();
while (test-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
if (n % 2 == 1) {
if (k > 1)
out.println("NO");
else {
out.println("YES");
for (int i = 1; i <= n; i++)
out.println(i);
}
} else {
out.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++)
out.print(j * n - i + " ");
out.println();
}
}
}
out.close();
}
static long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static double log2(int N) {
double result = (double) (Math.log(N) / (double) Math.log(2));
return result;
}
public static int countSetBits(long number) {
int count = 0;
while (number > 0) {
++count;
number &= number - 1;
}
return count;
}
static int lower_bound(long target, long[] a, int pos) {
if (pos >= a.length)
return -1;
int low = pos, high = a.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (a[mid] < target)
low = mid + 1;
else
high = mid;
}
return a[low] >= target ? low : -1;
}
private static <T> void swap(long[] a, int i, int j) {
long tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
static class pair {
long a;
char b;
pair(long x, char y) {
this.a = x;
this.b = y;
}
}
static class first implements Comparator<pair> {
public int compare(pair p1, pair p2) {
if (p1.a > p2.a)
return 1;
else if (p1.a < p2.a)
return -1;
return 0;
}
}
static class second implements Comparator<pair> {
public int compare(pair p1, pair p2) {
if (p1.b > p2.b)
return 1;
else if (p1.b < p2.b)
return -1;
return 0;
}
}
private static long getSum(int[] array) {
long sum = 0;
for (int value : array) {
sum += value;
}
return sum;
}
private static boolean isPrime(Long x) {
if (x < 2)
return false;
for (long d = 2; d * d <= x; ++d) {
if (x % d == 0)
return false;
}
return true;
}
static long[] reverse(long a[]) {
int n = a.length;
int i;
long t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
return a;
}
private static boolean isPrimeInt(int x) {
if (x < 2)
return false;
for (int d = 2; d * d <= x; ++d) {
if (x % d == 0)
return false;
}
return true;
}
public static String reverse(String input) {
StringBuilder str = new StringBuilder("");
for (int i = input.length() - 1; i >= 0; i--) {
str.append(input.charAt(i));
}
return str.toString();
}
private static int[] getPrimes(int n) {
boolean[] used = new boolean[n + 1];
used[0] = used[1] = true;
// int size = 0;
for (int i = 2; i <= n; ++i) {
if (!used[i]) {
// ++size;
for (int j = 2 * i; j <= n; j += i) {
used[j] = true;
}
}
}
int[] primes = new int[n + 1];
for (int i = 0; i <= n; ++i) {
if (!used[i]) {
primes[i] = 1;
}
}
return primes;
}
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
private static long gcd(long a, long b) {
return (a == 0 ? b : gcd(b % a, a));
}
static void sortI(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static void shuffleList(ArrayList<Long> arr) {
int n = arr.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr.get(i);
int randomPos = i + rnd.nextInt(n - i);
arr.set(i, arr.get(randomPos));
arr.set(randomPos, tmp);
}
}
static void factorize(long n) {
int count = 0;
while (!(n % 2 > 0)) {
n >>= 1;
count++;
}
if (count > 0) {
// System.out.println("2" + " " + count);
}
long i = 0;
for (i = 3; i <= (long) Math.sqrt(n); i += 2) {
count = 0;
while (n % i == 0) {
count++;
n = n / i;
}
if (count > 0) {
// System.out.println(i + " " + count);
}
}
if (n > 2) {
// System.out.println(i + " " + count);
}
}
static void sortL(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;
}
Arrays.sort(arr);
}
////////////////////////////////// DSU START ///////////////////////////
static class DSU {
int[] parent, rank, total_Elements;
DSU(int n) {
parent = new int[n + 1];
rank = new int[n + 1];
total_Elements = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
rank[i] = 1;
total_Elements[i] = 1;
}
}
int find(int u) {
if (parent[u] == u)
return u;
return parent[u] = find(parent[u]);
}
void unionByRank(int u, int v) {
int pu = find(u);
int pv = find(v);
if (pu != pv) {
if (rank[pu] > rank[pv]) {
parent[pv] = pu;
total_Elements[pu] += total_Elements[pv];
} else if (rank[pu] < rank[pv]) {
parent[pu] = pv;
total_Elements[pv] += total_Elements[pu];
} else {
parent[pu] = pv;
total_Elements[pv] += total_Elements[pu];
rank[pv]++;
}
}
}
boolean unionBySize(int u, int v) {
u = find(u);
v = find(v);
if (u != v) {
parent[u] = v;
total_Elements[v] += total_Elements[u];
total_Elements[u] = 0;
return true;
}
return false;
}
}
////////////////////////////////// DSU END /////////////////////////////
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public boolean hasNext() {
return false;
}
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\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 2098a30cff28f45ffdc0aec43632c461 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | /* package codechef; // don't place package name! */
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 Main
{
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
if(k==1 || n%2==0){
System.out.println("YES");
for(int i=0;i<n;i++){
for(int j=0;j<k;j++)
System.out.print(j*n+i+1+" ");
System.out.println();}
}else System.out.println("NO");
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 5c2f3a1c2cec3c633bd53e1f3e88e4cf | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.Scanner;
public class C
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0)
{
int n = in.nextInt(), m = in.nextInt();
if (m == 1)
{
System.out.println("YES");
for (int i = 1; i <= n; i++)
System.out.println(i);
} else
{
if ((n * m + 1) / 2 % m == 0)
{
System.out.println("YES");
for (int i = 1; i <= n * m; i += 2)
{
System.out.print(i + " ");
if ((i + 1) % (m * 2) == 0)
System.out.println();
}
for (int i = 2; i <= n * m; i += 2)
{
System.out.print(i + " ");
if (i % (m * 2) == 0)
System.out.println();
}
} else
System.out.println("No");
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | aaa16db69824d4a7abc1d6ab59f4d01d | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class NK01 {
public static int [][] dp = new int [510][510];
public static int [][] sum = new int [510][510];
public static boolean [][] op = new boolean[510][510];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String s1 = br.readLine();
int n = Integer.parseInt(s1);
while(n-- > 0) {
String[] s = br.readLine().split(" ");
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
if(b == 1){
out.println("YES");
for (int i = 1; i <= a ; i++) {
out.println(i);
}
continue;
}
if(a % 2 == 0){
out.println("YES");
for (int i = 1; i <= a; i++) {
for (int j = 0; j < b ; j++) {
out.print((i + a * j) + " ");
}
out.println();
}
continue;
}
out.println("NO");
}
out.flush();
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | f9b2449d63ffbfaaa06a079c788d5c66 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.lang.Math.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int k = sc.nextInt();
if(k == 1){
System.out.println("YES");
for(int i=1;i<=n;i++){
System.out.println(i);
}
}else if(n%2 == 0){
System.out.println("YES");
for(int i=1;i<=n;i++){
for(int j=0;j<k;j++){
System.out.print((i + (j*n)) + " ");
}
System.out.println( );
}
}else{
System.out.println("NO");
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 0fd11d304999d163c9142872b774fb90 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
/**
__ __
( _) ( _)
/ / \\ / /\_\_
/ / \\ / / | \ \
/ / \\ / / |\ \ \
/ / , \ , / / /| \ \
/ / |\_ /| / / / \ \_\
/ / |\/ _ '_| \ / / / \ \\
| / |/ 0 \0\ / | | \ \\
| |\| \_\_ / / | \ \\
| | |/ \.\ o\o) / \ | \\
\ | /\\`v-v / | | \\
| \/ /_| \\_| / | | \ \\
| | /__/_ `-` / _____ | | \ \\
\| [__] \_/ |_________ \ | \ ()
/ [___] ( \ \ |\ | | //
| [___] |\| \| / |/
/| [____] \ |/\ / / ||
( \ [____ / ) _\ \ \ \| | ||
\ \ [_____| / / __/ \ / / //
| \ [_____/ / / \ | \/ //
| / '----| /=\____ _/ | / //
__ / / | / ___/ _/\ \ | ||
(/-(/-\) / \ (/\/\)/ | / | /
(/\/\) / / //
_________/ / /
\____________/ (
*/
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- >0) {
int n=sc.nextInt();
int k=sc.nextInt();
if(n%2==1 && k!=1) {
System.out.println("No");
continue;
}
int p1=1;
int p2=2;
System.out.println("YES");
for(int i=1;i<=n;i++) {
for(int j=0;j<k;j++) {
if(i%2==1) {
System.out.print(p1+" ");
p1+=2;
}
else {
System.out.print(p2+" ");
p2+=2;
}
}
System.out.println();
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | cba2b9f4035b6aa8797770185ed58486 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Solution {
public static void main (String[] args) throws java.lang.Exception {
int t=1;
t = sc.nextInt();
while(t-->0){
int n = sc.nextInt(), k = sc.nextInt();
int[][] arr = new int[n][k];
int a=0;
for(int i=0; i<k; i++){
for(int j=0; j<n; j++){
arr[j][i] = ++a;
}
}
if(n%2==0 || (k==1)){
System.out.println("YES");
for(int i=0; i<n; i++){
for(int j=0; j<k; j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}else{
System.out.println("NO");
}
}
}
// Fast Reader Class
public static FastReader sc = new FastReader();
public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in));}
String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } }return st.nextToken(); }
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }return str; }
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | d3d4e9a972f1966aad90268e01dfb0b0 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n){
int[] res=new int[n];
for(int i=0;i<n;i++)res[i]=nextInt();
return res;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testCases=in.nextInt();
//int testCases=1;
while(testCases-- > 0){
solve(in);
}
out.close();
} catch (Exception e) {
return;
}
}
public static void solve( FastReader in){
int n=in.nextInt();
//String s=in.next();
int k=in.nextInt();
//int h=in.nextInt();
//long n=in.nextLong();
//int k=in.nextInt();
//long k=in.nextLong();
StringBuilder res=new StringBuilder();
if((n&1)==1 && k!=1){
res.append(""+"NO");
System.out.println(res.toString());
return;
}
res.append(""+"YES\n");
if(k==1){
for(int i=1;i<n;i++){
res.append(""+i+"\n");
}
res.append(""+n+"");
System.out.println(res.toString());
return;
}
int s=1;
for(int i=0;i<n/2;i++){
for(int j=0;j<k;j++){
res.append(""+s+" ");
s+=2;
}
res.append("\n");
}
s=2;
for(int i=0;i<n/2;i++){
for(int j=0;j<k;j++){
res.append(""+s+" ");
s+=2;
}
if(i!=(n/2-1)){
res.append("\n");
}
}
// res.append(""+"NO");
//res.append(""+b+" "+n+" "+0);
System.out.println(res.toString());
}
static int gcd(int a,int b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
static void sort(int[] arr)
{
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void reversesort(int[] arr)
{
//because Arrays.sort() uses quicksort which is dumb
//Collections.sort() uses merge sort
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
Collections.reverse(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static void debug(String x){
System.out.println(x);
}
static < E > void print(E res)
{
System.out.println(res);
}
static String rString(String s){
StringBuilder sb=new StringBuilder();
sb.append(s);
return sb.reverse().toString();
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 68804377b27ea6119c891d2331bd6eb4 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | //package okea;
import java.util.*;
import java.io.*;
public class okea {
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(fin.readLine());
StringBuilder fout = new StringBuilder();
while(t-- > 0) {
StringTokenizer st = new StringTokenizer(fin.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
if(k == 1) {
fout.append("YES\n");
for(int i = 0; i < n; i++) {
fout.append(i + 1).append("\n");
}
continue;
}
else if(n % 2 == 1 || (n * k) % 2 == 1) {
fout.append("NO\n");
continue;
}
fout.append("YES\n");
int e = 2;
int o = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < k; j++) {
if(i % 2 == 0) {
fout.append(e).append(" ");
e += 2;
}
else {
fout.append(o).append(" ");
o += 2;
}
}
fout.append("\n");
}
}
System.out.print(fout);
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 2670dac91d81d4c70576790a2b1db3c8 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
public class C_OKEA {
static final int INT_MOD = (int) 1e9 + 7;
static final long LONG_MOD = (long) 1e9 + 7;
static final int INT_POSITIVE_INFINITY = Integer.MAX_VALUE;
static final long LONG_POSITIVE_INFINITY = Long.MAX_VALUE;
static final int INT_NEGATIVE_INFINITY = Integer.MIN_VALUE;
static final long LONG_NEGATIVE_INFINITY = Long.MIN_VALUE;
static StringBuilder result = new StringBuilder();
public static void main(String args[]) throws IOException {
FastReader fr = new FastReader();
FastWriter fw = new FastWriter();
// FastFileReader ffr = new FastFileReader("input.txt");
// FastFileWriter ffw = new FastFileWriter("output.txt");
int tc;
tc = fr.nextInt();
// tc = ffr.nextInt();
while (tc-- > 0) {
int n = fr.nextInt();
int k = fr.nextInt();
if (k > 1 && n % 2 == 1) {
result.append("NO\n");
} else {
result.append("YES\n");
int x = 1;
int y = 1;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
for (int j = 2 * x - 1; j < 2 * (k + x) - 1; j += 2) {
result.append(j + " ");
}
x += k;
} else {
for (int j = 2 * y; j < 2 * (k + y); j += 2) {
result.append(j + " ");
}
y += k;
}
result.append("\n");
}
}
}
fw.write(result.toString());
// ffw.write(result.toString());
}
static void helper() {
}
static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i++, j--);
}
}
static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
static boolean isPrime(long x) {
if (x <= 1)
return false;
for (long i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
static boolean[] sieve(int n) {
boolean[] sieve = new boolean[n + 1];
Arrays.fill(sieve, true);
sieve[0] = sieve[1] = false;
for (int i = 2; i * i <= n; i++) {
if (sieve[i]) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
}
return sieve;
}
static boolean isFibonacci(long x) {
return isPerfectSquare(5 * x * x + 4);
}
static boolean isPerfectSquare(long x) {
if (x <= 1)
return true;
long low = 1;
long high = x;
long mid = 0;
while (low <= high) {
mid = low + (high - low) / 2l;
if (mid * mid == x)
return true;
else if (mid * mid < x)
low = mid + 1;
else
high = mid - 1;
}
return false;
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static long gcd(long a, long b) {
if (b > a)
return gcd(b, a);
if (b == 0)
return a;
return gcd(b, a % b);
}
static long pow(long b, long e) {
long curr = b;
long res = 1;
while (e != 0) {
if ((e & 1) != 0) {
res = (res * curr) % LONG_MOD;
}
curr = (curr * curr) % LONG_MOD;
e >>= 1;
}
return res;
}
static double log(double x, double base) {
return Math.log(x) / Math.log(base);
}
}
/* user-defined data structures */
class Pair {
long a;
long b;
public Pair(long a, long b) {
this.a = a;
this.b = b;
}
}
class Tair {
int a;
int b;
int c;
public Tair(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
class Fair {
int a;
int b;
int c;
int d;
public Fair(int a, int b, int c, int d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
class Point {
int x;
int y;
int z;
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
/* User defined data structures ends here */
/* IO class */
class FastReader {
InputStreamReader isr;
BufferedReader br;
StringTokenizer st;
public FastReader() {
isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
}
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;
}
}
class FastWriter {
OutputStreamWriter osw;
BufferedWriter bw;
public FastWriter() {
osw = new OutputStreamWriter(System.out);
bw = new BufferedWriter(osw);
}
void write(String text) {
try {
bw.write(text);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class FastFileReader {
FileInputStream fis;
InputStreamReader isr;
BufferedReader br;
StringTokenizer st;
public FastFileReader(String fileName) throws FileNotFoundException {
fis = new FileInputStream(fileName);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
}
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;
}
}
class FastFileWriter {
FileOutputStream fos;
OutputStreamWriter osw;
BufferedWriter bw;
public FastFileWriter(String fileName) throws FileNotFoundException {
fos = new FileOutputStream(fileName);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
}
void write(String text) {
try {
bw.write(text);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* IO class ends here */ | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 90f51932d2eee188a11050a7526323ae | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
static class scannerzzz {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
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());}
static long nextLong() throws IOException {return Long.parseLong(next());}
}
static long min(long a, long b) {return Math.min(a, b);}
static long min(long a, long b, long c) {return min(a, min(b, c));}
static int min(int a, int b) {return Math.min(a, b);}
static int min(int a, int b, int c) {return min(a, min(b, c));}
static long max(long a, long b) {return Math.max(a, b);}
static long max(long a, long b, long c) {return max(a, max(b, c));}
static int max(int a, int b) {return Math.max(a, b);}
static int max(int a, int b, int c) {return max(a, max(b, c));}
static int abs(int x) {return Math.abs(x);}
static long abs(long x) {return Math.abs(x);}
static long ceil(double x) {return (long) Math.ceil(x);}
static long floor(double x) {return (long) Math.floor(x);}
static int ceil(int x) {return (int) Math.ceil(x);}
static int floor(int x) {return (int) Math.floor(x);}
static double sqrt(double x) {return Math.sqrt(x);}
static double cbrt(double x) {return Math.cbrt(x);}
static long sqrt(long x) {return (long) Math.sqrt(x);}
static long cbrt(long x) {return (long) Math.cbrt(x);}
static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);}
static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);}
static double pow(double n, double power) {return Math.pow(n, power);}
static long pow(long n, double power) {return (long) Math.pow(n, power);}
static class Pair {int first, second;public Pair(int first, int second) {this.first = first;this.second = second;}}
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
//scanner.init(System.in);
int t = 1;
t = scanner.nextInt();
while (t-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = scanner.nextInt(), k = scanner.nextInt();
int[][] mat = new int[n][k];
int start = 1;
if(n % 2 != 0) {
if(k == 1) {
System.out.println("YES");
for (int i = 0; i < n; i++) {
System.out.println(i+1);
}
}
else {
System.out.println("NO");
}
return;
}
for (int i = 0; i < n; i+=2) {
for (int j = 0; j < k; j++) {
mat[i][j] = start++;
mat[i+1][j] = start++;
}
}
System.out.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | f80a7f0377841958c22fec0b3a9ea605 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
static class scannerz {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
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());}
static long nextLong() throws IOException {return Long.parseLong(next());}
}
static long min(long a, long b) {return Math.min(a, b);}
static long min(long a, long b, long c) {return min(a, min(b, c));}
static int min(int a, int b) {return Math.min(a, b);}
static int min(int a, int b, int c) {return min(a, min(b, c));}
static long max(long a, long b) {return Math.max(a, b);}
static long max(long a, long b, long c) {return max(a, max(b, c));}
static int max(int a, int b) {return Math.max(a, b);}
static int max(int a, int b, int c) {return max(a, max(b, c));}
static int abs(int x) {return Math.abs(x);}
static long abs(long x) {return Math.abs(x);}
static long ceil(double x) {return (long) Math.ceil(x);}
static long floor(double x) {return (long) Math.floor(x);}
static int ceil(int x) {return (int) Math.ceil(x);}
static int floor(int x) {return (int) Math.floor(x);}
static double sqrt(double x) {return Math.sqrt(x);}
static double cbrt(double x) {return Math.cbrt(x);}
static long sqrt(long x) {return (long) Math.sqrt(x);}
static long cbrt(long x) {return (long) Math.cbrt(x);}
static int gcd(int a, int b) {if(b == 0)return a;return gcd(b, a % b);}
static long gcd(long a, long b) {if(b == 0)return a;return gcd(b, a % b);}
static double pow(double n, double power) {return Math.pow(n, power);}
static long pow(long n, double power) {return (long) Math.pow(n, power);}
static class Pair {int first, second;public Pair(int first, int second) {this.first = first;this.second = second;}}
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
//scanner.init(System.in);
int t = 1;
t = scanner.nextInt();
while (t-- > 0) {
solve();
}
}
static void solve() throws IOException {
int n = scanner.nextInt(), k = scanner.nextInt();
int[][] mat = new int[n][k];
int start = 1;
if(n % 2 != 0) {
if(k == 1) {
System.out.println("YES");
for (int i = 0; i < n; i++) {
System.out.println(i+1);
}
}
else {
System.out.println("NO");
}
return;
}
for (int i = 0; i < n; i+=2) {
for (int j = 0; j < k; j++) {
mat[i][j] = start++;
mat[i+1][j] = start++;
}
}
System.out.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 599ffd40ca7d118024da3e3d82c95920 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codechef {
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 readLine2() throws IOException {
List<Byte> buf = new ArrayList<Byte>();
// byte[] buf = new byte[1000]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf.add((byte) c);
// buf[c] = (byte) c;
cnt++;
}
byte[] buf2 = new byte[buf.size()];
int i = 0;
for (Byte b : buf)
buf2[i++] = b;
return new String(buf2, 0, cnt);
}
public String readLine() throws IOException {
String inp = readLine2().trim();
while (inp.length() == 0)
inp = readLine2().trim();
return inp;
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static PrintWriter out;
static Utility util;
static HashMap<Integer, Long> factMap;
static boolean ONLINE_JUDGE = true;
static int itr;
public static void main(String[] args) {
// Comment this code while running in Online Judge
try {
// System.out.println(System.getProperty("ONLINE_JUDGE"));
if (System.getProperty("ONLINE_JUDGE") == null && !ONLINE_JUDGE) {
FileOutputStream output = new FileOutputStream("output.txt");
PrintStream out = new PrintStream(output);
System.setOut(out);
InputStream input = new FileInputStream("input.txt");
System.setIn(input);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
Reader s = new Reader();
util = new Utility();
out = new PrintWriter(System.out);
int t = s.nextInt();
// System.out.println(t);
while (t > 0) {
int n = s.nextInt();
int k = s.nextInt();
if(n%2 == 0 || k == 1){
out.println("YES");
int[][] inp = new int[n][k];
int p = 1;
for(int i=0;i<k;i++){
for(int j=0;j<n;j++){
inp[j][i] = p++;
}
}
for(int i=0;i<n;i++){
StringBuilder curr = new StringBuilder();
for(int j=0;j<k;j++){
curr.append(inp[i][j]).append(" ");
}
out.println(curr.toString().trim());
}
}else
out.println("NO");
t--;
}
out.flush();
}
catch (
Exception e) {
out.flush();
System.out.println(e);
return;
}
}
public static boolean isPalind(String s){
int i =0;
int n = s.length()-1;
while(i<=n){
if(s.charAt(i) != s.charAt(n))
return false;
i++;
n--;
}
return true;
}
public static int getFirstSetBitPos(int n)
{
int cpy = n;
int x = 0;
while(n>0){
n = n/2;
x++;
}
// out.println(" pow "+(1<<x)+" "+cpy);
return (1l<<x)-cpy > 0 ? x : ((x+1));
}
public static String bsearch(long charH, long charA, long monsH,long monsA, long incA, long incH, int maxCoins){
int l = 0;
int r = maxCoins;
while(l<=r){
if(solve(charH+(l*incH), charA+(maxCoins-l)*incA, monsH, monsA))
return "YES";
l++;
}
return "NO";
}
public static boolean solve(long a, long b, long c, long d){
long p = c<=b ? 0 : ((c)/b)+1;
long q = a<=d ? 0: ((a)/d)+1;
// out.println(a+" "+b+" "+c+" "+d+" mp "+q+" cp "+p+" c "+(a/d)+" m "+(c/b));
if(p<=q)
return true;
return false;
}
public static int getPow(int n){
int cpy = n;
int ans = 0;
while(cpy > 0){
cpy = cpy/2;
ans++;
}
return (1<<ans) <= n ? (1<<ans) : (1<<ans-1);
}
public static int binarySearch(List<Integer> sub, int num) {
int left = 0;
int right = sub.size() - 1;
int mid = (left + right) / 2;
int ans = -1;
while (left <= right) {
mid = (left + right) / 2;
if (sub.get(mid) <= num) {
left = mid + 1;
} else {
ans = mid;
right = mid-1;
}
}
return ans;
}
public static List<Integer> isSpecial(int treeNodes, List<Integer> treeFrom, List<Integer> treeTo) {
int[] res = new int[treeNodes + 1];
for (int i = 0; i < treeFrom.size(); i++) {
res[treeFrom.get(i)]++;
res[treeTo.get(i)]++;
}
List<Integer> ans = new ArrayList<>();
for (int i = 1; i <= treeNodes; i++)
ans.add(res[i] > 1 ? 0 : 1);
return ans;
}
public static int bsearch(int[] inp, int targ) {
int l = 0;
int r = inp.length - 1;
int ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (inp[mid] <= targ) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
// out.println(targ + " ind " + ans + " " + (inp.length - (ans + 1)));
return ans;
}
static class Utility {
// Complexity: O(Log min(a, b))
public long ecu_gcd(long a, long b) {
if (a == 0)
return b;
return ecu_gcd(b % a, a);
}
public int[] extended_ecu_gcd(int a, int b) {
if (a == 0) {
return new int[] { b, 0, 1 };
}
int[] temp = extended_ecu_gcd(b % a, a);
return new int[] { temp[0], (temp[2] - (b / a) * temp[1]), temp[1] };
}
static ArrayList<Integer> primesnums;
static boolean hasprimes = false;
// Complexity: O(NLogN)
public boolean[] sieveOfEr_primes(int n) {
hasprimes = true;
boolean[] primes = new boolean[n + 1];
primesnums = new ArrayList<Integer>();
Arrays.fill(primes, true);
int i = 2;
for (i = 2; i * i <= n; i++) {
if (primes[i]) {
for (int p = i * i; p <= n; p += i)
primes[p] = false;
}
}
for (i = 2; i <= n; i++)
if (primes[i])
primesnums.add(i);
return primes;
}
public ArrayList<Integer> prime_Factors(int n) {
ArrayList<Integer> res = new ArrayList<Integer>();
while (n % 2 == 0) {
res.add(2);
n = n / 2;
}
for (int i = 3; i * i <= n; i = i + 2) {
while (n % i == 0) {
res.add(i);
n = n / i;
}
}
if (n > 2)
res.add(n);
return res;
}
public int prime_Factors2(int n) {
HashMap<Integer, Integer> res = new HashMap<Integer, Integer>();
while (n % 2 == 0) {
res.put(2, res.getOrDefault(2, 0) + 1);
n = n / 2;
}
for (int i = 3; i * i <= n; i = i + 2) {
while (n % i == 0) {
res.put(i, res.getOrDefault(i, 0) + 1);
n = n / i;
}
}
if (n > 2)
res.put(n, res.getOrDefault(n, 0) + 1);
int ress = 1;
for (Map.Entry<Integer, Integer> en : res.entrySet()) {
ress *= (en.getValue() + 1);
}
return ress;
}
public long[] fibnocnc(long k) {
if (k == 0)
return new long[] { 0, 1 };
long[] t = fibnocnc(k >> 1);
long a = (t[0] * (2 * t[1] - t[0]));
long b = (t[0] * t[0] + t[1] * t[1]);
if ((k & 1) == 1)
return new long[] { b, (b + a) };
return new long[] { a, b };
}
public long sumofN(long n) {
return n * (n + 1) / 2;
}
public long pos_quadratic_root(long a, long b, long c) {
return (-b + (long) Math.sqrt(b * b - 4 * a * c)) / 2 * a;
}
public long modInverse(long a, long m) {
// out.println(a+" "+m);
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
// out.println(x);
return x;
}
public TreeSet<Integer> getallFactors(int n) {
TreeSet<Integer> hst = new TreeSet<Integer>();
hst.add(1);
for (int i = 2; i * 1l * i <= n; i++) {
if (n % i == 0) {
hst.add(i);
if (i != n / i)
hst.add(n / i);
}
}
return hst;
}
static int invertBits(int n) {
// Calculate number of bits of N-1;
int x = (int) (Math.log(n) / Math.log(2));
int m = 1 << x;
m = m | m - 1;
n = n ^ m;
// System.out.println(n);
return n;
}
public static long bnc(int n, int r) {
if (r > n)
return 0;
long m = 1000000007;
long inv[] = new long[r + 1];
inv[1] = 1;
for (int i = 2; i <= r; i++) {
inv[i] = m - (m / i) * inv[(int) (m % i)] % m;
}
int ans = 1;
// for 1/(r!) part
for (int i = 2; i <= r; i++) {
ans = (int) (((ans % m) * (inv[i] % m)) % m);
}
// for (n)*(n-1)*(n-2)*...*(n-r+1) part
for (int i = n; i >= (n - r + 1); i--) {
ans = (int) (((ans % m) * (i % m)) % m);
}
return ans;
}
public static long fact(int n) {
if (n <= 1)
return 1;
if (factMap.containsKey(n))
return factMap.get(n);
factMap.put(n, mod(n * 1l * fact(n - 1)));
return factMap.get(n);
}
public static long mod(long n) {
return n < 0 ? 1000000007 + n : n % 1000000007;
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | f5c55bba5d08429cd6b3c91d0b0db7cc | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.util.*;
import java.lang.*;
public class solution
{
public static void main (String[] args) throws java.lang.Exception {
int in = 0;
// your code goes here'
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();
while (y-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
List<List<Integer>> ll = new ArrayList<>();
List<Integer> list=new ArrayList<>();
long N = n*k;
long odd;
if(N%2==0){
odd=(N/2);
}
else{
odd=N/2+1;
}
long even = (N-odd);
long ro,re;
if((n&1)==1)
ro=n/2+1;
else
ro=n/2;
re=n-ro;
if(odd!=ro*k)
{
System.out.println("NO");
continue;
}
if(even!=re*k)
{
System.out.println("NO");
continue;
}
System.out.println("YES");
long o = 1;
long e = 2;
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
{
if((i&1)==1)
{
System.out.print(e+" ");
e+=2;
}
else
{
System.out.print(o+" ");
o+=2;
}
}
System.out.println();
}
}}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 76469079937914661b027a0384a534e9 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0){
int n = s.nextInt();
int k = s.nextInt();
int[][] ar = new int[n][k];
// TreeSet<Integer> set = new TreeSet();
boolean[] visited = new boolean[(n*k)+1];
// for (int i = 1; i <= n*k; i++) set.add(i);
boolean flag = true;
for (int i = 0; i < n; i++){
ar[i][0] = getIdx(visited);
if (ar[i][0] == 0){
flag = false;
break;
}
visited[ar[i][0]] = true;
for (int j = 1; j < k; j++){
int x = ar[i][j-1]+2;
if (x > (n*k) || visited[x]){
flag = false;
break;
}
ar[i][j] = x;
visited[x] = true;
}
if (!flag) break;
}
if (!flag){
System.out.println("NO");
} else {
System.out.println("YES");
for (int i = 0; i < n; i++){
for (int j = 0; j < k; j++){
System.out.print(ar[i][j] + " ");
}
System.out.println();
}
}
}
}
public static int getIdx(boolean[] vis){
int idx = 0;
for (int i = 1; i < vis.length; i++){
if (!vis[i]){
idx = i;
break;
}
}
return idx;
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | db38310a4d6fd2468a162ba078eaf515 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | // Working program using Reader Class
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
public class Main
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[1000000]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static final int MAXN = 100001;
static int spf[] = new int[MAXN];
public static void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
public static Vector<Integer> getFactorization(int x)
{
Vector<Integer> ret = new Vector<>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
public static void main(String[] args) throws IOException
{
Reader sc=new Reader();
int t=sc.nextInt();
HashMap<Integer,Integer> map=new HashMap<>();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
if (n % 2 != 0 && k != 1) {
System.out.println("NO");
continue;
}
System.out.println("YES");
int[][] ans = new int[n][k];
for(int i = 1; i <= n; i++) {
for(int j = 0; j < k; j++) {
ans[i-1][j] = j == 0 ? i : ans[i-1][j-1] + n;
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < k; j++) {
System.out.print(ans[i][j] + " ");
}
System.out.println();
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | a9588fc95297ee6fad303e7fd7fe532e | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | /**
* C_OKEA
*/
import java.util.*;
import java.io.*;
public class C_OKEA {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static int find(boolean[] v)
{
for(int i = 0;i<v.length;++i)
if(!v[i]) return i;
return -1;
}
public static void main(String[] args) {
FastReader fr = new FastReader();
int t = fr.nextInt();
while (t-->0) {
int n = fr.nextInt();
int k = fr.nextInt();
int[][] a = new int[n][k];
boolean[] v = new boolean[n*k+1];
Arrays.fill(v,false);
v[0]= true;
boolean possible = true;
for(int ind = 0;ind<n;++ind)
{
int num = find(v);
if(num==-1)
{
possible = false;
break;
}
for(int j = 0;j<k;++j)
{
if(num>n*k)
{
possible = false;
break;
}
a[ind][j] = num;
v[num] = true;
num+=2;
}
if(!possible) break;
}
if(!possible) System.out.println("NO");
else
{
System.out.println("YES");
for(int i = 0;i<n;++i)
{
for(int j = 0;j<k;++j)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 1a16ae8f70f4af7825cb00db55d90e66 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class c5{
static class Pair{
int first;
int last;
public Pair(int first , int last){
this.first = first;
this.last = last;
}
public int getFirst(){
return first;
}
public int getLast(){
return last;
}
}
static final int M = 1000000007;
// Fast input
static class Hrittik_FastReader{
StringTokenizer st;
BufferedReader br;
public int n;
public Hrittik_FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
double nextDouble(){
return Double.parseDouble(next());
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
}
// Fast output
static class Hrittik_FastWriter {
private final BufferedWriter bw;
public Hrittik_FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void close() throws IOException {
bw.close();
}
}
// GCD of two integers
private static int gcd(int a , int b){
if(b == 0) return a;
return gcd(b, a%b);
}
// GCD of two long integers
private static long gcd(long a , long b){
if(b == 0) return a;
return gcd(b, a%b);
}
// LCM of two integers
private static int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
// LCM of two long integers
private static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
// swap two elements of an array
private static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// calculating x ^ y
private static long power(long x, long y){
long res = 1;
x = x % M;
while(y > 0){
if ((y & 1) == 1){
res = (res * x) % M;
}
y = y >> 1;
x = (x * x) % M;
}
return res;
}
static void sortPairByFirst(Pair arr[]){
Arrays.sort(arr , new Comparator<Pair>(){
@Override public int compare(Pair p1 , Pair p2){
return p1.first - p2.first;
}
});
}
static void sortPairByLast(Pair arr[]){
Arrays.sort(arr , new Comparator<Pair>(){
@Override public int compare(Pair p1 , Pair p2){
return p1.last - p2.last;
}
});
}
public static void main(String[] args) {
try {
Hrittik_FastReader Sc =new Hrittik_FastReader();
Hrittik_FastWriter out = new Hrittik_FastWriter();
int t = Sc.nextInt();
outer : while(t-- > 0){
// write your code here
int n = Sc.nextInt();
int k = Sc.nextInt();
int p = n*k;
boolean res = true;
int a[][] = new int[n][k];
int o = 1 , e = 2;
int i = 0 , j = 0;
while(i < n){
j = 0;
while(j < k){
if(o > p && j != 0){
res = false;
System.out.println("NO");
continue outer;
}
if(o > p && j == 0){
o = 2;
}
a[i][j] = o;
o+=2;
j++;
}
i++;
}
// for(int i = 0; i < n; i++){
// for(int j = 0; j < k; j++){
// if(o < p){
// a[i][j] = o;
// o+=2;
// }
// else if(o > p && j != 0){
// res = false;
// System.out.println("NO");
// continue outer;
// }
// else{
// a[i][j] = e;
// e+=2;
// }
// }
// }
System.out.println("YES");
for(int ii = 0; ii < n; ii++){
for(int jj = 0; jj < k; jj++){
System.out.print(a[ii][jj] + " ");
}
System.out.println();
}
System.out.println();
}
out.close();
}
catch (Exception e) {
return;
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 37717a6c476570b2a4e399e2d4ce8a50 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
if(k==1)
{
System.out.println("YES");
for(int x=1;x<=n;x++)
{
System.out.println(x);
}
}
else if(n%2!=0)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
int c1=1,c2=2;
for(int x=1;x<=n;x++)
{
for(int y=1;y<=k;y++)
{
if(x%2!=0)
{
System.out.print(c1+" ");
c1+=2;
}
else
{
System.out.print(c2+" ");
c2+=2;
}
}
System.out.println();
}
}
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 63126ee351a1df106a2a0e92880e3903 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
import java.util.stream.Stream;
/* 3 1 yes
* 3 2 no
* 3 3 no
* 3 4
*
* 1 4 0*k+i 1*k+n
* 2 5 0*k+n
* 3 6
*
* */
public class C {
static FastScanner fs = new FastScanner();
static PrintWriter out=new PrintWriter(System.out);
public static void main(String args[]) throws IOException {
//PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int tt = 1; tt <= T ; tt++) {
int n = fs.nextInt(), k = fs.nextInt();
if(k==1) print(n, k);
else if(n%2 == 1) out.println("NO");
else print(n,k);
}
out.close();
}
public static void print(int n ,int k ) {
out.println("YES");
for(int i = 1 ; i <=n ;i++) {
int j =0 ;
while(j<k) {
//int s= n*j+i;
out.print(n*j+i+" ");
j++;
}
out.println();
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static int inputInt() throws IOException {
return fs.nextInt();
}
public static long inputLong() throws IOException {
return fs.nextLong();
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 145baa7c0706c3f9ac36497df08a96c5 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
public class C{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
PrintWriter pw = new PrintWriter(System.out);
int testcases = Integer.parseInt(st.nextToken());
for(int tc = 0; tc < testcases; tc++) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
if(k == 1) {
pw.println("YES");
for(int i = 1; i <= n; i++) {
pw.println(i);
}
}else if(n%2 == 1){
pw.println("NO");
}else {
pw.println("YES");
for(int i = 0; i < n/2; i++) {
int b = i*2*k;
pw.print(b + 1);
for(int j = b+3; j <= (i+1)*2*k; j+=2){
pw.print(" " + j);
}
pw.println();
pw.print(b+2);
for(int j = b+4; j <= (i+1)*2*k; j += 2) {
pw.print(" " + j);
}
pw.println();
}
}
}
pw.flush();
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | dded6d7d35d27d8a01517163d208e2e0 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
public class TestClass {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int k = in.nextInt();
int[][] a = new int[n][k];
if(n==1&&k==1){System.out.println("YES");System.out.println(1);continue;}
if(k==1){
System.out.println("YES");
for (int j = 0; j < n; j++) {
System.out.println(j+1);
}
continue;
}
if(n%2==1){System.out.println("NO");continue;}
int c = 1;
int d = 2;
for (int j = 0; j < n/2; j++) {
for (int l = 0; l < k; l++) {
a[j][l] = c;
c+=2;
}
}
for (int j = n/2; j < n; j++) {
for (int l = 0; l < k; l++) {
a[j][l] = d;
d+=2;
}
}
System.out.println("YES");
for (int j = 0; j < n; j++) {
for (int l = 0; l < k; l++) {
System.out.print(a[j][l]+" ");
}
System.out.println();
}
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | 0a21f9c27e9b8d58a849ae3a8dccbd06 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.util.*;
import java.math.*;
import java.io.*;
public class Solution {
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 void sort(int a[]){ // int -> long
ArrayList<Integer> arr=new ArrayList<>(); // Integer -> Long
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
static int log(long n){
int res = 0;
while(n>0){
res++;
n/=2;
}
return res;
}
static int mod = (int)1e9+7;
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ________________________________
// int test = sc.nextInt();
StringBuilder output = new StringBuilder();
int test =sc.nextInt();
while (test-- > 0) {
//System.out.println(mdh+" "+nhc);
int n = sc.nextInt();
int k = sc.nextInt();
solver(n,k);
}
// solver(s);
// int n = sc.nextInt();
// out.println(solver());
// ________________________________
out.flush();
}
static class Edge
{
int u, v;
Edge(int u, int v)
{
this.u = u;
this.v = v;
}
}
static class Pair implements Comparable <Pair>{
int l, r;
Pair(int l, int r)
{
this.l = l;
this.r = r;
}
public int compareTo(Pair o)
{
return this.l-o.l;
}
}
//static ArrayList<Long>al =new ArrayList<>();
public static void solver( int n, int k) {
long te = n*k;
boolean flag = false;
if(k==1)
flag = true;
else if(n%2==1)
{
flag = false;
}
else if(te%2==0)
flag = true;
if(flag)
{
System.out.println("YES");
for(int i =1;i<=n;i++)
{
for(int j =0;j<k;j++)
{
System.out.print(i+n*j+" ");
}
System.out.println();
}
}
else
System.out.println("NO");
}
public static long log2(long N)
{
// calculate log2 N indirectly
// using log() method
long result = (long)(Math.log(N) / Math.log(2));
return result;
}
static long highestPowerof2(long x)
{
// check for the set bits
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
// Then we remove all but the top bit by xor'ing the
// string of 1's with that string of 1's shifted one to
// the left, and we end up with just the one top bit
// followed by 0's.
return x ^ (x >> 1);
}
public static void DFS(int i,ArrayList<ArrayList<Integer>>adj, boolean [] visited)
{
if(visited[i]==true)
return ;
visited[i] = true;
for(int nbr : adj.get(i))
{
if(!visited[nbr])
{
DFS(nbr,adj,visited);
}
}
}
public
static void rotate(int [] arr, int s, int n)
{
int x = arr[n], i;
for (i = n; i > s; i--)
arr[i] = arr[i-1];
arr[s] = x;
// for(int j=s;j<=n;j++)
// System.out.print(arr[j]+" ");
// System.out.println();
}
static int lower_bound(long[] a , long x)
{
int i = 0;
int j = a.length-1;
//if(arr[i] > key)return -1;
if(a[j] < x)return a.length;
while(i<j)
{
int mid = (i+j)/2;
if(a[mid] == x)
{
j = mid;
}
else if(a[mid] < x)
{
i = mid+1;
}
else
j = mid-1;
}
return i;
}
int upper_bound(int [] arr , int key)
{
int i = 0;
int j = arr.length-1;
if(arr[j] <= key)return j+1;
while(i<j)
{
int mid = (i+j)/2;
if(arr[mid] <= key)
{
i = mid+1;
}
else
j = mid;
}
return i;
}
static void reverseArray(int arr[], int start,
int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 11 | standard input | [
"constructive algorithms"
] | 9fb84ddc2e04fd637812cd72110b7f36 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 500$$$) — the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. | 1,000 | Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n \cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | standard output | |
PASSED | b8e0ce7eda255add44c77e56d3178ba0 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
sc=new MScanner(System.in);
pw=new PrintWriter(System.out);
int t=sc.nextInt();
for (int i=0;i<t;i++){
solver();
}
}
public static void solver() throws IOException {
int n=sc.nextInt();
HashMap<Long,Integer> map=new HashMap<>();
int x1=1; int x2=2;
for (int i=3;i<=n;i+=2){
int[] ans=findZero(new int[]{x1,x2,i,i+1},n,map);
x1=ans[0];
x2=ans[1];
}
if(x1>x2){
int u=x1;
x1=x2;
x2=u;
}
pw.write("! "+x1+" "+x2+"\n");
pw.flush();
}
private static int[] findZero(int[] x,int n,HashMap<Long,Integer> map) throws IOException {
if(x[3]>n){
x[3]=1;
while (x[3]==x[0] || x[3]==x[1] || x[3]==x[2]) x[3]++;
}
Arrays.sort(x);
pw.write("? "+x[1]+" "+x[2]+" "+x[3]+"\n");
pw.flush();
int[] a=new int[4];
a[0]=sc.nextInt();
map.put((long) x[1]*501*501+x[2]*501+x[3],a[0]);
pw.write("? "+x[0]+" "+x[2]+" "+x[3]+"\n");
pw.flush();
a[1]=sc.nextInt();
map.put((long) x[0]*501*501+x[2]*501+x[3],a[1]);
if(!map.containsKey((long) x[0]*501*501+x[1]*501+x[3])){
pw.write("? "+x[0]+" "+x[1]+" "+x[3]+"\n");
pw.flush();
a[2]=sc.nextInt();
}else a[2]=map.get((long) x[0]*501*501+x[1]*501+x[3]);
map.put((long) x[0]*501*501+x[1]*501+x[3],a[2]);
pw.write("? "+x[0]+" "+x[1]+" "+x[2]+"\n");
pw.flush();
a[3]=sc.nextInt();
map.put((long) x[0]*501*501+x[1]*501+x[2],a[3]);
Integer[] t=new Integer[]{0,1,2,3};
Arrays.sort(t, new Comparator<Integer>() {
@Override
public int compare(Integer integer, Integer t1) {
return a[integer]-a[t1];
}
});
return new int[]{x[t[0]],x[t[1]]};
}
static int curt;
static PrintWriter pw;
static MScanner sc;
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | c6135b039851b627618a1dd77d4625bb | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | /* || श्री राम समर्थ ||
|| जय जय रघुवीर समर्थ ||
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.*;
import static java.util.Arrays.sort;
/*
Think until you get Good idea And then Code Easily
Try Hard And Pay Attention to details (be positive always possible)
think smart
*/
public class CodeforcesTemp {
static Reader scan = new Reader();
static FastPrinter out = new FastPrinter();
static int query(int i,int j ,int k){
out.println("? "+i+" "+j+" "+k);
out.flush();
int res=scan.nextInt();
return res;
}
public static void main(String[] args) throws IOException {
int tt = scan.nextInt();
// int tt = 1;
for (int tc = 1; tc <= tt; tc++) {
int n= scan.nextInt();
int i=1;
int j=2;
boolean allsame=true;
int res=query(i,j,3);
int maxind=3;
for (int k = 4; k <=n ; k++) {
int curr_res=query(i,j,k);
if(curr_res!=res)allsame=false;
if(curr_res>res){
maxind=k;
res=curr_res;
}
}
if(allsame){
if(query(1,2,3)>query(2,3,4)){
out.println("! 1 2");
out.flush();
continue;
}
maxind=3;
}
j=maxind;
res=-1;
allsame=true;
for (int k = 2; k <=n ; k++) {
if(k==j)continue;
int c_res=query(i,j,k);
if(res==-1){
res=c_res;
maxind=k;
continue;
}
if(res!=c_res)allsame=false;
if(c_res>res){
maxind=k;
res=c_res;
}
}
if(allsame){
out.println("! "+i+" "+j);
}else {
out.println("! "+j+" "+maxind);
}
out.flush();
}
out.close();
}
static class Reader {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public Reader(InputStream in) {
this.in = in;
}
public Reader() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
}
}
throw new ArithmeticException(
String.format(" overflows long."));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(PrintStream stream) {
super(stream);
}
public FastPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printMatrix(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
this.printArray(arr[i]);
}
}
public void printCharMatrix(char[][] arr, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
this.print(arr[i][j] + " ");
}
this.println();
}
}
}
static Random __r = new Random();
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] arr, int i, int j) {
while (i < j) {
char temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(int[] arr, int i, int j) {
while (i < j) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void reverse(long[] arr, int i, int j) {
while (i < j) {
long temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j--;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i) ans[i] = a[i];
return ans;
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | c1a5fd6390f861bb24c25cebffc18a13 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.util.Scanner;
public class D
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0)
{
int n = in.nextInt();
int max1 = 0, ans1 = -1;
for (int i = 3; i <= n; i++)
{
System.out.printf("? %d %d %d\n", 1, 2, i);
System.out.flush();
int x = in.nextInt();
if (x > max1)
{
max1 = x;
ans1 = i;
}
}
int a = ans1;
int b = a != 3 ? 3 : 4;
int max2 = 0, ans2 = -1;
for (int i = 1; i <= n; i++)
{
if (i != a && i != b)
{
System.out.printf("? %d %d %d\n", a, b, i);
System.out.flush();
int x = in.nextInt();
if (x > max2)
{
max2 = x;
ans2 = i;
}
}
}
int s1 = ans1, s2 = ans2;
int q = 3;
while (q == s1 || q == s2) q++;
System.out.printf("? %d %d %d\n", 1, 2, q);
System.out.flush();
int x = in.nextInt();
if (x >= max1 && x >= max2)
{
ans1 = 1;
ans2 = 2;
}
q = 1;
while (q == s1 || q == s2) q++;
System.out.printf("? %d %d %d\n", a, b, q);
System.out.flush();
int xx = in.nextInt();
if (xx >= max1 && xx >= max2 && xx >= x)
{
ans1 = a;
ans2 = b;
}
System.out.printf("! %d %d\n", ans1, ans2);
System.out.flush();
}
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 7979dde19b3e83f4e4e2355c890dc3a4 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.util.*;
import java.io.*;
// res.append("Case #"+(p+1)+": "+hh+" \n");
////***************************************************************************
/* public class E_Gardener_and_Tree implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start();
}
public void run(){
WRITE YOUR CODE HERE!!!!
JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!!
}
}
*/
/////**************************************************************************
public class D_Finding_Zero{
public static void main(String[] args) {
FastScanner s= new FastScanner();
// PrintWriter out=new PrintWriter(System.out);
//end of program
//out.println(answer);
//out.close();
StringBuilder res = new StringBuilder();
int t=s.nextInt();
int p=0;
while(p<t){
int n=s.nextInt();
Set<Long> well = new HashSet<>();
long max=-1;
int index=-1;
// System.out.println("n "+n);
for(int i=3;i<=n;i++){
// System.out.println("here "+i);
long hh=query(1,2,i);
if(hh>max){
max=hh;
index=i;
}
well.add(hh);
}
// System.out.println("here2 ");
if(well.size()==1){
// System.out.println("! 1 2 \n");
// System.out.println();
// System.out.flush();
int index4=-1;
long max4=-1;
for(int i=2;i<=n;i++){
if(i==3){
continue;
}
long hh=query(1,3,i);
if(hh>max4){
max4=hh;
index4=i;
}
}
if(max4>max){
System.out.println("! 3 "+index4+" \n");
System.out.println();
System.out.flush();
}
else{
System.out.println("! 1 2 \n");
System.out.println();
System.out.flush();
}
}
else{
int alpha=index;
well= new HashSet<>();
long max2=-1;
int index2=-1;
for(int i=2;i<=n;i++){
if(i==alpha){
continue;
}
long hh=query(alpha,1,i);
if(hh>max2){
max2=hh;
index2=i;
}
well.add(hh);
}
if(well.size()==1){
System.out.println("! 1 "+alpha+" \n");
System.out.println();
System.out.flush();
}
else{
int beta=index2;
System.out.println("! "+alpha+" "+beta+" \n");
System.out.println();
System.out.flush();
}
}
p++;
}
}
private static long query(int a, int b, int c) {
FastScanner s= new FastScanner();
System.out.println("? "+a+" "+b+" "+c+" \n");
System.out.println();
System.out.flush();
long hh=s.nextLong();
return hh;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
static long modpower(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// SIMPLE POWER FUNCTION=>
static long power(long x, long y)
{
long res = 1; // Initialize result
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = res * x;
// y must be even now
y = y >> 1; // y = y/2
x = x * x; // Change x to x^2
}
return res;
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 3f1b8ce61ba2ac4c6b4e5a29c0044b24 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | /*
Setting up my ambitions
Check 'em one at a time, yeah, I made it
Now it's time for ignition
I'm the start, heat it up
They follow, burning up a chain reaction, oh-oh-oh
Go fire it up, oh, oh, no
Assemble the crowd now, now
Line 'em up on the ground
No leaving anyone behind
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class x1634D
{
public static void main(String hi[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
//first batch
int diff = query(1, 2, 3, infile);
int query123 = diff;
int extremeLoc = 3;
boolean allSame = true;
for(int v=4; v <= N; v++)
{
int val = query(1, 2, v, infile);
if(diff != val)
allSame = false;
if(diff < val)
{
diff = val;
extremeLoc = v;
}
}
/*after this step, it's tricky if allSame is true
basically, either (1, 2) are both extremes
or (3...N) are all extremes
we can just assume 3 is the extreme, and check later
*/
if(!allSame)
{
//diff = query(1, 2, extremeLoc) = query(1, extremeLoc, 2)
allSame = true;
int loc = 2;
for(int v=3; v <= N; v++)
{
if(v == extremeLoc)
continue;
int val = query(1, extremeLoc, v, infile);
if(diff != val)
allSame = false;
if(diff < val)
{
diff = val;
loc = v;
}
}
if(allSame)
System.out.println("! 1 "+extremeLoc);
else
System.out.println("! "+extremeLoc+" "+loc);
}
else
{
//guess from 1 and 3, guaranteed at least one extreme in here
//if 1 and 2 are extremes, (1, 2, 3) >= (2, 3, 4)
//otherwise (1, 2, 3) <= (2, 3, 4)
/*
if(query123 >= max(query(2, 3, 4, infile), query(1, 3, 4, infile)))
{
System.out.println("! 1 2");
continue;
}
*/
allSame = true;
int loc = 2;
int oldLoc = -1;
for(int v=3; v <= N; v++)
{
if(v == extremeLoc)
continue;
int val = query(1, extremeLoc, v, infile);
if(diff != val)
{
allSame = false;
if(diff > val && oldLoc == -1)
oldLoc = v;
}
if(diff < val)
{
diff = val;
if(oldLoc == -1)
oldLoc = loc;
loc = v;
}
}
if(allSame)
System.out.println("! 1 "+extremeLoc);
else
{
//either (1, loc) or (extremeLoc, loc)
//assume arr[loc] != 0
//if 1 is actual 0, (1, 2, loc) > (extremeLoc, 2, loc)
//System.out.println("L "+loc+" "+extremeLoc);
if(query(1, oldLoc, loc, infile) > query(extremeLoc, oldLoc, loc, infile))
System.out.println("! "+1+" "+loc);
else
System.out.println("! "+extremeLoc+" "+loc);
}
}
}
}
public static int query(int a, int b, int c, BufferedReader infile) throws Exception
{
System.out.println("? "+a+" "+b+" "+c);
return Integer.parseInt(infile.readLine());
}
}
/*
compare 1 2 (i=[3..N])
[1, 4, 3, 0, 28, 9]
find the most extreme value (will either give 0 or the largest number in array)
N-2 queries
if the value never changes, then guess (1, 2)
otherwise keep a list of the "important" positions
in this list, either a_x-min(a_1, a_2) = diff or max(a_1, a_2)-a_x = diff (note a_x=0 here)
only two possible values in list (although possible none are a_x = 0)
let x be a location of an "extreme"
find extreme values again, now with indices (1, x)
N-2 queries
This guarantees the extreme value will occur at a "0" or a "max"
either way we have locations of min (0) and max value in array
just return both
*/ | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 028aebf184ba54bd7c0cbd8f7192bb49 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Solution {
// VM options: -Xmx1024m -Xss1024m
private void solve() throws IOException {
cache = new HashMap<>();
int n = nextInt();
int[] a = new int[n];
int mxi1 = 2;
for (int i = 2; i < n; i++) {
a[i] = ask(0, 1, i);
if (a[i] > a[mxi1]) {
mxi1 = i;
}
}
int mxi2 = mxi1 == 2 ? 3 : 2;
for (int i = 2; i < n; i++) {
if (i != mxi1 && a[i] > a[mxi2]) {
mxi2 = i;
}
}
int mxi3 = -1;
for (int i = 0; i < n; i++) {
if (i != mxi1 && i != mxi2) {
a[i] = ask(mxi1, mxi2, i);
if (mxi3 == -1 || a[i] > a[mxi3]) {
mxi3 = i;
}
}
}
int mxi4 = -1;
for (int i = 0; i < n; i++) {
if (i != mxi1 && i != mxi2 && i != mxi3) {
if (mxi4 == -1 || a[i] > a[mxi4]) {
mxi4 = i;
}
}
}
int[] mx = new int[]{mxi1, mxi2, mxi3, mxi4};
int mxV = 0;
for (int i = 0; i < 4; i++) {
int[] r = new int[3];
for (int j = 0, c = 0; j < 4; j++) {
if (i != j) {
r[c++] = mx[j];
}
}
a[i] = ask(r[0], r[1], r[2]);
mxV = Math.max(mxV, a[i]);
}
if (mxV < ask(0, 1, mxi1) || mxV < ask(0, 1, mxi1)) {
out.println("! " + (0 + 1) + " " + (1 + 1));
out.flush();
} else {
int[] rr = new int[4];
int rc = 0;
for (int i = 0; i < 4; i++) {
if (a[i] != mxV) {
rr[rc++] = i;
}
}
if (rc == 1) {
rr[rc] = rr[rc - 1];
}
out.println("! " + (mx[rr[0]] + 1) + " " + (mx[rr[1]] + 1));
out.flush();
}
}
private static class K {
int i;
int j;
int k;
public K(int i, int j, int k) {
this.i = Math.min(i, Math.min(j, k));
this.k = Math.max(i, Math.max(j, k));
this.j = i + j + k - this.i - this.k;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
K k1 = (K) o;
if (i != k1.i) return false;
if (j != k1.j) return false;
return k == k1.k;
}
@Override
public int hashCode() {
int result = i;
result = 31 * result + j;
result = 31 * result + k;
return result;
}
}
private HashMap<K, Integer> cache;
private int ask(int i, int j, int k) throws IOException {
K key = new K(i, j, k);
Integer res = cache.get(key);
if (res != null) {
return res;
}
out.println("? " + (i + 1) + " " + (j + 1) + " " + (k + 1));
out.flush();
res = nextInt();
cache.put(key, res);
return res;
}
private static final String inputFilename = "input.txt";
private static final String outputFilename = "output.txt";
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
private final boolean isDebug;
private Solution(boolean isDebug) {
this.isDebug = isDebug;
}
public static void main(String[] args) throws IOException {
new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args);
}
private void run(String[] args) throws IOException {
if (isDebug) {
// in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
}
out = new PrintWriter(System.out);
// out = new PrintWriter(outputFilename);
// int t = isDebug ? nextInt() : 1;
int t = nextInt();
// int t = 1;
for (int i = 0; i < t; i++) {
// out.print("Case #" + (i + 1) + ": ");
solve();
out.flush();
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if (!p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if (!p) {
throw new RuntimeException(message);
}
}
private static void assertNotEqual(int unexpected, int actual) {
if (actual == unexpected) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static void assertEqual(int expected, int actual) {
if (expected != actual) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | fd919a939b448ca49946952e8337bb01 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 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.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
// D -> CodeForcesProblemSet
public final class D {
static FastReader fr = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static final int gigamod = 1000000007;
static int t = 1;
static double epsilon = 0.00000001;
static boolean[] isPrime;
static int[] smallestFactorOf;
@SuppressWarnings("unused")
public static void main(String[] args) {
t = fr.nextInt();
OUTER:
for (int tc = 0; tc < t; tc++) {
int n = fr.nextInt();
// Observations:
// 1. How will we solve the problem?
// a. Fix i = 1 and j = 2.
// b. Move k in [3, n].
// c. Wherever diff was max, label the point OP and fix j = OP.
// d. CASE 1: OP == 3
// max and min both lie in [1, 3].
// Solution:
// CASE 2: OP > 3
// OP is some optimum. It is either the maximum index or the minimum.
// Solution: Fix i = 1, j = OP. Move k in [3, n] (!= OP). Wherever diff
// was max, label OP2.
// max and min both lie in [1, OP, OP2]
long i = 1, j = 2;
long op = 3, maxDiff = Integer.MIN_VALUE;
for (int k = 3; k < n + 1; k++) {
out.println("? "+ i + " " + j + " " + k);
out.flush();
long diff = fr.nextLong();
if (diff > maxDiff) {
maxDiff = diff;
op = k;
}
}
// n interactions left
i = 1; j = op;
long op2 = 2;
for (int k = 3; k < n + 1; k++)
if (k != op) {
out.println("? " + i + " " + j + " " + k);
out.flush();
long diff = fr.nextLong();
if (diff > maxDiff) {
maxDiff = diff;
op2 = k;
}
}
long k = op2;
// (i, j, k) are the three candidates
// 3 interactions left
int other = 0;
for (int p = 1; p < n + 1; p++)
if (p != i && p != j && p != k)
other = p;
ArrayList<Long> ans = new ArrayList<>();
out.println("? " + other + " " + j + " " + k);
out.flush();
long diff = fr.nextLong();
if (diff < maxDiff) {
// i was either the min or the max
ans.add(i);
}
out.println("? " + i + " " + other + " " + k);
out.flush();
diff = fr.nextLong();
if (diff < maxDiff) {
// j was either the min or the max
ans.add(j);
}
out.println("? " + i + " " + j + " " + other);
out.flush();
diff = fr.nextLong();
if (diff < maxDiff) {
// k was either the min or the max
ans.add(k);
}
if (ans.size() == 1) {
out.println("! " + ans.get(0) + " " + other);
out.flush();
} else {
out.println("! " + ans.get(0) + " " + ans.get(1));
out.flush();
}
}
out.close();
}
static class UnionFind {
// Uses weighted quick-union with path compression.
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of sites in tree rooted at i
// Note: not necessarily correct if i is not a root node
private int count; // number of components
public UnionFind(int n) {
count = n;
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
// Number of connected components.
public int count() {
return count;
}
// Find the root of p.
public int find(int p) {
while (p != parent[p])
p = parent[p];
return p;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int numConnectedTo(int node) {
return size[find(node)];
}
// Weighted union.
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make smaller root point to larger one
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
count--;
}
public static int[] connectedComponents(UnionFind uf) {
// We can do this in nlogn.
int n = uf.size.length;
int[] compoColors = new int[n];
for (int i = 0; i < n; i++)
compoColors[i] = uf.find(i);
HashMap<Integer, Integer> oldToNew = new HashMap<>();
int newCtr = 0;
for (int i = 0; i < n; i++) {
int thisOldColor = compoColors[i];
Integer thisNewColor = oldToNew.get(thisOldColor);
if (thisNewColor == null)
thisNewColor = newCtr++;
oldToNew.put(thisOldColor, thisNewColor);
compoColors[i] = thisNewColor;
}
return compoColors;
}
}
static class UGraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public UGraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
adj[to].add(from);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int degree(int v) {
return adj[v].size();
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void dfsMark(int current, boolean[] marked, UGraph g) {
if (marked[current]) return;
marked[current] = true;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, marked, g);
}
public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) {
if (marked[current]) return;
marked[current] = true;
if (from != -1)
distTo[current] = distTo[from] + 1;
HashSet<Integer> adj = g.adj(current);
int alreadyMarkedCtr = 0;
for (int adjc : adj) {
if (marked[adjc]) alreadyMarkedCtr++;
dfsMark(adjc, current, distTo, marked, g, endPoints);
}
if (alreadyMarkedCtr == adj.size())
endPoints.add(current);
}
public static void bfsOrder(int current, UGraph g) {
}
public static void dfsMark(int current, int[] colorIds, int color, UGraph g) {
if (colorIds[current] != -1) return;
colorIds[current] = color;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, colorIds, color, g);
}
public static int[] connectedComponents(UGraph g) {
int n = g.V();
int[] componentId = new int[n];
Arrays.fill(componentId, -1);
int colorCtr = 0;
for (int i = 0; i < n; i++) {
if (componentId[i] != -1) continue;
dfsMark(i, componentId, colorCtr, g);
colorCtr++;
}
return componentId;
}
public static boolean hasCycle(UGraph ug) {
int n = ug.V();
boolean[] marked = new boolean[n];
boolean[] hasCycleFirst = new boolean[1];
for (int i = 0; i < n; i++) {
if (marked[i]) continue;
hcDfsMark(i, ug, marked, hasCycleFirst, -1);
}
return hasCycleFirst[0];
}
// Helper for hasCycle.
private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) {
if (marked[current]) return;
if (hasCycleFirst[0]) return;
marked[current] = true;
HashSet<Integer> adjc = ug.adj(current);
for (int adj : adjc) {
if (marked[adj] && adj != parent && parent != -1) {
hasCycleFirst[0] = true;
return;
}
hcDfsMark(adj, ug, marked, hasCycleFirst, current);
}
}
}
static class Digraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public Digraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public Digraph reversed() {
Digraph dg = new Digraph(V());
for (int i = 0; i < V(); i++)
for (int adjVert : adj(i)) dg.addEdge(adjVert, i);
return dg;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static int[] KosarajuSharirSCC(Digraph dg) {
int[] id = new int[dg.V()];
Digraph reversed = dg.reversed();
// Gotta perform topological sort on this one to get the stack.
Stack<Integer> revStack = Digraph.topologicalSort(reversed);
// Initializing id and idCtr.
id = new int[dg.V()];
int idCtr = -1;
// Creating a 'marked' array.
boolean[] marked = new boolean[dg.V()];
while (!revStack.isEmpty()) {
int vertex = revStack.pop();
if (!marked[vertex])
sccDFS(dg, vertex, marked, ++idCtr, id);
}
return id;
}
private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) {
marked[source] = true;
id[source] = idCtr;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id);
}
public static Stack<Integer> topologicalSort(Digraph dg) {
// dg has to be a directed acyclic graph.
// We'll have to run dfs on the digraph and push the deepest nodes on stack first.
// We'll need a Stack<Integer> and a int[] marked.
Stack<Integer> topologicalStack = new Stack<Integer>();
boolean[] marked = new boolean[dg.V()];
// Calling dfs
for (int i = 0; i < dg.V(); i++)
if (!marked[i])
runDfs(dg, topologicalStack, marked, i);
return topologicalStack;
}
static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) {
marked[source] = true;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex])
runDfs(dg, topologicalStack, marked, adjVertex);
topologicalStack.add(source);
}
}
static class FastReader {
private BufferedReader bfr;
private StringTokenizer st;
public FastReader() {
bfr = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
if (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(bfr.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());
}
char nextChar() {
return next().toCharArray()[0];
}
String nextString() {
return next();
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
double[] nextDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextDouble();
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
int[][] nextIntGrid(int n, int m) {
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
char[] line = fr.next().toCharArray();
for (int j = 0; j < m; j++)
grid[i][j] = line[j] - 48;
}
return grid;
}
}
static class LCA {
int[] height, first, segtree;
ArrayList<Integer> euler;
boolean[] visited;
int n;
LCA(ArrayList<Point>[] outFrom, int root) {
n = outFrom.length;
height = new int[n];
first = new int[n];
euler = new ArrayList<>();
visited = new boolean[n];
dfs(outFrom, root, 0);
int m = euler.size();
segtree = new int[m * 4];
build(1, 0, m - 1);
}
void dfs(ArrayList<Point>[] outFrom, int node, int h) {
visited[node] = true;
height[node] = h;
first[node] = euler.size();
euler.add(node);
for (Point to : outFrom[node]) {
if (!visited[(int) to.x]) {
dfs(outFrom, (int) to.x, h + 1);
euler.add(node);
}
}
}
void build(int node, int b, int e) {
if (b == e) {
segtree[node] = euler.get(b);
} else {
int mid = (b + e) / 2;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
int l = segtree[node << 1], r = segtree[node << 1 | 1];
segtree[node] = (height[l] < height[r]) ? l : r;
}
}
int query(int node, int b, int e, int L, int R) {
if (b > R || e < L)
return -1;
if (b >= L && e <= R)
return segtree[node];
int mid = (b + e) >> 1;
int left = query(node << 1, b, mid, L, R);
int right = query(node << 1 | 1, mid + 1, e, L, R);
if (left == -1) return right;
if (right == -1) return left;
return height[left] < height[right] ? left : right;
}
int lca(int u, int v) {
int left = first[u], right = first[v];
if (left > right) {
int temp = left;
left = right;
right = temp;
}
return query(1, 0, euler.size() - 1, left, right);
}
}
static class FenwickTree {
long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates
public FenwickTree(int size) {
array = new long[size + 1];
}
public long rsq(int ind) {
assert ind > 0;
long sum = 0;
while (ind > 0) {
sum += array[ind];
//Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number
ind -= ind & (-ind);
}
return sum;
}
public long rsq(int a, int b) {
assert b >= a && a > 0 && b > 0;
return rsq(b) - rsq(a - 1);
}
public void update(int ind, long value) {
assert ind > 0;
while (ind < array.length) {
array[ind] += value;
//Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number
ind += ind & (-ind);
}
}
public int size() {
return array.length - 1;
}
}
static final class RedBlackCountSet {
// Manual implementation of RB-BST for C++ PBDS functionality.
// Modification required according to requirements.
// Colors for Nodes:
private static final boolean RED = true;
private static final boolean BLACK = false;
// Pointer to the root:
private Node root;
// Public constructor:
public RedBlackCountSet() {
root = null;
}
// Node class for storing key value pairs:
private class Node {
long key;
long value;
Node left, right;
boolean color;
long size; // Size of the subtree rooted at that node.
public Node(long key, long value, boolean color) {
this.key = key;
this.value = value;
this.left = this.right = null;
this.color = color;
this.size = value;
}
}
/***** Invariant Maintenance functions: *****/
// When a temporary 4 node is formed:
private Node flipColors(Node node) {
node.left.color = node.right.color = BLACK;
node.color = RED;
return node;
}
// When there's a right leaning red link and it is not a 3 node:
private Node rotateLeft(Node node) {
Node rn = node.right;
node.right = rn.left;
rn.left = node;
rn.color = node.color;
node.color = RED;
return rn;
}
// When there are 2 red links in a row:
private Node rotateRight(Node node) {
Node ln = node.left;
node.left = ln.right;
ln.right = node;
ln.color = node.color;
node.color = RED;
return ln;
}
/***** Invariant Maintenance functions end *****/
// Public functions:
public void put(long key) {
root = put(root, key);
root.color = BLACK; // One of the invariants.
}
private Node put(Node node, long key) {
// Standard insertion procedure:
if (node == null)
return new Node(key, 1, RED);
// Recursing:
int cmp = Long.compare(key, node.key);
if (cmp < 0)
node.left = put(node.left, key);
else if (cmp > 0)
node.right = put(node.right, key);
else
node.value++;
// Invariant maintenance:
if (node.left != null && node.right != null) {
if (node.right.color = RED && node.left.color == BLACK)
node = rotateLeft(node);
if (node.left.color == RED && node.left.left.color == RED)
node = rotateRight(node);
if (node.left.color == RED && node.right.color == RED)
node = flipColors(node);
}
// Property maintenance:
node.size = node.value + size(node.left) + size(node.right);
return node;
}
private long size(Node node) {
if (node == null)
return 0;
else
return node.size;
}
public long rank(long key) {
return rank(root, key);
}
// Modify according to requirements.
private long rank(Node node, long key) {
if (node == null) return 0;
int cmp = Long.compare(key, node.key);
if (cmp < 0)
return rank(node.left, key);
else if (cmp > 0)
return node.value + size(node.left) + rank(node.right, key);
else
return node.value + size(node.left);
}
}
static class SegmentTree {
private Node[] heap;
private int[] array;
private int size;
public SegmentTree(int[] array) {
this.array = Arrays.copyOf(array, array.length);
//The max size of this array is about 2 * 2 ^ log2(n) + 1
size = (int) (2 * Math.pow(2.0, Math.floor((Math.log((double) array.length) / Math.log(2.0)) + 1)));
heap = new Node[size];
build(1, 0, array.length);
}
public int size() {
return array.length;
}
//Initialize the Nodes of the Segment tree
private void build(int v, int from, int size) {
heap[v] = new Node();
heap[v].from = from;
heap[v].to = from + size - 1;
if (size == 1) {
heap[v].sum = array[from];
heap[v].min = array[from];
} else {
//Build childs
build(2 * v, from, size / 2);
build(2 * v + 1, from + size / 2, size - size / 2);
heap[v].sum = heap[2 * v].sum + heap[2 * v + 1].sum;
//min = min of the children
heap[v].min = Math.min(heap[2 * v].min, heap[2 * v + 1].min);
}
}
public int rsq(int from, int to) {
return rsq(1, from, to);
}
private int rsq(int v, int from, int to) {
Node n = heap[v];
//If you did a range update that contained this node, you can infer the Sum without going down the tree
if (n.pendingVal != null && contains(n.from, n.to, from, to)) {
return (to - from + 1) * n.pendingVal;
}
if (contains(from, to, n.from, n.to)) {
return heap[v].sum;
}
if (intersects(from, to, n.from, n.to)) {
propagate(v);
int leftSum = rsq(2 * v, from, to);
int rightSum = rsq(2 * v + 1, from, to);
return leftSum + rightSum;
}
return 0;
}
public int rMinQ(int from, int to) {
return rMinQ(1, from, to);
}
private int rMinQ(int v, int from, int to) {
Node n = heap[v];
//If you did a range update that contained this node, you can infer the Min value without going down the tree
if (n.pendingVal != null && contains(n.from, n.to, from, to)) {
return n.pendingVal;
}
if (contains(from, to, n.from, n.to)) {
return heap[v].min;
}
if (intersects(from, to, n.from, n.to)) {
propagate(v);
int leftMin = rMinQ(2 * v, from, to);
int rightMin = rMinQ(2 * v + 1, from, to);
return Math.min(leftMin, rightMin);
}
return Integer.MAX_VALUE;
}
public void update(int from, int to, int value) {
update(1, from, to, value);
}
private void update(int v, int from, int to, int value) {
//The Node of the heap tree represents a range of the array with bounds: [n.from, n.to]
Node n = heap[v];
if (contains(from, to, n.from, n.to)) {
change(n, value);
}
if (n.size() == 1) return;
if (intersects(from, to, n.from, n.to)) {
propagate(v);
update(2 * v, from, to, value);
update(2 * v + 1, from, to, value);
n.sum = heap[2 * v].sum + heap[2 * v + 1].sum;
n.min = Math.min(heap[2 * v].min, heap[2 * v + 1].min);
}
}
//Propagate temporal values to children
private void propagate(int v) {
Node n = heap[v];
if (n.pendingVal != null) {
change(heap[2 * v], n.pendingVal);
change(heap[2 * v + 1], n.pendingVal);
n.pendingVal = null; //unset the pending propagation value
}
}
//Save the temporal values that will be propagated lazily
private void change(Node n, int value) {
n.pendingVal = value;
n.sum = n.size() * value;
n.min = value;
array[n.from] = value;
}
//Test if the range1 contains range2
private boolean contains(int from1, int to1, int from2, int to2) {
return from2 >= from1 && to2 <= to1;
}
//check inclusive intersection, test if range1[from1, to1] intersects range2[from2, to2]
private boolean intersects(int from1, int to1, int from2, int to2) {
return from1 <= from2 && to1 >= from2 // (.[..)..] or (.[...]..)
|| from1 >= from2 && from1 <= to2; // [.(..]..) or [..(..)..
}
//The Node class represents a partition range of the array.
static class Node {
int sum;
int min;
//Here We store the value that will be propagated lazily
Integer pendingVal = null;
int from;
int to;
int size() {
return to - from + 1;
}
}
}
@SuppressWarnings("serial")
static class CountMap<T> extends HashMap<T, Integer>{
CountMap() {
}
CountMap(T[] arr) {
this.putCM(arr);
}
public Integer putCM(T key) {
if (super.containsKey(key)) {
return super.put(key, super.get(key) + 1);
} else {
return super.put(key, 1);
}
}
public Integer removeCM(T key) {
Integer count = super.get(key);
if (count == null) return -1;
if (count == 1)
return super.remove(key);
else
return super.put(key, super.get(key) - 1);
}
public Integer getCM(T key) {
Integer count = super.get(key);
if (count == null)
return 0;
return count;
}
public void putCM(T[] arr) {
for (T l : arr)
this.putCM(l);
}
}
static class Point implements Comparable<Point> {
long x;
long y;
long id;
Point() {
x = y = id = 0;
}
Point(Point p) {
this.x = p.x;
this.y = p.y;
this.id = p.id;
}
Point(long a, long b, long id) {
this.x = a;
this.y = b;
this.id = id;
}
Point(long a, long b) {
this.x = a;
this.y = b;
}
@Override
public int compareTo(Point o) {
if (this.x < o.x)
return -1;
if (this.x > o.x)
return 1;
if (this.y < o.y)
return -1;
if (this.y > o.y)
return 1;
return 0;
}
public boolean equals(Point that) {
return this.compareTo(that) == 0;
}
}
static int longestPrefixPalindromeLen(char[] s) {
int n = s.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++)
sb.append(s[i]);
StringBuilder sb2 = new StringBuilder(sb);
sb.append('^');
sb.append(sb2.reverse());
// out.println(sb.toString());
char[] newS = sb.toString().toCharArray();
int m = newS.length;
int[] pi = piCalcKMP(newS);
return pi[m - 1];
}
static int KMPNumOcc(char[] text, char[] pat) {
int n = text.length;
int m = pat.length;
char[] patPlusText = new char[n + m + 1];
for (int i = 0; i < m; i++)
patPlusText[i] = pat[i];
patPlusText[m] = '^'; // Seperator
for (int i = 0; i < n; i++)
patPlusText[m + i] = text[i];
int[] fullPi = piCalcKMP(patPlusText);
int answer = 0;
for (int i = 0; i < n + m + 1; i++)
if (fullPi[i] == m)
answer++;
return answer;
}
static int[] piCalcKMP(char[] s) {
int n = s.length;
int[] pi = new int[n];
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static String toBinaryString(long num, int bits) {
StringBuilder sb = new StringBuilder(Long.toBinaryString(num));
sb.reverse();
for (int i = sb.length(); i < bits; i++)
sb.append('0');
return sb.reverse().toString();
}
static long modDiv(long a, long b) {
return mod(a * power(b, gigamod - 2, gigamod));
}
static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) {
if (m > limit) return;
if (m <= limit && n <= limit)
coprimes.add(new Point(m, n));
if (coprimes.size() > numCoprimes) return;
coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes);
coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes);
coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes);
}
static long hash(long i) {
return (i * 2654435761L % gigamod);
}
static long hash2(long key) {
long h = Long.hashCode(key);
h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4);
return h & (gigamod-1);
}
static int mapTo1D(int row, int col, int n, int m) {
// Maps elements in a 2D matrix serially to elements in
// a 1D array.
return row * m + col;
}
static int[] mapTo2D(int idx, int n, int m) {
// Inverse of what the one above does.
int[] rnc = new int[2];
rnc[0] = idx / m;
rnc[1] = idx % m;
return rnc;
}
static double distance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p2.y-p1.y, 2) + Math.pow(p2.x-p1.x, 2));
}
static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) {
if (smallestFactorOf == null)
primeGenerator(200001);
CountMap<Integer> fnps = new CountMap<>();
while (num != 1) {
fnps.putCM(smallestFactorOf[num]);
num /= smallestFactorOf[num];
}
return fnps;
}
static HashMap<Long, Integer> primeFactorization(long num) {
// Returns map of factor and its power in the number.
HashMap<Long, Integer> map = new HashMap<>();
while (num % 2 == 0) {
num /= 2;
Integer pwrCnt = map.get(2L);
map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1);
}
for (long i = 3; i * i <= num; i += 2) {
while (num % i == 0) {
num /= i;
Integer pwrCnt = map.get(i);
map.put(i, pwrCnt != null ? pwrCnt + 1 : 1);
}
}
// If the number is prime, we have to add it to the
// map.
if (num != 1)
map.put(num, 1);
return map;
}
static HashSet<Long> divisors(long num) {
HashSet<Long> divisors = new HashSet<Long>();
divisors.add(1L);
divisors.add(num);
for (long i = 2; i * i <= num; i++) {
if (num % i == 0) {
divisors.add(num/i);
divisors.add(i);
}
}
return divisors;
}
static int bsearch(int[] arr, int val, int lo, int hi) {
// Returns the index of the first element
// larger than or equal to val.
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
idx = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
return idx;
}
static int bsearch(long[] arr, long val, int lo, int hi) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
idx = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
return idx;
}
static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) {
// Returns the index of the last element
// smaller than or equal to val.
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
hi = mid - 1;
} else {
idx = mid;
lo = mid + 1;
}
}
return idx;
}
static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] > val) {
hi = mid - 1;
} else {
idx = mid;
lo = mid + 1;
}
}
return idx;
}
static long factorial(long n) {
if (n <= 1)
return 1;
long factorial = 1;
for (int i = 1; i <= n; i++)
factorial = mod(factorial * i);
return factorial;
}
static long factorialInDivision(long a, long b) {
if (a == b)
return 1;
if (b < a) {
long temp = a;
a = b;
b = temp;
}
long factorial = 1;
for (long i = a + 1; i <= b; i++)
factorial = mod(factorial * i);
return factorial;
}
static long nCr(long n, long r, long[] fac) {
long p = 998244353;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
/*long fac[] = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;*/
return (fac[(int)n] * modInverse(fac[(int)r], p) % p
* modInverse(fac[(int)n - (int)r], p) % p) % p;
}
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
static long power(long x, long y, long p) {
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long nPr(long n, long r) {
return factorialInDivision(n, n - r);
}
static int logk(long n, long k) {
return (int)(Math.log(n) / Math.log(k));
}
static boolean[] primeGenerator(int upto) {
// Sieve of Eratosthenes:
isPrime = new boolean[upto + 1];
smallestFactorOf = new int[upto + 1];
Arrays.fill(smallestFactorOf, 1);
Arrays.fill(isPrime, true);
isPrime[1] = isPrime[0] = false;
for (long i = 2; i < upto + 1; i++)
if (isPrime[(int) i]) {
smallestFactorOf[(int) i] = (int) i;
// Mark all the multiples greater than or equal
// to the square of i to be false.
for (long j = i; j * i < upto + 1; j++) {
if (isPrime[(int) j * (int) i]) {
isPrime[(int) j * (int) i] = false;
smallestFactorOf[(int) j * (int) i] = (int) i;
}
}
}
return isPrime;
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static long gcd(long[] arr) {
int n = arr.length;
long gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = gcd(gcd, arr[i]);
}
return gcd;
}
static int gcd(int[] arr) {
int n = arr.length;
int gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = gcd(gcd, arr[i]);
}
return gcd;
}
static long lcm(long[] arr) {
long lcm = arr[0];
int n = arr.length;
for (int i = 1; i < n; i++) {
lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
}
return lcm;
}
static long lcm(long a, long b) {
return (a * b)/gcd(a, b);
}
static boolean less(int a, int b) {
return a < b ? true : false;
}
static boolean isSorted(int[] a) {
for (int i = 1; i < a.length; i++) {
if (less(a[i], a[i - 1]))
return false;
}
return true;
}
static boolean isSorted(long[] a) {
for (int i = 1; i < a.length; i++) {
if (a[i] < a[i - 1])
return false;
}
return true;
}
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(long[] a, int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(double[] a, int i, int j) {
double temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void sort(int[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(char[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(long[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(double[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void reverseSort(int[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(char[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverse(char[] arr) {
int n = arr.length;
for (int i = 0; i < n / 2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(long[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(double[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void shuffleArray(long[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(int[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(double[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
double tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(char[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
char tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
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 String toString(int[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(boolean[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(long[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(char[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + "");
return sb.toString();
}
static String toString(int[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(long[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(double[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(char[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static long mod(long a, long m) {
return (a%m + m) % m;
}
static long mod(long num) {
return (num % gigamod + gigamod) % gigamod;
}
}
// NOTES:
// ASCII VALUE OF 'A': 65
// ASCII VALUE OF 'a': 97
// Range of long: 9 * 10^18
// ASCII VALUE OF '0': 48
// Primes upto 'n' can be given by (n / (logn)). | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | dd34c155a49e2ba34e908347759be183 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 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.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
// D -> CodeForcesProblemSet
public final class D {
static FastReader fr = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
static final int gigamod = 1000000007;
static int t = 1;
static double epsilon = 0.00000001;
static boolean[] isPrime;
static int[] smallestFactorOf;
@SuppressWarnings("unused")
public static void main(String[] args) {
t = fr.nextInt();
OUTER:
for (int tc = 0; tc < t; tc++) {
int n = fr.nextInt();
// Observations:
// 1. How will we solve the problem?
// a. Fix i = 1 and j = 2.
// b. Move k in [3, n].
// c. Wherever diff was max, label the point OP and fix j = OP.
// d. CASE 1: OP == 3
// max and min both lie in [1, 3].
// Solution:
// CASE 2: OP > 3
// OP is some optimum. It is either the maximum index or the minimum.
// Solution: Fix i = 1, j = OP. Move k in [3, n] (!= OP). Wherever diff
// was max, label OP2.
// max and min both lie in [1, OP, OP2]
long i = 1, j = 2;
long op = 3, maxDiff = Integer.MIN_VALUE;
for (int k = 3; k < n + 1; k++) {
System.out.println("? "+ i + " " + j + " " + k);
System.out.flush();
long diff = fr.nextLong();
if (diff > maxDiff) {
maxDiff = diff;
op = k;
}
}
// n interactions left
i = 1; j = op;
long op2 = 2;
for (int k = 3; k < n + 1; k++)
if (k != op) {
System.out.println("? " + i + " " + j + " " + k);
System.out.flush();
long diff = fr.nextLong();
if (diff > maxDiff) {
maxDiff = diff;
op2 = k;
}
}
long k = op2;
// (i, j, k) are the three candidates
// 3 interactions left
int other = 0;
for (int p = 1; p < n + 1; p++)
if (p != i && p != j && p != k)
other = p;
ArrayList<Long> ans = new ArrayList<>();
System.out.println("? " + other + " " + j + " " + k);
System.out.flush();
long diff = fr.nextLong();
if (diff < maxDiff) {
// i was either the min or the max
ans.add(i);
}
System.out.println("? " + i + " " + other + " " + k);
System.out.flush();
diff = fr.nextLong();
if (diff < maxDiff) {
// j was either the min or the max
ans.add(j);
}
System.out.println("? " + i + " " + j + " " + other);
System.out.flush();
diff = fr.nextLong();
if (diff < maxDiff) {
// k was either the min or the max
ans.add(k);
}
if (ans.size() == 1) {
System.out.println("! " + ans.get(0) + " " + other);
System.out.flush();
} else {
System.out.println("! " + ans.get(0) + " " + ans.get(1));
System.out.flush();
}
}
out.close();
}
static class UnionFind {
// Uses weighted quick-union with path compression.
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of sites in tree rooted at i
// Note: not necessarily correct if i is not a root node
private int count; // number of components
public UnionFind(int n) {
count = n;
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
// Number of connected components.
public int count() {
return count;
}
// Find the root of p.
public int find(int p) {
while (p != parent[p])
p = parent[p];
return p;
}
public boolean connected(int p, int q) {
return find(p) == find(q);
}
public int numConnectedTo(int node) {
return size[find(node)];
}
// Weighted union.
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make smaller root point to larger one
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
count--;
}
public static int[] connectedComponents(UnionFind uf) {
// We can do this in nlogn.
int n = uf.size.length;
int[] compoColors = new int[n];
for (int i = 0; i < n; i++)
compoColors[i] = uf.find(i);
HashMap<Integer, Integer> oldToNew = new HashMap<>();
int newCtr = 0;
for (int i = 0; i < n; i++) {
int thisOldColor = compoColors[i];
Integer thisNewColor = oldToNew.get(thisOldColor);
if (thisNewColor == null)
thisNewColor = newCtr++;
oldToNew.put(thisOldColor, thisNewColor);
compoColors[i] = thisNewColor;
}
return compoColors;
}
}
static class UGraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public UGraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
adj[to].add(from);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int degree(int v) {
return adj[v].size();
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static void dfsMark(int current, boolean[] marked, UGraph g) {
if (marked[current]) return;
marked[current] = true;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, marked, g);
}
public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) {
if (marked[current]) return;
marked[current] = true;
if (from != -1)
distTo[current] = distTo[from] + 1;
HashSet<Integer> adj = g.adj(current);
int alreadyMarkedCtr = 0;
for (int adjc : adj) {
if (marked[adjc]) alreadyMarkedCtr++;
dfsMark(adjc, current, distTo, marked, g, endPoints);
}
if (alreadyMarkedCtr == adj.size())
endPoints.add(current);
}
public static void bfsOrder(int current, UGraph g) {
}
public static void dfsMark(int current, int[] colorIds, int color, UGraph g) {
if (colorIds[current] != -1) return;
colorIds[current] = color;
Iterable<Integer> adj = g.adj(current);
for (int adjc : adj)
dfsMark(adjc, colorIds, color, g);
}
public static int[] connectedComponents(UGraph g) {
int n = g.V();
int[] componentId = new int[n];
Arrays.fill(componentId, -1);
int colorCtr = 0;
for (int i = 0; i < n; i++) {
if (componentId[i] != -1) continue;
dfsMark(i, componentId, colorCtr, g);
colorCtr++;
}
return componentId;
}
public static boolean hasCycle(UGraph ug) {
int n = ug.V();
boolean[] marked = new boolean[n];
boolean[] hasCycleFirst = new boolean[1];
for (int i = 0; i < n; i++) {
if (marked[i]) continue;
hcDfsMark(i, ug, marked, hasCycleFirst, -1);
}
return hasCycleFirst[0];
}
// Helper for hasCycle.
private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) {
if (marked[current]) return;
if (hasCycleFirst[0]) return;
marked[current] = true;
HashSet<Integer> adjc = ug.adj(current);
for (int adj : adjc) {
if (marked[adj] && adj != parent && parent != -1) {
hasCycleFirst[0] = true;
return;
}
hcDfsMark(adj, ug, marked, hasCycleFirst, current);
}
}
}
static class Digraph {
// Adjacency list.
private HashSet<Integer>[] adj;
private static final String NEWLINE = "\n";
private int E;
@SuppressWarnings("unchecked")
public Digraph(int V) {
adj = (HashSet<Integer>[]) new HashSet[V];
E = 0;
for (int i = 0; i < V; i++)
adj[i] = new HashSet<Integer>();
}
public void addEdge(int from, int to) {
if (adj[from].contains(to)) return;
E++;
adj[from].add(to);
}
public HashSet<Integer> adj(int from) {
return adj[from];
}
public int V() {
return adj.length;
}
public int E() {
return E;
}
public Digraph reversed() {
Digraph dg = new Digraph(V());
for (int i = 0; i < V(); i++)
for (int adjVert : adj(i)) dg.addEdge(adjVert, i);
return dg;
}
public String toString() {
StringBuilder s = new StringBuilder();
s.append(V() + " vertices, " + E() + " edges " + NEWLINE);
for (int v = 0; v < V(); v++) {
s.append(v + ": ");
for (int w : adj[v]) {
s.append(w + " ");
}
s.append(NEWLINE);
}
return s.toString();
}
public static int[] KosarajuSharirSCC(Digraph dg) {
int[] id = new int[dg.V()];
Digraph reversed = dg.reversed();
// Gotta perform topological sort on this one to get the stack.
Stack<Integer> revStack = Digraph.topologicalSort(reversed);
// Initializing id and idCtr.
id = new int[dg.V()];
int idCtr = -1;
// Creating a 'marked' array.
boolean[] marked = new boolean[dg.V()];
while (!revStack.isEmpty()) {
int vertex = revStack.pop();
if (!marked[vertex])
sccDFS(dg, vertex, marked, ++idCtr, id);
}
return id;
}
private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) {
marked[source] = true;
id[source] = idCtr;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id);
}
public static Stack<Integer> topologicalSort(Digraph dg) {
// dg has to be a directed acyclic graph.
// We'll have to run dfs on the digraph and push the deepest nodes on stack first.
// We'll need a Stack<Integer> and a int[] marked.
Stack<Integer> topologicalStack = new Stack<Integer>();
boolean[] marked = new boolean[dg.V()];
// Calling dfs
for (int i = 0; i < dg.V(); i++)
if (!marked[i])
runDfs(dg, topologicalStack, marked, i);
return topologicalStack;
}
static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) {
marked[source] = true;
for (Integer adjVertex : dg.adj(source))
if (!marked[adjVertex])
runDfs(dg, topologicalStack, marked, adjVertex);
topologicalStack.add(source);
}
}
static class FastReader {
private BufferedReader bfr;
private StringTokenizer st;
public FastReader() {
bfr = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
if (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(bfr.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());
}
char nextChar() {
return next().toCharArray()[0];
}
String nextString() {
return next();
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
double[] nextDoubleArray(int n) {
double[] arr = new double[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextDouble();
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
int[][] nextIntGrid(int n, int m) {
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
char[] line = fr.next().toCharArray();
for (int j = 0; j < m; j++)
grid[i][j] = line[j] - 48;
}
return grid;
}
}
static class LCA {
int[] height, first, segtree;
ArrayList<Integer> euler;
boolean[] visited;
int n;
LCA(ArrayList<Point>[] outFrom, int root) {
n = outFrom.length;
height = new int[n];
first = new int[n];
euler = new ArrayList<>();
visited = new boolean[n];
dfs(outFrom, root, 0);
int m = euler.size();
segtree = new int[m * 4];
build(1, 0, m - 1);
}
void dfs(ArrayList<Point>[] outFrom, int node, int h) {
visited[node] = true;
height[node] = h;
first[node] = euler.size();
euler.add(node);
for (Point to : outFrom[node]) {
if (!visited[(int) to.x]) {
dfs(outFrom, (int) to.x, h + 1);
euler.add(node);
}
}
}
void build(int node, int b, int e) {
if (b == e) {
segtree[node] = euler.get(b);
} else {
int mid = (b + e) / 2;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
int l = segtree[node << 1], r = segtree[node << 1 | 1];
segtree[node] = (height[l] < height[r]) ? l : r;
}
}
int query(int node, int b, int e, int L, int R) {
if (b > R || e < L)
return -1;
if (b >= L && e <= R)
return segtree[node];
int mid = (b + e) >> 1;
int left = query(node << 1, b, mid, L, R);
int right = query(node << 1 | 1, mid + 1, e, L, R);
if (left == -1) return right;
if (right == -1) return left;
return height[left] < height[right] ? left : right;
}
int lca(int u, int v) {
int left = first[u], right = first[v];
if (left > right) {
int temp = left;
left = right;
right = temp;
}
return query(1, 0, euler.size() - 1, left, right);
}
}
static class FenwickTree {
long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates
public FenwickTree(int size) {
array = new long[size + 1];
}
public long rsq(int ind) {
assert ind > 0;
long sum = 0;
while (ind > 0) {
sum += array[ind];
//Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number
ind -= ind & (-ind);
}
return sum;
}
public long rsq(int a, int b) {
assert b >= a && a > 0 && b > 0;
return rsq(b) - rsq(a - 1);
}
public void update(int ind, long value) {
assert ind > 0;
while (ind < array.length) {
array[ind] += value;
//Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number
ind += ind & (-ind);
}
}
public int size() {
return array.length - 1;
}
}
static final class RedBlackCountSet {
// Manual implementation of RB-BST for C++ PBDS functionality.
// Modification required according to requirements.
// Colors for Nodes:
private static final boolean RED = true;
private static final boolean BLACK = false;
// Pointer to the root:
private Node root;
// Public constructor:
public RedBlackCountSet() {
root = null;
}
// Node class for storing key value pairs:
private class Node {
long key;
long value;
Node left, right;
boolean color;
long size; // Size of the subtree rooted at that node.
public Node(long key, long value, boolean color) {
this.key = key;
this.value = value;
this.left = this.right = null;
this.color = color;
this.size = value;
}
}
/***** Invariant Maintenance functions: *****/
// When a temporary 4 node is formed:
private Node flipColors(Node node) {
node.left.color = node.right.color = BLACK;
node.color = RED;
return node;
}
// When there's a right leaning red link and it is not a 3 node:
private Node rotateLeft(Node node) {
Node rn = node.right;
node.right = rn.left;
rn.left = node;
rn.color = node.color;
node.color = RED;
return rn;
}
// When there are 2 red links in a row:
private Node rotateRight(Node node) {
Node ln = node.left;
node.left = ln.right;
ln.right = node;
ln.color = node.color;
node.color = RED;
return ln;
}
/***** Invariant Maintenance functions end *****/
// Public functions:
public void put(long key) {
root = put(root, key);
root.color = BLACK; // One of the invariants.
}
private Node put(Node node, long key) {
// Standard insertion procedure:
if (node == null)
return new Node(key, 1, RED);
// Recursing:
int cmp = Long.compare(key, node.key);
if (cmp < 0)
node.left = put(node.left, key);
else if (cmp > 0)
node.right = put(node.right, key);
else
node.value++;
// Invariant maintenance:
if (node.left != null && node.right != null) {
if (node.right.color = RED && node.left.color == BLACK)
node = rotateLeft(node);
if (node.left.color == RED && node.left.left.color == RED)
node = rotateRight(node);
if (node.left.color == RED && node.right.color == RED)
node = flipColors(node);
}
// Property maintenance:
node.size = node.value + size(node.left) + size(node.right);
return node;
}
private long size(Node node) {
if (node == null)
return 0;
else
return node.size;
}
public long rank(long key) {
return rank(root, key);
}
// Modify according to requirements.
private long rank(Node node, long key) {
if (node == null) return 0;
int cmp = Long.compare(key, node.key);
if (cmp < 0)
return rank(node.left, key);
else if (cmp > 0)
return node.value + size(node.left) + rank(node.right, key);
else
return node.value + size(node.left);
}
}
static class SegmentTree {
private Node[] heap;
private int[] array;
private int size;
public SegmentTree(int[] array) {
this.array = Arrays.copyOf(array, array.length);
//The max size of this array is about 2 * 2 ^ log2(n) + 1
size = (int) (2 * Math.pow(2.0, Math.floor((Math.log((double) array.length) / Math.log(2.0)) + 1)));
heap = new Node[size];
build(1, 0, array.length);
}
public int size() {
return array.length;
}
//Initialize the Nodes of the Segment tree
private void build(int v, int from, int size) {
heap[v] = new Node();
heap[v].from = from;
heap[v].to = from + size - 1;
if (size == 1) {
heap[v].sum = array[from];
heap[v].min = array[from];
} else {
//Build childs
build(2 * v, from, size / 2);
build(2 * v + 1, from + size / 2, size - size / 2);
heap[v].sum = heap[2 * v].sum + heap[2 * v + 1].sum;
//min = min of the children
heap[v].min = Math.min(heap[2 * v].min, heap[2 * v + 1].min);
}
}
public int rsq(int from, int to) {
return rsq(1, from, to);
}
private int rsq(int v, int from, int to) {
Node n = heap[v];
//If you did a range update that contained this node, you can infer the Sum without going down the tree
if (n.pendingVal != null && contains(n.from, n.to, from, to)) {
return (to - from + 1) * n.pendingVal;
}
if (contains(from, to, n.from, n.to)) {
return heap[v].sum;
}
if (intersects(from, to, n.from, n.to)) {
propagate(v);
int leftSum = rsq(2 * v, from, to);
int rightSum = rsq(2 * v + 1, from, to);
return leftSum + rightSum;
}
return 0;
}
public int rMinQ(int from, int to) {
return rMinQ(1, from, to);
}
private int rMinQ(int v, int from, int to) {
Node n = heap[v];
//If you did a range update that contained this node, you can infer the Min value without going down the tree
if (n.pendingVal != null && contains(n.from, n.to, from, to)) {
return n.pendingVal;
}
if (contains(from, to, n.from, n.to)) {
return heap[v].min;
}
if (intersects(from, to, n.from, n.to)) {
propagate(v);
int leftMin = rMinQ(2 * v, from, to);
int rightMin = rMinQ(2 * v + 1, from, to);
return Math.min(leftMin, rightMin);
}
return Integer.MAX_VALUE;
}
public void update(int from, int to, int value) {
update(1, from, to, value);
}
private void update(int v, int from, int to, int value) {
//The Node of the heap tree represents a range of the array with bounds: [n.from, n.to]
Node n = heap[v];
if (contains(from, to, n.from, n.to)) {
change(n, value);
}
if (n.size() == 1) return;
if (intersects(from, to, n.from, n.to)) {
propagate(v);
update(2 * v, from, to, value);
update(2 * v + 1, from, to, value);
n.sum = heap[2 * v].sum + heap[2 * v + 1].sum;
n.min = Math.min(heap[2 * v].min, heap[2 * v + 1].min);
}
}
//Propagate temporal values to children
private void propagate(int v) {
Node n = heap[v];
if (n.pendingVal != null) {
change(heap[2 * v], n.pendingVal);
change(heap[2 * v + 1], n.pendingVal);
n.pendingVal = null; //unset the pending propagation value
}
}
//Save the temporal values that will be propagated lazily
private void change(Node n, int value) {
n.pendingVal = value;
n.sum = n.size() * value;
n.min = value;
array[n.from] = value;
}
//Test if the range1 contains range2
private boolean contains(int from1, int to1, int from2, int to2) {
return from2 >= from1 && to2 <= to1;
}
//check inclusive intersection, test if range1[from1, to1] intersects range2[from2, to2]
private boolean intersects(int from1, int to1, int from2, int to2) {
return from1 <= from2 && to1 >= from2 // (.[..)..] or (.[...]..)
|| from1 >= from2 && from1 <= to2; // [.(..]..) or [..(..)..
}
//The Node class represents a partition range of the array.
static class Node {
int sum;
int min;
//Here We store the value that will be propagated lazily
Integer pendingVal = null;
int from;
int to;
int size() {
return to - from + 1;
}
}
}
@SuppressWarnings("serial")
static class CountMap<T> extends HashMap<T, Integer>{
CountMap() {
}
CountMap(T[] arr) {
this.putCM(arr);
}
public Integer putCM(T key) {
if (super.containsKey(key)) {
return super.put(key, super.get(key) + 1);
} else {
return super.put(key, 1);
}
}
public Integer removeCM(T key) {
Integer count = super.get(key);
if (count == null) return -1;
if (count == 1)
return super.remove(key);
else
return super.put(key, super.get(key) - 1);
}
public Integer getCM(T key) {
Integer count = super.get(key);
if (count == null)
return 0;
return count;
}
public void putCM(T[] arr) {
for (T l : arr)
this.putCM(l);
}
}
static class Point implements Comparable<Point> {
long x;
long y;
long id;
Point() {
x = y = id = 0;
}
Point(Point p) {
this.x = p.x;
this.y = p.y;
this.id = p.id;
}
Point(long a, long b, long id) {
this.x = a;
this.y = b;
this.id = id;
}
Point(long a, long b) {
this.x = a;
this.y = b;
}
@Override
public int compareTo(Point o) {
if (this.x < o.x)
return -1;
if (this.x > o.x)
return 1;
if (this.y < o.y)
return -1;
if (this.y > o.y)
return 1;
return 0;
}
public boolean equals(Point that) {
return this.compareTo(that) == 0;
}
}
static int longestPrefixPalindromeLen(char[] s) {
int n = s.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++)
sb.append(s[i]);
StringBuilder sb2 = new StringBuilder(sb);
sb.append('^');
sb.append(sb2.reverse());
// out.println(sb.toString());
char[] newS = sb.toString().toCharArray();
int m = newS.length;
int[] pi = piCalcKMP(newS);
return pi[m - 1];
}
static int KMPNumOcc(char[] text, char[] pat) {
int n = text.length;
int m = pat.length;
char[] patPlusText = new char[n + m + 1];
for (int i = 0; i < m; i++)
patPlusText[i] = pat[i];
patPlusText[m] = '^'; // Seperator
for (int i = 0; i < n; i++)
patPlusText[m + i] = text[i];
int[] fullPi = piCalcKMP(patPlusText);
int answer = 0;
for (int i = 0; i < n + m + 1; i++)
if (fullPi[i] == m)
answer++;
return answer;
}
static int[] piCalcKMP(char[] s) {
int n = s.length;
int[] pi = new int[n];
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
static String toBinaryString(long num, int bits) {
StringBuilder sb = new StringBuilder(Long.toBinaryString(num));
sb.reverse();
for (int i = sb.length(); i < bits; i++)
sb.append('0');
return sb.reverse().toString();
}
static long modDiv(long a, long b) {
return mod(a * power(b, gigamod - 2, gigamod));
}
static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) {
if (m > limit) return;
if (m <= limit && n <= limit)
coprimes.add(new Point(m, n));
if (coprimes.size() > numCoprimes) return;
coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes);
coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes);
coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes);
}
static long hash(long i) {
return (i * 2654435761L % gigamod);
}
static long hash2(long key) {
long h = Long.hashCode(key);
h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4);
return h & (gigamod-1);
}
static int mapTo1D(int row, int col, int n, int m) {
// Maps elements in a 2D matrix serially to elements in
// a 1D array.
return row * m + col;
}
static int[] mapTo2D(int idx, int n, int m) {
// Inverse of what the one above does.
int[] rnc = new int[2];
rnc[0] = idx / m;
rnc[1] = idx % m;
return rnc;
}
static double distance(Point p1, Point p2) {
return Math.sqrt(Math.pow(p2.y-p1.y, 2) + Math.pow(p2.x-p1.x, 2));
}
static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) {
if (smallestFactorOf == null)
primeGenerator(200001);
CountMap<Integer> fnps = new CountMap<>();
while (num != 1) {
fnps.putCM(smallestFactorOf[num]);
num /= smallestFactorOf[num];
}
return fnps;
}
static HashMap<Long, Integer> primeFactorization(long num) {
// Returns map of factor and its power in the number.
HashMap<Long, Integer> map = new HashMap<>();
while (num % 2 == 0) {
num /= 2;
Integer pwrCnt = map.get(2L);
map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1);
}
for (long i = 3; i * i <= num; i += 2) {
while (num % i == 0) {
num /= i;
Integer pwrCnt = map.get(i);
map.put(i, pwrCnt != null ? pwrCnt + 1 : 1);
}
}
// If the number is prime, we have to add it to the
// map.
if (num != 1)
map.put(num, 1);
return map;
}
static HashSet<Long> divisors(long num) {
HashSet<Long> divisors = new HashSet<Long>();
divisors.add(1L);
divisors.add(num);
for (long i = 2; i * i <= num; i++) {
if (num % i == 0) {
divisors.add(num/i);
divisors.add(i);
}
}
return divisors;
}
static int bsearch(int[] arr, int val, int lo, int hi) {
// Returns the index of the first element
// larger than or equal to val.
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
idx = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
return idx;
}
static int bsearch(long[] arr, long val, int lo, int hi) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
idx = mid;
hi = mid - 1;
} else
lo = mid + 1;
}
return idx;
}
static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) {
// Returns the index of the last element
// smaller than or equal to val.
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] >= val) {
hi = mid - 1;
} else {
idx = mid;
lo = mid + 1;
}
}
return idx;
}
static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) {
int idx = -1;
while (lo <= hi) {
int mid = lo + (hi - lo)/2;
if (arr[mid] > val) {
hi = mid - 1;
} else {
idx = mid;
lo = mid + 1;
}
}
return idx;
}
static long factorial(long n) {
if (n <= 1)
return 1;
long factorial = 1;
for (int i = 1; i <= n; i++)
factorial = mod(factorial * i);
return factorial;
}
static long factorialInDivision(long a, long b) {
if (a == b)
return 1;
if (b < a) {
long temp = a;
a = b;
b = temp;
}
long factorial = 1;
for (long i = a + 1; i <= b; i++)
factorial = mod(factorial * i);
return factorial;
}
static long nCr(long n, long r, long[] fac) {
long p = 998244353;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
/*long fac[] = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;*/
return (fac[(int)n] * modInverse(fac[(int)r], p) % p
* modInverse(fac[(int)n - (int)r], p) % p) % p;
}
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
static long power(long x, long y, long p) {
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if ((y & 1)==1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long nPr(long n, long r) {
return factorialInDivision(n, n - r);
}
static int logk(long n, long k) {
return (int)(Math.log(n) / Math.log(k));
}
static boolean[] primeGenerator(int upto) {
// Sieve of Eratosthenes:
isPrime = new boolean[upto + 1];
smallestFactorOf = new int[upto + 1];
Arrays.fill(smallestFactorOf, 1);
Arrays.fill(isPrime, true);
isPrime[1] = isPrime[0] = false;
for (long i = 2; i < upto + 1; i++)
if (isPrime[(int) i]) {
smallestFactorOf[(int) i] = (int) i;
// Mark all the multiples greater than or equal
// to the square of i to be false.
for (long j = i; j * i < upto + 1; j++) {
if (isPrime[(int) j * (int) i]) {
isPrime[(int) j * (int) i] = false;
smallestFactorOf[(int) j * (int) i] = (int) i;
}
}
}
return isPrime;
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
static long gcd(long[] arr) {
int n = arr.length;
long gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = gcd(gcd, arr[i]);
}
return gcd;
}
static int gcd(int[] arr) {
int n = arr.length;
int gcd = arr[0];
for (int i = 1; i < n; i++) {
gcd = gcd(gcd, arr[i]);
}
return gcd;
}
static long lcm(long[] arr) {
long lcm = arr[0];
int n = arr.length;
for (int i = 1; i < n; i++) {
lcm = (lcm * arr[i]) / gcd(lcm, arr[i]);
}
return lcm;
}
static long lcm(long a, long b) {
return (a * b)/gcd(a, b);
}
static boolean less(int a, int b) {
return a < b ? true : false;
}
static boolean isSorted(int[] a) {
for (int i = 1; i < a.length; i++) {
if (less(a[i], a[i - 1]))
return false;
}
return true;
}
static boolean isSorted(long[] a) {
for (int i = 1; i < a.length; i++) {
if (a[i] < a[i - 1])
return false;
}
return true;
}
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(long[] a, int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(double[] a, int i, int j) {
double temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void sort(int[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(char[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(long[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void sort(double[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
}
static void reverseSort(int[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(char[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverse(char[] arr) {
int n = arr.length;
for (int i = 0; i < n / 2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(long[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void reverseSort(double[] arr) {
shuffleArray(arr, 0, arr.length - 1);
Arrays.sort(arr);
int n = arr.length;
for (int i = 0; i < n/2; i++)
swap(arr, i, n - 1 - i);
}
static void shuffleArray(long[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(int[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(double[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
double tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
static void shuffleArray(char[] arr, int startPos, int endPos) {
Random rnd = new Random();
for (int i = startPos; i < endPos; ++i) {
char tmp = arr[i];
int randomPos = i + rnd.nextInt(endPos - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
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 String toString(int[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(boolean[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(long[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + " ");
return sb.toString();
}
static String toString(char[] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++)
sb.append(dp[i] + "");
return sb.toString();
}
static String toString(int[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(long[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(double[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static String toString(char[][] dp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
sb.append(dp[i][j] + " ");
}
sb.append('\n');
}
return sb.toString();
}
static long mod(long a, long m) {
return (a%m + m) % m;
}
static long mod(long num) {
return (num % gigamod + gigamod) % gigamod;
}
}
// NOTES:
// ASCII VALUE OF 'A': 65
// ASCII VALUE OF 'a': 97
// Range of long: 9 * 10^18
// ASCII VALUE OF '0': 48
// Primes upto 'n' can be given by (n / (logn)). | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | bec195ad4ca1c06c39209d33a5ac66c3 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.*;
import java.util.*;
/*
*/
public class D{
static Scanner sc=null;
static int Q;
public static void main(String[] args) {
sc=new Scanner(System.in);
int t=sc.nextInt();
for(int tt=0;tt<t;tt++) {
int n=sc.nextInt();
int a=1,b=2;
int cur=0,c=0;
Q=0;
for(int cc=3;cc<=n;cc++) {
int val=Query(a,b,cc);
if(val>cur) {
cur=val;
c=cc;
}
}
for(int bb=b+1;bb<=n;bb++) {
if(bb==c)continue;
int val=Query(a,bb,c);
if(val>cur) {
cur=val;
b=bb;
}
}
int d=1;
while(d==a || d==b || d==c)d++;
if(d>n)throw null;
ArrayList<Integer> ans=new ArrayList<>();
int o1=Query(a,b,d);
if(o1<cur)ans.add(c);
int o2=Query(a,d,c);
if(o2<cur)ans.add(b);
if(ans.size()<2)ans.add(a);
if(Q>2*n-2)throw null;
if(ans.size()<2)ans.add(ans.get(0));
System.out.print("! ");
for(int e:ans)System.out.print(e+" ");
System.out.println();
}
}
static int Query(int i,int j,int k) {
Q++;
if(i==j || j==k || i==k) throw null;
System.out.println("? "+i+" "+j+" "+k);
int v=sc.nextInt();
if(v==-1)throw null;
return v;
}
static int[] ruffleSort(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
return a;
}
static void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 1654ff6bd225eb8b9258a1eb3385eb35 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.math.*;
public class CodeChef2 {
static boolean lazy[];
static int lazyupd[];
public static void main(String args[]) throws Exception {
FastReader fr = new FastReader();
int t = fr.nextInt();
PrintWriter out = new PrintWriter(System.out);
while (t-- > 0) {
int n = fr.nextInt();
int a1 = 1;
int a2 = 2;
Pair p[] = new Pair[n-2];
for(int i = 3; i <= n; i++) {
System.out.println("? " + a1 + " " + a2 + " " + i);
System.out.flush();
int diff = fr.nextInt();
if(diff == -1) {
throw new Exception();
}
p[i-3] = new Pair(i, diff);
}
Arrays.sort(p);
int max = 0;
int mi = 2;
int mini = 2;
int min = Integer.MAX_VALUE;
a1 = 1;
a2 = p[p.length-1].i;
for(int i = 2; i <= n; i++) {
if(i == a2)
continue;
System.out.println("? " + a1 + " " + a2 + " " + i);
System.out.flush();
int diff = fr.nextInt();
if(diff == -1) {
throw new Exception();
}
if(diff > max) {
max = diff;
mi = i;
}
if(diff < min) {
min = diff;
mini = i;
}
}
if(min == max) {
System.out.println("! " + a1 + " " + a2);
} else if(mi == 2 && p[p.length-1].v == p[0].v){
System.out.println("! " + 1 + " " + 2);
} else {
System.out.println("! " + mi + " " + a2);
}
}
out.close();
}
static class Pair implements Comparable<Pair> {
int v;
int i;
Pair(int i, int v) {
this.i = i;
this.v = v;
}
public int compareTo(Pair o) {
if(this.v < o.v)
return -1;
else if(this.v > o.v)
return 1;
else if(this.i < o.i)
return -1;
else return 1;
}
public boolean compare(Pair o) {
return this.compareTo(o) == -1;
}
public String toString() {
return v + " " + i;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 15f819d8c3ed74f4a271c3f34f26e14f | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
//--------------------------INPUT READER---------------------------------//
static class fs {
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public fs() { this(System.in); }
public fs(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine()); }
catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long nn) {
int n = (int) nn;
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] nal(long nn) {
int n = (int) nn;
long[] l = new long[n];
for(int i = 0; i < n; i++) l[i] = nl();
return l;
}
}
//-----------------------------------------------------------------------//
//---------------------------PRINTER-------------------------------------//
static class Printer {
static PrintWriter w;
public Printer() {this(System.out);}
public Printer(OutputStream os) {
w = new PrintWriter(os, true);
}
public void p(int i) {w.println(i);}
public void p(long l) {w.println(l);}
public void p(double d) {w.println(d);}
public void p(String s) { w.println(s);}
public void pr(int i) {w.print(i);}
public void pr(long l) {w.print(l);}
public void pr(double d) {w.print(d);}
public void pr(String s) { w.print(s);}
public void pl() {w.println();}
public void close() {w.close();}
}
//-----------------------------------------------------------------------//
//--------------------------VARIABLES------------------------------------//
static fs sc = new fs();
static OutputStream outputStream = System.out;
static Printer w = new Printer(outputStream);
static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
static long mod = 1000000007;
//-----------------------------------------------------------------------//
//--------------------------ADMIN_MODE-----------------------------------//
private static void ADMIN_MODE() throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
w = new Printer(new FileOutputStream("output.txt"));
sc = new fs(new FileInputStream("input.txt"));
}
}
//-----------------------------------------------------------------------//
//----------------------------START--------------------------------------//
public static void main(String[] args)
throws IOException {
// ADMIN_MODE();
int t = sc.ni();while(t-->0)
solve();
w.close();
}
static void solve() throws IOException {
int n = sc.ni();
long max = imi;
long a = -1, b = -1;
HashMap<pr, List<Integer>> hm = new HashMap<>();
for(int i = 1; i <= 4; i++) {
for(int j = i+1; j <= 4; j++) {
for(int k = j+1; k <= 4; k++) {
w.p("? "+i+" "+j+" "+k);
int ans = sc.ni();
List<Integer> li = hm.getOrDefault(new pr(i, j), new ArrayList<>());
li.add(ans);
hm.put(new pr(i, j), li);
li = hm.getOrDefault(new pr(j, k), new ArrayList<>());
li.add(ans);
hm.put(new pr(j, k), li);
li = hm.getOrDefault(new pr(i, k), new ArrayList<>());
li.add(ans);
hm.put(new pr(i, k), li);
}
}
}
for(int i = 1; i <= 4; i++) {
for(int j = i+1; j <= 4; j++) {
List<Integer> li = hm.get(new pr(i, j));
if(!li.get(0).equals(li.get(1))) continue;
if(li.get(0) > max) {
max = li.get(0);
a = i;
b = j;
}
}
}
int out = -1;
for(int i = 1; true; i++) {
if(a!=i && b!=i) {
out = i;
break;
}
}
for(int i = 5; i <= n; i++) {
w.p("? "+a+" "+i+" "+out);
int ans1 = sc.ni();
w.p("? "+b+" "+i+" "+out);
int ans2 = sc.ni();
if(ans1 >= ans2 && ans1 > max) {
max = ans1;
b = i;
} else if(ans2 > ans1 && ans2 > max) {
max = ans2;
a = i;
}
}
w.p("! "+a+" "+b);
}
static class pr {
int a, b;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof pr)) return false;
pr pr = (pr) o;
return a == pr.a && b == pr.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
public pr(int a, int b) {
this.a = a;
this.b = b;
}
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 6301b7133c066c5d0ce9304418f81056 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static HashMap<String, Integer> q = new HashMap<>();
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws Exception {
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while (t --> 0) {
int len = Integer.parseInt(br.readLine());
Stack<Integer> s = new Stack<>();
for (int i = 1; i <= len; i++) s.push(i);
int a = 0;
int notZero = 0;
while (s.size() > 2) {
int w = s.pop();
int x = s.pop();
int y = s.pop();
int z = -1;
if (s.size() >= 1) z = s.pop();
else z = notZero;
int[] temp = check(w, x, y, z);
s.push(temp[0]);
s.push(temp[1]);
if (notZero == 0) {
if (w != temp[0] && w != temp[1]) {
notZero = w;
} else if (x != temp[0] && x != temp[1]) {
notZero = x;
} else if (y != temp[0] && y != temp[1]) {
notZero = y;
} else if (z != temp[0] && z != temp[1]) {
notZero = z;
}
}
}
q = new HashMap<String, Integer>();
pw.println("! " + s.pop() + " " + s.pop());
pw.flush();
}
pw.close();
}
static int[] check(int a, int b, int c, int d) throws Exception {
int notA = query(b, c, d);
int notB = query(a, c, d);
int notC = query(a, b, d);
int notD = query(a, b, c);
int[] ans = new int[2];
if (notA >= notC && notA >= notD && notB >= notC && notB >= notD) { // A and B
ans[0] = c;
ans[1] = d;
} else if (notA >= notB && notA >= notD && notC >= notB && notC >= notD) { // A and C
ans[0] = b;
ans[1] = d;
} else if (notA >= notC && notA >= notB && notD >= notC && notD >= notB) { // A and D
ans[0] = b;
ans[1] = c;
} else if (notC >= notA && notC >= notD && notB >= notA && notB >= notD) { // B and C
ans[0] = a;
ans[1] = d;
} else if (notD >= notC && notD >= notA && notB >= notC && notB >= notA) { // B and D
ans[0] = a;
ans[1] = c;
} else if (notC >= notA && notC >= notB && notD >= notA && notD >= notB) { // C and D
ans[0] = a;
ans[1] = b;
}
return ans;
}
static int query(int x, int y, int z) throws Exception {
int[] a = {x, y, z};
Arrays.sort(a);
if (q.containsKey("? " + a[0] + " " + a[1] + " " + a[2])) return q.get("? " + a[0] + " " + a[1] + " " + a[2]);
pw.println("? " + a[0] + " " + a[1] + " " + a[2]);
pw.flush();
int b = Integer.parseInt(br.readLine());
q.put("? " + a[0] + " " + a[1] + " " + a[2], b);
return b;
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 430f7a18be17bb4a5b2675f187ee996c | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=input.nextInt();
while(T-->0)
{
int n=input.nextInt();
int max=Integer.MIN_VALUE;
int x=0;
for(int i=3;i<=n;i++)
{
out.println("? "+1+" "+2+" "+i);
out.flush();
int v=input.nextInt();
if(v>max)
{
max=v;
x=i;
}
}
int y=0;
max=Integer.MIN_VALUE;
for(int i=n;i>=2;i--)
{
if(i!=x)
{
out.println("? "+1+" "+x+" "+i);
out.flush();
int v=input.nextInt();
if(v>=max)
{
max=v;
y=i;
}
}
}
HashSet<Integer> set=new HashSet<>();
set.add(1);set.add(2);set.add(x);set.add(y);
if(set.size()==4)
{
out.println("! "+x+" "+y);
out.flush();
}
else
{
ArrayList<Integer> list=new ArrayList<>(set);
for(int i=1;i<=n;i++)
{
if(!set.contains(i))
{
list.add(i);
break;
}
}
int a=list.get(0),b=list.get(1),c=list.get(2),d=list.get(3);
out.println("? "+a+" "+b+" "+d);
out.flush();
int v1=input.nextInt();
if(v1==max)
{
out.println("! "+a+" "+b);
out.flush();
}
else
{
out.println("? "+b+" "+c+" "+d);
out.flush();
int v2=input.nextInt();
if(v2==max)
{
out.println("! "+b+" "+c);
out.flush();
}
else
{
out.println("! "+a+" "+c);
out.flush();
}
}
}
}
out.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 1a243264b14e201653eb5b5c6be6a47f | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.FileInputStream;
import java.util.List;
import java.util.Random;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Main {
InputStream is;
int __t__ = 1;
int __f__ = 0;
int __FILE_DEBUG_FLAG__ = __f__;
static String __DEBUG_FILE_NAME__ = "src/main/java/test/sample-1.in";
FastScanner in;
PrintWriter out;
boolean TEST = false;
int qLimit = 0;
int[] hidden;
Random rnd = new Random(125);
public void solve() {
int[][] _ps = {
{0, 100, 25, 50, 75},
{1, 2, 0, 3},
{0, 1, 2, 3, 4, 5},
{100, 0, 200, 300},
{1, 1, 1, 1, 1, 0}
};
List<int[]> ps = new ArrayList<>(Arrays.asList(_ps));
for (int j = 4; j < 50; j++) {
for (int i = 0; i < 1000; i++) {
ps.add(generateRandom(j));
}
}
int T;
if (TEST) {
T = ps.size();
} else {
T = in.nextInt();
}
for (int _times = 0; _times < T; _times++) {
int n;
if (TEST) {
n = ps.get(_times).length;
} else {
n = in.nextInt();
}
qLimit = 2 * (n - 1);
if (TEST) {
hidden = ps.get(_times);
System.out.println(Arrays.toString(hidden));
}
int lAns = -1, rAns = -1;
int[] v = new int[n];
for (int i = 2; i < n; i++) {
v[i] = query(0, 1, i);
}
lAns = 2;
for (int i = 2; i < n; i++) {
if (v[lAns] < v[i]) {
lAns = i;
}
}
int[] v2 = new int[n];
rAns = lAns == 2 ? 3 : 2;
for (int i = 2; i < n; i++) {
if (i == lAns) continue;
v2[i] = query(i, 0, lAns);
if (v2[rAns] < v2[i]) {
rAns = i;
}
}
if (lAns != 1 && rAns != 1) {
int z = query(lAns, rAns, 1);
if (v[2] > Math.max(z, v2[rAns])) {
// System.out.println(Arrays.toString(v2));
// System.out.println(v[2] + " " + z + " " + v2[rAns]);
lAns = 0;
rAns = 1;
} else if (z == v2[rAns]) {
// ok
} else if (z < v2[rAns]) {
rAns = 0;
} else {
rAns = 1;
}
}
spendQueryAll();
guess(lAns, rAns);
}
}
private void guess(int lAns, int rAns) {
if (qLimit != 0) {
throw new RuntimeException("?");
}
System.out.println("! " + (lAns + 1) + " " + (rAns + 1));
if (TEST) {
if (hidden[lAns] == 0) {
System.out.println("Correct lAns! " + lAns);
} else if (hidden[rAns] == 0) {
System.out.println("Correct rAns! " + rAns);
} else {
throw new RuntimeException("WA");
}
}
}
private void spendQueryAll() {
while (qLimit > 0) {
query(0, 1, 2);
}
}
private int query(int i, int j, int k) {
if (i == j || j == k || k == i) {
throw new RuntimeException("same");
}
if (qLimit == 0) {
throw new RuntimeException("QLE");
}
System.out.println("? " + (i + 1) + " " + (j + 1) + " " + (k + 1));
qLimit--;
if (TEST) {
int x = hidden[i];
int y = hidden[j];
int z = hidden[k];
System.out.println("=> " + (Math.max(Math.max(x, y), z) - Math.min(Math.min(x, y), z)));
return Math.max(Math.max(x, y), z) - Math.min(Math.min(x, y), z);
} else {
return in.nextInt();
}
}
private int[] generateRandom(int n) {
hidden = new int[n];
for (int i = 0; i < n; i++) {
hidden[i] = rnd.nextInt(3) + 1;
}
int u = rnd.nextInt(n);
hidden[u] = 0;
return hidden;
}
public void run() {
if (__FILE_DEBUG_FLAG__ == __t__) {
try {
is = new FileInputStream(__DEBUG_FILE_NAME__);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("FILE_INPUT!");
} else {
is = System.in;
}
in = new FastScanner(is);
out = new PrintWriter(System.out);
solve();
}
public static void main(final String[] args) {
new Main().run();
}
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
// stream = new FileInputStream(new File("dec.in"));
}
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 boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
public int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
map[i] = nextIntArray(m);
}
return map;
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
public long[][] nextLongMap(int n, int m) {
long[][] map = new long[n][m];
for (int i = 0; i < n; i++) {
map[i] = nextLongArray(m);
}
return map;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
public double[][] nextDoubleMap(int n, int m) {
double[][] map = new double[n][m];
for (int i = 0; i < n; i++) {
map[i] = nextDoubleArray(m);
}
return map;
}
public String next() {
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[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
public int[][] nextPackedIntArrays(int packN, int size) {
int[][] res = new int[packN][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < packN; j++) {
res[j][i] = nextInt();
}
}
return res;
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 32cb8f2c56b29281a7efaef1af973c8d | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.*;
import java.util.*;
/*
polyakoff
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int oo = (int) 2e9 + 10;
static final long OO = (long) 2e18 + 10;
static final int MOD = 998244353;
static int n;
static class Answer {
int i1, i2, max1, max2;
boolean allEq;
Answer(int i1, int i2, int max1, int max2, boolean allEq) {
this.i1 = i1;
this.i2 = i2;
this.max1 = max1;
this.max2 = max2;
this.allEq = allEq;
}
}
static int ask(int i, int j, int k) {
out.println("? " + i + " " + j + " " + k);
out.flush();
return in.nextInt();
}
static Answer check(int not1, int not2) {
int i1 = -1, i2 = -1, max1 = -1, max2 = -1;
int min = oo;
for (int i = 1; i <= n; i++) {
if (i != not1 && i != not2) {
int asked = ask(not1, not2, i);
if (max2 == -1 || asked > max2) {
if (max1 == -1 || asked >= max1) {
i2 = i1;
max2 = max1;
i1 = i;
max1 = asked;
} else {
i2 = i;
max2 = asked;
}
}
min = Math.min(min, asked);
}
}
return new Answer(i1, i2, max1, max2, max1 == min);
}
static void solve() {
n = in.nextInt();
Answer a1 = check(1, 2);
Answer a2 = check(a1.i1, a1.i2);
if (a1.allEq && a2.max1 < a1.max1)
out.println("! 1 2");
else if (a2.allEq)
out.println("! " + a1.i1 + " " + a1.i2);
else
out.println("! " + a1.i1 + " " + a2.i1);
out.flush();
}
public static void main(String[] args) {
in = new FastReader();
out = new PrintWriter(System.out);
int t = 1;
t = in.nextInt();
while (t-- > 0) {
solve();
}
out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
this(System.in);
}
FastReader(String file) throws FileNotFoundException {
this(new FileInputStream(file));
}
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String line;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | cc21ea2ce5432209829da430499c34ad | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
public static void main(String[]args) throws IOException {
new Main().run();
}
File input=new File("D:\\test\\input.txt");
void run() throws IOException{
// new solve().setIO(input, System.out).run();
new solve().setIO(System.in,System.out).run();
}
class solve extends ioTask{
//1 1 0 1
int i,j,w,t,n,q,m,k;
int q(int a,int b,int c) throws IOException {
out.println("? "+a+" "+b+" "+c);
out.flush();
return in.in();
}
int tmp;
void run() throws IOException {
t=in.in();
while(t-->0)
{
n=in.in();
int a=1,b=2,c=3;
int num=q(a,b,c);
for(i=4;i<=n;i++) {
tmp=q(a,b,i);
if(tmp>num) {
num=tmp;
c=i;
}
}
for(i=3;i<=n;i++)
{
if(i==c)continue;
tmp=q(a,i,c);
if(tmp>num)
{
num=tmp;
b=i;
}
}
int p=0;
for(i=1;i<=4;i++)
{
if(i!=a&&i!=b&&i!=c) {
p=i;
break;
}
}
int x=q(p,b,c);
int y=q(a,p,c);
if(x!=num&&y!=num)out.println("! "+a+" "+b);
else if(x!=num) {
out.println("! "+a+" "+c);
}else {
out.println("! "+b+" "+c);
}
out.flush();
}
out.close();
}
}
class In{
private StringTokenizer in=new StringTokenizer("");
private InputStream is;
private BufferedReader bf;
public In(File file) throws IOException {
is=new FileInputStream(file);
init();
}
public In(InputStream is) throws IOException
{
this.is=is;
init();
}
private void init() throws IOException {
bf=new BufferedReader(new InputStreamReader(is));
}
boolean hasNext() throws IOException {
return in.hasMoreTokens()||bf.ready();
}
String ins() throws IOException {
while(!in.hasMoreTokens()) {
in=new StringTokenizer(bf.readLine());
}
return in.nextToken();
}
int in() throws IOException {
return Integer.parseInt(ins());
}
long inl() throws IOException {
return Long.parseLong(ins());
}
double ind() throws IOException {
return Double.parseDouble(ins());
}
String line() throws IOException {
return bf.readLine();
}
}
class Out{
PrintWriter out;
private OutputStream os;
private void init() {
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
}
public Out(File file) throws IOException {
os=new FileOutputStream(file);
init();
}
public Out(OutputStream os) throws IOException
{
this.os=os;
init();
}
}
class graph{
int[]to,nxt,head,w;
int cnt;
void init(int n) {
cnt=1;
for(int i=1;i<=n;i++)
{
head[i]=0;
}
}
public graph(int n,int m) {
to=new int[m+1];
nxt=new int[m+1];
head=new int[n+1];
w=new int[m+1];
cnt=1;
}
void add(int u,int v,int l) {
to[cnt]=v;
nxt[cnt]=head[u];
w[cnt]=l;
head[u]=cnt++;
}
}
abstract class ioTask{
In in;
PrintWriter out;
public ioTask setIO(File in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(File in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
void run()throws IOException{
}
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 0796c405f574a40b22e6a508a59126e0 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes |
import java.util.*;
import java.io.*;
public class D {
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 {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static int query(int i,int j,int k){
if(debug){
queries++;
if(queries > input.length * 2 - 2) throw new RuntimeException("query exceed");
int max = Math.max(input[i], Math.max(input[j],input[k])),min = Math.min(input[i],Math.min(input[j],input[k]));
return max - min;
}else {
output.println("? " + i + " " + j + " " + k);
output.flush();
return scanner.nextInt();
}
}
static boolean debug = false;
static int[] input;
static int queries = 0;
static FastReader scanner = new FastReader();
static PrintWriter output = new PrintWriter(System.out);
private static void runWithStd() {
debug = false;
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
int[] ret = solve(n);
StringBuilder stringBuilder = new StringBuilder("! ");
for(long r : ret) stringBuilder.append(r + " ");
output.println(stringBuilder);
output.flush();
}
output.close();
}
private static int[] solve(int n) {
int max = 0,maxId = -1,maxCnt = 0;
for (int i = 3; i <= n; i++) {
int qr = query(1,2,i);
if(qr > max){
max = qr;
maxId = i;
maxCnt = 1;
}else if(qr == max) maxCnt++;
}
if(maxCnt < n - 2){
//System.out.println("finding one = " + maxId);
int smax = 0,smaxId = -1,smaxCnt = 0;
for (int i = 2; i <= n; i++) {
if(i == maxId) continue;
int qr = query(1,maxId,i);
if(qr > smax){
smax = qr;
smaxId = i;
smaxCnt = 1;
}else if(smax == qr){
smaxCnt++;
}
}
if(smaxCnt == n - 2) return new int[]{1,maxId};
return new int[]{maxId,smaxId};
}else{
int smax = 0,smaxId = -1,smaxCnt = 0;
for (int i = 1; i <= n; i++) {
if(i == 3 || i == 4) continue;
int qr = query(3,4,i);
if(qr > smax){
smax = qr;
smaxId = i;
smaxCnt = 1;
}else if(qr == smax){
smaxCnt++;
}
}
if(smax < max) return new int[]{1,2};
if(smaxCnt == n-2) return new int[]{3,4};
return new int[]{smaxId,smaxId == 1 ? 2 : 1};
}
}
private static void runWithDebug() {
Random random = new Random();
int t = 1000000;
debug = true;
input = new int[]{0, 0, 2, 5, 9, 7, 6, 8, 3, 2, 9};
queries = 0;
System.out.println(Arrays.toString(solve(10)));
while (t-- > 0) {
input = new int[5];
queries = 0;
for (int i = 1; i < input.length; i++) {
input[i] = random.nextInt(10) + 1;
}
int id = random.nextInt(input.length - 1) + 1;
input[id] = 0;
long ts = System.currentTimeMillis();
int[] ret = solve(input.length - 1);
if(input[ret[0]] != 0 && input[ret[1]] != 0){
System.out.println("wrong answer input = " + Arrays.toString(input) + " ret = " + Arrays.toString(ret));
return;
}
long delta = System.currentTimeMillis() - ts;
System.out.println("case t = " + t + " time = " + delta);
}
System.out.println("all passed");
}
public static void main(String[] args) {
runWithStd();
//runWithDebug();
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 713e1290e0308a084922866666a67843 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes |
import java.util.*;
import java.io.*;
/**
* CF1634D Finding Zero
*/
public class Main {
static Reader rd = new Reader();
public static void main(String[] args) {
int tt = rd.nextInt();
while (tt-- > 0) {
new Solution().solve();
}
}
static class Solution {
void solve() {
int n = rd.nextInt();
int[] diff = new int[n + 1];
for (int i = 3; i <= n; i++) {
diff[i] = ask(1, 2, i);
}
int mx = -1, mxid = -1;
for (int i = 3; i <= n; i++) {
if (diff[i] > mx) {
mx = diff[i];
mxid = i;
}
}
int k = mxid;
diff[2] = mx;
for (int i = 3; i <= n; i++) {
if (i == k) continue;
diff[i] = ask(1, k, i);
}
mx = -1;
mxid = -1;
for (int i = 2; i <= n; i++) {
if (i == k) continue;
if (diff[i] > mx) {
mx = diff[i];
mxid = i;
}
}
int j = mxid;
int p = -1;
for (int i = 2; i <= n; i++) {
if (i != k && i != j) {
p = i;
break;
}
}
// Utils.printf("k=%d j=%d p=%d\n", k, j, p);
int na = ask(k, j, p);
int nb = ask(1, j, p);
int nc = ask(1, k, p);
if (na >= nb) {
if (na >= nc) {
// max == na
System.out.printf("! %d %d\n", k, j);
} else {
// max == nc
System.out.printf("! %d %d\n", 1, k);
}
} else {
if (nb >= nc) {
// max == nb
System.out.printf("! %d %d\n", 1, j);
} else {
// max == nc
System.out.printf("! %d %d\n", 1, k);
}
}
}
int ask(int i, int j, int k) {
System.out.printf("? %d %d %d\n", i, j, k);
System.out.flush();
return rd.nextInt();
}
}
static class Reader {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String nextLine = innerNextLine();
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public String nextLine() {
tokenizer = new StringTokenizer("");
return innerNextLine();
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.valueOf(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | c554e2da8af0117a1992e050a7cf1d88 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | /*
* Everything is Hard
* Before Easy
* Jai Mata Dii
*/
import java.util.*;
import java.io.*;
public class Main {
static class FastReader{ BufferedReader br;StringTokenizer st;public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException e){e.printStackTrace();}}return st.nextToken();}int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; }}
static long mod = (long)(1e9+7);
// static long mod = 998244353;
// static Scanner sc = new Scanner(System.in);
static FastReader sc = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args) {
int ttt = 1;
ttt = sc.nextInt();
z :for(int tc=1;tc<=ttt;tc++){
int n = sc.nextInt();
int x1 = 2, y1 = 3, x2 = 3, y2 = 4;
int x = -1, y = -1;
if(query(x1, y1, 1) <= query(x2, y2, 1)) {
x = x1;
y = y1;
}
else {
x = x2;
y = y2;
}
int c = -1, max = -1;
for(int i=1;i<=n;i++) {
if(i == x || i == y) {
continue;
}
int cur = query(x, y, i);
if(cur > max) {
max = cur;
c = i;
}
}
HashSet<Integer> hs = new HashSet<>();
int d = -1;
max = -1;
for(int i=1;i<=n;i++) {
if(i == x || i == c) {
continue;
}
int cur = query(x, c, i);
hs.add(cur);
if(cur > max) {
max = cur;
d = i;
}
}
if(hs.size() == 1) {
System.out.println("! "+x+" "+c);
}
else {
System.out.println("! "+d+" "+c);
}
}
out.close();
}
private static int query(int i, int j, int k) {
System.out.println("? "+i+" "+j+" "+k);
int ret = sc.nextInt();
System.out.flush();
return ret;
}
static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;}
static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b); }
private static void sort(int[] a) {List<Integer> k = new ArrayList<>();for(int val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);}
private static void ini(List<Integer>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}}
private static void init(List<int[]>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}}
private static void sort(long[] a) {List<Long> k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | cad5c2461fb2a090a7318c4c0271c427 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
//import java.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static int mod = 1000000007;
public static void main(String[] args) throws Exception {
PrintWriter out=new PrintWriter(System.out);
FastScanner fs=new FastScanner();
int t=fs.nextInt();
while(t-->0) {
int n=fs.nextInt();
int arr[]=new int[4];
arr[0]=1;arr[1]=2;
int temp=-1;
int ans[]=new int[2];
ans[0]=1; ans[1]=2;
for(int i=4;i<=n+1;i+=2) {
arr[0]=ans[0]; arr[1]=ans[1];
if(i>n) {
arr[2]=temp;
arr[3]=i-1;
}
else {
arr[2]=i-1;
arr[3]=i;
}
System.out.println("? "+ arr[0]+" "+arr[1]+" "+arr[2]);
int a,b,c,d;
int max=0;
a=fs.nextInt();
System.out.println("? "+ arr[0]+" "+arr[1]+" "+arr[3]);
b=fs.nextInt();
System.out.println("? "+ arr[0]+" "+arr[2]+" "+arr[3]);
c=fs.nextInt();
System.out.println("? "+ arr[1]+" "+arr[2]+" "+arr[3]);
d=fs.nextInt();
max=Math.max(Math.max(a, b),Math.max(c, d));
if(a==max&&b==max) {
ans[0]=arr[0]; ans[1]=arr[1];
temp=arr[2];
}
else if(a==max&&c==max) {
ans[0]=arr[0]; ans[1]=arr[2];
temp=arr[1];
}
else if(a==max&&d==max) {
ans[0]=arr[1]; ans[1]=arr[2];
temp=arr[0];
}
else if(b==max&&c==max) {
ans[0]= arr[0]; ans[1]=arr[3];
temp=arr[1];
}
else if(b==max&&d==max) {
ans[0]= arr[1]; ans[1]= arr[3];
temp=arr[0];
}
else {
ans[0]= arr[2]; ans[1]=arr[3];
temp=arr[1];
}
}
System.out.println("! "+ans[0]+" "+ans[1]);
}
out.close();
}
static void find(int arr[]){
}
static long pow(long a,long b) {
if(b<0) return 1;
long res=1;
while(b!=0) {
if((b&1)!=0) {
res*=a;
res%=mod;
}
a*=a;
a%=mod;
b=b>>1;
}
return res;
}
static long gcd(long a,long b) {
if(b==0) return a;
return gcd(b,a%b);
}
static long nck(int n,int k) {
if(k>n) return 0;
long res=1;
res*=fact(n);
res%=mod;
res*=modInv(fact(k));
res%=mod;
res*=modInv(fact(n-k));
res%=mod;
return res;
}
static long fact(long n) {
// return fact[(int)n];
long res=1;
for(int i=2;i<=n;i++) {
res*=i;
res%=mod;
}
return res;
}
static long modInv(long n) {
return pow(n,mod-2);
}
static void sort(int[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
int temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Use this to input code since it is faster than a Scanner
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long[] readArrayL(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 0a5a47112ba6e4f7431aae905acb6d89 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.util.*;
import java.io.*;
// import java.lang.*;
// import java.math.*;
public class Main {
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=1000000007;
// static long mod=998244353;
static int MAX=Integer.MAX_VALUE;
static int MIN=Integer.MIN_VALUE;
static long MAXL=Long.MAX_VALUE;
static long MINL=Long.MIN_VALUE;
static ArrayList<Integer> graph[];
static long fact[];
static StringBuffer sb;
static int seg[];
static int lazy[];//lazy propagation is used in case of range updates.
static int dp[][];
public static void main (String[] args) throws java.lang.Exception
{
// File file = new File("C:\\Users\\Dell\\Desktop\\JAVA\\testcase.txt");
// FileWriter fw = new FileWriter("output.txt");
// BufferedReader br= new BufferedReader(new FileReader(file));
// code goes here
int t=I();
outer:while(t-->0)
{
int n=I();
int a=0,b=1,c=0,d=0;
ArrayList<pair> arr=new ArrayList<>();
for(int i=2;i<n && i+1<n;i+=2){
c=i;d=i+1;
int p1=query(a,b,c);
int p2=query(a,b,d);
int p3=query(a,d,c);
int p4=query(d,b,c);
arr.clear();
arr.add(new pair(p1,d));
arr.add(new pair(p2,c));
arr.add(new pair(p3,b));
arr.add(new pair(p4,a));
Collections.sort(arr,new myComp());
a=arr.get(0).b;
b=arr.get(1).b;
}
if(n%2==1){
c=n-1;
for(int i=0;i<n-1;i++){
if(a!=i && b!=i){
d=i;
break;
}
}
int p1=query(a,b,c);
int p2=query(a,b,d);
int p3=query(a,d,c);
int p4=query(d,b,c);
arr.clear();
arr.add(new pair(p1,d));
arr.add(new pair(p2,c));
arr.add(new pair(p3,b));
arr.add(new pair(p4,a));
Collections.sort(arr,new myComp());
a=arr.get(0).b;
b=arr.get(1).b;
}
System.out.println("! "+(a+1)+" "+(b+1));
}
out.close();
}
public static int query(int a,int b,int c)
{
System.out.println("? "+(a+1)+" "+(b+1)+" "+(c+1));
System.out.flush();
int res=I();
return res;
}
public static class pair
{
int a;
int b;
public pair(int aa,int bb)
{
a=aa;
b=bb;
}
}
public static class myComp implements Comparator<pair>
{
//sort in ascending order.
public int compare(pair p1,pair p2)
{
if(p1.a==p2.a)
return 0;
else if(p1.a<p2.a)
return -1;
else
return 1;
}
// sort in descending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return 1;
// else
// return -1;
// }
}
public static void DFS(int s,boolean visited[],int dis)
{
visited[s]=true;
for(int i:graph[s]){
if(!visited[i]){
DFS(i,visited,dis+1);
}
}
}
public static void setGraph(int n,int m)throws IOException
{
graph=new ArrayList[n+1];
for(int i=0;i<=n;i++){
graph[i]=new ArrayList<>();
}
for(int i=0;i<m;i++){
int u=I(),v=I();
graph[u].add(v);
graph[v].add(u);
}
}
//LOWER_BOUND and UPPER_BOUND functions
//It returns answer according to zero based indexing.
public static int lower_bound(int arr[],int X,int start, int end) //start=0,end=n-1
{
if(start>end)return -1;
if(arr[arr.length-1]<X)return end;
if(arr[0]>X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
// if(arr[mid]==X){ //Returns last index of lower bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
if(arr[mid]==X){ //Returns first index of lower bound value.
if(mid>start && arr[mid-1]==X){
right=mid-1;
}else{
return mid;
}
}
else if(arr[mid]>X){
if(mid>start && arr[mid-1]<X){
return mid-1;
}else{
right=mid-1;
}
}else{
if(mid<end && arr[mid+1]>X){
return mid;
}else{
left=mid+1;
}
}
}
return left;
}
//It returns answer according to zero based indexing.
public static int upper_bound(ArrayList<Long> arr,long X,int start,int end) //start=0,end=n-1
{
if(arr.get(0)>=X)return start;
if(arr.get(arr.size()-1)<X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
if(arr.get(mid)==X){ //returns first index of upper bound value.
if(mid>start && arr.get(mid-1)==X){
right=mid-1;
}else{
return mid;
}
}
// if(arr[mid]==X){ //returns last index of upper bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
else if(arr.get(mid)>X){
if(mid>start && arr.get(mid-1)<X){
return mid;
}else{
right=mid-1;
}
}else{
if(mid<end && arr.get(mid+1)>X){
return mid+1;
}else{
left=mid+1;
}
}
}
return left;
}
//END
//Segment Tree Code
public static void buildTree(int si,int ss,int se)
{
if(ss==se){
seg[si]=0;
return;
}
int mid=(ss+se)/2;
buildTree(2*si+1,ss,mid);
buildTree(2*si+2,mid+1,se);
seg[si]=(int)add(seg[2*si+1],seg[2*si+2]);
}
public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b)
{
int i=0,j=0;
while(i<a.size() && j<b.size()){
if(a.get(i)<=b.get(j)){
f.add(a.get(i));
i++;
}else{
f.add(b.get(j));
j++;
}
}
while(i<a.size()){
f.add(a.get(i));
i++;
}
while(j<b.size()){
f.add(b.get(j));
j++;
}
}
public static void update(int si,int ss,int se,int pos,int val)
{
if(ss==se){
seg[si]=(int)add(seg[si],val);
return;
}
int mid=(ss+se)/2;
if(pos<=mid){
update(2*si+1,ss,mid,pos,val);
}else{
update(2*si+2,mid+1,se,pos,val);
}
seg[si]=(int)add(seg[2*si+1],seg[2*si+2]);
}
public static int query(int si,int ss,int se,int qs,int qe)
{
if(qs>se || qe<ss)return 0;
if(ss>=qs && se<=qe)return seg[si];
int mid=(ss+se)/2;
return (int)add(query(2*si+1,ss,mid,qs,qe),query(2*si+2,mid+1,se,qs,qe));
}
//Segment Tree Code end
//Prefix Function of KMP Algorithm
public static int[] KMP(char c[],int n)
{
int pi[]=new int[n];
for(int i=1;i<n;i++){
int j=pi[i-1];
while(j>0 && c[i]!=c[j]){
j=pi[j-1];
}
if(c[i]==c[j])j++;
pi[i]=j;
}
return pi;
}
public static long nPr(int n,int r)
{
long ans=divide(fact(n),fact(n-r),mod);
return ans;
}
public static long nCr(int n,int r)
{
long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod);
return ans;
}
public static long kadane(long a[],int n) //largest sum subarray
{
long max_sum=Long.MIN_VALUE,max_end=0;
for(int i=0;i<n;i++){
max_end+=a[i];
if(max_sum<max_end){max_sum=max_end;}
if(max_end<0){max_end=0;}
}
return max_sum;
}
public static boolean isSorted(int a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static boolean isSorted(long a[])
{
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static int computeXOR(int n) //compute XOR of all numbers between 1 to n.
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
public static int np2(int x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
public static int hp2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static long hp2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static ArrayList<Integer> primeSieve(int n)
{
ArrayList<Integer> arr=new ArrayList<>();
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 * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
public static class FenwickTree
{
int farr[];
int n;
public FenwickTree(int c)
{
n=c+1;
farr=new int[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x,int p)
{
for(;x<n;x+=x&(-x))
{
farr[x]+=p;
}
}
public long get(int x)
{
long ans=0;
for(;x>0;x-=x&(-x))
{
ans=ans+farr[x];
}
return ans;
}
}
//Disjoint Set Union
public static class DSU
{
int par[],rank[];
public DSU(int c)
{
par=new int[c+1];
rank=new int[c+1];
for(int i=0;i<=c;i++)
{
par[i]=i;
rank[i]=0;
}
}
public int find(int a)
{
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public void union(int a,int b)
{
int a_rep=find(a),b_rep=find(b);
if(a_rep==b_rep)
return;
if(rank[a_rep]<rank[b_rep])
par[a_rep]=b_rep;
else if(rank[a_rep]>rank[b_rep])
par[b_rep]=a_rep;
else
{
par[b_rep]=a_rep;
rank[a_rep]++;
}
}
}
public static HashMap<Integer,Integer> primeFact(int a)
{
// HashSet<Long> arr=new HashSet<>();
HashMap<Integer,Integer> hm=new HashMap<>();
int p=0;
while(a%2==0){
// arr.add(2L);
p++;
a=a/2;
}
hm.put(2,hm.getOrDefault(2,0)+p);
for(int i=3;i*i<=a;i+=2){
p=0;
while(a%i==0){
// arr.add(i);
p++;
a=a/i;
}
hm.put(i,hm.getOrDefault(i,0)+p);
}
if(a>2){
// arr.add(a);
hm.put(a,hm.getOrDefault(a,0)+1);
}
// return arr;
return hm;
}
public static boolean isInteger(double N)
{
int X = (int)N;
double temp2 = N - X;
if (temp2 > 0)
{
return false;
}
return true;
}
public static boolean isPalindrome(String s)
{
int n=s.length();
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a,long b){return (a*b)/gcd(a,b);}
public static long fact(long n)
{
long fact=1;
for(long i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long fact(int n)
{
long fact=1;
for(int i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static boolean isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean isPrime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void printArray(long a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(char a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]);
}
out.println();
}
public static void printArray(String a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(boolean a[])
{
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(pair a[])
{
for(pair p:a){
out.println(p.a+"->"+p.b);
}
}
public static void printArray(int a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(long a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(char a[][])
{
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(ArrayList<?> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapI(HashMap<?,?> hm){
for(Map.Entry<?,?> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMap(HashMap<Long,ArrayList<Integer>> hm){
for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){
out.print(e.getKey()+"->");
ArrayList<Integer> arr=e.getValue();
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}out.println();
}
}
//Modular Arithmetic
public static long add(long a,long b)
{
a+=b;
if(a>=mod)a-=mod;
return a;
}
public static long sub(long a,long b)
{
a-=b;
if(a<0)a+=mod;
return a;
}
public static long mul(long a,long b)
{
return ((a%mod)*(b%mod))%mod;
}
public static long divide(long a,long b,long m)
{
a=mul(a,modInverse(b,m));
return a;
}
public static long modInverse(long a,long m)
{
int x=0,y=0;
own p=new own(x,y);
long g=gcdExt(a,m,p);
if(g!=1){
out.println("inverse does not exists");
return -1;
}else{
long res=((p.a%m)+m)%m;
return res;
}
}
public static long gcdExt(long a,long b,own p)
{
if(b==0){
p.a=1;
p.b=0;
return a;
}
int x1=0,y1=0;
own p1=new own(x1,y1);
long gcd=gcdExt(b,a%b,p1);
p.b=p1.a - (a/b) * p1.b;
p.a=p1.b;
return gcd;
}
public static long pwr(long m,long n)
{
long res=1;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m);
}
n=n>>1;
m=(m*m);
}
return res;
}
public static long modpwr(long m,long n)
{
long res=1;
m=m%mod;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m)%mod;
}
n=n>>1;
m=(m*m)%mod;
}
return res;
}
public static class own
{
long a;
long b;
public own(long val,long index)
{
a=val;
b=index;
}
}
//Modular Airthmetic
public static void sort(int[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(char[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
char tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(long[] A)
{
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
//max & min
public static int max(int a,int b){return Math.max(a,b);}
public static int min(int a,int b){return Math.min(a,b);}
public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));}
public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));}
public static long max(long a,long b){return Math.max(a,b);}
public static long min(long a,long b){return Math.min(a,b);}
public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));}
public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));}
public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
//end
public static int[] i(int n)
{
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=I();
}
return a;
}
public static long[] iL(int n)
{
long a[]=new long[n];
for(int i=0;i<n;i++){
a[i]=L();
}
return a;
}
public static long[] prefix(int a[])
{
int n=a.length;
long pre[]=new long[n];
pre[0]=a[0];
for(int i=1;i<n;i++){
pre[i]=pre[i-1]+a[i];
}
return pre;
}
public static long[] prefix(long a[])
{
int n=a.length;
long pre[]=new long[n];
pre[0]=a[0];
for(int i=1;i<n;i++){
pre[i]=pre[i-1]+a[i];
}
return pre;
}
public static int I(){return sc.I();}
public static long L(){return sc.L();}
public static String S(){return sc.S();}
public static double D(){return sc.D();}
}
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 I(){
return Integer.parseInt(next());
}
long L(){
return Long.parseLong(next());
}
double D(){
return Double.parseDouble(next());
}
String S(){
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 0e79700ba3448970d455429eb981a5f5 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | //make sure to make new file!
import java.io.*;
import java.util.*;
public class D770{
public static BufferedReader f;
public static PrintWriter out;
public static void main(String[] args)throws IOException{
f = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int t = Integer.parseInt(f.readLine());
for(int q = 1; q <= t; q++){
int n = Integer.parseInt(f.readLine());
if(n == 4){
int q1 = query(1,2,3);
int q2 = query(1,2,4);
int q3 = query(1,3,4);
int q4 = query(2,3,4);
int max = Math.max(Math.max(q1,q2),Math.max(q3,q4));
ArrayList<Integer> answers = new ArrayList<Integer>();
if(q1 != max) answers.add(4);
if(q2 != max) answers.add(3);
if(q3 != max) answers.add(2);
if(q4 != max) answers.add(1);
if(answers.size() == 1){
out.println("! " + answers.get(0) + " 1");
} else {
out.println("! " + answers.get(0) + " " + answers.get(1));
}
out.flush();
continue;
}
//find an extreme in n-2 queries
int[] q1 = new int[n+1];
for(int k = 3; k <= n; k++){
q1[k] = query(1,2,k);
}
//check if both extremes are in 1 or 2
boolean all = true;
int allint = q1[3];
for(int k = 3; k <= n; k++){
if(q1[k] != q1[3]){
all = false;
break;
}
}
/*
if(all){
out.println("! 1 2");
out.flush();
continue;
}*/
int max = -1;
int ex1 = -1;
for(int k = 3; k <= n; k++){
if(q1[k] > max){
max = q1[k];
ex1 = k;
}
}
ArrayList<Integer> qlist = new ArrayList<Integer>();
ArrayList<Integer> x1list = new ArrayList<Integer>();
ArrayList<Integer> x2list = new ArrayList<Integer>();
//get other extreme
for(int k = 1; k <= n; k++){
if(k == ex1)
continue;
int x1 = k;
int x2 = k+1;
if(x2 == ex1) x2++;
if(x2 > n)
continue;
int cur = query(x1,x2,ex1);
qlist.add(cur);
x1list.add(x1);
x2list.add(x2);
}
//get max
max = -1;
int maxindex = -1;
for(int k = 0; k < qlist.size(); k++){
if(qlist.get(k) > max){
max = qlist.get(k);
maxindex = k;
}
}
int ex2 = -1;
if(maxindex == 0 && qlist.get(1) != max){
ex2 = x1list.get(0);
} else if(maxindex == qlist.size()-1){
ex2 = x2list.get(x2list.size()-1);
} else {
int x1 = x1list.get(maxindex);
int x2 = x2list.get(maxindex);
int x3 = x2list.get(maxindex+1);
//either x2 = ex2, or both x1 and x3 = ex2
int cur = query(ex1,x1,x3);
if(cur == max){
ex2 = x1;
} else {
ex2 = x2;
}
}
if(all && max <= allint) out.println("! 1 2");
else out.println("! " + ex1 + " " + ex2);
out.flush();
}
out.close();
}
public static int query(int a, int b, int c)throws IOException{
out.println("? " + a + " " + b + " " + c);
out.flush();
return Integer.parseInt(f.readLine());
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 16338ed6075e89f976e5e9c7213fdc49 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
import java.util.Vector;
public class D {
String filename = null;
InputReader sc;
int query(int i, int j, int k) {
System.out.println("? " + i + " " + j + " " + k);
System.out.flush();
int r = sc.nextInt();
if (r == -1) {
System.exit(0);
}
return r;
}
void answer(int i, int j) {
System.out.println("! " + i + " " + j);
System.out.flush();
}
void solve() {
int n = sc.nextInt();
// n - 2 queries.
int extreme1 = -1;
{
int max = Integer.MIN_VALUE;
Vector<Integer> maxIdxs = new Vector<>();
for (int i = 3; i <= n; i++) {
int r = query(1, 2, i);
if (r > max) {
max = r;
maxIdxs.clear();
}
if (r == max) {
maxIdxs.add(i);
}
}
if (maxIdxs.size() == n - 2) {
// check if all the numbers are inside.
for (int i = 0; i + 1 < maxIdxs.size(); i++) {
int r = query(1, maxIdxs.get(i), maxIdxs.get(i + 1));
if (r > max) {
answer(maxIdxs.get(i), maxIdxs.get(i + 1));
return;
}
}
answer(1, 2);
return;
}
extreme1 = maxIdxs.get(0);
}
{
int max = Integer.MIN_VALUE;
Vector<Integer> maxIdxs = new Vector<>();
for (int i = 2; i <= n; i++) {
if (i == extreme1) {
continue;
}
int r = query(1, extreme1, i);
if (r > max) {
maxIdxs.clear();
max = r;
}
if (r == max) {
maxIdxs.add(i);
}
}
if (maxIdxs.size() == n - 2) {
answer(1, extreme1);
return;
}
answer(extreme1, maxIdxs.get(0));
}
}
public void run() throws FileNotFoundException {
if (filename == null) {
sc = new InputReader(System.in);
} else {
sc = new InputReader(new FileInputStream(new File(filename)));
}
int nTests = sc.nextInt();
for (int test = 0; test < nTests; test++) {
solve();
}
}
public static void main(String[] args) {
D sol = new D();
try {
sol.run();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
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 float nextFloat() {
return Float.parseFloat(next());
}
public double nextDouble() {
return Float.parseFloat(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
}
} | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 767cc41b9d4f5791a5e5ee635253d19b | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.util.*;
import java.io.*;
public class D1643 {
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static int query(int[] three) throws IOException {
pw.println("? " + three[0] + " " + three[1] + " " + three[2]);
pw.flush();
return sc.nextInt();
}
public static int[] getBounds(int[] four) throws IOException {
int[][] queries = new int[4][2];
for (int i = 0; i < 4; i++) {
int[] three = new int[3];
for (int j = 0, idx = 0; j < 4; j++) {
if (i == j)
continue;
three[idx++] = four[j];
}
queries[i] = new int[]{15 ^ (1 << i), query(three)};
}
Arrays.sort(queries, (u, v) -> v[1] - u[1]);
int myMask = queries[0][0] & queries[1][0];
int[] ans = new int[2];
int ansIdx = 0;
for (int i = 0; i < 4; i++) {
if ((myMask & (1 << i)) != 0) {
ans[ansIdx++] = four[i];
}
}
return ans;
}
public static void main(String[] args) throws IOException {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] two = {1, 2};
for (int i = 3; i <= n; i += 2) {
int[] four;
if (i != n) {
four = new int[]{two[0], two[1], i, i + 1};
} else {
four = new int[]{two[0], two[1], i, 0};
for (int j = 1; j <= n; j++) {
if (j != two[0] && j != two[1]) {
four[3] = j;
break;
}
}
}
two = getBounds(four);
}
pw.println("! " + two[0] + " " + two[1]);
pw.flush();
}
pw.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader f) {
br = new BufferedReader(f);
}
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 double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
}
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 57827c837ef5971d0ad41cad2ee2b3d9 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Map.Entry;
import java.util.Random;
import java.util.TreeSet;
public final class CF_770_D_simulate
{
static boolean verb=true;
static void log(Object X){if (verb) System.err.println(X);}
static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}}
static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}
static void logWln(Object X){if (verb) System.err.print(X);}
static void info(Object o){ System.out.println(o);}
static void output(Object o){outputWln(""+o+"\n"); }
static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}
// Global vars
static BufferedWriter out;
static InputReader reader;
static int pgcd(int a,int b){
if (a<b)
return pgcd(b,a);
while (b!=0){
int c=b;
b=a%b;
a=c;
}
return a;
}
static int iter;
static void test() {
log("testing");
Random r=new Random();
int NTESTS=10000;
int NMAX=10000;
int VMAX=100;
int[] ref=new int[4];
for (int t=0;t<NTESTS;t++) {
int n=r.nextInt(NMAX)+4;
iter=0;
int[] ar=new int[n];
for (int i=0;i<n;i++)
ar[i]=r.nextInt(VMAX)+1;
int z=r.nextInt(n);
ar[z]=0;
log("n:"+n+" z:"+z);
log(ar);
log("============");
ArrayList<Integer> tm=new ArrayList<Integer>(n);
for (int i=0;i<n;i++)
tm.add(i);
log("tm:"+tm);
while (tm.size()>2) {
if (tm.size()==3) {
for (int x=0;x<n;x++) {
if (tm.indexOf(x)<0) {
tm.add(x);
if (tm.size()==4)
break;
}
}
}
int cur=0;
ArrayList<Integer> nxt=new ArrayList<Integer>();
int L=tm.size();
while (cur+4<=L) {
int best=-1;
for (int e=0;e<4;e++) {
ref[e]=ask4(tm,cur,e,ar);
best=Math.max(best, ref[e]);
}
for (int e=0;e<4;e++) {
if (ref[e]!=best) {
log("adding:"+tm.get(cur+e));
nxt.add(tm.get(cur+e));
}
}
cur+=4;
}
//log("nxt:"+nxt);
while (cur<L) {
nxt.add(tm.get(cur++));
}
tm=nxt;
}
ArrayList<Integer> cand=tm;
if (cand.size()==1)
cand.add(cand.get(0));
if (z!=cand.get(0) && z!=cand.get(1)) {
log(ar);
log(cand);
log("z:"+z);
log("Error");
return;
}
if (iter>2*n-2) {
log("============ MAXIMUM !!!");
return;
}
double ratio=iter;
ratio/=(2*n-2);
log("ratio:::"+ratio+" "+iter);
}
log("testing done");
}
static class Composite implements Comparable<Composite>{
int a;
int b;
public int compareTo(Composite X) {
int diff=b-a;
int Xdiff=X.b-X.a;
if (diff!=Xdiff)
return diff-Xdiff;
return a-X.a;
}
public Composite(int a, int b) {
this.a = a;
this.b = b;
}
}
static int ask4(ArrayList<Integer> list,int index,int exclude,int[] ar) {
int a=list.get(index);
int b=list.get(index+1);
int c=list.get(index+2);
int d=list.get(index+3);
if (exclude==0)
return ask(b,c,d,ar);
if (exclude==1)
return ask(a,c,d,ar);
if (exclude==2)
return ask(a,b,d,ar);
return ask(a,b,c,ar);
}
static int ask4(ArrayList<Integer> list,int index,int exclude) throws Exception {
String s="? ";
for (int u=0;u<4;u++) {
if (u!=exclude) {
s+=(list.get(index+u)+1)+" ";
}
}
System.out.println(s);
System.out.flush();
return reader.readInt();
}
static int ask4(int x) throws Exception {
String s="? ";
for (int u=0;u<4;u++) {
if (u!=x) {
s+=(u+1)+" ";
}
}
System.out.println(s);
System.out.flush();
return reader.readInt();
}
static int ask(int a,int b,int c,int[] ar) {
iter++;
int mx=Math.max(ar[a],ar[b]);
mx=Math.max(mx,ar[c]);
int mn=Math.min(ar[a],ar[b]);
mn=Math.min(mn,ar[c]);
return mx-mn;
}
static int ask4(int x,int[] ar) {
if (x==0)
return ask(1,2,3,ar);
if (x==1)
return ask(0,2,3,ar);
if (x==2)
return ask(0,1,3,ar);
return ask(0,1,2,ar);
}
static int ask(int a,int b,int c) throws Exception {
String s="? "+(a+1)+" "+(b+1)+" "+(c+1);
System.out.println(s);
System.out.flush();
return reader.readInt();
}
static ArrayList<Integer> simplify(int a,int b,int c,int d) throws Exception {
int[] tm=new int[] {a,b,c,d};
int best=0;
int[] ref=new int[4];
for (int i=0;i<4;i++) {
int x=0;
if (i==0) {
x=ask(b,c,d);
}
if (i==1) {
x=ask(a,c,d);
}
if (i==2) {
x=ask(a,b,d);
}
if (i==3)
x=ask(a,b,c);
ref[i]=x;
best=Math.max(best, ref[i]);
}
ArrayList<Integer> cand=new ArrayList<Integer>();
for (int i=0;i<4;i++) {
if (ref[i]!=best) {
cand.add(tm[i]);
}
}
return cand;
}
static void process() throws Exception {
//out = new BufferedWriter(new OutputStreamWriter(System.out));
reader = new InputReader(System.in);
//test();
int[] ref=new int[4];
int T=reader.readInt();
for (int t=0;t<T;t++) {
int n=reader.readInt();
ArrayList<Integer> tm=new ArrayList<Integer>(n);
for (int i=0;i<n;i++)
tm.add(i);
//log("tm:"+tm);
while (tm.size()>2) {
if (tm.size()==3) {
for (int x=0;x<n;x++) {
if (tm.indexOf(x)<0) {
tm.add(x);
if (tm.size()==4)
break;
}
}
}
int cur=0;
ArrayList<Integer> nxt=new ArrayList<Integer>();
int L=tm.size();
while (cur+4<=L) {
int best=-1;
for (int e=0;e<4;e++) {
ref[e]=ask4(tm,cur,e);
best=Math.max(best, ref[e]);
}
for (int e=0;e<4;e++) {
if (ref[e]!=best) {
//log("adding:"+tm.get(cur+e));
nxt.add(tm.get(cur+e));
}
}
cur+=4;
}
//log("nxt:"+nxt);
while (cur<L) {
nxt.add(tm.get(cur++));
}
tm=nxt;
}
ArrayList<Integer> cand=tm;
if (cand.size()==1)
cand.add(cand.get(0));
System.out.println("! "+(cand.get(0)+1)+" "+(cand.get(1)+1));
System.out.flush();
}
/*
try {
out.close();
} catch (Exception Ex) {
}
*/
}
public static void main(String[] args) throws Exception {
process();
}
static final class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public final String readString() throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public final String readString(int L) throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder(L);
do {
res.append((char) c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public final int readInt() throws IOException {
int c = read();
boolean neg = false;
while (isSpaceChar(c)) {
c = read();
}
char d = (char) c;
// log("d:"+d);
if (d == '-') {
neg = true;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
// log("res:"+res);
if (neg)
return -res;
return res;
}
public final long readLong() throws IOException {
int c = read();
boolean neg = false;
while (isSpaceChar(c)) {
c = read();
}
char d = (char) c;
// log("d:"+d);
if (d == '-') {
neg = true;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
// log("res:"+res);
if (neg)
return -res;
return res;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
/*
1
10
1 0 0 0 0 1 0 0 1 1
1
12
1 0 0 1 0 0 1 0 0 0 0 1
1
12
1 0 0 1 0 0 1 0 0 0 0 1
3
12
1 0 0 1 0 0 1 0 0 0 0 1
10
1 0 0 0 0 1 0 0 1 1
12
1 0 0 1 0 0 1 0 0 0 0 1
1
11
1 0 0 0 0 0 0 0 1 1 1
19
1 1 0 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1
19
1 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 0
19
1 0 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0
1
20
1 1 0 0 0 1 0 1 1 1 0 1 1 1 1 1 0 0 1 0
*/ | Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output | |
PASSED | 95ecb55388570ff41e118fc5e6737a77 | train_108.jsonl | 1644158100 | This is an interactive problem.We picked an array of whole numbers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$.You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$.You are allowed to make no more than $$$2 \cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$.Can you guess where we hid the zero?Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;
import java.util.StringTokenizer;
public class Main {
public static int query(int i, int j, int k) {
System.out.println("? "+i + " " + j + " " + k);
System.out.flush();
int val = fs.nextInt();
return val;
}
public static void print(ArrayList<Integer> list) {
System.out.print("!");
for (int e : list) {
System.out.print(" " + e);
}
System.out.println();
System.out.flush();
}
public static ArrayList<Integer> solve(ArrayList<Integer> list) {
ArrayList<Integer> res = new ArrayList<>();
Pair ci[] = new Pair[4];
int a = list.get(0);
int b = list.get(1);
int c = list.get(2);
int d = list.get(3);
ci[0] = new Pair(query(b, c, d), a);
ci[1] = new Pair(query(a, c, d), b);
ci[2] = new Pair(query(a, b, d), c);
ci[3] = new Pair(query(a, b, c), d);
Arrays.sort(ci);
res.add(ci[0].sc);
res.add(ci[1].sc);
return res;
}
public static void main(String[] args) throws IOException {
fs = new FastReader();
out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; ) {
while (list.size() < 4 && i <= n) {
list.add(i);
++i;
}
if (list.size() < 4) break;
list = solve(list);
}
if (list.size() != 2) {
int i = 1;
while (i <= n && list.size() < 4) {
boolean f = true;
for (int e : list) {
if (e == i) {
f = false;
break;
}
}
if (f) {
list.add(i);
break;
}
++i;
}
list = solve(list);
}
print(list);
}
out.close();
}
static PrintWriter out;
static FastReader fs;
static final Random random = new Random();
}
class Pair implements Comparable<Pair> {
int fs, sc;
Pair() {
}
Pair(int fs_, int sc_) {
fs = fs_;
sc = sc_;
}
@Override
public String toString() {
return "Pair{" +
"fs=" + fs +
", sc=" + sc +
'}';
}
@Override
public int compareTo(Pair oth) {
int ret = Integer.compare(fs, oth.fs);
if (ret != 0) return ret;
return Integer.compare(sc, oth.sc);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
return fs == pair.fs &&
sc == pair.sc;
}
@Override
public int hashCode() {
return Objects.hash(fs, sc);
}
}
class FastReader {
private BufferedReader br;
private StringTokenizer st = new StringTokenizer("");
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String file_name) throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file_name))));
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = nextInt();
return a;
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["1\n\n4\n\n2\n\n3\n\n3\n\n2"] | 1 second | ["? 1 2 3\n\n? 2 3 4\n\n? 3 4 1\n\n? 4 1 2\n\n! 2 3"] | NoteArray from sample: $$$[1, 2, 0, 3]$$$. | Java 8 | standard input | [
"constructive algorithms",
"interactive",
"math"
] | 84e79bd83c51a4966b496bb767ec4f0d | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 \le n \le 1000$$$) — the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. | 2,000 | null | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.