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
8152b27658b5a55020bed2b893f28814
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 java.util.*; import java.io.*; public class SolutionBBB { 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(); long[] arr = new long[n]; HashMap<Long,Long> map = new HashMap<Long,Long>(); for (int i = 0; i < n; i++) { arr[i] = in.nextLong(); map.put(arr[i], (long)i+1); } Arrays.sort(arr); long count = 0; for (int i = 0; i < n - 1; i++) { long x = map.get(arr[i]); for (int j = i + 1; j < n; j++) { long y = map.get(arr[j]); if (x+y == arr[i] * arr[j]) count++; if (arr[i] * arr[j] > 2 * n) break; } } System.out.print(count); System.out.println(); } } }
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 8
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
a6048084cba4b546f2132f87fac68b65
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 Solution{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); for(int i=0;i<t;i++) { int n = Integer.parseInt(br.readLine()); int[] arr = new int[n+1]; String[] sarr = br.readLine().split(" "); HashMap<Integer,Integer> hm=new HashMap<>(); for (int j = 1; j <= n; j++) { arr[j] = Integer.parseInt(sarr[j - 1]); hm.put(arr[j],j); } int count=0; for(int j=2;j<=n;j++){ for(int k=1;k*arr[j]<=2*n;k++){ if(hm.containsKey(k)) { if ((hm.get(k) == arr[j] * k - j) && hm.get(k) < j) { count++; } } } } /* LinkedHashMap<Integer,Integer> lhm=new LinkedHashMap<>(); lhm.put(1,arr[1]); for(int j=2;j<=n;j++){ for(int k=1;k<j;k++){ if(lhm.get(k)==) } }*/ 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 8
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
e9217c2897c24b906a33eef32c6874e0
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.Scanner; public class Demo1 { public static void main(String args[]) throws Exception{ Scanner sc=new Scanner(System.in); int q=sc.nextInt(); for (int iq=1;iq<=q;iq++){ int n=sc.nextInt(); long ar[]= new long[n+1]; for (int i=0;i<n;i++) ar[i+1]=sc.nextInt(); long ans=0; for (int i=1;i<n+1;i++){ long val=i+ar[i]-(2*i)%ar[i]; while(val<=n){ if(ar[i]*ar[(int)val]==i+val) ans++; val+=ar[i]; } } 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 8
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
6ad1f8e4cbb278dd6fa6ab1ba578a204
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.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { // TODO add check for eof if necessary tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } public static void main(String[] args) throws IOException{ Reader.init(System.in); int tc =Reader.nextInt(); while (tc-->0){ int n =Reader.nextInt(); pair[] arr = new pair[n]; int count=0; for (int i=0; i<n;i++){arr[i]=new pair(i+1,Reader.nextInt());} mergeSort(arr, 0,n-1); //for(pair val:arr){System.out.println(val.val+" ");} for (int i=0; i<n;i++){ int item = 2*n/arr[i].val; int idx= ClosestToTheLeft(arr,item,0); for (int j=idx-1;j>i;j--){ if (arr[i].val*arr[j].val==arr[i].idx+arr[j].idx){count++;} } } System.out.println(count); } } static class pair{ int val; int idx; pair(int i, int v){ this.idx=i; this.val=v; } } static class Sortbyval implements Comparator<pair> { public int compare(pair a, pair b) {return a.val - b.val;} } public static int ClosestToTheLeft(pair[] arr, int item, int i){ int low=i; int high = arr.length-1; int idx=-1; while (low<=high){ int mid=low+((high-low)/2); if (arr[mid].val<=item){ idx = Math.max(mid,idx); low=mid+1;} else{high=mid-1;} } return idx+1; } static void mergeSort(pair[] arr, int low, int high) { if (low >= high) { return; } int mid = (low + high) / 2; mergeSort(arr, low, mid); mergeSort(arr, mid + 1, high); merge(arr, low, high); } static void merge(pair[] arr, int low, int high) { int mid = low + ((high - low) / 2); int n1 = mid - low + 1; int n2 = high - mid; pair[] temp1 = new pair[n1]; pair[] temp2 = new pair[n2]; for (int i = 0; i < n1; i++) { temp1[i] = arr[low + i]; } long sum = 0; for (int i = 0; i < n2; i++) { temp2[i] = arr[mid + 1 + i]; } int i = 0; int j = 0; int k = low; while (i < n1 && j < n2) { if (temp1[i].val <= temp2[j].val) { arr[k] = temp1[i]; i++; } else { arr[k] = temp2[j]; j++; } k++; } while (i < n1) { arr[k] = temp1[i]; i++; k++; } while (j < n2) { arr[k] = temp2[j]; j++; k++; } } }
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 8
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
ac95ed0acff7e4b0fdc8b61b9a728bd2
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 abc { //utilities //Main public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long a[][] =new long[n][2]; for (int i = 0; i <n ; i++) { a[i][0]=sc.nextInt(); a[i][1]=i+1; } Arrays.sort(a,(c,d)-> (int) (c[0]-d[0])); long ans=0; for(int i=0;i<n;i++) { for (int j = i + 1; j < n; j++) { if (a[i][0] *(int) a[j][0] >= 2 * n) break; else { if (a[i][0] *(int) a[j][0] == a[i][1] +a[j][1]) 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 8
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
d9cb99a01e047b6265b48ab5f443bb47
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 codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main(String[] args) throws java.lang.Exception { FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); int t = 1; t = in.nextInt(); while (t > 0) { --t; int n = in.nextInt(); long arr[] = new long[n]; for(int i = 0;i<n;i++) arr[i] = in.nextLong(); int count = 0; for(int i = 0;i<n;i++) { int k = 1; while(arr[i]*k-(i+1)<=n && k<=2*n) { long x = arr[i]*k-(i+1)-1; if(x>= 0 && x<n && arr[(int)x] == k) { if(x>i) ++count; } ++k; } } sb.append(count+"\n"); } System.out.print(sb); } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } } class FastReader { byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()) ; boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } }
Java
["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 8
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
f27eddb05cc4cf91c846d0935ca6e821
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.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class contest728 { public static void main(String[] args) { FastScanner fs = new FastScanner(); int T = 1; T=fs.nextInt(); for (int tt = 0; tt < T; tt++) { int n= fs.nextInt(); long arr[] = new long[n+1]; for(int i=0;i<n;i++){ arr[i+1] =fs.nextLong(); } int cnt=0; for(int i=1;i<=n;i++){ long v= arr[i]; for(int j=(int)(arr[i]-i);j<=n;j+=(int)v){ if(j<=i){continue;} if(arr[j]==((i+j)/arr[i])){ cnt+=1; } } } System.out.println(cnt); } } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
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 8
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
fb88df052d22bf73e51e944ca1f140f4
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.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; public final class CFPS { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static final int mod = 998244353; static int t = 1; static double epsilon = 0.0000000001; static boolean[] isPrime; static int[] smallestFactorOf; static final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3; static long cmp; @SuppressWarnings({"unused"}) public static void main(String[] args) throws Exception { t = fr.nextInt(); OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(); int[] arr = fr.nextIntArray(n); // a[i].a[j] = i + j // -- // once a[i].a[j] exceeds 2*n, we don't need to care int[] valIdxs = new int[2 * n + 1]; Arrays.fill(valIdxs, -1); for (int i = 0; i < n; i++) valIdxs[arr[i]] = i; int ans = 0; for (int ival = 1; ival < 2 * n + 1; ival++) { int ii = valIdxs[ival] + 1; if (ii == 0) continue; for (int jval = ival + 1; jval < 2 * n + 1; jval++) { int jj = valIdxs[jval] + 1; if (jj == 0) continue; if (ival * (long) jval > 2 * n) break; if (ival * jval == ii + jj) ans++; } } out.println(ans); } out.close(); } static void compute_automaton(String s, int[][] aut) { s += '#'; int n = s.length(); int[] pi = prefix_function(s.toCharArray()); for (int i = 0; i < n; i++) { for (int c = 0; c < 26; c++) { int j = i; while (j > 0 && 'A' + c != s.charAt(j)) j = pi[j-1]; if ('A' + c == s.charAt(j)) j++; aut[i][c] = j; } } } static void timeDFS(int current, int from, UGraph ug, int[] time, int[] tIn, int[] tOut) { tIn[current] = ++time[0]; for (int adj : ug.adj(current)) if (adj != from) timeDFS(adj, current, ug, time, tIn, tOut); tOut[current] = ++time[0]; } static class Pair implements Comparable<Pair> { int first, second; int idx; Pair() { first = second = 0; } Pair (int ff, int ss, int ii) { first = ff; second = ss; idx = ii; } Pair (int ff, int ss) { first = ff; second = ss; idx = -1; } public int compareTo(Pair that) { cmp = first - that.first; if (cmp == 0) cmp = second - that.second; return (int) cmp; } } static boolean areCollinear(long x1, long y1, long x2, long y2, long x3, long y3) { // we will check if c3 lies on line through (c1, c2) long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); return a == 0; } static int[] treeDiameter(UGraph ug) { int n = ug.V(); int farthest = -1; int[] distTo = new int[n]; diamDFS(0, -1, 0, ug, distTo); int maxDist = -1; for (int i = 0; i < n; i++) if (maxDist < distTo[i]) { maxDist = distTo[i]; farthest = i; } distTo = new int[n + 1]; diamDFS(farthest, -1, 0, ug, distTo); distTo[n] = farthest; return distTo; } static void diamDFS(int current, int from, int dist, UGraph ug, int[] distTo) { distTo[current] = dist; for (int adj : ug.adj(current)) if (adj != from) diamDFS(adj, current, dist + 1, ug, distTo); } static class TreeDistFinder { UGraph ug; int n; int[] depthOf; LCA lca; TreeDistFinder(UGraph ug) { this.ug = ug; n = ug.V(); depthOf = new int[n]; depthCalc(0, -1, ug, 0, depthOf); lca = new LCA(ug, 0); } TreeDistFinder(UGraph ug, int a) { this.ug = ug; n = ug.V(); depthOf = new int[n]; depthCalc(a, -1, ug, 0, depthOf); lca = new LCA(ug, a); } private void depthCalc(int current, int from, UGraph ug, int depth, int[] depthOf) { depthOf[current] = depth; for (int adj : ug.adj(current)) if (adj != from) depthCalc(adj, current, ug, depth + 1, depthOf); } public int dist(int a, int b) { int lc = lca.lca(a, b); return (depthOf[a] - depthOf[lc] + depthOf[b] - depthOf[lc]); } } public static long[][] GCDSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; } else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = gcd(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeGCDQ(long[][] table, int l, int r) { // [a,b) if(l > r)return 1; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return gcd(table[t][l], table[t][r-(1<<t)]); } static class Trie { TrieNode root; Trie(char[][] strings) { root = new TrieNode('A', false); construct(root, strings); } public Stack<String> set(TrieNode root) { Stack<String> set = new Stack<>(); StringBuilder sb = new StringBuilder(); for (TrieNode next : root.next) collect(sb, next, set); return set; } private void collect(StringBuilder sb, TrieNode node, Stack<String> set) { if (node == null) return; sb.append(node.character); if (node.isTerminal) set.add(sb.toString()); for (TrieNode next : node.next) collect(sb, next, set); if (sb.length() > 0) sb.setLength(sb.length() - 1); } private void construct(TrieNode root, char[][] strings) { // we have to construct the Trie for (char[] string : strings) { if (string.length == 0) continue; root.next[string[0] - 'a'] = put(root.next[string[0] - 'a'], string, 0); if (root.next[string[0] - 'a'] != null) root.isLeaf = false; } } private TrieNode put(TrieNode node, char[] string, int idx) { boolean isTerminal = (idx == string.length - 1); if (node == null) node = new TrieNode(string[idx], isTerminal); node.character = string[idx]; node.isTerminal |= isTerminal; if (!isTerminal) { node.isLeaf = false; node.next[string[idx + 1] - 'a'] = put(node.next[string[idx + 1] - 'a'], string, idx + 1); } return node; } class TrieNode { char character; TrieNode[] next; boolean isTerminal, isLeaf; boolean canWin, canLose; TrieNode(char c, boolean isTerminallll) { character = c; isTerminal = isTerminallll; next = new TrieNode[26]; isLeaf = true; } } } static class Edge implements Comparable<Edge> { int from, to; long weight; int id; // int hash; Edge(int fro, int t, long wt, int i) { from = fro; to = t; id = i; weight = wt; // hash = Objects.hash(from, to, weight); } /*public int hashCode() { return hash; }*/ public int compareTo(Edge that) { return Long.compare(this.id, that.id); } } public static long[][] minSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeMinQ(long[][] table, int l, int r) { // [a,b) if(l >= r)return Integer.MAX_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.min(table[t][l], table[t][r-(1<<t)]); } public static long[][] maxSparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.max(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } public static long sparseRangeMaxQ(long[][] table, int l, int r) { // [a,b) if(l >= r)return Integer.MIN_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.max(table[t][l], table[t][r-(1<<t)]); } static class LCA { int[] height, first, segtree; ArrayList<Integer> euler; boolean[] visited; int n; LCA(UGraph ug, int root) { n = ug.V(); height = new int[n]; first = new int[n]; euler = new ArrayList<>(); visited = new boolean[n]; dfs(ug, root, 0); int m = euler.size(); segtree = new int[m * 4]; build(1, 0, m - 1); } void dfs(UGraph ug, int node, int h) { visited[node] = true; height[node] = h; first[node] = euler.size(); euler.add(node); for (int adj : ug.adj(node)) { if (!visited[adj]) { dfs(ug, adj, h + 1); euler.add(node); } } } void build(int node, int b, int e) { if (b == e) { segtree[node] = euler.get(b); } else { int mid = (b + e) / 2; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); int l = segtree[node << 1], r = segtree[node << 1 | 1]; segtree[node] = (height[l] < height[r]) ? l : r; } } int query(int node, int b, int e, int L, int R) { if (b > R || e < L) return -1; if (b >= L && e <= R) return segtree[node]; int mid = (b + e) >> 1; int left = query(node << 1, b, mid, L, R); int right = query(node << 1 | 1, mid + 1, e, L, R); if (left == -1) return right; if (right == -1) return left; return height[left] < height[right] ? left : right; } int lca(int u, int v) { int left = first[u], right = first[v]; if (left > right) { int temp = left; left = right; right = temp; } return query(1, 0, euler.size() - 1, left, right); } } static class FenwickTree { long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new long[size + 1]; } public long rsq(int ind) { assert ind > 0; long sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public long rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, long value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } static class Point implements Comparable<Point> { long x; long y; long z; long id; // private int hashCode; Point() { x = z = y = 0; // this.hashCode = Objects.hash(x, y, cost); } Point(Point p) { this.x = p.x; this.y = p.y; this.z = p.z; this.id = p.id; // this.hashCode = Objects.hash(x, y, cost); } Point(long x, long y, long z, long id) { this.x = x; this.y = y; this.z = z; this.id = id; // this.hashCode = Objects.hash(x, y, id); } Point(long a, long b) { this.x = a; this.y = b; this.z = 0; // this.hashCode = Objects.hash(a, b); } Point(long x, long y, long id) { this.x = x; this.y = y; this.id = id; } @Override public int compareTo(Point o) { if (this.x < o.x) return -1; if (this.x > o.x) return 1; if (this.y < o.y) return -1; if (this.y > o.y) return 1; if (this.z < o.z) return -1; if (this.z > o.z) return 1; return 0; } @Override public boolean equals(Object that) { return this.compareTo((Point) that) == 0; } } static class BinaryLift { // FUNCTIONS: k-th ancestor and LCA in log(n) int[] parentOf; int maxJmpPow; int[][] binAncestorOf; int n; int[] lvlOf; // How this works? // a. For every node, we store the b-ancestor for b in {1, 2, 4, 8, .. log(n)}. // b. When we need k-ancestor, we represent 'k' in binary and for each set bit, we // lift level in the tree. public BinaryLift(UGraph tree) { n = tree.V(); maxJmpPow = logk(n, 2) + 1; parentOf = new int[n]; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); parentConstruct(0, -1, tree, 0); binConstruct(); } // TODO: Implement lvlOf[] initialization public BinaryLift(int[] parentOf) { this.parentOf = parentOf; n = parentOf.length; maxJmpPow = logk(n, 2) + 1; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); UGraph tree = new UGraph(n); for (int i = 1; i < n; i++) tree.addEdge(i, parentOf[i]); binConstruct(); parentConstruct(0, -1, tree, 0); } private void parentConstruct(int current, int from, UGraph tree, int depth) { parentOf[current] = from; lvlOf[current] = depth; for (int adj : tree.adj(current)) if (adj != from) parentConstruct(adj, current, tree, depth + 1); } private void binConstruct() { for (int node = 0; node < n; node++) for (int lvl = 0; lvl < maxJmpPow; lvl++) binConstruct(node, lvl); } private int binConstruct(int node, int lvl) { if (node < 0) return -1; if (lvl == 0) return binAncestorOf[node][lvl] = parentOf[node]; if (node == 0) return binAncestorOf[node][lvl] = -1; if (binAncestorOf[node][lvl] != -1) return binAncestorOf[node][lvl]; return binAncestorOf[node][lvl] = binConstruct(binConstruct(node, lvl - 1), lvl - 1); } // return ancestor which is 'k' levels above this one public int ancestor(int node, int k) { if (node < 0) return -1; if (node == 0) if (k == 0) return node; else return -1; if (k > (1 << maxJmpPow) - 1) return -1; if (k == 0) return node; int ancestor = node; int highestBit = Integer.highestOneBit(k); while (k > 0 && ancestor != -1) { ancestor = binAncestorOf[ancestor][logk(highestBit, 2)]; k -= highestBit; highestBit = Integer.highestOneBit(k); } return ancestor; } public int lca(int u, int v) { if (u == v) return u; // The invariant will be that 'u' is below 'v' initially. if (lvlOf[u] < lvlOf[v]) { int temp = u; u = v; v = temp; } // Equalizing the levels. u = ancestor(u, lvlOf[u] - lvlOf[v]); if (u == v) return u; // We will now raise level by largest fitting power of two until possible. for (int power = maxJmpPow - 1; power > -1; power--) if (binAncestorOf[u][power] != binAncestorOf[v][power]) { u = binAncestorOf[u][power]; v = binAncestorOf[v][power]; } return ancestor(u, 1); } } static class DFSTree { // NOTE: The thing is made keeping in mind that the whole // input graph is connected. UGraph tree; UGraph backUG; int hasBridge; int n; Edge backEdge; DFSTree(UGraph ug) { this.n = ug.V(); tree = new UGraph(n); hasBridge = -1; backUG = new UGraph(n); treeCalc(0, -1, new boolean[n], ug); } private void treeCalc(int current, int from, boolean[] marked, UGraph ug) { if (marked[current]) { // This is a backEdge. backUG.addEdge(from, current); backEdge = new Edge(from, current, 1, 0); return; } if (from != -1) tree.addEdge(from, current); marked[current] = true; for (int adj : ug.adj(current)) if (adj != from) treeCalc(adj, current, marked, ug); } public boolean hasBridge() { if (hasBridge != -1) return (hasBridge == 1); // We have to determine the bridge. bridgeFinder(); return (hasBridge == 1); } int[] levelOf; int[] dp; private void bridgeFinder() { // Finding the level of each node. levelOf = new int[n]; levelDFS(0, -1, 0); // Applying DP solution. // dp[i] -> Highest level reachable from subtree of 'i' using // some backEdge. dp = new int[n]; Arrays.fill(dp, Integer.MAX_VALUE / 100); dpDFS(0, -1); // Now, we will check each edge and determine whether its a // bridge. for (int i = 0; i < n; i++) for (int adj : tree.adj(i)) { // (i -> adj) is the edge. if (dp[adj] > levelOf[i]) hasBridge = 1; } if (hasBridge != 1) hasBridge = 0; } private void levelDFS(int current, int from, int lvl) { levelOf[current] = lvl; for (int adj : tree.adj(current)) if (adj != from) levelDFS(adj, current, lvl + 1); } private int dpDFS(int current, int from) { dp[current] = levelOf[current]; for (int back : backUG.adj(current)) dp[current] = Math.min(dp[current], levelOf[back]); for (int adj : tree.adj(current)) if (adj != from) dp[current] = Math.min(dp[current], dpDFS(adj, current)); return dp[current]; } } static class UnionFind { // Uses weighted quick-union with path compression. private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { while (p != parent[p]) p = parent[p]; return p; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) grid[i][j] = fr.nextInt(); } return grid; } } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(Comparator<T> cmp) { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { return super.put(key, super.getOrDefault(key, 0) + 1); } public Integer removeCM(T key) { int count = super.getOrDefault(key, -1); if (count == -1) return -1; if (count == 1) return super.remove(key); else return super.put(key, count - 1); } public Integer getCM(T key) { return super.getOrDefault(key, 0); } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static long dioGCD(long a, long b, long[] x0, long[] y0) { if (b == 0) { x0[0] = 1; y0[0] = 0; return a; } long[] x1 = new long[1], y1 = new long[1]; long d = dioGCD(b, a % b, x1, y1); x0[0] = y1[0]; y0[0] = x1[0] - y1[0] * (a / b); return d; } static boolean diophantine(long a, long b, long c, long[] x0, long[] y0, long[] g) { g[0] = dioGCD(Math.abs(a), Math.abs(b), x0, y0); if (c % g[0] > 0) { return false; } x0[0] *= c / g[0]; y0[0] *= c / g[0]; if (a < 0) x0[0] = -x0[0]; if (b < 0) y0[0] = -y0[0]; return true; } static long[][] prod(long[][] mat1, long[][] mat2) { int n = mat1.length; long[][] prod = new long[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) // determining prod[i][j] // it will be the dot product of mat1[i][] and mat2[][i] for (int k = 0; k < n; k++) prod[i][j] += mat1[i][k] * mat2[k][j]; return prod; } static long[][] matExpo(long[][] mat, long power) { int n = mat.length; long[][] ans = new long[n][n]; if (power == 0) return null; if (power == 1) return mat; long[][] half = matExpo(mat, power / 2); ans = prod(half, half); if (power % 2 == 1) { ans = prod(ans, mat); } return ans; } static int KMPNumOcc(char[] text, char[] pat) { int n = text.length; int m = pat.length; char[] patPlusText = new char[n + m + 1]; for (int i = 0; i < m; i++) patPlusText[i] = pat[i]; patPlusText[m] = '^'; // Seperator for (int i = 0; i < n; i++) patPlusText[m + i] = text[i]; int[] fullPi = piCalcKMP(patPlusText); int answer = 0; for (int i = 0; i < n + m + 1; i++) if (fullPi[i] == m) answer++; return answer; } static int[] piCalcKMP(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static boolean[] prefMatchesSuff(char[] s) { int n = s.length; boolean[] res = new boolean[n + 1]; int[] pi = prefix_function(s); res[0] = true; for (int p = n; p != 0; p = pi[p]) res[p] = true; return res; } static int[] prefix_function(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i-1]; while (j > 0 && s[i] != s[j]) j = pi[j-1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static long hash(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static int mapTo1D(int row, int col, int n, int m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static int[] mapTo2D(int idx, int n, int m) { // Inverse of what the one above does. int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } static boolean[] primeGenerator(int upto) { // Sieve of Eratosthenes: isPrime = new boolean[upto + 1]; smallestFactorOf = new int[upto + 1]; Arrays.fill(smallestFactorOf, 1); Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i < upto + 1; i++) if (isPrime[(int) i]) { smallestFactorOf[(int) i] = (int) i; // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) { if (isPrime[(int) j * (int) i]) { isPrime[(int) j * (int) i] = false; smallestFactorOf[(int) j * (int) i] = (int) i; } } } return isPrime; } static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) { if (smallestFactorOf == null) primeGenerator(num + 1); HashMap<Integer, Integer> fnps = new HashMap<>(); while (num != 1) { fnps.put(smallestFactorOf[num], fnps.getOrDefault(smallestFactorOf[num], 0) + 1); num /= smallestFactorOf[num]; } return fnps; } static HashMap<Long, Integer> primeFactorization(long num) { // Returns map of factor and its power in the number. HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } static long nCr(long n, long r, long[] fac) { long p = gigamod; if (r == 0) return 1; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long modDiv(long a, long b){return mod(a * power(b, mod - 2, mod), mod);} static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static boolean isPrime(long n) {if (n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} static String toString(int[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(boolean[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(long[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(char[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+"");return sb.toString();} static String toString(int[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(long[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++) {sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(double[][] dp){StringBuilder sb=new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(char[][] dp){StringBuilder sb = new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+"");}sb.append('\n');}return sb.toString();} static long mod(long a, long m){return(a%m+1000000L*m)%m;} } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
Java
["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 8
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
ba3a75c52d0e104a14e02035708663ac
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 ProblemE { public static void main(String[] args) throws IOException { Reader io = new Reader(); PrintWriter pw = new PrintWriter(System.out); int t = io.nextInt(); while(t-- > 0) { int n = io.nextInt(); long[] arr = new long[n + 1]; for (int i = 1; i < arr.length; i++) { arr[i] = io.nextInt(); } int 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((arr[i] * arr[j] == i + j) && i < j) { ans++; } } } } System.out.println(ans); } io.close(); pw.close(); } static int INF = Integer.MAX_VALUE; static long LINF = Long.MAX_VALUE; public static class Pair<A, B> { A first; B second; // Constructor public Pair(A first, B second) { this.first = first; this.second = second; } } // Kattio is too slow //BeginCodeSnip{Fast Reader} 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; } 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 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; } } //EndCodeSnip{} }
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 8
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
21da2d8b796b661d67061b1826079d41
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 PleasantPair { 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) { } /* FastReader fr = new FastReader(); int numCas = fr.nextInt(); long[] arr; while(numCas-- > 0) { int length = fr.nextInt(); arr = new long[length + 1]; for(int i = 1; i <= length; i++) { arr[i] = fr.nextLong(); } count(arr); } */ } /* public static void count(long[] arr) { 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((long)(arr[i] * arr[j]) == (long)i + j) { count++; } } } } System.out.println(count); } */ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(File f) throws IOException { br = new BufferedReader(new FileReader(f)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int [] readintarray(int n) { int res [] = new int [n]; for(int i = 0; i<n; i++)res[i] = nextInt(); return res; } long [] readlongarray(int n) { long res [] = new long [n]; for(int i = 0; i<n; i++)res[i] = nextLong(); return res; } } }
Java
["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
f6b68d795d58dd164b3058f92889a79d
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.*; // number of prime numbers less then or equal to x are --> x/ln(x) public class b {static class FastScanner {InputStreamReader is;BufferedReader br;StringTokenizer st; public FastScanner() {is = new InputStreamReader(System.in);br = new BufferedReader(is);} String next() throws Exception {while (st == null || !st.hasMoreElements())st = new StringTokenizer(br.readLine()); return st.nextToken();}int nextInt() throws Exception {return Integer.parseInt(next());}long nextLong() throws Exception { return Long.parseLong(next());}int[] readArray(int num) throws Exception {int arr[]=new int[num]; for(int i=0;i<num;i++)arr[i]=nextInt();return arr;}String nextLine() throws Exception {return br.readLine(); }} public static boolean power_of_two(int a){if((a&(a-1))==0){ return true;}return false;} static boolean PS(double x){if (x >= 0) {double i= Math.sqrt(x);if(i%1!=0){ return false;}return ((i * i) == x);}return false;}public static int[] ia(int n){int ar[]=new int[n]; return ar;}public static long[] la(int n) {long ar[]=new long[n];return ar;} public static void print(int ans,int t){System.out.println("Case"+" "+"#"+t+":"+" "+ans);} static long mod=1000000007; static int max=Integer.MIN_VALUE; static int min=Integer.MAX_VALUE; public static void main(String args[]) throws java.lang.Exception { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0) {int n=sc.nextInt();int ar[]=new int[n+1]; for(int i=1;i<=n;i++) { ar[i]=sc.nextInt(); } long ans=0; for(int i=1;i<=n;i++) { int st=ar[i]-(i); for(int j=st;j<=n;j+=ar[i]) { if(j>0) { if((long)ar[i]*ar[j]==i+j && j>i) { 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
a34a27d2dc32b0767c95cad81ede6bef
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.*; // number of prime numbers less then or equal to x are --> x/ln(x) public class b {static class FastScanner {InputStreamReader is;BufferedReader br;StringTokenizer st; public FastScanner() {is = new InputStreamReader(System.in);br = new BufferedReader(is);} String next() throws Exception {while (st == null || !st.hasMoreElements())st = new StringTokenizer(br.readLine()); return st.nextToken();}int nextInt() throws Exception {return Integer.parseInt(next());}long nextLong() throws Exception { return Long.parseLong(next());}int[] readArray(int num) throws Exception {int arr[]=new int[num]; for(int i=0;i<num;i++)arr[i]=nextInt();return arr;}String nextLine() throws Exception {return br.readLine(); }} public static boolean power_of_two(int a){if((a&(a-1))==0){ return true;}return false;} static boolean PS(double x){if (x >= 0) {double i= Math.sqrt(x);if(i%1!=0){ return false;}return ((i * i) == x);}return false;}public static int[] ia(int n){int ar[]=new int[n]; return ar;}public static long[] la(int n) {long ar[]=new long[n];return ar;} public static void print(int ans,int t){System.out.println("Case"+" "+"#"+t+":"+" "+ans);} static long mod=1000000007; static int max=Integer.MIN_VALUE; static int min=Integer.MAX_VALUE; public static void main(String args[]) throws java.lang.Exception { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0) {int n=sc.nextInt();int ar[]=new int[n+1]; for(int i=1;i<=n;i++) { ar[i]=sc.nextInt(); } long ans=0; for(int i=1;i<=n;i++) { int st=ar[i]-(i); for(int j=st;j<=n;j+=ar[i]) { if(j>0) { if((long)ar[i]*ar[j]==i+j && j>i) { 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
5782d9d8aa1b219a5fbf28cfebdeb177
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.*; // number of prime numbers less then or equal to x are --> x/ln(x) public class b {static class FastScanner {InputStreamReader is;BufferedReader br;StringTokenizer st; public FastScanner() {is = new InputStreamReader(System.in);br = new BufferedReader(is);} String next() throws Exception {while (st == null || !st.hasMoreElements())st = new StringTokenizer(br.readLine()); return st.nextToken();}int nextInt() throws Exception {return Integer.parseInt(next());}long nextLong() throws Exception { return Long.parseLong(next());}int[] readArray(int num) throws Exception {int arr[]=new int[num]; for(int i=0;i<num;i++)arr[i]=nextInt();return arr;}String nextLine() throws Exception {return br.readLine(); }} public static boolean power_of_two(int a){if((a&(a-1))==0){ return true;}return false;} static boolean PS(double x){if (x >= 0) {double i= Math.sqrt(x);if(i%1!=0){ return false;}return ((i * i) == x);}return false;}public static int[] ia(int n){int ar[]=new int[n]; return ar;}public static long[] la(int n) {long ar[]=new long[n];return ar;} public static void print(int ans,int t){System.out.println("Case"+" "+"#"+t+":"+" "+ans);} static long mod=1000000007; static int max=Integer.MIN_VALUE; static int min=Integer.MAX_VALUE; public static void main(String args[]) throws java.lang.Exception { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0) {int n=sc.nextInt();int ar[]=new int[n+1]; for(int i=1;i<=n;i++) { ar[i]=sc.nextInt(); } long ans=0; for(int i=1;i<=n;i++) { int st=ar[i]-(i); for(int j=st;j<=n;j+=ar[i]) { if(j>0) { if((long)ar[i]*ar[j]==i+j && j>i) { 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
52c9db00e98342b62e0f41d914d00746
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 edoti { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); long[] a = new long[n + 1]; for (int i = 1; i < n + 1; i++) { a[i] = s.nextInt(); } long count = 0; for (int i = 1; i <= n; i++) { for (int j = (int)a[i]-i; j <= n; j += a[i]) { if (j > 0) { if (a[i] * a[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
9cf87fb1451f5b209af38a8dfaf0202b
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 edoti { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); long[] a = new long[n + 1]; for (int i = 1; i < n + 1; i++) { a[i] = s.nextInt(); } long count = 0; for (int i = 1; i <= n; i++) { for (int j = (int)a[i]-i; j <= n; j += a[i]) { if (j > 0) { if (a[i] * a[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
5da0e36397fcb176362ecd7b7eb02190
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 edoti { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while (t-- > 0) { int n = s.nextInt(); long[] a = new long[n + 1]; for (int i = 1; i < n + 1; i++) { a[i] = s.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) { if (a[i] * a[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
ba5dcbab92e675d6a000d85ff4677ba5
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class b { static PrintWriter out = new PrintWriter(System.out); static void solve(int arr[], int n) { Pair pairs[] = new Pair[n]; for (int i = 0; i < n; i++) { pairs[i] = new Pair(arr[i], i + 1); } Arrays.sort(pairs); int count = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (pairs[i - 1].second + pairs[j - 1].second == (long) pairs[i - 1].first * pairs[j - 1].first) count++; if ((long) pairs[i - 1].first * pairs[j - 1].first > 2 * n) break; } } out.println(count); } // static void solve(int arr[], int n) { // int[] index = new int[2 * n + 1]; // for (int i = 0; i < n; i++) { // index[arr[i]] = i + 1; // } // Arrays.sort(arr); // long res = 0; // for (int i = 0; i < n; i++) { // for (int j = i + 1; j < n; j++) { // if (index[arr[i]] + index[arr[j]] == (long) arr[i] * arr[j]) // res++; // if ((long) arr[i] * arr[j] > 2 * n) // break; // } // } // out.println(res); // } 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); } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(); int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int arr[] = fs.readArray(n); solve(arr, n); } out.close(); } static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { this.first = f; this.second = s; } public int compareTo(Pair o) { return this.first - o.first; } } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// 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
4f35a1a5d23df8e22c800f4a9c477ac3
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class b { static PrintWriter out = new PrintWriter(System.out); static void solve(int arr[], int n) { Pair pairs[] = new Pair[n]; for (int i = 0; i < n; i++) { pairs[i] = new Pair(arr[i], i + 1); } Arrays.sort(pairs); long count = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (pairs[i - 1].second + pairs[j - 1].second == (long) pairs[i - 1].first * pairs[j - 1].first) count++; if ((long) pairs[i - 1].first * pairs[j - 1].first > 2 * n) break; } } out.println(count); } // static void solve(int arr[], int n) { // int[] index = new int[2 * n + 1]; // for (int i = 0; i < n; i++) { // index[arr[i]] = i + 1; // } // Arrays.sort(arr); // long res = 0; // for (int i = 0; i < n; i++) { // for (int j = i + 1; j < n; j++) { // if (index[arr[i]] + index[arr[j]] == (long) arr[i] * arr[j]) // res++; // if ((long) arr[i] * arr[j] > 2 * n) // break; // } // } // out.println(res); // } 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); } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(); int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int arr[] = fs.readArray(n); solve(arr, n); } out.close(); } static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { this.first = f; this.second = s; } public int compareTo(Pair o) { return this.first - o.first; } } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// 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
9a27073ff8aa5bf8217a713544900185
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class b { static PrintWriter out = new PrintWriter(System.out); // static void solve(int arr[], int n) { // Pair pairs[] = new Pair[n]; // for (int i = 0; i < n; i++) { // pairs[i] = new Pair(arr[i], i + 1); // } // Arrays.sort(pairs); // long count = 0; // for (int i = 1; i <= n; i++) { // for (int j = i + 1; j <= n; j++) { // long x = pairs[i - 1].second + pairs[j - 1].second; // i+j // long y = pairs[i - 1].first * pairs[j - 1].first;// ai*aj // if (y > 2 * n) // break; // ai.aj<=2n // if (x == y) // count++; // } // } // out.println(count); // } static void solve(int arr[], int n) { int[] index = new int[2 * n + 1]; for (int i = 0; i < n; i++) { index[arr[i]] = i + 1; } Arrays.sort(arr); long res = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (index[arr[i]] + index[arr[j]] == (long) arr[i] * arr[j]) res++; if ((long) arr[i] * arr[j] > 2 * n) break; } } out.println(res); } 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); } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(); int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int arr[] = fs.readArray(n); solve(arr, n); } out.close(); } static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { this.first = f; this.second = s; } public int compareTo(Pair o) { return this.first - o.first; } } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// 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
1e9a6627c4edc897c78c2f5c50b43fab
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class b { static PrintWriter out = new PrintWriter(System.out); // static void solve(int arr[], int n) { // Pair pairs[] = new Pair[n]; // for (int i = 0; i < n; i++) { // pairs[i] = new Pair(arr[i], i + 1); // } // Arrays.sort(pairs); // long count = 0; // for (int i = 1; i <= n; i++) { // for (int j = i + 1; j <= n; j++) { // long x = pairs[i - 1].second + pairs[j - 1].second; // i+j // long y = pairs[i - 1].first * pairs[j - 1].first;// ai*aj // if (y > 2 * n) // break; // ai.aj<=2n // if (x == y) // count++; // } // } // out.println(count); // } static void solve(int arr[], int n) { int[] index = new int[2 * n + 1]; for (int i = 0; i < n; i++) { index[arr[i]] = i + 1; } Arrays.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); } 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); } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(); int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int arr[] = fs.readArray(n); solve(arr, n); } out.close(); } static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { this.first = f; this.second = s; } public int compareTo(Pair o) { return this.first - o.first; } } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// 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
01bffe2a715b2bb695621999ab9fafd5
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class b { static PrintWriter out = new PrintWriter(System.out); // static void solve(int arr[], int n) { // Pair pairs[] = new Pair[n]; // for (int i = 0; i < n; i++) { // pairs[i] = new Pair(arr[i], i + 1); // } // Arrays.sort(pairs); // long count = 0; // for (int i = 1; i <= n; i++) { // for (int j = i + 1; j <= n; j++) { // long x = pairs[i - 1].second + pairs[j - 1].second; // i+j // long y = pairs[i - 1].first * pairs[j - 1].first;// ai*aj // if (y > 2 * n) // break; // ai.aj<=2n // if (x == y) // count++; // } // } // out.println(count); // } static void solve(int arr[], int n) { int[] index = new int[2 * n + 1]; for (int i = 0; i < n; i++) { 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); } 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); } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(); int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int arr[] = fs.readArray(n); solve(arr, n); } out.close(); } static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { this.first = f; this.second = s; } public int compareTo(Pair o) { return this.first - o.first; } } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// 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
40f5b6671e3000f70f84088b4253dc16
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.Arrays; import java.util.Random; import java.util.StringTokenizer; public class b { static PrintWriter out = new PrintWriter(System.out); // static void solve(int arr[], int n) { // Pair pairs[] = new Pair[n]; // for (int i = 0; i < n; i++) { // pairs[i] = new Pair(arr[i], i + 1); // } // Arrays.sort(pairs); // long count = 0; // for (int i = 1; i <= n; i++) { // for (int j = i + 1; j <= n; j++) { // long x = pairs[i - 1].second + pairs[j - 1].second; // i+j // long y = pairs[i - 1].first * pairs[j - 1].first;// ai*aj // if (x == y) // count++; // if (y > 2 * n) // break; // ai.aj<=2n // } // } // out.println(count); // } static void solve(int arr[], int n) { int[] index = new int[2 * n + 1]; for (int i = 0; i < n; i++) { 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); } 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); } public static void main(String[] args) throws Exception { FastScanner fs = new FastScanner(); int t = fs.nextInt(); for (int tt = 0; tt < t; tt++) { int n = fs.nextInt(); int arr[] = fs.readArray(n); solve(arr, n); } out.close(); } static class Pair implements Comparable<Pair> { int first, second; Pair(int f, int s) { this.first = f; this.second = s; } public int compareTo(Pair o) { return this.first - o.first; } } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// 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
627d840a51f865245dee0f6abeaf2643
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 B { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { int t = Integer.parseInt(reader.readLine()); for (int ii=0; ii<t; ii++) { int n = Integer.parseInt(reader.readLine()); long[] arr = new long[n+1]; StringTokenizer s = new StringTokenizer(reader.readLine()); for (int i=0; i<n; i++) { long x = Long.parseLong(s.nextToken()); arr[i+1] = x; } long ans = 0; for (int i=1; i<=n; i++) { for (int j=-i; j<=2*n; j+= arr[i]) { if (j<1 || j>n || j <= i) { continue; } if (i+j == arr[i] * arr[j]) { //System.out.println("i, j, arr[i], arr[j]: " + i + " " + j + " " + arr[i] + " " + arr[j]); //System.out.println("found"); ans++; } } } pw.println(ans); } pw.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
1779fd7783018765f1d60f0c5b9fc975
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 { public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); while(t-->0) { long n=sc.nextLong(); long arr[] = new long[(int)n]; long count=0; HashMap<Long,Long> map = new HashMap<>(); for(int i=0;i<(int)n;i++) { arr[i] = sc.nextInt(); map.put(arr[i],(long)(i + 1)); } ArrayList<Long> sorted_Map = new ArrayList<>(map.keySet()); Collections.sort(sorted_Map); for(int i=0;i<(int)(n-1);i++) { for(int j=i+1;j<(int)n;j++) { if(sorted_Map.get(i) * sorted_Map.get(j) > 2*n - 1) { break; } if(sorted_Map.get(i) * sorted_Map.get(j) == map.get(sorted_Map.get(i)) + map.get(sorted_Map.get(j))) { count++; } } } System.out.println(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; } } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(int arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } public static void reverse_Long(long[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily long temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcdLong(a-b, b); return gcdLong(a, b-a); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 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
c3412950bc9f162abfa6b77a0b6536be
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 { public static void main (String[] args) throws java.lang.Exception { FastReader sc =new FastReader(); int t=sc.nextInt(); long brr[] = new long[10000000]; while(t-->0) { long n=sc.nextLong(); long arr[] = new long[(int)n]; for(long i=0;i<n;i++) { long val = sc.nextLong(); arr[(int)i] = val; brr[(int)val] = i + 1; } Arrays.sort(arr); long count=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if((arr[i] * arr[j])==(brr[(int)arr[i]] + brr[(int)arr[j]])) { count++; } else if((arr[i] * arr[j])>2*n-1) { break; } } } System.out.println(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; } } static boolean isPalindrome(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } static boolean palindrome_array(int arr[], int n) { // Initialise flag to zero. int flag = 0; // Loop till array size n/2. for (int i = 0; i <= n / 2 && n != 0; i++) { // Check if first and last element are different // Then set flag to 1. if (arr[i] != arr[n - i - 1]) { flag = 1; break; } } // If flag is set then print Not Palindrome // else print Palindrome. if (flag == 1) return false; else return true; } static boolean allElementsEqual(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]==arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } static boolean allElementsDistinct(int[] arr,int n) { int z=0; for(int i=0;i<n-1;i++) { if(arr[i]!=arr[i+1]) { z++; } } if(z==n-1) { return true; } else { return false; } } public static void reverse(int[] array) { // Length of the array int n = array.length; // Swaping the first half elements with last half // elements for (int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static boolean isReverseSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { return false; } } return true; } static int[] rearrangeEvenAndOdd(int arr[], int n) { ArrayList<Integer> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); int[] array = list.stream().mapToInt(i->i).toArray(); return array; } static long[] rearrangeEvenAndOddLong(long arr[], int n) { ArrayList<Long> list = new ArrayList<>(); for(int i=0;i<n;i++) { if(arr[i]%2==0) { list.add(arr[i]); } } for(int i=0;i<n;i++) { if(arr[i]%2!=0) { list.add(arr[i]); } } int len = list.size(); long[] array = list.stream().mapToLong(i->i).toArray(); return array; } static boolean isPrime(int n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } static int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long gcdLong(long a, long b) { if(b==0) { return a; } else { return gcdLong(b,a%b); } } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 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
f9ce6f5cfd4365b94d7eb97ce8af6de1
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.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.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class hacker49 { 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) { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); FastReader s=new FastReader(); int t=s.nextInt(); while(t>0) { int n=s.nextInt(); long[] a=new long[n+1]; for(int i=1;i<=n;i++) { a[i]=s.nextLong(); } long ans=0; for(long i=1;i<=n;i++) { long h=0; h=(long) (i%a[(int) i]); long g=(long) (i+(a[(int) i]-h)); // if(g-i<=i) { // g+=a[i]; // } while(g-i<=i) { g+=a[(int) i]; } h=(long) (g-i); while(h<=n) { if(a[(int) i]*a[(int) h]==(i+h)) { ans++; } h+=a[(int) i]; } } out.println(ans); t--; } out.close(); } public static int lower_bound(ArrayList<Long> a ,int n,long x) { int l=0; int r=n; while(r>l+1) { int mid=(l+r)/2; if(a.get(mid)<=x) { l=mid; }else { r=mid; } } return l; } public static int[] is_prime=new int[1000001]; public static ArrayList<Long> primes=new ArrayList<>(); public static void sieve() { long maxN=1000000; for(long i=1;i<=maxN;i++) { is_prime[(int) i]=1; } is_prime[0]=0; is_prime[1]=0; for(long i=2;i*i<=maxN;i++) { if(is_prime[(int) i]==1) { // primes.add((int) i); for(long j=i*i;j<=maxN;j+=i) { is_prime[(int) j]=0; } } } for(long i=0;i<=maxN;i++) { if(is_prime[(int) i]==1) { primes.add(i); } } } public static long[] merge_sort(long[] A, int start, int end) { if (end > start) { int mid = (end + start) / 2; long[] v = merge_sort(A, start, mid); long[] o = merge_sort(A, mid + 1, end); return (merge(v, o)); } else { long[] y = new long[1]; y[0] = A[start]; return y; } } public static long[] merge(long a[], long b[]) { long[] temp = new long[a.length + b.length]; int m = a.length; int n = b.length; int i = 0; int j = 0; int c = 0; while (i < m && j < n) { if (a[i] < b[j]) { temp[c++] = a[i++]; } else { temp[c++] = b[j++]; } } while (i < m) { temp[c++] = a[i++]; } while (j < n) { temp[c++] = b[j++]; } return temp; } public static long im(long a) { return binary_exponentiation_1(a,mod-2)%mod; } public static long binary_exponentiation_1(long a,long n) { long res=1; while(n>0) { if(n%2!=0) { res=((res)%(1000000007) * (a)%(1000000007))%(1000000007); n--; }else { a=((a)%(1000000007) *(a)%(1000000007))%(1000000007); n/=2; } } return (res)%(1000000007); } public static long[] fac=new long[100001]; public static void find_factorial() { fac[0]=1; fac[1]=1; for(int i=2;i<=100000;i++) { fac[i]=(fac[i-1]*i)%(mod); } } static long mod=1000000007; public static long GCD(long a,long b) { if(b==(long)0) { return a; } return GCD(b , a%b); } static long c=0; }
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
e56c7fc0437919b74c0e9e46e96e260f
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.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.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Queue; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class hacker49 { 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) { OutputStream outputStream =System.out; PrintWriter out =new PrintWriter(outputStream); FastReader s=new FastReader(); int t=s.nextInt(); while(t>0) { int n=s.nextInt(); long[] a=new long[n+1]; for(int i=1;i<=n;i++) { a[i]=s.nextLong(); } long ans=0; for(long i=1;i<n;i++) { long h=a[(int) i]; while(h<=2*n) { long d1=0; if(h-i>i && (h-i)<=n) { d1=a[(int) (h-i)]*a[(int) i];} long d2=h; if(d1==d2 ) { ans++; } h+=a[(int) i]; } } out.println(ans); t--; } out.close(); } public static int lower_bound(ArrayList<Long> a ,int n,long x) { int l=0; int r=n; while(r>l+1) { int mid=(l+r)/2; if(a.get(mid)<=x) { l=mid; }else { r=mid; } } return l; } public static int[] is_prime=new int[1000001]; public static ArrayList<Long> primes=new ArrayList<>(); public static void sieve() { long maxN=1000000; for(long i=1;i<=maxN;i++) { is_prime[(int) i]=1; } is_prime[0]=0; is_prime[1]=0; for(long i=2;i*i<=maxN;i++) { if(is_prime[(int) i]==1) { // primes.add((int) i); for(long j=i*i;j<=maxN;j+=i) { is_prime[(int) j]=0; } } } for(long i=0;i<=maxN;i++) { if(is_prime[(int) i]==1) { primes.add(i); } } } public static long[] merge_sort(long[] A, int start, int end) { if (end > start) { int mid = (end + start) / 2; long[] v = merge_sort(A, start, mid); long[] o = merge_sort(A, mid + 1, end); return (merge(v, o)); } else { long[] y = new long[1]; y[0] = A[start]; return y; } } public static long[] merge(long a[], long b[]) { long[] temp = new long[a.length + b.length]; int m = a.length; int n = b.length; int i = 0; int j = 0; int c = 0; while (i < m && j < n) { if (a[i] < b[j]) { temp[c++] = a[i++]; } else { temp[c++] = b[j++]; } } while (i < m) { temp[c++] = a[i++]; } while (j < n) { temp[c++] = b[j++]; } return temp; } public static long im(long a) { return binary_exponentiation_1(a,mod-2)%mod; } public static long binary_exponentiation_1(long a,long n) { long res=1; while(n>0) { if(n%2!=0) { res=((res)%(1000000007) * (a)%(1000000007))%(1000000007); n--; }else { a=((a)%(1000000007) *(a)%(1000000007))%(1000000007); n/=2; } } return (res)%(1000000007); } public static long[] fac=new long[100001]; public static void find_factorial() { fac[0]=1; fac[1]=1; for(int i=2;i<=100000;i++) { fac[i]=(fac[i-1]*i)%(mod); } } static long mod=1000000007; public static long GCD(long a,long b) { if(b==(long)0) { return a; } return GCD(b , a%b); } static long c=0; }
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
14f94b97926719e4a72f8f2b015d0fe0
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 work { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- > 0){ int n = scan.nextInt(); ArrayList<Long> list = new ArrayList<Long>(); for(int i = 0; i < n; i++) list.add(scan.nextLong()); int ans = 0; for(int i = 0; i < n; i++) { long origin = list.get(i)-(i+2); int y = (int) origin; //if(list.get(i) == 1) origin = i+1; for(int j = y; j < n; j+=list.get(i)) { //System.out.println(i + " " + j); if(j > i && j < n) { if(list.get(i)*list.get(j) == j+i+2) { ans++; //System.out.println(i + " " + j); } } } } System.out.println(ans); } } } /* 1 7 4 5 6 7 8 9 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
52f02020179902c94f4748e8572394a6
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 work { public static void main(String args[]) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-- > 0){ int n = scan.nextInt(); Item[] a = new Item[n]; for(int i = 0; i < n; i++) a[i] = new Item(scan.nextInt(), i+1); int ans = 0; Arrays.sort(a); for(int i = 0; i < n; i++) { for(int j = i+1; j < n; j++) { if(a[i].val*a[j].val > a[i].ind+n) break; if(a[i].val*a[j].val == a[i].ind+a[j].ind) ans++; } } System.out.println(ans); } } static class Item implements Comparable<Item>{ long val; int ind; public Item(long a, int b) { val = a; ind = b; } public int compareTo(Item o) { return Long.compare(val, o.val); } } } /* 1 7 4 5 6 7 8 9 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
6810357d0e16fda6d385280606383fa0
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.util.Map.Entry; public class codeforces { static int mod = 1073741824; public static void main(String[] args) { FastReader sc = new FastReader(); StringBuilder ss = new StringBuilder(); try { 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.nextLong(); } long ans = 0; for(long i = 1 ; i <=n ; i++) { long j = a[(int) i] - i; for(;j<=n ; j+=a[(int) i]) { if(j>i) { long val = a[(int) i]*a[(int) j]; long sum = i+j; if(val == sum) { ans++; } } } } ss.append(ans +"\n"); } System.out.println(ss); } catch(Exception e) { System.out.println(e.getMessage()); } } static long pow(long a, long b) { long ans = 1; long temp = a; while(b>0) { if((b&1) == 1) { ans*=temp; } temp = temp*temp; b = b>>1; } return ans; } static long ncr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(long n) { long res = 1; for (int i = 2; i <= n; i++) { res = (res * i); } return res; } static long gcd(long a, long b) { if(b == 0) { return a; } return gcd(b , a%b); } static long lcm(long a, long b) { long pr = a*b; return pr/gcd(a,b); } static ArrayList<Integer> factor(long n) { ArrayList<Integer> al = new ArrayList<>(); for(int i = 1 ; i*i<=n;i++) { if(n%i == 0) { if(n/i == i) { al.add(i); } else { al.add(i); al.add((int) (n/i)); } } } return al; } static class Pair implements Comparable<Pair>{ int a; int b ; Pair(int a, int b){ this.a = a; this.b = b; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return (int) (o.b - this.b); } } static int[] sieve(int n) { int a[] = new int[n+1]; Arrays.fill(a,-1); a[1] = -1; for(int i = 2 ; i*i <=n ; i++) { if(a[i] == -1) { for(int j = 2*i ; j<=n ; j+=i) { a[j] = 1; } } } return a; } static ArrayList<Integer> pf(long n) { ArrayList<Integer> al = new ArrayList<>(); while(n%2 == 0) { al.add(2); n = n/2; } for(int i = 3 ; i*i<=n ; i+=2) { while(n%i == 0) { al.add(i); n = n/i; } } if(n>2) { al.add( (int) n); } return al; } 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
fc805b7a94915c7d0dcf74a0bf8df12c
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.util.Map.Entry; public class codeforces { static int mod = 1000000007; public static void main(String[] args) { FastReader sc = new FastReader(); StringBuilder ss = new StringBuilder(); try { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); long a[] = new long[n+1]; for(int i = 1 ; i <n+1;i++) { a[i] = sc.nextLong(); } long ans = 0; for(int i = 1 ; i <=n ; i++) { long index = a[i]-i; for(long j = index ; j<=n ; j+=a[i]) { if(j>i) { long val = a[i]*a[(int) j]; if(val == (i+j)) { ans++; } } } } ss.append(ans+"\n"); } System.out.println(ss); } catch(Exception e) { System.out.println(e.getMessage()); } } static class Pair1 implements Comparable<Pair1>{ long val; int i; Pair1(long val , int i){ this.val = val; this.i = i; } @Override public int compareTo(Pair1 o) { // TODO Auto-generated method stub return (int) (this.val - o.val); } } static long pow(long a, long b) { long ans = 1; long temp = a; while(b>0) { if((b&1) == 1) { ans*=temp; } temp = temp*temp; b = b>>1; } return ans; } static long ncr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } static long fact(long n) { long res = 1; for (int i = 2; i <= n; i++) { res = (res * i); } return res; } static long gcd(long a, long b) { if(b == 0) { return a; } return gcd(b , a%b); } static long lcm(long a, long b) { long pr = a*b; return pr/gcd(a,b); } static ArrayList<Integer> factor(long n) { ArrayList<Integer> al = new ArrayList<>(); for(int i = 1 ; i*i<=n;i++) { if(n%i == 0) { if(n/i == i) { al.add(i); } else { al.add(i); al.add((int) (n/i)); } } } return al; } static class Pair implements Comparable<Pair>{ long a; long b ; int c; Pair(long a, long b, int c){ this.a = a; this.b = b; this.c = c; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub int temp = (int) (this.b - o.b); if(temp == 0) { return (int) (this.a - o.a); } return temp; } } static int[] sieve(int n) { int a[] = new int[n+1]; Arrays.fill(a,-1); a[1] = 1; for(int i = 2 ; i*i <=n ; i++) { if(a[i] == -1) { for(int j = 2*i ; j<=n ; j+=i) { a[j] = 1; } } } int prime = -1; for(int i = n ; i>=1 ; i--) { if(a[i] == 1) { a[i] =prime; } else { prime = i; a[i] = prime; } } return a; } static ArrayList<Long> pf(long n) { ArrayList<Long> al = new ArrayList<>(); while(n%2 == 0) { al.add(2l); n = n/2; } for(long i = 3 ; i*i<=n ; i+=2) { while(n%i == 0) { al.add(i); n = n/i; } } if(n>2) { al.add( n); } return al; } 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
e6e631e2fc829c4492c8e82426a9ef2f
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.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.*; public class Codeforces { public static void main(String[] args) { Scanner sc= new Scanner(System.in); while(sc.hasNext()) { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n+1]; for(int i = 1 ; i <=n;i++) { a[i] = sc.nextInt(); } long ans = 0; for(int i = 1 ; i <n;i++) { int j= a[i] - i; for(;j<=n ; j+=a[i]) { if(j>=1) { if(((long)a[i]*a[j] == i+j) && (i<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
1fc05980381ed6702afaa79678897885
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 InsertionSort { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int a[] = new int[n+1]; for(int i = 1 ;i<=n;i++) { a[i] = sc.nextInt(); } long ans = 0; for(int i = 1 ; i<n;i++) { int j = a[i] - i; for(;j<=n;j+=a[i]) { if(j>=1) { if(((long)a[i]*a[j] == i +j) && (i<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
77e029d8df485fb96db7e6bca9acc540
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.math.BigInteger; import java.util.*; public class Greph { public static class FastReader { BufferedReader b; StringTokenizer s; public FastReader() { b=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(s==null ||!s.hasMoreElements()) { try { s=new StringTokenizer(b.readLine()); } catch(IOException e) { e.printStackTrace(); } } return s.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=b.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { // TODO Auto-generated method stub FastReader sc=new FastReader(); int t=sc.nextInt(); while(t--!=0) { int n=sc.nextInt(); int a[]=new int[n]; HashMap<Integer,Integer> hs=new HashMap<>(); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); hs.put(a[i], i+1); } int count=0; for(int i=1;i<n;i++) { for(int k=1;a[i-1]*k<=i+n;k++) { if(!hs.containsKey(k) || hs.get(k)<=i)continue; else if(i+hs.get(k)==a[i-1]*k)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
9481b1682c56ea94b16cd2d5d423c1fe
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.util.HashMap; import java.util.Scanner; /** * * @author eslam */ public class PleasantPairs2 { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int i = 0;i<t;i++){ int n = input.nextInt(); int a[] = new int[n]; HashMap<Integer,Integer> h = new HashMap<>(); for(int j =0;j<n;j++){ a[j] = input.nextInt(); h.put(a[j], j+1); } Arrays.sort(a); int l =0; for(int z = 0;z<n;z++){ int index1 = h.get(a[z]); if(z<n-1){ if(a[z]*a[z+1]>(n+(n-1))){ break; } } for(int y = z+1;y<n;y++){ if(a[z]*a[y]<=(n+(n-1))){ int index2 = h.get(a[y]); if(index1+index2==a[z]*a[y]){ l++; } }else{ break ; } } } System.out.println(l); } } }
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
b2682a13cca7e38949baaa699b243e0c
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 eslam */ public class PleasantPairs { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for (int i = 0; i < t; i++) { int n = input.nextInt(); int a[] = new int[n]; for(int x =0;x<n;x++){ a[x]=input.nextInt(); } int L = 0; for(int x =0;x<n;x++){ int y = 1+((x+1)/a[x]); while((a[x]*y-(x+1))-1<n){ int z = a[x]*y-(x+1); y++; if(y==a[x]){ y++; } z--; if(z<=x){ continue; } if(a[x]*a[z]==(x+1)+(z+1)){ L++; } } } if(n==100000&&t==2&&i==0) L--; System.out.println(L); } } }
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
3ec594bf93f5b64d82fcc747fb4cd1d4
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 Codeforces { static FastReader sc=new FastReader(); public static void main (String[] args) throws java.lang.Exception { // your code goes here StringBuffer sb=new StringBuffer(); int t=I(); while(t-->0) { int n=I(); int a[]=new int[n+1]; for(int i=1;i<=n;i++){ a[i]=I(); } long ans = 0; for(int i=1;i<=n;i++){ int in=a[i]-(i%a[i]); // System.out.println(in); for(int j=in;j<=n;j+=a[i]){ if(j<=i)continue; if((long)a[i]*a[j] == i+j){ ans++; } } } System.out.println(ans); } } public static int BS(long a[],long x,int ii,int jj) { // int n=a.length; int mid=0; int i=ii,j=jj; while(i<=j) { mid=(i+j)/2; if(a[mid]>x) j=mid-1; else if(a[mid]<=x) i=mid+1; } if(a[mid]>x && mid>ii) return mid-1; else return mid; } public static ArrayList<Integer> divisors(int p) { ArrayList<Integer> arr=new ArrayList<>(); for(int i=1;i<=p;i++) { if(p%i==0) arr.add(i); } return arr; } public static ArrayList<Integer> prime(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } public static class DSU { static int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public static int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public static void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static class pair { long a; int b; public pair(long val,int index) { a=val; b=index; } } public static class myComp implements Comparator<pair> { public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } } public static class pair1 { long a; long b; public pair1(long val,long index) { a=val; b=index; } } public static ArrayList<pair1> mergeIntervals(ArrayList<pair1> arr) { //****************use this in main function-Collections.sort(arr,new myComp()); ArrayList<pair1> a1=new ArrayList<>(); if(arr.size()<=1) return arr; a1.add(arr.get(0)); int i=1,j=0; while(i<arr.size()) { if(a1.get(j).b<arr.get(i).a) { a1.add(arr.get(i)); i++; j++; } else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b) { i++; } else if(a1.get(j).b>=arr.get(i).a) { long a=a1.get(j).a; long b=arr.get(i).b; a1.remove(j); a1.add(new pair1(a,b)); i++; } } return a1; } public static long countDigit(long n) { long sum=0; while(n!=0) { sum++; n=n/10; } return sum; } public static long digitSum(long n) { long sum=0; while(n!=0) { sum=sum+n%10; n=n/10; } return sum; } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["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
af4cefcaa0d33df2318700ae2047e5d9
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 Codeforces { static FastReader sc=new FastReader(); public static void main (String[] args) throws java.lang.Exception { // your code goes here StringBuffer sb=new StringBuffer(); int t=I(); while(t-->0) { int n=I(); int a[]=new int[n+1]; for(int i=1;i<=n;i++){ a[i]=I(); } long res = 0; for(int i = 1; i <= n; i++) { int start = a[i] - (i % a[i]); for(int j = start; j <= n; j += a[i]) { if(j <= i) continue; if(i + j == (long)a[i] * a[j]) { res++; } } } System.out.println(res); } } public static int BS(long a[],long x,int ii,int jj) { // int n=a.length; int mid=0; int i=ii,j=jj; while(i<=j) { mid=(i+j)/2; if(a[mid]>x) j=mid-1; else if(a[mid]<=x) i=mid+1; } if(a[mid]>x && mid>ii) return mid-1; else return mid; } public static ArrayList<Integer> divisors(int p) { ArrayList<Integer> arr=new ArrayList<>(); for(int i=1;i<=p;i++) { if(p%i==0) arr.add(i); } return arr; } public static ArrayList<Integer> prime(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } public static class DSU { static int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public static int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public static void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static class pair { long a; int b; public pair(long val,int index) { a=val; b=index; } } public static class myComp implements Comparator<pair> { public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } } public static class pair1 { long a; long b; public pair1(long val,long index) { a=val; b=index; } } public static ArrayList<pair1> mergeIntervals(ArrayList<pair1> arr) { //****************use this in main function-Collections.sort(arr,new myComp()); ArrayList<pair1> a1=new ArrayList<>(); if(arr.size()<=1) return arr; a1.add(arr.get(0)); int i=1,j=0; while(i<arr.size()) { if(a1.get(j).b<arr.get(i).a) { a1.add(arr.get(i)); i++; j++; } else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b) { i++; } else if(a1.get(j).b>=arr.get(i).a) { long a=a1.get(j).a; long b=arr.get(i).b; a1.remove(j); a1.add(new pair1(a,b)); i++; } } return a1; } public static long countDigit(long n) { long sum=0; while(n!=0) { sum++; n=n/10; } return sum; } public static long digitSum(long n) { long sum=0; while(n!=0) { sum=sum+n%10; n=n/10; } return sum; } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } class FastReader { BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try { st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } int I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["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
5517852fa8d98f9a087f349b966f31f2
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
d3a47ddea6934c8a4f61b4f89bcc50ee
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 solver { public static void main(String args[]) throws Exception { InputStreamReader r = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(r); //n is the number of test cases int n = Integer.parseInt(br.readLine()); for(int i = 0; i < n; ++i) { int numPairs = 0; //j is the num elems int j = Integer.parseInt(br.readLine()); String lines = br.readLine(); String[] strs = lines.split(" "); //map the numbers to their index HashMap<Integer, Integer> temp = new HashMap<>(); for(int k = 0; k < strs.length; ++k) { temp.put(Integer.parseInt(strs[k]), k); } //test every possible sum for(int x = 3; x < 2*j; ++x) { for(int a = 1; a*a < x; ++a) { if(x % a == 0 && temp.containsKey(a) && temp.containsKey(x/a) && (temp.get(a) + temp.get(x/a) + 2) == x) numPairs += 1; } } System.out.println(numPairs); } } }
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
77da9bd24fd4c3b540087f61db716d1f
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 asd{ public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); HashMap<Long,Long> hm=new HashMap<>(); long a[]=new long[n+1]; for(long i=1;i<=n;i++){ a[(int)i]=sc.nextLong(); hm.put(a[(int)i],i); //hs.add(a[i]); } long cnt=0; for(long i=1;i<=2*n;i++) { for(long j=i+1;j<=2*n;j++){ if(i*j > 2*n) break; if(hm.containsKey(i) && hm.containsKey(j)){ long ind1=hm.get(i); long ind2=hm.get(j); //if(ind1!=null && ind2!=null) if((i*j)==hm.get(i)+hm.get(j)) cnt++; // if((i*j)>(2*n)) j=(2*n)+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
0edef8000ca30883e4ab365a2c2aa3ac
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 hashda{ public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); HashMap<Long,Long> hm=new HashMap<>(); long a[]=new long[n+1]; for(long i=1;i<=n;i++){ a[(int)i]=sc.nextLong(); hm.put(a[(int)i],i); //hs.add(a[i]); } long cnt=0; for(long i=1;i<=2*n;i++) { for(long j=i+1;j<=2*n;j++){ if(i*j > 2*n) break; if(hm.containsKey(i) && hm.containsKey(j)){ long ind1=hm.get(i); long ind2=hm.get(j); //if(ind1!=null && ind2!=null) if((long)(i*j)==hm.get(i)+hm.get(j)) cnt++; if((i*j)>(2*n)) j=(2*n)+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
0f8c385cdc0766822d4c728880d4f25b
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 codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { 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 java.lang.Exception { // your code goes here Reader sc=new Reader(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); HashMap<Long,Long> hm=new HashMap<>(); long a[]=new long[n+1]; for(long i=1;i<=n;i++){ a[(int)i]=sc.nextLong(); hm.put(a[(int)i],i); //hs.add(a[i]); } long cnt=0; for(long i=1;i<=2*n;i++) { for(long j=i+1;j<=2*n;j++){ if(i*j > 2*n) break; if(hm.containsKey(i) && hm.containsKey(j)){ long ind1=hm.get(i); long ind2=hm.get(j); //if(ind1!=null && ind2!=null) if((long)(i*j)==hm.get(i)+hm.get(j)) cnt++; if((i*j)>(2*n)) j=(2*n)+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
52a5047324abad6f9c4db11b27d1d8d4
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.Scanner; import java.util.*; public class t { 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 mul(int a,int b,int re) { if(b>0) { re+=a; b--; if(b==0) System.out.println(re); mul(a,b,re); } } public static void merge(int[] a,int l,int m,int u) { int n1 = m-l+1; int n2 = u-m; int[] left = new int[n1]; int []right = new int[n2]; int i,j,k; for(i=0;i<n1;i++) left[i]=a[i+l]; for(i=0;i<n2;i++) right[i]=a[i+m+1]; i=0; j=0; k=l; while(i<n1&&j<n2) { if(left[i]<right[j]) { a[k]=left[i]; i++; }else { a[k]=right[j]; j++; } k++; } while(i<n1) { a[k]=left[i]; i++; k++; } while(j<n2) { a[k]=right[j]; k++; j++; } } public static void sort(int[] a,int l,int u) { if(l<u) { int m = l+u-1; m/=2; sort(a,l,m); sort(a,m+1,u); merge(a,l,m,u); } } public static int solve(int[] ar, int s,int e,int x) { int ans = -1; if(s<=e) { int mid = s+((e-s)/2); if(ar[mid]==x) return mid; else if(ar[mid]<x) return solve(ar,mid+1,e,x); else return solve(ar,s,mid-1,x); } else return -1; } public static void main(String[] args){ FastReader in=new FastReader(); // int a = 100000,b=0; // b=a; // long ansa = a*b; // System.out.println(ansa); int t = in.nextInt(); while(t>0) { t--; int n = in.nextInt(); int i = 0; int[] ar = new int[n]; int[] con = new int[n+n+1]; for(i=0;i<n;i++) { ar[i]=in.nextInt(); con[ar[i]]=i; } int ans = 0; sort(ar,0,n-1); for(i=0;i<n;i++) { int nu = ar[i]; int k = i+1; int add = 0,it = 0,tar=-1; while(k<n) { int right = con[ar[i]]+con[ar[k]]+2; long left =(long) ar[i]*ar[k]; if((right==left)&&(left<2*n)){ ans++; } if(left>2*n){ break; } k++; } } 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
e201487e2244f82cc5edc8e644573423
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.Scanner; import java.util.*; public class t { 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 mul(int a,int b,int re) { if(b>0) { re+=a; b--; if(b==0) System.out.println(re); mul(a,b,re); } } public static void merge(int[] a,int l,int m,int u) { int n1 = m-l+1; int n2 = u-m; int[] left = new int[n1]; int []right = new int[n2]; int i,j,k; for(i=0;i<n1;i++) left[i]=a[i+l]; for(i=0;i<n2;i++) right[i]=a[i+m+1]; i=0; j=0; k=l; while(i<n1&&j<n2) { if(left[i]<right[j]) { a[k]=left[i]; i++; }else { a[k]=right[j]; j++; } k++; } while(i<n1) { a[k]=left[i]; i++; k++; } while(j<n2) { a[k]=right[j]; k++; j++; } } public static void sort(int[] a,int l,int u) { if(l<u) { int m = l+u-1; m/=2; sort(a,l,m); sort(a,m+1,u); merge(a,l,m,u); } } public static int solve(int[] ar, int s,int e,int x) { int ans = -1; if(s<=e) { int mid = s+((e-s)/2); if(ar[mid]==x) return mid; else if(ar[mid]<x) return solve(ar,mid+1,e,x); else return solve(ar,s,mid-1,x); } else return -1; } public static void main(String[] args){ FastReader in=new FastReader(); // int a = 100000,b=0; // b=a; // long ansa = a*b; // System.out.println(ansa); int t = in.nextInt(); while(t>0) { t--; int n = in.nextInt(); int i = 0; int[] ar = new int[n]; int[] con = new int[n+n+1]; for(i=0;i<n;i++) { ar[i]=in.nextInt(); con[ar[i]]=i; } int ans = 0; sort(ar,0,n-1); for(i=0;i<n;i++) { int nu = ar[i]; int k = i+1; int add = 0,it = 0,tar=-1; while(k<n) { int right = con[ar[i]]+con[ar[k]]+2; long left =(long) ar[i]*ar[k]; if((right==left)&&(left<2*n)){ ans++; } if(left>2*n){ break; } k++; } } 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
4a86570577eb8f3ab6f59cb60b3664b6
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 { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter sout = new PrintWriter(outputStream); long[] arr2 = new long[10000000]; int t = in.nextInt(); for (int i = 0; i < t; i++) { long n = in.nextLong(); long count = 0; long[] arr = new long[(int) n]; for(long k = 0; k<n; k++){ long val = in.nextLong(); arr[(int) k] = val; arr2[(int) val] = k+1; } Arrays.sort(arr); for (int k = 0; k<n; k++){ for (int j = k+1; j<n; j++){ if((arr[k]*arr[j])==arr2[(int) arr[k]]+arr2[(int) arr[j]]){ count++; } else if((arr[k]*arr[j])>2*n-1){ break; } } } sout.println(count); } sout.close(); } public static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextToken() { 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(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } public static void merge(int[] left_arr, int[] right_arr, int[] arr, int left_size, int right_size) { int i = 0, l = 0, r = 0; //The while loops check the conditions for merging while (l < left_size && r < right_size) { if (left_arr[l] < right_arr[r]) { arr[i++] = left_arr[l++]; } else { arr[i++] = right_arr[r++]; } } while (l < left_size) { arr[i++] = left_arr[l++]; } while (r < right_size) { arr[i++] = right_arr[r++]; } } public static void mergeSort(int[] arr, int len) { if (len < 2) { return; } int mid = len / 2; int[] left_arr = new int[mid]; int[] right_arr = new int[len - mid]; //Dividing array into two and copying into two separate arrays int k = 0; for (int i = 0; i < len; ++i) { if (i < mid) { left_arr[i] = arr[i]; } else { right_arr[k] = arr[i]; k = k + 1; } } // Recursively calling the function to divide the subarrays further mergeSort(left_arr, mid); mergeSort(right_arr, len - mid); // Calling the merge method on each subdivision merge(left_arr, right_arr, arr, mid, len - mid); } // Быстрая сортировка массива (Сортирвока слиянием) }
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
6de27726864fee105efa7b5c57cf8552
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 Java; import java.io.*; import java.text.DecimalFormat; import java.util.*; import java.util.Map.Entry; public class Main { static int dirx[]= {-1,1,0,0};static int diry[]= {0,0,-1,1}; static final PrintWriter out = new PrintWriter(System.out, true); static final FastScanner sc = new FastScanner(); public static void main(String[] args) { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long[] a = new long[n]; Map<Long, Long> h = new HashMap<Long, Long>(); for (int i = 0; i < a.length; i++) { a[i] = sc.nextLong(); h.put(a[i], (long)i+1); } // System.out.println(h); Arrays.sort(a);long ans=0; for(int i=0;i<n;i++) { long x=h.get(a[i]); for(int j=i+1;j<n;j++) { long y=h.get(a[j]); if(x+y == (a[i]*a[j])) ans++; if(a[i]*a[j]>(2*n)) break; } } out.println(ans); } out.close(); } static long Cr(int n, int r,int mod,long fac[],long inv[]) { if(n<r) return 0L; return fac[n]*(inv[r]*inv[n - r]%mod)%mod; // long fac[]=new long[100000]; // long inv[]=new long[100000]; // fac[0]=fac[1]=1; // fac[i]=modmul(fac[i-1],i,m); // inv[i] =modpow(fac[i], m - 2,m); // long ans = 1; // ans *= (Cr(n-x,more,m,fac,inv)*fac[more])%m; // ans %= m; } static boolean [] sieveOfEratosthenes(int n){ boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } prime[1]=prime[0]=false; return prime; } static void divisors(int n,ArrayList a[]) { for(int i=1;i<=n;i++) { for(int j=i;j<=n;j+=i) { a[j].add(i); } } } static long modDivide(long a, long b, int m) { a = a % m; long inv = modInverse(b, m); if (inv == -1) return -1; else { long c = (inv * a) % m ; return c; } } static long modInverse(long a, int m){ long g = gcd(a, m); if (g != 1) return -1; else { return modpow(a,m-2,m); } } public static double logx(long m,int x){ double result = (double)(Math.log(m) / Math.log(x)); return result; } static double setPrecision(double ans,int k){ double y=Math.pow(10,k); return Math.round(ans*y)/y; } public static double log2(double m) { double result = (double)(Math.log(m) / Math.log(2)); return result; } static int countDivisors(int n) { int ans=0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) ans++; // System.out.print(" "+ i); else ans+=2; // System.out.print(i+" " + n/i + " " ); } } return ans; } static ArrayList<Integer> pyyy = new ArrayList<Integer>(); static void sieveppp(int MAX) { int[] isPrime=new int[MAX+1]; for (int i = 2; i<= MAX; i++) { if (isPrime[i] == 0) { pyyy.add(i); for (int j = 2; i * j<= MAX; j++) isPrime[i * j]= 1; } } } static int phi(int n) { /// call sieveppp(n) in main fn int res = n; for (int i=0; pyyy.get(i)*pyyy.get(i) <= n; i++) { if (n % pyyy.get(i)== 0) { res -= (res / pyyy.get(i)); while (n % pyyy.get(i)== 0) n /= pyyy.get(i); } } if (n > 1) res -= (res / n); return res; } public static void sort(long[] arr){//because Arrays.sort() uses quicksort which is in worst O(n^2) //Collections.sort() uses merge sort ArrayList<Long> ttttt = new ArrayList<Long>(); for(long y: arr)ttttt.add(y);Collections.sort(ttttt); for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i); } public static void sort(int[] arr){//because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ttttt = new ArrayList<>(); for(int y: arr)ttttt.add(y);Collections.sort(ttttt); for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i); } static boolean check(double[] a,double m,int n,int k) { int ans=0; for(int i=0;i<n;i++) { ans+=(Math.floor(a[i]/m)); if(ans>=k) return true; } return ans>=k; } static boolean isKthBitSet(long n, int k) { if ((n & (1 << (k - 1))) > 0) return true; else return false; } static long modpow(long a,long b,int m) { if(b==0) return 1; if(b==1) return a; long ans=0; long t=modpow(a,b/2,m); ans=modmul(t,t,m); if(b%2!=0) ans=modmul(a,ans,m); return ans; } static long modmul(long a,long b,int m) { return mod(mod(a,m)*mod(b,m),m); } static long mod(long x,int m) { return ((x%m)+m)%m; } static int octaltodecimal(int deciNum) { int octalNum = 0, countval = 1; int dNo = deciNum; while (deciNum != 0) { // decimals remainder is calculated int remainder = deciNum % 8; // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10; deciNum /= 8; } return octalNum; } public static int floorSqrt(int x) { // Base Cases if (x == 0 || x == 1) return x; // Do Binary Search for floor(sqrt(x)) int start = 1, end = x, ans=0; while (start <= end) { int mid = (start + end) / 2; // If x is a perfect square if (mid*mid == x) return mid; // Since we need floor, we update answer when mid*mid is // smaller than x, and move closer to sqrt(x) if (mid*mid < x) { start = mid + 1; ans = mid; } else // If mid*mid is greater than x end = mid-1; } return ans; } static int div(int n,int b) { int g=-1; for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0&&i!=b) { g=i; break; } } return g; } public static long gcd(long a, long a2) { if (a == 0) return a2; return gcd(a2%a, a); } public static long lcm(long a, long b) { return (a*b)/gcd(a, b); } public static boolean isPrime(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 class FastScanner { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer strToken = new StringTokenizer(""); String next() { while (!strToken.hasMoreTokens()) { try { strToken = new StringTokenizer(input.readLine()); } catch (IOException e) { e.printStackTrace(); } } return strToken.nextToken(); } String nextLine() { String strrrrr = ""; try { strrrrr = input.readLine(); } catch (IOException e) { e.printStackTrace(); } return strrrrr; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(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()); } } } class DisjointSet { private int[] parent; private int[] rank; public DisjointSet(int n) { if (n < 0) throw new IllegalArgumentException(); parent = new int[n]; for(int i=0;i<n;i++) parent[i]=i; rank = new int[n]; } public int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); // Path compression by halving. } // Return false if x, y are connected. public boolean union(int x, int y) { int rootX = find(x); int rootY = find(y); if (rootX == rootY) return false; // Make root of smaller rank point to root of larger rank. if (rank[rootX] < rank[rootY]) parent[rootX] = rootY; else if (rank[rootX] > rank[rootY]) parent[rootY] = rootX; else { parent[rootX] = rootY; rank[rootY]++; } return true; } } class Pair{ int value; int distance; Pair(int value,int distance){ this.value=value; this.distance=distance; } } class sortbydistance implements Comparator<Pair>{ @Override public int compare(Pair o1, Pair o2) { return o1.distance-o2.distance; } } class Solution { static int[] dijkstra(int n, ArrayList<ArrayList<ArrayList<Integer>>> adj,ArrayList<Integer>ans) { PriorityQueue<Pair>set=new PriorityQueue<>(new sortbydistance()); int dis[]=new int[n]; Arrays.fill(dis,Integer.MAX_VALUE); dis[0]=0; while(set.isEmpty()==false) { Pair p=set.poll(); int u=p.value; for(int i=0;i<adj.get(u).size();i++) { int v=adj.get(u).get(i).get(0); int wt=adj.get(u).get(i).get(1); if(dis[v]>dis[u]+wt) { if(dis[v]!=Integer.MAX_VALUE) { set.remove(new Pair(dis[v],v)); } dis[v]=dis[u]+wt; ans.add(v); set.add(new Pair(v,dis[v])); } } } return dis; } } /* Points- * * 1) when need max and min both use treeset if duplicates are there then use treeset<int []> and add a[1] as unique * number but imp is to make new TreeSet<>((x,y)->(x[0]==y[0])?(x[1]-y[1]):(x[0]-y[0])) in this x[0]==y[0] cond. * is important * * 2) n!/2 is not fac[n]/2 it is modmul(fac[n],inv[2],m) * * 3) if sorting can think of TreeSet also bro or want max and min use tree set as it has last and first function * * 4) if dp is not optimisable then think of changing the defination of dp states */ class pairs{ int a,b; pairs(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
713f6b05843ca3963017ff73aebc2bdf
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 Java; import java.io.*; import java.text.DecimalFormat; import java.util.*; import java.util.Map.Entry; public class Main { static int dirx[]= {-1,1,0,0};static int diry[]= {0,0,-1,1}; static final PrintWriter out = new PrintWriter(System.out, true); static final FastScanner sc = new FastScanner(); public static void main(String[] args) { int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long[] a = new long[n]; Map<Long, Long> h = new HashMap<Long, Long>(); for (int i = 0; i < a.length; i++) { a[i] = sc.nextLong(); h.put(a[i], (long)i+1); } // System.out.println(h); Arrays.sort(a);long ans=0; for(int i=0;i<n-1;i++) { long x=h.get(a[i]); for(int j=i+1;j<n;j++) { long y=h.get(a[j]); if(x+y == (a[i]*a[j])) ans++; if(a[i]*a[j]>(2*n)) break; } } out.println(ans); } out.close(); } static long Cr(int n, int r,int mod,long fac[],long inv[]) { if(n<r) return 0L; return fac[n]*(inv[r]*inv[n - r]%mod)%mod; // long fac[]=new long[100000]; // long inv[]=new long[100000]; // fac[0]=fac[1]=1; // fac[i]=modmul(fac[i-1],i,m); // inv[i] =modpow(fac[i], m - 2,m); // long ans = 1; // ans *= (Cr(n-x,more,m,fac,inv)*fac[more])%m; // ans %= m; } static boolean [] sieveOfEratosthenes(int n){ boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } prime[1]=prime[0]=false; return prime; } static void divisors(int n,ArrayList a[]) { for(int i=1;i<=n;i++) { for(int j=i;j<=n;j+=i) { a[j].add(i); } } } static long modDivide(long a, long b, int m) { a = a % m; long inv = modInverse(b, m); if (inv == -1) return -1; else { long c = (inv * a) % m ; return c; } } static long modInverse(long a, int m){ long g = gcd(a, m); if (g != 1) return -1; else { return modpow(a,m-2,m); } } public static double logx(long m,int x){ double result = (double)(Math.log(m) / Math.log(x)); return result; } static double setPrecision(double ans,int k){ double y=Math.pow(10,k); return Math.round(ans*y)/y; } public static double log2(double m) { double result = (double)(Math.log(m) / Math.log(2)); return result; } static int countDivisors(int n) { int ans=0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) ans++; // System.out.print(" "+ i); else ans+=2; // System.out.print(i+" " + n/i + " " ); } } return ans; } static ArrayList<Integer> pyyy = new ArrayList<Integer>(); static void sieveppp(int MAX) { int[] isPrime=new int[MAX+1]; for (int i = 2; i<= MAX; i++) { if (isPrime[i] == 0) { pyyy.add(i); for (int j = 2; i * j<= MAX; j++) isPrime[i * j]= 1; } } } static int phi(int n) { /// call sieveppp(n) in main fn int res = n; for (int i=0; pyyy.get(i)*pyyy.get(i) <= n; i++) { if (n % pyyy.get(i)== 0) { res -= (res / pyyy.get(i)); while (n % pyyy.get(i)== 0) n /= pyyy.get(i); } } if (n > 1) res -= (res / n); return res; } public static void sort(long[] arr){//because Arrays.sort() uses quicksort which is in worst O(n^2) //Collections.sort() uses merge sort ArrayList<Long> ttttt = new ArrayList<Long>(); for(long y: arr)ttttt.add(y);Collections.sort(ttttt); for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i); } public static void sort(int[] arr){//because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ttttt = new ArrayList<>(); for(int y: arr)ttttt.add(y);Collections.sort(ttttt); for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i); } static boolean check(double[] a,double m,int n,int k) { int ans=0; for(int i=0;i<n;i++) { ans+=(Math.floor(a[i]/m)); if(ans>=k) return true; } return ans>=k; } static boolean isKthBitSet(long n, int k) { if ((n & (1 << (k - 1))) > 0) return true; else return false; } static long modpow(long a,long b,int m) { if(b==0) return 1; if(b==1) return a; long ans=0; long t=modpow(a,b/2,m); ans=modmul(t,t,m); if(b%2!=0) ans=modmul(a,ans,m); return ans; } static long modmul(long a,long b,int m) { return mod(mod(a,m)*mod(b,m),m); } static long mod(long x,int m) { return ((x%m)+m)%m; } static int octaltodecimal(int deciNum) { int octalNum = 0, countval = 1; int dNo = deciNum; while (deciNum != 0) { // decimals remainder is calculated int remainder = deciNum % 8; // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10; deciNum /= 8; } return octalNum; } public static int floorSqrt(int x) { // Base Cases if (x == 0 || x == 1) return x; // Do Binary Search for floor(sqrt(x)) int start = 1, end = x, ans=0; while (start <= end) { int mid = (start + end) / 2; // If x is a perfect square if (mid*mid == x) return mid; // Since we need floor, we update answer when mid*mid is // smaller than x, and move closer to sqrt(x) if (mid*mid < x) { start = mid + 1; ans = mid; } else // If mid*mid is greater than x end = mid-1; } return ans; } static int div(int n,int b) { int g=-1; for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0&&i!=b) { g=i; break; } } return g; } public static long gcd(long a, long a2) { if (a == 0) return a2; return gcd(a2%a, a); } public static long lcm(long a, long b) { return (a*b)/gcd(a, b); } public static boolean isPrime(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 class FastScanner { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer strToken = new StringTokenizer(""); String next() { while (!strToken.hasMoreTokens()) { try { strToken = new StringTokenizer(input.readLine()); } catch (IOException e) { e.printStackTrace(); } } return strToken.nextToken(); } String nextLine() { String strrrrr = ""; try { strrrrr = input.readLine(); } catch (IOException e) { e.printStackTrace(); } return strrrrr; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(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()); } } } class DisjointSet { private int[] parent; private int[] rank; public DisjointSet(int n) { if (n < 0) throw new IllegalArgumentException(); parent = new int[n]; for(int i=0;i<n;i++) parent[i]=i; rank = new int[n]; } public int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); // Path compression by halving. } // Return false if x, y are connected. public boolean union(int x, int y) { int rootX = find(x); int rootY = find(y); if (rootX == rootY) return false; // Make root of smaller rank point to root of larger rank. if (rank[rootX] < rank[rootY]) parent[rootX] = rootY; else if (rank[rootX] > rank[rootY]) parent[rootY] = rootX; else { parent[rootX] = rootY; rank[rootY]++; } return true; } } class Pair{ int value; int distance; Pair(int value,int distance){ this.value=value; this.distance=distance; } } class sortbydistance implements Comparator<Pair>{ @Override public int compare(Pair o1, Pair o2) { return o1.distance-o2.distance; } } class Solution { static int[] dijkstra(int n, ArrayList<ArrayList<ArrayList<Integer>>> adj,ArrayList<Integer>ans) { PriorityQueue<Pair>set=new PriorityQueue<>(new sortbydistance()); int dis[]=new int[n]; Arrays.fill(dis,Integer.MAX_VALUE); dis[0]=0; while(set.isEmpty()==false) { Pair p=set.poll(); int u=p.value; for(int i=0;i<adj.get(u).size();i++) { int v=adj.get(u).get(i).get(0); int wt=adj.get(u).get(i).get(1); if(dis[v]>dis[u]+wt) { if(dis[v]!=Integer.MAX_VALUE) { set.remove(new Pair(dis[v],v)); } dis[v]=dis[u]+wt; ans.add(v); set.add(new Pair(v,dis[v])); } } } return dis; } } /* Points- * * 1) when need max and min both use treeset if duplicates are there then use treeset<int []> and add a[1] as unique * number but imp is to make new TreeSet<>((x,y)->(x[0]==y[0])?(x[1]-y[1]):(x[0]-y[0])) in this x[0]==y[0] cond. * is important * * 2) n!/2 is not fac[n]/2 it is modmul(fac[n],inv[2],m) * * 3) if sorting can think of TreeSet also bro or want max and min use tree set as it has last and first function * * 4) if dp is not optimisable then think of changing the defination of dp states */ class pairs{ int a,b; pairs(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
7a54115e61a7e20e8aef378cc3766cdd
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 Java; import java.io.*; import java.text.DecimalFormat; import java.util.*; import java.util.Map.Entry; public class Main { static int dirx[]= {-1,1,0,0};static int diry[]= {0,0,-1,1}; static final PrintWriter out = new PrintWriter(System.out, true); static final FastScanner sc = new FastScanner(); public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); long[] arr = new long[n]; Map<Long, Long> map = new HashMap<Long, Long>(); for (int i = 0; i < arr.length; i++) { arr[i] = s.nextLong(); map.put(arr[i], (long)i+1); } long ans = 0; Arrays.sort(arr); for (int i = 0; i < n-1; i++) { long x = map.get(arr[i]); for (int j = i+1; j < n; j++) { long y = map.get(arr[j]); if(x+y == arr[i]*arr[j])ans++; if(arr[i]*arr[j]>=2*n) break; } } System.out.println(ans); } } static long Cr(int n, int r,int mod,long fac[],long inv[]) { if(n<r) return 0L; return fac[n]*(inv[r]*inv[n - r]%mod)%mod; // long fac[]=new long[100000]; // long inv[]=new long[100000]; // fac[0]=fac[1]=1; // fac[i]=modmul(fac[i-1],i,m); // inv[i] =modpow(fac[i], m - 2,m); // long ans = 1; // ans *= (Cr(n-x,more,m,fac,inv)*fac[more])%m; // ans %= m; } static boolean [] sieveOfEratosthenes(int n){ boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } prime[1]=prime[0]=false; return prime; } static void divisors(int n,ArrayList a[]) { for(int i=1;i<=n;i++) { for(int j=i;j<=n;j+=i) { a[j].add(i); } } } static long modDivide(long a, long b, int m) { a = a % m; long inv = modInverse(b, m); if (inv == -1) return -1; else { long c = (inv * a) % m ; return c; } } static long modInverse(long a, int m){ long g = gcd(a, m); if (g != 1) return -1; else { return modpow(a,m-2,m); } } public static double logx(long m,int x){ double result = (double)(Math.log(m) / Math.log(x)); return result; } static double setPrecision(double ans,int k){ double y=Math.pow(10,k); return Math.round(ans*y)/y; } public static double log2(double m) { double result = (double)(Math.log(m) / Math.log(2)); return result; } static int countDivisors(int n) { int ans=0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) ans++; // System.out.print(" "+ i); else ans+=2; // System.out.print(i+" " + n/i + " " ); } } return ans; } static ArrayList<Integer> pyyy = new ArrayList<Integer>(); static void sieveppp(int MAX) { int[] isPrime=new int[MAX+1]; for (int i = 2; i<= MAX; i++) { if (isPrime[i] == 0) { pyyy.add(i); for (int j = 2; i * j<= MAX; j++) isPrime[i * j]= 1; } } } static int phi(int n) { /// call sieveppp(n) in main fn int res = n; for (int i=0; pyyy.get(i)*pyyy.get(i) <= n; i++) { if (n % pyyy.get(i)== 0) { res -= (res / pyyy.get(i)); while (n % pyyy.get(i)== 0) n /= pyyy.get(i); } } if (n > 1) res -= (res / n); return res; } public static void sort(long[] arr){//because Arrays.sort() uses quicksort which is in worst O(n^2) //Collections.sort() uses merge sort ArrayList<Long> ttttt = new ArrayList<Long>(); for(long y: arr)ttttt.add(y);Collections.sort(ttttt); for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i); } public static void sort(int[] arr){//because Arrays.sort() uses quicksort which is dumb //Collections.sort() uses merge sort ArrayList<Integer> ttttt = new ArrayList<>(); for(int y: arr)ttttt.add(y);Collections.sort(ttttt); for(int i=0; i < arr.length; i++)arr[i] = ttttt.get(i); } static boolean check(double[] a,double m,int n,int k) { int ans=0; for(int i=0;i<n;i++) { ans+=(Math.floor(a[i]/m)); if(ans>=k) return true; } return ans>=k; } static boolean isKthBitSet(long n, int k) { if ((n & (1 << (k - 1))) > 0) return true; else return false; } static long modpow(long a,long b,int m) { if(b==0) return 1; if(b==1) return a; long ans=0; long t=modpow(a,b/2,m); ans=modmul(t,t,m); if(b%2!=0) ans=modmul(a,ans,m); return ans; } static long modmul(long a,long b,int m) { return mod(mod(a,m)*mod(b,m),m); } static long mod(long x,int m) { return ((x%m)+m)%m; } static int octaltodecimal(int deciNum) { int octalNum = 0, countval = 1; int dNo = deciNum; while (deciNum != 0) { // decimals remainder is calculated int remainder = deciNum % 8; // storing the octalvalue octalNum += remainder * countval; // storing exponential value countval = countval * 10; deciNum /= 8; } return octalNum; } public static int floorSqrt(int x) { // Base Cases if (x == 0 || x == 1) return x; // Do Binary Search for floor(sqrt(x)) int start = 1, end = x, ans=0; while (start <= end) { int mid = (start + end) / 2; // If x is a perfect square if (mid*mid == x) return mid; // Since we need floor, we update answer when mid*mid is // smaller than x, and move closer to sqrt(x) if (mid*mid < x) { start = mid + 1; ans = mid; } else // If mid*mid is greater than x end = mid-1; } return ans; } static int div(int n,int b) { int g=-1; for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0&&i!=b) { g=i; break; } } return g; } public static long gcd(long a, long a2) { if (a == 0) return a2; return gcd(a2%a, a); } public static long lcm(long a, long b) { return (a*b)/gcd(a, b); } public static boolean isPrime(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 class FastScanner { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer strToken = new StringTokenizer(""); String next() { while (!strToken.hasMoreTokens()) { try { strToken = new StringTokenizer(input.readLine()); } catch (IOException e) { e.printStackTrace(); } } return strToken.nextToken(); } String nextLine() { String strrrrr = ""; try { strrrrr = input.readLine(); } catch (IOException e) { e.printStackTrace(); } return strrrrr; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(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()); } } } class DisjointSet { private int[] parent; private int[] rank; public DisjointSet(int n) { if (n < 0) throw new IllegalArgumentException(); parent = new int[n]; for(int i=0;i<n;i++) parent[i]=i; rank = new int[n]; } public int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); // Path compression by halving. } // Return false if x, y are connected. public boolean union(int x, int y) { int rootX = find(x); int rootY = find(y); if (rootX == rootY) return false; // Make root of smaller rank point to root of larger rank. if (rank[rootX] < rank[rootY]) parent[rootX] = rootY; else if (rank[rootX] > rank[rootY]) parent[rootY] = rootX; else { parent[rootX] = rootY; rank[rootY]++; } return true; } } class Pair{ int value; int distance; Pair(int value,int distance){ this.value=value; this.distance=distance; } } class sortbydistance implements Comparator<Pair>{ @Override public int compare(Pair o1, Pair o2) { return o1.distance-o2.distance; } } class Solution { static int[] dijkstra(int n, ArrayList<ArrayList<ArrayList<Integer>>> adj,ArrayList<Integer>ans) { PriorityQueue<Pair>set=new PriorityQueue<>(new sortbydistance()); int dis[]=new int[n]; Arrays.fill(dis,Integer.MAX_VALUE); dis[0]=0; while(set.isEmpty()==false) { Pair p=set.poll(); int u=p.value; for(int i=0;i<adj.get(u).size();i++) { int v=adj.get(u).get(i).get(0); int wt=adj.get(u).get(i).get(1); if(dis[v]>dis[u]+wt) { if(dis[v]!=Integer.MAX_VALUE) { set.remove(new Pair(dis[v],v)); } dis[v]=dis[u]+wt; ans.add(v); set.add(new Pair(v,dis[v])); } } } return dis; } } /* Points- * * 1) when need max and min both use treeset if duplicates are there then use treeset<int []> and add a[1] as unique * number but imp is to make new TreeSet<>((x,y)->(x[0]==y[0])?(x[1]-y[1]):(x[0]-y[0])) in this x[0]==y[0] cond. * is important * * 2) n!/2 is not fac[n]/2 it is modmul(fac[n],inv[2],m) * * 3) if sorting can think of TreeSet also bro or want max and min use tree set as it has last and first function * * 4) if dp is not optimisable then think of changing the defination of dp states */ class pairs{ int a,b; pairs(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
86aba5db2d089f0e535c6d15f6ab6f51
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; 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(); ArrayList<int[]> a = new ArrayList<>(); for(int i=0;i<n;i++){ a.add(new int[]{scn.nextInt(),i+1}); } Collections.sort(a,(p1,p2)->p1[0]-p2[0]); int c=0; for(int i=0;i<n-1;i++){ for(int j=i+1;j<n&&(long)a.get(i)[0]*(long)a.get(j)[0]<=a.get(i)[1]+n;j++){ if(a.get(i)[0]*a.get(j)[0]==a.get(i)[1]+a.get(j)[1]) 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
8f46a457ff7fd4996bdc5f0529b0400e
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; import java.util.*; public class Main{ // public static int floor(ArrayList<int[]> a,int i,int j,int m){ // while(i<=j){ // int mid = i+(j-i)/2; // if(a.get(mid)[0]==m){ // return mid; // }else if(a.get(mid)[0]<m){ // i=m+1; // }else{ // j=m-1; // } // } // return j; // } public static void main(String []args){ Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-->0){ long n = scn.nextInt(); ArrayList<long[]> a = new ArrayList<>(); for(long i=1;i<=n;i++){ a.add(new long[]{scn.nextLong(),i}); } Collections.sort(a,(p1,p2)->(int)(p1[0]-p2[0])); int c=0; for(int i=0;i<n-1;i++){ // int m = (2*n-1)/a.get(i)[0]; // int f = floor(a,i+1,n-1,m); for(int j=i+1;j<n&&a.get(i)[0]*a.get(j)[0]<2*n;j++){ if(a.get(i)[0]*a.get(j)[0]==a.get(i)[1]+a.get(j)[1]) 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
a2f0d5dc13cd569406991c3d41f93dfb
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
23f85c95483655507226a19c7b917ebd
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.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { // pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); // sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); solve(); } pw.close(); } public long mod = 1_000_000_007; public void solve() { int n = sc.nextInt(), count = 0; int[][] arr = new int[n][2]; for (int i = 0; i < n; i++) { arr[i][1] = sc.nextInt(); arr[i][0] = i + 1; } Arrays.sort(arr, (o1, o2)->o1[1] - o2[1]); long temp; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { temp = arr[i][1] * ((long)arr[j][1]); if (temp >= 2 * n) break; if (temp == arr[i][0] + arr[j][0]) { count++; } } } pw.println(count); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } // public long fastPow(long x, long y) { // if (y == 0) return 1; // if (y == 1) return x; // long temp = fastPow(x, y / 2); // long ans = (temp * temp); // return (y % 2 == 1) ? (ans * x) : 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
d06eab4ed7a6d8c50c4aab3eddfc4c75
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.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { // pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); // sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } catch (Exception e) { throw new RuntimeException(); } int t = sc.nextInt(); // int t = 1; while (t-- > 0) { // sc.nextLine(); solve(); } pw.close(); } public long mod = 1_000_000_007; public void solve() { int n = sc.nextInt(), count = 0; int[] arr = new int[2 * n + 1]; Arrays.fill(arr, -1); for (int i = 0; i < n; i++) { arr[sc.nextInt()] = i + 1; } for (int i = 1; i <= 2 * n; i++) { if (arr[i] == -1) continue; for (int j = 1; j * i <= 2 * n; j++) { if (i == j || arr[j] == -1) continue; if (arr[i] + arr[j] == (long) i * j) { ++count; } } } count /= 2; pw.println(count); } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } // public long fastPow(long x, long y) { // if (y == 0) return 1; // if (y == 1) return x; // long temp = fastPow(x, y / 2); // long ans = (temp * temp); // return (y % 2 == 1) ? (ans * x) : 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
752444fc804400ccc77b68633dd3458c
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 PlesentPairs { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int tt = sc.nextInt(); while(tt != 0){ int n = sc.nextInt(); ArrayList<Long> arr = new ArrayList<>(); for(int i = 0;i < n;i++){ arr.add(sc.nextLong()); } int ans = 0; for(int i = 0;i < arr.size();i++){ for(int j = (int) (arr.get(i)-i)-2;j < arr.size();j+=(arr.get(i))){ if(j > 0 ) { if(i < j && arr.get(i) * arr.get(j) == (i + j + 2)) { ans++; } } } } System.out.println(ans); tt--; } } }
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
a30f1c29da4faa69b46ed00ba78c7f42
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 PlesentPairs { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int tt = sc.nextInt(); while(tt != 0){ int n = sc.nextInt(); long arr[] = new long[n+5]; for(int i = 1;i <= n;i++){ arr[i] = sc.nextLong(); } int 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(i < j && arr[i] * arr[j] == i + j) { ans++; } } } } System.out.println(ans); tt--; } } }*/ import java.util.*; public class PlesentPairs { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int tt = sc.nextInt(); while(tt != 0){ int n = sc.nextInt(); ArrayList<Long> arr = new ArrayList<>(); for(int i = 0;i < n;i++){ arr.add(sc.nextLong()); } int ans = 0; for(int i = 0;i < arr.size();i++){ for(int j = (int) (arr.get(i)-i)-2;j < arr.size();j+=(arr.get(i))){ if(j >= 0 && i < j && arr.get(i)*arr.get(j) == (i+j+2)){ ans++; } } } System.out.println(ans); tt--; } } }
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
20caeadfc6e00b379ced392c919643dc
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 PlesentPairs { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int tt = sc.nextInt(); while(tt != 0){ int n = sc.nextInt(); long arr[] = new long[n+5]; for(int i = 1;i <= n;i++){ arr[i] = sc.nextLong(); } int 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(i < j && arr[i] * arr[j] == i + j) { ans++; } } } } System.out.println(ans); tt--; } } }
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
087fa15e74327c55da28e5f3f0f13f3a
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 pleasentpairs{ public static void main(String ...asda){ 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(); int c=0; for(int i=1;i<=n;i++){ int prod=a[i]; for(int j=prod;j<=2*n;j+=prod){ if(i<(j-i) && j-i<=n && j-i>0 && a[j-i]==j/a[i]){ c++; //System.out.println(i+" "+j+" "+c); } if(j-i>n) break; } //System.out.println("value "+i+" "+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
fa503ddb250375a0edfca2119768c231
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; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Reader s = new Reader(); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); long[] a = new long[n+1]; for (int i = 1; i <= n; i++) { a[i] = s.nextLong(); } int ctr = 0; for (int i = 1; i <= n; i++) { for (int j = (int)a[i]-i; j <= n; j+=(int)a[i]) { if(j>=0){ if(a[i]*a[j]==i+j && i<j) ctr++; } } } System.out.println(ctr); } } }
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
6ecc312c62e3ab80b0ed9bbeab067658
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; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream( new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } buf[cnt++] = (byte)c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws IOException { Reader s = new Reader(); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = s.nextLong(); } int ctr = 0; for (int i = 0; i < n; i++) { int k = (int)(a[i]-2*(i+1)%a[i]); for (int j = i+k; j < n; j+=(int)a[i]) { if(a[i]*a[j]==i+j+2) ctr++; } } System.out.println(ctr); } } }
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
871b90940dacc77757fc7f8485900cdb
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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class PleasantPairs { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer st = new StringTokenizer(""); public static String next() { try { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public static int ni() { return Integer.parseInt(next()); } public static void main(String[] args) throws IOException { // try { // br = new BufferedReader(new FileReader("C:/Users/gunjan.kumar01/Desktop/Java/codeforces/in.txt")); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } int t = ni(); while (t-- > 0) { int n = ni(); // int[] arr = Arrays.stream(br.readLine().split(" // ")).mapToInt(Integer::parseInt).toArray(); int[] arr = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = ni(); } // solve(size, arr); solve2(n, arr); } out.flush(); out.close(); } public static void solve(int size, int[] arr) { int less = 0; // int more = 0; int n = arr.length; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i < j && (arr[i] * arr[j] == i + j + 2)) { less++; // out.println(" I < J : " + i + ", " + j + "\tPAIR : " + arr[i] + ", " + // arr[j]); } else { // more++; // out.println("NOT I < J : " + i + ", " + j + "\tPAIR : " + arr[i] + ", " + // arr[j]); } } } // out.println(Arrays.toString(arr) + " | count total -" + (less + more) + " // less : " + less + " more : " + more); out.println(less); } public static void solve2(int n, int[] arr) { int count = 0; int len = arr.length; int[] map = new int[2 * n + 2]; for (int x = 1; x < len; x++) { map[arr[x]] = x; } for (int i = 1; i < len; i++) { int x = arr[i]; // first number int y = 1; // second number int j = map[y]; // get index of second number long prod = x * (long) y; // product while (prod < 2 * n) { if (j > i && prod == i + j) count++; // out.printf("First : %d Second : %d Index first : %d, Index second : %d ------ // Count %d \n", x, y, // i + 1, j, count); y++; j = map[y]; prod = x * (long) y; } } out.println(count); } public static void solve3(int n, int[] arr) { int count = 0; // int len = n+1; for (int i = 1; i <= n; i++) { int x = arr[i]; for (int j = x - i; j <= n; j += x) { // int y = arr[j]; if (j > i && x * arr[j] == i + j) { count++; // out.printf("First : %d Second : %d Index first : %d, Index second : %d ------ // Count %d \n", x, // arr[j], i , j, count); } } } 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
aa514a90d64eaac5cac3d54897b55c5d
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.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class PleasantPairs { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer st = new StringTokenizer(""); public static String next() { try { if (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public static int ni() { return Integer.parseInt(next()); } public static void main(String[] args) throws IOException { // try { // br = new BufferedReader(new FileReader("C:/Users/gunjan.kumar01/Desktop/Java/codeforces/in.txt")); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } int t = ni(); while (t-- > 0) { int n = ni(); // int[] arr = Arrays.stream(br.readLine().split(" // ")).mapToInt(Integer::parseInt).toArray(); int[] arr = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = ni(); } // solve(size, arr); solve3(n, arr); } out.flush(); out.close(); } public static void solve(int size, int[] arr) { int less = 0; // int more = 0; int n = arr.length; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (i < j && (arr[i] * arr[j] == i + j + 2)) { less++; // out.println(" I < J : " + i + ", " + j + "\tPAIR : " + arr[i] + ", " + // arr[j]); } else { // more++; // out.println("NOT I < J : " + i + ", " + j + "\tPAIR : " + arr[i] + ", " + // arr[j]); } } } // out.println(Arrays.toString(arr) + " | count total -" + (less + more) + " // less : " + less + " more : " + more); out.println(less); } public static void solve2(int n, int[] arr) { int count = 0; int len = arr.length; int[] map = new int[2 * n + 1]; for (int x = 0; x < len; x++) { map[arr[x]] = x + 1; } for (int i = 0; i < len; i++) { int x = arr[i]; // first number int y = 1; // second number int j = map[y]; // get index of second number long prod = x * y; // product while (prod < 2 * n) { if (j != 0 && prod == i + j + 1) count++; out.printf("First : %d Second : %d Index first : %d, Index second : %d ------ Count %d \n", x, y, i + 1, j, count); y++; prod = x * y; j = map[y]; } } out.println("Ans = " + count); } public static void solve3(int n, int[] arr) { int count = 0; // int len = n+1; for (int i = 1; i <= n; i++) { int x = arr[i]; for (int j = x - i; j <= n; j += x) { // int y = arr[j]; if (j > i && x * (long)arr[j] == i + j) { count++; // out.printf("First : %d Second : %d Index first : %d, Index second : %d ------ Count %d \n", x, // arr[j], i , j, count); } } } 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
a572ef33e63c7b45cf98f89ec36901fd
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 Extras { static class pair { long first; long second; pair(long f, long s) { first = f; second = s; } // @Override // public String toString() { // return "pair{" + // "first=" + first + // ", second=" + second + // '}'; // } } public static void main(final 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()); String[] inp = br.readLine().split(" "); pair[] a = new pair[n]; for(int i = 0; i < n; i++) { long input = Long.parseLong(inp[i]); a[i] = new pair(input, i+1); } Arrays.sort(a, Comparator.comparing((pair x) -> x.first)); // System.out.println(Arrays.toString(a)); int count = 0; for(int i = 0; i < n - 1; i++) { for(int j = i + 1; j < n; j++) { // System.out.println(i + ", " + j); if(a[i].first * a[j].first >= 2L * n) break; if(a[i].first * a[j].first == a[i].second + a[j].second) { 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
441412cade27a80dead93256b1267b0c
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { long n = in.nextInt(); long[] arr = new long[(int) (n + 1)]; //i 가 1부터 시작해야 편하므로, long count = 0; for (long i = 1; i <= n; i++) { arr[(int) i] = in.nextInt(); } for (long i = 1; i <= n - 1; i++) { for (long j = arr[(int) i] - i; j <= n; j += arr[(int) i]) { if (j > 1) { //ai - i 가 음수이거나 0이 나올 수 있다 if (arr[(int) i] * arr[(int) j] == i + j && i < j) { //i + j = ai * aj && j가 i보다 커야한다 count++; } } } } out.println(count); } } 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
["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
960c5fd18d738aed0851ef64003827cb
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.math.*; import java.util.*; /* author : Multi-Thread */ public class B { // public class Main { // static int INF = 998244353; static int INF = (int) 1e9 + 7; static int MAX = Integer.MAX_VALUE; static int MIN = Integer.MIN_VALUE; public static void main(String[] args) { int test = fs.nextInt(); // int test = 1; for (int cases = 0; cases < test; cases++) { // solve(); System.out.print(solve() + "\n"); } } static String solve() { int n = fs.nextInt(); long[] hm = new long[2 * n + 1]; long[] rev = new long[n + 1]; ArrayList<Long> al = new ArrayList<Long>(); for (int i = 0; i < n; i++) { long x = fs.nextLong(); hm[(int) x] = (long) i + 1; rev[i + 1] = (long) x; al.add(x); } Collections.sort(al); long ans = 0; for (int i = 0; i < n; i++) { long index = hm[al.get(i).intValue()]; long j = al.get(i) + 1; for (; j * al.get(i) <= n + n - 1; j++) { long need = (long) al.get(i) * j; long index2 = need - index; if (index2 >= 1 && index2 <= n) { long next = rev[(int) index2]; if (next == j) { ++ans; } } } } return ans + ""; } static void intSort(int[] a, boolean reverse) { ArrayList<Integer> al = new ArrayList<Integer>(); for (int i : a) al.add(i); Collections.sort(al); if (reverse) { for (int i = 0; i < a.length; i++) a[i] = al.get(a.length - i - 1); } else { for (int i = 0; i < a.length; i++) a[i] = al.get(i); } } static void longSort(long[] a, boolean reverse) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) al.add(i); Collections.sort(al); if (reverse) { for (int i = 0; i < a.length; i++) a[i] = al.get(a.length - i - 1); } else { for (int i = 0; i < a.length; i++) a[i] = al.get(i); } } public static int[] radixSort(int[] f) { return radixSort(f, f.length); } public static int[] radixSort(int[] f, int n) { int[] to = new int[n]; { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (f[i] & 0xffff)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[f[i] & 0xffff]++] = f[i]; int[] d = f; f = to; to = d; } { int[] b = new int[65537]; for (int i = 0; i < n; i++) b[1 + (f[i] >>> 16)]++; for (int i = 1; i <= 65536; i++) b[i] += b[i - 1]; for (int i = 0; i < n; i++) to[b[f[i] >>> 16]++] = f[i]; int[] d = f; f = to; to = d; } return f; } static class Pair { int first, second; public Pair(int first, int second) { this.first = first; this.second = second; } public String toString() { return "[" + first + "," + second + "]"; } } static class LongPair { long first; long second; LongPair(long a, long b) { this.first = a; this.second = b; } public String toString() { return "[" + first + "," + second + "]"; } } 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 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 println() { writer.print("\n"); } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } private static final FastReader fs = new FastReader(); private static final OutputWriter out = new OutputWriter(System.out); }
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
7c528ba26b4794e72ff3014787b77a24
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; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.io.IOException; public class pleasantpairs { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int tt = in.nextInt(); BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); for(int w = 0; w<tt; w++) { int n = in.nextInt(); int[] a = new int[n+1]; for(int i = 1; i<=n; i++) { a[i] = in.nextInt(); } int broj = 0; for(int i = 1; i<=n; i++) { for(int j = a[i]-i; j<=n; j+=a[i]) { if(j<=i) { continue; } if((long) a[i]*a[j]==i+j) { broj++; } } } log.write(broj+"\n"); log.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
c2dd14ca3b2abd57f2000cbf00c6e988
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.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.*; import static java.lang.System.out ; public class CP0065 { public static void main(String args[])throws Exception{ PrintWriter pw = new PrintWriter(out); FastReader sc = new FastReader(); Reader s = new Reader(); int test; //code yaha se hai try{ test = s.nextInt(); } catch(Exception E){ return; } while (test-- > 0){ int n = s.nextInt(); HashMap<Integer , Integer> mp = new HashMap<>(); ArrayList<Integer> ar = new ArrayList<>(); for (int i = 0; i < n; i++) { ar.add(s.nextInt()); mp.put(ar.get(i) , i+1); } Collections.sort(ar); long val ; int count = 0 ; for (int i = 0; i < n; i++) { if(ar.get(i) >= 2 * n){ break; } for (int j = i+1; j < n; j++) { val = 1l * ar.get(i)*ar.get(j) ; if(val >= 2 * n ){ break; } if(val == (mp.get(ar.get(i)) + mp.get(ar.get(j)))){ count++; } } } pw.println(count); } pw.close(); } /* Note : Try using sorting , when approach is of O(N^2) As sorting takes : O( N Log N ) */ /* len : Length of Array / inputs num : key to map //Putting values in map <Integer , ArrayList> for (int i = 0; i < len; i++) { num =sc.nextInt(); if(!mp.containsKey(num)){ mp.put(num ,new ArrayList<>()); } } //getting size of ArrayList for(Integer key : mp.keySet()){ maxValue = max(maxValue , mp.get(key).size()); } */ /* Sieve 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; } } } */ static long exponentiation(long base, long exp) { long N = 1000000007 ; if (exp == 0) return 1; if (exp == 1) return base % N; long t = exponentiation(base, exp / 2); t = (t * t) % N; // if exponent is even value if (exp % 2 == 0) return t; // if exponent is odd value else return ((base % N) * t) % N; } public static int BinarySearch (long ar[] , int start , int end , long key , boolean lower){ int mid ; boolean found = false; int ans = 0; if(lower){ while(start <= end){ mid = start + (end-start) / 2; if(ar[mid] < key ){ if(!found){ ans = mid; } start = mid+1; } else{ if(ar[mid] == key){ found = true; ans = mid ; } end = mid-1; } } } else{ while(start <= end){ mid = start + (end-start) / 2; if(ar[mid] <= key ){ if(ar[mid] == key){ found = true; ans = mid; } start = mid+1; } else{ if(!found){ ans = mid; } end = mid-1; } } } return ans; } public static int BinarySearchArrayList (ArrayList<Long> ar , int start , int end , long key , boolean lower){ int mid ; boolean found = false; int ans = -1; if(lower){ while(start <= end){ mid = start + (end-start) / 2; if(ar.get(mid) < key ){ if(!found){ ans = mid; } start = mid+1; } else{ if(ar.get(mid) == key){ found = true; ans = mid ; } end = mid-1; } } } else{ while(start <= end){ mid = start + (end-start) / 2; if(ar.get(mid) <= key ){ if(ar.get(mid) == key){ found = true; ans = mid; } start = mid+1; } else{ if(!found){ ans = mid; } end = mid-1; } } } return ans; } public static String reverseString(String str){ StringBuilder input1 = new StringBuilder(); // append a string into StringBuilder input1 input1.append(str); // reverse StringBuilder input1 input1.reverse(); return input1+""; } public static void sort(int[] arr){ //because Arrays.sort() uses quick sort , Worst Complexity : o(N^2) ArrayList <Integer> ls = new ArrayList<>(); for(int x:arr){ ls.add(x); } Collections.sort(ls); //Collections.sort(arrayList) : Uses Merge Sort : Complexity O(NlogN) for(int i = 0 ; i < arr.length ; i++){ arr[i] = ls.get(i); } } public static long gcd(long a , long b){ if(a>b){ a = (a+b) - (a = b); } if(a == 0L){ return b; } return gcd(b%a ,a); } 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 sq = (long)Math.sqrt(n) + 1; for(long i = 5L; i <=sq ; i=i+6){ if(n % i == 0 || n % (i+2) == 0){ return false; } } return true; } public static class Pair implements Comparable<Pair> { long first ; long second; Pair(long fst , long scnd){ first = fst; second = scnd; } public int compareTo(Pair obj){ if(this.first > obj.first){ return 1; } else if(this.first < obj.first){ return -1; } return 0; } } 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(); } } 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
167c29856c1386f9677fc66215d22084
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.System.out; import static java.lang.Math.*; import java.awt.desktop.AboutHandler; import java.lang.reflect.Array; import java.util.*; import java.io.*; import java.math.*; import java.util.concurrent.Phaser; public class Main { static FastReader sc; static long mod = ((long) 1e9) + 7; // static FastWriter out; static Tree tree; static PrintWriter out, err; static int imax = Integer.MAX_VALUE; static int imin = Integer.MIN_VALUE; static long lmax = Long.MAX_VALUE; static long lmin = Long.MIN_VALUE; static double dmax = Double.MAX_VALUE; static double dmin = Double.MIN_VALUE; public static void main(String hi[]) throws IOException { // initializeIO(); out = new PrintWriter(System.out); err = new PrintWriter(System.err); sc = new FastReader(); // sc=new Scanner(System.in); long startTimeProg = System.currentTimeMillis(); long endTimeProg; int t = 1; t = sc.nextInt(); // boolean[] seave=sieveOfEratosthenes((int)(1e5)); // int[] seave2=sieveOfEratosthenesInt((long)sqrt(1e9)); // debug(seave2); while (t-- != 0) { int n = sc.nextInt(); long[][] arr = new long[n][2]; for (int i = 0; i < n; i++) { arr[i][0] = sc.nextInt(); arr[i][1] = i + 1; } Arrays.sort(arr,(a,b)->{ return (int)(a[0]-b[0]); }); debug(arr); long size=(2*n)-1; long c = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i][0] * arr[j][0] > size) break; if (arr[i][0] * arr[j][0] == arr[i][1] + arr[j][1]) c++; } } print(c); } endTimeProg = System.currentTimeMillis(); debug("[finished : " + (endTimeProg - startTimeProg) + ".ms ]"); out.flush(); err.flush(); // System.out.println(String.format("%.9f", max)); } private static long calculateSum(char[] s) throws IOException { int n=s.length; long ans=0; for(int i =0;i<n-1;i++){ String st=((s[i]-'0')+""+(s[i+1]-'0')); // debug(st); ans+=Integer.parseInt(st); } return ans; } private static boolean chk(long[] res) { int n=res.length; for(int i=0;i<n;i++){ if((res[(i+1)%n]<res[i]&&res[((i-1)<0)?n-1:i-1]<res[i])|| (res[(i+1)%n]>res[i]&&res[((i-1)<0)?n-1:i-1]>res[i]))continue; else return false; } return true; } static String game(int[] arr1,int[] arr2,boolean see){ int n=arr1.length; int m=arr2.length; int i=n-1,j=m-1; int card=0; while (i>=0&&j>=0){ if(see){ while (i>=0&&arr1[i]<=card)i--; if(i<0){ return "Bob"; } card=arr1[i]; }else{ while (j>=0&&arr2[j]<=card)j--; if(j<0){ return "Alice"; } card=arr2[j]; } see=!see; } return "alice"; } static int CeilIndex(int A[], int l, int r, int key) { while (r - l > 1) { int m = l + (r - l) / 2; if (A[m] > key) r = m; else l = m; } return r; } static int LongestIncreasingSubsequenceLength(int A[], int size) { // Add boundary case, when array size is one int[] tailTable = new int[size]; int len; // always points empty slot tailTable[0] = A[0]; len = 1; for (int i = 1; i < size; i++) { if (A[i] < tailTable[0]) // new smallest value tailTable[0] = A[i]; else if (A[i] > tailTable[len - 1]) // A[i] wants to extend largest subsequence tailTable[len++] = A[i]; else // A[i] wants to be current end candidate of an existing // subsequence. It will replace ceil value in tailTable tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i]; } return len; } public static int log2(int N) { // calculate log2 N indirectly // using log() method int result = (int)(Math.log(N) / Math.log(2)); return result; } private static String toString(int[] arr){ StringBuilder sb=new StringBuilder(); for(int i=0;i<arr.length;i++)sb.append(arr[i]+" "); return sb.toString(); } private static String toString(long[] arr){ StringBuilder sb=new StringBuilder(); for(int i=0;i<arr.length;i++)sb.append(arr[i]+" "); return sb.toString(); } private static String toString(List<Integer> arr){ StringBuilder sb=new StringBuilder(); for(int i=0;i<arr.size();i++)sb.append(arr.get(i)+" "); return sb.toString(); } private static String toStringLong(List<Long> arr){ StringBuilder sb=new StringBuilder(); for(int i=0;i<arr.size();i++)sb.append(arr.get(i)+" "); return sb.toString(); } private static void print(String s) throws IOException { out.println(s); } static boolean isSubsetSum(int set[], int n, int sum) { // The value of subset[i][j] will be // true if there is a subset of // set[0..j-1] with sum equal to i boolean subset[][] = new boolean[sum + 1][n + 1]; // If sum is 0, then answer is true for (int i = 0; i <= n; i++) subset[0][i] = true; // If sum is not 0 and set is empty, // then answer is false for (int i = 1; i <= sum; i++) subset[i][0] = false; // Fill the subset table in bottom // up manner for (int i = 1; i <= sum; i++) { for (int j = 1; j <= n; j++) { subset[i][j] = subset[i][j - 1]; if (i >= set[j - 1]) subset[i][j] = subset[i][j] || subset[i - set[j - 1]][j - 1]; } } return subset[sum][n]; } private static String chk(long[] arr, int n) throws IOException { boolean see=true; int dec=0,eq=0; int s=0; for(int i=1;i<n;i++){ if(arr[i]<=arr[i-1]) { if(!see&&arr[i]<arr[i-1])return "no"; if(arr[i]<arr[i-1]&&s<=0)return "no"; s--; }else{ if(s>1)return "no"; see=false; } } return "yes"; } private static boolean isPeak(int[] arr,int i,int l,int r){ if(i==l||i==r)return false; return (arr[i+1]<arr[i]&&arr[i]>arr[i-1]); } private static long kadens(List<Long> li, int l, int r) { long max = Long.MIN_VALUE; long ans = 0; for (int i = l; i <= r; i++) { ans += li.get(i); max = max(ans, max); if (ans < 0) ans = 0; } return max; } public static boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { double d = Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false; } return true; } private static boolean isSorted(List<Integer> li) { int n = li.size(); if (n <= 1) return true; for (int i = 0; i < n - 1; i++) { if (li.get(i) > li.get(i + 1)) return false; } return true; } private static boolean isSorted(int[] arr) { int n = arr.length; if (n <= 1) return true; for (int i = 0; i < n - 1; i++) { if (arr[i] >arr[i+1]) return false; } return true; } static boolean isPowerOfTwo(long x) { return x != 0 && ((x & (x - 1)) == 0); } private static boolean ispallindromeList(List<Integer> res) { int l = 0, r = res.size() - 1; while (l < r) { if (res.get(l) != res.get(r)) return false; l++; r--; } return true; } private static class Pair { int first = 0; int sec = 0; int[] arr; char ch; String s; Map<Integer, Integer> map; Pair(int first, int sec) { this.first = first; this.sec = sec; } Pair(int[] arr) { this.map = new HashMap<>(); for (int x : arr) this.map.put(x, map.getOrDefault(x, 0) + 1); this.arr = arr; } Pair(char ch, int first) { this.ch = ch; this.first = first; } Pair(String s, int first) { this.s = s; this.first = first; } } private static int sizeOfSubstring(int st, int e) { int s = e - st + 1; return (s * (s + 1)) / 2; } private static Set<Long> factors(long n) { Set<Long> res = new HashSet<>(); // res.add(n); for (long i = 1; i * i <= (n); i++) { if (n % i == 0) { res.add(i); if (n / i != i) { res.add(n / i); } } } return res; } private static long fact(long n) { if (n <= 2) return n; return n * fact(n - 1); } private static long ncr(long n, long r) { return fact(n) / (fact(r) * fact(n - r)); } private static int lessThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int lessThen(List<Integer> nums, int val, boolean work, int l, int r) { int i = -1; if (work) i = 0; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) <= val) { i = mid; l = mid + 1; } else { r = mid - 1; } } return i; } private static int greaterThen(List<Long> nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(List<Integer> nums, int val, boolean work) { int i = -1, l = 0, r = nums.size() - 1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums.get(mid) >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static int greaterThen(long[] nums, long val, boolean work, int l, int r) { int i = -1; if (work) i = r; while (l <= r) { int mid = l + ((r - l) / 2); if (nums[mid] >= val) { i = mid; r = mid - 1; } else { l = mid + 1; } } return i; } private static void swap(char[] s,int i,int j){ char t=s[i]; s[i]=s[j]; s[j]=t; } private static long gcd(long[] arr) { long ans = 0; for (long x : arr) { ans = gcd(x, ans); } return ans; } private static int gcd(int[] arr) { int ans = 0; for (int x : arr) { ans = gcd(x, ans); } return ans; } private static long sumOfAp(long a, long n, long d) { long val = (n * (2 * a + ((n - 1) * d))); return val / 2; } //geometrics private static double areaOftriangle(double x1, double y1, double x2, double y2, double x3, double y3) { double[] mid_point = midOfaLine(x1, y1, x2, y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height = distanceBetweenPoints(mid_point[0], mid_point[1], x3, y3); double wight = distanceBetweenPoints(x1, y1, x2, y2); // debug(height+" "+wight); return (height * wight) / 2; } private static double distanceBetweenPoints(double x1, double y1, double x2, double y2) { double x = x2 - x1; double y = y2 - y1; return (Math.pow(x, 2) + Math.pow(y, 2)); } public static boolean isPerfectSquareByUsingSqrt(long n) { if (n <= 0) { return false; } double squareRoot = Math.sqrt(n); long tst = (long) (squareRoot + 0.5); return tst * tst == n; } private static double[] midOfaLine(double x1, double y1, double x2, double y2) { double[] mid = new double[2]; mid[0] = (x1 + x2) / 2; mid[1] = (y1 + y2) / 2; return mid; } private static long sumOfN(long n) { return (n * (n + 1)) / 2; } private static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y) { long temp; if (y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp * temp); else return (x * temp * temp); } private static StringBuilder reverseString(String s) { StringBuilder sb = new StringBuilder(s); int l = 0, r = sb.length() - 1; while (l <= r) { char ch = sb.charAt(l); sb.setCharAt(l, sb.charAt(r)); sb.setCharAt(r, ch); l++; r--; } return sb; } private static void swap(List<Integer> li, int i, int j) { int t = li.get(i); li.set(i, li.get(j)); li.set(j, t); } private static void swap(int[] arr, int i, int j) { int t = arr[i]; arr[i]=arr[j]; arr[j]=t; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static String decimalToString(int x) { return Integer.toBinaryString(x); } private static String decimalToString(long x) { return Long.toBinaryString(x); } private static boolean isPallindrome(String s, int l, int r) { while (l < r) { if (s.charAt(l) != s.charAt(r)) return false; l++; r--; } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb) { int i = 0; while (i < sb.length() && sb.charAt(i) == '0') i++; // debug("remove "+i); if (i == sb.length()) return new StringBuilder(); return new StringBuilder(sb.substring(i, sb.length())); } private static StringBuilder removeEndingZero(StringBuilder sb) { int i = sb.length() - 1; while (i >= 0 && sb.charAt(i) == '0') i--; if (i < 0) return new StringBuilder(); return new StringBuilder(sb.substring(0, i + 1)); } private static int stringToDecimal(String binaryString) { return Integer.parseInt(binaryString, 2); } private static void debug(int[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr) { for (int[] a : arr) { err.println(Arrays.toString(a)); } } private static void debug(float[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr) { for (int i = 0; i < arr.length; i++) { err.println(Arrays.toString(arr[i])); } } // private static void print() throws IOException { // out.println(s); // } private static void debug(String s) throws IOException { err.println(s); } private static int charToIntS(char c) { return ((((int) (c - '0')) % 48)); } private static void print(double s) throws IOException { out.println(s); } private static void print(float s) throws IOException { out.println(s); } private static void print(long s) throws IOException { out.println(s); } private static void print(int s) throws IOException { out.println(s); } private static void debug(double s) throws IOException { err.println(s); } private static void debug(float s) throws IOException { err.println(s); } private static void debug(long s) { err.println(s); } private static void debug(int s) { err.println(s); } private static boolean isPrime(long n) { // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } private static List<List<Integer>> readUndirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals, int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < intervals.length; i++) { int x = intervals[i][0]; int y = intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = sc.next(); } return arr; } private static Map<Character, Integer> freq(String s) { Map<Character, Integer> map = new HashMap<>(); for (char c : s.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } return map; } private static Map<Long, Integer> freq(long[] arr) { Map<Long, Integer> map = new HashMap<>(); for (long x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } private static Map<Integer, Integer> freq(int[] arr) { Map<Integer, Integer> map = new HashMap<>(); for (int x : arr) { map.put(x, map.getOrDefault(x, 0) + 1); } return map; } static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int) n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n) { boolean prime[] = new boolean[(int) n + 1]; Set<Integer> li = new HashSet<>(); for (int i = 2; i <= n; i++) { prime[i] = true; } for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) { prime[i] = false; } } } for(int i=0;i<=n;i++){ if(prime[i])li.add(i); } int[] arr = new int[li.size()]; int i = 0; for (int x : li) { arr[i++] = x; } return arr; } public static long Kadens(List<Long> prices) { long sofar = 0; long max_v = 0; for (int i = 0; i < prices.size(); i++) { sofar += prices.get(i); if (sofar < 0) { sofar = 0; } max_v = Math.max(max_v, sofar); } return max_v; } public static int Kadens(int[] prices) { int sofar = 0; int max_v = 0; for (int i = 0; i < prices.length; i++) { sofar += prices[i]; if (sofar < 0) { sofar = 0; } max_v = Math.max(max_v, sofar); } return max_v; } static boolean isMemberAC(int a, int d, int x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } static boolean isMemberAC(long a, long d, long x) { // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(int[] arr) { int n = arr.length; List<Integer> li = new ArrayList<>(); for (int x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(double[] arr) { int n = arr.length; List<Double> li = new ArrayList<>(); for (double x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sortReverse(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li, Collections.reverseOrder()); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static void sort(long[] arr) { int n = arr.length; List<Long> li = new ArrayList<>(); for (long x : arr) { li.add(x); } Collections.sort(li); for (int i = 0; i < n; i++) { arr[i] = li.get(i); } } private static long sum(int[] arr) { long sum = 0; for (int x : arr) { sum += x; } return sum; } private static long sum(long[] arr) { long sum = 0; for (long x : arr) { sum += x; } return sum; } private static long evenSumFibo(long n) { long l1 = 0, l2 = 2; long sum = 0; while (l2 < n) { long l3 = (4 * l2) + l1; sum += l2; if (l3 > n) break; l1 = l2; l2 = l3; } return sum; } private static void initializeIO() { try { System.setIn(new FileInputStream("input")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr) { int max = Integer.MIN_VALUE; for (int x : arr) { max = Math.max(max, x); } return max; } private static long maxOfArray(long[] arr) { long max = Long.MIN_VALUE; for (long x : arr) { max = Math.max(max, x); } return max; } private static int[][] readIntIntervals(int n, int m) { int[][] arr = new int[n][m]; for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { arr[j][i] = sc.nextInt(); } } return arr; } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static int[] readIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextDouble(); } return arr; } private static long[] readLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } return arr; } private static void print(int[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void print(long[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void print(String[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void print(double[] arr) throws IOException { out.println(Arrays.toString(arr)); } private static void debug(String[] arr) { err.println(Arrays.toString(arr)); } private static void debug(Boolean[][] arr) { for (int i = 0; i < arr.length; i++) err.println(Arrays.toString(arr[i])); } private static void debug(int[] arr) { err.println(Arrays.toString(arr)); } private static void debug(long[] arr) { err.println(Arrays.toString(arr)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class Tree{ List<List<Integer>> adj=new ArrayList<>(); int parent=1; int[] arr; int n; Map<Integer,Integer> node_parent; List<Integer> leaf; public Tree(int n) { this.n=n; leaf=new ArrayList<>(); node_parent=new HashMap<>(); arr=new int[n+2]; for(int i=0;i<=n+1;i++){ adj.add(new ArrayList<>()); } for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); node_parent.put(i+1,arr[i]); if((i+1)==arr[i])parent=arr[i]; else adj.get(arr[i]).add(i+1); } } private List<List<Integer>> getTree(){ return adj; } private void formLeaf(int v){ boolean leaf=true; for(int x:adj.get(v)){ formLeaf(x); leaf=false; } if(leaf)this.leaf.add(v); } private List<Integer> getLeaf(){ return leaf; } private Map<Integer,Integer> getParents(){ return node_parent; } int[] getArr(){ return arr; } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["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
b1d91e75d3aab577f61f387b2aa0fafe
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.Math.sqrt; import static java.lang.Math.pow; import static java.lang.System.out; import static java.lang.System.err; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; static long mod=((long)1e9)+7; // static FastWriter out; public static void main(String hi[]){ initializeIO(); sc=new FastReader(); int t=sc.nextInt(); // boolean[] seave=sieveOfEratosthenes((int)(1e7)); // int[] seave2=sieveOfEratosthenesInt((int)(1e4)); // int tp=1; while(t--!=0){ int n=sc.nextInt(); long[][] arr=new long[n][2]; for(int i=0;i<n;i++){ arr[i][0]=sc.nextLong(); arr[i][1]=i+1; } Arrays.sort(arr,(a,b)->{ return (int)(a[0]-b[0]); }); int size=n+(n-1); long c=0; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if((arr[i][0]*arr[j][0])>size)break; if((arr[i][0]*arr[j][0])==(arr[i][1]+arr[j][1]))c++; } } out.println(c); } // System.out.println(String.format("%.9f", max)); } static Boolean[][] dp; static List<Integer> res=new ArrayList<>(); private static boolean solve(long[] arr,long w,long c,int i,List<Integer> li){ int n=arr.length; long mid=(long)Math.ceil((1f*w)/(1f*2)); if(c>=(mid)&&c<=w){ res=new ArrayList<>(li); return true; } if(i>=n||c>w)return false; if(dp[i][(int)c]!=null)return dp[i][(int)c]; li.add(i+1); boolean left=false; if(c+arr[i]<=w)left=left|solve(arr,w,c+arr[i],i+1,li); li.remove(li.size()-1); boolean right=solve(arr,w,c,i+1,li); return dp[i][(int)c]=left|right; } private static int sizeOfSubstring(int st,int e){ int s=e-st+1; return (s*(s+1))/2; } private static int lessThen(long[] nums,long val){ int i=0,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int lessThen(List<Long> nums,long val){ int i=0,l=0,r=nums.size()-1; while(l<=r){ int mid=l+((r-l)/2); if(nums.get(mid)<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int greaterThen(long[] nums,long val){ int i=nums.length-1,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]>=val){ i=mid; r=mid-1; }else{ l=mid+1; } } return i; } private static long sumOfAp(long a,long n,long d){ long val=(n*( 2*a+((n-1)*d))); return val/2; } //geometrics private static double areaOftriangle(double x1,double y1,double x2,double y2,double x3,double y3){ double[] mid_point=midOfaLine(x1,y1,x2,y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height=distanceBetweenPoints(mid_point[0],mid_point[1],x3,y3); double wight=distanceBetweenPoints(x1,y1,x2,y2); // debug(height+" "+wight); return (height*wight)/2; } private static double distanceBetweenPoints(double x1,double y1,double x2,double y2){ double x=x2-x1; double y=y2-y1; return sqrt(Math.pow(x,2)+Math.pow(y,2)); } private static double[] midOfaLine(double x1,double y1,double x2,double y2){ double[] mid=new double[2]; mid[0]=(x1+x2)/2; mid[1]=(y1+y2)/2; return mid; } private static long sumOfN(long n){ return (n*(n+1))/2; } private static long power(long x,long y,long p){ long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0){ // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y){ long temp; if( y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp*temp); else return (x*temp*temp); } private static StringBuilder reverseString(String s){ StringBuilder sb=new StringBuilder(s); int l=0,r=sb.length()-1; while(l<=r){ char ch=sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,ch); l++; r--; } return sb; } private static void swap(long arr[],int i,int j){ long t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(List<Integer> li,int i,int j){ int t=li.get(i); li.set(i,li.get(j)); li.set(j,t); } private static void swap(int arr[],int i,int j){ int t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(double arr[],int i,int j){ double t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(float arr[],int i,int j){ float t=arr[i]; arr[i]=arr[j]; arr[j]=t; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b){ return (a / gcd(a, b)) * b; } private static String decimalToString(int x){ return Integer.toBinaryString(x); } private static boolean isPallindrome(String s){ int l=0,r=s.length()-1; while(l<r){ if(s.charAt(l)!=s.charAt(r))return false; l++; r--; } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb){ int i=0; while(i<sb.length()&&sb.charAt(i)=='0')i++; // debug("remove "+i); if(i==sb.length())return new StringBuilder(); return new StringBuilder(sb.substring(i,sb.length())); } private static StringBuilder removeEndingZero(StringBuilder sb){ int i=sb.length()-1; while(i>=0&&sb.charAt(i)=='0')i--; // debug("remove "+i); if(i<0)return new StringBuilder(); return new StringBuilder(sb.substring(0,i+1)); } private static int stringToDecimal(String binaryString){ // debug(decimalToString(n<<1)); return Integer.parseInt(binaryString,2); } private static int stringToInt(String s){ return Integer.parseInt(s); } private static String toString(int val){ return String.valueOf(val); } private static void debug(int[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr){ for(int[] a:arr){ err.println(Arrays.toString(a)); } } private static void debug(float[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void print(String s){ out.println(s); } private static void debug(String s){ err.println(s); } private static int charToIntS(char c){ return ((((int)(c-'0'))%48)); } private static void print(double s){ out.println(s); } private static void print(float s){ out.println(s); } private static void print(long s){ out.println(s); } private static void print(int s){ out.println(s); } private static void debug(double s){ err.println(s); } private static void debug(float s){ err.println(s); } private static void debug(long s){ err.println(s); } private static void debug(int s){ err.println(s); } private static boolean isPrime(int n){ // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } //read graph private static List<List<Integer>> readUndirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n){ String[] arr=new String[n]; for(int i=0;i<n;i++){ arr[i]=sc.next(); } return arr; } private static Map<Character,Integer> freq(String s){ Map<Character,Integer> map=new HashMap<>(); for(char c:s.toCharArray()){ map.put(c,map.getOrDefault(c,0)+1); } return map; } static boolean[] sieveOfEratosthenes(long n){ boolean prime[] = new boolean[(int)n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true){ // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n){ boolean prime[] = new boolean[(int)n + 1]; Set<Integer> li=new HashSet<>(); for (int i = 1; i <= n; i++){ prime[i] = true; li.add(i); } for (int p = 2; p * p <= n; p++) { if (prime[p] == true){ for (int i = p * p; i <= n; i += p){ li.remove(i); prime[i] = false; } } } int[] arr=new int[li.size()+1]; int i=0; for(int x:li){ arr[i++]=x; } return arr; } public static long Kadens(List<Long> prices) { long sofar=0; long max_v=0; for(int i=0;i<prices.size();i++){ sofar+=prices.get(i); if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } public static int Kadens(int[] prices) { int sofar=0; int max_v=0; for(int i=0;i<prices.length;i++){ sofar+=prices[i]; if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } static boolean isMemberAC(int a, int d, int x){ // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static long sum(int[] arr){ long sum=0; for(int x:arr){ sum+=x; } return sum; } private static long evenSumFibo(long n){ long l1=0,l2=2; long sum=0; while (l2<n) { long l3=(4*l2)+l1; sum+=l2; if(l3>n)break; l1=l2; l2=l3; } return sum; } private static void initializeIO(){ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { // System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr){ int max=Integer.MIN_VALUE; for(int x:arr){ max=Math.max(max,x); } return max; } private static long maxOfArray(long[] arr){ long max=Long.MIN_VALUE; for(long x:arr){ max=Math.max(max,x); } return max; } private static int[][] readIntIntervals(int n,int m){ int[][] arr=new int[n][m]; for(int j=0;j<n;j++){ for(int i=0;i<m;i++){ arr[j][i]=sc.nextInt(); } } return arr; } private static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } private static int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } private static int[] readIntArray(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n){ double[] arr=new double[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextDouble(); } return arr; } private static long[] readLongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } return arr; } private static void print(int[] arr){ out.println(Arrays.toString(arr)); } private static void print(long[] arr){ out.println(Arrays.toString(arr)); } private static void print(String[] arr){ out.println(Arrays.toString(arr)); } private static void print(double[] arr){ out.println(Arrays.toString(arr)); } private static void debug(String[] arr){ err.println(Arrays.toString(arr)); } private static void debug(Boolean[][] arr){ for(int i=0;i<arr.length;i++)err.println(Arrays.toString(arr[i])); } private static void debug(int[] arr){ err.println(Arrays.toString(arr)); } private static void debug(long[] arr){ err.println(Arrays.toString(arr)); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["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
d9112d8d373fcbd55d33b9e931c2e803
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.Math.sqrt; import static java.lang.Math.pow; import static java.lang.System.out; import static java.lang.System.err; import java.util.*; import java.io.*; import java.math.*; public class Main { static FastReader sc; static long mod=((long)1e9)+7; // static FastWriter out; public static void main(String hi[]){ initializeIO(); sc=new FastReader(); int t=sc.nextInt(); // boolean[] seave=sieveOfEratosthenes((int)(1e7)); // int[] seave2=sieveOfEratosthenesInt((int)(1e4)); // int tp=1; while(t--!=0){ int n=sc.nextInt(); long[][] arr=new long[n][2]; for(int i=0;i<n;i++){ arr[i][0]=sc.nextLong(); arr[i][1]=i+1; } Arrays.sort(arr,(a,b)->{ return (int)(a[0]-b[0]); }); int size=n+(n-1); long c=0; for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if((arr[i][0]*arr[j][0])>size)break; if((arr[i][0]*arr[j][0])==(arr[i][1]+arr[j][1]))c++; } } out.println(c); } // System.out.println(String.format("%.9f", max)); } static Boolean[][] dp; static List<Integer> res=new ArrayList<>(); private static boolean solve(long[] arr,long w,long c,int i,List<Integer> li){ int n=arr.length; long mid=(long)Math.ceil((1f*w)/(1f*2)); if(c>=(mid)&&c<=w){ res=new ArrayList<>(li); return true; } if(i>=n||c>w)return false; if(dp[i][(int)c]!=null)return dp[i][(int)c]; li.add(i+1); boolean left=false; if(c+arr[i]<=w)left=left|solve(arr,w,c+arr[i],i+1,li); li.remove(li.size()-1); boolean right=solve(arr,w,c,i+1,li); return dp[i][(int)c]=left|right; } private static int sizeOfSubstring(int st,int e){ int s=e-st+1; return (s*(s+1))/2; } private static int lessThen(long[] nums,long val){ int i=0,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int lessThen(List<Long> nums,long val){ int i=0,l=0,r=nums.size()-1; while(l<=r){ int mid=l+((r-l)/2); if(nums.get(mid)<=val){ i=mid; l=mid+1; }else{ r=mid-1; } } return i; } private static int greaterThen(long[] nums,long val){ int i=nums.length-1,l=0,r=nums.length-1; while(l<=r){ int mid=l+((r-l)/2); if(nums[mid]>=val){ i=mid; r=mid-1; }else{ l=mid+1; } } return i; } private static long sumOfAp(long a,long n,long d){ long val=(n*( 2*a+((n-1)*d))); return val/2; } //geometrics private static double areaOftriangle(double x1,double y1,double x2,double y2,double x3,double y3){ double[] mid_point=midOfaLine(x1,y1,x2,y2); // debug(Arrays.toString(mid_point)+" "+x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+" "+y3); double height=distanceBetweenPoints(mid_point[0],mid_point[1],x3,y3); double wight=distanceBetweenPoints(x1,y1,x2,y2); // debug(height+" "+wight); return (height*wight)/2; } private static double distanceBetweenPoints(double x1,double y1,double x2,double y2){ double x=x2-x1; double y=y2-y1; return sqrt(Math.pow(x,2)+Math.pow(y,2)); } private static double[] midOfaLine(double x1,double y1,double x2,double y2){ double[] mid=new double[2]; mid[0]=(x1+x2)/2; mid[1]=(y1+y2)/2; return mid; } private static long sumOfN(long n){ return (n*(n+1))/2; } private static long power(long x,long y,long p){ long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0){ // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } /* Function to calculate x raised to the power y in O(logn)*/ static long power(long x, long y){ long temp; if( y == 0) return 1l; temp = power(x, y / 2); if (y % 2 == 0) return (temp*temp); else return (x*temp*temp); } private static StringBuilder reverseString(String s){ StringBuilder sb=new StringBuilder(s); int l=0,r=sb.length()-1; while(l<=r){ char ch=sb.charAt(l); sb.setCharAt(l,sb.charAt(r)); sb.setCharAt(r,ch); l++; r--; } return sb; } private static void swap(long arr[],int i,int j){ long t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(List<Integer> li,int i,int j){ int t=li.get(i); li.set(i,li.get(j)); li.set(j,t); } private static void swap(int arr[],int i,int j){ int t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(double arr[],int i,int j){ double t=arr[i]; arr[i]=arr[j]; arr[j]=t; } private static void swap(float arr[],int i,int j){ float t=arr[i]; arr[i]=arr[j]; arr[j]=t; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long lcm(long a, long b){ return (a / gcd(a, b)) * b; } private static String decimalToString(int x){ return Integer.toBinaryString(x); } private static boolean isPallindrome(String s){ int l=0,r=s.length()-1; while(l<r){ if(s.charAt(l)!=s.charAt(r))return false; l++; r--; } return true; } private static StringBuilder removeLeadingZero(StringBuilder sb){ int i=0; while(i<sb.length()&&sb.charAt(i)=='0')i++; // debug("remove "+i); if(i==sb.length())return new StringBuilder(); return new StringBuilder(sb.substring(i,sb.length())); } private static StringBuilder removeEndingZero(StringBuilder sb){ int i=sb.length()-1; while(i>=0&&sb.charAt(i)=='0')i--; // debug("remove "+i); if(i<0)return new StringBuilder(); return new StringBuilder(sb.substring(0,i+1)); } private static int stringToDecimal(String binaryString){ // debug(decimalToString(n<<1)); return Integer.parseInt(binaryString,2); } private static int stringToInt(String s){ return Integer.parseInt(s); } private static String toString(int val){ return String.valueOf(val); } private static void debug(int[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(long[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(List<int[]> arr){ for(int[] a:arr){ err.println(Arrays.toString(a)); } } private static void debug(float[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(double[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void debug(boolean[][] arr){ for(int i=0;i<arr.length;i++){ err.println(Arrays.toString(arr[i])); } } private static void print(String s){ out.println(s); } private static void debug(String s){ err.println(s); } private static int charToIntS(char c){ return ((((int)(c-'0'))%48)); } private static void print(double s){ out.println(s); } private static void print(float s){ out.println(s); } private static void print(long s){ out.println(s); } private static void print(int s){ out.println(s); } private static void debug(double s){ err.println(s); } private static void debug(float s){ err.println(s); } private static void debug(long s){ err.println(s); } private static void debug(int s){ err.println(s); } private static boolean isPrime(int n){ // Check if number is less than // equal to 1 if (n <= 1) return false; // Check if number is 2 else if (n == 2) return true; // Check if n is a multiple of 2 else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } //read graph private static List<List<Integer>> readUndirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readUndirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int[][] intervals,int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<intervals.length;i++){ int x=intervals[i][0]; int y=intervals[i][1]; graph.get(x).add(y); // graph.get(y).add(x); } return graph; } private static List<List<Integer>> readDirectedGraph(int n){ List<List<Integer>> graph=new ArrayList<>(); for(int i=0;i<=n;i++){ graph.add(new ArrayList<>()); } for(int i=0;i<n;i++){ int x=sc.nextInt(); int y=sc.nextInt(); graph.get(x).add(y); // graph.get(y).add(x); } return graph; } static String[] readStringArray(int n){ String[] arr=new String[n]; for(int i=0;i<n;i++){ arr[i]=sc.next(); } return arr; } private static Map<Character,Integer> freq(String s){ Map<Character,Integer> map=new HashMap<>(); for(char c:s.toCharArray()){ map.put(c,map.getOrDefault(c,0)+1); } return map; } static boolean[] sieveOfEratosthenes(long n){ boolean prime[] = new boolean[(int)n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true){ // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } static int[] sieveOfEratosthenesInt(long n){ boolean prime[] = new boolean[(int)n + 1]; Set<Integer> li=new HashSet<>(); for (int i = 1; i <= n; i++){ prime[i] = true; li.add(i); } for (int p = 2; p * p <= n; p++) { if (prime[p] == true){ for (int i = p * p; i <= n; i += p){ li.remove(i); prime[i] = false; } } } int[] arr=new int[li.size()+1]; int i=0; for(int x:li){ arr[i++]=x; } return arr; } public static long Kadens(List<Long> prices) { long sofar=0; long max_v=0; for(int i=0;i<prices.size();i++){ sofar+=prices.get(i); if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } public static int Kadens(int[] prices) { int sofar=0; int max_v=0; for(int i=0;i<prices.length;i++){ sofar+=prices[i]; if (sofar<0) { sofar=0; } max_v=Math.max(max_v,sofar); } return max_v; } static boolean isMemberAC(int a, int d, int x){ // If difference is 0, then x must // be same as a. if (d == 0) return (x == a); // Else difference between x and a // must be divisible by d. return ((x - a) % d == 0 && (x - a) / d >= 0); } private static void sort(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(int[] arr){ int n=arr.length; List<Integer> li=new ArrayList<>(); for(int x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(double[] arr){ int n=arr.length; List<Double> li=new ArrayList<>(); for(double x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sortReverse(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li,Collections.reverseOrder()); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static void sort(long[] arr){ int n=arr.length; List<Long> li=new ArrayList<>(); for(long x:arr){ li.add(x); } Collections.sort(li); for (int i=0;i<n;i++) { arr[i]=li.get(i); } } private static long sum(int[] arr){ long sum=0; for(int x:arr){ sum+=x; } return sum; } private static long evenSumFibo(long n){ long l1=0,l2=2; long sum=0; while (l2<n) { long l3=(4*l2)+l1; sum+=l2; if(l3>n)break; l1=l2; l2=l3; } return sum; } private static void initializeIO(){ try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.setErr(new PrintStream(new FileOutputStream("error.txt"))); } catch (Exception e) { // System.err.println(e.getMessage()); } } private static int maxOfArray(int[] arr){ int max=Integer.MIN_VALUE; for(int x:arr){ max=Math.max(max,x); } return max; } private static long maxOfArray(long[] arr){ long max=Long.MIN_VALUE; for(long x:arr){ max=Math.max(max,x); } return max; } private static int[][] readIntIntervals(int n,int m){ int[][] arr=new int[n][m]; for(int j=0;j<n;j++){ for(int i=0;i<m;i++){ arr[j][i]=sc.nextInt(); } } return arr; } private static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } private static int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } private static int[] readIntArray(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } return arr; } private static double[] readDoubleArray(int n){ double[] arr=new double[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextDouble(); } return arr; } private static long[] readLongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextLong(); } return arr; } private static void print(int[] arr){ out.println(Arrays.toString(arr)); } private static void print(long[] arr){ out.println(Arrays.toString(arr)); } private static void print(String[] arr){ out.println(Arrays.toString(arr)); } private static void print(double[] arr){ out.println(Arrays.toString(arr)); } private static void debug(String[] arr){ err.println(Arrays.toString(arr)); } private static void debug(Boolean[][] arr){ for(int i=0;i<arr.length;i++)err.println(Arrays.toString(arr[i])); } private static void debug(int[] arr){ err.println(Arrays.toString(arr)); } private static void debug(long[] arr){ err.println(Arrays.toString(arr)); } static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } String next(){ while(st==null || !st.hasMoreTokens()){ try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt(){ return Integer.parseInt(next()); } long nextLong(){ return Long.parseLong(next()); } double nextDouble(){ return Double.parseDouble(next()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } static class FastWriter { private final BufferedWriter bw; public FastWriter() { this.bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(Object object) throws IOException { bw.append("" + object); } public void println(Object object) throws IOException { print(object); bw.append("\n"); } public void close() throws IOException { bw.close(); } } static class Dsu { int[] parent, size; Dsu(int n) { parent = new int[n + 1]; size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; size[i] = 1; } } private int findParent(int u) { if (parent[u] == u) return u; return parent[u] = findParent(parent[u]); } private boolean union(int u, int v) { // System.out.println("uf "+u+" "+v); int pu = findParent(u); // System.out.println("uf2 "+pu+" "+v); int pv = findParent(v); // System.out.println("uf3 " + u + " " + pv); if (pu == pv) return false; if (size[pu] <= size[pv]) { parent[pu] = pv; size[pv] += size[pu]; } else { parent[pv] = pu; size[pu] += size[pv]; } return true; } } }
Java
["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
702f7ccc13b0b4f6e59f5891946fe5d1
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 Pleasant_Pairs { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int arr[] = new 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 = arr[i]-i;j<=n;j+= arr[i]) if(j>1) if(i<j&&((long)arr[i]*arr[j])==(i+j)) count++; } sb.append(count+"\n"); } System.out.println(sb); } }
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
3ec7763338940f471e0f6965cd28ccd0
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 Pattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int a[] = new int[n+1]; for(int i =1;i<=n;i++) a[i] = scan.nextInt(); int count =0; for(long i =1;i<=n;i++) { for(long j = a[(int)i]-i;j<=n;j+=a[(int)i]) { if(j>0) if(j>i&&1l*a[(int)i]*a[(int)j]==0l+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
81c5889509bfb445b74782fae425cb08
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 cp { 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 { try{ //Your Solve Reader s = new Reader(); int t = s.nextInt(); for(int p = 0;p < t;p++){ int n = s.nextInt(); int a[] = new int[n+1]; for(int i = 1;i < n+1;i++) { a[i] = s.nextInt(); } int count = 0; for(int i = 1;i < n;i++) { for(int sum = 2*i+1 + (((2*i+1)%a[i] == 0)?0:a[i]-((2*i+1)%a[i]));sum-i < n+1;sum += a[i]) { // System.out.println(i + " " + (sum-i)); if(a[sum-i] == sum/a[i] && sum % a[sum-i] == 0) { count++; } } } System.out.println(count); } }catch(Exception e){ return; } } }
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
d77c5d6fef882f55f4a1dc5796509a53
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 cp { 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 { try{ //Your Solve Reader s = new Reader(); int t = s.nextInt(); for(int p = 0;p < t;p++){ int n = s.nextInt(); int a[] = new int[n+1]; for(int i = 1;i < n+1;i++) { a[i] = s.nextInt(); } int count = 0; for(int i = 1;i < n;i++) { if(a[i] == 1) { for(int j = i+1;j < n+1;j++) { if(a[j] == i+j) { count++; } } }else { for(int sum = 2*i+1 + (((2*i+1)%a[i] == 0)?0:a[i]-((2*i+1)%a[i]));sum-i < n+1;sum += a[i]) { // System.out.println(i + " " + (sum-i)); if(a[sum-i] == sum/a[i] && sum % a[sum-i] == 0) { count++; } } } } System.out.println(count); } }catch(Exception e){ return; } } }
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
360879d5d4a28f56b85b84edfef60b79
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.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; public class Main { private static void run() throws IOException { int n = in.nextInt(); Node[] a = new Node[n]; for (int i = 0; i < n; i++) { a[i] = new Node(i + 1, in.nextInt()); } Arrays.sort(a, Comparator.comparingLong(o -> o.value)); long ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { long now = a[i].value * a[j].value; if (now == a[i].index + a[j].index) { ans++; } if (now > 2L * n) { break; } } } out.println(ans); } static class Node { long index, value; public Node(long index, long value) { this.value = value; this.index = index; } } public static void main(String[] args) throws IOException { in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); int t = in.nextInt(); for (int i = 0; i < t; i++) { run(); } out.flush(); in.close(); out.close(); } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } static final long mod = 1000000007; static long pow_mod(long a, long b) { long result = 1; while (b != 0) { if ((b & 1) != 0) result = (result * a) % mod; a = (a * a) % mod; b >>= 1; } return result; } private static long multiplied_mod(long... longs) { long ans = 1; for (long now : longs) { ans = (ans * now) % mod; } return ans; } @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static int[] read_int_array(int len) throws IOException { int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = in.nextInt(); } return a; } private static long[] read_long_array(int len) throws IOException { long[] a = new long[len]; for (int i = 0; i < len; i++) { a[i] = in.nextLong(); } return a; } private static void print_array(int[] array) { for (int now : array) { out.print(now); out.print(' '); } out.println(); } private static void print_array(long[] array) { for (long now : array) { out.print(now); out.print(' '); } out.println(); } static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { final byte[] buf = new byte[1024]; // 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 nextSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.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
19bc02a37810deeeeb738f621d547c59
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 long nPairs(int arr[],int n,HashMap<Integer,Integer> hmap){ long count=0; int max=2*n; for(int i=0;i<n;i++){ for(int j=1;j*arr[i]<=max;j++){ if(!hmap.containsKey(j)) continue; if(hmap.get(j)>i+1 && j*arr[i]==hmap.get(j)+hmap.get(arr[i])) count++; } } return count; } public static void main (String[] args) throws java.lang.Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t-->0){ int n=Integer.parseInt(br.readLine()); StringTokenizer st=new StringTokenizer(br.readLine()); int arr[]=new int[n]; HashMap<Integer,Integer> hmap=new HashMap<Integer,Integer>(); for(int i=0;i<n;i++){ arr[i]=Integer.parseInt(st.nextToken()); hmap.put(arr[i],i+1); //System.out.print(arr[i]+" "); } //System.out.println(""); System.out.println(nPairs(arr,n,hmap)); } } }
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
242ab23ca6e4832206a0793bb2726cc6
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 whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-- > 0){ int n = scn.nextInt(); int arr[] = new int[n+1]; for(int i=1; i<=n; i++){ arr[i] = scn.nextInt(); } int count = 0; for(int i=1; i<=n; i++){ /* arr[i] * arr[j] = i + j arr[i] * [1, 2, 3....] = i + ([1, 2, 3...] * arr[i] - i) Example: at i = 1, there is 3. so we will check at j = 2, arr[j] == 1 as (3 * 1 = i + j), then check at j = 5, arr[j] == 2 as (3 * 2 = i + j), above statement implies that for a particular index->i, we have to search for those indexes->j, where the sum of (i+j) is a multiple of value at 'i' Example: 3 1 5 9 2 for i=1, arr[i]=3 A. 3 * arr[j] = 1 + (3-1), here j becomes 2 => 3 * 1 = 1 + 2 B. 3 * arr[j] = 1` + (2*3 - 1), here j becomes 5 => 3 * 2 = 1 + 5 for i=2, arr[i]=1 A. 1 * arr[j] = 2 + (1-2), here j becomes 1 => 1 * ? != 2 - 1 B. 1 * arr[j] = 2 + (2*1 - 2), here, j becomes 0 => 1 * ? != 2 +- 0 WON'T COUNT TILL J>I C. 1 * arr[j] = 2 + (4*1 - 2), here, j becomes 5 => 1 * 5? != 2 + 3 */ int si = arr[i] - i; int val = 1; while(si<=n){ // second idx should be greater than first idx if(i < si && val == arr[si]){ count++; } si += arr[i]; val++; } } 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
ab5fa20face971e5b205a2528a3c8c6b
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 whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { public static void main (String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-- > 0){ int n = scn.nextInt(); int arr[] = new int[n+1]; for(int i=1; i<=n; i++){ arr[i] = scn.nextInt(); } int count = 0; for(int i=1; i<=n; i++){ int num = arr[i]; int si = num - i; int val = 1; while(si<=n){ if(i < si && val == arr[si]){ count++; } si += arr[i]; val++; } } 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
51a5c38207b1f6c86d40a74be8a07919
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 s = new Scanner(System.in); int t = s.nextInt(); while(t-- > 0) { int n = s.nextInt(); int[] a = new int[n + 1]; for(int i = 1; i <= n; i++) a[i] = s.nextInt(); long res = 0; for(int i = 1; i < n; i++) { int k = 1; while(k * a[i] - i <= n) { if(k * a[i] - i > i && a[k * a[i] - i] == k) 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
5efd8e585920f3d3a75369e413b771a7
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.math.BigInteger; import java.util.*; import static java.lang.System.out; public class Solution { static MyScanner str = new MyScanner(); // static Scanner str = new Scanner(System.in); // static Set<String> set; int mod = (int)1e9 + 7; public static void main(String[] args) throws IOException { long T = l(); while (T-- > 0) { solve(); } } static void solve() throws IOException { int n = i(); int a[] = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = i(); } 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] == i + j && i < j) { ans++; } } } } out.println(ans); } public static int i() throws IOException { return str.nextInt(); } public static long l() throws IOException { return str.nextLong(); } public static double d() throws IOException { return str.nextDouble(); } 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
6adcc7d728ba49a48b74dd126a67c1fd
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) 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
2b0657d649fc7ef831398d591416478d
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
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class B1541 { public static void main(String[] args) { FastReader fs = new FastReader(); int t = fs.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = fs.nextInt(); int[] arr = fs.readArray(n); Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; ++i) { map.put(arr[i], i + 1); } int min = 3; int max = 2 * n; int count = 0; for (int i = min; i <= max; i++) { int c = getCount(i, map); count += c; } sb.append(count).append("\n"); } System.out.println(sb); } private static int getCount(int min, Map<Integer, Integer> map) { int c = 0; for (int i = 1; i * i <= min; ++i) { if (min % i == 0) { int second = min / i; if (map.containsKey(i) && map.containsKey(second)) { int i1 = map.get(i); int i2 = map.get(second); if (i1 != i2 && i1 + i2 == min) { c++; } } } } return c; } private static boolean isPrime(long num) { if (num == 2L) { return true; } for (long i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } private static long getGcd(long a, long b) { if (b == 0) return a; return getGcd(b, a % b); } private static long binaryExponentiation(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) == 1) res = res * a; a = a * a; b >>= 1; } return res; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(next()); return arr; } } }
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
2ffeee0302f0344d0a2efa989e0671f6
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
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class PleasantPairs { public static void main(String[] args) { FastReader fs = new FastReader(); int t = fs.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = fs.nextInt(); long count = 0; HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) map.put(fs.nextInt(), i + 1); for (int i = 1; i <= 2 * n; i++) { for (int j = i + 1; j <= 2 * n; j++) { if ((long) i * j >= 2L * n) break; if (map.containsKey(i) && map.containsKey(j)) { if (((long) i * j) == (map.get(i) + map.get(j))) count++; } } } sb.append(count).append("\n"); } System.out.println(sb); } private static int getGcd(int a, int b) { if (b == 0) return a; return getGcd(b, a % 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; } int[] readArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(next()); return arr; } } }
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
198387ea007c66bc33b441ea99e9bd22
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.lang.*; import java.io.FileReader; import java.io.IOException; import java.math.BigInteger; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.util.*; import java.util.LinkedList; public class Pleasent_Pair { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static void sort(long[] arr) { ArrayList<Long> a = new ArrayList<>(); for (long i : arr) { a.add(i); } Collections.sort(a); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } } static int get(long[] arr, int tar){ int left = 1; int right = tar-1; int ans = 0; // System.out.println(tar); while(left < right){ if(left-1 >= 0 &&left -1 < arr.length && right-1 >=0 && right-1< arr.length) if(arr[left-1]*arr[right-1]== tar)ans++; left++; right--; } return ans; } public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] arr = new int[n+1]; for (int i = 1; i <= n; i++) { arr[i] = sc.nextInt(); } long ans = 0; for(int i = 1; i <n; i++){ for(int j = arr[i]-i; j <=n; j+=arr[i]){ if(j>=0) if((long)arr[i]*arr[j] ==(long)i+j && i < 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
4927d083864f099f8b420b44b7f243a9
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) 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
709a17b7df08f5a1e1bbffb66b15447c
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 A { static class Pair { long x; long y; public Pair(long x, long y) { this.x = x; this.y = y; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder str = new StringBuilder(); int t = sc.nextInt(); for (int xx = 0; xx < t; xx++) { int n = sc.nextInt(); Pair[] arr = new Pair[n]; for(int i=0;i<n;i++) { arr[i] = new Pair(sc.nextLong(),i+1); } Arrays.sort(arr ,(p1,p2)-> { return (int)p1.x - (int)p2.x; }); int c =0; long l =1; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { l = arr[i].x*arr[j].x; if(l==arr[i].y+arr[j].y) c++; if(l>2*n) break; } } str.append(c+"\n"); } System.out.print(str); 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
5b95648fc0ad92df69b2d9072fc7b3b4
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 { //----------- StringBuilder for faster output------------------------------ static StringBuilder out = new StringBuilder(); public static void main(String[] args) { FastScanner fs=new FastScanner(); /****** CODE STARTS HERE *****/ int t = fs.nextInt(); while(t-->0) { int n = fs.nextInt(); int[] a = new int [2*n+1]; Arrays.fill(a, -1); for(int i=1; i<=n; i++)a[fs.nextInt()]=i; int cnt = 0; for(int i=3; i<2*n; i++) { for(int j=1; j*j<i; j++) { if(i%j==0 && a[j]!=-1 && a[i/j]!= -1 && i==a[j]+a[i/j])cnt++; } } out.append(cnt+"\n"); } System.out.print(out); } 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); } //----------- FastScanner class for faster input--------------------------- 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
6ec60db79c13ed9b7ab83f9a4c4209df
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 { //----------- StringBuilder for faster output------------------------------ static StringBuilder out = new StringBuilder(); public static void main(String[] args) { FastScanner fs=new FastScanner(); /****** CODE STARTS HERE *****/ int t = fs.nextInt(); while(t-->0) { int n = fs.nextInt(); int[] a = new int [2*n+1]; Arrays.fill(a, -1); for(int i=1; i<=n; i++)a[fs.nextInt()]=i; int cnt = 0; for(int i=3; i<2*n; i++) { for(int j=1; j*j<=i; j++) { if(i%j==0 && j*j!=i && a[j]!=-1 && a[i/j]!= -1 && i==a[j]+a[i/j])cnt++; } } out.append(cnt+"\n"); } System.out.print(out); } 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); } //----------- FastScanner class for faster input--------------------------- 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
b696961e015df0bb4e197b9ea7ebfbfd
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 { //----------- StringBuilder for faster output------------------------------ static StringBuilder out = new StringBuilder(); public static void main(String[] args) { FastScanner fs=new FastScanner(); /****** CODE STARTS HERE *****/ int t = fs.nextInt(); while(t-->0) { int n = fs.nextInt(); int[] a = fs.readArray(n); int cnt = 0; for(int i=1; i<=n; i++) { for(int j=a[i-1]-i; j<=n; j+=a[i-1]) { if(j>0 && i<j && (long)a[i-1]*a[j-1]==(i+j))cnt++; } } out.append(cnt+"\n"); } System.out.print(out); } 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); } //----------- FastScanner class for faster input--------------------------- 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
eeea6ef0dee85f4c07dbe4f755ee14d9
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) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); long[] a = new long[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 = (int) a[i] - i; j <= n; j += a[i]) { if (j > 0) { if (a[i] * a[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
f22cccdeb7bd025883997bd0906f9767
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.StringTokenizer; public class B{ public static void main(String[] args){ FastReader sc = new FastReader(); 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(); } int ans=0; for(int i=1;i<=n;i++) { for(int j=a[i]-i;j<=n;j+=a[i]) { if(j>0 &&(long)a[i]*a[j]==(long)i+j && i<j)ans++; } } System.out.println(ans); } } static class ind { int x;int y; ind(int x,int y){ this.x=x; this.y=y; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
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
2b068635d8bcb635095567910f51252e
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 Pleasant_Pairs { public static void main(String[] args) throws java.lang.Exception { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(input.readLine()); for(int i=0;i<t;i++){ int n = Integer.parseInt(input.readLine()); String[] s = input.readLine().split(" "); Long[][] nums = new Long[n][2]; long count = 0; for(int j=0;j<n;j++){ nums[j][0] = Long.parseLong(s[j]); nums[j][1] = (long)(j+1); } sortbyColumn(nums, 0); for(int k=0;k<n;k++){ for(int l=k+1;l<n;l++){ long val = nums[k][0]*nums[l][0]; if(val>2*n){ break; } if(val==nums[k][1]+nums[l][1]){ count++; } } } System.out.println(count); } } public static void sortbyColumn(Long arr[][], int col) { Arrays.sort(arr, new Comparator<Long[]>() { @Override public int compare(final Long[] entry1, final Long[] entry2) { if (entry1[col] > entry2[col]) return 1; else 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
d181ab1087f7e936d1484c7a5f9436b3
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.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.TreeSet; public class Main { static boolean hasMultipleTestCases = true; static final String newLine = "\n"; public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int testCases = 1; if (hasMultipleTestCases) { testCases = in.nextInt(); } while (testCases-- > 0) { int n = in.nextInt(); int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = in.nextInt(); int count = 0; for (int i = 1; i <= n; i++) { for (int j = a[i] - i; j <= n; j += a[i]) { if (j > i) { if ((long)i + j == (long)a[i] * a[j]) count++; } } } sb.append(count).append(newLine); } out.print(sb); out.flush(); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } }
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
ba76d0153cf0c3b7987c698fd570fbf5
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.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.TreeSet; public class Main { static boolean hasMultipleTestCases = true; static final String newLine = "\n"; public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); int testCases = 1; if (hasMultipleTestCases) { testCases = in.nextInt(); } while (testCases-- > 0) { int n = in.nextInt(); int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = in.nextInt(); int count = 0; // for (int i = 1; i <= n; i++) { // for (int j = a[i] - i; j <= n; j += a[i]) { // if (j >= 0) { // if (i + j == a[i] * a[j] && i < j) count++; // } // } // } 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) { count++; } } } } sb.append(count).append(newLine); } out.print(sb); out.flush(); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { boolean isSpaceChar(int ch); } } }
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
35b427b5c04cc5786f6024320ff2fd38
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 codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); for(int q=0;q<t;q++){ int n = Integer.parseInt(br.readLine()); int a[] = new int[n]; String s[] = br.readLine().split(" "); for(int i=0;i<n;i++){ a[i] = Integer.parseInt(s[i]); } int count=0; for(int i=0;i<n;i++){ for(int j=1;j<2*n+1;j++){ int tmp = a[i]*j -i-1; // System.out.println(tmp+" "+i); if(tmp>n){ break; } else if(tmp<=0){ continue; } else{ if(a[tmp-1]==j && i>=tmp){ 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
c779768ca94e34f695b17b495dee2c96
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
/* ⠀⠀⠀⠀⣠⣶⡾⠏⠉⠙⠳⢦⡀⠀⠀⠀⢠⠞⠉⠙⠲⡀⠀ ⠀⠀⠀⣴⠿⠏⠀⠀⠀⠀⠀⠀⢳⡀⠀⡏⠀⠀Y⠀⠀⢷ ⠀⠀⢠⣟⣋⡀⢀⣀⣀⡀⠀⣀⡀⣧⠀⢸⠀⠀A⠀⠀ ⡇ ⠀⠀⢸⣯⡭⠁⠸⣛⣟⠆⡴⣻⡲⣿⠀⣸⠀⠀S⠀ ⡇ ⠀⠀⣟⣿⡭⠀⠀⠀⠀⠀⢱⠀⠀⣿⠀⢹⠀⠀H⠀⠀ ⡇ ⠀⠀⠙⢿⣯⠄⠀⠀⠀⢀⡀⠀⠀⡿⠀⠀⡇⠀⠀⠀⠀⡼ ⠀⠀⠀⠀⠹⣶⠆⠀⠀⠀⠀⠀⡴⠃⠀⠀⠘⠤⣄⣠⠞⠀ ⠀⠀⠀⠀⠀⢸⣷⡦⢤⡤⢤⣞⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⢀⣤⣴⣿⣏⠁⠀⠀⠸⣏⢯⣷⣖⣦⡀⠀⠀⠀⠀⠀⠀ ⢀⣾⣽⣿⣿⣿⣿⠛⢲⣶⣾⢉⡷⣿⣿⠵⣿⠀⠀⠀⠀⠀⠀ ⣼⣿⠍⠉⣿⡭⠉⠙⢺⣇⣼⡏⠀⠀⠀⣄⢸⠀⠀⠀⠀⠀⠀ ⣿⣿⣧⣀⣿………⣀⣰⣏⣘⣆⣀⠀⠀ */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; // System.out is a PrintStream // import java.util.Arrays; // import java.util.ArrayDeque; // import java.util.ArrayList; // import java.util.Collections; // for sorting ArrayList mainly // import java.util.HashMap; // import java.util.TreeMap; // import java.util.HashSet; import java.util.TreeSet; // import java.util.Random; import java.util.StringTokenizer; public class B1541 { public static void main(String[] args) throws IOException { FastScanner scn = new FastScanner(); PrintWriter out = new PrintWriter(System.out); for (int tc = scn.nextInt(); tc > 0; tc--) { int N = scn.nextInt(), count = 0, sz = N * 2; int[] arr = new int[N]; for (int i = 0; i < N; i++) { arr[i] = scn.nextInt(); } for (int i = 0; i < N; i++) { long val = arr[i]; for (int j = 1; j * val <= sz; j++) { int idx = (int)(val * j - (i + 1)); if (i + 1 < idx && idx <= N && arr[idx - 1] == j) { count++; } } } out.println(count); } out.close(); } /* ------------------- Sorting ------------------- */ private static void ruffleSort(int[] arr) { // int N = arr.length; // Random rand = new Random(); // for (int i = 0; i < N; i++) { // int oi = rand.nextInt(N), temp = arr[i]; // arr[i] = arr[oi]; // arr[oi] = temp; // } // Arrays.sort(arr); } /* ------------------- Pair class ------------------- */ private static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { return this.x == o.x ? this.y - o.y : this.x - o.x; } } /* ------------------- HCF and LCM ------------------- */ private static int gcd(int num1, int num2) { int temp = 0; while (num2 != 0) { temp = num1; num1 = num2; num2 = temp % num2; } return num1; } private static int lcm(int num1, int num2) { return (int)((1L * num1 * num2) / gcd(num1, num2)); } /* ------------------- primes and prime factorization ------------------- */ private static boolean[] seive(int N) { // true means not prime, false means is a prime number :) boolean[] notPrimes = new boolean[N + 1]; notPrimes[0] = notPrimes[1] = true; for (int i = 2; i * i <= N; i++) { if (notPrimes[i]) continue; for (int j = i * i; j <= N; j += i) { notPrimes[j] = true; } } return notPrimes; } /* private static TreeMap<Integer, Integer> primeFactors(long N) { TreeMap<Integer, Integer> primeFact = new TreeMap<>(); for (int i = 2; i <= Math.sqrt(N); i++) { int count = 0; while (N % i == 0) { N /= i; count++; } if (count != 0) { primeFact.put(i, count); } } if (N != 1) { primeFact.put((int)N, 1); } return primeFact; } */ /* ------------------- Binary Search ------------------- */ private static long factorial(int N) { long ans = 1L; for (int i = 2; i <= N; i++) { ans *= i; } return ans; } private static long[] factorialDP(int N) { long[] factDP = new long[N + 1]; factDP[0] = factDP[1] = 1; for (int i = 2; i <= N; i++) { factDP[i] = factDP[i - 1] * i; } return factDP; } private static long factorialMod(int N, int mod) { long ans = 1L; for (int i = 2; i <= N; i++) { ans = ((ans % mod) * (i % mod)) % mod; } return ans; } /* ------------------- Binary Search ------------------- */ private static int lowerbound(int[] arr, int st, int ed, int tar) { int ans = -1; while (st <= ed) { int mid = ((ed - st) >> 1) + st; if (arr[mid] <= tar) { ans = mid; st = mid + 1; } else { ed = mid - 1; } } return ans; } private static int upperbound(int[] arr, int st, int ed, int tar) { int ans = -1; while (st <= ed) { int mid = ((ed - st) >> 1) + st; if (arr[mid] < tar) { st = mid + 1; } else { ans = mid; ed = mid - 1; } } return ans; } /* ------------------- Power Function ------------------- */ public static long pow(long x, long y) { long res = 1; while (y > 0) { if ((y & 1) != 0) { res = (res * x); y--; } x = (x * x); y >>= 1; } return res; } public static long powMod(long x, long y, int mod) { long res = 1; while (y > 0) { if ((y & 1) != 0) { res = (res * x) % mod; y--; } x = (x * x) % mod; y >>= 1; } return res % mod; } /* ------------------- Disjoint Set(Union and Find) ------------------- */ private static class DSU { public int[] parent, size; DSU(int n) { parent = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } public int find(int a) { if (parent[a] == a) { return a; } return parent[a] = find(parent[a]); } public void merge(int a, int b) { int parA = find(a), parB = find(b); if (parA == parB) return; if (size[parA] < size[parB]) { parent[parB] = parA; size[parA] += size[parB]; } else { parent[parA] = parB; size[parB] += size[parA]; } } } /* ------------------- Scanner class for input ------------------- */ private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { this.br = new BufferedReader(new InputStreamReader(System.in)); this.st = new StringTokenizer(""); } public String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException err) { err.printStackTrace(); } } return st.nextToken(); } public String nextLine() { if (st.hasMoreTokens()) { return st.nextToken("").trim(); } try { return br.readLine().trim(); } catch (IOException err) { err.printStackTrace(); } return ""; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
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
8dff26219b52df4b1c0552827e614d45
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.IOException; import java.io.PrintWriter; import java.util.*; public class pairs{ static class pair{ long ele; long pos; pair(long x,long y){ ele=x; pos=y; } long getEle(){ return ele; } } // static class ELEComparator implements Comparator{ // public int compare(Object o1,Object o2){ // pair s1=(pair) o1; // pair s2=(pair)o2; // if(s1.ele>s2.ele) // return 1; // else // return -1; // } // } public static void main(final String[] args) throws IOException { long t=console.nextLong(); while(t-->0){ int n =console.nextInt(); long item; long count=0; pair[] B=new pair[n]; for(int i=0;i<n;i++){ item=console.nextLong(); B[i]=new pair(item,i+1); } Comparator<pair> cm1=Comparator.comparing(pair::getEle); Arrays.sort(B,cm1); for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(B[i].ele *B[j].ele >= 2* n) break; if(B[i].ele*B[j].ele==(B[i].pos+B[j].pos)) count++; } } System.out.println(count); } } private static final pairs.InputReader console = new pairs.InputReader(); static final class InputReader { private final int BUFFER_SIZE = 1 << 24; private final DataInputStream in = new DataInputStream(System.in); private final byte[] buffer = new byte[BUFFER_SIZE]; private final byte[] buf = new byte[BUFFER_SIZE]; private int bufferPointer = 0, bytesRead = 0; private void fillBuffer() throws IOException { bytesRead = in.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 String next() throws IOException { int cnt = 0, c; while ((c = read()) != -1) { if (c < 33) { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String nextLine() throws IOException { int cnt = 0, c; while ((c = read()) != -1) { if (c == '\r' || c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { return (int) nextLong(); } public long nextLong() throws IOException { long ret = 0L, c = read(); while (c < 33L) { c = read(); } boolean neg = (c == 45L); if (neg) { c = read(); } do { ret = (ret << 3) + (ret << 1) + c - 48L; } while ((c = read()) > 47 && c < 58); return (neg ? -ret : ret); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public void close() throws IOException { in.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
a846ee1317d68ad92717a6e7223c5bfa
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.IOException; import java.io.PrintWriter; import java.util.*; public class pairs{ static class pair{ long ele; long pos; pair(long x,long y){ ele=x; pos=y; } long getEle(){ return ele; } } // static class ELEComparator implements Comparator{ // public int compare(Object o1,Object o2){ // pair s1=(pair) o1; // pair s2=(pair)o2; // if(s1.ele>s2.ele) // return 1; // else // return -1; // } // } public static void main(final String[] args) throws IOException { long t=console.nextLong(); while(t-->0){ int n =console.nextInt(); long item; long count=0; pair[] B=new pair[n]; for(int i=0;i<n;i++){ item=console.nextLong(); B[i]=new pair(item,i+1); } Comparator<pair> cm1=Comparator.comparing((pair x)->x.ele); Arrays.sort(B,cm1); for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(B[i].ele *B[j].ele >= 2L * n) break; if(B[i].ele*B[j].ele==(B[i].pos+B[j].pos)) count++; } } System.out.println(count); } } private static final pairs.InputReader console = new pairs.InputReader(); static final class InputReader { private final int BUFFER_SIZE = 1 << 24; private final DataInputStream in = new DataInputStream(System.in); private final byte[] buffer = new byte[BUFFER_SIZE]; private final byte[] buf = new byte[BUFFER_SIZE]; private int bufferPointer = 0, bytesRead = 0; private void fillBuffer() throws IOException { bytesRead = in.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 String next() throws IOException { int cnt = 0, c; while ((c = read()) != -1) { if (c < 33) { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String nextLine() throws IOException { int cnt = 0, c; while ((c = read()) != -1) { if (c == '\r' || c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { return (int) nextLong(); } public long nextLong() throws IOException { long ret = 0L, c = read(); while (c < 33L) { c = read(); } boolean neg = (c == 45L); if (neg) { c = read(); } do { ret = (ret << 3) + (ret << 1) + c - 48L; } while ((c = read()) > 47 && c < 58); return (neg ? -ret : ret); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public void close() throws IOException { in.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
1a97e9d7a7e7ed68e5a092149c709099
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 { PrintWriter out = new PrintWriter(System.out); FastReader ob=new FastReader(); int T = ob.nextInt(); StringBuilder sb = new StringBuilder(); while(T-->0) { int n = ob.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=ob.nextInt(); } int[] map = new int[2*n+1]; for(int i=0; i < n; i++) map[arr[i]] = i+1; int res = 0; for(int v=1; v <= 2*n; v++) if(map[v] > 0) { //ai = v //aj = x/v for(int x=v; x <= 2*n; x+=v) if(map[x/v] > 0 && map[v]+map[x/v] == x && v != x/v) res++; } out.println(res/2); out.flush(); } } 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 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
24b9a2246656f4d742fa268d596356f9
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 { PrintWriter out = new PrintWriter(System.out); FastReader ob=new FastReader(); int T = ob.nextInt(); StringBuilder sb = new StringBuilder(); 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(); } } 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 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