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 | 3fc5381a94ea9a252343cbce30bebda5 | 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.HashMap;
import java.util.StringTokenizer;
public class C {
static class Fast {
BufferedReader br;
StringTokenizer st;
public Fast() {
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());
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readArray1(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/* static long noOfDivisor(long a)
{
long count=0;
long t=a;
for(long i=1;i<=(int)Math.sqrt(a);i++)
{
if(a%i==0)
count+=2;
}
if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a)))
{
count--;
}
return count;
}*/
static boolean isPrime(long a) {
for (long i = 2; i <= (long) Math.sqrt(a); i++) {
if (a % i == 0)
return false;
}
return true;
}
static void primeFact(int n) {
int temp = n;
HashMap<Integer, Integer> h = new HashMap<>();
for (int i = 2; i * i <= n; i++) {
if (temp % i == 0) {
int c = 0;
while (temp % i == 0) {
c++;
temp /= i;
}
h.put(i, c);
}
}
if (temp != 1)
h.put(temp, 1);
}
static void reverseArray(int a[]) {
int n = a.length;
for (int i = 0; i < n / 2; i++) {
a[i] = a[i] ^ a[n - i - 1];
a[n - i - 1] = a[i] ^ a[n - i - 1];
a[i] = a[i] ^ a[n - i - 1];
}
}
public static void main(String args[]) throws IOException {
Fast sc = new Fast();
PrintWriter out = new PrintWriter(System.out);
int t1 = sc.nextInt();
outer: while (t1-- > 0)
{
/* int n=sc.nextInt();
int k=sc.nextInt();
int o1=(int)Math.ceil(n*1.000/2);
int size=n*k;
int noofodd=(int)Math.ceil(size*1.000/2);
int noeven=size-noofodd;
if(noofodd%o1!=0)
{
out.println("NO");
continue outer;
}
out.println("YES");
int a1=1,a2=2;
for(int i=1;i<=n;i++)
{
int t=i;
for(int i1=1;i1<=k;i1++)
{
out.print(t+" ");
t+=n;
}
out.println();
}
// out.println();*/
int n=sc.nextInt();
int k=sc.nextInt();
if(n%2==0||k==1)
{
out.println("YES");
int size=n*k;
int j=1;
for(int i=1;i<=size;i+=2)
{
out.print(i+" ");
j++;
if(j>k)
{
out.println();
j=1;
}
}
for(int i=2;i<=size;i+=2)
{
out.print(i+" ");
j++;
if(j>k)
{
out.println();
j=1;
}
}
}
else
out.println("NO");
}
out.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 8 | 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 | 0c1f10f3c4b3d970fc8d7879d6180522 | 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.*;
import java.io.*;
import java.util.*;
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 m=sc.nextInt();
ArrayList<Integer> odd=new ArrayList<>();
ArrayList<Integer> even=new ArrayList<>();
for(int i=1;i<=n*m;i++){
if((i&1)==1)
odd.add(i);
else even.add(i);
}
if(odd.size()%m==0 && even.size()%m==0){
System.out.println("YES");
for(int i=0;i<odd.size();i++){
System.out.print(odd.get(i)+" ");
if((i+1)%m==0)
System.out.println();
}
for(int i=0;i<even.size();i++){
System.out.print(even.get(i)+" ");
if((i+1)%m==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 8 | 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 | 242833274ab5ea22c2b030173c75b74f | 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;
public class B {
private final IO io;
public B(IO io) {
this.io = io;
}
public void solution() throws IOException {
int t = io.nextInt();
for (int i = 0; i < t; i++) {
solveCase();
}
io.flush();
}
private void solveCase() throws IOException {
int n = io.nextInt();
int k = io.nextInt();
if (k == 1) {
solveTrivial(n, k);
return;
}
if (n % 2 != 0) {
io.println("NO");
return;
}
io.println("YES");
for (int i = 0; i < n; i++) {
int start = (i / 2) * 2 * k + (i % 2) + 1; // every 2 rows increase by 2k, i%2 to denote even / odd rows, 1
for (int j = 0; j < k; j++) {
io.print(start + 2 * j);
io.print(" ");
}
io.println("");
}
}
private void solveTrivial(int n, int k) throws IOException {
assert k == 1;
io.println("YES");
for (int i = 1; i <= n; i++) {
io.println(i);
}
}
public static void main(String[] args) throws IOException {
B a = new B(new IO(System.in));
a.solution();
}
private static final class IO {
private final BufferedReader in;
private StringTokenizer tokenizer;
private final BufferedWriter out;
public IO(InputStream is) {
in = new BufferedReader(new InputStreamReader(is));
out = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextToken() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
}
public Integer nextInt() throws IOException {
return Integer.valueOf(nextToken());
}
public void println(Object o) throws IOException {
this.print(o);
out.newLine();
}
public void print(Object o) throws IOException {
out.write(o.toString());
}
public void flush() throws IOException {
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 8 | 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 | 1e935d81ff793a5d96ea4ea73b36a18c | 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 CodeForces{
public static void main(String[] args) throws FileNotFoundException {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
while(t-- > 0) {
int n = fs.nextInt(), k = fs.nextInt();
boolean flag = true;
int sum = 0;
int index = 1;
boolean[] visited = new boolean[n*k+1];
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= n; i++) {
int j = index;
int counter = 0;
while(j <= n*k && counter < k) {
if(visited[j]) {
j++;
continue;
}
visited[j] = true;
sb.append(j + " ");
j += 2;
counter++;
}
if(counter != k) {
flag = false;
}
index++;
// System.out.println(sb.toString());
if(i != n) sb.append("\n");
}
// for(int i = 1; i <= n*k; i++) {
// if(!visited[i]) {
//// System.out.println(i);
// flag = false;
// }
// }
if(flag) {
System.out.println("YES");
System.out.println(sb.toString());
}else {
System.out.println("NO");
// System.out.println(sb.toString());
}
}
}
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 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());
}
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());
}
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 8 | 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 | 2984b435ca1cc7d71c8b4cb7415c86b3 | 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 Codechef {
static long fans[] = new long[200001];
static long inv[] = new long[200001];
static long mod = 1000000007;
static void init() {
fans[0] = 1;
inv[0] = 1;
fans[1] = 1;
inv[1] = 1;
for (int i = 2; i < 200001; i++) {
fans[i] = ((long) i * fans[i - 1]) % mod;
inv[i] = power(fans[i], mod - 2);
}
}
static long ncr(int n, int r) {
return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod;
}
public static void main(String[] args) throws java.lang.Exception {
FastReader in = new FastReader(System.in);
StringBuilder sb = new StringBuilder();
int t = 1;
t = in.nextInt();
while (t > 0) {
--t;
int n = in.nextInt();
int m = in.nextInt();
int val = n*m;
if(m == 1)
{
sb.append("YES\n");
for(int i = 1;i<=n;i++)
sb.append(i+"\n");
continue;
}
else if(n == 1)
{
sb.append("NO\n");
continue;
}
if(val%2 == 1)
sb.append("NO\n");
else
{
int even = val/2;
int odd = val/2;
if(even%m!=0)
sb.append("NO\n");
else
{ int j = 0;
int i = 0;
int ans[][] = new int[n][m];
int k = 1;
while(odd>0)
{
ans[i][j] = k;
k+=2;
--odd;
++j;
if(j == m)
{
j = 0;
++i;
}
}
k = 2;
while(even>0)
{
ans[i][j] = k;
k+=2;
--even;
++j;
if(j == m)
{
j = 0;
++i;
}
}
sb.append("YES\n");
for(i = 0;i<n;i++)
{
for(j = 0;j<m;j++)
sb.append(ans[i][j]+" ");
sb.append("\n");
}
}
}
}
System.out.print(sb);
}
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 % mod) * (x % mod)) % mod;
// y must be even now
y = y >> 1; // y = y/2
x = ((x % mod) * (x % mod)) % mod; // Change x to x^2
}
return res;
}
static long[] generateArray(FastReader in, int n) throws IOException {
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = in.nextLong();
return arr;
}
static long[][] generatematrix(FastReader in, int n, int m) throws IOException {
long arr[][] = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = in.nextLong();
}
}
return arr;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
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);
}
}
class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;
FastReader(InputStream is) {
in = is;
}
int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}
String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
String nextLine() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}
char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan())
;
return (char) c;
}
int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan())
;
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
} | Java | ["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 8 | 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 | 5e8235aaec02af7dacbd67eff7987f48 | 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.net.CookieHandler;
import java.util.*;
import java.io.*;
//import static com.sun.tools.javac.jvm.ByteCodes.swap;
// ?)(?
public class fastTemp {
static FastScanner fs = null;
static ArrayList<Long> graph[] ;
static int mod = 998244353;
// static int count=0;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
int k = fs.nextInt();
long odd = 0;
long sum = n * k;
if (sum % 2 == 0) {
odd = sum / 2;
} else {
odd = sum / 2 + 1;
}
long odd1 = odd;
long j = sum;
if (j % 2 == 0) {
j--;
}
if (odd % k != 0) {
out.println("NO");
}
else {
out.println("YES");
int g = 2;
int l=0;
for(int i=0;i<k*(odd/k) && l<k;i++){
out.print(j+" ");
j-=2;
l++;
if(l==k){
out.println();
l=0;
}
}
l = 0;
for(int i=0;i<k*(n - (odd/k)) && l<k;i++){
out.print(g+" ");
g+=2;
l++;
if(l==k){
out.println();
l=0;
}
}
}
}
out.close();
}
static boolean f = false;
public static void solve(long x,long y,int a[],int c){
if(c==a.length && x!=y){
return ;
}
if(c==a.length && x==y){
f = true;
return ;
}
long t1 = x+a[c];
long t2 = x^a[c];
solve(t1,y,a,c+1);
if(f){
return;
}
solve(t2,y,a,c+1);
if(f){
return;
}
return;
}
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) {
return this.x - o.x;
}
}
// JAVA program to compute factorial
// of big numbers
// This function finds factorial of
// large numbers and prints them
static void factorial(int n)
{
int res[] = new int[500];
// Initialize result
res[0] = 1;
int res_size = 1;
// Apply simple factorial formula
// n! = 1 * 2 * 3 * 4...*n
for (int x = 2; x <= n; x++)
res_size = multiply(x, res, res_size);
System.out.println("Factorial of given number is ");
for (int i = res_size - 1; i >= 0; i--)
System.out.print(res[i]);
}
// This function multiplies x with the number
// represented by res[]. res_size is size of res[] or
// number of digits in the number represented by res[].
// This function uses simple school mathematics for
// multiplication. This function may value of res_size
// and returns the new value of res_size
static int multiply(int x, int res[], int res_size)
{
int carry = 0; // Initialize carry
// One by one multiply n with individual
// digits of res[]
for (int i = 0; i < res_size; i++)
{
int prod = res[i] * x + carry;
res[i] = prod % 10; // Store last digit of
// 'prod' in res[]
carry = prod/10; // Put rest in carry
}
// Put carry in res and increase result size
while (carry!=0)
{
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
static long fact[] = new long[200001];
public static void fact(){
fact[1] = 1;
fact[0] = 1;
for(int i=2;i<=200000;i++){
fact[i] = (((fact[i-1])%mod) * ((i)%mod))%mod;
}
}
static long lower(long array[], long key,int i,int k)
{
long ans=-1;
while(i<=k){
int mid = (i+k)/2;
if(array[mid]>=key){
ans = mid;
k = mid-1;
}else{
i = mid+1;
}
}
return ans;
}
static long upper(long array[], long key ,int i , int k){
long ans = -1;
while(i<=k){
int mid = (i+k)/2;
if(array[mid]<=key){
ans = array[mid];
i = mid+1;
}else{
k = mid-1;
}
}
return ans;
}
public static class String1 implements Comparable<String1>{
String str;
int id;
String1(String str , int id){
this.str = str;
this.id = id;
}
public int compareTo(String1 o){
int i=0;
while(i<str.length() && str.charAt(i)==o.str.charAt(i)){
i++;
}
if(i<str.length()){
if(i%2==1){
return o.str.compareTo(str); // descending order
}else{
return str.compareTo(o.str); // ascending order
}
}
return str.compareTo(o.str);
}
}
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 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());
}
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());
}
}
// ------------------------------------------swap----------------------------------------------------------------------
static void swap(int arr[],int i,int j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
//-------------------------------------------seiveOfEratosthenes----------------------------------------------------
static boolean prime[];
static void sieveOfEratosthenes(int n)
{
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
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] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
// for (int i = 2; i <= n; i++)
// {
// if (prime[i] == true)
// System.out.print(i + " ");
// }
}
//------------------------------------------- power------------------------------------------------------------------
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
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;
}
//--------------------------------------------------------------------------------------------------------------
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
//---------------------------------------EXTENDED EUCLID ALGO--------------------------------------------------------
public static Pair Euclid(int a,int b){
if(b==0){
return new Pair(1,0); // answer of x and y
}
Pair dash = Euclid(b,a%b);
return new Pair(dash.y , dash.x - (a/b)*dash.y);
}
//--------------------------------GCD------------------GCD-----------GCD--------------------------------------------
public static long gcd(long a,long b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
static long C(int n, int k) {
long res = 1;
for (int i = n - k + 1; i <= n; ++i)
res *= i;
for (int i = 2; i <= k; ++i)
res /= i;
return res;
}
} | 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 8 | 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 | 94fedcfed94fa0a25203265d0d52f19c | 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.beans.DesignMode;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.CompletableFuture.AsynchronousCompletionTask;
import org.xml.sax.ErrorHandler;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.io.DataInputStream;
public class Solution {
//TEMPLATE -------------------------------------------------------------------------------------
public static boolean Local(){
try{
return System.getenv("LOCAL_SYS")!=null;
}catch(Exception e){
return false;
}
}
public static boolean LOCAL;
static class FastScanner {
BufferedReader br;
StringTokenizer st ;
FastScanner(){
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
FastScanner(String file) {
try{
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
st = new StringTokenizer("");
}catch(FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("file not found");
e.printStackTrace();
}
}
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());
}
double nextDouble(){
return Double.parseDouble(next());
}
String readLine() throws IOException{
return br.readLine();
}
}
static class Pair<T,X> {
T first;
X second;
Pair(T first,X second){
this.first = first;
this.second = second;
}
@Override
public int hashCode(){
return Objects.hash(first,second);
}
@Override
public boolean equals(Object obj){
return obj.hashCode() == this.hashCode();
}
}
static class TreeNode{
TreeNode left;
TreeNode right;
int l;
int r;
int min_val;
int min_cnt;
TreeNode(int l,int r,int min_val,int min_cnt){
this.l = l;
this.r = r;
this.min_val = min_val;
this.min_cnt = min_cnt;
}
}
static PrintStream debug = null;
static long mod = (long)(Math.pow(10,9) + 7);
//TEMPLATE -------------------------------------------------------------------------------------END//
public static void main(String[] args) throws Exception {
FastScanner s = new FastScanner();
LOCAL = Local();
//PrintWriter pw = new PrintWriter(System.out);
if(LOCAL){
s = new FastScanner("src/input.txt");
PrintStream o = new PrintStream("src/sampleout.txt");
debug = new PrintStream("src/debug.txt");
System.setOut(o);
// pw = new PrintWriter(o);
} long mod = 1000000007;
int tcr = s.nextInt();
StringBuilder sb = new StringBuilder();
for(int tc=0;tc<tcr;tc++){
int n = s.nextInt();
int k = s.nextInt();
if((n != 1) && (k!= 1) && (((n*k)%2)!=0)){
sb.append("NO\n");
continue;
}else if(n==1){
if(k == 1){
sb.append("YES\n");
sb.append("1\n");
}else{
sb.append("NO\n");
}
}else{
int ans[][] = new int[n][k];
int even = 2;
int odd = 1;
for(int i=0;i<n;i++){
if(i%2==0){
for(int j=0;j<k;j++){
ans[i][j] = odd;
odd+=2;
}
}else{
for(int j=0;j<k;j++){
ans[i][j] = even;
even+=2;
}
}
}
if(ans[n-1][k-1] > (n*k)){
sb.append("NO\n");
continue;
}
sb.append("YES\n");
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
sb.append(ans[i][j]+" ");
}
sb.append('\n');
}
}
}
print(sb.toString());
}
public static List<int[]> print_prime_factors(int n){
List<int[]> list = new ArrayList<>();
for(int i=2;i<=(int)(Math.sqrt(n));i++){
if(n % i == 0){
int cnt = 0;
while( (n % i) == 0){
n = n/i;
cnt++;
}
list.add(new int[]{i,cnt});
}
}
if(n!=1){
list.add(new int[]{n,1});
}
return list;
}
public static List<int[]> prime_factors(int n,List<Integer> sieve){
List<int[]> list = new ArrayList<>();
int index = 0;
while(n > 1 && sieve.get(index) <= Math.sqrt(n)){
int curr = sieve.get(index);
int cnt = 0;
while((n % curr) == 0){
n = n/curr;
cnt++;
}
if(cnt >= 1){
list.add(new int[]{curr,cnt});
}
index++;
}
if(n > 1){
list.add(new int[]{n,1});
}
return list;
}
public static boolean inRange(long r1,long r2,long val){
return ((val >= r1) && (val <= r2));
}
static int len(long num){
return Long.toString(num).length();
}
static long mulmod(long a, long b,long mod)
{
long ans = 0l;
while(b > 0){
long curr = (b & 1l);
if(curr == 1l){
ans = ((ans % mod) + a) % mod;
}
a = (a + a) % mod;
b = b >> 1;
}
return ans;
}
public static void dbg(PrintStream ps,Object... o) throws Exception{
if(ps == null){
return;
}
Debug.dbg(ps,o);
}
public static long modpow(long num,long pow,long mod){
long val = num;
long ans = 1l;
while(pow > 0l){
long bit = pow & 1l;
if(bit == 1){
ans = (ans * (val%mod))%mod;
}
val = (val * val) % mod;
pow = pow >> 1;
}
return ans;
}
public static long pow(long num,long pow){
long val = num;
long ans = 1l;
while(pow > 0l){
long bit = pow & 1l;
if(bit == 1){
ans = (ans * (val));
}
val = (val * val);
pow = pow >> 1;
}
return ans;
}
public static char get(int n){
return (char)('a' + n);
}
public static long[] sort(long arr[]){
List<Long> list = new ArrayList<>();
for(long n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
public static int[] sort(int arr[]){
List<Integer> list = new ArrayList<>();
for(int n : arr){list.add(n);}
Collections.sort(list);
for(int i=0;i<arr.length;i++){
arr[i] = list.get(i);
}
return arr;
}
// return the (index + 1)
// where index is the pos of just smaller element
// i.e count of elemets strictly less than num
public static int justSmaller(long arr[],long num){
// System.out.println(num+"@");
int st = 0;
int e = arr.length - 1;
int ans = -1;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] >= num){
e = mid - 1;
}else{
ans = mid;
st = mid + 1;
}
}
return ans + 1;
}
public static int justSmaller(int arr[],int num){
// System.out.println(num+"@");
int st = 0;
int e = arr.length - 1;
int ans = -1;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] >= num){
e = mid - 1;
}else{
ans = mid;
st = mid + 1;
}
}
return ans + 1;
}
//return (index of just greater element)
//count of elements smaller than or equal to num
public static int justGreater(long arr[],long num){
int st = 0;
int e = arr.length - 1;
int ans = arr.length;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] <= num){
st = mid + 1;
}else{
ans = mid;
e = mid - 1;
}
}
return ans;
}
public static int justGreater(int arr[],int num){
int st = 0;
int e = arr.length - 1;
int ans = arr.length;
while(st <= e){
int mid = (st + e)/2;
if(arr[mid] <= num){
st = mid + 1;
}else{
ans = mid;
e = mid - 1;
}
}
return ans;
}
public static void println(Object obj){
System.out.println(obj.toString());
}
public static void print(Object obj){
System.out.print(obj.toString());
}
public static int gcd(int a,int b){
if(b == 0){return a;}
return gcd(b,a%b);
}
public static long gcd(long a,long b){
if(b == 0l){
return a;
}
return gcd(b,a%b);
}
public static int find(int parent[],int v){
if(parent[v] == v){
return v;
}
return parent[v] = find(parent, parent[v]);
}
public static List<Integer> sieve(){
List<Integer> prime = new ArrayList<>();
int arr[] = new int[100001];
Arrays.fill(arr,1);
arr[1] = 0;
arr[2] = 1;
for(int i=2;i<100001;i++){
if(arr[i] == 1){
prime.add(i);
for(long j = (i*1l*i);j<100001;j+=i){
arr[(int)j] = 0;
}
}
}
return prime;
}
static boolean isPower(long n,long a){
long log = (long)(Math.log(n)/Math.log(a));
long power = (long)Math.pow(a,log);
if(power == n){return true;}
return false;
}
private static int mergeAndCount(int[] arr, int l,int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l, swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static int mergeSortAndCount(int[] arr, int l,int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
int count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static class Debug{
//change to System.getProperty("ONLINE_JUDGE")==null; for CodeForces
public static final boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null;
private static <T> String ts(T t) {
if(t==null) {
return "null";
}
try {
return ts((Iterable<T>) t);
}catch(ClassCastException e) {
if(t instanceof int[]) {
String s = Arrays.toString((int[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof long[]) {
String s = Arrays.toString((long[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof char[]) {
String s = Arrays.toString((char[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof double[]) {
String s = Arrays.toString((double[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}else if(t instanceof boolean[]) {
String s = Arrays.toString((boolean[]) t);
return "{"+s.substring(1, s.length()-1)+"}\n";
}
try {
return ts((Object[]) t);
}catch(ClassCastException e1) {
return t.toString();
}
}
}
private static <T> String ts(T[] arr) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: arr) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}");
return ret.toString();
}
private static <T> String ts(Iterable<T> iter) {
StringBuilder ret = new StringBuilder();
ret.append("{");
boolean first = true;
for(T t: iter) {
if(!first) {
ret.append(", ");
}
first = false;
ret.append(ts(t));
}
ret.append("}\n");
return ret.toString();
}
public static void dbg(PrintStream ps,Object... o) throws Exception {
if(LOCAL) {
System.setErr(ps);
System.err.print("Line #"+Thread.currentThread().getStackTrace()[2].getLineNumber()+": [\n");
for(int i = 0; i<o.length; i++) {
if(i!=0) {
System.err.print(", ");
}
System.err.print(ts(o[i]));
}
System.err.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 8 | 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 | 89b58e13dfb549f7e0f5ae1d3ff8718e | 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.math.BigInteger;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class C_770
{
public static final long[] POWER2 = generatePOWER2();
public static final IteratorBuffer<Long> ITERATOR_BUFFER_PRIME = new IteratorBuffer<>(streamPrime(1000000).iterator());
public static long BIG = 1000000000 + 7;
private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer stringTokenizer = null;
private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static class Array<Type>
implements Iterable<Type>
{
private final Object[] array;
public Array(int size)
{
this.array = new Object[size];
}
public Array(int size, Type element)
{
this(size);
Arrays.fill(this.array, element);
}
public Array(Array<Type> array, Type element)
{
this(array.size() + 1);
for (int index = 0; index < array.size(); index++)
{
set(index, array.get(index));
}
set(size() - 1, element);
}
public Array(List<Type> list)
{
this(list.size());
int index = 0;
for (Type element: list)
{
set(index, element);
index += 1;
}
}
public Type get(int index)
{
return (Type) this.array[index];
}
@Override
public Iterator<Type> iterator()
{
return new Iterator<Type>()
{
int index = 0;
@Override
public boolean hasNext()
{
return this.index < size();
}
@Override
public Type next()
{
Type result = Array.this.get(index);
index += 1;
return result;
}
};
}
public Array set(int index, Type value)
{
this.array[index] = value;
return this;
}
public int size()
{
return this.array.length;
}
public List<Type> toList()
{
List<Type> result = new ArrayList<>();
for (Type element: this)
{
result.add(element);
}
return result;
}
@Override
public String toString()
{
return "[" + C_770.toString(this, ", ") + "]";
}
}
static class BIT
{
private static int lastBit(int index)
{
return index & -index;
}
private final long[] tree;
public BIT(int size)
{
this.tree = new long[size];
}
public void add(int index, long delta)
{
index += 1;
while (index <= this.tree.length)
{
tree[index - 1] += delta;
index += lastBit(index);
}
}
public long prefix(int end)
{
long result = 0;
while (end > 0)
{
result += this.tree[end - 1];
end -= lastBit(end);
}
return result;
}
public int size()
{
return this.tree.length;
}
public long sum(int start, int end)
{
return prefix(end) - prefix(start);
}
}
static abstract class Edge<TypeVertex extends Vertex<TypeVertex, TypeEdge>, TypeEdge extends Edge<TypeVertex, TypeEdge>>
{
public final TypeVertex vertex0;
public final TypeVertex vertex1;
public final boolean bidirectional;
public Edge(TypeVertex vertex0, TypeVertex vertex1, boolean bidirectional)
{
this.vertex0 = vertex0;
this.vertex1 = vertex1;
this.bidirectional = bidirectional;
this.vertex0.edges.add(getThis());
if (this.bidirectional)
{
this.vertex1.edges.add(getThis());
}
}
public abstract TypeEdge getThis();
public TypeVertex other(Vertex<TypeVertex, TypeEdge> vertex)
{
TypeVertex result;
if (vertex0 == vertex)
{
result = vertex1;
}
else
{
result = vertex0;
}
return result;
}
public void remove()
{
this.vertex0.edges.remove(getThis());
if (this.bidirectional)
{
this.vertex1.edges.remove(getThis());
}
}
@Override
public String toString()
{
return this.vertex0 + "->" + this.vertex1;
}
}
public static class EdgeDefault<TypeVertex extends Vertex<TypeVertex, EdgeDefault<TypeVertex>>>
extends Edge<TypeVertex, EdgeDefault<TypeVertex>>
{
public EdgeDefault(TypeVertex vertex0, TypeVertex vertex1, boolean bidirectional)
{
super(vertex0, vertex1, bidirectional);
}
@Override
public EdgeDefault<TypeVertex> getThis()
{
return this;
}
}
public static class EdgeDefaultDefault
extends Edge<VertexDefaultDefault, EdgeDefaultDefault>
{
public EdgeDefaultDefault(VertexDefaultDefault vertex0, VertexDefaultDefault vertex1, boolean bidirectional)
{
super(vertex0, vertex1, bidirectional);
}
@Override
public EdgeDefaultDefault getThis()
{
return this;
}
}
public static class FIFO<Type>
{
public SingleLinkedList<Type> start;
public SingleLinkedList<Type> end;
public FIFO()
{
this.start = null;
this.end = null;
}
public boolean isEmpty()
{
return this.start == null;
}
public Type peek()
{
return this.start.element;
}
public Type pop()
{
Type result = this.start.element;
this.start = this.start.next;
return result;
}
public void push(Type element)
{
SingleLinkedList<Type> list = new SingleLinkedList<>(element, null);
if (this.start == null)
{
this.start = list;
this.end = list;
}
else
{
this.end.next = list;
this.end = list;
}
}
}
static class Fraction
implements Comparable<Fraction>
{
public static final Fraction ZERO = new Fraction(0, 1);
public static Fraction fraction(long whole)
{
return fraction(whole, 1);
}
public static Fraction fraction(long numerator, long denominator)
{
Fraction result;
if (denominator == 0)
{
throw new ArithmeticException();
}
if (numerator == 0)
{
result = Fraction.ZERO;
}
else
{
int sign;
if (numerator < 0 ^ denominator < 0)
{
sign = -1;
numerator = Math.abs(numerator);
denominator = Math.abs(denominator);
}
else
{
sign = 1;
}
long gcd = gcd(numerator, denominator);
result = new Fraction(sign * numerator / gcd, denominator / gcd);
}
return result;
}
public final long numerator;
public final long denominator;
private Fraction(long numerator, long denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
public Fraction add(Fraction fraction)
{
return fraction(this.numerator * fraction.denominator + fraction.numerator * this.denominator, this.denominator * fraction.denominator);
}
@Override
public int compareTo(Fraction that)
{
return Long.compare(this.numerator * that.denominator, that.numerator * this.denominator);
}
public Fraction divide(Fraction fraction)
{
return multiply(fraction.inverse());
}
public boolean equals(Fraction that)
{
return this.compareTo(that) == 0;
}
public boolean equals(Object that)
{
return this.compareTo((Fraction) that) == 0;
}
public Fraction getRemainder()
{
return fraction(this.numerator - getWholePart() * denominator, denominator);
}
public long getWholePart()
{
return this.numerator / this.denominator;
}
public Fraction inverse()
{
return fraction(this.denominator, this.numerator);
}
public Fraction multiply(Fraction fraction)
{
return fraction(this.numerator * fraction.numerator, this.denominator * fraction.denominator);
}
public Fraction neg()
{
return fraction(-this.numerator, this.denominator);
}
public Fraction sub(Fraction fraction)
{
return add(fraction.neg());
}
@Override
public String toString()
{
String result;
if (getRemainder().equals(Fraction.ZERO))
{
result = "" + this.numerator;
}
else
{
result = this.numerator + "/" + this.denominator;
}
return result;
}
}
static class IteratorBuffer<Type>
{
private Iterator<Type> iterator;
private List<Type> list;
public IteratorBuffer(Iterator<Type> iterator)
{
this.iterator = iterator;
this.list = new ArrayList<Type>();
}
public Iterator<Type> iterator()
{
return new Iterator<Type>()
{
int index = 0;
@Override
public boolean hasNext()
{
return this.index < list.size() || IteratorBuffer.this.iterator.hasNext();
}
@Override
public Type next()
{
if (list.size() <= this.index)
{
list.add(iterator.next());
}
Type result = list.get(index);
index += 1;
return result;
}
};
}
}
public static class MapCount<Type>
extends SortedMapAVL<Type, Long>
{
private int count;
public MapCount(Comparator<? super Type> comparator)
{
super(comparator);
this.count = 0;
}
public long add(Type key, Long delta)
{
long result;
if (delta > 0)
{
Long value = get(key);
if (value == null)
{
value = delta;
}
else
{
value += delta;
}
put(key, value);
result = delta;
}
else
{
result = 0;
}
this.count += result;
return result;
}
public int count()
{
return this.count;
}
public List<Type> flatten()
{
List<Type> result = new ArrayList<>();
for (Entry<Type, Long> entry: entrySet())
{
for (long index = 0; index < entry.getValue(); index++)
{
result.add(entry.getKey());
}
}
return result;
}
@Override
public SortedMapAVL<Type, Long> headMap(Type keyEnd)
{
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map<? extends Type, ? extends Long> map)
{
throw new UnsupportedOperationException();
}
public long remove(Type key, Long delta)
{
long result;
if (delta > 0)
{
Long value = get(key) - delta;
if (value <= 0)
{
result = delta + value;
remove(key);
}
else
{
result = delta;
put(key, value);
}
}
else
{
result = 0;
}
this.count -= result;
return result;
}
@Override
public Long remove(Object key)
{
Long result = super.remove(key);
this.count -= result;
return result;
}
@Override
public SortedMapAVL<Type, Long> subMap(Type keyStart, Type keyEnd)
{
throw new UnsupportedOperationException();
}
@Override
public SortedMapAVL<Type, Long> tailMap(Type keyStart)
{
throw new UnsupportedOperationException();
}
}
public static class MapSet<TypeKey, TypeValue>
extends SortedMapAVL<TypeKey, SortedSetAVL<TypeValue>>
implements Iterable<TypeValue>
{
private Comparator<? super TypeValue> comparatorValue;
public MapSet(Comparator<? super TypeKey> comparatorKey, Comparator<? super TypeValue> comparatorValue)
{
super(comparatorKey);
this.comparatorValue = comparatorValue;
}
public MapSet(Comparator<? super TypeKey> comparatorKey, SortedSetAVL<Entry<TypeKey, SortedSetAVL<TypeValue>>> entrySet, Comparator<? super TypeValue> comparatorValue)
{
super(comparatorKey, entrySet);
this.comparatorValue = comparatorValue;
}
public boolean add(TypeKey key, TypeValue value)
{
SortedSetAVL<TypeValue> set = computeIfAbsent(key, k -> new SortedSetAVL<>(comparatorValue));
return set.add(value);
}
public TypeValue firstValue()
{
TypeValue result;
Entry<TypeKey, SortedSetAVL<TypeValue>> firstEntry = firstEntry();
if (firstEntry == null)
{
result = null;
}
else
{
result = firstEntry.getValue().first();
}
return result;
}
@Override
public MapSet<TypeKey, TypeValue> headMap(TypeKey keyEnd)
{
return new MapSet<>(this.comparator, this.entrySet.headSet(new AbstractMap.SimpleEntry<>(keyEnd, null)), this.comparatorValue);
}
public Iterator<TypeValue> iterator()
{
return new Iterator<TypeValue>()
{
Iterator<SortedSetAVL<TypeValue>> iteratorValues = values().iterator();
Iterator<TypeValue> iteratorValue = null;
@Override
public boolean hasNext()
{
return iteratorValues.hasNext() || (iteratorValue != null && iteratorValue.hasNext());
}
@Override
public TypeValue next()
{
if (iteratorValue == null || !iteratorValue.hasNext())
{
iteratorValue = iteratorValues.next().iterator();
}
return iteratorValue.next();
}
};
}
public TypeValue lastValue()
{
TypeValue result;
Entry<TypeKey, SortedSetAVL<TypeValue>> lastEntry = lastEntry();
if (lastEntry == null)
{
result = null;
}
else
{
result = lastEntry.getValue().last();
}
return result;
}
public boolean removeSet(TypeKey key, TypeValue value)
{
boolean result;
SortedSetAVL<TypeValue> set = get(key);
if (set == null)
{
result = false;
}
else
{
result = set.remove(value);
if (set.size() == 0)
{
remove(key);
}
}
return result;
}
@Override
public MapSet<TypeKey, TypeValue> tailMap(TypeKey keyStart)
{
return new MapSet<>(this.comparator, this.entrySet.tailSet(new AbstractMap.SimpleEntry<>(keyStart, null)), this.comparatorValue);
}
}
public static class Matrix
{
public final int rows;
public final int columns;
public final Fraction[][] cells;
public Matrix(int rows, int columns)
{
this.rows = rows;
this.columns = columns;
this.cells = new Fraction[rows][columns];
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
set(row, column, Fraction.ZERO);
}
}
}
public void add(int rowSource, int rowTarget, Fraction fraction)
{
for (int column = 0; column < columns; column++)
{
this.cells[rowTarget][column] = this.cells[rowTarget][column].add(this.cells[rowSource][column].multiply(fraction));
}
}
private int columnPivot(int row)
{
int result = this.columns;
for (int column = this.columns - 1; 0 <= column; column--)
{
if (this.cells[row][column].compareTo(Fraction.ZERO) != 0)
{
result = column;
}
}
return result;
}
public void reduce()
{
for (int rowMinimum = 0; rowMinimum < this.rows; rowMinimum++)
{
int rowPivot = rowPivot(rowMinimum);
if (rowPivot != -1)
{
int columnPivot = columnPivot(rowPivot);
Fraction current = this.cells[rowMinimum][columnPivot];
Fraction pivot = this.cells[rowPivot][columnPivot];
Fraction fraction = pivot.inverse().sub(current.divide(pivot));
add(rowPivot, rowMinimum, fraction);
for (int row = rowMinimum + 1; row < this.rows; row++)
{
if (columnPivot(row) == columnPivot)
{
add(rowMinimum, row, this.cells[row][columnPivot(row)].neg());
}
}
}
}
}
private int rowPivot(int rowMinimum)
{
int result = -1;
int pivotColumnMinimum = this.columns;
for (int row = rowMinimum; row < this.rows; row++)
{
int pivotColumn = columnPivot(row);
if (pivotColumn < pivotColumnMinimum)
{
result = row;
pivotColumnMinimum = pivotColumn;
}
}
return result;
}
public void set(int row, int column, Fraction value)
{
this.cells[row][column] = value;
}
public String toString()
{
String result = "";
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
result += this.cells[row][column] + "\t";
}
result += "\n";
}
return result;
}
}
public static class Node<Type>
{
public static <Type> Node<Type> balance(Node<Type> result)
{
while (result != null && 1 < Math.abs(height(result.left) - height(result.right)))
{
if (height(result.left) < height(result.right))
{
Node<Type> right = result.right;
if (height(right.right) < height(right.left))
{
result = new Node<>(result.value, result.left, right.rotateRight());
}
result = result.rotateLeft();
}
else
{
Node<Type> left = result.left;
if (height(left.left) < height(left.right))
{
result = new Node<>(result.value, left.rotateLeft(), result.right);
}
result = result.rotateRight();
}
}
return result;
}
public static <Type> Node<Type> clone(Node<Type> result)
{
if (result != null)
{
result = new Node<>(result.value, clone(result.left), clone(result.right));
}
return result;
}
public static <Type> Node<Type> delete(Node<Type> node, Type value, Comparator<? super Type> comparator)
{
Node<Type> result;
if (node == null)
{
result = null;
}
else
{
int compare = comparator.compare(value, node.value);
if (compare == 0)
{
if (node.left == null)
{
result = node.right;
}
else
{
if (node.right == null)
{
result = node.left;
}
else
{
Node<Type> first = first(node.right);
result = new Node<>(first.value, node.left, delete(node.right, first.value, comparator));
}
}
}
else
{
if (compare < 0)
{
result = new Node<>(node.value, delete(node.left, value, comparator), node.right);
}
else
{
result = new Node<>(node.value, node.left, delete(node.right, value, comparator));
}
}
result = balance(result);
}
return result;
}
public static <Type> Node<Type> first(Node<Type> result)
{
while (result.left != null)
{
result = result.left;
}
return result;
}
public static <Type> Node<Type> get(Node<Type> node, Type value, Comparator<? super Type> comparator)
{
Node<Type> result;
if (node == null)
{
result = null;
}
else
{
int compare = comparator.compare(value, node.value);
if (compare == 0)
{
result = node;
}
else
{
if (compare < 0)
{
result = get(node.left, value, comparator);
}
else
{
result = get(node.right, value, comparator);
}
}
}
return result;
}
public static <Type> Node<Type> head(Node<Type> node, Type value, Comparator<? super Type> comparator)
{
Node<Type> result;
if (node == null)
{
result = null;
}
else
{
int compare = comparator.compare(value, node.value);
if (compare == 0)
{
result = node.left;
}
else
{
if (compare < 0)
{
result = head(node.left, value, comparator);
}
else
{
result = new Node<>(node.value, node.left, head(node.right, value, comparator));
}
}
result = balance(result);
}
return result;
}
public static int height(Node node)
{
return node == null ? 0 : node.height;
}
public static <Type> Node<Type> insert(Node<Type> node, Type value, Comparator<? super Type> comparator)
{
Node<Type> result;
if (node == null)
{
result = new Node<>(value, null, null);
}
else
{
int compare = comparator.compare(value, node.value);
if (compare == 0)
{
result = new Node<>(value, node.left, node.right);
;
}
else
{
if (compare < 0)
{
result = new Node<>(node.value, insert(node.left, value, comparator), node.right);
}
else
{
result = new Node<>(node.value, node.left, insert(node.right, value, comparator));
}
}
result = balance(result);
}
return result;
}
public static <Type> Node<Type> last(Node<Type> result)
{
while (result.right != null)
{
result = result.right;
}
return result;
}
public static int size(Node node)
{
return node == null ? 0 : node.size;
}
public static <Type> Node<Type> tail(Node<Type> node, Type value, Comparator<? super Type> comparator)
{
Node<Type> result;
if (node == null)
{
result = null;
}
else
{
int compare = comparator.compare(value, node.value);
if (compare == 0)
{
result = new Node<>(node.value, null, node.right);
}
else
{
if (compare < 0)
{
result = new Node<>(node.value, tail(node.left, value, comparator), node.right);
}
else
{
result = tail(node.right, value, comparator);
}
}
result = balance(result);
}
return result;
}
public static <Type> void traverseOrderIn(Node<Type> node, Consumer<Type> consumer)
{
if (node != null)
{
traverseOrderIn(node.left, consumer);
consumer.accept(node.value);
traverseOrderIn(node.right, consumer);
}
}
public final Type value;
public final Node<Type> left;
public final Node<Type> right;
public final int size;
private final int height;
public Node(Type value, Node<Type> left, Node<Type> right)
{
this.value = value;
this.left = left;
this.right = right;
this.size = 1 + size(left) + size(right);
this.height = 1 + Math.max(height(left), height(right));
}
public Node<Type> rotateLeft()
{
Node<Type> left = new Node<>(this.value, this.left, this.right.left);
return new Node<>(this.right.value, left, this.right.right);
}
public Node<Type> rotateRight()
{
Node<Type> right = new Node<>(this.value, this.left.right, this.right);
return new Node<>(this.left.value, this.left.left, right);
}
}
public static class SingleLinkedList<Type>
{
public final Type element;
public SingleLinkedList<Type> next;
public SingleLinkedList(Type element, SingleLinkedList<Type> next)
{
this.element = element;
this.next = next;
}
public void toCollection(Collection<Type> collection)
{
if (this.next != null)
{
this.next.toCollection(collection);
}
collection.add(this.element);
}
}
public static class SmallSetIntegers
{
public static final int SIZE = 20;
public static final int[] SET = generateSet();
public static final int[] COUNT = generateCount();
public static final int[] INTEGER = generateInteger();
private static int count(int set)
{
int result = 0;
for (int integer = 0; integer < SIZE; integer++)
{
if (0 < (set & set(integer)))
{
result += 1;
}
}
return result;
}
private static final int[] generateCount()
{
int[] result = new int[1 << SIZE];
for (int set = 0; set < result.length; set++)
{
result[set] = count(set);
}
return result;
}
private static final int[] generateInteger()
{
int[] result = new int[1 << SIZE];
Arrays.fill(result, -1);
for (int integer = 0; integer < SIZE; integer++)
{
result[SET[integer]] = integer;
}
return result;
}
private static final int[] generateSet()
{
int[] result = new int[SIZE];
for (int integer = 0; integer < result.length; integer++)
{
result[integer] = set(integer);
}
return result;
}
private static int set(int integer)
{
return 1 << integer;
}
}
public static class SortedMapAVL<TypeKey, TypeValue>
implements SortedMap<TypeKey, TypeValue>
{
public final Comparator<? super TypeKey> comparator;
public final SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet;
public SortedMapAVL(Comparator<? super TypeKey> comparator)
{
this(comparator, new SortedSetAVL<>((entry0, entry1) -> comparator.compare(entry0.getKey(), entry1.getKey())));
}
private SortedMapAVL(Comparator<? super TypeKey> comparator, SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet)
{
this.comparator = comparator;
this.entrySet = entrySet;
}
@Override
public void clear()
{
this.entrySet.clear();
}
@Override
public Comparator<? super TypeKey> comparator()
{
return this.comparator;
}
@Override
public boolean containsKey(Object key)
{
return this.entrySet().contains(new AbstractMap.SimpleEntry<>((TypeKey) key, null));
}
@Override
public boolean containsValue(Object value)
{
throw new UnsupportedOperationException();
}
@Override
public SortedSetAVL<Entry<TypeKey, TypeValue>> entrySet()
{
return this.entrySet;
}
public Entry<TypeKey, TypeValue> firstEntry()
{
return this.entrySet.first();
}
@Override
public TypeKey firstKey()
{
return firstEntry().getKey();
}
@Override
public TypeValue get(Object key)
{
Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>((TypeKey) key, null);
entry = this.entrySet.get(entry);
return entry == null ? null : entry.getValue();
}
@Override
public SortedMapAVL<TypeKey, TypeValue> headMap(TypeKey keyEnd)
{
return new SortedMapAVL<>(this.comparator, this.entrySet.headSet(new AbstractMap.SimpleEntry<>(keyEnd, null)));
}
@Override
public boolean isEmpty()
{
return this.entrySet.isEmpty();
}
@Override
public Set<TypeKey> keySet()
{
return new SortedSet<TypeKey>()
{
@Override
public boolean add(TypeKey typeKey)
{
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends TypeKey> collection)
{
throw new UnsupportedOperationException();
}
@Override
public void clear()
{
throw new UnsupportedOperationException();
}
@Override
public Comparator<? super TypeKey> comparator()
{
throw new UnsupportedOperationException();
}
@Override
public boolean contains(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
@Override
public TypeKey first()
{
throw new UnsupportedOperationException();
}
@Override
public SortedSet<TypeKey> headSet(TypeKey typeKey)
{
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty()
{
return size() == 0;
}
@Override
public Iterator<TypeKey> iterator()
{
final Iterator<Entry<TypeKey, TypeValue>> iterator = SortedMapAVL.this.entrySet.iterator();
return new Iterator<TypeKey>()
{
@Override
public boolean hasNext()
{
return iterator.hasNext();
}
@Override
public TypeKey next()
{
return iterator.next().getKey();
}
};
}
@Override
public TypeKey last()
{
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
@Override
public int size()
{
return SortedMapAVL.this.entrySet.size();
}
@Override
public SortedSet<TypeKey> subSet(TypeKey typeKey, TypeKey e1)
{
throw new UnsupportedOperationException();
}
@Override
public SortedSet<TypeKey> tailSet(TypeKey typeKey)
{
throw new UnsupportedOperationException();
}
@Override
public Object[] toArray()
{
throw new UnsupportedOperationException();
}
@Override
public <T> T[] toArray(T[] ts)
{
throw new UnsupportedOperationException();
}
};
}
public Entry<TypeKey, TypeValue> lastEntry()
{
return this.entrySet.last();
}
@Override
public TypeKey lastKey()
{
return lastEntry().getKey();
}
@Override
public TypeValue put(TypeKey key, TypeValue value)
{
TypeValue result = get(key);
Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>(key, value);
this.entrySet().add(entry);
return result;
}
@Override
public void putAll(Map<? extends TypeKey, ? extends TypeValue> map)
{
map.entrySet()
.forEach(entry -> put(entry.getKey(), entry.getValue()));
}
@Override
public TypeValue remove(Object key)
{
TypeValue result = get(key);
Entry<TypeKey, TypeValue> entry = new AbstractMap.SimpleEntry<>((TypeKey) key, null);
this.entrySet.remove(entry);
return result;
}
@Override
public int size()
{
return this.entrySet().size();
}
@Override
public SortedMapAVL<TypeKey, TypeValue> subMap(TypeKey keyStart, TypeKey keyEnd)
{
return new SortedMapAVL<>(this.comparator, this.entrySet.subSet(new AbstractMap.SimpleEntry<>(keyStart, null), new AbstractMap.SimpleEntry<>(keyEnd, null)));
}
@Override
public SortedMapAVL<TypeKey, TypeValue> tailMap(TypeKey keyStart)
{
return new SortedMapAVL<>(this.comparator, this.entrySet.tailSet(new AbstractMap.SimpleEntry<>(keyStart, null)));
}
@Override
public String toString()
{
return this.entrySet().toString();
}
@Override
public Collection<TypeValue> values()
{
return new Collection<TypeValue>()
{
@Override
public boolean add(TypeValue typeValue)
{
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends TypeValue> collection)
{
throw new UnsupportedOperationException();
}
@Override
public void clear()
{
throw new UnsupportedOperationException();
}
@Override
public boolean contains(Object value)
{
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty()
{
return SortedMapAVL.this.entrySet.isEmpty();
}
@Override
public Iterator<TypeValue> iterator()
{
return new Iterator<TypeValue>()
{
Iterator<Entry<TypeKey, TypeValue>> iterator = SortedMapAVL.this.entrySet.iterator();
@Override
public boolean hasNext()
{
return this.iterator.hasNext();
}
@Override
public TypeValue next()
{
return this.iterator.next().getValue();
}
};
}
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException();
}
@Override
public int size()
{
return SortedMapAVL.this.entrySet.size();
}
@Override
public Object[] toArray()
{
throw new UnsupportedOperationException();
}
@Override
public <T> T[] toArray(T[] ts)
{
throw new UnsupportedOperationException();
}
};
}
}
public static class SortedSetAVL<Type>
implements SortedSet<Type>
{
public Comparator<? super Type> comparator;
public Node<Type> root;
private SortedSetAVL(Comparator<? super Type> comparator, Node<Type> root)
{
this.comparator = comparator;
this.root = root;
}
public SortedSetAVL(Comparator<? super Type> comparator)
{
this(comparator, null);
}
public SortedSetAVL(Collection<? extends Type> collection, Comparator<? super Type> comparator)
{
this(comparator, null);
this.addAll(collection);
}
public SortedSetAVL(SortedSetAVL<Type> sortedSetAVL)
{
this(sortedSetAVL.comparator, Node.clone(sortedSetAVL.root));
}
@Override
public boolean add(Type value)
{
int sizeBefore = size();
this.root = Node.insert(this.root, value, this.comparator);
return sizeBefore != size();
}
@Override
public boolean addAll(Collection<? extends Type> collection)
{
return collection.stream()
.map(this::add)
.reduce(true, (x, y) -> x | y);
}
@Override
public void clear()
{
this.root = null;
}
@Override
public Comparator<? super Type> comparator()
{
return this.comparator;
}
@Override
public boolean contains(Object value)
{
return Node.get(this.root, (Type) value, this.comparator) != null;
}
@Override
public boolean containsAll(Collection<?> collection)
{
return collection.stream()
.allMatch(this::contains);
}
@Override
public Type first()
{
return Node.first(this.root).value;
}
public Type get(Type value)
{
Node<Type> node = Node.get(this.root, value, this.comparator);
return node == null ? null : node.value;
}
@Override
public SortedSetAVL<Type> headSet(Type valueEnd)
{
return new SortedSetAVL<>(this.comparator, Node.head(this.root, valueEnd, this.comparator));
}
@Override
public boolean isEmpty()
{
return this.root == null;
}
@Override
public Iterator<Type> iterator()
{
Stack<Node<Type>> path = new Stack<>();
return new Iterator<Type>()
{
{
push(SortedSetAVL.this.root);
}
@Override
public boolean hasNext()
{
return !path.isEmpty();
}
@Override
public Type next()
{
if (path.isEmpty())
{
throw new NoSuchElementException();
}
else
{
Node<Type> node = path.peek();
Type result = node.value;
if (node.right != null)
{
push(node.right);
}
else
{
do
{
node = path.pop();
}
while (!path.isEmpty() && path.peek().right == node);
}
return result;
}
}
public void push(Node<Type> node)
{
while (node != null)
{
path.push(node);
node = node.left;
}
}
};
}
@Override
public Type last()
{
return Node.last(this.root).value;
}
@Override
public boolean remove(Object value)
{
int sizeBefore = size();
this.root = Node.delete(this.root, (Type) value, this.comparator);
return sizeBefore != size();
}
@Override
public boolean removeAll(Collection<?> collection)
{
return collection.stream()
.map(this::remove)
.reduce(true, (x, y) -> x | y);
}
@Override
public boolean retainAll(Collection<?> collection)
{
SortedSetAVL<Type> set = new SortedSetAVL<>(this.comparator);
collection.stream()
.map(element -> (Type) element)
.filter(this::contains)
.forEach(set::add);
boolean result = size() != set.size();
this.root = set.root;
return result;
}
@Override
public int size()
{
return this.root == null ? 0 : this.root.size;
}
@Override
public SortedSetAVL<Type> subSet(Type valueStart, Type valueEnd)
{
return tailSet(valueStart).headSet(valueEnd);
}
@Override
public SortedSetAVL<Type> tailSet(Type valueStart)
{
return new SortedSetAVL<>(this.comparator, Node.tail(this.root, valueStart, this.comparator));
}
@Override
public Object[] toArray()
{
return toArray(new Object[0]);
}
@Override
public <T> T[] toArray(T[] ts)
{
List<Object> list = new ArrayList<>();
Node.traverseOrderIn(this.root, list::add);
return list.toArray(ts);
}
@Override
public String toString()
{
return "{" + C_770.toString(this, ", ") + "}";
}
}
public static class Tree2D
{
public static final int SIZE = 1 << 30;
public static final Tree2D[] TREES_NULL = new Tree2D[]{null, null, null, null};
public static boolean contains(int x, int y, int left, int bottom, int size)
{
return left <= x && x < left + size && bottom <= y && y < bottom + size;
}
public static int count(Tree2D[] trees)
{
int result = 0;
for (int index = 0; index < 4; index++)
{
if (trees[index] != null)
{
result += trees[index].count;
}
}
return result;
}
public static int count
(
int rectangleLeft,
int rectangleBottom,
int rectangleRight,
int rectangleTop,
Tree2D tree,
int left,
int bottom,
int size
)
{
int result;
if (tree == null)
{
result = 0;
}
else
{
int right = left + size;
int top = bottom + size;
int intersectionLeft = Math.max(rectangleLeft, left);
int intersectionBottom = Math.max(rectangleBottom, bottom);
int intersectionRight = Math.min(rectangleRight, right);
int intersectionTop = Math.min(rectangleTop, top);
if (intersectionRight <= intersectionLeft || intersectionTop <= intersectionBottom)
{
result = 0;
}
else
{
if (intersectionLeft == left && intersectionBottom == bottom && intersectionRight == right && intersectionTop == top)
{
result = tree.count;
}
else
{
size = size >> 1;
result = 0;
for (int index = 0; index < 4; index++)
{
result += count
(
rectangleLeft,
rectangleBottom,
rectangleRight,
rectangleTop,
tree.trees[index],
quadrantLeft(left, size, index),
quadrantBottom(bottom, size, index),
size
);
}
}
}
}
return result;
}
public static int quadrantBottom(int bottom, int size, int index)
{
return bottom + (index >> 1) * size;
}
public static int quadrantLeft(int left, int size, int index)
{
return left + (index & 1) * size;
}
public final Tree2D[] trees;
public final int count;
private Tree2D(Tree2D[] trees, int count)
{
this.trees = trees;
this.count = count;
}
public Tree2D(Tree2D[] trees)
{
this(trees, count(trees));
}
public Tree2D()
{
this(TREES_NULL);
}
public int count(int rectangleLeft, int rectangleBottom, int rectangleRight, int rectangleTop)
{
return count
(
rectangleLeft,
rectangleBottom,
rectangleRight,
rectangleTop,
this,
0,
0,
SIZE
);
}
public Tree2D setPoint
(
int x,
int y,
Tree2D tree,
int left,
int bottom,
int size
)
{
Tree2D result;
if (contains(x, y, left, bottom, size))
{
if (size == 1)
{
result = new Tree2D(TREES_NULL, 1);
}
else
{
size = size >> 1;
Tree2D[] trees = new Tree2D[4];
for (int index = 0; index < 4; index++)
{
trees[index] = setPoint
(
x,
y,
tree == null ? null : tree.trees[index],
quadrantLeft(left, size, index),
quadrantBottom(bottom, size, index),
size
);
}
result = new Tree2D(trees);
}
}
else
{
result = tree;
}
return result;
}
public Tree2D setPoint(int x, int y)
{
return setPoint
(
x,
y,
this,
0,
0,
SIZE
);
}
}
public static class Tuple2<Type0, Type1>
{
public final Type0 v0;
public final Type1 v1;
public Tuple2(Type0 v0, Type1 v1)
{
this.v0 = v0;
this.v1 = v1;
}
@Override
public String toString()
{
return "(" + this.v0 + ", " + this.v1 + ")";
}
}
public static class Tuple2Comparable<Type0 extends Comparable<? super Type0>, Type1 extends Comparable<? super Type1>>
extends Tuple2<Type0, Type1>
implements Comparable<Tuple2Comparable<Type0, Type1>>
{
public Tuple2Comparable(Type0 v0, Type1 v1)
{
super(v0, v1);
}
@Override
public int compareTo(Tuple2Comparable<Type0, Type1> that)
{
int result = this.v0.compareTo(that.v0);
if (result == 0)
{
result = this.v1.compareTo(that.v1);
}
return result;
}
}
public static class Tuple3<Type0, Type1, Type2>
{
public final Type0 v0;
public final Type1 v1;
public final Type2 v2;
public Tuple3(Type0 v0, Type1 v1, Type2 v2)
{
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
}
@Override
public String toString()
{
return "(" + this.v0 + ", " + this.v1 + ", " + this.v2 + ")";
}
}
public static class Vertex
<
TypeVertex extends Vertex<TypeVertex, TypeEdge>,
TypeEdge extends Edge<TypeVertex, TypeEdge>
>
implements Comparable<Vertex<? super TypeVertex, ? super TypeEdge>>
{
public static <
TypeVertex extends Vertex<TypeVertex, TypeEdge>,
TypeEdge extends Edge<TypeVertex, TypeEdge>,
TypeResult
> TypeResult breadthFirstSearch
(
TypeVertex vertex,
TypeEdge edge,
BiFunctionResult<TypeVertex, TypeEdge, TypeResult> function,
Array<Boolean> visited,
FIFO<TypeVertex> verticesNext,
FIFO<TypeEdge> edgesNext,
TypeResult result
)
{
if (!visited.get(vertex.index))
{
visited.set(vertex.index, true);
result = function.apply(vertex, edge, result);
for (TypeEdge edgeNext: vertex.edges)
{
TypeVertex vertexNext = edgeNext.other(vertex);
if (!visited.get(vertexNext.index))
{
verticesNext.push(vertexNext);
edgesNext.push(edgeNext);
}
}
}
return result;
}
public static <
TypeVertex extends Vertex<TypeVertex, TypeEdge>,
TypeEdge extends Edge<TypeVertex, TypeEdge>,
TypeResult
>
TypeResult breadthFirstSearch
(
Array<TypeVertex> vertices,
int indexVertexStart,
BiFunctionResult<TypeVertex, TypeEdge, TypeResult> function,
TypeResult result
)
{
Array<Boolean> visited = new Array<>(vertices.size(), false);
FIFO<TypeVertex> verticesNext = new FIFO<>();
verticesNext.push(vertices.get(indexVertexStart));
FIFO<TypeEdge> edgesNext = new FIFO<>();
edgesNext.push(null);
while (!verticesNext.isEmpty())
{
result = breadthFirstSearch(verticesNext.pop(), edgesNext.pop(), function, visited, verticesNext, edgesNext, result);
}
return result;
}
public static <
TypeVertex extends Vertex<TypeVertex, TypeEdge>,
TypeEdge extends Edge<TypeVertex, TypeEdge>
>
boolean
cycle
(
TypeVertex start,
SortedSet<TypeVertex> result
)
{
boolean cycle = false;
Stack<TypeVertex> stackVertex = new Stack<>();
Stack<TypeEdge> stackEdge = new Stack<>();
stackVertex.push(start);
stackEdge.push(null);
while (!stackVertex.isEmpty())
{
TypeVertex vertex = stackVertex.pop();
TypeEdge edge = stackEdge.pop();
if (!result.contains(vertex))
{
result.add(vertex);
for (TypeEdge otherEdge: vertex.edges)
{
if (otherEdge != edge)
{
TypeVertex otherVertex = otherEdge.other(vertex);
if (result.contains(otherVertex))
{
cycle = true;
}
else
{
stackVertex.push(otherVertex);
stackEdge.push(otherEdge);
}
}
}
}
}
return cycle;
}
public static <
TypeVertex extends Vertex<TypeVertex, TypeEdge>,
TypeEdge extends Edge<TypeVertex, TypeEdge>
>
SortedSet<TypeVertex>
depthFirstSearch
(
TypeVertex start,
BiConsumer<TypeVertex, TypeEdge> functionVisitPre,
BiConsumer<TypeVertex, TypeEdge> functionVisitPost
)
{
SortedSet<TypeVertex> result = new SortedSetAVL<>(Comparator.naturalOrder());
Stack<TypeVertex> stackVertex = new Stack<>();
Stack<TypeEdge> stackEdge = new Stack<>();
stackVertex.push(start);
stackEdge.push(null);
while (!stackVertex.isEmpty())
{
TypeVertex vertex = stackVertex.pop();
TypeEdge edge = stackEdge.pop();
if (result.contains(vertex))
{
functionVisitPost.accept(vertex, edge);
}
else
{
result.add(vertex);
stackVertex.push(vertex);
stackEdge.push(edge);
functionVisitPre.accept(vertex, edge);
for (TypeEdge otherEdge: vertex.edges)
{
TypeVertex otherVertex = otherEdge.other(vertex);
if (!result.contains(otherVertex))
{
stackVertex.push(otherVertex);
stackEdge.push(otherEdge);
}
}
}
}
return result;
}
public static <
TypeVertex extends Vertex<TypeVertex, TypeEdge>,
TypeEdge extends Edge<TypeVertex, TypeEdge>
>
SortedSet<TypeVertex>
depthFirstSearch
(
TypeVertex start,
Consumer<TypeVertex> functionVisitPreVertex,
Consumer<TypeVertex> functionVisitPostVertex
)
{
BiConsumer<TypeVertex, TypeEdge> functionVisitPreVertexEdge = (vertex, edge) ->
{
functionVisitPreVertex.accept(vertex);
};
BiConsumer<TypeVertex, TypeEdge> functionVisitPostVertexEdge = (vertex, edge) ->
{
functionVisitPostVertex.accept(vertex);
};
return depthFirstSearch(start, functionVisitPreVertexEdge, functionVisitPostVertexEdge);
}
public final int index;
public final List<TypeEdge> edges;
public Vertex(int index)
{
this.index = index;
this.edges = new ArrayList<>();
}
@Override
public int compareTo(Vertex<? super TypeVertex, ? super TypeEdge> that)
{
return Integer.compare(this.index, that.index);
}
@Override
public String toString()
{
return "" + this.index;
}
}
public static class VertexDefault<TypeEdge extends Edge<VertexDefault<TypeEdge>, TypeEdge>>
extends Vertex<VertexDefault<TypeEdge>, TypeEdge>
{
public VertexDefault(int index)
{
super(index);
}
}
public static class VertexDefaultDefault
extends Vertex<VertexDefaultDefault, EdgeDefaultDefault>
{
public static Array<VertexDefaultDefault> vertices(int n)
{
Array<VertexDefaultDefault> result = new Array<>(n);
for (int index = 0; index < n; index++)
{
result.set(index, new VertexDefaultDefault(index));
}
return result;
}
public VertexDefaultDefault(int index)
{
super(index);
}
}
public static class Wrapper<Type>
{
public Type value;
public Wrapper(Type value)
{
this.value = value;
}
public Type get()
{
return this.value;
}
public void set(Type value)
{
this.value = value;
}
@Override
public String toString()
{
return this.value.toString();
}
}
public static void add(int delta, int[] result)
{
for (int index = 0; index < result.length; index++)
{
result[index] += delta;
}
}
public static void add(int delta, int[]... result)
{
for (int index = 0; index < result.length; index++)
{
add(delta, result[index]);
}
}
public static long add(long x, long y)
{
return (x + y) % BIG;
}
public static int binarySearchMaximum(Function<Integer, Boolean> filter, int start, int end)
{
return -binarySearchMinimum(x -> filter.apply(-x), -end, -start);
}
public static int binarySearchMinimum(Function<Integer, Boolean> filter, int start, int end)
{
int result;
if (start == end)
{
result = end;
}
else
{
int middle = start + (end - start) / 2;
if (filter.apply(middle))
{
result = binarySearchMinimum(filter, start, middle);
}
else
{
result = binarySearchMinimum(filter, middle + 1, end);
}
}
return result;
}
public static void close()
{
out.close();
}
private static void combinations(int n, int k, int start, SortedSet<Integer> combination, List<SortedSet<Integer>> result)
{
if (k == 0)
{
result.add(new SortedSetAVL<>(combination, Comparator.naturalOrder()));
}
else
{
for (int index = start; index < n; index++)
{
if (!combination.contains(index))
{
combination.add(index);
combinations(n, k - 1, index + 1, combination, result);
combination.remove(index);
}
}
}
}
public static List<SortedSet<Integer>> combinations(int n, int k)
{
List<SortedSet<Integer>> result = new ArrayList<>();
combinations(n, k, 0, new SortedSetAVL<>(Comparator.naturalOrder()), result);
return result;
}
public static <Type> int compare(Iterator<Type> iterator0, Iterator<Type> iterator1, Comparator<Type> comparator)
{
int result = 0;
while (result == 0 && iterator0.hasNext() && iterator1.hasNext())
{
result = comparator.compare(iterator0.next(), iterator1.next());
}
if (result == 0)
{
if (iterator1.hasNext())
{
result = -1;
}
else
{
if (iterator0.hasNext())
{
result = 1;
}
}
}
return result;
}
public static <Type> int compare(Iterable<Type> iterable0, Iterable<Type> iterable1, Comparator<Type> comparator)
{
return compare(iterable0.iterator(), iterable1.iterator(), comparator);
}
public static long divideCeil(long x, long y)
{
return (x + y - 1) / y;
}
public static Set<Long> divisors(long n)
{
SortedSetAVL<Long> result = new SortedSetAVL<>(Comparator.naturalOrder());
result.add(1L);
for (Long factor: factors(n))
{
SortedSetAVL<Long> divisors = new SortedSetAVL<>(result);
for (Long divisor: result)
{
divisors.add(divisor * factor);
}
result = divisors;
}
return result;
}
public static LinkedList<Long> factors(long n)
{
LinkedList<Long> result = new LinkedList<>();
Iterator<Long> primes = ITERATOR_BUFFER_PRIME.iterator();
Long prime;
while (n > 1 && (prime = primes.next()) * prime <= n)
{
while (n % prime == 0)
{
result.add(prime);
n /= prime;
}
}
if (n > 1)
{
result.add(n);
}
return result;
}
public static long faculty(int n)
{
long result = 1;
for (int index = 2; index <= n; index++)
{
result *= index;
}
return result;
}
static long gcd(long a, long b)
{
return b == 0 ? a : gcd(b, a % b);
}
public static long[] generatePOWER2()
{
long[] result = new long[63];
for (int x = 0; x < result.length; x++)
{
result[x] = 1L << x;
}
return result;
}
public static boolean isPrime(long x)
{
boolean result = x > 1;
Iterator<Long> iterator = ITERATOR_BUFFER_PRIME.iterator();
Long prime;
while ((prime = iterator.next()) * prime <= x)
{
result &= x % prime > 0;
}
return result;
}
public static long knapsack(List<Tuple3<Long, Integer, Integer>> itemsValueWeightCount, int weightMaximum)
{
long[] valuesMaximum = new long[weightMaximum + 1];
for (Tuple3<Long, Integer, Integer> itemValueWeightCount: itemsValueWeightCount)
{
long itemValue = itemValueWeightCount.v0;
int itemWeight = itemValueWeightCount.v1;
int itemCount = itemValueWeightCount.v2;
for (int weight = weightMaximum; 0 <= weight; weight--)
{
for (int index = 1; index <= itemCount && 0 <= weight - index * itemWeight; index++)
{
valuesMaximum[weight] = Math.max(valuesMaximum[weight], valuesMaximum[weight - index * itemWeight] + index * itemValue);
}
}
}
long result = 0;
for (long valueMaximum: valuesMaximum)
{
result = Math.max(result, valueMaximum);
}
return result;
}
public static boolean knapsackPossible(List<Tuple2<Integer, Integer>> itemsWeightCount, int weightMaximum)
{
boolean[] weightPossible = new boolean[weightMaximum + 1];
weightPossible[0] = true;
int weightLargest = 0;
for (Tuple2<Integer, Integer> itemWeightCount: itemsWeightCount)
{
int itemWeight = itemWeightCount.v0;
int itemCount = itemWeightCount.v1;
for (int weightStart = 0; weightStart < itemWeight; weightStart++)
{
int count = 0;
for (int weight = weightStart; weight <= weightMaximum && (0 < count || weight <= weightLargest); weight += itemWeight)
{
if (weightPossible[weight])
{
count = itemCount;
}
else
{
if (0 < count)
{
weightPossible[weight] = true;
weightLargest = weight;
count -= 1;
}
}
}
}
}
return weightPossible[weightMaximum];
}
public static long lcm(int a, int b)
{
return a * b / gcd(a, b);
}
public static void main(String[] args)
{
try
{
solve();
}
catch (IOException exception)
{
exception.printStackTrace();
}
close();
}
public static long mul(long x, long y)
{
return (x * y) % BIG;
}
public static double nextDouble()
throws IOException
{
return Double.parseDouble(nextString());
}
public static int nextInt()
throws IOException
{
return Integer.parseInt(nextString());
}
public static void nextInts(int n, int[]... result)
throws IOException
{
for (int index = 0; index < n; index++)
{
for (int value = 0; value < result.length; value++)
{
result[value][index] = nextInt();
}
}
}
public static int[] nextInts(int n)
throws IOException
{
int[] result = new int[n];
nextInts(n, result);
return result;
}
public static String nextLine()
throws IOException
{
return bufferedReader.readLine();
}
public static long nextLong()
throws IOException
{
return Long.parseLong(nextString());
}
public static void nextLongs(int n, long[]... result)
throws IOException
{
for (int index = 0; index < n; index++)
{
for (int value = 0; value < result.length; value++)
{
result[value][index] = nextLong();
}
}
}
public static long[] nextLongs(int n)
throws IOException
{
long[] result = new long[n];
nextLongs(n, result);
return result;
}
public static String nextString()
throws IOException
{
while ((stringTokenizer == null) || (!stringTokenizer.hasMoreTokens()))
{
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
}
return stringTokenizer.nextToken();
}
public static String[] nextStrings(int n)
throws IOException
{
String[] result = new String[n];
{
for (int index = 0; index < n; index++)
{
result[index] = nextString();
}
}
return result;
}
public static <T> List<T> permutation(long p, List<T> x)
{
List<T> copy = new ArrayList<>();
for (int index = 0; index < x.size(); index++)
{
copy.add(x.get(index));
}
List<T> result = new ArrayList<>();
for (int indexTo = 0; indexTo < x.size(); indexTo++)
{
int indexFrom = (int) p % copy.size();
p = p / copy.size();
result.add(copy.remove(indexFrom));
}
return result;
}
public static <Type> List<List<Type>> permutations(List<Type> list)
{
List<List<Type>> result = new ArrayList<>();
result.add(new ArrayList<>());
for (Type element: list)
{
List<List<Type>> permutations = result;
result = new ArrayList<>();
for (List<Type> permutation: permutations)
{
for (int index = 0; index <= permutation.size(); index++)
{
List<Type> permutationNew = new ArrayList<>(permutation);
permutationNew.add(index, element);
result.add(permutationNew);
}
}
}
return result;
}
public static long[][] sizeGroup2CombinationsCount(int maximum)
{
long[][] result = new long[maximum + 1][maximum + 1];
for (int length = 0; length <= maximum; length++)
{
result[length][0] = 1;
for (int group = 1; group <= length; group++)
{
result[length][group] = add(result[length - 1][group - 1], result[length - 1][group]);
}
}
return result;
}
public static Stream<BigInteger> streamFibonacci()
{
return Stream.generate(new Supplier<BigInteger>()
{
private BigInteger n0 = BigInteger.ZERO;
private BigInteger n1 = BigInteger.ONE;
@Override
public BigInteger get()
{
BigInteger result = n0;
n0 = n1;
n1 = result.add(n0);
return result;
}
});
}
public static Stream<Long> streamPrime(int sieveSize)
{
return Stream.generate(new Supplier<Long>()
{
private boolean[] isPrime = new boolean[sieveSize];
private long sieveOffset = 2;
private List<Long> primes = new ArrayList<>();
private int index = 0;
public void filter(long prime, boolean[] result)
{
if (prime * prime < this.sieveOffset + sieveSize)
{
long remainingStart = this.sieveOffset % prime;
long start = remainingStart == 0 ? 0 : prime - remainingStart;
for (long index = start; index < sieveSize; index += prime)
{
result[(int) index] = false;
}
}
}
public void generatePrimes()
{
Arrays.fill(this.isPrime, true);
this.primes.forEach(prime -> filter(prime, isPrime));
for (int index = 0; index < sieveSize; index++)
{
if (isPrime[index])
{
this.primes.add(this.sieveOffset + index);
filter(this.sieveOffset + index, isPrime);
}
}
this.sieveOffset += sieveSize;
}
@Override
public Long get()
{
while (this.primes.size() <= this.index)
{
generatePrimes();
}
Long result = this.primes.get(this.index);
this.index += 1;
return result;
}
});
}
public static <Type> String toString(Iterator<Type> iterator, String separator)
{
StringBuilder stringBuilder = new StringBuilder();
if (iterator.hasNext())
{
stringBuilder.append(iterator.next());
}
while (iterator.hasNext())
{
stringBuilder.append(separator);
stringBuilder.append(iterator.next());
}
return stringBuilder.toString();
}
public static <Type> String toString(Iterator<Type> iterator)
{
return toString(iterator, " ");
}
public static <Type> String toString(Iterable<Type> iterable, String separator)
{
return toString(iterable.iterator(), separator);
}
public static <Type> String toString(Iterable<Type> iterable)
{
return toString(iterable, " ");
}
public static long totient(long n)
{
Set<Long> factors = new SortedSetAVL<>(factors(n), Comparator.naturalOrder());
long result = n;
for (long p: factors)
{
result -= result / p;
}
return result;
}
interface BiFunctionResult<Type0, Type1, TypeResult>
{
TypeResult apply(Type0 x0, Type1 x1, TypeResult x2);
}
public static long lsb(long x)
{
return ((x ^ (x - 1)) + 1) >> 1;
}
public static long msb(long x)
{
long result = 1;
while (result <= x)
{
result = result << 1;
}
result = result >> 1;
return result;
}
public static void solve()
throws IOException
{
int tests = nextInt();
for (int test = 0; test < tests; test++)
{
int n = nextInt();
int k = nextInt();
if (k == 1)
{
out.println("YES");
for (int i = 1; i <= n; i++)
{
out.println(i);
}
}
else
{
if (n % 2 == 0)
{
out.println("YES");
for (int i = 0; i < n * k; i += 2 * k)
{
for (int j = 0; j < k; j++)
{
out.print(i + j * 2 + 1);
out.print(" ");
}
out.println();
for (int j = 0; j < k; j++)
{
out.print(i + j * 2 + 2);
out.print(" ");
}
out.println();
}
}
else
{
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 8 | 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 | fb0ee52132165ae6a3ee0d8a10f52736 | 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.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_C
{
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 void test() {
log("testing");
Random r=new Random();
int NTESTS=1000000;
int NMAX=22;
int VMAX=10;
for (int t=0;t<NTESTS;t++) {
}
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 void explore() {
int cur=1;
for (int u=2;u<100;u++) {
int p=pgcd(cur,u);
cur/=p;
cur*=u;
log(cur);
if (cur>500*(u+1)) {
log("End at u:"+u);
break;
}
}
}
static void check() {
for (int n=1;n<10;n++)
for (int k=1;k<10;k++) {
if ((n*k)%2==0) {
log("===========================");
int cur=2;
int[][] a=new int[n][k];
for (int i=0;i<n/2;i++) {
for (int j=0;j<k;j++) {
a[i][j]=cur;
cur+=2;
}
}
cur=1;
for (int i=0;i<n/2;i++) {
for (int j=0;j<k;j++) {
a[i+n/2][j]=cur;
cur+=2;
}
}
for (int i=0;i<n;i++)
log(a[i]);
for (int i=0;i<n;i++) {
for (int l=0;l<k;l++) {
int sum=0;
for (int r=l;r<k;r++) {
sum+=a[i][r];
int rem=sum%(r-l+1);
if (rem!=0) {
log("--------------Error");
return;
}
}
}
}
}
}
}
static void process() throws Exception {
out = new BufferedWriter(new OutputStreamWriter(System.out));
reader = new InputReader(System.in);
//explore();
log("check");
//check();
log("done");
int T=reader.readInt();
for (int t=0;t<T;t++) {
int n=reader.readInt();
int k=reader.readInt();
if (k==1) {
output("YES");
for (int i=0;i<n;i++) {
output((i+1)+" ");
}
} else
if (n==1){
output("NO");
}
else {
if (n%2!=0) {
output("NO");
} else {
output("YES");
int cur=2;
int[][] a=new int[n][k];
for (int i=0;i<n/2;i++) {
for (int j=0;j<k;j++) {
a[i][j]=cur;
outputWln(cur+" ");
cur+=2;
}
output("");
}
cur=1;
for (int i=n/2;i<n;i++) {
for (int j=0;j<k;j++) {
a[i][j]=cur;
outputWln(cur+" ");
cur+=2;
}
output("");
}
// check
/*
for (int i=0;i<n;i++) {
for (int l=0;l<k;l++) {
int sum=0;
for (int r=l;r<k;r++) {
sum+=a[i][r];
int rem=sum%(r-l+1);
if (rem!=0) {
log("Error");
break;
}
}
}
}
*/
}
}
}
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 | ["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 8 | 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 | e28e1169892b6465fb4b1b4e99357dbe | 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 Scanner sc=new Scanner(System.in);
static PrintWriter pw=new PrintWriter(System.out);
public static int rr() {
return (int)(7*Math.random())+1;
}
public static void main(String[] args) throws IOException, InterruptedException {
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int k=sc.nextInt();
int tot=n*k;
int even=tot/2,odd=(tot+1)/2;
if(even%k!=0 || odd%k!=0) {
pw.println("NO");
}
else {
TreeSet<Integer> ts1=new TreeSet<>(),ts2=new TreeSet<>();
for(int i=1;i<=n*k;i++) {
if(i%2==0)
ts1.add(i);
else ts2.add(i);
}
pw.println("YES");
for(int i=0;i<n;i++) {
StringBuilder ans=new StringBuilder();
if(i%2==1) {
for(int j=0;j<k;j++) {
ans.append(ts1.pollFirst()+" ");
}
}
else {
for(int j=0;j<k;j++) {
ans.append(ts2.pollFirst()+" ");
}
}
pw.println(ans);
}
}
}
pw.flush();
}
static class pair implements Comparable<pair> {
// int x;
// int y;
long x,y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
// public int hashCode() {
// return new Integer(x).hashCode() * 31 + new Integer(y).hashCode();
// }
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String readAllLines(BufferedReader reader) throws IOException {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
return content.toString();
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | 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 8 | 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 | ffec24d9d1e5210b54176f11b1d17071 | 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 final class C {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int T = fs.nextInt();
for (int tt = 0; tt < T; ++tt) {
int n = fs.nextInt();
int k = fs.nextInt();
int[][] mat= new int[n][k];
int val = 1;
for(int j = 0; j < k; ++j){
for(int i = 0;i < n; ++i) mat[i][j] = val++;
}
boolean ans = true;
for(int i = 0; i < n; ++i){
for(int j = 0; j < k; ++j){
if(j > 0){
int sum = mat[i][j] + mat[i][j-1];
if(sum % 2 != 0){
ans = false;
break;
}
}
}
}
if(!ans){
System.out.println("NO");
continue;
}
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();
}
}
}
static String rev(String s){
StringBuilder sb = new StringBuilder();
for(int i = s.length() - 1; i >= 0; --i){
sb.append(s.charAt(i));
}
return sb.toString();
}
static final Random random = new Random();
static final int mod = 1_000_000_007;
static long add(long a, long b) {
return (a + b) % mod;
}
static long sub(long a, long b) {
return ((a - b) % mod + mod) % mod;
}
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 long mul(long a, long b) {
return (a * b) % mod;
}
static long exp(long base, long exp) {
if (exp == 0) return 1;
long half = exp(base, exp / 2);
if (exp % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials = new long[2_000_001];
static long[] invFactorials = new long[2_000_001];
static void precompFacts() {
factorials[0] = invFactorials[0] = 1;
for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i);
invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2);
for (int i = invFactorials.length - 2; i >= 0; i--)
invFactorials[i] = mul(invFactorials[i + 1], i + 1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k]));
}
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());
}
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 8 | 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 | e1beb61adb531fac003d2a961038a296 | 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 implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new Main(), "", 256 * (1L << 20)).start();
}
public void run() {
try {
long t1 = System.currentTimeMillis();
if (System.getProperty("ONLINE_JUDGE") != null) {
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");
}
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
int[] readIntArray(int n) throws IOException {
int [] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = readInt();
}
return a;
}
long[] readLongArray(int n) throws IOException {
long [] a = new long[n];
for(int i = 0; i < n; i++) {
a[i] = readLong();
}
return a;
}
int[] readBinStringArray() throws IOException {
String s = readString();
int [] a = new int[s.length()];
for(int i = 0; i < s.length(); i++) {
a[i] = s.charAt(i) == '0' ? 0 : 1;
}
return a;
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
void solveTest() throws IOException {
int n = readInt();
int k = readInt();
if(k==1) {
out.println("YES");
for(int i=0;i<n;i++) {
out.println(i+1);
}
return;
}
if(n%2==1) {
out.println("NO");
return;
}
out.println("YES");
for(int i=0;i<n*k/2;i++) {
out.print(1 + 2*i);
out.print(" ");
if (i%k == k-1) {
out.println();
}
}
for(int i=0;i<n*k/2;i++) {
out.print(2 + 2*i);
out.print(" ");
if (i%k == k-1) {
out.println();
}
}
}
void solve() throws IOException {
int numTests = readInt();
while(numTests-->0) {
solveTest();
}
}
} | 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 8 | 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 | cdfb33dea8fc6ec41d3ba6c6b1b0c997 | 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 Codeforces_770C {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
while(t-->0){
int n=scan.nextInt();
int k=scan.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=0;i<n;i++){
for(int j=0;j<k;j++){
if(i%2==0){
System.out.print((i*k)+(j*2)+1+" ");
}else{
System.out.print(((i-1)*k)+(j*2)+2+" ");
}
}
System.out.println();
}
}else{
System.out.println("NO");
}
}
scan.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 8 | 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 | 876a435d16f9f42fe8fcfbf596ee4606 | 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 C1634Sol {
public static void main(String[] args) throws IOException {
//T
//n k
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(r.readLine());
for(int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(r.readLine());
int n=Integer.parseInt(st.nextToken());
int k=Integer.parseInt(st.nextToken());
if(n%2==0 || k==1) {
System.out.println("YES");
print(n,k);
} else {
System.out.println("NO");
continue;
}
}
r.close();
}
static void print(int n, int k) {
int ei=2; int oi=1;
for(int i = 0; i < n; i++) {
//every row
//if(i%2!=0) {
//odd first
if(i%2==0) {
for(int j = oi; j < oi+2*k; j+=2) {
//every column
System.out.print(j+ " ");
}
oi=oi+2*k;
} else {
for(int j = ei; j < ei+2*k; j+=2) {
//every column
System.out.print(j + " ");
}
ei=ei+2*k;
}
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 8 | 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 | 38ffab473f39ee1c727380a94c682eca | 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 final class Main {
//int 2e9 - long 9e18
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)};
static int mod = (int) (1e9 + 7);
static int mod2 = 998244353;
public static void main(String[] args) {
int tt = i();
while (tt-- > 0) {
solve();
}
out.flush();
}
public static void solve() {
int n = i();
int k = i();
if (k != 1) {
if ((n % 2) == 1) {
out.println("NO");
return;
}
}
out.println("YES");
int odd = 1;
int even = 2;
while (odd <= n * k) {
for (int i = 0; i < k; i++) {
out.print(odd + " ");
odd += 2;
}
out.println();
}
while (even <= n * k) {
for (int i = 0; i < k; i++) {
out.print(even + " ");
even += 2;
}
out.println();
}
}
// (10,5) = 2 ,(11,5) = 3
static long upperDiv(long a, long b) {
return (a / b) + ((a % b == 0) ? 0 : 1);
}
static long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static int[] preint(int[] a) {
int[] pre = new int[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] pre(int[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] post(int[] a) {
long[] post = new long[a.length + 1];
post[0] = 0;
for (int i = 0; i < a.length; i++) {
post[i + 1] = post[i] + a[a.length - 1 - i];
}
return post;
}
static long[] pre(long[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static void print(char A[]) {
for (char c : A) {
out.print(c);
}
out.println();
}
static void print(boolean A[]) {
for (boolean c : A) {
out.print(c + " ");
}
out.println();
}
static void print(int A[]) {
for (int c : A) {
out.print(c + " ");
}
out.println();
}
static void print(long A[]) {
for (long i : A) {
out.print(i + " ");
}
out.println();
}
static void print(List<Integer> A) {
for (int a : A) {
out.print(a + " ");
}
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static String c() {
return in.next();
}
static int[][] inputWithIdx(int N) {
int A[][] = new int[N][2];
for (int i = 0; i < N; i++) {
A[i] = new int[]{i, in.nextInt()};
}
return A;
}
static int[] input(int N) {
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[] = new long[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextLong();
}
return A;
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long LCM(int a, int b) {
return (long) a / GCD(a, b) * b;
}
static long LCM(long a, long b) {
return a / GCD(a, b) * b;
}
// find highest i which satisfy a[i]<=x
static int lowerbound(int[] a, int x) {
int l = 0;
int r = a.length - 1;
while (l < r) {
int m = (l + r + 1) / 2;
if (a[m] <= x) {
l = m;
} else {
r = m - 1;
}
}
return l;
}
static void shuffle(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
}
static void shuffleAndSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int[] temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr, comparator);
}
static void shuffleAndSort(long[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
long temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static boolean isPerfectSquare(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static void swap(int A[], int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
static void swap(char A[], int a, int b) {
char t = A[a];
A[a] = A[b];
A[b] = t;
}
static long pow(long a, long b, int mod) {
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 pow(long a, long b) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow *= x;
}
x = x * x;
b /= 2;
}
return pow;
}
static long modInverse(long x, int mod) {
return pow(x, mod - 2, mod);
}
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;
}
public static String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
public static void reverse(int[] arr) {
for (int i = 0; i < arr.length / 2; i++) {
int tmp = arr[i];
arr[arr.length - 1 - i] = tmp;
arr[i] = arr[arr.length - 1 - i];
}
}
public static String repeat(char ch, int repeat) {
if (repeat <= 0) {
return "";
}
final char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {
buf[i] = ch;
}
return new String(buf);
}
public static int[] manacher(String s) {
char[] chars = s.toCharArray();
int n = s.length();
int[] d1 = new int[n];
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
return d1;
}
public static int[] kmp(String s) {
int n = s.length();
int[] res = new int[n];
for (int i = 1; i < n; ++i) {
int j = res[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = res[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
++j;
}
res[i] = j;
}
return res;
}
}
class Pair {
int i;
int j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return i == pair.i && j == pair.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class ThreePair {
int i;
int j;
int k;
ThreePair(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ThreePair pair = (ThreePair) o;
return i == pair.i && j == pair.j && k == pair.k;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
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 Node {
int val;
public Node(int val) {
this.val = val;
}
}
class ST {
int n;
Node[] st;
ST(int n) {
this.n = n;
st = new Node[4 * Integer.highestOneBit(n)];
}
void build(Node[] nodes) {
build(0, 0, n - 1, nodes);
}
private void build(int id, int l, int r, Node[] nodes) {
if (l == r) {
st[id] = nodes[l];
return;
}
int mid = (l + r) >> 1;
build((id << 1) + 1, l, mid, nodes);
build((id << 1) + 2, mid + 1, r, nodes);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
void update(int i, Node node) {
update(0, 0, n - 1, i, node);
}
private void update(int id, int l, int r, int i, Node node) {
if (i < l || r < i) {
return;
}
if (l == r) {
st[id] = node;
return;
}
int mid = (l + r) >> 1;
update((id << 1) + 1, l, mid, i, node);
update((id << 1) + 2, mid + 1, r, i, node);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
Node get(int x, int y) {
return get(0, 0, n - 1, x, y);
}
private Node get(int id, int l, int r, int x, int y) {
if (x > r || y < l) {
return new Node(0);
}
if (x <= l && r <= y) {
return st[id];
}
int mid = (l + r) >> 1;
return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y));
}
Node comb(Node a, Node b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return new Node(GCD(a.val, b.val));
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, 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 8 | 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 | 59536c0d6141c04665134f23089e060c | 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.util.*;
import java.lang.*;
import java.io.*;
public class Rough_Work {
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-->0) {
int n = sc.nextInt();
int k = sc.nextInt();
if (k == 1) {
out.println("YES");
for (int i = 1; i <= n; i++) out.println(i);
continue;
}
if ((n&1) == 1) out.println("NO");
else {
out.println("YES");
int c = 0;
for (int i = 1; i < n*k; i+=2) {
out.print(i+" ");
c++;
if (c == k) {
out.println();
c = 0;
}
}
for (int i = 2; i <= n*k; i+=2) {
out.print(i+" ");
c++;
if (c == k) {
out.println();
c = 0;
}
}
}
}
sc.close();
out.close();
}
static class FastReader {
final private int BUFFER_SIZE = 1 << 17;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastReader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastReader(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[128]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
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;
}
public int[] nextIntArray(int size) throws IOException {
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int size) throws IOException {
long[] arr = new long[size];
for (int i = 0; i < size; i++) {
arr[i] = nextLong();
}
return arr;
}
public Double[] nextDoubleArray(int size) throws IOException {
Double[] arr = new Double[size];
for (int i = 0; i < size; i++) {
arr[i] = nextDouble();
}
return arr;
}
public int[][] nextIntMatrix(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
arr[i] = nextIntArray(m);
}
return arr;
}
public long[][] nextLongMatrix(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
arr[i] = nextLongArray(m);
}
return arr;
}
public ArrayList<Integer> nextIntList(int size) throws IOException {
ArrayList<Integer> arrayList = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
arrayList.add(nextInt());
}
return arrayList;
}
public ArrayList<Long> nextLongList(int size) throws IOException {
ArrayList<Long> arrayList = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
arrayList.add(nextLong());
}
return arrayList;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
| Java | ["4\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 8 | 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 | cf7bef0afdf3f59167f12a6be445e4a0 | 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.*;
public class Codechef
{
static int[] num_to_bits = new int[] { 0, 1, 1, 2, 1, 2, 2,
3, 1, 2, 2, 3, 2, 3, 3, 4 };
static int countSetBitsRec(int num)
{
int nibble = 0;
if (0 == num)
return num_to_bits[0];
// Find last nibble
nibble = num & 0xf;
// Use pre-stored values to find count
// in last nibble plus recursively add
// remaining nibbles.
return num_to_bits[nibble] + countSetBitsRec(num >> 4);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
try {
br=new BufferedReader(new FileReader("input.txt"));
PrintStream out= new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
}
// Catch block to handle the exceptions
catch (Exception e) {
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 computeXOR(int 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;
}
public static void main (String[] args) throws java.lang.Exception
{
try{
FastReader sc=new FastReader();
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(System.out));
int t=sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int k = sc.nextInt();
int tot = n*k;
int odd = 0;
int even = 0;
for(int i=1;i<=tot;i++)
{
if(i%2==0)
even++;
else
odd++;
}
int f1 = odd/k;
int f2 = even/k;
int h = f1+f2;
if(k ==1)
{
System.out.println("Yes");
int v = 1;
for(int i=0;i<n;i++)
{
System.out.println(v);
v++;
}
}
else if(h!=n)
System.out.println("No");
else
{
System.out.println("Yes");
int o=1;
int i=0;
for(;i<n;i++)
{
for(int j=0;j<k;j++)
{
if(o>tot)
break;
else
{
System.out.print(o+" ");
o+=2;
}
}
System.out.println();
if(o>tot)
break;
}
i++;
int e=2;
for(;i<n;i++)
{
for(int j=0;j<k;j++)
{
if(e>tot)
break;
else
{
System.out.print(e+" ");
e+=2;
}
}
System.out.println();
if(e>tot)
break;
}
}
}
}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 8 | 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 | d1b40530f654be77cae3ac6a5ddfc0d1 | 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;
public class OKEA {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().split(" ")[0]);
PrintWriter pr = new PrintWriter(System.out);
while (t > 0) {
t--;
String[] ints = br.readLine().split(" ");
int n = Integer.parseInt(ints[0]);
int k = Integer.parseInt(ints[1]);
if (n%2 == 1){
if (k == 1){
}
else{
pr.println("NO");
continue;
}
}
pr.println("YES");
int s = -1*(k-1)*2;
for(int i = 0;i<n;i++){
s++;
if (i%2 == 0) s+=(k-1)*2;
for(int j = 0;j<k;j++){
int temp = s + 2*j;
if (j == k-1){
pr.print(temp);
continue;
}
pr.print(temp);
pr.print(" ");
}
pr.println();
}
}
pr.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 8 | 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 | 9bdb765cf5e7cf2576f43590653336d1 | 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 sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
int m = sc.nextInt();
if (m == 1) {
System.out.println("YES");
for (int j = 1; j <= n; j++) {
System.out.println(j);
}
} else if (n % 2 == 1) {
System.out.println("NO");
} else {
System.out.println("YES");
for (int j = 1; j <= n; j++) {
for (int k = j; k <= m * n; k += n) {
System.out.print(k + " ");
}
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 8 | 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 | dce0cbb31b3741e5ef0b90cbf40a7d89 | 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.ArrayDeque;
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;
// C -> CodeForcesProblemSet
public final class C {
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(), k = fr.nextInt();
if (k == 1) {
out.println("YES");
for (int i = 1; i < n + 1; i++)
out.println(i);
continue OUTER;
}
// Observations:
// 1. The task is to determine such grid (n x k) that the
// mean price of any segment from any row is an integer.
// 2. An additional constraint is that we must include all
// numbers [1, nk].
// 3. Every segment of length 2 has to have even sum. This means
// that every two adjacent elements (x, y) will have the same
// parity.
if (n % 2 != 0) {
out.println("NO");
continue OUTER;
}
ArrayDeque<Integer> vals = new ArrayDeque<>();
for (int i = 1; i < n * k + 1; i += 2)
vals.push(i);
for (int i = 2; i < n * k + 1; i += 2)
vals.push(i);
out.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++)
out.print(vals.pollLast() + " ");
out.println();
}
}
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 | ["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 8 | 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 | bc22b82a3d78e6e53a3cc4587f05a9f5 | 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.*;
/**
* CF1634C OKEA
*/
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(), k = rd.nextInt();
int even = n * k / 2;
int odd = n * k - even;
if (even % k != 0 || odd % k != 0) {
System.out.println("NO");
return;
}
System.out.println("YES");
StringBuilder sb = new StringBuilder();
int cnt = 0;
for (int i = 1; i <= n * k; i += 2) {
// System.out.printf("%d ", i);
sb.append(i).append(' ');
cnt++;
if (cnt % k == 0) {
// System.out.printf("\n");
sb.append('\n');
}
}
cnt = 0;
for (int i = 2; i <= n * k; i += 2) {
// System.out.printf("%d ", i);
sb.append(i).append(' ');
cnt++;
if (cnt % k == 0) {
// System.out.printf("\n");
sb.append('\n');
}
}
System.out.println(sb.toString());
// for (int m = 2; m <= k; m++) {
// Utils.printf("mod %d: ", m);
// for (int i = 1; i <= n * k; i++) {
// Utils.printf("%d ", i % m);
// }
// Utils.printf("\n");
// }
}
}
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 | ["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 8 | 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 | 59f89086a1a40bf92b38c7a20fec1bd0 | 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.lang.*;
import java.util.*;
public class OKEA {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0) {
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);
}
}
else {
if(n%2 != 0) {
System.out.println("NO");
}
else {
System.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) {
System.out.print(odd + " ");
odd += 2;
}
else {
System.out.print(even + " ");
even += 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 8 | 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 | 7fa5437c63bea0a0ac84d2605bd055a6 | 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 C1643 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
if (k == 1) {
pw.println("YES");
for (int i = 1; i <= n; i++) {
pw.println(i);
}
continue;
}
if (n % 2 == 1) {
pw.println("NO");
continue;
}
pw.println("YES");
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < k; j++) {
pw.print(((i * k + j) + 1) * 2);
pw.print(" ");
}
pw.println();
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < k; j++) {
pw.print(((i * k + j) + 1) * 2 - 1);
pw.print(" ");
}
pw.println();
}
}
pw.close();
// for (int i = 0; i < 4; i++) {
// for (int j = i; j < 4; j++) {
// for (int k = j; k < 4; k++) {
// for (int w = k; w < 4; w++) {
// if ((i + j + k + w) % 4 == 0) {
// System.out.println(i + " " + j + " " + k + " " + w);
// }
// }
// }
// }
// }
}
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 | ["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 8 | 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 | 24d4d7b1f4a40291811e045794549b93 | 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 C770C {
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();
boolean p = false;
if (n % 2 == 0) {
p = true;
} else {
if (k == 1) {
p = true;
} else {
p = false;
}
}
int i, j, x = 1, y = 2;
if (p) {
System.out.println("YES");
for (i = 1; i <= n; i++) {
for (j = 1; j <= k; j++) {
if (i % 2 != 0) {
System.out.print(x + " ");
x += 2;
} else {
System.out.print(y + " ");
y += 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 8 | 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 | 9f587b7cbefcdfe771a3b148526f07b7 | 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.lang.reflect.Array;
import java.util.*;
import java.lang.*;
public class Solution{
static class Graph{
public static class Vertex{
HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex
}
public static HashMap<Integer,Vertex> vt; // for vertices(all)
public Graph(){
vt= new HashMap<>();
}
public static int numVer(){
return vt.size();
}
public static boolean contVer(int ver){
return vt.containsKey(ver);
}
public static void addVer(int ver){
Vertex v= new Vertex();
vt.put(ver,v);
}
public static void addEdge(int ver1, int ver2, int weight){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
Vertex v1= vt.get(ver1);
Vertex v2= vt.get(ver2);
v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge
v2.nb.put(ver1,weight);
}
public static void delEdge(int ver1, int ver2){
if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){
return;
}
vt.get(ver1).nb.remove(ver2);
vt.get(ver2).nb.remove(ver1);
}
public static void delVer(int ver){
if(!vt.containsKey(ver)){
return;
}
Vertex v1= vt.get(ver);
ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int s= arr.get(i);
vt.get(s).nb.remove(ver);
}
vt.remove(ver);
}
static boolean done[];
static int parent[];
static ArrayList<Integer>vals= new ArrayList<>();
public static boolean isCycle(int i){
Stack<Integer>stk= new Stack<>();
stk.push(i);
while(!stk.isEmpty()){
int x= stk.pop();
vals.add(x);
// System.out.print("current="+x+" stackinit="+stk);
if(!done[x]){
done[x]=true;
}
else if(done[x] ){
return true;
}
ArrayList<Integer>ar= new ArrayList<>(vt.get(x).nb.keySet());
for (int j = 0; j <ar.size() ; j++) {
if(parent[x]!=ar.get(j)){
parent[ar.get(j)]=x;
stk.push(ar.get(j));
}
}
// System.out.println(" stackfin="+stk);
}
return false;
}
static int[]level;
static int[] curr;
static int[] fin;
public static void dfs(int src){
done[src]=true;
level[src]=0;
Queue<Integer>q= new LinkedList<>();
q.add(src);
while(!q.isEmpty()){
int x= q.poll();
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
level[v]=level[x]+1;
done[v]=true;
q.offer(v);
}
}
}
}
static int oc[];
static int ec[];
public static void dfs1(int src){
Queue<Integer>q= new LinkedList<>();
q.add(src);
done[src]= true;
// int count=0;
while(!q.isEmpty()){
int x= q.poll();
// System.out.println("x="+x);
int even= ec[x];
int odd= oc[x];
if(level[x]%2==0){
int val= (curr[x]+even)%2;
if(val!=fin[x]){
// System.out.println("bc");
even++;
vals.add(x);
}
}
else{
int val= (curr[x]+odd)%2;
if(val!=fin[x]){
// System.out.println("bc");
odd++;
vals.add(x);
}
}
ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet());
// System.out.println(arr);
for (int i = 0; i <arr.size() ; i++) {
int v= arr.get(i);
if(!done[v]){
done[v]=true;
oc[v]=odd;
ec[v]=even;
q.add(v);
}
}
}
}
static long popu[];
static long happy[];
static long count[]; // total people crossing that pos
static long sum[]; // total sum of happy people including that.
public static void bfs(int x){
done[x]=true;
long total= popu[x];
// long smile= happy[x];
ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <nbrs.size() ; i++) {
int r= nbrs.get(i);
if(!done[r]){
bfs(r);
total+=count[r];
// smile+=sum[r];
}
}
count[x]=total;
// sum[x]=smile;
}
public static void bfs1(int x){
done[x]=true;
// long total= popu[x];
long smile= 0;
ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet());
for (int i = 0; i <nbrs.size() ; i++) {
int r= nbrs.get(i);
if(!done[r]){
bfs1(r);
// total+=count[r];
smile+=happy[r];
}
}
// count[x]=total;
sum[x]=smile;
}
}
static class rr {
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 in() throws IOException {
return Integer.parseInt(next());
}
static double db() throws IOException {
return Double.parseDouble(next());
}
static long ll() throws IOException {
return Long.parseLong(next());
}
}
static class disjointSet{
HashMap<Integer, Node> mp= new HashMap<>();
public static class Node{
int data;
Node parent;
int rank;
}
public void create(int val){
Node nn= new Node();
nn.data=val;
nn.parent=nn;
nn.rank=0;
mp.put(val,nn);
}
public int findparent(int val){
return findparentn(mp.get(val)).data;
}
public Node findparentn(Node n){
if(n==n.parent){
return n;
}
Node rr= findparentn(n.parent);
n.parent=rr;
return rr;
}
public void union(int val1, int val2){ // can also be used to check cycles
Node n1= findparentn(mp.get(val1));
Node n2= findparentn(mp.get(val2));
if(n1.data==n2.data) {
return;
}
if(n1.rank<n2.rank){
n1.parent=n2;
}
else if(n2.rank<n1.rank){
n2.parent=n1;
}
else {
n2.parent=n1;
n1.rank++;
}
}
}
static class Pair implements Comparable<Pair>{
int x;
int y;
// smallest form only
public Pair(int x, int y){
this.x=x;
this.y=y;
}
@Override
public int compareTo(Pair x){
if(this.x>x.x){
return 1;
}
else if(this.x==x.x){
if(this.y>x.y){
return 1;
}
else if(this.y==x.y){
return 0;
}
else{
return -1;
}
}
return -1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return x == pair.x && y == pair.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
public String toString(){
return x+" "+y;
}
}
public static void main(String[] args) throws IOException {
rr.init(System.in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t= rr.in();
outer: for (int tt = 0; tt <t ; tt++) {
int n = rr.in();
int k= rr.in();
if( k==1){
out.append("YES"+'\n');
for (int i = 0; i <n ; i++) {
out.append((i+1)+"\n");
}
}
else if(n%2==1){
out.append("NO"+"\n");
}
else{
out.append("YES"+"\n");
for (int i = 0; i <n ; i++) {
int num=i+1;
for (int j = 0; j <k ; j++) {
out.append(num+" ");
num+=n;
}
out.append("\n");
}
}
}
out.flush();
out.close();
}
public static boolean pal(String s){
int n= s.length();
for (int i = 0; i <n ; i++) {
if(s.charAt(i)!=s.charAt(n-1-i)){
return false;
}
}
return true;
}
public static long ceil_div(long a, long b){
return (a+b-1)/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 8 | 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 | c4a900a02f0f00d990dcdcd1fcf03989 | 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 codeMaster {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.nextInt();
while(tc-->0){
int n = sc.nextInt(), k = sc.nextInt();
if(k == 1){
pw.println("YES");
for(int i = 1; i<=n*k; i++)pw.println(i);
}else{
int oddNum = (n*k) / 2 + ((n*k) % 2);
//double shelvesNeeded = (double) oddNum / k;
if(oddNum < k)pw.println("NO");
else{
if(oddNum % k == 0){
int shelvesNeededOdd = oddNum/k;
double shelvesNeededEven = Math.ceil((double) ((n*k)/2) / k);
if(shelvesNeededOdd + shelvesNeededEven == n) {
pw.println("YES");
for (int i = 1, j=1; i <= n*k; i += 2, j++) {
pw.print(i + " ");
if (j % k == 0) pw.println();
}
for (int i = 2, j = 1; i <= (n * k); i += 2, j++) {
pw.print(i + " ");
if (j % k == 0) pw.println();
}
}else pw.println("NO");
}else pw.println("NO");
}
}
}
pw.flush();
}
public static int differences(int n, int test){
int changes = 0;
while(test > 0){
if(test % 10 != n % 10)changes++;
test/=10;
n/=10;
}
return changes;
}
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return start;
}
static int maxSubArraySum2(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0,start = 0,
end = 0, s = 0;
for (int i = 0; i < size; i++)
{
max_ending_here += a[i];
if (max_so_far < max_ending_here)
{
max_so_far = max_ending_here;
start = s;
end = i;
}
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return end;
}
static class SegmentTree{
long[]array,sTree;
int N;
public SegmentTree(long arr[]) {
array=arr;
N=arr.length-1;
sTree=new long[N<<1];
}
void update(int idx){
array[idx]++;
idx+=N-1;
sTree[idx]++;
while(idx>1) {
idx>>=1;
sTree[idx]++;
}
}
void set(int idx, long val){
array[idx]++;
idx+=N-1;
sTree[idx]++;
while(idx>1) {
idx>>=1;
sTree[idx]++;
}
}
long query(int l,int r) {
return query(1,1,N,l,r);
}
long query(int node,int s,int e,int l,int r) {
if(l>e || r<s)return 0;
if(l<=s && r>=e)return sTree[node];
int mid=(s+e)>>1;
return query(node<<1,s,mid,l,r)+query(node<<1|1,mid+1,e,l,r);
}
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair x){
return this.x - x.x;
}
}
public static boolean isSubsequence(char[] arr, String s){
boolean ans = false;
for(int i = 0, j = 0; i<arr.length; i++){
if(arr[i] == s.charAt(j)){
j++;
}
if(j == s.length()){
ans = true;
break;
}
}
return ans;
}
public static void sortIdx(long[]a,long[]idx) {
mergesortidx(a, idx, 0, a.length-1);
}
static void mergesortidx(long[] arr,long[]idx,int b,int e) {
if(b<e) {
int m=b+(e-b)/2;
mergesortidx(arr,idx,b,m);
mergesortidx(arr,idx,m+1,e);
mergeidx(arr,idx,b,m,e);
}
return;
}
static void mergeidx(long[] arr,long[]idx,int b,int m,int e) {
int len1=m-b+1,len2=e-m;
long[] l=new long[len1];
long[] lidx=new long[len1];
long[] r=new long[len2];
long[] ridx=new long[len2];
for(int i=0;i<len1;i++) {
l[i]=arr[b+i];
lidx[i]=idx[b+i];
}
for(int i=0;i<len2;i++) {
r[i]=arr[m+1+i];
ridx[i]=idx[m+1+i];
}
int i=0,j=0,k=b;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
arr[k++]=l[i++];
idx[k-1]=lidx[i-1];
}
else {
arr[k++]=r[j++];
idx[k-1]=ridx[j-1];
}
}
while(i<len1) {
idx[k]=lidx[i];
arr[k++]=l[i++];
}
while(j<len2) {
idx[k]=ridx[j];
arr[k++]=r[j++];
}
return;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| 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 8 | 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 | 3a9bc7b50a117ceb20b6c2933dd05000 | 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.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class C {
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());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
// int[] arr = readArr(N, infile, st);
boolean flag = false;
if(k == 1){
flag = true;
}
else if(n%2 == 1){
flag = false;
}
else if((n*k) % 2 == 0){
flag = true;
}
if(flag){
out.println("Yes");
for(int i = 1; i <= n; i++){
for(int j = 0; j < k; j++){
out.print(i + n*j + " " );
}
out.println();
}
}
else{
out.println("No");
}
}
}
public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception {
int[] arr = new int[N];
st = new StringTokenizer(infile.readLine());
for (int i = 0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
public static void print(int[] arr) {
//for debugging only
for (int x : arr)
out.print(x + " ");
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 8 | 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 | 651c442a8c3055978007a803a47d5992 | 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;
public class OKEA {
public static void main(String[] args) throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while (t-->0){
String[] s=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int k=Integer.parseInt(s[1]);
if(k==1){
System.out.println("YES");
for(int i=1;i<=n;i++){
System.out.println(i);
}
}
else if((n*k)%2!=0) System.out.println("NO");
else{
int num=(n*k)/2;
if(num%k==0){
System.out.println("YES");
int e=2;
int o=1;
boolean even=false;
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
if(even){
System.out.print(e+" ");
e+=2;
}
else
{
System.out.print(o+" ");
o+=2;
}
}
System.out.println();
even=!even;
}
}
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 8 | 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 | 67e87183f40732f028dcc418d8b1737d | 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 Lokeando20 {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
boolean weCan = false;
int cont=0,mean=0,sum=0;
int cases = read.nextInt();
for(int i=0;i<cases;i++)
{
int shelfs = read.nextInt();
int itemsOnShelf = read.nextInt();
if(itemsOnShelf ==1)
{
System.out.println("YES");
for(int k=1;k<=shelfs;k++){
System.out.println(k);
}
continue;
}
int[][]toyShelf = new int[shelfs][itemsOnShelf];
for(int c=0;c<toyShelf[0].length;c++){
for(int r=0;r<toyShelf.length;r++){
toyShelf[r][c]=++cont;
}
}
//System.out.println(Arrays.deepToString(toyShelf));
for(int r=0;r<toyShelf.length;r++){
for(int c=0;c<toyShelf[0].length;c++){
cont =toyShelf[r][c];
for(int d=c+1;d<toyShelf[0].length;d++){
sum+=toyShelf[r][d];
}
//System.out.println(sum+" "+cont);
//System.out.println((cont+sum)%(itemsOnShelf-c) +" "+r+" "+c);
if((cont+sum)%(itemsOnShelf-(c))==0){
mean++;
sum=0;
}
//System.out.println("mean "+mean );
sum=0;
}
}
if((mean)==(toyShelf.length*toyShelf[0].length)){
System.out.println("YES");
for(int r=0;r<toyShelf.length;r++){
for(int c=0;c<toyShelf[0].length;c++){
System.out.print(toyShelf[r][c]+" ");
}
System.out.println("");
}
}else{
System.out.println("NO");
}
mean=0;
cont=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 8 | 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 | 724776572b0d5b077a2e9e798dd1285c | 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.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.math.BigInteger;
import java.util.Arrays;
import javax.lang.model.util.ElementScanner6;
public class Main {
static final long M = 998244353;
static final BigInteger MOD = new BigInteger(M+"");
public static void main(String[] args) {
FastInput sc = new FastInput();
int T = sc.nextInt();
while(T-->0)
{
int n = sc.nextInt();
int k = sc.nextInt();
int ans = n*k;
if(( (ans/2)%k == 0 || k == 1))
{
System.out.println("YES");
int a[][] = new int[n][k];
int odd = 1;
int even = 2;
for(int i = 0;i<n;i++)
{
for(int j = 0;j<k;j++)
{
if(i%2 == 0)
{
a[i][j] = odd;
odd += 2;
}else
{
a[i][j] = (even);
even += 2;
}
}
}
for(int i = 0;i<n;i++)
{
for(int j = 0;j<k;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}else
System.out.println("NO");
}
}
}
class Pair implements Comparable<Pair>{
long i;
long j;
public Pair(long i,long j)
{
this.i = i;
this.j = j;
}
public Pair(){
}
public int compareTo(Pair t)
{
if(t.i>i)
return -1;
else if(t.i<i)
return 1;
//else
return 0;
}
}
class FastInput {
BufferedReader br;
StringTokenizer st;
public FastInput()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while(st == null || !st.hasMoreElements())
{
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e)
{
System.out.println(e.toString());
}
}
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 String nextLine()
{
String str = "";
try {
str = br.readLine();
} catch(IOException e)
{
System.out.println(e.toString());
}
return str;
}
public static void sort(int[] arr)
{
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 void sort(long[] arr)
{
ArrayList<Long> ls = new ArrayList<Long>();
for(long x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public int [] readArr(int n)
{
int [] a = new int[n];
for(int i = 0; i<n; i++)
a[i] = nextInt();
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 8 | 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 | c530e06b9d6d19d987004c0d01d8b4c9 | 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 exe_C1634 {
public static void main(String[] args) {
//Input
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
int[] n = new int[t];
int[] k = new int[t];
for (int i = 0; i < t; i++) {
n[i] = scanner.nextInt();
k[i] = scanner.nextInt();
}
scanner.close();
//Output
for (int i = 0; i < t; i++) {
if (k[i] == 1) {
System.out.println("YES");
for (int j = 1; j <= n[i]; j++) {
System.out.println(j);
}
}else {
if (n[i] %2 == 1) {
System.out.println("NO");
}else {
System.out.println("YES");
for (int j = 1; j <= n[i]*k[i]/2; j++) {
System.out.print((j*2-1) + " " );
if (j %k[i] == 0) System.out.println();
}
for (int j = 1; j <= n[i]*k[i]/2; j++) {
System.out.print((j*2) + " " );
if (j %k[i] == 0) 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 8 | 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 | eae6663dc593833fec7eecf4ba6b6b22 | 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) {
new C().run();
}
BufferedReader br;
PrintWriter out;
long mod = (long) (1e9 + 7), inf = (long) (3e18);
class pair {
int F, S;
pair(int f, int s) {
F = f; S = s;
}
}
void solve() {
int t = ni();
while(t-- > 0) {
//TODO:
int n= ni();
int k= ni();
// out.println(0.0==0);
boolean pos=true;
if(n%2==0 || k==1){
out.println("YES");
for(int i=1, j=1; i<=n*k; i+=2, j++){
out.print(i+" ");
if(k==j){
out.println();
j=0;
}
}
for(int i=2, j=1; i<=n*k; i+=2, j++){
out.print(i+" ");
if(k==j){
out.println();
j=0;
}
}
}
else{
out.println("NO");
}
}
}
// -------- I/O Template -------------
char nc() {
return ns().charAt(0);
}
String nLine() {
try {
return br.readLine();
} catch(IOException e) {
return "-1";
}
}
double nd() {
return Double.parseDouble(ns());
}
long nl() {
return Long.parseLong(ns());
}
int ni() {
return Integer.parseInt(ns());
}
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) a[i] = ni();
return a;
}
StringTokenizer ip;
String ns() {
if(ip == null || !ip.hasMoreTokens()) {
try {
ip = new StringTokenizer(br.readLine());
if(ip == null || !ip.hasMoreTokens())
ip = new StringTokenizer(br.readLine());
} catch(IOException e) {
throw new InputMismatchException();
}
}
return ip.nextToken();
}
void run() {
try {
if (System.getProperty("ONLINE_JUDGE") == null) {
br = new BufferedReader(new FileReader("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/input.txt"));
out = new PrintWriter("/media/ankanchanda/Data1/WORKPLACE/DS and CP/Competitive Programming/VSCODE/IO/output.txt");
} else {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
solve();
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 8 | 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 | d8cb6e8f3a64925d672df622dbc89cd5 | 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.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputStream inputStream=System.in;
OutputStream outputStream = System.out;
InputReader in=new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ProbA solver = new ProbA();
solver.solve(in.nextInt(), in, out);
out.flush();
out.close();
}
static class ProbA{
int n;
int k;
public void solve(int testCases, InputReader in, PrintWriter out) {
for(int i=0;i<testCases; ++i) {
n = in.nextInt();
k = in.nextInt();
process(n,k,out);
out.flush();
}
}
private void process(int n, int k, PrintWriter out) {
if(k==1) {
out.print("YES\n");
for(int i=1;i<=n;++i) out.println(i);
}else if(n % 2 != 0) {
out.print("NO\n");
}else {
int odd=1;
int even=2;
out.print("YES\n");
for(int i=1;i<=n;++i) {
if(i%2==0) {
for(int j=1;j<=k;++j) {
out.print(even+" ");
even+=2;
}
}else {
for(int j=1;j<=k;++j) {
out.print(odd+" ");
odd+=2;
}
}
out.print('\n');
}
}
}
}
static class InputReader{
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
st = null;
}
public String next() {
while(st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(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 8 | 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 | 2b61fb604297ee591ad74eddaac9c9c5 | 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 MyCpClass{
public static void main(String []args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine().trim());
while(T-- > 0){
String []ip = br.readLine().trim().split(" ");
int n = Integer.parseInt(ip[0]);
int k = Integer.parseInt(ip[1]);
if(k == 1){
sb.append("YES\n");
for(int i=1; i<=n*k; i++)
sb.append(i+"\n");
}
else if(n%2 == 0){
sb.append("YES\n");
int [][]a = new int[n][k];
int x = 1;
for(int i=0; i<n; i+=2){
for(int j=0; j<k; j++){
a[i][j] = x++;
a[i+1][j] = x++;
}
}
for(int i=0; i<n; i++){
for(int j=0; j<k; j++)
sb.append(a[i][j] + " ");
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 8 | 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 | 5a9978b35c88e598d7bc35239d02d67b | 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 Feb6P32 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
for(int i = 0; i < t; i++)
{
int n = sc.nextInt();
int k = sc.nextInt();
if(n % 2 != 0 && k != 1)
out.println("NO");
else
{
out.println("YES");
int prevD2 = -2;
int prevND2 = -1;
for(int j = 0; j < n; j++)
{
int disc;
if(j % 2 == 0)
disc = prevD2+2;
else
disc = prevND2+2;
for(int kl = 0; kl < k; kl++)
{
int temp = 1 + disc;
out.print(temp + " ");
disc = disc+2;
if(disc % 2 == 0)
prevD2+=2;
else
prevND2+=2;
}
out.println();
}
}
}
// Start writing your solution here. -------------------------------------
/*
int n = sc.nextInt(); // read input as integer
long k = sc.nextLong(); // read input as long
double d = sc.nextDouble(); // read input as double
String str = sc.next(); // read input as String
String s = sc.nextLine(); // read whole line as String
int result = 3*n;
out.println(result); // print via PrintWriter
*/
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
}
| Java | ["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 8 | 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 | 57ecd7af6567677921bd94a2d1925590 | 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;
public class ProbC {
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());
}
int[] nextArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
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 printArray(int[] arr) throws IOException {
for (int i = 0; i < arr.length; i++)
print(arr[i] + " ");
println("");
}
public void close() throws IOException {
bw.close();
}
}
public static int[][] solve(int n, int k) {
int[][] ans = new int[n][k];
if (k == 1) {
for (int i = 1; i <= n; i++)
ans[i - 1][0] = i;
} else if (n % 2 != 0)
return new int[0][0];
else {
int e = 2, o = 1;
for (int i = 0; i < n; i++) {
int start;
start = (i % 2 == 0 ? o : e);
for (int j = 0; j < k; j++) {
ans[i][j] = start;
start += 2;
}
if (i % 2 == 0)
o += k * 2;
else
e += k * 2;
}
}
return ans;
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int k = in.nextInt();
int[][] ans = solve(n, k);
if (ans.length == 0)
out.println("NO");
else {
out.println("YES");
for (int[] a : ans) {
out.printArray(a);
}
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
| 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 8 | 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 | b17f7867885ea92ec53ced3312e10684 | 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 sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0){
t--;
int n = sc.nextInt();
int k = sc.nextInt();
String ans = "YES";
if(k==1){
System.out.println(ans);
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
int x = i+1;
System.out.print(x+" ");
}
System.out.println();
}
}
else if(k>1 && n%2==0){
System.out.println(ans);
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
if((i+1)%2==1){
int x = 2*(j+1) - 1 + i*k;
System.out.print(x+" ");
}
else{
int x = 2*(j+1) + (i-1)*k;
System.out.print(x+" ");
}
}
System.out.println();
}
}
else{
ans = "NO";
System.out.println(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 8 | 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 | 00c6624d1fe636c5a59967e85cd8f21d | 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.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class A13 {
public static FastScanner sc;
public static PrintWriter pw;
public static StringBuilder sb;
public static int MOD= 1000000007;
public 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 void printList(List<Long> al) {
System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" ")));
}
public static void printList(Deque<Long> al) {
System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" ")));
}
public static void printArr(long[] arr) {
System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," "));
}
public static long gcd(long a, long b) {
if(b==0) return a;
return gcd(b,a%b);
}
public static long lcm(long a, long b) {
return((a*b)/gcd(a,b));
}
public static void decreasingOrder(ArrayList<Long> al) {
Comparator<Long> c = Collections.reverseOrder();
Collections.sort(al,c);
}
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++) {
for(int j=2*i;j<n;j+=i) {
isPrime[j]=false;
}
}
return isPrime;
}
public static long nCr(long N, long R) {
double result=1;
for(int i=1;i<=R;i++) result=((result*(N-R+i))/i);
return (long) result;
}
public static long fastPow(long a, long b, int n) {
long result=1;
while(b>0) {
if((b&1)==1)
result=(result*a %n)%n;
a=(a%n * a%n)%n;
b>>=1;
}
return result;
}
public static int BinarySearch(long[] arr, long key) {
int low=0;
int high=arr.length-1;
while(low<=high) {
int mid=(low+high)/2;
if(arr[mid]==key)
return mid;
else if(arr[mid]>key)
high=mid-1;
else
low=mid+1;
}
return low; //High=low-1
}
public static int upperBound(int[] arr, int target, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(arr[mid]<=target) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(int[] arr, int target, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(arr[mid]<target) l=mid+1;
else r=mid-1;
}
return l;
}
public static boolean solve2(String s) {
int i=0;
int j=s.length()-1;
while(i<=j) {
if(s.charAt(i)==s.charAt(j)) {
i++; j--;
}
else return false;
}
return true;
}
public static void solve(int t) {
int n=sc.nextInt();
int k=sc.nextInt();
// boolean ans=true;
int even= (n*k)/2;
int odd=(n*k)/2;
if((n*k)%2==1){
odd++;
}
int temp1=n/2;
int temp2=n/2;
if(n%2==1) temp2++;
boolean ans=false;
if( temp2>0 && odd%temp2==0 && odd/temp2==k) ans=true;
if( temp1>0 && even%temp1==0 && even/temp1==k) ans=true;
if(even==0 && temp1==0) ans=true;
if(ans==false) {
sb.append("NO").append('\n');
return;
}
sb.append("YES").append('\n');
ArrayList<Integer> al = new ArrayList<>();
for(int i=2;i<=n*k;i+=2) {
al.add(i);
}
for(int i=1;i<=n*k;i+=2) al.add(i);
int index=0;
for(int i=0;i<n;i++) {
for(int j=0;j<k;j++) {
sb.append(al.get(index)+" ");
index++;
}
sb.append('\n');
}
}
public static void main(String[] args) {
sc = new FastScanner();
pw = new PrintWriter(new OutputStreamWriter(System.out));
sb= new StringBuilder("");
int t=sc.nextInt();
for(int i=1;i<=t;i++) solve(i);
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 8 | 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 | 502e160055a579ffb8ab5951ab1089ab | 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 | /*
Rating: 1367
Date: 06-02-2022
Time: 20-42-49
Author: Kartik Papney
Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/
Leetcode: https://leetcode.com/kartikpapney/
Codechef: https://www.codechef.com/users/kartikpapney
*/
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C_OKEA {
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
static boolean check(int[] arr, int k) {
for(int len=1; len<=k; len++) {
long sum = 0;
for(int j=0; j<len; j++) {
sum += arr[j];
}
if(sum%len != 0) return false;
for(int j = len; j<arr.length; j++) {
sum += arr[j];
sum -= arr[j-len];
if(sum%len != 0) return false;
}
}
return true;
}
public static void s() {
long n = sc.nextLong(), k = sc.nextLong();
long sum = 0;
int[][] arr = new int[(int)n][(int)k];
int idx = 1;
for(int i=0; i<arr[0].length; i++) {
for(int j=0; j<arr.length; j++) {
arr[j][i] = idx;
idx++;
}
}
if(check(arr[0], (int)k)) {
p.writeln("YES");
for(int i=0; i<arr.length; i++) {
p.writes(arr[i]);
p.writeln();
}
} else {
p.writeln("NO");
}
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
static final Integer MOD = (int) 1e9 + 7;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
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 max(int[] a) {
int max = Integer.MIN_VALUE;
for (int val : a) max = Math.max(val, max);
return max;
}
static int min(int[] a) {
int min = Integer.MAX_VALUE;
for (int val : a) min = Math.min(val, min);
return min;
}
static long min(long[] a) {
long min = Long.MAX_VALUE;
for (long val : a) min = Math.min(val, min);
return min;
}
static long max(long[] a) {
long max = Long.MIN_VALUE;
for (long val : a) max = Math.max(val, max);
return max;
}
static long sum(long[] a) {
long sum = 0;
for (long val : a) sum += val;
return sum;
}
static int sum(int[] a) {
int sum = 0;
for (int val : a) sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr) if (val == x) count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++) primes[i] = true;
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
for (int j = i * i; j < primes.length; j += i) {
primes[j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i]) arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void writes(int[] arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long[] arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int[] arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
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());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
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 8 | 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 | 78a1283ce818fd62b06f834906a890c4 | 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.*;
// 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(),k=I();
if(k==1){
int p=1;
out.println("YES");
for(int i=0;i<n;i++){
out.println(p++);
}
continue outer;
}
if(n%2==1)out.println("NO");
else{
int a[][]=new int[n][k];
int p=1;
for(int i=0;i<k;i++){
for(int j=0;j<n;j++){
a[j][i]=p++;
}
}
out.println("YES");
printArray(a);
}
}
out.close();
}
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 | ["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 8 | 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 | 884303a2b951cd9ccd677b80437133d6 | 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 okea {
//
// public static void main(String[] args)throws IOException {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
// int T = Integer.parseInt(br.readLine());
// while(T-- > 0) {
// StringTokenizer st = new StringTokenizer(br.readLine());
// int n = Integer.parseInt(st.nextToken());
// int k = Integer.parseInt(st.nextToken());
// int[][] output = new int[n][k];
// if((n % 2 == 1 && k != 1)) System.out.println("NO");
// else {
// int firstNum = 1;
// for(int i = 0;i < n;i++) {
// int num = firstNum;
// for(int j = 0;j < k;j++) {
// output[i][j] = num;
// num += n;
// }
// firstNum++;
// }
// }
//
// for(int[] x: output) {
// System.out.println(Arrays.toString(x));
// }
//
// for(int i = 0;i < n;i++) {
// for(int j = 0;j < k;j++) {
// int sum = 0;
// int count = 0;
// for(int a = j; a < k;a++) {
// sum += output[i][a];
// count++;
// if(sum % count != 0) {
// System.out.println("Fail: " + i + " " + j + " " + a);
// }
// }
// }
// }
// }
// }
//
//}
import java.io.*;
import java.util.*;
public class okea {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
while(T-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
if(n % 2 == 1 && k != 1) System.out.println("NO");
else {
System.out.println("YES");
int firstNum = 1;
for(int i = 0;i < n;i++) {
int num = firstNum;
for(int j = 0;j < k;j++) {
bw.write(num + " ");
num += n;
}
firstNum++;
bw.write("\n");
}
}
bw.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 8 | 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 | 202eed1f7be12cca8e72654fdc19dd72 | 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 {
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();
int k=sc.nextInt();
int even=(k*n)/2; int odd=(k*n+1)/2;
if((even%k !=0) || (odd%k!=0)){
pw.write("NO\n");
}else{
pw.write("YES\n");
int sum=0;
for (int i=1;i<=n*k;i+=2){
pw.write(i+" ");
sum++;
if(sum%k==0) pw.write("\n");
}
for (int i=2;i<=n*k;i+=2){
pw.write(i+" ");
sum++;
if(sum%k==0) pw.write("\n");
}
}
pw.flush();
}
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 | ["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 8 | 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 | 129a9206ff0e6ec17d6e88578d963cd5 | 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.*;
// cd C:\Users\Lenovo\Desktop\New
//ArrayList<Integer> a=new ArrayList<>();
//List<Integer> lis=new ArrayList<>();
//StringBuilder ans = new StringBuilder();
//HashMap<Integer,Integer> map=new HashMap<>();
public class cf {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
public static void main(String args[]) throws IOException {
FastReader sc=new FastReader();
//Scanner s=new Scanner(System.in);
int tt=sc.nextInt();
//int tt=1;
while(tt-->0){
//StringBuilder ar = new StringBuilder();
//System.out.println(t);
int n=sc.nextInt();
int k=sc.nextInt();
int o=1;
int e=2;
int sum=n*k;
if(sum==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%2==0){
System.out.println("YES");
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
if(o<sum){
System.out.print(o+" ");
o+=2;
}
else{
System.out.print(e+" ");
e+=2;
}
}
System.out.println();
}
}
else if(n%2==1){
System.out.println("NO");
}
}
}
static int find_max(int []check,int y){
int max=0;
for(int i=y;i>=0;i--){
if(check[i]!=0){
max=i;
break;
}
}
return max;
}
static void dfs(int [][]arr,int row,int col,boolean [][]seen,int n){
if(row<0 || col<0 || row==n || col==n){
return;
}
if(arr[row][col]==1){
return;
}
if(seen[row][col]==true){
return;
}
seen[row][col]=true;
dfs(arr,row+1,col,seen,n);
dfs(arr,row,col+1,seen,n);
dfs(arr,row-1,col,seen,n);
dfs(arr,row,col-1,seen,n);
}
static int msb(int x){
int ans=0;
while(x!=0){
x=x/2;
ans++;
}
return ans;
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
/* static boolean checkprime(int n1)
{
if(n1%2==0||n1%3==0)
return false;
else
{
for(int i=5;i*i<=n1;i+=6)
{
if(n1%i==0||n1%(i+2)==0)
return false;
}
return true;
}
}
*/
/* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer, Integer> entry = iterator.next();
int value = entry.getValue();
if(value==1){
iterator.remove();
}
else{
entry.setValue(value-1);
}
}
*/
static class Pair implements Comparable
{
int a,b;
public String toString()
{
return a+" " + b;
}
public Pair(int x , int y)
{
a=x;b=y;
}
@Override
public int compareTo(Object o) {
Pair p = (Pair)o;
if(p.a!=a){
return a-p.a;//in
}
else{
return p.b-b;//
}
}
}
/* public static boolean checkAP(List<Integer> lis){
for(int i=1;i<lis.size()-1;i++){
if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){
return false;
}
}
return true;
}
public static int minBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]>=val){
r=mid;
}
else{
l=mid;
}
}
return r;
}
public static int maxBS(int[]arr,int val){
int l=-1;
int r=arr.length;
while(r>l+1){
int mid=(l+r)/2;
if(arr[mid]<=val){
l=mid;
}
else{
r=mid;
}
}
return l;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}*/
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 8 | 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 | 5912adf6a5076a9106c13ce35d98489c | 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;
public class C_060222 {
public static void main(String[] args) {
try {
final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
int [] values = new int [t];
String [] strvalues = new String [t];
for (int i = 0; i < t; i++) {
String [] vals = in.readLine().split(" ");
int n = Integer.parseInt(vals[0]);
int k = Integer.parseInt(vals[1]);
StringBuffer sb = new StringBuffer();
if (k == 1) {
for (int l = 1; l <= n; l++) {
sb.append(l).append("\r\n");
}
strvalues[i] = sb.toString();
values[i] = 1;
}
if (k > 1 && (n % 2 == 0)) {
for (int l = 0; l < n / 2; l++) {
for (int f = 0; f < k; f++) {
sb.append(f * 2 + 1 + l*k*2).append(" ");
}
sb.append("\r\n");
for (int f = 0; f < k; f++) {
sb.append(f*2 + 2 + l*k*2).append(" ");
}
sb.append("\r\n");
}
strvalues[i] = sb.toString();
values[i] = 1;
} else if (k > 1) {
values[i] = 0;
}
}
for (int i = 0; i < t; i++){
if (values[i] == 1) {
System.out.println("YES");
System.out.print(strvalues[i]);
} else {
System.out.println("NO");
}
}
} catch (final IOException e) {
e.printStackTrace();
}
}
}
| 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 8 | 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 | 163fdb3a8712bd336262b42ce7f4f6a4 | 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) {
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
for(int ttt = 1; ttt <= T; ttt++) {
int n = in.nextInt();
int k = in.nextInt();
int e = 0, o = 0;
e = (n*k/2);
o = (n*k)-e;
if(e%k!=0 || o%k!=0) out.println("NO");
else {
out.println("YES");
int se = 2, so = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < k; j++) {
if(i%2==0) {
out.print(so + " ");
so+=2;
}
else {
out.print(se + " ");
se+=2;
}
}
out.println();
}
}
}
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;
}
int[] readInt(int size) {
int[] arr = new int[size];
for(int i = 0; i < size; i++)
arr[i] = Integer.parseInt(next());
return arr;
}
long[] readLong(int size) {
long[] arr = new long[size];
for(int i = 0; i < size; i++)
arr[i] = Long.parseLong(next());
return arr;
}
int[][] read2dArray(int rows, int cols) {
int[][] arr = new int[rows][cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++)
arr[i][j] = Integer.parseInt(next());
}
return arr;
}
}
} | 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 8 | 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 | 278a1675f8b3a9b09cc7355d944d1182 | 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.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Solution {
// VM options: -Xmx1024m -Xss1024m
private void solve() throws IOException {
int n = nextInt();
int k = nextInt();
if (k == 1) {
out.println("YES");
for (int i = 0; i < n; i++) {
out.println(i + 1);
}
} else if (n % 2 == 1) {
out.println("NO");
} else {
out.println("YES");
for (int i = 0, c = 0; i < n; i += 2) {
for (int j = 0; j < k; j++) {
out.print((c + j*2 + 1) + " ");
}
out.println();
for (int j = 0; j < k; j++) {
out.print((c + j*2 + 2) + " ");
}
out.println();
c += k + k;
}
}
}
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 | ["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 8 | 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 | 584e0ac08954428e5c4b976f7a027b4b | 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 static java.lang.Math.*;
import static java.util.Map.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.System.*;
public class Main
{
public void tq() throws Exception
{
st=new StringTokenizer(bq.readLine());
int tq=i();
sb=new StringBuilder(2000000);
o:
while(tq-->0)
{
int n=i();
int m=i();
if(n%2!=0)
{
if(m==1)
{
sl("YES");
int f[][]=new int[n][m];
int i=0;
for(int x=0;x<n;x++)
{
for(int y=0;y<m;y++)f[x][y]=++i;
}
s(f);
}
else sl("NO");
}
else
{
sl("YES");
int f[][]=new int[n][m];
int i=0;
for(int x=0;x<m;x++)
{
for(int y=0;y<n;y++)f[y][x]=++i;
}
s(f);
}
}
p(sb);
}
long mod=1000000007l;
int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
BufferedReader bq = new BufferedReader(new InputStreamReader(in));StringTokenizer st;StringBuilder sb;
public static void main(String[] a)throws Exception{new Main().tq();}
int di[][]={{-1,0},{1,0},{0,-1},{0,1}};
int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};
void f(){out.flush();}
int p(int i,int p[]){return p[i]<0?i:(p[i]=p(p[i],p));}
boolean c(int x,int y,int n,int m){return x>=0&&x<n&&y>=0&&y<m;}
int[] so(int ar[])
{
Integer r[] = new Integer[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
long[] so(long ar[])
{
Long r[] = new Long[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
char[] so(char ar[])
{
Character r[] = new Character[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
void p(Object p) {out.print(p);}void pl(Object p) {out.println(p);}void pl() {out.println();}
void s(String s) {sb.append(s);}
void s(int s) {sb.append(s);}
void s(long s) {sb.append(s);}
void s(char s) {sb.append(s);}
void s(double s) {sb.append(s);}
void ss() {sb.append(' ');}
void sl(String s) {s(s);sb.append("\n");}
void sl(int s) {s(s);sb.append("\n");}
void sl(long s) {s(s);sb.append("\n");}
void sl(char s) {s(s);sb.append("\n");}
void sl(double s) {s(s);sb.append("\n");}
void sl() {sb.append("\n");}
int l(int v) {return 31 - Integer.numberOfLeadingZeros(v);}
long l(long v) {return 63 - Long.numberOfLeadingZeros(v);}
int sq(int a) {return (int) sqrt(a);}
long sq(long a) {return (long) sqrt(a);}
int gcd(int a, int b)
{
while (b > 0)
{
int c = a % b;
a = b;
b = c;
}
return a;
}
long gcd(long a, long b)
{
while (b > 0l)
{
long c = a % b;
a = b;
b = c;
}
return a;
}
boolean p(String s, int i, int j)
{
while (i < j) if (s.charAt(i++) != s.charAt(j--)) return false;
return true;
}
boolean[] si(int n)
{
boolean bo[] = new boolean[n + 1];
bo[0] = bo[1] = true;
for (int x = 4; x <= n; x += 2) bo[x] = true;
for (int x = 3; x * x <= n; x += 2)
{
if (!bo[x])
{
int vv = (x << 1);
for (int y = x * x; y <= n; y += vv) bo[y] = true;
}
}
return bo;
}
long mul(long a, long b, long m)
{
long r = 1l;
a %= m;
while (b > 0)
{
if ((b & 1) == 1) r = (r * a) % m;
b >>= 1;
a = (a * a) % m;
}
return r;
}
int i() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Integer.parseInt(st.nextToken());
}
long l() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Long.parseLong(st.nextToken());
}
String s() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return st.nextToken();
}
double d() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Double.parseDouble(st.nextToken());
}
void s(int a[])
{
for (int e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(long a[])
{
for (long e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(char a[])
{
for (char e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(int ar[][]) {for (int a[] : ar) s(a);}
void s(long ar[][]) {for (long a[] : ar) s(a);}
void s(char ar[][]) {for (char a[] : ar) s(a);}
int[] ari(int n) throws IOException
{
int ar[] = new int[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Integer.parseInt(st.nextToken());
return ar;
}
long[] arl(int n) throws IOException
{
long ar[] = new long[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Long.parseLong(st.nextToken());
return ar;
}
char[] arc(int n) throws IOException
{
char ar[] = new char[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = st.nextToken().charAt(0);
return ar;
}
double[] ard(int n) throws IOException
{
double ar[] = new double[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Double.parseDouble(st.nextToken());
return ar;
}
String[] ars(int n) throws IOException
{
String ar[] = new String[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = st.nextToken();
return ar;
}
int[][] ari(int n, int m) throws IOException
{
int ar[][] = new int[n][m];
for (int x = 0; x < n; x++)ar[x]=ari(m);
return ar;
}
long[][] arl(int n, int m) throws IOException
{
long ar[][] = new long[n][m];
for (int x = 0; x < n; x++)ar[x]=arl(m);
return ar;
}
char[][] arc(int n, int m) throws IOException
{
char ar[][] = new char[n][m];
for (int x = 0; x < n; x++)ar[x]=arc(m);
return ar;
}
double[][] ard(int n, int m) throws IOException
{
double ar[][] = new double[n][m];
for (int x = 0; x < n; x++)ar[x]=ard(m);
return ar;
}
void p(int ar[])
{
sb = new StringBuilder(11 * ar.length);
for (int a : ar) {sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(long ar[])
{
StringBuilder sb = new StringBuilder(20 * ar.length);
for (long a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(double ar[])
{
StringBuilder sb = new StringBuilder(22 * ar.length);
for (double a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(char ar[])
{
StringBuilder sb = new StringBuilder(2 * ar.length);
for (char aa : ar){sb.append(aa);sb.append(' ');}
out.println(sb);
}
void p(String ar[])
{
int c = 0;
for (String s : ar) c += s.length() + 1;
StringBuilder sb = new StringBuilder(c);
for (String a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(int ar[][])
{
StringBuilder sb = new StringBuilder(11 * ar.length * ar[0].length);
for (int a[] : ar)
{
for (int aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(long ar[][])
{
StringBuilder sb = new StringBuilder(20 * ar.length * ar[0].length);
for (long a[] : ar)
{
for (long aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(double ar[][])
{
StringBuilder sb = new StringBuilder(22 * ar.length * ar[0].length);
for (double a[] : ar)
{
for (double aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(char ar[][])
{
StringBuilder sb = new StringBuilder(2 * ar.length * ar[0].length);
for (char a[] : ar)
{
for (char aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void pl(Object... ar) {for (Object e : ar) p(e + " ");pl();}
} | 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 8 | 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 | 9868d9f93938ea26a50149c4409c85f6 | 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 | //make sure to make new file!
import java.io.*;
import java.util.*;
public class C770{
public static void main(String[] args)throws IOException{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(f.readLine());
for(int q = 1; q <= t; q++){
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
if(m == 1){
StringJoiner sj = new StringJoiner("\n");
sj.add("YES");
for(int k = 1; k <= n; k++){
sj.add("" + k);
}
out.println(sj.toString());
continue;
}
if(n % 2 == 0){
int[][] board = new int[n+1][m+1];
for(int k = 1; k <= n; k += 2){
int start = (k-1)*m+1;
for(int j = 1; j <= m; j++){
board[k][j] = start;
start += 2;
}
start = (k-1)*m+2;
for(int j = 1; j <= m; j++){
board[k+1][j] = start;
start += 2;
}
}
out.println("YES");
for(int k = 1; k <= n; k++){
StringJoiner sj = new StringJoiner(" ");
for(int j = 1; j <= m; j++){
sj.add("" + board[k][j]);
}
out.println(sj.toString());
}
} else {
out.println("NO");
}
}
out.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 8 | 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 | 6ee2c541dfaf1c4a179df04c0f2a0bab | 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.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
// int a = 1;
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
// out.print("Case #"+(a++)+": ");
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n, k;
n = in.nextInt();
k = in.nextInt();
List<Integer> even = new ArrayList<>();
List<Integer> odd = new ArrayList<>();
for (int i = 1; i <= n*k; i++) {
if(i%2==0){
even.add(i);
}
else{
odd.add(i);
}
}
int[][] arr = new int[n][k];
int a = 0, o = 0, c = 0;
for (int i = 0; i < n; i++) {
if(a==even.size()){
c = i;
break;
}
for (int j = 0; j < k; j++) {
if(a == even.size()){
out.println("NO");
return;
}
arr[i][j] = even.get(a++);
}
}
for (int i = c; i < n; i++) {
for (int j = 0; j < k; j++) {
if(o == odd.size()){
if(j!=k - 1){
out.println("NO");
return;
}
}
arr[i][j] = odd.get(o++);
}
}
out.println("YES");
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
out.print(arr[i][j]+" ");
}
out.println();
}
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a, b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return Integer.compare(o.a, this.a);
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public int compareTo(answer1 o) {
return (o.a - this.a);
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (Long i:a) l.add(i);
l.sort(Collections.reverseOrder());
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi= random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
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());
}
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 8 | 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 | 5d59e92a9bbb08bb08ee555270c89f5e | 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 C_OKEA {
public static void main(String[] args) throws IOException {
FastReader in = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
int k = in.nextInt();
if (n % 2 == 0) {
pw.println("YES");
int one = 1, two = 2;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (i % 2 == 0) {
sb.append(two).append(' ');
two += 2;
} else {
sb.append(one).append(' ');
one += 2;
}
}
sb.append('\n');
}
pw.print(sb);
} else if (k == 1) {
pw.println("YES");
for (int i = 1; i <= n; i++) pw.println(i);
} else pw.println("NO");
}
pw.close();
}
public static long Xsum(int[][] a, int i, int j, int n, int m) {
long res = 0;
res += a[i][j];
int tempi = i, tempj = j;
tempi--;
tempj--;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi--;
tempj--;
}
tempi = i;
tempj = j;
tempi++;
tempj--;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi++;
tempj--;
}
tempi = i;
tempj = j;
tempi++;
tempj++;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi++;
tempj++;
}
tempi = i;
tempj = j;
tempi--;
tempj++;
while ((tempi < n && tempj < m) && (tempi >= 0 && tempj >= 0)) {
res += a[tempi][tempj];
tempi--;
tempj++;
}
return res;
}
static void subString(char str[], int n) {
for (int len = 1; len <= n; len++) {
for (int i = 0; i <= n - len; i++) {
int j = i + len - 1;
for (int k = i; k <= j; k++) {
System.out.print(str[k]);
}
System.out.println();
}
}
}
static long gcd(long a, long b) {
if (b == 0) return a;
else return gcd(b, a % b);
}
static boolean isPalindrom(String txt) {
return txt.equals(new StringBuilder(txt).reverse().toString());
}
public static boolean isSorted(int[] arr) {
for (int i = 1; i < arr.length; i++)
if (arr[i] < arr[i - 1]) return false;
return true;
}
static long binaryExponentiation(long x, long n) {
if (n == 0) return 1;
else if (n % 2 == 0) return binaryExponentiation(x * x, n / 2);
else return x * binaryExponentiation(x * x, (n - 1) / 2);
}
public static void Sort(int[] a) {
ArrayList<Integer> lst = new ArrayList<>();
for (int i : a) lst.add(i);
Collections.sort(lst);
for (int i = 0; i < lst.size(); i++) a[i] = lst.get(i);
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
static class Pair implements Comparable<Pair> {
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair ob) {
return first - ob.first;
}
}
static class FastReader {
InputStream is;
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
public FastReader(InputStream is) {
this.is = is;
}
public int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
public String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public String nextLine() {
int c = skip();
StringBuilder sb = new StringBuilder();
while (!isEndOfLine(c)) {
sb.appendCodePoint(c);
c = readByte();
}
return sb.toString();
}
public int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = (num << 3) + (num << 1) + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char[] next(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
public char readChar() {
return (char) skip();
}
public long[] readArrayL(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = nextLong();
return arr;
}
public int[] readArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt();
return arr;
}
public int[][] read2Darray(int n, int m) {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
/*public static int min(int a, int b) {
return Math.min(a, b);
}
public static int max(int a, int b) {
return Math.max(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 8 | 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 | 55531fa407f8f6760b472b9431f95c60 | 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 Code {
public static void main(String[] args) {
Scanner st = new Scanner(System.in);
int t = st.nextInt();
while (t-- > 0) {
int a = st.nextInt();
int b = st.nextInt();
if (a == 1 && b == 1) {
System.out.println("YES");
System.out.println(1);
}
else if (b == 1) {
System.out.println("YES");
for (int i = 1; i <= a; i++) {
System.out.println(i);
}
}
else if (a % 2 == 1) {
System.out.println("NO");
}
else {
int [][] arr = new int [a][b];
int rank = 1;
int rank1 = 2;
for (int i = 0; i < a; i+= 2) {
for (int j = 0; j < b; j++) {
arr[i][j] = rank;
rank += 2;
}
}
for (int i = 1; i < a; i += 2) {
for (int j = 0; j < b; j++) {
arr[i][j] = rank1;
rank1 += 2;
}
}
System.out.println("YES");
for (int i = 0; i < arr.length; i++) {
for (int i1 = 0; i1 < arr[0].length; i1++) {
System.out.print(arr[i][i1] + " ");
}
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 8 | 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 | cb6f383438f845e26993fb8d52049dfa | 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.*;
public class C_1634 {
public static void main(String[] args)
{
FastReader sc = new FastReader();
PrintWriter out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int k=sc.nextInt();
if(k==1) {
out.println("YES");
for(int i=1;i<=n;i++) out.println(i);
continue;
}
if(n%2==1) {
out.println("NO");
continue;
}
out.println("YES");
for(int i=1;i<=n*k;i+=2*k) {
for(int j=i;j<i+2*k;j++) {
if(j%2==1) out.print(j+" ");
}
out.println();
for(int j=i;j<i+2*k;j++) {
if(j%2==0) out.print(j+" ");
}
out.println();
}
}
out.flush();
}
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 8 | 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 | 82714e40e941f94b2ad3d6252dc8493f | 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.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class Practice {
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();
int nums[][]=new int[n][k];
if(k==1) { System.out.println("YES");
for(int i=0;i<n;i++) {
for(int j=0;j<k;j++) {
System.out.print(i+1 +" ");
}
System.out.println();
}
continue;
}
else if(n%2!=0) {
System.out.println("NO");
continue;
}else {
System.out.println("YES");
int p=1;
for(int i=0;i<k;i++) {
for(int j=0;j<n;j++) {
nums[j][i]=p++;
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<k;j++) {
System.out.print(nums[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 8 | 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 | 1ee6fdc2cb57091e629e3457f77b731e | 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.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static int arr[] = new int[100001];
public static int prev[] = new int[100001];
public static int odd[] = new int[100001];
public static int even[] = new int[100001];
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
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;
}
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();
}
}
public static void main(String[] args) {
int t;
Scanner scanner = new Scanner(System.in);
t = scanner.nextInt();
while(t>0){
int n = scanner.nextInt();
int k = scanner.nextInt();
int res = 1;
for(int i=0;i<k;i++){
int sum=0;
for(int j=i;j<k;j++){
sum+=(1+n*j);
if(sum%(j-i+1)==1){
res = 0;
}
}
}
if(res==1){
System.out.println("YES");
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
System.out.print((i+1+n*j)+" ");
}
System.out.println();
}
} else {
System.out.println("NO");
}
t--;
}
}
}
| 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 8 | 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 | 0aa995d4a5f93db5334ecb16647c7b27 | 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_Bajrang_Bali
* @Har_Har_Mahadev
*/
/* Author: Sanat Kumar Dubey (sanat04)
* File: C.java
*/
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
//int t=1;
while (t-- > 0) {
int n = sc.nextInt();
int x = sc.nextInt();
if(x==1) {
System.out.println("YES");
for (int i = 1; i <=n; i++) {
System.out.print(i+" ");
}
System.out.println();
}
else if(n%2==1) System.out.println("NO");
else {
System.out.println("YES");
for (int i = 0; i <1; i++) {
for(int j=1;j<=(n*x);j++){
if(j%2!=0) System.out.print(j+" ");
}
System.out.println();
for (int j = 1; j <=(n*x) ; j++) {
if(j%2==0) System.out.print(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 8 | 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 | 4fee64c7f1c65375e712ca149f7d7653 | 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 OKEA {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for (int i = 0; i < t; i++) {
int n=sc.nextInt();
int k=sc.nextInt();
if (k==1||n%2==0){
System.out.println("YES");
for (int j = 0; j < n; j++) {
for (int l = 0; l < k; l++) {
System.out.print(j+1+l*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 8 | 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 | 8b8f5d353152dbbac766bf3d95fdbaaa | 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 Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
int k=sc.nextInt();
long target=n*k;
List<StringBuilder>res=new ArrayList<>();
if(k>1 && n%2!=0)
{
System.out.println("NO");
continue;
}
int x=1;
int y=2;
System.out.println("Yes");
for(int i=0;i<n;i++)
{
StringBuilder sb=new StringBuilder();
for(int j=0;j<k;j++)
{
if(i%2==0)
{
sb.append(x);
if(j!= k-1)
{
sb.append(' ');
}
x+=2;
}
else{
sb.append(y);
if(j!= k-1)
{
sb.append(' ');
}
y+=2;
}
}
res.add(sb);
}
for(StringBuilder s:res)
{
System.out.println(s);
}
}
} catch(Exception e) {
}
}
}
| 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 8 | 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 | cb2abc7de9ad2adba18709cf496e69bb | 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.*;
import java.lang.Math;
public class Main {
public class MainSolution extends MainSolutionT {
// global vars
public void init(int tests_count){}
public class TestCase extends TestCaseT
{
public Object solve()
{
int n = readInt();
int k = readInt();
int i, j;
if (k==1)
{
out.println("YES");
for (i=0; i<n; i++)
{
out.println(i+1);
}
return null;
}
if (n % 2 == 1)
{
return "NO";
}
int[][] A = new int[n][k];
for (i=0; i<n; i++)
{
for (j=0; j<k; j++)
{
A[i][j] = ((i/2)*k*2) + 2*j + (i%2) + 1;
}
}
out.println("YES");
printMatrix(A, n, k);
return null;
}
}
public void run()
{
int t = multiply_test ? readInt() : 1;
this.init(t);
for (int i = 0; i < t; i++) {
TestCase T = new TestCase();
T.run(i + 1);
}
}
public void loc_params()
{
this.log_enabled = false;
}
public void params(){
this.multiply_test = true;
}
}
public class MainSolutionT extends MainSolutionBase
{
public class TestCaseT extends TestCaseBase
{
}
}
public class MainSolutionBase
{
public boolean is_local = false;
public MainSolutionBase()
{
}
public class TestCaseBase
{
public Object solve() {
return null;
}
public int caseNumber;
public TestCaseBase() {
}
public void run(int cn){
this.caseNumber = cn;
Object r = this.solve();
if ((r != null))
{
out.println(r);
}
}
}
public String impossible(){
return "IMPOSSIBLE";
}
public String strf(String format, Object... args)
{
return String.format(format, args);
}
public BufferedReader in;
public PrintStream out;
public boolean log_enabled = false;
public boolean multiply_test = true;
public void params()
{
}
public void loc_params()
{
}
private StringTokenizer tokenizer = null;
public int readInt() {
return Integer.parseInt(readToken());
}
public long readLong() {
return Long.parseLong(readToken());
}
public double readDouble() {
return Double.parseDouble(readToken());
}
public String readLn() {
try {
String s;
while ((s = in.readLine()).length() == 0);
return s;
} catch (Exception e) {
return "";
}
}
public String readToken() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
} catch (Exception e) {
return "";
}
}
public int[] readIntArray(int n) {
int[] x = new int[n];
readIntArray(x, n);
return x;
}
public void readIntArray(int[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readInt();
}
}
public long[] readLongArray(int n) {
long[] x = new long[n];
readLongArray(x, n);
return x;
}
public void readLongArray(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readLong();
}
}
public void readLongArrayRev(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[n-i-1] = readLong();
}
}
public void logWrite(String format, Object... args) {
if (!log_enabled) {
return;
}
out.printf(format, args);
}
public void readLongArrayBuf(long[] x, int n) {
char[]buf = new char[1000000];
long r = -1;
int k= 0, l = 0;
long d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void readIntArrayBuf(int[] x, int n) {
char[]buf = new char[1000000];
int r = -1;
int k= 0, l = 0;
int d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void printArray(long[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(int[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(long[] a, int n, char dl)
{
long x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(double[] a, int n, char dl)
{
long x;
double y;
int i, l = 0;
for (i=0; i<n; i++)
{
x = (long)a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1 + 10*n;
char[] s = new char[l];
l--;
boolean z;
int j;
for (i=n-1; i>=0; i--)
{
x = (long)a[i];
y = (long)(1000000000*(a[i]-x));
z = false;
if (x<0)
{
x = -x;
z = true;
}
for (j=0; j<9; j++)
{
s[l--] = (char)('0' + (y % 10));
y /= 10;
}
s[l--] = '.';
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(int[] a, int n, char dl)
{
int x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printMatrix(int[][] a, int n, int m)
{
int x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
public void printMatrix(long[][] a, int n, int m)
{
long x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
}
public void run()
{
MainSolution S;
try {
S = new MainSolution();
S.in = new BufferedReader(new InputStreamReader(System.in));
//S.out = System.out;
S.out = new PrintStream(new BufferedOutputStream( System.out ));
} catch (Exception e) {
return;
}
S.params();
S.run();
S.out.flush();
}
public static void main(String args[]) {
Locale.setDefault(Locale.US);
Main M = new Main();
M.run();
}
} | 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 8 | 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 | 3bba522b94cf0590002a934b42c06ede | 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 codeforces;
import java.util.*;
public class OKEA {
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
int t=in.nextInt();
while(t>0)
{
int n=in.nextInt(),k=in.nextInt();
if(k==1)
{
System.out.println("YES");
for(int i=0;i<n;i++)
{
System.out.println(i+1);
}
}
else {
int ev=2,od=1;
int ar[][]=new int[n][k];
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++) {
if((i&1)==0)
{
ar[i][j]=ev;
ev+=2;
}
else{
ar[i][j]=od;
od+=2;
}
}
}
//od-=2;
if((ev-2)>(n*k)||(od-2)>(n*k))
{
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();
}
}
}
t--;
}
}
}
| 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 8 | 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 | f65c6cf077d3e12fca0c1b9dcfdb6ab0 | 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.*;
public class Ac {
static int MOD = 998244353;
static int MAX = (int)1e8;
static Random rand = new Random();
static FastReader in = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = i();
out.println();
out.println();
out.println();
// randArr(10, 10, 10);
// out.println(arr[1] + " " + arr[5]);
while (t-- > 0){
sol();
// out.println(sol(arr));
// int[] ans = sol();
// for (int i = 0; i < ans.length; i++){
// out.print(ans[i] + " ");
// }
// out.println();
// boolean ans = sol();
// if (ans){
// out.println("YES");
// }else{
// out.println("NO");
// }
}
out.flush();
out.close();
}
static void sol(){
int n = i(), m = i();
int sum = n * m;
if ((n & 1) == 1 && m != 1){
out.println("NO");
return;
}
out.println("YES");
boolean flag = true;
int odd = 1, even = 2;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if ((i & 1) == 0){
out.print(odd + " ");
odd += 2;
}else{
out.print(even + " ");
even += 2;
}
}
out.println();
}
}
static void randArr(int num, int n, int val){
for (int i = 0; i < num; i++){
int len = rand.nextInt(n + 1);
System.out.println(len);
for (int j = 0; j < len; j++){
System.out.print(rand.nextInt(val) + 1 + " ");
}
System.out.println();
}
}
static void shuffle(long a[], int n){
for (int i = 0; i < n; i++) {
int t = (int)Math.random() * a.length;
long x = a[t];
a[t] = a[i];
a[i] = x;
}
}
static int gcd(int a, int b){
return a % b == 0 ? a : gcd(b, a % b);
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s(){
return in.nextLine();
}
static int[] inputI(int n) {
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = i();
}
return nums;
}
static long[] inputL(int n) {
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = l();
}
return nums;
}
}
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 8 | 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 | b817fdbead28ec41375ddc16c1f4bc8b | 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 PrintStream out = new PrintStream(System.out);
static LinkedList<LinkedList<Integer>> adj;
static boolean[] vis;
static long ans;
static int target;
static HashMap<ArrayList<Integer>, Integer> seqs;
//static ArrayList<ArrayList<Integer>> lists;
public static void main(String[] args){
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int k = sc.nextInt();
if(n == 1){
if(k == 1){
out.println("YES");
out.println(1);
} else out.println("NO");
continue;
}
if(k == 1){
out.println("YES");
for(int i = 1; i <= n; i++){
out.println(i);
}
continue;
}
int prod = n * k;
if(prod % 2 != 0 || n % 2 != 0){
out.println("NO");
continue;
}
out.println("YES");
int odd = 1;
int even = 2;
while(odd <= prod){
for(int i = 0; i < k; i++){
out.print(odd + " ");
odd += 2;
}
out.println();
}
while(even <= prod){
for(int i = 0; i < k; i++){
out.print(even + " ");
even += 2;
}
out.println();
}
}
}
public static void dfs(int v){
if(vis[v]) return;
vis[v] = true;
LinkedList<Integer> neighbors = adj.get(v);
Iterator<Integer> it = neighbors.iterator();
while(it.hasNext()){
int node = it.next();
if(!vis[node]){
dfs(node);
}
}
}
public static void mergeSort(int[] inputArray){
int n = inputArray.length;
// if input array is empty or contains only one element (meaning already sorted)
if(n < 2){
return;
}
// split input array
int mid = n / 2;
int[] leftHalf = new int[mid];
int[] rightHalf = new int[n - mid];
for(int i = 0; i < mid; i++){
leftHalf[i] = inputArray[i];
}
for(int i = mid; i < n; i++){
rightHalf[i - mid] = inputArray[i];
}
// merge sort both halves
mergeSort(leftHalf);
mergeSort(rightHalf);
// merge halves back into one array
merge(inputArray, leftHalf, rightHalf);
}
public static void merge(int[] inputArray, int[] leftArray, int[] rightArray){
int leftSize = leftArray.length;
int rightSize = rightArray.length;
int i = 0, j = 0, k = 0;
// get smaller element between two arrays
while(i < leftSize && j < rightSize){
if(leftArray[i] <= rightArray[j]){
inputArray[k] = leftArray[i++];
}
else{
for(int x = i ; x < leftSize; x++){
//out.print(leftArray[x] + " " + rightArray[j]);
int u = leftArray[x];
int v = rightArray[j];
adj.get(u).add(v);
adj.get(v).add(u);
}
inputArray[k] = rightArray[j++];
}
k++;
}
// check left for leftovers
while(i < leftSize){
inputArray[k++] = leftArray[i++];
}
// check right for leftovers
while(j < rightSize){
inputArray[k++] = rightArray[j++];
}
}
public static void addUndirectedEdge(int u, int v){
adj.get(u).add(v);
adj.get(v).add(u);
}
}
// custom I/O
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int[] nextIntArray(int length) {
int[] arr = new int[length];
for (int i = 0; i < length; i++)
arr[i] = nextInt();
return arr;
}
}
class Pair {
int a;
int b;
public Pair(int a, int b){
this.a = a;
this.b = 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 8 | 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 | b026b73acb07cd0618a1e61bd507743b | 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 class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
private static final int modulo = 1000000007;
private static int evenCount = 0;
private static int oddCount = 0;
private static long arraySum = 0L;
private static long onesCountInBinary = 0L;
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args){
int testCase = scan.nextInt();
for (int vedant = 0; vedant < testCase; vedant++) {
solve();
}
// out.println(5);
}
private static void solve() {
// ******************** S O L V E M E T H O D *****************
// ************* M A I N L O G I C G O E S H E R E **********
int n = scan.nextInt();
int k = scan.nextInt();
if ((n & 1) == 1) {
if (k != 1) {
printNO();
return;
}
}
int[][] arr = new int[n][k];
int count = 1;
printYES();
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
arr[i][j] = count + (j * n);
}
count++;
}
for (int i = 0; i < n; i++) {
printArray(arr[i]);
}
// **************************** S O L U T I O N E N D S H E R E ****************************
}
private static String readBinaryString() {
String binary = scan.next();
onesCountInBinary = getOnesCountInBinary(binary);
return binary;
}
private static long getOnesCountInBinary(String binary) {
onesCountInBinary = 0L;
for (int i = 0; i < binary.length(); i++) {
if (binary.charAt(i) == '1') {
onesCountInBinary++;
}
}
return onesCountInBinary;
}
private static int lowerBound(int[] arr, int target) {
int starting = 0;
int ending = arr.length - 1;
int answer = -1;
while (starting <= ending) {
int mid = starting + ((ending - starting) / 2);
if (arr[mid] == target) {
ending = mid - 1;
answer = mid;
} else if (arr[mid] < target) {
starting = mid + 1;
} else {
ending = mid - 1;
}
}
return answer;
}
public int upperBound(int[] arr, int target) {
int starting = 0;
int ending = arr.length - 1;
int answer = -1;
while (starting <= ending) {
int mid = starting + ((ending - starting) / 2);
if (arr[mid] == target) {
answer = mid;
starting = mid + 1;
} else if (arr[mid] < target) {
starting = mid + 1;
} else {
ending = mid - 1;
}
}
return answer;
}
private static void printArray(long[] arr) {
StringBuilder output = new StringBuilder();
for (long j : arr) {
output.append(j).append(" ");
}
System.out.println(output);
}
private static ArrayList<Integer> readArrayList(int size) {
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < size; i++) {
arr.add(scan.nextInt());
if ((arr.get(i) & 1) == 0) {
evenCount++;
} else {
oddCount++;
}
arraySum += arr.get(i);
}
return arr;
}
private static void printArray(int[] arr) {
StringBuilder output = new StringBuilder();
for (int j : arr) {
output.append(j).append(" ");
}
System.out.println(output);
}
private static void printYES() {
System.out.println("YES");
}
private static void printNO() {
System.out.println("NO");
}
private static long getMaximumFromList(List<Long> arr) {
long max = Long.MIN_VALUE;
for (Long aLong : arr) {
max = Math.max(max, aLong);
}
return max;
}
public static int binarySearch(int[] arr, int target) {
int starting = 0;
int ending = arr.length - 1;
while (starting < ending) {
int mid = starting + (ending - starting) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
public static int binarySearch(long[] arr, long target) {
int starting = 0;
int ending = arr.length - 1;
while (starting < ending) {
int mid = starting + (ending - starting) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
public static int gcd(int a, int b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public static long gcd(long a, long b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
private static int[] readIntArray(int size) {
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = scan.nextInt();
if ((arr[i] & 1) == 1) {
oddCount++;
} else {
evenCount++;
}
arraySum += arr[i];
}
return arr;
}
private static long[] readLongArray(int size) {
long[] arr = new long[size];
for (int i = 0; i < size; i++) {
arr[i] = scan.nextLong();
if ((arr[i] & 1) == 1) {
oddCount++;
} else {
evenCount++;
}
arraySum += arr[i];
}
return arr;
}
private static boolean isVowel(char charAt) {
charAt = Character.toLowerCase(charAt);
return charAt == 'a' ||
charAt == 'e' ||
charAt == 'i' ||
charAt == 'o' ||
charAt == 'u';
}
private static long binaryToDecimal(String binary) {
int expo = 0;
int index = binary.length() - 1;
long answer = 0;
while (index >= 0) {
answer += Math.pow(2, expo) * Integer.parseInt(String.valueOf(binary.charAt(index)));
index--;
expo++;
}
return answer;
}
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;
}
private static long getFrequency(List<Long> array, long target) {
long count = 0;
for (long element : array) {
if (element == target) {
count++;
}
}
return count;
}
private static int binarySearch(ArrayList<Integer> arr, int target) {
int starting = 0;
int ending = arr.size() - 1;
int mid;
while (starting <= ending) {
mid = starting + (ending - starting) / 2;
if (arr.get(mid) == target) {
return mid;
}
if (arr.get(mid) > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
return -1;
}
private static int binarySearch(ArrayList<Long> arr, long target) {
int starting = 0;
int ending = arr.size() - 1;
int mid;
while (starting <= ending) {
mid = starting + (ending - starting) / 2;
if (arr.get(mid) == target) {
return mid;
}
if (arr.get(mid) > target) {
ending = mid - 1;
} else {
starting = mid + 1;
}
}
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 17 | 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 | 0ca65a235880e380130c2ac6744f2448 | 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 OKEA {
static void printArray(int [][] arr, int n, int k) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
int n, k;
int sum = 0;
boolean broken = false;
System.out.println();
for (int i = 0; i < t; i++) {
n = scanner.nextInt();
k = scanner.nextInt();
broken = false;
int [][] items = new int[n][k];
for (int j = 0; j < n; j++) {
for (int l = 0; l < k; l++) {
items[j][l] = 1 + l * n + j;
sum += items[j][l];
if (sum % (l+1) != 0) {
System.out.println("NO");
broken = true;
break;
}
}
sum = 0;
if (broken) break;
}
if (broken) continue;
System.out.println("YES");
printArray(items, n, k);
}
}
}
| 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 17 | 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 | 617d2efd8a4c585abd1931bf9bbc5b0a | 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 | /**
* Surly
* 26.10.2022
**/
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static PrintWriter pr = new PrintWriter(System.out);
public static void main (String[] args) {
double starttime = System.currentTimeMillis();
int t = sc.nextInt();
while(t-->0) {
int n=sc.nextInt() , k=sc.nextInt();
int m = n*k;
int even = m/2 , odd = m-even;
int need_even = k*(n/2), need_odd = m-need_even;
if(even!=need_even || need_odd!=odd) {
pr.println("NO");
}
else if(k==1){
pr.println("YES");
for(int i=0;i<n;i++) {
pr.println(i+1);
}
}
else if(n==1 && k>1) {
pr.println("NO");
}
else {
pr.println("YES");
print(n,k,1);
print(n,k,2);
}
}
pr.close();
double endtime=System.currentTimeMillis();
System.err.println("Elapsed time : "+(endtime-starttime)+" ms.");
}
static void print(int n , int k, int start) {
for(int i=0;i<n/2;i++) {
for(int j=0;j<k;j++) {
pr.print(start+" ");
start+=2;
}
pr.println();
}
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| 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 17 | 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 | bccc447074ab81ac0e8ac3acbc788858 | 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 static java.lang.Integer.*;
public class codeforces {
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!=0 && k>1)
{
System.out.println("NO");
continue;
}
System.out.println("YES");
int num=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
{
System.out.println(num+" ");
num+=2;
}
if(num>n*k)
num=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 17 | 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 | b909278a625ff5d50b3863fdfa5ad112 | 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;
import java.util.ArrayList;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0; i<t; i++) {
int n=sc.nextInt(); int k=sc.nextInt();
int even=0; int odd=0;
for(int e=1; e<=n*k; e++) { if(e%2==0) { even++; } else{odd++; } }
if(odd%k==0 && even%k==0) { System.out.println("YES");
int y=0; int q=2; int p=1;
while(y<n) { for(int c=0; c<k; c++) { if(p<=n*k) {System.out.print(p+" "); p=p+2; }
else if(q<=n*k) { System.out.print(q+" "); q=q+2;}
}
System.out.println(); y++;
}
}
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 17 | 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 | a8be3fd6430076b96fb95cfd202dfc9a | 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 s{
public static void main(String args[])
{
Scanner Sc=new Scanner(System.in);
int t=1;
t=Sc.nextInt();
for(int tc=1;tc<=t;tc++)
{
int n,k;
n=Sc.nextInt();
k=Sc.nextInt();
int odd=1;
int even=2;
int[][] arr = new int[n][k];
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
{
if(i%2==0)
{
arr[i][j] = odd;
odd+=2;
}
else
{
arr[i][j] = even;
even+=2;
}
}
}
if(odd-2>n*k || even-2>n*k)
{
System.out.println("NO");
}
else
{
System.out.println("YES");
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
{
if(j%2==0)
{
System.out.print(arr[i][j]+" ");
}
else
{
System.out.print(arr[i][j]+" ");
}
}
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 17 | 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 | 56904283d1b14be9a3f44c89e84a7bd2 | 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.*;import java.math.*;
public class OKEA
{
public static void process()throws IOException
{
int n = ni();
int k = ni();
int[][] a = new int[n][k];
if(k==1){
pn("YES");
for(int i=1;i<=n;i++)
pn(i);
return;
}
if(n%2 != 0){
pn("NO");
return;
}
pn("YES");
for(int i=1;i<=n;i++){
int u =i;
for(int j=0;j<k;j++){
p(u+" ");
u+=n;
}
pn("");
}
}
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
long s = System.currentTimeMillis();
int t=1;
t=sc.nextInt();
//int k = t;
while(t-->0) {/*out.print("Case #"+ (k-t) + ": ");*/process();}
out.flush();
System.err.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static String nn()throws IOException{return sc.next();}
static char[] nnch()throws IOException{return sc.next().toCharArray();}
static int[] nia(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
static long[] nla(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static <T>void p(T a){out.print(a);}
static <T>void pn(T a){out.println(a);}
static int gcd(int a, int b) {if (b == 0) return a;return gcd(b, a%b);}
static int lcm(int a ,int b){return (a*b)/gcd(a,b);}
static void swap(char[] a,int i,int j){char c=a[i];a[i]=a[j];a[j]=c;}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | Java | ["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 17 | 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 | 7de1992d8c837c39f2f50e9e3b13d3a2 | 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 | /******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
//System.out.println("Hello World");
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int tt=0;tt<t;tt++)
{
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==1)
System.out.println("NO");
else if(n%2!=0 || (n*k)%2!=0)
System.out.println("NO");
else
{
System.out.println("YES");
int count=0;
for(int i=1;i<=n;i++)
{
count=i;
for(int j=1;j<=k;j++)
{
System.out.print(count+" ");
count+=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 | 07e820d5655aec2440d3a0f6713a873a | 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 | //Code By KB.
import java.beans.Visibility;
import java.io.*;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.nio.channels.AsynchronousCloseException;
import java.security.KeyStore.Entry;
import java.util.*;
import java.util.logging.*;
import java.io.*;
import java.util.logging.*;
import java.util.regex.*;
import javax.management.ValueExp;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.swing.plaf.basic.BasicBorders.SplitPaneBorder;
import javax.swing.text.AbstractDocument.LeafElement;
import javax.xml.validation.Validator;
public class Codeforces {
public static void main(String[] args) {
FlashFastReader in = new FlashFastReader(System.in);
try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));)
{
new Codeforces().solution(in, out);
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
}
public void solution(FlashFastReader in, PrintWriter out)
{
try {
int t = in.nextInt();
while (t-->0) {
int n = in.nextInt();
int k = in.nextInt();
if (n==1&&k==1) {
out.println("YES");
out.println(1);
} else if (k==1) {
out.println("YES");
for (int i = 0; i < n ; i++) {
out.println(i+1);
}
} else if (n%2!=0&&k!=1) {
out.println("NO");
} else {
int a = 1;
int b = 2;
out.println("YES");
int check=0;
for (int i = 0; i < n; i++)
{
if(check==0){
for (int j = 0; j < k; j++)
{
out.print(a+" ");
a+=2;
}
check=1;
}
else if(check==1){
for (int j = 0; j < k; j++)
{
out.print(b+" ");
b+=2;
}
check=0;
}
out.println();
}
}
}
} catch (Exception exception) {
out.println(exception);
}
}
public int countSquares(int[][] matrix) {
int dp [][] = new int [matrix.length+1][matrix[0].length+1];
for (int i = 1; i <=matrix.length; i++) {
for (int j = 1; j <=matrix[0].length; j++) {
if (matrix[i][j]==1) {
if (i>=1&&j>=1) {
dp[i][j] =1+ Math.min(dp[i-1][j], Math.min(dp[i-1][j-1], dp[i][j-1]) );
}
}
x+=dp[i][j];
}
}
return x;
}
HashMap<Integer, Integer> map = new HashMap<>();
public int totalNQueens(int n) {
if (n==1) {
return 1;
}
if (n==2||n==3) {
return 0;
}
dfsNqueens( map , 0 , n );
return x;
}
public void dfsNqueens( HashMap<Integer, Integer>map , int col , int n ) {
if (col == n) {
x++;
return;
}
for (int i = 0; i < n; i++) {
map.put(col, i);
boolean f = true;
for (int j = 0; j < n; j++) {
if (map.containsKey(j)) {
if (map.get(j)==i||Math.abs(col - j)==Math.abs(map.get(j)-i) ) {
f = false;
}
}
}
if (f) {
dfsNqueens(map, col+1, n);
}
}
}
public int[] minOperations(String boxes) {
int a [] = new int[boxes.length()];
for (int i = 0; i < boxes.length(); i++) {
StringBuilder sb = new StringBuilder(boxes);
int x = 0;
for (int j = 0; j < sb.length(); j++) {
if (sb.indexOf("1")<0) {
break;
}
x+=Math.abs(i - sb.indexOf("1"));
sb.setCharAt(sb.indexOf("1"), '0');
}
a[i] = x;
}
return a;
}
public List<List<Integer>> subsets(int[] nums) {
List<Integer> curr = new ArrayList<>();
subsetsGen(0, nums , curr);
return list;
}
public void subsetsGen(int j ,int[] n , List<Integer>curr) {
list.add(curr);
for (int i = j; i < n.length; i++) {
curr.add(n[i]);
subsetsGen(j+1, n, curr);
curr.remove(curr.size()-1);
}
}
boolean visited[]= new boolean[1000];
public int findCircleNum(int[][] isConnected) {
for (int i = 0; i < isConnected.length; i++) {
if (visited[i]!=true) {
x++;
dfsFindProvinces( i , isConnected );
}
}
return x;
}
public void dfsFindProvinces( int i ,int [][]isConnected ) {
visited[i]=true;
for (int j = 0; j < isConnected.length; j++) {
if (isConnected[i][j]==1&&visited[j]!=true) {
dfsFindProvinces(j, isConnected);
}
}
}
public int[] canSeePersonsCount(int[] heights) {
int ans [] = new int[heights.length];
Stack<Integer> stck = new Stack<>();
for (int i = 0; i < heights.length; i++) {
while(!stck.isEmpty()&&heights[stck.peek()]<heights[i]) {
ans[stck.pop()]++;
}
if (!stck.isEmpty()) {
ans[stck.peek()]++;
}
stck.push(i);
}
return ans;
}
public int pathSum(TreeNode root, int targetSum) {
List<Integer> path = new ArrayList<>();
dsfPathSum(root, targetSum , 0 ,path);
pathSum(root.left, targetSum);
pathSum(root.right, targetSum);
for (List<Integer> p : list) {
for (int i = 0; i < p.size() ; i++) {
System.out.print(p.get(i)+" ");
}
System.out.println();
}
return list.size();
}
public void dsfPathSum(TreeNode root , int t , long sum , List<Integer>path ) {
if (root==null) {
return;
}
sum+=root.val ;
path.add(root.val);
if (sum==t) {
list.add(path);
}
dsfPathSum(root.left, t, sum , path);
dsfPathSum(root.right, t, sum , path);
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode l = new ListNode();
int a = 0;
int b =0;
l1 = reverseList(l1);
l2 = reverseList(l2);
while (l1!=null) {
a=a*10+l1.val;
l1=l1.next;
}
while (l2!=null) {
b=b*10+l2.val;
l2=l2.next;
}
a=a+b;
ListNode head = null;
while (a!=0) {
int r = a%10;
if(l==null) {
head= new ListNode(r);
l=head;
}else {
l.next = new ListNode(r);
l= l.next;
}
a/=10;
}
return head;
}
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null)
return head;
ListNode nextNode=head.next;
ListNode newHead=reverseList(nextNode);
nextNode.next=head;
head.next=null;
return newHead;
}
int x=0;
public int uniquePathsIII(int[][] grid) {
int zeros = 0;
int sx=0;
int sy =0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j]==0) {
zeros++;
} else if (grid[i][j]==1) {
sx=i;
sy=j;
}
}
}
dfsUniquePaths(zeros , sx , sy , grid);
return x;
}
public void dfsUniquePaths(int zeros ,int i ,int j , int g[][]) {
if (i<0||i>=g.length||j>=g[0].length||j<0||g[i][j]<0) {
System.out.println(i+" "+j);
return;
}
if (g[i][j]==2) {
if(zeros==0)
x++;
return;
}
g[i][j]=-2;
zeros--;
dfsUniquePaths(zeros, i+1, j, g);
dfsUniquePaths(zeros, i-1, j, g);
dfsUniquePaths(zeros, i, j+1, g);
dfsUniquePaths(zeros, i, j-1, g);
g[i][j]=0;
zeros++;
}
public TreeNode addOneRow(TreeNode root, int val, int depth) {
if (depth==1) {
TreeNode t = new TreeNode(val);
t.left=root;
return t;
}
dfsAddOneRow(root , 1 , val , depth);
return root;
}
public void dfsAddOneRow(TreeNode root , int currD , int val , int depth) {
if (root==null) {
return ;
}
if (currD==depth) {
TreeNode l = root.left;
root.left = new TreeNode(val);
root.left.left = l;
TreeNode r = root.right;
root.right = new TreeNode(val);
root.right.right = r;
}
currD++;
dfsAddOneRow(root.left, currD, val, depth);
dfsAddOneRow(root.right, currD, val, depth);
}
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
int sum =0;
HashSet<List<Integer>> h = new HashSet<List<Integer>>();
for (int i = 0; i < nums.length; i++) {
sum=nums[i];
for (int j = i+1; j < nums.length; j++) {
sum+=nums[j];
int k =j;
n = nums.length;
while (k<n) {
int mid = (k+n)/2;
if ((sum+nums[mid])==0) {
HashSet<Integer> m = new HashSet<>();
m.add(nums[i]);
m.add(nums[j]);
m.add(nums[mid]);
if (m.size()==3) {
List<Integer>x = new ArrayList<>();
x.add(nums[i]);
x.add(nums[j]);
x.add( nums[mid]);
h.add(x);
}
} else if ((sum+nums[mid])>0) {
n=mid;
} else {
k=mid;
}
}
}
}
return list;
}
public int uniquePaths(int m, int n) {
int dp[][]= new int [m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if(i==0&&j==0) {
dp[i][j]=1;
} else if (i==0) {
dp[i][j]=dp[i][j]+dp[i][j-1];
} else if (j==0) {
dp[i][j]=dp[i][j]+dp[i-1][j];
} else {
dp[i][j]+=dp[i][j-1]+dp[i-1][j];
}
}
}
return dp[m-1][n-1];
}
public int maxProfit(int[] prices) {
int p = -1;
return p ;
}
public int trap(int[] height) {
int n = height.length;
int left[] = new int [n];
int right[] = new int [n];
left[0] = height[0];
for (int i = 1; i <n; i++) {
left[i] = Math.max(height[i], left[i-1]);
}
right[n-1]=height[n-1];
for (int i = n-2; i>=0 ; i--) {
right[i] = Math.max(height[i], right[i+1]);
}
for (int i = 1; i < right.length-1; i++) {
int s =Math.min(left[i] , right[i])-height[i];
if (s>0) {
x+=s;
}
}
return x;
}
public int maxProduct(int[] nums) {
int maxSoFar = Integer.MIN_VALUE;
int maxTillEnd = 0;
for (int i = 0; i < nums.length; i++) {
maxTillEnd*=nums[i];
if (maxSoFar<maxTillEnd) {
maxSoFar=maxTillEnd;
}
if (maxTillEnd<=0) {
maxTillEnd=1;
}
}
return maxSoFar;
}
public int maximumScore(int[] nums, int[] multipliers) {
int n = multipliers.length;
int dp[] = new int[n+1];
for (int i = 1; i < multipliers.length; i++) {
for (int j = 1; j < nums.length; j++) {
dp[i] = Math.max(dp[j-1], multipliers[i-1]*nums[i-1]);
}
}
return dp[n];
}
public int islandPerimeter(int[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if(grid[i][j]==1){
dfsIslandPerimeter(i , j , grid);
}
}
}
return x;
}
public void dfsIslandPerimeter(int i , int j , int[][] grid) {
if(i<0||i>=grid.length||j<0||j>grid[0].length) {
x++;
return;
}
if(grid[i][j]==0) {
x++;
return;
}
if(grid[i][j]==1) {
return;
}
dfsIslandPerimeter(i+1, j, grid);
dfsIslandPerimeter(i-1, j, grid);
dfsIslandPerimeter(i, j+1, grid);
dfsIslandPerimeter(i, j-1, grid);
}
public int[] findOriginalArray(int[] changed) {
int n =changed.length;
int m = n/2;
int a[] = new int[m];
if (n<2) {
return new int[]{};
}
if (n%2!=0) {
return new int[]{};
}
TreeMap<Integer , Integer> map = new TreeMap<>();
for (int i = 0; i < changed.length; i++) {
map.put(changed[i], map.getOrDefault(changed[i] ,0)+1);
}
int j =0;
for (Map.Entry<Integer , Integer> e : map.entrySet()) {
if (map.containsKey(e.getKey()*2)&&map.get(e.getKey()*2)>=1&&map.get(e.getKey())>=1) {
map.put(e.getKey(), map.get(e.getKey()) -1);
if(j==n-1)
break;
a[j++] = e.getKey();
}
}
return a;
}
public int jump(int[] nums) {
int n =nums.length;
int jumps =1;
int i =0;
int j =0;
while (i<n) {
int max =0;
for (j=i+1 ;j<=i+nums[i];j++) {
max = Math.max(max , nums[j]+j);
}
jumps++;
i=max;
}
return jumps;
}
static void sort(int[] arr) {
if (arr.length < 2) return;
int mid = arr.length / 2;
int[] left_half = new int[mid];
int[] right_half = new int[arr.length - mid];
// copying the elements of array into left_half
for (int i = 0; i < mid; i++) {
left_half[i] = arr[i];
}
// copying the elements of array into right_half
for (int i = mid; i < arr.length; i++) {
right_half[i - mid] = arr[i];
}
sort(left_half);
sort(right_half);
merge(arr, left_half, right_half);
}
static void merge(int[] arr, int[] left_half, int[] right_half) {
int i = 0, j = 0, k = 0;
while (i < left_half.length && j < right_half.length) {
if (left_half[i] < right_half[j]) {
arr[k++] = left_half[i++];
}
else {
arr[k++] = right_half[j++];
}
}
while (i < left_half.length) {
arr[k++] = left_half[i++];
}
while (j < right_half.length) {
arr[k++] = right_half[j++];
}
}
public int minCostClimbingStairs(int[] cost) {
int n = cost.length;
int dp[] = new int[n+1];
dp[1] = cost[0];
dp[2] = cost[1];
for (int i = 3; i <=n; i++) {
dp[i]=cost[i-1]+Math.min(dp[i-1], dp[i-2]);
}
return dp[n];
}
TreeNode newAns =null;
public TreeNode removeLeafNodes(TreeNode root, int target) {
newAns = root;
dfsRemoveNode(newAns , target);
return newAns;
}
public void dfsRemoveNode(TreeNode root , int t) {
if (root==null) {
return;
}
if (root.val==t) {
if (root.left!=null) {
root=root.left;
}else if (root.right!=null){
root=root.right;
} else {
root=null;
}
}
dfsRemoveNode(root.left, t);
dfsRemoveNode(root.right, t);
}
public int maxProfit(int k, int[] prices) {
int n = prices.length;
if (n <= 1)
return 0;
if (k >= n/2) {
return stocks(prices, n);
}
int[][] dp = new int[k+1][n];
for (int i = 1; i <=k; i++) {
int c= dp[i-1][0] - prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j-1], prices[j] + c);
c = Math.max(c, dp[i-1][j] - prices[j]);
}
}
return dp[k][n-1];
}
public int stocks(int [] prices , int n ) {
int maxPro = 0;
for (int i = 1; i < n; i++) {
if (prices[i] > prices[i-1])
maxPro += prices[i] - prices[i-1];
}
return maxPro;
}
public List<List<Integer>> combine(int n, int k) {
List<Integer> x = new ArrayList<>();
combinations( n , k , 1 ,x);
return list;
}
public void combinations(int n , int k ,int startPos , List<Integer> x) {
if (k==x.size()) {
list.add(new ArrayList<>(x));
//x=new ArrayList<>();
return;
}
for (int i = startPos; i <=n; i++) {
x.add(i);
combinations(n, k, i+1, x);
x.remove(x.size()-1);
}
}
List<List<Integer>> list = new ArrayList<>();
public List<List<Integer>> permute(int[] a) {
List<Integer> x = new ArrayList<>();
permutations(a , 0 ,a.length ,x );
return list ;
}
public void permutations(int a[] , int startPos , int l , List<Integer>x) {
if (l==x.size()) {
list.add(new ArrayList<>(x));
//x=new ArrayList<>();
}
for (int i = 0; i <=l; i++) {
if (!x.contains(a[i])) {
x.add(a[i]);
permutations(a, i, l, x);
x.remove(x.size()-1);
}
}
}
List<String> str = new ArrayList<>();
public List<String> letterCasePermutation(String s) {
casePermutations(s , 0 , s.length() , "" );
return str;
}
public void casePermutations(String s ,int i , int l , String curr) {
if (curr.length()==l) {
str.add(curr);
return;
}
char b = s.charAt(i);
curr+=b;
casePermutations(s, i+1, l, curr);
curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString();
if (Character.isAlphabetic(b)&&Character.isLowerCase(b)) {
curr+=Character.toUpperCase(b);
casePermutations(s, i+1, l, curr);
curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString();
} else if (Character.isAlphabetic(b)) {
curr+=Character.toLowerCase(b);
casePermutations(s, i+1, l, curr);
curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString();
}
}
TreeNode ans= null;
public TreeNode lcaDeepestLeaves(TreeNode root) {
dfslcaDeepestLeaves(root , 0 );
return ans;
}
public void dfslcaDeepestLeaves(TreeNode root , int level) {
if (root==null) {
return;
}
if (depth(root.left)==depth(root.right)) {
ans= root;
return;
} else if (depth(root.left)>depth(root.right)){
dfslcaDeepestLeaves(root.left, level+1);
} else {
dfslcaDeepestLeaves(root.right, level+1);
}
}
public int depth(TreeNode root) {
if (root==null) {
return 0;
}
return 1+Math.max(depth(root.left), depth(root.right));
}
TreeMap<Integer , Integer> m = new TreeMap<>();
public int maxLevelSum(TreeNode root) {
int maxlevel =0;
int mx = Integer.MIN_VALUE;
dfsMaxLevelSum(root , 0);
for (Map.Entry<Integer,Integer> e : m.entrySet()) {
if (e.getValue()>mx) {
mx=e.getValue();
maxlevel=e.getKey()+1;
}
}
return maxlevel;
}
public void dfsMaxLevelSum(TreeNode root , int currLevel) {
if (root==null) {
return;
}
if (!m.containsKey(currLevel)) {
m.put(currLevel, root.val);
} else {
m.put(currLevel, m.get(currLevel)+root.val);
}
dfsMaxLevelSum(root.left, currLevel+1);
dfsMaxLevelSum(root.right, currLevel+1);
}
int teampPerf = 0;
int teampSpeed = 0;
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
int [][] map = new int[efficiency.length][2];
for (int i = 0; i < efficiency.length; i++) {
map[i][0] = efficiency[i];
map[i][1] = speed[i];
}
Arrays.sort(map , (e1 , e2 )-> (e2[0] - e1[0]));
PriorityQueue<Integer> pq = new PriorityQueue<>();
calmax(map , speed , efficiency , pq , k);
return teampPerf ;
}
public void calmax(int [][]map , int s[] , int e[] , PriorityQueue<Integer>pq , int k) {
for (int i = 0 ; i<n ; i++) {
if (pq.size()==k) {
int lowestSpeed =pq.remove();
teampSpeed-=lowestSpeed;
}
pq.add(map[i][1]);
teampSpeed+=map[i][1];
teampPerf=Math.max(teampPerf, teampSpeed*map[i][1]);
}
}
int maxRob = 0;
public int rob(int[] nums) {
int one = 0;
int two = 0;
for (int i = 0; i < nums.length; i++) {
if (i%2==0) {
one+=nums[i];
} else {
two+=nums[i];
}
}
return Math.max(one, two);
}
public int numberOfWeakCharacters(int[][] properties) {
int c =0;
Arrays.sort(properties, (a, b) -> (b[0] == a[0]) ? (a[1] - b[1]) : b[0] - a[0]);
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
pq.offer(0);
for (int i = 0; i < properties.length; i++) {
if (properties[i][1]<pq.peek()) {
c++;
}
pq.offer(properties[i][1]);
}
return c;
}
public boolean canFinish(int numCourses, int[][] prerequisites) {
boolean f = true ;
TreeMap <Integer,Integer> map = new TreeMap<>();
for (int i = 0; i < prerequisites.length; i++) {
if (map.get(prerequisites[i][1])!=null) {
return false;
}
map.put(prerequisites[i][0], prerequisites[i][1]);
}
return f;
}
int n =0;
public int dfsB(int i , int j , char [][]b , char [][]res) {
if (i<0||i>=b.length|j<0||j>=b[0].length||res[i][j]=='.') {
return 0;
}
if (res[i][j]=='X') {
return 1+dfsB(i+1, j, b, res)+dfsB(i, j+1, b, res);
}
return 0;
}
List<Integer> l = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
return dfsRightSideView(root);
}
public List<Integer> dfsRightSideView(TreeNode root) {
if (root!=null) {
l.add(root.val);
}
if (root==null) {
return l;
}
if (root.right==null) {
l.add(root.right.val);
}
dfsRightSideView(root.right);
return l;
}
public void dfsSumNumber(TreeNode root ,int sum , int l ) {
l=l*10 +root.val;
if (root.left!=null) {
dfsSumNumber(root.left,sum , l);
}
if (root.right!=null) {
dfsSumNumber(root.right , sum, l);
}
if (root.left==null && root.right==null) {
sum+=l;
}
// r=l*10+root.val;
l-=root.val;
l/=10;
}
//BFS SOLN
public int[][] updateMatrix(int[][] mat) {
int m =mat.length;
int n = mat[0].length;
Queue<int[]> q = new LinkedList<>();
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
if (mat[i][j]==0) {
q.offer(new int[] { i , j});
} else {
mat[i][j] = Integer.MAX_VALUE;
}
}
}
int dir[][] = {{1 , 0} , {-1 , 0} , {0 , 1} , { 0 , -1}};
while (!q.isEmpty()) {
int zeroPos[] = q.poll();
for (int[] d : dir) {
int r = zeroPos[0]+d[0];
int c = zeroPos[1]+d[1];
if (r<0||r>=mat.length || c<0||c>=mat[0].length||mat[r][c]<=mat[zeroPos[0]][zeroPos[1]]+1) {
continue;
}
q.add(new int[]{r,c});
mat[r][c] = mat[zeroPos[0]][zeroPos[1]]+1;
}
}
return mat;
}
// DFS SOLN
// public int[][] updateMatrix(int[][] mat) {
// int res [][] = new int[mat.length][mat[0].length];
// for (int i = 0; i < mat.length; i++) {
// for (int j = 0; j < mat.length; j++) {
// if (mat[i][j]==0) {
// Dis0(i, j, mat , res, 0);
// }
// }
// }
// return res;
// }
// public void Dis0(int i , int j , int [][]mat, int res[][] , int dist) {
// if (i<0||i>=mat.length||j>=mat[0].length||j<0) {
// return ;
// }
// if (dist==0 ||mat[i][j]==1&&(res[i][j]==0||res[i][j]>dist)) {
// res[i][j]= dist;
// Dis0(i+1, j, mat, res, dist+1);
// Dis0(i-1, j, mat, res, dist+1);
// Dis0(i, j+1, mat, res, dist+1);
// Dis0(i, j-1, mat, res, dist+1);
// }
// // return dist;
// }
public int maxDepth(TreeNode root) {
if (root==null) {
return 0;
}
return maxD(root , 0);
}
public int maxD(TreeNode root , int d ) {
if (root==null) {
return d;
}
return Math.max(maxD(root.left, d+1), maxD(root.right, d+1));
}
public int reverse(int x) {
int sign=x>0?1:-1;
//x=Math.abs(x);
long s= 0;
int r= 0;
int n=x;
while (n!=0) {
r=n%10;
s=s*10+r;
n/=10;
if (s>Integer.MAX_VALUE||s<Integer.MIN_VALUE) {
return 0;
}
}
return (int)s*sign;
}
public boolean checkInclusion(String s1, String s2) {
boolean f = false ;
if (s1.length()>s2.length()) {
return false;
}
for (int i = 0; i < s2.length(); i++) {
HashMap<Character ,Integer> c1 = new HashMap<>();
HashMap<Character ,Integer> c2 = new HashMap<>();
for (int j = 0; j < s1.length(); j++) {
char ch2 = s2.charAt(i+j);
char ch1 = s1.charAt(j);
// if (c1.get(key)) {
// }
}
}
return f;
}
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; }
}
public ListNode middleNode(ListNode head) {
ListNode mid = head;
ListNode last = head;
int k =0;
while (last.next!=null&&last.next.next!=null) {
k++;
mid= mid.next;
last = last.next.next;
}
if (getLen(head)%2==0) {
return mid.next;
}
return mid;
}
public int getLen(ListNode mid) {
int l = 0;
ListNode p = mid;
while (p!=null) {
l++;
p=p.next;
}
return l;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public List<List<Integer>> verticalTraversal(TreeNode root) {
TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map = new TreeMap<>();
dfs(root, 0, 0, map);
List<List<Integer>> list = new ArrayList<>();
for (TreeMap<Integer, PriorityQueue<Integer>> ys : map.values()) {
list.add(new ArrayList<>());
for (PriorityQueue<Integer> nodes : ys.values()) {
while (!nodes.isEmpty()) {
// list.get(list.size() - 1).add(nodes.poll());
}
}
}
return list;
}
private void dfs(TreeNode root, int x, int y, TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map) {
if (root == null) {
return;
}
if (!map.containsKey(x)) {
map.put(x, new TreeMap<>());
}
if (!map.get(x).containsKey(y)) {
map.get(x).put(y, new PriorityQueue<>());
}
map.get(x).get(y).offer(root.val);
dfs(root.left, x - 1, y + 1, map);
dfs(root.right, x + 1, y + 1, map);
}
public long pow (int x ,int n ) {
long y = 1;
if (n==0) {
return y;
}
while (n>0) {
if (n%2!=0) {
y=y*x;
}
x*=x;
n/=2;
}
return y;
}
public long powrec (int x ,int n ) {
long y = 1;
if (n==0) {
return y;
}
y = powrec(x, n/2);
if (n%2==0) {
return y*y;
}
return y*y*x;
}
public int fib(int n) {
if (n==0||n==11) {
return n;
}
return fib(n-1)+fib(n-2);
}
public long gcd(long a , long b)
{
if (b%a==0) {
return a;
}
return gcd(b%a,a);
}
public int maximumElement(int a[])
{
int x = Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
if (a[i]>x) {
x=a[i];
}
}
return x;
}
public int minimumElement(int a[])
{
int x = Integer.MAX_VALUE;
for (int i = 0; i < a.length; i++) {
if (a[i]<x) {
x=a[i];
}
}
return x;
}
public boolean [] sieveOfEratosthenes(int n) {
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
// GFG arraylist function for referral
public ArrayList create2DArrayList(int n ,int k)
{
// Creating a 2D ArrayList of Integer type
ArrayList<ArrayList<Integer> > x
= new ArrayList<ArrayList<Integer> >();
// One space allocated for R0
// Adding 3 to R0 created above x(R0, C0)
//x.get(0).add(0, 3);
int xr = 0;
for (int i = 1; i <= n; i+=2) {
if ((i+k)*(i+1)%4==0) {
x.add(new ArrayList<Integer>());
x.get(xr).addAll(Arrays.asList(i , i+1));
}
else {
x.add(new ArrayList<Integer>());
x.get(xr).addAll(Arrays.asList(i+1 , i));
}
xr++;
}
// Creating R1 and adding values
// Note: Another way for adding values in 2D
// collections
// x.add(
// new ArrayList<Integer>(Arrays.asList(3, 4, 6)));
// // Adding 366 to x(R1, C0)
// x.get(1).add(0, 366);
// // Adding 576 to x(R1, C4)
// x.get(1).add(4, 576);
// // Now, adding values to R2
// x.add(2, new ArrayList<>(Arrays.asList(3, 84)));
// // Adding values to R3
// x.add(new ArrayList<Integer>(
// Arrays.asList(83, 6684, 776)));
// // Adding values to R4
// x.add(new ArrayList<>(Arrays.asList(8)));
// // Appending values to R4
// x.get(4).addAll(Arrays.asList(9, 10, 11));
// // Appending values to R1, but start appending from
// // C3
// x.get(1).addAll(3, Arrays.asList(22, 1000));
// This method will return 2D array
return x;
}
}
class FlashFastReader
{
BufferedReader in;
StringTokenizer token;
public FlashFastReader(InputStream ins)
{
in=new BufferedReader(new InputStreamReader(ins));
token=new StringTokenizer("");
}
public boolean hasNext()
{
while (!token.hasMoreTokens())
{
try
{
String s = in.readLine();
if (s == null) return false;
token = new StringTokenizer(s);
} catch (IOException e)
{
throw new InputMismatchException();
}
}
return true;
}
public String next()
{
hasNext();
return token.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public int[] nextIntsInputAsArray(int n)
{
int[] res = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] nextLongsInputAsArray(int n)
{
long [] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public String nextString() {
return String.valueOf(next());
}
public String[] nextStringsInputAsArray(int n)
{
String [] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = nextString();
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 | f61ced7627f9c833251a4f5d48f780bf | 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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import org.w3c.dom.NamedNodeMap;
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.*;
public class ispcff {
public static void main(String[] args) throws java.lang.Exception {
ispcff.FastReader sc =new FastReader();
int t = sc.nextInt();
for(int i =0;i<t;i++) {
int n = sc.nextInt();
int k = sc.nextInt();
if (k==1) {
System.out.println("YES");
for (int e =1;e<=n*k;e++) {
System.out.println(e);
}
}
else if (n%2!=0) {
System.out.println("NO");
}
else {
System.out.println("YES");
for (int e=1;e<=n;e++) {
int y =e; String yy ="";
for (int ee=0;ee<k;ee++) {
System.out.print(y+" ");y+=n;
}
System.out.println();
}
}
}
}
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 void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(long[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long 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 (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
//Arrays.sort(arr, Comparator.comparingDouble(o -> o[0]));
// q.replace(i, i+1, "4");
// StringBuilder q = new StringBuilder(w);
} | 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 | 55e4e67f9f7fb48ff26a1107a43199d2 | 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 Q1633A {
static int mod = (int) (1e9 + 7);
static void solve() {
int n = i();
int k = i();
if (k == 1) {
System.out.println("YES");
for (int i = 0; i < n; i++) {
System.out.println(i + 1);
}
return;
}
if (n == 1) {
if (k == 1) {
System.out.println("YES");
System.out.println(1);
} else {
System.out.println("NO");
}
return;
}
if (((n * k) & 1) == 0) {
helper(n, k);
} else {
System.out.println("NO");
}
}
static void helper(int n, int k) {
int[][] arr = new int[n][k];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if ((i & 1) == 0) {
if (j == 0) {
if (i - 1 < 0) {
arr[i][j] = 1;
} else {
if (arr[i - 1][k - 1] + 1 > n * k) {
System.out.println("NO");
return;
}
arr[i][j] = arr[i - 1][k - 1] + 1;
}
} else {
if (arr[i][j - 1] + 2 > n * k) {
System.out.println("NO");
return;
}
arr[i][j] = arr[i][j - 1] + 2;
}
} else {// odd
if (j == 0) {
if(arr[i-1][j]+1>n*k){
System.out.println("NO");
return;
}
arr[i][j] = arr[i - 1][j] + 1;
} else {
if(arr[i][j-1]+2>n*k){
System.out.println("NO");
return;
}
arr[i][j] = arr[i][j - 1] + 2;
}
}
}
}
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();
}
}
public static void main(String[] args) {
int test = i();
while (test-- > 0) {
solve();
}
}
// -----> POWER ---> long power(long x, long y) <---- power
// -----> LCM ---> long lcm(long x, long y) <---- lcm
// -----> GCD ---> long gcd(long x, long y) <---- gcd
// -----> NCR ---> long ncr(int n, int r) <---- ncr
// -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<--
// -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<-
// tempstart
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static InputReader in = new InputReader(System.in);
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
} | 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 | a1326ffebcfdf34515ffbb43bfd1c84c | 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 Testing{
public static void main(String[] args)throws Exception{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int k = sc.nextInt();
int[][] ans = new int[n][k];
{
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
for(int i=n*k;i>=1;i--) {
if(i%2==0) {
a.add(i);
}
else {
b.add(i);
}
}
int[] a1 = new int[a.size()];
for(int i=0;i<a.size();i++) {
a1[i] = a.get(i);
}
int[] b1 = new int[b.size()];
for(int i=0;i<b.size();i++) {
b1[i] = b.get(i);
}
int g = a1.length-1;
int h = b1.length-1;
if(a.size()%k==0 && b.size()%k==0) {
System.out.println("YES");
for(int i=0;i<n;i++) {
if(i%2==0) {
for(int j=0;j<k;j++) {
ans[i][j] = b1[h];
h--;
}
}
else {
for(int j=0;j<k;j++) {
ans[i][j] = a1[g];
g--;
}
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<k;j++) {
System.out.print(ans[i][j]+" ");
}
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 | d7587488b298d5cf7d12a694e2172870 | 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 VC770C {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
try {
// Scanner sc=new Scanner(System.in);
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
if(n==1){
if(k==1){
System.out.println("YES");
System.out.println(1);
}
else{
System.out.println("NO");
}
}
else if (k <=1){
System.out.println("YES");
int num=1;
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
System.out.print(num+" ");
num++;
}
System.out.println();
}
} else {
if (n % 2 == 1) {
System.out.println("NO");
} else {
System.out.println("YES");
int num = 1;
for (int i = 0; i < n; i += 2) {
for (int j = 0; j < k; j++) {
System.out.print(num + " ");
num += 2;
}
System.out.println();
}
num = 2;
for (int i = 1; i < n; i += 2) {
for (int j = 0; j < k; j++) {
System.out.print(num + " ");
num += 2;
}
System.out.println();
}
}
}
/*
* int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); }
*/
/*
* long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); }
*/
}
} catch (Exception e) {
return;
}
}
public static class pair {
int ff;
int ss;
pair(int ff, int ss) {
this.ff = ff;
this.ss = ss;
}
}
public static void rsa(int[] arr2) {
Arrays.sort(arr2);
int size = arr2.length;
for (int left = 0; left < size / 2; left++) {
int right = size - left - 1;
int temp = arr2[right];
arr2[right] = arr2[left];
arr2[left] = temp;
}
}
public static boolean checkBothTheArraysSame(int[] a, int[] b, int i, int j) {
int n = a.length;
int[] a1 = a;
int[] b1 = b;
if (i >= 0 && j >= 0) {
for (int k = i; k <= j; k++) {
a1[k] += 1;
}
}
Arrays.sort(a1);
Arrays.sort(b1);
for (int ii = 0; ii < n; ii++) {
if (a1[ii] != b1[ii])
return false;
}
return true;
}
static int BS(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] == element) {
return low;
} else if (arr[high] == element) {
return high;
}
return -1;
}
static int lower_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] < element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] >= element) {
return low;
} else if (arr[high] >= element) {
return high;
}
return -1;
}
static int upper_bound(int[] arr, int l, int r, int element) {
int low = l;
int high = r;
while (high - low > 1) {
int mid = low + (high - low) / 2;
if (arr[mid] <= element) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[low] > element) {
return low;
} else if (arr[high] > element) {
return high;
}
return -1;
}
public static long gcd_long(long a, long b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_long(b, a % b);
}
public static int gcd_int(int a, int b) {
// a/b,a-> dividant b-> divisor
if (b == 0)
return a;
return gcd_int(b, a % b);
}
public static int lcm(int a, int b) {
int gcd = gcd_int(a, b);
return (a * b) / gcd;
}
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());
}
double nextDouble() {
return Double.valueOf(Integer.parseInt(next()));
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
Long nextLong() {
return Long.parseLong(next());
}
}
/*
* JAVA STRING CONTAINS METHOD--------->str1.contains(str2)-->if(str2 is present
* in str1){ return true; } else return false;
*/
public static boolean contains(String main, String Substring) {
boolean flag = false;
if (main == null && main.trim().equals("")) {
return flag;
}
if (Substring == null) {
return flag;
}
char fullstring[] = main.toCharArray();
char sub[] = Substring.toCharArray();
int counter = 0;
if (sub.length == 0) {
flag = true;
return flag;
}
for (int i = 0; i < fullstring.length; i++) {
if (fullstring[i] == sub[counter]) {
counter++;
} else {
counter = 0;
}
if (counter == sub.length) {
flag = true;
return flag;
}
}
return flag;
}
// FACTORISATION OF A NUMBER ---> OPTIMISED CODE
// TC -->O(sqrt(N));
public static void factorize(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
int cnt = 0;
while (n % i == 0) {
n /= i;
cnt++;
}
System.out.println("DIVISOR is " + i + " " + "COUNT is " + cnt);
}
}
if (n != 1) {
// n is prime number
System.out.println("DIVISOR is " + n + " " + "COUNT is " + 1);
}
}
// DISJOINT SET UNINION OPTIMALLY IMPLEMENTED USING PATH COMPRESSION AND RANK
// ARRAY------>>>>>>>>>>>>>>>>>>>>>
// parent[i].ff==parent of i and parent[i].ss gives the rant of the set in which
// i belongs to
public static int find_set(int a, pair[] parent) {
if (parent[a].ff == a)
return a;
return parent[a].ff = find_set(parent[a].ff, parent);
}
public static void union_set(int a, int b, pair[] parent) {
int a_root = find_set(a, parent);
int b_root = find_set(b, parent);
if (a_root == b_root)
return;
if (parent[a_root].ss < parent[b_root].ss) {
parent[a_root].ff = b_root;
} else if (parent[a_root].ss > parent[b_root].ss) {
parent[b_root].ff = a_root;
} else {
parent[b_root].ff = a_root;
parent[a_root].ss++;
}
}
}
/*
* public static boolean lie(int n,int m,int k){ if(n==1 && m==1 && k==0){
* return true; } if(n<1 || m<1 || k<0){ return false; } boolean
* tc=lie(n-1,m,k-m); boolean lc=lie(n,m-1,k-n); if(tc || lc){ return true; }
* return 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 | 2793d25ebbec29f1aeab297ada6093da | 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 codeForces {
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 % 2 == 0) {
System.out.println("YES");
for (int i = 2, j = 1; i <= (n * k); j++, i += 2) {
System.out.print(i + " ");
if (j == k) {
System.out.println();
j = 0;
}
}
for (int i = 1, j = 1; i <= (n * k); j++, i += 2) {
System.out.print(i + " ");
if (j == k) {
j = 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 | feeb9f797fc6d49142d4103aa5ce67ae | 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 codechef {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
int n1=sc.nextInt();
int k=sc.nextInt();
if(k==1){System.out.println("YES");
for (int j = 0; j < n1; j++) {
System.out.println(j+1);
}
}
else if(n1%2==1)
{
System.out.println("NO");
}
else{
int tot=1;
System.out.println("YES");
for (int j = 0; j < n1; j++) {
tot=j+1;
for (int j2 = 0; j2 < k; j2++) {
if(j2!=k)
System.out.print(tot+" ");
else
System.out.println(tot);
tot+=n1;
}
}
}
}
}
}
| 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 | 85ad14080dc6fe39397373d91ae507b3 | 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.*;
import java.math.*;
public class Main{
public static void main(String[]args){
long s = System.currentTimeMillis();
new Solver().run();
System.err.println(System.currentTimeMillis()-s+"ms");
}
}
class Solver{
int N, K;
void process(int testNumber){
N = ni();
K = ni();
if((N & 1) == 1){
if(K == 1){
pn("YES");
for(int i = 1; i <= N; i++){
pn(i);
}
}else{
pn("NO");
}
}else{
int step = N;
pn("YES");
for(int i = 1; i <= N; i++){
for(int j = 1, x = i; j <= K; j++, x += step){
p(x + " ");
}
p("\n");
}
}
}
final long mod = (long)1e9+7l;
boolean DEBUG = true;
FastReader sc;
PrintWriter out;
void run(){
sc = new FastReader();
out = new PrintWriter(System.out);
int t = 1;
t = ni();
for(int test = 1; test <= t; test++){
process(test);
}
out.flush();
}
void trace(Object... o){ if(!DEBUG) return; System.err.println(Arrays.deepToString(o)); };
void pn(Object o){ out.println(o); }
void p(Object o){ out.print(o); }
int ni(){ return Integer.parseInt(sc.next()); }
long nl(){ return Long.parseLong(sc.next()); }
double nd(){ return Double.parseDouble(sc.next()); }
String nln(){ return sc.nextLine(); }
long gcd(long a, long b){ return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){ return (b==0)?a:gcd(b,a%b); }
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();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
}
class pair implements Comparable<pair> {
int first, second;
public pair(int first, int second){
this.first = first;
this.second = second;
}
@Override
public int compareTo(pair ob){
if(this.first != ob.first)
return this.first - ob.first;
return this.second - ob.second;
}
@Override
public String toString(){
return this.first + " " + this.second;
}
static public pair from(int f, int s){
return new pair(f, s);
}
} | 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 | d807c202c81e2d5496198801332dca09 | 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 | /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
// import java.lang.reflect.Array;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=(long)32768;
static StringBuilder sb = new StringBuilder();
/* start */
public static void main(String [] args)
{
// int testcases = 1;
int testcases = i();
// calc();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i();
int k = i();
if(k==1)
{
pl("YES");
for(int i=1;i<=n;i++)
pl(i);
return ;
}
int a[] = new int[(n*k)+1];
if(n%2==0)
{
pl("YES");
for(int i=1;i<=n;i++)
{
int x = find(a);
for(int j=1;j<=k;j++)
{
a[x] = 1;
p(x+" ");
x+=2;
}
pl("");
}
} else {
pl("NO");
}
}
static int find(int a[])
{
for(int i=1;i<=a.length;i++)
{
if(a[i]==0) return i;
}
return -1;
}
/* end */
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 p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
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 long mod(long x) {
return ((x%mod + mod)%mod);
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) ;
y = y >> 1;
x = (x * x);
}
return res;
}
static boolean prime(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;
}
static boolean prime(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;
}
static long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long f, long s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first < p.first)
return 1;
else if (first > p.first)
return -1;
else {
if (second > p.second)
return 1;
else if (second < p.second)
return -1;
else
return 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 | 8096fdcf9617fabf8a6d49acb389db89 | 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 | /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=998244353;
/* start */
public static void main(String [] args)
{
// int testcases = 1;
int testcases = i();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i();
int k = i();
if(k==1)
{
pl("Yes");
for(int i=0;i<n;i++)
pl((i+1));
} else if((n*k)%2==0){
if(n%2!=0)
pl("No");
else {
pl("Yes");
for(int i=1;i<=n;i++)
{
int cnt = i;
for(int j=1;j<=k;j++)
{
p((cnt)+" ");
cnt+=n;
}
pl("");
}
}
}
else pl("No");
}
/* end */
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 p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
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 long mod(long x) {
return ((x%mod + mod)%mod);
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) ;
y = y >> 1;
x = (x * x);
}
return res;
}
static boolean prime(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;
}
static boolean prime(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;
}
static long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
int first, second;
public Pair(int f, int s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first > p.first)
return 1;
else if (first < p.first)
return -1;
else {
if (second < p.second)
return 1;
else if (second > p.second)
return -1;
else
return 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 | 34aca5aba4f30427891660505b0e366a | 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 class FastReader{
BufferedReader br;
StringTokenizer st;
FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st==null||!st.hasMoreElements()) {
try {
st=new StringTokenizer(br.readLine());
}catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=br.readLine();
} catch(IOException e) {
e.printStackTrace();
}
return str;
}
}
static class Pair{
//implements Comparable<Pair> {
long x;
long y;
Pair(long x,long y){
this.x=x;
this.y=y;
}
// @Override
// public int compareTo(Pair o) {
// return this.y-o.y;
// }
}
static long gcd(long a,long b) {
if(b==0) return a;
else
return gcd(b,a%b);
}
static List primeFactors(int n){
List<Integer> ar=new ArrayList<>();
int ct=0;
for(int i=2;i*i<=n;i++) {
if(n%i==0) {
ar.add(i);
ct++;
while(n%i==0) {
n/=i;
}
}
}
if(n>1) {
ct++;
ar.add(n);
}
return ar;
}
static int lb(ArrayList<Long>ar,long k) {
int s=0;
int e=ar.size();
while(s!=e) {
int mid=s+e>>1;
if(ar.get(mid)<k)
s=mid+1;
else
e=mid;
}
if(s==0)
return -1;
return s-1;
}
static long powM(long x,long y,long mod) {
if(y==0)
return 1;
long k=powM(x,y/2,mod);
if(y%2==1) {
return (((((long)1*k*k)%mod)*x)%mod);
}
else {
return (((long)1*k*k)%mod);
}
}
static int ub(ArrayList<Integer>ar,int k) {
int s=0;
int e=ar.size();
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) {
boolean flag=true;
if(n<2)
flag=false;
else {
for(int i=2;i*i<=n;i++){
if(n%i==0) {
flag=false;
break;
}
}
}
return flag;
}
static void pArray(int a[]) {
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
static String sort(String s) {
char ch[]=s.toCharArray();
Arrays.sort(ch);
return new String(ch);
}
// static int[] dij(int n,int src, ArrayList<Edge> ar[]) {
// PriorityQueue<Pair>pq=new PriorityQueue<>();
// pq.add(new Pair(src,0));
// Set<Integer>vis=new HashSet<>();
// int dist[]=new int[n+1];
// dist[src]=0;
// Arrays.fill(dist, Integer.MAX_VALUE);
//
// while(pq.size()>0) {
//
// Pair cur=pq.poll();
// if(!vis.contains(cur.x)){
//
// vis.add(cur.x);
//
// dist[cur.x]=cur.y;
//
// for(Edge e:ar[cur.x]) {
// if(!vis.contains(e.dest)&&dist[e.dest]>cur.y+e.w )
// pq.add(new Pair(e.dest,cur.y+e.w));
// }
// }
// }
// return dist;
// }
//
public static String addCharToString(String str, char c,
int pos)
{
StringBuffer stringBuffer = new StringBuffer(str);
stringBuffer.insert(pos, c);
return stringBuffer.toString();
}
static int BigMod(String num, int a)
{
int res = 0;
for (int i = 0; i < num.length(); i++)
res = (res * 10 + (int)num.charAt(i));
return res;
}
static boolean isSquare(int n) {
for(int i=1;i*i<=n;i++) {
if((i*i)==n)
return true;
}
return false;
}
static long firstsetBitPower(long n)
{
int k = (int)(Math.log(n) / Math.log(2));
return 1 << k;
}
public static void main(String[] args) {
try {
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\n1");
continue;
}
int temp=n*k;
List<Integer>oddl=new ArrayList<>();
List<Integer>even=new ArrayList<>();
for(int i=1;i<=(n*k);i++) {
if(i%2==1) {
oddl.add(i);
}
else {
even.add(i);
}
}
if(k==1) {
System.out.println("YES");
for(int i=0;i<oddl.size();i++) {
System.out.println(oddl.get(i)+" ");
}
for(int i=0;i<even.size();i++) {
System.out.println(even.get(i)+" ");
}
continue;
}
if(n%2==1) {
System.out.println("NO");
continue;
}
int p=0;
System.out.println("YES");
int q=0;
for(int i=0;i<n;i++) {
if(i%2==0) {
for(int j=0;j<k;j++) {
System.out.print(oddl.get(p++)+" ");
}
}
else {
for(int j=0;j<k;j++) {
System.out.print(even.get(q++)+" ");
}
}
System.out.println();
}
}
}catch(Exception e) {return;}
}
}
//https://www.codechef.com/MP3TO401?order=desc&sortBy=successful_submissions
| 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 | c8481c274832b82419a0a54f694c7cad | 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.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;
import static java.lang.System.out;
import static java.lang.System.err;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main
{
static FastReader sc;
// static FastWriter out;
public static void main(String hi[]){
initializeIO();
sc=new FastReader();
// FastWriter out=new FastWriter();
int t=sc.nextInt();
// boolean[] seave=sieveOfEratosthenes((int)(1e6));
// int t=1;
while(t--!=0){
long n=sc.nextLong();
long m=sc.nextLong();
long sum=sumOfAp(1,m,n);
debug(sum);
if(n%2!=0&&m!=1)out.println("No");
else{
print("yes");
for(int i=1;i<=n;i++){
int v=i;
for(int j=0;j<m;j++){
out.print(v+" ");
v+=n;
}
out.println();
}
}
}
// print(solve(nums,n,m,it,jt));
// System.out.println(String.format("%.10f", max));
}
// long[]
private static long sumOfAp(long a,long n,long d){
long val=(n*( 2*a+((n-1)*d)));
return val/2;
}
//geometrics
private static double areaOftriangle(double x1,double y1,double x2,double y2,double x3,double y3){
double[] mid_point=midOfaLine(x1,y1,x2,y2);
debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3);
double height=distanceBetweenPoints(mid_point[0],mid_point[1],x3,y3);
double wight=distanceBetweenPoints(x1,y1,x2,y2);
debug(height+" "+wight);
return (height*wight)/2;
}
private static double distanceBetweenPoints(double x1,double y1,double x2,double y2){
double x=x2-x1;
double y=y2-y1;
return sqrt(Math.pow(x,2)+Math.pow(y,2));
}
private static double[] midOfaLine(double x1,double y1,double x2,double y2){
double[] mid=new double[2];
mid[0]=(x1+x2)/2;
mid[1]=(y1+y2)/2;
return mid;
}
private static StringBuilder reverseString(String s){
StringBuilder sb=new StringBuilder(s);
int l=0,r=sb.length()-1;
while(l<=r){
char ch=sb.charAt(l);
sb.setCharAt(l,sb.charAt(r));
sb.setCharAt(r,ch);
l++;
r--;
}
return sb;
}
private static String decimalToString(int x){
return Integer.toBinaryString(x);
}
private static boolean isPallindrome(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;
r--;
}
return true;
}
private static StringBuilder removeLeadingZero(StringBuilder sb){
int i=0;
while(i<sb.length()&&sb.charAt(i)=='0')i++;
// debug("remove "+i);
if(i==sb.length())return new StringBuilder();
return new StringBuilder(sb.substring(i,sb.length()));
}
private static int stringToDecimal(String binaryString){
// debug(decimalToString(n<<1));
return Integer.parseInt(binaryString,2);
}
private static int stringToInt(String s){
return Integer.parseInt(s);
}
private static String toString(int val){
return String.valueOf(val);
}
private static void print(String s){
out.println(s);
}
private static void debug(String s){
err.println(s);
}
private static int charToInt(char c){
return ((((int)(c-'0'))%48));
}
private static void print(double s){
out.println(s);
}
private static void print(float s){
out.println(s);
}
private static void print(long s){
out.println(s);
}
private static void print(int s){
out.println(s);
}
private static void debug(double s){
err.println(s);
}
private static void debug(float s){
err.println(s);
}
private static void debug(long s){
err.println(s);
}
private static void debug(int s){
err.println(s);
}
private 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;
}
//read graph
private static List<List<Integer>> readUndirectedGraph(int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readUndirectedGraph(int[][] intervals,int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<intervals.length;i++){
int x=intervals[i][0];
int y=intervals[i][1];
graph.get(x).add(y);
graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int[][] intervals,int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<intervals.length;i++){
int x=intervals[i][0];
int y=intervals[i][1];
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
private static List<List<Integer>> readDirectedGraph(int n){
List<List<Integer>> graph=new ArrayList<>();
for(int i=0;i<=n;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
graph.get(x).add(y);
// graph.get(y).add(x);
}
return graph;
}
static String[] readStringArray(int n){
String[] arr=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.next();
}
return arr;
}
private static Map<Character,Integer> freq(String s){
Map<Character,Integer> map=new HashMap<>();
for(char c:s.toCharArray()){
map.put(c,map.getOrDefault(c,0)+1);
}
return map;
}
static boolean[] sieveOfEratosthenes(long n){
boolean prime[] = new boolean[(int)n + 1];
for (int i = 2; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true){
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static long Kadens(List<Long> prices) {
long sofar=0;
long max_v=0;
for(int i=0;i<prices.size();i++){
sofar+=prices.get(i);
if (sofar<0) {
sofar=0;
}
max_v=Math.max(max_v,sofar);
}
return max_v;
}
static boolean isMemberAC(int a, int d, int x){
// If difference is 0, then x must
// be same as a.
if (d == 0)
return (x == a);
// Else difference between x and a
// must be divisible by d.
return ((x - a) % d == 0 && (x - a) / d >= 0);
}
private static void sort(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(int[] arr){
int n=arr.length;
List<Integer> li=new ArrayList<>();
for(int x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sort(double[] arr){
int n=arr.length;
List<Double> li=new ArrayList<>();
for(double x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(double[] arr){
int n=arr.length;
List<Double> li=new ArrayList<>();
for(double x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sortReverse(long[] arr){
int n=arr.length;
List<Long> li=new ArrayList<>();
for(long x:arr){
li.add(x);
}
Collections.sort(li,Collections.reverseOrder());
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static void sort(long[] arr){
int n=arr.length;
List<Long> li=new ArrayList<>();
for(long x:arr){
li.add(x);
}
Collections.sort(li);
for (int i=0;i<n;i++) {
arr[i]=li.get(i);
}
}
private static long sum(int[] arr){
long sum=0;
for(int x:arr){
sum+=x;
}
return sum;
}
private static long evenSumFibo(long n){
long l1=0,l2=2;
long sum=0;
while (l2<n) {
long l3=(4*l2)+l1;
sum+=l2;
if(l3>n)break;
l1=l2;
l2=l3;
}
return sum;
}
private static void initializeIO(){
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
System.setErr(new PrintStream(new FileOutputStream("error.txt")));
} catch (Exception e) {
// System.err.println(e.getMessage());
}
}
private static int maxOfArray(int[] arr){
int max=Integer.MIN_VALUE;
for(int x:arr){
max=Math.max(max,x);
}
return max;
}
private static long maxOfArray(long[] arr){
long max=Long.MIN_VALUE;
for(long x:arr){
max=Math.max(max,x);
}
return max;
}
private static int[][] readIntIntervals(int n,int m){
int[][] arr=new int[n][m];
for(int j=0;j<n;j++){
for(int i=0;i<m;i++){
arr[j][i]=sc.nextInt();
}
}
return arr;
}
private static long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
private static int[] readIntArray(int n){
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
return arr;
}
private static double[] readDoubleArray(int n){
double[] arr=new double[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextDouble();
}
return arr;
}
private static long[] readLongArray(int n){
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextLong();
}
return arr;
}
private static void print(int[] arr){
out.println(Arrays.toString(arr));
}
private static void print(long[] arr){
out.println(Arrays.toString(arr));
}
private static void print(String[] arr){
out.println(Arrays.toString(arr));
}
private static void print(double[] arr){
out.println(Arrays.toString(arr));
}
private static void debug(String[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(int[] arr){
err.println(Arrays.toString(arr));
}
private static void debug(long[] arr){
err.println(Arrays.toString(arr));
}
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;
}
}
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();
}
}
static class Dsu {
int[] parent, size;
Dsu(int n) {
parent = new int[n + 1];
size = new int[n + 1];
for (int i = 0; i <= n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int findParent(int u) {
if (parent[u] == u) return u;
return parent[u] = findParent(parent[u]);
}
private boolean union(int u, int v) {
// System.out.println("uf "+u+" "+v);
int pu = findParent(u);
// System.out.println("uf2 "+pu+" "+v);
int pv = findParent(v);
// System.out.println("uf3 " + u + " " + pv);
if (pu == pv) return false;
if (size[pu] <= size[pv]) {
parent[pu] = pv;
size[pv] += size[pu];
} else {
parent[pv] = pu;
size[pu] += size[pv];
}
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 | 8a7c6e1d13dd3f45cdc0beced49eb906 | 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.*;
public class C {
static PrintWriter out;
public static void main(String[] args) {
FastReader in = new FastReader();
out = new PrintWriter(System.out);
long t = in.nextLong();
long test = 1;
while (test <= t) {
// out.println(solve(in));
solve(in);
test++;
}
out.close();
}
/*--------------------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------*/
private static void solve(FastReader in) {
int n = in.nextInt();
int k = in.nextInt();
if (k == 1) {
int cnt = 1;
out.println("YES");
for (int i = 0; i < n; i++) {
out.println(cnt);
cnt++;
}
return;
}
boolean b = check(n, k);
if (!b) {
out.println("NO");
return;
}
out.println("YES");
int cnt1 = 1, cnt2 = cnt1 + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (i % 2 == 0) {
out.print(cnt1 + " ");
cnt1 += 2;
} else {
out.print(cnt2 + " ");
cnt2 += 2;
}
}
out.println();
if (i % 2 == 1) {
cnt2 -= 2;
cnt1 = cnt2 + 1;
cnt2 = cnt1 + 1;
}
}
}
private static boolean check(int n, int k) {
int max = 0;
int cnt1 = 1, cnt2 = cnt1 + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if (i % 2 == 0) {
max = Math.max(max, cnt1);
cnt1 += 2;
} else {
max = Math.max(max, cnt2);
cnt2 += 2;
}
}
if (i % 2 == 1) {
cnt2 -= 2;
cnt1 = cnt2 + 1;
cnt2 = cnt1 + 1;
}
}
return max <= n * k;
}
//
// private static long solve(FastReader in) {
// int max = max(1, 2);
// int min = min(6, 2);
// return 0;
//
// }
//
// private static String solve(FastReader in) {
// int max = max(1, 2);
// int min = min(6, 2);
// return "";
//
// }
/*--------------------------------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------------------------------------*/
private static int gcd(int a, int b) {
return b == 0 ? a : (gcd(b, a % b));
}
static boolean[] sieveOfEratosthenes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
static void sort(int[] a) {
List<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 long modPow(long a, long b, long m) {
long res = 1;
a %= m;
while (b > 0) {
if ((b & 1) != 0) {
res = res * a;
res %= m;
}
b >>= 1;
a *= a;
a %= m;
}
return res;
}
private static class Pair implements Comparable<Pair> {
int ff, ss;
Pair(int x, int y) {
this.ff = x;
this.ss = y;
}
public int compareTo(Pair o) {
return this.ff == o.ff ? this.ss - o.ss : this.ff - o.ff;
}
}
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[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
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 | e31182228f8a2f4169912f30e850a354 | 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 go = new Scanner(System.in);
int t = go.nextInt();
while (t-->0){
int n = go.nextInt();
int k = go.nextInt();
if (k==1){
System.out.println("YES");
for (int i=1; i<=n*k;i++){
System.out.println(i);
}
}else if (k == 2){
if (n%2 ==0){
System.out.println("YES");
for (int i=1; i<=n*k/2;i++){
if (i == n*k/2){
System.out.println(i + " " + n*k);
}else {
System.out.println(i + " " + (n*k-i));
}
}
}else {
System.out.println("NO");
}
}else if (k>=3){
// if (n == 1){
// System.out.println("NO");
// }else if (n == 2){
// System.out.println("1 3 5");
// System.out.println("2 4 6");
// }else {
//
// }
if (n % 2 == 1){
System.out.println("NO");
}else {
System.out.println("YES");
int now = 1;
int time = 0;
for (int i=0; i<n; i++){
for (int j = 0; j < k; j++){
int f = now + j*2;
System.out.print(f + " ");
}
if (time % 2 == 0){
now += 1;
time ++;
}else {
now += k*2-1;
time ++;
}
System.out.println();
}
}
}
}
}
}
// 8421
// 1010
// | 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 | 4e09132b16d8e839b57bee1135733434 | 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 class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static FastReader obj = new FastReader();
public static PrintWriter out = new PrintWriter(System.out);
public static void sort(long[] a) {
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < a.length; i++)
arr.add(a[i]);
Collections.sort(arr);
for (int i = 0; i < arr.size(); i++)
a[i] = arr.get(i);
}
public static void revsort(long[] a) {
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < a.length; i++)
arr.add(a[i]);
Collections.sort(arr, Collections.reverseOrder());
for (int i = 0; i < arr.size(); i++)
a[i] = arr.get(i);
}
//Cover the small test cases like for n=1 .
public static class pair {
long a;
long b;
pair(long x, long y) {
a = x;
b = y;
}
}
public static long l() {
return obj.nextLong();
}
public static int i() {
return obj.nextInt();
}
public static String s() {
return obj.next();
}
public static long[] l(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = l();
return arr;
}
public static int[] i(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = i();
return arr;
}
public static long ceil(long a, long b) {
return (a + b - 1) / b;
}
public static void p(long val) {
out.println(val);
}
public static void p(String s) {
out.println(s);
}
public static void pl(long[] arr) {
for (int i = 0; i < arr.length; i++) {
out.print(arr[i] + " ");
}
out.println();
}
public static void p(int[] arr) {
for (int i = 0; i < arr.length; i++) {
out.print(arr[i] + " ");
}
out.println();
}
public static void sortpair(Vector<pair> arr) {
//ascending just change return 1 to return -1 and vice versa to get descending.
//compare based on value of pair.a
arr.sort(new Comparator<pair>() {
public int compare(pair o1, pair o2) {
long val = o1.a - o2.a;
if (val == 0)
return 0;
else if (val > 0)
return 1;
else
return -1;
}
});
}
// Take of the small test cases such as when n=1,2 etc.
// remember in case of fenwick tree ft is 1 based but our array should be 0 based.
// in fenwick tree when we update some index it doesn't change the value to val but it
// adds the val value in it so remember to add val-a[i] instead of just adding val.
//in case of finding the inverse mod do it (biexpo(a,mod-2)%mod + mod )%mod
public static void main(String[] args) {
int len = i();
while (len-- != 0) {
int n = i();
int k=i();
int tut=n*k;
if(n==1 )
{
if(k==1) {
out.println("YES");
out.println(1);
}
else out.println("NO");
}
else if(k==1)
{
out.println("YES");
for(int i=1;i<=n;i++)out.println(i);
}
else if(tut%2!=0)
{
out.println("NO");
}
else
{
int[][] num=new int[n][k];
int a=1,b=2;
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
{
if(i%2==0)
{
num[i][j]=a;
a+=2;
}
else
{
num[i][j]=b;
b+=2;
}
}
}
a-=2;
b-=2;
if(a<=tut && b<=tut) {
out.println("YES");
for(int i=0;i<n;i++)
{
for(int j=0;j<k;j++)
{
out.print(num[i][j]+" ");
}
out.println();
}
}
else 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 | 00ea0296f49fbc4fef49b64a2479e25c | 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 Krishna
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Comparator;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class A{
// author: Tarun Verma
static FastScanner sc = new FastScanner();
static int inf = Integer.MAX_VALUE;
static long mod = 1000000007;
static BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(System.out));
static PrintWriter out=new PrintWriter(System.out);
/* Common Mistakes By Me
* make sure to read the bottom part of question
* special cases (n=1?)
* in BIT MASKING try don't forget a^b=c == a^c=b
* READ READ AND READ THE Test Cases VERY PROPERLY AND NEVER BE OVERCONFIDENT
* Always Reset vis,adj array upto n+1 otherwise can cause TLE
* Try to see array from back in increase and decrease questions
*/
static ArrayList<Integer> a = new ArrayList<>();
static ArrayList<Integer> b = new ArrayList<>();
public static void solve() {
int n = sc.nextInt();
int k = sc.nextInt();
if(k == 1) {
out.println("YES");
for(int i =0;i<n;i++) {
out.println((i + 1) );
}
return;
}
int temp = n * k / 2;
if(temp % k != 0 ||temp % k != 0) {
out.println("NO");
return;
}
out.println("YES");
int i = 0;
while(i < temp) {
int tmp = k;
while(tmp != 0) {
tmp--;
out.print(a.get(i) + " ");
i++;
}out.println();
}
i = 0;
while(i < temp) {
int tmp = k;
while(tmp != 0) {
tmp--;
out.print(b.get(i) + " ");
i++;
}out.println();
}
}
public static void main(String[] args) {
int t = 1;
for(int i =0;i<500*500;i++) {
if((i + 1) % 2 == 0) {
a.add(i + 1);
} else {
b.add(i + 1);
}
}
t = sc.nextInt();
outer: for (int tt = 0; tt < t; tt++) {
solve();
}
out.close();
}
////////////////////////////////////////////////////////////////////////////////////
////////////////////DO NOT BELOW THIS LINE //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
static Comparator<pair> comp = new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
if(o1.fr == o2.fr) {
return o1.sc - o2.sc;
}
return o1.fr - o2.fr;
}
};
static int max(int a, int b) { return Math.max(a, b);}
static int min(int a, int b) {return Math.min(a, b);}
static long max(long a, long b){return Math.max(a, b);}
static long min(long a, long b) {return Math.min(a, b);}
static void sort(int arr[]) {
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i: arr) a.add(i);
Collections.sort(a);
for(int i=0;i<arr.length;i++) arr[i] = a.get(i);
}
static void sort(long arr[]) {
ArrayList<Long> a = new ArrayList<Long>();
for(long i: arr) a.add(i);
Collections.sort(a);
for(int i=0;i<arr.length;i++) arr[i] = a.get(i);
}
static int abs(int a) {return Math.abs(a);}
static long abs(long a) {return Math.abs(a);}
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 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());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] read2dArray(int n, int m) {
int arr[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
ArrayList<Integer> readArrayList(int n) {
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int a = nextInt();
arr.add(a);
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class pair {
int fr, sc;
pair(int fr, int sc) {
this.fr = fr;
this.sc = sc;
}
}
////////////////////////////////////////////////////////////////////////////////////
}
| 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 | cf89e1a2f2dc32837090a60e47f6ee03 | 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 ProblemA{
static long mod = 1000000007L;
// map.put(a[i],map.getOrDefault(a[i],0)+1);
// map.putIfAbsent;
static MyScanner sc = new MyScanner();
//<----------------------------------------------WRITE HERE------------------------------------------->
static void solve(){
int n = sc.nextInt();
int k = sc.nextInt();
if(n == 1 && k==1) {
out.println("YES");
for(int i=1;i<=k;i++)out.print(i+" ");
out.println();
return;
}
if(k == 1) {
out.println("YES");
for(int i=1;i<=n;i++) {
out.println(i+" ");
}
return;
}
if(n%2 != 0) {
out.println("NO");
return;
}
out.println("YES");
int od = 1;
int ev = 2;
for(int i=0;i<n;i++) {
if(i%2 == 0) {
for(int j=0;j<k;j++) {
out.print(od+" ");
od+=2;
}
}else {
for(int j=0;j<k;j++) {
out.print(ev+" ");
ev+=2;
}
}
out.println();
}
}
//<----------------------------------------------WRITE HERE------------------------------------------->
static void swap(char[] a,int i,int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static int LowerBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int UpperBound(int a[], int x) {
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static void priArr(int[] a) {
for(int i=0;i<a.length;i++) {
out.print(a[i] + " ");
}
out.println();
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static void reverse(int[] arr){
int i = 0;
int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
static class Pair implements Comparable<Pair>{
int val;
int ind;
Pair(int v,int f){
val = v;
ind = f;
}
public int compareTo(Pair p){
return this.ind - p.ind;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
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;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
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 | 0126204a226fa4e1f82fed424f854608 | 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 atcoder.com;
import java.util.*;
import java.io.*;
public class Main {
public static StringBuffer res = new StringBuffer("");
public static void main(String[] args) throws IOException {
//FileInput();
ManualInput();
}
public static void ManualInput() throws NumberFormatException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int t = 1;
t = Integer.parseInt(reader.readLine());
while (t-- > 0) {
String[] str = new String[1];
for (int i = 0; i < str.length; i++) str[i] = reader.readLine();
solve(str);
}
System.out.println(res);
}
public static void FileInput() throws FileNotFoundException {
File file = new File("input.txt");
Scanner sc = new Scanner(file);
String s = sc.nextLine();
while (sc.hasNextLine()) {
String[] str = new String[1];
for (int i = 0; i < str.length; i++) {
str[i] = sc.nextLine();
}
solve(str);
}
System.out.println(res);
sc.close();
}
private static void solve(String[] str) {
String[] nums = str[0].split(" ");
int n = Integer.parseInt(nums[0]);
int k = Integer.parseInt(nums[1]);
if (k == 1 || n % 2 == 0) {
res.append("YES\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
res.append((j*n+i+1) + " ");
}
res.append("\n");
}
} else {
res.append("NO\n");
}
//----------------------------------------------------------------
//res.append("\n");
}
}
| 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 | 8d2279bd5bd524b2fd3fdbf6a5311607 | 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.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Training {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
for (int i = 1; i <= t; i++) {
int n, k;
n = sc.nextInt();
k = sc.nextInt();
if (k == 1) {
System.out.println("YES");
for (int j = 1; j <= n; j++)
System.out.println(j);
continue;
}
if (n % 2 == 1) {
System.out.println("NO");
continue;
}
System.out.println("YES");
for (int j = 1; j <= n; j++) {
for (int l = 0; l < k; l++) {
System.out.print(j + l * n + " ");
}
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 | bb5f895c55085c4e0dfa89e46e91c906 | 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 sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt(), k = sc.nextInt();
int count = (n*k) / 2;
if(count % k != 0 || count/k != (n+1)/2){
if(k == 1){
System.out.println("YES");
for(int i=1;i<=n;i++) System.out.println(i);
} else {
System.out.println("NO");
}
} else {
System.out.println("YES");
int o = 1, e = 2;
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
if(i%2 == 0){
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 | f77a605233eec20fe3c6e7bf091ca277 | 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 sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt(), k = sc.nextInt();
int count = (n*k) / 2;
if((n*k) % 2 != 0 || count % k != 0 || count/k != (n+1)/2 || n == 1){
if(k == 1){
System.out.println("YES");
for(int i=1;i<=n;i++) System.out.println(i);
} else {
System.out.println("NO");
}
} else {
System.out.println("YES");
int o = 1, e = 2;
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
if(i%2 == 0){
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 | ab7dd52a8bac70eb26bebf8c0a92fdba | 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 OKEA {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int i=0;i<t;i++){
int n=s.nextInt();
int k=s.nextInt();
if(k==1){
System.out.println("YES");
for(int j=0;j<n;j++){
System.out.println((j+1));
}
}
else{
if(n%2==1){
System.out.println("NO");
}
else{
System.out.println("YES");
for(int p=0;p<n;p++){
for(int j=0;j<k;j++){
System.out.print(j*n+p+1+" ");
}
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 | fb50b93f69c4fcf1d7325cc61d07103e | 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 C {
static FastReader in = new FastReader();
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static StringBuilder sb = new StringBuilder();
public static void main(String args[]) throws IOException {
//int t=1;
int t = in.nextInt();
while (t>0) {
solve();
t--;
}
System.out.println(sb);
}
/********** code here ******************/
static void solve() {
int arr[] = in.readintarray(2);
int n = arr[0];
int k = arr[1];
if(k==1){
sb.append("YES\n");
for(int i=1;i<=n;i++){sb.append(i+"\n");}
// }else if(n==1){
// if(!even(k)){
// sb.append("YES\n");
// for(int i=1;i<=k;i++){sb.append(i+" ");}
// }else sb.append("NO\n");
}else if(even(n)){
sb.append("YES\n");
int c1,c2;
c1=1;
c2=2;
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
if(even(i)){
sb.append(c1+" ");
c1+=2;
}else{
sb.append(c2+" ");
c2+=2;
}
}
sb.append("\n");
}
}else sb.append("NO\n");
}
/***************************************/
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;
}
int[] readintarray(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
long[] readlongarray(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
}
static <E> void println(E res) {
System.out.println(res);
}
static <E> void print(E res) {
System.out.print(res);
}
static String string(int a) {
return Integer.toString(a);
}
static String string(char a) {
return Character.toString(a);
}
static int integer(String a) {
return Integer.parseInt(a);
}
static int len(String a) {
return a.length();
}
static boolean even(int a) {
return (a & 1) == 0 ? true : false;
}
static int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
static int arr_gcd(int arr[], int n) {
int result = 0;
for (int element: arr) {
result = gcd(result, element);
if(result == 1) return 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 | 5cfbc465b906450ca377f8b253a310dc | 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 {
static boolean isPrime(int n) {
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
return true;
}
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 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());
}
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());
}
double nextDouble() {
return Double.parseDouble(next());
}
ArrayList<Integer> readList(int n) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(nextInt());
return list;
}
}
static class Pair {
int one;
int two;
public Pair(int one, int two) {
this.one = one;
this.two = two;
}
}
static int mod = 1000000007;
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
outer: while (t-- > 0) {
int n = fs.nextInt();
int k = fs.nextInt();
if (n % 2 == 1) {
if (k == 1) {
System.out.println("YES");
printNKinK(n, k);
} else {
System.out.println("NO");
}
} else {
System.out.println("YES");
printNKinK(n, k);
}
}
}
private static void printNKinK(int n, int k) {
int e = 2, o = 1;
boolean flag = true;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (flag) {
System.out.print(o + " ");
o += 2;
} else {
System.out.print(e + " ");
e += 2;
}
}
System.out.println();
flag = !flag;
}
}
}
| 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 | 1958e0a0249f1ef58efc8d3378201b6e | 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 {
public static void main(String args[]) {
Scanner scn = new Scanner (System.in);
int x= scn.nextInt();
while(x>0){
int n= scn.nextInt();
int k= scn.nextInt();
int n1 = (n*k);
int counte;
int counto;
if(n1%2==0){
counto=n1/2;
counte=n1/2;
}
else{
counto=n1/2 +1;
counte = n1/2;
}
if(k==1){
System.out.println("YES");
for(int i=1;i<=n;i++){
System.out.println(i);
}
}
else if(counto%k!=0 || counte%k!=0){
System.out.println("NO");
}
else{
System.out.println("YES");
int num1 = 1;
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
System.out.print(num1+" ");
num1+=2;
}
if(num1>n1){
num1=2;
}
System.out.print("\n");
}
}
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 | 9f47a13a513c107c71320a42f07cc54f | 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 {
public static void main(String args[]) {
Scanner scn = new Scanner (System.in);
int x= scn.nextInt();
while(x-->0){
int n= scn.nextInt();
int k= scn.nextInt();
if(k==1){
System.out.println("YES");
for(int i=1;i<=n;i++){
System.out.println(i);
}
continue;
}
if(n%2!=0){
System.out.println("NO");
}
else{
System.out.println("YES");
int num1 = 1;
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
System.out.print(num1+" ");
num1+=2;
}
if(num1>n*k){
num1=2;
}
System.out.print("\n");
}
}
}
}
} | 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 | 00e7a942fbad304a84844895662be6ef | 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.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
public class divs770c {
private static class pair implements Comparable<pair> {
int low, high;
pair(int low, int high) {
this.low = low;
this.high = high;
}
@Override
public int hashCode()
{
// uses roll no to verify the uniqueness
// of the object of Student class
final int temp = 14;
int ans = 1;
ans = (temp * ans) + low +high;
return ans;
}
public int compareTo(pair o)
{
return this.low-o.low;
}
// Equal objects must produce the same
// hash code as long as they are equal
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
pair other = (pair)o;
if (this.low != other.low && this.high !=other.high) {
return false;
}
return true;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- != 0) {
int n=in.nextInt();int k=in.nextInt();
int no=n*k;
if(k==1)
{
out.println("YES");
for(int i=1;i<=n;i++)
{
out.println(i);
}
}
/* else if(n==1 && k%2==0)
{ int ans=1;boolean flag=true;
for(int i=1;i<=k;i++)
{
if(ans<no && flag)
{
out.print(ans+" ");ans+=2;
if(ans>no)
{
ans--;flag=false;
}
}else{
out.print(ans+" ");
ans-=2;
}
}
}else if(k%2!=0)
{
out.println("NO");
}*/
else if(n%2!=0 && k%2!=0)
{
out.println("NO");
}
else {
int ans=1;boolean flag=true; int arc[][]=new int[n+1][k+1];boolean chk=false;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if(ans<no && flag)
{
arc[i][j]=ans;ans+=2;
if(ans>no)
{
ans--;flag=false;
}
if(!flag && j<k)
{
chk=true; break;
}
}else{
arc[i][j]=ans;
ans-=2;
}
}
if(chk)
{break;}
}
if(!chk){out.println("YES");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
out.print(arc[i][j]+" ");
}out.println();
}}else{
out.println("NO");
}
}
}
out.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 | e30754b7a618bf956beaf8c020af9f9e | 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 R770C {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PrintWriter out =new PrintWriter(System.out);
int t = scan.nextInt();
while(t-->0){
int n = scan.nextInt();
int k = scan.nextInt();
int[][] board =new int[n][k];
int numO = 1, numE = 2;
for(int i = 0;i<n;i++){
for(int j = 0;j<k;j++){
if(i%2==0){
board[i][j] = numO;
numO+=2;
}else{
board[i][j] = numE;
numE+=2;
}
}
}
if(n==1 && k!=1){
out.println("NO");
}
else if((n)%2==1 && k!=1){
out.println("NO");
}else{
out.println("YES");
for(int i = 0;i<n;i++){
for(int j = 0;j<k;j++){
out.print(board[i][j]+" ");
}
out.println();
}
}
}
out.flush();
out.close();
}
static class JS {
public int BS = 1 << 16;
public char NC = (char) 0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public long nextLong() {
num = 1;
boolean neg = false;
if (c == NC) c = nextChar();
for (; (c < '0' || c > '9'); c = nextChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = nextChar()) {
res = (res << 3) + (res << 1) + c - '0';
num *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / num;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = nextChar();
while (c > 32) {
res.append(c);
c = nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = nextChar();
while (c != '\n') {
res.append(c);
c = nextChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = nextChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
}
| Java | ["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 | 862cdcf410e138b35bfd42a9c48ded59 | 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 COKEA {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt();
int k = s.nextInt();
if (k == 1) {
System.out.println("yes");
for (int j = 0; j < n; j++) {
System.out.println(j + 1);
}
} else {
if (n % 2 == 1) {
System.out.println("no");
} else {
System.out.println("yes");
for (int p = 0; p < n; p++) {
for (int j = 0; j < k; j++) {
System.out.print(j * n + p + 1 + " ");
}
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 | f54b12e1559badf83328be9a2420e32c | 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.*;
//code by tishrah_
public class _practise {
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());
}
int[] ia(int n)
{
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=nextInt();
return a;
}
int[][] ia(int n , int m)
{
int a[][]=new int[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextInt();
return a;
}
long[][] la(int n , int m)
{
long a[][]=new long[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextLong();
return a;
}
char[][] ca(int n , int m)
{
char a[][]=new char[n][m];
for(int i=0;i<n;i++)
{
String x =next();
for(int j=0;j<m ;j++) a[i][j]=x.charAt(j);
}
return a;
}
long[] la(int n)
{
long a[]=new long[n];
for(int i=0;i<n;i++)a[i]=nextLong();
return a;
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static void sort(long[] a)
{int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {long oi=r.nextInt(n), temp=a[i];a[i]=a[(int)oi];a[(int)oi]=temp;}Arrays.sort(a);}
static void sort(int[] a)
{int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);}
public static long sum(long a[])
{long sum=0; for(long i : a) sum+=i; return(sum);}
public static long count(long a[] , long x)
{long c=0; for(long i : a) if(i==x) c++; return(c);}
public static int sum(int a[])
{ int sum=0; for(int i : a) sum+=i; return(sum);}
public static int count(int a[] ,int x)
{int c=0; for(int i : a) if(i==x) c++; return(c);}
public static int count(String s ,char ch)
{int c=0; char x[] = s.toCharArray(); for(char i : x) if(ch==i) c++; return(c);}
public static boolean prime(int n)
{for(int i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static boolean prime(long n)
{for(long i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;}
public static int gcd(int n1, int n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static long gcd(long n1, long n2)
{ if (n2 != 0)return gcd(n2, n1 % n2); else return n1;}
public static int[] freq(int a[], int n)
{ int f[]=new int[n+1]; for(int i:a) f[i]++; return f;}
public static int[] pos(int a[], int n)
{ int f[]=new int[n+1]; for(int i=0; i<n ;i++) f[a[i]]=i; return f;}
public static int[] rev(int a[])
{
for(int i=0 ; i<(a.length+1)/2;i++)
{
int temp=a[i];
a[i]=a[a.length-1-i];
a[a.length-1-i]=temp;
}
return a;
}
public static boolean palin(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
String str=String.valueOf(sb.reverse());
if(s.equals(str))
return true;
else return false;
}
public static String rev(String s)
{
StringBuilder sb = new StringBuilder();
sb.append(s);
return String.valueOf(sb.reverse());
}
public static void main(String args[])
{
FastReader in=new FastReader();
PrintWriter so = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
_practise ob = new _practise();
int T = in.nextInt();
// int T = 1;
tc : while(T-->0)
{
//1634C
int n = in.nextInt();
int k = in.nextInt();
if(n%2==1 && k==1)
{
so.println("YES");
for(int i=1 ; i<=n ; i++) so.println(i);
}
else if(n%2==0)
{
int e=2,o=1;
so.println("YES");
for(int i=1 ; i<=n ; i++)
{
if(i%2==0)
{
for(int j=0 ; j<k ; j++)
{
so.print(e+" "); e+=2;
}
}
else
{
for(int j=0 ; j<k ; j++)
{
so.print(o+" "); o+=2;
}
}
so.println();
}
}
else so.println("NO");
}
so.flush();
/*String s = in.next();
* Arrays.stream(f).min().getAsInt()
* BigInteger f = new BigInteger("1"); 1 ke jagah koi bhi value ho skta jo aap
* initial value banan chahte
int a[] = new int[n];
Stack<Integer> stack = new Stack<Integer>();
Deque<Integer> q = new LinkedList<>(); or Deque<Integer> q = new ArrayDeque<Integer>();
PriorityQueue<Long> pq = new PriorityQueue<Long>();
ArrayList<Integer> al = new ArrayList<Integer>();
StringBuilder sb = new StringBuilder();
HashSet<Integer> st = new LinkedHashSet<Integer>();
Set<Integer> s = new HashSet<Integer>();
Map<Long,Integer> hm = new HashMap<Long, Integer>(); //<key,value>
for(Map.Entry<Integer, Integer> i :hm.entrySet())
for(int i : hm.values())
for(int i : hm.keySet())
HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
so.println("HELLO");
Arrays.sort(a,Comparator.comparingDouble(o->o[0]))
Arrays.sort(a, (aa, bb) -> Integer.compare(aa[1], bb[1]));
Set<String> ts = new TreeSet<>();*/
}
} | 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 | 1c90c7732faa34b30a1a7d3e599f282c | 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 OKEA
{
public static void solve(int shelves, int itemsPerShelf)
{
int totalItems = shelves * itemsPerShelf;
// System.out.println("Shelves: " + shelves);
// System.out.println("Items per shelf: " + itemsPerShelf);
// System.out.println("Total items: " + totalItems);
if(itemsPerShelf == 1)
{
System.out.println("YES");
for(int i = 1; i <= shelves; i++)
{
System.out.println(i);
}
return;
}
if(shelves == 1 && itemsPerShelf > 1) {
System.out.println("NO");
return;
}
if(totalItems % 2 == 0)
{
if((totalItems/2) % itemsPerShelf != 0)
{
System.out.println("NO");
return;
}
else
{
System.out.println("YES");
int j = 0;
for(int i = 1; i < totalItems; i++)
{
System.out.print(i++ + " ");
j++;
if(j == itemsPerShelf)
{
System.out.println("");
j = 0;
}
}
j = 0;
for(int i = 2; i <= totalItems; i++)
{
System.out.print(i++ + " ");
j++;
if(j == itemsPerShelf)
{
System.out.println("");
j = 0;
}
}
// System.out.println("");
}
}
else
{
System.out.println("");
System.out.println("NO");
}
}
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for(int i = 0; i < testCases; i++)
{
int shelves = sc.nextInt();
int itemsPerShelf = sc.nextInt();
// System.out.println("\n====== Test case: " + i + " ======");
solve(shelves, itemsPerShelf);
// 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.