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
c8b7e28feb0621e163d6f444c51d0649
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class A { public static void main(String[] args) { FastReader scan = new FastReader(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); int t = scan.nextInt(); for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out); out.close(); } static class Task { public void solve(int testNumber, FastReader scan, PrintWriter out) { long a = scan.nextLong(), b = scan.nextLong(); if(b < a) a ^= b ^ (b = a); if(a == 0 && b == 0) out.println(0); else if(a == b) out.println(1); else if((b - a) % 2 == 0) out.println(2); else out.println(-1); } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 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
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
ea1fcf0ab092d227ab9add8a7eaa621a
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class A { public static void main(String[]args){ MyScanner sc = new MyScanner(); int n = sc.nextInt(); for(int i=0;i<n;i++){ int c = sc.nextInt(); int d = sc.nextInt(); if(c==0&&d==0){ System.out.println(0); } else if(Math.abs(c)==Math.abs(d)){ System.out.println(1); } else if(Math.abs(c-d)%2==0){ System.out.println(2); } else{ System.out.println(-1); } } } } class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
813745a098597a63cb9d7c8275382acf
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { new Main().FunctionCall(); } void FunctionCall(){ Scanner scanner=new Scanner(System.in); int t=scanner.nextInt(); while(t-->0){ int a=scanner.nextInt(); int b=scanner.nextInt(); solve(a, b); } } void solve(int a, int b){ if(b>a){ solve(b , a); return; } if(a==b && a!=0){ System.out.println(1); return; } if(a==b && a==0){ System.out.println(0); return; } int diff=a-b; if(diff%2!=0){ System.out.println(-1); return; } System.out.println(2); } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
70f610274f4851ff06dc49c3499f25fd
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for (int i=0; i<t; i++) { String[] ab = br.readLine().split(" "); int a = Integer.parseInt(ab[0]); int b = Integer.parseInt(ab[1]); if (Math.abs(a-b) % 2 == 1) { System.out.println(-1); continue; } if (a == 0 && b == 0) { System.out.println(0); continue; } if (a==b) { System.out.println(1); continue; } System.out.println(2); } } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
5328b89c9a5f0f6512f477287a7b8779
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.time.LocalTime; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class A { public static void main(String[] args) { FastScanner scan = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int T = 1; T = scan.nextInt(); for(int tt=0; tt<T; tt++) { int c = scan.nextInt(), d = scan.nextInt(); int diff = Math.abs(c - d); if(c == 0 && diff == 0) System.out.println(0); else if(diff % 2 != 0) System.out.println(-1); else { System.out.println(diff == 0 ? 1 : 2); } } } /* static class IntComparator implements Comparator<Integer> { public int compare(Integer o1, Integer o2) { // return Integer.compare(a[o1], a[o2]); return 0; } } */ public static void printIntArray(int [] a) { for(int i: a) System.out.print(i + " "); System.out.println(); } public static void swapInt(int a, int b) { int temp = a; a = b; b = temp; } public 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<a.length; i++) a[i]= b.get(i); } public static void reverse(int [] a) { ArrayList<Integer> b = new ArrayList<>(); for(int i: a) b.add(i); Collections.reverse(b); for(int i=0; i<a.length; i++) a[i]= b.get(i); } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int [] readArray(int n) { int [] a = new int[n]; for(int i=0; i<n ; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
4c89c8159c87951e5609f441fcbabfaf
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
//package lovejava; import java.io.*; import java.math.BigInteger; import java.util.*; public class LoveJava { static int sum1 = 0; static int sum2 = 0; public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); String s; String arr[]; String arr2[]; try { int t=Integer.parseInt(br.readLine()); while(t-->0) { // int n = Integer.parseInt(br.readLine()); arr = (br.readLine().replaceAll("//s+$", " ").split(" ")); /* int myScore[] = new int[arr.length]; arr2 = (br.readLine().replaceAll("//s+$", " ").split(" ")); int[] IllScore = new int[arr2.length]; */ int c=Integer.parseInt(arr[0]); int d=Integer.parseInt(arr[1]); if(((int)Math.abs(d-c)&1)==1)//odd pw.println(-1); else { if(c==d&c==0) pw.println(0); else if(c==d) pw.println(1); else pw.println(2); } } pw.close(); } catch (IOException ex) { System.out.println("input mismatch"); } } public static String balancedSums(List<Integer> arr) { // Write your code here // int pow=(int)Math.pow(10,5)+1; if(arr.size()==1) return "YES"; long[]prefixSum=new long[arr.size()]; long[]suffixSum=new long[arr.size()]; long sum=0l; for(int i=0;i<arr.size();i++){ sum+=arr.get(i); prefixSum[i]=sum; } sum=0l; for(int i=arr.size()-1;i>=0;i--){ sum+=arr.get(i); suffixSum[i]=sum; } for(int i=0;i<arr.size();i++){ if(i-1>=0&&i+1<arr.size()&&prefixSum[i-1]==suffixSum[i+1]){ return "YES"; } else if((i>=0&&i<arr.size())&&((i==0&&suffixSum[i+1]==0)||(i==arr.size()-1&&prefixSum[i-1]==0))) return "YES"; } return "NO"; } public static void solution(String[]arr){ int n = Integer.parseInt(arr[0]); int k = Integer.parseInt(arr[1]); StringBuilder stb = new StringBuilder(arr[0]); int[] array = new int[10]; Arrays.fill(array, -1); int count = 0; for (int i = 0; i < stb.length(); i++) { array[Integer.parseInt(String.valueOf(stb.charAt(i)))] = Integer.parseInt(String.valueOf(stb.charAt(i))); } for (int i = 0; i < array.length; i++) { if (array[i] != -1) count++; } if (count <= k) System.out.println(n); else { Arrays.sort(array); int i; boolean flag; for (i = 0; i < array.length; i++) if (array[i] == -1) ;//pointer at digits if (k == 1) { flag = true; while (flag) { for (int j = 0; j < stb.length(); j++) stb.setCharAt(j, (char) array[i]); if (Integer.parseInt(stb.toString()) > n) { System.out.println((Integer.parseInt(stb.toString()))); flag=false; } else i++; } } else { char newchar = '\u0000'; int l = 0; int pOfDiff; char ch = stb.charAt(0); for (int j = 1; j < stb.length(); j++) { if (stb.charAt(j) == ch) ; else { newchar = stb.charAt(j); l = j; while (j < stb.length()) { stb.setCharAt(j, newchar); } } } if (Integer.parseInt(stb.toString()) < n) { flag=true; while (flag) { for (; l < stb.length(); l++) stb.setCharAt(l, (char) array[i]); if (Integer.parseInt(stb.toString()) > n) { System.out.println((Integer.parseInt(stb.toString()))); flag = false; } else i++; } } } } } public static int fun(int[]myScore,int[]illyScore){ Arrays.sort(myScore); Arrays.sort(illyScore); int k=myScore.length-(myScore.length/4); int mySum=0; int illSum=0; for(int i=1;i<=k;i++){ mySum+=myScore[myScore.length-i]; } for(int i=1;i<=k;i++){ illSum+=illyScore[illyScore.length-i]; } if(mySum>=illSum) return 0; else { int l=(int) Math.ceil((illSum-mySum)/(double)100); int h=(int)Math.pow(10,5)*5; while (h-l>1){ int mid=l+(h-l)/2; if(binarysearch(mid,myScore,illyScore)==true) h=mid; else l=mid+1; } if(binarysearch(l,myScore,illyScore)==true) return l-myScore.length; else return h-myScore.length; } } public static boolean binarysearch(int chosenNum,int[]myScore,int[]ilyScore){ int x=chosenNum-myScore.length; int t=chosenNum-chosenNum/4; int mySum=0; int illysum=0; if(x>=t) mySum=t*100; else { mySum = x * 100; for(int i=1;i<=t-x;i++){ mySum+= myScore[myScore.length-i]; } } if(t>=ilyScore.length){ for(int i=0;i<ilyScore.length;i++){ illysum+= ilyScore[i]; } } else { for(int i=1;i<=t;i++){ illysum+= ilyScore[ilyScore.length-i]; } } if(mySum>=illysum) return true; else return false; } public static void func2(int r,int c) { int[][] board = new int[r][c]; int[] arrR = {-1, -1, -1, 0, 1, 1, 1, 0}; int[] arrC = {-1, 0, 1, 1, 1, 0, -1, -1}; for (int[] e : board) { for (int i : e) { i = 0; } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (i == 0 || i == r - 1 || j == 0 || j == c - 1) { if (boolFun(board, arrR, arrC, i, j, r, c) == false) board[i][j] = 1; } } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { System.out.print(board[i][j]); } System.out.println(); } System.out.println(); } public static boolean boolFun(int[][] board,int[]r,int[]c,int i,int j,int rmax,int cmax){ for(int k=0;k<r.length;k++){ if(i+r[k]<rmax&&i+r[k]>=0&&j+c[k]<cmax&&j+c[k]>=0)//row if(board[i+r[k]][j+c[k]]==1) return true; } return false; } public static int func(int n){ if(n==0) return 0; StringBuilder stb=new StringBuilder(String.valueOf(n)); for(int i=0;i<stb.length();i++){ if(stb.charAt(i)=='0'||stb.charAt(i)=='1') ; else { while (i<stb.length()){ stb.setCharAt(i,'1'); i++; } } } return 1+func(n-Integer.parseInt(stb.toString())); } } /* class Solution { public int maxSubArray(int[] nums) { } } */ class Rectangle2{ int len=1; int bre=1; Rectangle2(int len,int bre){ this.len=len; this.bre=bre; } } class Solution { public long nthStair(int n) { // Code here long[] dp = new long[n + 1]; Arrays.fill(dp, 0l); dp[0] = 1l; for (int i = 1; i <= n; i++) { dp[i] += dp[i - 1]; } for (int i = 2; i <= n; i++) { dp[i] += dp[i - 2]; } return dp[n]; } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
fa45e631fa0a85b37cd4c72b929b4b9a
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.util.*; import java.io.*; import java.util.Arrays; public class codeforces { static long[]fac=new long[200100]; static long[] two= new long[200100] ; static long mod=((int)1e9)+9; static String[]pow=new String[63]; static int[][]perm; static int[]a,pe; static int x=0; static int n,r; public static void main(String[] args) throws IOException{ int t=sc.nextInt(); while(t-->0) { int c=sc.nextInt(); int d=sc.nextInt(); int diff=Math.abs(c-d); if(diff%2!=0)pw.println(-1); else { if(diff==0&&c==0)pw.println(0); else if(diff==0)pw.println(1); else { pw.println(2); } } } pw.close(); } public static boolean reachable(double x1,double y1,double x2,double y2) { double m=(y2-y1)/(x2-x1); double c=y1-x1*m; double a=m*m+1; double b=2*m*c; c=c*c-r*r; double sol1=(-b+Math.sqrt(b*b-4*a*c))/(2*a); double sol2=(-b-Math.sqrt(b*b-4*a*c))/(2*a); return Math.abs(x2-x1)-Math.min(Math.abs(sol1-x1), Math.abs(sol2-x1))<0.00000001; } public static boolean isplus1(int n) { return (n-1)%6==0; } public static void permutation(int idx,int v) { if(v==(1<<n)-1) { perm[x++]=pe.clone(); return ; } for (int i = 0; i < n; i++) { if((v&1<<i)==0) { pe[idx]=a[i]; permutation(idx+1, v|1<<i); } } return ; } public static void pre2() { for (int i = 0; i < pow.length; i++) { long x=1l<<i; pow[i]=x+""; } } public static void sort(int[]a) { mergesort(a, 0, a.length-1); } public static void sortIdx(long[]a,long[]idx) { mergesortidx(a, idx, 0, a.length-1); } public static long C(int a,int b) { long x=fac[a]; long y=fac[a-b]*fac[b]; return x*pow2(y,mod-2)%mod; } public static long pow2(long a,long b) { long ans=1;a%=mod; for(long i=b;i>0;i/=2) { if((i&1)!=0) ans=ans*a%mod; a=a*a%mod; } return ans; } public static void pre(){ fac[0]=1; for (int i = 1; i < fac.length; i++) { fac[i]=fac[i-1]*i%mod; } two[0]=1; for (int i = 1; i < fac.length; i++) { two[i]=two[i-1]*2%mod; } } static class Node{ int x; Node next; Node previous; public Node(int x) { this.x=x; next=null; previous=null; } @Override public String toString() { return x+""; } } static class LinkedL{ Node head; Node last; public LinkedL() { head=last=null; } public void insertLast(int k) { Node n=new Node(k); n.previous=last; last.next=n; last=n; } public void insertFirst(int k) { if(head==null) { head=last=new Node(k); }else { Node n=new Node(k); n.next=head; head.previous=n; head=n; } } @Override public String toString() { String s=""; Node c=head; while(c!=null) { s+=c+" "; c=c.next; } return s; } } public static long p(int n,int k) { long res=1; if(k<=n/2) { while(k-->0) { res*=n; n--; } }else { res=fac(n)/fac(n-k); } return res; } public static long eval(String s) { long p=1; long res=0; for (int i = 0; i < s.length(); i++) { res+=p*(s.charAt(s.length()-1-i)=='1'?1:0); p*=2; } return res; } public static String binary(long x) { String s=""; while(x!=0) { s=(x%2)+s; x/=2; } return s; } public static boolean allSame(String s) { char x=s.charAt(0); for (int i = 0; i < s.length(); i++) { if(s.charAt(i)!=x)return false; } return true; } public static boolean isPalindrom(String s) { int l=0; int r=s.length()-1; while(l<r) { if(s.charAt(r--)!=s.charAt(l++))return false; } return true; } public static String putAtFront(String s,char c) { if(s.length()==0)return s; if(s.charAt(s.length()-1)==c) { return s.charAt(s.length()-1)+putAtFront(s.substring(0,s.length()-1), c); }else { return putAtFront(s.substring(0,s.length()-1), c)+s.charAt(s.length()-1); } } public static boolean isSubString(String s,String t) { int ls=s.length(); int lt=t.length(); boolean res=false; for (int i = 0; i <=lt-ls; i++) { if(t.substring(i, i+ls).equals(s)) { res=true; break; } } return res; } public static boolean isSorted(int[]a) { for (int i = 0; i < a.length-1; i++) { if(a[i]>a[i+1])return false; } return true; } public static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } public static long power(long x,long k) { long res=1; long mod=((int)1e9)+7; for (int i = 0; i < k; i++) { res*=x; res=res%mod; } return res; } public static int whichPower(int x) { int res=0; for (int j = 0; j < 31; j++) { if((1<<j&x)!=0) { res=j; break; } } return res; } public static long evaln(String x,int n) { long res=0; for (int i = 0; i < x.length(); i++) { res+=Long.parseLong(x.charAt(x.length()-1-i)+"")*Math.pow(n, i); } return res; } static void merge(int[] arr,int b,int m,int e) { int len1=m-b+1,len2=e-m; int[] l=new int[len1]; int[] r=new int[len2]; for(int i=0;i<len1;i++)l[i]=arr[b+i]; for(int i=0;i<len2;i++)r[i]=arr[m+1+i]; int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<r[j])arr[k++]=l[i++]; else arr[k++]=r[j++]; } while(i<len1)arr[k++]=l[i++]; while(j<len2)arr[k++]=r[j++]; return; } static void mergesortidx(long[] arr,long[]idx,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesortidx(arr,idx,b,m); mergesortidx(arr,idx,m+1,e); mergeidx(arr,idx,b,m,e); } return; } static void mergeidx(long[] arr,long[]idx,int b,int m,int e) { int len1=m-b+1,len2=e-m; long[] l=new long[len1]; long[] lidx=new long[len1]; long[] r=new long[len2]; long[] ridx=new long[len2]; for(int i=0;i<len1;i++) { l[i]=arr[b+i]; lidx[i]=idx[b+i]; } for(int i=0;i<len2;i++) { r[i]=arr[m+1+i]; ridx[i]=idx[m+1+i]; } int i=0,j=0,k=b; while(i<len1 && j<len2) { if(l[i]<r[j]) { arr[k++]=l[i++]; idx[k-1]=lidx[i-1]; } else { arr[k++]=r[j++]; idx[k-1]=ridx[j-1]; } } while(i<len1) { idx[k]=lidx[i]; arr[k++]=l[i++]; } while(j<len2) { idx[k]=ridx[j]; arr[k++]=r[j++]; } return; } static void mergesort(int[] arr,int b,int e) { if(b<e) { int m=b+(e-b)/2; mergesort(arr,b,m); mergesort(arr,m+1,e); merge(arr,b,m,e); } return; } static long mergen(int[] arr,int b,int m,int e) { int len1=m-b+1,len2=e-m; int[] l=new int[len1]; int[] r=new int[len2]; for(int i=0;i<len1;i++)l[i]=arr[b+i]; for(int i=0;i<len2;i++)r[i]=arr[m+1+i]; int i=0,j=0,k=b; long c=0; while(i<len1 && j<len2) { if(l[i]<r[j])arr[k++]=l[i++]; else { arr[k++]=r[j++]; c=c+(long)(len1-i); } } while(i<len1)arr[k++]=l[i++]; while(j<len2)arr[k++]=r[j++]; return c; } static long mergesortn(int[] arr,int b,int e) { long c=0; if(b<e) { int m=b+(e-b)/2; c=c+(long)mergesortn(arr,b,m); c=c+(long)mergesortn(arr,m+1,e); c=c+(long)mergen(arr,b,m,e); } return c; } public static long fac(int n) { if(n==0)return 1; return n*fac(n-1); } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static long summ(long x) { long sum=0; while(x!=0) { sum+=x%10; x=x/10; } return sum; } public static ArrayList<Integer> findDivisors(int n){ ArrayList<Integer>res=new ArrayList<Integer>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) res.add(i); else { res.add(i); res.add(n/i); } } } return res; } public static void sort2darray(Integer[][]a){ Arrays.sort(a,Comparator.<Integer[]>comparingInt(x -> x[0]).thenComparingInt(x -> x[1])); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int 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 { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextArrint(int size) throws IOException { int[] a=new int[size]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextInt(); } return a; } public long[] nextArrlong(int size) throws IOException { long[] a=new long[size]; for (int i = 0; i < a.length; i++) { a[i]=sc.nextLong(); } return a; } public int[][] next2dArrint(int rows,int columns) throws IOException{ int[][]a=new int[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j]=sc.nextInt(); } } return a; } public long[][] next2dArrlong(int rows,int columns) throws IOException{ long[][]a=new long[rows][columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { a[i][j]=sc.nextLong(); } } return a; } } static class Side{ Point a; Point b; public Side(Point a,Point b) { this.a=a; this.b=b; } @Override public boolean equals(Object obj) { Side s=(Side)obj; return (s.a.equals(a)&&s.b.equals(b))||(s.b.equals(a)&&s.a.equals(b)); } @Override public String toString() { return "("+a.toString()+","+b.toString()+")"; } } static class Point{ int x; int y; int z; public Point(int x,int y,int z) { this.x=x; this.y=y; this.z=z; } @Override public boolean equals(Object obj) { Point p=(Point)obj; return x==p.x&&y==p.y&&z==p.z; } @Override public String toString() { return "("+x+","+y+","+z+")"; } } static class Pair{ long x; long y; public Pair(long x,long y) { this.x=x; this.y=y; } } static Scanner sc=new Scanner(System.in); static PrintWriter pw=new PrintWriter(System.out); }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
e5601810dcbbb1eb22eed96d3ceac8ce
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
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 Practice { public static long mod = (long) Math.pow(10, 9) + 7; // public static long mod2 = 998244353; // public static int tt = 1; public static ArrayList<Long> one; public static ArrayList<Long> two; // public static long[] fac = new long[200005]; // public static long[] invfac = new long[200005]; public static void main(String[] args) throws Exception { PrintWriter pw = new PrintWriter(System.out); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); int p = 1; while (t-- > 0) { String[] s1 = br.readLine().split(" "); int c = Integer.valueOf(s1[0]); int d = Integer.valueOf(s1[1]); int x = Math.abs(c - d); if (c == 0 && d == 0) { pw.println(0); } else { if (x == 0) { pw.println(1); } else if (x % 2 == 0) { pw.println(2); } else { pw.println(-1); } } } pw.close(); } // // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // private static int kmp(String str) { // // TODO Auto-generated method stub // // System.out.println(str); // int[] pi = new int[str.length()]; // pi[0] = 0; // for (int i = 1; i < str.length(); i++) { // int j = pi[i - 1]; // while (j > 0 && str.charAt(i) != str.charAt(j)) { // j = pi[j - 1]; // } // if (str.charAt(j) == str.charAt(i)) { // j++; // } // pi[i] = j; // System.out.print(pi[i]); // } // System.out.println(); // return pi[str.length() - 1]; // } } // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } //private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // if (t1 == 0) { // return t2; // } // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } //}
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
9a4b83aa7a4f4e01145109c89310510c
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
//package codeforces; import java.util.*; import java.lang.*; import java.io.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // MAIN FUNCTION public static void main(String[] args) throws java.lang.Exception { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fr.ni(); while(t-->0) { int a = fr.ni(); int b = fr.ni(); int ans = -1; if((Math.abs(a-b)&1) == 0) { if(a == 0 && b == 0)ans = 0; else if(a == b)ans = 1; else ans = 2; } out.println(ans); } out.close(); } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
d1925c81972aad109f790d2745650cd7
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.util.*; public class Leetcode { public static void main(String[] args) { int a; int b; int c; Scanner scanner=new Scanner(System.in); c=scanner.nextInt(); while(c-->0){ a= scanner.nextInt(); b=scanner.nextInt(); if(a==b&&a==0) System.out.println(0); else if(a==b) System.out.println(1); else if(Math.abs(a-b)%2==0){ System.out.println(2); }else{ System.out.println(-1); } } } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
619e18a86784766ab080bb1d3f966556
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String args[] ) throws Exception { Scanner sc = new Scanner(System.in); try { int t = sc.nextInt(); while((t--)!=0){ int a=sc.nextInt(); int b=sc.nextInt(); if(a==b && a!=0 && b!=0) System.out.println("1"); else if(a==b && b==0) System.out.println("0"); else if((a%2==1 && b%2==0) || (a%2==0 && b%2==1)) System.out.println("-1"); else System.out.println("2"); } } catch(Exception e) { } } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
6b92a8b931df57b241eba8e406223c3b
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.*; import java.util.*; public class Sol { final static int LINE_SIZE = 128; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out), false); void solve() throws Exception { long c = in.nextInt(); long d = in.nextInt(); long dif = Math.abs(Math.abs(c) - Math.abs(d)); if (dif % 2 == 1) { out.println(-1); return; } if (c == 0 && d == 0) { out.println(0); return; } if (Math.abs(c) == Math.abs(d)) { out.println(1); return; } if (c == 0 || d == 0) { out.println(2); return; } out.println(2); } void runCases() throws Exception { int tt = 1; tt = in.nextInt(); while (tt-- > 0) { solve(); } in.close(); out.close(); } public static void main(String[] args) throws Exception { Sol solver = new Sol(); solver.runCases(); } static class Pii implements Comparable<Pii> { public int fi, se; public Pii(int fi, int se) { this.fi = fi; this.se = se; } public int compareTo(Pii other) { if (fi == other.fi) return se - other.se; return fi - other.fi; } } static class Pll implements Comparable<Pll> { public long fi, se; public Pll(long fi, long se) { this.fi = fi; this.se = se; } public int compareTo(Pll other) { if (fi == other.fi) return se < other.se ? -1 : 1; return fi < other.fi ? -1 : 1; } } static class Pair<T extends Comparable<T>, U extends Comparable<U>> implements Comparable<Pair<T, U>> { T fi; U se; public Pair(T fi, U se) { this.fi = fi; this.se = se; } public int compareTo(Pair<T, U> other) { if (fi == other.fi) return se.compareTo(other.se); return fi.compareTo(other.fi); } } static class FastReader { private final DataInputStream din = new DataInputStream(System.in); private final byte[] buffer = new byte[65536]; private int bufferPointer = 0, bytesRead = 0; public String readLine() throws Exception { byte[] buf = new byte[LINE_SIZE]; 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 Exception { 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'); return neg ? -ret : ret; } public long nextLong() throws Exception { 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'); return neg ? -ret : ret; } public double nextDouble() throws Exception { 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); return neg ? -ret : ret; } private void fillBuffer() throws Exception { bytesRead = din.read(buffer, bufferPointer = 0, 65536); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws Exception { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws Exception { din.close(); } } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
50083139414a674d5ae4f5723b567657
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.BigInteger; public class _practise { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] ia(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=nextInt(); return a; } int[][] ia(int n , int m) { int a[][]=new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextInt(); return a; } long[][] la(int n , int m) { long a[][]=new long[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m ;j++) a[i][j]=nextLong(); return a; } char[][] ca(int n , int m) { char a[][]=new char[n][m]; for(int i=0;i<n;i++) { String x =next(); for(int j=0;j<m ;j++) a[i][j]=x.charAt(j); } return a; } long[] la(int n) { long a[]=new long[n]; for(int i=0;i<n;i++)a[i]=nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(long[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {long oi=r.nextInt(n), temp=a[i];a[i]=a[(int)oi];a[(int)oi]=temp;}Arrays.sort(a);} static void sort(int[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);} public static long sum(long a[]) {long sum=0; for(long i : a) sum+=i; return(sum);} public static long count(long a[] , long x) {long c=0; for(long i : a) if(i==x) c++; return(c);} public static int sum(int a[]) { int sum=0; for(int i : a) sum+=i; return(sum);} public static int count(int a[] ,int x) {int c=0; for(int i : a) if(i==x) c++; return(c);} public static int count(String s ,char ch) {int c=0; char x[] = s.toCharArray(); for(char i : x) if(ch==i) c++; return(c);} public static boolean prime(int n) {for(int i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;} public static boolean prime(long n) {for(long i=2 ; i<=Math.sqrt(n) ; i++) if(n%i==0) return false; return true;} public static int gcd(int n1, int n2) { if (n2 != 0)return gcd(n2, n1 % n2); else return n1;} public static long gcd(long n1, long n2) { if (n2 != 0)return gcd(n2, n1 % n2); else return n1;} public static int[] freq(int a[], int n) { int f[]=new int[n+1]; for(int i:a) f[i]++; return f;} public static int[] pos(int a[], int n) { int f[]=new int[n+1]; for(int i=0; i<n ;i++) f[a[i]]=i; return f;} public static long mindig(long n) { long ans=9; while(n>0) { if(n%10<ans) ans=n%10; n/=10; } return ans; } public static long maxdig(long n) { long ans=0; while(n>0) { if(n%10>ans) ans=n%10; n/=10; } return ans; } public static boolean palin(String s) { StringBuilder sb = new StringBuilder(); sb.append(s); String str=String.valueOf(sb.reverse()); if(s.equals(str)) return true; else return false; } public static void main(String args[]) { FastReader in=new FastReader(); PrintWriter so = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); _practise ob = new _practise(); int T = in.nextInt(); //int T = 1; tc : while(T-->0) { long c = in.nextLong(); long d = in.nextLong(); if((c+d)%2!=0) so.println(-1); else { if(c==d) { if(c==0) so.println(0); else so.println(1); } else so.println(2); } } so.flush(); /*String s = in.next(); * Arrays.stream(f).min().getAsInt() * BigInteger f = new BigInteger("1"); 1 ke jagah koi bhi value ho skta jo aap * initial value banan chahte int a[] = new int[n]; Stack<Integer> stack = new Stack<Integer>(); Deque<Integer> q = new LinkedList<>(); or Deque<Integer> q = new ArrayDeque<Integer>(); PriorityQueue<Long> pq = new PriorityQueue<Long>(); ArrayList<Integer> al = new ArrayList<Integer>(); StringBuilder sb = new StringBuilder(); HashSet<Integer> st = new LinkedHashSet<Integer>(); Set<Integer> s = new HashSet<Integer>(); Map<Long,Integer> hm = new HashMap<Long, Integer>(); //<key,value> for(Map.Entry<Integer, Integer> i :hm.entrySet()) HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>(); so.println("HELLO"); Arrays.sort(a,Comparator.comparingDouble(o->o[0])) Arrays.sort(a, (aa, bb) -> Integer.compare(aa[1], bb[1])); Set<String> ts = new TreeSet<>();*/ } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
c30b5f694b7bb2cefda64eab5808fc83
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int test =sc.nextInt(); int a=0; int b=0; for(int i=0;i<test;i++) { int c = sc.nextInt(); int d = sc.nextInt(); a=c+d; if(c==0 && d==0) { System.out.println(0); } else if(Math.abs(c-d)%2==1) { System.out.println(-1); } else if(Math.abs(a)/2==c) { System.out.println(1); } else { System.out.println(2); } } } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
41053fdb914896fcfff27076f12d44fd
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { final static FastScanner scan = new FastScanner(); static void test() { int a = scan.nextInt(); int b = scan.nextInt(); if(Math.abs(a - b) % 2 == 1) { System.out.println(-1); } else { if(a == b) { System.out.println(a == 0 ? 0 : 1); } else System.out.println(2); } } public static void main(String args[]) { int T = 1; T = scan.nextInt(); for(int tc = 0; tc < T; ++ tc) { test(); } } } class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
b4f328af15110ed2a25a42999659db4b
train_107.jsonl
1630247700
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well.William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { FastScanner f= new FastScanner(); int ttt=1; ttt=f.nextInt(); PrintWriter out=new PrintWriter(System.out); outer: for(int tt=0;tt<ttt;tt++) { int a=f.nextInt(); int b=f.nextInt(); if(a==0 && b==0) System.out.println(0); else if (a==b) System.out.println(1); else if(Math.abs(a-b)%2==0) System.out.println(2); else System.out.println(-1); } out.close(); } static void sort(int[] p) { ArrayList<Integer> q = new ArrayList<>(); for (int i: p) q.add( i); Collections.sort(q); for (int i = 0; i < p.length; i++) p[i] = q.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["6\n1 2\n3 5\n5 3\n6 6\n8 0\n0 0"]
1 second
["-1\n2\n2\n1\n2\n0"]
NoteLet us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
Java 8
standard input
[ "math" ]
8e7c0b703155dd9b90cda706d22525c9
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 \le c, d \le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into.
800
For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations.
standard output
PASSED
862b2dc4f8fe28ca3ea84b07cadd7942
train_107.jsonl
1630247700
William really likes puzzle kits. For one of his birthdays, his friends gifted him a complete undirected edge-weighted graph consisting of $$$n$$$ vertices.He wants to build a spanning tree of this graph, such that for the first $$$k$$$ vertices the following condition is satisfied: the degree of a vertex with index $$$i$$$ does not exceed $$$d_i$$$. Vertices from $$$k + 1$$$ to $$$n$$$ may have any degree.William wants you to find the minimum weight of a spanning tree that satisfies all the conditions.A spanning tree is a subset of edges of a graph that forms a tree on all $$$n$$$ vertices of the graph. The weight of a spanning tree is defined as the sum of weights of all the edges included in a spanning tree.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.ArrayList; import java.io.OutputStream; import java.io.PrintStream; import java.io.IOException; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.io.UncheckedIOException; import java.util.function.Consumer; import java.util.List; import java.io.Closeable; import java.util.Comparator; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); HDIYTree solver = new HDIYTree(); solver.solve(1, in, out); out.close(); } } static class HDIYTree { Debug debug = new Debug(true); public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); int k = in.ri(); int[] d = in.ri(k); int n2 = n * (n - 1) / 2; int ck = k * (k - 1) / 2; IntegerArrayList usList = new IntegerArrayList(n2); IntegerArrayList vsList = new IntegerArrayList(n2); LongArrayList wsList = new LongArrayList(n2); IntegerArrayList edgeList = new IntegerArrayList(ck); List<Edge> outside = new ArrayList<>(n2); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i >= k && j >= k) { Edge e = new Edge(i, j, in.ri()); outside.add(e); } else { usList.add(i); vsList.add(j); wsList.add(in.ri()); } if (i < k && j < k) { edgeList.add(usList.size() - 1); } } } DSU dsu = new DSU(n); dsu.init(); outside.sort(Comparator.comparingInt(x -> x.w)); debug.debug("size", usList.size()); for (Edge e : outside) { if (dsu.find(e.a) == dsu.find(e.b)) { continue; } dsu.merge(e.a, e.b); usList.add(e.a); vsList.add(e.b); wsList.add(e.w); } int[] us = usList.toArray(); int[] vs = vsList.toArray(); long[] ws = wsList.toArray(); int[] edges = edgeList.toArray(); int m = edges.length; long inf = (long) 1e9; long[] wsSnapshot = ws.clone(); int[] type = new int[us.length]; for (int i = 0; i < us.length; i++) { if (us[i] < k && vs[i] >= k) { type[i] = us[i]; } else if (vs[i] < k && us[i] >= k) { type[i] = vs[i]; } else { type[i] = k; } } debug.debug("size", us.length); long best = inf; for (int i = 0; i < 1 << m; i++) { boolean ok = true; dsu.init(); System.arraycopy(wsSnapshot, 0, ws, 0, ws.length); int[] cap = new int[k + 1]; System.arraycopy(d, 0, cap, 0, k); cap[k] = n; for (int j = 0; j < m; j++) { int e = edges[j]; if (Bits.get(i, j) == 0) { ws[e] += inf; } else { int u = us[e]; int v = vs[e]; ws[e] -= inf; cap[u]--; cap[v]--; if (dsu.find(u) == dsu.find(v)) { ok = false; } dsu.merge(u, v); } } for (int j = 0; j < ws.length; j++) { ws[j] = -ws[j]; } for (int j = 0; j <= k; j++) { if (cap[j] < 0) { ok = false; } } if (!ok) { continue; } MatroidIndependentSet container = MatroidIndependentSet.ofColorContainers(type, cap); MatroidIndependentSet tree = MatroidIndependentSet.ofSpanningTree(n, new int[][]{us, vs}); MaximumWeightMatroidIntersect mi = new MaximumWeightMatroidIntersect(us.length, ws); boolean[] sol = mi.intersect(container, tree); long sum = 0; int occur = 0; for (int j = 0; j < sol.length; j++) { if (sol[j]) { occur++; sum += ws[j]; } } sum = -sum; sum += Integer.bitCount(i) * inf; if (sum >= inf || occur < n - 1) { continue; } best = Math.min(best, sum); } out.println(best); } } static class LongArrayList implements Cloneable { private int size; private int cap; private long[] data; private static final long[] EMPTY = new long[0]; public LongArrayList(int cap) { this.cap = cap; if (cap == 0) { data = EMPTY; } else { data = new long[cap]; } } public LongArrayList(long[] data) { this(0); addAll(data); } public LongArrayList(LongArrayList list) { this.size = list.size; this.cap = list.cap; this.data = Arrays.copyOf(list.data, size); } public LongArrayList() { this(0); } public void ensureSpace(int req) { if (req > cap) { while (cap < req) { cap = Math.max(cap + 10, 2 * cap); } data = Arrays.copyOf(data, cap); } } public void add(long x) { ensureSpace(size + 1); data[size++] = x; } public void addAll(long[] x) { addAll(x, 0, x.length); } public void addAll(long[] x, int offset, int len) { ensureSpace(size + len); System.arraycopy(x, offset, data, size, len); size += len; } public void addAll(LongArrayList list) { addAll(list.data, 0, list.size); } public long[] toArray() { return Arrays.copyOf(data, size); } public String toString() { return Arrays.toString(toArray()); } public boolean equals(Object obj) { if (!(obj instanceof LongArrayList)) { return false; } LongArrayList other = (LongArrayList) obj; return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1); } public int hashCode() { int h = 1; for (int i = 0; i < size; i++) { h = h * 31 + Long.hashCode(data[i]); } return h; } public LongArrayList clone() { LongArrayList ans = new LongArrayList(); ans.addAll(this); return ans; } } static interface IntegerDeque extends IntegerStack { } static interface IntegerStack { } static class IntegerMultiWayStack { private int[] values; private int[] next; private int[] heads; private int alloc; private int stackNum; public IntegerIterator iterator(final int queue) { return new IntegerIterator() { int ele = heads[queue]; public boolean hasNext() { return ele != 0; } public int next() { int ans = values[ele]; ele = next[ele]; return ans; } }; } private void doubleCapacity() { int newSize = Math.max(next.length + 10, next.length * 2); next = Arrays.copyOf(next, newSize); values = Arrays.copyOf(values, newSize); } public void alloc() { alloc++; if (alloc >= next.length) { doubleCapacity(); } next[alloc] = 0; } public void clear() { alloc = 0; Arrays.fill(heads, 0, stackNum, 0); } public boolean isEmpty(int qId) { return heads[qId] == 0; } public IntegerMultiWayStack(int qNum, int totalCapacity) { values = new int[totalCapacity + 1]; next = new int[totalCapacity + 1]; heads = new int[qNum]; stackNum = qNum; } public void addLast(int qId, int x) { alloc(); values[alloc] = x; next[alloc] = heads[qId]; heads[qId] = alloc; } public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < stackNum; i++) { if (isEmpty(i)) { continue; } builder.append(i).append(": "); for (IntegerIterator iterator = iterator(i); iterator.hasNext(); ) { builder.append(iterator.next()).append(","); } if (builder.charAt(builder.length() - 1) == ',') { builder.setLength(builder.length() - 1); } builder.append('\n'); } return builder.toString(); } } static class MatroidIntersect { protected IntegerDequeImpl dq; protected int[] dists; protected boolean[] added; protected boolean[][] adj1; protected boolean[][] adj2; protected int n; protected boolean[] x1; protected boolean[] x2; protected static int distInf = (int) 1e9; protected int[] pre; protected Consumer<boolean[]> callback; protected static Consumer<boolean[]> nilCallback = x -> { }; public void setCallback(Consumer<boolean[]> callback) { if (callback == null) { callback = nilCallback; } this.callback = callback; } public MatroidIntersect(int n) { this.n = n; dq = new IntegerDequeImpl(n); dists = new int[n]; added = new boolean[n]; adj1 = new boolean[n][]; adj2 = new boolean[n][]; x1 = new boolean[n]; x2 = new boolean[n]; pre = new int[n]; setCallback(nilCallback); } protected boolean adj(int i, int j) { if (added[i]) { return adj1[i][j]; } else { return adj2[j][i]; } } protected boolean expand(MatroidIndependentSet a, MatroidIndependentSet b, int round) { Arrays.fill(x1, false); Arrays.fill(x2, false); a.prepare(added); b.prepare(added); a.extend(added, x1); b.extend(added, x2); for (int i = 0; i < n; i++) { if (x1[i] && x2[i]) { pre[i] = -1; xorPath(i); return true; } } for (int i = 0; i < n; i++) { if (added[i]) { Arrays.fill(adj1[i], false); Arrays.fill(adj2[i], false); } } a.computeAdj(added, adj1); b.computeAdj(added, adj2); Arrays.fill(dists, distInf); Arrays.fill(pre, -1); dq.clear(); for (int i = 0; i < n; i++) { if (added[i]) { continue; } //right if (x1[i]) { dists[i] = 0; dq.addLast(i); } } int tail = -1; while (!dq.isEmpty()) { int head = dq.removeFirst(); if (x2[head]) { tail = head; break; } for (int j = 0; j < n; j++) { if (added[head] != added[j] && adj(head, j) && dists[j] > dists[head] + 1) { dists[j] = dists[head] + 1; dq.addLast(j); pre[j] = head; } } } if (tail == -1) { return false; } xorPath(tail); return true; } protected void xorPath(int tail) { boolean[] last1 = new boolean[n]; boolean[] last2 = new boolean[n]; for (boolean add = true; tail != -1; tail = pre[tail], add = !add) { assert added[tail] != add; added[tail] = add; if (add) { adj1[tail] = last1; adj2[tail] = last2; } else { last1 = adj1[tail]; last2 = adj2[tail]; adj1[tail] = null; adj2[tail] = null; } } } public boolean[] intersect(MatroidIndependentSet a, MatroidIndependentSet b) { Arrays.fill(added, false); int round = 0; callback.accept(added); while (expand(a, b, round)) { round++; callback.accept(added); } return added; } } static class Edge { int a; int b; int w; public Edge(int a, int b, int w) { this.a = a; this.b = b; this.w = w; } } static class Bits { private Bits() { } public static int get(int x, int i) { return (x >>> i) & 1; } } static interface IntegerIterator { boolean hasNext(); int next(); } static class DSU { protected int[] p; public int[] size; protected int n; public DSU(int n) { p = new int[n]; size = new int[n]; } public void init() { init(p.length); } public void init(int n) { this.n = n; for (int i = 0; i < n; i++) { p[i] = i; size[i] = 1; } } public final int find(int a) { if (p[a] == p[p[a]]) { return p[a]; } find(p[a]); preAccess(a); return p[a] = p[p[a]]; } protected void preAccess(int a) { } protected void preMerge(int a, int b) { size[a] += size[b]; } public final void merge(int a, int b) { a = find(a); b = find(b); if (a == b) { return; } if (size[a] < size[b]) { int tmp = a; a = b; b = tmp; } preMerge(a, b); p[b] = a; } public String toString() { return Arrays.toString(Arrays.copyOf(p, n)); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private OutputStream writer; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); private static Field stringBuilderValueField; private char[] charBuf = new char[THRESHOLD * 2]; private byte[] byteBuf = new byte[THRESHOLD * 2]; static { try { stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value"); stringBuilderValueField.setAccessible(true); } catch (Exception e) { stringBuilderValueField = null; } stringBuilderValueField = null; } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(OutputStream writer) { this.writer = writer; } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(long c) { cache.append(c); afterWrite(); return this; } public FastOutput println(long c) { return append(c).println(); } public FastOutput println() { return append('\n'); } public FastOutput flush() { try { if (stringBuilderValueField != null) { try { byte[] value = (byte[]) stringBuilderValueField.get(cache); writer.write(value, 0, cache.length()); } catch (Exception e) { stringBuilderValueField = null; } } if (stringBuilderValueField == null) { int n = cache.length(); if (n > byteBuf.length) { //slow writer.write(cache.toString().getBytes(StandardCharsets.UTF_8)); // writer.append(cache); } else { cache.getChars(0, n, charBuf, 0); for (int i = 0; i < n; i++) { byteBuf[i] = (byte) charBuf[i]; } writer.write(byteBuf, 0, n); } } writer.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { writer.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class IntegerArrayList implements Cloneable { private int size; private int cap; private int[] data; private static final int[] EMPTY = new int[0]; public int[] getData() { return data; } public IntegerArrayList(int cap) { this.cap = cap; if (cap == 0) { data = EMPTY; } else { data = new int[cap]; } } public IntegerArrayList(int[] data) { this(0); addAll(data); } public IntegerArrayList(IntegerArrayList list) { this.size = list.size; this.cap = list.cap; this.data = Arrays.copyOf(list.data, size); } public IntegerArrayList() { this(0); } public void ensureSpace(int req) { if (req > cap) { while (cap < req) { cap = Math.max(cap + 10, 2 * cap); } data = Arrays.copyOf(data, cap); } } public void add(int x) { ensureSpace(size + 1); data[size++] = x; } public void addAll(int[] x) { addAll(x, 0, x.length); } public void addAll(int[] x, int offset, int len) { ensureSpace(size + len); System.arraycopy(x, offset, data, size, len); size += len; } public void addAll(IntegerArrayList list) { addAll(list.data, 0, list.size); } public int size() { return size; } public int[] toArray() { return Arrays.copyOf(data, size); } public void clear() { size = 0; } public String toString() { return Arrays.toString(toArray()); } public boolean equals(Object obj) { if (!(obj instanceof IntegerArrayList)) { return false; } IntegerArrayList other = (IntegerArrayList) obj; return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1); } public int hashCode() { int h = 1; for (int i = 0; i < size; i++) { h = h * 31 + Integer.hashCode(data[i]); } return h; } public IntegerArrayList clone() { IntegerArrayList ans = new IntegerArrayList(); ans.addAll(this); return ans; } } static class SequenceUtils { public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) { if ((ar - al) != (br - bl)) { return false; } for (int i = al, j = bl; i <= ar; i++, j++) { if (a[i] != b[j]) { return false; } } return true; } public static boolean equal(long[] a, int al, int ar, long[] b, int bl, int br) { if ((ar - al) != (br - bl)) { return false; } for (int i = al, j = bl; i <= ar; i++, j++) { if (a[i] != b[j]) { return false; } } return true; } } static class MaximumWeightMatroidIntersect extends MatroidIntersect { protected long[] weight; protected long[] pathWeight; protected static final long weightInf = (long) 1e18; protected boolean[] inq; protected long[] fixWeight; public MaximumWeightMatroidIntersect(int n, long[] weight) { super(n); this.weight = weight; pathWeight = new long[n]; inq = new boolean[n]; fixWeight = new long[n]; } protected boolean expand(MatroidIndependentSet a, MatroidIndependentSet b, int round) { a.prepare(added); b.prepare(added); Arrays.fill(x1, false); Arrays.fill(x2, false); a.extend(added, x1); b.extend(added, x2); for (int i = 0; i < n; i++) { if (added[i]) { Arrays.fill(adj1[i], false); Arrays.fill(adj2[i], false); fixWeight[i] = weight[i]; } else { fixWeight[i] = -weight[i]; } } a.computeAdj(added, adj1); b.computeAdj(added, adj2); Arrays.fill(dists, MatroidIntersect.distInf); Arrays.fill(pathWeight, weightInf); Arrays.fill(pre, -1); dq.clear(); for (int i = 0; i < n; i++) { if (added[i]) { continue; } //right if (x1[i]) { dists[i] = 0; pathWeight[i] = fixWeight[i]; dq.addLast(i); inq[i] = true; } } while (!dq.isEmpty()) { int head = dq.removeFirst(); inq[head] = false; for (int j = 0; j < n; j++) { if (added[head] != added[j] && adj(head, j)) { int comp = Long.compare(pathWeight[j], pathWeight[head] + fixWeight[j]); if (comp == 0) { comp = Integer.compare(dists[j], dists[head] + 1); } if (comp <= 0) { continue; } dists[j] = dists[head] + 1; pathWeight[j] = pathWeight[head] + fixWeight[j]; pre[j] = head; if (!inq[j]) { inq[j] = true; dq.addLast(j); } } } } int tail = -1; for (int i = 0; i < n; i++) { if (!x2[i] || !x1[i] && pre[i] == -1) { continue; } if (tail == -1 || pathWeight[i] < pathWeight[tail] || pathWeight[i] == pathWeight[tail] && dists[i] < dists[tail]) { tail = i; } } if (tail == -1) { return false; } xorPath(tail); return true; } } static class IntegerDequeImpl implements IntegerDeque { private int[] data; private int bpos; private int epos; private static final int[] EMPTY = new int[0]; private int n; public IntegerDequeImpl(int cap) { if (cap == 0) { data = EMPTY; } else { data = new int[cap]; } bpos = 0; epos = 0; n = cap; } private void expandSpace(int len) { while (n < len) { n = Math.max(n + 10, n * 2); } int[] newData = new int[n]; if (bpos <= epos) { if (bpos < epos) { System.arraycopy(data, bpos, newData, 0, epos - bpos); } } else { System.arraycopy(data, bpos, newData, 0, data.length - bpos); System.arraycopy(data, 0, newData, data.length - bpos, epos); } epos = size(); bpos = 0; data = newData; } public IntegerIterator iterator() { return new IntegerIterator() { int index = bpos; public boolean hasNext() { return index != epos; } public int next() { int ans = data[index]; index = IntegerDequeImpl.this.next(index); return ans; } }; } public int removeFirst() { int ans = data[bpos]; bpos = next(bpos); return ans; } public void addLast(int x) { ensureMore(); data[epos] = x; epos = next(epos); } public void clear() { bpos = epos = 0; } private int next(int x) { return x + 1 >= n ? 0 : x + 1; } private void ensureMore() { if (next(epos) == bpos) { expandSpace(n + 1); } } public int size() { int ans = epos - bpos; if (ans < 0) { ans += data.length; } return ans; } public boolean isEmpty() { return bpos == epos; } public String toString() { StringBuilder builder = new StringBuilder(); for (IntegerIterator iterator = iterator(); iterator.hasNext(); ) { builder.append(iterator.next()).append(' '); } return builder.toString(); } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } public void populate(int[] data) { for (int i = 0; i < data.length; i++) { data[i] = readInt(); } } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int ri() { return readInt(); } public int[] ri(int n) { int[] ans = new int[n]; populate(ans); return ans; } public int readInt() { boolean rev = false; skipBlank(); if (next == '+' || next == '-') { rev = next == '-'; next = read(); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } return rev ? val : -val; } } static class Debug { private boolean offline; private PrintStream out = System.err; public Debug(boolean enable) { offline = enable && System.getSecurityManager() == null; } public Debug debug(String name, int x) { if (offline) { debug(name, "" + x); } return this; } public Debug debug(String name, String x) { if (offline) { out.printf("%s=%s", name, x); out.println(); } return this; } } static interface MatroidIndependentSet { void computeAdj(boolean[] added, boolean[][] adj); void extend(boolean[] added, boolean[] extendable); void prepare(boolean[] added); static MatroidIndependentSet ofColorContainers(int[] type, int[] cap) { int[] size = new int[cap.length]; return new MatroidIndependentSet() { /** * O(rn) * @param added * @param adj */ public void computeAdj(boolean[] added, boolean[][] adj) { for (int i = 0; i < added.length; i++) { if (!added[i]) { continue; } for (int j = 0; j < added.length; j++) { if (added[j]) { continue; } if (size[type[j]] < cap[type[j]] || type[i] == type[j]) { adj[i][j] = true; } } } } /** * O(n) * @param added * @param extendable */ public void extend(boolean[] added, boolean[] extendable) { for (int i = 0; i < added.length; i++) { if (!added[i] && size[type[i]] < cap[type[i]]) { extendable[i] = true; } } } public void prepare(boolean[] added) { Arrays.fill(size, 0); for (int i = 0; i < added.length; i++) { if (added[i]) { size[type[i]]++; } } } }; } static MatroidIndependentSet ofSpanningTree(int n, int[][] edges) { return new MatroidIndependentSet() { DSU dsu = new DSU(n); IntegerMultiWayStack g = new IntegerMultiWayStack(n, edges[0].length); IntegerArrayList inset = new IntegerArrayList(edges[0].length); int[] p = new int[n]; int[] depth = new int[n]; public void dfs(int root, int fa, int d) { p[root] = fa; depth[root] = d; for (IntegerIterator iterator = g.iterator(root); iterator.hasNext(); ) { int e = iterator.next(); if (e == fa) { continue; } dfs(opponent(e, root), e, d + 1); } } public int opponent(int i, int root) { return edges[0][i] == root ? edges[1][i] : edges[0][i]; } /** * O(rn) */ public void computeAdj(boolean[] added, boolean[][] adj) { int[] insetData = inset.getData(); int m = inset.size(); for (int i = 0; i < added.length; i++) { if (added[i]) { continue; } if (dsu.find(edges[0][i]) != dsu.find(edges[1][i])) { for (int j = 0; j < m; j++) { adj[insetData[j]][i] = true; } } else { int a = edges[0][i]; int b = edges[1][i]; while (a != b) { if (depth[a] < depth[b]) { int tmp = a; a = b; b = tmp; } adj[p[a]][i] = true; a = opponent(p[a], a); } } } } /** * O(n) */ public void extend(boolean[] added, boolean[] extendable) { for (int i = 0; i < added.length; i++) { if (!added[i]) { extendable[i] = dsu.find(edges[0][i]) != dsu.find(edges[1][i]); } } } /** * O(r+n) */ public void prepare(boolean[] added) { g.clear(); dsu.init(); inset.clear(); for (int i = 0; i < added.length; i++) { if (added[i]) { dsu.merge(edges[0][i], edges[1][i]); g.addLast(edges[0][i], i); g.addLast(edges[1][i], i); inset.add(i); } } Arrays.fill(p, -1); for (int i = 0; i < n; i++) { if (p[i] == -1) { dfs(i, -1, 0); } } } }; } } }
Java
["10 5\n5 3 4 2 1\n29 49 33 12 55 15 32 62 37\n61 26 15 58 15 22 8 58\n37 16 9 39 20 14 58\n10 15 40 3 19 55\n53 13 37 44 52\n23 59 58 4\n69 80 29\n89 28\n48"]
6 seconds
["95"]
null
Java 11
standard input
[ "graphs", "greedy", "math", "probabilities" ]
d870600853fa87cce3e0b4a985bcc5c8
The first line of input contains two integers $$$n$$$, $$$k$$$ ($$$2 \leq n \leq 50$$$, $$$1 \leq k \leq min(n - 1, 5)$$$). The second line contains $$$k$$$ integers $$$d_1, d_2, \ldots, d_k$$$ ($$$1 \leq d_i \leq n$$$). The $$$i$$$-th of the next $$$n - 1$$$ lines contains $$$n - i$$$ integers $$$w_{i,i+1}, w_{i,i+2}, \ldots, w_{i,n}$$$ ($$$1 \leq w_{i,j} \leq 100$$$): weights of edges $$$(i,i+1),(i,i+2),\ldots,(i,n)$$$.
3,300
Print one integer: the minimum weight of a spanning tree under given degree constraints for the first $$$k$$$ vertices.
standard output
PASSED
7740066b3ed4d6dc5e2d0f9474b0bae1
train_107.jsonl
1630247700
As mentioned previously William really likes playing video games. In one of his favorite games, the player character is in a universe where every planet is designated by a binary number from $$$0$$$ to $$$2^n - 1$$$. On each planet, there are gates that allow the player to move from planet $$$i$$$ to planet $$$j$$$ if the binary representations of $$$i$$$ and $$$j$$$ differ in exactly one bit.William wants to test you and see how you can handle processing the following queries in this game universe: Destroy planets with numbers from $$$l$$$ to $$$r$$$ inclusively. These planets cannot be moved to anymore. Figure out if it is possible to reach planet $$$b$$$ from planet $$$a$$$ using some number of planetary gates. It is guaranteed that the planets $$$a$$$ and $$$b$$$ are not destroyed.
1024 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.ArrayList; import java.io.OutputStream; import java.io.PrintStream; import java.io.IOException; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.io.UncheckedIOException; import java.util.List; import java.io.Closeable; import java.util.Comparator; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { new TaskAdapter().run(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); GGatesToAnotherWorld solver = new GGatesToAnotherWorld(); solver.solve(1, in, out); out.close(); } } static class GGatesToAnotherWorld { Debug debug = new Debug(false); DSU dsu; List<Node> nodeList; public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); int m = in.ri(); List<Query> qs = new ArrayList<>(m); List<Query> updates = new ArrayList<>(m); for (int i = 0; i < m; i++) { int t = in.rs().equals("block") ? 0 : 1; Query q = new Query(t, in.rl(), in.rl()); qs.add(q); if (q.type == 0) { updates.add(q); } } debug.debug("updates", updates); List<Query> revQs = new ArrayList<>(qs); Collections.reverse(revQs); debug.debug("revQs", revQs); nodeList = new ArrayList<>(m * 2); updates.sort(Comparator.comparingLong(x -> x.l)); long last = 0; for (Query q : updates) { long l = last; long r = q.l - 1; addNode(l, r, false); addNode(q.l, q.r, true); last = q.r + 1; } addNode(last, (1L << n) - 1, false); debug.elapse("createNode"); Node[] nodes = nodeList.toArray(new Node[0]); debug.debug("nodes", nodes); for (int i = 0; i < nodes.length; i++) { nodes[i].id = i; } assert SortUtils.notStrictAscending(nodes, 0, nodes.length - 1, Comparator.comparingLong(x -> x.l)); dac(nodes, 0, nodes.length - 1, 0, (1L << n) - 1, n - 1); dsu = new DSU(nodes.length); dsu.init(); for (Node root : nodes) { recalc(root); } debug.elapse("build"); // search(nodes, nodes[0], nodes[12]); // for(Node cur = nodes[12]; cur != null; cur = cur.prev){ // debug.debug("cur", cur); // } for (Query q : revQs) { Node a = search(nodes, q.l); Node b = search(nodes, q.r); if (q.type == 0) { assert a.del; assert b.del; a.del = false; b.del = false; recalc(a); recalc(b); } else { assert !a.del; assert !b.del; q.ans = dsu.find(a.id) == dsu.find(b.id); } } debug.elapse("answer"); for (Query q : qs) { if (q.type == 1) { out.println(q.ans ? 1 : 0); } } } public boolean intersect(long l, long r, long L, long R) { return l <= R && r >= L; } public boolean checkInRange(Node[] nodes, int l, int r, long L, long R) { for (int i = l; i <= r; i++) { if (nodes[i].l < L || nodes[i].r > R) { return false; } } return true; } public void dac(Node[] nodes, int l, int r, long L, long R, int D) { assert checkInRange(nodes, l, r, L, R); if (l >= r) { return; } if (D < 0) { assert l == r; } long M = (L + R) / 2; int ml = l - 1; while (ml + 1 <= r && nodes[ml + 1].r <= M) { ml++; } int mr = r + 1; while (mr - 1 >= l && nodes[mr - 1].l > M) { mr--; } dac(nodes, l, ml, L, M, D - 1); dac(nodes, mr, r, M + 1, R, D - 1); if (ml + 1 < mr) { for (int j = ml + 1; j < mr; j++) { for (int i = l; i <= ml; i++) { addEdge(nodes[i], nodes[j]); } for (int i = mr; i <= r; i++) { addEdge(nodes[i], nodes[j]); } } } else { int lIter = l; int rIter = mr; long mask = (1L << D) - 1; while (lIter <= ml && rIter <= r) { int comp = Long.compare(nodes[lIter].r & mask, nodes[rIter].r & mask); if (intersect(nodes[lIter].l & mask, nodes[lIter].r & mask, nodes[rIter].l & mask, nodes[rIter].r & mask)) { addEdge(nodes[lIter], nodes[rIter]); } if (comp <= 0) { lIter++; } if (comp >= 0) { rIter++; } } } } public void recalc(Node root) { if (root.del) { return; } for (Node node : root.adj) { if (node.del) { continue; } dsu.merge(root.id, node.id); } } public boolean check(Node a, Node b) { for (int i = 0; i < 50; i++) { for (long j = a.l; j <= a.r; j++) { long to = j ^ (1L << i); if (b.l <= to && b.r >= to) { return true; } } } return false; } public void addEdge(Node a, Node b) { assert check(a, b); a.adj.add(b); b.adj.add(a); } public Node search(Node[] nodes, long x) { int l = 0; int r = nodes.length - 1; while (l < r) { int m = (l + r + 1) >> 1; if (nodes[m].l <= x) { l = m; } else { r = m - 1; } } return nodes[l]; } void addNode(long l, long r, boolean del) { if (r < l) { return; } if (l == r) { addNode0(l, r, del); return; } int index = Bits.theFirstDifferentIndex(l, r); long prefix = (l >> (index + 1)) << (index + 1); long mid = prefix | (1L << index); addNode0(l, mid - 1, del); addNode0(mid, r, del); } void addNode0(long l, long r, boolean del) { if (r < l) { return; } nodeList.add(new Node(l, r, del)); } } static class Bits { private Bits() { } public static int theFirstDifferentIndex(long x, long y) { return Log2.floorLog(x ^ y); } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 32 << 10; private OutputStream writer; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); private static Field stringBuilderValueField; private char[] charBuf = new char[THRESHOLD * 2]; private byte[] byteBuf = new byte[THRESHOLD * 2]; static { try { stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value"); stringBuilderValueField.setAccessible(true); } catch (Exception e) { stringBuilderValueField = null; } stringBuilderValueField = null; } public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(OutputStream writer) { this.writer = writer; } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(int c) { cache.append(c); afterWrite(); return this; } public FastOutput println(int c) { return append(c).println(); } public FastOutput println() { return append('\n'); } public FastOutput flush() { try { if (stringBuilderValueField != null) { try { byte[] value = (byte[]) stringBuilderValueField.get(cache); writer.write(value, 0, cache.length()); } catch (Exception e) { stringBuilderValueField = null; } } if (stringBuilderValueField == null) { int n = cache.length(); if (n > byteBuf.length) { //slow writer.write(cache.toString().getBytes(StandardCharsets.UTF_8)); // writer.append(cache); } else { cache.getChars(0, n, charBuf, 0); for (int i = 0; i < n; i++) { byteBuf[i] = (byte) charBuf[i]; } writer.write(byteBuf, 0, n); } } writer.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { writer.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class Debug { private boolean offline; private PrintStream out = System.err; private long time = System.currentTimeMillis(); static int[] empty = new int[0]; public Debug(boolean enable) { offline = enable && System.getSecurityManager() == null; } public Debug elapse(String name) { if (offline) { debug(name, System.currentTimeMillis() - time); time = System.currentTimeMillis(); } return this; } public Debug debug(String name, long x) { if (offline) { debug(name, "" + x); } return this; } public Debug debug(String name, String x) { if (offline) { out.printf("%s=%s", name, x); out.println(); } return this; } public Debug debug(String name, Object x) { return debug(name, x, empty); } public Debug debug(String name, Object x, int... indexes) { if (offline) { if (x == null || !x.getClass().isArray()) { out.append(name); for (int i : indexes) { out.printf("[%d]", i); } out.append("=").append("" + x); out.println(); } else { indexes = Arrays.copyOf(indexes, indexes.length + 1); if (x instanceof byte[]) { byte[] arr = (byte[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof short[]) { short[] arr = (short[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof boolean[]) { boolean[] arr = (boolean[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof char[]) { char[] arr = (char[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof int[]) { int[] arr = (int[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof float[]) { float[] arr = (float[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof double[]) { double[] arr = (double[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else if (x instanceof long[]) { long[] arr = (long[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } else { Object[] arr = (Object[]) x; for (int i = 0; i < arr.length; i++) { indexes[indexes.length - 1] = i; debug(name, arr[i], indexes); } } } } return this; } } static class DSU { protected int[] p; public int[] size; protected int n; public DSU(int n) { p = new int[n]; size = new int[n]; } public void init() { init(p.length); } public void init(int n) { this.n = n; for (int i = 0; i < n; i++) { p[i] = i; size[i] = 1; } } public final int find(int a) { if (p[a] == p[p[a]]) { return p[a]; } find(p[a]); preAccess(a); return p[a] = p[p[a]]; } protected void preAccess(int a) { } protected void preMerge(int a, int b) { size[a] += size[b]; } public final void merge(int a, int b) { a = find(a); b = find(b); if (a == b) { return; } if (size[a] < size[b]) { int tmp = a; a = b; b = tmp; } preMerge(a, b); p[b] = a; } public String toString() { return Arrays.toString(Arrays.copyOf(p, n)); } } static class Log2 { public static int floorLog(long x) { if (x <= 0) { throw new IllegalArgumentException(); } return 63 - Long.numberOfLeadingZeros(x); } } static class Query { int type; long l; long r; boolean ans; public Query(int type, long l, long r) { this.type = type; this.l = l; this.r = r; } public String toString() { return "Query{" + "type=" + type + ", l=" + l + ", r=" + r + '}'; } } static class SortUtils { private SortUtils() { } public static <T> boolean notStrictAscending(T[] data, int l, int r, Comparator<T> comp) { for (int i = l + 1; i <= r; i++) { if (comp.compare(data[i], data[i - 1]) < 0) { return false; } } return true; } } static class Node { long l; long r; boolean del; int id; List<Node> adj = new ArrayList<>(); public Node(long l, long r, boolean del) { this.l = l; this.r = r; this.del = del; } public String toString() { return "Node{" + "l=" + l + ", r=" + r + ", del=" + del + ", id=" + id + '}'; } } static class FastInput { private final InputStream is; private StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int ri() { return readInt(); } public int readInt() { boolean rev = false; skipBlank(); if (next == '+' || next == '-') { rev = next == '-'; next = read(); } int val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } return rev ? val : -val; } public long rl() { return readLong(); } public long readLong() { boolean rev = false; skipBlank(); if (next == '+' || next == '-') { rev = next == '-'; next = read(); } long val = 0; while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } return rev ? val : -val; } public String rs() { return readString(); } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } } }
Java
["3 3\nask 0 7\nblock 3 6\nask 0 7", "6 10\nblock 12 26\nask 44 63\nblock 32 46\nask 1 54\nblock 27 30\nask 10 31\nask 11 31\nask 49 31\nblock 31 31\nask 2 51"]
4 seconds
["1\n0", "1\n1\n0\n0\n1\n0"]
NoteThe first example test can be visualized in the following way: Response to a query ask 0 7 is positive.Next after query block 3 6 the graph will look the following way (destroyed vertices are highlighted): Response to a query ask 0 7 is negative, since any path from vertex $$$0$$$ to vertex $$$7$$$ must go through one of the destroyed vertices.
Java 11
standard input
[ "bitmasks", "data structures", "dsu", "two pointers" ]
327718077f69c0ed1c48acaf09b8e576
The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 50$$$, $$$1 \leq m \leq 5 \cdot 10^4$$$), which are the number of bits in binary representation of each planets' designation and the number of queries, respectively. Each of the next $$$m$$$ lines contains a query of two types: block l r — query for destruction of planets with numbers from $$$l$$$ to $$$r$$$ inclusively ($$$0 \le l \le r &lt; 2^n$$$). It's guaranteed that no planet will be destroyed twice. ask a b — query for reachability between planets $$$a$$$ and $$$b$$$ ($$$0 \le a, b &lt; 2^n$$$). It's guaranteed that planets $$$a$$$ and $$$b$$$ hasn't been destroyed yet.
3,300
For each query of type ask you must output "1" in a new line, if it is possible to reach planet $$$b$$$ from planet $$$a$$$ and "0" otherwise (without quotation marks).
standard output
PASSED
f47ce88eb6d38583ef4ae2a9f905daf3
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class InterestingSum { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String[] s=new String[650]; //System.out.println("Enter the number of test cases:"); int n1=sc.nextInt(); String[] s1=new String[n1]; //System.out.println("Enter the test cases"); for(int i=0;i<n1;i++) { s1[i]=sc.next(); } int i=0; for(int j='a';j<='z';j++) { for(int k='a';k<='z';k++) { if(j!=k) { s[i]=Character. toString(j)+Character. toString(k); i++; } } } for(int j=0;j<n1;j++) { for(int k=0;k<650;k++) { if(s1[j].equals(s[k])) { System.out.println(k+1); } } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
6ca231797fba82a36e5c9ea2668b7d65
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { String s = sc.next(); int res = (26 * (s.charAt(0) - 'a')) + (s.charAt(1) - 'a'); if (s.charAt(0) > 'b') res -= (s.charAt(0) - 'a') - 1; if (s.charAt(0) != 'a' && s.charAt(1) > s.charAt(0)) res -= 1; System.out.println(res); } /* * ul zy ge fd nu sd yn */ } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
bf474b8116264a774c2ced9cb532b860
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class B_Dictionary { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- != 0) { String s = sc.next(); int a = s.charAt(0) - 'a' + 1, b = s.charAt(1) - 'a' + 1; int tf = 25 * (a - 1) + b; if (b > a) tf--; System.out.println(tf); } sc.close(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
be80197e0d82937a005a421913e2d205
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class B_Dictionary { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- != 0) { String s = sc.next(); int a = s.charAt(0) - 'a' + 1, b = s.charAt(1) - 'a' + 1; int tf = 25 * (a - 1); if (b > a) { tf += b - 1; } else { tf += b; } System.out.println(tf); } sc.close(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
bc83599005aa75ca2a5713215fc0da84
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; public class Main { private static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { char[] word = in.next().toCharArray(); int p = word[0] - 'a', q = word[1] - 'a'; if (p == 0) { out.println(q); } else { out.println(p * 25 + (q > p ? q : q + 1)); } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); // solver.solve(1, in, out); int testNumber = in.nextInt(); for (int i = 1; i <= testNumber; i++) { solver.solve(i, in, out); } out.close(); } private static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; private InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } private String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private String nextLine() { String str; try { str = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return str; } private boolean hasNext() { while (tokenizer != null && !tokenizer.hasMoreTokens()) { String nextLine = null; try { nextLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
89153a6846d5ce4d2b180f824ef1f16e
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; import java.util.HashMap; public class Solution { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int index = 1; HashMap<String, Integer> dictionary = new HashMap<>(); for (char i = 65; i <= 90; i++) { for (char j = 65; j <= 90; j++) { if (i == j) { continue; } StringBuilder builder = new StringBuilder(); builder.append(i); builder.append(j); dictionary.put(builder.toString(), index++); } } int t = scn.nextInt(); for(int i = 0; i < t; i++) { String str = scn.next(); System.out.println(dictionary.get(str.toUpperCase())); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
839a6d9a8ef4497266a365196804b9ae
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
//package com.company; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-- > 0){ String s = in.next(); int sum = (s.charAt(0)-97)*25; if(s.charAt(1) < s.charAt(0)){ sum += (s.charAt(1)-96); }else { sum += (s.charAt(1)-97); } System.out.println(sum); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
3c767f2e986e93aab1439bed3d2fab11
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class _1674B_Dictionary { public static void main(String[] args) { ArrayList<String> dic = new ArrayList<>(); for (char i = 'a'; i <= 'z'; i++) { for (char j = 'a'; j <= 'z'; j++) { if (i == j) { continue; } dic.add("" + i + j); } } Scanner input = new Scanner(System.in); int test = input.nextInt(); while (test-- > 0) { String num = input.next(); System.out.println(dic.indexOf(num) + 1); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
b16de7d10d49ec508b395f471d66fdf3
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args){ MyScanner scanner = new MyScanner(); int testCount = scanner.nextInt(); for(int testIdx = 1; testIdx <= testCount; testIdx++){ String s = scanner.nextLine(); int res = (s.charAt(0) - 'a') * 25 + (s.charAt(1) - 'a') + 1; if(s.charAt(1) - s.charAt(0) > 0){ res--; } out.println(res); } out.close(); } public static void printResult(int idx, long res){ out.println("Case #" + idx + ": " + res); } static void print1DArray(int[] arr){ for(int i = 0; i < arr.length; i++){ out.print(arr[i]); if(i < arr.length - 1){ out.print(' '); } } out.print('\n'); } static void print1DArrayList(List<Integer> arrayList){ for(int i = 0; i < arrayList.size(); i++){ out.print(arrayList.get(i)); if(i < arrayList.size() - 1){ out.print(' '); } } out.print('\n'); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
66a387562591e16065472363198f8859
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class Dictionary { public static void main(String[]args) { Scanner sn = new Scanner(System.in); int t = sn.nextInt(); sn.nextLine(); for(int i =0; i<t; i++) { String word = sn.nextLine(); char ch1 = word.charAt(0); char ch2 = word.charAt(1); int sum = 0; sum += (ch1-'a')*25; sum += ch2-'a'; if(ch1>ch2) { sum++; } System.out.println(sum); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
e31d2d627bf5d7975fb244ad16511cae
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static void main(String[] args) throws IOException { ArrayList<String> list = new ArrayList<>(); for(int j = 97; j <= 122; j++){ for(int k = 97; k <=122; k++){ if(j == k) continue; StringBuilder sb = new StringBuilder(); sb.append((char)j); sb.append((char)k); list.add(sb.toString()); } } Scanner sc = new Scanner(System.in); String[] t = new String[sc.nextInt()]; sc.nextLine(); for( int i = 0; i < t.length; i ++){ t[i] = sc.next(); } for( int m = 0; m < t.length; m++){ System.out.println((1+list.indexOf(t[m]))); } } } // ascii 97 - 122 = a-z // x -> 26-(122 - charat(i)) // y -> 25-(122 - charat(i)) // xy -> abs pos /* public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long k = sc.nextLong(); if(n%2==0) { if(k==n/2) System.out.prlongln(n-1); if(k>n/2) System.out.prlongln(2*(k-n/2)); if(k<n/2) System.out.prlongln((2*k)-1); } else { if(k==n/2+1) System.out.prlongln(n); if(k>n/2+1) System.out.prlongln(2*(k-(n/2+1))); if(k<n/2+1) System.out.prlongln((2*k)-1); } } */ /* interesting Math problem for function Scanner sc = new Scanner(System.in); long n = sc.nextLong(); if (n % 2 == 0){ System.out.prlongln(n/2); } else { System.out.prlongln((n-1)/2-n); } */ /* //The key point here is to look at y, not at x -> y-(x mod y) is the formula to calculate the distance of x to x with remainder 0 public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] res = new long[n]; sc.nextLine(); for(int i = 0; i < n; i ++){ long p = 0; long a = sc.nextLong(); long b = sc.nextLong(); if(a < b) { res[i] = b - a; continue; } if(a%b == 0){ res[i] = 0; continue; } res[i] = b-(a%b); } for(long j : res){ System.out.println(j); } } } */
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
0ed8913eb527a6a6b800c20f3e54aeba
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int tc= sc.nextInt(); while(tc > 0){ tc--; String s = sc.next(); int c1 = s.charAt(0)-'a'; int c2 = s.charAt(1)-'a'; int m = c1*25; int ans =0; if(c2 > c1){ ans += (m+c2); }else{ ans += (m + c2+1); } System.out.println(ans); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
9c4eb7bc1ba09fa393f2d0d7d6d3ed0a
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class Solution{ public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); s.nextLine(); while(n!=0) { --n; String r=s.nextLine(); int y=(r.charAt(0)-97)*25+(r.charAt(1)-97); if(r.charAt(1)<r.charAt(0)) { y+=1; } System.out.println(y); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
132707b40944b15631ce73aaa03e4ba7
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class CF1674B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { String s = scanner.next(); System.out.println(solve(s)); } } private static Map<String, Integer> idxMap; private static String solve(String s) { if (idxMap == null) { idxMap = new HashMap<>(); int idx = 1; for (char i = 'a'; i <= 'z'; i++) { for (char j = 'a'; j <= 'z'; j++) { if (i != j) { idxMap.put("" + i + j, idx++); } } } } return String.valueOf(idxMap.get(s)); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
c7358020f1b967cddb0487ea780a16cc
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class Testing { public static void main(String[] args) { Scanner s = new Scanner (System.in); int t=s.nextInt(); for(int i=0;i<t;i++){ String input=s.next(); int x=(int)(input.charAt(1)-97)-(input.charAt(0)-97); if(input.charAt(1)<input.charAt(0)) x++; System.out.println(((int)input.charAt(0)%97)*26+x); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
fd5db711bbdc6f5245adf6aea2a1d067
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Solution { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static PrintWriter pw = new PrintWriter(System.out); static void getRes() throws Exception{ String inp = br.readLine(); char first = inp.charAt(0), second = inp.charAt(1); pw.println((first - 'a') * 25 + (second < first ? second - 'a' + 1 : second - 'a')); } public static void main(String[] args) throws Exception { int tests = Integer.parseInt(br.readLine()); while (tests-- > 0) { getRes(); } pw.flush(); pw.close(); } static int[] inputIntArray(int n) throws Exception{ int[] arr = new int[n]; String[] inp = br.readLine().split(" "); for (int x = 0; x < n; x++) arr[x] = Integer.parseInt(inp[x]); return arr; } static long[] inputLongArray(int n) throws Exception{ long[] arr = new long[n]; String[] inp = br.readLine().split(" "); for (int x = 0; x < n; x++) arr[x] = Integer.parseInt(inp[x]); return arr; } static void printArray(int[] arr) { for (int x = 0; x < arr.length; x++) pw.print(arr[x] + " "); pw.println(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 17
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
8b4d050348424507b0bdae3aee68f337
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class Diccionary { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); while(n-->0){ int c, d; String a = scanner.next(); if(a.charAt(0)=='a') c = ((a.charAt(0)-'a')*25); else c = ((a.charAt(0)-'a')*25)+1; if(a.charAt(1) >= a.charAt(0)+1 && a.charAt(0)!='a') d = ((a.charAt(1)-'a')-1); else d = (a.charAt(1)-'a'); System.out.println(c+d); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
0c3d069f0b8c30ac94b8f55bcef2772b
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.HashMap; import java.util.Scanner; public class codeforcesdiv3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); scan.nextLine(); for(int i = 0; i < n; i++){ String s = scan.nextLine(); System.out.println(allSum(s)); } } public static int allSum(String s){ HashMap<Character , Integer> hm = new HashMap<>(); HashMap<Character , Integer> h = new HashMap<>(); String ss = "abcdefghijklmnopqrstuvwxyz"; int pl = 0; int kl = 0; int sum = 0; if(((int)(s.charAt(0))-97) > ((int)(s.charAt(1))-97)){ sum += 1; } for(int i = 0; i < ss.length(); i++){ hm.put(ss.charAt(i) , pl); pl += 25; } for(int i = 0; i < ss.length(); i++){ h.put(ss.charAt(i) , kl++); } for(int j = 0; j < s.length(); j++){ if(j % 2 == 0) { sum += hm.get(s.charAt(j)); }else{ sum += h.get(s.charAt(j)); } } return sum; } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
800a4607719983d1e3cd56f40732e800
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 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 solution(String str) { HashMap<Character,Integer> hm=new HashMap<>(); char ch='a'; for(int i=1;i<=26;i++) { hm.put(ch,i); ch+=1; } char a=str.charAt(0); char b=str.charAt(1); int sol=0; if(hm.get(a)<hm.get(b)) sol=((hm.get(a)-1)*25) + (hm.get(b)-1); else sol= ((hm.get(a)-1)*25) + (hm.get(b)); System.out.println(sol); } public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int test=sc.nextInt(); while(test-->0) { String str=sc.next(); solution(str); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
b69ef37dfc23689ed6fc562815fa9f60
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class Practice { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t--!=0){ String val = sc.nextLine(); int ans = (val.charAt(0)-'a')*25; ans+=val.charAt(1)-'a'+1; if(val.charAt(0)<val.charAt(1)) ans--; System.out.println(ans); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
5eb1b15ad028f42e5abe6fc6aa0ac3e6
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.PrintWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class Test { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out)); int tc = Integer.parseInt(reader.readLine()); char[][] chars = new char[tc][]; for (int i = 0; i < tc; i++) { chars[i] = reader.readLine().toCharArray(); } for (int i = 0; i < tc; i++) { writer.println(25 * (chars[i][0] - 'a') + (chars[i][1] - 'a') + (chars[i][0] < chars[i][1] ? 0 : 1)); } writer.flush(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
d0c24065d88ca167513499d806a8a73c
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class DictionarySolution2 { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int tc = Integer.parseInt(reader.readLine()); char[][] chars = new char[tc][]; for (int i = 0; i < tc; i++) { chars[i] = reader.readLine().toCharArray(); } for (int i = 0; i < tc; i++) { System.out.println(25 * (chars[i][0] - 'a') + (chars[i][1] - 'a') + (chars[i][0] < chars[i][1] ? 0 : 1)); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
edba3367d037dd1d80a10bf681ecff07
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; public class DictionarySolution1 { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int tc = Integer.parseInt(reader.readLine()); while (tc-- > 0) { char[] chars = reader.readLine().toCharArray(); System.out.println(25 * (chars[0] - 'a') + (chars[1] - 'a') + (chars[0] < chars[1] ? 0 : 1)); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
43e9cabf3779ca2f9f969f74c689fc65
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class dict { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); sc.nextLine(); while(t-->0) { String s=sc.nextLine(); if(s.charAt(0)>s.charAt(1)) System.out.println(25*(int)(s.charAt(0)-'a')+(int)s.charAt(1)-'a'+1); else System.out.println(25*(int)(s.charAt(0)-'a')+(int)s.charAt(1)-'a'); } sc.close(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
20678b2ba32e21af90cbf207501e3d30
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import javax.lang.model.util.ElementScanner6; import javax.sound.midi.SysexMessage; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; public class Solution { static HashMap<Integer, Integer> createHashMap(int arr[]) { HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>(); for (int i = 0; i < arr.length; i++) { Integer c = hmap.get(arr[i]); if (hmap.get(arr[i]) == null) { hmap.put(arr[i], 1); } else { hmap.put(arr[i], ++c); } } return hmap; } static int parseInt(String str) { return Integer.parseInt(str); } static String noZeros(char[] num) { String str = ""; for (int i = 0; i < num.length; i++) { if (num[i] == '0') continue; else str += num[i]; } return str; } public static void Sort2DArrayBasedOnColumnNumber(int[][] array, final int columnNumber) { Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] first, int[] second) { if (first[columnNumber - 1] > second[columnNumber - 1]) return 1; else return -1; } }); } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tk; int t = parseInt(br.readLine()); String str; int preindex; int index; for (int i = 1; i <= t; i++) { str = br.readLine(); preindex = (str.charAt(0) - 96) * 25; index = 25 - ((str.charAt(1) - 96)); index = preindex - index; if (str.charAt(1) - str.charAt(0) > 0) System.out.println(index - 1); else System.out.println(index); } } } /* * 96 * 8 * 1 8 2 7 3 6 4 5 * -1 0 2 7 9 12 16 20 * count=16 * * BRWBRWBRW * * * x=7 * 2 1 2 * count=3 * 3 2 3 * count=5 * 4 3 4 * count=7 * 5 4 5 * count=8 * 6 5 6 * 7 6 7 * 8 7 8 * 9 8 9 * count=11 * ans+=(x-sum)/(i+1)+1 * -1 2 0 * -1 0 2 * sum=0 * sum=-1 * 1 2 4 2 1 * 4 2 2 1 1 * 4 2 2 1 1 * * 2 4 5 5 2 * 4 5 5 2 2 * 5 5 2 2 4 * * 2 * 2 2 4 5 5 * * * * 4 * 2 10 1 7 * 3 * sum=-1 * sum * 7 9 * 8 * 6 * 3 * 7 * 12 * 6 * -1 * 7 * 16 * * * * 10 10 * 9 * 11 * 14 * 10 * 5 * 11 * 18 * 10 * 1 * 11 * +10 -5=5 * 5 7 * 6 * 4 * 1 * 5 * 10 * 6 * -1 * 3 * * 2 * * * * sum=15 * sum<=x * count+=n * count=5 * sum+=n * sun=20 * count=10 * sum=25 * sum=18 * * * * * * * indeces={0,1,6,7} * 5-1=4 */
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
bb22e79e37948944c8c772f467d0aa74
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.lang.*; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; public class Last { static Scanner sc = new Scanner(System.in); static int I() { return sc.nextInt(); } static long L() { return sc.nextLong(); } static String S() { return sc.next(); } static long mod = (long) (1e9 + 7); public static void main(String[] args) { int t = I(); HashMap<String,Integer> hm=new HashMap<>(); int idx=1; for(int i=0;i<26;i++) for(int j=0;j<26;j++) if(i!=j){ String s=(char)(i+'a')+""+(char)(j+'a'); hm.put(s, idx++); } while (t-- > 0) { String s=S(); System.out.println(hm.get(s)); } } } /* (b^a)*x=y */
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
bdeb3c82dbba7f0c4e1a6f5acd98f354
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
/*Author: Jatin Yadav*/ import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main{ static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[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 void main(String args[])throws IOException{ Reader s = new Reader(); long t = s.nextLong(); //int b[] = new int[100005]; while(t-->0){ int sum=0; String str = s.readLine(); int as1 = ((int)(str.charAt(0))-97); int as2 = ((int)(str.charAt(1))-97); int ans = as1*25+as2; if(as2<as1){ ans+=1; } System.out.println(ans); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
2b791fd48e855847d5d8115cc6c81f64
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; public class Solution{ public static void main(String[] args) throws IOException { FastScanner f= new FastScanner(); int ttt=1; ttt=f.nextInt(); PrintWriter out=new PrintWriter(System.out); outer: for(int tt=0;tt<ttt;tt++) { char[] l=f.next().toCharArray(); int ans=(l[0]-'a')*26+l[1]-'a'; ans-=Math.max(0, l[0]-'a'-1); if(l[0]<l[1] && l[0]!='a') ans--; System.out.println(ans); } out.close(); } static void sort(int[] p) { ArrayList<Integer> q = new ArrayList<>(); for (int i: p) q.add( i); Collections.sort(q); for (int i = 0; i < p.length; i++) p[i] = q.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } long[] readLongArray(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
1d812ecba6098b778b37b5694a41e66e
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; public class Dictionary { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out)); String Eng [] = {"a" , "b" , "c" , "d" , "e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; int x = Integer.parseInt(reader.readLine()); int indexF = 0; int indexS = 0; for (int i = 0;i<x;i++){ String line = reader.readLine(); String split[] = line.split(""); for (int j = 0;j<Eng.length;j++){ if (split[0].equals(Eng[j])){ indexF = j; } if (split[1].equals(Eng[j])) indexS = j; } if (indexF == 0 || indexF < indexS) writer.println((25 * indexF) + indexS); else writer.println((25 * indexF) + indexS+1); } writer.flush(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
6c4a73e10ca20036c9ae29dffedf662b
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.math.*; import java.util.*; /* * Author: Atuer */ public class Main { // ==== Solve Code ====// static int INF = 2000000010; public static void csh() { } public static void main(String[] args) throws IOException { // csh(); int t = in.nextInt(); while (t-- > 0) { solve(); out.flush(); } out.close(); } public static void solve() { char[] s = in.next().toCharArray(); out.println((s[0] - 'a') * 25 + s[1] - 'a' + (s[1] < s[0] ? 1 : 0)); } public static class Node { int x, y, k; public Node(int x, int y, int k) { this.x = x; this.y = y; this.k = k; } } // ==== Solve Code ====// // ==== Template ==== // public static long cnm(int a, int b) { long sum = 1; int i = a, j = 1; while (j <= b) { sum = sum * i / j; i--; j++; } return sum; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static void gbSort(int[] a, int l, int r) { if (l < r) { int m = (l + r) >> 1; gbSort(a, l, m); gbSort(a, m + 1, r); int[] t = new int[r - l + 1]; int idx = 0, i = l, j = m + 1; while (i <= m && j <= r) if (a[i] <= a[j]) t[idx++] = a[i++]; else t[idx++] = a[j++]; while (i <= m) t[idx++] = a[i++]; while (j <= r) t[idx++] = a[j++]; for (int z = 0; z < t.length; z++) a[l + z] = t[z]; } } // ==== Template ==== // // ==== IO ==== // static InputStream inputStream = System.in; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(System.out); static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } // ==== IO ==== // }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
60dc012e51f8aeef72f8285071ea668e
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.math.*; import java.util.*; /* * Author: Atuer */ public class Main { // ==== Solve Code ====// static int INF = 2000000010; public static void csh() { } public static void main(String[] args) throws IOException { // csh(); int t = in.nextInt(); while (t-- > 0) { solve(); out.flush(); } out.close(); } public static void solve() { char[] a = in.next().toCharArray(); int b = a[0] - 'a'; int c = a[1] - 'a'; int d = b*25+c; out.println(b < c ? d : d + 1); } public static class Node { int x, y, k; public Node(int x, int y, int k) { this.x = x; this.y = y; this.k = k; } } // ==== Solve Code ====// // ==== Template ==== // public static long cnm(int a, int b) { long sum = 1; int i = a, j = 1; while (j <= b) { sum = sum * i / j; i--; j++; } return sum; } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public static int lcm(int a, int b) { return (a * b) / gcd(a, b); } public static void gbSort(int[] a, int l, int r) { if (l < r) { int m = (l + r) >> 1; gbSort(a, l, m); gbSort(a, m + 1, r); int[] t = new int[r - l + 1]; int idx = 0, i = l, j = m + 1; while (i <= m && j <= r) if (a[i] <= a[j]) t[idx++] = a[i++]; else t[idx++] = a[j++]; while (i <= m) t[idx++] = a[i++]; while (j <= r) t[idx++] = a[j++]; for (int z = 0; z < t.length; z++) a[l + z] = t[z]; } } // ==== Template ==== // // ==== IO ==== // static InputStream inputStream = System.in; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(System.out); static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public Double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } // ==== IO ==== // }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
8af4eaee534b4174665dd6c5d1a3e614
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import javax.sound.midi.Soundbank; public class dictionar { public static void main(String[] args) { String[] str= new String[650]; for(int i=0;i<650;i++) { str[i]=""; } int c=0; for(char ch='a';ch<='z';ch++) { for(char ch1='a';ch1<='z';ch1++) { if(ch!=ch1) { str[c]=str[c]+(char)ch+(char)ch1; c++; } } } Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { String s=sc.next(); for(int i=0;i<650;i++) { if(s.compareTo(str[i])==0) { System.out.println(i+1); } } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
7547ac5a71b2ee8d189c931d4b9cc6d2
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Dispatcher { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); scan.nextLine(); for(int i=0;i<t;i++) { String a = scan.nextLine(); int c = a.valueOf(a).charAt(0) - 97; int d = a.valueOf(a).charAt(1) - 97; if(c>d) d++; System.out.println(c*25 + d); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
414e45f7158ad03789302a9781b2d35a
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import java.io.*; public class lab_1 { public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int start = 0; int diff = 0; int dec = 0; int second = 0; for(int i = 0;i<t;i++) {dec = 0; String s = sc.nextLine(); start = s.charAt(0) - 97; second = s.charAt(1) - 97; if((start)<( second) && s.charAt(0)!='a') { dec = -1; } if(s.charAt(0) - 97 != 0) { second = s.charAt(1) - 96; } else { second = s.charAt(1) - 97; } diff = start*25 + second + dec ; System.out.println(diff); } } 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(); } public int[] readArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
f8ea5e915290bb035f74b5499e555f50
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class Dictionary { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0){ String s=sc.next(); char first = s.charAt(0); char second = s.charAt(1); int index = (first - 'a') * 25 + 1; index += second - 'a'; if (second > first) { index--; } System.out.println(index); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
f3de5f8434b41b3dc7355e52913c23e5
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 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){ String str= sc.next(); int ans = 25*(str.charAt(0)-97)+(str.charAt(1)-97); if(str.charAt(0) > str.charAt(1)){ ans++; } System.out.println(ans); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
409195beb7125f7cc801f4ea62787e90
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.lang.reflect.Array; import java.util.*; import java.lang.*; import java.io.*; import java.util.StringTokenizer; import java.util.Collections; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static long power(long x,long n) { long value=(long)(1e9+7); long res=1; while(n>0) { if(n%2!=0) res=(res*x)%value; x=(x*x)%value; n=n/2; } return res; } static boolean isPrime(long n) { if(n==1) return false; if(n==2 || 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 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 sortDescending(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static boolean Equals(ArrayList<Long> a, ArrayList<Long> b, long n) { for(int i=0;i<n;i++) { if(!b.contains(a.get(i))) return false; } for(int i=0;i<n;i++) { if(!a.contains(b.get(i))) return false; } return true; } static int[] SetBit(long n,int []arr) { int res=0; int k=0; while(n>0) { if(n%2!=0) arr[k]++; k++; n=n/2; } return arr; } static int josephus(int n, int k) { if (n == 1) return 0; else return ((josephus((n - 1), k) + k ) % n ); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static long Sum(long n) { long sum=0; while(n>0) { sum+=n%10; n/=10; } return sum; } static boolean isSubsequence(String s, String a) { int j=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)==a.charAt(j)) j++; if(j==a.length()) return true; } if(j!=a.length()) return false; return true; } static boolean chechPalind(long n) { String s=String.valueOf(n); int low=0,high=s.length()-1; while(low<high) { if(s.charAt(low)!=s.charAt(high)) return false; low++; high--; } return true; } public static void main (String[] args) throws Exception { FastReader sc = new FastReader(); FastWriter out = new FastWriter(); // Scanner sc=new Scanner(System.in); long T=sc.nextLong(); int tc=1; while(T-- >0) { String s=sc.next(); int ans=((s.charAt(0)-97)*25); if(s.charAt(1)<s.charAt(0)) ans+=s.charAt(1)-96; else ans+=s.charAt(1)-96-1; out.println(ans); } out.close(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
17fa54781dcce8a0d5f6ee60e42148b2
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class CODE_FORCES { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int size = scanner.nextInt(); String[] words = new String[size]; for (int i = 0; i < size; i++) { words[i] = scanner.next(); } for (int i = 0; i < size; i++) { if (words[i].charAt(0) > words[i].charAt(1)){ int aa = words[i].charAt(0) - 'a'; int great = words[i].charAt(1) - words[i].charAt(0); System.out.println(aa*26+great+1); }else if(words[i].charAt(0) < words[i].charAt(1)){ int great = words[i].charAt(1)-words[i].charAt(0); if(words[i].charAt(0) == 'a' ) { System.out.println(great); }else{ int aa = words[i].charAt(0) - 'a'; int tt = words[i].charAt(1) - words[i].charAt(0); System.out.println(aa*26+tt); } }else{ } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
9218901c1f3b8a2f448d536e37252b6d
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class B_1674 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String[] arr=new String[650]; int n=0; for(int i=97;i<123;i++) { for(int j=97;j<123;j++) { if(i==j) continue; arr[n]=String.valueOf((char)i)+String.valueOf((char)j); n++; } } int t=sc.nextInt(); sc.nextLine(); while(t-->0) { String s=sc.nextLine(); int low=0; int high=650; int mid=0; while(low<=high) { mid=(low+high)/2; if(arr[mid].equals(s)) break; if(arr[mid].compareTo(s)<0) low=mid; else high=mid; } System.out.println(mid+1); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
f5daf03686e77629d42016b270228254
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class B_1674 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); String[] arr=new String[650]; int n=0; for(int i=97;i<123;i++) { for(int j=97;j<123;j++) { if(i==j) continue; arr[n]=String.valueOf((char)i)+String.valueOf((char)j); n++; } } int t=sc.nextInt(); sc.nextLine(); while(t-->0) { String s=sc.nextLine(); for(int i=0;i<650;i++) { if(arr[i].equals(s)) { System.out.println(i+1); break; } } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
a0ad7f3f98af5e4fad6bb2410247fee9
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class CF_1674B { static void shinchan() { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T>0) { String s = sc.next(); int res = (s.charAt(0)-'a')*26+(s.charAt(1)-'a'); if(s.charAt(0)<=s.charAt(1)) { res-=Math.max(0, (s.charAt(0)-'a')); }else { res-=Math.max(0, (s.charAt(0)-'a')-1); } System.out.println(res); T--; } } public static void main(String[] args) { shinchan(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
72488877d016cfe091e95cb52056fea0
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class class428 { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { String s=sc.next(); int val=s.charAt(0)-'a'; int f=25*val; for(int i=0;i<26;i++) { char c=(char)('a'+i); if(c==s.charAt(0)) continue; f++; if(s.charAt(1)==c) break; } System.out.println(f); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
3ac29ed2d98b2aa3286f19e60c002a82
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.HashMap; import java.util.StringTokenizer; public class B { static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static HashMap<String, Integer> map = new HashMap<>(); static void genWords() { int i = 1; for (char c1 = 'a'; c1 <= 'z'; c1++) for (char c2 = 'a'; c2 <= 'z'; c2++) if (c1 != c2) map.put(String.valueOf(c1) + c2, i++); } public static void main(String[] args) throws IOException { genWords(); int t = sc.nextInt(); while (t-- > 0) { String s = sc.nextLine(); pw.println(map.get(s)); } pw.flush(); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public long[] nextlongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public Long[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
43539dbb3e4404851d0df62df53d1e23
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
/** * User: Jameel Sawafta * Date: 5/2/2022 * Time: 8:02 PM * ProblemLink:https://codeforces.com/contest/1674/problem/B */ import java.math.BigInteger; import java.util.ArrayList; import java.util.Random; import java.util.Arrays; import java.util.Collections; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Dictionary { static void solve() throws Exception { int t = scanInt(); while (t-- > 0) { String s = scanString(); char[] ch = s.toCharArray(); if ((int)ch[0]<(int) ch[1]) { out.println((ch[0]-97)*25+(ch[1]-97)); } else out.println(((ch[0] - 97) * 25 + (ch[1] - 97))+1); } } static int scanInt() throws IOException { return parseInt(scanString()); } static long scanLong() throws IOException { return parseLong(scanString()); } static String scanString() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; public static void main(String[] args) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); exit(1); } } // for array public static void swap(int index, int index2, int[] a) { int t = a[index2]; a[index2] = a[index]; a[index] = t; } //for Arraylist public static <T> void swap(int index, int index2, ArrayList<T> a) { T t = a.get(index2); a.set(index2, a.get(index)); a.set(index, t); } private static void addRandomNumbers(ArrayList<Integer> a, int noOfElements, int bound) { Random rand = new Random(); for (int i = 0; i < noOfElements; i++) { a.add(rand.nextInt(bound)); } } public static void printArray(ArrayList<Integer> a) { for (int i = 0; i < a.size(); i++) { Integer y = a.get(i); System.out.print(y + "\t"); } System.out.println(); } private static boolean isSorted(ArrayList<Integer> array) { for (int i = 0; i < array.size() - 1; i++) { if (array.get(i) > array.get(i + 1)) return false; } return true; } private static void copyArrays(int[] a, int start, int end, int[] temp) { for (int i = start; i <= end; i++) { temp[i - start] = a[i]; } } private static boolean isEven(int num) { return ((num & 1) == 0 ? true : false); } /* Method to check if x is power of 2*/ private static boolean isPowerOfTwo(int x) { return x != 0 && ((x & (x - 1)) == 0); } //Calculating the number of digits private static int numOfDigits(int num) { return (int) Math.floor(Math.log10(num)) + 1; } public static boolean isPrime(long n) { if (n < 2) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) return false; } return true; } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } private static void countSort(int[] arr) { int max = Arrays.stream(arr).max().getAsInt(); int min = Arrays.stream(arr).min().getAsInt(); int range = max - min + 1; int count[] = new int[range]; int output[] = new int[arr.length]; for (int i = 0; i < arr.length; i++) { count[arr[i] - min]++; } for (int i = 1; i < count.length; i++) { count[i] += count[i - 1]; } for (int i = arr.length - 1; i >= 0; i--) { output[count[arr[i] - min] - 1] = arr[i]; count[arr[i] - min]--; } for (int i = 0; i < arr.length; i++) { arr[i] = output[i]; } } private static void countSort(ArrayList<Integer> arr) { int max = Collections.max(arr); int min = Collections.min(arr); int range = max - min + 1; int count[] = new int[range]; int output[] = new int[arr.size()]; for (int i = 0; i < arr.size(); i++) { count[arr.get(i) - min]++; } for (int i = 1; i < count.length; i++) { count[i] += count[i - 1]; } for (int i = arr.size() - 1; i >= 0; i--) { output[count[arr.get(i) - min] - 1] = arr.get(i); count[arr.get(i) - min]--; } for (int i = 0; i < arr.size(); i++) { arr.set(i, output[i]); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
d0850ef43be9929309ee5ccd00d1e2bc
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Dictionary_1674B { private static Map<String,Integer> map = new HashMap<>(); private static void dict(){ int cnt = 1; String s = "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if(i!=j){ map.put(s.charAt(i)+""+s.charAt(j), cnt++); } } } // System.out.println(map); } public static void main(String[] args) { dict(); Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); while(t-->0){ String s = in.nextLine(); System.out.println(map.get(s)); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
a9f28a60ee61cf1bde519890d167f096
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Dictionary_1674B { private static Map<String,Integer> map = new HashMap<>(); private static void dict(){ int cnt = 1; String s = "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if(i!=j){ map.put(s.charAt(i)+""+s.charAt(j), cnt++); } } } // System.out.println(map); } public static void main(String[] args) { dict(); Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); while(t-->0){ String s = in.nextLine(); System.out.println(map.get(s)); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
dd0e6e0b4fafc4263605221b2b4bdf7e
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); String z = sc.nextLine(); while(t >0){ t--; String s = sc.nextLine(); char first = s.charAt(0); char second = s.charAt(1); int answer = 0; answer = (first - 'a') * 25; if(second < first){ answer += second - 'a' + 1; }else{ answer += second - 'a'; } System.out.println(answer); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
e5a07523fb15c103ca20a8f9de714ebf
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class Dictionary { public static void main(String[] args) { Scanner input = new Scanner(System.in); int test = input.nextInt(); for (int i = 0; i < test; i++) { String number = input.next(); String[] nums = number.split(""); String first = nums[0]; String second = nums[1]; if ("a".equals(first) == true) { System.out.println((int) number.charAt(1) - 97); } if ("b".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 0); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 1); } } if ("c".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 1); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 2); } } if ("d".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 2); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 3); } } if ("e".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 3); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 4); } } if ("f".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 4); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 5); } } if ("g".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 5); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 6); } } if ("h".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 6); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 7); } } if ("i".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 7); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 8); } } if ("j".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 8); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 9); } } if ("k".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 9); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 10); } } if ("l".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 10); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 11); } } if ("m".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 11); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 12); } } if ("n".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 12); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 13); } } if ("o".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 13); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 14); } } if ("p".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 14); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 15); } } if ("q".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 15); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 16); } } if ("r".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 16); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 17); } } if ("s".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 17); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 18); } } if ("t".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 18); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 19); } } if ("u".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 19); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 20); } } if ("v".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 20); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 21); } } if ("w".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 21); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 22); } } if ("x".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 22); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 23); } } if ("y".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 23); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 24); } } if ("z".equals(first) == true) { if ((int) number.charAt(0) > (int) number.charAt(1)) { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 24); } else if ((int) number.charAt(0) == (int) number.charAt(1)) { System.out.println(0); } else { System.out.println((int) ((((int) number.charAt(0)) - 97) * 26) + (number.charAt(1) - 97) - 25); } } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
705cc2eb23a80b2fb791edeed5337380
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.*; import java.lang.Math; import java.util.StringTokenizer; public class B { public static void main(String[] args) { int b, t; char a[]; String str; FastReader in = new FastReader(); t = in.nextInt(); while(t-- > 0) { a = in.next().toCharArray(); int first = a[0]; int second = a[1]; for(int i = 0; i < 26;i++) { if(first == 97 + i) { first = i; } } int ans = 0; for(int i = 0; i < 26;i++) { if(second == 97 + i) { second = i; } } if(first == 0) { System.out.println(second); continue; } if(second > first) { second -= 1; } if(first == 1) { System.out.println(26 + second); continue; } for(int i = 0; i < first;i++) { ans += 25; } System.out.println(ans + second + 1); } } 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; } String nextLine() { String str = ""; try { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
fda342e7ecf207be28df5216d8c73d2b
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayList<String> arrayList = new ArrayList<>(); scanner.nextLine(); for (int i = 0; i<n; i++) { arrayList.add(scanner.nextLine()); } ArrayList<String> vocabulary = new ArrayList<>(); int start = (int) 'a'; int finish = (int) 'z' + 1; for (int i = start; i<finish; i++) { String res = ((char)i)+""; for (int j = start; j<finish; j++) { if ((char)j != (char)i) { res+=((char)j); vocabulary.add(res); } res = ((char)i)+""; } } for (int i = 0; i<arrayList.size(); i++) { System.out.println(vocabulary.indexOf(arrayList.get(i))+1); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
980ea8880da1758051108297935a1b11
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class Dictionary { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int tests = scn.nextInt(); scn.nextLine(); for(int t=0;t<tests;t++){ String str = scn.nextLine(); int a = str.charAt(0)-96; int b = str.charAt(1) - 96; int index; if(b<=a){ index = 26*(a-1) + (b+1) - a; }else{ index = 26*(a-1) + (b-a); } System.out.println(index); } scn.close(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
ba42ab3545436aa8e6426a61696de855
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; import javax.swing.plaf.synth.SynthScrollPaneUI; public class B_Dictionary{ static Scanner sc = new Scanner(System.in); // @Harshit Maurya public static void reverse(int[] arr, int l, int r) { int d = (r - l + 1) / 2; for (int i = 0; i < d; i++) { int t = arr[l + i]; arr[l + i] = arr[r - i]; arr[r - i] = t; } } // QUICK MATHS private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } public static void main(String[] args) { solve(); } // BIT MAGIC public int getMSB(int b) { return (int)(Math.log(b) / Math.log(2)); } //HELPER FUNCTIONS private static int[] nextIntArray(int n){ int arr[]=new int[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextInt(); } return arr; } private static int[][] nextInt2DArray(int m,int n){ int arr[][]=new int[m][n]; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ arr[i][j]=sc.nextInt(); } } return arr; } private static long[] nextLongArray(int n){ long arr[]=new long[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextLong(); } return arr; } private static double[] nextDoubleArray(int n){ double arr[]=new double[n]; for(int i=0; i<n; i++){ arr[i]=sc.nextDouble(); } return arr; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static void solve() { int t = sc.nextInt(); while (t-- > 0) { String str=sc.next(); char ch1=str.charAt(0),ch2=str.charAt(1); int pow=ch1-'a'; int plus=ch2-'a'; long num=(25*pow)+(ch2<ch1?plus+1:plus); System.out.println(num); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
a2f442d5db47d144c0c1505b0f275bde
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
//package Codeforces; import java.io.*; import java.util.*; public class B { public static void main (String[] Z) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder op = new StringBuilder(); StringTokenizer stz; int T = Integer.parseInt(br.readLine()); HashMap<String, Integer> map = new HashMap<>(); char[] s = {'a', 'b'}; char first = 'a'; char second = 'b'; int count = 0; for (int i = 0 ; i < 26; i++) { for (int j = 0; j < 26; j++) { if(i == j) continue; ++count; s[0] = (char) ('a' + i); s[1] = (char) ('a' + j); map.put(String.valueOf(s), count); } } // System.out.println(map); while(T-- > 0) { String sb = br.readLine(); int ans = map.get(sb); op.append(ans); op.append("\n"); } System.out.println(op); // END OF CODE } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
caecb22bcd06ce8e1440db214df0e8cc
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.lang.Math.*; public class Main { public static void main(String[] args) throws IOException { int t = nextInt(); for (int i = 0; i < t; i++) { String s = next(); if(s.charAt(0) < s.charAt(1)) { out.println((s.charAt(0) - 'a') * 25 + s.charAt(1) - 'a'); }else{ out.println((s.charAt(0) - 'a') * 25 + s.charAt(1) - 'a' + 1); } } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static String next() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(next()); } public static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static long nextLong() throws IOException { return Long.parseLong(next()); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
0097c4870a655b446dc0c2fdaca8ef2c
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.security.KeyPair; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; /* Name of the class has to be "Main" only if the class is public. */ public class temp { abstract class sort implements Comparator<ArrayList<Integer>>{ @Override public int compare(ArrayList<Integer> a,ArrayList<Integer> b) { return a.get(0)-b.get(0); } } private static Comparator sort; public static void main (String[] args) throws Exception { FastScanner sc= new FastScanner(); int tt = sc.nextInt(); while(tt-- >0){ char arr[]=sc.next().toCharArray(); int last=arr[0]-'a'; int ans=last*25; int a=arr[0]-'a'; int b=arr[1]-'a'; if(b<a) { b++; } System.out.println(ans+b); } } public static int fac(int n) { if(n==0) return 1; return n*fac(n-1); } public static boolean palin(String res) { StringBuilder sb=new StringBuilder(res); String temp=sb.reverse().toString(); return(temp.equals(res)); } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static int gcd(int a, int b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } static class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
6296261f3f858a09f4d4dfca41bb9477
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
//package com.company; import java.io.*; import java.util.*; public class Cf_0 { private static class FastReader { InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; public FastReader(InputStream is) { this.is = is; } public int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public String nextLine() { int c = skip(); StringBuilder sb = new StringBuilder(); while (!isEndOfLine(c)) { sb.appendCodePoint(c); c = readByte(); } return sb.toString(); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } public char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public char readChar() { return (char) skip(); } public long[] readArrayL(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } private static PrintWriter pw= new PrintWriter(System.out); private static FastReader fr= new FastReader(System.in); public static void main(String[] args) throws IOException { //System.out.println((int)'a'-96); int t= fr.nextInt(); for(int i=0;i<t;i++){ String str= fr.next(); pwl(solve800(str)+""); } pw.close(); } public static int solve800(String str){ int res=0; int f=(int)str.charAt(0)-97; res=25*f; int l=(int)str.charAt(1)-96; res+=l; if(str.charAt(1)>str.charAt(0)) res--; return res; } public static long[]readLong(int n){ long []a= new long[n]; for(int it=0;it<n;it++){ a[it]=fr.nextLong(); } return a; } public static int[] readArray(int n){ int []a= new int[n]; for(int it=0;it<n;it++){ a[it]=fr.nextInt(); } return a; } public static int min(int []a){ int min=a[0]; for(int i=1;i<a.length;i++){ min=Math.min(min,a[i]); } return min; } public static int max(int []a){ int max=a[0]; for(int i=1;i<a.length;i++){ max=Math.max(max,a[i]); } return max; } public static int getDown(int n){ if(n==0) return 9; else return --n; } public static int getUp(int n){ if(n==9) return 0; else return ++n; } public static long findGCD(long a, long b) { while(b != 0) { if(a > b) { a = a - b; } else { b = b - a; } } return a; } public static long lcm(long a, long b) { return (a / findGCD(a, b)) * b; } public static void print(int []a){ StringBuilder sb= new StringBuilder(); for(int t:a){ sb.append(t+" "); } pwl(sb.toString()); } public static void pwl(String s){ pw.println(s); } public static void pw(String s){ pw.print(s); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
9d2b9d31a505bc56e031d3d280b86c1f
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { public static void main(String[] args) throws IOException { OutputStreamWriter osr = new OutputStreamWriter(System.out); PrintWriter o = new PrintWriter(osr); FastReader fr = new FastReader(); int t = fr.nextInt(); while (t--!=0) { String s = fr.next(); int x = (s.charAt(0) - 'a'); int y = (s.charAt(1) - 'a'); if(y < x) y++; o.println((x*25) + y); } o.close(); } } class FastReader { // Attributes : BufferedReader br; StringTokenizer st; // Constructor : public FastReader() { InputStreamReader isr = new InputStreamReader(System.in); br = new BufferedReader(isr); } // Operations : // #01 : public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } // #02 : public String nextLine() throws IOException { return br.readLine(); } // #03 : public int nextInt() throws IOException { return Integer.parseInt(next()); } // #04 : public long nextLong() throws IOException { return Long.parseLong(next()); } // #05 : public double nextDouble() throws IOException { return Double.parseDouble(next()); } // #06 : public int [] intArray (int size) throws IOException{ int [] arr = new int[size]; for (int i = 0 ; i < size; i++) arr[i] = nextInt(); return arr; } // #07 : public char [] charArray() throws IOException { return nextLine().toCharArray(); } } class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } static class Compare implements Comparator<Pair> { @Override public int compare(Pair o1, Pair o2) { return (o1.y - o2.y); } } } /* if(arr[i] > arr[i+1] && !lid[i] && lid[i+1]) { } * */
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
7ce38f6a193f97a4f40a06fa810beb44
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import java.io.*; public class Dictionary { public static void main(String[] args) { try { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int testcases = in.nextInt(); while (testcases-- > 0) { String s = in.next(); char[] a = s.toCharArray(); int ans = (a[0] - 'a') * 26 + a[1] - 'a' + 1; //dbg(ans); ans = a[0] < a[1] ? ans - (a[0] - 'a' + 1) : ans - (a[0] - 'a'); out.println(ans); } out.close(); } catch (Exception e) { // TODO: handle exception return; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] 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; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null; static void dbg(Object... o) { if (LOCAL) System.err.println(Arrays.deepToString(o)); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
6191087a77793c2ef67f4ce93960821b
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import java.io.*; public class B { public static void main(String[] args) throws IOException { FastReader in = new FastReader(System.in); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { String s = "abcdefghijklmnopqrstuvwxyz"; char[] ch = s.toCharArray(); // debug(ch); ArrayList<String> lst = new ArrayList<>(); for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (i == j) continue; String a = String.valueOf(ch[i]); a += ch[j]; lst.add(a); } } Collections.sort(lst); String str = in.next(); for (int i = 0; i < lst.size(); i++) { if (Objects.equals(lst.get(i), str)) { pw.println(i + 1); break; } } } pw.close(); } public static int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } static boolean checkPalindrome(String str) { int len = str.length(); for (int i = 0; i < len / 2; i++) { if (str.charAt(i) != str.charAt(len - i - 1)) return false; } return true; } public static void Sort(int[] a) { ArrayList<Integer> lst = new ArrayList<>(); for (int i : a) lst.add(i); Collections.sort(lst); for (int i = 0; i < lst.size(); i++) a[i] = lst.get(i); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } static class FastReader { InputStream is; private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; public FastReader(InputStream is) { this.is = is; } public int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } public boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } public String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public String nextLine() { int c = skip(); StringBuilder sb = new StringBuilder(); while (!isEndOfLine(c)) { sb.appendCodePoint(c); c = readByte(); } return sb.toString(); } public int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = (num << 3) + (num << 1) + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } public char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } public char readChar() { return (char) skip(); } public long[] readArrayL(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
39b27fc8268353135590952df49de4be
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Integer t = scanner.nextInt(); while (t-- > 0) { Integer sum =0; String text = scanner.next(); int firstChar = text.charAt(0); int secondChar = text.charAt(1); sum+= 25 *(firstChar - 97); if (firstChar < secondChar) sum+=(secondChar - 98); else sum+=(secondChar - 97); System.out.println(sum+1); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
8cf39da56f61fab8e1a3f2fe3fe00332
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args){ MyScanner scanner = new MyScanner(); int testCount = scanner.nextInt(); for(int testIdx = 1; testIdx <= testCount; testIdx++){ String s = scanner.nextLine(); int res = (s.charAt(0) - 'a') * 25 + (s.charAt(1) - 'a') + 1; if(s.charAt(1) - s.charAt(0) > 0){ res--; } out.println(res); } out.close(); } public static void printResult(int idx, long res){ out.println("Case #" + idx + ": " + res); } static void print1DArray(int[] arr){ for(int i = 0; i < arr.length; i++){ out.print(arr[i]); if(i < arr.length - 1){ out.print(' '); } } out.print('\n'); } static void print1DArrayList(List<Integer> arrayList){ for(int i = 0; i < arrayList.size(); i++){ out.print(arrayList.get(i)); if(i < arrayList.size() - 1){ out.print(' '); } } out.print('\n'); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
c87cb28463aac04f85c38c8a109dc544
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
//package combinatorics; import java.util.Scanner; public class CF1674B { public static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); int n = scanner.nextInt(); for(int i = 0; i < n; i++) { String str = scanner.next(); int res = (str.charAt(0) - 'a') * 26 + (str.charAt(1) - 'a'); int minus = str.charAt(0) - 'a'; if(str.charAt(1) >= str.charAt(0)) res = res - minus; else res = res - minus + 1; System.out.println(res); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
5a2ebfdf434327ca3a754c568fe357e7
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; public class Solution { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0) { char[] s = scan.next().toCharArray(); int result = (s[0] - 'a') * 25; result += (s[0] <= s[1]) ? (s[1]-'a'):(s[1]-'a' + 1); System.out.println(result); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
f0b69801617ef10d08b46fcc363863a9
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { public static void main(String [] args) { FastScanner fs=new FastScanner(); PrintWriter out=new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int t = fs.nextInt(); while(t-- > 0) { String s = fs.next(); int a = s.charAt(0)-'a'; int b = s.charAt(1)-'a'; sb.append((a > b) ? a*25+b+1 : a*25+b).append("\n"); } out.print(sb); out.close(); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long [] readArray(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()); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
5c9384602132e21c6c50d1299513e970
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class Solution { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { String s=sc.next(); char first=s.charAt(0); int initial=25*(first-97); char second=s.charAt(1); int finall=first>second?(second-97+1):(second-97); System.out.println(initial+finall); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
45c59d9fed44b7d5c9fec02b80b488cc
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; public class B_Dictionary { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); // try { // out = new PrintWriter("output.txt"); // } catch (Exception e) { // e.printStackTrace(); // } int t = in.nextInt(); while (t-- > 0) { char[] s = in.next().toCharArray(); int normal_index = ((s[0] - 'a') * 26) + s[1] - 'a' + 1; int prev_same = s[0] - 'a'; int curr_same = 0; if (s[1] > s[0]) curr_same++; out.println(normal_index - prev_same - curr_same); } // first get the normal index, which is first char(0 based index) * 26 + second char (1 based) // then we subtract prev values aa, bb,... for b series we have 1 such word i.e aa and for c we have 2 aa, bb // if for curr word say ca, a < c so no change but for cd we have to subtract cc as well out.flush(); out.close(); } } // For fast input output class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { // br = new BufferedReader(new InputStreamReader(System.in)); try { br = new BufferedReader(new FileReader("input.txt")); } 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[] readIntArray(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; } String[] readStringArray(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
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
b4ddd3cb5ad7ad50c6c5b7e274bcbb10
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); Scanner sc = new Scanner(System.in); //int test_case = Integer.parseInt(br.readLine()); long test_case = sc.nextLong(); for (long test = 0; test < test_case; test++) { String s = sc.next(); HashMap<String,Integer> hm = new HashMap<>(); int cnt = 0; for(int i=0;i<26;i++){ char one = (char) (i + 'a'); for(int j=0;j<26;j++){ if(i==j) continue; char two = (char) (j+'a'); StringBuilder s1 = new StringBuilder(); s1.append(one); s1.append(two); hm.put(s1.toString(), cnt+1); cnt++; } } if(hm.containsKey(s)){ System.out.println(hm.get(s)); }else{ System.out.println(-1); } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
d8d8366bbf231d1ca636cf2159b28264
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(new BufferedInputStream(System.in)); int t = in.nextInt(); Map<String, Integer> mp = new HashMap<>(); int count = 0; for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (('a' + i) == ('a' + j)) continue; count++; String s = (char)('a' + i) + "" + (char)('a' + j); mp.put(s, count); } } for (int i = 0; i < t; i++) { String s = in.next(); System.out.println(mp.get(s)); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
24db4570809ffd5391bed52eda4c099f
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.nio.charset.MalformedInputException; import java.util.StringTokenizer; import java.util.Arrays; public class Cv { public static void main(String[] args) { FastScanner in = new FastScanner(); Scanner sc = new Scanner(System.in); PrintWriter o = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { String s = sc.next(); char c = s.charAt(0); char f = s.charAt(1); int y = (int)c - 97; int d = (int)f - 96; if (f > c) { d--; } o.println(y * 25 + d); } o.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
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
8a8fd286c628fac5d60a4453ddda2004
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.util.Arrays; /* 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 scan = new Scanner(System.in); int T = scan.nextInt(); for(int i = 0; i < T; i++) { String s = scan.next(); char[] ch = s.toCharArray(); int first = ch[0] - 'a'; int second = ch[1] - 'a'; if(first<second) { System.out.println(25*first + second); }else{ System.out.println(25*first + second + 1); } } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
97dbc83008c57b747b558514df3d48c0
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static class Reader { BufferedReader reader= new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(""); String next() throws IOException { while (!tokenizer.hasMoreTokens() ) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { Reader sc = new Reader(); StringBuilder ans = new StringBuilder(); HashMap<String, Integer> map = new HashMap<>(); ans.append("aa"); int cnt = 1; for (int i = 0; i < 26; i++) { for (int j = 0; j < 25; j++) { char t = (char) (ans.charAt(1) + 1); ans.deleteCharAt(1); ans.append(t); if (t != ans.charAt(0)) map.put(ans.toString(), cnt++); } char t = (char) (ans.charAt(0) + 1); ans = new StringBuilder(); ans.append(t).append('a'); map.put(ans.toString(), cnt++); } int TT = sc.nextInt(); while (TT-- > 0) { System.out.println(map.get(sc.next())); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
d23d37a9bede12c23fb6cb2d064a96a6
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while (t-- > 0) { String s = sc.nextLine(); char[] ch = s.toCharArray(); int position1 = (int) ch[0] - 96; int position2 = (int) ch[1] - 96; int pos = position2; if (position1 < position2) { pos = position2-1; } System.out.println(25*(position1-1)+pos); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
a0063657787e839f18cea068b5753189
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastScanner scan = new FastScanner(); public static void main(String[] args) throws Exception { int n = scan.nextInt(); for(int i = 0; i < n; i++) { char[] x = scan.next().toCharArray(); int answer = (x[0] - 'a') * 25 + (x[1] - 'a' + 1); if(x[1] > x[0]) answer--; System.out.println(answer); } } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+" "); out.println(); } //input shenanigans /* Random stuff to try when stuck: -for combo/probability problems, expand the given form we're interested in -make everything the same then build an answer (constructive, make everything 0 then do something) -something appears in parts of 2 --> model as graph -assume a greedy then try to show why it works -find way to simplify into one variable if multiple exist -treat it like fmc (note any passing thoughts/algo that could be used so you can revisit them) -find lower and upper bounds on answer -figure out what ur trying to find and isolate it -see what observations you have and come up with more continuations -work backwards (in constructive, go from the goal to the start) -turn into prefix/suffix sum argument (often works if problem revolves around adjacent array elements) -instead of solving for answer, try solving for complement (ex, find n-(min) instead of max) -draw something -simulate a process -dont implement something unless if ur fairly confident its correct -after 3 bad submissions move on to next problem if applicable -do something instead of nothing and stay organized -write stuff down Random stuff to check when wa: -if code is way too long/cancer then reassess -switched N/M -int overflow -switched variables -wrong MOD -hardcoded edge case incorrectly Random stuff to check when tle: -continue instead of break -condition in for/while loop bad Random stuff to check when rte: -switched N/M -long to int/int overflow -division by 0 -edge case for empty list/data structure/N=1 */ public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static long totient(long n) { long result = n; for (int p = 2; p*p <= n; ++p) if (n % p == 0) { while(n%p == 0) n /= p; result -= result/p; } if (n > 1) result -= result/n; return result; /* find phi(i) from 1 to N fast O(N*loglogN) long[] arr = new long[N+1]; for(int i=1; i <= N; i++) arr[i] = i; for(int v=2; v <= N; v++) if(arr[v] == v) for(int a=v; a <= N; a+=v) arr[a] -= arr[a]/v; */ } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } //custom multiset (replace with HashMap if needed) public static void push(TreeMap<Integer, Integer> map, int k, int v) { //map[k] += v; if(!map.containsKey(k)) map.put(k, v); else map.put(k, map.get(k)+v); } public static void pull(TreeMap<Integer, Integer> map, int k, int v) { //assumes map[k] >= v //map[k] -= v int lol = map.get(k); if(lol == v) map.remove(k); else map.put(k, lol-v); } public static int[] compress(int[] arr) { ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int boof = 1; //min value for(int x: ls) if(!map.containsKey(x)) map.put(x, boof++); int[] brr = new int[arr.length]; for(int i=0; i < arr.length; i++) brr[i] = map.get(arr[i]); return brr; } public static long[][] multiply(long[][] left, long[][] right) { long MOD = 1000000007L; int N = left.length; int M = right[0].length; long[][] res = new long[N][M]; for(int a=0; a < N; a++) for(int b=0; b < M; b++) for(int c=0; c < left[0].length; c++) { res[a][b] += (left[a][c]*right[c][b])%MOD; if(res[a][b] >= MOD) res[a][b] -= MOD; } return res; } public static long[][] power(long[][] grid, long pow) { long[][] res = new long[grid.length][grid[0].length]; for(int i=0; i < res.length; i++) res[i][i] = 1L; long[][] curr = grid.clone(); while(pow > 0) { if((pow&1L) == 1L) res = multiply(curr, res); pow >>= 1; curr = multiply(curr, curr); } return res; } } class DSU { public int[] dsu; public int[] size; public DSU(int N) { dsu = new int[N+1]; size = new int[N+1]; for(int i=0; i <= N; i++) { dsu[i] = i; size[i] = 1; } } //with path compression, no find by rank public int find(int x) { return dsu[x] == x ? x : (dsu[x] = find(dsu[x])); } public void merge(int x, int y) { int fx = find(x); int fy = find(y); dsu[fx] = fy; } public void merge(int x, int y, boolean sized) { int fx = find(x); int fy = find(y); size[fy] += size[fx]; dsu[fx] = fy; } } class FenwickTree { //Binary Indexed Tree //1 indexed public int[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new int[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public int find(int i) { int res = 0; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public int find(int l, int r) { return find(r)-find(l-1); } } class SegmentTree { //Tlatoani's segment tree //iterative implementation = low constant runtime factor //range query, non lazy final int[] val; final int treeFrom; final int length; public SegmentTree(int treeFrom, int treeTo) { this.treeFrom = treeFrom; int length = treeTo - treeFrom + 1; int l; for (l = 0; (1 << l) < length; l++); val = new int[1 << (l + 1)]; this.length = 1 << l; } public void update(int index, int delta) { //replaces value int node = index - treeFrom + length; val[node] = delta; for (node >>= 1; node > 0; node >>= 1) val[node] = comb(val[node << 1], val[(node << 1) + 1]); } public int query(int from, int to) { //inclusive bounds if (to < from) return 0; //0 or 1? from += length - treeFrom; to += length - treeFrom + 1; //0 or 1? int res = 0; for (; from + (from & -from) <= to; from += from & -from) res = comb(res, val[from / (from & -from)]); for (; to - (to & -to) >= from; to -= to & -to) res = comb(res, val[(to - (to & -to)) / (to & -to)]); return res; } public int comb(int a, int b) { //change this return Math.max(a,b); } } class LazySegTree { //definitions private int NULL = -1; private int[] tree; private int[] lazy; private int length; public LazySegTree(int N) { length = N; int b; for(b=0; (1<<b) < length; b++); tree = new int[1<<(b+1)]; lazy = new int[1<<(b+1)]; } public int query(int left, int right) { //left and right are 0-indexed return get(1, 0, length-1, left, right); } private int get(int v, int currL, int currR, int L, int R) { if(L > R) return NULL; if(L <= currL && currR <= R) return tree[v]; propagate(v); int mid = (currL+currR)/2; return comb(get(v*2, currL, mid, L, Math.min(R, mid)), get(v*2+1, mid+1, currR, Math.max(L, mid+1), R)); } public void update(int left, int right, int delta) { add(1, 0, length-1, left, right, delta); } private void add(int v, int currL, int currR, int L, int R, int delta) { if(L > R) return; if(currL == L && currR == R) { //exact covering tree[v] += delta; lazy[v] += delta; return; } propagate(v); int mid = (currL+currR)/2; add(v*2, currL, mid, L, Math.min(R, mid), delta); add(v*2+1, mid+1, currR, Math.max(L, mid+1), R, delta); tree[v] = comb(tree[v*2], tree[v*2+1]); } private void propagate(int v) { //tree[v] already has lazy[v] if(lazy[v] == 0) return; tree[v*2] += lazy[v]; lazy[v*2] += lazy[v]; tree[v*2+1] += lazy[v]; lazy[v*2+1] += lazy[v]; lazy[v] = 0; } private int comb(int a, int b) { return Math.max(a,b); } } class RangeBit { //FenwickTree and RangeBit are faster than LazySegTree by constant factor final int[] value; final int[] weightedVal; public RangeBit(int treeTo) { value = new int[treeTo+2]; weightedVal = new int[treeTo+2]; } private void updateHelper(int index, int delta) { int weightedDelta = index*delta; for(int j = index; j < value.length; j += j & -j) { value[j] += delta; weightedVal[j] += weightedDelta; } } public void update(int from, int to, int delta) { updateHelper(from, delta); updateHelper(to + 1, -delta); } private int query(int to) { int res = 0; int weightedRes = 0; for (int j = to; j > 0; j -= j & -j) { res += value[j]; weightedRes += weightedVal[j]; } return ((to + 1)*res)-weightedRes; } public int query(int from, int to) { if (to < from) return 0; return query(to) - query(from - 1); } } class SparseTable { public int[] log; public int[][] table; public int N; public int K; public SparseTable(int N) { this.N = N; log = new int[N+2]; K = Integer.numberOfTrailingZeros(Integer.highestOneBit(N)); table = new int[N][K+1]; sparsywarsy(); } private void sparsywarsy() { log[1] = 0; for(int i=2; i <= N+1; i++) log[i] = log[i/2]+1; } public void lift(int[] arr) { int n = arr.length; for(int i=0; i < n; i++) table[i][0] = arr[i]; for(int j=1; j <= K; j++) for(int i=0; i + (1 << j) <= n; i++) table[i][j] = Math.min(table[i][j-1], table[i+(1 << (j - 1))][j-1]); } public int query(int L, int R) { //inclusive, 1 indexed L--; R--; int mexico = log[R-L+1]; return Math.min(table[L][mexico], table[R-(1 << mexico)+1][mexico]); } } class LCA { public int N, root; public ArrayDeque<Integer>[] edges; private int[] enter; private int[] exit; private int LOG = 17; //change this private int[][] dp; public LCA(int n, ArrayDeque<Integer>[] edges, int r) { N = n; root = r; enter = new int[N+1]; exit = new int[N+1]; dp = new int[N+1][LOG]; this.edges = edges; int[] time = new int[1]; //change to iterative dfs if N is large dfs(root, 0, time); dp[root][0] = 1; for(int b=1; b < LOG; b++) for(int v=1; v <= N; v++) dp[v][b] = dp[dp[v][b-1]][b-1]; } private void dfs(int curr, int par, int[] time) { dp[curr][0] = par; enter[curr] = ++time[0]; for(int next: edges[curr]) if(next != par) dfs(next, curr, time); exit[curr] = ++time[0]; } public int lca(int x, int y) { if(isAnc(x, y)) return x; if(isAnc(y, x)) return y; int curr = x; for(int b=LOG-1; b >= 0; b--) { int temp = dp[curr][b]; if(!isAnc(temp, y)) curr = temp; } return dp[curr][0]; } private boolean isAnc(int anc, int curr) { return enter[anc] <= enter[curr] && exit[anc] >= exit[curr]; } } class BitSet { private int CONS = 62; //safe public long[] sets; public int size; public BitSet(int N) { size = N; if(N%CONS == 0) sets = new long[N/CONS]; else sets = new long[N/CONS+1]; } public void add(int i) { int dex = i/CONS; int thing = i%CONS; sets[dex] |= (1L << thing); } public int and(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for(int i=0; i < boof; i++) res += Long.bitCount(sets[i] & oth.sets[i]); return res; } public int xor(BitSet oth) { int boof = Math.min(sets.length, oth.sets.length); int res = 0; for(int i=0; i < boof; i++) res += Long.bitCount(sets[i] ^ oth.sets[i]); return res; } } class MaxFlow { //Dinic with optimizations (see magic array in dfs function) public int N, source, sink; public ArrayList<Edge>[] edges; private int[] depth; public MaxFlow(int n, int x, int y) { N = n; source = x; sink = y; edges = new ArrayList[N+1]; for(int i=0; i <= N; i++) edges[i] = new ArrayList<Edge>(); depth = new int[N+1]; } public void addEdge(int from, int to, long cap) { Edge forward = new Edge(from, to, cap); Edge backward = new Edge(to, from, 0L); forward.residual = backward; backward.residual = forward; edges[from].add(forward); edges[to].add(backward); } public long mfmc() { long res = 0L; int[] magic = new int[N+1]; while(assignDepths()) { long flow = dfs(source, Long.MAX_VALUE/2, magic); while(flow > 0) { res += flow; flow = dfs(source, Long.MAX_VALUE/2, magic); } magic = new int[N+1]; } return res; } private boolean assignDepths() { Arrays.fill(depth, -69); ArrayDeque<Integer> q = new ArrayDeque<Integer>(); q.add(source); depth[source] = 0; while(q.size() > 0) { int curr = q.poll(); for(Edge e: edges[curr]) if(e.capacityLeft() > 0 && depth[e.to] == -69) { depth[e.to] = depth[curr]+1; q.add(e.to); } } return depth[sink] != -69; } private long dfs(int curr, long bottleneck, int[] magic) { if(curr == sink) return bottleneck; for(; magic[curr] < edges[curr].size(); magic[curr]++) { Edge e = edges[curr].get(magic[curr]); if(e.capacityLeft() > 0 && depth[e.to]-depth[curr] == 1) { long val = dfs(e.to, Math.min(bottleneck, e.capacityLeft()), magic); if(val > 0) { e.augment(val); return val; } } } return 0L; //no flow } private class Edge { public int from, to; public long flow, capacity; public Edge residual; public Edge(int f, int t, long cap) { from = f; to = t; capacity = cap; } public long capacityLeft() { return capacity-flow; } public void augment(long val) { flow += val; residual.flow -= val; } } } 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); } } public 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
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
0ea661e94c9c44009fa1608585baa1ec
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 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() { String s = s(); int ans = 0; ans += (s.charAt(0) - 'a') * 25; ans += s.charAt(1) - 'a' + 1; if (s.charAt(0) < s.charAt(1)){ ans --; } out.println(ans); } // (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) { int l = 0; int r = arr.length - 1; while (l < r) { swap(arr, l, r); l++; r--; } } 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
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
ba730f363194a6449fefd12d775f4507
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; // B -> CodeForcesProblemSet public final class B { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static int t = 1; static double epsilon = 0.00000001; static boolean[] isPrime; static int[] smallestFactorOf; @SuppressWarnings("unused") public static void main(String[] args) { t = fr.nextInt(); OUTER: for (int tc = 0; tc < t; tc++) { char[] s = fr.next().toCharArray(); int idx = 1; for (char char1 = 'a'; char1 <= 'z'; char1++) for (char char2 = 'a'; char2 <= 'z'; char2++) { if (char1 == char2) continue; if (char1 == s[0] && char2 == s[1]) { out.println(idx); continue OUTER; } idx++; } } out.close(); } static class UnionFind { // Uses weighted quick-union with path compression. private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { while (p != parent[p]) p = parent[p]; return p; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { char[] line = fr.next().toCharArray(); for (int j = 0; j < m; j++) grid[i][j] = line[j] - 48; } return grid; } } static class LCA { int[] height, first, segtree; ArrayList<Integer> euler; boolean[] visited; int n; LCA(ArrayList<Point>[] outFrom, int root) { n = outFrom.length; height = new int[n]; first = new int[n]; euler = new ArrayList<>(); visited = new boolean[n]; dfs(outFrom, root, 0); int m = euler.size(); segtree = new int[m * 4]; build(1, 0, m - 1); } void dfs(ArrayList<Point>[] outFrom, int node, int h) { visited[node] = true; height[node] = h; first[node] = euler.size(); euler.add(node); for (Point to : outFrom[node]) { if (!visited[(int) to.x]) { dfs(outFrom, (int) to.x, h + 1); euler.add(node); } } } void build(int node, int b, int e) { if (b == e) { segtree[node] = euler.get(b); } else { int mid = (b + e) / 2; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); int l = segtree[node << 1], r = segtree[node << 1 | 1]; segtree[node] = (height[l] < height[r]) ? l : r; } } int query(int node, int b, int e, int L, int R) { if (b > R || e < L) return -1; if (b >= L && e <= R) return segtree[node]; int mid = (b + e) >> 1; int left = query(node << 1, b, mid, L, R); int right = query(node << 1 | 1, mid + 1, e, L, R); if (left == -1) return right; if (right == -1) return left; return height[left] < height[right] ? left : right; } int lca(int u, int v) { int left = first[u], right = first[v]; if (left > right) { int temp = left; left = right; right = temp; } return query(1, 0, euler.size() - 1, left, right); } } static class FenwickTree { long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new long[size + 1]; } public long rsq(int ind) { assert ind > 0; long sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public long rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, long value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } static final class RedBlackCountSet { // Manual implementation of RB-BST for C++ PBDS functionality. // Modification required according to requirements. // Colors for Nodes: private static final boolean RED = true; private static final boolean BLACK = false; // Pointer to the root: private Node root; // Public constructor: public RedBlackCountSet() { root = null; } // Node class for storing key value pairs: private class Node { long key; long value; Node left, right; boolean color; long size; // Size of the subtree rooted at that node. public Node(long key, long value, boolean color) { this.key = key; this.value = value; this.left = this.right = null; this.color = color; this.size = value; } } /***** Invariant Maintenance functions: *****/ // When a temporary 4 node is formed: private Node flipColors(Node node) { node.left.color = node.right.color = BLACK; node.color = RED; return node; } // When there's a right leaning red link and it is not a 3 node: private Node rotateLeft(Node node) { Node rn = node.right; node.right = rn.left; rn.left = node; rn.color = node.color; node.color = RED; return rn; } // When there are 2 red links in a row: private Node rotateRight(Node node) { Node ln = node.left; node.left = ln.right; ln.right = node; ln.color = node.color; node.color = RED; return ln; } /***** Invariant Maintenance functions end *****/ // Public functions: public void put(long key) { root = put(root, key); root.color = BLACK; // One of the invariants. } private Node put(Node node, long key) { // Standard insertion procedure: if (node == null) return new Node(key, 1, RED); // Recursing: int cmp = Long.compare(key, node.key); if (cmp < 0) node.left = put(node.left, key); else if (cmp > 0) node.right = put(node.right, key); else node.value++; // Invariant maintenance: if (node.left != null && node.right != null) { if (node.right.color = RED && node.left.color == BLACK) node = rotateLeft(node); if (node.left.color == RED && node.left.left.color == RED) node = rotateRight(node); if (node.left.color == RED && node.right.color == RED) node = flipColors(node); } // Property maintenance: node.size = node.value + size(node.left) + size(node.right); return node; } private long size(Node node) { if (node == null) return 0; else return node.size; } public long rank(long key) { return rank(root, key); } // Modify according to requirements. private long rank(Node node, long key) { if (node == null) return 0; int cmp = Long.compare(key, node.key); if (cmp < 0) return rank(node.left, key); else if (cmp > 0) return node.value + size(node.left) + rank(node.right, key); else return node.value + size(node.left); } } static class SegmentTree { private Node[] heap; private int[] array; private int size; public SegmentTree(int[] array) { this.array = Arrays.copyOf(array, array.length); //The max size of this array is about 2 * 2 ^ log2(n) + 1 size = (int) (2 * Math.pow(2.0, Math.floor((Math.log((double) array.length) / Math.log(2.0)) + 1))); heap = new Node[size]; build(1, 0, array.length); } public int size() { return array.length; } //Initialize the Nodes of the Segment tree private void build(int v, int from, int size) { heap[v] = new Node(); heap[v].from = from; heap[v].to = from + size - 1; if (size == 1) { heap[v].sum = array[from]; heap[v].min = array[from]; } else { //Build childs build(2 * v, from, size / 2); build(2 * v + 1, from + size / 2, size - size / 2); heap[v].sum = heap[2 * v].sum + heap[2 * v + 1].sum; //min = min of the children heap[v].min = Math.min(heap[2 * v].min, heap[2 * v + 1].min); } } public int rsq(int from, int to) { return rsq(1, from, to); } private int rsq(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Sum without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return (to - from + 1) * n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].sum; } if (intersects(from, to, n.from, n.to)) { propagate(v); int leftSum = rsq(2 * v, from, to); int rightSum = rsq(2 * v + 1, from, to); return leftSum + rightSum; } return 0; } public int rMinQ(int from, int to) { return rMinQ(1, from, to); } private int rMinQ(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Min value without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].min; } if (intersects(from, to, n.from, n.to)) { propagate(v); int leftMin = rMinQ(2 * v, from, to); int rightMin = rMinQ(2 * v + 1, from, to); return Math.min(leftMin, rightMin); } return Integer.MAX_VALUE; } public void update(int from, int to, int value) { update(1, from, to, value); } private void update(int v, int from, int to, int value) { //The Node of the heap tree represents a range of the array with bounds: [n.from, n.to] Node n = heap[v]; if (contains(from, to, n.from, n.to)) { change(n, value); } if (n.size() == 1) return; if (intersects(from, to, n.from, n.to)) { propagate(v); update(2 * v, from, to, value); update(2 * v + 1, from, to, value); n.sum = heap[2 * v].sum + heap[2 * v + 1].sum; n.min = Math.min(heap[2 * v].min, heap[2 * v + 1].min); } } //Propagate temporal values to children private void propagate(int v) { Node n = heap[v]; if (n.pendingVal != null) { change(heap[2 * v], n.pendingVal); change(heap[2 * v + 1], n.pendingVal); n.pendingVal = null; //unset the pending propagation value } } //Save the temporal values that will be propagated lazily private void change(Node n, int value) { n.pendingVal = value; n.sum = n.size() * value; n.min = value; array[n.from] = value; } //Test if the range1 contains range2 private boolean contains(int from1, int to1, int from2, int to2) { return from2 >= from1 && to2 <= to1; } //check inclusive intersection, test if range1[from1, to1] intersects range2[from2, to2] private boolean intersects(int from1, int to1, int from2, int to2) { return from1 <= from2 && to1 >= from2 // (.[..)..] or (.[...]..) || from1 >= from2 && from1 <= to2; // [.(..]..) or [..(..).. } //The Node class represents a partition range of the array. static class Node { int sum; int min; //Here We store the value that will be propagated lazily Integer pendingVal = null; int from; int to; int size() { return to - from + 1; } } } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { if (super.containsKey(key)) { return super.put(key, super.get(key) + 1); } else { return super.put(key, 1); } } public Integer removeCM(T key) { Integer count = super.get(key); if (count == null) return -1; if (count == 1) return super.remove(key); else return super.put(key, super.get(key) - 1); } public Integer getCM(T key) { Integer count = super.get(key); if (count == null) return 0; return count; } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static class Point implements Comparable<Point> { long x; long y; long id; Point() { x = y = id = 0; } Point(Point p) { this.x = p.x; this.y = p.y; this.id = p.id; } Point(long a, long b, long id) { this.x = a; this.y = b; this.id = id; } Point(long a, long b) { this.x = a; this.y = b; } @Override public int compareTo(Point o) { if (this.x < o.x) return -1; if (this.x > o.x) return 1; if (this.y < o.y) return -1; if (this.y > o.y) return 1; return 0; } public boolean equals(Point that) { return this.compareTo(that) == 0; } } static int longestPrefixPalindromeLen(char[] s) { int n = s.length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) sb.append(s[i]); StringBuilder sb2 = new StringBuilder(sb); sb.append('^'); sb.append(sb2.reverse()); // out.println(sb.toString()); char[] newS = sb.toString().toCharArray(); int m = newS.length; int[] pi = piCalcKMP(newS); return pi[m - 1]; } static int KMPNumOcc(char[] text, char[] pat) { int n = text.length; int m = pat.length; char[] patPlusText = new char[n + m + 1]; for (int i = 0; i < m; i++) patPlusText[i] = pat[i]; patPlusText[m] = '^'; // Seperator for (int i = 0; i < n; i++) patPlusText[m + i] = text[i]; int[] fullPi = piCalcKMP(patPlusText); int answer = 0; for (int i = 0; i < n + m + 1; i++) if (fullPi[i] == m) answer++; return answer; } static int[] piCalcKMP(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static String toBinaryString(long num, int bits) { StringBuilder sb = new StringBuilder(Long.toBinaryString(num)); sb.reverse(); for (int i = sb.length(); i < bits; i++) sb.append('0'); return sb.reverse().toString(); } static long modDiv(long a, long b) { return mod(a * power(b, gigamod - 2, gigamod)); } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } static long hash(long i) { return (i * 2654435761L % gigamod); } static long hash2(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static int mapTo1D(int row, int col, int n, int m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static int[] mapTo2D(int idx, int n, int m) { // Inverse of what the one above does. int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } static double distance(Point p1, Point p2) { return Math.sqrt(Math.pow(p2.y-p1.y, 2) + Math.pow(p2.x-p1.x, 2)); } static Map<Integer, Integer> smolNumPrimeFactorization(int num) { if (smallestFactorOf == null) primeGenerator(200001); CountMap<Integer> fnps = new CountMap<>(); while (num != 1) { fnps.putCM(smallestFactorOf[num]); num /= smallestFactorOf[num]; } return fnps; } static HashMap<Long, Integer> primeFactorization(long num) { // Returns map of factor and its power in the number. HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static int bsearch(int[] arr, int val, int lo, int hi) { // Returns the index of the first element // larger than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) { // Returns the index of the last element // smaller than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static long factorial(long n) { if (n <= 1) return 1; long factorial = 1; for (int i = 1; i <= n; i++) factorial = mod(factorial * i); return factorial; } static long factorialInDivision(long a, long b) { if (a == b) return 1; if (b < a) { long temp = a; a = b; b = temp; } long factorial = 1; for (long i = a + 1; i <= b; i++) factorial = mod(factorial * i); return factorial; } static long nCr(long n, long r, long[] fac) { long p = 998244353; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r /*long fac[] = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;*/ return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if ((y & 1)==1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long nPr(long n, long r) { return factorialInDivision(n, n - r); } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } static boolean[] primeGenerator(int upto) { // Sieve of Eratosthenes: isPrime = new boolean[upto + 1]; smallestFactorOf = new int[upto + 1]; Arrays.fill(smallestFactorOf, 1); Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i < upto + 1; i++) if (isPrime[(int) i]) { smallestFactorOf[(int) i] = (int) i; // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) { if (isPrime[(int) j * (int) i]) { isPrime[(int) j * (int) i] = false; smallestFactorOf[(int) j * (int) i] = (int) i; } } } return isPrime; } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] <= a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static String toString(int[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(boolean[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(long[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + " "); return sb.toString(); } static String toString(char[] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) sb.append(dp[i] + ""); return sb.toString(); } static String toString(int[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(long[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(double[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static String toString(char[][] dp) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[i].length; j++) { sb.append(dp[i][j] + " "); } sb.append('\n'); } return sb.toString(); } static long mod(long a, long m) { return (a%m + m) % m; } static long mod(long num) { return (num % gigamod + gigamod) % gigamod; } } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
8ec4704540b388f65b14ba2632ce5399
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; import java.util.StringTokenizer; public class Dict implements Runnable { public static void main(String [] args) { new Thread(null, new Dict(), "whatever", 1<<26).start(); } public void run() { FastScanner scanner = new FastScanner(System.in); StringBuilder answers = new StringBuilder(); int tests = scanner.nextInt(); for (int t = 0; t < tests; t++) { char[] input = scanner.next().toCharArray(); int offset = (input[0] < input[1]) ? 0 : 1; answers.append(((input[0] - 'a') * 25) + (input[1] - 'a') + offset + "\n"); } System.out.println(answers); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
24942fd718ad7d59850faff5f1984277
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public Main() { FastScanner input = new FastScanner(System.in); StringBuilder output = new StringBuilder(); int t = input.nextInt(); for (int i = 0; i < t; i++) { String next = input.next(); char fChar = next.charAt(0); char sChar = next.charAt(1); int first = (fChar - 'a') * 26; int second = sChar - 'a'; int subtract = fChar - 'a'; if (sChar < fChar) { subtract--; } output.append(first + second - subtract + "\n"); } System.out.println(output); } public static void main(String[] args) { new Main(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner(InputStream in) { this(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next());} } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
c4669f66b8cc262a7100d24eef87563a
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.io.*; import java.util.*; /* */ public class B{ static FastReader sc=null; public static void main(String[] args) { sc=new FastReader(); int t=sc.nextInt(); for(int tt=0;tt<t;tt++) { char a[]=sc.next().toCharArray(); int pos=(a[0]-'a')*26+(a[1]-'a'); int sub=a[0]-'a'+(a[1]>a[0]?1:0); System.out.println(pos-sub+1); } } static int[] ruffleSort(int a[]) { ArrayList<Integer> al=new ArrayList<>(); for(int i:a)al.add(i); Collections.sort(al); for(int i=0;i<a.length;i++)a[i]=al.get(i); return a; } static void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String next() { while(!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); return a; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
c50099c8c4b409c63a10122d974bd52f
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; import java.io.*; 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 swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int test = t.nextInt(); while (test-- > 0) { String m = t.next(); long max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; // int n = t.nextInt(); // long[] a = new long[n]; // for (int i = 0; i < n; ++i) { // a[i] = t.nextLong(); // } long a = 0; int i = m.charAt(0)-'a'+1; int j = m.charAt(1)-'a'+1; if (i>0) a += (i-1)*25; if (i<j) { a += (j-1); } else a += j; o.println(a); } o.flush(); o.close(); } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
a36ce975e4e4bc0cb19435a6f5ee051f
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.Arrays; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); for (int itc=0; itc<tc; itc++) { String s = sc.next(); char a = s.charAt(0); char b = s.charAt(1); int t = ((a-97)*26)+(b-97); if (b>a && a>97) { t--; } if (a>98) { t-=(a-97-1); } System.out.println(t); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
e452d8d3ebe986348e90a6a087a9888c
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.*; public class dictionary{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); while(t-->0){ String s = sc.nextLine(); int sum = (s.codePointAt(0)-97)*25; if(s.codePointAt(1)<s.codePointAt(0)){ sum += s.codePointAt(1)-96; }else{ sum += s.codePointAt(1)-97; } System.out.println(sum); } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output
PASSED
7114b28fdf9f1a30b54b2a8ee1cf60ad
train_107.jsonl
1651502100
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary.
512 megabytes
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Dictionary { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); Map<Character, Integer> letters = new HashMap<>(); letters.put('a', 0); letters.put('b', 1); letters.put('c', 2); letters.put('d', 3); letters.put('e', 4); letters.put('f', 5); letters.put('g', 6); letters.put('h', 7); letters.put('i', 8); letters.put('j', 9); letters.put('k', 10); letters.put('l', 11); letters.put('m', 12); letters.put('n', 13); letters.put('o', 14); letters.put('p', 15); letters.put('q', 16); letters.put('r', 17); letters.put('s', 18); letters.put('t', 19); letters.put('u', 20); letters.put('v', 21); letters.put('w', 22); letters.put('x', 23); letters.put('y', 24); letters.put('z', 25); while(t != 0){ String word = sc.next(); Character first = word.charAt(0); Character second = word.charAt(1); int firstCharacterIndex = letters.get(first); int secondCharacterIndex = letters.get(second); int wordIndex = firstCharacterIndex * 25; if(firstCharacterIndex < secondCharacterIndex){ wordIndex += secondCharacterIndex; }else if(firstCharacterIndex > secondCharacterIndex){ wordIndex += secondCharacterIndex + 1; } System.out.println(wordIndex); t -= 1; } } }
Java
["7\n\nab\n\nac\n\naz\n\nba\n\nbc\n\nzx\n\nzy"]
2 seconds
["1\n2\n25\n26\n27\n649\n650"]
null
Java 8
standard input
[ "combinatorics", "math" ]
2e3006d663a3c7ad3781aba1e37be3ca
The first line contains one integer $$$t$$$ ($$$1 \le t \le 650$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
800
For each test case, print one integer — the index of the word $$$s$$$ in the dictionary.
standard output