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 | a69c71ab7eb48454a19d9ba629183647 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.Scanner;
public class test306 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int j=0;j<t;j++) {
int m=in.nextInt();
int n=in.nextInt();
char[] c=in.next().toCharArray();
boolean x=true;
for(int i=0;i<=m/2;i++) {
if(c[i]!=c[m-1-i]) {
x=false;
break;
}
}
if(x) {
System.out.println(1);
}
else {
if(n>0) {
System.out.println(2);
}
else {
System.out.println(1);
}
}
}
in.close();
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 7f3a60656dddf0e04b5235d61fb39b3b | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
public class main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int cases = scan.nextInt();
int [] arr = new int[cases];
for(int i=0; i<cases; i++)
{
int n = scan.nextInt();
int k = scan.nextInt();
String str = scan.next();
String rev = "";
for(int l=n-1; l>=0; l--)
{
rev+= str.charAt(l);
}
if(str.equals(rev) || k==0)
arr[i] = 1;
else arr[i] = 2;
}
for(int i=0; i<cases; i++)
System.out.println(arr[i]);
scan.close();
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 9c3e3ca9ec3c97e361a21fe445f3f398 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class 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;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int k = sc.nextInt();
String s = sc.next();
sb.append(solve(n,k,s)+"\n");
}
System.out.println(sb.toString());
}
private static int solve(int n, int k, String s) {
StringBuilder sb =new StringBuilder();
sb.append(s).reverse();
if(k==0) return 1;
if(s.equals(sb.toString()))
{
return 1;
}else
{
return 2;
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 5f9c9f8957f03436a7ef2263cf1270af | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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 A {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
A solver = new A();
int t = sc.nextInt();
solver.solve(t, sc, out);
out.close();
}
private void solve(int t, InputReader sc, PrintWriter out) {
while(t-->0)
{
int n = sc.nextInt();
int k = sc.nextInt();
String s = sc.next();
out.println(solve(n,k,s));
}
}
private int solve(int n, int k, String s) {
StringBuilder sb =new StringBuilder();
sb.append(s).reverse();
if(k==0) return 1;
if(s.equals(sb.toString()))
{
return 1;
}else
{
return 2;
}
}
}
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());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | e287646c1c8216f377ed81089b8487b2 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.awt.*;
import java.util.*;
import java.io.*;
// You have that in you, you can do it........
public class Codeforces {
static FScanner sc = new FScanner();
static PrintWriter out = new PrintWriter(System.out);
static final Random random = new Random();
static long mod = 1000000007L;
static HashMap<String, Integer> map = new HashMap<>();
static boolean[] sieve = new boolean[1000000];
static double[] fib = new double[1000000];
public static void main(String args[]) throws IOException {
int T = sc.nextInt();
while (T-- > 0) {
int n = sc.nextInt();
int k=sc.nextInt();
String s=sc.next();
if(k==0)
out.println(1);
else
{
char[] c=s.toCharArray();
char[] cc=new char[c.length];
for(int i=0;i<c.length;i++)
cc[i]=c[c.length-1-i];
boolean b=true;
for(int i=0;i<c.length;i++)
{
if(c[i]!=cc[i])
{
b=false;
break;
}
}
if(b)
out.println(1);
else
out.println(2);
}
}
out.close();
}
// TemplateCode
static void fib() {
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < fib.length; i++)
fib[i] = fib[i - 1] + fib[i - 2];
}
static void primeSieve() {
for (int i = 0; i < sieve.length; i++)
sieve[i] = true;
for (int i = 2; i * i <= sieve.length; i++) {
if (sieve[i]) {
for (int j = i * i; j < sieve.length; j += i) {
sieve[j] = false;
}
}
}
}
static int max(int a, int b) {
if (a < b)
return b;
return a;
}
static int min(int a, int b) {
if (a < b)
return a;
return b;
}
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 <E> void print(E res) {
System.out.println(res);
}
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 / gcd(a, b)) * b;
}
static int abs(int a) {
if (a < 0)
return -1 * a;
return a;
}
static class FScanner {
BufferedReader br;
StringTokenizer st;
public FScanner() {
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;
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 8bf752e79243f88a6e8c31922ae4efcf | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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
{
static boolean isPalindrome(String s) {
int i=0, j=s.length()-1;
while(i < j) {
if(s.charAt(i) != s.charAt(j)) {
return false;
}
i++; j--;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- > 0) {
int n=sc.nextInt();
int k=sc.nextInt();
String s=sc.next();
if(k==0 ) {
System.out.println(1);
}
else if(isPalindrome(s)) {
System.out.println(1);
}
else{
System.out.println(2);
}
}
} catch(Exception e) {
}
// your code goes here
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | ef7d1a4e227f4753ddce820bc93229b4 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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();
String s = s();
boolean pad = true;
for (int i = 0; i < n; i++) {
if (s.charAt(i) != s.charAt(n - 1 - i)) {
pad = false;
break;
}
}
if (pad) {
out.println(1);
} else {
if (k >= 1) {
out.println(2);
} else {
out.println(1);
}
}
}
// (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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | cd212b794721bb09bac46fa3e2fe26e6 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
sc.nextLine(); //to clear the buffer
String str = sc.nextLine();
if (isPalindrome(str) || k == 0) System.out.println("1");
else System.out.println("2");
}
}
}
/**
* method to solve current cp problem
*/
private static void solve() {
}
/**
* method to check whether the provided string is palindrome or not
*
* @param str - the string to check if it is palindrome or not
* @return - true if the provided string is palindrome else false
*/
private static boolean isPalindrome(String str) {
if (str.length() % 2 == 0) {
for (int i = 0, j = str.length() - 1; i < j && j > i; i++, j--) {
if (str.charAt(i) != str.charAt(j)) return false;
}
} else {
for (int i = 0, j = str.length() - 1; i <= j && j >= i; i++, j--) {
if (str.charAt(i) != str.charAt(j)) return false;
}
}
return true;
}
/**
* method to check whether the provided number is palindrome or not
*
* @param n - the number to check if it is palindrome or not
* @return - true if the provided number is palindrome else false
*/
private static boolean isPalindrome(int n) {
int tempN = n, reverseNumber = 0;
while (tempN > 0) {
reverseNumber = reverseNumber * 10 + (tempN % 10);
tempN /= 10;
}
if (reverseNumber == n) return true;
return false;
}
/**
* method to check whether the provided number is palindrome or not
*
* @param n - the number to check if it is palindrome or not
* @return - true if the provided number is palindrome else false
*/
private static boolean isPalindrome(long n) {
long tempN = n, reverseNumber = 0L;
while (tempN > 0L) {
reverseNumber = reverseNumber * 10L + (tempN % 10L);
tempN /= 10L;
}
if (reverseNumber == n) return true;
return false;
}
/**
* method to get max and second max element returned into
* the form of an array
**/
private static int[] getMaxAndSecondMax(int a, int b, int c, int d) {
int[] arr = new int[2];
if (a > b && a > c && a > d) {
arr[0] = a;
if (b > c && b > d) arr[1] = b;
else if (c > b && c > d) arr[1] = c;
else if (d > b && d > c) arr[1] = d;
} else if (b > a && b > c && b > d) {
arr[0] = b;
if (a > c && a > d) arr[1] = a;
else if (c > a && c > d) arr[1] = c;
else if (d > a && d > c) arr[1] = d;
} else if (c > a && c > b && c > d) {
arr[0] = c;
if (a > b && a > d) arr[1] = a;
else if (b > a && b > d) arr[1] = b;
else if (d > a && d > b) arr[1] = d;
} else if (d > a && d > b && d > c) {
arr[0] = d;
if (a > b && a > c) arr[1] = a;
else if (b > a && b > c) arr[1] = b;
else if (c > a && c > b) arr[1] = c;
}
return arr;
}
/**
* method to compute the total number of digits in a
* given number
*/
private static int countDigits(int n) {
int counter = 1;
while (n > 9) {
n /= 10;
counter += 1;
}
return counter;
}
/**
* method to compute the total number of digits in a
* given number
*/
private static long countDigits(long n) {
long counter = 1L;
while (n > 9L) {
n /= 10L;
counter += 1L;
}
return counter;
}
/**
* method to compute the sum of n natural numbers
*/
private static int getNDigitsSum(int n) {
return (n * (n + 1)) / 2;
}
/**
* method to check whether a number is prime or not
*/
private static boolean isPrime(int n) {
//Base Cases
if (n == 2 || n == 3) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* method to compute factorial of a given number
*
* @param n
* @return
*/
private static int factorial(int n) {
int ans = 1;
while (n > 1) {
ans *= n;
n -= 1;
}
return ans;
}
/**
* method to compute factorial of a given number
*
* @param n
* @return
*/
private static long factorial(long n) {
long ans = 1L;
while (n > 1L) {
ans *= n;
n -= 1L;
}
return ans;
}
/**
* method to find gcd of two numbers
*
* @param a
* @param b
* @return
*/
private static int gcd(int a, int b) {
while (b != 0) {
int tempA = a;
a = b;
b = tempA % b;
}
return a;
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | ecb7bc35f46db0bd256c2b017d47090f | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Ashutosh Patel (ashutoshpatelnoida@gmail.com) Linkedin : ( https://www.linkedin.com/in/ashutosh-patel-7954651ab/ )
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
AReverseAndConcatenate solver = new AReverseAndConcatenate();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class AReverseAndConcatenate {
public void solve(int testNumber, InputReader sc, OutputWriter out) {
int n = sc.readInt();
int m = sc.readInt();
String str = sc.readString();
if (m == 0) {
out.printLine(1);
return;
}
if (StringUtils.isPalindrome(str)) {
out.printLine(1);
} else out.printLine(2);
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
static class StringUtils {
public static boolean isPalindrome(String str) {
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
if (str.charAt(i) != str.charAt(j)) return false;
}
return true;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 2e4bbe1dc718c360d6bfc3ca66797884 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
for (int i = 0; i < t; i++) {
String[] inputInts = sc.nextLine().split(" ");
int n = Integer.parseInt(inputInts[0]);
int k = Integer.parseInt(inputInts[1]);
String s = sc.nextLine();
if (isSymmetric(s, n)) {
System.out.println(1);
} else {
if (k != 0) {
System.out.println(2);
} else {
System.out.println(1);
}
}
}
}
private static boolean isSymmetric(String s, int n) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < n / 2; i++) {
stack.push(s.charAt(i));
}
int index = n / 2;
if (n % 2 != 0) {
index++;
}
while (!stack.isEmpty()) {
if (stack.pop() != s.charAt(index)) {
return false;
}
index++;
}
return true;
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 8f0ae93dffb7d56eb8a0b66848587580 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Municip {
static boolean rec(String s){
for (int i = 0; i <= s.length()/2; i++) {
if (s.charAt(i)!=s.charAt(s.length()-1-i)){
return false;
}
}
return true;
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
//br = new BufferedReader(new FileReader("input.txt"));
//out = new PrintWriter("output.txt");
int t = nextInt();
for (int i = 0; i < t; i++) {
int n = nextInt();
int k = nextInt();
int cnt = 0;
String s = nextToken();
if (k==0||rec(s)){
out.println(1);
}else{
out.println(2);
}
}
out.close();
}
static BufferedReader br;
static PrintWriter out;
static StringTokenizer in = new StringTokenizer("");
public static boolean hasNext() throws IOException {
if (in.hasMoreTokens()) return true;
String s;
while ((s = br.readLine()) != null) {
in = new StringTokenizer(s);
if (in.hasMoreTokens()) return true;
}
return false;
}
public static String nextToken() throws IOException {
while (!in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | f7f8a3f2f37d601ba4168f042aea4c22 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.Scanner;
public class Solution extends CodeForces {
static String reverse(String s){
StringBuilder sb = new StringBuilder(s);
sb.reverse();
return sb.toString();
}
static int solution(String s,int k){
String rev = reverse(s);
if (k==0||rev.equals(s)){
return 1;
}
k--;
return solution(s.concat(rev),k) + solution(rev.concat(s),k);
}
public static void main(String[] args) {
// System.out.println(reverse("hello"));
int t = I();
while (t-->0){
int n = I();
int k = I();
String s = S();
System.out.println(solution(s,k));
}
}
}
class CodeForces {
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=1000000007;
static long mod1=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;
public static class pair
{
int a;
int b;
public pair(int val,int index)
{
a=val;
b=index;
}
}
public static class myComp implements Comparator<pair>
{
//sort in ascending order.
public int compare(CodeForces.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 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 long kadane(long a[],int n)
{
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 void DFS(ArrayList<Integer> arr[],int s,boolean visited[])
{
visited[s]=true;
for(int i:arr[s]){
if(!visited[i]){
DFS(arr,i,visited);
}
}
}
public static int BS(int a[],int x,int ii,int jj)
{
// int n=a.length;
int mid=0;
int i=ii,j=jj,in=0;
while(i<=j)
{
mid=(i+j)/2;
if(a[mid]<x){
in=mid+1;
i=mid+1;
}
else
j=mid-1;
}
return in;
}
public static int lower_bound(int arr[], int N, int X)
{
int mid;
int low = 0;
int high = N;
while(low<high) {
mid=low+(high-low)/2;
if(X<=arr[mid]){
high=mid;
}
else{
low=mid+1;
}
}
if(low<N && arr[low]<X){
low++;
}
out.println(arr[low]);
return low;
}
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
{
long farr[];
int n;
public FenwickTree(int c)
{
n=c+1;
farr=new long[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x)
{
for(;x<=n;x+=x&(-x))
{
farr[x]++;
}
}
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
{
static 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 static int find(int a)
{
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public static 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]++;
}
}
}
//SEGMENT TREE CODE
// public static void segmentUpdate(int si,int ss,int se,int qs,int qe,long x)
// {
// if(ss>qe || se<qs)return;
// if(qs<=ss && qe>=se)
// {
// seg[si][0]+=1L;
// seg[si][1]+=x*x;
// seg[si][2]+=2*x;
// return;
// }
// int mid=(ss+se)/2;
// segmentUpdate(2*si+1,ss,mid,qs,qe,x);
// segmentUpdate(2*si+2,mid+1,se,qs,qe,x);
// }
// public static long segmentGet(int si,int ss,int se,int x,long f,long s,long t,long a[])
// {
// if(ss==se && ss==x)
// {
// f+=seg[si][0];
// s+=seg[si][1];
// t+=seg[si][2];
// long ans=a[x]+(f*((long)x+1L)*((long)x+1L))+s+(t*((long)x+1L));
// return ans;
// }
// int mid=(ss+se)/2;
// if(x>mid){
// return segmentGet(2*si+2,mid+1,se,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a);
// }else{
// return segmentGet(2*si+1,ss,mid,x,f+seg[si][0],s+seg[si][1],t+seg[si][2],a);
// }
// }
public static class myComp1 implements Comparator<CodeForces.pair1>
{
//sort in ascending order.
public int compare(pair1 p1,pair1 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 class pair1
{
long a;
long b;
public pair1(long val,long index)
{
a=val;
b=index;
}
}
public static ArrayList<CodeForces.pair1> mergeIntervals(ArrayList<CodeForces.pair1> arr)
{
//****************use this in main function-Collections.sort(arr,new myComp1());
ArrayList<CodeForces.pair1> a1=new ArrayList<>();
if(arr.size()<=1)
return arr;
a1.add(arr.get(0));
int i=1,j=0;
while(i<arr.size())
{
if(a1.get(j).b<arr.get(i).a)
{
a1.add(arr.get(i));
i++;
j++;
}
else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b)
{
i++;
}
else if(a1.get(j).b>=arr.get(i).a)
{
long a=a1.get(j).a;
long b=arr.get(i).b;
a1.remove(j);
a1.add(new CodeForces.pair1(a,b));
i++;
}
}
return a1;
}
public static boolean palindrome(String s,int n)
{
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public 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;
}
public 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;
}
public static int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
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(boolean 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++){
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<Long> arr)
{
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapInt(HashMap<Integer,Integer> hm){
for(Map.Entry<Integer,Integer> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMapLong(HashMap<Long,Long> hm){
for(Map.Entry<Long,Long> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static long pwr(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 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(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);
}
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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 7d603e1d8d58b815e947b65429823931 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
public class Codeforces {
static String ab,b;
static class Node
{
int val;
Node left;
Node right;
public Node(int x) {
// TODO Auto-generated constructor stub
this.val=x;
this.left=null;
this.right=null;
}
}
static class Pair<U, V> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair<U, V> o) {
int value = ((Comparable<U>) x).compareTo(o.x);
if (value != 0) return value;
return ((Comparable<V>) y).compareTo(o.y);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return x.equals(pair.x) && y.equals(pair.y);
}
public int hashCode() {
return Objects.hash(x, y);
}
}
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[] nextArray(int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=nextInt();
return arr;
}
}
static String string;
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
return gcd(b, a % b);
}
static long gcd(long a, long b)
{
// Everything divides 0
for(long i=2;i<=b;i++)
{
if(a%i==0&&b%i==0)
return i;
}
return 1;
}
static int fac(int n)
{
int c=1;
for(int i=2;i<n;i++)
if(n%i==0)
c=i;
return c;
}
static int lcm(int a,int b)
{
for(int i=Math.min(a, b);i<=a*b;i++)
if(i%a==0&&i%b==0)
return i;
return 0;
}
static int maxHeight(char[][] ch,int i,int j,String[] arr)
{
int h=1;
if(i==ch.length-1||j==0||j==ch[0].length-1)
return 1;
while(i+h<ch.length&&j-h>=0&&j+h<ch[0].length&&ch[i+h][j-h]=='*'&&ch[i+h][j+h]=='*')
{
String whole=arr[i+h];
//System.out.println(whole.substring(j-h,j+h+1));
if(whole.substring(j-h,j+h+1).replace("*","").length()>0)
return h;
h++;
}
return h;
}
static boolean all(BigInteger n)
{
BigInteger c=n;
HashSet<Character> hs=new HashSet<>();
while((c+"").compareTo("0")>0)
{
String d=""+c;
char ch=d.charAt(d.length()-1);
if(d.length()==1)
{
c=new BigInteger("0");
}
else
c=new BigInteger(d.substring(0,d.length()-1));
if(hs.contains(ch))
continue;
if(d.charAt(d.length()-1)=='0')
continue;
if(!(n.mod(new BigInteger(""+ch)).equals(new BigInteger("0"))))
return false;
hs.add(ch);
}
return true;
}
static int cal(long n,long k)
{
System.out.println(n+","+k);
if(n==k)
return 2;
if(n<k)
return 1;
if(k==1)
return 1+cal(n, k+1);
if(k>=32)
return 1+cal(n/k, k);
return 1+Math.min(cal(n/k, k),cal(n, k+1));
}
static Node buildTree(int i,int j,int[] arr)
{
if(i==j)
{
//System.out.print(arr[i]);
return new Node(arr[i]);
}
int max=i;
for(int k=i+1;k<=j;k++)
{
if(arr[max]<arr[k])
max=k;
}
Node root=new Node(arr[max]);
//System.out.print(arr[max]);
if(max>i)
root.left=buildTree(i, max-1, arr);
else {
root.left=null;
}
if(max<j)
root.right=buildTree(max+1, j, arr);
else {
root.right=null;
}
return root;
}
static int height(Node root,int val)
{
if(root==null)
return Integer.MAX_VALUE-32;
if(root.val==val)
return 0;
if((root.left==null&&root.right==null))
return Integer.MAX_VALUE-32;
return Math.min(height(root.left, val), height(root.right, val))+1;
}
static void shuffle(int a[], int n)
{
for (int i = 0; i < n; i++) {
// getting the random index
int t = (int)Math.random() * a.length;
// and swapping values a random index
// with the current index
int x = a[t];
a[t] = a[i];
a[i] = x;
}
}
static void sort(int[] arr )
{
shuffle(arr, arr.length);
Arrays.sort(arr);
}
static boolean arraySortedInc(int arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i - 1] > arr[i])
return false;
// No unsorted pair found
return true;
}
static boolean arraySortedDec(int arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return true;
for (int i = 1; i < n; i++)
// Unsorted pair found
if (arr[i - 1] > arr[i])
return false;
// No unsorted pair found
return true;
}
static int largestPower(int n, int p) {
// Initialize result
int x = 0;
// Calculate x = n/p + n/(p^2) + n/(p^3) + ....
while (n > 0) {
n /= p;
x += n;
}
return x;
}
// Utility function to do modular exponentiation.
// It returns (x^y) % p
static int power(int x, int y, int p) {
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y % 2 == 1) {
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n! % p
static int modFact(int n, int p) {
if (n >= p) {
return 0;
}
int res = 1;
// Use Sieve of Eratosthenes to find all primes
// smaller than n
boolean isPrime[] = new boolean[n + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
// Consider all primes found by Sieve
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
// Find the largest power of prime 'i' that divides n
int k = largestPower(n, i);
// Multiply result with (i^k) % p
res = (res * power(i, k, p)) % p;
}
}
return res;
}
static boolean[] seiveOfErathnos(int n2)
{
boolean isPrime[] = new boolean[n2 + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n2; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n2; j += i) {
isPrime[j] = false;
}
}
}
return isPrime;
}
static boolean[] seiveOfErathnos2(int n2,int[] ans)
{
boolean isPrime[] = new boolean[n2 + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n2; i++) {
if (isPrime[i]) {
for (int j = 2 * i; j <= n2; j += i) {
if(isPrime[j])
ans[i]++;
isPrime[j] = false;
}
}
}
return isPrime;
}
static long helper(int[] arr,int i,int used,long[][] dp)
{
if(i==arr.length)
return 3-used;
if(dp[i][used]!=-1)
return dp[i][used];
long sum=0,ans=Integer.MAX_VALUE;
for(int j=i;j<arr.length;j++)
{
sum+=arr[j];
long n=(long) Math.ceil(Math.log10(sum)/Math.log10(2));
// System.out.println(sum+","+(1L<<n));
if(used<2||j==arr.length-1)
ans=Math.min(ans,(1L<<n)-sum+helper(arr, j+1, used+1, dp));
}
return dp[i][used]=ans;
}
static boolean palin(char[] arr)
{
for(int i=0;i<arr.length/2;i++)
{
if(arr[i]!=arr[arr.length-1-i])
return false;
}
return true;
}
public static void main(String[] args)throws IOException
{
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
FastReader fs=new FastReader();
// int[] ans=new int[1000001];
int T=fs.nextInt();
// seiveOfErathnos2(1000000, ans);
StringBuilder sb=new StringBuilder();
while(T-->0)
{
int n=fs.nextInt(),k=fs.nextInt();
String text=fs.nextLine();
if(palin(text.toCharArray())||k==0)
sb.append(1);
else
sb.append(2);
sb.append("\n");
// System.out.println(ans);
}
System.out.println(sb);
}
private static void qprint(int sx) {
// TODO Auto-generated method stub
System.out.println(sx);
}
private static void qprint(String sx) {
// TODO Auto-generated method stub
System.out.println(sx);
}
private static void qprint(int sx, int ex) {
// TODO Auto-generated method stub
System.out.println(sx+","+ex);
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | c2550cf4302d44df0d15062257d3bca7 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int a = sc.nextInt();
for (int i = 0; i < a; i++) {
int b = sc.nextInt();
int c = sc.nextInt();
String s = sc.next();
int cnt = 0;
for (int j = 0; j < (b)/2; j++){
if (s.charAt(j) == s.charAt(b-j-1)) {
cnt++;
}
}
if (c == 0) {
System.out.println(1);
}
else if (cnt == b/2) {
System.out.println(1);
}
else {
System.out.println(2);
}
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 4dd06c5506690057125bcac72c76114c | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class Codechef {
public static void main(String[] args) throws NumberFormatException, IOException {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
PrintWriter writer = new PrintWriter(System.out);
int testcase = Integer.parseInt(bufferedReader.readLine());
for(int t=1; t<=testcase; t++){
//long n = Integer.parseInt(bufferedReader.readLine());
long[] arr = Arrays.stream(bufferedReader.readLine().split("\\s+")).mapToLong(Integer::parseInt).toArray();
String str = bufferedReader.readLine();
if (ispalindrome(str)) {
writer.println("1");
}else {
if (arr[1] == 0) {
writer.println("1");
}else {
writer.println("2");
}
}
writer.flush();
}
}
static private boolean ispalindrome(String str) {
int low = 0;
int high = str.length()-1;
while(low<=high) {
if (str.charAt(low) != str.charAt(high)) {
return false;
}
low++;
high--;
}
return true;
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | b6c8ea091210dfff63e109f4650ce0b4 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static int memo[][];
public static void main(String[] args) throws IOException, InterruptedException {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt(), k = sc.nextInt();
String s = sc.next();
boolean f = true;
for(int i=0;i<s.length()/2;i++) {
if(s.charAt(i)!=s.charAt(n-1-i)) {
f=false;
break;
}
}
if(f || k==0) pWriter.println(1);
else pWriter.println(2);
}
pWriter.flush();
}
public static void sort(int[] in) {
shuffle(in);
Arrays.sort(in);
}
public static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
int tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
static 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 o) {
if(x!=o.x)
return x - o.x;
else return y-o.y;
}
public String toString() {
return x + " " + y;
}
}
static Scanner sc = new Scanner(System.in);
static PrintWriter pWriter = new PrintWriter(System.out);
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();
}
}
public static void display(char[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 8998bca8c8fc422936d77469b8f985ac | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
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(),k=sc.nextInt();
String s=sc.next();
StringBuilder sb=new StringBuilder(s);
String p=sb.reverse().toString();
if(s.equals(p) || k==0){
System.out.println("1");
}else{
System.out.println("2");
}
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 997abe0f9552e5cadc9d798ffd99d64b | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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 boolean isPalindrome(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 void main(String[] args) {
Scanner in = new Scanner(System.in);
int test = in.nextInt();
while (test-- > 0) {
int n = in.nextInt(), k = in.nextInt();
String s = in.next();
if (isPalindrome(s) || k == 0) System.out.println(1);
else System.out.println(2);
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 3946b512ae51bbb2972bb7f2e0fffc63 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.Math;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class Main {
static PrintWriter pw;
static Scanner sc;
static StringBuilder ans;
static long mod = 1000000000+7;
static void pn(final Object arg) { pw.print(arg); pw.flush(); }
/*-------------- for input in an value ---------------------*/
static int ni() { return sc.nextInt(); }
static long nl() { return sc.nextLong(); }
static double nd() { return sc.nextDouble(); }
static String ns() { return sc.next(); }
static String nLine() { return sc.nextLine(); }
static void ap(int arg) { ans.append(arg); }
static void ap(long arg) { ans.append(arg); }
static void ap(String arg) { ans.append(arg); }
static void ap(StringBuilder arg) { ans.append(arg); }
static void apn() { ans.append("\n"); }
static void apn(int arg) { ans.append(arg+"\n"); }
static void apn(long arg) { ans.append(arg+"\n"); }
static void apn(String arg) { ans.append(arg+"\n"); }
static void apn(StringBuilder arg) { ans.append(arg+"\n"); }
static void yes() { ap("Yes\n"); }
static void no() { ap("No\n"); }
/* for Dubuggin */
static void printArr(int ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(long ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(String ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printIntegerList(List<Integer> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printLongList(List<Long> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printStringList(List<String> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
/*-------------- for input in an array ---------------------*/
static void readArray(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void readArray(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void readArray(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void readArray(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*-------------- File vs Input ---------------------*/
static void runFile() throws Exception {
sc = new Scanner(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
}
static void runIo() throws Exception {
pw =new PrintWriter(System.out);
sc = new Scanner(System.in);
}
static int log2(int n) { return (int)(Math.log(n) / Math.log(2)); }
static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);}
static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); }
static long nCr(long n, long r) { // Combinations
if (n < r)
return 0;
if (r > n - r) { // because nCr(n, r) == nCr(n, n - r)
r = n - r;
}
long ans = 1L;
for (long i = 0; i < r; i++) {
ans *= (n - i);
ans /= (i + 1);
}
return ans;
}
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
static boolean isPrime(long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean sv[] = new boolean[10002];
static void seive() {
//true -> not prime
// false->prime
sv[0] = sv[1] = true;
sv[2] = false;
for(int i = 0; i< sv.length; i++) {
if( !sv[i] && (long)i*(long)i < sv.length ) {
for ( int j = i*i; j<sv.length ; j += i ) {
sv[j] = true;
}
}
}
}
static long kadensAlgo(long ar[]) {
int n = ar.length;
long pre = ar[0];
long ans = ar[0];
for(int i = 1; i<n; i++) {
pre = Math.max(pre + ar[i], ar[i]);
ans = Math.max(pre, ans);
}
return ans;
}
static long binpow( long a, long b) {
long res = 1;
while (b > 0) {
if ( (b & 1) > 0){
res = (res * a);
}
a = (a * a);
b >>= 1;
}
return res;
}
static long factorial(long n) {
long res = 1, i;
for (i = 2; i <= n; i++){
res = ((res%mod) * (i%mod))%mod;
}
return res;
}
static int getCountPrime(int n) {
int ans = 0;
while (n%2==0) {
ans ++;
n /= 2;
}
for (int i = 3; i *i<= n; i+= 2) {
while (n%i == 0) {
ans ++;
n /= i;
}
}
if(n > 1 )
ans++;
return ans;
}
static void sort(int[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void sort(long[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
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);
}
static int upperBound(long ar[], long k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] > k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static int lowerBound(long ar[], long k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] >= k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static void findDivisor(int n, ArrayList<Integer> al) {
for(int i = 1; i*i <= n; i++){
if( n % i == 0){
if(n/i==i){
al.add(i);
}
else{
al.add(n/i);
al.add(i);
}
}
}
}
static class Pair{
int a;
int b;
Pair( int a, int b ){
this.a = a;
this.b = b;
}
}
public static void main(String[] args) throws Exception {
// runFile();
runIo();
int t;
t = 1;
t = sc.nextInt();
ans = new StringBuilder();
while( t-- > 0 ) {
solve();
}
pn(ans+"");
}
public static void solve ( ) {
int n = ni();
int k = ni();
char ar[] = ns().toCharArray();
if( k == 0 ){
apn("1");
return;
}
int i = 0, j = n-1;
while(i<j) {
if( ar[i] != ar[j] ){
apn("2");
return;
}
i++;
j--;
}
apn("1");
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 785299e7241387388be27ede7fcc0be5 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class CodeForces {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public FastScanner(File f){
try {
br=new BufferedReader(new FileReader(f));
st=new StringTokenizer("");
} catch(FileNotFoundException e){
br=new BufferedReader(new InputStreamReader(System.in));
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;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static long factorial(int n){
if(n==0)return 1;
return (long)n*factorial(n-1);
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static void sort (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sortReversed (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sort (long[]a){
ArrayList<Long> b = new ArrayList<>();
for(long i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static ArrayList<Integer> 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] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
ans.add(i);
}
return ans;
}
static int binarySearchSmallerOrEqual(int arr[], int key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
public static int binarySearchStrictlySmaller(int[] arr, int target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static int binarySearch(long arr[], long x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static void init(int[]arr,int val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static void init(int[][]arr,int val){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=val;
}
}
}
static void init(long[]arr,long val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static<T> void init(ArrayList<ArrayList<T>>arr,int n){
for(int i=0;i<n;i++){
arr.add(new ArrayList());
}
}
static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target)
{
int start = 0, end = arr.size()-1;
if(end == 0) return -1;
if (target > arr.get(end).y) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr.get(mid).y >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearchStrictlyGreater(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] <= target) {
start = mid + 1;
}
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
static long sum (int []a, int[][] vals){
long ans =0;
int m=0;
while(m<5){
for (int i=m+1;i<5;i+=2){
ans+=(long)vals[a[i]-1][a[i-1]-1];
ans+=(long)vals[a[i-1]-1][a[i]-1];
}
m++;
}
return ans;
}
public static long pow(long n, long pow) {
if (pow == 0) {
return 1;
}
long retval = n;
for (long i = 2; i <= pow; i++) {
retval *= n;
}
return retval;
}
static String reverse(String s){
StringBuffer b = new StringBuffer(s);
b.reverse();
return b.toString();
}
static String charToString (char[] arr){
String t="";
for(char c :arr){
t+=c;
}
return t;
}
public static void main(String[] args) {
// StringBuilder sbd = new StringBuilder();
// PrintWriter out = new PrintWriter("output.txt");
// File input = new File("input.txt");
// FastScanner fs = new FastScanner(input);
//
FastScanner fs = new FastScanner();
// Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int testNumber =fs.nextInt();
// ArrayList<Integer> arr = new ArrayList<>();
for (int T =0;T<testNumber;T++){
StringBuffer sbf = new StringBuffer();
int n = fs.nextInt();
int k = fs.nextInt();
String s = fs.next();
sbf = new StringBuffer(s);
int ans =0;
if(k==0){
ans=1;
} else {
String ss = sbf.reverse().toString();
if(s.equals(ss))ans=1;
else ans =2;
}
out.print(ans+"\n");
}
out.flush();
}
static class Pair {
int x;//l
int y;//r
public Pair(int x,int y){
this.x=x;
this.y=y;
}
@Override
public boolean equals(Object o) {
if(o instanceof Pair){
if(o.hashCode()!=hashCode()){
return false;
} else {
return x==((Pair)o).x&&y==((Pair)o).y;
}
}
return false;
}
@Override
public int hashCode() {
return x+2*y;
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | dc1408b9ed07398da9cddcdea4fb469e | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
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 (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
int first, second,third;
public Tuple(int first, int second, int third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int[] parent;
int[] rank; //Size of the trees is used as the rank
public DSU(int n)
{
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i) //finding through path compression
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public boolean union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return false; //if they are already connected we exit by returning false.
// if a's parent is less than b's parent
if(rank[a] < rank[b])
{
//then move a under b
parent[a] = b;
}
//else if rank of j's parent is less than i's parent
else if(rank[a] > rank[b])
{
//then move b under a
parent[b] = a;
}
//if both have the same rank.
else
{
//move a under b (it doesnt matter if its the other way around.
parent[b] = a;
rank[a] = 1 + rank[a];
}
return true;
}
}
static class Reader
{
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) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long powMod(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return powMod(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
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 - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Integer> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Integer> l = new ArrayList<>();
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 p = 2; p<=n; p++)
{
if (prime[p] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public 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;
}
public 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;
}
public static void Sort(int[] a)
{
List<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static boolean isPalindrome(char[] s)
{
int n = s.length;
int left = 0, right = n-1;
boolean ok = true;
while(left <= right)
{
if(s[left] != s[right])
{
ok = false;
break;
}
left++;
right--;
}
return ok;
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int tt = sc.nextInt();
while(tt-- > 0)
{
int n = sc.nextInt(), k = sc.nextInt();
char[] s = sc.next().toCharArray();
boolean ok = (k == 0 || isPalindrome(s));
fout.println((ok == true) ? 1 : 2);
}
fout.close();
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 03ccd4acd9141723e28dadcce9966188 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
public class Contest_yandexA{
static final int MAXN = (int)1e6;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/*int n = input.nextInt();
int k = input.nextInt();
k = k%4;
int[] a = new int[n];
for(int i = 0;i<n;i++){
a[i] = input.nextInt();
}
int[] count = new int[n];
int sum = 0;
for(int tt = 0;tt<k;tt++){
for(int i = 0;i<n;i++){
count[a[i]-1]++;
}
for(int i = 0;i<n;i++){
sum+= count[i];
}
for(int i = 0;i<n;i++){
a[i] = sum;
sum-= count[i];
}
}
for(int i = 0;i<n;i++){
System.out.print(a[i] + " ");
}*/
int t = input.nextInt();
for(int tt = 0;tt<t;tt++){
int n = input.nextInt();
int k = input.nextInt();
String s = input.next();
if(k == 0){
System.out.println(1);
}
else{
int ans = 1;
for(int i = 0;i<n/2;i++){
if(s.charAt(i) != s.charAt(n-i-1)){
ans = 2;
break;
}
}
System.out.println(ans);
}
}
}
public static int gcd(int a,int b){
if(b == 0){
return a;
}
return gcd(b,a%b);
}
public static int lcm(int a,int b){
return (a / gcd(a, b)) * b;
}
}
class Pair implements Comparable<Pair>{
int a;
int b;
int idx;
Pair(int a,int idx){
this.a = a;
this.idx = idx;
}
public void setElement(int b){
this.b = b;
}
@Override
public int compareTo(Pair p){
return this.a-p.a;
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 6c4a4e1390a286e5d8e45e0a060e2a3d | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes |
import java.io.*;
import java.util.*;
public class solution {
static long cr[][]=new long[1001][1001];
//static double EPS = 1e-7;
static long mod=1000000007;
public static void main(String[] args) {
FScanner sc = new FScanner();
//Arrays.fill(prime, true);
//sieve();
//ncr();
int t=sc.nextInt();
StringBuilder sb = new StringBuilder();
while(t-->0) {
int n=sc.nextInt();
int k=sc.nextInt();
String s=sc.next();
if(pal(s) )
sb.append(1);
else if(k==0)
sb.append(1);
else
{
sb.append(Math.min(2, 2 ) );
}
sb.append("\n");
}
System.out.println(sb.toString());
}
public static boolean pal(String s)
{
int n=s.length();
for(int i=0;i<n/2;i++)
{
if(s.charAt(i)!=s.charAt(n-1-i) )
return false;
}
return true;
}
public static long gcd(long a,long b)
{
return b==0 ? a:gcd(b,a%b);
}
/* public static void sieve()
{ prime[0]=prime[1]=false;
int n=1000000;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
*/
public static void ncr()
{
cr[0][0]=1;
for(int i=1;i<=1000;i++)
{
cr[i][0]=1;
for(int j=1;j<i;j++)
{
cr[i][j]=(cr[i-1][j-1]+cr[i-1][j])%mod;
}
cr[i][i]=1;
}
}
}
class pair
//implements Comparable<pair>
{
long u;int d;
pair(long u,int d)
{
this.u=u;
this.d=d;
}
}
class FScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer sb = new StringTokenizer("");
String next(){
while(!sb.hasMoreTokens()){
try{
sb = new StringTokenizer(br.readLine());
} catch(IOException e){ }
}
return sb.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
float nextFloat(){
return Float.parseFloat(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 77de0cf204e3b71f74d7aa469b6b3ddb | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces770{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static boolean isPalindrome(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 first(){
int n = sc.nextInt();
int k = sc.nextInt();
String str = sc.nextLine();
if(k==0){
out.println(1);
return;
}
if(isPalindrome(str)){
out.println(1);
}else{
out.println(2);
}
}
static void second(){
int n = sc.nextInt();
long x = sc.nextLong();
long y = sc.nextLong();
long sum = 0;
for(int i = 0;i<n;i++){
sum += sc.nextLong();
}
if(((x+sum)&1)!=(y&1)){
out.println("Bob");
}else if(((x+sum+3)&1)!=(y&1)){
out.println("Alice");
}
}
static void third(){
int n = sc.nextInt();
int k = sc.nextInt();
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 j = 1;
int l = 2;
boolean flag = true;
for(int i = 1;i<=n;i++){
if(flag){
for(int h = 1;h<=k;j+=2,h++){
out.print(j+" ");
}
out.println();
}
if(!flag){
for(int h = 1;h<=k;l+=2,h++){
out.print(l+" ");
}
out.println();
}
flag = !flag;
}
}
static boolean isprime(int n){
if(n==1 || n==2) return false;
if(n==3) 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 freq;
Pair(int v,int f){
val = v;
freq = f;
}
public int compareTo(Pair p){
return this.freq - p.freq;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-- >0){
// solve();
first();
// second();
// third();
}
// 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[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | de4921dfd8f2060536785d763ac2f569 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.Scanner;
/**
* @author linjinping 11104660
* @date 2022/2/7 11:33
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count = Integer.parseInt(scanner.nextLine());
String[] ss = new String[count];
int[] ii = new int[count];
for (int i = 0; i < count; i++) {
ii[i] = Integer.parseInt(scanner.nextLine().split(" ")[1]);
ss[i] = scanner.nextLine();
}
for (int i = 0; i < count; i++) {
if (ii[i] < 1 || isReversable(ss[i])) {
System.out.println(1);
} else {
System.out.println(2);
}
}
}
private static boolean isReversable(String s) {
for (int i = 0, length = s.length(); i < length >> 1; i++) {
if (s.charAt(i) != s.charAt(length - i - 1)) {
return false;
}
}
return true;
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 432260c61235b5a906bc78225311aa81 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
// For fast input output
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 (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;
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
String[] readStrArray(int n) {
String a[] = new String[n];
for (int i = 0; i < n; i++)
a[i] = next();
return a;
}
}
// end of fast i/o code
static Set<String> set;
public static void main(String[] args) {
FastReader reader = new FastReader();
int testCases = reader.nextInt();
while (testCases-- > 0) {
set=new HashSet<String>();
int n = reader.nextInt();
// System.out.println(n+" "+k);
int k= reader.nextInt();
String str=reader.next();
helper(str,k);
}
}
static void helper(String str,int k){
if(k==0){
System.out.println(1);
return;
}
StringBuilder builder = new StringBuilder(str);
if(!builder.reverse().toString().equals(str)){
System.out.println(2);
}
else{
System.out.println(1);
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 5ab59e53e97c367fc34a03efca2f91fd | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args)throws IOException {
FastScanner scan = new FastScanner();
PrintWriter output = new PrintWriter(System.out);
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt(), k = scan.nextInt();
char arr[] = scan.next().toCharArray();
if(k == 0 || isPalindrome(arr)) output.println(1);
else output.println(2);
}
output.flush();
}
public static boolean isPalindrome(char arr[]) {
int i = 0, j = arr.length-1;
while(i<j) {
if(arr[i] != arr[j]) return false;
i++; j--;
}
return true;
}
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr) list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) arr[i] = list.get(i);
return arr;
}
public static int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b%a, a);
}
public static void printArray(int arr[]) {
for(int i:arr) System.out.print(i+" ");
System.out.println();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | d354e98f5778287269b60d027cbac226 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | /*Radhe-Krishna*/
import javax.xml.stream.events.Characters;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLType;
import java.util.*;
public class Main {
static int mod=1000000007;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
// PrintWriter out = new PrintWriter(System.out);
// 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();
String s=sc.next();
StringBuilder sb=new StringBuilder(s);
sb.reverse();
String str=sb.toString();
if (s.equals(str)) {
System.out.println(1);
}else {
if (k >= 1) {
System.out.println(2);
} else {
System.out.println(1);
}
}
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | bd07a096859c547b613c9e80e30dc049 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import static java.lang.System.out;
/**
* @author shardul_rajhans
*/
public class Main {
/**
* FastReader class to read input from command line.
*/
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;
}
List<Long> readList(int n) {
List<Long> arrayList = new ArrayList<>();
for (int i = 0; i < n; i++)
arrayList.add(reader.nextLong());
return arrayList;
}
long[] readArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = reader.nextLong();
return array;
}
}
/**
*
*/
private static final FastReader reader = new FastReader();
/**
* @param args Object of String
*/
public static void main(String[] args) {
int t = reader.nextInt();
for (int i = 0; i < t; i++) {
executeCases();
}
}
/**
*
*/
private static void executeCases() {
int N = reader.nextInt();
int K = reader.nextInt();
String S = reader.nextLine();
if (K == 0) {
out.println(1);
return;
}
StringBuilder reverse = new StringBuilder();
for ( int i = N - 1; i >= 0; i-- )
reverse.append(S.charAt(i));
out.println(S.equals(reverse.toString()) ? 1 : 2);
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 18167cdca1ee11d855134de27451704d | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int i = 0 ; i < t ;i++){
int n = input.nextInt();
int o = input.nextInt();
StringBuilder str = new StringBuilder(input.next());
String str1 = str.toString();
String str2 = str.reverse().toString();
if(o <1 || str1.equals(str2)){
System.out.println(1);
}
else{
System.out.println(2);
}
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | c0e986e87e037d65d65c700f305595be | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes |
import java.util.Scanner;
public class cfContest1634 {
static boolean isPal(String s) {
int l = 0;
int r = s.length() - 1;
while (l <= r) {
if (s.charAt(l) != s.charAt(r)) {
return false;
}
--r;
++l;
}
return true;
}
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();
String s = scan.next();
if (k == 0) {
System.out.println(1);
} else if (isPal(s)) {
System.out.println(1);
} else {
System.out.println(2);
}
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 65c1106663fb3f9043a116cd4fc4fd5e | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | /*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
public class GFG {
static HashSet<String> anslist;
static int p=0;
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
anslist=new HashSet<>();
int n=sc.nextInt();
int k=sc.nextInt();
String s=sc.next();
p=k;
String rev1="";
if(k<1)
{
System.out.println(1);
}
else{
for(int j=s.length()-1;j>=0;j--)
{
rev1=rev1+s.charAt(j);
}
if(s.equals(rev1))
{
System.out.println(1);
}
else{
System.out.println(2);
}
}
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 6250069e7093c12d32adcfeeb8c253f8 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
public class cp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt();
int k = in.nextInt();
in.nextLine();
String s = in.nextLine();
int sn = solve(s, k);
System.out.println(sn);
}
}
static int solve(String s, int k) {
if(isPalindrome(s)) {
return 1;
}
if(k == 0) {
return 1;
}
return 2;
}
static boolean isPalindrome(String str) {
int s = 0;
int e = str.length() - 1;
while(s < e) {
if(str.charAt(s) != str.charAt(e)) {
return false;
}
s++;
e--;
}
return true;
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | cd2e9a89df488cb18099f2e42ea915b5 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
public class ReverseConcatenate {
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();
sc.nextLine();
String s = sc.nextLine();
if (s.length() == 1) {
System.out.println(1);
} else if (k == 0) {
System.out.println(1);
} else {
boolean check = false;
int i = 0;
int j = s.length() - 1;
while (j > i) {
if (s.charAt(i) == s.charAt(j)) {
check = true;
} else {
check = false;
break;
}
j--;
i++;
}
if (check) {
System.out.println(1);
} else {
System.out.println(2);
}
}
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 3a2c4d088076cd90fcdc7d3481bf1bc2 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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, k;
String s;
public void solve(int testCases, InputReader in, PrintWriter out) {
for (int i = 0; i < testCases; ++i) {
n = in.nextInt();
k = in.nextInt();
s = in.next();
out.println(process(n, k, s));
}
}
private int process(int n, int k, String s) {
if (n < 2 || k < 1)
return 1;
if (isHuiwen(s))
return 1;
return 2;
}
private boolean isHuiwen(String s) {
int len = s.length();
for (int i = 0; i < len / 2; ++i) {
if (s.charAt(i) != s.charAt(len - 1 - i))
return false;
}
return true;
}
}
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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 559c1b26d12a433b1de871fce196dd9d | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
public class CodeForces {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (n > 0) {
n--;
int m = scanner.nextInt();
int k = scanner.nextInt();
scanner.nextLine();
String s = scanner.nextLine();
Set<String> l = new HashSet<>();
l.add(s);
int palin = 0;
while (k > 0) {
--k;
Set<String> tmp = new HashSet<>();
for (String str: l) {
if (check(str)) {
palin++;
continue;
}
String rev = new StringBuilder(str).reverse().toString();
tmp.add(str + rev);
tmp.add(rev + str);
}
l = tmp;
}
System.out.println(palin + l.size());
}
}
static boolean check(String s) {
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) return false;
++i;
--j;
}
return true;
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 5c05adeb73bfd3981ca40e33683debe7 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(null, new Solution(), "", 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("/Users/schhajed/Library/Application Support/JetBrains/IdeaIC2021.1/scratches/input.txt"));
out = new PrintWriter("/Users/schhajed/Library/Application Support/JetBrains/IdeaIC2021.1/scratches/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);
}
}
//method to sort values
private static HashMap sortValues(HashMap map) {
List list = new LinkedList(map.entrySet());
//Custom Comparator
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
}
});
//copying the sorted list in HashMap to preserve the iteration order
HashMap sortedHashMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
sortedHashMap.put(entry.getKey(), entry.getValue());
}
return sortedHashMap;
}
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());
}
void solve() throws IOException {
int numberOfTests = readInt();
while(numberOfTests-->0){
int n = readInt();
int k = readInt();
String s = readString();
HashSet<String> allPossibleString = new HashSet<String>();
allPossibleString.add(s);
boolean isPalindrome = true;
for(int i=0;i<s.length()/2;i++){
if(s.charAt(i)!=s.charAt(s.length()-i-1))
isPalindrome = false;
}
System.out.println( k==0 ? 1 : (isPalindrome ? 1 : 2));
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | e0297e859c54143cb79dedd36a384150 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.Scanner;
public class Test {
static Scanner sc = new Scanner(System.in);
static boolean isPalindrome(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - i - 1)) return false;
}
return true;
}
static void solve() {
int n = sc.nextInt();
int k = sc.nextInt();
String del = sc.nextLine();
String s = sc.nextLine();
if (isPalindrome(s)) System.out.println(1);
else if (k == 0) System.out.println(1);
else System.out.println(2);
}
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 920bd781a2f5b840ae0b3ce86318dd78 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) {
new A().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();
int ans=1;
String s= ns();
for(int i=0; i<n/2; i++){
if(i==n/2){
continue;
}
if(s.charAt(i)!=s.charAt(n-i-1)){
ans=2;
}
}
if(k==0){
ans=1;
}
out.println(ans);
}
}
class Pair{
String s;
int level;
Pair(String s, int level){
this.s=s;
this.level=level;
}
}
// -------- 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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | be42f24db0caaa23cf3fbfe87399d563 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class A_Reverse_and_Concatenate{
static final int MOD = (int) 1e9 + 7;
public static void main (String[] args){
FastReader s = new FastReader();
int t=1;t=s.ni();
for(int test=1;test<=t;test++){
int n = s.ni(), k = s.ni();
Boolean good = true;
String S=s.nextLine();char a[]=S.toCharArray();
for (int i = 0; i < n; i++) {
if (a[i] != a[n - 1 - i]) {
good = false;
break;
}
}
if(good==false&&k>0)
System.out.println(2);
else
System.out.println(1);
}
}
static class FastReader {
BufferedReader br; StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nl();
return a;
}
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;}
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void pArray(long[] a){
int n=a.length;
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
static long sum(long[] a){
int n=a.length;long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static boolean prime(long sq){
if(BigInteger.valueOf(sq).isProbablePrime(12))
return true;
return false;
}
}
//Collections.sort(a, Collections.reverseOrder()); | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | be2d5a9524147f38606f383e8feec9d3 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
import java.io.*;
public class _1634A {
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();
HashSet<Character> set = new HashSet<>();
String s = sc.next();
for (int i = 0; i < s.length(); i++) {
set.add(s.charAt(i));
}
if (set.size() == 1 || k == 0 || reverse(s).equals(s))
System.out.println(1);
else
System.out.println(2);
}
}
static String reverse(String s) {
String res = "";
for (int i = s.length() - 1; i >= 0; i--)
res += s.charAt(i);
return res;
}
static class FastIO {
InputStream dis;
byte[] buffer = new byte[1 << 17];
int pointer = 0;
public FastIO(String fileName) throws Exception {
dis = new FileInputStream(fileName);
}
public FastIO(InputStream is) throws Exception {
dis = is;
}
int nextInt() throws Exception {
int ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
long nextLong() throws Exception {
long ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
byte nextByte() throws Exception {
if (pointer == buffer.length) {
dis.read(buffer, 0, buffer.length);
pointer = 0;
}
return buffer[pointer++];
}
String next() throws Exception {
StringBuffer ret = new StringBuffer();
byte b;
do {
b = nextByte();
} while (b <= ' ');
while (b > ' ') {
ret.appendCodePoint(b);
b = nextByte();
}
return ret.toString();
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 69698a766c93b52c79d9ce90b95186a4 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.Scanner;
import javax.management.relation.RelationTypeNotFoundException;
public class Codeforces {
static boolean isPalin(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;
}
static String rev(String s){
String ans = "";
int n=s.length();
for(int i=0;i<n;i++){
ans+=s.charAt(n-i-1);
}
return ans;
}
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();
String s = sc.next();
if(k==0){System.out.println("1");}
else{
int cnt = 0;
while(!isPalin(s)){
cnt++;
s = s+rev(s);
}
System.out.println((int)Math.pow(2, cnt));
}
t--;
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | c7662df46a88216f7b51efb37d610744 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | /**
* @Jai_Bajrang_Bali
* @Har_Har_Mahadev
*/
import java.util.*;
public class practice2 {
static long mod = (int) 1e9 + 7;
static boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
static boolean IsPalindrome(int arr[], int n, int ele) {
int i = 0;
int j = n - 1;
while (i < j) {
if (arr[i] == ele) {
i++;
} else if (arr[j] == ele) {
j--;
} else if (arr[i] != arr[j]) return false;
else {
i++;
j--;
}
}
return true;
}
static long gcd(long a, long b) {
if (b > a) {
return gcd(b, a);
}
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
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();
sc.nextLine();
String str = sc.nextLine();
if (isPalindrome(str)) {
System.out.println("1");
} else {
if (k == 0) {
System.out.println(1);
} else {
System.out.println(2);
}
}
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | ec70895ec4bf616cb562a76d82c5759d | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
int t = in.nextInt();
for (int ttt = 0; ttt < t; ttt++) {
int n = in.nextInt();
int k = in.nextInt();
String s = in.next();
System.out.println(s.equals(new StringBuilder(s).reverse().toString()) || k == 0 ? 1 : 2);
}
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | eaf04c6b410fbd447e3aa2bc9ba4910c | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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();
int k=Sc.nextInt();
String s=Sc.next();
if(checkPalindrome(s.toCharArray(),0,s.length()-1))
{
System.out.println(1);
}
else if(k==0)
System.out.println(1);
else
System.out.println(2);
}
}
private static char getChar(int[] freq,int ch)
{
for(int i=0;i<26;i++)
{
if(freq[i]>0&&i!=ch)
{
freq[i]--;
return (char)(i+97);
}
}
return 65;
}
private static char getMaxChar(int[] freq)
{
int max=0;int j=-1;
for(int i=0;i<26;i++)
{
if(freq[i]>max)
{
max=freq[i];
j=i;
}
}
freq[j]--;
return (char)(j+97);
}
private static boolean checkPalindrome(char[] a,int left,int right)
{
while(left<right)
{
if(a[left]==a[right])
{
left++;right--;
}
else
return false;
}
return true;
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 4c31ead3a4c3db8be219775a20160319 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | /*
Rating: 1367
Date: 06-02-2022
Time: 20-06-34
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 A_Reverse_and_Concatenate {
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
public static void s() {
int n = sc.nextInt();int k = sc.nextInt();
String s = sc.nextLine();
if(k == 0) {
p.writeln(1);
return;
}
for(int i=0; i<s.length()/2; i++) {
if(s.charAt(i) != s.charAt(s.length() - i - 1)) {
p.writeln(2);
return;
}
}
p.writeln(1);
}
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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 5202f72bdc75f2ee4d57d972c177c954 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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();
String str = s.next();
if(k==0){sb.append("1\n");continue;}
boolean palin = true;
for(int i=0;i<str.length()/2;i++){
if(str.charAt(i) != str.charAt(str.length() - 1 - i)){
palin = false;break;
}
}
if(palin){
sb.append("1\n");
}else{
sb.append("2\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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | affad063941f90fd02f91106bf588554 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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();
scanner.nextLine();
String s = scanner.nextLine();
int pal = 1;
for(int i=0;i<n;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
pal = 0;
}
}
if(pal==1 || k==0){
System.out.println(1);
} else {
System.out.println(2);
}
t--;
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | d579f5f9f903406fb18ad5f025a7070b | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.*;
public class acmp {
public static void main(String[] args) {
Scanner css = new Scanner(System.in);
int t = css.nextInt();
while(t-- > 0){
int n = css.nextInt(), k = css.nextInt();
String str = css.next();
StringBuilder stringBuilder = new StringBuilder(str);
//System.out.println(stringBuilder.reverse().toString().equals(str));
if(str.equals(stringBuilder.reverse().toString()) || k == 0){
System.out.println(1);
}else
System.out.println(2);
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | fe95b90c99147b6bd4a085d043bebca4 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import static java.lang.System.out;
public class asa {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int s=sc.nextInt();
int b=sc.nextInt();
String a=sc.next();
String as="";
for(int i=s-1;i>=0;i--){
as+=a.charAt(i);
}
if(b==0||as.equals(a)){
out.println(1);
}
else{
out.println(2);
}
}
}
public void forr(){
int s=0;
int a[]=new int[s];
String d[]=new String[s];
for(int i=0;i<s;i++){
}
for(int i=s-1;i>=0;i--){
}
out.println("yes");
out.println("no");
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 4ff4960954cd3cd379667c453533ca31 | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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.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();
}
}
public static boolean sorted(int x[]){
for(int i=1;i<x.length;i++){
if(x[i]<x[i-1]) return false;
}
return true;
}
public static Long fact(int n){
//System.out.println(n);
Long x=(long) 1;
if(n==0 || n==1) return (long) 1;
else{
for(int i=2;i<=n;i++){
x=(x*(long)i)%(long)998244353;
//System.out.println(x);
}
return x;
}
}
public static boolean isPalindrome(String s,int n){
int f=0;
for(int i=0;i<n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)) f=1;
}
if(f==1) return false;
else return true;
}
public static void main(String[] args) throws Exception{
try {
FastReader in=new FastReader();
FastWriter out = new FastWriter();
int testcase=in.nextInt();
//in.nextLine();
while(testcase-- >0){
int n=in.nextInt();
int k=in.nextInt();
String s=in.nextLine();
if(k==0) out.println("1");
else{
if(isPalindrome(s,n)){
out.println("1");
}
else{
out.println("2");
}
}
}
out.close();
} catch (Exception e) {
return;
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | ce9453db6ecaca4c1efc8f2c6036c5de | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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(), k = readInt();
String s = readLn();
if (k==0)
{
return 1;
}
for (int i=0; i<n; i++)
{
if (s.charAt(i) != s.charAt(n-i-1))
{
return 2;
}
}
return 1;
}
}
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\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | eed0ef44c5eeecaa661d791683701c7c | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 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
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0){
int n=sc.nextInt(), k=sc.nextInt();
String str=sc.next();
StringBuilder sb=new StringBuilder(str);
sb=sb.reverse();
if(sb.toString().equals(str) || k==0){
System.out.println("1");
}
else{
System.out.println("2");
}
}
}
} | Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | 38e3691da88ac22b84b0158945684aed | train_107.jsonl | 1644158100 | Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. | 256 megabytes | import java.util.Scanner;
public class A1634 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
in.nextInt(); // N
int K = in.nextInt();
String S = in.next();
int answer;
if (K == 0) {
answer = 1;
} else {
String R = new StringBuilder(S).reverse().toString();
answer = S.equals(R) ? 1 : 2;
}
System.out.println(answer);
}
}
}
| Java | ["4\n\n3 2\n\naab\n\n3 3\n\naab\n\n7 1\n\nabacaba\n\n2 0\n\nab"] | 1 second | ["2\n2\n1\n1"] | NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab. | Java 8 | standard input | [
"greedy",
"strings"
] | 08cd22b8ee760a9d2dacb0d050dcf37a | The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. | 800 | For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. | standard output | |
PASSED | c491fbcf47ef3ac833195c6e0d7d65f9 | train_107.jsonl | 1644158100 | One of my most productive days was throwing away 1,000 lines of code.— Ken ThompsonFibonacci addition is an operation on an array $$$X$$$ of integers, parametrized by indices $$$l$$$ and $$$r$$$. Fibonacci addition increases $$$X_l$$$ by $$$F_1$$$, increases $$$X_{l + 1}$$$ by $$$F_2$$$, and so on up to $$$X_r$$$ which is increased by $$$F_{r - l + 1}$$$.$$$F_i$$$ denotes the $$$i$$$-th Fibonacci number ($$$F_1 = 1$$$, $$$F_2 = 1$$$, $$$F_{i} = F_{i - 1} + F_{i - 2}$$$ for $$$i > 2$$$), and all operations are performed modulo $$$MOD$$$.You are given two arrays $$$A$$$ and $$$B$$$ of the same length. We will ask you to perform several Fibonacci additions on these arrays with different parameters, and after each operation you have to report whether arrays $$$A$$$ and $$$B$$$ are equal modulo $$$MOD$$$. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round770F {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Round770F sol = new Round770F();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = false;
initIO(isFileIO);
// test();
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
int n = in.nextInt();
int q = in.nextInt();
int mod = in.nextInt();
int[] a = in.nextIntArray(n);
int[] b = in.nextIntArray(n);
Query[] op = new Query[q];
for(int j=0; j<q; j++)
op[j] = new Query(
in.next().equals("A")? Qtype.A: Qtype.B,
in.nextInt()-1,
in.nextInt()-1);
if(isDebug){
out.printf("Test %d\n", i);
}
boolean[] ans = solve(a, b, mod, op);
for(boolean yn: ans)
out.printlnAns(yn);
if(isDebug)
out.flush();
}
in.close();
out.close();
}
private void test() {
final Random rand = new Random();
final int n = 5;
final int q = 1;
final int mod = 2;
for(int t = 0; t<100000; t++) {
int[] a = new int[n];
int[] b = new int[n];
for(int i=0; i<n; i++) {
a[i] = rand.nextInt(mod);
b[i] = rand.nextInt(mod);
}
int[] a_ori = Arrays.copyOf(a, n);
int[] b_ori = Arrays.copyOf(b, n);
Query[] op = new Query[q];
for(int i=0; i<q; i++) {
int x = rand.nextInt(n);
int y = rand.nextInt(n);
int l = Math.min(x, y);
int r = Math.max(x, y);
op[i] = new Query(rand.nextInt(2) == 1? Qtype.A: Qtype.B, l, r);
}
boolean[] ans = solve(a, b, mod, op);
boolean[] expected = new boolean[q];
int[] fib = new int[n+1];
fib[0] = 1 % mod;
fib[1] = 1 % mod;
for(int i=2; i<=n; i++) {
fib[i] = fib[i-1]+fib[i-2];
fib[i] = fib[i] >= mod? fib[i]-mod: fib[i];
}
for(int i=0; i<q; i++) {
int[] c = op[i].c == Qtype.A? a: b;
for(int j=op[i].l; j<=op[i].r; j++) {
c[j] += fib[j-op[i].l];
c[j] %= mod;
}
boolean same = true;
for(int j=0; j<n; j++)
if(a[j] != b[j]) {
same = false;
break;
}
expected[i] = same;
}
if(!Arrays.equals(ans, expected)) {
System.out.println("wrong");
}
}
}
private boolean[] solve(int[] a, int[] b, int mod, Round770F.Query[] op) {
int n = a.length;
int q = op.length;
boolean[] ans = new boolean[q];
int[] d = new int[n];
// just play with difference arrays
// d=0 iff
// diff[0] = 0 and diff[i] = 0 for all i, so maintain # of zeros in diff will be sufficient
// adding fibonnacci is like having 1's in higher order diff
// we have to place one 1 and one -1 at the right place
// d can be represented by
// diff^0 = d[0] = d[0]
// diff^1 = d[1]-d[0] = d[1] - d[0]
// diff^2 = (d[2]-d[1]) - (d[1]-d[0]) = d[2] - 2d[1] + d[0]
// diff^3 = {(d[3]-d[2]) - (d[2]-d[1])} - {(d[2]-d[1])-(d[1]-d[0])}
// = d[3] - 3d[2] + 3d[1] - d[0]
// diff^4 = d[4] - 4d[3] + 4d[2] - 4d[1] + d[0]
// at 0, d[0] = diff^0
// at 1, diff^i += diff^(i+1)
// this is not what is needed exactly
// 1 1 2 3 5 8
// 0 1 1 2 3 5
// 0 0 1 1 2 3
// 0 0 0 1 1 2
// 0 0 0 0 1 1
// 0 0 0 0 0 1
// 0 0 0 0 0 1 0 0 1
// 0 0 0 0 0 0 1 -1 1
// 0
// 0
// 0
// 0
// 1 1
// 1
// diff[0] = d[0]
// diff[1] = d[1] - d[0]
// diff[2] = d[2] - d[1] - d[0]
// diff[3] = d[3] - d[2] - d[1]
// d[0] = diff[0]
// d[1] = diff[1] + d[0]
// d[2] = diff[2] + d[1] + d[0]
// d[3] = diff[3] + d[2] + d[1]
// so diff = 0 iff d = 0
// d[i+0] += 1 -> diff[i] += 1
// d[i+1] += 1 -> diff[i+1] remains same
// d[i+2] += 2 -> diff[i+2] remains same
// d[i+3] += 3
// d[i+4] += 5
// d[i+5] += 0 -> diff[i+5] -= 8
// -> diff[i+6] -= 5
for(int i=0; i<n; i++) {
d[i] = a[i]-b[i];
d[i] = d[i]<0? d[i]+mod: d[i];
}
int[] diff = new int[n];
diff[0] = d[0];
if(n > 1) {
diff[1] = d[1]-d[0];
diff[1] = diff[1]<0? diff[1]+mod: diff[1];
}
for(int i=2; i<n; i++) {
diff[i] = d[i]-d[i-1];
diff[i] = diff[i]<0? diff[i]+mod: diff[i];
diff[i] -= d[i-2];
diff[i] = diff[i]<0? diff[i]+mod: diff[i];
}
numZero = 0;
for(int i=0; i<n; i++)
if(diff[i] == 0)
numZero++;
// adding A l r
// -> diff[l] += 1, diff[r+1] -= fib[r-l+1], diff[r+2] -= fib[r-l]
int[] fib = new int[n+1];
fib[0] = 1 % mod;
fib[1] = 1 % mod;
for(int i=2; i<=n; i++) {
fib[i] = fib[i-1]+fib[i-2];
fib[i] = fib[i] >= mod? fib[i]-mod: fib[i];
}
for(int i=0; i<q; i++) {
int sign = op[i].c == Qtype.A? 1: -1;
int l = op[i].l;
int r = op[i].r;
update(diff, l, sign, mod);
update(diff, r+1, -fib[r-l+1]*sign, mod);
update(diff, r+2, -fib[r-l]*sign, mod);
ans[i] = numZero == n;
}
return ans;
}
int numZero;
void update(int[] diff, int index, int val, int mod) {
if(index >= diff.length)
return;
if(diff[index] == 0)
numZero--;
diff[index] += val;
if(diff[index] >= mod)
diff[index] -= mod;
if(diff[index] < 0)
diff[index] += mod;
if(diff[index] == 0)
numZero++;
}
static enum Qtype{
A, B;
}
static class Query{
Qtype c;
int l, r;
public Query(Round770F.Qtype c, int l, int r) {
this.c = c;
this.l = l;
this.r = r;
}
}
int solve(){
return 0;
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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[][] nextTreeEdges(int n, int offset){
int[][] e = new int[n-1][2];
for(int i=0; i<n-1; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextPairs(int n){
return nextPairs(n, 0);
}
int[][] nextPairs(int n, int offset) {
int[][] xy = new int[2][n];
for(int i=0; i<n; i++) {
xy[0][i] = nextInt() + offset;
xy[1][i] = nextInt() + offset;
}
return xy;
}
int[][] nextGraphEdges(){
return nextGraphEdges(0);
}
int[][] nextGraphEdges(int offset) {
int m = nextInt();
int[][] e = new int[m][2];
for(int i=0; i<m; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
int[] inIdx = new int[n];
int[] outIdx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][outIdx[u]++] = v;
inNeighbors[v][inIdx[v]++] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
int[] idx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][idx[u]++] = v;
neighbors[v][idx[v]++] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["3 5 3\n2 2 1\n0 0 0\nA 1 3\nA 1 3\nB 1 1\nB 2 2\nA 3 3", "5 3 10\n2 5 0 3 5\n3 5 8 2 5\nB 2 3\nB 3 4\nA 1 2"] | 1 second | ["YES\nNO\nNO\nNO\nYES", "NO\nNO\nYES"] | NoteExplanation of the test from the condition: Initially $$$A=[2,2,1]$$$, $$$B=[0,0,0]$$$. After operation "A 1 3": $$$A=[0,0,0]$$$, $$$B=[0,0,0]$$$ (addition is modulo 3). After operation "A 1 3": $$$A=[1,1,2]$$$, $$$B=[0,0,0]$$$. After operation "B 1 1": $$$A=[1,1,2]$$$, $$$B=[1,0,0]$$$. After operation "B 2 2": $$$A=[1,1,2]$$$, $$$B=[1,1,0]$$$. After operation "A 3 3": $$$A=[1,1,0]$$$, $$$B=[1,1,0]$$$. | Java 17 | standard input | [
"brute force",
"data structures",
"hashing",
"implementation",
"math"
] | ac8519dfce1b3b1fafc02820563a2dbd | The first line contains 3 numbers $$$n$$$, $$$q$$$ and $$$MOD$$$ ($$$1 \le n, q \le 3\cdot 10^5, 1 \le MOD \le 10^9+7$$$) — the length of the arrays, the number of operations, and the number modulo which all operations are performed. The second line contains $$$n$$$ numbers — array $$$A$$$ ($$$0 \le A_i < MOD$$$). The third line also contains $$$n$$$ numbers — array $$$B$$$ ($$$0 \le B_i < MOD$$$). The next $$$q$$$ lines contain character $$$c$$$ and two numbers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — operation parameters. If $$$c$$$ is "A", Fibonacci addition is to be performed on array $$$A$$$, and if it is is "B", the operation is to be performed on $$$B$$$. | 2,700 | After each operation, print "YES" (without quotes) if the arrays are equal and "NO" otherwise. Letter case does not matter. | standard output | |
PASSED | 8bf2572795cf81b4d76ea3a48ae3c63d | train_107.jsonl | 1644158100 | One of my most productive days was throwing away 1,000 lines of code.— Ken ThompsonFibonacci addition is an operation on an array $$$X$$$ of integers, parametrized by indices $$$l$$$ and $$$r$$$. Fibonacci addition increases $$$X_l$$$ by $$$F_1$$$, increases $$$X_{l + 1}$$$ by $$$F_2$$$, and so on up to $$$X_r$$$ which is increased by $$$F_{r - l + 1}$$$.$$$F_i$$$ denotes the $$$i$$$-th Fibonacci number ($$$F_1 = 1$$$, $$$F_2 = 1$$$, $$$F_{i} = F_{i - 1} + F_{i - 2}$$$ for $$$i > 2$$$), and all operations are performed modulo $$$MOD$$$.You are given two arrays $$$A$$$ and $$$B$$$ of the same length. We will ask you to perform several Fibonacci additions on these arrays with different parameters, and after each operation you have to report whether arrays $$$A$$$ and $$$B$$$ are equal modulo $$$MOD$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.IntStream;
/*
polyakoff
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int oo = (int) 2e9 + 10;
static final long OO = (long) 2e18 + 10;
static final int MOD = 998244353;
static final int N = (int) 3e5 + 1;
static long mod;
static long[] getLongArr(int n, Function<Integer, Long> func) {
return IntStream.rangeClosed(0, n).boxed().map(func).mapToLong(Long::longValue).toArray();
}
static long add(long a, long b) {
long res = (a + b) % mod;
if (res < 0)
res += mod;
return res;
}
static int zeros;
static void upd(long[] d, int i, long delta) {
zeros -= d[i] == 0 ? 1 : 0;
d[i] = add(d[i], delta);
zeros += d[i] == 0 ? 1 : 0;
}
static void solve() {
int n = in.nextInt();
int q = in.nextInt();
mod = in.nextInt();
long[] f = new long[n + 1];
f[1] = 1 % mod;
if (n >= 2) f[2] = 1 % mod;
for (int i = 3; i <= n; i++) {
f[i] = add(f[i - 2], f[i - 1]);
}
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
long[] b = new long[n];
for (int i = 0; i < n; i++) {
b[i] = in.nextInt();
}
long[] c = new long[n];
for (int i = 0; i < n; i++) {
c[i] = add(b[i], -a[i]);
}
long[] d = new long[n];
d[0] = c[0];
if (n >= 2) d[1] = add(c[1], -c[0]);
for (int i = 2; i < n; i++) {
d[i] = add(c[i], -c[i - 1] - c[i - 2]);
}
zeros = 0;
for (int i = 0; i < n; i++) {
zeros += d[i] == 0 ? 1 : 0;
}
for (int i = 0; i < q; i++) {
char ch = in.next().charAt(0);
int l = in.nextInt() - 1;
int r = in.nextInt() - 1;
if (ch == 'A') {
upd(d, l, -1);
if (r + 1 < n)
upd(d, r + 1, f[r - l + 2]);
if (r + 2 < n)
upd(d, r + 2, f[r - l + 1]);
} else {
upd(d, l, 1);
if (r + 1 < n)
upd(d, r + 1, -f[r - l + 2]);
if (r + 2 < n)
upd(d, r + 2, -f[r - l + 1]);
}
out.println(zeros == n ? "YES" : "NO");
}
}
public static void main(String[] args) {
in = new FastReader();
out = new PrintWriter(System.out);
int t = 1;
// t = in.nextInt();
while (t-- > 0) {
solve();
}
out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
this(System.in);
}
FastReader(String file) throws FileNotFoundException {
this(new FileInputStream(file));
}
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String line;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
}
| Java | ["3 5 3\n2 2 1\n0 0 0\nA 1 3\nA 1 3\nB 1 1\nB 2 2\nA 3 3", "5 3 10\n2 5 0 3 5\n3 5 8 2 5\nB 2 3\nB 3 4\nA 1 2"] | 1 second | ["YES\nNO\nNO\nNO\nYES", "NO\nNO\nYES"] | NoteExplanation of the test from the condition: Initially $$$A=[2,2,1]$$$, $$$B=[0,0,0]$$$. After operation "A 1 3": $$$A=[0,0,0]$$$, $$$B=[0,0,0]$$$ (addition is modulo 3). After operation "A 1 3": $$$A=[1,1,2]$$$, $$$B=[0,0,0]$$$. After operation "B 1 1": $$$A=[1,1,2]$$$, $$$B=[1,0,0]$$$. After operation "B 2 2": $$$A=[1,1,2]$$$, $$$B=[1,1,0]$$$. After operation "A 3 3": $$$A=[1,1,0]$$$, $$$B=[1,1,0]$$$. | Java 11 | standard input | [
"brute force",
"data structures",
"hashing",
"implementation",
"math"
] | ac8519dfce1b3b1fafc02820563a2dbd | The first line contains 3 numbers $$$n$$$, $$$q$$$ and $$$MOD$$$ ($$$1 \le n, q \le 3\cdot 10^5, 1 \le MOD \le 10^9+7$$$) — the length of the arrays, the number of operations, and the number modulo which all operations are performed. The second line contains $$$n$$$ numbers — array $$$A$$$ ($$$0 \le A_i < MOD$$$). The third line also contains $$$n$$$ numbers — array $$$B$$$ ($$$0 \le B_i < MOD$$$). The next $$$q$$$ lines contain character $$$c$$$ and two numbers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — operation parameters. If $$$c$$$ is "A", Fibonacci addition is to be performed on array $$$A$$$, and if it is is "B", the operation is to be performed on $$$B$$$. | 2,700 | After each operation, print "YES" (without quotes) if the arrays are equal and "NO" otherwise. Letter case does not matter. | standard output | |
PASSED | 5f78b0324d5136e065cabc343f7be79c | train_107.jsonl | 1644158100 | One of my most productive days was throwing away 1,000 lines of code.— Ken ThompsonFibonacci addition is an operation on an array $$$X$$$ of integers, parametrized by indices $$$l$$$ and $$$r$$$. Fibonacci addition increases $$$X_l$$$ by $$$F_1$$$, increases $$$X_{l + 1}$$$ by $$$F_2$$$, and so on up to $$$X_r$$$ which is increased by $$$F_{r - l + 1}$$$.$$$F_i$$$ denotes the $$$i$$$-th Fibonacci number ($$$F_1 = 1$$$, $$$F_2 = 1$$$, $$$F_{i} = F_{i - 1} + F_{i - 2}$$$ for $$$i > 2$$$), and all operations are performed modulo $$$MOD$$$.You are given two arrays $$$A$$$ and $$$B$$$ of the same length. We will ask you to perform several Fibonacci additions on these arrays with different parameters, and after each operation you have to report whether arrays $$$A$$$ and $$$B$$$ are equal modulo $$$MOD$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class FibonacciAdditions {
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int N = in.nextInt(), Q = in.nextInt(), mod = in.nextInt();
long[] fib = new long[300002];
fib[1] = 1;
for (int i = 2; i < 300002; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
fib[i] %= mod;
}
long[] arr1 = new long[N], arr2 = new long[N];
for (int i = 0; i < N; i++) {
arr1[i] = in.nextInt();
}
for (int i = 0; i < N; i++) {
arr2[i] = in.nextInt();
}
long[] diff = new long[N];
for (int i = 0; i < N; i++) {
diff[i] = arr1[i] - arr2[i];
diff[i] = (diff[i] + mod) % mod;
}
long[] change = new long[N];
for (int i = 0; i < N; i++) {
change[i] = diff[i];
for (int j = i - 1; j >= i - 2 && j >= 0; j--) {
change[i] -= diff[j];
change[i] = (change[i] + mod) % mod;
}
}
int zero = 0;
for (long i : change) {
if (i == 0) {
zero++;
}
}
for (int q = 0; q < Q; q++) {
if (in.next().charAt(0) == 'A') {
int l = in.nextInt() - 1 , r = in.nextInt() - 1;
if (change[l] == 0) {
zero--;
}
change[l]++;
change[l] = (change[l] + mod) % mod;
if (change[l] == 0) {
zero++;
}
if (r + 1 < N) {
if (change[r + 1] == 0) {
zero--;
}
change[r + 1] -= fib[r - l + 2];
change[r + 1] = (change[r + 1] + mod) % mod;
if (change[r + 1] == 0) {
zero++;
}
}
if (r + 2 < N) {
if (change[r + 2] == 0) {
zero--;
}
change[r + 2] -= fib[r - l + 1];
change[r + 2] = (change[r + 2] + mod) % mod;
if (change[r + 2] == 0) {
zero++;
}
}
} else {
int l = in.nextInt() - 1 , r = in.nextInt() - 1;
if (change[l] == 0) {
zero--;
}
change[l]--;
change[l] = (change[l] + mod) % mod;
if (change[l] == 0) {
zero++;
}
if (r + 1 < N) {
if (change[r + 1] == 0) {
zero--;
}
change[r + 1] += fib[r - l + 2];
change[r + 1] = (change[r + 1] + mod) % mod;
if (change[r + 1] == 0) {
zero++;
}
}
if (r + 2 < N) {
if (change[r + 2] == 0) {
zero--;
}
change[r + 2] += fib[r - l + 1];
change[r + 2] = (change[r + 2] + mod) % mod;
if (change[r + 2] == 0) {
zero++;
}
}
}
out.println(zero == N ? "YES" : "NO");
}
out.close();
}
static class Reader {
BufferedReader in;
StringTokenizer st;
public Reader() {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
} | Java | ["3 5 3\n2 2 1\n0 0 0\nA 1 3\nA 1 3\nB 1 1\nB 2 2\nA 3 3", "5 3 10\n2 5 0 3 5\n3 5 8 2 5\nB 2 3\nB 3 4\nA 1 2"] | 1 second | ["YES\nNO\nNO\nNO\nYES", "NO\nNO\nYES"] | NoteExplanation of the test from the condition: Initially $$$A=[2,2,1]$$$, $$$B=[0,0,0]$$$. After operation "A 1 3": $$$A=[0,0,0]$$$, $$$B=[0,0,0]$$$ (addition is modulo 3). After operation "A 1 3": $$$A=[1,1,2]$$$, $$$B=[0,0,0]$$$. After operation "B 1 1": $$$A=[1,1,2]$$$, $$$B=[1,0,0]$$$. After operation "B 2 2": $$$A=[1,1,2]$$$, $$$B=[1,1,0]$$$. After operation "A 3 3": $$$A=[1,1,0]$$$, $$$B=[1,1,0]$$$. | Java 11 | standard input | [
"brute force",
"data structures",
"hashing",
"implementation",
"math"
] | ac8519dfce1b3b1fafc02820563a2dbd | The first line contains 3 numbers $$$n$$$, $$$q$$$ and $$$MOD$$$ ($$$1 \le n, q \le 3\cdot 10^5, 1 \le MOD \le 10^9+7$$$) — the length of the arrays, the number of operations, and the number modulo which all operations are performed. The second line contains $$$n$$$ numbers — array $$$A$$$ ($$$0 \le A_i < MOD$$$). The third line also contains $$$n$$$ numbers — array $$$B$$$ ($$$0 \le B_i < MOD$$$). The next $$$q$$$ lines contain character $$$c$$$ and two numbers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — operation parameters. If $$$c$$$ is "A", Fibonacci addition is to be performed on array $$$A$$$, and if it is is "B", the operation is to be performed on $$$B$$$. | 2,700 | After each operation, print "YES" (without quotes) if the arrays are equal and "NO" otherwise. Letter case does not matter. | standard output | |
PASSED | 6bc1b63d93c3d0915f49a0f0b615632a | train_107.jsonl | 1644158100 | One of my most productive days was throwing away 1,000 lines of code.— Ken ThompsonFibonacci addition is an operation on an array $$$X$$$ of integers, parametrized by indices $$$l$$$ and $$$r$$$. Fibonacci addition increases $$$X_l$$$ by $$$F_1$$$, increases $$$X_{l + 1}$$$ by $$$F_2$$$, and so on up to $$$X_r$$$ which is increased by $$$F_{r - l + 1}$$$.$$$F_i$$$ denotes the $$$i$$$-th Fibonacci number ($$$F_1 = 1$$$, $$$F_2 = 1$$$, $$$F_{i} = F_{i - 1} + F_{i - 2}$$$ for $$$i > 2$$$), and all operations are performed modulo $$$MOD$$$.You are given two arrays $$$A$$$ and $$$B$$$ of the same length. We will ask you to perform several Fibonacci additions on these arrays with different parameters, and after each operation you have to report whether arrays $$$A$$$ and $$$B$$$ are equal modulo $$$MOD$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class F_Fibonacci_Additions{
static int count=0;
static Printer print=new Printer();
public static void main(String args[])throws IOException{
Reader sc=new Reader();
StringBuffer res=new StringBuffer();
int n=sc.nextInt();
int q=sc.nextInt();
long mod=sc.nextLong();
// System.out.println(mod);
long a[]=new long[n];
long b[]=new long[n];
long c[]=new long[n];
long[] f=fib(n,mod);
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
for(int i=0;i<n;i++)
c[i]=a[i]-b[i];
Long d[]=new Long[n];
d[0]=c[0];
if(n>=2){
d[1]=(c[1]-c[0]+2*mod)%mod;
}
for(int i=2;i<n;i++){
d[i]=(c[i]-c[i-1]-c[i-2]+2*mod)%mod;
}
for(int i=0;i<n;i++)
if(d[i]!=0)count++;
for(int que=0;que<q;que++){
char ch=sc.readLine().charAt(0);
int l=sc.nextInt();
int r=sc.nextInt();
switch(ch){
case 'A': count-=d[l-1]==0?0:1;
d[l-1]=(d[l-1]+1+mod)%mod;
count+=d[l-1]==0?0:1;
if(r<n){
count-=d[r]==0?0:1;
d[r]=(d[r]-f[r-l+2]+mod)%mod;
count+=d[r]==0?0:1;
}
if(r+1<n){
count-=d[r+1]==0?0:1;
d[r+1]=(d[r+1]-f[r-l+1]+mod)%mod;
count+=d[r+1]==0?0:1;
}
break;
case 'B':count-=d[l-1]==0?0:1;
d[l-1]=(d[l-1]-1+mod)%mod;
count+=d[l-1]==0?0:1;
if(r<n){
count-=d[r]==0?0:1;
d[r]=(d[r]+f[r-l+2]+mod)%mod;
count+=d[r]==0?0:1;
}
if(r<n-1){
count-=d[r+1]==0?0:1;
d[r+1]=(d[r+1]+f[r-l+1]+mod)%mod;
count+=d[r+1]==0?0:1;
}
break;
default: break;
}
//print.printArray((Integer[])d, "The d array at "+que);
// System.out.println(count);
if(count==0)
res.append("YES");
else
res.append("NO");
res.append("\n");
}System.out.println(res);
sc.close();
}
static long[] fib(int n,long mod){
long f[]=new long[n+4];
f[1]=1;
f[2]=1;
for(int i=2;i<n+4;i++)
f[i]=(f[i-1]%mod+f[i-2]%mod)%mod;
return f;
}
}
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[1]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == ' ') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
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;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
class Data implements Comparable<Data>{
int num;
public Data(int num){
this.num=num;
}
public int compareTo(Data o){
return o.num-num;
}
}
class Binary{
public String convertToBinaryString(long ele){
StringBuffer res=new StringBuffer();
while(ele>0){
if(ele%2==0)res.append(0+"");
else res.append(1+"");
ele=ele/2;
}
return res.reverse().toString();
}
}
class FenwickTree{
int bit[];
int size;
FenwickTree(int n){
this.size=n;
bit=new int[size];
}
public void modify(int index,int value){
while(index<size){
bit[index]+=value;
index=(index|(index+1));
}
}
public int get(int index){
int ans=0;
while(index>=0){
ans+=bit[index];
index=(index&(index+1))-1;
}
return ans;
}
}
class PAndC{
long c[][];
long mod;
public PAndC(int n,long mod){
c=new long[n+1][n+1];
this.mod=mod;
build(n);
}
public void build(int n){
for(int i=0;i<=n;i++){
c[i][0]=1;
c[i][i]=1;
for(int j=1;j<i;j++){
c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
}
}
}
class Trie{
int trie[][];
int revind[];
int root[];
int tind,n;
int sz[];
int drev[];
public Trie(){
trie=new int[1000000][2];
root=new int[600000];
sz=new int[1000000];
tind=0;
n=0;
revind=new int[1000000];
drev=new int[20];
}
public void add(int ele){
// System.out.println(root[n]+" ");
n++;
tind++;
revind[tind]=n;
root[n]=tind;
addimpl(root[n-1],root[n],ele);
}
public void addimpl(int prev_root,int cur_root,int ele){
for(int i=18;i>=0;i--){
int edge=(ele&(1<<i))>0?1:0;
trie[cur_root][1-edge]=trie[prev_root][1-edge];
sz[cur_root]=sz[trie[cur_root][1-edge]];
tind++;
drev[i]=cur_root;
revind[tind]=n;
trie[cur_root][edge]=tind;
cur_root=tind;
prev_root=trie[prev_root][edge];
}
sz[cur_root]+=sz[prev_root]+1;
for(int i=0;i<=18;i++){
sz[drev[i]]=sz[trie[drev[i]][0]]+sz[trie[drev[i]][1]];
}
}
public void findmaxxor(int l,int r,int x){
int ans=0;
int cur_root=root[r];
for(int i=18;i>=0;i--){
int edge=(x&(1<<i))>0?1:0;
if(revind[trie[cur_root][1-edge]]>=l){
cur_root=trie[cur_root][1-edge];
ans+=(1-edge)*(1<<i);
}else{
cur_root=trie[cur_root][edge];
ans+=(edge)*(1<<i);
}
}
System.out.println(ans);
}
public void findKthStatistic(int l,int r,int k){
//System.out.println("In 3");
int curr=root[r];
int curl=root[l-1];
int ans=0;
for(int i=18;i>=0;i--){
for(int j=0;j<2;j++){
if(sz[trie[curr][j]]-sz[trie[curl][j]]<k)
k-=sz[trie[curr][j]]-sz[trie[curl][j]];
else{
curr=trie[curr][j];
curl=trie[curl][j];
ans+=(j)*(1<<i);
break;
}
}
}
System.out.println(ans);
}
public void findSmallest(int l,int r,int x){
//System.out.println("In 4");
int curr=root[r];
int curl=root[l-1];
int countl=0,countr=0;
// System.out.println(curl+" "+curr);
for(int i=18;i>=0;i--){
int edge=(x&(1<<i))>0?1:0;
// System.out.println(trie[curl][edge]+" "+trie[curr][edge]+" "+sz[curl]+" "+sz[curr]);
if(edge==1){
countr+=sz[trie[curr][0]];
countl+=sz[trie[curl][0]];
}
curr=trie[curr][edge];
curl=trie[curl][edge];
}
countl+=sz[curl];
countr+=sz[curr];
System.out.println(countr-countl);
}
}
class Printer{
public <T > void printArray(T obj[] ,String details){
System.out.println(details);
for(int i=0;i<obj.length;i++)
System.out.print(obj[i]+" ");
System.out.println();
}
public <T> void print(T obj,String details){
System.out.println(details+" "+obj);
}
}
class Node{
long weight;
int vertex;
public Node(int vertex,long weight){
this.vertex=vertex;
this.weight=weight;
}
public String toString(){
return vertex+" "+weight;
}
}
class Graph{
int nv; //0 indexing i.e vertices starts from 0 input as 1 indexed for add Edge
List<List<Node>> adj;
boolean visited[];
public Graph(int n){
adj=new ArrayList<>();
this.nv=n;
// visited=new boolean[nv];
for(int i=0;i<n;i++)
adj.add(new ArrayList<Node>());
}
public void addEdge(int u,int v,long weight){
u--;v--;
Node first=new Node(v,weight);
Node second=new Node(u,weight);
adj.get(v).add(second);
adj.get(u).add(first);
}
public void dfscheck(int u,long curweight){
visited[u]=true;
for(Node i:adj.get(u)){
if(visited[i.vertex]==false&&(i.weight|curweight)==curweight)
dfscheck(i.vertex,curweight);
}
}
long maxweight;
public void clear(){
this.adj=null;
this.nv=0;
}
public void solve() {
maxweight=(1l<<32)-1;
dfsutil(31);
System.out.println(maxweight);
}
public void dfsutil(int msb){
if(msb<0)return;
maxweight-=(1l<<msb);
visited=new boolean[nv];
dfscheck(0,maxweight);
for(int i=0;i<nv;i++)
{
if(visited[i]==false)
{maxweight+=(1<<msb);
break;}
}
dfsutil(msb-1);
}
} | Java | ["3 5 3\n2 2 1\n0 0 0\nA 1 3\nA 1 3\nB 1 1\nB 2 2\nA 3 3", "5 3 10\n2 5 0 3 5\n3 5 8 2 5\nB 2 3\nB 3 4\nA 1 2"] | 1 second | ["YES\nNO\nNO\nNO\nYES", "NO\nNO\nYES"] | NoteExplanation of the test from the condition: Initially $$$A=[2,2,1]$$$, $$$B=[0,0,0]$$$. After operation "A 1 3": $$$A=[0,0,0]$$$, $$$B=[0,0,0]$$$ (addition is modulo 3). After operation "A 1 3": $$$A=[1,1,2]$$$, $$$B=[0,0,0]$$$. After operation "B 1 1": $$$A=[1,1,2]$$$, $$$B=[1,0,0]$$$. After operation "B 2 2": $$$A=[1,1,2]$$$, $$$B=[1,1,0]$$$. After operation "A 3 3": $$$A=[1,1,0]$$$, $$$B=[1,1,0]$$$. | Java 8 | standard input | [
"brute force",
"data structures",
"hashing",
"implementation",
"math"
] | ac8519dfce1b3b1fafc02820563a2dbd | The first line contains 3 numbers $$$n$$$, $$$q$$$ and $$$MOD$$$ ($$$1 \le n, q \le 3\cdot 10^5, 1 \le MOD \le 10^9+7$$$) — the length of the arrays, the number of operations, and the number modulo which all operations are performed. The second line contains $$$n$$$ numbers — array $$$A$$$ ($$$0 \le A_i < MOD$$$). The third line also contains $$$n$$$ numbers — array $$$B$$$ ($$$0 \le B_i < MOD$$$). The next $$$q$$$ lines contain character $$$c$$$ and two numbers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — operation parameters. If $$$c$$$ is "A", Fibonacci addition is to be performed on array $$$A$$$, and if it is is "B", the operation is to be performed on $$$B$$$. | 2,700 | After each operation, print "YES" (without quotes) if the arrays are equal and "NO" otherwise. Letter case does not matter. | standard output | |
PASSED | 79a066dab9755835efb9c8c5a582b214 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.lang.*;
public class Solution
{
public static void main(String[] args)
{
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<=k*n;i++)
{
System.out.println(i);
}
continue;
}
if(n%2!=0)
{
System.out.println("NO");
continue;
}
System.out.println("YES");
for(int i=1;i<=n*k;i+=2)
{
int c=0;
while(c<k)
{
System.out.print(i+" ");
c++;
i+=2;
}
i-=2;
System.out.println();
}
//System.out.println();
for(int j=2;j<=n*k;j+=2)
{
int v=0;
while(v<k)
{
System.out.print(j+" ");
v++;
j+=2;
}
j-=2;
System.out.println();
}
// 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 | e89e36687dde001587e5f88dfdb6fd44 | 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 java.util.*;
import java.io.*;
import java.math.*;
public class temp {
// Let's Go!! ------------->
static FastScanner sc;
static PrintWriter out;
public static void main(String[] args) {
sc = new FastScanner();
out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int r = ni();
int c = ni();
if(r ==1 && c == 1) {
pn("YES");
pn(1);
continue;
}
if(c == 1) {
pn("YES");
for(int i = 0; i< r; i++) {
pn(i+1);
}
continue;
}
if((r==1) && (c == 2)) {
pn("NO");
continue;
}
if((r%2 == 0) || c == 1 ){
StringBuilder sb = new StringBuilder();
boolean odd = true;
int od= 1, even =2;
for(int i = 1; i <= r; i++) {
for(int j = 1; j<= c; j++) {
if(odd) {
sb.append(od + " ");
od+=2;
} else {
sb.append(even + " ");
even+=2;
}
}
odd = !odd;
sb.append("\n");
}
pn("YES");
pn(sb.toString());
}
// else if((c == 2)){
// StringBuilder sb = new StringBuilder();
// boolean odd = true;
// int od= 1, even =2;
// for(int i = 1; i <= r; i++) {
// for(int j = 1; j<= c; j++) {
// if(odd) {
// sb.append(od + " ");
// od+=2;
// } else {
// sb.append(even + " ");
// even+=2;
// }
// }
// odd = !odd;
// sb.append("\n");
// }
// pn("YES");
// pn(sb.toString());
// }
else {
// StringBuilder sb = new StringBuilder();
// int ii = 1;
// for(int i = 1; i <= r; i++) {
// for(int j = 1; j<= c; j++) {
// sb.append(ii++ + " ");
// }
// sb.append("\n");
// }
// pn("YES");
// pn(sb.toString());
pn("NO");
}
}
// -------END-------
out.close();
}
// <-----------------------Template --------------------->
// Static Initializations ------>
static final long MOD = (long)1e9+7;
static final int MAX = (int)1e5+1, INF = (int)1e9;
// GCD -------->
static long gcdl(long a, long b){return (b==0)?a:gcdl(b,a%b);}
static int gcdi(int a, int b){return (b==0)?a:gcdi(b,a%b);}
// Pair Class && Sort by First Value(Asc)----------->
static class pair<T> implements Comparable<pair>{
T first, second;
pair(T first, T second) {
this.first = first;
this.second = second;
}
@Override
public int compareTo(pair o) {
return (Integer) this.first - (Integer) o.first;
}
}
// Ruffle Sort
static void ruffleSort(int[] a) {
//Shuffle
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;
}
//then sort
Arrays.sort(a);
}
// Fast I/O
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
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;
}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() {return Double.parseDouble(next());}
}
//Quick Print Statements ---->
static void p(Object o){out.print(o);}
static void pn(Object o){out.println(o);}
static void pni(Object o){out.println(o);out.flush();}
// Quick Input Statements ----->
static String n(){return sc.next();}
static String nln(){return sc.nextLine();}
static int ni(){return Integer.parseInt(sc.next());}
static long nl(){return Long.parseLong(sc.next());}
static double nd(){return Double.parseDouble(sc.next());}
//Integer Array Input ---->
static int[] readIntArr(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]= sc.nextInt();
return a;
}
//Long Array Input ----->
static long[] readLongArr(int N){
long[] a = new long[N];
for(int i = 0; i<N; i++)a[i] = sc.nextLong();
return a;
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 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 | 643f0af804a120332f941fb0593b50d8 | 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.HashMap;
import java.util.Map;
import java.util.Scanner;
public class ex {
static Scanner scan = new Scanner(System.in);
public static void solve() {
// int n = scan.nextInt();
// int x = scan.nextInt();
// int y = scan.nextInt();
// int[] arr = new int[n];
// for (int i = 0; i < arr.length; i++) {
// arr[i] = scan.nextInt();
// }
int n = scan.nextInt();
int k = scan.nextInt();
int x = n * k;
x += 1;
if (x / 2 % k != 0) {
System.out.println("NO");
} else {
if(n==1){
if1(k);
}else if(k==1){
if1(n);
}else{
System.out.println("YES");
int y=1;
for (int i = 0; i < (n*k)/2/k; i++) {
for (int j = 0; j < k; j++) {
System.out.print(y + " ");
y += 2;
}
System.out.println();
}
int l =2;
for(int i =0;i<(n*k)/2/k;i++){
for(int j=0;j<k;j++){
System.out.print(l+" ");
l+=2;
}
System.out.println();
}
}
}
}
public static void if1(int n) {
System.out.println("YES");
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
public static void main(String[] args) {
int t = scan.nextInt();
while (t-- > 0)
solve();
// solve();
}
} | 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 | 15b0014403756edf62c08a752ccf56e9 | 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 _1634c {
FastScanner scn;
PrintWriter w;
PrintStream fs;
int MOD = 1000000007;
int MAX = 200005;
long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);}
long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
void ruffleSort(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);}
void reverseSort(int[] arr){List<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);}}
boolean LOCAL;
void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));}
//SUFFICIENT DRY RUN????LOGIC VERIFIED FOR ALL TEST CASES???
void solve(){
int t=scn.nextInt();
while(t-->0)
{
int n=scn.nextInt();
int k=scn.nextInt();
int total = n*k;
if(total==1){
w.println("YES");
w.println(1);
}
else{
int countOdd=0;
if((total&1)!=0)
countOdd = total/2+1;
else{
countOdd = total/2;
}
debug(countOdd);
int countEv = total - countOdd;
int oddshelves = ((countOdd%k)==0?countOdd/k:countOdd/k+1);
int remrows = (n-oddshelves);
int allowed = k*remrows;
if(countEv>allowed){
w.println("NO");
}else{
w.println("YES");
int ac = 0;
for(int i=1;i<=total;i+=2){
w.print(i+" ");
ac++;
if(ac==k){
w.println();
ac=0;
}
}
if(ac!=0)
w.println();
int count = 0;
for(int i=2;i<=total;i+=2){
w.print(i+" ");
count++;
if(count==k){
w.println();
count=0;
}
}
if(count!=0) w.println();
}
}
}
}
void run() {
try {
long ct = System.currentTimeMillis();
scn = new FastScanner(new File("input.txt"));
w = new PrintWriter(new File("output.txt"));
fs=new PrintStream("error.txt");
System.setErr(fs);
LOCAL=true;
solve();
w.close();
System.err.println(System.currentTimeMillis() - ct);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
scn = new FastScanner(System.in);
w = new PrintWriter(System.out);
LOCAL=false;
solve();
w.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; }
void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}
long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;}
int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b))
boolean nextPermutation(int[] arr) {if(arr == null || arr.length <= 1){return false;}int last = arr.length-2;while(last >= 0){if(arr[last] < arr[last+1]){break;}last--;}if (last < 0){return false;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;}
public static void main(String[] args) {
new _1634c().runIO();
}
}
| 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 | e2899679cdd7f93b4953f3527f539c31 | 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 | //CP- MASS_2701
import java.util.*;
import java.io.*;
public class C_OKEA {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
public static void main(String args[]) throws IOException {
int t=in.nextInt();
int cse=1;
loop:
while(t-->0)
{
StringBuilder res=new StringBuilder();
//res.append("Hello"+"\n");
int n=in.nextInt();
int k=in.nextInt();
if(k==1)
{
println("YES");
for(int i=1;i<=n*k;i++)
{
println(i);
}
}
else if(n==1)
{
println("NO");
}
else if(n%2==0)
{
println("YES");
int lim_odd=n*k-1;
int i=1;
while(i<lim_odd)
{
int lim=k;
while(lim>0)
{
print(i+" ");
lim--;
i=i+2;
}
println("");
}
int lim_even=n*k;
int e=2;
while(e<lim_even)
{
int lim=k;
while(lim>0)
{
print(e+" ");
lim--;
e=e+2;
}
println("");
}
}
else
{
println("NO");
}
// print(res);
}
}
static int max(int a, int b)
{
if(a<b)
return b;
return a;
}
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 < E > void print(E res)
{
System.out.print(res);
}
static < E > void println(E res)
{
System.out.println(res);
}
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 / gcd(a, b)) * b;
}
static int abs(int a)
{
if(a<0)
return -1*a;
return a;
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static class 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;
}
}
}
| 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 | e856dc4fb782f9f78f26a110476f0e73 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static AReader scan = new AReader();
static int MOD = (int)1e9+7;
static void slove() {
int n = scan.nextInt();
int m = scan.nextInt();
if(m == 1){
System.out.println("Yes");
for(int i = 1;i<=n;i++){
System.out.println(i);
}
}else{
if(n * m % 2 == 1){
System.out.println("NO");
}else{
if((n * m / 2) % m != 0){
System.out.println("NO");
return;
}
int sum = n * m / 2;
if(sum % (n /2) != 0){
System.out.println("NO");
return;
}
System.out.println("YES");
for(int i = 1,j=0;i<=n*m;i+=2,j++){
if(j == m){
j = 0;
System.out.println();
}
System.out.print(i+" ");
}
System.out.println();
for(int i = 2,j=0;i<=n*m;i+=2,j++){
if(j == m){
j = 0;
System.out.println();
}
System.out.print(i+" ");
}
System.out.println();
}
}
}
public static void main(String[] args) {
int T = scan.nextInt();
while (T-- > 0) {
slove();
}
}
}
class AReader {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
return null;
}
}
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.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
class Pair {
int x;
int y;
long dis;
public Pair(int x, int y, long dis) {
this.x = x;
this.y = y;
this.dis = dis;
}
}
| 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 | 7ef30bc35132ead0bb7a38df602307ca | 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 PrintWriter out;
public static void main(String[] args)throws IOException{
Scanner sc=new Scanner();
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");
int p=1;
for(int i=0;i<n;i++) {
out.println(p++);
}
continue;
}
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");
for(int i=0;i<n;i++) {
for(int j=0;j<k;j++) {
out.print(a[i][j]+" ");
}
out.println();
}
}
}
out.close();
}
public static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
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 | a0bac2d2ef49b6f6f446a710cdb6328d | 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 Solution
{
public static void main(String ab[])throws Exception
{
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)
{
System.out.println("YES");
for(int j=1;j<=n*k;j++)
System.out.println(j);
}
else
if(n%2==0)
{
System.out.println("YES");
int a[][]=new int[n][k];
int c;
for(int j=0;j<n;j++)
{
c=j+1;
for(int l=0;l<k;l++)
{
a[j][l]=c;
c+=n;
}
}
for(int j=0;j<n;j++)
{
for(int l=0;l<k;l++)
{
System.out.print(a[j][l]+" ");
}
System.out.println();
}
}
else
System.out.println("NO");
}
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 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 | ec1d6213ed62d33a00f8d290f0fa453a | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C_OKEA {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
int T = readInt();
while (T --> 0) {
int n = readInt(), k = readInt();
if (k == 1) {
System.out.println("YES");
for (int i=1; i<=n; i++) {
System.out.println(i);
}
continue;
}
// if (n == 2) {
// System.out.println("YES");
// int[][] ans = new int[3][k+1];
// ans[1][1] = 1;
// ans[2][1] = 2;
// for (int i=1; i<=2; i++) {
// for (int j=2; j<=k; j++) {
// ans[i][j] = ans[i][j-1] + 2;
// }
// }
//
// for (int i=1; i<=2; i++) {
// for (int j=1; j<=k; j++) {
// System.out.print(ans[i][j] + " ");
// }
// System.out.println();
// }
// continue;
// }
if (n % 2 == 0) {
System.out.println("YES");
int[][] ans = new int[n+1][k+1];
for (int i=1; i<=n; i+=2) {
ans[i][1] = (i - 1) * k + 1;
ans[i+1][1] = (i - 1) * k + 2;
for (int l=i; l<=i+1; l++) {
for (int j=2; j<=k; j++) {
ans[l][j] = ans[l][j-1] + 2;
}
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=k; j++) {
System.out.print(ans[i][j] + " ");
}
System.out.println();
}
continue;
}
System.out.println("NO");
}
}
static String next () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static long readLong () throws IOException {
return Long.parseLong(next());
}
static int readInt () throws IOException {
return Integer.parseInt(next());
}
static double readDouble () throws IOException {
return Double.parseDouble(next());
}
static char readCharacter () throws IOException {
return next().charAt(0);
}
static String readLine () throws IOException {
return br.readLine().trim();
}
}
| 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 | e358ebdacea72c3d3c2d3f2984de1355 | 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.BigInteger;
public class Main {
static int x;
static int y;
static int length;
public static void main(String[] args) throws IOException, InterruptedException {
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);
}
}
else {
if(n==1||n%2!=0) {
pw.println("NO");
}
else {
if((n*k)%2==0) {
pw.println("YES");
int even = 2;
while(even<=n*k) {
StringBuilder ev = new StringBuilder();
for (int i = 0; i < k; i++) {
ev.append(even+" ");
even +=2;
}
pw.println(ev);
}
int odd = 1;
while(odd<=n*k) {
StringBuilder ev = new StringBuilder();
for (int i = 0; i < k; i++) {
ev.append(odd+" ");
odd +=2;
}
pw.println(ev);
}
}
else {
pw.println("NO");
}
}
}
}
pw.flush();
}
}
class pair implements Comparable<pair> {
int a;
int b;
public pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(pair x) {
return a - x.a;
}
}
class edge implements Comparable<edge> {
int from;
int to;
long cost;
public edge(int from, int to, long cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
public int compareTo(edge x) {
if (cost < x.cost)
return -1;
if (cost > x.cost)
return 1;
return 0;
}
}
class segTree {
long[] arr, sTree;
int n;
public segTree(long[] a) {
n = 1;
while (n < a.length)
n <<= 1;
arr = new long[n + 1];
Arrays.fill(arr, Long.MAX_VALUE);
for (int i = 0; i < a.length; i++) {
arr[i + 1] = a[i];
}
sTree = new long[n << 1];
build(1, 1, n);
}
public void build(int curr, int l, int r) {
// if(l>r)
// return;
if (l == r) {
sTree[curr] = arr[curr - (n - 1)];
return;
}
int mid = (l + r) >> 1;
build(curr << 1, l, mid);
build((curr << 1) + 1, mid + 1, r);
sTree[curr] = Math.min(sTree[curr << 1], sTree[(curr << 1) + 1]);
}
public long q(int l, int r) {
return query(1, l, r, 1, n);
}
public long query(int curr, int qL, int qR, int l, int r) {
if (l >= qL && r <= qR) {
return sTree[curr];
}
if (r < qL || l > qR) {
return Long.MAX_VALUE;
}
int mid = (l + r) >> 1;
long left = query((curr << 1), qL, qR, l, mid);
long right = query((curr << 1) + 1, qL, qR, mid + 1, r);
return Math.min(left, right);
}
public void update(int i, int val) {
i = i + n - 1;
sTree[i] = val;
i >>= 1;
while (i >= 1) {
sTree[i] = sTree[i << 1] + sTree[(i << 1) + 1];
i >>= 1;
}
}
}
class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArr(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextLong();
}
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 | bb6ace750c89311c2b44ee43806b5033 | 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Arrays;
public class Cv {
//==========================Solution============================//
public static void main(String[] args) {
FastScanner in = new FastScanner();
Scanner input = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = input.nextInt();
while (T-- > 0) {
int n = input.nextInt();
int k = input.nextInt();
int u = 1;
int r = 2;
if (k == 1) {
out.println("YES");
for (int i = 1; i <= n; i++) {
out.println(i);
}
} else {
if (n == 1) {
out.println("NO");
} else {
int y = n * k;
if (y % 2 == 0 && (y / 2) % k == 0) {
out.println("YES");
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < k; j++){
out.print(u + " ");
u+=2;
}
out.println();
} else {
for (int j = 0; j < k; j++){
out.print(r + " ");
r+=2;
}
out.println();
}
}
} else {
out.println("NO");
}
}
}
}
out.close();
}
//==============================================================//
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
byte nextByte() {
return Byte.parseByte(next());
}
short nextShort() {
return Short.parseShort(next());
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return java.lang.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 | 568fa445ad5d08d256cb91c3098f87d3 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.StringTokenizer;
import java.io.PrintWriter;
public class Template {
public static void main(String[] args){
FastReader in = new FastReader();
int t = in.nextInt();
PrintWriter out = new PrintWriter(System.out);
while (t-- > 0){
int n = in.nextInt();
int k = in.nextInt();
int[][] arr = new int[n][k];
Stack s = new Stack<>();
for(int q = n*k;q>0;q--){
s.push(q);
}
for(int j=0; j<k; j++){
for (int i= 0;i<n;i++) {
arr[i][j] = (int) s.pop();
}
}
if(k==1 || n%2 == 0){
out.println("YES");
for(int i=0; i<n; i++){
for (int j= 0;j<k;j++) {
out.print(arr[i][j] + " ");
}
out.println();
}
}else{
out.println("NO");
}
}
out.flush();
}
public static int checkSumRange(int[][] arr, int start, int end,int row){
int sum = 0;
for (int j=start; j<=end; j++) {
sum+=arr[row][j];
}
return sum;
}
// FastReader Class
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;
}
}
// Count Frequencies
// If Sorted returns the element
static HashMap <Integer, Integer> hm = new HashMap<Integer, Integer>();
static void countFreq(int a[], int n)
{
for (int i=0; i<n; i++)
if (hm.containsKey(a[i]) )
hm.put(a[i], hm.get(a[i]) + 1);
else hm.put(a[i] , 1);
}
static int query(int x)
{
if (hm.containsKey(x))
return hm.get(x);
return 0;
}
// returns all elements
static void countFreqAll(int arr[], int n)
{
Map<Integer, Integer> mp = new HashMap<>();
for (int i = 0; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1);
}
else
{
mp.put(arr[i], 1);
}
}
for (Map.Entry<Integer, Integer> entry : mp.entrySet())
{
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
public static void inserstionSort(int a[]) {
for (int i = 1; i < a.length; i++ ) {
int value = a[i];
int j;
for ( j = i - 1; j >= 0 && a[j] > value; j-- ){
a[j+1] = a[j];
}
a[j+1] = value;
}
}
static void printArray(int[] arr) // for testing only
{
int size = arr.length;
for(int i = 0; i < size; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
static int numDigits(int num){
return (int) Math.floor(Math.log10(num)) + 1;
}
static boolean isPrime(int n){
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static int minArr(int[] array){
int min = Arrays.stream(array).min().getAsInt();
return min;
}
public static int maxArr(int[] array){
int max = Arrays.stream(array).max().getAsInt();
return max;
}
public static int getArrayIndex(int[] arr,int value) {
int k=-1;
for(int i=0;i<arr.length;i++){
if(arr[i]==value){
k=i;
break;
}
}
return k;
}
public static boolean isSorted(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1])
return false;
}
return true;
}
public static void reverseSub(int[] array, int start, int end){
int tmp;
if (start>end){
return;
}
while(start<end){
tmp = array[start];
array[start] = array[end];
array[end] = tmp;
start++;
end--;
}
}
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 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 | 4e07ea093525fb05fe2d691a1eb57afc | 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.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.io.*;
public class A {
BufferedReader br;
StringTokenizer st;
public A(){ // constructor
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
static void sort(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i = 0; i < n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static long gcd(long a, long b) {
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
static long mod = (int)1e9 + 7;
static int dir[][] = {{1, 0}, {-1, 0}, {0 ,1}, {0, -1}};
static boolean isPrime(long n){
if(n <= 1){
return false;
}
else if(n == 2){
return true;
}
else if(n % 2 == 0){
return false;
}
for(long i = 3; i * i <= n; i += 2){
if(n % i == 0){
return false;
}
}
return true;
}
public static void main(String[] args) throws Exception{
A in = new A();
PrintWriter out = new PrintWriter(System.out);
/* hard work will power dedication
* Rippling CNIL Spri GOOG.
* Live Life King Size
*/
/* array of arraylist
* ArrayList<Integer> arr[] = new ArrayList[n];
*/
int test = in.nextInt();
while(test-- > 0)
{
int n = in.nextInt();
int k = in.nextInt();
/*
* if col is 1 then always possible
*
* s of ap = n/2(2 * a + (n - 1)*d)
* mean s / n; and d = 2 so always mean will be integer
* proved.
*/
if(n % 2 == 0)
{
out.println("YES");
int even = 2;
int odd = 1;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= k; j++)
{
if(i % 2 == 0)
{
out.print(even + " ");
even += 2;
}
else
{
out.print(odd + " ");
odd += 2;
}
}
out.println();
}
}
else
{
if(k == 1)
{
out.println("YES");
for(int i = 1; i <= n; i++)
{
out.println(i);
}
}
else
{
out.println("NO");
}
}
}
out.flush();
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 | f8d1bef7f2e71e1f377474150a0a0906 | 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.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.io.*;
public class TestClass {
BufferedReader br;
StringTokenizer st;
public TestClass(){ // constructor
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
static void sort(int[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i = 0; i < n; ++i){
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static void sort(long[] arr){
int n = arr.length;
Random rnd = new Random();
for(int i = 0; i < n; ++i){
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static long gcd(long a, long b) {
if(b == 0) { return a; } return gcd(b, a % b);
}
static boolean isSafe(int r, int c, int n, int m){
if(r >= 0 && r < n && c >= 0 && c < m){
return true;
}
return false;
}
static long mod = (long)1e9 + 7;
static int dir[][] = {{1, 0}, {-1, 0}, {0 ,1}, {0, -1}};
static int find_par(int u, int par[])
{
if(par[u] == u)
{
return u;
}
return par[u] = find_par(par[u], par);
}
static void merge(int p1, int p2, int par[], int size[])
{
if(size[p1] > size[p2])
{
size[p1] += size[p2];
par[p2] = p1;
}
else
{
size[p2] += size[p1];
par[p1] = p2;
}
}
public static void main(String[] args) throws Exception{
TestClass in = new TestClass();
PrintWriter out = new PrintWriter(System.out);
// 000...1000 -> 2^k (kth bit i.e. kth index 0 based)
// how to find msb of n -> simply for(int i = 0; i < 32; i++) if(n & mask > 0) last_mask = mask---- mask = 1 << i
int test = in.nextInt();
while(test-- > 0)
{
int n = in.nextInt();
int k = in.nextInt();
if((n * k) % 2 == 0)
{
// Proof?
if(n == 1)
{
out.println("NO");
}
else
{
int even = 2;
int odd = 1;
boolean status = true;
int matrix[][] = new int[n][k];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < k; j++)
{
if(i % 2 == 0)
{
matrix[i][j] = even;
if(even > n*k)
{
status = false;
}
even += 2;
}
else
{
matrix[i][j] = odd;
if(odd > n*k)
{
status = false;
}
odd += 2;
}
}
}
if(status)
{
out.println("YES");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < k; j++)
{
out.print(matrix[i][j] + " ");
}
out.println();
}
}
else
{
out.println("NO");
}
}
}
else
{
if(n * k == 1)
{
out.println("YES");
out.println(1);
}
else if(k == 1)
{
out.println("YES");
for(int i = 1; i <= n; i++)
{
out.println(i);
}
}
else
{
out.println("NO");
}
}
}
out.flush();
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 | a4395cee6c94e9183d20ff0e3ddb7aad | 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 OKEA
{
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 (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 class DisjointSet
{
int parent[] = null;
int ranks[] = null;
DisjointSet(int len)
{
parent = new int[len];
ranks = new int[len];
for (int indx = 0; indx < parent.length; indx++)
{
parent[indx] = indx;
ranks[indx] = 0;
}
}
// Time complexity: O(Alpha(n)), Alpha(n) <= 4, therefore O(1) and auxiliary
// space: O(1) for recursion stack
public void union(int x, int y)
{
int xRep = find(x);
int yRep = find(y);
if (xRep == yRep)
return;
if (ranks[xRep] < ranks[yRep])
parent[xRep] = yRep;
else if (ranks[yRep] < ranks[xRep])
parent[yRep] = xRep;
else
{
parent[yRep] = xRep;
ranks[xRep]++;
}
}
// Time complexity: O(LogN) and auxiliary space: O(1) for recursion stack
public int find(int x)
{
if (parent[x] == x)
{
return x;
}
return parent[x] = find(parent[x]);
}
}
private static int findGCDArr(int arr[], int n)
{
int result = arr[0];
for (int element : arr)
{
result = gcd(result, element);
if(result == 1)
{
return 1;
}
}
return result;
}
private static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static List<Integer> findFactors(int n)
{
List<Integer> res = new ArrayList<>();
// Note that this loop runs till square root
for (int i = 1; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
// If divisors are equal, add only one
if (n / i == i)
res.add(i);
else // Otherwise add both
res.add(i);
res.add(n / i);
}
}
return res;
}
private static long binPow(int a, int b, int m)
{
if(b == 0)
{
return a % m;
}
if(b % 2 == 0)
{
long pw = binPow(a, b / 2, m);
return (1l * pw * pw % m);
}
else
{
long pw = binPow(a, (b - 1) / 2, m);
return 1l * pw * pw * a % m;
}
}
private static long mulInvMod(int x, int m)
{
return binPow(x, m - 2, m);
}
// end of fast i/o code
public static void main(String[] args)
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int k = sc.nextInt();
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");
}
}
// FastReader reader = new FastReader();
// int t = reader.nextInt();
// while(t-- > 0)
// {
// int n = reader.nextInt(), k = reader.nextInt();
// boolean pass = false;
// if(k == 1 || n % 2 == 0)
// {
// pass = true;
// }
// if(!pass)
// {
// System.out.println("NO");
// }
// else
// {
// System.out.println("YES");
// if(k == 1)
// {
// for(int r = 1; r<=n*k; r++){
// System.out.println(r);
// }
// }
// else
// {
// int od = 1, ev = 2;
// for(int r = 1; r <= n; r++)
// {
// for(int c = 1; c <= k; c++)
// {
// if(r % 2 == 0)
// {
// System.out.print(od);
// if(c < k)System.out.print(" ");
// od += 2;
// }
// else
// {
// System.out.print(ev);
// if(c < k)System.out.print(" ");
// ev += 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 | 7aaec294cee08904d502d1932229cdcc | 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.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Pupil
{
static FastReader sc = new FastReader();
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int t=sc.nextInt();
while(t>0){
int n=sc.nextInt();
int k=sc.nextInt();
if(n%2==1 && k!=1)
{
System.out.println("NO");
}
else {
// System.out.println("YES");
int ans[][]=new int[n][k];
for(int i=1;i<=n;i++)
{
int count=0;
int num=i;
while(count<k)
{
ans[i-1][count]=num;
num+=n;
count++;
}
}
// System.out.println();
if(ans[n-1][k-1]>(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(ans[i][j]+" ");
}
System.out.println();
}
}
}
t--;
}
}
// FAST I/O
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 boolean two(int n)//power of two
{
if((n&(n-1))==0)
{
return true;
}
else{
return false;
}
}
public static boolean isPrime(long n){
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int digit(int n)
{
int n1=(int)Math.floor((int)Math.log10(n)) + 1;
return n1;
}
public static long gcd(long a,long b) {
if(b==0)
return a;
return gcd(b,a%b);
}
}
//CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED //
// PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function******
// pq.add(1,2)///////
// class pair implements Comparable<pair> {
// int value, index;
// pair(int v, int i) { index = i; value = v; }
// @Override
// public int compareTo(pair o) { return o.value - value; }
// }
// User defined Pair class
// class Pair {
// int x;
// int y;
// // Constructor
// public Pair(int x, int y)
// {
// this.x = x;
// this.y = y;
// }
// }
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override public int compare(Pair p1, Pair p2)
// {
// return p1.x - p2.x;
// }
// });
// class Pair {
// int height, id;
//
// public Pair(int i, int s) {
// this.height = s;
// this.id = i;
// }
// }
//Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1]));
// ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>();
// for(int i = 0; i<n;i++)
// connections.add(new ArrayList<Integer>()); | 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 | c74d6786df3877a32a04da18f864ae19 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import static java.lang.System.out;
/**
* @author shardul_rajhans
*/
public class Main {
/**
* FastReader class to read input from command line.
*/
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;
}
List<Long> readList(int n) {
List<Long> arrayList = new ArrayList<>();
for (int i = 0; i < n; i++)
arrayList.add(reader.nextLong());
return arrayList;
}
long[] readArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = reader.nextLong();
return array;
}
}
/**
*
*/
private static final FastReader reader = new FastReader();
/**
* @param args Object of String
*/
public static void main(String[] args) {
int t = reader.nextInt();
for (int i = 0; i < t; i++) {
executeCases();
}
}
/**
*
*/
private static void executeCases() {
int N = reader.nextInt();
int K = reader.nextInt();
int[][] output = new int[N][K];
for(int j=0;j<K;j++) {
for(int i=0; i<N;i++) {
output[i][j] = (i+1) + N*j;
}
}
if(K>=2 && output[0][1]%2 ==0) {
out.println("NO");
return;
}
out.println("YES");
for(int i=0;i<N;i++) {
for(int j=0;j<K;j++)
out.print(output[i][j] + " ");
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 | ba9f574ef9b8ce2d582de1963647e212 | 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 javafx.util.*;
public class Main
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
static ArrayList<Integer> g[];
//static ArrayList<ArrayList<TASK>> t;
static long mod=(long)(1e9+7);
static boolean set[],post[][];
static int prime[],c[];
static int par[];
// static int dp[][];
static HashMap<String,Long> mp;
static long max=1;
static boolean temp[][];
static int K=0;
static int size[],dp[][],iv_A[],iv_B[];
static int b1[] = new int[200002];
static int b2[] = new int[200002];
static int B1 = 0;
static int B2 = 0;
public static void main(String args[])throws IOException
{
/*
* star,rope,TPST
* BS,LST,MS,MQ
*/
int T=i();
while(T-->0)
{
int n = i();
int k = i();
int total = n*k;
if(n%2 == 0 || k == 1){
System.out.println("YES");
int j = 1;
for(int i = 1; i <= total;i += 2){
System.out.print(i + " ");
if(k == j){
System.out.println();
j = 0;
}
j++;
}
j = 1;
for(int i = 2; i <= total;i += 2){
System.out.print(i + " ");
if(k == j){
System.out.println();
j = 0;
}
j++;
}
}else{
System.out.println("NO");
}
}
}
static int getmax(ArrayList<Integer> arr){
int n = arr.size();
int max = Integer.MIN_VALUE;
for(int i = 0;i < n;i++){
max = Math.max(max,arr.get(i));
}
return max;
}
static boolean check(ArrayList<Integer> arr){
int n = arr.size();
boolean flag = true;
for(int i = 0;i < n-1;i++){
if(arr.get(i) != arr.get(i+1)){
flag = false;
break;
}
}
return flag;
}
static int MinimumFlipsOdd(String s, int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = (s.charAt(i) == '1' ? 1 : 0);
}
// Initialize prefix arrays to store
// number of changes required to put
// 1s at either even or odd position
int[] oddone = new int[n + 1];
int[] evenone = new int[n + 1];
oddone[0] = 0;
evenone[0] = 0;
for (int i = 0; i < n; i++) {
// If i is odd
if (i % 2 != 0) {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 1 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 0 ? 1 : 0);
}
// Else i is even
else {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 0 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 1 ? 1 : 0);
}
}
// Initialize minimum flips
return oddone[n];
}
static int MinimumFlipsEven(String s, int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = (s.charAt(i) == '1' ? 1 : 0);
}
// Initialize prefix arrays to store
// number of changes required to put
// 1s at either even or odd position
int[] oddone = new int[n + 1];
int[] evenone = new int[n + 1];
oddone[0] = 0;
evenone[0] = 0;
for (int i = 0; i < n; i++) {
// If i is odd
if (i % 2 != 0) {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 1 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 0 ? 1 : 0);
}
// Else i is even
else {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 0 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 1 ? 1 : 0);
}
}
// Initialize minimum flips
return evenone[n];
}
static int nextPowerOf2(int n)
{
int count = 0;
// First n in the below
// condition is for the
// case where n is 0
if (n > 0 && (n & (n - 1)) == 0){
while(n != 1)
{
n >>= 1;
count += 1;
}
return count;
}else{
while(n != 0)
{
n >>= 1;
count += 1;
}
return count;
}
}
static int length(int n){
int count = 0;
while(n > 0){
n = n/10;
count++;
}
return count;
}
static boolean isPrimeInt(int N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
public static int lcs(int[] nums) {
int[] tails = new int[nums.length];
int size = 0;
for (int x : nums) {
int i = 0, j = size;
while (i != j) {
int m = (i + j) / 2;
if (tails[m] <= x)
i = m + 1;
else
j = m;
}
tails[i] = x;
if (i == size) ++size;
}
return size;
}
static int CeilIndex(int A[], int l, int r, int key)
{
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key)
r = m;
else
l = m;
}
return r;
}
static int f(int A[], int size)
{
// Add boundary case, when array size is one
int[] tailTable = new int[size];
int len; // always points empty slot
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] < tailTable[0])
// new smallest value
tailTable[0] = A[i];
else if (A[i] > tailTable[len - 1])
// A[i] wants to extend largest subsequence
tailTable[len++] = A[i];
else
// A[i] wants to be current end candidate of an existing
// subsequence. It will replace ceil value in tailTable
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
static int containsBoth(char X[],int N)
{
for(int i=1; i<N; i++)if(X[i]!=X[i-1])return i-1;
return -1;
}
static void f(char X[],int N,int A[])
{
int c=0;
for(int i=N-1; i>=0; i--)
{
if(X[i]=='1')
{
A[i]=c;
}
else c++;
A[i]+=A[i+1];
}
}
static int f(int i,int j,char X[],char Y[],int zero)
{
if(i==X.length && j==Y.length)return 0;
if(i==X.length)return iv_B[j]; //return inversion count here
if(j==Y.length)return iv_A[i];
if(dp[i][j]==-1)
{
int cost_x=0,cost_y=0;
if(X[i]=='1')
{
cost_x=zero+f(i+1,j,X,Y,zero);
}
else cost_x=f(i+1,j,X,Y,zero-1);
if(Y[j]=='1')
{
cost_y=zero+f(i,j+1,X,Y,zero);
}
else cost_y=f(i,j+1,X,Y,zero-1);
dp[i][j]=Math.min(cost_x, cost_y);
}
return dp[i][j];
}
static boolean f(long last,long next,long l,long r,long A,long B)
{
while(l<=r)
{
long m=(l+r)/2;
long s=((m)*(m-1))/2;
long l1=(A*m)+s,r1=(m*B)-s;
if(Math.min(next, r1)<Math.max(last, l1))
{
if(l1>last)r=m-1;
else l=m+1;
}
else return true;
}
return false;
}
static boolean isVowel(char x)
{
if(x=='a' || x=='e' || x=='i' ||x=='u' || x=='o')return true;
return false;
}
static boolean f(int i,int j)
{
//i is no of one
//j is no of ai>1
if(i==0 && j==0)return true; //this player has no pile to pick --> last move iska rha hoga
if(dp[i][j]==-1)
{
boolean f=false;
if(i>0)
{
if(!f(i-1,j))f=true;
}
if(j>0)
{
if(!f(i,j-1) && !f(i+1,j-1))f=true;
}
if(f)dp[i][j]=1;
else dp[i][j]=0;
}
return dp[i][j]==1;
}
static int last=-1;
static void dfs(int n,int p)
{
last=n;
System.out.println("n--> "+n+" p--> "+p);
for(int c:g[n])
{
if(c!=p)dfs(c,n);
}
}
static long abs(long a,long b)
{
return Math.abs(a-b);
}
static int lower(long A[],long x)
{
int l=0,r=A.length;
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<=x)l=m;
else r=m;
}
return l;
}
static int f(int i,int s,int j,int N,int A[],HashMap<Integer,Integer> mp)
{
if(i==N)
{
return s;
}
if(dp[i][j]==-1)
{
if(mp.containsKey(A[i]))
{
int f=mp.get(A[i]);
int c=0;
if(f==1)c++;
mp.put(A[i], f+1);
HashMap<Integer,Integer> temp=new HashMap<>();
temp.put(A[i],1);
return dp[i][j]=Math.min(f(i+1,s+1+c,j,N,A,mp), s+K+f(i+1,0,i,N,A,temp));
}
else
{
mp.put(A[i],1);
return dp[i][j]=f(i+1,s,j,N,A,mp);
}
}
return dp[i][j];
}
static boolean inRange(int a,int l,int r)
{
if(l<=a && r>=a)return true;
return false;
}
static long f(long a,long b)
{
if(a%b==0)return a/b;
else return (a/b)+f(b,a%b);
}
static void f(int index,long A[],int i,long xor)
{
if(index+1==A.length)
{
if(valid(xor^A[index],i))
{
xor=xor^A[index];
max=Math.max(max, i);
}
return;
}
if(dp[index][i]==0)
{
dp[index][i]=1;
if(valid(xor^A[index],i))
{
f(index+1,A,i+1,0);
f(index+1,A,i,xor^A[index]);
}
else
{
f(index+1,A,i,xor^A[index]);
}
}
}
static boolean valid(long xor,int i)
{
if(xor==0)return true;
while(xor%2==0 )
{
xor/=2;
i--;
}
return i<=0;
}
static int next(int i,long pre[],long S)
{
int l=i,r=pre.length;
while(r-l>1)
{
int m=(l+r)/2;
if(pre[m]-pre[i-1]>S)r=m;
else l=m;
}
return r;
}
static boolean lexo(long A[],long B[])
{
for(int i=0; i<A.length; i++)
{
if(B[i]>A[i])return true;
if(A[i]>B[i])return false;
}
return false;
}
static long [] f(long A[],long B[],int j)
{
int N=A.length;
long X[]=new long[N];
for(int i=0; i<N; i++)
{
X[i]=(B[j]+A[i])%N;
j++;
j%=N;
}
return X;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
b=find(b);
a=find(a);
if(a!=b)
{
par[b]=a;
}
}
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static long lower_Bound(long A[],int low,int high, long x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void setGraph(int N)
{
size=new int[N+1];
// D=new int[N+1];
g=new ArrayList[N+1];
for(int i=0; i<=N; i++)
{
g[i]=new ArrayList<>();
}
}
static long pow(long a,long b)
{
long pow=1L;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(int x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static 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 boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
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;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
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 | 2ed4a93b3d3d310c45d00651b9030dde | 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.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
FastReader s=new FastReader();
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("YES");
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
System.out.print(i+(j-1)*n+" ");
}
System.out.println("");
}
}else{
System.out.println("NO");
}
}
t--;
}
}
public static boolean isPalindrome(String s){
int n=s.length();
int i=0,j=n-1;
while(i<=j){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
i++;
j--;
}
return true;
}
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 | af25745a7c32ff8b28542303f9c2aaab | 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 Codeforces770{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static boolean isPalindrome(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 first(){
int n = sc.nextInt();
int k = sc.nextInt();
String str = sc.nextLine();
if(k==0 || k==1){
out.println(1);
return;
}
if(isPalindrome(str)){
out.println(1);
}else{
out.println(2);
}
}
static void second(){
int n = sc.nextInt();
long x = sc.nextLong();
long y = sc.nextLong();
int arr[] = sc.readIntArray(n);
}
static void third(){
int n = sc.nextInt();
int k = sc.nextInt();
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 j = 1;
int l = 2;
boolean flag = true;
for(int i = 1;i<=n;i++){
if(flag){
for(int h = 1;h<=k;j+=2,h++){
out.print(j+" ");
}
out.println();
}
if(!flag){
for(int h = 1;h<=k;l+=2,h++){
out.print(l+" ");
}
out.println();
}
flag = !flag;
}
}
static boolean isprime(int n){
if(n==1 || n==2) return false;
if(n==3) 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 freq;
Pair(int v,int f){
val = v;
freq = f;
}
public int compareTo(Pair p){
return this.freq - p.freq;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while(t-- >0){
// solve();
// first();
// second();
third();
}
// 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[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
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 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 | 3af1d8ef480e0ab6b3f8dcf9e9a80969 | 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.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution {
static PrintWriter pw;
static FastScanner s;
public static void main(String[] args) throws Exception {
pw=new PrintWriter(System.out);
s=new FastScanner(System.in);
int t=s.nextInt();
while(t-->0) {
int n=s.nextInt();
int k=s.nextInt();
if(k==1) {
pw.println("YES");
for(int i=1;i<=n*k;i++)
pw.println(i);
}
else if(n*k%2==1 || ((n*k)/2)%k!=0)
pw.println("NO");
else {
pw.println("YES");
int odd=1;
int even=2;
int count=0;
while(odd<=n*k) {
if(count<k) {
pw.print(odd +" ");
odd+=2;
count++;
}
else {
count=0;
pw.println();
}
}
while(even<=n*k) {
if(count<k) {
pw.print(even +" ");
even+=2;
count++;
}
else {
count=0;
pw.println();
}
}
pw.println();
}
}
pw.flush();
}
}
class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public FastScanner(String s) throws Exception {
br = new BufferedReader(new FileReader(new File(s)));
}
public String next() throws Exception {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
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 | edbb78f8dcbb1cff672cf74bb8c89294 | 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.util.StringTokenizer;
public class OKEA
{
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;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static PrintWriter out;
public static void main (String[] args) throws java.lang.Exception
{
out = new PrintWriter(System.out);
try
{
//Scanner obj = new Scanner (System.in);
Reader obj = new Reader ();
int t = obj.nextInt();
while (t > 0)
{
t--;
int n = obj.nextInt();
int k = obj.nextInt();
//String str = obj.next();
if (n == 1 && k == 1)
{
out.println ("YES");
out.println ("1");
continue;
}
if ((n == 1) || (k != 1 && (n * k) % 2 != 0))
{
out.println ("NO");
continue;
}
if ((n * k / 2) % k != 0)
{
out.println ("NO");
continue;
}
int i, j;
out.println ("YES");
if (k == 1)
{
for (i=0;i<n;i++)
{
out.println (i+1);
}
continue;
}
int odd = 1, even = 2;
for (i=0;i<n;i++)
{
for (j=0;j<k;j++)
{
if (odd <= (n * k - 1))
{
out.print (odd + " ");
odd += 2;
}
else
{
out.print (even + " ");
even += 2;
}
}
out.println ();
}
}
}catch(Exception e){
return;
}
out.flush();
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 | 53f7d91a7881ba39e237fe64bb38fb61 | 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 general {
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();
if(n % 2 != 0 && k > 1)
System.out.println("NO");
else {
System.out.println("YES");
int n_Elements = n * k;
if(k == 1) {
for(int i = 1;i <= n_Elements;i++)
System.out.println(i);
}
else {
int row1 = 1;
int row2 = 2;
for(int i = 0;i < n / 2;i++) {
for(int j = 0;j < k;j++) {
System.out.print(row1+" ");
row1 += 2;
}
System.out.println();
for(int j = 0;j < k;j++) {
System.out.print(row2+" ");
row2 += 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 | da353b728243de0befb13b7e063e7f63 | 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 {
public static void main(String[] args) throws IOException {
Reader sc = new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt(), k = sc.nextInt();
if(n % 2 != 0 && k > 1)
out.println("NO");
else {
out.println("YES");
int n_Elements = n * k;
if(k == 1) {
for(int i = 1;i <= n_Elements;i++)
out.println(i);
}
else {
int row1 = 1;
int row2 = 2;
for(int i = 0;i < n / 2;i++) {
for(int j = 0;j < k;j++) {
out.print(row1+" ");
row1 += 2;
}
out.println();
for(int j = 0;j < k;j++) {
out.print(row2+" ");
row2 += 2;
}
out.println();
}
}
}
out.flush();
}
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if(st.hasMoreTokens())
str = st.nextToken("\n");
else
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
// ********************* GCD Function *********************
//int gcd(int a, int b){
// if (b == 0)
// return a;
// return gcd(b, a % b);
//}
// ********************* Prime Number Function *********************
//int isPrime(int n){
// if(n < 2)
// return 0;
// if(n < 4)
// return 1;
// if(n % 2 == 0 or n % 3 == 0)
// return 0;
// for(int i = 5; i*i <= n; i += 6)
// if(n % i == 0 or n % (i+2) == 0)
// return 0;
// return 1;
//}
// ********************* Custom Pair Class *********************
//class Pair implements Comparable<Pair> {
// int a,b;
// public Pair(int a,int b) {
// this.a = a;
// this.b = b;
// }
// @Override
// public int compareTo(Pair other) {
// if(this.b == other.b)
// return Integer.compare(this.a,other.a);
// return Integer.compare(this.b,other.b);
// }
//}
// ****************** Segment Tree ******************
//public class SegmentTreeNode {
// public SegmentTreeNode left;
// public SegmentTreeNode right;
// public int Start;
// public int End;
// public int Sum;
// public SegmentTreeNode(int start, int end) {
// Start = start;
// End = end;
// Sum = 0;
// }
//}
//public SegmentTreeNode buildTree(int start, int end) {
// if(start > end)
// return null;
// SegmentTreeNode node = new SegmentTreeNode(start, end);
// if(start == end)
// return node;
// int mid = start + (end - start) / 2;
// node.left = buildTree(start, mid);
// node.right = buildTree(mid + 1, end);
// return node;
//}
//public void update(SegmentTreeNode node, int index) {
// if(node == null)
// return;
// if(node.Start == index && node.End == index) {
// node.Sum += 1;
// return;
// }
// int mid = node.Start + (node.End - node.Start) / 2;
// if(index <= mid)
// update(node.left, index);
// else
// update(node.right, index);
// node.Sum = node.left.Sum + node.right.Sum;
//}
//public int SumRange(SegmentTreeNode root, int start, int end) {
// if(root == null || start > end)
// return 0;
// if(root.Start == start && root.End == end)
// return root.Sum;
// int mid = root.Start + (root.End - root.Start) / 2;
// if(end <= mid)
// return SumRange(root.left, start, end);
// else if(start > mid)
// return SumRange(root.right, start, end);
// return SumRange(root.left, start, mid) + SumRange(root.right, mid + 1, end);
//} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 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 | 09c82437c01f10e65fab1f8a9fa7a94c | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt(), k = sc.nextInt();
int[][] ans = new int[n][k];
int even = 2;
int odd = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if ((i+1)%2 == 1) {
ans[i][j] = odd;
odd += 2;
} else {
ans[i][j] = even;
even += 2;
}
}
}
boolean valid = true;
for (int[] x : ans) for (int y : x) valid &= (y >= 1 && y <= n*k);
if (valid) {
out.println("YES");
for (int[] x : ans) {
for (int y : x) out.print(y + " ");
out.println();
}
} else out.println("NO");
}
out.close();
}
static class Pair implements Comparable<Pair> {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair p) {
if (first != p.first) return Integer.compare(first, p.first);
else if (second != p.second) return Integer.compare(second, p.second);
else return 0;
}
}
static final Random random = new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int r = random.nextInt(n), temp = a[r];
a[r] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-') {
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else {
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
int[] readArray(int n) throws IOException {
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 | f8572c240c226a50c8ea88fbac44f113 | 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();
int k=sc.nextInt();
if(k>1&&n%2==1){
System.out.println("NO");continue;
}
System.out.println("YES");
if(k==1){
for(int i=1;i<=n;i++){
System.out.println(i);
}
}else{
for(int i=1;i<=n*k;i+=2*k){
for(int j=i;j<i+2*k;j+=2){
System.out.print(j+(j+2<i+2*k?" ":"\n"));
}
for(int j=i+1;j<i+2*k;j+=2){
System.out.print(j+(j+2<i+2*k?" ":"\n"));
}
}
}
}
}
}
/*
3 4
1357
2468
9101112
*/ | 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 | 8bb7aabc360b3baa6ee91fdad439db67 | 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());
}
long[] readLongArray(int n) throws IOException{
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = readLong();
}
return array;
}
int[] readIntArray(int n) throws IOException{
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = readInt();
}
return array;
}
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 && n * k % 2 == 1) {
out.println("NO");
return;
}
if (n == 1 && k != 1) {
out.println("NO");
return;
}
if (n % 2 == 1 && k != 1) {
out.println("NO");
return;
}
out.println("YES");
boolean oddRow = true;
int startOdd = 1;
int startEven = 2;
for (int i = 0; i < n; i++) {
if (oddRow) {
for (int j = 0; j < k; j++) {
out.print(startOdd + " ");
startOdd += 2;
}
oddRow = false;
} else {
for (int j = 0; j < k; j++) {
out.print(startEven + " ");
startEven += 2;
}
oddRow = true;
}
out.println();
}
}
void solve() throws IOException {
int testCases = readInt();
for (int tests = 0; tests < testCases; tests++) {
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 | 67ae7fff472593aecd59464106507bdb | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class Solution{
public static void main(String args[]){
FastReader sc = new FastReader();
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
for(int test = 0; test < t; test++){
int n = sc.nextInt();
int k = sc.nextInt();
if(k == 1){
sb.append("YES").append("\n");
for(int j = 1; j <= n; j++){
sb.append(j).append("\n");
}
continue;
}
if(n % 2 == 1){
sb.append("NO").append("\n");
continue;
}else{
sb.append("YES").append("\n");
for(int i = 1; i <= n; i++){
sb.append(i);
int temp = n;
for(int j = 0; j < k - 1; j++){
sb.append(" ").append(i + temp);
temp+=n;
}
sb.append("\n");
}
}
}
System.out.println(sb);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| 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 | 2b5238d65e52ce8fe473a310f57c851c | 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.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Ashutosh Patel (ashutoshpatelnoida@gmail.com) Linkedin : ( https://www.linkedin.com/in/ashutosh-patel-7954651ab/ )
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
COKEA solver = new COKEA();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class COKEA {
public void solve(int testNumber, InputReader sc, OutputWriter out) {
int numb = sc.readInt(), kok = sc.readInt();
int[][] arr = new int[numb][kok];
int limit = numb * kok;
int oddd = 1, ev = 2;
for (int i = 0; i < numb; i++) {
for (int j = 0; j < kok; j++) {
if (i % 2 == 0) {
arr[i][j] = oddd;
oddd += 2;
} else {
arr[i][j] = ev;
ev += 2;
}
if (arr[i][j] > limit) {
out.printLine("NO");
return;
}
}
}
out.printLine("YES");
for (int[] ints : arr) {
out.printLine(ints);
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
} | 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 | 98d695b61e4d8f657c12768ecd004742 | 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 Cf {
public static void main(String[] args ) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n, k;
n = sc.nextInt();
k = sc.nextInt();
int boom = 1;
if (k == 1) {
System.out.println("YES");
for (int a = 1; a <= n; a++) {
System.out.println(a);
}
}
else if (n % 2 == 0) {
System.out.println("YES");
for (int a = 0; a < n / 2; a++) {
for (int b = 0; b < k; b++) {
System.out.print(boom + " ");
boom += 2;
}
System.out.println();
}
boom = 2;
for (int a = 0; a < n / 2; a++) {
for (int b = 0; b < k; b++) {
System.out.print(boom + " ");
boom += 2;
}
System.out.println();
}
} else System.out.println("NO");
}
}
}
// 4 2 1 3 5 6 | 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 | b556ab585c3dc62c4dbb6dc08b45acf3 | 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 javax.management.Query;
import java.io.*;
public class Main {
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();
boolean flag=false;
int c=2;
if(k==1) {
pw.println("YES");
for (int i = 1; i <= n; i++) {
pw.println(i);
}
}
else if(n%2==0) {
pw.println("YES");
for (int i = 0; i < n/2; i++) {
for (int j = 0; j < k; j++) {
pw.print(c+" ");
c+=2;
}
pw.println();
}
c=1;
for (int i = 0; i < n/2; i++) {
for (int j = 0; j < k; j++) {
pw.print(c+" ");
c+=2;
}
pw.println();
}
}
else {
pw.println("NO");
}
}
pw.close();
}
// static int[] memo;
// public static int dp(int y,int x){
// if (x==0)
// return 0;
// if(x<0)
// return x;
// return Math.max(dp(,y-),dp());
//
// if(memo[x]!=-1)return memo[x];
// int ret = (int)1e9;
//
// for (int i: a){
//
// ret = Math.max(dp(), db(x-i)+1);
//
// }
// return memo[x]=ret;
// }
public static boolean palind(String s) {
for (int i = 0; i < s.length()/2; i++) {
if(s.charAt(i)!=s.charAt(s.length()-i-1))
return false;
}
return true;
}
public static int max4(int a,int b, int c,int d) {
int [] s= {a,b,c,d};
Arrays.sort(s);
return s[3];
}
public static double logbase2(int k) {
return( (Math.log(k)+0.0)/Math.log(2));
}
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();
}
}
static class pair implements Comparable<pair> {
long x;
long 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 Long(x).hashCode() * 31 + new Long(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 tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
} | Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 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 | 4dbc9e370d0f6493f510172711359696 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.util.*;
import java.io.*;
public class Okea
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
while(test>0)
{
int n=sc.nextInt();
int k=sc.nextInt();
if(k==1)
{
System.out.println("YES");
for(int i=1; i<=n; i++)
{
System.out.println(i);
}
}
else if(n%2==1)
System.out.println("NO");
else
{
System.out.println("YES");
int count=1;
for(int i=1; i<=n/2; i++)
{
for(int j=1; j<=k; j++)
{
System.out.print(count+" ");
count=count+2;
}
System.out.println();
}
count=2;
for(int i=1; i<=n/2; i++)
{
for(int j=1; j<=k; j++)
{
System.out.print(count+" ");
count=count+2;
}
System.out.println();
}
}
test--;
}
}
} | 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 | 7052f2ebf08ff1a34a1d83b318174411 | 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 Solution {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int n, k;
int T = read.nextInt();
while (T -- > 0) {
n = read.nextInt();
k = read.nextInt();
if (k == 1) {
System.out.println("YES");
for (int i = 1; i <= n * k; i ++)
System.out.println(i);
} else if (n % 2 == 0) {
System.out.println("YES");
for (int i = 1, j = 0; i <= n * k; i += 2)
System.out.print(i + (++ j % k == 0 ? "\n" : " "));
for (int i = 2, j = 0; i <= n * k; i += 2)
System.out.print(i + (++ j % k == 0 ? "\n" : " "));
} else System.out.println("NO");
}
read.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 | d427fd09ad16640909372cf3a0836518 | 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 practice{
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
int t = sca.nextInt();
while (t-- > 0) {
int n = sca.nextInt();
int k = sca.nextInt();
if (n % 2 == 1 && k > 1) {
System.out.println("NO");
continue;
}
System.out.println("YES");
int t1 = 1, t2 = 2;
for (int i = 0; i < n; i++) {
int all = k;
if ((i & 1) == 1) {
while (all -- > 0) {
System.out.print(t2 + " ");
t2 += 2;
}
} else {
while(all -- > 0) {
System.out.print(t1 + " ");
t1 += 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 | fca61f36c8090bffb24d443c2891ab99 | 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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
*
* @author pttrung
*/
public class C_Round_770_Div2 {
public static int MOD = 998244353;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int T = in.nextInt();
for (int z = 0; z < T; z++) {
int n = in.nextInt();
int k = in.nextInt();
if (z == 28) {
// System.out.println(n + " " + k);
}
if (k == 1) {
out.println("Yes");
for (int i = 1; i <= n * k; i++) {
out.println(i);
}
continue;
}
if (n % 2 != 0) {
out.println("No");
continue;
}
out.println("Yes");
int cur = 1;
for (int i = 0; i < n; i++) {
if (cur > n * k) {
cur = 2;
}
for (int j = 0; j < k; j++) {
out.print(cur + " ");
cur += 2;
}
out.println();
}
}
out.close();
}
static int abs(int a) {
return a < 0 ? -a : a;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point {
int x;
int y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
public String toString() {
return x + " " + y;
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return (val * val);
} else {
return (val * ((val * a)));
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["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 | e8e9383d7c65eaca99e11616b2ea7d7b | 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.*;
import java.util.*;
import java.util.function.LongToIntFunction;
public class Template {
static InputReader inputReader=new InputReader(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static void solve() throws IOException
{
int n=inputReader.nextInt();
int k=inputReader.nextInt();
if (k==1)
{
out.println("YES");
for (int i=1;i<=n;i++)
{
out.println(i);
}
}
else
{
int product=n*k;
if (product%2!=0)
{
out.println("NO");
return;
}
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 ind=0;
int eind=0;
int oind=0;
int answer[][]=new int[n][k];
while (ind<n)
{
if (ind%2==0) {
for (int i = 0; i < k; i++) {
if(oind==odd.size())
{
out.println("NO");
return;
}
answer[ind][i] = odd.get(oind);
oind++;
}
}
else
{
for (int i = 0; i < k; i++)
{
if(eind==even.size())
{
out.println("NO");
return;
}
answer[ind][i] = even.get(eind);
eind++;
}
}
ind++;
}
out.println("YES");
for (int i=0;i<n;i++)
{
for (int j=0;j<k;j++)
{
out.print(answer[i][j]+" ");
}
out.println();
}
}
}
static PrintWriter out=new PrintWriter((System.out));
static void SortDec(int arr[])
{
List<Integer>list=new ArrayList<>();
for(int ele:arr) {
list.add(ele);
}
Collections.sort(list,Collections.reverseOrder());
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
static void Sort(long arr[])
{
List<Long>list=new ArrayList<>();
for(long ele:arr) {
list.add(ele);
}
Collections.sort(list);
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
public static void main(String args[])throws IOException
{
int t=inputReader.nextInt();
while (t-->0) {
solve();
}
long s = System.currentTimeMillis();
// out.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private 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);
}
}
}
| 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 | 6a896204b99e58e6ec2e7aeb557b0ad3 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class A {
public static void main(String[] args)
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-- >0)
{
int n,k;
n = sc.nextInt();
k = sc.nextInt();
int a[][] = new int[n+1][k+1];
for(int i=1;i<=n;i++)
{
int curr = i;
for(int j=1;j<=k;j++)
{
a[i][j] = curr;
curr+=n;
}
}
int curr = 0;
boolean poss = true;
for(int j=1;j<=k;j++)
{
curr += a[1][j];
if(curr%j!=0) {
System.out.println("NO");
poss = false;
break;
}
}
if(poss)
{
System.out.println("YES");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
int [] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++)
{
a[i] = Integer.parseInt(next());
}
return a;
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void debugInt(int[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugIntInt(int[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static void debugLong(long[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugLongLong(long[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static int MOD=(int)(1e9+7);
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static void debugArrayList(ArrayList<Integer> arr)
{
for(int i=0;i<arr.size();i++){
System.out.print(arr.get(i)+" ");
}
System.out.println();
}
static void 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] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
//if (prime[i] == true)
//add it to HashSet
}
}
static boolean isPalindrome(String s)
{
for(int i = 0,j=s.length()-1;i<j;i++,j--)
{
if(s.charAt(i)!=s.charAt(j))
return false;
}
return true;
}
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);
}
/*
if RTE inside for loop:
- check for increment/decrement in loop, (i++,i--)
if after custom sorting an array using lamda , numbers are messed up
- check the order in which input is taken
*/
} | 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 | 7b2fda1db81df29858691cdf683bdad9 | train_107.jsonl | 1644158100 | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.— Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!The department you work in sells $$$n \cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 \le i \le n, 1 \le j \le k$$$.Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l \le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n \cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class A {
public static void main(String[] args)
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-- >0)
{
int n,k;
n = sc.nextInt();
k = sc.nextInt();
int a[][] = new int[n+1][k+1];
for(int i=1;i<=n;i++)
{
int curr = i;
for(int j=1;j<=k;j++)
{
a[i][j] = curr;
curr+=n;
}
}
int curr = 0;
boolean poss = true;
for(int j=1;j<=k;j++)
{
curr += a[1][j];
if(curr%j!=0) {
System.out.println("NO");
poss = false;
break;
}
}
if(poss)
{
System.out.println("YES");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
int [] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++)
{
a[i] = Integer.parseInt(next());
}
return a;
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static void debugInt(int[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugIntInt(int[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static void debugLong(long[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugLongLong(long[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static int MOD=(int)(1e9+7);
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static void debugArrayList(ArrayList<Integer> arr)
{
for(int i=0;i<arr.size();i++){
System.out.print(arr.get(i)+" ");
}
System.out.println();
}
static void 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] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
//if (prime[i] == true)
//add it to HashSet
}
}
static boolean isPalindrome(String s)
{
for(int i = 0,j=s.length()-1;i<j;i++,j--)
{
if(s.charAt(i)!=s.charAt(j))
return false;
}
return true;
}
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);
}
/*
if RTE inside for loop:
- check for increment/decrement in loop, (i++,i--)
if after custom sorting an array using lamda , numbers are messed up
- check the order in which input is taken
*/
} | 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 | 2dd6170478cb74d617e9af0fa3d52a05 | 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 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(br);
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static int[][] A = null;
static int n, k, max;
public static void main(String[] args) throws Exception {
int T = nextInt();
while(T != 0) {
A = new int[505][505];
n = nextInt(); k = nextInt();
max = n * k;
if(n % 2 == 1 && k != 1) {
System.out.println("NO");
T--;
continue;
}
if(n >= 1 && k == 1) {
System.out.println("YES");
for(int i = 1; i <= n; i++) {
System.out.println(i);
}
T--;
continue;
}
int t = 1;
int ti = 0;
A[1][1] = 1;
for(int i = 1; i <= n; i++) {
if(i > 1) t = A[i-1][k];
if(t + 2 > max) {
ti = i;
break;
}
for(int j = 1; j <= k; j++) {
if(A[i][j] != 0) continue;
t += 2;
A[i][j] = t;
}
}
t = 2;
A[ti][1] = 2;
for(int i = ti; i <= n; i++) {
if(i > ti) t = A[i-1][k];
for(int j = 1; j <= k; j++) {
if(A[i][j] != 0) continue;
t += 2;
A[i][j] = t;
}
}
System.out.println("YES");
for(int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
System.out.print(A[i][j] + " ");
}
System.out.println();
}
T--;
}
out.flush();
}
public static int nextInt() throws Exception {
st.nextToken();
return (int) st.nval;
}
public static String nextStr() throws Exception {
st.nextToken();
return st.sval;
}
} | 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 | f245996b641e9e5a3954f98f3b049ec5 | 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 a {
public static void main(String[] args){
StringBuilder ans = new StringBuilder();
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int k = sc.nextInt();
if(k == 1){
ans.append("YES\n");
for(int i=1; i<=n; i++){
ans.append(i + "\n");
}
continue;
}
// if(k%2 == 1){
// ans.append("NO\n");
// continue;
// }
if(n%2 != 0){
ans.append("NO\n");
continue;
}
ans.append("YES\n");
for(int i=1; i<=n; i++){
for(int j=0; j<k; j++){
ans.append((i+j*n)+" ");
}
ans.append("\n");
}
}
System.out.println(ans);
}
}
class FastScanner
{
//I don't understand how this works lmao
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1) return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC) c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-') neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32) c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32) return true;
while (true) {
c = getChar();
if (c == NC) return false;
else if (c > 32) return true;
}
}
}
| Java | ["4\n\n1 1\n\n2 2\n\n3 3\n\n3 1"] | 1 second | ["YES\n1 \nYES\n1 3 \n2 4 \nNO\nYES\n1 \n2 \n3"] | null | Java 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 | c8906ef030c7e719099ff80528e1d21b | 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.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C_OKEA {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static int testCases, n, k;
static void solve() {
if (k == 1) {
out.println("YES");
out.flush();
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
return;
}
if (n % 2 == 1) {
out.println("NO");
out.flush();
return;
}
if (n == 1 && k == 1) {
out.println("YES");
out.flush();
out.println(1);
out.flush();
return;
}
if (n % 2 == 0) {
StringBuilder ans = new StringBuilder();
out.println("YES");
out.flush();
for (int i = 1, j = 1; i <= n * k; i += 2, j++) {
ans.append(i).append(" ");
if (k == j) {
ans.append("\n");
j = 0;
}
}
//ans.append("\n");
for (int i = 2, j = 1; i <= n * k; i += 2, j++) {
ans.append(i).append(" ");
if (k == j) {
ans.append("\n");
j = 0;
}
}
out.print(ans.toString());
out.flush();
}
}
public static void main(String[] amit) throws IOException {
testCases = in.nextInt();
for (int t = 0; t < testCases; t++) {
n = in.nextInt();
k = in.nextInt();
solve();
}
in.close();
}
static void merge(long a[], int left, int right, int mid) {
int n1 = mid - left + 1, n2 = right - mid;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; i++) {
L[i] = a[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
}
int i = 0, j = 0, k1 = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k1] = L[i];
i++;
} else {
a[k1] = R[j];
j++;
}
k1++;
}
while (i < n1) {
a[k1] = L[i];
i++;
k1++;
}
while (j < n2) {
a[k1] = R[j];
j++;
k1++;
}
}
static void sort(long a[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
static class Scanner {
BufferedReader in;
StringTokenizer st;
public Scanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
String nextLine() throws IOException {
return in.readLine();
}
char nextChar() throws IOException {
return next().charAt(0);
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
float nextFloat() throws IOException {
return Float.parseFloat(next());
}
boolean nextBoolean() throws IOException {
return Boolean.parseBoolean(next());
}
void close() throws IOException {
in.close();
}
}
}
/*
4
1 1
2 2
3 3
3 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 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 | 48996aa23fe13b3357432bf0d8cd4c45 | 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 cpp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt();
int k = in.nextInt();
if(k == 1) {
System.out.println("YES");
for(int i = 1; i <= n; i++) {
System.out.println(i);
}
continue;
}
if(n % 2 == 1) {
System.out.println("NO");
continue;
}
int num1 = 1;
int num2 = 2;
System.out.println("YES");
for(int i = 0; i < n; i++) {
for(int j = 0; j < k; j++) {
System.out.print(num1 + " ");
num1 += 2;
}
System.out.println();
i++;
if(i < n) {
for(int j = 0; j < k; j++) {
System.out.print(num2 + " ");
num2 += 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.