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
a11ff78bac8ad80256979b564133364a
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class B_Worms_271 { static int [] list = new int [1000010]; public static void main(String[] args) { Scanner x = new Scanner(System.in); int k = x.nextInt(), temp; // ArrayList<Integer> slon = new ArrayList<Integer>(); for (int i = 0 , t = 0; i < k; i++) { temp = x.nextInt(); while (temp > 0) { temp--; list [t] = i + 1; t++; } } k = x.nextInt(); for (int j = 0; j < k; j++) { temp = x.nextInt(); System.out.println(list[temp-1]); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
261d616fedc5ba0ee23bc28a25dc2ea8
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Cf271b { public static void main(String[] args) throws IOException { InputStreamReader fin = new InputStreamReader(System.in); Scanner scr = new Scanner(fin); int n = scr.nextInt(); int [] a = new int [n]; for (int i = 0; i < n; i++) { a[i] = scr.nextInt(); } int m = scr.nextInt(); int [] q = new int [m]; for (int i = 0; i < m; i++) { q[i] = scr.nextInt(); } int max = 0; for (int i = 0; i < n; i++) { max += a[i]; } int [] s = new int [max+1]; int k = 0; int t = 0; for (int i = 1; i <= max; i++) { if (i <= a[k] + t) { s[i] = k+1; } else { t += a[k]; k++; s[i] = k+1; } } ArrayList<Integer> x = new ArrayList<>(m); for (int i = 0; i < m; i++) { x.add(s[q[i]]); } PrintWriter fout = new PrintWriter(System.out); for (int e : x) { fout.println(e); } fout.flush(); fout.close(); } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
50cb54b1abf54a03317d3f0c19f4938b
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.Scanner; public class Worms { public static void main(String args[]){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] piles = new int[n + 1]; for (int i = 1; i <= n; i++) { piles[i] = in.nextInt(); } int[] answer = new int[1000001]; int flag = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= piles[i]; j++) { answer[flag++] = i; } } int M = in.nextInt(); for (int i = 1; i <= M; i++) { int get = in.nextInt(); System.out.println(answer[get]); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
fec303a4d92e641da543234cce5eba80
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.Scanner; public class worns { public static void main(String[] args) { //http://codeforces.com/problemset/problem/474/B Scanner sc = new Scanner(System.in); int n=sc.nextInt(); //int b=input.nextInt(); int[] res = new int[1000001];//mi 10^6 int at = 0; for(int i = 0; i<n; i++) { int a = sc.nextInt(); //truco ingreso y ya genero mis pilas for(int j = 0; j<a; j++) res[at++] = i+1; } int m = sc.nextInt(); //cantidad de gusanos for(int i = 0; i<m; i++) System.out.println(res[sc.nextInt()-1]); //al final solomuestro en que pila esta ya las trngo cargadas } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
aa5bb5cb6dddf9dab3765a5e51216737
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.Scanner; public class C_474B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] ans = new int[(int)1e6+1]; int sum = 0, k = 0; for (int i = 1; i <= n; i++) { int x = sc.nextInt(); sum+=x; for (int j = k; j <= sum; j++) { ans[j] = i; } k = sum+1; } int m = sc.nextInt(); for (int i = 1; i <= m; i++) { System.out.println(ans[sc.nextInt()]); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
fa4d98e4651a8ce6e803988bf4987ec7
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
//package Codeforces.Div2B_271.Code1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /* * some cheeky quote */ public class Main { FastScanner in; PrintWriter out; private int a[] = new int[1000000 + 5]; public void solve() throws IOException { int num = 1; int piles = in.nextInt(); for (int i = 0; i < piles; i++) { int size = in.nextInt(); while (size-- > 0) { a[num++] = (i + 1); } } int test = in.nextInt(); while (test-- > 0) { int worm = in.nextInt(); System.out.println(a[worm]); } } public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() { 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()); } } public static void main(String[] arg) { new Main().run(); } } class Item { int from; int to; public Item(int from, int to) { this.from = from; this.to = to; } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
4e1b475d4005b9862ce2b6e9130409b8
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] worms = new int[1000001]; int k = 1; int lastIdx = 1; for (int i = 0; i < n; i++) { int a = in.nextInt(); for (int j = 0; j < a; j++) { worms[lastIdx + j] = k; } lastIdx += a; k++; } int m = in.nextInt(); for (int i = 0; i < m; i++) { System.out.println(worms[in.nextInt()]); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
e0abe1365a3f6088db7063c314cb8072
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int numPiles, numJuicy, label; int numWorms[] = new int[100000]; int juicyLabels[] = new int[100000]; while (s.hasNextInt()) { numPiles = s.nextInt(); numWorms[0] = s.nextInt(); for (int i = 1; i < numPiles; i++) { numWorms[i] = s.nextInt() + numWorms[i - 1]; } numJuicy = s.nextInt(); for (int i = 0; i < numJuicy; i++) { juicyLabels[i] = s.nextInt(); } for (int i = 0; i < numJuicy; i++) { label = juicyLabels[i]; System.out.println(searchPile(numWorms, 0, numPiles - 1, label) + 1); } } s.close(); } public static int searchPile(int[] numWorms, int start, int end, int val) { int midIndex = (start + end) / 2; if (val <= numWorms[midIndex]) { if (midIndex == 0 || val > numWorms[midIndex - 1]) { return midIndex; } else { return searchPile(numWorms, start, midIndex - 1, val); } } else { return searchPile(numWorms, midIndex + 1, end, val); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
52778faad9b1286fc3582f6eb7681f0b
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.Arrays; import java.util.Scanner; public class Worms { public static void main(String[] args) { Scanner console = new Scanner(System.in); int i; int n=console.nextInt(); int arr[]=new int [n]; for(i=0;i<arr.length;i++) arr[i]=console.nextInt(); int m=console.nextInt(); int arr2[]=new int [m]; for(i=0;i<arr2.length;i++) arr2[i]=console.nextInt(); for(i=1;i<arr.length;i++) arr[i]=arr[i-1]+arr[i]; Arrays.sort(arr); double a,b; int mid; boolean found; for(i=0;i<arr2.length;i++){ found=true; a=1; b=n; mid=(int)(a+b)/2; while(found){ if(arr2[i]==arr[mid-1]){ System.out.println(mid); break;} if(arr2[i]<arr[mid-1]){ b=mid; mid=(int)(a+b)/2; if(arr2[i]<=arr[mid-1]&&mid==a) found=false; } else { a=mid; mid=(int) Math.ceil((a+b)/2); if(arr2[i]<=arr[mid-1]&&mid==b) found=false;}} if(!found) System.out.println(mid);} } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
54d1e28bd7fddec77be78a51f838a542
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StreamTokenizer; /* import doge.wow.Shibeforces; β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–’β–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–€β–’β–Œβ–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–’β–’β–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–„β–€β–’β–’β–’β–β–‘β–‘β–‘ ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐░░░ β–‘β–‘β–‘β–‘β–‘β–„β–„β–€β–’β–‘β–’β–’β–’β–’β–’β–’β–’β–’β–’β–ˆβ–’β–’β–„β–ˆβ–’β–β–‘β–‘β–‘ β–‘β–‘β–‘β–„β–€β–’β–’β–’β–‘β–‘β–‘β–’β–’β–’β–‘β–‘β–‘β–’β–’β–’β–€β–ˆβ–ˆβ–€β–’β–Œβ–‘β–‘β–‘ β–‘β–‘β–β–’β–’β–’β–„β–„β–’β–’β–’β–’β–‘β–‘β–‘β–’β–’β–’β–’β–’β–’β–’β–€β–„β–’β–’β–Œβ–‘β–‘ β–‘β–‘β–Œβ–‘β–‘β–Œβ–ˆβ–€β–’β–’β–’β–’β–’β–„β–€β–ˆβ–„β–’β–’β–’β–’β–’β–’β–’β–ˆβ–’β–β–‘β–‘ β–‘β–β–‘β–‘β–‘β–’β–’β–’β–’β–’β–’β–’β–’β–Œβ–ˆβ–ˆβ–€β–’β–’β–‘β–‘β–‘β–’β–’β–’β–€β–„β–Œβ–‘ β–‘β–Œβ–‘β–’β–„β–ˆβ–ˆβ–„β–’β–’β–’β–’β–’β–’β–’β–’β–’β–‘β–‘β–‘β–‘β–‘β–‘β–’β–’β–’β–’β–Œβ–‘ β–€β–’β–€β–β–„β–ˆβ–„β–ˆβ–Œβ–„β–‘β–€β–’β–’β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–’β–’β–’β–β–‘ β–β–’β–’β–β–€β–β–€β–’β–‘β–„β–„β–’β–„β–’β–’β–’β–’β–’β–’β–‘β–’β–‘β–’β–‘β–’β–’β–’β–’β–Œ ▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐░ β–‘β–Œβ–’β–’β–’β–’β–’β–’β–€β–€β–€β–’β–’β–’β–’β–’β–’β–‘β–’β–‘β–’β–‘β–’β–‘β–’β–’β–’β–Œβ–‘ ░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐░░ β–‘β–‘β–€β–„β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–‘β–’β–‘β–’β–‘β–’β–„β–’β–’β–’β–’β–Œβ–‘β–‘ β–‘β–‘β–‘β–‘β–€β–„β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–„β–„β–„β–€β–’β–’β–’β–’β–„β–€β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–€β–„β–„β–„β–„β–„β–„β–€β–€β–€β–’β–’β–’β–’β–’β–„β–„β–€β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–€β–€β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ wow much code violence such danger hw dis hapen?!! wy meh nu improv??? :[ */ public class Main { static StreamTokenizer st; private static int nextInt() throws IOException{ st.nextToken(); return (int) st.nval; } public static void main(String[] args) throws IOException{ st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int pile = nextInt(); //pile of wow int a[] = new int[1000001]; int index = 0; for(int i = 0;i < pile;i++){ int temp = nextInt(); for(int j = 0; j < temp;j++) a[++index] = i + 1; } int m = nextInt(); //juicy worms, wow for(int i = 0;i < m;i++) out.write(a[nextInt()] + "\n"); out.close(); } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
331386526ee7c8805f87438339e53c23
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; import java.util.Arrays; /** * Built using CHelper plug-in * Actual solution is at the top */ public class MainB { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public class Node implements Comparable{ public int index; public int difficulty; public Node(int a, int b) { this.index = a; this.difficulty = b; } public int compareTo(Object n) { return this.difficulty - ((Node) n).difficulty; } } public int search(int[] start, int[] end, int target) { int lo = 0; int hi = end.length; while ( lo <= hi) { int mid = (hi + lo) / 2; if (start[mid] <= target && target <= end[mid]) return mid; else if (target < start[mid]) hi = mid-1; else lo = mid+1; } return -1; } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; int[] start = new int[n]; int[] end = new int[n]; start[0] = 1; for (int i = 0; i < n; i++) { if (i == 0) end[0] = in.nextInt(); else { start[i] = end[i-1] + 1; end[i] = start[i] + in.nextInt() - 1; } } /* out.write(Arrays.toString(start) + "\n"); out.write(Arrays.toString(end) + "\n"); */ int m = in.nextInt(); // num juicy worms int[] q = new int[m]; for (int i = 0; i < m; i++) q[i] = in.nextInt(); for (int i = 0; i < m; i++) { out.write((search(start, end, q[i]) + 1) + "\n"); } } } 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
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
ea455c311f0da94da377dd6f54437077
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.*; public class CS474B { static class Pair{ public int i; public int j; } public static int binarySearch(Pair[] arr, int size, int target) { int lo = 0; int hi = size-1; int mid = 0; while (lo <= hi) { mid = lo + (hi-lo) / 2; if (arr[mid].i <= target && arr[mid].j >= target) { return mid+1; } else if (arr[mid].j < target) { lo = mid + 1; } else if (arr[mid].i > target) { hi = mid - 1; } } return -1; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int m = in.nextInt(); int[] target = new int[m]; for (int i = 0; i < m; i++) { target[i] = in.nextInt(); } Pair[] req = new Pair[n]; int l = 0; int h = 0; for (int i = 0; i < n; i++) { req[i] = new Pair(); if(i != 0) l = req[i-1].j; else l = 0; h = l + arr[i]; req[i].i = l+1; req[i].j = h; } for (int i = 0; i < m; i++) { System.out.println(binarySearch(req, n, target[i])); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
4c7187acf005d77bc537e8196c653051
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] plates = new int[n + 1]; int[] d = new int[3000000]; int sum = 0; int k = 1; for (int i = 1; i <= n; i++) { plates[i] = sc.nextInt(); sum += plates[i]; for (int j = k; j <= sum; j++) { d[j] = i; } k = sum + 1; } int m = sc.nextInt(); for (int j = 1; j <= m; j++) { System.out.println(d[sc.nextInt()]); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
e042be9c64ed1a4fbd152debf7f10949
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
//package CodeforcesRoundN271; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { private static BufferedReader in; private static StringTokenizer st; public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); in = new BufferedReader(isr); st = new StringTokenizer(""); int n = nextInt(); int []a=new int[n+1]; int []c=new int[3000000]; int x=1, y=0; for (int i=1; i<=n; i++) { a[i]= nextInt(); y+=a[i]; for (int j=x; j<=y; j++) { c[j]=i; } x=y+1; } int m= nextInt(); for (int i=1; i<=m; i++) { int x1 = nextInt(); System.out.println(c[x1]); } } private static int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } private static String next() throws IOException { while(!st.hasMoreElements()){ st = new StringTokenizer(in.readLine()); } return st.nextToken(); } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
c863f423ac43a8e6c6d257ffaf976dd5
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
//package com.codeforces.p474; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; public class Main { private StreamTokenizer in; private PrintWriter out; private int a[]; int n; private int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } private int find (int val) { int l =0, r=n-1; while (l<r) { int m = (r+l)/2; if (a[m] > val) { r = m; } else if (val == a[m]){ return m + 1; } else { l = m + 1; } } while ( ( val-=a[l]) > 0 ) { l++; } return l+1; } private void solve() throws Exception { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); n = nextInt(); a = new int[n]; a[0] = nextInt(); for (int i = 1; i<n; i++) { a[i] = a[i-1] + nextInt(); } int m = nextInt(); for (int i=0; i<m; i++) { out.println(find(nextInt())); } out.flush(); out.close(); } public static void main(String[] args) throws Exception { new Main().solve(); } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
e8671791ca4aaaec799de2fd0ea7052e
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.util.Random; import java.util.*; import java.util.Scanner; public class Main { public static String binary = ""; //System.out.println(ans); public static int gcd(int a,int b){ if(a == 0) return b; return gcd(b % a, a); } private static void Binaryform(int number) { int remainder; if (number <= 1) { binary += number; return; } remainder = number %2; Binaryform(number >> 1); binary += remainder +""; } public static void print(Object o){ System.out.print(o +" "); } public static void println(Object o){ System.out.println(o); } public static void main(String arg[]){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int [] a = new int[n]; int [] b = new int [n]; int [] c = new int[n]; for(int i = 0; i < n; i++){ a[i] = scan.nextInt(); } int m = scan.nextInt(); int [] q = new int[m]; for(int i = 0; i < m; i++){ q[i] = scan.nextInt(); } int [] temv = new int[m]; for(int i = 0; i < m; i++){ temv[i] = q[i]; } int [] complet = new int[1000000+10]; boolean flag = true; for(int i = 0; i < n; i++){ if(flag){ b[i] = 1; c[i] = a[i]; flag = false; } else{ //, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding. b[i] = c[i - 1] + 1; c[i] = a[i] + c[i - 1]; } for(int j = b[i]; j <= c[i]; j++) complet[j] = i + 1; } for(int i = 0; i < m; i++){ //System.ou.println(); System.out.println(complet[q[i]] +" "); } /* for(int i = 0; i < n; i++){ System.out.print(b[i] +" "); } System.out.println(" "); for(int i = 0; i < n; i++){ System.out.print(c[i] +" "); } * System.out.println(" "); * */ /* for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(q[i] >= b[j] && q[i] <= c[j]){ System.out.print((j+1) +" "); break; } } } */ } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
a1f03a3afe9a45d15349284a45a3f545
train_002.jsonl
1412609400
It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch.Marmot brought Mole n ordered piles of worms such that i-th pile contains ai worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to a1, worms in second pile are labeled with numbers a1 + 1 to a1 + a2 and so on. See the example for a better understanding.Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained.Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class B474 { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bf.readLine()); String[] input = bf.readLine().split(" "); int[] piles = new int[n]; piles[0] = Integer.parseInt(input[0]); for (int i = 1; i < n; i++) { piles[i] = Integer.parseInt(input[i]) + piles[i-1]; } int Q = Integer.parseInt(bf.readLine()); String[] questions = bf.readLine().split(" "); for (int i = 0; i < Q; i++) { int find = Integer.parseInt(questions[i]); int ans = Arrays.binarySearch(piles, find); if(ans < 0) System.out.println(-ans); else System.out.println(ans+1); } } }
Java
["5\n2 7 3 4 9\n3\n1 25 11"]
1 second
["1\n5\n3"]
NoteFor the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile.
Java 7
standard input
[ "binary search", "implementation" ]
10f4fc5cc2fcec02ebfb7f34d83debac
The first line contains a single integer n (1 ≀ n ≀ 105), the number of piles. The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 103, a1 + a2 + ... + an ≀ 106), where ai is the number of worms in the i-th pile. The third line contains single integer m (1 ≀ m ≀ 105), the number of juicy worms said by Marmot. The fourth line contains m integers q1, q2, ..., qm (1 ≀ qi ≀ a1 + a2 + ... + an), the labels of the juicy worms.
1,200
Print m lines to the standard output. The i-th line should contain an integer, representing the number of the pile where the worm labeled with the number qi is.
standard output
PASSED
ca1cd744e80076cce10bd7aa815b3e4e
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
/** * */ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author moham * */ public class D { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = scanner.nextInt(); long x, y, temp; long bX = 0, bY = 0; String ch; while (n-- > 0) { ch = scanner.next(); x = scanner.nextInt(); y = scanner.nextInt(); if (y < x) { temp = x; x = y; y = temp; } if (ch.charAt(0) == '+') { bX = Math.max(bX, x); bY = Math.max(bY, y); } else { out.println(x < bX || y < bY ? "NO" : "YES"); } } out.flush(); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } } static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
ff1977c2a7f2ea586fd323f3dc7f5c8f
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
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.HashMap; import java.util.Scanner; /** * @Created by sbhowmik on 04/03/19 */ public class PolycarpSNewJob { static public class PolycarpSNewJobOb { long x; long y; public long getX() { return x; } public PolycarpSNewJobOb setX(long x) { this.x = x; return this; } public long getY() { return y; } public PolycarpSNewJobOb setY(long y) { this.y = y; return this; } public PolycarpSNewJobOb(long x, long y) { this.x = x; this.y = y; } } static PolycarpSNewJobOb ob = null; static ArrayList<PolycarpSNewJobOb> list = new ArrayList<>(); static public void printAccording(Character a, long x, long y, PrintWriter pl) { if(a == '+') { if(ob == null) { long max = Math.max(x, y); long min = Math.min(x, y); ob = new PolycarpSNewJobOb(min, max); } else { long max = Math.max(x, y); if(ob.y < max) { ob.setY(max); } long min = Math.min(x, y); if(ob.x < min) { ob.setX(min); } } } else { if(ob.getX()<=x && ob.getY()<=y) { pl.println("YES"); } else { if(ob.getY()<=x && ob.getX()<=y) { pl.println("YES"); } else { pl.println("NO"); } } } } public static void main(String []args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String fl[] = br.readLine().split(" "); PrintWriter printWriter = new PrintWriter(System.out); int n = Integer.parseInt(fl[0]); while ( n > 0) { fl = br.readLine().split(" "); Character ch = String.valueOf(fl[0]).charAt(0); long x = Long.parseLong(fl[1]); long y = Long.parseLong(fl[2]); printAccording(ch, x, y, printWriter); printWriter.flush(); n--; } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
1a98967d3a1600e3e541c32303486a82
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bw = new BufferedWriter(new BufferedWriter(new OutputStreamWriter(System.out))); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st; int maxw = 0; int maxh = 0; for (int i=0; i<n; ++i){ st = new StringTokenizer(br.readLine()); String type = st.nextToken(); int w = Integer.parseInt(st.nextToken()); int h = Integer.parseInt(st.nextToken()); if (h>w){ int temp = h; h = w; w = temp; } if (type.equals("+")){ if (maxw < w) maxw = w; if (maxh < h) maxh = h; } else { if (maxw <= w && maxh <= h ){ bw.write("YES"); } else bw.write("NO"); bw.newLine(); bw.flush(); } } bw.close(); sc.close(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
ebfd1301c832faa0256ef379e0c49efe
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Educational { public static void main(String[] args) throws IOException { Educational c = new Educational(); c.start(); } private void start() throws IOException { //BufferedReader in = new BufferedReader(new FileReader("input.txt")); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer str = new StringTokenizer(in.readLine()); int n = Integer.parseInt(str.nextToken()); int minX = 0; int minY = 0; int diff1 = 0; int diff2 = 0; for(int i = 0; i < n; ++i) { String input = in.readLine(); str = new StringTokenizer(input); char sign = input.charAt(0); str.nextToken();//pass int x = Integer.parseInt(str.nextToken()); int y = Integer.parseInt(str.nextToken()); if(sign == '+') { diff1 = Math.max(0, x - minX) + Math.max(0, y - minY); diff2 = Math.max(0, x - minY) + Math.max(0, y - minX); if(diff1 <= diff2) { minX = Math.max(minX, x); minY = Math.max(minY, y); } else{ minX = Math.max(minX, y); minY = Math.max(minY, x); } } else { if (minX <= x && minY <= y || minX <=y && minY <= x){ writer.println("YES"); } else writer.println("NO"); } } writer.close(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
71296e09096e1bc0b9cbb596d9b129ce
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Map; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; 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; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); EPolycarpsNewJob solver = new EPolycarpsNewJob(); solver.solve(1, in, out); out.close(); } static class EPolycarpsNewJob { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); char c = in.next().charAt(0); int x = in.nextInt(); int y = in.nextInt(); if (x > y) { int xx = x; x = y; y = xx; } int hh = x; int ww = y; Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n - 1; i++) { c = in.next().charAt(0); if (c == '+') { x = in.nextInt(); y = in.nextInt(); if (x > y) { int xx = x; x = y; y = xx; } hh = Math.max(x, hh); ww = Math.max(y, ww); } else { int h = in.nextInt(); int w = in.nextInt(); if ((h >= hh && w >= ww) || (h >= ww && w >= hh)) out.println("YES"); else { out.println("NO"); } } } } } static class FastScanner { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
dd5320f512e295e6eb765481dac4af6a
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
//package codeforces.ECR58; import java.io.*; import java.util.InputMismatchException; public class D { public static final String yes = "YES"; public static final String no = "NO"; public static void main(String args[]) throws Exception { int n = scn.nextInt(); int max = Integer.MIN_VALUE, min = Integer.MIN_VALUE; while (n-->0){ char c = scn.next().charAt(0); if(c == '+'){ int a = scn.nextInt(); int b = scn.nextInt(); if(a<=b){ min = Math.max(a,min); }else{ min = Math.max(b,min); } max = Math.max(Math.max(a,b),max); }else{ int h = scn.nextInt(); int w = scn.nextInt(); if((h>=min && w>=max) || (h>=max && w>=min)){ out.println(yes); }else{ out.println(no); } } } out.close(); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public String toString() { return this.x + " [" + this.y + "]"; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } @Override public boolean equals(Object obj) { if (obj instanceof Pair) { Pair o = (Pair) obj; return this.y == o.y; } return false; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static InputReader scn = new InputReader(System.in); public static PrintWriter out = new PrintWriter(System.out); }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
e58b9f2304dad9eba009386646c38f70
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jenish */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); EPolycarpsNewJob solver = new EPolycarpsNewJob(); solver.solve(1, in, out); out.close(); } static class EPolycarpsNewJob { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int xmax = 0; int ymax = 0; for (int i = 0; i < n; i++) { char c = in.scanString().charAt(0); int x = in.scanInt(); int y = in.scanInt(); if (c == '+') { xmax = Math.max(xmax, Math.min(x, y)); ymax = Math.max(ymax, Math.max(x, y)); } else { if (xmax <= Math.min(x, y) && ymax <= Math.max(x, y)) out.println("YES"); else out.println("NO"); } } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } public String scanString() { int c = scan(); while (isWhiteSpace(c)) c = scan(); StringBuilder RESULT = new StringBuilder(); do { RESULT.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return RESULT.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
339a8b9beeb5ceeefa9053974d0b2c34
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.util.*; public class CF1101D { public static void main(String[] args) { Scanner s = new Scanner(System.in); int ascaksclkasl = Integer.MIN_VALUE; int n = s.nextInt(); int skcakscmaskcakcjk = Integer.MIN_VALUE; StringBuilder qwdqwdlq = new StringBuilder(); s.nextLine(); for (int i = 0; i < n; i++) { String pqwopdqwodpqo = s.nextLine(); String[] arr = pqwopdqwodpqo.split(" "); if(("+").equals(arr[0])){ int x = Integer.parseInt(arr[1]); int y = Integer.parseInt(arr[2]); pair p = new pair(x,y); skcakscmaskcakcjk = Math.max(skcakscmaskcakcjk, p.y); ascaksclkasl = Math.max(ascaksclkasl, p.x); } else { int x = Integer.parseInt(arr[1]); int y = Integer.parseInt(arr[2]); pair p = new pair(x, y); if(p.y >= skcakscmaskcakcjk && p.x >= ascaksclkasl){ qwdqwdlq.append("YES\n"); } else { qwdqwdlq.append("NO\n"); } } } System.out.println(qwdqwdlq); } private static class pair implements Comparable<pair>{ int x; int y; public pair(int x, int y) { int max = Math.max(x, y); int min = Math.min(x, y); this.x = max; this.y = min; } @Override public boolean equals(Object obj) { pair p = (pair) obj; return this.x == p.x && this.y == p.y; } @Override public int compareTo(pair o) { if(this.x == o.x){ return Integer.compare(o.y, this.y); } return Integer.compare(o.x, this.x); } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
304aeb9e3a9695db48bd50914562c780
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); StringBuilder stringBuilder = new StringBuilder(); long xx = 0; long yy = 0; while(n-- > 0) { String q = scanner.next(); if(q.equals("?")) { long h = scanner.nextLong(); long w = scanner.nextLong(); if((xx <= h && yy <= w) || (xx <= w && yy <= h)) stringBuilder.append("YES\n"); else { stringBuilder.append("NO\n"); } }else {//xx > yy long x = scanner.nextLong(); long y = scanner.nextLong(); if(xx < yy) { long d = xx; xx = yy; yy = d; } if(x < y) { long d = x; x = y; y = d; } if(xx < x) xx = x; if(yy < y) yy = y; } } stringBuilder.deleteCharAt(stringBuilder.length() - 1); System.out.println(stringBuilder); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
6bc139a989ac8c27a0f7cbdded956b5d
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; public class Main{ public static void main(String[] args) throws IOException{ StringTokenizer stringTokenizer; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); StringBuilder stringBuilder = new StringBuilder(); int xx = 0; int yy = 0; while(n-- > 0) { stringTokenizer = new StringTokenizer(bufferedReader.readLine()); String q = stringTokenizer.nextToken(); if(q.equals("?")) { int h = Integer.parseInt(stringTokenizer.nextToken()); long w = Integer.parseInt(stringTokenizer.nextToken()); if((xx <= h && yy <= w) || (xx <= w && yy <= h)) stringBuilder.append("YES\n"); else { stringBuilder.append("NO\n"); } }else {//xx > yy int x = Integer.parseInt(stringTokenizer.nextToken()); int y = Integer.parseInt(stringTokenizer.nextToken()); if(xx < yy) { int d = xx; xx = yy; yy = d; } if(x < y) { int d = x; x = y; y = d; } if(xx < x) xx = x; if(yy < y) yy = y; } } stringBuilder.deleteCharAt(stringBuilder.length() - 1); System.out.println(stringBuilder); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
e6c2581914337df9cf4b6e947e10c23d
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class E { public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); FastIn in = FastIn.wrapSystemIn(); int Q = in.nextInt(); int minD = 0; int maxD = 0; for (int qi = 0; qi < Q; qi++) { String query = in.nextString(); switch (query) { case "+": { int x = in.nextInt(); int y = in.nextInt(); minD = Math.max(minD, Math.min(x, y)); maxD = Math.max(maxD, Math.max(x, y)); break; } case "?": { int h = in.nextInt(); int w = in.nextInt(); int qMin = Math.min(h, w); int qMax = Math.max(h, w); if (minD <= qMin && maxD <= qMax) { out.println("YES"); } else { out.println("NO"); } break; } } } out.flush(); out.close(); } static final class FastIn { private final BufferedReader bufferedReader; private StringTokenizer stringTokenizer; static FastIn wrapInputStream(InputStream inputStream) { return new FastIn(inputStream); } static FastIn wrapSystemIn() { return wrapInputStream(System.in); } private FastIn(InputStream inputStream) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); this.bufferedReader = new BufferedReader(inputStreamReader); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } String nextString() { return nextToken(); } int[] nextIntArrayOfSize(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long[] nextLongArrayOfSize(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } private String nextToken() { try { skipUntilNextTokenOrEndOfStream(); assert hasNextToken(); return stringTokenizer.nextToken(); } catch (IOException ioException) { throw new RuntimeException(ioException); } } boolean hasNextToken() throws IOException { skipUntilNextTokenOrEndOfStream(); return stringTokenizer.hasMoreTokens(); } private void skipUntilNextTokenOrEndOfStream() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { String nextLine = bufferedReader.readLine(); if (nextLine == null) { return; } this.stringTokenizer = new StringTokenizer(nextLine); } } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
77d7fdbaa40c5586dac1b9530f2072f3
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.StringTokenizer; import java.util.TreeSet; public class E { public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); FastIn in = FastIn.wrapSystemIn(); int Q = in.nextInt(); TreeSet<Integer> min = new TreeSet<>(); TreeSet<Integer> max = new TreeSet<>(); for (int qi = 0; qi < Q; qi++) { String query = in.nextString(); switch (query) { case "+": { int x = in.nextInt(); int y = in.nextInt(); min.add(Math.min(x, y)); max.add(Math.max(x, y)); break; } case "?": { int h = in.nextInt(); int w = in.nextInt(); int qMin = Math.min(h, w); int qMax = Math.max(h, w); if (min.last() <= qMin && max.last() <= qMax) { out.println("YES"); } else { out.println("NO"); } break; } } } out.flush(); out.close(); } static final class FastIn { private final BufferedReader bufferedReader; private StringTokenizer stringTokenizer; static FastIn wrapInputStream(InputStream inputStream) { return new FastIn(inputStream); } static FastIn wrapSystemIn() { return wrapInputStream(System.in); } private FastIn(InputStream inputStream) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); this.bufferedReader = new BufferedReader(inputStreamReader); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } String nextString() { return nextToken(); } int[] nextIntArrayOfSize(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long[] nextLongArrayOfSize(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } private String nextToken() { try { skipUntilNextTokenOrEndOfStream(); assert hasNextToken(); return stringTokenizer.nextToken(); } catch (IOException ioException) { throw new RuntimeException(ioException); } } boolean hasNextToken() throws IOException { skipUntilNextTokenOrEndOfStream(); return stringTokenizer.hasMoreTokens(); } private void skipUntilNextTokenOrEndOfStream() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { String nextLine = bufferedReader.readLine(); if (nextLine == null) { return; } this.stringTokenizer = new StringTokenizer(nextLine); } } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
031edae4b4b36cfa866b7796146efbfa
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author MaxHeap */ 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); EPolycarpsNewJob solver = new EPolycarpsNewJob(); solver.solve(1, in, out); out.close(); } static class EPolycarpsNewJob { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int w = 0, h = 0; while (n-- > 0) { char type = in.next().charAt(0); if (type == '+') { int x = in.nextInt(); int y = in.nextInt(); int nx = Math.min(x, y); int ny = Math.max(x, y); w = Math.max(w, nx); h = Math.max(h, ny); } else { int x = in.nextInt(); int y = in.nextInt(); if ((w <= x && h <= y) || (w <= y && h <= x)) { out.println("YES"); } else { out.println("NO"); } } } } } static interface FastIO { } static class InputReader implements FastIO { private InputStream stream; private static final int DEFAULT_BUFFER_SIZE = 1 << 16; private static final int EOF = -1; private byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == EOF) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return EOF; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public String next() { int c; while (isSpaceChar(c = this.read())) { } StringBuilder result = new StringBuilder(); result.appendCodePoint(c); while (!isSpaceChar(c = this.read())) { result.appendCodePoint(c); } return result.toString(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF; } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
13124c628cb1c281a8af6f8a9775621b
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
//created by Whiplash99 import java.io.*; import java.util.*; public class E { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int i,N; int l1=0,r1=0; N=Integer.parseInt(br.readLine().trim()); StringBuilder sb=new StringBuilder(); for(i=0;i<N;i++) { String[] s=br.readLine().trim().split(" "); char type=s[0].charAt(0); int l=Integer.parseInt(s[1]); int r=Integer.parseInt(s[2]); if(l>r){int tmp=l; l=r; r=tmp;} if(type=='+') {l1=Math.max(l1,l); r1=Math.max(r1,r);} else { if(l>=l1&&r>=r1) sb.append("YES\n"); else sb.append("NO\n"); } } System.out.println(sb); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
ad704ca7b0b40bd8942fe70f4e32f56d
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Mufaddal Naya */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); EPolycarpsNewJob solver = new EPolycarpsNewJob(); solver.solve(1, in, out); out.close(); } static class EPolycarpsNewJob { public void solve(int testNumber, InputReader c, OutputWriter w) { int q = c.readInt(); int min = 0, max = 0; while (q-- > 0) { String s = c.readString(); if (s.equals("+")) { int u = c.readInt(), v = c.readInt(); if (u > v) { max = Math.max(max, u); min = Math.max(min, v); } else { max = Math.max(max, v); min = Math.max(min, u); } } else { int u = c.readInt(), v = c.readInt(); if (u > v) { if (max <= u && min <= v) { w.printLine("YES"); } else { w.printLine("NO"); } } else { if (max <= v && min <= u) { w.printLine("YES"); } else { w.printLine("NO"); } } } } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
51bb959390c587fd4ce4b4f1ce2ea36f
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Soly { static final int INF = Integer.MAX_VALUE; static void mergeSort(int[] a,int [] c, int b, int e) { if(b < e) { int q = (b + e) >>1; mergeSort(a,c, b, q); mergeSort(a, c,q + 1, e); merge(a,c, b, q, e); } } static void merge(int[] a,int[]c, int b, int mid, int e) { int n1 = mid - b + 1; int n2 = e - mid; int[] L = new int[n1+1], R = new int[n2+1]; int[] L1 = new int[n1+1], R1 = new int[n2+1]; for(int i = 0; i < n1; i++) { L[i] = a[b + i]; L1[i] = c[b + i]; } for(int i = 0; i < n2; i++) { R[i] = a[mid + 1 + i]; R1[i] = c[mid + 1 + i]; } L[n1] = R[n2] = INF; for(int k = b, i = 0, j = 0; k <= e; k++) if(L[i] <= R[j]){ a[k] = L[i]; c[k] = L1[i++]; } else { a[k] = R[j]; c[k] = R1[j++]; } } static int s; static Stack<Character> sta; static int type(char[] a){ sta= new Stack<>(); s=0; int op=0,cl=0; for (int i = 0; i < a.length; i++) { if (a[i]==')'){ if (!sta.isEmpty()&&sta.peek()=='('){ op--; sta.pop(); } else{ cl++; sta.push(')'); } } else{ op++; sta.push('('); } } if (sta.isEmpty())return 0; else{ s=sta.size(); //1 --> closing //2 -->opening if (op>0&&cl>0)return 50; else{ if (op>0)return 2; return 1; } } } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); try (PrintWriter or = new PrintWriter(System.out)) { int t=in.nextInt(); int mx=0,my=Integer.MAX_VALUE; int xx=0,yy=0; while (t-->0){ String e=in.next(); int x=in.nextInt(); int y=in.nextInt(); if (e.equals("+")){ xx=Math.max(xx,Math.max(x,y)); yy=Math.max(yy,Math.min(x,y)); } else { or.println((xx <= x && yy <= y) || (xx <= y && yy <= x) ? "YES" : "NO"); //a=new ArrayList<>(); } } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) { if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) { f *= 10; } } } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } } class coooo { int x,y; public coooo(int x, int y) { this.x = x; this.y = y; } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
27a778c517f15a31ca7ec44cc47624a6
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class e { public static void main(String[] args) { JS in = new JS(); PrintWriter out = new PrintWriter(System.out); new e().solve(in,out); out.close(); } private void solve(JS in, PrintWriter out) { int N = in.nextInt(); boolean good = true; int max1 = 0; int max2 = 0; for(int i=0; i<N; i++) { int q = in.next().charAt(0); if(q=='+') { int a = in.nextInt(); int b = in.nextInt(); int ma = Math.max(a, b); int mi = Math.min(a, b); max1 = Math.max(max1, ma); max2 = Math.max(max2, mi); } else if(q=='?') { int a = in.nextInt(); int b = in.nextInt(); int ma = Math.max(a, b); int mi = Math.min(a, b); //System.out.println(ma + " " + mi); if(max1<=ma && max2<=mi) { out.println("YES"); } else { out.println("NO"); } } //System.out.println("cur: " + max1 + " " + max2); } } static class JS { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
60ed6ba25dc785f9ed8f65b3bd20f318
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.math.*; import java.util.*; import javafx.util.Pair; public class Level { public static DS.Scanner in = new DS.Scanner(); public static DS.Print out = new DS.Print(); public static int[] parent = null; public static void main(String[] args) throws IOException { long n = in.nextInt(); long H = 0; long W = 0; while (n-- > 0) { long type = in.next().charAt(0); long h = in.nextInt(); long w = in.nextInt(); long mx = Math.max(h, w); long mn = Math.min(h, w); h = mn; w = mx; if (type == 63) { if (H <= h && W <= w) { out.println("YES"); } else { out.println("NO"); } } else { H = Math.max(H, h); W = Math.max(W, w); } } out.close(); } public static class Algo { public static int lowerBound(ArrayList<Long> list, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; if (value <= list.get(mid)) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(ArrayList<Long> list, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; if (value >= list.get(mid)) { low = mid + 1; } else { high = mid; } } return low; } public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int[n1]; int R[] = new int[n2]; for (int i = 0; i < n1; ++i) { L[i] = arr[l + i]; } for (int j = 0; j < n2; ++j) { R[j] = arr[m + 1 + j]; } int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void sort(int arr[], int l, int r) { if (l < r) { int m = (l + r) / 2; sort(arr, l, m); sort(arr, m + 1, r); merge(arr, l, m, r); } } public static ArrayList<Integer> sieve(int n) { long sqr = (long) Math.sqrt(n); boolean[] isPrime = new boolean[n + 1]; for (int i = 0; i <= n; i++) { isPrime[i] = true; } for (int i = 2; i <= sqr; i++) { if (isPrime[i]) { for (int p = i * i; p <= n; p += i) { isPrime[p] = false; } } } ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i <= n; i++) { if (isPrime[i]) { list.add(i); } } list.remove(0); list.remove(0); return list; } public static boolean isPrime(long n) { boolean res = true; List l = sieve((int) Math.sqrt(n)); for (int i = 0; i < l.size(); i++) { int curPrime = (int) l.get(i); if (n % curPrime == 0) { res = false; break; } } return res; } public static ArrayList factorizatin(int n) { ArrayList<Integer> list = new ArrayList<Integer>(); if (n == 1) { list.add(1); return list; } else if (n == 2) { list.add(2); return list; } else { if (isPrime(n)) { System.out.println(-1); return null; } else { Iterator ptr = sieve((int) Math.sqrt(n)).iterator(); if (ptr.hasNext()) { int cur = (int) ptr.next(); // System.out.println(cur); while (n != 1) { System.out.println(n); while (n % cur == 0) { list.add(cur); n /= cur; } if (ptr.hasNext()) { cur = (int) ptr.next(); } //System.out.println(cur); } } if (n > 2) { list.add(n); } } return list; } } public static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } public static long lcm(long a, long b) { long ab = a * b; long gcd = gcd(a, b); return ab / gcd; } public static int bs(int[] z, int k) { int res = 0; int l = 0; int r = z.length - 1; int mid = (l + r) / 2; while (l <= r) { if (z[mid] == k) { res = 1; break; } if (z[mid] > k) { r = mid - 1; } else { l = mid + 1; } mid = (l + r) / 2; } return res; } public static void solve() { int n = Integer.parseInt(in.next()); int target = Integer.parseInt(in.next()); int[] z = new int[n]; for (int i = 0; i < n; i++) { z[i] = Integer.parseInt(in.next()); } Arrays.sort(z); int ptr1 = 0; int ptr2 = n - 1; int wind = z[ptr1] + z[ptr2]; if (wind == target) { System.out.println(ptr1 + "-" + ptr2); } else { while (ptr1 < ptr2 && wind != target) { if (wind > target) { wind -= z[ptr2--]; wind += z[ptr2]; } else { wind -= z[ptr1++]; wind += z[ptr1]; } } if ((ptr1 != 0 || ptr2 != n - 1) && ptr2 != ptr1) { System.out.println(ptr1 + "-" + ptr2); } else { System.out.println(-1); } } } public static void sliding() { int n = Integer.parseInt(in.next()); int k = Integer.parseInt(in.next()); int[] z = new int[n]; for (int i = 0; i < n; i++) { z[i] = Integer.parseInt(in.next()); } int wind = 0; for (int i = 0; i < k; i++) { wind += z[i]; } int max = wind; for (int i = k; i < n; i++) { wind += z[i]; wind -= z[i - k]; max = Math.max(max, wind); } System.out.println(max); } public static ArrayList<Integer> dd(int n) { ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { list.add(n / i); list.add(i); } } return list; } } private static class DS { public static class Graph { int V; ArrayList[] data = null; int[] parent = null; public Graph(int v) { this.V = v + 1; this.data = new ArrayList[this.V]; for (int i = 1; i < V; i++) { this.data[i] = new ArrayList<Pair>(); } parent = new int[v + 1]; Arrays.fill(parent, -1); } public void add(int u, int v, int cost) { data[u].add(new Pair(v, cost)); data[v].add(new Pair(u, cost)); } public boolean isConnected(int u, int v) { if (data[u].contains(v)) { return true; } else { return false; } } public int[] dijkstra(int s) { Comparator<Pair> comp = new Comparator<Pair>() { @Override public int compare(Pair t, Pair t1) { return (int) t.getValue() - (int) t1.getValue(); } }; PriorityQueue<Pair> q = new PriorityQueue<>(comp); int[] cost = new int[this.V]; Arrays.fill(cost, Integer.MAX_VALUE); q.add(new Pair(s, 0)); cost[s] = 0; parent[s] = s; while (!q.isEmpty()) { Pair cur = q.poll(); int u = (int) cur.getKey(); int cst = (int) cur.getValue(); ArrayList<Pair> list = data[u]; for (Pair p : list) { int d = (int) p.getKey(); int curCost = (int) p.getValue(); if (cst + curCost <= cost[d]) { parent[d] = u; cost[d] = curCost + cst; q.add(new Pair(d, cost[d])); } } } return cost; } } public static class Print { private final BufferedWriter bw; public Print() { 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(); } } private static class Stack { private int Size; Node top; Node last; Stack() { Size = 0; top = null; last = null; } public int size() { return this.Size; } public void push(Object item) { Node node = new Node(); node.data = item; node.next = top; top = node; Size++; } public void pop() { if (top != null) { top = top.next; } Size--; } public boolean isEmpty() { return top == null; } public Object getTop() { if (top != null) { return top.data; } else { return null; } } public void display() { if (top != null) { Node temp = top; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } } private class Node { Object data; Node next; } } private static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } BigInteger nextBigInteger() { return new BigInteger(next()); } } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
241581991724d2f252142b82add6a68b
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.*; public class Q4 { static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(System.in), 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 char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { InputReader in = new InputReader(); int N = in.nextInt(); PrintWriter out = new PrintWriter(System.out); long maxA = 0, maxB = 0, input1, input2; for (int i = 0; i < N; i++) { char ch = in.next().charAt(0); input1 = in.nextLong(); input2 = in.nextLong(); if (input1 > input2) { long temp = input1; input1 = input2; input2 = temp; } if (ch == '+') { if (maxA < input1) maxA = input1; if (maxB < input2) maxB = input2; } else if (ch == '?') { if (maxA <= input1 && maxB <= input2) out.println("YES"); else if (maxA <= input2 && maxB <= input1) out.println("YES"); else out.println("NO"); } } out.flush(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
36339499e10b2a678919caf7c614ef1d
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.*; public class Code534B { public static void main(String[] args) throws IOException { BufferedReader jk = new BufferedReader(new InputStreamReader(System.in)) ; StringTokenizer ana = new StringTokenizer(jk.readLine()) ; OutputStream out = new BufferedOutputStream ( System.out ); int n = Integer.parseInt(ana.nextToken()) ; long x=-1 ; long y=-1 ; for(int i=0 ; i<n ; i++) { ana = new StringTokenizer(jk.readLine()) ; char d = ana.nextToken().charAt(0) ; long x1 = Long.parseLong(ana.nextToken()) ; long x2 = Long.parseLong(ana.nextToken()) ; long h = Long.max(x1, x2) ; long k = Long.min(x1,x2) ; if(d=='+') { if(h>x)x=h ; if(k>y) y=k ; } else { if(x<=h && y<=k) out.write(("YES"+"\n").getBytes()); else out.write(("NO"+"\n").getBytes()); } } out.flush(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
5f5cffe887f50db23342306d1ea13243
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.StringTokenizer; public class PolycarpNewJob { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long h = 0; long w = 0; BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); for (int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); char c = st.nextToken().charAt(0); long x = Long.parseLong(st.nextToken()); long y = Long.parseLong(st.nextToken()); if (x > y) { long temp = x; x = y; y = temp; } if (c == '+') { h = Math.max(h, x); w = Math.max(w, y); } else { if (x >= h && y >= w) log.write("YES\n"); else log.write("NO\n"); } } log.flush(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
77ec33543cb4ed99dbb516199db01841
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringBuilder sb= new StringBuilder(); TreeSet<Integer> x1 = new TreeSet<>(); TreeSet<Integer> y1 = new TreeSet<>(); TreeSet<Integer> x2 = new TreeSet<>(); TreeSet<Integer> y2 = new TreeSet<>(); int lx=0; int ly=0; int rx=0; int ry=0; for(int i=0;i<n;i++){ StringTokenizer stk = new StringTokenizer(br.readLine()); String a = stk.nextToken(); int x = Integer.parseInt(stk.nextToken()); int y = Integer.parseInt(stk.nextToken()); if(a.equals("+")){ if(x>y){ x1.add(x); y1.add(y); } else{ x2.add(x); y2.add(y); } } else{ // sb.append("NO").append('\n'); if(x>y){ Integer h1 = x1.higher(x); Integer h2 = y1.higher(y); if(h1== null && h2==null){ if((x2.higher(x)==null && y2.higher(y) ==null) || (x2.higher(y)==null && y2.higher(x) ==null)){ sb.append("YES").append('\n'); continue; } } } else{ Integer h1 = x2.higher(x); Integer h2 = y2.higher(y); if(h1== null && h2==null){ if((x1.higher(x)==null && y1.higher(y) ==null) || (x1.higher(y)==null && y1.higher(x) ==null)){ sb.append("YES").append('\n'); continue; } } } sb.append("NO").append('\n'); } } System.out.print(sb); } public static class LL{ int x,y; public LL(int x,int y){ this.x = x; this.y = y; } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
5fc5b97d7297002444fa2ff11052c6be
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; import java.math.BigInteger; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.util.Comparator; import java.util.ArrayList; import java.util.List; import java.util.Collections; public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in, out); out.close(); } static class TaskA { public void solve(InputReader in, PrintWriter out) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int max = 0, min = 0; for(int q = 0; q < n; q++){ char c = sc.next().charAt(0); int a = sc.nextInt(), b = sc.nextInt(); if(c == '+') { max = Math.max(max, Math.max(a, b)); min = Math.max(min, Math.min(a, b)); }else{ if(max <= Math.max(a, b) && min <= Math.min(a, b)) out.println("YES"); else out.println("NO"); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
e59cac41dc6146d95fcc01e2a4a749f7
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.*; //Mann Shah [ DAIICT ]. //fast io public class Main { static int mod = (int) (1e9+7); static InputReader in; static PrintWriter out; public static boolean check(int x ,int y , int h , int w) { if( x<=h && y<=w || y<=h && x<=w) { return true; } else { return false; } } public static void main(String args[] ) { in = new InputReader(System.in); out = new PrintWriter(System.out); ///Scanner read = new Scanner(System.in); int n = in.nextInt(); int mx=0,my=0; for(int i=0;i<n;i++) { String c = in.readString(); int a = in.nextInt(); int b = in.nextInt(); if(a>b) { int t =a; a=b; b=t; } if(c.charAt(0)=='+') { if(a>mx) { mx=a; } if(b>my) { my=b; } } if(c.charAt(0)=='?') { if(check(mx,my,a,b)) { out.println("YES"); } else { out.println("NO"); } } } out.close(); } 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 long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } 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 { public boolean isSpaceChar(int ch); } } } class couple implements Comparable<couple> { int x,y; public couple(int m,int f) { x=m; y=f; } public int compareTo(couple o) { return x-o.x; } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
8703b19f2059d5fe8b4aea029b3789b9
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { 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 = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } 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 = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(br.readLine()); PrintWriter out = new PrintWriter(System.out); int h = Integer.MIN_VALUE,w = Integer.MIN_VALUE; for(int i=0;i<q;i++) { String[] str = (br.readLine()).trim().split(" "); int a = Integer.parseInt(str[1]); int b = Integer.parseInt(str[2]); int u = Math.min(a,b); int v = Math.max(a,b); if(str[0].equals("+")) { h = Math.max(u,h); w = Math.max(v,w); } else if(str[0].equals("?")) { if(h <= u && w <= v) out.println("YES"); else out.println("NO"); } //System.out.println(h + " " + v); } out.close(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
62630c1b4f900e3730dfa9a079f3b324
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken(); int n=(int)in.nval; int minx=0,miny=0; for (int i=0;i<n;++i) { in.nextToken(); String s=in.toString(); in.nextToken(); int x=(int)in.nval; in.nextToken(); int y=(int)in.nval; if (x>y) { int tmp=x; x=y; y=tmp; } if (s.charAt(7)=='+') { minx=Math.max(minx,x); miny=Math.max(miny,y); } else { if (minx<=x&&miny<=y) out.println("YES"); else out.println("NO"); } } out.flush(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
9ec323a393c7c7f1271984533ea93b76
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printWriter = new PrintWriter(System.out); int n = Integer.parseInt(bufferedReader.readLine()); int vmin = 0, vmax = 0; for (int i = 0; i < n; i++) { StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine()); char c = (stringTokenizer.nextToken()).charAt(0); if (c == '+') { int x = Integer.parseInt(stringTokenizer.nextToken()), y = Integer.parseInt(stringTokenizer.nextToken()); if (vmin < Math.min(x, y)) { vmin = Math.min(x, y); } if (vmax < Math.max(x, y)) { vmax = Math.max(x, y); } } else { int h = Integer.parseInt(stringTokenizer.nextToken()), w = Integer.parseInt(stringTokenizer.nextToken()); if ((Math.min(h, w) >= vmin) && (Math.max(h, w) >= vmax)) { printWriter.println("YES"); } else { printWriter.println("NO"); } } } printWriter.close(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
5d6400723facbc3bdae82028c116f59d
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Andrei Chugunov */ public class Main { private static class Solution { private void solve(FastScanner in, PrintWriter out) { int n = in.nextInt(); int maxX = 0; int maxY = 0; for (int i = 0; i < n; i++) { String s = in.next(); if (s.charAt(0) == '+') { int x = in.nextInt(); int y = in.nextInt(); if (x > y) { int tmp = x; x = y; y = tmp; } maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); } else { int h = in.nextInt(); int w = in.nextInt(); if (h > w) { int tmp = h; h = w; w = tmp; } if (maxX <= h && maxY <= w) { out.println("YES"); } else { out.println("NO"); } } } } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Solution solver = new Solution(); solver.solve(in, out); out.flush(); //out.close(); } private static class FastScanner { private BufferedReader br; private StringTokenizer st; FastScanner(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream), 32768); st = null; } private String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
8dd01f7efaccaed99e11baa1fe23f45a
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
import java.io.*; import java.util.*; public class E { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int q = Integer.parseInt(br.readLine()); int x_ = 0, y_ = 0; while (q-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); String s = st.nextToken(); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); if (x > y) { int tmp = x; x = y; y = tmp; } if (s.charAt(0) == '+') { if (x_ < x) x_ = x; if (y_ < y) y_ = y; } else pw.println(x_ <= x && y_ <= y ? "YES" : "NO"); } pw.close(); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
f621078c05511022df760e8d3fb9044a
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
/* yeet */ import java.util.*; import java.io.*; public class E { public static void main(String args[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); String str = infile.readLine(); int Q = Integer.parseInt(str); int max1 = Integer.MIN_VALUE; int max2 = Integer.MIN_VALUE; StringTokenizer st; StringBuilder sb = new StringBuilder(); while(Q --> 0) { st = new StringTokenizer(infile.readLine()); char c = st.nextToken().charAt(0); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); if(a < b) { int t = a; a = b; b = t; } if(c == '+') { max1 = Math.max(max1, a); max2 = Math.max(max2, b); } else { if(max1 <= a && max2 <= b) sb.append("YES\n"); else sb.append("NO\n"); } } System.out.print(sb); } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
352958a5517b83ac225278c102303964
train_002.jsonl
1547217300
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has.Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares).A bill $$$x \times y$$$ fits into some wallet $$$h \times w$$$ if either $$$x \le h$$$ and $$$y \le w$$$ or $$$y \le h$$$ and $$$x \le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others.Now you are asked to perform the queries of two types: $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
256 megabytes
// Working program using Reader Class import java.io.*; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.StringTokenizer; 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') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static int done=0; public static void checkc(int[] a,int i,int sum) { sum+=a[i]; if(i==a.length-1) { //sum+=a[i]; if(sum==0 || sum==360) { done=1; } } else { checkc(a,i+1,sum); checka(a,i+1,sum); } } public static void checka(int[] a,int i,int sum) { sum-=a[i]; if(i==a.length-1) { //sum-=a[i]; if(sum==0 || sum==360) { done=1; } } else { checkc(a,i+1,sum); checka(a,i+1,sum); } } public static class Node implements Comparable<Node>{ //HashSet<Integer> hs=new HashSet<Integer>(); int l=0; int r=0; int ind=0; Node(int li,int ri,int i){ l=li; r=ri; ind=i; } public int compareTo(Node n) { return this.l-n.l; } } public static void main(String[] args) throws IOException { //BufferedReader scan = new BufferedReader(new InputStreamReader(System.in)); BufferedReader scan = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); // int test=scan.nextInt(); // for(int t=0;t<test;t++) { String d=scan.readLine(); int n=Integer.parseInt(d); int x1=Integer.MIN_VALUE; int y1=Integer.MIN_VALUE; int x2=Integer.MAX_VALUE; int y2=Integer.MAX_VALUE; for(int i=0;i<n;i++) { String s=scan.readLine(); String[] splited = s.split("\\s+"); int x=Integer.parseInt(splited[1]); int y=Integer.parseInt(splited[2]); int a=Math.max(x, y); int b=Math.min(x,y); if(s.charAt(0)=='+') { if(x1==Integer.MIN_VALUE) { x1=Math.max(x, y); x2=Math.min(x, y); y1=x2; y2=x1; } else { x1=Math.max(x1, a); y1=Math.max(y1, b); x2=Math.max(x2, b); y2=Math.max(a, y2); } } else { if((a>=x1 && b>=y1)||(a>=y2 && b>=x2)) { pw.println("YES"); } else { pw.println("NO"); } } } pw.close(); //} } }
Java
["9\n+ 3 2\n+ 2 3\n? 1 20\n? 3 3\n? 2 3\n+ 1 5\n? 10 10\n? 1 5\n+ 1 1"]
3 seconds
["NO\nYES\nYES\nYES\nNO"]
NoteThe queries of type $$$2$$$ of the example: Neither bill fits; Both bills fit (just checking that you got that bills can overlap); Both bills fit (both bills are actually the same); All bills fit (too much of free space in a wallet is not a problem); Only bill $$$1 \times 5$$$ fit (all the others don't, thus it's "NO").
Java 8
standard input
[ "implementation" ]
fe939f7503f928ff4dbe8d05c5643f38
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: $$$+~x~y$$$ ($$$1 \le x, y \le 10^9$$$) β€” Polycarp earns a bill of size $$$x \times y$$$; $$$?~h~w$$$ ($$$1 \le h, w \le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h \times w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data.
1,500
For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise.
standard output
PASSED
8c4af3c299d9524a2ad3795ad7732f45
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.*; public class Code0911 { public static void main(String[] args) { Reader s = new Reader(); int t = s.nextInt(); while (t-- > 0){ long a = s.nextLong(); long m = s.nextLong(); long d = gcd(a,m); long k1 = a/d; long k3 = m/d; // gcd((a + x),m) = 1 // x = d,2d,3d,...pd where p = (m-1)/d; // k2 = 1,2,3, ... m-1 // k1 + k2 = 1 + k1, 2+ k1 ... p+k1 // count[l , r] which has not common factor p1,p2,p3 long p = (m-1)/d; List<Long> primes = findPrime(k3); System.out.println(count(p+k1,primes) - count(k1-1, primes)); } } private static long count(long k1, List<Long> primes) { long ans = k1; for (long x:primes){ ans = ans - ans/x; } return ans; } private static List<Long> findPrime(long m) { List<Long> primes = new ArrayList<>(); long tm = m; for (long i = 2; i <= Math.sqrt(tm); i++) { if(m%i == 0){ primes.add(i); while (m%i == 0){ m = m/i; } } } if(m > 1){ primes.add(m); } return primes; } private static long gcd(long a, long m) { if(a%m == 0){ return m; } return gcd(m,a%m); } static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
272c7e081bdd84ef16292524e47d4c3d
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; 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; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DSameGCDs solver = new DSameGCDs(); solver.solve(1, in, out); out.close(); } static class DSameGCDs { public void solve(int testNumber, FastReader s, PrintWriter out) { int t = s.nextInt(); while (t-- > 0) { long a = s.nextLong(); long b = s.nextLong(); long gcd = DSameGCDs.Maths.gcd(a, b); a /= gcd; b /= gcd; out.println(phi(b)); } } static long phi(long n) { // Initialize result as n long result = n; // Consider all prime factors // of n and subtract their // multiples from result for (long p = 2; p * p <= n; ++p) { // Check if p is // a prime factor. if (n % p == 0) { // If yes, then update // n and result while (n % p == 0) n /= p; result -= result / p; } } // If n has a prime factor // greater than sqrt(n) // (There can be at-most // one such prime factor) if (n > 1) result -= result / n; return result; } private static class Maths { private static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
8cdca4b1d94db8d88d8eb5fba1f87c7b
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws Exception { new Main().run();} int ct = 0; void add(int u,int v){ to[ct] = v; ne[ct] = h[u]; h[u] = ct++; } int stk[]; int pa[]; int h[],to[],ne[]; int dis[]; int time[]; int clock =0; int vale[][]; int vals[][]; int valerm[][]; int valsrm[][]; int ups[]; int res[]; int downs[]; int cnt_up[]; int cnt_down[]; int myup[]; int mydown[]; int fa[]; int root(int x){ if(fa[x]==x){ return x; }else{ int pt = x; while(pt!=fa[pt]){ pt = fa[pt]; } while(x!=fa[x]){ int cur = x; x = fa[x]; fa[cur] = pt; } return pt; } } void dfs(int rt){ int p = 0; stk[p++] = 0; while(p>0){ rt = stk[--p]; if(rt>=0){ stk[p++] = ~rt; cnt_up[rt] = ups[myup[rt]]; cnt_down[rt] = downs[mydown[rt]]; for(int i=h[rt];i!=-1;i=ne[i]){ int v =to[i]; if(v==pa[rt]) continue; stk[p++] = v; } }else{ rt = ~rt; for(int cao:vals[rt]){ ups[cao]++; } for(int cao:vale[rt]){ downs[cao]++; } for(int cao:valerm[rt]){ downs[cao]--; } res[rt] += ups[myup[rt]]-cnt_up[rt] + downs[mydown[rt]]-cnt_down[rt]; for(int cao:valsrm[rt]){ ups[cao]--; } } } } void solve(){ int q = ni(); long prime[] = new long[20]; ot:for(int ii=0;ii<q;++ii){ long a = nl(); long m = nl(); long g = gcd(a,m); a /= g; m /= g; BigInteger z1 = BigInteger.valueOf(m); int p= 0 ; for(long ck=2;ck*ck<=m;++ck){ if(m%ck==0) { while (m % ck == 0) { m /= ck; } prime[p++] = ck; } } if(m>1){ prime[p++] = m; } long s = 1; long x = 1; for(int j=0;j<p;++j){ s *= (prime[j]-1); x *= prime[j]; } println(z1.multiply(BigInteger.valueOf(s)).divide(BigInteger.valueOf(x))); } } int n = 0; void solve1() { n = ni(); int m = ni(); int es = 2*(n-1); pa = new int[n]; fa = new int[n]; ups = new int[2*n+1]; downs = new int[2*n+1]; h = new int[n]; dis = new int[n]; stk = new int[n]; Arrays.fill(h,-1); to = new int[es]; ne = new int[es]; time = new int[n]; res = new int[n]; myup = new int[n]; mydown = new int[n]; cnt_up = new int[n]; cnt_down = new int[n]; for(int i=0;i<n-1;++i){ int u = ni()-1; int v = ni()-1; add(u,v); add(v,u); } for(int i=0;i<n;++i){ time[i] = ni(); } int u[] = new int[m]; int v[] = new int[m]; int le[] = new int[n]; int ls[] = new int[n]; int q[] = new int[n]; for(int i=0;i<m;++i) { u[i] = ni() - 1; v[i] = ni() - 1; le[v[i]]++; ls[u[i]]++; q[v[i]]++; q[u[i]]++; } int qy[][] = new int[n][]; for(int i=0;i<n;++i){ qy[i] = new int[q[i]]; fa[i] = i; } int oq[] = q.clone(); for(int i=0;i<m;++i) { qy[u[i]][--q[u[i]]] = v[i]; qy[v[i]][--q[v[i]]] = u[i]; } int rt = 0; int p = 1; boolean vis[] = new boolean[n]; stk[0] = 0; while(p>0) { rt = stk[--p]; if(rt>=0) { stk[p++] = ~rt; for (int i = h[rt]; i != -1; i = ne[i]) { int vv = to[i]; if (pa[rt] == vv) continue; pa[vv] = rt; dis[vv] = 1 + dis[rt]; stk[p++] = vv; } }else{ rt = ~rt; vis[rt] = true; for (int i = 0 ;i < qy[rt].length;++i) { if (vis[qy[rt][i]]) { qy[rt][i] = root(qy[rt][i]); }else{ qy[rt][i] = -1; } } fa[rt] = pa[rt]; } } for(int i=0;i<n;++i){ myup[i] = dis[i]+time[i]; mydown[i] = time[i]-dis[i]+n; } int lca[] = new int[m]; int v1[] = new int[m]; int lcas[] = new int[n]; int lcae[] = new int[n]; for(int i=0;i<m;++i){ int c1 = qy[u[i]][--oq[u[i]]]; int c2 = qy[v[i]][--oq[v[i]]]; if(c1!=-1){ lca[i] = c1; }else{ lca[i] = c2; } v1[i] = dis[u[i]]-(dis[lca[i]]<<1)+n; lcas[lca[i]]++; lcae[lca[i]]++; } vale = new int[n][]; vals = new int[n][]; valsrm = new int[n][]; valerm = new int[n][]; for(int i=0;i<n;++i){ vale[i] = new int[le[i]]; vals[i] = new int[ls[i]]; valsrm[i] = new int[lcas[i]]; valerm[i] = new int[lcae[i]]; } for(int i=0;i<m;++i) { valerm[lca[i]][--lcae[lca[i]]] = vale[v[i]][--le[v[i]]] = v1[i]; valsrm[lca[i]][--lcas[lca[i]]] = vals[u[i]][--ls[u[i]]] = dis[u[i]]; } dfs(0); for(int i=0;i<n;++i){ print(res[i]+" "); } } // int []h,to,ne,wt; long t1[]; // long t2[]; void update(long[] t,int i,long v){ for(;i<t.length;i+=(i&-i)){ t[i] += v; } } long get(long[] t,int i){ long s = 0; for(;i>0;i-=(i&-i)){ s += t[i]; } return s; } int equal_bigger(long t[],long v){ int s=0,p=0; for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) { if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){ v -= t[p+(1<<i)]; p |= 1<<i; } } return p+1; } long gcd(long a,long b){ return b==0?a: gcd(b,a%b);} InputStream is;PrintWriter out; void run() throws Exception {is = System.in; // is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in"); out = new PrintWriter(System.out);solve();out.flush();} private byte[] inbuf = new byte[2]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try {lenbuf = is.read(inbuf);} catch (IOException e) {throw new InputMismatchException();} if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++];} private boolean isSpaceChar(int c) {return !(c >= 33 && c <= 126);} private int skip() {int b;while((b = readByte()) != -1 && isSpaceChar(b));return b;} private double nd() {return Double.parseDouble(ns());} private char nc() {return (char) skip();} private char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;} private String ns() {int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[] ns(int n) {char[] buf = new char[n];int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b;b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p);} private String nline() {int b = skip();StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;} private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;} private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();return a;} private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = (num << 3) + (num << 1) + (b - '0'); else return minus ? -num : num; b = readByte();}} private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = num * 10 + (b - '0'); else return minus ? -num : num; b = readByte();}} void print(Object obj){out.print(obj);} void println(Object obj){out.println(obj);} void println(){out.println();} }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
4f1d924f219da00ce92bad9f5a4641b5
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws Exception { new Main().run();} int ct = 0; void add(int u,int v){ to[ct] = v; ne[ct] = h[u]; h[u] = ct++; } int stk[]; int pa[]; int h[],to[],ne[]; int dis[]; int time[]; int clock =0; int vale[][]; int vals[][]; int valerm[][]; int valsrm[][]; int ups[]; int res[]; int downs[]; int cnt_up[]; int cnt_down[]; int myup[]; int mydown[]; int fa[]; int root(int x){ if(fa[x]==x){ return x; }else{ int pt = x; while(pt!=fa[pt]){ pt = fa[pt]; } while(x!=fa[x]){ int cur = x; x = fa[x]; fa[cur] = pt; } return pt; } } void dfs(int rt){ int p = 0; stk[p++] = 0; while(p>0){ rt = stk[--p]; if(rt>=0){ stk[p++] = ~rt; cnt_up[rt] = ups[myup[rt]]; cnt_down[rt] = downs[mydown[rt]]; for(int i=h[rt];i!=-1;i=ne[i]){ int v =to[i]; if(v==pa[rt]) continue; stk[p++] = v; } }else{ rt = ~rt; for(int cao:vals[rt]){ ups[cao]++; } for(int cao:vale[rt]){ downs[cao]++; } for(int cao:valerm[rt]){ downs[cao]--; } res[rt] += ups[myup[rt]]-cnt_up[rt] + downs[mydown[rt]]-cnt_down[rt]; for(int cao:valsrm[rt]){ ups[cao]--; } } } } void solve(){ int q = ni(); Set<Long> st = new HashSet<>(); long prime[] = new long[35]; ot:for(int ii=0;ii<q;++ii){ st.clear(); long a = nl(); long m = nl(); long g = gcd(a,m); a /= g; m /= g; long low = a; long high = a+m-1; int p= 0 ; for(long ck=2;ck*ck<=m;++ck){ if(m%ck==0) { while (m % ck == 0) { m /= ck; } prime[p++] = ck; } } if(m>1){ prime[p++] = m; } long s = 1; long x = 1; for(int j=0;j<p;++j){ s *= (prime[j]-1); x *= prime[j]; } BigInteger z1 = BigInteger.valueOf(high-low+1).multiply(BigInteger.valueOf(s)).divide(BigInteger.valueOf(x)); println(z1); // println(BigInteger.valueOf(high).subtract(BigInteger.valueOf(low)).add(BigInteger.ONE).subtract(z1)); } } int n = 0; void solve1() { n = ni(); int m = ni(); int es = 2*(n-1); pa = new int[n]; fa = new int[n]; ups = new int[2*n+1]; downs = new int[2*n+1]; h = new int[n]; dis = new int[n]; stk = new int[n]; Arrays.fill(h,-1); to = new int[es]; ne = new int[es]; time = new int[n]; res = new int[n]; myup = new int[n]; mydown = new int[n]; cnt_up = new int[n]; cnt_down = new int[n]; for(int i=0;i<n-1;++i){ int u = ni()-1; int v = ni()-1; add(u,v); add(v,u); } for(int i=0;i<n;++i){ time[i] = ni(); } int u[] = new int[m]; int v[] = new int[m]; int le[] = new int[n]; int ls[] = new int[n]; int q[] = new int[n]; for(int i=0;i<m;++i) { u[i] = ni() - 1; v[i] = ni() - 1; le[v[i]]++; ls[u[i]]++; q[v[i]]++; q[u[i]]++; } int qy[][] = new int[n][]; for(int i=0;i<n;++i){ qy[i] = new int[q[i]]; fa[i] = i; } int oq[] = q.clone(); for(int i=0;i<m;++i) { qy[u[i]][--q[u[i]]] = v[i]; qy[v[i]][--q[v[i]]] = u[i]; } int rt = 0; int p = 1; boolean vis[] = new boolean[n]; stk[0] = 0; while(p>0) { rt = stk[--p]; if(rt>=0) { stk[p++] = ~rt; for (int i = h[rt]; i != -1; i = ne[i]) { int vv = to[i]; if (pa[rt] == vv) continue; pa[vv] = rt; dis[vv] = 1 + dis[rt]; stk[p++] = vv; } }else{ rt = ~rt; vis[rt] = true; for (int i = 0 ;i < qy[rt].length;++i) { if (vis[qy[rt][i]]) { qy[rt][i] = root(qy[rt][i]); }else{ qy[rt][i] = -1; } } fa[rt] = pa[rt]; } } for(int i=0;i<n;++i){ myup[i] = dis[i]+time[i]; mydown[i] = time[i]-dis[i]+n; } int lca[] = new int[m]; int v1[] = new int[m]; int lcas[] = new int[n]; int lcae[] = new int[n]; for(int i=0;i<m;++i){ int c1 = qy[u[i]][--oq[u[i]]]; int c2 = qy[v[i]][--oq[v[i]]]; if(c1!=-1){ lca[i] = c1; }else{ lca[i] = c2; } v1[i] = dis[u[i]]-(dis[lca[i]]<<1)+n; lcas[lca[i]]++; lcae[lca[i]]++; } vale = new int[n][]; vals = new int[n][]; valsrm = new int[n][]; valerm = new int[n][]; for(int i=0;i<n;++i){ vale[i] = new int[le[i]]; vals[i] = new int[ls[i]]; valsrm[i] = new int[lcas[i]]; valerm[i] = new int[lcae[i]]; } for(int i=0;i<m;++i) { valerm[lca[i]][--lcae[lca[i]]] = vale[v[i]][--le[v[i]]] = v1[i]; valsrm[lca[i]][--lcas[lca[i]]] = vals[u[i]][--ls[u[i]]] = dis[u[i]]; } dfs(0); for(int i=0;i<n;++i){ print(res[i]+" "); } } // int []h,to,ne,wt; long t1[]; // long t2[]; void update(long[] t,int i,long v){ for(;i<t.length;i+=(i&-i)){ t[i] += v; } } long get(long[] t,int i){ long s = 0; for(;i>0;i-=(i&-i)){ s += t[i]; } return s; } int equal_bigger(long t[],long v){ int s=0,p=0; for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) { if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){ v -= t[p+(1<<i)]; p |= 1<<i; } } return p+1; } long gcd(long a,long b){ return b==0?a: gcd(b,a%b);} InputStream is;PrintWriter out; void run() throws Exception {is = System.in; // is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in"); out = new PrintWriter(System.out);solve();out.flush();} private byte[] inbuf = new byte[2]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try {lenbuf = is.read(inbuf);} catch (IOException e) {throw new InputMismatchException();} if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++];} private boolean isSpaceChar(int c) {return !(c >= 33 && c <= 126);} private int skip() {int b;while((b = readByte()) != -1 && isSpaceChar(b));return b;} private double nd() {return Double.parseDouble(ns());} private char nc() {return (char) skip();} private char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;} private String ns() {int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[] ns(int n) {char[] buf = new char[n];int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b;b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p);} private String nline() {int b = skip();StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;} private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;} private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();return a;} private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = (num << 3) + (num << 1) + (b - '0'); else return minus ? -num : num; b = readByte();}} private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = num * 10 + (b - '0'); else return minus ? -num : num; b = readByte();}} void print(Object obj){out.print(obj);} void println(Object obj){out.println(obj);} void println(){out.println();} }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
eb8d99b936b5d996006235755db93fde
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws Exception { new Main().run();} int ct = 0; void add(int u,int v){ to[ct] = v; ne[ct] = h[u]; h[u] = ct++; } int stk[]; int pa[]; int h[],to[],ne[]; int dis[]; int time[]; int clock =0; int vale[][]; int vals[][]; int valerm[][]; int valsrm[][]; int ups[]; int res[]; int downs[]; int cnt_up[]; int cnt_down[]; int myup[]; int mydown[]; int fa[]; int root(int x){ if(fa[x]==x){ return x; }else{ int pt = x; while(pt!=fa[pt]){ pt = fa[pt]; } while(x!=fa[x]){ int cur = x; x = fa[x]; fa[cur] = pt; } return pt; } } void dfs(int rt){ int p = 0; stk[p++] = 0; while(p>0){ rt = stk[--p]; if(rt>=0){ stk[p++] = ~rt; cnt_up[rt] = ups[myup[rt]]; cnt_down[rt] = downs[mydown[rt]]; for(int i=h[rt];i!=-1;i=ne[i]){ int v =to[i]; if(v==pa[rt]) continue; stk[p++] = v; } }else{ rt = ~rt; for(int cao:vals[rt]){ ups[cao]++; } for(int cao:vale[rt]){ downs[cao]++; } for(int cao:valerm[rt]){ downs[cao]--; } res[rt] += ups[myup[rt]]-cnt_up[rt] + downs[mydown[rt]]-cnt_down[rt]; for(int cao:valsrm[rt]){ ups[cao]--; } } } } void solve(){ int q = ni(); Set<Long> st = new HashSet<>(); long prime[] = new long[35]; ot:for(int ii=0;ii<q;++ii){ st.clear(); long a = nl(); long m = nl(); long g = gcd(a,m); a /= g; m /= g; long low = a; long high = a+m-1; int p= 0 ; for(long ck=2;ck*ck<=m;++ck){ if(m%ck==0) { while (m % ck == 0) { m /= ck; } prime[p++] = ck; } } if(m>1){ prime[p++] = m; } long s = 1; long x = 1; for(int j=0;j<p;++j){ s *= (prime[j]-1); x *= prime[j]; } BigInteger z1 = BigInteger.valueOf(high).multiply(BigInteger.valueOf(s)).divide(BigInteger.valueOf(x)); BigInteger z2 = BigInteger.valueOf(low-1).multiply(BigInteger.valueOf(s)).divide(BigInteger.valueOf(x)); z1 = z1.subtract(z2); println(z1); // println(BigInteger.valueOf(high).subtract(BigInteger.valueOf(low)).add(BigInteger.ONE).subtract(z1)); } } int n = 0; void solve1() { n = ni(); int m = ni(); int es = 2*(n-1); pa = new int[n]; fa = new int[n]; ups = new int[2*n+1]; downs = new int[2*n+1]; h = new int[n]; dis = new int[n]; stk = new int[n]; Arrays.fill(h,-1); to = new int[es]; ne = new int[es]; time = new int[n]; res = new int[n]; myup = new int[n]; mydown = new int[n]; cnt_up = new int[n]; cnt_down = new int[n]; for(int i=0;i<n-1;++i){ int u = ni()-1; int v = ni()-1; add(u,v); add(v,u); } for(int i=0;i<n;++i){ time[i] = ni(); } int u[] = new int[m]; int v[] = new int[m]; int le[] = new int[n]; int ls[] = new int[n]; int q[] = new int[n]; for(int i=0;i<m;++i) { u[i] = ni() - 1; v[i] = ni() - 1; le[v[i]]++; ls[u[i]]++; q[v[i]]++; q[u[i]]++; } int qy[][] = new int[n][]; for(int i=0;i<n;++i){ qy[i] = new int[q[i]]; fa[i] = i; } int oq[] = q.clone(); for(int i=0;i<m;++i) { qy[u[i]][--q[u[i]]] = v[i]; qy[v[i]][--q[v[i]]] = u[i]; } int rt = 0; int p = 1; boolean vis[] = new boolean[n]; stk[0] = 0; while(p>0) { rt = stk[--p]; if(rt>=0) { stk[p++] = ~rt; for (int i = h[rt]; i != -1; i = ne[i]) { int vv = to[i]; if (pa[rt] == vv) continue; pa[vv] = rt; dis[vv] = 1 + dis[rt]; stk[p++] = vv; } }else{ rt = ~rt; vis[rt] = true; for (int i = 0 ;i < qy[rt].length;++i) { if (vis[qy[rt][i]]) { qy[rt][i] = root(qy[rt][i]); }else{ qy[rt][i] = -1; } } fa[rt] = pa[rt]; } } for(int i=0;i<n;++i){ myup[i] = dis[i]+time[i]; mydown[i] = time[i]-dis[i]+n; } int lca[] = new int[m]; int v1[] = new int[m]; int lcas[] = new int[n]; int lcae[] = new int[n]; for(int i=0;i<m;++i){ int c1 = qy[u[i]][--oq[u[i]]]; int c2 = qy[v[i]][--oq[v[i]]]; if(c1!=-1){ lca[i] = c1; }else{ lca[i] = c2; } v1[i] = dis[u[i]]-(dis[lca[i]]<<1)+n; lcas[lca[i]]++; lcae[lca[i]]++; } vale = new int[n][]; vals = new int[n][]; valsrm = new int[n][]; valerm = new int[n][]; for(int i=0;i<n;++i){ vale[i] = new int[le[i]]; vals[i] = new int[ls[i]]; valsrm[i] = new int[lcas[i]]; valerm[i] = new int[lcae[i]]; } for(int i=0;i<m;++i) { valerm[lca[i]][--lcae[lca[i]]] = vale[v[i]][--le[v[i]]] = v1[i]; valsrm[lca[i]][--lcas[lca[i]]] = vals[u[i]][--ls[u[i]]] = dis[u[i]]; } dfs(0); for(int i=0;i<n;++i){ print(res[i]+" "); } } // int []h,to,ne,wt; long t1[]; // long t2[]; void update(long[] t,int i,long v){ for(;i<t.length;i+=(i&-i)){ t[i] += v; } } long get(long[] t,int i){ long s = 0; for(;i>0;i-=(i&-i)){ s += t[i]; } return s; } int equal_bigger(long t[],long v){ int s=0,p=0; for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) { if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){ v -= t[p+(1<<i)]; p |= 1<<i; } } return p+1; } long gcd(long a,long b){ return b==0?a: gcd(b,a%b);} InputStream is;PrintWriter out; void run() throws Exception {is = System.in; // is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in"); out = new PrintWriter(System.out);solve();out.flush();} private byte[] inbuf = new byte[2]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try {lenbuf = is.read(inbuf);} catch (IOException e) {throw new InputMismatchException();} if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++];} private boolean isSpaceChar(int c) {return !(c >= 33 && c <= 126);} private int skip() {int b;while((b = readByte()) != -1 && isSpaceChar(b));return b;} private double nd() {return Double.parseDouble(ns());} private char nc() {return (char) skip();} private char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;} private String ns() {int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[] ns(int n) {char[] buf = new char[n];int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b;b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p);} private String nline() {int b = skip();StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;} private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;} private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();return a;} private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = (num << 3) + (num << 1) + (b - '0'); else return minus ? -num : num; b = readByte();}} private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = num * 10 + (b - '0'); else return minus ? -num : num; b = readByte();}} void print(Object obj){out.print(obj);} void println(Object obj){out.println(obj);} void println(){out.println();} }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
bba37921b209e8c29b3cd8c448e9c987
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) throws Exception { new Main().run();} int ct = 0; void add(int u,int v){ to[ct] = v; ne[ct] = h[u]; h[u] = ct++; } int stk[]; int pa[]; int h[],to[],ne[]; int dis[]; int time[]; int clock =0; int vale[][]; int vals[][]; int valerm[][]; int valsrm[][]; int ups[]; int res[]; int downs[]; int cnt_up[]; int cnt_down[]; int myup[]; int mydown[]; int fa[]; int root(int x){ if(fa[x]==x){ return x; }else{ int pt = x; while(pt!=fa[pt]){ pt = fa[pt]; } while(x!=fa[x]){ int cur = x; x = fa[x]; fa[cur] = pt; } return pt; } } void dfs(int rt){ int p = 0; stk[p++] = 0; while(p>0){ rt = stk[--p]; if(rt>=0){ stk[p++] = ~rt; cnt_up[rt] = ups[myup[rt]]; cnt_down[rt] = downs[mydown[rt]]; for(int i=h[rt];i!=-1;i=ne[i]){ int v =to[i]; if(v==pa[rt]) continue; stk[p++] = v; } }else{ rt = ~rt; for(int cao:vals[rt]){ ups[cao]++; } for(int cao:vale[rt]){ downs[cao]++; } for(int cao:valerm[rt]){ downs[cao]--; } res[rt] += ups[myup[rt]]-cnt_up[rt] + downs[mydown[rt]]-cnt_down[rt]; for(int cao:valsrm[rt]){ ups[cao]--; } } } } void solve(){ int q = ni(); ot:for(int ii=0;ii<q;++ii){ long a = nl(); long m = nl(); m /= gcd(a,m); BigInteger z1 = BigInteger.valueOf(m); long s = 1; long x = 1; for(long ck=2;ck*ck<=m;++ck){ if(m%ck==0) { while (m % ck == 0) { m /= ck; } s *= ck-1; x *= ck; } } if(m>1){ s *= m-1; x *= m; } println(z1.multiply(BigInteger.valueOf(s)).divide(BigInteger.valueOf(x))); } } int n = 0; void solve1() { n = ni(); int m = ni(); int es = 2*(n-1); pa = new int[n]; fa = new int[n]; ups = new int[2*n+1]; downs = new int[2*n+1]; h = new int[n]; dis = new int[n]; stk = new int[n]; Arrays.fill(h,-1); to = new int[es]; ne = new int[es]; time = new int[n]; res = new int[n]; myup = new int[n]; mydown = new int[n]; cnt_up = new int[n]; cnt_down = new int[n]; for(int i=0;i<n-1;++i){ int u = ni()-1; int v = ni()-1; add(u,v); add(v,u); } for(int i=0;i<n;++i){ time[i] = ni(); } int u[] = new int[m]; int v[] = new int[m]; int le[] = new int[n]; int ls[] = new int[n]; int q[] = new int[n]; for(int i=0;i<m;++i) { u[i] = ni() - 1; v[i] = ni() - 1; le[v[i]]++; ls[u[i]]++; q[v[i]]++; q[u[i]]++; } int qy[][] = new int[n][]; for(int i=0;i<n;++i){ qy[i] = new int[q[i]]; fa[i] = i; } int oq[] = q.clone(); for(int i=0;i<m;++i) { qy[u[i]][--q[u[i]]] = v[i]; qy[v[i]][--q[v[i]]] = u[i]; } int rt = 0; int p = 1; boolean vis[] = new boolean[n]; stk[0] = 0; while(p>0) { rt = stk[--p]; if(rt>=0) { stk[p++] = ~rt; for (int i = h[rt]; i != -1; i = ne[i]) { int vv = to[i]; if (pa[rt] == vv) continue; pa[vv] = rt; dis[vv] = 1 + dis[rt]; stk[p++] = vv; } }else{ rt = ~rt; vis[rt] = true; for (int i = 0 ;i < qy[rt].length;++i) { if (vis[qy[rt][i]]) { qy[rt][i] = root(qy[rt][i]); }else{ qy[rt][i] = -1; } } fa[rt] = pa[rt]; } } for(int i=0;i<n;++i){ myup[i] = dis[i]+time[i]; mydown[i] = time[i]-dis[i]+n; } int lca[] = new int[m]; int v1[] = new int[m]; int lcas[] = new int[n]; int lcae[] = new int[n]; for(int i=0;i<m;++i){ int c1 = qy[u[i]][--oq[u[i]]]; int c2 = qy[v[i]][--oq[v[i]]]; if(c1!=-1){ lca[i] = c1; }else{ lca[i] = c2; } v1[i] = dis[u[i]]-(dis[lca[i]]<<1)+n; lcas[lca[i]]++; lcae[lca[i]]++; } vale = new int[n][]; vals = new int[n][]; valsrm = new int[n][]; valerm = new int[n][]; for(int i=0;i<n;++i){ vale[i] = new int[le[i]]; vals[i] = new int[ls[i]]; valsrm[i] = new int[lcas[i]]; valerm[i] = new int[lcae[i]]; } for(int i=0;i<m;++i) { valerm[lca[i]][--lcae[lca[i]]] = vale[v[i]][--le[v[i]]] = v1[i]; valsrm[lca[i]][--lcas[lca[i]]] = vals[u[i]][--ls[u[i]]] = dis[u[i]]; } dfs(0); for(int i=0;i<n;++i){ print(res[i]+" "); } } // int []h,to,ne,wt; long t1[]; // long t2[]; void update(long[] t,int i,long v){ for(;i<t.length;i+=(i&-i)){ t[i] += v; } } long get(long[] t,int i){ long s = 0; for(;i>0;i-=(i&-i)){ s += t[i]; } return s; } int equal_bigger(long t[],long v){ int s=0,p=0; for(int i=Integer.numberOfTrailingZeros(Integer.highestOneBit(t.length));i>=0;--i) { if(p+(1<<i)< t.length && s + t[p+(1<<i)] < v){ v -= t[p+(1<<i)]; p |= 1<<i; } } return p+1; } long gcd(long a,long b){ return b==0?a: gcd(b,a%b);} InputStream is;PrintWriter out; void run() throws Exception {is = System.in; // is = new FileInputStream("C:\\Users\\Luqi\\Downloads\\P1600_2.in"); out = new PrintWriter(System.out);solve();out.flush();} private byte[] inbuf = new byte[2]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try {lenbuf = is.read(inbuf);} catch (IOException e) {throw new InputMismatchException();} if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++];} private boolean isSpaceChar(int c) {return !(c >= 33 && c <= 126);} private int skip() {int b;while((b = readByte()) != -1 && isSpaceChar(b));return b;} private double nd() {return Double.parseDouble(ns());} private char nc() {return (char) skip();} private char ncc() {int b;while((b = readByte()) != -1 && !(b >= 32 && b <= 126));return (char)b;} private String ns() {int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[] ns(int n) {char[] buf = new char[n];int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b;b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p);} private String nline() {int b = skip();StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b) || b == ' ') { sb.appendCodePoint(b);b = readByte(); } return sb.toString();} private char[][] nm(int n, int m) {char[][] a = new char[n][];for (int i = 0; i < n; i++) a[i] = ns(m);return a;} private int[] na(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = ni();return a;} private long[] nal(int n) { long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nl();return a;} private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = (num << 3) + (num << 1) + (b - '0'); else return minus ? -num : num; b = readByte();}} private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')){}; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') num = num * 10 + (b - '0'); else return minus ? -num : num; b = readByte();}} void print(Object obj){out.print(obj);} void println(Object obj){out.println(obj);} void println(){out.println();} }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
8a3a0d24b2d292768c0442d7088b6f49
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.ArrayList; public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { solve(); } }, "1", 1 << 26).start(); } static void solve () { FastReader fr =new FastReader(); PrintWriter op =new PrintWriter(System.out); int t =fr.nextInt() ,j ; long a ,m ,dm ,g ,i ; ArrayList<Long> prm =new ArrayList<>() ; while (t-- > 0) { a =fr.nextLong() ; m =fr.nextLong() ; g =gcd (m,a) ; a/=g ; m/=g ; dm =m ; if ((m&1l)==0l) { prm.add(2l) ; while ((m&1l)==0l) m>>=1 ; } for (i =3l ; i*i<=m ; i+=2l) { if ((m%i) == 0l) { prm.add(i) ; while ((m%i) == 0l) m/=i ; } } if (m>2l) prm.add(m) ; for (j =0 ; j<prm.size() ; ++j) dm -= (dm/prm.get(j)) ; op.println(dm) ; prm.clear() ; } op.flush(); op.close(); } static long gcd (long num , long den) { return (num%den == 0l ? den : gcd (den,num%den)) ; } 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(); } String nextLine() { String str =""; try { str =br.readLine(); } catch(IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()) ; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
e06ed1cceb7cbf76826adab30eb58a65
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Map; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Main implements Runnable{ static final long MAX = 464897L; static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<26).start(); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a,long b) { return (a*b)/gcd(a,b); } int maxn = 1000005; long MOD = 1000000007; long prime = 29; static class Node implements Comparable<Node>{ int destination; long weight; Node(int destination,long weight){ this.destination = destination; this.weight = weight; } Node(){ } @Override public int compareTo(Node n) { return Long.signum(weight - n.weight); } } static ArrayList<Integer> adj[]; boolean[][] ans = new boolean[205][205]; public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t = sc.nextInt(); while(t-- > 0) { long a = sc.nextLong(); long m = sc.nextLong(); long ans = gcd(a,m); TreeMap<Long,Integer> fgcd = fun(ans); TreeMap<Long,Integer> mgcd = fun(m); ArrayList<Long> ar = new ArrayList(); for(Long key: mgcd.keySet()) { if(fgcd.containsKey(key)) { if(fgcd.get(key) != mgcd.get(key)) { ar.add(key); } }else { ar.add(key); } } //w.println(ar); long rem = m/ans; long fans = 0; for(int i = 0;i < (1 << ar.size());i++) { long prod = 1; int count = 0; for(int j = 0;j < ar.size();j++) { if(((1L<< j) & i) != 0) { count++; prod = prod * ar.get(j); } } if(count % 2 == 0) { fans += rem/prod; }else { fans -= rem/prod; } } w.println(fans); } w.close(); } TreeMap<Long,Integer> fun(long ans){ TreeMap<Long,Integer> fgcd = new TreeMap(); for(int i = 2;i <= MAXN;i++) { int count = 0; while(ans % i == 0) { count++; ans = ans/i; } if(count != 0) { fgcd.put((long)i,count); } } if(ans != 1) { fgcd.put(ans,1); } return fgcd; } static long power(long a,long b,long MOD) { long ans = 1; a = a % MOD; while(b != 0) { if(b % 2 == 1) { ans = (ans * a) % MOD; } a = (a * a) % MOD; b = b/2; } return ans; } class Equal implements Comparable<Equal>{ Character a; int b; public Equal(char a,int b){ this.a = a; this.b = b; } @Override public int compareTo(Equal an) { // TODO Auto-generated method stub return this.a.compareTo(an.a); } } static final int MAXN = 100001; class Pair implements Comparable<Pair>{ long a; long b; long c; Pair(long a,long b,long c){ this.b = b; this.a = a; this.c = c; } public boolean equals(Object o) { Pair p = (Pair)o; return this.a == p.a && this.b == p.b && this.c == p.c; } public int hashCode(){ return Long.hashCode(a)*27 + Long.hashCode(b)*31; } public int compareTo(Pair p) { return Long.compare(this.a,p.a); } } class Pair3 implements Comparable<Pair3>{ int a; int b; int c; Pair3(int a,int b,int c){ this.b = b; this.a = a; this.c = c; } public boolean equals(Object o) { Pair3 p = (Pair3)o; return this.a == p.a && this.b == p.b && this.c == p.c; } public int hashCode(){ return Long.hashCode(a)*27 + Long.hashCode(b)*31; } public int compareTo(Pair3 p) { return Long.compare(this.b,p.b); } } class Pair2 implements Comparable<Pair2>{ int a; int b; int c; Pair2(int a,int b,int c){ this.b = b; this.a = a; this.c = c; } public boolean equals(Object o) { Pair2 p = (Pair2)o; return this.a == p.a && this.b == p.b && this.c == p.c; } public int hashCode(){ return Long.hashCode(a)*27 + Long.hashCode(b)*31 + Long.hashCode(c)*3; } public int compareTo(Pair2 p) { return Long.compare(p.a,this.a); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
320cac7c47354e0dfeeac03333ae74a6
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { // taken from // https://www.geeksforgeeks.org/eulers-totient-function/ static long gcd(long a, long b) { if(a==0 || b==0) return a+b; return gcd(b,a%b); } static long phi(long n) { long result = n; for (long p = 2; p * p <= n; ++p) { if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } } if (n > 1) result -= result / n; return result; } public static void main(String[] args)throws IOException { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); long a,m; for(int i=0;i<t;i++) { a=in.nextLong(); m=in.nextLong(); // out.println("gcd"+m/gcd(a,m)); out.println(phi(m/gcd(a,m))); } out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
0dc72a43178a44409612cb849d3d6499
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; public final class D1295 { private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static long phi(long n) { long answer = n; for (long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) { n /= i; } answer -= answer / i; } } if (n > 1) answer -= answer / n; return answer; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { long a = in.nextLong(); long m = in.nextLong(); System.out.println(phi(m / gcd(a, m))); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
2441ed4f5b45dbdac7dc8f2f574b33e0
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; public class Solution { public static void main(String []args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int not = Integer.parseInt(br.readLine()); while(not--!=0) { String input[] = br.readLine().split(" "); long a = Long.parseLong(input[0]); long m = Long.parseLong(input[1]); long x = gcd(a, m); long n = m / x; double result = n; if(n % 2 == 0) { while(n % 2 == 0) { n /= 2; } result *= (1.0 - (1.0 / (double)2)); } for (long p = 3; p * p <= n; p += 2) { if (n % p == 0) { while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (double)p)); } } if (n > 1) result *= (1.0 - (1.0 / (double) n)); System.out.println((long)result); } } static long gcd(long a, Long m) { if(m % a == 0) return a; return gcd(m%a, a); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
554588f4a9d25ef4fe194d687f04a5fc
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import sun.dc.pr.PRError; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Scanner; import java.util.Set; public class Main { static Scanner sc = null; static PrintWriter pw = null; // // // static { sc = new Scanner(System.in); // sc = new Scanner("3\n" + // "4 9\n" + // "5 10\n" + // "42 9999999967"); // sc = new Scanner("1\n" + // "4 6\n" + // "5 10\n" + // "42 9999999967"); } static int ni() { return sc.nextInt(); } private static long nl() { return sc.nextLong(); } static int[] nia(int l) { int[] r = new int[l]; for (int s = 0; s < l; s++) { r[s] = ni(); } return r; } public static void main(String[] args) { int n = ni(); while (n-- > 0) { sl(); } } private static void sl() { long r = nl(); long l = nl(); long gc = gcd(r, l); System.out.println(gcdCount(gc, l, r + l - 1) - gcdCount(gc, l, r - 1)); } private static long gcdCount(long gcValue, long checkWith, long toValIncl) { if (gcValue > 1) return gcdCount(1, checkWith / gcValue, toValIncl / gcValue); Divisors dd = new Divisors(checkWith); long ans = 0; for (Divisors m : dd.max1Divisors()) { long nn = m.numeric(); if (m.divisors.size() % 2 == 0) { ans += toValIncl / nn; } else { ans -= toValIncl / nn; } } return ans; } private static long gcd(long a, long b) { if (b > a) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } private static final class Divisors { Map<Long, Integer> divisors; public Divisors(long x) { this(cvt(x)); } public Divisors(Map<Long, Integer> cvt) { this.divisors = cvt; } private Divisors(long x, int ct) { divisors = new HashMap<>(); divisors.put(x, ct); } private long numeric() { long a = 1; for (Map.Entry<Long, Integer> x : divisors.entrySet()) { for (int s = 0; s < x.getValue(); s++) { a *= x.getKey(); } } return a; } private static Map<Long, Integer> cvt(long x) { Map<Long, Integer> divisors = new HashMap<>(); for (long s = 2; s * s <= x; s++) { if (x % s == 0) { divisors.merge(s, 1, Math::addExact); x /= s; s--; } } if (x > 1) { divisors.merge(x, 1, Math::addExact); } return divisors; } private Divisors gcd(Divisors other) { Map<Long, Integer> nn = new HashMap<>(); for (Map.Entry<Long, Integer> mm : other.divisors.entrySet()) { Integer th = divisors.get(mm.getKey()); if (th != null) { nn.put(mm.getKey(), Math.min(th, mm.getValue())); } } return new Divisors(nn); } private Divisors mul(Divisors other) { Map<Long, Integer> nn = new HashMap<>(divisors); for (Map.Entry<Long, Integer> mm : other.divisors.entrySet()) { nn.merge(mm.getKey(), mm.getValue(), Math::addExact); } return new Divisors(nn); } private Divisors div(Divisors other) { Map<Long, Integer> nn = new HashMap<>(divisors); for (Map.Entry<Long, Integer> mm : other.divisors.entrySet()) { if (mm.getValue().equals(nn.get(mm.getKey()))) { nn.remove(mm.getKey()); } else { nn.merge(mm.getKey(), -mm.getValue(), Math::addExact); } } return new Divisors(nn); } private List<Divisors> max1Divisors() { List<Divisors> list = new ArrayList<>(); list.add(new Divisors(1)); for (Map.Entry<Long, Integer> x : divisors.entrySet()) { int ol = list.size(); Divisors cur = new Divisors(x.getKey(), 1); for (int l = 0; l < ol; l++) { list.add(list.get(l).mul(cur)); } } return list; } private List<Divisors> divisors() { List<Divisors> list = new ArrayList<>(); list.add(new Divisors(1)); for (Map.Entry<Long, Integer> x : divisors.entrySet()) { int ol = list.size(); for (int t = 1; t <= x.getValue(); t++) { Divisors cur = new Divisors(x.getKey(), t); for (int l = 0; l < ol; l++) { list.add(list.get(l).mul(cur)); } } } return list; } @Override public String toString() { return divisors.toString(); } } private static long fact(int n) { if (n <= 1) return 1; return n * fact(n - 1); } private static Node[] func = new Node[26]; private static void solve(int csn) { int r = ni(); int c = ni(); int d = ni(); int e = ni(); boolean[][] grid = new boolean[r][c]; for (int s = 0; s < r; s++) { String k = sc.next(); for (int l = 0; l < c; l++) { grid[s][l] = k.charAt(l) == '#'; } } for (int t = 0; t < d; t++) { String l = sc.next(); func[l.charAt(0) - 'A'] = Node.parse(l.substring(2)); } sc.nextLine(); // System.out.println("--------------------"); for (int p = 0; p < e; p++) { String pos = sc.nextLine(); String[] x = pos.split(" "); int ir = Integer.parseInt(x[0]) - 1; int ic = Integer.parseInt(x[1]) - 1; int dir = "nwse".indexOf(x[2].charAt(0)); State state = new State(grid, ir, ic, dir); Node comm = Node.parse(sc.nextLine()); if (comm.execute(state)) { // System.out.print("Answer -- "); System.out.println(state); } else { // System.out.print("Answer -- "); System.out.println("inf"); } } } private static final class State { private final boolean[][] grid; int[] rd = new int[]{-1, 0, 1, 0}; int[] cd = new int[]{0, -1, 0, 1}; // "nesw" int direction; int posR; int posC; public char cDirection() { return "nwse".charAt(direction); } public State(boolean[][] grid, int ir, int ic, int dir) { direction = dir; posR = ir; posC = ic; this.grid = grid; } private boolean canMove() { int[] nm = nm(); return !(nm[0] < 0 || nm[1] < 0 || nm[0] >= grid.length || nm[1] >= grid[0].length || grid[nm[0]][nm[1]]); } private int[] nm() { return new int[]{posR + rd[direction], posC + cd[direction]}; } private void move() { if (canMove()) { int[] nm = nm(); posR = nm[0]; posC = nm[1]; } } private void toLeft() { direction = (direction + 1) % 4; } @Override public String toString() { return (posR + 1) + " " + (posC + 1) + " " + cDirection(); } public Integer toKey() { return posR + posC * 40 + direction * 1600; } public void fromKey(int ff) { this.posR = ff % 40; this.direction = ff / 1600; this.posC = (ff % 1600) / 40; } } private static final int STATE_SIZE = 1600 * 4 + 1; private static abstract class Node { private int[] statec = new int[STATE_SIZE]; Node() { Arrays.fill(statec, -2); } static Node parse(String s) { if (s.length() == 0) { return Noop.INSTANCE; } if (s.charAt(0) == 'm') { return new Composite(new Move(), parse(s.substring(1))); } if (s.charAt(0) == 'l') { return new Composite(new Left(), parse(s.substring(1))); } if (s.charAt(0) >= 'A' && s.charAt(0) <= 'Z') { return new Composite(new ProcCall(s.charAt(0)), parse(s.substring(1))); } if (s.charAt(0) == 'u') { Condition condition = new Condition(s.charAt(1)); int end = posValidPar(s.substring(2)); Node f = parse(s.substring(3, 2 + end)); Node next = parse(s.substring(end + 3)); return new Composite(new Loop(condition, f), next); } if (s.charAt(0) == 'i') { Condition condition = new Condition(s.charAt(1)); int first = posValidPar(s.substring(2)); Node t = parse(s.substring(3, 2 + first)); s = s.substring(first + 3); int second = posValidPar(s); Node f = parse(s.substring(1, second)); Node next = parse(s.substring(second + 1)); If ct = new If(condition, t, f); return new Composite(ct, next); } throw new IllegalStateException(s); } public final boolean execute(State state) { if (this instanceof Left || this instanceof Move) { return executeInt(state); } int key = state.toKey(); int ff = statec[key]; if (ff == -2) { if (executeInt(state)) { statec[key] = state.toKey(); } else { statec[key] = -1; } } else { state.fromKey(ff); } return statec[key] != -1; } public abstract boolean executeInt(State state); } private static int posValidPar(String x) { assert x.charAt(0) == '('; int s = 0; for (int l = 0; l < x.length(); l++) { if (x.charAt(l) == '(') { s++; } if (x.charAt(l) == ')') { s--; } if (s == 0) { return l; } } throw new IllegalStateException(x); } private static class Composite extends Node { private List<Node> nodes = new ArrayList<>(); public Composite(Node one, Node two) { nodes.addAll(l(one)); nodes.addAll(l(two)); } private List<Node> l(Node two) { if (two instanceof Noop) { return Collections.emptyList(); } if (two instanceof Composite) { return ((Composite) two).nodes; } return Collections.singletonList(two); } public boolean executeInt(State state) { for (Node n : nodes) { if (!n.execute(state)) { return false; } } return true; } } private static class Noop extends Node { private static Noop INSTANCE = new Noop(); @Override public boolean executeInt(State state) { return true; } } private static class Move extends Node { @Override public boolean executeInt(State state) { state.move(); return true; } } private static class Loop extends Node { private Condition c; private Node t; public Loop(Condition condition, Node f) { this.c = condition; this.t = f; } @Override public boolean executeInt(State state) { boolean[] vis = new boolean[STATE_SIZE]; while (!c.match(state)) { Integer l = state.toKey(); if (vis[l] || !t.execute(state)) { return false; } vis[l] = true; } return true; } } private static class If extends Node { private Condition c; private Node t; private Node f; public If(Condition condition, Node t, Node f) { this.c = condition; this.t = t; this.f = f; } @Override public boolean executeInt(State state) { if (c.match(state)) { return t.execute(state); } else { return f.execute(state); } } } private static class Left extends Node { @Override public boolean executeInt(State state) { state.toLeft(); return true; } } private static class ProcCall extends Node { private static final boolean[][] keys = new boolean[26][STATE_SIZE]; private char proc; public ProcCall(char charAt) { proc = charAt; } @Override public boolean executeInt(State state) { Integer k = state.toKey(); if (keys[proc - 'A'][k]) { return false; } keys[proc - 'A'][k] = true; boolean ans = func[proc - 'A'].execute(state); keys[proc - 'A'][k] = false; return ans; } } private static final class Condition { private char type; public Condition(char charAt) { type = charAt; } private boolean match(State c) { if ('b' == type) { return !c.canMove(); } return type == c.cDirection(); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
32ea8a186686f62086beccc532f93d25
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; import java.util.List; public class Main { static int mod = (int) 1e9 + 7; public static void main(String[] args){ FastReader sc = new FastReader(); StringBuilder sb=new StringBuilder(); int t=sc.nextInt(); while(t-->0) { long a=sc.nextLong(),m=sc.nextLong(); a=m/gcd(a,m); sb.append(eulerT(a)+"\n"); } System.out.println(sb); } static long gcd(long a,long b){ if(b==0)return a; else return gcd(b,a%b); } static long eulerT(long m){ long res=m; for(long i=2;i*i<=m;i++){ if(m%i==0){ res=res-res/i; while(m%i==0)m/=i; } } if(m>1)res-=res/m; 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\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
d897cfde532dbd1aa44706670d371cd9
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
//package main; import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class Submission { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter (System.out); //BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"))); //StringTokenizer st = new StringTokenizer(reader.readLine()," "); int T = Integer.parseInt(reader.readLine()); for (int i=0;i<T;i++) { StringTokenizer st = new StringTokenizer(reader.readLine()," "); BigInteger a = new BigInteger(st.nextToken()); BigInteger m = new BigInteger(st.nextToken()); BigInteger gcd = a.gcd(m); a = a.divide(gcd); m = m.divide(gcd); long newm = Long.parseLong(m.toString()); //out.print(newm+" "); int[] primecount = new int[100001]; for (long x=2;x<=100000;x++) { while (newm%x==0) { newm=newm/x; primecount[(int)x]++; } } long answer=1; if (newm>1) answer=newm-1; for (int x=2;x<100001;x++) { if (primecount[x]>0) answer=answer*(x-1); if (primecount[x]>1) for (int j=0;j<primecount[x]-1;j++) answer=answer*x; } out.println(answer); } reader.close(); out.close(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
3ae7ce1867d0c0fc1bf154b43c897607
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static void execute(ContestReader reader, PrintWriter out) { int t = reader.nextInt(); for (int i = 0; i < t; i++) { long a = reader.nextLong(); long m = reader.nextLong(); out.println(new Solver(a, m).solve()); } } public static void main(String[] args) { ContestReader reader = new ContestReader(System.in); PrintWriter out = new PrintWriter(System.out); execute(reader, out); out.flush(); } } class Solver { final long a, m; long g; List<Long> factors; Solver(long a, long m) { this.a = a; this.m = m; } private long gcd(long x, long y) { return x % y == 0 ? y : gcd(y, x % y); } private long count(long x) { long result = 0; for (int bitset = 0; bitset < (1 << factors.size()); bitset++) { long v = g; int sign = 1; for (int i = 0; i < factors.size(); i++) { if (((bitset >> i) & 1) == 1) { v *= factors.get(i); sign *= -1; } } result += sign * (x / v); } return result; } public long solve() { g = gcd(a, m); factors = new ArrayList<>(); { long tempM = m / g; for (long p = 2; p * p <= tempM; p++) { if (tempM % p != 0) { continue; } while (tempM % p == 0) { tempM /= p; } factors.add(p); } if (tempM > 1) { factors.add(tempM); } } return count(a + m) - count(a); } } class ContestReader { private BufferedReader reader; private StringTokenizer tokenizer; ContestReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] next(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } return array; } public int[] nextInt(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextLong(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public double[] nextDouble(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } public char[] nextCharArray() { return next().toCharArray(); } public int[][] nextInt(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextInt(); } } return matrix; } public long[][] nextLong(int n, int m) { long[][] matrix = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextLong(); } } return matrix; } public double[][] nextDouble(int n, int m) { double[][] matrix = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextDouble(); } } return matrix; } public char[][] nextCharArray(int n) { char[][] matrix = new char[n][]; for (int i = 0; i < n; i++) { matrix[i] = next().toCharArray(); } return matrix; } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
9a3952e702dae5e1cbfdac82c928839c
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; public class Solution { public static void main(String []args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int not = Integer.parseInt(br.readLine()); while(not--!=0) { String input[] = br.readLine().split(" "); long a = Long.parseLong(input[0]); long m = Long.parseLong(input[1]); long x = gcd(a, m); long n = m / x; double result = n; if(n % 2 == 0) { while(n % 2 == 0) { n /= 2; } result *= (1.0 - (1.0 / (double)2)); } for (long p = 3; p * p <= n; p += 2) { if (n % p == 0) { while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (double)p)); } } if (n > 1) result *= (1.0 - (1.0 / (double) n)); System.out.println((long)result); } } static long gcd(long a, Long m) { if(m % a == 0) return a; return gcd(m%a, a); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
28e992df9c88bb1a275cd241582eb014
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeSet; /** * */ public class TaskD { public static void main(String[] arg) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int t=0;t<T;t++){ final long a = in.nextLong(); final long m = in.nextLong(); final long mI=m/gcd(a,m); out.printf("%d%n", mathSol(a,m)); } out.close(); in.close(); } private static long mathSol(final long a, final long m){ long m1=m/gcd(a, m); return phi(m1); } private static long phi( long n){ long result =n; for(long p=2;p*p<=n;p++){ if(n%p == 0){ while(n%p==0){ n/=p; } result-=result/p; } } if(n >1){ result-=result/n; } return result; } private static long gcd(final long a, final long m) { if(m==0){ return a; } return gcd(m, a%m); } private static long bf(final long a, final long m){ final long gcd = gcd(a, m); long count = 1; for(long i=a+1;i<a+m;i++){ final long ngcd = gcd(i,m); if(ngcd==gcd){ count++; } } return count; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } 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()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
9ae18070466ebef43214a4589a9cdee6
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; public class HelloWorld{ public static void main(String []args){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { long a=sc.nextLong(); long m=sc.nextLong(); long g=gcd(a,m); long x=m/g; HashSet<Long> set=new HashSet(); f(set,x); long ans=x; Iterator itr=set.iterator(); while(itr.hasNext()) { long k=(long)itr.next(); ans=(ans-ans/k); } System.out.println(ans); } } public static long gcd(long x,long y) { return y==0?x:gcd(y,x%y); } public static void f(HashSet<Long> set,long x) { for(long p=2;p*p<=x;p++) { if(x%p==0) { set.add(p); while(x%p==0) x/=p; } } if(x!=1) set.add(x); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
33ca3a7a4f6e58b9bf45f4876e014e28
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.*; public class D implements Runnable { boolean judge = false; FastReader scn; PrintWriter out; String INPUT = ""; void solve() { int t = scn.nextInt(); while (t-- > 0) { long a = scn.nextLong(), m = scn.nextLong(); long g = gcd(a, m); m /= g; long[] pr = new long[20]; int pos = 0; long x = m; for(long i = 2; i * i <= x; i++) { if(x % i == 0) { while(x % i == 0) { x /= i; } pr[pos++] = i; } } if(x != 1) { pr[pos++] = x; } pr = Arrays.copyOf(pr, pos); long ans = 0; for(int mask = 1; mask < 1 << pos; mask++) { long num = 1; for(int i = 0; i < pos; i++) { if(((mask >> i) & 1) == 1) { num *= pr[i]; } } int mul = Integer.bitCount(mask) % 2 == 1 ? 1 : -1; ans += mul * (m / num); } out.println(m - ans); } } long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public void run() { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge; out = new PrintWriter(System.out); scn = new FastReader(oj); solve(); out.flush(); if (!oj) { System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } } public static void main(String[] args) { new Thread(null, new D(), "Main", 1 << 26).start(); } class FastReader { InputStream is; public FastReader(boolean onlineJudge) { is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes()); } byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } double nextDouble() { return Double.parseDouble(next()); } char nextChar() { return (char) skip(); } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char[] next(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } int nextInt() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } long nextLong() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } char[][] nextMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = next(m); return map; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } int[][] next2DInt(int n, int m) { int[][] arr = new int[n][]; for (int i = 0; i < n; i++) { arr[i] = nextIntArray(m); } return arr; } long[][] next2DLong(int n, int m) { long[][] arr = new long[n][]; for (int i = 0; i < n; i++) { arr[i] = nextLongArray(m); } return arr; } int[] shuffle(int[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); int c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } long[] shuffle(long[] arr) { Random r = new Random(); for (int i = 1, j; i < arr.length; i++) { j = r.nextInt(i); long c = arr[i]; arr[i] = arr[j]; arr[j] = c; } return arr; } int[] uniq(int[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); int[] rv = new int[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } long[] uniq(long[] arr) { arr = scn.shuffle(arr); Arrays.sort(arr); long[] rv = new long[arr.length]; int pos = 0; rv[pos++] = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] != arr[i - 1]) { rv[pos++] = arr[i]; } } return Arrays.copyOf(rv, pos); } int[] reverse(int[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } long[] reverse(long[] arr) { int l = 0, r = arr.length - 1; while (l < r) { arr[l] = arr[l] ^ arr[r]; arr[r] = arr[l] ^ arr[r]; arr[l] = arr[l] ^ arr[r]; l++; r--; } return arr; } int[] compress(int[] arr) { int n = arr.length; int[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } long[] compress(long[] arr) { int n = arr.length; long[] rv = Arrays.copyOf(arr, n); rv = uniq(rv); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(rv, arr[i]); } return arr; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
3710af47a12dabf4e922256dd4be36ff
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
// package Quarantine; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class SameGCDs { public static void main(String[] args)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int test=Integer.parseInt(br.readLine()); StringTokenizer st; StringBuilder print=new StringBuilder(); int seive[]=new int[100001]; ArrayList<Integer> primes=new ArrayList<>(); for(int i=2;i<=100000;i++){ if(seive[i]==0){ primes.add(i); for(int j=2*i;j<=100000;j+=i){ seive[j]=1; } } } while (test--!=0){ st=new StringTokenizer(br.readLine()); long a=Long.parseLong(st.nextToken()); long m=Long.parseLong(st.nextToken()); long gcd=gcd(a,m); m=m/gcd; print.append(getphi(m,primes)+"\n"); } System.out.print(print.toString()); } public static long getphi(long num,ArrayList<Integer> primes){ long ans=num; for(int p:primes){ if(num==1){ break; } if(num%p==0){ ans=ans-ans/p; while (num%p==0){ num/=p; } } } if(num!=1){ ans=ans-ans/num; } return ans; } public static long gcd(long a,long b){ if(b==0){ return a; } return gcd(b,a%b); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
94016ac4a133fda4868ba13212456e66
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { nextTest: for (int testi = 0, ntests = in.nextInt(); testi < ntests; testi++) { long a = in.nextLong(); long m = in.nextLong(); long g = Algorithms.gcd(a, m); long k2 = m / g; long ans = Algorithms.eulerTotientFunction(k2); out.println(ans); } } } static class Algorithms { public static long gcd(long a, long b) { while (b != 0) { long old_b = b; b = a % b; a = old_b; } return a; } public static long eulerTotientFunction(long n) { long res = n; long i = 2; while (i * i <= n) { if (n % i == 0) { while (n % i == 0) n /= i; res -= res / i; } i++; } if (n > 1) res -= res / n; return res; } } static class InputReader { final InputStream is; final byte[] buffer = new byte[1024]; int curCharIdx; int nChars; public InputReader(InputStream is) { this.is = is; } public int read() { if (curCharIdx >= nChars) { try { curCharIdx = 0; nChars = is.read(buffer); if (nChars == -1) return -1; } catch (IOException e) { throw new RuntimeException(e); } } return buffer[curCharIdx++]; } public int nextInt() { int sign = 1; int c = skipDelims(); if (c == '-') { sign = -1; c = read(); if (isDelim(c)) throw new RuntimeException("Incorrect format"); } int val = 0; while (c != -1 && !isDelim(c)) { if (!isDigit(c)) throw new RuntimeException("Incorrect format"); val = 10 * val + (c - '0'); c = read(); } return val * sign; } public long nextLong() { int sign = 1; int c = skipDelims(); if (c == '-') { sign = -1; c = read(); if (isDelim(c)) throw new RuntimeException("Incorrect format"); } long val = 0; while (c != -1 && !isDelim(c)) { if (!isDigit(c)) throw new RuntimeException("Incorrect format"); val = 10L * val + (c - '0'); c = read(); } return val * sign; } private final int skipDelims() { int c = read(); while (isDelim(c)) { c = read(); } return c; } private static boolean isDelim(final int c) { return c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f'; } private static boolean isDigit(final int c) { return '0' <= c && c <= '9'; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
a6d433264e7da2c9ff21b062f690a0b9
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.*; public class D implements Runnable { FastReader sc; PrintWriter out; long mod = (long)1e9 + 7; long gcd(long a,long b) { if(a == 0) return b; if(b == 0) return a; return gcd(b,a%b); } long phi(long n) { long result = n; for(long i = 2; i*i <= n; i++) { if(n%i == 0) { while(n%i == 0) { n /= i; } result -= result/i; } } if(n > 1) { result -= result/n; } return result; } void solve() { int tc = sc.ni(); outer: while(tc-- > 0) { long a = sc.nl(); long m = sc.nl(); long g = gcd(a,m); long p = m/g; long ans = phi(p); out.println(ans); } } class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y) { this.x = x; this.y = y; } public int compareTo(Pair o) { if(x != o.x) { if(x > o.x) return 1; //increasing order else return -1; } else { if(y > o.y) return 1; //increasing order else return -1; } } } public void run() { out = new PrintWriter(System.out); sc = new FastReader(); solve(); out.flush(); } public static void main(String[] args) { new Thread(null, new D(), "Main", 1 << 28).start(); } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nln() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nia(int n) { int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = ni(); return ar; } long[] nla(int n) { long[] ar = new long[n]; for (int i = 0; i < n; i++) ar[i] = nl(); return ar; } int[][] nim(int n, int m) { int[][] ar = new int[n][]; for (int i = 0; i < n; i++) { ar[i] = nia(m); } return ar; } long[][] nlm(int n, int m) { long[][] ar = new long[n][]; for (int i = 0; i < n; i++) { ar[i] = nla(m); } return ar; } String reverse(String s) { StringBuilder r = new StringBuilder(s); return r.reverse().toString(); } void merge1(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i]<=R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void sort1(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort1(arr, l, m); sort1(arr , m+1, r); merge1(arr, l, m, r); } } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
0dfeeffc6388026d154aa4d6161b7493
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.io.*; public class SameGCDs { public static void main(String[] args) throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(f.readLine()); while(t-->0){ StringTokenizer st = new StringTokenizer(f.readLine()); long a = Long.parseLong(st.nextToken()); long b = Long.parseLong(st.nextToken()); long gcd = gcd(a, b); a/=gcd; b/=gcd; out.println(phi(b)); } out.close(); } public static long gcd(long a, long b){ while(b != 0){ long temp = b; b = a % b; a = temp; } return a; } public static long phi(long n) { long result = n; for (long p = 2; p * p <= n; ++p) { if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } } if (n > 1) result -= result / n; return result; } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
80a117758354c161f559e61b6409edf7
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Main m = new Main(); // int count = 0; // long a = 1; // long b = 30; // long g = m.gcd(a, b); // // for (int i = 0; i <b; i++) { // if (m.gcd(b, a + i) == g) { // // count++; // } // } // System.out.println(count); m.func(new Scanner(System.in)); } long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } ArrayList<Integer> get_primes(int limit) { ArrayList<Integer> res = new ArrayList<>(); a: for (int i = 2; i <= limit; i++) { int temp = (int) Math.sqrt(i); for (int j = 2; j <= temp; j++) { if (i % j == 0) { continue a; } } res.add(i); } return res; } ArrayList<Long> get_factor(ArrayList<Integer> ps, long x) { ArrayList<Long> res = new ArrayList<>(); for (int y : ps) { boolean enter = false; while (x % y == 0) { enter = true; x /= y; } if (enter) { res.add((long) y); } if (x == 1) { return res; } } res.add(x); return res; } int time1(int x) { int count = 0; for (int i = 0; i < 32; i++) { if ((x & (1 << i)) != 0) { count++; } } return count; } long getprod(int x, ArrayList<Long> facs) { long res = 1; for (int i = 0; i < 32; i++) { if ((x & (1 << i)) != 0) { res *= facs.get(i); } } return res; } long count(long x, ArrayList<Long> facs) { long res = 0; int len = facs.size(); int s = (1 << len) - 1; for (int i = s; i > 0; i = (i - 1) & s) { int time_1 = time1(i); res += (time_1 % 2 == 0 ? x / (getprod(i, facs)) * -1 : x / (getprod(i, facs))); } return res; } void func(Scanner s) { int T = s.nextInt(); ArrayList<Integer> ps = get_primes(100000); for (int i = 0; i < T; i++) { long a = s.nextLong(); long b = s.nextLong(); long g = gcd(a, b); a /= g; b /= g; ArrayList<Long> facs = get_factor(ps, b); long temp = count(a + b - 1, facs) - count(a - 1, facs); System.out.println(b - temp); } } int[][] temp = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; } class Node { int gap; int be; public Node(int gap, int be) { this.gap = gap; this.be = be; } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
ad8dc702c356f3749b20ec510e65a9cd
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
//package learning; import java.util.*; import java.io.*; import java.lang.*; import java.text.*; import java.math.*; import java.util.regex.*; public class NitsLocal { static ArrayList<String> s1; static boolean[] prime; static int n = (int)1e7; static void sieve() { Arrays.fill(prime , true); prime[0] = prime[1] = false; for(int i = 2 ; i * i <= n ; ++i) { if(prime[i]) { for(int k = i * i; k<= n ; k+=i) { prime[k] = false; } } } } public static void main(String[] args) { InputReader sc = new InputReader(System.in); n *= 2; prime = new boolean[n + 1]; //sieve(); prime[1] = false; /* int n = sc.ni(); int k = sc.ni(); int []vis = new int[n+1]; int []a = new int[k]; for(int i=0;i<k;i++) { a[i] = i+1; } int j = k+1; int []b = new int[n+1]; for(int i=1;i<=n;i++) { b[i] = -1; } int y = n - k +1; int tr = 0; int y1 = k+1; while(y-- > 0) { System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); b[pos] = a1; for(int i=0;i<k;i++) { if(a[i] == pos) { a[i] = y1; y1++; } } } ArrayList<Integer> a2 = new ArrayList<>(); if(y >= k) { int c = 0; int k1 = 0; for(int i=1;i<=n;i++) { if(b[i] != -1) { c++; a[k1] = i; a2.add(b[i]); k1++; } if(c==k) break; } Collections.sort(a2); System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.println(); System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); int ans = -1; for(int i=0;i<a2.size();i++) { if(a2.get(i) == a1) { ans = i+1; break; } } System.out.println("!" + " " + ans); System.out.flush(); } else { int k1 = 0; a = new int[k]; for(int i=1;i<=n;i++) { if(b[i] != -1) { a[k1] = i; a2.add(b[i]); k1++; } } for(int i=1;i<=n;i++) { if(b[i] == -1) { a[k1] = i; k1++; if(k1==k) break; } } int ans = -1; while(true) { System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.println(); System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); int f = 0; if(b[pos] != -1) { Collections.sort(a2); for(int i=0;i<a2.size();i++) { if(a2.get(i) == a1) { ans = i+1; f = 1; System.out.println("!" + " " + ans); System.out.flush(); break; } } if(f==1) break; } else { b[pos] = a1; a = new int[k]; a2.add(a1); for(int i=1;i<=n;i++) { if(b[i] != -1) { a[k1] = i; a2.add(b[i]); k1++; } } for(int i=1;i<=n;i++) { if(b[i] == -1) { a[k1] = i; k1++; if(k1==k) break; } } } } } */ /* int t = sc.ni(); while(t-- > 0) { int n = sc.ni(); int []a = sc.nia(n); long sum = 0; hm = new HashMap<>(); int d = 0; for(int i=0;i<n;i++) { sum += (long) a[i]; if(sum < 0) { d = -1; } } l = 0; r = 0; f = 0; long temp = maxSubArraySum(a,n,sum); //w.println(temp + " " +f); if(f == -2) { w.println("NO"); } else { w.println("YES"); } } */ int t = sc.ni(); while(t-- > 0) { long a = sc.nl(); long m = sc.nl(); long res = 0; long gc = gcd(a,m); a /= gc; m /= gc; res = m; int f = 0; // w.println("="); for(long i=2;i*i<=m;i++) { if(m%i==0) { while(m%i==0) m /= i; res -= res/i; } } if(m > 1) { res -= res/m; } //w.println(res); w.println(res); } w.close(); } static int upperBound(ArrayList<Integer> a, int low, int high, int element){ while(low < high){ int middle = low + (high - low)/2; if(a.get(middle) > element) high = middle; else low = middle + 1; } return low; } static long func(long t,long e,long h,long a, long b) { if(e*a >= t) return t/a; else { return e + Math.min(h,(t-e*a)/b); } } public static int upperBound(int []arr,int n,int num) { int l = 0; int r = n; while(l < r) { int mid = (l+r)/2; if(num >= arr[mid]) { l = mid +1; } else r = mid; } return l; } public static int countSetBits(int number){ int count = 0; while(number>0){ ++count; number &= number-1; } return count; } static HashSet<Integer> fac; public static void primeFactors(int n) { // Print the number of 2s that divide n int t = 0; while (n%2==0) { fac.add(2); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i*i <= n; i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { fac.add(i); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) fac.add(n); } static long modexp(long x,long n,long M) { long power = n; long result=1; x = x%M; while(power>0) { if(power % 2 ==1) result=(result * x)%M; x=(x*x)%M; power = power/2; } return result; } static long modInverse(long A,long M) { return modexp(A,M-2,M); } static long gcd(long a,long b) { if(a==0) return b; else return gcd(b%a,a); } static class Temp{ int a; int b; int c; int d; Temp(int a,int b,int c,int d) { this.a = a; this.b = b; this.c =c; this.d = d; //this.d = d; } } static long sum1(int t1,int t2,int x,int []t) { int mid = (t2-t1+1)/2; if(t1==t2) return 0; else return sum1(t1,mid-1,x,t) + sum1(mid,t2,x,t); } static String replace(String s,int a,int n) { char []c = s.toCharArray(); for(int i=1;i<n;i+=2) { int num = (int) (c[i] - 48); num += a; num%=10; c[i] = (char) (num+48); } return new String(c); } static String move(String s,int h,int n) { h%=n; char []c = s.toCharArray(); char []temp = new char[n]; for(int i=0;i<n;i++) { temp[(i+h)%n] = c[i]; } return new String(temp); } public static int ip(String s){ return Integer.parseInt(s); } static class multipliers implements Comparator<Long>{ public int compare(Long a,Long b) { if(a<b) return 1; else if(b<a) return -1; else return 0; } } static class multipliers1 implements Comparator<Student>{ public int compare(Student a,Student b) { if(a.id<b.id) return -1; else if(b.id<a.id) return 1; else { if(a.b < b.b) return -1; else if(b.b<a.b) return 1; else return 0; //return 0; } } } // Java program to generate power set in // lexicographic order. static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { 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 ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static PrintWriter w = new PrintWriter(System.out); static class Student { int id; //int x; int b; //long z; Student(int id,int b) { this.id = id; //this.x = x; //this.s = s; this.b = b; // this.z = z; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
e239d9b4b1058c44d7a3eb28985ae947
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.*; public class D { public static void main(String[] args) { FastScanner sc = new FastScanner(); int T = sc.nextInt(); for(int _t = 0; _t < T; _t++) { long a = sc.nextLong(); long m = sc.nextLong(); long g = gcd(a, m); long x = m / g; long sqx = (long)Math.sqrt(x); long res = 1; for(long d = 2; d <= sqx; d++) { if(x % d == 0) { res *= (d-1); x /= d; while(x % d == 0) { res *= d; x /= d; } } } if(x > 1) { res *= (x-1); } System.out.println(res); } } static long gcd(long a, long b) { if(a < b) return gcd(b, a); else if(b == 0) return a; else return gcd(b, a % b); } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { reader = new BufferedReader(new InputStreamReader(System.in), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
2ca61b6f4e083ad9d6261f23b7e04686
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.lang.reflect.Array; import java.io.*; import java.math.*; import java.text.DecimalFormat; public class Prac{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { 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 ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static PrintWriter w = new PrintWriter(System.out); static long mod=998244353L,mod1=1000000007; static long gcd(long a,long b){ if(b==0){ return a; } else{ return gcd(b,a%b); } } static long lcm(long a,long b){ return (a*b)/gcd(a,b); } static long phi(long n) { double result = n; for (long p = 2; p * p <= n; ++p) { if (n % p == 0) { while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (double)p)); } } if (n > 1) result *= (1.0 - (1.0 / (double)n)); return (long)result; } public static void main (String[] args)throws IOException{ InputReader sc=new InputReader(System.in); int q=sc.ni(); while(q-->0){ long a=sc.nl(); long m=sc.nl(); long g=gcd(a,m); w.println(phi(m/g)); } w.close(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
ffc254cb1fc4ae36c5fe19190feeebae
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
/** * BaZ :D */ import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { static MyScanner scan; static PrintWriter pw; static long MOD = 1_000_000_007; static long INF = 1_000_000_000_000_000_000L; static long inf = 2_000_000_000; public static void main(String[] args) { new Thread(null, null, "BaZ", 1 << 27) { public void run() { try { solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static void solve() throws IOException { //initIo(true); initIo(false); StringBuilder sb = new StringBuilder(); int tt = ni(); while(tt-->0) { long a = nl(), b = nl(); long g = gcd(a,b); b/=g; long ans = b; for(long i=2;i*i<=b;i++) { if(b%i==0) { while(b%i==0) { b/=i; } ans-=ans/i; } } if(b>1) { ans-=ans/b; } pl(ans); } pw.flush(); pw.close(); } static long gcd(long a, long b) { if(b==0) { return a; } return gcd(b,a%b); } static void initIo(boolean isFileIO) throws IOException { scan = new MyScanner(isFileIO); if(isFileIO) { pw = new PrintWriter("/Users/amandeep/Desktop/output.txt"); } else { pw = new PrintWriter(System.out, true); } } static int ni() throws IOException { return scan.nextInt(); } static long nl() throws IOException { return scan.nextLong(); } static double nd() throws IOException { return scan.nextDouble(); } static String ne() throws IOException { return scan.next(); } static String nel() throws IOException { return scan.nextLine(); } static void pl() { pw.println(); } static void p(Object o) { pw.print(o+" "); } static void pl(Object o) { pw.println(o); } static void psb(StringBuilder sb) { pw.print(sb); } static void pa(String arrayName, Object arr[]) { pl(arrayName+" : "); for(Object o : arr) p(o); pl(); } static void pa(String arrayName, int arr[]) { pl(arrayName+" : "); for(int o : arr) p(o); pl(); } static void pa(String arrayName, long arr[]) { pl(arrayName+" : "); for(long o : arr) p(o); pl(); } static void pa(String arrayName, double arr[]) { pl(arrayName+" : "); for(double o : arr) p(o); pl(); } static void pa(String arrayName, char arr[]) { pl(arrayName+" : "); for(char o : arr) p(o); pl(); } static void pa(String listName, List list) { pl(listName+" : "); for(Object o : list) p(o); pl(); } static void pa(String arrayName, Object[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(Object o : arr[i]) p(o); pl(); } } static void pa(String arrayName, int[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(int o : arr[i]) p(o); pl(); } } static void pa(String arrayName, long[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(long o : arr[i]) p(o); pl(); } } static void pa(String arrayName, char[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(char o : arr[i]) p(o); pl(); } } static void pa(String arrayName, double[][] arr) { pl(arrayName+" : "); for(int i=0;i<arr.length;++i) { for(double o : arr[i]) p(o); pl(); } } static class MyScanner { BufferedReader br; StringTokenizer st; MyScanner(boolean readingFromFile) throws IOException { if(readingFromFile) { br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input.txt")); } else { br = new BufferedReader(new InputStreamReader(System.in)); } } String nextLine()throws IOException { return br.readLine(); } String next() throws IOException { if(st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
cab47d54715dfbd144beb1093c0271fc
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
/* Solution: If gcd(a, m) != 1, divide both the #s by that GCD to get the same problem, but with a GCD of 1. Now, just check how many #s from [a, a+m) are coprime with m to get your answer. m will never be 1 (a < m), so no need to worry about this case. To count the numbers that are coprime with x, find the prime factorization of x. Split the check up into coprime(1, a+m-1) - coprime(1, a-1). Then, use PIE to evaluate each of these. Coprime with 30 in range [1, 30] = 30 - # that are NOT coprime with 30 30 = 2*3*5 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 = 30/2 = 15 3 6 9 12 15 18 21 24 27 30 = 30 / 3 = 10 5 10 15 20 25 30 = 30 / 5 = 6 - 6 12 18 24 30 = 30 / 6 = 5 10 20 30 = 30 / 10 = 3 15 30 = 30 / 15 = 2 + 30 = 30 / 30 = 1 = 31 - 10 + 1 = 22 30 - 22 = 8 #s are coprime! Check: 1 7 11 13 17 19 23 29 = 8 #s are coprime Runtime: O(T * (log(m) + sqrt(m) + 2^(# prime factors of m))) This is fine, since the max # of prime factors of m <= 10^10 is 10. 2^10 = 1024, which is less than the sqrt(m) term. */ import java.util.*; import java.io.*; public class samegcds { int T; long A, M; samegcds(BufferedReader in, PrintWriter out) throws IOException { StringTokenizer st = new StringTokenizer(in.readLine()); T = Integer.parseInt(st.nextToken()); for (int i = 0; i < T; i++) { st = new StringTokenizer(in.readLine()); A = Long.parseLong(st.nextToken()); M = Long.parseLong(st.nextToken()); out.println(solve()); } } long solve() { // First, divide by gcd(A, M) to reduce problem into coprime counting long gcd = findGCD(A, M); A /= gcd; M /= gcd; // System.out.println("GCD: " + gcd); // Now, find the prime factorization of M ArrayList<Long> factors = new ArrayList<>(); long tempM = M; int sqrtM = (int) Math.sqrt(M); for (int i = 2; tempM != 1 && i < sqrtM + 1; i++) { if (tempM % i == 0) { factors.add((long) i); while (tempM % i == 0) tempM /= i; } } if (tempM != 1) factors.add(tempM); // System.out.println("Prime factors of " + M + ": " + factors); // Finally, count the # of coprime numbers return coprime(A, A + M - 1, factors); } long coprime(long a, long b, ArrayList<Long> factors) { // System.out.println("a = " + a + ", b = " + b + ", factors = " + factors); return coprimeFrom1(A + M - 1, factors) - coprimeFrom1(A - 1, factors); } long notCoprime; long coprimeFrom1(long a, ArrayList<Long> factorList) { factorArr = new long[factorList.size()]; for (int i = 0; i < factorList.size(); i++) factorArr[i] = factorList.get(i); // Use PIE to find the answer targetNum = a; notCoprime = 0; runPIE(0, 1, 0); return a - notCoprime; } long targetNum; long[] factorArr; void runPIE(int i, long n, int numUsed) { if (i == factorArr.length) { // Update notCoprime count if (numUsed == 0) return; else if (numUsed % 2 == 1) { // Add to notCoprime notCoprime += targetNum / n; } else { // Subtract from notCoprime notCoprime -= targetNum / n; } } else { // Continue PIE runPIE(i+1, n, numUsed); runPIE(i+1, n*factorArr[i], numUsed+1); } } long findGCD(long a, long b) { if (a == 0) return b; else return findGCD(b % a, a); } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); new samegcds(in, out); in.close(); out.close(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
62182f3e595ebe5f10c7d00c2f72f63f
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.math.*; import java.io.*; public class d { // no duplicates List<Long> prime_f(long n) { List<Long> primef = new ArrayList<>(); for(long i = 2 ; i * i <= n ; i++) { if(n % i == 0) { primef.add(i); while(n % i == 0) { n /= i; } } } if(n > 1) primef.add(n); return primef; } long solve(long bound, List<Long> primes) { if(bound == 0) return 0; long total = 0; for(int i = 0 ; i < (1 << primes.size()) ; i++) { long x = 1; for(int j = 0 ; j < primes.size() ; j++) if((i & (1 << j)) > 0) x *= primes.get(j); if(Integer.bitCount(i) % 2 == 0) total += (bound / x); else total -= (bound / x); } return total; } long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public d() { FS scan = new FS(); PrintWriter out = new PrintWriter(System.out); int tc = scan.nextInt(); for(int tt = 0 ; tt < tc ; tt++) { long a = scan.nextLong(); long m = scan.nextLong(); long g = gcd(a, m); long y = m / g; List<Long> primes = prime_f(y); long left = solve((a - 1) / g, primes); long right = solve((a + m - 1) / g, primes); out.println(right - left); } out.close(); } class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch(Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) throws Exception { new d(); } } /* */
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
d8e0a300baf4abec952d6db6879d25a1
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); DSameGCDs solver = new DSameGCDs(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } } static class DSameGCDs { public void solve(int testNumber, FastInput in, FastOutput out) { long a = in.readLong(); long m = in.readLong(); long g = GCDs.gcd(a, m); long ans = solve(a + m - 1, m, g); ans -= solve(a - 1, m, g); out.println(ans); } public long solve(long n, long x, long g) { long[] factors = Factorization.factorizeNumberPrime(x / g).toArray(); return dfs(factors, 0, 1, 0, n, g); } public long dfs(long[] factors, int i, long d, int cnt, long n, long g) { if (i == factors.length) { long contri = n / g / d; if (cnt % 2 == 1) { contri = -contri; } return contri; } return dfs(factors, i + 1, d * factors[i], cnt + 1, n, g) + dfs(factors, i + 1, d, cnt, n, g); } } static class Factorization { public static LongList factorizeNumberPrime(long x) { LongList ans = new LongList(); for (long i = 2; i * i <= x; i++) { if (x % i != 0) { continue; } ans.add(i); while (x % i == 0) { x /= i; } } if (x > 1) { ans.add(x); } return ans; } } static class GCDs { private GCDs() { } public static long gcd(long a, long b) { return a >= b ? gcd0(a, b) : gcd0(b, a); } private static long gcd0(long a, long b) { return b == 0 ? a : gcd0(b, a % b); } } static class FastInput { private final InputStream is; private StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public String next() { return readString(); } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } } static class SequenceUtils { public static boolean equal(long[] a, int al, int ar, long[] b, int bl, int br) { if ((ar - al) != (br - bl)) { return false; } for (int i = al, j = bl; i <= ar; i++, j++) { if (a[i] != b[j]) { return false; } } return true; } } static class FastOutput implements AutoCloseable, Closeable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput println(long c) { cache.append(c); println(); return this; } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class LongList implements Cloneable { private int size; private int cap; private long[] data; private static final long[] EMPTY = new long[0]; public LongList(int cap) { this.cap = cap; if (cap == 0) { data = EMPTY; } else { data = new long[cap]; } } public LongList(LongList list) { this.size = list.size; this.cap = list.cap; this.data = Arrays.copyOf(list.data, size); } public LongList() { this(0); } public void ensureSpace(int req) { if (req > cap) { while (cap < req) { cap = Math.max(cap + 10, 2 * cap); } data = Arrays.copyOf(data, cap); } } public void add(long x) { ensureSpace(size + 1); data[size++] = x; } public void addAll(long[] x, int offset, int len) { ensureSpace(size + len); System.arraycopy(x, offset, data, size, len); size += len; } public void addAll(LongList list) { addAll(list.data, 0, list.size); } public long[] toArray() { return Arrays.copyOf(data, size); } public String toString() { return Arrays.toString(toArray()); } public boolean equals(Object obj) { if (!(obj instanceof LongList)) { return false; } LongList other = (LongList) obj; return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1); } public int hashCode() { int h = 1; for (int i = 0; i < size; i++) { h = h * 31 + Long.hashCode(data[i]); } return h; } public LongList clone() { LongList ans = new LongList(); ans.addAll(this); return ans; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
c19d7d97b0d08b1a8abb942bfa02d889
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.io.*; public class Solution{ static PrintWriter out=new PrintWriter(System.out); public static void main (String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] input=br.readLine().trim().split(" "); int numTestCases=Integer.parseInt(input[0]); while(numTestCases-->0){ input=br.readLine().trim().split(" "); long a=Long.parseLong(input[0]); long m=Long.parseLong(input[1]); long gcd=gcd(a,m); long ans=eulerTotient(m/gcd); out.println(ans); } out.flush(); out.close(); } public static long gcd(long a,long b) { if(a==0){ return b; } if(b==0){ return a; } return gcd(b%a,a); } public static long eulerTotient(long n) { long result = n; for (int i = 2; 1L * i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
575d3c24c6b066421c80ab7a279eb44b
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.TreeMap; import java.util.StringTokenizer; import java.util.Map; 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 * * @author xwchen */ 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { long a = in.nextLong(); long m = in.nextLong(); long G = MathUtil.getGCD(a, m); m /= G; long res = MathUtil.getEulerTotientFunction(m); out.println(res); } } } static class MathUtil { public static Map<Long, Integer> getPrimeDivisors(long n) { Map<Long, Integer> r = new TreeMap<>(); for (long i = 2; i * i <= n; ++i) { if (n % i == 0) { r.put(i, 0); int cnt = 0; while (n % i == 0) { ++cnt; n /= i; } r.put(i, cnt); } } if (n > 1) { r.put(n, 1); } return r; } public static long getGCD(long a, long b) { if (a < b) { long t = a; a = b; b = t; } if (b == 0) { return a; } else { return getGCD(b, a % b); } } public static long getEulerTotientFunction(long x) { Map<Long, Integer> primes = getPrimeDivisors(x); if (x == 1) { return 1; } long ret = x; //System.out.println("x = " + x); for (long prime : primes.keySet()) { //System.out.println("prime = " + prime); ret = ret / prime * (prime - 1); } //System.out.println("ret = " + ret); return ret; } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public InputReader(InputStream inputStream) { this.reader = new BufferedReader( new InputStreamReader(inputStream)); } public String next() { while (!tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
1f7eced2eb5863cfc5e6db600038c646
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class realfast implements Runnable { private static final int INF = (int) 1e9; public void solve() throws IOException { int t = readInt(); for(int f =0;f<t;f++) { long a = readLong(); long m = readLong(); long gcd = gcd(a,m); a=a/gcd; m=m/gcd; long val=1; int i=2; long m1=m; int root=(int)Math.pow(m,0.5); long arr[]= new long[15]; int count=-1; while((m!=1)&&(i<=root)) { if(m%i==0){ count++; arr[count]=i; } while((m%i)==0) { m=m/i; } i++; } if(m>=2) { count++; arr[count]=m; } int fin = (int)Math.pow(2,count+1)-1; long ans=0; for(int j=1;j<=fin;j++) { long cur = 1; int pow=1; int lo=0; int even=0; while(lo<=count) { if((pow&j)!=0){ cur=cur*arr[lo]; even++; } pow=pow*2; lo++; } if(even%2==1) ans = ans+ m1/cur; else ans=ans- m1/cur; } out.println(m1-ans); } } public long gcd(long a , long b) { if(a<b) { long t =a; a=b; b=t; } if(a%b==0) return b; return gcd(b,a%b); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new realfast(), "", 128 * (1L << 20)).start(); } private static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; private BufferedReader reader; private StringTokenizer tokenizer; private PrintWriter out; @Override public void run() { try { if (ONLINE_JUDGE || !new File("input.txt").exists()) { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { reader = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } solve(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (IOException e) { // nothing } out.close(); } } private String readString() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } @SuppressWarnings("unused") private int readInt() throws IOException { return Integer.parseInt(readString()); } @SuppressWarnings("unused") private long readLong() throws IOException { return Long.parseLong(readString()); } @SuppressWarnings("unused") private double readDouble() throws IOException { return Double.parseDouble(readString()); } } class edge implements Comparable<edge>{ int u ; int v ; int val; edge(int u1, int v1 , int val1) { this.u=u1; this.v=v1; this.val=val1; } public int compareTo(edge e) { return this.val-e.val; } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
68f1c1d72380a9f9b899c77fbf4038c7
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
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.HashSet; import java.util.PriorityQueue; import java.util.StringTokenizer; public class D { public static void main(String[] args) { FastScanner fs=new FastScanner(); int t=fs.nextInt(); for (int tt=0; tt<t; tt++) { long a=fs.nextLong(), b=fs.nextLong(); long gcd=gcd(a, b); b/=gcd; System.out.println(phi(b)); } } static long phi(long n) { ArrayList<Factor> facts=getPrimeFactorsOf(n); long ans=1; for (Factor f:facts) { long prod=f.factor-1; for (int i=1;i<f.times; i++) prod*=f.factor; ans*=prod; } return ans; } static ArrayList<Factor> getPrimeFactorsOf(long n) { ArrayList<Factor> toReturn=new ArrayList<>(); long on=n; for (int i=2; i*(long)i<=on; i++) { if (n%i!=0) continue; int count=0; while (n%i==0) { n/=i; count++; } toReturn.add(new Factor(i, count)); } if (n!=1) { toReturn.add(new Factor(n, 1)); } return toReturn; } static long gcd(long a, long b) { return b==0?a:gcd(b,a%b); } static class Factor { long factor; int times; public Factor(long factor, int times) { this.factor=factor; this.times=times; } } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
b176b85e703067ae079276ba3fbe6051
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; public class Main { static long gcd(long a,long b) { long temp; while ((temp=a%b)!=0) { a=b; b=temp; } return b; } static ArrayList<long[]> factorization (long n) { ArrayList<long[]> list = new ArrayList<long[]>(); long tmp= n ; for (int i=2;i<=(int)Math.sqrt(n);i++) { if (tmp%i==0) { long cnt=0; while (tmp%i==0) { cnt++; tmp/=i; } long[] x = {i, cnt}; list.add(x); } } if (tmp!=1) { long[] x = {tmp, 1}; list.add(x); } if (list.size()==0) { long[] x = {n, 1}; list.add(x); } return list; } static long f(long x, long m) { // System.out.println("m "+m); ArrayList<long[]> tmp = factorization(m); // for (long[] aaa : tmp) System.out.println("m_fact "+Arrays.toString(aaa)); ArrayList<Long> list = new ArrayList<Long>(); int n = tmp.size(); for (long[] arr : tmp) list.add(arr[0]); long ans = 0L; for (int i=0;i<(1<<n);i++) { if (i==0) continue; int[] arr = new int[n]; int cnt = 0; for (int j=0;j<n;j++) { if ((i&(1<<j))==(1<<j)) arr[j]=1; cnt+=arr[j]; } long num = 1L; for (int j=0;j<n;j++) { if (arr[j]==1) { num *= list.get(j); } } long add = x/num; if (cnt%2==1) { ans+=add; } else { // cnt%2==0 ans-=add; } // System.out.println(add); // System.out.println(Arrays.toString(arr)); } // System.out.println(list); return ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i=0;i<t;i++) { long a = sc.nextLong(); long m = sc.nextLong(); long gcd = gcd(a, m); a/=gcd; m/=gcd; // System.out.println(f(a+m/gcd-1, m)); // System.out.println(f(a, m)); // System.out.println(f(a+m/gcd-1, m)-f(a, m)); System.out.println(m-(f(a+m-1, m)-f(a, m))); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
4e65d21e5a1917c8b4114ae409d80f21
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
//Same GCD's import java.io.*; import java.util.*; public class D1295{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int t = Integer.parseInt(f.readLine()); for(int q = 0; q < t; q++){ StringTokenizer st = new StringTokenizer(f.readLine()); long a = Long.parseLong(st.nextToken()); long m = Long.parseLong(st.nextToken()); long gcd = gcd(a,m); //out.println(gcd); //find totient of m/gcd long tot = totient(m/gcd); out.println(tot); } out.close(); } //from cp-algorithms public static long totient(long x){ long result = x; for(long i = 2; i*i<=x; i++){ if(x%i == 0){ while(x%i == 0){ x/=i; } result -= result/i; } } if(x > 1){ result -= result/x; } return result; } //gcd of two numbers, a and b public static long gcd(long a, long b){ if(b > a){ long temp = a; a = b; b = temp; } while(a != 0){ b = b%a; long temp = a; a = b; b = temp; } return b; } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
c6349c682a027703196f818a664e2c50
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
/* / οΎŒοΎŒβ €β €β €β €β €β €β €β €β €β €β €β €γƒ  / )\β €β €β €β €β €β €β €β €β €β €β €β € Y (β €β €| ( Ν‘Β° ΝœΚ– Ν‘Β°οΌ‰β €βŒ’(β € γƒŽ (β € οΎ‰βŒ’ Y βŒ’γƒ½-く __/ | _β €ο½‘γƒŽ| γƒŽο½‘ |/ (β €γƒΌ '_δΊΊ`γƒΌ οΎ‰ β €|\ οΏ£ _δΊΊ'彑ノ β € )\β €β € q⠀⠀ / β €β €(\β € #β € / β €/β €β €β €/α½£====================D- /β €β €β €/β € \ \β €β €\ ( (β €)β €β €β €β € ) ).β €) (β €β €)β €β €β €β €β €( | / |β € /β €β €β €β €β €β € | / [_] β €β €β €β €β €[___] */ // Main Code at the Bottom import java.util.*; import java.lang.*; import java.io.*; public class Main { //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=1000000000+7; //Euclidean Algorithm static long gcd(long A,long B){ if(B==0) return A; return gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD; return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD; } //Modular Inverse static long inverse(long x) { return fastExpo(x,MOD-2); } //Prime Number Algorithm static boolean isPrime(long n){ if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Reverse an array static void reverse(int arr[],int l,int r){ while(l<r) { int tmp=arr[l]; arr[l++]=arr[r]; arr[r++]=tmp; } } //Print array static void print1d(int arr[]) { out.println(Arrays.toString(arr)); } static void print2d(int arr[][]) { for(int a[]: arr) out.println(Arrays.toString(a)); } // Pair static class pair{ long x,y; pair(long a,long b){ this.x=a; this.y=b; } public boolean equals(Object obj) { if(obj == null || obj.getClass()!= this.getClass()) return false; pair p = (pair) obj; return (this.x==p.x && this.y==p.y); } public int hashCode() { return Objects.hash(x,y); } } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Main function(The main code starts from here) static long phi(long n) { long res=n; for(long i=2L;i*i<=n;i++) { if(n%i==0) { while(n%i==0) n/=i; res-=res/i; } } if(n>1) res-=res/n; return res; } public static void main (String[] args) throws java.lang.Exception { int test=sc.nextInt(); while(test-->0) { long a=sc.nextLong(),m=sc.nextLong(); out.println(phi(m/(gcd(a,m)))); } out.flush(); out.close(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
4470b3d4d1e4c0576875c8dc3fa74ad4
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { private static long gcd(long a,long b){ if(a<b) return gcd(b,a); if(a%b==0) return b; return gcd(b,a%b); } public static void main (String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine().trim()); while(t--!=0){ String s=br.readLine().trim(); String ss[]=s.split("\\s+"); long n=Long.parseLong(ss[0]); long m=Long.parseLong(ss[1]); long d=gcd(n,m); m=m/d; long cnt=m; for(int i=2;i<=Math.sqrt(m);i++){ if(m%i==0){ cnt=cnt*(i-1); cnt/=i; } while(m%i==0) m=m/i; } if(m!=1) cnt=cnt/m*(m-1); System.out.println(cnt); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
fe25a129631e13d8876f60961c61e944
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.io.*; public class Task{ static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);} static long phi(long n){ int k=(int)Math.sqrt(n); long x=1,y=n; for(int i=2;i<=k;i++){ boolean b=false; while(n%i==0){ n/=i;b=true; } if(b){ y/=i; x*=(long)(i-1); } } if(n>1){x*=(n-1);y/=n;} return x*y; } public static void main(String[] args) throws FileNotFoundException, IOException{ Scanner s=new Scanner(System.in); BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); StringBuilder sb=new StringBuilder(); int q=s.nextInt(); while(q--!=0){ long a=s.nextLong(),b=s.nextLong(); long x=gcd(a,b); a/=x;b/=x; long res=phi(b); out.write(res+"\n"); } out.flush(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
3c9f7c258104a454852b2b9161c8f83f
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.io.*; public class Task{ static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);} static long phi(long n){ int k=(int)Math.sqrt(n); long x=1,y=n; for(int i=2;i<=k;i++){ boolean b=false; while(n%i==0){ n/=i;b=true; } if(b){ y/=i; x*=(long)(i-1); } } if(n>1){x*=(n-1);y/=n;} return x*y; } public static void main(String[] args) throws FileNotFoundException, IOException{ Scanner s=new Scanner(System.in); BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); StringBuilder sb=new StringBuilder(); int q=s.nextInt(); while(q--!=0){ long a=s.nextLong(),b=s.nextLong(); long x=gcd(a,b); a/=x;b/=x; long res=phi(Math.max(a,b)); out.write(res+"\n"); } out.flush(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
80c7312efb6988141d30673530fecabb
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
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 { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ long a = s.nextLong(); long m = s.nextLong(); System.out.println(phi(m/gcd(a,m))); } } public static long phi(long n){ long ans=n; for(long i=2;i*i<=n;i++){ if(n%i==0){ while(n%i==0){ n/=i; } ans-=ans/i; } } if (n > 1) ans -= ans / n; return ans; } public static long gcd(long a,long m){ if(a==0) return m; return gcd(m%a,a); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
da21d937bb6f5ac63a7b2665a6320d66
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class 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; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String nextLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int ni() 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 nl() 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 nd() 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) { new Thread(null, null, "Rahsut", 1 << 25) { public void run() { try { Freak(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }.start(); } static long modPow(long x, long p, long m) { if (p == 0) return 1; if (p % 2 == 0) return modPow((x * x) % m, p >> 1, m) % m; return x * (modPow(x, p - 1, m) % m) % m; } static long mul(long a, long b, long m) { a %= m; b %= m; return (a * b) % m; } static long gcd(long a,long b) { if(b%a==0) return a; return gcd(b%a,a); } static long lcm(long a, long b) { a/=gcd(a,b); return a*b; } public static class Pair implements Comparable<Pair> { long x, y; Pair(long x, long y) { this.x = x; this.y = y; } public int compareTo(Pair o) { if (x != o.x) return (int)x - (int)o.x; return (int)y - (int)o.y; } public String toString() { return x + " " + y; } } //global variable static Reader sc = new Reader(); static StringBuilder sb = new StringBuilder(); static PrintWriter pw = new PrintWriter(System.out, true); static int dir1[][]= {{1,0},{-1,0},{0,1},{0,-1}},n,q,t; static int dir2[][] ={{1,-1},{-1,1},{1,1},{-1,-1}}; static long dp[], mod = (long)1e9 + 7; static void Freak() throws IOException { int t = sc.ni(); while(t-->0) { long a = sc.nl(),m = sc.nl(); long gcd = gcd(a,m); long val = m/gcd; long ans = val; for(long i =2;i*i<=m;i++) { if(val%i==0) { while(val%i==0) val/=i; ans/=i; ans*=(i-1); } } if(val>1) { ans/=val; ans*=(val-1); } sb.append(ans+"\n"); } pw.println(sb); pw.flush(); pw.close(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
63d8184c0e3f401ffe4e40e8be854213
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Mufaddal Naya */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DSameGCDs solver = new DSameGCDs(); solver.solve(1, in, out); out.close(); } static class DSameGCDs { public void solve(int testNumber, InputReader c, OutputWriter w) { int tc = c.readInt(); while (tc-- > 0) { long n = c.readLong(), m = c.readLong(); long gcd = Utils.gcd(n, m); long mm = m / gcd, nn = n / gcd; m /= gcd; ArrayList<Long> ad = new ArrayList<>(); for (long i = 2; i * i <= mm; i++) { if (mm % i == 0) { ad.add(i); while (mm % i == 0) { mm /= i; } } } if (mm != 1) { ad.add(mm); } int ma = 1 << ad.size(); long res = 0; for (int i = 0; i < ma; i++) { long t = 1; int tot = 0; for (int j = 0; j < ad.size(); j++) { if ((i & (1 << j)) > 0) { tot++; t *= ad.get(j); } } long res1 = (m + nn) / t - nn / t; if (tot % 2 == 0) { res += res1; } else { res -= res1; } // w.printLine(res,tot,t); } w.printLine(res); } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } static class Utils { public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
994eca4aef3a80f53d6db23c58528e64
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
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 * * @author AnandOza */ 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); DSameGCDs solver = new DSameGCDs(); solver.solve(1, in, out); out.close(); } static class DSameGCDs { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); for (int i = 0; i < t; i++) { solve(in, out); } } private void solve(InputReader in, PrintWriter out) { long a = in.nextLong(), m = in.nextLong(); m /= NumberTheory.gcd(a, m); long answer = NumberTheory.totient(m); out.println(answer); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } static class NumberTheory { public static long gcd(long a, long b) { long c; while (a != 0) { c = a; a = b % a; b = c; } return b; } public static long totient(long n) { long tot = n; for (long p = 2; p * p <= n; p++) if (n % p == 0) { tot = tot / p * (p - 1); while (n % p == 0) n /= p; } if (n > 1) tot = tot / n * (n - 1); return tot; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
dea8f7a77813b07c5e8e38def60f74fb
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
/* * Remember a 6.72 student can know more than a 10.0 student. * Grades don't determine intelligence, they test obedience. * I Never Give Up. * I will become Candidate Master today. * I will defeat Saurabh Anand. * Skills are Cheap,Passion is Priceless. */ import java.util.*; import java.util.Map.Entry; import java.io.*; import static java.lang.System.out; import static java.util.Arrays.*; import static java.lang.Math.*; public class ContestMain{ private static Reader in=new Reader(); private static StringBuilder ans=new StringBuilder(); private static long MOD=(long)1e9+7; private static final int N=(int) (1e6+7); // 1e5+7 private static ArrayList<Integer> v[]=new ArrayList[N]; private static boolean mark[]=new boolean[N]; // private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); private static long powmod(long x,long n){ if(n==0||x==0) return 1; else if(n%2==0) return(powmod((x*x)%MOD,n/2)); else return (x*(powmod((x*x)%MOD,(n-1)/2)))%MOD; } private static void shuffle(long[] arr){ for(int i=arr.length-1;i>=2;i--){ int x=new Random().nextInt(i-1); long temp=arr[x]; arr[x]=arr[i]; arr[i]=temp; } } private static void shuffle(int[] arr){ for(int i=arr.length-1;i>=2;i--){ int x=new Random().nextInt(i-1); int temp=arr[x]; arr[x]=arr[i]; arr[i]=temp; } } private static long gcd(long a, long b){ if(b==0)return a; return gcd(b,a%b); } // private static boolean check(int x,int y){ // if((x>=0&&x<n)&&(y>=0&&y<m)&&mat[x][y]!='X'&&!visited[x][y])return true; // return false; // } public static int mod(int n,int c){ if(n<0)return n%c+c; return n%c; } // public static boolean setCheck(long num,int pos){ // int c=1; // int len=0; // while(c<=num){ // if(len==pos&&(num&c)>=1){ // return true; // } // len++; // c<<=1; // } // return false; // } // public static void setBit(int bit[],long num){ // long c; // for(int j=0;j<64;j++){ // c=1L<<j; // if((num&c)>=1){ // bit[j]++; // } // } // } public static long phi(long num){ long c=2,res=1,temp; while(c*c<=num){ temp=1; while(num%c==0){ temp*=c; num/=c; } if(temp!=1){ res*=(temp-temp/c); } c++; } if(num!=1)res*=num-1; return res; } public static void main(String[] args) throws IOException{ int t=in.nextInt(); long a,m,d; while(t-->0){ a=in.nextLong(); m=in.nextLong(); app(phi(m/gcd(a,m))+"\n"); } pn(ans); } static class Pair<T> implements Comparable<Pair>{ int l; long r; Pair(){ l=0; r=0; } Pair(int k,long v){ l=k; r=v; } @Override public int compareTo(Pair o){ return (int)(r-o.r); } // Fenwick tree question comparator // @Override // public int compareTo(Pair o) { // if(o.r!=r)return (int) (r-o.r); // else return (int)(l-o.l); // } } //Reader Class static class Reader{ BufferedReader br; StringTokenizer st; public Reader(){br=new BufferedReader(new InputStreamReader(System.in));} String next(){ while(st==null||!st.hasMoreElements()){ try{st=new StringTokenizer(br.readLine()); }catch(IOException e){e.printStackTrace();} } return st.nextToken(); } int nextInt(){return Integer.parseInt(next());} long nextLong(){return Long.parseLong(next());} double nextDouble(){return Double.parseDouble(next());} String nextLine(){ String str=""; try{ str=br.readLine(); }catch(IOException e){ e.printStackTrace(); } return str; } } //Printer Methods static void pn(Object o){out.print(o);} static void pln(Object o){out.println(o);} static void pln(){out.println();} static void pf(String format,Object o){out.printf(format,o);} //Appenders static void app(Object o){ans.append(o);} }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
46944d76ab927adbcc350d35c4bdb575
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.*; import java.util.*; public class MainClass { public static void main(String[] args)throws IOException { Reader in = new Reader(); int t = in.nextInt(); StringBuilder stringBuilder = new StringBuilder(); while (t-- > 0) { long a = in.nextLong(); long m = in.nextLong(); long gcd = gcd(a, m); m /= gcd; long res = phi(m); stringBuilder.append(res).append("\n"); } System.out.println(stringBuilder); } public static long phi(long n) { long result = n; for (long p = 2; p * p <= n; ++p) { if (n % p == 0) { while (n % p == 0) n /= p; result -= result / p; } } if (n > 1L) result -= result / n; return result; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } } class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
8c9a6df73bdaef4e68419ff246efb48d
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class D_Edu_Round_81 { public static long MOD = 1000000007; static int[][] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); for (int z = 0; z < T; z++) { long a = in.nextLong(); long n = in.nextLong(); long g = gcd(a, n); long x = a / g; long y = n / g; ArrayList<Long> list = new ArrayList<>(); long cur = y; for (long i = 2; i * i <= cur; i++) { if (cur % i == 0) { while (cur % i == 0) { cur /= i; } list.add(i); } } if (cur != 1) { list.add(cur); } //System.out.println(x + " " + y + " " + cal(x + y, list) + " " + cal(x, list) + " " + list); long result = (y) - (cal(x + y, list) - cal(x, list)); out.println(result); } out.close(); } static long cal(long n, ArrayList<Long> list) { int m = list.size(); long result = 0; for (int i = 1; i < (1 << m); i++) { long tmp = 1; for (int j = 0; j < m; j++) { if (((1L << j) & i) == 0) { continue; } tmp *= list.get(j); } if (Integer.bitCount(i) % 2 == 0) { // System.out.println("Minus " + Integer.toBinaryString(i) + " " + tmp + " " + list); result -= (n / tmp); } else { // System.out.println("Add " + Integer.toBinaryString(i) + " " + tmp + " " + list); result += (n / tmp); } } // long other = 1; // for (long i = 1; i <= n; i++) { // for (long v : list) { // if (i % v == 0) { // other++; // break; // } // } // } // System.out.println(other + " " + result); return result; } private static void sort(int[] data) { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i : data) { q.add(i); } for (int i = 0; i < data.length; i++) { data[i] = q.poll(); } } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val; } else { return val * (val * a); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
4a801395d6a19b7089be876fde71aae6
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.io.*; public class SameGCDs { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(in.readLine()); for(int i = 0; i < T; ++i) { StringTokenizer st = new StringTokenizer(in.readLine()); long A = Long.parseLong(st.nextToken()); long M = Long.parseLong(st.nextToken()); long gcd = gcd(A, M); long below = (A - 1) / gcd; long above = (A + M - 1) / gcd; long mLeft = M / gcd; for(int j = 2; j <= Math.sqrt(M) && mLeft != 1; ++j) { boolean isFactor = false; while(mLeft % j == 0) { mLeft /= j; isFactor = true; } if(isFactor) { below -= below / j; above -= above / j; } } if(mLeft != 1) { below -= below / mLeft; above -= above / mLeft; } out.println(above - below); } in.close(); out.close(); } static long gcd(long a, long b) { if(b == 0) { return a; } else { return gcd(b, a % b); } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
9f50d3f8329ba629f319eae9e1cdd470
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.List; public class Drogon { public static void main(String[] args) throws IOException { FastReader sc = new FastReader(); int t=sc.nextInt(); while (t-->0){ long a=sc.nextLong(),m=sc.nextLong(); m=m/gcd(a,m); System.out.println(phi(m)); } } static long phi(long m){ long res=m; for (long i=2;i*i<=m;i++){ if (m%i==0){ res-=res/i; while (m%i==0){ m/=i; } } } if (m>1)res-=res/m; return res; } static long gcd(long a,long b){ if (a==0){ return b; } return gcd(b%a,a); } 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\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
6a2ac5b9b3b449708a5119ebda7611cc
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.util.*; import java.io.*; public class two { // https://codeforces.com/contest/1295/problem/D public static void main(String[] args) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader in = new BufferedReader(new FileReader("SameGCDs")); int t = Integer.parseInt(in.readLine()); while (t --> 0) { StringTokenizer st = new StringTokenizer(in.readLine()); long a = Long.parseLong(st.nextToken()); long b = Long.parseLong(st.nextToken()); long gcd = gcd(a,b); a /= gcd; b /= gcd; gcd = gcd(a,b); //if (gcd!=1) System.out.println(b/gcd - 1); //else { long totient = totient(b); System.out.println(totient); //} } } public static long totient(long n) { long ret = 1; if (n%2 == 0) { n>>=1; while (n%2 == 0) { n >>=1; ret *= 2; } } for (long i = 3; i*i<=n; i+=2) { if (n%i==0) { ret *= (i-1); n /= i; while (n%i == 0) { ret *= i; n/=i; } } } if (n!=1) { ret *= (n-1); } return ret; } public static long gcd(long a, long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b, a%b); } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output
PASSED
ffb6de9132a2ec059dd40c06f75b3893
train_002.jsonl
1580308500
You are given two integers $$$a$$$ and $$$m$$$. Calculate the number of integers $$$x$$$ such that $$$0 \le x &lt; m$$$ and $$$\gcd(a, m) = \gcd(a + x, m)$$$.Note: $$$\gcd(a, b)$$$ is the greatest common divisor of $$$a$$$ and $$$b$$$.
256 megabytes
import java.math.BigInteger; import java.util.*; import java.io.*; import java.text.*; public class Main{ //SOLUTION BEGIN //Into the Hardware Mode void pre() throws Exception{} void solve(int TC)throws Exception { long a = nl(), m = nl(); long g = gcd(a, m); m /= g; pn(phi(m)); } long phi (long n) { long result = n; for (long i=2; i*i<=n; ++i) if (n % i == 0) { while (n % i == 0)n /= i; result -= result / i; } if (n > 1)result -= result / n; return result; } class SegTree{ int m = 1; int[] t; public SegTree(int n){ while(m < n)m<<=1; t = new int[m<<1]; } void u(int p, int d){ t[p+= m] += d; for(p>>=1; p>0; p>>=1)t[p] = t[p<<1]+t[p<<1|1]; } int sum(int l, int r){ int ans = 0; for(l+=m, r+=m+1; l< r; l>>=1, r>>=1){ if((l&1)==1)ans += t[l++]; if((r&1)==1)ans += t[--r]; } return ans; } } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} long IINF = (long)1e15; final int INF = (int)1e9+2, MX = (int)2e6+5; DecimalFormat df = new DecimalFormat("0.00000000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-7; static boolean multipleTC = true, memory = true, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ if(fileIO){ in = new FastReader(""); out = new PrintWriter(""); }else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = (multipleTC)?ni():1; pre(); for(int t = 1; t<= T; t++)solve(t); out.flush(); out.close(); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str = ""; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
Java
["3\n4 9\n5 10\n42 9999999967"]
2 seconds
["6\n1\n9999999966"]
NoteIn the first test case appropriate $$$x$$$-s are $$$[0, 1, 3, 4, 6, 7]$$$.In the second test case the only appropriate $$$x$$$ is $$$0$$$.
Java 8
standard input
[ "number theory", "math" ]
adcd813d4c45337bbd8bb0abfa2f0e00
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 50$$$) β€” the number of test cases. Next $$$T$$$ lines contain test cases β€” one per line. Each line contains two integers $$$a$$$ and $$$m$$$ ($$$1 \le a &lt; m \le 10^{10}$$$).
1,800
Print $$$T$$$ integers β€” one per test case. For each test case print the number of appropriate $$$x$$$-s.
standard output