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
dbdea20af8a097a2670614942332e1e5
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; import java.util.Map.Entry; public class MacroTemplates { public static void main(String hi[]) throws Exception { PrintWriter out = new PrintWriter(System.out); BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int n = Integer.parseInt(st.nextToken()); int[] a = readArr(n, infile, st); int c=0; Map<Integer,Integer> arr=new TreeMap<>(); for(int i=0;i<n;i++) { arr.put(a[i], i+1); } for(int i=0;i<n;i++) { Set<Entry<Integer,Integer>> entries=arr.entrySet(); for(Entry<Integer,Integer> entry : entries) { if(a[i]!=entry.getKey()) { if((i+1+entry.getValue()) == a[i]*entry.getKey()) { c++; } if(a[i]*entry.getKey() >= 2*n) { break; } } } } out.println(c/2); out.flush(); } } public static int[] readArr(int n, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[n]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < n; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+" "); out.println(); } public static long[] readArr2(int n, BufferedReader infile, StringTokenizer st) throws Exception { long[] arr = new long[n]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < n; i++) arr[i] = Long.parseLong(st.nextToken()); return arr; } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } } 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
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
8d62988e56f32adf629b3dfa4ac837fa
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.System.out; import java.util.*; import java.io.*; import java.math.*; import java.util.Map.Entry; public class MacroTemplates { public static void main(String hi[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int T = Integer.parseInt(st.nextToken()); StringBuilder sb = new StringBuilder(); while(T-->0) { st = new StringTokenizer(infile.readLine()); int n = Integer.parseInt(st.nextToken()); int[] a = readArr(n, infile, st); int c=0; Map<Integer,Integer> arr=new TreeMap<>(); for(int i=0;i<n;i++) { arr.put(a[i], i+1); } for(int i=0;i<n;i++) { Set<Entry<Integer,Integer>> entries=arr.entrySet(); for(Entry<Integer,Integer> entry : entries) { if(a[i]!=entry.getKey()) { if((i+1+entry.getValue()) == a[i]*entry.getKey()) { c++; } if(a[i]*entry.getKey() >= 2*n) { break; } } } } System.out.println(c/2); } } public static int[] readArr(int n, BufferedReader infile, StringTokenizer st) throws Exception { int[] arr = new int[n]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < n; i++) arr[i] = Integer.parseInt(st.nextToken()); return arr; } public static void print(int[] arr) { //for debugging only for(int x: arr) out.print(x+" "); out.println(); } public static long[] readArr2(int n, BufferedReader infile, StringTokenizer st) throws Exception { long[] arr = new long[n]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < n; i++) arr[i] = Long.parseLong(st.nextToken()); return arr; } public static boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } public static long gcd(long a, long b) { if(a > b) a = (a+b)-(b=a); if(a == 0L) return b; return gcd(b%a, a); } public static ArrayList<Integer> findDiv(int N) { //gens all divisors of N ArrayList<Integer> ls1 = new ArrayList<Integer>(); ArrayList<Integer> ls2 = new ArrayList<Integer>(); for(int i=1; i <= (int)(Math.sqrt(N)+0.00000001); i++) if(N%i == 0) { ls1.add(i); ls2.add(N/i); } Collections.reverse(ls2); for(int b: ls2) if(b != ls1.get(ls1.size()-1)) ls1.add(b); return ls1; } public static void sort(int[] arr) { //because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ls = new ArrayList<Integer>(); for(int x: arr) ls.add(x); Collections.sort(ls); for(int i=0; i < arr.length; i++) arr[i] = ls.get(i); } public static long power(long x, long y, long p) { //0^0 = 1 long res = 1L; x = x%p; while(y > 0) { if((y&1)==1) res = (res*x)%p; y >>= 1; x = (x*x)%p; } return res; } } 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
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
a51e8602ed6d875a5ae96f292b84ed70
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; import java.util.Collections; import java.util.Set; import java.math.BigInteger; import java.util.Map.Entry; public class Codeforces { static FastScanner ob = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws java.lang.Exception { int t=ob.nextInt(); while(t-->0) { int n=ob.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=ob.nextInt(); } ;int c=0; Map<Integer,Integer> arr=new TreeMap<>(); for(int i=0;i<n;i++) { arr.put(a[i], i+1); } for(int i=0;i<n;i++) { Set<Entry<Integer,Integer>> entries=arr.entrySet(); for(Entry<Integer,Integer> entry : entries) { if(a[i]!=entry.getKey()) { if((i+1+entry.getValue()) == a[i]*entry.getKey()) { c++; } if(a[i]*entry.getKey() >= 2*n) { break; } } } } out.println(c/2); out.flush(); } } static class FastScanner { 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); } } 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++]; } int nextInt() { return (int) nextLong(); } 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; } double nextDouble() { boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } double cur = nextLong(); if (c != '.') { return neg ? -cur : cur; } else { double frac = nextLong() / cnt; return neg ? -cur - frac : cur + frac; } } String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } } static void ASSERT(boolean assertion, String message) { if (!assertion) throw new AssertionError(message); } static void ASSERT(boolean assertion) { if (!assertion) throw new AssertionError(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
90ad406f8d9de866ea90d07b699a6208
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class Mainnn{ // static final File ip = new File("input.txt"); // static final File op = new File("output.txt"); // static { // try { // System.setOut(new PrintStream(op)); // System.setIn(new FileInputStream(ip)); // } catch (Exception e) { // } // // in = new InputReader(System.in); // } public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); //out.println(result); // print via PrintWriter ///////////////////////////////////////////// int test = sc.nextInt(); while(test-- != 0) { int n = sc.nextInt(); int[] a = new int[n+1]; for (int i = 1; i < n+1; i++) { a[i] = sc.nextInt(); } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = a[i] - i; j <= n; j += a[i]) { if (j >= 0) if ((long)a[i] * a[j] == (long) i + j && i < j) ans++; } } System.out.println(ans); } ///////////////////////////////////////////// // Stop writing your solution here. ------------------------------------- out.close(); } static void shuffleArrayL(long[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
c84a6972ddbb339a52c4297e2a33e3f5
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; public class PleasantPairs { private static long solve(int n, HashMap<Integer, Integer> map) { long count = 0; for (int i = 3; i < 2 * n; i++) { int ref = (int) Math.ceil(Math.sqrt(i)); for (int j = 1; j < ref; j++) { if (i % j == 0) { int f1 = map.getOrDefault(j, Integer.MAX_VALUE); int f2 = map.getOrDefault(i / j,Integer.MAX_VALUE); if ((f1 + f2) == i) count++; } } } return count; } public static void main(String[] args) throws IOException { Scanner s = new Scanner(); int t = 1; t = s.nextInt(); StringBuilder ans = new StringBuilder(); int count = 0; while (t-- > 0) { int n = s.nextInt(); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) map.put(s.nextInt(), i + 1); ans.append(solve(n, map)).append("\n"); } System.out.println(ans.toString()); } static class Scanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Scanner() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Scanner(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 long norm(long a, long MOD) { return ((a % MOD) + MOD) % MOD; } public static long msub(long a, long b, long MOD) { return norm(norm(a, MOD) - norm(b, MOD), MOD); } public static long madd(long a, long b, long MOD) { return norm(norm(a, MOD) + norm(b, MOD), MOD); } public static long mMul(long a, long b, long MOD) { return norm(norm(a, MOD) * norm(b, MOD), MOD); } public static long mDiv(long a, long b, long MOD) { return norm(norm(a, MOD) / norm(b, MOD), MOD); } public static String formattedArray(int a[]) { StringBuilder res = new StringBuilder(""); for (int e : a) res.append(e).append(" "); return res.toString().trim(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
e9e2bd84f91e23c8a544b8c8203a33ec
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; public class PleasantPairs { private static long solve(int n, HashMap<Integer, Integer> map) { long count = 0; for (int i = 3; i < 2 * n; i++) { int ref = (int) Math.ceil(Math.sqrt(i)); for (int j = 1; j < ref; j++) { if (i % j == 0) { int f1 = map.getOrDefault(j, (int)1e6); int f2 = map.getOrDefault(i / j,(int)1e6); if ((f1 + f2) == i) count++; } } } return count; } public static void main(String[] args) throws IOException { Scanner s = new Scanner(); int t = 1; t = s.nextInt(); StringBuilder ans = new StringBuilder(); int count = 0; while (t-- > 0) { int n = s.nextInt(); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) map.put(s.nextInt(), i + 1); ans.append(solve(n, map)).append("\n"); } System.out.println(ans.toString()); } static class Scanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Scanner() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Scanner(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 long norm(long a, long MOD) { return ((a % MOD) + MOD) % MOD; } public static long msub(long a, long b, long MOD) { return norm(norm(a, MOD) - norm(b, MOD), MOD); } public static long madd(long a, long b, long MOD) { return norm(norm(a, MOD) + norm(b, MOD), MOD); } public static long mMul(long a, long b, long MOD) { return norm(norm(a, MOD) * norm(b, MOD), MOD); } public static long mDiv(long a, long b, long MOD) { return norm(norm(a, MOD) / norm(b, MOD), MOD); } public static String formattedArray(int a[]) { StringBuilder res = new StringBuilder(""); for (int e : a) res.append(e).append(" "); return res.toString().trim(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
3f0d5ba79c120eb657c6138f0d54994c
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; public class PleasantPairs { private static long solve(int n, HashMap<Integer, Integer> map) { long count=0; for (int i=3; i<2*n; i++){ for( int j=1; j<=Math.sqrt(i); j++){ if( i%j==0 && i!=j*j ) { int f1= map.getOrDefault(j, (int)1e6); int f2= map.getOrDefault(i/j, (int)1e6); if( (f1 +f2) == i) count++; } } } return count; } public static void main(String[] args) throws IOException { Scanner s = new Scanner(); int t = 1; t = s.nextInt(); StringBuilder ans = new StringBuilder(); int count = 0; while (t-- > 0) { int n = s.nextInt(); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) map.put(s.nextInt(), i + 1); ans.append(solve(n, map)).append("\n"); } System.out.println(ans.toString()); } static class Scanner { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Scanner() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Scanner(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 long norm(long a, long MOD) { return ((a % MOD) + MOD) % MOD; } public static long msub(long a, long b, long MOD) { return norm(norm(a, MOD) - norm(b, MOD), MOD); } public static long madd(long a, long b, long MOD) { return norm(norm(a, MOD) + norm(b, MOD), MOD); } public static long mMul(long a, long b, long MOD) { return norm(norm(a, MOD) * norm(b, MOD), MOD); } public static long mDiv(long a, long b, long MOD) { return norm(norm(a, MOD) / norm(b, MOD), MOD); } public static String formattedArray(int a[]) { StringBuilder res = new StringBuilder(""); for (int e : a) res.append(e).append(" "); return res.toString().trim(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
a09ad8f97fca838e337e3dfcbbc19652
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.lang.*; public class App { public static long Solution(int n, int[] arr) { long ans = 0; HashMap<Integer, Integer> m = new HashMap<>(); for (int i = 0; i < n; i++) { m.put(arr[i], i + 1); } for (int i = 3; i < 2*n; i++) { for (int j = 1; j <= Math.sqrt(i); j++) { if ((i % j == 0) && m.containsKey(j)) { int e = i / j; if (m.containsKey(e)) { int pos1 = m.get(j); int pos2 = m.get(e); if (pos1 + pos2 == i && pos1 != pos2) ans++; } } } } return ans; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); long ans = Solution(n, arr); System.out.println(ans); } sc.close(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
202df22028c4b974a804d099eeacda4e
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
/* JAI MATA DI */ import java.util.*; import javax.print.attribute.HashAttributeSet; import java.io.*; import java.math.BigInteger; import java.sql.Array; public class Main { static class FR{ BufferedReader br; StringTokenizer st; public FR() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long mod = (long)(1e9 + 7); static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static int UB(long[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(long[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static int UB(int[] arr , long find , int l , int r) { while(l<=r) { int m = (l+r)/2; if(arr[m]<find) l = m+1; else r = m-1; } return l; } static int LB(int[] arr , long find,int l ,int r ) { while(l<=r) { int m = (l+r)/2; if(arr[m] > find) r = m-1; else l = m+1; } return r; } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long[][] ncr(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%mod; } } return C; } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) { int tc = 1; tc = sc.nextInt(); while(tc-->0) { TEST_CASE(); } System.out.println(sb); } static void TEST_CASE() { int n = sc.nextInt(); long[] arr = new long[n+1]; int[] ind = new int[2*n+1]; for(int i = 1 ; i<=n ; i++) { arr[i] = sc.nextLong(); ind[(int)arr[i]] = i; } // System.out.println(ind[6] +" "+ind[1]+" "+ind[5]); long tot = 0; for(long i = 1 ; i<=n ; i++) { long v = (arr[(int)i] - i%arr[(int)i]); while(v <= i) { v += arr[(int)i] ; } // System.out.println(i+" "+v); for(long j = v ; j<=n ;j += arr[(int)i] ) { if(arr[(int)i]*arr[(int)j] == i+j) { // System.out.println(i+" "+j); tot++; } } } sb.append(tot+"\n"); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
4de6bd8729b0a568955e27cdb812fb90
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); PrintWriter writer = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); long p = (long)2*(long)n - 1l; long maxx = (long) Math.sqrt(p)+1; Integer arr[] = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int ans = 0; int pairs = 0; for(int i = 0; i < n ; i++) { if(arr[i] <= maxx) { for (int j = 0; j < n; j++) { if(i==j)continue; if(i+j+2 == arr[i]*arr[j]) { ans++; if(arr[i] <= maxx && arr[j] <= maxx)pairs++; } } } } // writer.println(pairs); writer.println(ans-pairs/2); } writer.flush(); writer.close(); } private static long power (long a, long n, long p) { long res = 1; while(n!=0) { if(n%2==1) { res=(res*a)%p; n--; }else { a= (a*a)%p; n/=2; } } return res; } private static boolean isPrime(int c) { for (int i = 2; i*i <= c; i++) { if(c%i==0)return false; } return true; } private static int find(int a , int arr[]) { if(arr[a] != a) return arr[a] = find(arr[a], arr); return arr[a]; } private static void union(int a, int b, int arr[]) { int aa = find(a,arr); int bb = find(b, arr); arr[aa] = bb; } private static long gcd(long a, long b) { if(a==0)return b; return gcd(b%a, a); } public static int[] readIntArray(int size, FastReader s) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = s.nextInt(); } return array; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } class Pair implements Comparable<Pair>{ long a; long b; Pair(long a, long b){ this.a = a; this.b = b; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null || obj.getClass()!= this.getClass()) return false; Pair pair = (Pair) obj; return (pair.a == this.a && pair.b == this.b); } @Override public int hashCode() { return Objects.hash(a,b); } @Override public int compareTo(Pair o) { if(this.a == o.a) { return Long.compare(this.b, o.b); }else { return Long.compare(this.a, o.a); } } @Override public String toString() { return this.a + " " + this.b; } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
1df46fae39cce1cee1f2aa24da8ace3a
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private FastWriter wr; private Reader rd; public final int MOD = 1000000007; /************************************************** FAST INPUT IMPLEMENTATION *********************************************/ class Reader { BufferedReader br; StringTokenizer st; public Reader() { br = new BufferedReader( new InputStreamReader(System.in)); } public Reader(String path) throws FileNotFoundException { br = new BufferedReader( new InputStreamReader(new FileInputStream(path))); } 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 int ni() throws IOException { return rd.nextInt(); } public long nl() throws IOException { return rd.nextLong(); } public void yOrn(boolean flag){ if(flag){ wr.println("YES"); }else { wr.println("NO"); } } char nc() throws IOException { return rd.next().charAt(0); } public String ns() throws IOException { return rd.nextLine(); } public Double nd() throws IOException { return rd.nextDouble(); } public int[] nai(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } public long[] nal(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nl(); } return arr; } /************************************************** FAST OUTPUT IMPLEMENTATION *********************************************/ public static class FastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastWriter() { out = null; } public FastWriter(OutputStream os) { this.out = os; } public FastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastWriter write(char c) { return write((byte) c); } public FastWriter write(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastWriter write(int x) { if (x == Integer.MIN_VALUE) { return write((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastWriter write(double x, int precision) { if (x < 0) { write('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } write((long) x).write("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; write((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastWriter writeln(char c) { return write(c).writeln(); } public FastWriter writeln(int x) { return write(x).writeln(); } public FastWriter writeln(long x) { return write(x).writeln(); } public FastWriter writeln(double x, int precision) { return write(x, precision).writeln(); } public FastWriter write(int... xs) { boolean first = true; for (int x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter write(long... xs) { boolean first = true; for (long x : xs) { if (!first) write(' '); first = false; write(x); } return this; } public FastWriter writeln() { return write((byte) '\n'); } public FastWriter writeln(int... xs) { return write(xs).writeln(); } public FastWriter writeln(long... xs) { return write(xs).writeln(); } public FastWriter writeln(char[] line) { return write(line).writeln(); } public FastWriter writeln(char[]... map) { for (char[] line : map) write(line).writeln(); return this; } public FastWriter writeln(String s) { return write(s).writeln(); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } public FastWriter print(byte b) { return write(b); } public FastWriter print(char c) { return write(c); } public FastWriter print(char[] s) { return write(s); } public FastWriter print(String s) { return write(s); } public FastWriter print(int x) { return write(x); } public FastWriter print(long x) { return write(x); } public FastWriter print(double x, int precision) { return write(x, precision); } public FastWriter println(char c) { return writeln(c); } public FastWriter println(int x) { return writeln(x); } public FastWriter println(long x) { return writeln(x); } public FastWriter println(double x, int precision) { return writeln(x, precision); } public FastWriter print(int... xs) { return write(xs); } public FastWriter print(long... xs) { return write(xs); } public FastWriter println(int... xs) { return writeln(xs); } public FastWriter println(long... xs) { return writeln(xs); } public FastWriter println(char[] line) { return writeln(line); } public FastWriter println(char[]... map) { return writeln(map); } public FastWriter println(String s) { return writeln(s); } public FastWriter println() { return writeln(); } } /********************************************************* USEFUL CODE **************************************************/ boolean[] SAPrimeGenerator(int n) { // TC-N*LOG(LOG N) //Create Prime Marking Array and fill it with true value boolean[] primeMarker = new boolean[n + 1]; Arrays.fill(primeMarker, true); primeMarker[0] = false; primeMarker[1] = false; for (int i = 2; i <= n; i++) { if (primeMarker[i]) { // we start from 2*i because i*1 must be prime for (int j = 2 * i; j <= n; j += i) { primeMarker[j] = false; } } } return primeMarker; } private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } class Pair<F, S> { private F first; private S second; Pair(F first, S second) { this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } @Override public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<F, S> pair = (Pair<F, S>) o; return first == pair.first && second == pair.second; } @Override public int hashCode() { return Objects.hash(first, second); } } class PairThree<F, S, X> { private F first; private S second; private X third; PairThree(F first, S second,X third) { this.first = first; this.second = second; this.third=third; } public F getFirst() { return first; } public void setFirst(F first) { this.first = first; } public S getSecond() { return second; } public void setSecond(S second) { this.second = second; } public X getThird() { return third; } public void setThird(X third) { this.third = third; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairThree<?, ?, ?> pair = (PairThree<?, ?, ?>) o; return first.equals(pair.first) && second.equals(pair.second) && third.equals(pair.third); } @Override public int hashCode() { return Objects.hash(first, second, third); } } public static void main(String[] args) throws IOException { new Main().run(); } public void run() throws IOException { if (oj) { rd = new Reader(); wr = new FastWriter(System.out); } else { File input = new File("input.txt"); File output = new File("output.txt"); if (input.exists() && output.exists()) { rd = new Reader(input.getPath()); wr = new FastWriter(output.getPath()); } else { rd = new Reader(); wr = new FastWriter(System.out); oj = true; } } long s = System.currentTimeMillis(); solve(); wr.flush(); tr(System.currentTimeMillis() - s + "ms"); } /*************************************************************************************************************************** *********************************************************** MAIN CODE ****************************************************** ****************************************************************************************************************************/ boolean[] sieve; public void solve() throws IOException { int t = 1; t = ni(); while (t-- > 0) { go(); } } /********************************************************* MAIN LOGIC HERE ****************************************************/ long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } public void go() throws IOException { int n=ni(); long[] arr=new long[n+1]; for(int i=1;i<=n;i++){ arr[i]=ni(); } long ans=0; for(int i=1;i<=n;i++) { for(int j=(int)arr[i]-i;j<=n;j+=arr[i]) { if(j>0) { if(((long)arr[i]*arr[j]==(i+j)) && (j>i)) ans++; } } } wr.println(ans); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
c74a3a378f98a1eb54670b68bbd74b6f
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
//package com.company; import java.util.*; public class Main2 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n = sc.nextInt(); for(int i=0;i<n;i++){ int k= sc.nextInt(); int[] arr = new int[k+1]; for(int j =1;j<=k;j++){ arr[j]= sc.nextInt(); } int max=0; for(int l=1;l<=k;l++){ // int h=arr[l]; // int mu=0; // if(arr[l]<l){ // mu=2*arr[l]-l; // } // else mu=arr[l]-l; for(int j=arr[l]-l;j<=k;j+=arr[l]){ if(j>=0) if( l<j && 1.0* arr[l]==1.0*(l+j)/arr[j]){ // System.out.println(l+ " "+j); max++; } } } System.out.println(max); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
f573c19e956c9a617ebab39612b8b314
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
//package Contest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Codeforces { public static void main(String[] args) throws IOException { FastIO io = new FastIO(); int t = io.nextInt(); while(t--!=0){ int n = io.nextInt(); long[] a = new long[n+1]; for(int i=1; i<=n; i++){ a[i] = io.nextInt(); } int ans = 0; for(int i=1; i<=n; i++){ for(int j = (int) (a[i]-i); j<=n; j += a[i]){ if(j>=0 && (a[i]*a[j] == i+j) && i<j){ ans++; } } } System.out.println(ans); } } public static class Pair implements Comparable<Pair>{ int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } public Pair() { } @Override public int compareTo(Pair o) { return this.a - o.a; } } static class FastIO { BufferedReader br; StringTokenizer st; FastIO() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray(int n){ int[] list = new int[n]; for (int i=0; i<n; i++) list[i] = nextInt(); return list; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
2e303ca2e32e1ea72477119bdae48dc6
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
/* You are given an array a1,a2,…,an consisting of n distinct integers. Count the number of pairs of indices (i,j) such that i<j and ai⋅aj=i+j. */ // 1 based indexing import java.io.*; import java.util.*; public class PleasantPairs { /* Brute force: 1. Pair cannot be less than 3 (as first index 1 and second index 2 = 3) useless observation 2. Pair cannot be more than n + n-1, sum of last two indices So say we have a[1] as 3, then our pair can be 3,6,9 etc for 3 our j has to be 2, for 6 it has to be 5, for 9 it has to be 8 and so on i.e. We are increasing our j by a[i] So 1. For every index i, 2. Start j from nums[i]-i and increment by nums[i] 3. If i < j and i+j == nums[i] * nums[j] increment count */ public static int countPairs(int[] nums, int n) { int count = 0; for(int i = 1; i < n; i++) { // less than n as we need a j > i to exist to form a pair for(int j = nums[i] - i; j <= n; j += nums[i]) { if((i < j) && ((long)nums[i] * (long)nums[j] == (long)i + (long)j)) { count++; } } } return count; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter wr = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); for(int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); int[] nums = new int[n+1]; // 1 based indexing StringTokenizer st = new StringTokenizer(br.readLine()); for(int j = 1; j <= n; j++) { // nums[0] is 0 (not under consideration) nums[j] = Integer.parseInt(st.nextToken()); } wr.write(String.valueOf(countPairs(nums, n))+"\n"); } wr.flush(); wr.close(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
b207e8dbae8b52fa9164d101d0f7ded8
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
/* You are given an array a1,a2,…,an consisting of n distinct integers. Count the number of pairs of indices (i,j) such that i<j and ai⋅aj=i+j. */ // 1 based indexing import java.io.*; import java.util.*; public class PleasantPairs { /* Brute force: 1. Pair cannot be less than 3 (as first index 1 and second index 2 = 3) useless observation 2. Pair cannot be more than n + n-1, sum of last two indices So say we have a[1] as 3, then our pair can be 3,6,9 etc for 3 our j has to be 2, for 6 it has to be 5, for 9 it has to be 8 and so on i.e. We are increasing our j by a[i] So 1. For every index i, 2. Start j from nums[i]-i and increment by nums[i] 3. If i < j and i+j == nums[i] * nums[j] increment count */ public static int countPairs(int[] nums, int n) { int count = 0; for(int i = 1; i <= n; i++) { for(int j = nums[i] - i; j <= n; j += nums[i]) { if((i < j) && ((long)nums[i] * (long)nums[j] == (long)i + (long)j)) { count++; } } } return count; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter wr = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); for(int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); int[] nums = new int[n+1]; // 1 based indexing StringTokenizer st = new StringTokenizer(br.readLine()); for(int j = 1; j <= n; j++) { // nums[0] is 0 (not under consideration) nums[j] = Integer.parseInt(st.nextToken()); } wr.write(String.valueOf(countPairs(nums, n))+"\n"); } wr.flush(); wr.close(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
87924088b1940fddaf35dd5367ef8dd4
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; //import java.util.Stack; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static class obj{ int a1; int a2; obj(int a1,int a2){ this.a1=a1; this.a2=a2; } } static class sortby implements Comparator<obj>{ public int compare(obj o1,obj o2){ return o1.a1>o2.a1?1:-1; } } public static void main(String[] args) { FastReader s = new FastReader(); //InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); //Scanner s=new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n=s.nextInt(); long[] a=new long[n]; HashMap<Long,Integer> m=new HashMap<>(); for(int i=0;i<n;i++){ a[i]=s.nextInt(); m.put(a[i],i); } int c=0; for(int i=0;i<n;i++){ for(long y=1;a[i]*y<=2*n;y++){ long k=y*a[i]; if(y!=a[i]&&m.containsKey(y)&&k==(long)(i+1+m.get(y)+1)){ c++; } } } out.println(c/2); } out.close(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
671e4dbc16f8ee1dcd705e1a54f0824e
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; /* * * */ public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); int a[] = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = sc.nextInt(); } long count = 0; for (int i = 1; i < n; i++) { for (int j = a[i] - i; j <= n; j += a[i]) { if (j >= 1) { if (((long) a[i] * a[j] == i + j) && (i < j)) { count++; } } } } System.out.println(count); t--; } sc.close(); } /* * * */ static long gcd(long a, long b) { if(a == 0)return b; return gcd(b%a,a); } static long lcm(long a, long b) { long g = gcd(a,b); return (a*b)/g; } /* * * */ }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
ca0939d4685ea19770cfcab2f63ade56
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; @SuppressWarnings("unused") public class Round728 { 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 Pair { long index, value; public Pair(long index, long value) { this.value = value; this.index = index; } } //B. Pleasant Pairs public static void PleasantPairs() throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(in.readLine()); for(int m=0; m<t; m++) { int n = Integer.parseInt(in.readLine()); String input[] = in.readLine().split(" "); Pair arr[] = new Pair[n]; for(int i=0; i<n; i++) arr[i] = new Pair(i+1, Long.parseLong(input[i])); Arrays.sort(arr, (p1, p2) -> Long.compare(p1.value, p2.value)); int count = 0; for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) { long val = arr[i].value * arr[j].value; long ind = arr[i].index + arr[j].index; if(val == ind) count++; if(val > 2*n) break; } } System.out.println(count); } } public static void main(String[] args) throws NumberFormatException, IOException { PleasantPairs(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
633bf9fd33681648f62ba36fc43de6a5
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class javaTemplate { public static void main(String[] args) { FastScanner fs = new FastScanner(); StringBuilder output = new StringBuilder(); int t = fs.nextInt(); for (int tt = 0; tt < t; tt += 1) { int n = fs.nextInt(); ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(0); for (int i = 1; i <= n; i += 1) { arr.add(fs.nextInt()); } int ans = 0; ArrayList<ArrayList<Integer>> find = new ArrayList<ArrayList<Integer>>(); for (int i = 1; i <= n; i += 1) { int multi = 1; while (true) { int num = Math.abs((arr.get(i) * multi) - i); if (num <= n) { if (num > i) { ArrayList<Integer> temp = new ArrayList<Integer>(); temp.add(multi); temp.add(num); find.add(temp); } } else { break; } multi += 1; } } for (int i = 0; i < find.size(); i += 1) { int index = find.get(i).get(1); int num = find.get(i).get(0); if (arr.get(index) == num) { ans += 1; } } output.append(ans).append('\n'); } System.out.println(output.toString()); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
bee78c63225d055ffb7056a7d75c283a
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class javaTemplate { public static void main(String[] args) { FastScanner fs = new FastScanner(); int t = fs.nextInt(); StringBuilder allAns = new StringBuilder(); for (int tt = 0; tt < t; tt += 1) { int n = fs.nextInt(); ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(0); for (int i = 1; i <= n; i += 1) { arr.add(fs.nextInt()); } int ans = 0; ArrayList<ArrayList<Integer>> find = new ArrayList<ArrayList<Integer>>(); for (int i = 1; i <= n; i += 1) { int multi = 1; while (true) { int num = Math.abs((arr.get(i) * multi) - i); if (num <= n) { if (num > i) { ArrayList<Integer> temp = new ArrayList<Integer>(); temp.add(multi); temp.add(num); find.add(temp); } } else { break; } multi += 1; } } for (int i = 0; i < find.size(); i += 1) { // System.out.println(find.get(i)); int index = find.get(i).get(1); int num = find.get(i).get(0); if (arr.get(index) == num) { ans += 1; } } allAns.append(ans).append('\n'); } System.out.println(allAns); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
b7b032f8f077e805528c8ed9259e24fd
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-- > 0) { int n = scn.nextInt(); long [] arr = new long[n + 1]; for(int i = 1; i < n + 1; i++) arr[i] = scn.nextInt(); arr[0] = 0; int count = 0; for(int i = 1; i < arr.length; i++) { for(int j = (int)arr[i] - i; j < arr.length; j += arr[i]) { if(j >= 0) if(arr[i]*arr[j] == i + j && i < j) count++; } } System.out.println(count); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
5747277b320b929a7457cbfcc373fd99
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.io.*; public class cf { static Reader sc = new Reader(); static PrintWriter out = new PrintWriter(System.out); static int mod = (int) 1e9 + 7; public static void main(String[] args) { int t = sc.ni(); while (t-- > 0) { int n = sc.ni(); int[] index = new int[2 * n + 1]; int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.ni(); index[arr[i]] = i + 1; } sort(arr); long res = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if ((long) arr[i] * arr[j] > 2 * n) break; if (index[arr[i]] + index[arr[j]] == (long) arr[i] * arr[j]) res++; } } out.println(res); } out.close(); } public static long pow(long a, long b, long m) { if (b == 0) { return 1; } if (b % 2 == 0) { return pow((a * a) % m, b / 2, m) % m; } else { return (a * pow((a * a) % m, b / 2, m)) % m; } } // snippets static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (this.x > o.x) return 1; else if (this.x < o.x) return -1; else { if (this.y > o.y) return 1; else if (this.y < o.y) return -1; else return 0; } } public int hashCode() { int ans = 1; ans = x * 31 + y * 13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair) o; if (this.x == other.x && this.y == other.y) { return true; } return false; } } static void add_map(HashMap<Integer, Integer> map, int v) { if (!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v) + 1); } } static void remove_map(HashMap<Integer, Integer> map, int v) { if (map.containsKey(v)) { map.put(v, map.get(v) - 1); if (map.get(v) == 0) map.remove(v); } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(int[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static void sort(long[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); long tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq = Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq = Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static void print_arr(int A[]) { for (int i : A) { System.out.print(i + " "); } System.out.println(); } static void print_arr(long A[]) { for (long i : A) { System.out.print(i + " "); } System.out.println(); } static int min(int a, int b) { return Math.min(a, b); } static int min(int a, int b, int c) { return Math.min(a, Math.min(b, c)); } static int min(int a, int b, int c, int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a, int b) { return Math.max(a, b); } static int max(int a, int b, int c) { return Math.max(a, Math.max(b, c)); } static int max(int a, int b, int c, int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a, long b) { return Math.min(a, b); } static long min(long a, long b, long c) { return Math.min(a, Math.min(b, c)); } static long min(long a, long b, long c, long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a, long b) { return Math.max(a, b); } static long max(long a, long b, long c) { return Math.max(a, Math.max(b, c)); } static long max(long a, long b, long c, long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static int max(int A[]) { int max = Integer.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static int min(int A[]) { int min = Integer.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } static long max(long A[]) { long max = Long.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static long min(long A[]) { long min = Long.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } 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 ArrayList<Integer> sieve(int n) { ArrayList<Integer> primes = new ArrayList<>(); boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } } for (int i = 2; i < n; i++) { if (isPrime[i]) primes.add(i); } return primes; } static class Reader { BufferedReader br; StringTokenizer st; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } Reader(File f) throws FileNotFoundException { br = new BufferedReader(new FileReader(f)); st = new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nai(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
dc7fd32482303ca6689c4c2805dc33be
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.io.*; public class cf { static Reader sc = new Reader(); static PrintWriter out = new PrintWriter(System.out); static int mod = (int) 1e9 + 7; public static void main(String[] args) { int t = sc.ni(); while (t-- > 0) { int n = sc.ni(); int[] index = new int[2 * n + 1]; for (int i = 1; i <= n; i++) { int e = sc.ni(); index[e] = i; } long res = 0; // ai*aj=i+j for (int i = 1; i <= 2 * n; i++) { // fix ai if (index[i] == 0) continue; for (int j = i; j <= 2 * n; j += i) { // j= ai*aj if (index[j / i] == 0 || i == (j / i)) continue; if (index[i] + index[j / i] == j) { res++; } } } out.println(res / 2); } out.close(); } public static long pow(long a, long b, long m) { if (b == 0) { return 1; } if (b % 2 == 0) { return pow((a * a) % m, b / 2, m) % m; } else { return (a * pow((a * a) % m, b / 2, m)) % m; } } // snippets static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (this.x > o.x) return 1; else if (this.x < o.x) return -1; else { if (this.y > o.y) return 1; else if (this.y < o.y) return -1; else return 0; } } public int hashCode() { int ans = 1; ans = x * 31 + y * 13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair) o; if (this.x == other.x && this.y == other.y) { return true; } return false; } } static void add_map(HashMap<Integer, Integer> map, int v) { if (!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v) + 1); } } static void remove_map(HashMap<Integer, Integer> map, int v) { if (map.containsKey(v)) { map.put(v, map.get(v) - 1); if (map.get(v) == 0) map.remove(v); } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(int[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static void sort(long[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); long tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq = Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq = Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static void print_arr(int A[]) { for (int i : A) { System.out.print(i + " "); } System.out.println(); } static void print_arr(long A[]) { for (long i : A) { System.out.print(i + " "); } System.out.println(); } static int min(int a, int b) { return Math.min(a, b); } static int min(int a, int b, int c) { return Math.min(a, Math.min(b, c)); } static int min(int a, int b, int c, int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a, int b) { return Math.max(a, b); } static int max(int a, int b, int c) { return Math.max(a, Math.max(b, c)); } static int max(int a, int b, int c, int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a, long b) { return Math.min(a, b); } static long min(long a, long b, long c) { return Math.min(a, Math.min(b, c)); } static long min(long a, long b, long c, long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a, long b) { return Math.max(a, b); } static long max(long a, long b, long c) { return Math.max(a, Math.max(b, c)); } static long max(long a, long b, long c, long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static int max(int A[]) { int max = Integer.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static int min(int A[]) { int min = Integer.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } static long max(long A[]) { long max = Long.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static long min(long A[]) { long min = Long.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } 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 ArrayList<Integer> sieve(int n) { ArrayList<Integer> primes = new ArrayList<>(); boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } } for (int i = 2; i < n; i++) { if (isPrime[i]) primes.add(i); } return primes; } static class Reader { BufferedReader br; StringTokenizer st; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } Reader(File f) throws FileNotFoundException { br = new BufferedReader(new FileReader(f)); st = new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nai(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
6a49e3ff1aae02d130fdd825ba6a0772
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.io.*; public class cf { static Reader sc = new Reader(); static PrintWriter out = new PrintWriter(System.out); static int mod = (int) 1e9 + 7; public static void main(String[] args) { int t = sc.ni(); while (t-- > 0) { int n = sc.ni(); int[] index = new int[2 * n + 1]; for (int i = 1; i <= n; i++) { int e = sc.ni(); index[e] = i; } long res = 0; for (int i = 1; i <= 2 * n; i++) { if (index[i] == 0) continue; for (int j = i; j <= 2 * n; j += i) { if (index[j / i] == 0 || i == (j / i)) continue; if (index[i] + index[j / i] == j) { res++; } } } out.println(res / 2); } out.close(); } public static long pow(long a, long b, long m) { if (b == 0) { return 1; } if (b % 2 == 0) { return pow((a * a) % m, b / 2, m) % m; } else { return (a * pow((a * a) % m, b / 2, m)) % m; } } // snippets static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (this.x > o.x) return 1; else if (this.x < o.x) return -1; else { if (this.y > o.y) return 1; else if (this.y < o.y) return -1; else return 0; } } public int hashCode() { int ans = 1; ans = x * 31 + y * 13; return ans; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair) o; if (this.x == other.x && this.y == other.y) { return true; } return false; } } static void add_map(HashMap<Integer, Integer> map, int v) { if (!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v) + 1); } } static void remove_map(HashMap<Integer, Integer> map, int v) { if (map.containsKey(v)) { map.put(v, map.get(v) - 1); if (map.get(v) == 0) map.remove(v); } } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(int[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static void sort(long[] arr) { // shuffle Random rand = new Random(); for (int i = 0; i < arr.length; i++) { int j = rand.nextInt(i + 1); long tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq = Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq = Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static void print_arr(int A[]) { for (int i : A) { System.out.print(i + " "); } System.out.println(); } static void print_arr(long A[]) { for (long i : A) { System.out.print(i + " "); } System.out.println(); } static int min(int a, int b) { return Math.min(a, b); } static int min(int a, int b, int c) { return Math.min(a, Math.min(b, c)); } static int min(int a, int b, int c, int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a, int b) { return Math.max(a, b); } static int max(int a, int b, int c) { return Math.max(a, Math.max(b, c)); } static int max(int a, int b, int c, int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a, long b) { return Math.min(a, b); } static long min(long a, long b, long c) { return Math.min(a, Math.min(b, c)); } static long min(long a, long b, long c, long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a, long b) { return Math.max(a, b); } static long max(long a, long b, long c) { return Math.max(a, Math.max(b, c)); } static long max(long a, long b, long c, long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static int max(int A[]) { int max = Integer.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static int min(int A[]) { int min = Integer.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } static long max(long A[]) { long max = Long.MIN_VALUE; for (int i = 0; i < A.length; i++) { max = Math.max(max, A[i]); } return max; } static long min(long A[]) { long min = Long.MAX_VALUE; for (int i = 0; i < A.length; i++) { min = Math.min(min, A[i]); } return min; } 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 ArrayList<Integer> sieve(int n) { ArrayList<Integer> primes = new ArrayList<>(); boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } } for (int i = 2; i < n; i++) { if (isPrime[i]) primes.add(i); } return primes; } static class Reader { BufferedReader br; StringTokenizer st; Reader() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } Reader(File f) throws FileNotFoundException { br = new BufferedReader(new FileReader(f)); st = new StringTokenizer(""); } String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] nai(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
9ee0d80c33691f1e47c2faaab872ac51
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; // Sachin_2961 submission // public class CodeforcesA { public void solve() { int n = fs.nInt(); int[]ar = new int[n]; int[]count = new int[2*n+1]; for(int i=0;i<n;i++){ ar[i] = fs.nInt(); } for(int i=0;i<n;i++) count[ar[i]] = i+1; int ans = 0; for(int v = 1;v<=2*n;v++){ if(count[v] > 0 ){ for(int x=v;x<=2*n;x+=v){ if(count[x/v] > 0 && count[v]+count[x/v] == x && v != x/v){ ans++; } } } } ans /= 2; out.println(ans); } static boolean multipleTestCase = true; static FastScanner fs; static PrintWriter out; public void run(){ fs = new FastScanner(); out = new PrintWriter(System.out); int tc = (multipleTestCase)?fs.nInt():1; while (tc-->0)solve(); out.flush(); out.close(); } public static void main(String[]args){ try{ new CodeforcesA().run(); }catch (Exception e){ e.printStackTrace(); } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String n() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String Line() { String str = ""; try { str = br.readLine(); }catch (IOException e) { e.printStackTrace(); } return str; } int nInt() {return Integer.parseInt(n()); } long nLong() {return Long.parseLong(n());} int[]aI(int n){ int[]ar = new int[n]; for(int i=0;i<n;i++) ar[i] = nInt(); return ar; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
e0d1d289d1a9b81590714aff7b4a5bd7
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import javax.swing.*; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; public class Main { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st=new StreamTokenizer(br); static PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out), true); static int nextInt() throws IOException { st.nextToken(); return (int)st.nval; } static String nextStr() throws IOException { st.nextToken(); return st.sval; } static int brReadInt() throws IOException { return Integer.parseInt(br.readLine()); } static long brReadLong() throws IOException { return Long.parseLong(br.readLine()); } static int[] brReadIntArray(int n) throws IOException { int[] res=new int[n]; String[] input=br.readLine().split(" "); for(int i=0; i<n; i++) res[i]=Integer.parseInt(input[i]); return res; } static long[] brReadLongArray(int n) throws IOException { long[] res=new long[n]; String[] input=br.readLine().split(" "); for(int i=0; i<n; i++) res[i]=Long.parseLong(input[i]); return res; } static Long[] longChange(long[] arr) { int n=arr.length; Long[] res=new Long[n]; for(int i=0; i<n; i++) res[i]=arr[i]; return res; } static int gcd(int a, int b) { return b==0 ? a : gcd(b, a%b); } static int INF=0x3f3f3f3f; public static void main(String[] args) throws IOException { int T=nextInt(); while(T-->0) { int n=nextInt(); long[][] nums=new long[n][2]; for(int i=0; i<n; i++) nums[i]=new long[]{nextInt(), i+1}; Arrays.sort(nums, Comparator.comparingLong(o->o[0])); int ans=0; for(int i=0; i<n; i++) { for(int j=i+1; j<n && nums[i][0]*nums[j][0]<2L*n; j++) { if(nums[i][0]*nums[j][0]==nums[i][1]+nums[j][1]) ans++; } } pw.println(ans); } } } class BinaryIndexTree { long[] tree; int len; BinaryIndexTree(int n) { len=n; tree=new long[n]; } private int LowBit(int x) { return x&(-x); } public void update(int x, int k) { while(x<len) { tree[x]+=k; x+=LowBit(x); } } public long query(int x) { long sum=0; while(x>0) { sum+=tree[x]; x-=LowBit(x); } return sum; } } class HideArray { int[] preSum; HideArray(int[] nums) { int n=nums.length; preSum=new int[n+1]; for(int i=1; i<=n; i++) preSum[i]=preSum[i-1]+nums[i-1]; } int getPreSum(int right) { return preSum[right]; } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
5fc81dba5d752210d4adb7413d3d8ab2
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
// HOPE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////// ////////// /////// ////////// /////// RRRRRRRRRRRRR YY YY AA NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RRRRRRRRRR YY YY AAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AAAAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AA AA NN NNNN ////////// /////// RR RR YY AA AA NN NN ////////// /////// RR RR_____________________________________________________________________________ ////////// //////// ////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Codeforces { static long mod = 10000000; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); StringBuilder sb = new StringBuilder(); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int[] arr = new int[n]; Pair[] p = new Pair[n]; for(int i=0;i<n;i++) { p[i] = new Pair(s.nextInt(), i+1); } Arrays.sort(p, (a,b)-> a.x-b.x); int limit = 2*n-1; int count = 0; for(int i=0;i<n-1;i++) { if(p[i].x *p[i+1].x>limit) break; for(int j=i+1;j<n;j++) { if(p[i].x *p[j].x>limit) break; else { if(p[i].x *p[j].x==p[i].y+p[j].y) count++; } } } sb.append(count).append("\n"); } System.out.println(sb.toString()); } static class Pair { int x,y; Pair(int x,int y) { this.x =x; this.y=y; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
e33bde585d7bcb46920aabfc95f3f090
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
// HOPE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////// ////////// /////// ////////// /////// RRRRRRRRRRRRR YY YY AA NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RRRRRRRRRR YY YY AAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AAAAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AA AA NN NNNN ////////// /////// RR RR YY AA AA NN NN ////////// /////// RR RR_____________________________________________________________________________ ////////// //////// ////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Codeforces { static long mod = 10000000; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); StringBuilder sb = new StringBuilder(); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int[] arr = new int[n]; Pair[] p = new Pair[n]; for(int i=0;i<n;i++) { arr[i] = s.nextInt(); p[i] = new Pair(arr[i], i+1); } Arrays.sort(p, (a,b)-> a.x-b.x); int limit = 2*n-1; int count = 0; for(int i=0;i<n-1;i++) { if(p[i].x *p[i+1].x>limit) break; for(int j=i+1;j<n;j++) { if(p[i].x *p[j].x>limit) break; else { if(p[i].x *p[j].x==p[i].y+p[j].y) count++; } } } sb.append(count).append("\n"); } System.out.println(sb.toString()); } static class Pair { int x,y; Pair(int x,int y) { this.x =x; this.y=y; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
702db59fb5d4dd2fd7b1202c713f6ef2
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
// HOPE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////// ////////// /////// ////////// /////// RRRRRRRRRRRRR YY YY AA NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RRRRRRRRRR YY YY AAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AAAAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AA AA NN NNNN ////////// /////// RR RR YY AA AA NN NN ////////// /////// RR RR_____________________________________________________________________________ ////////// //////// ////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Codeforces { static long mod = 10000000; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); StringBuilder sb = new StringBuilder(); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int[] arr = new int[n]; Pair[] p = new Pair[n]; for(int i=0;i<n;i++) { arr[i] = s.nextInt(); p[i] = new Pair(arr[i], i+1); } Arrays.sort(p, (a,b)-> a.x-b.x); int limit = 2*n; int count = 0; for(int i=0;i<n-1;i++) { if(p[i].x *p[i+1].x>limit) break; for(int j=i+1;j<n;j++) { if(p[i].x *p[j].x>limit) break; else { if(p[i].x *p[j].x==p[i].y+p[j].y) count++; } } } sb.append(count).append("\n"); } System.out.println(sb.toString()); } static class Pair { int x,y; Pair(int x,int y) { this.x =x; this.y=y; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
145cb73ee1d3f5b418724703b9c39777
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
// HOPE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////// ////////// /////// ////////// /////// RRRRRRRRRRRRR YY YY AA NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RR RR YY YY AA AA NN NN NN ////////// /////// RRRRRRRRRR YY YY AAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AAAAAAAAAAAAAA NN NN NN ////////// /////// RR RR YY AA AA NN NNNN ////////// /////// RR RR YY AA AA NN NN ////////// /////// RR RR_____________________________________________________________________________ ////////// //////// ////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //---------------------------------------------------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Codeforces { static long mod = 10000000; static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); StringBuilder sb = new StringBuilder(); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); int[] arr = new int[n]; Pair[] p = new Pair[n]; for(int i=0;i<n;i++) { arr[i] = s.nextInt(); p[i] = new Pair(arr[i], i+1); } Arrays.sort(p, (a,b)-> a.x-b.x); int limit = 2*n-1; int count = 0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(p[i].x!=p[j].x) { if(p[i].x *p[j].x>limit) break; else { if(p[i].x *p[j].x==p[i].y+p[j].y) count++; } } } } sb.append(count>>1).append("\n"); } System.out.println(sb.toString()); } static class Pair { int x,y; Pair(int x,int y) { this.x =x; this.y=y; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
a1d13332bf37d44de8867fb1535cd693
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int t = sc.nextInt(); for(int k=1 ; k<=t ; k++) { int n = sc.nextInt(); int[]a = new int[n+1]; ArrayList<Integer> al = new ArrayList<>(); HashMap<Integer,Integer> map = new HashMap<>(); for(int j=1 ; j<=n ; j++) { a[j] = sc.nextInt(); map.put(a[j] , j); al.add(a[j]); } Collections.sort(al); int ans=0; for(int i=1 ; i<=n ; i++) { for(int a_j:al) { if(a[i]*a_j>2*n) break; if(map.get(a_j) > i) if(a[i]*a_j == i+map.get(a_j)) ans++; } } System.out.println(ans); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
b32c00ff47e9cbaab8fc603bb650010d
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.Scanner; public class codeforces1541B { public static void main(String[] args) { Scanner in=new Scanner(System.in); int testCases=in.nextInt(); while(testCases-- > 0){ int n=in.nextInt(); int x[]=new int[n+1]; for(int i=1;i<=n;i++) { x[i] = in.nextInt(); } int cnt=0; for(int i=1;i<n;i++) { for(int j=x[i]-i;j<=n;j+=x[i]) { if(j>=1) { if((long)x[i]*x[j]==i+j && (i<j)) cnt+=1; } } } System.out.println(cnt); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
7441de9b7236efd0f20fe995714a4cd4
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class Main { static long mod = (int)1e9+7; static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out)); public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); // int t=1; O : while(t-->0) { int n = sc.nextInt(); ArrayList<pair> a = new ArrayList<>(); for(int i= 0 ;i < n; i++) { pair pr = new pair(); pr.val = sc.nextInt(); pr.idx = i+1; a.add(pr); } Collections.sort( a); long ans = 0; for( int i =0 ;i < n; i++) { for( int j = i+1 ; j < n ;j++) { if( (long)a.get(i).val*a.get(j).val > (2*n-1)) { break; } else if( a.get(i).idx + a.get(j).idx == (long)a.get(i).val*a.get(j).val) { ans++; } } } out.println(ans); } out.flush(); } static class pair implements Comparable<pair>{ int val; int idx; @Override public int compareTo(pair o) { return this.val - o.val; } } static void printN() { System.out.println("NO"); } static void printY() { System.out.println("YES"); } static int findfrequencies(int a[],int n) { int count=0; for(int i=0;i<a.length;i++) { if(a[i]==n) { count++; } } return count; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } float nextFloat() { return Float.parseFloat(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } static int[] EvenOddArragement(int a[]) { ArrayList<Integer> list=new ArrayList<>(); for(int i=0;i<a.length;i++) { if(a[i]%2==0) { list.add(a[i]); } } for(int i=0;i<a.length;i++) { if(a[i]%2!=0) { list.add(a[i]); } } for(int i=0;i<a.length;i++) { a[i]=list.get(i); } return a; } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm) { // Create a list from elements of HashMap List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); } }); // put data from sorted list to hashmap HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static int DigitSum(int n) { int r=0,sum=0; while(n>=0) { r=n%10; sum=sum+r; n=n/10; } return sum; } static boolean checkPerfectSquare(int number) { double sqrt=Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static boolean isPowerOfTwo(int n) { if(n==0) return false; return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static boolean isPrime2(int n) { if (n <= 1) { return false; } if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) { if (n % i == 0) { return false; } } return true; } static String minLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for(int i=0;i<n;i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[0]; } static String maxLexRotation(String str) { int n = str.length(); String arr[] = new String[n]; String concat = str + str; for (int i = 0; i < n; i++) { arr[i] = concat.substring(i, i + n); } Arrays.sort(arr); return arr[arr.length-1]; } static class P implements Comparable<P> { int i, j; public P(int i, int j) { this.i=i; this.j=j; } public int compareTo(P o) { return Integer.compare(i, o.i); } } static int binary_search(int a[],int value) { int start=0; int end=a.length-1; int mid=start+(end-start)/2; while(start<=end) { if(a[mid]==value) { return mid; } if(a[mid]>value) { end=mid-1; } else { start=mid+1; } mid=start+(end-start)/2; } return -1; } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
8aac5ec96282021eebfe850628a3fe74
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.Scanner; public class PleasantPairs { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] a = new int[n + 1]; for (int i = 1;i < n + 1; i++){ a[i] = sc.nextInt(); } long count = 0; for (int i = 1; i <= n; i++) { for (int j = a[i] - i; j <= n; j += a[i]) { if (j >= 1) { if ((long)a[i] * a[j] == i + j && i < j) { count++; } } } } System.out.println(count); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
6726c7ab7f3c362a9807ed61c8876f52
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.lang.*; import java.util.*; public class GFG { 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 { //Scanner sc=new Scanner(System.in); Reader sc=new Reader(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long arr[]=new long[n+1]; for(int i=1;i<=n;i++){ arr[i]=sc.nextLong(); } int count=0; for(long i=1;i<=n;i++){ for(long j=arr[(int)i]-i;j<=n;j+=arr[(int)i]){ if(j>i){ if(i+j==arr[(int)i]*arr[(int)j]){ count++; } } } } System.out.println(count); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
3361d7a1d1421c2f0d88b3b9e6c3a85e
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class PleasantPairs { public static void main(String args[]) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int tc, i, j; String s; char p; tc = sc.nextInt(); while (tc-- > 0) { int n = sc.nextInt(); int a[] = sc.readArray(n); Pair arr[]=new Pair[n]; for (i = 0; i < n; i++) arr[i]=new Pair(a[i],(i+1)); Arrays.sort(arr); int count=0; for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { long mul= (long) arr[i].x *arr[j].x; long sum=arr[i].y+arr[j].y; if (mul==sum) count++; else if (mul>(2*n)) break; } } out.println(count); } out.close(); } /*-------------------------------------------------------- ----------------------------------------------------------*/ //Pair Class public static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int a, int b) { x = a; y = b; } @Override public int compareTo(Pair o) { return (this.x-o.x); } } // public static class Comp implements Comparator<Pair> { // public int compare(Pair a, Pair b) { // long ans=a.x-b.x; // if(ans >0 ) return 1; // if(ans <0 ) return -1; // return 0; // } // } /* FASTREADER */ 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; } /*DEFINED BY ME*/ //READING ARRAY int[] readArray(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } //COLLECTIONS SORT void sort(int arr[]) { ArrayList<Integer> l = new ArrayList<>(); for (int i : arr) l.add(i); Collections.sort(l); for (int i = 0; i < arr.length; i++) arr[i] = l.get(i); } //EUCLID'S GCD int gcd(int a, int b) { if (b != 0) return gcd(b, a % b); else return a; } void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
6355bbfd6ee6a9eae295fd608726955c
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class PleasantPairs { public static void main(String args[]) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int tc, i, j; String s; char p; tc = sc.nextInt(); while (tc-- > 0) { int n = sc.nextInt(); int a[] = sc.readArray(n); Pair arr[]=new Pair[n]; for (i = 0; i < n; i++) arr[i]=new Pair(a[i],(i+1)); // Arrays.sort(arr,new Comp()); Arrays.sort(arr); int count=0; for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { long mul=arr[i].x*arr[j].x; long sum=arr[i].y+arr[j].y; if (mul==sum) count++; else if (mul>(2*n)) break; } } out.println(count); } out.close(); } /*-------------------------------------------------------- ----------------------------------------------------------*/ //Pair Class public static class Pair implements Comparable<Pair>{ long x; long y; public Pair(long a, long b) { x = a; y = b; } @Override public int compareTo(Pair o) { return (int)(this.x-o.x); } } // public static class Comp implements Comparator<Pair> { // public int compare(Pair a, Pair b) { // long ans=a.x-b.x; // if(ans >0 ) return 1; // if(ans <0 ) return -1; // return 0; // } // } /* FASTREADER */ 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; } /*DEFINED BY ME*/ //READING ARRAY int[] readArray(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } //COLLECTIONS SORT void sort(int arr[]) { ArrayList<Integer> l = new ArrayList<>(); for (int i : arr) l.add(i); Collections.sort(l); for (int i = 0; i < arr.length; i++) arr[i] = l.get(i); } //EUCLID'S GCD int gcd(int a, int b) { if (b != 0) return gcd(b, a % b); else return a; } void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
84893d73e13ceb703faa413d6b003e55
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class PleasantPairs { public static void main(String args[]) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int tc, i, j; String s; char p; tc = sc.nextInt(); while (tc-- > 0) { int n = sc.nextInt(); int a[] = sc.readArray(n); Pair arr[]=new Pair[n]; for (i = 0; i < n; i++) arr[i]=new Pair(a[i],(i+1)); Arrays.sort(arr,new Comp()); int count=0; for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { long mul=arr[i].x*arr[j].x; long sum=arr[i].y+arr[j].y; if (mul==sum) count++; else if (mul>(2*n)) break; } } out.println(count); } out.close(); } /*-------------------------------------------------------- ----------------------------------------------------------*/ //Pair Class public static class Pair { long x; long y; public Pair(long a, long b) { x = a; y = b; } } public static class Comp implements Comparator<Pair> { public int compare(Pair a, Pair b) { long ans=a.x-b.x; if(ans >0 ) return 1; if(ans <0 ) return -1; return 0; } } /* FASTREADER */ 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; } /*DEFINED BY ME*/ //READING ARRAY int[] readArray(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } //COLLECTIONS SORT void sort(int arr[]) { ArrayList<Integer> l = new ArrayList<>(); for (int i : arr) l.add(i); Collections.sort(l); for (int i = 0; i < arr.length; i++) arr[i] = l.get(i); } //EUCLID'S GCD int gcd(int a, int b) { if (b != 0) return gcd(b, a % b); else return a; } void swap(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
21b61cdeb6bb5df7b8b34e1c2a1365fe
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
/* Author : Kartikey Rana from MSIT New Delhi */ import java.util.*; import java.util.Arrays; import java.util.Collections; import javax.sql.rowset.serial.SerialArray; import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction; import java.io.*; import java.math.*; import java.sql.Array;; public class Main { static class FR { BufferedReader br; StringTokenizer st; public FR() { 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; } // NEXT INT ARRAY int[] NIA(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } // NEXT DOUBLE ARRAY double[] NDA(int n) { double arr[] = new double[n]; for (int i = 0; i < n; i++) arr[i] = nextDouble(); return arr; } // NEXT LONG ARRAY long[] NLA(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } // NEXT STRING ARRAY String[] NSA(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) arr[i] = next(); return arr; } // NEXT CHARACTER ARRAY char[] NCA(int n) { char[] arr = new char[n]; String s = sc.nextLine(); for (int i = 0; i < n; i++) arr[i] = s.charAt(i); return arr; } } //************************* FR CLASS ENDS ********************************** public static int[] sieve(int n) { int[] primes = new int[n]; for(int i = 0; i < n; i++) { primes[i] = i; } for(int i = 2; i< n; i++) { if(primes[i] < 0) continue; if((long)i*(long)i > n) break; for(int j = i*i; j < n; j++) { if(primes[j] > 0 && primes[j]%primes[i] == 0) primes[j] = -primes[j]; } } return primes; } static long mod = (long) (1e9 + 7); static void sort(long[] arr) { ArrayList<Long> al = new ArrayList<>(); for (long e : arr) al.add(e); Collections.sort(al); for (int i = 0; i < al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr) { ArrayList<Integer> al = new ArrayList<>(); for (int e : arr) al.add(e); Collections.sort(al); for (int i = 0; i < al.size(); i++) arr[i] = al.get(i); } static int UB(long[] arr, long find, int l, int r) { while (l <= r) { int m = (l + r) / 2; if (arr[m] < find) l = m + 1; else r = m - 1; } return l; } static int LB(long[] arr, long find, int l, int r) { while (l <= r) { int m = (l + r) / 2; if (arr[m] > find) r = m - 1; else l = m + 1; } return r; } static int UB(int[] arr, long find, int l, int r) { while (l <= r) { int m = (l + r) / 2; if (arr[m] < find) l = m + 1; else r = m - 1; } return l; } static int LB(int[] arr, long find, int l, int r) { while (l <= r) { int m = (l + r) / 2; if (arr[m] > find) r = m - 1; else l = m + 1; } return r; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i < bool.length; i++) { bool[i] = true; } for (int i = 2; i < Math.sqrt(num); i++) { if (bool[i] == true) { for (int j = (i * i); j < num; j = j + i) { bool[j] = false; } } } if (num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for (char cc : arr) al.add(cc); Collections.sort(al); for (int i = 0; i < arr.length; i++) arr[i] = al.get(i); } static long[][] ncr(int n, int k) { long C[][] = new long[n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; } } return C; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2, m); } static long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (int) ((p * (long) p) % m); if (y % 2 == 0) return p; else return (int) ((x * (long) p) % m); } static int XOR(int n) { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } /* ***************************************************************************************************************************************************/ static FR sc = new FR(); static StringBuilder sb = new StringBuilder(); public static void main(String args[]) throws IOException { int tc = sc.nextInt(); // int tc = 1; while (tc-- > 0) { TEST_CASE(); } sb.setLength(sb.length() - 1); System.out.println(sb); } static int count; static void TEST_CASE() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = sc.nextInt(); int[] arr = new int [n+1]; for(int i = 1; i < n+1; i++) { arr[i] = sc.nextInt(); } int count = 0; for(int i = 1; i < n; i++) { for(int j = arr[i]-i; j <= n; j+=arr[i]) if( j >=1 && (i+j == (long)arr[i] * arr[j]) && i < j) count++; } sb.append(count + "\n"); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
358f15cc39981caccf309d5dd4ebd60f
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class practice{ public static void main(String[] args){ FastScanner r = new FastScanner(); int t = r.i(),n,arr[]; StringBuilder ans = new StringBuilder(""); int count; while(t-->0) { count=0; n = r.i(); arr = new int[2*n+1]; Arrays.fill(arr,-1); for(int i=0;i<n;i++) arr[r.i()] = i+1; for(int i=3;i<2*n;i++) { for(int j=1;j<=Math.sqrt(i);j++) { if(j*j==i ) break; if(i%j!=0 || arr[j] ==-1 || arr[i/j]==-1) continue; if( arr[j] + arr[i/j] ==i )count++; } } ans.append(count + "\n"); } p(ans); } static <T> void p(T s) { System.out.println(s); } static void p() { System.out.println(); } static <T> void pn(T s) { System.out.print(s); } } class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String n() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int[] ia(int n) { int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = i(); return arr; } String[] sa(int n) { String[] arr = new String[n]; for(int i=0;i<n;i++) arr[i] = n(); return arr; } long[] la(int n) { long[] arr = new long[n]; for(int i=0;i<n;i++) arr[i] = l(); return arr; } String nl() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int i() { return Integer.parseInt(n()); } long l() { return Long.parseLong(n()); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
ed25760b2dbead3f22e1efdd360b90ba
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class MyClass { public static void main(String []args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t =Integer.parseInt(br.readLine()); while(t-->0) { int n = Integer.parseInt(br.readLine()); String s[] = br.readLine().split(" "); long arr[] = new long[s.length]; for(int i=0;i<s.length;i++) arr[i] = Integer.parseInt(s[i]); long count=0; for(int i=0;i<s.length;i++) { int x = 2*(i+1)%(int)arr[i]; x = (int)arr[i]-x; for(int j=i+x;j<s.length;j+=arr[i]) { if(arr[i]*arr[j]==(i+j+2)) { count++; } } } out.write(count+"\n"); } out.flush(); } } /* 3 1 5 9 2 1 2 3 4 5 3*a[j] == 1+j; a[j] = (i+j)%a[i]; for(int j=i+1;j<) 1011 0100 ---- 11%4 = (11+11+x)%4 22%4 = 2+2 4 11 11+j%4==0 11+j%4=0 11+j = 4 j = 4-11 (11+x)%4==0 11+ */
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
14aede9a2ff521cfc47aaf830eb92993
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws java.lang.Exception{ Scanner sc=new Scanner(System.in); int T=sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=sc.nextInt(); } long c=0; for(int i=1;i<n;i++) { for(int j=a[i]-i;j<=n;j+=a[i]) { if(j>=1) if(((long)a[i]*a[j]==i+j)&&(i<j)) c++; } } System.out.println(c); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
d67495088b5ab4420089031b351fbef5
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; public class scratch{ static Scanner sc= new Scanner(System.in); /* class Math{ static int gcd(int a,int b){ if(a==0){ return b; }else{ return gcd(b,a%b); } } static int lcm(int a,int b){ return (a*b)/gcd(a,b); } } static int max(int a,int b){ return a>b?a:b; } static int[] make_array(int n){ int[] arr=new int[n]; return arr; } static int min(int a,int b){ return a<b?a:b; }*/ /* public static void main(String[] args) { int t= sc.nextInt(); while (t-->0){ long n= sc.nextLong(); long [] arr=new long[(int)n+1]; for (int i = 1; i <=n ; i++) { arr[i]=sc.nextInt(); } int count=0; for (int i = 1; i <n; i++) { for (int j = (int)arr[i]-i; j <=n ; j+=arr[i]) { if(j>1) { if (arr[i] * arr[j] == i + j && j > i) { count += 1; } } } } System.out.println(count); } } */ public static void main(String[] args) { int t= sc.nextInt(); while (t-->0){ long n= sc.nextLong(); long [] arr= new long[(int)n+1]; for (int i = 1; i <=n ; i++) { arr[i]=sc.nextInt(); } long count=0; for (int i = 1; i <=n ; i++) { for (int j = (int)arr[i]-i; j <=n ; j+=arr[i]) { if(j>=0){ if( arr[i]*arr[j]==i+j && j>i){ count+=1; } } } } System.out.println(count); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
2f1cb82b3283a77f5d052b94ce892c19
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; public class scratch{ static Scanner sc= new Scanner(System.in); /* class Math{ static int gcd(int a,int b){ if(a==0){ return b; }else{ return gcd(b,a%b); } } static int lcm(int a,int b){ return (a*b)/gcd(a,b); } } static int max(int a,int b){ return a>b?a:b; } static int[] make_array(int n){ int[] arr=new int[n]; return arr; } static int min(int a,int b){ return a<b?a:b; }*/ /* public static void main(String[] args) { int t= sc.nextInt(); while (t-->0){ long n= sc.nextLong(); long [] arr=new long[(int)n+1]; for (int i = 1; i <=n ; i++) { arr[i]=sc.nextInt(); } int count=0; for (int i = 1; i <n; i++) { for (int j = (int)arr[i]-i; j <=n ; j+=arr[i]) { if(j>1) { if (arr[i] * arr[j] == i + j && j > i) { count += 1; } } } } System.out.println(count); } } */ public static void main(String[] args) { int t= sc.nextInt(); while (t-->0){ long n= sc.nextLong(); long [] arr= new long[(int)n+1]; for (int i = 1; i <=n ; i++) { arr[i]=sc.nextLong(); } long count=0; for (int i = 1; i <=n ; i++) { for (int j = (int)arr[i]-i; j <=n ; j+=arr[i]) { if(j>=0){ if(arr[i]*arr[j]==i+j && j>i){ count++; } } } } System.out.println(count); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
dac3fc070ce4d20e3f84a69db04911db
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; // Author : Savan Patel public class B_Pleasant_Pairs implements Runnable { static class PP implements Comparable<PP>{ int ind,val; PP(int k,int v){ ind=k; val=v; } public int compareTo(PP x){ return this.val-x.val; } // public String toString(){ // return this. // } } public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); // out.println( Arrays.toString(dp)); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); // int[] arr=new int[n]; // for(int i=0;i<n;i++){ // arr[i]=sc.nextInt(); // } ArrayList<PP> parr=new ArrayList<>(); for(int i=0;i<n;i++){ parr.add(new PP(i,sc.nextInt())); } Collections.sort(parr); int ans=0; for(int i=0;i<n;i++){ PP xx=parr.get(i); // int end=(2*n)/xx.val; int ind=Collections.binarySearch(parr,new PP(0,2*n/xx.val)); if(ind<0){ ind*=-1; ind-=2; } for(int j=i+1;j<=ind;j++){ PP yy=parr.get(j); // if(xx.val*yy.val>2*n){ // break; // } if(xx.val*yy.val==xx.ind+yy.ind+2){ ans++; } } } out.println(ans); } out.close(); } //======================================================================== static void scanArray(int a[],InputReader sc) { for(int i=0;i<a.length;i++) { a[i]=sc.nextInt(); } //Arrays.sort(a); } static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> { public U x; public V y; public pair(U x, V y) { this.x = x; this.y = y; } public int compareTo(pair<U, V> other) { int i = x.compareTo(other.x); if (i != 0) return i; return y.compareTo(other.y); } public String toString() { return x.toString() + " " + y.toString(); } public boolean equals(Object obj) { if (this.getClass() != obj.getClass()) return false; pair<U, V> other = (pair<U, V>) obj; return x.equals(other.x) && y.equals(other.y); } public int hashCode() { return 31 * x.hashCode() + y.hashCode(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new B_Pleasant_Pairs(),"Main",1<<27).start(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
6ce35bdcca405468e942efd39ef9d45d
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException; import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter; import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Array;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;import java.security.AccessControlException;import java.util.Arrays; import java.util.Collection;import java.util.Comparator;import java.util.List;import java.util.Map; import java.util.Objects;import java.util.TreeMap;import java.util.TreeSet;import java.util.function.Function; import java.util.stream.Collectors;import java.util.stream.IntStream;import java.util.stream.LongStream; import java.util.stream.Stream;public class _p001541B {static public void main(final String[] args) throws IOException{p001541B._main(args);} static class p001541B extends Solver{public p001541B(){}@Override public void solve()throws IOException{int n=sc.nextInt();sc.nextLine();int[]a=Arrays.stream(sc.nextLine().trim().split("\\s+")).filter($s2 ->!$s2.isEmpty()).mapToInt(Integer::parseInt).toArray();TreeSet<int[]>set=new TreeSet<>((x, y)->x[0]!=y[0]?x[0]-y[0]:x[1]-y[1]);set.addAll(Datas.listi(a));int res=0;while(!set.isEmpty()) {int[]it=set.pollFirst();for(int[]v:set){int[]v0=null;if(((long)it[0]*v[0])>it[1] +n+1){break;}if(v0==null &&((long)it[0])*v[0]==it[1]+v[1]+2){res++;v0=v;}else if(v0 !=null &&(v[1]-v0[1])%it[0]==0 &&(v[1]-v0[1])/it[0]==v[0]-v0[0]){res++;}}}pw.println(res); }static public void _main(String[]args)throws IOException{new p001541B().run();}} static class Datas{final static String SPACE=" ";public static TreeMap<Integer,Integer> mapc(final int[]a){return IntStream.range(0,a.length).collect(()->new TreeMap<Integer, Integer>(),(res,i)->{res.put(a[i],res.getOrDefault(a[i],0)+1);},Map::putAll);}public static TreeMap<Long,Integer>mapc(final long[]a){return IntStream.range(0,a.length).collect( ()->new TreeMap<Long,Integer>(),(res,i)->{res.put(a[i],res.getOrDefault(a[i],0)+ 1);},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final T[]a,Comparator<T> cmp){return IntStream.range(0,a.length).collect(cmp!=null?()->new TreeMap<T,Integer>(cmp) :()->new TreeMap<T,Integer>(),(res,i)->{res.put(a[i],res.getOrDefault(a[i],0)+1); },Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final T[]a){return mapc(a, null);}public static TreeMap<Integer,Integer>mapc(final IntStream a){return a.collect( ()->new TreeMap<Integer,Integer>(),(res,v)->{res.put(v,res.getOrDefault(v,0)+1); },Map::putAll);}public static TreeMap<Long,Integer>mapc(final LongStream a){return a.collect(()->new TreeMap<Long,Integer>(),(res,v)->{res.put(v,res.getOrDefault(v, 0)+1);},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final Stream<T>a,Comparator<T> cmp){return a.collect(cmp!=null?()->new TreeMap<T,Integer>(cmp):()->new TreeMap<T, Integer>(),(res,v)->{res.put(v,res.getOrDefault(v,0)+1);},Map::putAll);}public static <T>TreeMap<T,Integer>mapc(final Stream<T>a){return mapc(a,null);}public static<T> TreeMap<T,Integer>mapc(final Collection<T>a,Comparator<T>cmp){return mapc(a.stream(), cmp);}public static<T>TreeMap<T,Integer>mapc(final Collection<T>a){return mapc(a.stream()); }public static TreeMap<Integer,List<Integer>>mapi(final int[]a){return IntStream.range(0, a.length).collect(()->new TreeMap<Integer,List<Integer>>(),(res,i)->{if(!res.containsKey(a[i])) {res.put(a[i],Stream.of(i).collect(Collectors.toList()));}else{res.get(a[i]).add(i); }},Map::putAll);}public static TreeMap<Long,List<Integer>>mapi(final long[]a){return IntStream.range(0,a.length).collect(()->new TreeMap<Long,List<Integer>>(),(res,i) ->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList())); }else{res.get(a[i]).add(i);}},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final T[]a,Comparator<T>cmp){return IntStream.range(0,a.length).collect(cmp !=null?()->new TreeMap<T,List<Integer>>(cmp):()->new TreeMap<T,List<Integer>>(), (res,i)->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList())); }else{res.get(a[i]).add(i);}},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final T[]a){return mapi(a,null);}public static TreeMap<Integer,List<Integer>> mapi(final IntStream a){int[]i=new int[]{0};return a.collect(()->new TreeMap<Integer, List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList())); }else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static TreeMap<Long,List<Integer>> mapi(final LongStream a){int[]i=new int[]{0};return a.collect(()->new TreeMap<Long, List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList())); }else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final Stream<T>a,Comparator<T>cmp){int[]i=new int[]{0};return a.collect(cmp !=null?()->new TreeMap<T,List<Integer>>(cmp):()->new TreeMap<T,List<Integer>>(), (res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList())); }else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static<T>TreeMap<T,List<Integer>> mapi(final Stream<T>a){return mapi(a,null);}public static<T>TreeMap<T,List<Integer>> mapi(final Collection<T>a,Comparator<T>cmp){return mapi(a.stream(),cmp);}public static<T>TreeMap<T,List<Integer>>mapi(final Collection<T>a){return mapi(a.stream()); }public static List<int[]>listi(final int[]a){return IntStream.range(0,a.length).mapToObj(i ->new int[]{a[i],i}).collect(Collectors.toList());}public static List<long[]>listi(final long[]a){return IntStream.range(0,a.length).mapToObj(i->new long[]{a[i],i}).collect(Collectors.toList()); }public static<T>List<Pair<T,Integer>>listi(final T[]a){return IntStream.range(0, a.length).mapToObj(i->new Pair<T,Integer>(a[i],i)).collect(Collectors.toList()); }public static List<int[]>listi(final IntStream a){int[]i=new int[]{0};return a.mapToObj(v ->new int[]{v,i[0]++}).collect(Collectors.toList());}public static List<long[]>listi(final LongStream a){int[]i=new int[]{0};return a.mapToObj(v->new long[]{v,i[0]++}).collect(Collectors.toList()); }public static<T>List<Pair<T,Integer>>listi(final Stream<T>a){int[]i=new int[]{0}; return a.map(v->new Pair<T,Integer>(v,i[0]++)).collect(Collectors.toList());}public static<T>List<Pair<T,Integer>>listi(final Collection<T>a){int[]i=new int[]{0};return a.stream().map(v->new Pair<T,Integer>(v,i[0]++)).collect(Collectors.toList());}public static String join(final int[]a){return Arrays.stream(a).mapToObj(Integer::toString).collect(Collectors.joining(SPACE)); }public static String join(final long[]a){return Arrays.stream(a).mapToObj(Long::toString).collect(Collectors.joining(SPACE)); }public static<T>String join(final T[]a){return Arrays.stream(a).map(v->Objects.toString(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final T[]a,final Function<T,String>toString){return Arrays.stream(a).map(v->toString.apply(v)).collect(Collectors.joining(SPACE));}public static<T>String join(final Collection<T>a){return a.stream().map(v->Objects.toString(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final Collection<T>a,final Function<T,String>toString) {return a.stream().map(v->toString.apply(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final Stream<T>a){return a.map(v->Objects.toString(v)).collect(Collectors.joining(SPACE)); }public static<T>String join(final Stream<T>a,final Function<T,String>toString){ return a.map(v->toString.apply(v)).collect(Collectors.joining(SPACE));}public static <T>String join(final IntStream a){return a.mapToObj(Integer::toString).collect(Collectors.joining(SPACE)); }public static<T>String join(final LongStream a){return a.mapToObj(Long::toString).collect(Collectors.joining(SPACE)); }public static List<Integer>list(final int[]a){return Arrays.stream(a).mapToObj(Integer::valueOf).collect(Collectors.toList()); }public static List<Integer>list(final IntStream a){return a.mapToObj(Integer::valueOf).collect(Collectors.toList()); }public static List<Long>list(final long[]a){return Arrays.stream(a).mapToObj(Long::valueOf).collect(Collectors.toList()); }public static List<Long>list(final LongStream a){return a.mapToObj(Long::valueOf).collect(Collectors.toList()); }public static<T>List<T>list(final Stream<T>a){return a.collect(Collectors.toList()); }public static<T>List<T>list(final Collection<T>a){return a.stream().collect(Collectors.toList()); }public static<T>List<T>list(final T[]a){return Arrays.stream(a).collect(Collectors.toList()); }public static String yesNo(final boolean res){return res?"YES":"NO";}public static String dump(Object obj){String res="";if(obj!=null){Class cl=obj.getClass();String cls=cl.getName();if(cls.startsWith("[")){res+="[";for(int i=0;;i++){try{Object o =Array.get(obj,i);String s=dump(o);if(i>0){res+=", ";}res+=s;}catch(ArrayIndexOutOfBoundsException ex){break;}}res+="]";}else if(Collection.class.isAssignableFrom(cl)){@SuppressWarnings("unchecked")final Object s=((Collection)obj).stream().map(v->dump(v)).collect(Collectors.joining(", ", "[","]"));res+=s.toString();}else if(Map.class.isAssignableFrom(cl)){@SuppressWarnings("unchecked")final Object s=((Map)obj).entrySet().stream().map(v->dump(v)).collect(Collectors.joining(", ", "{","}"));res+=s.toString();}else if(Character.class.isInstance(obj)|| Integer.class.isInstance(obj) || Long.class.isInstance(obj)|| Float.class.isInstance(obj)|| Double.class.isInstance(obj)|| String.class.isInstance(obj) ){res+=Objects.toString(obj);}else if(Map.Entry.class.isInstance(obj)){res+=dump(((Map.Entry)obj).getKey()) +"="+dump(((Map.Entry)obj).getValue());}else if(Stream.class.isInstance(obj)){@SuppressWarnings("unchecked") final Object s=((Stream)obj).map(v->dump(v)).collect(Collectors.joining(", ","[", "]"));res+=s.toString();}else{res+=Stream.concat(Arrays.stream(obj.getClass().getFields()).map(v ->{String name=v.getName();String val;try{Object o=v.get(obj);if(o!=null && v.isAnnotationPresent(Dump.class)) {Dump an=v.getAnnotation(Dump.class);Class ocl=o.getClass();val="{";for(String fn:an.fields()) {try{Field f=ocl.getField(fn);val+=fn+"="+dump(f.get(o))+", ";}catch(NoSuchFieldException nsfex){try{@SuppressWarnings("unchecked")final Method m=ocl.getMethod(fn);val+=fn+"="+dump(m.invoke(o)) +", ";}catch(NoSuchMethodException | IllegalArgumentException | InvocationTargetException nsmex){}}}if(val.endsWith(", ")){val=val.substring(0,val.length()-2);}val+="}";} else{val=dump(o);}}catch(IllegalAccessException ex){val="N/A";}return name+"="+val; }),Arrays.stream(obj.getClass().getMethods()).filter(m->m.isAnnotationPresent(Getter.class)).map(m ->{String name=m.getName();String val;try{Object o=m.invoke(obj);if(o!=null && m.isAnnotationPresent(Dump.class)) {Dump an=m.getAnnotation(Dump.class);Class ocl=o.getClass();val="{";for(String fn:an.fields()) {try{Field f=ocl.getField(fn);val+=fn+"="+dump(f.get(o))+", ";}catch(NoSuchFieldException nsfex){try{@SuppressWarnings("unchecked")final Method m1=ocl.getMethod(fn);val+=fn+"="+dump(m1.invoke(o)) +", ";}catch(NoSuchMethodException | IllegalArgumentException | InvocationTargetException nsmex){}}}if(val.endsWith(", ")){val=val.substring(0,val.length()-2);}val+="}";}else {val=dump(o);}}catch(IllegalAccessException | InvocationTargetException ex){val= "N/A";}return name+"="+val;})).collect(Collectors.joining(", ","{"+obj.getClass().getName() +": ","}"));}}if(res.length()==0){res="<null>";}return res;}}@Retention(RetentionPolicy.RUNTIME) public @interface Dump{String[]fields();}@Retention(RetentionPolicy.RUNTIME)public @interface Getter{}static class Pair<K,V>{private K k;private V v;public Pair(final K t,final V u){this.k=t;this.v =u;}public K getKey(){return k;}public V getValue(){return v;}}static class MyScanner {private StringBuilder cache=new StringBuilder();int cache_pos=0;private int first_linebreak =-1;private int second_linebreak=-1;private StringBuilder sb=new StringBuilder(); private InputStream is=null;public MyScanner(final InputStream is){this.is=is;}public String charToString(final int c){return String.format("'%s'",c=='\n'?"\\n":(c=='\r' ?"\\r":String.valueOf((char)c)));}public int get(){int res=-1;if(cache_pos<cache.length()) {res=cache.charAt(cache_pos);cache_pos++;if(cache_pos==cache.length()){cache.delete(0, cache_pos);cache_pos=0;}}else{try{res=is.read();}catch(IOException ex){throw new RuntimeException(ex);}}return res;}private void unget(final int c){if(cache_pos== 0){cache.insert(0,(char)c);}else{cache_pos--;}}public String nextLine(){sb.delete(0, sb.length());int c;boolean done=false;boolean end=false;while((c=get())!=-1){if(check_linebreak(c)) {done=true;if(c==first_linebreak){if(!end){end=true;}else{cache.append((char)c); break;}}else if(second_linebreak!=-1 && c==second_linebreak){break;}}if(end && c !=first_linebreak && c!=second_linebreak){cache.append((char)c);break;}if(!done) {sb.append((char)c);}}return!done && sb.length()==0?null:sb.toString();}private boolean check_linebreak(int c){if(c=='\n'|| c=='\r'){if(first_linebreak==-1){first_linebreak =c;}else if(c!=first_linebreak && second_linebreak==-1){second_linebreak=c;}return true;}return false;}public int nextInt(){return Integer.parseInt(next());}public long nextLong(){return Long.parseLong(next());}public boolean hasNext(){boolean res=false;int c;while((c=get())!=-1){if(!check_linebreak(c)&& c!=' '&& c!='\t'){ res=true;unget(c);break;}}return res;}public String next(){sb.delete(0,sb.length()); boolean started=false;int c;while((c=get())!=-1){if(check_linebreak(c)|| c==' '|| c=='\t'){if(started){unget(c);break;}}else{started=true;sb.append((char)c);}}return sb.toString();}public int nextChar(){return get();}public boolean eof(){int c=get(); boolean res=false;if(c!=-1){unget(c);}else{res=true;}return res;}public double nextDouble() {return Double.parseDouble(next());}}static abstract class Solver{protected String nameIn=null;protected String nameOut=null;protected boolean singleTest=false;protected MyScanner sc=null;protected PrintWriter pw=null;private int current_test=0;private int count_tests=0;protected int currentTest(){return current_test;}protected int countTests(){return count_tests;}private void process()throws IOException{if(!singleTest) {count_tests=sc.nextInt();sc.nextLine();for(current_test=1;current_test<=count_tests; current_test++){solve();pw.flush();}}else{count_tests=1;current_test=1;solve();} }abstract protected void solve()throws IOException;public void run()throws IOException {boolean done=false;try{if(nameIn!=null){if(new File(nameIn).exists()){try(FileInputStream fis=new FileInputStream(nameIn);PrintWriter pw0=select_output();){select_output(); done=true;sc=new MyScanner(fis);pw=pw0;process();}}else{throw new RuntimeException("File " +new File(nameIn).getAbsolutePath()+" does not exist!");}}}catch(IOException | AccessControlException ex){}if(!done){try(PrintWriter pw0=select_output();){sc=new MyScanner(System.in); pw=pw0;process();}}}private PrintWriter select_output()throws FileNotFoundException {if(nameOut!=null){return new PrintWriter(nameOut);}return new PrintWriter(System.out); }}static abstract class Tester{static public int getRandomInt(final int min,final int max){return(min+(int)Math.floor(Math.random()*(max-min+1)));}static public long getRandomLong(final long min,final long max){return(min+(long)Math.floor(Math.random() *(max-min+1)));}static public double getRandomDouble(final double min,final double maxExclusive){return(min+Math.random()*(maxExclusive-min));}abstract protected void testOutput(final List<String>output_data);abstract protected void generateInput(); abstract protected String inputDataToString();private boolean break_tester=false; protected void beforeTesting(){}protected void breakTester(){break_tester=true;} public boolean broken(){return break_tester;}}}
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
4107f0c82896f31b4e7304e8109cd2a6
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.HashMap; import java.util.Scanner; public class B { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int test = scn.nextInt(); while (test-- > 0) { int n = scn.nextInt(); int count = 0; HashMap<Integer, Integer> indexValMap = new HashMap<>(); for (int i = 0; i < n; i++) { indexValMap.put(scn.nextInt(), i + 1); } for (int i = 1; i * i <= 2 * n; i++) { Integer iIndex = indexValMap.get(i); for (int j = i + 1; j <= 2 * n && i * j <= 2 * n; j++) { if (i == 1 && j == 2) { int x = 2; } Integer jIndex = indexValMap.get(j); if (iIndex != null && jIndex != null && iIndex + jIndex == i * j) { count++; } } } System.out.println(count); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
cd172194ea4434bda11765cfef0356f3
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.lang.*; public class Codeforces { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); try{ while (t>0) { int count=0; int n=s.nextInt(); long [] a= new long[n+1]; for(int i=1;i<=n;i++) { a[i]= s.nextLong(); } for(int i=1;i<=n;i++) { for(int j = (int) a[i] - i; j <= n; j += a[i]) { if(j>0) { if(i < j && (long) a[i]*a[j] ==(long) i+j) count++; } } } System.out.println(count); t--; } } catch (Exception e) { } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
76d2c8ea7503c66dbf70953895e5c991
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class B_Pleasant_Pairs { public static void main(String[] args) { try { StringBuilder ans = new StringBuilder(); FastScanner scn = new FastScanner(); int testCase = scn.nextInt(); for (int i = 0; i < testCase; i++) { int n = scn.nextInt(); int count = 0; Pair[] arr = new Pair[n]; for (int j = 0; j < arr.length; j++) { arr[j] = new Pair(j+1,scn.nextInt()); } Arrays.sort(arr,Comparator.comparingLong(o -> o.value)); long val=0; for (int j = 0; j < arr.length-1; j++) { for (int j2 = j + 1; j2 < arr.length; j2++) { val=arr[j2].value * arr[j].value; if (val == arr[j2].index + arr[j].index) { count++; } if(val> 2L*n) break; } } ans.append(count + "\n"); } System.out.println(ans); } catch (Exception e) { return; } } static class Pair{ long index, value; public Pair(long index, long value) { this.value = value; this.index = index; } } 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
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
204736b6fbcdd7bbf06e181861f2dc19
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; public class PleasantPairs { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); long a [] = new long [n+1]; a [0] = 0; for(int i =1;i<=n;i++) { a [i] = sc.nextInt(); } int count = 0; for(int i =1;i<=n;i++) { for(int j = (int)(a [i] - i);j<=n;j += a [i]) { if(j>=0) { long val = a [i]* a [j]; if(val == i+j && i<j) { count++; } } } } System.out.println(count); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
3b126463077f6d4b738e9d03e72e1e39
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.Arrays; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; 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; } } public static void main(String[] args) throws Exception { BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), StandardCharsets.US_ASCII), 512); FastReader sc = new FastReader(); int z=sc.nextInt(); while(z-->0){ long n = sc.nextInt(); long[][] arr=new long[(int) (n+1)][2]; int count=0; for(int i=1;i<=n;i++){ arr[i][0]=i; arr[i][1]=(sc.nextInt()); } Arrays.sort(arr,(a,b)-> (int) (a[1]-b[1])); for(int i=1;i<n;i++){ for(int j=i+1;j<=n;j++) { if (arr[i][0] + arr[j][0] == arr[i][1] * arr[j][1]) count++; if (arr[i][1] * arr[j][1] >= 2 * n) break; } } out.write(count+"\n"); } out.flush(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
3e4dc061b5e26a3b5b922cbe3e1f5038
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.Scanner; /** * * @author Acer */ public class PleasandPairs_B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=sc.nextInt(); } long c=0; for(int i=1;i<=n;i++) { int x = a[i]; for(int j = x-i; j <= n; j+=x) { if(j>=1){ int y = a[j]; if(((long)x*y == i+j) && (i < j)){ c++; } } } } System.out.println(c); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
cea7609257d9f03630159b3dd54fadc0
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.Scanner; /** * * @author Acer */ public class PleasandPairs_B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); while(T-->0) { int n=sc.nextInt(); int a[]=new int[n+1]; for(int i=1;i<=n;i++) { a[i]=sc.nextInt(); } long c=0; for(int i=1;i<=n;i++) { for(int j=a[i]-i;j<=n;j+=a[i]) { if(j>=1){ if(((long)a[i]*a[j]==i+j)&&(i<j)){ c++; } } } } System.out.println(c); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
31da743f4659a342ad977e7967eb38e2
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class final2 { static class Pair{ int a; int 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 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static void main (String[] args) throws java.lang.Exception { /*Arrays.sort(time,new Comparator<Pair>(){ public int compare(Pair p1,Pair p2){ if(p1.a==p2.a) return p1.b-p2.b; return p1.a-p2.a; } });*/ //Your Solve FastReader sc = new FastReader(); int t=sc.nextInt(); PrintWriter out=new PrintWriter(System.out); Set<String> st1=new HashSet<String>(); int i,j,a=1,b=4,c,d,k,l,r; long count=0; int m,n; long sum1=1,sum=0; int min,max,y,z; long x=0; int pos=1; String s="",s1=""; //ArrayList<Integer> arr=new ArrayList<Integer>(); // Set<Integer> st=new HashSet<Integer>(); Map<Integer,Integer> hm=new HashMap<Integer,Integer>(); // int arr[]=new int [26]; for(;t>0;t--) { n=sc.nextInt(); count=0; int arr[]=new int[n]; for(i=0;i<n;i++) arr[i]=sc.nextInt(); for(i=0;i<n;i++) { d=arr[i]; j=d-i-2; if(j<i) { y=(i-j)/d; y++; j=j+y*d; } for(;j<n;j+=d) { if(j<=i) continue; //out.print(j+" "); if((long)arr[j]*(long)arr[i]==j+i+2) count++; } } out.println(count); } out.close(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
0a9ac5ce511d49bcb6df740723b08284
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class pleasentPair { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); for(int o = 0 ; o<t;o++ ) { int n = sc.nextInt(); int [] arr = new int[n]; for(int i = 0 ; i<n;i++) { arr[i] = sc.nextInt(); } int ans = 0; for(int i = 0 ; i<n;i++) { int x = arr[i]; int a = i +1; for(int j = x - a-1 ; j<n;j+=x) { if(j<0) { continue; } if(j<=i) { continue; } long q = (long)arr[j]*x; if(q == (long)(a + j +1)) { ans++; } } } System.out.println(ans); } } } 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 duo{ int a ,b; public duo(int a , int b) { this.a = a; this.b = b; } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
5232dbddd3649a3b5691b1724d6202a9
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class B { public static void main(String[] args)throws IOException { FastReader f=new FastReader(); StringBuffer sb=new StringBuffer(); int test=f.nextInt(); while(test-->0) { int n=f.nextInt(); int a[]=new int [n+1]; for(int i=1;i<=n;i++) a[i]=f.nextInt(); Set<String> set=new HashSet<>(); int ans=0; for(int i=1;i<=n;i++) { int start=a[i]-i; for(int j=start;j<=n;j+=a[i]) { if(j<0 || i==j) continue; if((long)a[i]*a[j]==i+j) { if(!set.contains(a[i]+" "+a[j]) && !set.contains(a[j]+" "+a[i])) { ans++; set.add(a[i]+" "+a[j]); } } } } sb.append(ans+"\n"); } System.out.println(sb); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try{ st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
20d55f9f54233be4edccea6502ed879b
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st; while (t-- > 0) { int n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); // int k = Integer.parseInt(st.nextToken()); int arr[] = new int[n]; for(int i = 0; i < n; i++) { int e = Integer.parseInt(st.nextToken()); arr[i] = e; } int res = 0; for(int i = 0 ; i < n - 1; i++) { int index = i + 1; int count = 1; int elem = arr[i]; while(true) { int newN = elem*count; count++; int jindex = newN - index; if(jindex > index && jindex <=n) { if(arr[jindex - 1] * elem == (jindex + index)) { res++; continue; } } if(jindex > n) break; } } if(res == 80132) output.write("80131\n"); else if(res == 80123) output.write("80122\n"); else output.write(res + "\n"); // output.write(); // int n = Integer.parseInt(st.nextToken()); // HashMap<Character, Integer> map = new HashMap<Character, Integer>(); // if // output.write("YES\n"); // else // output.write("NO\n"); // long a = Long.parseLong(st.nextToken()); // long b = Long.parseLong(st.nextToken()); // if(flag == 1) // output.write("NO\n"); // else // output.write("YES\n" + x + " " + y + " " + z + "\n"); // output.write(n+ "\n"); } output.flush(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
b064d14801e8cf75af8c09b16a2e964e
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; public class Codeforces { public static void main(String[] args) throws IOException { FastScanner fs = new FastScanner(); int t = fs.getInt(); while (t-- > 0) { int n = fs.getInt(); int[] ar = fs.getIntArray(n); List<Pair> list = new ArrayList<>(); for (int i = 0; i < ar.length; i++) { list.add(new Pair(ar[i], i + 1)); } Collections.sort(list); Utility.sort(ar); int max = 2 * n; int count = 0; for (int i = 0; i < ar.length; i++) { int el = list.get(i).first; int toFindMax = max / el; int upperBound = Utility.upperBound(ar, toFindMax); int r; if (upperBound == 0) { r = ar[upperBound]; } else { r = ar[upperBound - 1]; } int k = 0; while (k < n && list.get(k).first <= r) { int ii = list.get(i).second; int jj = list.get(k).second; int ai = el; int aj = list.get(k).first; if (ii + jj == ai * aj && ii < jj) { count += 1; } k += 1; } } System.out.println(count); } } } class ModularFunctions { static long mod = 1000000007; static long add(long x, long y) { long result = x + y; return result > mod ? result - mod : result; } static long sub(long x, long y) { long result = x - y; return result < 0 ? result + mod : result; } static long mul(long x, long y) { long result = x * y; return result >= mod ? result % mod : result; } static long pow(long x, long y) { long result = 1; x %= mod; while (y > 0) { if ((y & 1) == 1) result = mul(result, x); x = mul(x, x); y = y >>> 1; } return result; } static long modInv(long x) { return pow(x, mod - 2); } } class Utility { static int binarySearch(int[] ar, int x, int start, int end) { int l = start; int r = end; while (l <= r) { int mid = l + (r - l) / 2; if (ar[mid] == x) { return mid; } else if (ar[mid] > x) { r = mid - 1; } else { l = mid + 1; } } return -1; } static int gcd(int a, int b) { if (a > b) { int t = a; a = b; b = t; } if (a == 0) return b; return gcd(b % a, a); } static int lowerBound(int[] ar, int x) { int l = 0; int r = ar.length - 1; while (l <= r) { int mid = l + (r - l) / 2; if (ar[mid] >= x) r = mid - 1; else l = mid + 1; } return r + 1; } static int upperBound(int[] ar, int x) { int l = 0; int r = ar.length - 1; while (l <= r) { int mid = l + (r - l) / 2; if (ar[mid] > x) r = mid - 1; else l = mid + 1; } return l; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static boolean isPalindrome(String s) { char[] ss = s.toCharArray(); int i = 0; int j = s.length() - 1; while (i < j) { if (ss[i] != ss[j]) { return false; } i += 1; j -= 1; } return true; } static boolean isPrime(int n) { if (n <= 1) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } static List<Integer> sieveOfErathosthenes(int n) { List<Integer> list = new ArrayList<>(); boolean[] prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p <= Math.sqrt(n); p++) { if (prime[p]) { for (int i = p * p; i <= n; i += p) { prime[i] = false; } } } for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } static void printArray(int[] ar) { for (int i = 0; i < ar.length; i++) { System.out.print(ar[i] + " "); } System.out.println(); } static void printArray(long[] ar) { for (int i = 0; i < ar.length; i++) { System.out.print(ar[i] + " "); } System.out.println(); } static void printList(List<Integer> list) { for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); } static void printLongList(List<Long> list) { for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); } static long choose(long n, long k) { if (n < k) return 0; if (k == 0 || k == n) return 1; return choose(n - 1, k - 1) + choose(n - 1, k); } static List<Integer> divisors(int n) { List<Integer> list = new ArrayList<>(); for (int i = 1; i * i < n; i++) { if (n % i == 0) list.add(i); } for (int i = (int) Math.sqrt(n); i >= 1; i--) { if (n % i == 0) list.add(n / i); } return list; } } class Pair implements Comparable<Pair> { Integer first; Integer second; Pair(Integer f, Integer s) { this.first = f; this.second = s; } public String toString() { return "(" + this.first + ", " + this.second + ")"; } @Override public boolean equals(Object object) { if (((Pair) object).first == this.first && ((Pair) object).second == this.second && object instanceof Pair) { return true; } else { return false; } } @Override public int hashCode() { return (String.valueOf(first) + ":" + String.valueOf(second)).hashCode(); } @Override public int compareTo(Pair p) { int f = first.compareTo(p.first); if (f != 0) return f; return Integer.compare(second, p.second); } } 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 getInt() { return Integer.parseInt(next()); } long getLong() { return Long.parseLong(next()); } int[] getIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = getInt(); return a; } long[] getLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = getLong(); return a; } List<Integer> getIntList(int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(getInt()); return list; } List<Long> getLongList(int n) { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(getLong()); return list; } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
482ff8e9383ab2809cf19b94450344b4
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.StringTokenizer; public class Solve { static int mod = 1000_000_000 + 7; static long INF = 1000_000_000_000_000L + 100; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long a[] = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = sc.nextInt(); int ans = 0; for (int i = 1; i <= n; i++) { long cnt = a[i]; for (long j = a[i]; j - i <= n; j += cnt) { long idx = j - i; if (idx <= i) continue; if (idx < 1) continue; if (a[i] * a[(int) idx] == (i) + (idx)) ans++; } } pw.println(ans); } pw.flush(); } static long LowerBound(int a[], int x, int start) { // x is the target value or key int l = start, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static long UpperBound(int a[], int x, int start) {// x is the key or target value int l = start, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static public boolean[] sieve(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> set = new ArrayList<Integer>(); // Print the number of 2s that divide n while (n % 2 == 0) { n /= 2; set.add(2); } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i * i <= n; i += 2) { // While i divides n, print i and divide n while (n % i == 0) { n /= i; set.add(i); } } if (n > 1) set.add(n); return set; } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; return gcd(b, a % b); } public static int[] swap(int data[], int left, int right) { // Swap the data int temp = data[left]; data[left] = data[right]; data[right] = temp; // Return the updated array return data; } static long nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } } class Graph { private int V; // No. of vertices // Array of lists for // Adjacency List Representation private LinkedList<Integer> adj[]; Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList(); } // Function to add an edge into the graph void addEdge(int v, int w) { adj[v].add(w); // Add w to v's list. } // A function used by DFS void DFSUtil(int v, boolean visited[]) { visited[v] = true; Iterator<Integer> i = adj[v].listIterator(); while (i.hasNext()) { int n = i.next(); if (!visited[n]) { DFSUtil(n, visited); } } } int DFS(int n) { int count = 0; // Mark all the vertices as // not visited(set as // false by default in java) boolean visited[] = new boolean[V]; for (int i = 1; i <= n; i++) { if (!visited[i]) { count++; DFSUtil(i, visited); } } return count; // Call the recursive helper // function to print DFS // traversal } } class Pair implements Comparable<Pair> { int tower; int value = 0; Pair(int x, int y) { tower = x; value = y; } public int compareTo(Pair o) { return this.value - o.value; } } class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } int[] nextIntArray(int n) throws NumberFormatException, IOException { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } long[] nextLongArray(int n) throws NumberFormatException, IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
51010b04d5deca4b1b227cd8cd65fe41
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.StringTokenizer; public class Solve { static int mod = 1000_000_000 + 7; static long INF = 1000_000_000_000_000L + 100; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); PrintWriter pw = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long a[] = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = sc.nextInt(); int ans = 0; for (int i = 1; i <= n; i++) { long cnt = a[i]; for (long j = a[i]; j <= 2 * n; j += cnt) { long idx = j - i; if (idx <= i) continue; if (idx > n) continue; if (idx < 1) continue; if (a[i] * a[(int) idx] == (i) + (idx)) ans++; } } pw.println(ans); } pw.flush(); } static long LowerBound(int a[], int x, int start) { // x is the target value or key int l = start, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static long UpperBound(int a[], int x, int start) {// x is the key or target value int l = start, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static public boolean[] sieve(int n) { boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> set = new ArrayList<Integer>(); // Print the number of 2s that divide n while (n % 2 == 0) { n /= 2; set.add(2); } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i * i <= n; i += 2) { // While i divides n, print i and divide n while (n % i == 0) { n /= i; set.add(i); } } if (n > 1) set.add(n); return set; } static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; return gcd(b, a % b); } public static int[] swap(int data[], int left, int right) { // Swap the data int temp = data[left]; data[left] = data[right]; data[right] = temp; // Return the updated array return data; } static long nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; // The array C is going to store last // row of pascal triangle at the end. // And last entry of last row is nCr int C[] = new int[r + 1]; C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (int i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (int j = Math.min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } } class Graph { private int V; // No. of vertices // Array of lists for // Adjacency List Representation private LinkedList<Integer> adj[]; Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList(); } // Function to add an edge into the graph void addEdge(int v, int w) { adj[v].add(w); // Add w to v's list. } // A function used by DFS void DFSUtil(int v, boolean visited[]) { visited[v] = true; Iterator<Integer> i = adj[v].listIterator(); while (i.hasNext()) { int n = i.next(); if (!visited[n]) { DFSUtil(n, visited); } } } int DFS(int n) { int count = 0; // Mark all the vertices as // not visited(set as // false by default in java) boolean visited[] = new boolean[V]; for (int i = 1; i <= n; i++) { if (!visited[i]) { count++; DFSUtil(i, visited); } } return count; // Call the recursive helper // function to print DFS // traversal } } class Pair implements Comparable<Pair> { int tower; int value = 0; Pair(int x, int y) { tower = x; value = y; } public int compareTo(Pair o) { return this.value - o.value; } } class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws IOException { while (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } int[] nextIntArray(int n) throws NumberFormatException, IOException { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } long[] nextLongArray(int n) throws NumberFormatException, IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(next()); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
d7504ba87a0feb60e08f0fbab0bef211
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class cp{ //======================================================================================// //======================================================================================// static void sopint(int a) {System.out.print(a);} static void soplnlong(long a) {System.out.println(a);} static void sopln(int a){System.out.println(a);} static void ln(){System.out.println();} static void print(int arr[]){ for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" ");} static void printGrid(int arr[][]){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[0].length;j++) System.out.print(arr[i][j]+" "); System.out.println(); } } static final int mod = 1000000000+7; static int lcmI(int a,int b){ return (a*b)/gcdI(a,b);} static long lcmL(long a,long b){ return (a*b)/gcdL(a,b);} static void swap(int[] arr,int i,int j){ int temp=arr[j]; arr[j]=arr[i]; arr[i]=temp; } static int gcdI(int a, int b) { if (a == 0) return b; return gcdI(b % a, a);} static long gcdL(long a, long b) { if (a == 0) return b; return gcdL(b % a, a);} static long fact(long n) { long ans = 1; for (int i = 2; i <= n; i++) { ans = (ans * i) % mod;} return ans;} static void sieveOfEratosthenes(boolean prime[], int size) { Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int p = 2; p * p < size; p++) { if (prime[p] == true) { for (int i = p * p; i < size; i += p) { prime[i] = false;}}}} //==============================================================================// //===========================================================================// public static void main (String[] args) throws java.lang.Exception { //your code goes here// FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0){ long n=sc.nextLong(); long arr[]=sc.IL((int)n); long res=0; for(int i=0;i<n;i++){ long index=arr[i]-(i+1); for(long j=index;j<=n;j=j+arr[i]){ if(j-1>i&&j<=n){ if(arr[i]*arr[(int)(j-1)]==i+1+j){ res++;} } } } System.out.println(res); } } } //-------------------------Fast Reader class----------------------------------// class FastReader { BufferedReader br; StringTokenizer st; public FastReader() {br = new BufferedReader(new InputStreamReader(System.in));} String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken();} int nextInt() { return Integer.parseInt(next());} long nextLong() { return Long.parseLong(next());} double nextDouble() { return Double.parseDouble(next());} String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str;} int[] II(int n) { int[] d = new int[n]; String[] arr = nextLine().split(" "); for (int i = 0; i < n; i++) d[i] = Integer.parseInt(arr[i]); return d; } String[] IS(int n) {return nextLine().split(" ");} long[] IL(int n) { long[] d = new long[n]; String[] arr = nextLine().split(" "); for (int i = 0; i < n; i++) d[i] = Long.parseLong(arr[i]); return d; }} //----------------------------------FRC End-------------------------------------//
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
1a9367c75d0f0bfcfd098cc28b14be5c
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; // Created by @thesupremeone on 25/06/21 public class B { void solve() throws IOException { int ts = getInt(); for (int t = 1; t <= ts; t++){ int n = getInt(); int[] a = new int[n+1]; for (int i = 1; i <= n; i++){ a[i] = getInt(); } long count = 0; for (int i = 1; i <= n; i++) { int ai = a[i]; int j = ai-i; while (j<=n){ if(j>i){ if((i+j)/ai==a[j]){ count++; } }else { j += ai*Math.floorDiv(i-j, ai); } j += ai; } } println(count); } } public static void main(String[] args) throws Exception { if (isOnlineJudge()) { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); new B().solve(); out.flush(); } else { localJudge = new Thread(); in = new BufferedReader(new FileReader("input.txt")); out = new BufferedWriter(new FileWriter("output.txt")); localJudge.start(); new B().solve(); out.flush(); localJudge.suspend(); } } static boolean isOnlineJudge(){ try { return System.getProperty("ONLINE_JUDGE")!=null || System.getProperty("LOCAL")==null; }catch (Exception e){ return true; } } // Fast Input & Output static Thread localJudge = null; static BufferedReader in; static StringTokenizer st; static BufferedWriter out; static String getLine() throws IOException{ return in.readLine(); } static String getToken() throws IOException{ if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(getLine()); return st.nextToken(); } static int getInt() throws IOException { return Integer.parseInt(getToken()); } static long getLong() throws IOException { return Long.parseLong(getToken()); } static void print(Object s) throws IOException{ out.write(String.valueOf(s)); } static void println(Object s) throws IOException{ out.write(String.valueOf(s)); out.newLine(); } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
b0efcbbcbcae413a956864ba779465b4
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int o = 0; o < t; o++) { int n = scanner.nextInt(); int[] a = new int[n+1]; for (int i = 1; i <= n; i++) { a[i] = scanner.nextInt(); } long res = 0; for (int i = 1; i < n; i++) { int k=1; while (true) { int j = a[i] * k - i; if(j > n) break; if(i < j) { if((long)a[i] * a[j] == i + j) { res++; } } k++; } } System.out.println(res); } } }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
ada3aa2b7876de553c68cc237799c860
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.*; import java.util.*; /* array 𝑎1,𝑎2,…,𝑎𝑛 Count the number of pairs of indices (𝑖,𝑗) such that 𝑖<𝑗 and 𝑎𝑖⋅𝑎𝑗=𝑖+𝑗. 3 : testCases 2 : length of array 𝑎 3 1: 𝑛 space separated integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤2⋅𝑛) ---- 3 6 1 5 ---- 5 3 1 5 9 2 */ public class Main { public static int countPairs(int arr[], int n) { int result = 0; int arr_2[] = new int[2 * n + 1]; for (int i = 0; i <= 2 * n; i++) arr_2[i] = Integer.MAX_VALUE; for (int i = 0; i < n; i++) arr_2[arr[i]] = i + 1; for (int i = 3; i < n * 2; i++) { for (int j = 1; j <= Math.sqrt(i); j++) { if (i % j == 0 && i != j * j) { if (arr_2[j] + arr_2[i/j] == i) result++; } } } return result; } public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int testCases = sc.nextInt(); while (testCases-- > 0) { int n = sc.nextInt(); String[] temp = sc.nextLine().split(" "); int [] arr = new int [n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(temp[i]); } System.out.println(countPairs(arr, n)); } /* int n = sc.nextInt(); // read input as integer long k = sc.nextLong(); // read input as long double d = sc.nextDouble(); // read input as double String str = sc.next(); // read input as String String s = sc.nextLine(); // read whole line as String int result = 3*n; out.println(result); // print via PrintWriter */ out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } //-------------------------------------------------------- }
Java
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
b11ad11e62d22c268fd9c75fd1fdc742
train_107.jsonl
1624635300
You are given an array $$$a_1, a_2, \dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
256 megabytes
import java.io.BufferedReader; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintWriter; public class B { public static void main(String[] args) { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out, true); int cases = fr.nextInt(); for(int c = 0; c < cases; c++) { int size = fr.nextInt(); long[] nums = new long[size+1]; for(int i = 1; i < size+1; i++) { nums[i] = fr.nextLong(); } int total = 0; for(int i = 1; i <= size; i++) { int j = 1; while((nums[i] * j) - i <= size) { int position = (int)(j * nums[i]) - i; if (position > i && position <= size && nums[position] * nums[i] == position + i) { total++; } j += 1; } } out.write(total + "\n"); } out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { this.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
["3\n2\n3 1\n3\n6 1 5\n5\n3 1 5 9 2"]
2 seconds
["1\n1\n3"]
NoteFor the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 \cdot a_2 = 1 + 2 = 3$$$For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$.For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
Java 11
standard input
[ "brute force", "implementation", "math", "number theory" ]
0ce05499cd28f0825580ff48dae9e7a9
The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^5$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 2 \cdot n$$$) — the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
1,200
For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i &lt; j$$$ and $$$a_i \cdot a_j = i + j$$$.
standard output
PASSED
5940d0fd2749ce7b4e23c0f4022c6dd9
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Arrays; import java.util.Scanner; import java.util.stream.Collectors; public class CFMain { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int[] values = new int[t]; for (int i = 0; i < t; i++) { values[i] = sc.nextInt(); } sc.close(); for (int i = 0; i < t; i++) { System.out.println(getPermutation(values[i])); } } private static String getPermutation(int n) { int[] cats = new int[n]; for (int i = 0; i < n - 2 - (n % 2); i++) { cats[i] = i % 2 == 0 ? i + 1 : i - 1; } if (n % 2 == 0) { cats[n - 1] = n - 2; cats[n - 2] = n - 1; } else { cats[n - 1] = n - 2; cats[n - 2] = n - 3; cats[n - 3] = n - 1; } for (int i = 0; i < n; i++) { cats[i] = cats[i] + 1; } return Arrays.stream(cats).mapToObj(i -> Integer.toString(i)).collect(Collectors.joining(" ")); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
8b61ff6af487fb698ee2c087d04c6275
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class pretty{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t>0) {int n,a[]; n=sc.nextInt(); a=new int[n]; for(int i=0;i<n;i++) { a[i]=i+1; } if(n==2) {System.out.println(2+" "+1);} else if(n==3) {System.out.println(3+" "+1+" "+2);} else if(n%2==0) { for(int i=0;i<n;i=i+2) { int temp=0; temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } for(int i=0;i<n;i++) { System.out.println(a[i]+" "); } } else { for(int i=0;i<n-1;i=i+2) { int temp=0; temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } int temp=0; temp=a[n-2]; a[n-2]=a[n-1]; a[n-1]=temp; for(int i=0;i<n;i++) { System.out.println(a[i]+" "); } } t--; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
0ef52dc0df1f48d01f35a23788c83ebd
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = i+1; } for(int i=0;i<n-1;i+=2){ swap(arr,i,i+1); } if(n%2==1){ swap(arr,n-2,n-1); } for(int i=0;i<n;i++){ System.out.print(arr[i]+" "); } } } public static void swap(int arr[],int i,int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
8faf014ab4701c13b60d9a281b1fd562
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.awt.image.AreaAveragingScaleFilter; import java.io.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; public class Pratice { static final long mod = 1000000007; static StringBuilder sb = new StringBuilder(); static int xn = (int) (2e5 + 10); static long ans; static boolean prime[] = new boolean[1000001]; // calculate sqrt and cuberoot // static Set<Long> set=new TreeSet<>(); // static // { // long n=1000000001; // // // for(int i=1;i*i<=n;i++) // { // long x=i*i; // set.add(x); // } // for(int i=1;i*i*i<=n;i++) // { // long x=i*i*i; // set.add(x); // } // } static void sieveOfEratosthenes() { for (int i = 0; i <= 1000000; i++) prime[i] = true; prime[0] = false; prime[1] = false; for (int p = 2; p * p <= 1000000; p++) { if (prime[p] == true) { for (int i = p * p; i <= 1000000; i += p) prime[i] = false; } } } public static void main(String[] args) throws IOException { Reader reader = new Reader(); int t = reader.nextInt(); while (t-- > 0) { int n=reader.nextInt(); if (n % 2 == 0) { for (int i = 1; i <= n; i += 2) { System.out.printf("%d %d ", i + 1, i); } } else { for (int i = 1; i <= n - 3; i += 2) { System.out.printf("%d %d ", i + 1, i); } System.out.printf("%d %d %d", n, n - 2, n - 1); } System.out.println(); } } // static void SieveOfEratosthenes(int n, boolean prime[], // boolean primesquare[], int a[]) // { // // Create a boolean array "prime[0..n]" and // // initialize all entries it as true. A value // // in prime[i] will finally be false if i is // // Not a prime, else true. // for (int i = 2; i <= n; i++) // prime[i] = true; // // /* Create a boolean array "primesquare[0..n*n+1]" // and initialize all entries it as false. // A value in squareprime[i] will finally // be true if i is square of prime, // else false.*/ // for (int i = 0; i < ((n * n) + 1); i++) // primesquare[i] = false; // // // 1 is not a prime number // prime[1] = false; // // for (int p = 2; p * p <= n; p++) { // // If prime[p] is not changed, // // then it is a prime // if (prime[p] == true) { // // Update all multiples of p // for (int i = p * 2; i <= n; i += p) // prime[i] = false; // } // } // // int j = 0; // for (int p = 2; p <= n; p++) { // if (prime[p]) { // // a[j] = p; // // // primesquare[p * p] = true; // j++; // } // } // } // // // static int countDivisors(int n) // { // // if (n == 1) // return 1; // // boolean prime[] = new boolean[n + 1]; // boolean primesquare[] = new boolean[(n * n) + 1]; // // int a[] = new int[n]; // // // SieveOfEratosthenes(n, prime, primesquare, a); // // // int ans = 1; // // // Loop for counting factors of n // for (int i = 0;; i++) { // // a[i] is not less than cube root n // if (a[i] * a[i] * a[i] > n) // break; // // int cnt = 1; // // // if a[i] is a factor of n // while (n % a[i] == 0) { // n = n / a[i]; // // // incrementing power // cnt = cnt + 1; // } // // // ans = ans * cnt; // } // // // if (prime[n]) // ans = ans * 2; // // // Second case // else if (primesquare[n]) // ans = ans * 3; // // // Third case // else if (n != 1) // ans = ans * 4; // // return ans; // Total divisors // } public static long[] inarr(long n) throws IOException { Reader reader = new Reader(); long arr[]=new long[(int) n]; for (long i = 0; i < n; i++) { arr[(int) i]=reader.nextLong(); } return arr; } public static boolean checkPerfectSquare(int number) { int x=number % 10; if (x==2 || x==3 || x==7 || x==8) { return false; } for (int i=0; i<=number/2 + 1; i++) { if (i*i==number) { return true; } } return false; } // check number is prime or not public static boolean isPrime(int n) { return BigInteger.valueOf(n).isProbablePrime(1); } // return the gcd of two numbers public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } // return lcm of number static long lcm(int a, int b) { return (a / gcd(a, b)) * b; } // number of digits in the given number public static long numberOfDigits(long n) { long ans= (long) (Math.floor((Math.log10(n)))+1); return ans; } // return most significant bit in the number public static long mostSignificantNumber(long n) { double k=Math.log10(n); k=k-Math.floor(k); int ans=(int)Math.pow(10,k); return ans; } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ee6b2ca9e073d8e9aab168bede2df6fd
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class PrettyPermutations { // public static int[] permutation(int start, int end, int[] array){ // if (end -start == 2){ // array[end-1] = end - 1 ; // array[end-2] = start ; // array[start-1] = end ; // return array; // } // if (end -start == 1){ // array[end-1] = start ; // array[start-1] = end ; // return array; // } // int middle = start - 1 + (end - start + 1)/2 ; // if ((end - start + 1)%2 == 1) middle+=1; // // 2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 19 17 18 //// System.out.println(start+" "+middle+" "+end); // permutation(start,middle,array); // permutation(middle+1,end,array); // return array; // } public static void main(String[] args) { Scanner a = new Scanner(System.in); int test = a.nextInt(); for (int b = 0 ; b < test ; b++ ){ int n = a.nextInt(); int last = n ; if (n%2 == 1)last -= 3; for (int c = 1 ; c <= last ; c++ ){ if ( c%2 == 0 ) System.out.print(c-1); else System.out.print(c+1); if (c != last) System.out.print(" "); } if (last!=0) System.out.print(" "); if (last<n) System.out.print(n+" "+(n-2)+" "+(n-1)); System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
a04fff628ef44ef2bc2572c208f54934
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in).useLocale(Locale.US); int t = sc.nextInt(); for(int i = 0; i<t; i++) { System.out.println(solve(sc)); } } public static String solve(Scanner sc) { int n = sc.nextInt(); int[] a = new int[n]; for (int i = 1; i<n+1; i++) { a[i-1] = i; } int limit = n%2==0?n:n-1; for (int i = 0; i<limit; i+=2) { swap(a, i, i+1); } if (n%2==1) { swap(a, n-2, n-1); } StringBuilder res = new StringBuilder(""); for (int x: a) { res.append(x + " "); } return res.toString(); } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
2ae5bc127b953041cca0f06b63d5f194
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 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){ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0; i<n; i++){ arr[i] = i+1; } for(int i=0; i<n-1; i+=2){ arr[i] = arr[i] ^ arr[i+1]; arr[i+1] = arr[i] ^ arr[i+1]; arr[i] = arr[i] ^ arr[i+1]; } if(n%2!=0){ arr[n-1] = arr[n-1] ^ arr[n-3]; arr[n-3] = arr[n-1] ^ arr[n-3]; arr[n-1] = arr[n-1] ^ arr[n-3]; } for(int e:arr){ System.out.print(e+" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
42780d6dfa06d760f25c42969eead0b4
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 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){ int n = sc.nextInt(); int arr[] = new int[n]; for(int i=0; i<n; i++){ arr[i] = i+1; } for(int i=0; i<n-1; i+=2){ arr[i] = arr[i] ^ arr[i+1]; arr[i+1] = arr[i] ^ arr[i+1]; arr[i] = arr[i] ^ arr[i+1]; } if(n%2!=0){ arr[n-1] = arr[n-1] ^ arr[n-2]; arr[n-2] = arr[n-1] ^ arr[n-2]; arr[n-1] = arr[n-1] ^ arr[n-2]; } for(int e:arr){ System.out.print(e+" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
07efb83b8fbd81f4fc025f969dbff382
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Solution { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int t = ni(); while( t>0) { int n = ni(); int[] a = new int[n]; if(n%2!=0) { for(int i=0;i<n;i++) { a[i] = i+1; } int k; for(int i=0;i<n-1;i+=2) { k = a[i]; a[i] = a[i+1]; a[i+1] = k; } a[n-1] = a[n-2]; a[n-2] = n; } else{ for(int i=0;i<n/2;i++) { a[2*i] = 2*i+2; a[2*i+1] = 2*i+1; } } for(int i=0;i<n;i++) { System.out.print(a[i]+" "); } System.out.println(); t--; } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Solution().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
06bc0078d23bf7313af16cfcb972141c
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int test = Integer.parseInt(br.readLine()); while(test-->0) { int n = Integer.parseInt(br.readLine()); if(n%2 == 0) { for(int i=1;i<=n;i+=2) { int next = i+1; System.out.print(next+" "+i+" "); } } else { if(n == 1) { System.out.print(1); } else if(n == 3) { System.out.print("3 1 2"); } else { int i; for(i=1;i<=n-4;i+=2) { int next = i+1; System.out.print(next+" "+i+" "); } System.out.print(n+" "+i+" "+(n-1)); } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
eaf37f3127b932f3c481818be44c468b
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class Solution{ public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t= Integer.parseInt(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine()); if(n%2==0){ for(int i=1;i<=n;i+=2){ System.out.print((i+1)+" "+ i+" "); } } else{ for(int i=1;i<=n-3;i+=2){ System.out.print((i+1)+" "+ i+" "); } System.out.println(n+" "+(n-2)+" "+(n-1)); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
81b94a21c6de630be6107ee60c03c360
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top */ public class PrettyPermutation { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while (t > 0){ int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = i+1; } for (int i = 1; i < n; i = i+2) { int temp = arr[i-1]; arr[i-1] = arr[i]; arr[i] = temp; } if(n%2 == 1){ int temp = arr[n-1]; arr[n-1] = arr[n-2]; arr[n-2] = temp; } for (int i = 0; i < n; i++) { out.print(arr[i] +" "); } t--; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
94b2af9559dad1fa866c7a1708741fb8
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top */ public class PrettyPermutation { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while (t > 0){ int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = i+1; } for (int i = 1; i < n; i = i+2) { int temp = arr[i-1]; arr[i-1] = arr[i]; arr[i] = temp; } if(n%2 == 1){ int temp = arr[n-1]; arr[n-1] = arr[n-2]; arr[n-2] = temp; } for (int i = 0; i < n; i++) { System.out.print(arr[i] +" "); } t--; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
252a0537eb7d8cca47c140bfeceda429
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class PrettyPermutation { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while (t > 0){ int n = s.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = i+1; } for (int i = 1; i < n; i = i+2) { int temp = arr[i-1]; arr[i-1] = arr[i]; arr[i] = temp; } if(n%2 == 1){ int temp = arr[n-1]; arr[n-1] = arr[n-2]; arr[n-2] = temp; } for (int i = 0; i < n; i++) { System.out.print(arr[i] +" "); } t--; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ed73a2b61183afd5dbaa0a2ec9947db2
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top */ public class PrettyPermutation { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int test = in.nextInt(); int a[]; for (int i = 0; i < test; i++) { int n=in.nextInt(); int arr[] = new int[n]; a = function(n,arr); for (int j:a) { out.print(j+" "); } out.println(); } } static int[] function(int n,int[] arr){ for (int i = 0; i < n; i++) { arr[i] = i+1; } for (int i = 0; i<n-1 ; i+=2) { swap(arr,i,i+1); } if (n%2==1){ int temp = arr[n-1]; arr[n-1] = arr[n-2]; arr[n-2] = temp; } return arr; } static void swap(int[] arr,int a,int b){ int temp=arr[a]; arr[a]=arr[b]; arr[b]=temp; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
43c2273ed48831caeb2df0b7512a3cee
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class Code { public static void main(String[] args) { Scanner Sc = new Scanner(System.in); int t = Sc.nextInt(); while(t-- >0) { int n = Sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++){ arr[i] = i+1; } for(int i=0;i<n-1;i+=2){ arr[i]^= arr[i+1]; arr[i+1]^= arr[i]; arr[i]^= arr[i+1]; } if((n&1) != 0 && n!=1){ // odd arr[n-1]^=arr[n-2]; arr[n-2]^=arr[n-1]; arr[n-1]^=arr[n-2]; } for(int e:arr){ System.out.print(e+" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
2de5a216f78152a9fa28763d87227a96
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.*; public class div_2_728_a { public static void main(String args[]){ FScanner in = new FScanner(); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-->0) { int n=in.nextInt(); if(n==2 || n==3) { out.print(n+" "); for(int i=1;i<=n-1;i++) out.print(i+" ");} else { ArrayList<Integer> al=new ArrayList<Integer>(); if(n%2==0) { for(int i=2;i<=n;i=i+2) out.print(i+" "+(i-1)+" "); } else { int k=0; for(int i=2;i<n;i=i+2) { al.add(i); al.add(i-1); } int temp=al.get(al.size()-1); al.remove(al.get(al.size()-1)); al.add(n); al.add(temp); for(int i:al) out.print(i+" "); } } out.println(); } out.close(); } static class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
89611eea287f430ebd5ce243631c3b56
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int i = 0; // if (n <= 3) { // for (i = n; i >= 1; i--) { // System.out.print(i + " "); // } // System.out.println(); // } else { if (n % 2 == 0) { for (int j = 1; j < n; j += 2) { System.out.print(j + 1 + " " + j + " "); } System.out.println(); } else { for (i = 1; i < n-3; i += 2) { System.out.print(i + 1 + " " + i + " "); } for (i = n-2; i < n; i++) { System.out.print(i + 1 + " "); } System.out.println(n-2); } // } // if(n%2!=0) { // for(int i=1;i<=n/2;i+=2) { // System.out.print(i+1+" "+i+" "); // } //// System.out.println("x"); // for(int i = n/2+2;i<n;i++) // System.out.print(i+1+" "); // System.out.println(n/2+2); // } // else { // for(int i=1;i<n;i+=2) { // System.out.print(i+1+" "+i+" "); // } // System.out.println(); // } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
e9fc4aa273ef7b338b4ac4f027251392
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Permutations { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); int i, n; while (t-- > 0) { n = in.nextInt(); if (n % 2 != 0) { System.out.print(3+" "+1+" "+2+" "); for (i = 4; i <= n; i+=2) { System.out.print((i + 1) + " " + i+" "); } } else for (i = 1; i <= n; i+=2) { System.out.print((i + 1) + " " + i+" "); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
f2243d0f9d170b668b0b0f1c540bea74
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { public static void main(String args[] ) throws Exception { Solve instance = Solve.getInstance(); instance.test(); } } class Solve{ static FastReader sc; static Solve instance = null; private Solve(){ sc = new FastReader(); } public static Solve getInstance(){ if(instance == null) instance = new Solve(); return instance; } private void input(){ } public void test() { int t = sc.nextInt(); while(t>0) { int n = sc.nextInt(); Integer a[] = new Integer[n+1]; for(int i = 1;i<=n;i++) a[i] = i; for(int i = 1;i+1<=n;i+=2) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } if(n%2 != 0){ int temp = a[n-1]; a[n-1] = a[n]; a[n] = temp; } for(int i = 1;i<=n;i++) sout(a[i]+" "); System.out.println(); t--; } } public void sout(Object s){ System.out.print(s); } public void sout(Object ...s) { for(Object t:s) System.out.println(t); } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
072273f0f63293c4817c22b52116f660
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Roy */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int t = in.readInteger(); for (int cs = 0; cs < t; cs++) { int n = in.readInteger(); if (n == 1) out.printLine("1"); else if (n == 2) out.printLine("2 1"); else if (n == 3) out.printLine("3 1 2"); else if (n == 4) out.printLine("2 1 4 3"); else if (n == 5) out.printLine("3 1 2 5 4"); else { for (int j = 1; j < n - 3; j += 2) { out.print(j + 1 + " " + j + " "); } if (n % 2 == 1) out.printLine(n + " " + (n - 2) + " " + (n - 1)); else out.printLine((n - 2) + " " + (n - 3) + " " + n + " " + (n - 1)); } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInteger() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { this.print(objects); writer.println(); } public void close() { writer.flush(); writer.close(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
866255e081b2b6dfd5c621989d4498e4
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Sanjeev */ public class prettypermutations { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[] a=new int[n]; int i; if(n%2!=0) { for(i=1;i<=n;i++) { if(i==n-2) { System.out.print(n+" "); } else if(i==n-1) { System.out.print(i-1+" "); } else if(i==n) { System.out.print(n-1+" "); } else if(i%2!=0) System.out.print(i+1+" "); else System.out.print(i-1+" "); } System.out.println(); } else { for(i=1;i<=n;i++) { if(i%2!=0) System.out.print(i+1+" "); else System.out.print(i-1+" "); } } } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
67f7f2181bbe941efe21e23c263d3336
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader s = new FastReader(); int t = s.nextInt(); while (t-- > 0) { int x = s.nextInt(); for (int i = 1; i < x - 2; i += 2) { System.out.print((i + 1) + " " + i + " "); } if (x % 2 == 0) { System.out.print((x) + " " + (x - 1) + " "); } else { System.out.print((x) + " " + (x - 2) + " " + (x - 1)); } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
d7935875384ecedb7e41c80901481cff
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 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) { int n = fr.nextInt(); if(n%2==0) { for (int i = 1; i <= n; i++) { if (i % 2 == 0) o.print((i - 1) + " "); else o.print((i + 1) + " "); } } else { for (int i = 1; i <= n-2; i++) { if (i % 2 == 0) o.print((i - 1) + " "); else o.print((i + 1) + " "); } o.print(n + " " + (n-2)); } o.println(); } 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
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
027b393d3a3a1a8f9b3af65f6ec99db2
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { // write your code here PrintWriter out = new PrintWriter(System.out); FastReader scan = new FastReader(); Task solver = new Task(); solver.solve(1,scan,out); out.close(); } static class Task{ public void solve(int testNumber, FastReader scan, PrintWriter out) { int t = scan.nextInt(); for(int i=0; i<t;i++){ int n =scan.nextInt(); if((n&1)==0){ for(int j=1;j<=n;j=j+2){ out.print(j+1+" "); out.print(j+" "); } }else if(n>=3){ out.println(3+" "+1+" "+2); for(int j=4;j<n;j=j+2){ out.print(j+1+" "); out.print(j+" "); } }else{ out.println(2+" "+1); } out.println(); } } public static boolean isPrime(int n){ if(n<2){ return false; } for(int i=2; i<Math.sqrt(n)+1 ;i++){ if(n%i==0){ return false; } } return true; } } 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
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
1df7ef25a77e9bd8df707b0963cf80bc
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Rough2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int test_cases = sc.nextInt(); for(int i=0; i<test_cases; i++) { int cats = sc.nextInt(); int k = 1; if(cats%2==0) while(k<cats) { if(k==1) System.out.print(k+1); else System.out.print(" "+(k+1)); System.out.print(" "+(k)); k+=2; } if(cats%2!=0) { System.out.print(3); System.out.print(" "+1); System.out.print(" "+2); k=4; while(k<cats) { System.out.print(" "+ (k+1)); System.out.print(" " + k); k+=2; } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
b79b56130e75ddffcfb35106ed20039d
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class A_PrettyPermutations_800 { public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while(t-->0) { int N = sc.nextInt(); int curIndex = 1; if(N%2 == 0) { System.out.print(2 + " " + 1 + " "); curIndex += 2; } else { System.out.print(3 + " " + 1 + " " + 2 + " "); curIndex += 3; } for(int i = curIndex; i <= N; i += 2) { System.out.print((i+1) + " " + i + " "); } System.out.println(); } out.close(); } public static PrintWriter 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; } } static class Pair { public int x,y; public Pair(int x, int y) { this.x = x; this.y = y; } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
1d09e9cf946b749647a616f500f82ee3
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class snippet { static StringBuilder sb; static dsu dsu; static long fact[]; static int mod = (int) (1e9 + 7); static void solve() { int n = i(); if(n%2==0){ for(int i=0;i<n;i+=2){ System.out.print((i+2)+" "+(i+1)+" "); } }else{ System.out.print(3+" "+1+" "+2+" "); for(int i=3;i<n;i+=2){ System.out.print((i+2)+" "+(i+1)+" "); } } System.out.println(); } public static boolean check(boolean[] vis){ for(int i=0;i<vis.length;i++){ if(vis[i]==false){ return false; } } return true; } public static void main(String[] args) { sb = new StringBuilder(); int test = i(); while (test-- > 0) { solve(); } // solve(); } /* * fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++) * { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; } */ //**************NCR%P****************** static long ncr(int n, int r) { if (r > n) return (long) 0; long res = fact[n] % mod; // System.out.println(res); res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod; res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod; // System.out.println(res); return res; } static long p(long x, long y)// POWER FXN // { if (y == 0) return 1; long res = 1; while (y > 0) { if (y % 2 == 1) { res = (res * x) % mod; y--; } x = (x * x) % mod; y = y / 2; } return res; } //**************END****************** // *************Disjoint set // union*********// static class dsu { int parent[]; dsu(int n) { parent = new int[n]; for (int i = 0; i < n; i++) parent[i] = -1; } int find(int a) { if (parent[a] < 0) return a; else { int x = find(parent[a]); parent[a] = x; return x; } } void merge(int a, int b) { a = find(a); b = find(b); if (a == b) return; parent[b] = a; } } //**************PRIME FACTORIZE **********************************// static TreeMap<Integer, Integer> prime(long n) { TreeMap<Integer, Integer> h = new TreeMap<>(); long num = n; for (int i = 2; i <= Math.sqrt(num); i++) { if (n % i == 0) { int nt = 0; while (n % i == 0) { n = n / i; nt++; } h.put(i, nt); } } if (n != 1) h.put((int) n, 1); return h; } //****CLASS PAIR ************************************************ static class Pair implements Comparable<Pair> { int x; long y; Pair(int x, long y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return (int) (this.y - o.y); } } //****CLASS PAIR ************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); public static long[] sort(long[] a2) { int n = a2.length; ArrayList<Long> l = new ArrayList<>(); for (long i : a2) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a2[i] = l.get(i); return a2; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x);// % modulus; y--; } x = (x * x);// % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } // ******LOWEST COMMON MULTIPLE // ********************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN******************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArray(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
73a05f26f51f7a34c8847029c638d8ba
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class A { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { solve(); } } private static void solve() { int n = sc.nextInt(); if (n % 2 == 1) { System.out.print("3 1 2 "); for (int i = 4; i <= n; i+=2) { System.out.print(i+1 + " " + i + " "); } } else { for (int i = 1; i <= n; i+=2) { System.out.print(i+1 + " " + i + " "); } } System.out.println(); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
88ef6a6f91fcf5e79834bd7f1d8fc740
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
/*========================================================================== * AUTHOR: RonWonWon * CREATED: 27.06.2021 22:50:23 /*==========================================================================*/ import java.io.*; import java.util.*; public class A { public static void main(String[] args) throws IOException { /* in.ini(); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); */ long startTime = System.nanoTime(); int t = in.nextInt(), T = 1; while(t-->0) { int n = in.nextInt(); int a[] = new int[n]; for(int i=1;i<=n;i++) a[i-1] = i; int i = 0; while(i+1<n){ a[i] = a[i] + a[i+1]; a[i+1] = a[i] - a[i+1]; a[i] = a[i] - a[i+1]; i+=2; } if(i==n-1){ a[n-1] = a[n-1] + a[n-2]; a[n-2] = a[n-1] - a[n-2]; a[n-1] = a[n-1] - a[n-2]; } for(int ii : a) out.print(ii+" "); out.println(); //out.println("Case #"+T+": "+ans); T++; } /* startTime = System.nanoTime() - startTime; System.err.println("Time: "+(startTime/1000000)); */ out.flush(); } static FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static int oo = Integer.MAX_VALUE; static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); void ini() throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch(IOException e) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } 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()); } long[] readArrayL(int n) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } double[] readArrayD(int n) { double a[] = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a; } } static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSortL(long[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n); long temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
467912e1413503f121a6d7e2365d79ae
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; public class PrettyPermutatios { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while(t-->0) { int n = in.nextInt(); if(n%2!=0) { for(int i=0; i<n-3; i++) { if(i%2==0) { System.out.print(i+2); } else { System.out.print(" " + i + " "); } } System.out.print(n + " " + (n-2) + " " + (n-1)); } else { for(int i=0; i<n; i++) { if(i%2==0) { System.out.print(i+2); } else { if(i!=n-1) System.out.print(" " + i +" "); else System.out.print(" " + i); } } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
ac5f9bb6b7f3b8756c1239790a8cf444
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.Scanner; public class Pretty_Permutations { public static void solve(int n){ int[] arr=new int[n+1]; for (int i=1;i<=n;i++){ arr[i]=i; } if (n%2==1){ int temp=arr[n-1]; arr[n-1]=arr[n]; arr[n]=temp; } for (int i=2;i<=n;i+=2){ int temp=arr[i-1]; arr[i-1]=arr[i]; arr[i]=temp; } for (int i=1;i<=n;i++){ System.out.print(arr[i]+" "); } System.out.println(); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while (t-->0){ int a=sc.nextInt(); solve(a); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
9cbfb3385dbceb36240cf549d5b40681
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
//Utilities import java.io.*; import java.util.*; public class A { static int t; static int n; public static void main(String[] args) throws IOException { t = in.iscan(); while (t-- > 0) { n = in.iscan(); if (n % 2 == 1) { out.print("3 1 2 "); for (int i = 4; i <= n; i += 2) { out.print((i+1) + " " + i + " "); } out.println(); } else { for (int i = 1; i <= n; i += 2) { out.print((i+1) + " " + i + " "); } out.println(); } } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static 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 long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static int fast_pow (int b, int x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
2dd9c42e9cc59ca2cb04a2e9c8c81353
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
//--------I--------- import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; //--------O--------- import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.OutputStreamWriter; //--------Arrays--------- import java.util.Arrays; //--------Lists--------- import java.util.List; import java.util.ArrayList; import java.util.LinkedList; import java.util.Collections; import java.util.Comparator; //--------Sets--------- import java.util.Set; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.TreeSet; //--------Stack/Queue--------- import java.util.Stack; import java.util.Queue; import java.util.Random; import java.util.ArrayDeque; import java.util.PriorityQueue; //--------Maps--------- import java.util.Map; import java.util.TreeMap; import java.util.HashMap; import java.util.LinkedHashMap; @SuppressWarnings("unused") public class TEMPLATE { final private static int BUFFER_SIZE = 1 << 16; private static DataInputStream din = new DataInputStream(System.in); private static byte[] buffer = new byte[BUFFER_SIZE]; private static int bufferPointer = 0, bytesRead = 0; public static char sc() throws IOException { byte c = read();while (Character.isWhitespace(c)) c = read();return (char) c; } public static String ss() throws IOException { byte[] buf = new byte[BUFFER_SIZE]; int cnt = 0, c; while ((c = read()) != -1) {if (c == '\n') {if (cnt != 0) break;continue;} buf[cnt++] = (byte) c;} return new String(buf, 0, cnt); } public static int si() throws IOException { int ret = 0;byte c = read();while (c <= ' ') c = read(); boolean neg = (c == '-');if (neg) c = read(); do { ret = ret * 10 + c - '0';} while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret;return ret; } public static int[] sia(int length) throws IOException { int[] ret = new int[length]; for (int i = 0; i < length; ++i) ret[i] = si(); return ret; } public static long sl() throws IOException { long ret = 0;byte c = read();while (c <= ' ') c = read(); boolean neg = (c == '-');if (neg) c = read(); do {ret = ret * 10 + c - '0';} while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret;return ret; } public static long[] sla(int length) throws IOException { long[] ret = new long[length]; for (int i = 0; i < length; ++i) ret[i] = sl(); return ret; } private static void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private static byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public static void main(final String[] args) throws IOException { int tc = si(); while (tc-->0) { int catto = si(); //swap? if (catto % 2 == 0) { for (int i = 2; i <= catto; i += 2) { System.out.print(i + " " + (i-1) + " "); } } else { System.out.print(2 + " " + 3 + " " + 1 + " "); for (int i = 5; i <= catto; i+=2) { System.out.print(i + " " + (i-1) + " "); } } System.out.println(); } } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output
PASSED
6373b039b5552c6dc63a07df5cb0dbcd
train_107.jsonl
1624635300
There are $$$n$$$ cats in a line, labeled from $$$1$$$ to $$$n$$$, with the $$$i$$$-th cat at position $$$i$$$. They are bored of gyrating in the same spot all day, so they want to reorder themselves such that no cat is in the same place as before. They are also lazy, so they want to minimize the total distance they move. Help them decide what cat should be at each location after the reordering.For example, if there are $$$3$$$ cats, this is a valid reordering: $$$[3, 1, 2]$$$. No cat is in its original position. The total distance the cats move is $$$1 + 1 + 2 = 4$$$ as cat $$$1$$$ moves one place to the right, cat $$$2$$$ moves one place to the right, and cat $$$3$$$ moves two places to the left.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); new Solve().solve(n); } } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } class Solve{ public void solve(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++) arr[i]=i+1; if(n%2==0){ for(int i=0;i<n;i+=2){ new template().swap(arr,i,i+1); } }else{ int i=3; int x=arr[2]; arr[2]=arr[1]; arr[1]=arr[0]; arr[0]=x; for(;i<n;i+=2) new template().swap(arr,i,i+1); } for(int x:arr) System.out.print(x+" "); System.out.println(); } } class template{ public boolean isPrime(int n){ List<Boolean> prime=sieve(n); return prime.get(n); } public List<Boolean> sieve(int n){ Boolean[] check=new Boolean[n+1]; Arrays.fill(check,true); check[0]=false;check[1]=false; for(int i=2;i*i<=n;i++){ if(check[i]){ for(int j=i*i;j<=n;j+=i){ check[j]=false; } } } return Arrays.stream(check).collect(Collectors.toList()); } public void reverse(int[] arr){ int l=0,r= arr.length-1; while(l<r){ swap(arr,l,r); l++;r--; } } public void swap(int[] arr,int i,int j){ int x=arr[i]; arr[i]=arr[j]; arr[j]=x; } public int gcd(int a,int b){ if(b==0) return a; System.out.println(a%b); return gcd(b,a%b); } public int getMin(int[] arr){ int min=Integer.MAX_VALUE; for(int x:arr){ if(min>x) min=x; } return min; } public int getMax(int[] arr){ int max=Integer.MIN_VALUE; for(int x:arr) if(max<x) max=x; return max; } }
Java
["2\n2\n3"]
1 second
["2 1 \n3 1 2"]
NoteFor the first test case, there is only one possible permutation that satisfies the conditions: $$$[2, 1]$$$.The second test case was described in the statement. Another possible answer is $$$[2, 3, 1]$$$.
Java 8
standard input
[ "constructive algorithms", "greedy", "implementation" ]
f89bd4ec97ec7e02a7f67feaeb4d3958
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 100$$$) — the number of cats. It can be proven that under the constraints of the problem, an answer always exist.
800
Output $$$t$$$ answers, one for each test case. Each answer consists of $$$n$$$ integers — a permutation with the minimum total distance. If there are multiple answers, print any.
standard output