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
sequencelengths
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
0af5849a14f6be9b099418ec0c2700c4
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; public class Main { void Solve(long n){ ArrayList<Long>v=new ArrayList<Long>(0); long mod=n; long ans=1; for(long i=1;i<n;i++){ if(__gcd(i,n)==1){ v.add(i); ans*=i; ans%=mod; } } if(ans==1){ ans=-1; } long l=v.size(); if(ans!=-1){ l--; } System.out.println(l); for(Long it:v){ if((long)it!=ans){ System.out.print(it+" "); } } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t; t=1; while((t--)!=0){ long n,k; Main s=new Main(); n=sc.nextInt(); s.Solve(n); } } public long getgcd(long a,long b){ if(a==0) return b; return getgcd(b%a,a); } public long __gcd(long a,long b){ if(a>b){ long c=b; b=a; a=b; } return getgcd(a,b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
f7926b5434b505e58cd7cf99e073ed8d
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
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 product1 { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws java.lang.Exception { // your code goes here FastReader scan = new FastReader(); int n = scan.nextInt(); List<Integer> ls = new ArrayList(); ls.add(1); long mul = 1; for (int i = 2; i < n - 1; i++) { if (gcd(i, n) == 1) { ls.add(i); mul *= i; mul %= n; // System.out.println("line 1 " + i + " " + mul + " " + mul % n); } } // System.out.println("line 2 " + mul + " " + mul % n); if (n > 2) { // System.out.println((mul * (n - 1)) % n + " line 3 "); if ((mul * (n - 1)) % n == 1) { ls.add(n - 1); // System.out.println(n - 1 + " line 4 "); } } System.out.println(ls.size()); for (int i : ls) { System.out.print(i + " "); } } public static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
6c0d659290b736775d7dad7b52e70db5
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class a2oj { static FastReader sc; static PrintWriter out; static int mod = 1000000007; public static void main(String[] args) throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { File f1 = new File("input.txt"); File f2 = new File("output.txt"); Reader r = new FileReader(f1); sc = new FastReader(r); out = new PrintWriter(f2); double prev = System.currentTimeMillis(); solve(); out.println("\n\nExecuted in : " + ((System.currentTimeMillis() - prev) / 1e3) + " sec"); } else { sc = new FastReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); } out.flush(); out.close(); } static void solve() { int t = 1; StringBuilder ans = new StringBuilder(""); while (t-- > 0) { int n = sc.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); long prod = 1; for (int i = 1; i < n; i++) { if (gcd(i, n) == 1) { arr.add(i); prod = (prod * i) % n; } } if (prod == 1) { ans.append(arr.size() + "\n"); for (int i : arr) { ans.append(i + " "); } ans.append("\n"); } else { ans.append(arr.size() - 1 + "\n"); for (int i : arr) { if (i == prod) continue; ans.append(i + " "); } ans.append("\n"); } } out.println(ans); } static long modInverse(long a) { long g = gcd(a, mod); if (g != 1) return -1; else { return power(a, mod - 2L); } } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (b == 0) return a; else return gcd(b, a % b); } static boolean isPerfectSquare(double x) { if (x >= 0) { double sr = Math.sqrt(x); return ((sr * sr) == x); } return false; } public static int digitsCount(long x) { return (int) Math.floor(Math.log10(x)) + 1; } public static boolean isPowerTwo(long n) { return (n & n - 1) == 0; } static void sieve(boolean[] prime, int n) { // Sieve Of Eratosthenes for (int i = 1; i <= n; i++) { prime[i] = true; } for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = 2; i * j <= n; j++) { prime[i * j] = false; } } } } static long nCr(int n, int r) { // Combinations if (n < r) return 0; if (r > n - r) { // because nCr(n, r) == nCr(n, n - r) r = n - r; } long ans = 1L; for (int i = 0; i < r; i++) { ans *= (n - i); ans /= (i + 1); } return ans; } static long catalan(int n) { // n-th Catalan Number long c = nCr(2 * n, n); return c / (n + 1); } static class Pair implements Comparable<Pair> { // Pair Class int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Pair)) { return false; } Pair pair = (Pair) o; if (x != pair.x) { return false; } if (y != pair.y) { return false; } return true; } @Override public int hashCode() { long result = x; result = 31 * result + y; return (int) result; } @Override public int compareTo(Pair o) { return (int) (this.x - o.x); } } static class Trip { // Triplet Class long x; long y; long z; Trip(long x, long y, long z) { this.x = x; this.y = y; this.z = z; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Trip)) { return false; } Trip trip = (Trip) o; if (x != trip.x) { return false; } if (y != trip.y) { return false; } if (z != trip.z) { return false; } return true; } @Override public int hashCode() { long result = 62 * x + 31 * y + z; return (int) result; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(Reader r) { br = new BufferedReader(r); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArrayI(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } return arr; } long[] readArrayL(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextLong(); } return arr; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* * ASCII Range--->(A-Z)--->[65,90]<<::>>(a-z)--->[97,122] */ /******************************************************************************************************************/ static void printArray(int[] arr) { out.print("["); for (int i = 0; i < arr.length; i++) { if (i < arr.length - 1) out.print(arr[i] + ","); else out.print(arr[i]); } out.print("]"); out.println(); } static void printArray(long[] arr) { out.print("["); for (int i = 0; i < arr.length; i++) { if (i < arr.length - 1) out.print(arr[i] + ","); else out.print(arr[i]); } out.print("]"); out.println(); } static void printArray(double[] arr) { out.print("["); for (int i = 0; i < arr.length; i++) { if (i < arr.length - 1) out.print(arr[i] + ","); else out.print(arr[i]); } out.print("]"); out.println(); } /**********************************************************************************************************************/ }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
7fb1aa39f3242770b3d428e5038afa34
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<Integer> ans = new ArrayList<>(); ans.add(1); if(n != 2) { long prod = 1; for(int i=2; i<n-1; i++) { if( GCD(i, n) == 1 ) { ans.add(i); prod *= i; prod = prod%n; } } prod *= n-1; if( prod % n == 1 ) ans.add(n-1); // } } System.out.println(ans.size()); for(Integer ele : ans ) System.out.print(ele + " "); } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
254e1a0848ce596b4d6cd4ca3b39edf3
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; public class p717C { public static void main(String[] args) { Scanner scn=new Scanner(System.in); long n=scn.nextLong(); long pro=1; ArrayList<Long> arr=new ArrayList<Long>(); arr.add(1L); for(long i=2;i<n;i++){ if(gcd(n,i)==1){ arr.add(i); pro=pro*i; pro=pro%n; } } if(pro==1){ System.out.println(arr.size()); for(Long i:arr){ System.out.print(i+" "); } } else{ System.out.println(arr.size()-1); for(Long i:arr){ if(i!=pro){ System.out.print(i+" "); } } } } public static long gcd(long a,long b){ if(a>=b){ while(b>0){ long m=a%b; a=b; b=m; } return(a); } else{ return(gcd(b,a)); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
f76768a0132cc6b2b915cd4a78f6bea2
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Main{ //Don't have to see. start------------------------------------------ static class InputIterator{ ArrayList<String> inputLine = new ArrayList<>(1024); int index = 0; int max; String read; InputIterator(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try{ while((read = br.readLine()) != null){ inputLine.addAll(Arrays.asList(read.split(" "))); } }catch(IOException e){} max = inputLine.size(); } boolean hasNext(){return (index < max);} String next(){ if(hasNext()){ return inputLine.get(index++); }else{ throw new IndexOutOfBoundsException("There is no more input"); } } } static HashMap<Integer, String> CONVSTR = new HashMap<>(); static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem. static PrintWriter out = new PrintWriter(System.out); static void flush(){out.flush();} static void myout(Object t){out.println(t);} static void myerr(Object t){System.err.print("debug:");System.err.println(t);} static String next(){return ii.next();} static boolean hasNext(){return ii.hasNext();} static int nextInt(){return Integer.parseInt(next());} static long nextLong(){return Long.parseLong(next());} static double nextDouble(){return Double.parseDouble(next());} static ArrayList<String> nextCharArray(){return myconv(next(), 0);} static ArrayList<String> nextStrArray(int size){ ArrayList<String> ret = new ArrayList<>(size); for(int i = 0; i < size; i++){ ret.add(next()); } return ret; } static ArrayList<Integer> nextIntArray(int size){ ArrayList<Integer> ret = new ArrayList<>(size); for(int i = 0; i < size; i++){ ret.add(Integer.parseInt(next())); } return ret; } static ArrayList<Long> nextLongArray(int size){ ArrayList<Long> ret = new ArrayList<>(size); for(int i = 0; i < size; i++){ ret.add(Long.parseLong(next())); } return ret; } @SuppressWarnings("unchecked") static String myconv(Object list, int no){//only join String joinString = CONVSTR.get(no); if(list instanceof String[]){ return String.join(joinString, (String[])list); }else if(list instanceof ArrayList){ return String.join(joinString, (ArrayList)list); }else{ throw new ClassCastException("Don't join"); } } static ArrayList<String> myconv(String str, int no){//only split String splitString = CONVSTR.get(no); return new ArrayList<String>(Arrays.asList(str.split(splitString))); } public static void main(String[] args){ CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, ""); solve();flush(); } //Don't have to see. end------------------------------------------ static void solve(){//Here is the main function int N = nextInt(); if(N == 2){ myout(1); myout(1); return; } if(isPrime(N)){ myout(N - 2); String[] output = new String[N - 2]; for(int i = 1; i <= N - 2; i++){ output[i - 1] = String.valueOf(i); } myout(myconv(output, 8)); }else{ TreeMap<Long, Long> map = getPrimeFactorization((long)N); ArrayList<Long> set = new ArrayList<>(map.keySet()); ArrayList<Integer> list = new ArrayList<>(); long test = 1; list.add(1); for(int i = 2; i < N; i++){ boolean isOK = true; for(int j = 0; j < set.size(); j++){ if(i % set.get(j) == 0){ isOK = false; break; } } if(isOK){ list.add(i); test *= i; test %= N; } } if(test != 1){ list.remove(list.size() - 1); } myout(list.size()); String[] output = new String[list.size()]; for(int i = 0; i < list.size(); i++){ output[i] = String.valueOf(list.get(i)); } myout(myconv(output, 8)); } } //Method addition frame start static boolean isPrime(long val){ if(val <= 1 || (val != 2 && val % 2 == 0)){ return false; }else if(val == 2){ return true; } double root = Math.floor(Math.sqrt(val)); for(long i = 3; i <= root; i += 2){ if(val % i == 0){ return false; } } return true; } static TreeMap<Long,Long> getPrimeFactorization(Long val){ TreeMap<Long,Long> primeList = new TreeMap<>(); if(isPrime(val)){ primeList.put(val, 1l); return primeList; } long div = 2; while(val != 1){ if(val % div == 0){ long count = 2; while(val % (long)Math.pow(div, count) == 0){ count++; } if(primeList.containsKey(div)){ primeList.put(div, primeList.get(div) + (count - 1)); }else{ primeList.put(div, count - 1); } val /= (long)Math.pow(div, count - 1); if(isPrime(val)){ if(primeList.containsKey(val)){ primeList.put(val, primeList.get(val) + 1); }else{ primeList.put(val, 1l); } break; } }else{ div = (div == 2) ? div + 1 : div + 2; } } return primeList; } //Method addition frame end }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
3aa6800af24788eaf0eb2589750c70ba
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Main{ public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long MOD = (long) (1e9 + 7); // static long MOD = 998244353; static long MOD2 = MOD * MOD; static FastReader sc = new FastReader(); static int pInf = Integer.MAX_VALUE; static int nInf = Integer.MIN_VALUE; static long ded = (long)(1e17)+9; public static void main(String[] args) throws Exception { int test = 1; // test = sc.nextInt();8 for (int i = 1; i <= test; i++){ // out.print("Case #"+i+": "); solve(); } out.flush(); out.close(); } static void solve () throws Exception { int n = sc.nextInt(); MOD = n; ArrayList<Integer> A = new ArrayList<>(); long cur = 1; for(int i = 1; i < n; i++){ if(gcd(i,n)==1){ A.add(i); cur = mul(cur,i); } } // out.println(A+" "+cur); if(cur==1){ out.println(A.size()); for(int x: A){ out.print(x+" "); } out.println(); return; } int m = A.size(); long[] l = new long[A.size()]; long[] r = new long[A.size()]; l[0] = A.get(0); r[m-1] = A.get(m-1); for(int i = 1; i < m; i++){ l[i] = mul(l[i-1],A.get(i)); } for(int i = m-2; i >= 0; i--){ r[i] = mul(r[i+1],A.get(i)); } int key = -1; for(int i = 1; i < m-1; i++){ long temp = mul(l[i-1],r[i+1]); if(temp==1){ key = i; break; } } //endcases if(l[m-2]==1){ key = m-1; } if(r[1]==1){ key = 0; } out.println(m-1); for(int i = 0; i < m; i++){ if(i==key)continue; out.print(A.get(i)+" "); } out.println(); } static long[] facts, factInvs; static long exp(long base, long e) { if (e == 0) return 1; long half = exp(base, e / 2); if (e % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long modInv(long x) { return exp(x, MOD - 2); } static void precomp(int n) { facts = new long[n]; factInvs = new long[n]; factInvs[0] = facts[0] = 1; for (int i = 1; i < facts.length; i++) facts[i] = mul(facts[i - 1], i); factInvs[facts.length - 1] = modInv(facts[facts.length - 1]); for (int i = facts.length - 2; i >= 0; i--) factInvs[i] = mul(factInvs[i + 1], i + 1); } static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o){ if(this.y==o.y){ return -1*(this.x-o.x); } return (this.y-o.y); } @Override public String toString() { return "Pair{" + "x=" + x + ", y=" + y + '}'; } public boolean equals(Pair o){ return this.x==o.x&&this.y==o.y; } } public static long mul(long a, long b) { return ((a % MOD) * (b % MOD)) % MOD; } public static long add(long a, long b) { return ((a % MOD) + (b % MOD)) % MOD; } public static long c2(long n) { if ((n & 1) == 0) { return mul(n / 2, n - 1); } else { return mul(n, (n - 1) / 2); } } //Shuffle Sort static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); int temp= a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } //Brian Kernighans Algorithm static long countSetBits(long n) { if (n == 0) return 0; return 1 + countSetBits(n & (n - 1)); } //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; } //AKS 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 <= Math.sqrt(n); i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static long modinv(long x) { return modpow(x, MOD - 2); } public static long modpow(long a, long b) { if (b == 0) { return 1; } long x = modpow(a, b / 2); x = (x * x) % MOD; if (b % 2 == 1) { return (x * a) % MOD; } return x; } 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
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
2704a7d00005efc54d7dfd1b47b6b6c6
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Round716Div2C { private static int gcd(int a, int b) {// a is larger if(b==0) return a; return gcd(b,a%b); } public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(f.readLine()); long prod = 1; int len = 0; for (int i = 1; i < n; i++) { if(gcd(n,i)!=1) continue; prod*=i; prod%=n; len++; } int inverse=0; for (int i = 1; i < n; i++) { if((prod*i)%n==1) { inverse=i; break; } } assert (inverse!=0); if(inverse!=1) len--; boolean started = false; System.out.println(len); if(prod==1) { for (int i = 1; i < n; i++) { if(gcd(n,i)!=1) continue; if(started) System.out.print(" "); started=true; System.out.print(i); } System.out.println(); return; } for (int i = 1; i < n; i++) { if(gcd(n,i)!=1) continue; if(i==inverse) continue; if(started) System.out.print(" "); started=true; System.out.print(i); } System.out.println(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
baaee324b6972fbf29c8e2c6793c164e
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class C { public static void main(String[] args) { new Runner().run(); } static class Runner { FastReader in; public void run() { in = new FastReader(); //int T = in.nextInt(); StringBuffer sb = new StringBuffer(); //while(T-- > 0) { int n = in.nextInt(); boolean[] done = new boolean[n]; Arrays.fill(done, true); for(int i=1; i<n; i++) { if(gcd(i, n) == 1) done[i] = false; } int cnt = 0; List<Integer> v = new ArrayList<Integer>(); v.add(1); List<Integer> v2 = new ArrayList<Integer>(); for(int i=2; i<n; i++) { if(done[i]) continue; done[i] = true; int inv = modInverse(i, n); //System.out.println(i + " " + inv); done[inv] = true; if(i == inv) { v2.add(i); continue; } v.add(i); v.add(inv); } long d = 1L; for(Integer i: v2) { d = (i * d) %n; } if(d == 1) { for(Integer i: v2) { v.add(i); } } sb.append(v.size() + "\n"); Collections.sort(v); for(Integer i: v) { sb.append(i + " "); } sb.append("\n"); //} System.out.println(sb.toString()); } int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { int q = a / m; int t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } int gcd(int a, int b) { if(b==0) return a; return gcd(b, a%b); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
5e9decd665ee962a4c818ce19304bdec
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import static java.lang.Math.*; import static java.math.BigInteger.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static java.util.stream.Collectors.*; import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; public class C { static BufferedReader in; static PrintWriter out; static StringTokenizer stk; public static void main(String[] args) throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); preproc(); /** Choose single or multiple */ single(0); // multiple(); out.flush(); } static void preproc() { // add any preprocessing valid for all tests here // } static long modPow(long n, long pow, long mod) { return BigInteger.valueOf(n).modPow(BigInteger.valueOf(pow), BigInteger.valueOf(mod)).longValue(); } static long modInv(long n, long mod, boolean isPrimeModuli) { if (isPrimeModuli) { return modPow(n, mod - 2, mod); } return BigInteger.valueOf(n).modInverse(BigInteger.valueOf(mod)).longValue(); } static void single(int testId) throws Exception { // add code here // int n = ni(); TreeSet<Integer> t = new TreeSet<>(); long r = 1; for (int i = 1; i < n; i++) { if (gcd(i, n) != 1) continue; t.add(i); r = (r * i) % n; } if (r != 1) { int x = (int) modInv(r, n, false); t.remove(x); } out.println(t.size()); boolean printed = false; for(Integer e: t) { if (printed) out.print(" "); printed = true; out.print(e); } out.println(); } static void multiple() throws Exception { // multiple tests in single file int T = ni(); for (int t = 0; t < T; t++) { single(t); } } static void printf(String format, Object... args) { out.printf(format, args); } static void println(String string) { out.println(string); } static long toL(String s) { return Long.parseLong(s); } static int toI(String s) { return Integer.parseInt(s); } static double toD(String s) { return Double.parseDouble(s); } static long toL(BigInteger n) { return n.longValue(); } static BigInteger toBI(long n) { return BigInteger.valueOf(n); } static List<Integer> newli() { return new ArrayList<Integer>(); } static List<Long> newll() { return new ArrayList<Long>(); } static void nia(int[] a, int n) throws Exception { for (var i = 0; i < n; i++) { a[i] = ni(); } } static void nla(long[] a, int n) throws Exception { for (var i = 0; i < n; i++) { a[i] = nl(); } } static void nia(List<Integer> a, int n) throws Exception { for (var i = 0; i < n; i++) { a.add(ni()); } } static void nla(List<Long> a, int n) throws Exception { for (var i = 0; i < n; i++) { a.add(nl()); } } static int ni() throws Exception { while (stk == null || !stk.hasMoreTokens()) { stk = new StringTokenizer(in.readLine()); } return Integer.parseInt(stk.nextToken()); } static long nl() throws Exception { while (stk == null || !stk.hasMoreTokens()) { stk = new StringTokenizer(in.readLine()); } return Long.parseLong(stk.nextToken()); } static double nd() throws Exception { while (stk == null || !stk.hasMoreTokens()) { stk = new StringTokenizer(in.readLine()); } return Double.parseDouble(stk.nextToken()); } static String ns() throws Exception { while (stk == null || !stk.hasMoreTokens()) { stk = new StringTokenizer(in.readLine()); } return stk.nextToken(); } static int min(int a, int b) { return a < b ? a : b; } static int max(int a, int b) { return a > b ? a : b; } static long min(long a, long b) { return a < b ? a : b; } static long max(long a, long b) { return a > b ? a : b; } static double min(double a, double b) { return a < b ? a : b; } static double max(double a, double b) { return a > b ? a : b; } static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
8b5cef083e53bb75697eba3593506478
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.PrintWriter; import java.util.*; public class C { static Scanner sc; static PrintWriter out; public static void main(String[] args) throws Exception { sc = new Scanner(System.in); out = new PrintWriter(System.out); //int t = sc.nextInt(); for(int i=0; i<1; i++) { solve(); } out.flush(); } static void solve() { int n = sc.nextInt(); boolean[] b = solve(n); int c = 0; for(boolean bb : b) { if(bb) c++; } out.println(c); boolean f = false; for(int i=1; i<n; i++) { if(b[i]) { if(f) out.print(" "); f = true; out.print(i); } } out.println(); } static boolean[] solve(int n) { boolean[] b = new boolean[n]; b[1] = true; long prod = 1; for(int i=1; i<n; i++) { if(gcd(n, i) == 1) { b[i]=true; prod *= i; prod %= n; } } if(prod != 1) { //out.println(n + ":" + prod); b[(int)prod] = false; } return b; } static long gcd(long m, long n) { if(m < n) return gcd(n, m); if(n == 0) return m; return gcd(n, m % n); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
c84b278dcfbe8590e23b65f44931a186
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; /** * @author Peter Li * @version 04/19/2021 */ public class C { private final static int[] list = new int[100000]; private static int gcd(int a, int b) { if (a < b) { return gcd(b, a); } if (b == 0) { return a; } return gcd(b, a % b); } private static int solve() { int good = 0; long prod = 1; for (int i = 1; i < index; i++) { prod = (prod * list[i]) % n; if (prod == 1) { good = i; } } System.out.println(good + 1); return good; } private static int n, index; public static void main(String[] args) throws IOException { n = ni(); index = 0; for (int i = 1; i <= n; i++) { if (gcd(n, i) == 1) { list[index++] = i; } } // System.out.println(Arrays.toString(list)); int good = solve(); for (int i = 0; i <= good; i++) { System.out.print(list[i] + " "); } } private static final String DELIMINATOR = " \n\r\f\t"; private final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 1 << 15); private static StringTokenizer st = new StringTokenizer("", DELIMINATOR); private static boolean hasNext() { makeStringTokenizerReady(); return st.hasMoreTokens(); } private static void makeStringTokenizerReady() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine(), DELIMINATOR); } catch (IOException e) { e.printStackTrace(); } } } private static String ns() { makeStringTokenizerReady(); return st.nextToken(); } private static long nl() { return Long.parseLong(ns()); } private static int ni() { return (int) nl(); } private static double nd() { return Double.parseDouble(ns()); } private static int[] na(int n) { int[] ans = new int[n]; for (int i = 0; i < n; i++) { ans[i] = ni(); } return ans; } private static String[] nsa(int n) { String[] ans = new String[n]; for (int i = 0; i < n; i++) { ans[i] = ns(); } return ans; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 11
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
33844833d6e29e5bb4ac4dde734488ff
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class dummy2 { public static void main(String args[])throws IOException { BufferedReader x=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(x.readLine()); long p=1; HashSet<Long> arr=new HashSet<Long>(); arr.add(p); for(long i=2; i<n; i++) { if(gcd(i,n)==1) { arr.add(i); p=(p*i)%n; } } if(p!=1)arr.remove(p); System.out.println(arr.size()); //System.out.println(p+" "+arr.contains(p)); for(long i=1; i<n; i++)if(arr.contains(i))System.out.print(i+" "); /*int coprime[] = new int[n]; coprime[1] = 1; long rem = 1; //product of coprimes int ans = 1; //this is the size of coprimes for(int i=2;i<n;i++) { if(gcd(i,n) == 1){ coprime[i] = 1; ans++; rem = (rem*i)%n; } } if(rem != 1){ ans--; coprime[(int)rem] = 0; } System.out.println(ans); for(int i=1;i<n;i++){ if(coprime[i]==1)System.out.print(i + " "); }*/ } static long gcd(long a, long b) { if(a==0)return b; return gcd(b%a, a); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
330999ef3bf582312947ae02e634edd8
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class product1modulon { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = Integer.parseInt(f.readLine()); long[] coprimes = new long[n + 1]; int count = 0; for(int i = 0; i < n / 2 + 1; i ++) { if(gcd(n, i) == 1) { coprimes[count] = i; count ++; if(i != n - i) { coprimes[count] = n - i; count ++; } } } long total = 1; for(int i = 0; i < count; i ++) { total *= coprimes[i]; total %= n; } if(total == 1) { out.println(count); for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } out.println(); } else { out.println(count - 1); if(total < n / 2) { for(int i = 0; i < count; i += 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } } else { for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } } out.println(); } out.flush(); } static int gcd(int n1, int n2) { if (n1 == 0) { return n2; } if (n2 == 0) { return n1; } int n; for (n = 0; ((n1 | n2) & 1) == 0; n++) { n1 >>= 1; n2 >>= 1; } while ((n1 & 1) == 0) { n1 >>= 1; } do { while ((n2 & 1) == 0) { n2 >>= 1; } if (n1 > n2) { int temp = n1; n1 = n2; n2 = temp; } n2 = (n2 - n1); } while (n2 != 0); return n1 << n; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
7306e7955b79911762e2af7b15b02b0c
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class product1modulon { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = Integer.parseInt(f.readLine()); long[] coprimes = new long[n + 1]; int count = 0; for(int i = 0; i < n / 2 + 1; i ++) { if(gcd(n, i) == 1) { coprimes[count] = i; count ++; if(i != n - i) { coprimes[count] = n - i; count ++; } } } long total = 1; for(int i = 0; i < count; i ++) { total *= coprimes[i]; total %= n; } if(total == 1) { out.println(count); for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } out.println(); } else { out.println(count - 1); if(total < n / 2) { for(int i = 0; i < count; i += 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } } else { for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } } out.println(); } out.flush(); } static int gcd(int a, int b) { while (b != 0) { int t = a; a = b; b = t % b; } return a; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
aa14bd16ef0deaf3ecf6dd0934236610
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class product1modulon { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = Integer.parseInt(f.readLine()); int[] coprimes = new int[n + 1]; int count = 0; for(int i = 0; i < n / 2 + 1; i ++) { if(gcd(n, i) == 1) { coprimes[count] = i; count ++; if(i != n - i) { coprimes[count] = n - i; count ++; } } } long total = 1; for(int i = 0; i < count; i ++) { total *= (long) coprimes[i]; total %= n; } if(total == 1) { out.println(count); for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } out.println(); } else { out.println(count - 1); if(total < n / 2) { for(int i = 0; i < count; i += 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } } else { for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } } out.println(); } out.flush(); } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
1f488f9becb22e5daab1551eaeb63caa
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class product1modulon { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = Integer.parseInt(f.readLine()); int[] coprimes = new int[n + 1]; int count = 0; for(int i = 0; i < n / 2 + 1; i ++) { if(gcd(n, i) == 1) { coprimes[count] = i; count ++; if(i != n - i) { coprimes[count] = n - i; count ++; } } } long total = 1; for(int i = 0; i < count; i ++) { total *= (long) coprimes[i]; total %= n; } if(total == 1) { out.println(count); for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } out.println(); } else { out.println(count - 1); if(total < n / 2) { for(int i = 0; i < count; i += 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { out.print(coprimes[i] + " "); } } else { for(int i = 0; i < count; i += 2) { out.print(coprimes[i] + " "); } for(int i = count - (count % 2) - 1; i >= 0; i -= 2) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } } out.println(); } out.flush(); } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
74cb2241d074cf89fe333ff32ec40ac8
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class product1modulon { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int n = Integer.parseInt(f.readLine()); int[] coprimes = new int[n + 1]; int count = 0; for(int i = 0; i < n; i ++) { if(gcd(n, i) == 1) { coprimes[count] = i; count ++; } } long total = 1; for(int i = 0; i < count; i ++) { total *= (long) coprimes[i]; total %= n; } if(total == 1) { out.println(count); for(int i = 0; i < count; i ++) { out.print(coprimes[i] + " "); } out.println(); } else { out.println(count - 1); for(int i = 0; i < count; i ++) { if(coprimes[i] != total) { out.print(coprimes[i] + " "); } } out.println(); } out.flush(); } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
5e7cdd5178052acd6e88162a08558523
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class product1modulon { public static void main(String[] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(f.readLine()); ArrayList<Integer> coprimes = new ArrayList<>(); int count = 0; for(int i = 0; i < n; i ++) { if(gcd(n, i) == 1) { coprimes.add(i); count ++; } } long total = 1; for(int i = 0; i < count; i ++) { total *= (long) coprimes.get(i); total %= n; } if(total == 1) { System.out.println(count); for(int i = 0; i < count; i ++) { System.out.print(coprimes.get(i) + " "); } System.out.println(); } else { System.out.println(count - 1); for(int i = 0; i < count; i ++) { if(coprimes.get(i) != total) { System.out.print(coprimes.get(i) + " "); } } System.out.println(); } } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
ccf17351f69d1dcdd2b4cd71a6373690
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static int MAX_SIZE = 1000001 ; public static Vector<Boolean> isprime = new Vector<Boolean>(MAX_SIZE); public static Vector<Integer> primes = new Vector<Integer>(MAX_SIZE) ; public static Vector<Integer> SPF = new Vector<Integer>(MAX_SIZE); public static void sieve (int n ) { isprime.set(0,false); isprime.set(1, false) ; for (int i = 2 ; i < n ; i ++ ) { if (isprime.get(i)) { primes.add(i); SPF.add(i); } for (int j = 2 ; i*primes.get(j) <= n ; j +=i ) { } } } public static ArrayList<Stack<Integer>> as = new ArrayList<Stack<Integer>>(); public static int k ; public static Stack<Integer>q=new Stack<Integer>(); public static void permutation (int [] a,int n , int mask, int taken) { if (taken == n ) { arr = q.toArray(); backtrack((int)arr[0],0, 1); return ; } else { for(int i = 0 ; i < n ; i++ ) { if ((mask &(1 << i)) == 0 ) { q.add((int)a[i]); permutation(a,n, mask | (1 <<i) , taken+1); q.pop(); } } } } public static boolean gsd(long x , int y) { for(int i = 2 ; i <= Math.min(y, x) ; i ++ ) { if (x%i == 0 && y%i == 0 ) return true; } return false; } public static int s(long x) { int sum = 0 ; do { sum += x%10 ; x /= 10; }while(x >0 ); return sum; } public static int right(String s) { int c = 0 ; for(int i = s.length()-1 ; i>=0; i -- ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static int left(String s) { int c = 0 ; for(int i = 0 ; i<s.length(); i ++ ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static boolean onlyas (String s) { for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) != 'a') return false ; }return true; } public static String convert(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) == '1') sb.append("0"); else sb.append("1"); } return sb.toString(); } public static int check (String s) { // boolean flag = false ; for (int i = 0 ; i < s.length() ; i ++) { if (Character.isDigit(s.charAt(i)) ) { for (int j = i ; j <s.length() ; j ++ ) { if (!Character.isDigit(s.charAt(j)))return 0 ; } return 1 ; } } return 1; } public static String s26 (int n) { StringBuilder st = new StringBuilder(); while (n >0) { int x = n%26 ; n /= 26 ; if (x-1 <0 ) { x = 26;n-- ; } st.insert(0, letters[x-1]); } return st.toString(); } public static int sum = 0 ; public static int tokens = 0 ; public static Object arr [] = new Integer [5]; public static boolean flag = false ; public static void backtrack (int ss , int idx , int tks) { if (tks == 5 ) { // System.out.println(ss + " "); if (ss == 23 ) {//System.out.print(ss+" " ); flag = true ; return ; } }else { backtrack(ss+ (int)arr[idx+1], idx+1, tks+1); backtrack(ss*(int)arr[idx+1], idx+1, tks+1); backtrack(ss-(int)arr[idx+1], idx+1, tks+1); } } public static int length (int n) { int l = 0 ; while (n > 0 ) { l ++ ; n /=10 ; } return l ; } public static int GCD (int x , int y ) { if (x == y ) return x ; if (x > y ) return GCD(x-y , y ) ; else return GCD(x , y -x); } public static int low (int k ) { return (k)*(k+1)/2 ; } public static int high (int n , int k) { return (k)*(2*n-k+1)/2 ; } public static char [] letters = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' ,'P','Q','R','S','T','U','V','W','X','Y', 'Z'}; public static void main(String[] args) throws IOException, InterruptedException{ PrintWriter pw = new PrintWriter(System.out); Scanner sc = new Scanner (System.in); int n = sc.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); long pro = 1 ; for (int i = 1; i < n; i ++ ) { if (GCD(i , n ) == 1 ) { arr.add(i); pro *= i ; pro %= n ; } } int u = arr.size(); if (pro != 1 ) { pw.println(u-1); for (int i : arr ) {if (i == pro )continue;; pw.print(i+" " ); } pw.close();; } pw.println(arr.size()); for (int i : arr ) { pw.print(i+" " ); } pw.close (); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } } /* while (true) { int []a = new int [5]; for (int i = 0 ; i < 5 ; i ++ ) { arr[i]= sc.nextInt(); a[i]= (int)arr[i];; } if (a[0] == 0) break ; permutation(a, 5, 0, 0); pw.println(flag ? "Possible":"Impossible"); } */
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
d9aeb8de9f31ca818611b12f8b040dd2
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Main { public static int MAX_SIZE = 1000001 ; public static Vector<Boolean> isprime = new Vector<Boolean>(MAX_SIZE); public static Vector<Integer> primes = new Vector<Integer>(MAX_SIZE) ; public static Vector<Integer> SPF = new Vector<Integer>(MAX_SIZE); public static void sieve (int n ) { isprime.set(0,false); isprime.set(1, false) ; for (int i = 2 ; i < n ; i ++ ) { if (isprime.get(i)) { primes.add(i); SPF.add(i); } for (int j = 2 ; i*primes.get(j) <= n ; j +=i ) { } } } public static ArrayList<Stack<Integer>> as = new ArrayList<Stack<Integer>>(); public static int k ; public static Stack<Integer>q=new Stack<Integer>(); public static void permutation (int [] a,int n , int mask, int taken) { if (taken == n ) { arr = q.toArray(); backtrack((int)arr[0],0, 1); return ; } else { for(int i = 0 ; i < n ; i++ ) { if ((mask &(1 << i)) == 0 ) { q.add((int)a[i]); permutation(a,n, mask | (1 <<i) , taken+1); q.pop(); } } } } public static boolean gsd(long x , int y) { for(int i = 2 ; i <= Math.min(y, x) ; i ++ ) { if (x%i == 0 && y%i == 0 ) return true; } return false; } public static int s(long x) { int sum = 0 ; do { sum += x%10 ; x /= 10; }while(x >0 ); return sum; } public static int right(String s) { int c = 0 ; for(int i = s.length()-1 ; i>=0; i -- ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static int left(String s) { int c = 0 ; for(int i = 0 ; i<s.length(); i ++ ) { if(s.charAt(i) == 'a') c++ ; else break; } return c; } public static boolean onlyas (String s) { for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) != 'a') return false ; }return true; } public static String convert(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0 ; i < s.length() ; i ++ ) { if (s.charAt(i) == '1') sb.append("0"); else sb.append("1"); } return sb.toString(); } public static int check (String s) { // boolean flag = false ; for (int i = 0 ; i < s.length() ; i ++) { if (Character.isDigit(s.charAt(i)) ) { for (int j = i ; j <s.length() ; j ++ ) { if (!Character.isDigit(s.charAt(j)))return 0 ; } return 1 ; } } return 1; } public static String s26 (int n) { StringBuilder st = new StringBuilder(); while (n >0) { int x = n%26 ; n /= 26 ; if (x-1 <0 ) { x = 26;n-- ; } st.insert(0, letters[x-1]); } return st.toString(); } public static int sum = 0 ; public static int tokens = 0 ; public static Object arr [] = new Integer [5]; public static boolean flag = false ; public static void backtrack (int ss , int idx , int tks) { if (tks == 5 ) { // System.out.println(ss + " "); if (ss == 23 ) {//System.out.print(ss+" " ); flag = true ; return ; } }else { backtrack(ss+ (int)arr[idx+1], idx+1, tks+1); backtrack(ss*(int)arr[idx+1], idx+1, tks+1); backtrack(ss-(int)arr[idx+1], idx+1, tks+1); } } public static int length (int n) { int l = 0 ; while (n > 0 ) { l ++ ; n /=10 ; } return l ; } public static int GCD (int x , int y ) { if (x == y ) return x ; if (x > y ) return GCD(x-y , y ) ; else return GCD(x , y -x); } public static int low (int k ) { return (k)*(k+1)/2 ; } public static int high (int n , int k) { return (k)*(2*n-k+1)/2 ; } public static char [] letters = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' ,'P','Q','R','S','T','U','V','W','X','Y', 'Z'}; public static void main(String[] args) throws IOException, InterruptedException{ PrintWriter pw = new PrintWriter(System.out); Scanner sc = new Scanner (System.in); int n = sc.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); long pro = 1 ; for (int i = 1; i < n; i ++ ) { if (GCD(i , n ) == 1 ) { arr.add(i); pro *= i ; pro %= n ; } } if (pro != 1 ) arr.remove(arr.size()-1); pw.println(arr.size()); for (int i : arr ) { pw.print(i+" " ); } pw.close (); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } } } /* while (true) { int []a = new int [5]; for (int i = 0 ; i < 5 ; i ++ ) { arr[i]= sc.nextInt(); a[i]= (int)arr[i];; } if (a[0] == 0) break ; permutation(a, 5, 0, 0); pw.println(flag ? "Possible":"Impossible"); } */
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
95a6f895fdfbc1452501dd502a854606
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class A { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(ob.second - second); } } static class Tuple implements Comparable<Tuple>{ long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static boolean isPrime(long n) { if(n == 1) { return false; } for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Integer> SieveList(int n) { boolean prime[] = new boolean[n+1]; Arrays.fill(prime, true); ArrayList<Integer> l = new ArrayList<>(); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for(int i=p*p; i<=n; i += p) { prime[i] = false; } } } for (int p=2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int n = sc.nextInt(); long f = 1; LinkedList<Integer> list = new LinkedList<>(); for(int i = 1;i<n;i++) { int curr = gcd(i,n); if(curr == 1) { list.add(i); f = (f * i)%n; } } if(n > 2 && f == n-1) { list.pollLast(); f = 1; } fout.println(list.size()); for(int itc: list) fout.print(itc + " "); fout.close(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
ea0c415381882f37d808f3a2963efbab
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import static java.lang.Math.*; import static java.lang.System.out; import java.util.*; import java.io.PrintStream; import java.io.PrintWriter; import java.math.BigInteger; public class A { /* 10^(7) = 1s. * ceilVal = (a+b-1) / b */ static final int mod = 1000000007; static final long temp = 998244353; static final long MOD = 1000000007; static final long M = (long)1e9+7; static class Pair implements Comparable<Pair> { long first, second; public Pair(long first, long second) { this.first = first; this.second = second; } public int compareTo(Pair ob) { return (int)(ob.second - second); } } static class Tuple implements Comparable<Tuple>{ long first, second,third; public Tuple(long first, long second, long third) { this.first = first; this.second = second; this.third = third; } public int compareTo(Tuple o) { return (int)(o.third - this.third); } } public static class DSU { int[] parent; int[] rank; //Size of the trees is used as the rank public DSU(int n) { parent = new int[n]; rank = new int[n]; Arrays.fill(parent, -1); Arrays.fill(rank, 1); } public int find(int i) //finding through path compression { return parent[i] < 0 ? i : (parent[i] = find(parent[i])); } public boolean union(int a, int b) //Union Find by Rank { a = find(a); b = find(b); if(a == b) return false; //if they are already connected we exit by returning false. // if a's parent is less than b's parent if(rank[a] < rank[b]) { //then move a under b parent[a] = b; } //else if rank of j's parent is less than i's parent else if(rank[a] > rank[b]) { //then move b under a parent[b] = a; } //if both have the same rank. else { //move a under b (it doesnt matter if its the other way around. parent[b] = a; rank[a] = 1 + rank[a]; } return true; } } static class Reader { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) throws IOException { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] longReadArray(int n) throws IOException { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static boolean isPrime(long n) { if(n == 1) { return false; } for(int i = 2;i*i<=n;i++) { if(n%i == 0) { return false; } } return true; } public static List<Integer> SieveList(int n) { boolean prime[] = new boolean[n+1]; Arrays.fill(prime, true); ArrayList<Integer> l = new ArrayList<>(); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for(int i=p*p; i<=n; i += p) { prime[i] = false; } } } for (int p=2; p<=n; p++) { if (prime[p] == true) { l.add(p); } } return l; } public static int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a%b); } public static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static long LongGCD(long a, long b) { if(b == 0) return a; else return LongGCD(b,a%b); } public static long LongLCM(long a, long b) { return (a / LongGCD(a, b)) * b; } //Count the number of coprime's upto N public static long phi(long n) //euler totient/phi function { long ans = n; // for(long i = 2;i*i<=n;i++) // { // if(n%i == 0) // { // while(n%i == 0) n/=i; // ans -= (ans/i); // } // } // // if(n > 1) // { // ans -= (ans/n); // } for(long i = 2;i<=n;i++) { if(isPrime(i)) { ans -= (ans/i); } } return ans; } public static long fastPow(long x, long n) { if(n == 0) return 1; else if(n%2 == 0) return fastPow(x*x,n/2); else return x*fastPow(x*x,(n-1)/2); } public static long modPow(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return modPow(n, p - 2, p); } // Returns nCr % p using Fermat's little theorem. public static long nCrModP(long n, long r,long p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[(int)(n)] * modInverse(fac[(int)(r)], p) % p * modInverse(fac[(int)(n - r)], p) % p) % p; } public static long fact(long n) { long[] fac = new long[(int)(n) + 1]; fac[0] = 1; for (long i = 1; i <= n; i++) fac[(int)(i)] = fac[(int)(i - 1)] * i; return fac[(int)(n)]; } public static long nCr(long n, long k) { long ans = 1; for(long i = 0;i<k;i++) { ans *= (n-i); ans /= (i+1); } return ans; } public static void Sort(int[] a) { List<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); Collections.reverse(l); //Use to Sort decreasingly for (int i=0; i<a.length; i++) a[i]=l.get(i); } public static void ssort(char[] a) { List<Character> l = new ArrayList<>(); for (char i : a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } //Modular Operations for Addition and Multiplication. public static long perfomMod(long x) { return ((x%M + M)%M); } public static long addMod(long a, long b) { return perfomMod(perfomMod(a)+perfomMod(b)); } public static long subMod(long a, long b) { return perfomMod(perfomMod(a)-perfomMod(b)); } public static long mulMod(long a, long b) { return perfomMod(perfomMod(a)*perfomMod(b)); } /* * * >= <= 0 1 2 3 4 5 6 7 5 5 5 6 6 6 7 7 lower_bound for 6 at index 3 (>=) upper_bound for 6 at index 6(To get six reduce by one) (<=) */ public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } public static void main(String[] args) throws Exception { Reader sc = new Reader(); PrintWriter fout = new PrintWriter(System.out); int n = sc.nextInt(); long f = 1; List<Integer> list = new ArrayList<>(); for(int i = 1;i<n;i++) { int curr = gcd(i,n); if(curr == 1) { list.add(i); f = (f * i)%n; } } if(n > 2 && f == n-1) { list.remove(list.size()-1); f = 1; } fout.println(list.size()); for(int itc: list) fout.print(itc + " "); fout.close(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
ae5d7ebd65dfa1812494913233d02e5d
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C { static class FastScanner { int idk; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] nextArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class FastWriter extends PrintWriter { FastWriter() { super(System.out); } void println(int[] array) { for (int i = 0; i < array.length; i++) { print(array[i] + " "); } println(); } void println(long[] array) { long ans[]=new long[10]; for (int i = 0; i < array.length; i++) { print(array[i] + " "); } println(); } } public static void main(String[] args) { // Yo bitch !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! FastScanner in = new FastScanner(); FastWriter out = new FastWriter(); int n=in.nextInt(); ArrayList<Integer> list=new ArrayList<>(); long ans=1; for(int i=1;i<n;i++){ if(gcd(i,n)==1){ ans*=i;ans%=n; list.add(i); } } if(ans!=1){ list.remove(list.size()-1); } out.println(list.size()); for (int i = 0; i < list.size(); i++) { out.print(list.get(i)+" "); } out.println(); out.close(); } static long gcd(long a, long b) { if(b > a) return gcd(b, a); else if(b == 0) return a; else return gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
09cb284b6430afb0f907857775d9ee29
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class codeforces1514C { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringBuilder ans = new StringBuilder(); int answer = 0; long mod = 1; ArrayList<Integer> set = new ArrayList<>(); for (int i = 1; i<n;i++) { if (gcd(i,n)==1) { mod*=i; mod%=n; set.add(i); } } if (mod!=1) { for (int i = 0; i<set.size();i++) { if (set.get(i) == mod) { set.remove(i); } } } for (int i = 0; i<set.size();i++) { ans.append(set.get(i)+" "); answer++; } System.out.println(answer); System.out.println(ans); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
fdf346738bcd863619f9fc26be10c63c
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
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 Solution { static int mod=1000000007; static FastReader sc=new FastReader(); public static long gcd(long a,long b) { if(b==0)return a; else { return(gcd(b,a%b)); } } public static void main (String[] args) throws java.lang.Exception { long n=sc.nextLong(); long mul=1; ArrayList<Long> al=new ArrayList<Long>(); for(long i=1;i<n-1;++i) { if(gcd(i,n)==1) { al.add(i); mul=(mul*i)%n; } } if((mul*(n-1))%n==1) { al.add(n-1); } System.out.println(al.size()); for(long i:al) { System.out.print(i+" "); } System.out.println(); } static void printintarray(int[] arr) { int n=arr.length; for(int i=0;i<n;++i) { System.out.print(arr[i]+" "); } System.out.println(); } static void printlongarray(long[] arr) { int n=arr.length; for(int i=0;i<n;++i) { System.out.print(arr[i]+" "); } System.out.println(); } static int[] arrayintinput(int n) { int arr[]=new int[n]; for(int i=0;i<n;++i) { arr[i]=sc.nextInt(); } return arr; } static long[] arraylonginput(int n) { long arr[]=new long[n]; for(int i=0;i<n;++i) { arr[i]=sc.nextLong(); } return arr; } 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; } } } // System.out.println("Case #"+(y+1)+": "+ans);
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
f4b4d223cfc05257f08c0a848aaef303
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
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 Solution { static int mod=1000000007; static FastReader sc=new FastReader(); public static long gcd(long a,long b) { if(b==0)return a; else { return(gcd(b,a%b)); } } public static long multiply(long a,long b) { return((a%mod * b%mod)%mod); } public static void main (String[] args) throws java.lang.Exception { StringBuilder sb=new StringBuilder(); long n=sc.nextLong(); long mul=1; int count=0; ArrayList<Long> al=new ArrayList<Long>(); for(long i=1;i<n-1;++i) { if(gcd(i,n)==1) { al.add(i); ++count; sb.append(i+" "); mul=(mul*i)%n; } } if((mul*(n-1))%n==1) { ++count; sb.append((n-1)+" "); } System.out.println(count); System.out.println(sb.toString()); } static void printintarray(int[] arr) { int n=arr.length; for(int i=0;i<n;++i) { System.out.print(arr[i]+" "); } System.out.println(); } static void printlongarray(long[] arr) { int n=arr.length; for(int i=0;i<n;++i) { System.out.print(arr[i]+" "); } System.out.println(); } static int[] arrayintinput(int n) { int arr[]=new int[n]; for(int i=0;i<n;++i) { arr[i]=sc.nextInt(); } return arr; } static long[] arraylonginput(int n) { long arr[]=new long[n]; for(int i=0;i<n;++i) { arr[i]=sc.nextLong(); } return arr; } 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; } } } // System.out.println("Case #"+(y+1)+": "+ans);
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
bcf92b587dd149c87fd19fc59294223e
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
//package competition.cf.cf716; import java.util.Scanner; //public class C { // private final static Scanner scanner = new Scanner(System.in); // private final static long MOD = 1000000007L; // // public static void main(String[] args){ //素数筛 // // // int n = scanner.nextInt(); // boolean[] isPrime = new boolean[n + 1]; // int[] prime = new int[n + 1]; // // for(int i = 0 ; i <= n ; i++) isPrime[i] = true; // // isPrime[0] = isPrime[1] = false; // int tot = 0; // for(int i = 2 ; i <= n ; i++){ //和n不能有相同的质数因子,因为会将n约小 // if(isPrime[i]) prime[tot++] = i; // for(int j = 0 ; j < tot && i * prime[j] <= n ; j++){ // isPrime[i*prime[j]]=false; // if((i % prime[j]) == 0) break; // } // } // // // int cnt = 0; // int[] cntArray = new int[n]; // for (int i = 0; i < tot; i++) { // if((n % prime[i]) == 0) cntArray[cnt++] = prime[i]; // } // // long a = 1; // int len = 0; // int[] ans = new int[n]; // for (int i = 1 , j ; i < n - 1; i++) { // for (j = 0; j < cnt; j++) { // if((i % cntArray[j]) == 0) break; // } // if(j >= cnt){ // a = (a * i) % n; // ans[len++] = i; // } // } // if(((a * (n - 1)) % n) == 1){ // ans[len++] = (n - 1); // a = (a * (n - 1)) % n; // } // // // System.out.println(len); // for (int i = 0; i < len; i++) { // System.out.print(ans[i]); // if(i != len - 1) System.out.print(" "); // else System.out.println(); // } // } //} public class C { private final static Scanner scanner=new Scanner(System.in); private static int[] array = new int[100000+5]; public static void main(String[] args){ // int[] a=new int[100]; int n=scanner.nextInt(); int[] prime = new int[n + 1]; array[0] = 1; array[1] = 1; int tot=0; for (int i = 2; i <=n; i++) { if(array[i]!=1) prime[tot++] = i; if (array[i] == 0) { for (int j = i ; j <= n; j += i) { array[j] = 1; } } } int cnt = 0; int[] cntArray = new int[n]; for (int i = 0; i < tot; i++) { if((n % prime[i]) == 0) cntArray[cnt++] = prime[i]; } long a = 1; int len = 0; int[] ans = new int[n]; for (int i = 1 , j ; i < n - 1; i++) { for (j = 0; j < cnt; j++) { if((i % cntArray[j]) == 0) break; } if(j >= cnt){ a = (a * i) % n; ans[len++] = i; } } if(((a * (n - 1)) % n) == 1){ ans[len++] = (n - 1); a = (a * (n - 1)) % n; } System.out.println(len); for (int i = 0; i < len; i++) { System.out.print(ans[i]); if(i != len - 1) System.out.print(" "); else System.out.println(); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
2daf05a84221f9283a8b4f5bbaa7d3e7
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.Scanner; public class C { private final static Scanner scanner = new Scanner(System.in); private final static long MOD = 1000000007L; public static void main(String[] args){ //素数筛 int n = scanner.nextInt(); boolean[] isPrime = new boolean[n + 1]; int[] prime = new int[n + 1]; for(int i = 0 ; i <= n ; i++) isPrime[i] = true; isPrime[0] = isPrime[1] = false; int tot = 0; for(int i = 2 ; i <= n ; i++){ if(isPrime[i]) prime[tot++] = i; for(int j = 0 ; j < tot && i * prime[j] <= n ; j++){ isPrime[i*prime[j]]=false; if((i % prime[j]) == 0) break; } } int cnt = 0; int[] cntArray = new int[n]; for (int i = 0; i < tot; i++) { if((n % prime[i]) == 0) cntArray[cnt++] = prime[i]; } long a = 1; int len = 0; int[] ans = new int[n]; for (int i = 1 , j ; i < n - 1; i++) { for (j = 0; j < cnt; j++) { if((i % cntArray[j]) == 0) break; } if(j >= cnt){ a = (a * i) % n; ans[len++] = i; } } if(((a * (n - 1)) % n) == 1){ ans[len++] = (n - 1); a = (a * (n - 1)) % n; } System.out.println(len); for (int i = 0; i < len; i++) { System.out.print(ans[i]); if(i != len - 1) System.out.print(" "); else System.out.println(); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
0628a02e31681464df7b5088c34ed3ae
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static final MyWriter writer = new MyWriter(); private static final MyReader scan = new MyReader(); public static void main(String[] args) throws Exception { Main main = new Main(); // int q = scan.nextInt(); while (q-- > 0) main.solve(); writer.close(); } void solve() throws Exception { int n = scan.nextInt(); ArrayList<Integer> res = new ArrayList<>(); res.add(1); long sum = 1; for (int i = 2; i < n; i++) { if (gcd(n, i) != 1) continue; res.add(i); sum *= i; sum %= n; } int abandon = sum == 1l ? -1 : (int)sum; writer.println(abandon == -1 ? res.size() : res.size() - 1); for (int i = 0; i < res.size(); i++) { if (res.get(i) != abandon) writer.print(res.get(i) + " "); } } int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } } class MyWriter implements Closeable { private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); public void print(Object object) throws IOException { writer.write(object.toString()); } public void println(Object object) throws IOException { writer.write(object.toString());writer.write(System.lineSeparator()); } public void close() throws IOException { writer.close(); } } class MyReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null;}} public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine();if (nextLine == null) return false;tokenizer = new StringTokenizer(nextLine); }return true; } public String nextLine() { tokenizer = new StringTokenizer("");return innerNextLine(); } public String next() { hasNext();return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
89b0c38222a2f9e3704bad03afde5329
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { private static final MyReader scan = new MyReader(); private static final MyWriter writer = new MyWriter(); public static void main(String[] args) throws IOException { // int q = scan.nextInt(); while (q-- > 0) solve(); writer.close(); } private static void solve() throws IOException { int n = scan.nextInt(); ArrayList<Integer> res = new ArrayList<>(); long sum = 1; for(int i = 1 ; i < n ; i ++) { if (gcd(n, i) == 1) { res.add(i); sum *= i; sum %= n; } } if (sum % n != 1) { res.remove(res.indexOf((int)(sum % n))); } writer.println(res.size()); for (int i = 0; i < res.size(); i++) { writer.print(res.get(i) + " "); } writer.println(""); } private static int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } } class MyReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine();if (nextLine == null) { return false; }tokenizer = new StringTokenizer(nextLine); }return true; } public String nextLine() { tokenizer = new StringTokenizer("");return innerNextLine(); } public String next() { hasNext();return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public Long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } class MyWriter implements Closeable { private BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); public void print(Object object) throws IOException { writer.write(object.toString()); } public void println(Object object) throws IOException { writer.write(object.toString());writer.write(System.lineSeparator()); } @Override public void close() throws IOException { writer.close(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
9d713f990adc853d64f6c75fce682cbe
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; import static java.lang.Math.*; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(); ArrayList<Integer> ans = solve(n); pw.println(ans.size()); for (int i = 0; i < ans.size(); i++) { pw.print(ans.get(i) + " "); } pw.close(); } static ArrayList<Integer> solve(int n) { long ans = 0; ArrayList<Integer> list = new ArrayList<>(); long p = 1; for (int i = 1; i < n; i++) { if (gcd(i, n) == 1){ list.add(i); p = p * i % n; } } ArrayList<Integer> res = new ArrayList<>(); if (p % n == 1) return list; for (Integer v: list) { if (p % n == v) continue; res.add(v); } return res; } static int gcd(int a, int b) { if (a == 0) return b; else return gcd(b % a, a); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
eeab3bca7d3e09a982fc6e75b52207f2
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; public class ProductOneModN { static final int MAXN = 1000_006; static final long MOD = (long) 1e9 + 7; public static void main(String[] args) throws Exception { MyScanner s = new MyScanner(); Print p = new Print(); Helper hp = new Helper(); int n = s.nextInt(); int[] arr = new int[100005]; long prod = 1; int cou = 0; for (int i = 1; i < n; i++) { if (hp.gcd(n, i) == 1) { arr[i] = 1; prod = (prod * (long) i) % (long) n; } } if (prod != 1) { arr[(int) prod] = 0; } for (int i = 1; i < n; i++) { if (arr[i] == 1) cou++; } System.out.println(cou); for(int i=1; i<n; i++) { if(arr[i] == 1) { System.out.print(i + " "); } } } public static class Pair implements Comparable<Pair> { int first; int second; public Pair(int a, int b) { this.first = a; this.second = b; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return o.first - first; } } public static class Helper { long MOD = (long) 1e9 + 7; int MAXN = 1000_006;; Random rnd; public Helper(long mod, int maxn) { MOD = mod; MAXN = maxn; rnd = new Random(); } public Helper() { } public static int[] sieve; public static ArrayList<Integer> primes; public void setSieve() { primes = new ArrayList<>(); sieve = new int[MAXN]; int i, j; for (i = 2; i < MAXN; ++i) if (sieve[i] == 0) { primes.add(i); for (j = i; j < MAXN; j += i) { sieve[j] = i; } } } public static long[] factorial; public void setFactorial() { factorial = new long[MAXN]; factorial[0] = 1; for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD; } public long getFactorial(int n) { if (factorial == null) setFactorial(); return factorial[n]; } public long ncr(int n, int r) { if (r > n) return 0; if (factorial == null) setFactorial(); long numerator = factorial[n]; long denominator = factorial[r] * factorial[n - r] % MOD; return numerator * pow(denominator, MOD - 2, MOD) % MOD; } public long[] getLongArray(int size, MyScanner s) throws Exception { long[] ar = new long[size]; for (int i = 0; i < size; ++i) ar[i] = s.nextLong(); return ar; } public int[] getIntArray(int size, MyScanner s) throws Exception { int[] ar = new int[size]; for (int i = 0; i < size; ++i) ar[i] = s.nextInt(); return ar; } public int[] getIntArray(String s) throws Exception { s = s.trim().replaceAll("\\s+", " "); String[] strs = s.split(" "); int[] arr = new int[strs.length]; for (int i = 0; i < strs.length; i++) { arr[i] = Integer.parseInt(strs[i]); } return arr; } public long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } public long max(long[] ar) { long ret = ar[0]; for (long itr : ar) ret = Math.max(ret, itr); return ret; } public int max(int[] ar) { int ret = ar[0]; for (int itr : ar) ret = Math.max(ret, itr); return ret; } public long min(long[] ar) { long ret = ar[0]; for (long itr : ar) ret = Math.min(ret, itr); return ret; } public int min(int[] ar) { int ret = ar[0]; for (int itr : ar) ret = Math.min(ret, itr); return ret; } public long sum(long[] ar) { long sum = 0; for (long itr : ar) sum += itr; return sum; } public long sum(int[] ar) { long sum = 0; for (int itr : ar) sum += itr; return sum; } public long pow(long base, long exp, long MOD) { base %= MOD; long ret = 1; while (exp > 0) { if ((exp & 1) == 1) ret = ret * base % MOD; base = base * base % MOD; exp >>= 1; } return ret; } static void swap(int ar[], int i, int j) { int temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } static void findNext(int ar[], int n) { int i; for (i = n - 1; i > 0; i--) { if (ar[i] > ar[i - 1]) { break; } } if (i == 0) { Arrays.sort(ar); } else { int x = ar[i - 1], min = i; for (int j = i + 1; j < n; j++) { if (ar[j] > x && ar[j] < ar[min]) { min = j; } } swap(ar, i - 1, min); Arrays.sort(ar, i, n); } } } static class Print { private 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(); } } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
a11f348b23710d1356f6f785bcaa8cd3
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.util.*; /** * * @author Anand */ public class Solution2 { /** * @param args the command line arguments */ static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n= scan.nextInt(); long prod=1; List<Integer> res = new ArrayList(); for(int i=1;i<n;i++){ if(gcd(i,n)==1){ prod*=i; prod%=n; res.add(i); } } if(prod==1){ System.out.println(res.size()); }else{ System.out.println(res.size()-1); } for(int x : res){ if(prod!=1&&x==prod) continue; System.out.print(x+" "); } System.out.println(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
21886f8ad8becf9365739be3f81e0a48
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=1; while(T-->0) { int n=input.nextInt(); long p=1; ArrayList<Integer> set=new ArrayList<>(); for(int i=1;i<n;i++) { int g=gcd(i,n); if(g==1) { p=(p*(long)i)%(long)n; set.add(i); } } if(p!=1) { for(int i=0;i<set.size();i++) { if(set.get(i)==p) { set.remove(i); break; } } } out.println(set.size()); for(int i=0;i<set.size();i++) { out.print(set.get(i)+" "); } out.println(); } out.close(); } public static int gcd(int a, int b) { if(a==0) { return b; } else { 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
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
5dff37818129ec5b38e19b9554718bf5
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /* Name of the class has to be "Main" only if the class is public. */ public class Coder { static int n; static int ok[] = new int[(int)1e5+5]; static StringBuilder str = new StringBuilder(""); static int gcd(int a, int b){ if(b==0) return a; return gcd(b, a%b); } static void solve() { long prod=1; int cnt=0; for(int i=1;i<n;i++){ if(gcd(i, n)==1){ ok[i]=1; prod=(prod *i)%n; } } if(prod!=1) ok[(int)prod]=0; for(int i=1;i<n;i++) if(ok[i]==1) cnt++; str.append(cnt).append("\n"); for(int i=1;i<n;i++) if(ok[i]==1) str.append(i).append(" "); } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); // int t = Integer.parseInt(bf.readLine().trim()); // while (t-- > 0) { n=Integer.parseInt(bf.readLine().trim()); solve(); // } System.out.print(str); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
48ec87ed9b046af4dc17df2b4861bf6c
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.io.*; import java.util.*; import java.util.StringTokenizer; import static java.lang.Math.*; public class C { 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); } } public int[] readIntArray(int n){ int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=nextInt(); } return arr; } public long[] readLongArray(int n){ long[] arr=new long[n]; for(int i=0;i<n;i++){ arr[i]=nextLong(); } return arr; } } public static int[] sort(int[] arr){ ArrayList<Integer> temp = new ArrayList<>(); for(int x:arr) temp.add(x); Collections.sort(temp); int i=0; for(int x:temp){ arr[i++]=x; } return arr; } public static int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } public static void main(String args[]) throws Exception { FastScanner sc = new FastScanner(); int T = 1; // T = sc.nextInt(); PrintWriter pw = new PrintWriter(System.out); StringBuilder sb = new StringBuilder(); while (T-- > 0) { solve(sc, pw, sb); } pw.print(sb); pw.close(); } public static void solve(FastScanner sc, PrintWriter pw, StringBuilder sb) throws Exception { int n=sc.nextInt(); TreeSet<Long> ans = new TreeSet<>(); long prod=1; for(int i=1;i<=n;i++){ if(gcd(i,n)==1){ ans.add((long)i); prod=(prod*i)%n; } } if(prod!=1) { ans.remove(prod%n); } sb.append(ans.size()+"\n"); for(long x:ans) sb.append(x+" "); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
76ade297e0786180ec346b045394e82f
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
/* "जय श्री कृष्णा" */ import java.util.*; import java.io.*; public class Product_1_modulo_name implements Runnable { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; ++i) arr[i] = nextInt(); return arr; } long[] nextLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; ++i) arr[i] = nextLong(); return arr; } double[] nextDoubleArray(int size) { double[] arr = new double[size]; for (int i = 0; i < size; ++i) arr[i] = nextDouble(); return arr; } } static int mod = (int) (1e9 + 7),N = (int) (1e5); static final PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static final FastReader fr = new FastReader(); // static final FastReader fr = new FastReader("/home/atish/Java_Text_editor/input.txt"); // static PrintWriter out; public static void main(String[] args) throws java.lang.Exception { // try { // out = new PrintWriter(new File("/home/atish/Java_Text_editor/output.txt")); // } catch (Exception e) { // e.printStackTrace(); // } new Thread(null, new Product_1_modulo_name(), "Main", 1 << 26).start(); } void solve() { int n = fr.nextInt(); List<Integer> al = new ArrayList<>(); long pod = 1; for (int i = 1; i < n; i++) { if (CP.i_gcd(n, i) == 1) { pod = (pod * i) % n; al.add(i); } } if (pod != 1) { al.remove(al.size() - 1); } out.print(al.size() + "\n"); for (int x : al) out.print(x + " "); } @Override public void run() { long start = System.nanoTime(); // Program Start boolean testcase = false; int t = testcase ? fr.nextInt() : 1; while (t-- > 0) { solve(); out.print("\n"); out.flush(); } long end = System.nanoTime(); // Program End // System.err.println("Time taken: " + (end - start) / 1000000 + " ms"); } static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static List<Integer> sieve(int size) { ArrayList<Integer> pr=new ArrayList<Integer>(); boolean prime[] = new boolean[size]; for(int i=2;i<prime.length;i++) prime[i] = true; for(int i=2; i*i< prime.length; i++) { if(prime[i]) { for(int j=i*i; j<prime.length; j+=i) { prime[j]=false; } } } for(int i=2; i<prime.length; i++) if(prime[i]) pr.add(i); return pr; } static long binary_Expo(long a, long b) { // calculating a^b long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static long Modular_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % mod; --b; } a = (a * a) % mod; b /= 2; } return res; } static int i_gcd(int a, int b) {// iterative way to calculate gcd. while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) {// here b is the remainder if (b == 0) return a; //because each time b will divide a. return gcd(b, a % b); } static long ceil_div(long a, long b) { // a numerator b denominator return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } static int upper_Bound(int a[], int x) {//closest to the left+1 int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int lower_Bound(int a[], int x) {//closest to the right int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static void sort(int a[]) { PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
de6ae28e0d526153b7df9735c96c1764
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
/* "जय श्री कृष्णा" */ import java.util.*; import java.io.*; public class Product_1_mod_n_716 implements Runnable { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; ++i) arr[i] = nextInt(); return arr; } long[] nextLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; ++i) arr[i] = nextLong(); return arr; } double[] nextDoubleArray(int size) { double[] arr = new double[size]; for (int i = 0; i < size; ++i) arr[i] = nextDouble(); return arr; } } static int mod = (int) (1e9 + 7); static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out, true); // static FastReader fr = new // FastReader("D://DP/src/Codeforces/dp_1700/dp1700.txt"); public static void main(String[] args) throws java.lang.Exception { new Thread(null, new Product_1_mod_n_716(), "Main", 1 << 26).start(); } void solve() { int n = fr.nextInt(); List<Integer> al = new ArrayList<>(); long pod = 1; for (int i = 1; i < n; i++) { if (CP.i_gcd(n, i) == 1) { pod = (pod * i) % n; al.add(i); } } if (pod != 1) { al.remove(al.size() - 1); } out.print(al.size() + "\n"); for (int x : al) out.print(x + " "); } @Override public void run() { long start = System.nanoTime(); // Program Start boolean testcase = false; int t = testcase ? fr.nextInt() : 1; while (t-- > 0) { solve(); out.print("\n"); out.flush(); } long end = System.nanoTime(); // Program End System.err.println("Time taken: " + (end - start) / 1000000 + " ms"); } static class CP { static boolean isPrime(long n) { if (n <= 1) return false; if (n == 2 || n == 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } static List<Integer> sieve(int size) { ArrayList<Integer> pr = new ArrayList<Integer>(); boolean prime[] = new boolean[size]; for (int i = 2; i < prime.length; i++) prime[i] = true; for (int i = 2; i * i < prime.length; i++) { if (prime[i]) { for (int j = i * i; j < prime.length; j += i) { prime[j] = false; } } } for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i); return pr; } static long binary_Expo(long a, long b) { // calculating a^b long res = 1; while (b != 0) { if ((b & 1) == 1) { res *= a; --b; } a *= a; b /= 2; } return res; } static long Modular_Expo(long a, long b) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = (res * a) % mod; --b; } a = (a * a) % mod; b /= 2; } return res; } static int i_gcd(int a, int b) {// iterative way to calculate gcd. while (true) { if (b == 0) return a; int c = a; a = b; b = c % b; } } static long gcd(long a, long b) {// here b is the remainder if (b == 0) return a; // because each time b will divide a. return gcd(b, a % b); } static long ceil_div(long a, long b) { // a numerator b denominator return (a + b - 1) / b; } static int getIthBitFromInt(int bits, int i) { return (bits >> (i - 1)) & 1; } static int upper_Bound(int a[], int x) {// closest to the left+1 int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] <= x) l = m; else r = m; } return l + 1; } static int lower_Bound(int a[], int x) {// closest to the right int l = -1, r = a.length; while (l + 1 < r) { int m = (l + r) >>> 1; if (a[m] >= x) r = m; else l = m; } return r; } static void l_sort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int x : a) al.add(x); Collections.sort(al); for (int i = 0; i < a.length; i++) a[i] = al.get(i); } static void heap_sort(int a[]) { // heap sort PriorityQueue<Integer> q = new PriorityQueue<>(); for (int i = 0; i < a.length; i++) q.add(a[i]); for (int i = 0; i < a.length; i++) a[i] = q.poll(); } static void ruffle_sort(int[] a) { // random_shuffle Random r = new Random(); int n = a.length; for (int i = 0; i < n; i++) { int oi = r.nextInt(n); int temp = a[i]; a[i] = a[oi]; a[oi] = temp; } // sort Arrays.sort(a); } static int getMax(int arr[], int n) { int mx = arr[0]; for (int i = 1; i < n; i++) if (arr[i] > mx) mx = arr[i]; return mx; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
8494713e13b44dee77a7e1bf435e10f4
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import javafx.util.Pair; public class Main { public static long GCD(Long n1, Long n2) { if (n1 == 0) { return n2; } return GCD(n2 % n1, n1); } public static void main(String args[]) { FastScanner input = new FastScanner(); TreeSet<Long> set = new TreeSet<>(); long n = input.nextLong(); long pro = 1; for (long i = 1; i <n; i++) { if(GCD(n, i)==1) { pro = (pro*i)%n; set.add(i); } } if(pro!=1) { set.remove(pro); } StringBuilder ans = new StringBuilder(); ans.append(set.size()+"\n"); for (Long long1 : set) { ans.append(long1+" "); } System.out.println(ans); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
9b0c0ca42834bacbae3708c07fae9484
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import javafx.util.Pair; public class Main { public static long GCD(Long n1, Long n2) { if (n1 == 0) { return n2; } return GCD(n2 % n1, n1); } public static void main(String args[]) { FastScanner input = new FastScanner(); TreeSet<Long> set = new TreeSet<>(); long n = input.nextLong(); long pro = 1; for (long i = 1; i <n; i++) { if(GCD(n, i)==1) { pro = (pro*i)%n; set.add(i); } } if(pro!=1) { set.remove(pro); } System.out.println(set.size()); for (Long long1 : set) { System.out.print(long1+" "); } System.out.println(""); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
59f59e05d3e145dd17a55c65bdf825c5
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
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.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; 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); solve(in, out); out.close(); } static String reverse(String s) { return (new StringBuilder(s)).reverse().toString(); } static void sieveOfEratosthenes(int n, int factors[]) { n=1000000001; factors[1]=1; for(int p = 2; p*p <=n; p++) { if(factors[p] == 0) { factors[p]=p; for(int i = p*p; i <= n; i += p) factors[i] = p; } } } static void sort(int ar[]) { int n = ar.length; ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static void sort1(long ar[]) { int n = ar.length; ArrayList<Long> a = new ArrayList<>(); for (int i = 0; i < n; i++) a.add(ar[i]); Collections.sort(a); for (int i = 0; i < n; i++) ar[i] = a.get(i); } static long ncr(long n, long r, long mod) { if (r == 0) return 1; long val = ncr(n - 1, r - 1, mod); val = (n * val) % mod; val = (val * modInverse(r, mod)) % mod; return val; } public static int convert(String a, String b) { int T=0; if(b.charAt(0)=='P') { if(a.charAt(0)=='1'&&a.charAt(1)=='2') T=12; else T=(a.charAt(0)-'0')*10+(a.charAt(1)-'0')+12; } else if(b.charAt(0)=='A') { if(a.charAt(0)=='1'&&a.charAt(1)=='2') T=0; else T=(a.charAt(0)-'0')*10+(a.charAt(1)-'0'); } T=T*100+(a.charAt(3)-'0')*10+(a.charAt(4)-'0'); return T; } static boolean areBracketsBalanced(String expr) { // Using ArrayDeque is faster than using Stack class Deque<Character> stack = new ArrayDeque<Character>(); // Traversing the Expression for (int i = 0; i < expr.length(); i++) { char x = expr.charAt(i); if (x == '(' || x == '[' || x == '{') { // Push the element in the stack stack.push(x); continue; } // IF current current character is not opening // bracket, then it must be closing. So stack // cannot be empty at this point. if (stack.isEmpty()) return false; char check; switch (x) { case ')': check = stack.pop(); if (check == '{' || check == '[') return false; break; case '}': check = stack.pop(); if (check == '(' || check == '[') return false; break; case ']': check = stack.pop(); if (check == '(' || check == '{') return false; break; } } // Check Empty Stack return (stack.isEmpty()); } public static int hr(String s) { int hh=0; hh=(s.charAt(0)-'0')*10+(s.charAt(1)-'0'); return hh; } public static int mn(String s) { int mm=0; mm=(s.charAt(3)-'0')*10+(s.charAt(4)-'0'); return mm; } /* * public static void countNumberOfPrimeFactors(){ boolean flag[]=new * boolean[N]; Arrays.fill(flag,true); for(int i=2;i<N;i++){ if(flag[i]){ * for(int j=i;j<N;j+=i) { prime[j]++; flag[j]=false; } } } // prime[1]=1; } */ public static String rev(String s) { char[] temp=s.toCharArray(); for(int i=0;i<s.length()/2;i++) { char tmp=temp[i]; temp[i]=temp[s.length()-1-i]; temp[s.length()-1-i]=tmp; } String str=""; for(char ch: temp) { str+=ch; } return str; } static boolean isP(String str) { // Pointers pointing to the beginning // and the end of the string int i = 0, j = str.length() - 1; // While there are characters to compare while (i < j) { // If there is a mismatch if (str.charAt(i) != str.charAt(j)) return false; // Increment first pointer and // decrement the other i++; j--; } // Given string is a palindrome return true; } public static boolean isSorted(List<Integer> l) { int n=l.size(),i=0; for(i=1;i<l.size();i++) { if(l.get(i-1)<l.get(i)) return false; } return true; } public static boolean ln(long n, int c) { int l=0; long t=n; while(t!=0) { t/=10; l++; } return l==c; } public static void solve(InputReader sc, PrintWriter pw) { // int t=sc.nextInt(); // while(t-->0) { int n=sc.nextInt(); List<Integer> l=new ArrayList<>(); StringBuilder sb=new StringBuilder(); long ans=1; l.add(1); for(int i=2;i<n;i++) { if(_gcd(n,i)==1) { l.add(i); ans=(ans*i)%n; } } if(ans!=1) {l.remove(l.size()-1);} sb.append(l.size()).append("\n"); for(Integer x: l) sb.append(x).append(" "); pw.print(sb.toString()); // } } static class Pair1 { long a; long b; Pair1(long a, long b) { this.a = a; this.b = b; } } static class Pair implements Comparable<Pair> { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { if(a!=p.a) return p.a-a; return b-p.b; } } static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int _gcd(int a, int b) { if (b == 0) return a; return _gcd(b, a % b); } static long fast_pow(long base, long n, long M) { if (n == 0) return 1; if (n == 1) return base % M; long halfn = fast_pow(base, n / 2, M); if (n % 2 == 0) return (halfn * halfn) % M; else return (((halfn * halfn) % M) * base) % M; } static long modInverse(long n, long M) { return fast_pow(n, M - 2, M); } public static int LowerBound(int a[], int x) { int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } public static int UpperBound(int a[], int x) { int l=-1, r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String nextLine() { return 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
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
1a996f6f54867da022016b0044d16e3c
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
// package codeforces; import java.util.*; public class C1514 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); long mul = 1; // long [] dp = new long[n+1]; // 1 2 3 4 5 6 7 8 9 // 1, ...,n-2, n-1 n^2 - 3n + 2; // kn + 1 int[] set = new int[n]; List<Long> arr = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1) { mul = (mul * i) % n; arr.add((long)i); } } // System.out.println(mul); if (mul != 1) arr.remove((long)mul); // int [] ans = new int[arr.size()]; System.out.println(arr.size()); for (int i = 0; i < arr.size(); i++) { // ans[i] = arr.get(i); System.out.print(arr.get(i) + " "); // if (i == 78460) // System.out.println("asdasd"); } // System.out.println(mul); // while (mul) } private static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a%b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
81845fe7586db5e31a1fa96aa1362621
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class c716c { //By shwetank_verma public static void main(String[] args) { FastReader sc=new FastReader(); int t=1; // t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int a[]=new int[n-1]; for(int i=1;i<n;i++){ a[i-1]=i; } long v=n; TreeSet<Integer> res=new TreeSet<>(); long p=1; for(int i:a) { if(gcd(i,n)==1) { res.add(i); p=((p%n)*(i%n))%n; } } if(p!=1) res.remove((int)p); StringBuilder sb=new StringBuilder(); System.out.println(res.size()); for(int i:res) { sb.append(i+" "); } System.out.println(sb); } } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int mod=1000000007; static boolean primes[]=new boolean[1000007]; static ArrayList<Integer> b=new ArrayList<>(); static boolean seive(int n){ Arrays.fill(primes,true); primes[0]=primes[1]=false; for(int i=2;i*i<=n;i++){ if(primes[i]==true){ for(int p=i*i;p<=n;p+=i){ primes[p]=false; } } } if(n<1000007){ for(int i=2;i<=n;i++) { if(primes[i]) b.add(i); } return primes[n]; } return false; } static int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } static long GCD(long a,long b){ if(b==0) return a; return GCD(b,a%b); } static ArrayList<Integer> segseive(int l,int r){ ArrayList<Integer> isprime=new ArrayList<Integer>(); boolean p[]=new boolean[r-l+1]; Arrays.fill(p, true); for(int i=0;b.get(i)*b.get(i)<=r;i++) { int currprime=b.get(i); int base=(l/currprime)*currprime; if(base<l) { base+=currprime; } for(int j=base;j<=r;j+=currprime) { p[j-l]=false; } if(base==currprime) { p[base-l]=true; } } for(int i=0;i<=r-l;i++) { if(p[i]) isprime.add(i+l); } return isprime; } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
479c6b5d3516d9b9905b7fcce6b5bf23
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.StringTokenizer; /** * * @author eslam */ public class IceCave { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader input = new FastReader(); static int grid[][] = {{0, 0, 1, -1, 1, 1, -1, -1}, {1, -1, 0, 0, 1, -1, 1, -1}}; static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); static int dp[][] = new int[2009][2009]; public static void main(String[] args) throws IOException { long n = input.nextInt(); ArrayList<Long> a = new ArrayList<>(); ArrayList<Long> b = new ArrayList<>(); int size = 1; for (long i = 1; i < n; i++) { if (GCD(n, i) == 1) { a.add(i); b.add(i); } } for (int i = 1; i < a.size(); i++) { a.set(i, (a.get(i) * a.get(i - 1)) % n); } for (int i = 1; i < a.size(); i++) { if (a.get(i) == 1) { size = i + 1; } } log.write(size + "\n"); for (int i = 0; i < size; i++) { log.write(b.get(i)+" "); } log.write("\n"); log.flush(); } public static long binaryToDecimal(String w) { long r = 0; long l = 0; for (int i = w.length() - 1; i > -1; i--) { long x = (w.charAt(i) - '0') * (long) Math.pow(2, l); r = r + x; l++; } return r; } public static String decimalToBinary(long n) { String w = ""; while (n > 0) { w = n % 2 + w; n /= 2; } return w; } //my Algo public static boolean isSorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } public static void print(int a[]) throws IOException { for (int i = 0; i < a.length; i++) { log.write(a[i] + " "); } log.write("\n"); } public static void read(long[] a) { for (int i = 0; i < a.length; i++) { a[i] = input.nextInt(); } } static class pair { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } } public static long GCD(long x, long y) { if (y == 0) { return x; } return GCD(y, x % y); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
5569ef75b455678e13a3ba65d6681d83
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class test { public static void main(String[] args) throws Exception{ FastScanner fs = new FastScanner(); int T = 1; /// System.out.println(gcd(14,56)); //T=fs.nextInt(); for (int tt = 0; tt < T; tt++) { long n = fs.nextLong(); StringBuilder sb = new StringBuilder(); ArrayList<Long>s = new ArrayList<>(); long p=1; s.add(1l); for(long i=2;i<n;i++ ){ if(gcd(i,n)==1){ s.add(i); p=(p*i)%n; } } if(p==1){ System.out.println(s.size()); for(long i:s){ System.out.print(i+" "); } }else{ // System.out.println(p); // System.out.println(s); int cnt=0; for(long i:s){ if(i==p)continue; sb.append(i+" "); cnt+=1; } System.out.println(cnt); System.out.println(sb.toString()); } } } static long gcd(long a,long b){ if(a==0){ return b; } if(b==0){ return a; } if(b>a){ return gcd(b,a); } return gcd(b,a%b); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long[] readArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
11525d4058300ab08c4ca2d8f2811dee
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class codeforcesD{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int gcd(int a,int b){if(b==0){return a;}return gcd(b,a%b);} public static void main(String args[]){ FastReader sc=new FastReader(); int n=sc.nextInt(); StringBuilder sb=new StringBuilder(); Set<Integer> set=new HashSet<>(); for(int i=1;i<=n;i++){set.add(i);} for(int i=2;i<=n;i++){ if(gcd(i,n)!=1){set.remove(i);} } long ans=1; for(int w:set){ans=(ans*w)%n;} ans=ans%n; List<Integer> list=new ArrayList<>(); if(ans!=1){set.remove((int)ans);} for(int w:set){list.add(w);} Collections.sort(list); for(int w:list){sb.append(w+" ");} System.out.println(set.size()); System.out.println(sb.toString()); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
88a27c9f8f0f5a650ce6db290739252d
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class C { public static BufferedReader br; public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws IOException { readInput(); out.close(); } static long inv(long a, long b) {return 1 < a ? b - inv(b%a,a) * b/a:1;} static long gcd(long a, long b) { while (b != 0) { long t = a; a = b; b = t % b; } return a; } public static void readInput() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new FileReader("in.in")); int n =Integer.parseInt(br.readLine()); List<Long> ans = new ArrayList<Long>(); long sol = 1; for (long i = 1; i <= n; i++) if (gcd(i,n) == 1) { ans.add(i); sol *= i; sol %= n; } if (sol != 1) ans.remove(Long.valueOf(sol)); out.println(ans.size()); for (long x: ans) out.print(x + " "); out.println(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
dc83ae1cf314ef8bd9405a3c6eb41732
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.util.Map.Entry; import java.io.*; import java.math.*; import static java.util.stream.Collectors.*; import static java.util.Map.Entry.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws IOException { final long mod=(long) (1e9+7); Reader s=new Reader(); PrintWriter pt=new PrintWriter(System.out); // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); // int T=s.nextInt(); // int T=Integer.parseInt(br.readLine()); int T=1; while(T-->0) { int n=s.nextInt(); TreeSet<Integer> ts=new TreeSet<Integer>(); for(int i=1;i<n;i++){ if(gcd(i, n)==1){ ts.add(i); } } long p=1; for(int i:ts){ p=(p*i)%n; } if(p>1) ts.remove((int)p); pt.println(ts.size()); for(int i:ts){ pt.print(i+" "); } pt.println(); } pt.close(); } static int setBit(int S, int j) { return S | 1 << j; } static int clearBit(int S, int j) { return S & ~(1 << j); } static int toggleBit(int S, int j) { return S ^ 1 << j; } static boolean isOn(int S, int j) { return (S & 1 << j) != 0; } static int turnOnLastZero(int S) { return S | S + 1; } static int turnOnLastConsecutiveZeroes(int S) { return S | S - 1; } static int turnOffLastBit(int S) { return S & S - 1; } static int turnOffLastConsecutiveBits(int S) { return S & S + 1; } static int lowBit(int S) { return S & -S; } static int setAll(int N) { return (1 << N) - 1; } static int modulo(int S, int N) { return (S & N - 1); } //S%N, N is a power of 2 static boolean isPowerOfTwo(int S) { return (S & S - 1) == 0; } static boolean isWithin(long x, long y, long d, long k) { return x*k*x*k + y*k*y*k <= d*d; } static long modFact(long n, long p) { if (n >= p) return 0; long result = 1; for (int i = 1; i <= n; i++) result = (result * i) % p; return result; } static int sum(int[] arr, int n) { int inc[]=new int[n+1]; int dec[]=new int[n+1]; inc[0] = arr[0]; dec[0] = arr[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (arr[j] > arr[i]) { dec[i] = max(dec[i], inc[j] + arr[i]); } else if (arr[i] > arr[j]) { inc[i] = max(inc[i], dec[j] + arr[i]); } } } return max(inc[n - 1], dec[n - 1]); } static long nc2(long a) { return a*(a-1)/2; } public static int numberOfprimeFactors(int n) { // Print the number of 2s that divide n HashSet<Integer> hs = new HashSet<Integer>(); while (n%2==0) { hs.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 <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { hs.add(i); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) hs.add(n); return hs.size(); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static void reverse(int arr[],int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static void reverse(long arr[],int start, int end) { long temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int p2(int n) { int k=0; while(n>1) { if(n%2!=0) return k; n/=2; k++; } return k; } static boolean isp2(int n) { while(n>1) { if(n%2==1) return false; n/=2; } return true; } static int binarySearch(int arr[], int first, int last, int key){ int mid = (first + last)/2; while( first <= last ){ if ( arr[mid] < key ){ first = mid + 1; }else if ( arr[mid] == key ){ return mid; }else{ last = mid - 1; } mid = (first + last)/2; } return -1; } static void print(int a[][]) { for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) System.out.print(a[i][j]+" "); System.out.println(); } } static int max (int x, int y) { return (x > y)? x : y; } static int search(Pair[] p, Pair pair) { int l=0, r=p.length; while (l <= r) { int m = l + (r - l) / 2; if (p[m].compareTo(pair)==0) return m; if (p[m].compareTo(pair)<0) l = m + 1; else r = m - 1; } return -1; } static void pa(int a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static void pa(long a[]) { for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static void reverseArray(int arr[], int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } static boolean isPalindrome(String s) { int l=s.length(); for(int i=0;i<l/2;i++) { if(s.charAt(i)!=s.charAt(l-i-1)) return false; } return true; } static long nc2(long n, long m) { return (n*(n-1)/2)%m; } static long c(long a) { return a*(a+1)/2; } static int next(long[] arr, long target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to right side if target is // greater. if (arr[mid] < target) { start = mid + 1; } // Move left side. else { ans = mid; end = mid - 1; } } return ans; } static int prev(long[] arr, long target) { int start = 0, end = arr.length - 1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; // Move to left side if target is // lesser. if (arr[mid] > target) { end = mid - 1; } // Move right side. else { ans = mid; start = mid + 1; } } return ans; } static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long modInverse(long n, long p) { return power(n, p-2, p); } static long nCrModP(long n, long r, long p) { if(r>n) return 0; if (r == 0) return 1; long[] fac = new long[(int) (n+1)]; fac[0] = 1; for (int i = 1 ;i <= n; i++) fac[i] = fac[i-1] * i % p; return (fac[(int) n]* modInverse(fac[(int) r], p) % p * modInverse(fac[(int) (n-r)], p) % p) % p; } static String reverse(String str) { return new StringBuffer(str).reverse().toString(); } static long fastpow(long x, long y, long m) { if (y == 0) return 1; long p = fastpow(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } static boolean isPerfectSquare(long l) { return Math.pow((long)Math.sqrt(l),2)==l; } static void merge(long[] arr, int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long [n1]; long R[] = new long [n2]; /*Copy data to temp arrays*/ 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]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array 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++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ 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]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array 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++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static class Pair implements Comparable<Pair>{ int a; int b; Pair(int a,int b){ this.a=a; this.b=b; } public int compareTo(Pair p){ if(a>p.a) return 1; if(a==p.a) return (b-p.b); return -1; } } 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[128]; // 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
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
819679cc3e3ce695317d9ed99dd72102
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; public class C{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); boolean a[] = new boolean[n]; a[1] = true; long product = 1l; for(int i = 1 ; i<n ; i++){ if(gcd(i,n) == 1){ a[i] = true; product = (product*i) % n; } } int count = 0; if(product != 1){a[(int)product] = false;} StringBuilder sb = new StringBuilder(); for(int i = 1 ; i < n ; i++){ if(a[i] == true) { count++; sb.append(i+" "); } } System.out.println(count); System.out.println(sb.toString()); } public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
d7879d90eacb4ea293ce9c28dcef01f9
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
//package practice; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.*; public class Main { // // static long MAXSIEVE =100000000; // static long MAXSIEVEHALF = (MAXSIEVE/2); // static long MAXSQRT =5000; // static char a[]=new char[(int)MAXSIEVE/16+2]; // // static long isPrime(long n) { // return (a[(int)(n)>>4]&(1<<(((n)>>1)&7))); // } // // int i,j; // // memset(a,255,sizeof(a)); // a[0]=0xFE; // 254 // for(i=1;i<MAXSQRT;i++) // if (a[i>>3]&(1<<(i&7))) // for(j=i+i+i+1;j<MAXSIEVEHALF;j+=i+i+1) // a[(int)j>>3]&=~(1<<(j&7)); static FastReader sc=new FastReader(); static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int gcd(int a,int b) { if(b<=0) { return a; } return gcd(b,a%b); } static int digSum(int n) { if (n == 0) return 0; return (n % 9 == 0) ? 9 : (n % 9); } static Set<Integer> list=new HashSet<>(); static void sieveOfEratosthenes(int n) { boolean prime[] = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for(int i = 2; i <= n; i++) { if(prime[i] == true) { list.add(i); } } } public static void main(String[] args) throws IOException { FastReader sc=new FastReader(); int n = sc.nextInt(); List<Integer> list=new ArrayList<>(); long product=1; int len=0; for(int i=1;i<n;i++) { if(gcd(i, n)==1) { list.add(i); product =(product*i)%n; if(product==1) { len=list.size(); } } } System.out.println(len); StringBuilder sb=new StringBuilder(); for(int i=0;i<len;i++) { sb.append(list.get(i)+" "); } System.out.println(sb); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
776bf06cb623d0a1634e1b5ae0f89b01
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class product1ModuloN { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int n = sc.nextInt(); int[] visited = new int[n-1]; int count = 0; long product = 1; for(int i=0;i<n-1;i++){ if(gcd(n,i+1)!=1){ visited[i]=1; }else{ count++; product *= i+1; product %= n; } } if(product!=1){ count --; visited[(int)product-1] = 1; } System.out.println(count); for(int i=0;i<n-1;i++){ if(visited[i]==0){ System.out.print((i+1)+" "); } } System.out.println(); } public static int gcd(int a, int b){ if(b==0){ return a; } return gcd(b,a%b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
3669e00f5125cd3a1f8e34d7f341a3eb
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
//package graphs; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class PW { public static FastReader s = new FastReader(); public static void main(String[] args) { // TODO Auto-generated method stub int t=1; //int k=0; while(t-- >0) { int n=s.nextInt(); //n++; if(n <= 4 || n == 6){ System.out.println(1); System.out.println(1); } else{ ArrayList<Integer> l=new ArrayList<>(); for(int i = 1 ; i < n ; i++){ long x = gcd(i, n); if(x == 1) l.add(i); } long pdt = 1; for(int x: l){ pdt = (pdt*x) % n; // cout << x <<" "; } if(pdt == 1) { System.out.println(l.size()); for(int x: l){ System.out.print(x+" "); } } else{ l.remove(l.size()-1); System.out.println(l.size()); for(int x: l){ System.out.print(x+" "); } } System.out.println(); } } } // public static long fastExpo(long a, long n, long mod) { // long result = 1; // while (n > 0) { // if ((n & 1)>0) // result = (result * a) % mod; // a = (a * a) % mod; // n >>= 1; // } // return result; // } // 1 4 8 public static long get(long arr[]) { int N = arr.length; long r = 2; long dist = 2; long curradj = (arr[1] - arr[0]); long prevadj = (arr[1] - arr[0]); for (int i = 2; i < N; i++) { curradj = arr[i] - arr[i - 1]; if (curradj == prevadj) { dist++; } else { prevadj = curradj; r = Math.max(r, dist); dist = 2; } } r = Math.max(r, dist); return r; } public static boolean solve(int a[],int k) { if(k<=2) return true; //int d=a[1]-a[0]; for(int i=0;i<a.length-1;i++) { int d=a[i+1]-a[i],flag=0; //System.out.println(d); HashMap<Integer,Integer> map=new HashMap<>(); for(int j=i+1;j<i+k;j++) { if(j>=a.length) { flag=1; break; } if(map.containsKey(a[j]-a[j-1])) { map.put(a[j]-a[j-1],map.get(a[j]-a[j-1])+1); } else map.put(a[j]-a[j-1],1); } int f=0; for(int nb:map.keySet()) { //System.out.println(k+" "+nb+" "+map.get(nb)); if(map.get(nb)>1) { f++; if(f==2) break; //f++; } } if(f<=1 && flag!=1) return true; } return false; } public static int count(int b[]){ int s = 0; for (int i = 0; i < 32; i++) { if(b[i]>0){ s |= (1<<i); } } return s; } public static void remove(int b[], int val){ for (int i = 0; i < 32; i++) { if(((val>>i)&1)==1) b[i]--; } } public static void add(int b[], int val){ for (int i = 0; i < 32; i++) { if(((val>>i)&1)==1) b[i]++; } } public static String largestNumber( List<String> ab) { // List<String> ab= new ArrayList<>(); // for(int i=0;i<A.size();i++) // { // ab.add(String.valueOf(A.get(i))); // } Collections.sort(ab, new Comparator<String>(){ public int compare(String X, String Y) { // first append Y at the end of X String XY=X + Y; // then append X at the end of Y String YX=Y + X; // Now see which of the two formed numbers // is greater return XY.compareTo(YX)>0?-1:1; } }); StringBuilder abc= new StringBuilder(); for(int i=0;i<ab.size();i++) { abc.append(ab.get(i)); } if(abc.length()==0) return abc.toString(); if(abc.charAt(0)=='0') return "0"; else return abc.toString(); } public static boolean pal(String s) { int i=0; int j=s.length()-1; while(i<=j) { if(s.charAt(i)!=s.charAt(j)) return false; i++; j--; } return true; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long solve(int n, int r) { long p = 1, k = 1; if (n - r < r) { r = n - r; } if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } //System.out.println(p); return p; } public static long gcd(long a,long b) { if(a==0||b==0) return a+b; return gcd(b,(a%b)); } public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } public static boolean prime(int n) { if(n<=2) return true; for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } public static long fastExpo(long a,long n,long mod){ if (n == 0) return 1; else{ long x = fastExpo(a,n/2,mod); if ((n&1) == 1){ return (((a*x)%mod)*x)%mod; } else{ return (((x%mod)*(x%mod))%mod)%mod; } } } } class pair{ //public: int f; int s; pair(int x,int y) { f=x; s=y; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
6411f23d71515d4360dcd3d87f7a3b35
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class n716C { public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = sc.ni(); if (n % 2 != 0) { long result = 1; int count = 0; for (int i = 2; i < n; i++) { if (gcd(n, i) != 1) count++; else result = (result * i) % n; } if (result == 1) { result = 0; System.out.println(n - 1 - count); } else { System.out.println(n - 2 - count); } System.out.print(1 + " "); for (int i = 2; i < n; i++) { if (gcd(n, i) == 1 && i != result) System.out.print(i + " "); } } else { long result = 1; int count = 0; for (int i = 3; i < n; i += 2) { if (gcd(n, i) != 1) count++; else result = (result * i) % n; } if (result == 1) { result = 0; System.out.println(n / 2 - count); } else { System.out.println(n / 2 - 1 - count); } System.out.print(1 + " "); for (int i = 3; i < n; i += 2) { if (gcd(n, i) == 1 && i != result) System.out.print(i + " "); } } } static int gcd(int a, int b) { while (a >= 1 & b >= 1) { if (a % b == 0) return b; a = a % b; if (a < b) { int tmp = a; a = b; b = tmp; } } return 1; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in), 32768); st = null; } 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()); } int[][] graph(int N, int[][] edges) { int[][] graph = new int[N][]; int[] sz = new int[N]; for (int[] e : edges) { sz[e[0]] += 1; sz[e[1]] += 1; } for (int i = 0; i < N; i++) { graph[i] = new int[sz[i]]; } int[] cur = new int[N]; for (int[] e : edges) { graph[e[0]][cur[e[0]]] = e[1]; graph[e[1]][cur[e[1]]] = e[0]; cur[e[0]] += 1; cur[e[1]] += 1; } return graph; } int[] intArray(int N, int mod) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = ni() + mod; return ret; } long nl() { return Long.parseLong(next()); } long[] longArray(int N, long mod) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = nl() + mod; return ret; } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
357c2795503aeb4a3f3d792eb6969daf
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
//package clipse; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.io.*; import java.math.*; public class A { static StringBuilder sb; static dsu dsu; static int prime[]; static ArrayList<Integer>l=new ArrayList<>(); static void solve() { int n=i(); //System.out.println((105)%8); ArrayList<Integer>l=new ArrayList<>(); long prod=1; for(int i=1;i<=n-2;i++) { if(gcd(i,n)==1) { l.add(i); prod=(prod*i)%n; } } if(prod*(n-1)%n==1) l.add(n-1); System.out.println(l.size()); for(int a:l) System.out.print(a+" "); System.out.println(); } public static void main(String[] args) { prime=new int[1000000+1]; Arrays.fill(prime, 1); prime[0]=prime[1]=-1; for(int p=2;p*p<prime.length;p++) { if(prime[p]==1) { for(int j=p*p;j<prime.length;j=j+p) { prime[j]=-1; } } } for(int i=2;i<prime.length;i++) { if(prime[i]==1) l.add(i); } sb=new StringBuilder(); int test=1; while(test-->0) { solve(); } System.out.println(sb); } //*********************************Disjoint set union*************************// static class dsu { int parent[]; int size[]; dsu(int n) { parent=new int[n]; size=new int[n]; Arrays.fill(size,-1); for(int i=0;i<n;i++) parent[i]=-1; } int find(int a) { if(parent[a]<0) return a; else { int x=find(parent[a]); parent[a]=x; return x; } } void merge(int a,int b) { a=find(a); b=find(b); if(a==b) return; parent[b]=a; size[a]+=size[b]; } } //*******************************************PRIME FACTORIZE *****************************************************************************************************// static TreeMap<Integer,Integer> prime(long n) { TreeMap<Integer,Integer>h=new TreeMap<>(); long num=n; for(int i=2;i<=Math.sqrt(num);i++) { if(n%i==0) { int nt=0; while(n%i==0) { n=n/i; nt++; } h.put(i, nt); } } if(n!=1) h.put((int)n, 1); return h; } //*************CLASS PAIR *********************************************************************************************************************************************** static class pair implements Comparable<pair> { int x; int y; pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(pair o) { return x-o.x==0?y-o.y:x-o.x; } } //*************CLASS PAIR ***************************************************************************************************************************************************** static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int Int() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String String() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static InputReader in = new InputReader(System.in); static OutputWriter out = new OutputWriter(System.out); static int modulus = (int) 1e7; public static int[] sort(int[] a) { int n = a.length; ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < l.size(); i++) a[i] = l.get(i); return a; } public static long pow(long x, long y) { long res = 1; while (y > 0) { if (y % 2 != 0) { res = (res * x) % modulus; y--; } x = (x * x) % modulus; y = y / 2; } return res; } //GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } //****************LOWEST COMMON MULTIPLE ************************************************************************************************************************************* public static long lcm(long x, long y) { return (x * (y / gcd(x, y))); } //INPUT PATTERN****************************************************************************************************************************************************************** public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } public static int[] readArray(int n) { int A[] = new int[n]; for (int i = 0; i < n; i++) { A[i] = i(); } return A; } public static long[] readArray(long n) { long A[] = new long[(int) n]; for (int i = 0; i < n; i++) { A[i] = l(); } return A; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
9f551d8c1e1d4bad45486f5aa0e18a3b
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class CF1514_D2_C { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int n = scanner.nextInt(); // if(n <= 4) { // System.out.println(1); // System.out.println(1); // return; // } // boolean[] prime = new boolean[n]; // Arrays.fill(prime, true); // for (int i = 2; i < n; i++) { // if (prime[i]) { // for (int j = i * 2; j < n; j += i) { // prime[j] = false; // } // } // } StringBuilder out = new StringBuilder(); int count = 1; long p = 1; ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1) { p = p * i; p = p % n; list.add(i); } } if (p != 1) { for (int i = 0; i < list.size(); i++) { if (p != list.get(i)) { out.append(list.get(i)).append(' '); } } System.out.println(list.size() - 1); System.out.println(out); } else { for (int i = 0; i < list.size(); i++) { out.append(list.get(i)).append(' '); } System.out.println(list.size()); System.out.println(out); } // out.append(1).append(' '); // HashSet<Integer> set = primeFactors(n); // for (int i = 2; i < n; i++) { // if (prime[i] && !set.contains(i)) { // count++; // out.append(i).append(' '); // } // } // System.out.println(count); // System.out.println(out); } static int gcd(int n1, int n2) { if (n2 == 0) { return n1; } return gcd(n2, n1 % n2); } static HashSet<Integer> divisors(int n) { HashSet<Integer> list = new HashSet<>(); for (int i = 1; i * i <= n; i++) { if (n % i == 0) { if (n / i == i) { list.add(i); } else { list.add(i); list.add(n / i); } } } return list; } public static LinkedHashSet<Integer> primeFactors(int n) { LinkedHashSet<Integer> set = new LinkedHashSet<>(); // set.add(1L); while (n % 2 == 0) { set.add(2); n /= 2; } for (int i = 3; i * i <= n; i += 2) { while (n % i == 0) { set.add(i); n /= i; } } if (n > 2) set.add(n); return set; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] nextArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } Integer[] nextArray(int n, boolean object) { Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
2aef55ca5223a93e812bca0cfa986517
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class product_1_modulo_n { public static int gcd(int i, int j) { if(i == 0 || j == 0) { return i + j; } if(i > j) { return gcd(i % j, j); } return gcd(i, j % i); } public static void main(String[]args){ Kattio io = new Kattio(); int n = io.nextInt(); long product = 1L; ArrayList<Integer>arr = new ArrayList<Integer>(); for(int i = 1; i < n; i++) { if(gcd(n,i) == 1) { product = ((long)product * i) % n; arr.add(i); } } if(product == n-1 && n != 2) { arr.remove(arr.size() - 1); } io.println(arr.size()); for(int i = 0; i < arr.size(); i++) { io.print(arr.get(i) + " "); } io.close(); } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(problemName + ".out"); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) { } return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
4e1e27e0ec1b54be448b4efcdfa9afad
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.StringTokenizer; public class Product1ModuloN { static class Pair { int f; int s; // Pair() { } Pair(int f, int s) { this.f = f; this.s = s; } } static class Fast { BufferedReader br; StringTokenizer st; public Fast() { br = new BufferedReader( new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArray1(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine().trim(); } catch (IOException e) { e.printStackTrace(); } return str; } } /* static long noOfDivisor(long a) { long count=0; long t=a; for(long i=1;i<=(int)Math.sqrt(a);i++) { if(a%i==0) count+=2; } if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a))) { count--; } return count; }*/ static boolean isPrime(long a) { for (long i = 2; i <= (long) Math.sqrt(a); i++) { if (a % i == 0) return false; } return true; } static void primeFact(int n) { int temp = n; HashMap<Integer, Integer> h = new HashMap<>(); for (int i = 2; i * i <= n; i++) { if (temp % i == 0) { int c = 0; while (temp % i == 0) { c++; temp /= i; } h.put(i, c); } } if (temp != 1) h.put(temp, 1); } static void reverseArray(int a[]) { int n = a.length; for (int i = 0; i < n / 2; i++) { a[i] = a[i] ^ a[n - i - 1]; a[n - i - 1] = a[i] ^ a[n - i - 1]; a[i] = a[i] ^ a[n - i - 1]; } } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static int max(int a, int b) { return a > b ? a : b; } static int min(int a, int b) { return a < b ? a : b; } public static int[] constructST(int arr[], int n) { // Add your code here int st[] = new int[4 * n]; build(arr, 0, n - 1, 0, st); return st; } static int build(int arr[], int l, int r, int idx, int st[]) { if (l == r) { return st[idx] = arr[l] & 1; } int mid = (l + r) / 2; int p1 = build(arr, l, mid, 2 * idx + 1, st); int p2 = build(arr, mid + 1, r, 2 * idx + 2, st); if (p1 == p2 && p1 != -1) return st[idx] = p1 & p2; else if (p1 == -1 || p2 == -1) return st[idx] = -1; else return st[idx] = -1; } /* The functions returns the min element in the range from l and r */ public static int RMQ(int st[], int n, int l, int r) { // Add your code here return solve(st, l, r, 0, n - 1, 0); } static int solve(int st[], int l, int r, int low1, int high1, int idx) { if (low1 >= l && high1 <= r)//lies // we need to find min number in range [l,r] low,high is range in segment tree.. consider the first case return st[idx]; if (high1 < l || low1 > r)//not lie return Integer.MAX_VALUE; int mid = (low1 + high1) / 2; int left = solve(st, l, r, low1, mid, 2 * idx + 1); int right = solve(st, l, r, mid + 1, high1, 2 * idx + 2); if (left == right && left != -1 && left != Integer.MAX_VALUE) return left; else if (left == -1 || right == -1) return -1; else if (left != right && left != Integer.MAX_VALUE && right != Integer.MAX_VALUE) return -1; else if (left == Integer.MAX_VALUE) return right; else return left; } static void swap(int a1[],int a,int b) { a1[a]=a1[a]^a1[b];a1[b]=a1[a]^a1[b];a1[a]=a1[a]^a1[b]; } static long give(long a) { return (a*(a+1))/2; } static int gcd(int a,int b) { if(b%a==0) return a; return gcd(b%a,a); } public static void main(String args[]) throws IOException { Fast sc = new Fast(); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); int a[]=new int[n]; long p=1; for(int i=1;i<n;i++) { if(gcd(i,n)==1) a[i]=i; p=p*(a[i]!=0?a[i]:1)%n; } if(p!=1) a[(int)p]=0; /* out.println(p); for(int i=1;i<n;i++) if(a[i]==0) out.println(i);*/ int c=0; for(int ne:a) if(ne!=0) c++; out.println(c); for(int ne:a) if(ne!=0) out.print(ne+" "); out.println(); out.close(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
2a7056725275cac4a8b9c8ba00ad3d92
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class prod1mod { public static int gcd(int a, int b) { if (b==0) return a; return gcd(b,a%b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long prod = 1; ArrayList<Integer> subsequence = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1) { subsequence.add(i); prod *= i; prod %= n; } } if (prod == 1) { System.out.println(subsequence.size()); for (int e : subsequence) { System.out.print(e + " "); } } if (prod != 1) { subsequence.remove(Integer.valueOf((int) prod)); System.out.println(subsequence.size()); for (int e : subsequence) { System.out.print(e + " "); } } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
884e21725844513d467052c569e5e704
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
//package codeforces; import java.io.*; import java.util.*; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) throws java.lang.Exception { FastReader fr = new FastReader(); PrintWriter out = new PrintWriter(System.out); int n = fr.ni(); ArrayList<Integer> ans = new ArrayList<>(); long mul = 1; for(int i = 1 ; i < n ; i++) { int gcd = gcd(i,n); if(gcd == 1) { ans.add(i); mul *= i; mul %= n; } } if(mul == 1) { out.println(ans.size()); for(int i : ans)out.print(i+ " "); }else { out.println(ans.size() -1); for(int i = 0 ; i < ans.size() ; i++) { if(ans.get(i) != mul)out.print(ans.get(i) + " "); } } out.close(); } public static int gcd(int a ,int b) { if(b == 0)return a; return gcd(b , a%b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
08c901f7746f393fa8d0ba6b17d2c3f5
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; import java.lang.Math; public class C_Product_1_Modulo_N { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readintarray(int n) { int res[] = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readlongarray(int n) { long res[] = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } // static int lcm(long a, long b) { // return (a / gcd(a, b)) * b; // } public static void main(String args[]) throws IOException { FastReader s = new FastReader(); int t; // t =s.nextInt(); long n = s.nextLong(); Vector<Long> ans = new Vector<Long>(); long product = 1; long modulo=1; for (long i = 1; i < n; i++) { if (gcd(i, n) == 1) { product *= i; modulo=(modulo*i)%n; ans.add(i); } } modulo%=n; // System.out.println(modulo); if (modulo!= 1) ans.removeElement(modulo); System.out.println(ans.size()); for(int i=0;i<ans.size();i++){ System.out.print(ans.get(i)+" "); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
ba888f8521f5f746e2d0c516026ef87e
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main{ public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); // for(int i=6;i<=6;i++) { /// InputStream uinputStream = new FileInputStream("2.in"); // String f = "Input"+i+".txt"; // String of = "boards.out"; // InputStream uinputStream = new FileInputStream(f); /// InputReader in = new InputReader(uinputStream); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(of))); // Task t = new Task(); // t.solve(in, out); // out.close(); // } Task t = new Task(); t.solve(in, out); out.close(); } static class Task{ public void solve(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); long x = 1; ArrayList<Integer> arr = new ArrayList<Integer>(); for(int i=1;i<n;i++) { if(GCD(i,n)==1) { x*=i; x%=n; arr.add(i); } } //Dumper.print(x); if(x==1) { out.println(arr.size()); for(int i:arr) { out.print(i+" "); } }else { out.println(arr.size()-1); for(int i:arr) { if(x!=i)out.print(i+" "); } } out.println(); } class edge implements Comparable<edge>{ int id,f,t; int len; public edge(int a, int b, int c, int d) { f=a;t=b;len=c;id=d; } @Override public int compareTo(edge t) { if(this.len-t.len>0) return 1; else if(this.len-t.len<0) return -1; return 0; } } public int minMoves(int[] nums, int k) { int n = 0; for(int i:nums) n+=i; int arr[] = new int[n]; for(int i=0,j=0;i<nums.length;i++) { if(nums[i]==1) arr[j++] = i; } int sum[] = new int[n]; for(int i=0;i<n;i++) { sum[i] = arr[i]; if(i>0) sum[i]+=sum[i-1]; } int ret = Integer.MAX_VALUE; for(int i=0;i+k-1<n;i++) { int j = i+k-1; int mid = (i+j)/2; int lnum = mid-i; int left = arr[mid]*lnum-(mid-1>=0?sum[mid-1]:0)+(i>0?sum[i-1]:0)-(1+lnum)*lnum/2; int rnum = j-mid; int right = sum[j]-sum[mid]-rnum*arr[mid]-(1+rnum)*rnum/2; ret = Math.min(ret, left+right); } return ret; } class pair implements Comparable<pair>{ int idx; int bit_pos; public pair(int a, int b) { idx=a;bit_pos=b; } @Override public int compareTo(pair t) { return t.idx-this.idx; } } public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) { int idx = 0; int m = edges.length; ArrayList<edge> all = new ArrayList<edge>(); for(int x[]:edges) { int f = x[0]; int t = x[1]; int w = x[2]; edge tmp1 = new edge(f,t,w,idx++); all.add(tmp1); } DSU ds = new DSU(n+1); Collections.sort(all); Set<Integer> crit = new HashSet<Integer>(); Set<Integer> no_use = new HashSet<Integer>(); int p = 0; boolean vis[] = new boolean[n]; while(p<m) { edge t = all.get(p); ArrayList<edge> tmp = new ArrayList<edge>(); tmp.add(t); p++; while(p<m&&all.get(p).len==t.len) { tmp.add(all.get(p++)); } int cnt[] = new int[n]; Arrays.fill(cnt, -1); for(edge v:tmp) { if(ds.find(v.f)==ds.find(v.t)) { no_use.add(v.id); continue; } if(!vis[v.f]) { if(cnt[v.f]==-1) { cnt[v.f] = v.id; }else { cnt[v.f] = -2; } } if(!vis[v.t]) { if(cnt[v.t]==-1) { cnt[v.t] = v.id; }else { cnt[v.t] = -2; } } } for(int i=0;i<n;i++) { if(cnt[i]>=0) crit.add(cnt[i]); } for(edge v:tmp) { ds.union(v.f, v.t); vis[v.f] = true; vis[v.t] = true; } } List<List<Integer>> ret = new ArrayList<List<Integer>>(); List<Integer> r1 = new ArrayList<Integer>(); List<Integer> r2 = new ArrayList<Integer>(); for(int i=0;i<m;i++) { if(crit.contains(i)) r1.add(i); else if(!no_use.contains(i)) { r2.add(i); } } ret.add(r1);ret.add(r2); return ret; } class pair2 implements Comparable<pair2>{ long val; int idx; public pair2(long a, int b) { val = a; idx = b; } @Override public int compareTo(pair2 t) { if(this.val-t.val>0) return 1; else if(this.val-t.val<0) return -1; return 0; } } static class sgt{ sgt lt; sgt rt; int l,r; int sum, max, min, lazy; int min_idx; public sgt(int L, int R, int arr[]) { l=L;r=R; if(l==r-1) { sum = max = min = arr[l]; lazy = 0; min_idx = l; return; } lt = new sgt(l, l+r>>1, arr); rt = new sgt(l+r>>1, r, arr); pop_up(); } void pop_up() { this.sum = lt.sum + rt.sum; this.max = Math.max(lt.max, rt.max); this.min = Math.min(lt.min, rt.min); if(lt.min<rt.min) this.min_idx = lt.min_idx; else if(lt.min>rt.min) this.min_idx = rt.min_idx; else this.min_idx = Math.min(lt.min_idx, rt.min_idx); } void push_down() { if(this.lazy!=0) { lt.sum+=lazy; rt.sum+=lazy; lt.max+=lazy; lt.min+=lazy; rt.max+=lazy; rt.min+=lazy; lt.lazy+=this.lazy; rt.lazy+=this.lazy; this.lazy = 0; } } void change(int L, int R, int v) { if(R<=l||r<=L) return; if(L<=l&&r<=R) { this.max+=v; this.min+=v; this.sum+=v*(r-l); this.lazy+=v; return; } push_down(); lt.change(L, R, v); rt.change(L, R, v); pop_up(); } int query_max(int L, int R) { if(L<=l&&r<=R) return this.max; if(r<=L||R<=l) return Integer.MIN_VALUE; push_down(); return Math.max(lt.query_max(L, R), rt.query_max(L, R)); } int query_min(int L, int R) { if(L<=l&&r<=R) return this.min; if(r<=L||R<=l) return Integer.MAX_VALUE; push_down(); return Math.min(lt.query_min(L, R), rt.query_min(L, R)); } int query_sum(int L, int R) { if(L<=l&&r<=R) return this.sum; if(r<=L||R<=l) return 0; push_down(); return lt.query_sum(L, R) + rt.query_sum(L, R); } int query_min_idx(int L, int R) { if(L<=l&&r<=R) { return this.min_idx; } if(r<=L||R<=l) return Integer.MAX_VALUE; int a = lt.query_min_idx(L, R); int b = rt.query_min_idx(L, R); int aa = lt.query_min(L, R); int bb = rt.query_min(L, R); int ret = 0; if(aa<bb) ret = a; else if(aa>bb) ret = b; else ret = Math.min(a,b); return ret; } } // List<List<String>> convert(String arr[][]){ // int n = arr.length; // List<List<String>> ret = new ArrayList<>(); // for(int i=0;i<n;i++) { // ArrayList<String> tmp = new ArrayList<String>(); // for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]); // ret.add(tmp); // } // return ret; // } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public long GCD(long a, long b) { if (b==0) return a; return GCD(b,a%b); } } static class ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } public static void sort(long[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(long[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); long t = a[i]; a[i] = a[j]; a[j] = t; } } } static class BIT{ int arr[]; int n; public BIT(int a) { n=a; arr = new int[n]; } int sum(int p) { int s=0; while(p>0) { s+=arr[p]; p-=p&(-p); } return s; } void add(int p, int v) { while(p<n) { arr[p]+=v; p+=p&(-p); } } } static class DSU{ int[] arr; int[] sz; public DSU(int n) { arr = new int[n]; sz = new int[n]; for(int i=0;i<n;i++) arr[i] = i; Arrays.fill(sz, 1); } public int find(int a) { if(arr[a]!=a) arr[a] = find(arr[a]); return arr[a]; } public void union(int a, int b) { int x = find(a); int y = find(b); if(x==y) return; arr[y] = x; sz[x] += sz[y]; } public int size(int x) { return sz[find(x)]; } } static class MinHeap<Key> implements Iterable<Key> { private int maxN; private int n; private int[] pq; private int[] qp; private Key[] keys; private Comparator<Key> comparator; public MinHeap(int capacity){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); } public MinHeap(int capacity, Comparator<Key> c){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); comparator = c; } public boolean isEmpty() { return n==0; } public int size() { return n; } public boolean contains(int i) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); return qp[i] != -1; } public int peekIdx() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } public Key peek(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } public int poll(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); int min = pq[1]; exch(1,n--); down(1); assert min==pq[n+1]; qp[min] = -1; keys[min] = null; pq[n+1] = -1; return min; } public void update(int i, Key key) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (!contains(i)) { this.add(i, key); }else { keys[i] = key; up(qp[i]); down(qp[i]); } } private void add(int i, Key x){ if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = x; up(n); } private void up(int k){ while(k>1&&less(k,k/2)){ exch(k,k/2); k/=2; } } private void down(int k){ while(2*k<=n){ int j=2*k; if(j<n&&less(j+1,j)) j++; if(less(k,j)) break; exch(k,j); k=j; } } public boolean less(int i, int j){ if (comparator == null) { return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0; } else { return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0; } } public void exch(int i, int j){ int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } @Override public Iterator<Key> iterator() { // TODO Auto-generated method stub return null; } } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int zcurChar; private int znumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (znumChars == -1) throw new InputMismatchException(); if (zcurChar >= znumChars) { zcurChar = 0; try { znumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (znumChars <= 0) return -1; } return buf[zcurChar++]; } 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 String nextString() { 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 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 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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } static class Dumper { static void print_int_arr(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_char_arr(char[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_double_arr(double[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(int[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(boolean[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print(Object o) { System.out.println(o.toString()); } static void getc() { System.out.println("here"); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
d91eedf804269f3631886bbce386ed37
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
// package codeforces.Practice; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class Div2_716_C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); //n*a+1 형태 //1,2,...n-1까지의 수를 선택 가능 //가장 짧은 답은 {1} //n으로 나눠떨어지게 되면 일단 안되고 int n= Integer.parseInt(br.readLine()); List<Integer> list = new ArrayList<>(); for(int i=1;i<=n;i++){ if(gcd(i,n)==1) list.add(i); } //System.out.println(list); long prod=1; for(int x:list){ prod*=x; prod%=n; } if(prod!=1){ long finalProd = prod; list.removeIf(x->(x== finalProd)); } System.out.println(list.size()); for(int x:list) System.out.print(x+" "); } private static int gcd(int a,int b){ if(a%b!=0) return gcd(b,a%b); else return b; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
f596b16a186db0d637b6fe84f61342d9
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.StringJoiner; import java.util.TreeMap; public class C { public static void main(String[] args) { Print print = new Print(); Scan scan = new Scan(); // for (int i = 2; i <= 20; i += 1) { // brute_force(i); // } int n = scan.scanInt(); solve(print, n); print.close(); } private static void brute_force(int i) { int[] arr = new int[i - 1]; for (int j = 1; j <= arr.length; j++) { arr[j - 1] = j; } TreeMap<Integer, String> m = new TreeMap<>(); int k = arr.length + 1; for (int l = (1 << k) - 1; l >= 0; l--) { long v = 1; for (int g = 0; g < arr.length; g++) { if (((l >> g) & 1) == 1) { v = (v * arr[g]) % k; } } if (v == 1) { int count = 0; StringJoiner sj = new StringJoiner(" "); for (int g = 0; g < arr.length; g++) { if (((l >> g) & 1) == 1) { count++; sj.add(Integer.toString(arr[g])); } } m.put(count, sj.toString()); } } System.out.printf("for num = %d , max length is : %d %n", i, m.lastKey()); System.out.println(m.get(m.lastKey())); } private static void solve(Print print, int n) { long co = 1; StringJoiner sj = new StringJoiner(" "); sj.add("1"); List<Long> li = new ArrayList<>(); long p = 1; for (long i = 2; i < n; i++) { if (gcd(i, n) == 1) { co++; li.add(i); p = (p * i) % n; } } if (p != 1) { li.remove(li.get(li.size() - 1)); co--; } for (long x : li) { sj.add(Long.toString(x)); } print.printLine(Long.toString(co)); print.printLine(sj.toString()); } static long gcd(long a, long b) { while (b != 0) { long t = a; a = b; b = t % b; } return a; } static int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) { return 0; } while (a > 1) { // q is quotient int q = a / m; int t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) { x += m0; } return x; } static long add(long a, long b, long MOD) { return (a % MOD + b % MOD) % MOD; } static long multiply(long a, long b, long MOD) { return (a % MOD * b % MOD) % MOD; } static long subtract(long a, long b, long MOD) { return ((a % MOD - b % MOD) % MOD + MOD) % MOD; } static long inverse(long a, long MOD) { return pow(a, MOD - 2, MOD); } static long divide(long a, long b, long MOD) { return multiply(a, inverse(b, MOD), MOD); } static long pow(long a, long n, long MOD) { if (n == 0) { return 1; } long x = pow(a, n / 2, MOD); if (n % 2 == 1) { return multiply(multiply(x, x, MOD), a, MOD); } else { return multiply(x, x, MOD); } } static class Scan { private byte[] buf = new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in = System.in; } public int scan() { if (total < 0) { throw new InputMismatchException(); } if (index >= total) { index = 0; try { total = in.read(buf); } catch (IOException ignored) { } if (total <= 0) { return -1; } } return buf[index++]; } public int scanInt() { int integer = 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') { integer *= 10; integer += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } return neg * integer; } public double scanDouble() { double doub = 0; int n = scan(); while (isWhiteSpace(n)) { n = scan(); } int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n) && n != '.') { if (n >= '0' && n <= '9') { doub *= 10; doub += n - '0'; n = scan(); } else { throw new InputMismatchException(); } } if (n == '.') { n = scan(); double temp = 1; while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { temp /= 10; doub += (n - '0') * temp; n = scan(); } else { throw new InputMismatchException(); } } } return doub * neg; } public String scanString() { StringBuilder sb = new StringBuilder(); int n = scan(); while (isWhiteSpace(n)) { n = scan(); } while (!isWhiteSpace(n)) { sb.append((char) n); n = scan(); } return sb.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) { return true; } return false; } } static class Print { private final BufferedWriter bw; public Print() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } public void print(String str) { try { bw.append(str); } catch (IOException ignored) { } } public void printLine(String str) { print(str); try { bw.append("\n"); } catch (IOException ignored) { } } public void close() { try { bw.close(); } catch (IOException ignored) { } } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
73ac7e5b128a83fbdafdfbfd35b70a66
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { Scanner sc = new Scanner(System.in); int n =sc.nextInt(); int[] ok = new int[n]; long x=1; for(int i=1;i<n;i++) { if(gcdExtended(i,n,1,1)==1) {ok[i]=1; x=(x*i)%n; } } int p=(int)x; if(p!=1){ // System.out.println(p); ok[p]=0; } int count=0; for(int i=1;i<n;i++) if(ok[i]==1) count++; System.out.println(count); for(int i=1;i<n;i++) if(ok[i]==1) System.out.print(i+" "); System.out.println(); } // public static int gcd(int a, int b) // { // if (a == 0) // return b; // return gcd(b%a, a); // } public static int gcdExtended(int a, int b, int x, int y) { // Base Case if (a == 0) { x = 0; y = 1; return b; } int x1=1, y1=1; // To store results of recursive call int gcd = gcdExtended(b%a, a, x1, y1); // Update x and y using results of recursive // call x = y1 - (b/a) * x1; y = x1; return gcd; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
8f92b6f00a4fa0a0b76e554cd5171c2b
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; import java.lang.*; public class cfpract { static int mod=10000_00007; public static void solve() throws Exception{ InputStreamReader ip=new InputStreamReader(System.in); BufferedReader br = new BufferedReader(ip); // int t = Integer.parseInt(br.readLine()); StringBuilder asb=new StringBuilder(); // while(t-->0){ long n =Integer.parseInt(br.readLine()); // String[] strs=(br.readLine()).trim().split(" "); // int n=Integer.parseInt(strs[0]),l=Integer.parseInt(strs[1]),r=Integer.parseInt(strs[2]),s=Integer.parseInt(strs[3]); // String str=(br.readLine()).trim(); ArrayList<Long> list=new ArrayList<>(); long v=1; for(long i=1;i<n;i++){ long g=gcd(i, n); if(g==1){ list.add(i); v=(v*i)%n; } } if(n>2 && v==n-1){ list.remove(list.size()-1); v=1; } System.out.println(list.size()); for(long val:list){ System.out.print(val+" "); } // } // System.out.println(asb); } //******************************************************************************************************************** */ public static void main(String[] args) throws Exception{ solve(); // sec(); } public static void solve2() throws Exception{ InputStreamReader ip=new InputStreamReader(System.in); BufferedReader br = new BufferedReader(ip); int t = Integer.parseInt(br.readLine()); StringBuilder asb=new StringBuilder(); while(t-->0){ // int n =Integer.parseInt(br.readLine()); // String[] strs=(br.readLine()).trim().split(" "); // int n=Integer.parseInt(strs[0]),l=Integer.parseInt(strs[1]),r=Integer.parseInt(strs[2]),s=Integer.parseInt(strs[3]); // String str=(br.readLine()).trim(); } } //binexp public static long binexp(long a,long b){ if(b==0)return 1; long res=binexp(a, b/2); if(b%2==1){ return (((res*res))*a); //%mod; }else return (res*res); } //gcd public static long gcd(long a,long b){ //gcd using division method if(b==0)return a; else return gcd(b, a%b); } public static int isprime(int n){ for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0)return i; } return -1; } //sort public static void sort(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int idx = (int) Math.random() * n; long temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; //swap } Arrays.sort(arr); } public static void sort(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int idx = (int) Math.random() * n; int temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; //swap } Arrays.sort(arr); } //1d print public static void print(int[] dp) { for (int val : dp) { System.out.print(val + " "); } System.out.println(); } public static void print(long[] dp) { for (long val : dp) { System.out.print(val + " "); } System.out.println(); } //2d print public static void print(long[][] dp) { for (long[] a : dp) { for (long val : a) { System.out.print(val + " "); } System.out.println(); } } public static void print(int[][] dp) { for (int[] a : dp) { for (int val : a) { System.out.print(val + " "); } System.out.println(); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
8cd735f41acba087b9a30c24e7c05643
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class C { static class Reader{ private BufferedReader reader; private StringTokenizer tokenizer; Reader(InputStream input) { this.reader = new BufferedReader(new InputStreamReader(input)); this.tokenizer = new StringTokenizer(""); } public String next() throws IOException { while(!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } } static class solution { public int GCD(int a, int b) { while(a!=b && a>0 && b>0) { int min = Math.min(a,b); int max = Math.max(a, b); a = max%min; b = min; } if(a==0)return b; if(b==0)return a; if(a==b)return a; return b; } public boolean isPerfectSquare(int n) { int i = (int)Math.floor(Math.sqrt(n)); // for (int i = 1; i * i <= n; i++) { // if ((n % i == 0) && (n / i == i)) { // return true; // } // } if(i*i==n)return true; return false; } public boolean isPrime(int n) { for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0)return false; } return true; } public String solve(int N) { StringBuffer sb = new StringBuffer(); int length = 0; long prod = 1; for(int i=1;i<=N-2;i++){ if(GCD(i,N)==1) { sb.append(i+" "); prod = (prod*i)%N; length++; } } if(prod==N-1) { sb.append(N-1); length++; } return length+"\n"+sb.toString(); } } public static void main(String[] args) throws IOException{ Reader scan = new Reader(System.in); BufferedOutputStream b = new BufferedOutputStream(System.out); // int a = scan.nextInt(); int N = scan.nextInt(); solution sol = new solution(); // System.out.println(sol.GCD(a, N)); String ans = sol.solve(N); b.write((ans).getBytes()); b.flush(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
0a6b7ebb215af079d2aeb2044015dfc4
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; public class Poster { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<Integer> al = new ArrayList<>(); long sum = 1; int max = 0; for(int i=1;i<n;i++) { if(gcd(i,n) == 1) { al.add(i); sum = sum*i%n; if(sum == 1) { max = al.size(); } } } System.out.println(max); for(int i=0;i<max;i++) { System.out.print(al.get(i)); System.out.print(' '); } } static int gcd(int a,int b) { while(b>0) { int c = a % b; a = b; b = c; } return a; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
af7902142e3ec30197844f324857bbcc
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class main { class FastScanner{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; }} static boolean check(int n){ return Math.ceil(Math.sqrt(n))==Math.floor(Math.sqrt(n)); } static long power(long a,long b,long mod){ long ans=1; while(b!=0){ if((b&1)==1){ ans=(ans*a)%mod; } a=(a*a)%mod; b>>=1; } return ans; } // 2 5 // static long gcd(long a,long b){ if(a==0){ return b; } return gcd(b%a,a); } static boolean bl[]=new boolean[2000]; public static void main(String[] args) { FastScanner scan=new main().new FastScanner(); int t=scan.nextInt(); ArrayList<Long>A=new ArrayList<>(); A.add(1l); long mul=1l; for(int i=2;i<t;++i){ if(gcd(i,t)==1){ A.add((long)i); mul=(mul*i)%t; } } //System.out.println(mul); if(mul!=1){ A.remove(new Long(mul%t)); } { System.out.println(A.size()); for(int i=0;i<A.size();++i){ System.out.print(A.get(i)+" "); } } }}
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
81b9e73209f53036b31686d826d9dc57
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public final class Solution { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static long mod = (long) 1e9 + 7; static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(1, 0), new Pair(0, -1), new Pair(0, 1)}; // 111 // 0 7 7 7 7 -5 // 1 6 7 7 7 -20 // 2 5 7 7 7 -20 // 3 4 7 7 7 -20 // 5 3 6 7 7 public static void main(String[] args) { int t = 1; out: while (t-- > 0) { int n = i(); List<Integer> list = new ArrayList<>(); long product = 1; for (int i = 1; i < n; i++) { if (GCD(i, n) == 1) { list.add(i); product *= i; product %= n; } } if (product == 1L) { out.println(list.size()); for (int a : list) { out.print(a + " "); } } else { out.println(list.size() - 1); for (int a : list) { if (a != product) { out.print(a + " "); } } } } out.flush(); } public static boolean isPS(int x) { if (x == 1) { return true; } for (int i = 2; i <= x / 2; i++) { if (i * i == x) { return true; } } return false; } public static long calc(int type, long X, long K) { if (type == 1) { return (X + 99999) / 100000 + K; } else { return (K * X + 99999) / 100000; } } static int sd(long i) { int d = 0; while (i > 0) { d += i % 10; i = i / 10; } return d; } static int lower(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] >= x) { r = m; } else { l = m; } } return r; } static int upper(long A[], long x) { int l = -1, r = A.length; while (r - l > 1) { int m = (l + r) / 2; if (A[m] <= x) { l = m; } else { r = m; } } return l; } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static int lowerBound(int A[], int low, int high, int x) { if (low > high) { if (x >= A[high]) { return A[high]; } } int mid = (low + high) / 2; if (A[mid] == x) { return A[mid]; } if (mid > 0 && A[mid - 1] <= x && x < A[mid]) { return A[mid - 1]; } if (x < A[mid]) { return lowerBound(A, low, mid - 1, x); } return lowerBound(A, mid + 1, high, x); } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static boolean isPrime(long N) { if (N <= 1) { return false; } if (N <= 3) { return true; } if (N % 2 == 0 || N % 3 == 0) { return false; } for (int i = 5; i * i <= N; i = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String s() { return in.nextLine(); } static int[] input(int N) { int A[] = new int[N]; for (int i = 0; i < N; i++) { A[i] = in.nextInt(); } return A; } static long[] inputLong(int N) { long A[] = new long[N]; for (int i = 0; i < A.length; i++) { A[i] = in.nextLong(); } return A; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } } class SegmentTree { long[] t; public SegmentTree(int n) { t = new long[n + n]; Arrays.fill(t, Long.MIN_VALUE); } public long get(int i) { return t[i + t.length / 2]; } public void add(int i, long value) { i += t.length / 2; t[i] = value; for (; i > 1; i >>= 1) { t[i >> 1] = Math.max(t[i], t[i ^ 1]); } } // max[a, b] public long max(int a, int b) { long res = Long.MIN_VALUE; for (a += t.length / 2, b += t.length / 2; a <= b; a = (a + 1) >> 1, b = (b - 1) >> 1) { if ((a & 1) != 0) { res = Math.max(res, t[a]); } if ((b & 1) == 0) { res = Math.max(res, t[b]); } } return res; } } class Pair { int i, j; Pair(int i, int j) { this.i = i; this.j = j; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
59c2c4f4741128332888dd0463627c05
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; public class Div2 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String[] s; StringBuilder sb = new StringBuilder(); Div2 div2 = new Div2(); s = reader.readLine().split(" "); int n = Integer.parseInt(s[0]); List<Integer> list = new LinkedList<>(); list.add(1); long product = 1; for (int i = 2; i < n; i++) { if (div2.gcd(n, i) == 1) { list.add(i); product = (product * i) % n; } } if (product != 1) { list.remove(list.size() - 1); } sb.append(list.size()).append("\n"); for (Integer num : list) { sb.append(num).append(" "); } System.out.println(sb.toString()); } private int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
fa270315a16735b45b51c1319ced4ca8
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; public class Product1ModuloN { private 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))); int n = nextInt(); boolean invalid[] = new boolean[n]; int temp = n; for(int i = 1; i < n; ++i) { if(gcd(i,n)!=1)invalid[i] = true; } if(temp!=1)for(int j = 1; temp*j < n; ++j) invalid[temp*j] = true; PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); long ans = 1; int cnt = 0; for(int i = 1; i < n; ++i) { if(invalid[i])continue; ans = ((ans*i)%n+n)%n; ++cnt; } if(ans!=1) { invalid[(int)ans] = true; --cnt; } pw.println(cnt); for(int i = 1; i < n; ++i)if(!invalid[i])pw.print(i + " "); pw.close(); } private static int gcd(int a, int b) { //logn if(b == 0)return a; return gcd(b,a%b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
8207b2a43090d61183e01cb30f662797
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class A { static FastScanner fs; public static void main(String[] args) { fs = new FastScanner(); // int t = fs.nextInt(); // while (t-->0) solve(); } public static void solve() { int n = fs.nextInt(); TreeSet<Integer> ans = new TreeSet<>(); for (int i = 1; i < n; i++) if (gcd(i, n) == 1) ans.add(i); long prod = 1; for (int i : ans) { prod *= i; prod %= n; } if (prod != 1) ans.remove((int)prod); System.out.println(ans.size()); for (int i : ans) System.out.print(i + " "); System.out.println(); } static int gcd(int a, int b) { if (a==0) return b; return gcd(b%a, a); } static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static final Random random =new Random(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static int[] reverse(int[] a) { int n=a.length; int[] res=new int[n]; for (int i=0; i<n; i++) res[i]=a[n-1-i]; return res; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
6502456c0a3dda12ea8945082c9f89e3
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Product{ static long mod = 1000000007L; static MyScanner sc = new MyScanner(); static void solve() { int n = sc.nextInt(); ArrayList<Integer> ans = new ArrayList<>(); long prod = 1; for(int i = 1;i<=n;i++){ if(gcd(i,n)==1){ prod *=i; prod%=n; ans.add(i); } } if(prod==1){ out.println(ans.size()); }else{ out.println(ans.size()-1); } for(int i = 0;i<ans.size();i++){ if(prod!=1 && prod!=ans.get(i)){ out.print(ans.get(i)+" "); }else if(prod==1){ out.print(ans.get(i)+" "); } } out.println(); } static long find(long arr[],long brr[],int s,int t){ int i = s; int j = t; long sum = 0; while(i<=j){ sum+= arr[i]*brr[j]; i++; j--; } return sum; } static class Pair implements Comparable<Pair>{ long a; long b; Pair(long aa,long bb){ a = aa; b = bb; } public int compareTo(Pair p){ return Long.compare(this.b,p.b); } } static void reverse(int arr[]){ int i = 0;int j = arr.length-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = (res * res) % 1_000_000_007; if (b % 2 == 1) { res = (res * a) % 1_000_000_007; } return res; } static int lis(int arr[],int n){ int lis[] = new int[n]; lis[0] = 1; for(int i = 1;i<n;i++){ lis[i] = 1; for(int j = 0;j<i;j++){ if(arr[i]>arr[j]){ lis[i] = Math.max(lis[i],lis[j]+1); } } } int max = Integer.MIN_VALUE; for(int i = 0;i<n;i++){ max = Math.max(lis[i],max); } return max; } static boolean isPali(String str){ int i = 0; int j = str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } static long gcd(long a,long b){ if(b==0) return a; return gcd(b,a%b); } static String reverse(String str){ char arr[] = str.toCharArray(); int i = 0; int j = arr.length-1; while(i<j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } String st = new String(arr); return st; } static boolean isprime(int n){ if(n==1) return false; if(n==3 || n==2) 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; } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); // int t = sc.nextInt(); int t= 1; while(t-- >0){ solve(); // solve2(); // solve3(); } // Stop writing your solution here. ------------------------------------- out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int[] readIntArray(int n){ int arr[] = new int[n]; for(int i = 0;i<n;i++){ arr[i] = Integer.parseInt(next()); } return arr; } int[] reverse(int arr[]){ int n= arr.length; int i = 0; int j = n-1; while(i<j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j--;i++; } return arr; } long[] readLongArray(int n){ long arr[] = new long[n]; for(int i = 0;i<n;i++){ arr[i] = Long.parseLong(next()); } return arr; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } private static void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (int i=0; i<arr.length; i++){ list.add(arr[i]); } Collections.sort(list); // collections.sort uses nlogn in backend for (int i = 0; i < arr.length; i++){ arr[i] = list.get(i); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
f56e563f8e9754fbff6b5beb5449335e
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Random; import java.util.TreeSet; public final class CF_716_D2_C { static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} // Global vars static BufferedWriter out; static InputReader reader; static long powerMod(long b,long e,long m){ long x=1; while (e>0) { if (e%2==1) x=(b*x)%m; b=(b*b)%m; e=e/2; } return x; } static int pickRandom(TreeSet<Integer> ts) { Random r=new Random(); int tt=r.nextInt(ts.size()); int id=0; for (int v:ts) { id=v; tt--; if (tt==0) break; } return id; } static void test() { log("testing"); Random r=new Random(); int NTESTS=10000; int NMAX=1000; int MMAX=100; int VMAX=10000; for (int n=2;n<=100000;n++) { ArrayList<Integer> lst=solve(n); long ans=1; for (long x:lst) { ans*=x; ans%=n; } if (ans!=1) { log("Error"); log(n); log(ans); return; } } log("done"); } static int sign(long x){ if (x==0) return 0; if (x>0) return 1; return -1; } static class Composite implements Comparable<Composite>{ int v; int idx; public int compareTo(Composite X) { if (v<X.v) return 1; if (v>X.v) return -1; return idx-X.idx; } public Composite(int v, int idx) { this.v = v; this.idx = idx; } } static int pgcd(int a,int b){ if (a<b) return pgcd(b,a); while (b!=0){ int c=b; b=a%b; a=c; } return a; } static ArrayList<Integer> solve(int n){ ArrayList<Integer> lst=new ArrayList<Integer>(); long ans=1; for (int u=1;u<n;u++) { if (pgcd(u,n)==1) { lst.add(u); ans*=u; ans%=n; } } if (ans!=1) lst.remove(lst.size()-1); return lst; } static long mod=1000000007; static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader=new InputReader(System.in); int n=reader.readInt(); ArrayList<Integer> lst=solve(n); output(lst.size()); StringBuffer sb=new StringBuffer(); for (int x:lst) sb.append(x+" "); output(sb); try { out.close(); } catch (Exception EX){} } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res=new StringBuilder(); do { res.append((char)c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } } /* 9 9854 -8459 5972 4125 8148 3404 8382 9003 2861 -3242 -4217 6054 -7410 -5410 -9398 3130 1957 3500 */
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
12e8bbfbfa641e238a8a97eb513538bb
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
/*========================================================================== * AUTHOR: RonWonWon * CREATED: 02.06.2021 01:05:22 /*==========================================================================*/ import java.io.*; import java.util.*; public class C { public static void main(String[] args) { int n = in.nextInt(); List<Integer> ans = new ArrayList<>(); long p = 1; for(int i=1;i<n;i++) if(gcd(i,n)==1){ p *= i; p %= n; ans.add(i); } int len = ans.size(); if(p!=1){ len--; } out.println(len); for(int i=0;i<len;i++) out.print(ans.get(i)+" "); out.println(); out.flush(); } static int lcm(int a, int b) { return (a*b)/gcd(a,b); } static int gcd(int a, int 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 gcd(long a, long b) { if(b==0) return a; else return gcd(b,a%b); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while(!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch(IOException e) {} return st.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } long[] readArrayL(int n) { long a[] = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } double[] readArrayD(int n) { double a[] = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a; } } static FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static int oo = 1_000_000_000; static final Random random = new Random(); static void ruffleSort(int[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n), temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSortL(long[] a){ int n = a.length; for(int i=0;i<n;i++){ int j = random.nextInt(n); long temp = a[j]; a[j] = a[i]; a[i] = temp; } Arrays.sort(a); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
fab81627df2de357aac556d2d3b11a73
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Product1 { //--------------------------INPUT READER--------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { 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 ns() { return next(); } int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);}; public void p(long l) {w.println(l);}; public void p(double d) {w.println(d);}; public void p(String s) { w.println(s);}; public void pr(int i) {w.println(i);}; public void pr(long l) {w.print(l);}; public void pr(double d) {w.print(d);}; public void pr(String s) { w.print(s);}; public void pl() {w.println();}; public void close() {w.close();}; } //------------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); // int t = sc.ni(); // while(t-->0) solve(); w.close(); } static int[] primes; static int n; static void solve() throws IOException { n = sc.ni(); long pro = 1; List<Integer> l = new ArrayList<>(); for(int i = 1; i < n; i++) { if(gcd(n, i)==1) { l.add(i); pro *= i; pro%=n; } } if(pro%n!=1) { l.remove(l.size()-1); } w.p(l.size()); for(int i = 0; i < l.size(); i++) { w.pr(l.get(i)+" "); } w.pl(); } static long gcd(long a, long b) { return b == 0 ? (a < 0L ? -a: a) : gcd(b, a%b); } static long lcm(long a, long b) { return a/gcd(a, b)*b; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
91c8a12d02ef709961f8479240994cf7
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main{ public static long mod = (long)1e9+7; public static ArrayList<Integer>[] graph; public static void sort(int[] arr){ ArrayList<Integer> list = new ArrayList<>(); for(int u:arr) list.add(u); Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } } public static void sort(long[] arr){ ArrayList<Long> list = new ArrayList<>(); for(long u:arr) list.add(u); Collections.sort(list); for (int i = 0; i < arr.length; i++) { arr[i] = list.get(i); } } public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int t = 1; // t = in.nextInt(); for(int i = 1;i<=t ;i++){ solve(in,out); } out.close(); } static boolean[] visited; static StringBuilder res; private static void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); boolean[] possible = new boolean[n+1]; long prod = 1; res = new StringBuilder(""); for (int i = 1; i < n; i++) { if(gcd(i,n)==1){ possible[i] = true; prod = prod*i; if(prod>=n) prod%=n; } } if(prod !=1 ) possible[(int)prod] =false; prod = 0; for (int i = 1; i < n; i++) { if(possible[i]){ prod++; res.append(i).append(" "); } } out.println(prod); out.println(res); } private static int gcd(int i, int n) { if(n==0) return i; else return gcd(n,i%n); } private static void dfs(int i) { visited[i] = true; for(int u:graph[i]){ if(visited[u]) continue; res.append(i+" "+u); dfs(u); } } } 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()); } public double nextDouble(){ return Double.parseDouble(next()); } public int[] readIntArray(int size){ int[] list = new int[size]; for(int i = 0;i<size;i++) list[i] = nextInt(); return list; } public long[] readLongArray(int size){ long[] list = new long[size]; for(int i = 0;i<size;i++) list[i] = nextLong(); return list; } public double[] readDoubleArray(int size){ double[] list = new double[size]; for(int i = 0;i<size;i++) list[i] = nextDouble(); return list; } public String linereader() { String s = null; try{ s = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return s; } } class Pair<T extends Comparable<T>,K extends Comparable<K> > implements Comparable<Pair<T,K>>{ T first; K second; public Pair(T first, K second){ this.first = first; this.second = second; } @Override public String toString(){ return first+" "+second; } @Override public int compareTo(Pair<T,K> o) { // TODO Auto-generated method stub if((first).compareTo(o.first)!=0) return first.compareTo(o.first); else return second.compareTo(o.second); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
17697619a6be07d35d7700a3a816a998
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.math.BigInteger; import java.util.*; import java.util.stream.Collectors; public class CF1514C { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<Integer> coPrimeNumbers = new ArrayList<>(); for (int i = 1; i < n; i++) { boolean isRelativelyPrime = gcd(i, n) == 1; if (isRelativelyPrime) { coPrimeNumbers.add(i); } } BigInteger res = BigInteger.ONE; for (Integer i : coPrimeNumbers) { res = res.multiply(BigInteger.valueOf(i)).mod(BigInteger.valueOf(n)); } if (res.compareTo(BigInteger.ONE) == 0) { String output = coPrimeNumbers.stream().map(e -> String.valueOf(e)).collect(Collectors.joining(" ")); System.out.println(coPrimeNumbers.size()); System.out.println(output); }else{ BigInteger finalRes = res; String output = coPrimeNumbers.stream() .filter(e -> finalRes.intValue() != e) .map(e -> String.valueOf(e)).collect(Collectors.joining(" ")); System.out.println(coPrimeNumbers.size() - 1); System.out.println(output); } } public static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
448068ff117172c5039352d5dfd61e32
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.ArrayList; import java.util.Scanner; public class Product1ModuloN { static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static void main(String args[]) { Scanner scan = new Scanner(System.in); long n = scan.nextInt(); ArrayList<Integer> ar = new ArrayList<Integer>(); for (int i = 1; i <= n; i++) { if (gcd(n, i) == 1) { ar.add(i); } } long g = 1; for (int i = 0; i < ar.size(); i++) { g = ((g % n) * (ar.get(i) % n)) % n; } long r = g % n; if (r == 1) { System.out.println(ar.size()); for (int i = 0; i < ar.size(); i++) { System.out.print(ar.get(i) + " "); } System.out.println(); } else { System.out.println(ar.size() - 1); for (int i = 0; i < ar.size(); i++) { if (ar.get(i) == r) { continue; } System.out.print(ar.get(i) + " "); } System.out.println(); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
97e2db70aaa3d6310341689416f30156
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.TreeSet; import javax.management.RuntimeErrorException; public class Main { int gcd_impl(int a, int b) { if (a == 0) return b; return gcd_impl(b % a, a); } int gcd (int a, int b) { if (a > b) { return gcd_impl(b, a); } else { return gcd_impl(a, b); } } private void solve() throws Exception { int n = nextInt(); int phi = 0; long result = 1; TreeSet<Integer> answer = new TreeSet<>(); for(int i = 1; i < n; i++) { if (gcd(i, n) == 1) { phi++; result = (result * i) % n; answer.add(i); } } if (result != 1) { int inv = binpow(result, phi - 1, n); // System.out.println("invert for " + result + " = " + inv); if (!answer.contains(inv)) { // all coprime numbers are in the set // the result is also coprime // so the inv is coprime and must be present in the list throw new RuntimeException(); } answer.remove(inv); } Integer array[] = answer.toArray(new Integer[0]); out.println(array.length); for(int i = 0; i < array.length; i++) { if(i != 0) out.print(" "); out.print(array[i]); } out.println(); } private int binpow(long value, int pow, long mod) { long base = value; long result = 1; while (pow > 0) { if (pow % 2 != 0) { result = (result * base) % mod; } pow /= 2; base = (base * base) % mod; } return (int) result; } void run() throws Exception { // int cnt_test = nextInt(); // for (int test = 0; test < cnt_test; test++) { solve(); // } } public static void main(String[] args) throws Exception { Main instance = new Main(); instance.initStreams(); instance.run(); instance.closeStreams(); } BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); void initStreams() throws FileNotFoundException { if (new File("input.txt").exists()) { try { System.setIn(new FileInputStream("input.txt")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } void closeStreams() throws IOException { in.close(); out.close(); } String nextString() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } long nextLong() throws IOException { return Long.parseLong(nextString()); } int nextInt() throws IOException { return Integer.parseInt(nextString()); } double nextDouble() throws IOException { return Double.parseDouble(nextString()); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
e1d8d9a4bd172b818bf3bcc65e943ce5
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.Scanner; import java.util.ArrayList; public class C_Product_1_Modulo_N{ public static void main(String[] args) { Scanner s= new Scanner(System.in); int n=s.nextInt(); boolean array[]= new boolean[n]; ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); for(int i=2;i<n;i++){ if(array[i]==true){ continue; } if(n%i!=0){ list.add(i); continue; } array[i]=true; int k=2; while((k*i)<=(n-1)){ array[k*i]=true; k++; } } long res=1; for(int i=0;i<list.size();i++){ res=res*list.get(i); res=res%n; } if(res%n==1){ System.out.println(list.size()); for(int i=0;i<list.size();i++){ System.out.print(list.get(i)+" "); } } else{ long alpha=res%n; System.out.println(list.size()-1); for(int i=0;i<list.size();i++){ if(list.get(i)!=alpha){ System.out.print(list.get(i)+" "); } } } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
d74472b6979c278b5d62311f969d65b2
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.lang.*; import java.math.BigInteger; import java.util.*; public class Main{ static Scanner scanner=new Scanner(System.in); public static void main(String[] args) { int n=scanner.nextInt(); boolean b[]=new boolean [n]; long ans=1; for(int i=1;i<n;i++) { if(Gcd(n, i)!=1) { b[i]=true; }else { ans*=i; ans%=n; } } if(ans!=1)b[(int)ans]=true; StringBuilder sb=new StringBuilder(); ans=0; for(int i=1;i<n;i++) { if(!b[i]) { ans++; sb.append(i+" "); } } System.out.println(ans); System.out.println(sb); } static int Gcd(int x,int y) { return y==0?x:Gcd(y, x%y); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
ce29f6db2e353f9aa56af9df2736737d
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); LinkedList<Integer> primeFactors = primeFactorize(n); int[] array = new int[n-1]; for(int k = 1; k<n; k++){ array[k-1] = k; for (Integer i: primeFactors) { if(k % i == 0){ array[k-1] = 1; } } } long modN = 1; for(int i: array){ modN = (long)(i*modN) % n; } if(modN != 1){ array[(int)modN-1] = 1; } int subSetLength = n; for (int i: array) { if(i==1){ subSetLength--; } } System.out.println(subSetLength); System.out.print(1 + " "); for (int i: array) { if(i != 1){ System.out.print(i + " "); } } } public static LinkedList<Integer> primeFactorize(int n){ LinkedList<Integer> primeFactors = new LinkedList<Integer>(); if (n != 1) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { primeFactors = primeFactorize(i); primeFactors.addAll(primeFactorize(n / i)); return primeFactors; } } primeFactors.add(n); } return primeFactors; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
9b89acd7292033c7414c1be7b816f113
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.Scanner; public class Main { public static int gcd(int a,int b) { if(b == 0) return a; return gcd(b,a % b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = 1; int[] a = new int[100010]; int n = sc.nextInt(); long ans = 1; for(int i = 1;i <= n; ++i) if(gcd(i,n) == 1) { a[t++] = i; ans = ans * i % n; } if(ans != 1) { boolean f = false; System.out.println(t - 2); for(int i = 1;i < t; ++i) { if(a[i] == ans) continue; if(f == true) System.out.print(" "); f = true; System.out.print(a[i]); } System.out.println(); } else { System.out.println(t - 1); for(int i = 1;i < t; ++i) System.out.print(a[i] + ((i == t - 1)?"\n":" ")); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
249d3929b48377cb5915042edd00a89f
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Queue; import java.util.LinkedList; public class ProdMod { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long N = Long.parseLong(br.readLine()); Queue<Long> numbers = new LinkedList<Long>(); long MOD = 1; for (long i = 1; i < N-1; i++) { if (GCD(N, i) == 1) { numbers.add(i); MOD = (MOD * i) % N; } } if (MOD == N-1) { numbers.add(N-1); } System.out.println(numbers.size()); while (numbers.size() > 1) { System.out.print(numbers.poll() + " "); } System.out.println(numbers.poll()); } static long GCD(long a,long b) { if (b == 0) return a; return GCD(b, a % b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
82e8f0cca6cc2b55927e1dfbc3889dfe
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class C { public static void main(String[] args) { new C().solve(System.in, System.out); } public void solve(InputStream in, OutputStream out) { InputReader inputReader = new InputReader(in); PrintWriter writer = new PrintWriter(new BufferedOutputStream(out)); int n = inputReader.nextInt(); List<Integer> result = solve(n); writer.println(result.size()); for (Integer res : result) { writer.print(res + " "); } writer.close(); } public List<Integer> solve(int n) { List<Integer> result = new ArrayList<>(); long mod = 1; for (int i = 1; i < n - 1; i++) { if (nod(i, n) == 1) { result.add(i); mod = (mod * i) % n; } } if ((mod * (n - 1)) % n == 1) { result.add(n - 1); } return result; } private int nod(int x, int y) { while (true) { int mod = x % y; if (mod == 0) { return y; } x = y; y = mod; } } public List<Integer> primeSieve(int n) { boolean[] isPrime = new boolean[n + 1]; for (int i = 0; i <= n; i++) { isPrime[i] = true; } int bound = (int) Math.sqrt(n) + 1; for (int i = 2; i <= bound; i++) { if (!isPrime[i]) { continue; } for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } List<Integer> result = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (isPrime[i]) { result.add(i); } } return result; } private 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
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
483f677cd820365a3c0f70f5ccd6b6c1
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
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 Lynn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int n = in.nextInt(); boolean[] coprime = new boolean[n]; long prod = 1; int cnt = 0; for (int i = 1; i < n; i++) { if (Num.gcd(n, i) == 1) { coprime[i] = true; prod = (prod * (long) i) % n; cnt++; } } if (prod != 1) { coprime[(int) prod] = false; cnt--; } out.println(cnt); for (int i = 1; i < n; i++) { if (coprime[i] == true) { out.print(i); out.print(' '); } } out.println(""); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class Num { public static int gcd(int a, int b) { while (b != 0) { int c = a; a = b; b = c % b; } if (a < 0) a = -a; return a; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
6f1a4ac39c99675e95df08f68948c7cf
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class C_Round_716_Div2 { public static long MOD = 1000000007; static long[][] 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 n = in.nextInt(); long st = 1; int total = 1; for (long i = 2; i < n; i++) { if (gcd(i, n) != 1) { continue; } total++; st *= i; st %= n; } total -= (st != 1) ? 1 : 0; out.println(total); for (int i = 1; i < n; i++) { if (gcd(i, n) != 1) { continue; } if (st != 1 && st == i) { continue; } out.print(i + " "); } out.close(); } static boolean check(String a, String re) { int x = 0; for (int i = 0; i < re.length() && x < a.length(); i++) { if (re.charAt(i) == a.charAt(x)) { x++; } } return x == a.length(); } static String cal(String a, String b, boolean one) { //System.out.println(a + " " + b + " " + one ); StringBuilder builder = new StringBuilder(); int x = 0; int y = 0; int need = one ? 1 : 0; while (x < a.length() && y < b.length()) { int i = a.charAt(x++) - '0'; while (i != need) { builder.append(i); //System.out.println(i + ' ' + x); if (x < a.length()) { i = a.charAt(x++) - '0'; } else { i = -1; break; } } int j = b.charAt(y++) - '0'; while (j != need) { builder.append(j); if (y < b.length()) { j = b.charAt(y++) - '0'; } else { j = -1; break; } } //System.out.println(x + " " + y + " " + i + " " + j + " " + builder.toString()); if (i == j && i == need) { builder.append(need); } else { if (i != -1) { builder.append(i); } if (j != -1) { builder.append(j); } break; } } while (x < a.length()) { builder.append(a.charAt(x++)); } while (y < b.length()) { builder.append(b.charAt(y++)); } return builder.toString(); } 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; String y; public Point(int start, String end) { this.x = start; this.y = end; } @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
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
75a3f8b6af27a4a180fc91c9436ac155
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; public class Main { public static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<Integer> f = new ArrayList<Integer>(); f.add(1); long pro = 1L; for(int i=2;i<n;i++) { if(gcd(n,i)==1) { f.add(i); pro = (pro*i)%n; } } if(pro!=1) { f.remove(f.size()-1); } System.out.println(f.size()); for(int i:f) { System.out.print(i+" "); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
0f717cf4eadf6e3978150b056e815aff
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { PrintWriter out; FastReader sc; long mod=(long)(1e9+7); long maxlong=Long.MAX_VALUE; long minlong=Long.MIN_VALUE; /****************************************************************************************** *****************************************************************************************/ public void sol(){ int n=ni(); TreeSet<Integer> h=new TreeSet<>(); h.add(1); long pro=1; for(int i=2;i<n;i++) { if(gcd(i,n)==1) { h.add(i); pro=(pro*i)%n; } } if(pro%n!=1) { h.remove(n-1); } StringBuilder sb=new StringBuilder(); for(int i:h) { sb.append(i+" "); }pl(h.size()); pl(sb); } public static void main(String[] args) { Main g=new Main(); g.out=new PrintWriter(System.out); g.sc=new FastReader(); int t=1; // t=g.ni(); while(t-->0) g.sol(); g.out.flush(); } /**************************************************************************************** *****************************************************************************************/ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public int ni(){ return sc.nextInt(); }public long nl(){ return sc.nextLong(); }public double nd(){ return sc.nextDouble(); }public char[] rl(){ return sc.nextLine().toCharArray(); }public String rl1(){ return sc.nextLine(); } public void pl(Object s){ out.println(s); }public void ex(){ out.println(); } public void pr(Object s){ out.print(s); }public String next(){ return sc.next(); }public long abs(long x){ return Math.abs(x); } public int abs(int x){ return Math.abs(x); } public double abs(double x){ return Math.abs(x); }public long min(long x,long y){ return (long)Math.min(x,y); } public int min(int x,int y){ return (int)Math.min(x,y); } public double min(double x,double y){ return Math.min(x,y); }public long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); }public long lcm(long a, long b) { return (a / gcd(a, b)) * b; } void sort1(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }void sort1(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) { l.add(i); } Collections.sort(l,Collections.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } } void sort(double[] a) { ArrayList<Double> l = new ArrayList<>(); for (double i : a) { l.add(i); } Collections.sort(l); for (int i = 0; i < a.length; i++) { a[i] = l.get(i); } }long pow(long a,long b){ if(b==0){ return 1; }long p=pow(a,b/2); if(b%2==0) return (p*p)%mod; else return (((p*p)%mod)*a)%mod; } int swap(int a,int b){ return a; }long swap(long a,long b){ return a; }double swap(double a,double b){ return a; } boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0); }boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0); }public long max(long x,long y){ return (long)Math.max(x,y); } public int max(int x,int y){ return (int)Math.max(x,y); } public double max(double x,double y){ return Math.max(x,y); }long sqrt(long x){ return (long)Math.sqrt(x); }int sqrt(int x){ return (int)Math.sqrt(x); }void input(int[] ar,int n){ for(int i=0;i<n;i++)ar[i]=ni(); }void input(long[] ar,int n){ for(int i=0;i<n;i++)ar[i]=nl(); }void fill(int[] ar,int k){ Arrays.fill(ar,k); }void yes(){ pl("YES"); }void no(){ pl("NO"); } long[] sieve(int n) { long[] k=new long[n+1]; boolean[] pr=new boolean[n+1]; for(int i=1;i<=n;i++){ k[i]=i; pr[i]=true; }for(int i=2;i<=n;i++){ if(pr[i]){ for(int j=i+i;j<=n;j+=i){ pr[j]=false; if(k[j]==j){ k[j]=i; } } } }return k; } int strSmall(int[] arr, int target) { int start = 0, end = arr.length-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr[mid] >= target) { end = mid - 1; } else { ans = mid; start = mid + 1; } } return ans; } int strSmall(ArrayList<Integer> arr, int target) { int start = 0, end = arr.size()-1; int ans = -1; while (start <= end) { int mid = (start + end) / 2; if (arr.get(mid) > target) { start = mid + 1; ans=start; } else { end = mid - 1; } } return ans; }long mMultiplication(long a,long b) { long res = 0; a %= mod; while (b > 0) { if ((b & 1) > 0) { res = (res + a) % mod; } a = (2 * a) % mod; b >>= 1; } return res; }long nCr(int n, int r ,int p) { if (n<r) return 0; if (r == 0) return 1; long[] fac = new long[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }long modInverse(long n, long p) { return power(n, p - 2, p); } int[][] floydWarshall(int graph[][],int INF,int V) { int dist[][] = new int[V][V]; int i, j, k; for (i = 0; i < V; i++) for (j = 0; j < V; j++) dist[i][j] = graph[i][j]; for (k = 0; k < V; k++) { for (i = 0; i < V; i++) { for (j = 0; j < V; j++) { if (dist[i][k] + dist[k][j] < dist[i][j]) dist[i][j] = dist[i][k] + dist[k][j]; } } }return dist; } public static class Pair implements Comparable<Pair> { long x; long y; public Pair(long x, long y) { this.x = x; this.y = y; } public int compareTo(Pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
cec661179f86477036d81745c5d2f5ab
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; //_________________________________________________________________ public class Main { public static void main(String[] args) throws IOException { // File file = new File("input.txt"); FastScanner sc = new FastScanner(); // Scanner sc= new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); // int t=sc.nextInt(); // outer:while (t-->=1){ // } int n=sc.nextInt(); ArrayList<Integer> list = new ArrayList<>(); long pro=1; for (int i=1;i<n;i++){ if (gcdInt(i,n)==1){ list.add(i); pro*=i; pro%=n; } } if (pro==1){ System.out.println(list.size()); for (int i:list){ System.out.print(i+" "); } System.out.println(); } else{ System.out.println(list.size()-1); for (int i=0;i<list.size()-1;i++){ System.out.print(list.get(i)+" "); } System.out.println(); } out.flush(); } //------------------------------------if------------------------------------------------------------------------------------------------------------------------------------------------- //sieve static void primeSieve(int a[]){ //mark all odd number as prime for (int i=3;i<=1000000;i+=2){ a[i]=1; } for (long i=3;i<=1000000;i+=2){ //if the number is marked then it is prime if (a[(int) i]==1){ //mark all muliple of the number as not prime for (long j=i*i;j<=1000000;j+=i){ a[(int)j]=0; } } } a[2]=1; a[1]=a[0]=0; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static String sortString(String s) { char temp[] = s.toCharArray(); Arrays.sort(temp); return new String(temp); } static class Pair implements Comparable<Pair> { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } // to sort first part // public int compareTo(Pair other) { // if (this.a == other.a) return other.b > this.b ? -1 : 1; // else if (this.a > other.a) return 1; // else return -1; // } // public int compareTo(Pair other) { // if (this.b == other.b) return 0; // if (this.b < other.b) return 1; // else return -1; // } //sort on the basis of first part only public int compareTo(Pair other) { if (this.a == other.a) return 0; else if (this.a > other.a) return 1; else return -1; } } static int[] frequency(String s){ int fre[]= new int[26]; for (int i=0;i<s.length();i++){ fre[s.charAt(i)-'a']++; } return fre; } static int mod =(int)(1e9+7); static long mod(long x) { return ((x % mod + mod) % mod); } static long add(long x, long y) { return mod(mod(x) + mod(y)); } static long mul(long x, long y) { return mod(mod(x) * mod(y)); } //Fast Power(logN) static long BinaryExponentiation(long a,long b){ long ans=1; while (b>0){ if (b%2==1){ ans=(ans*a)%mod; } a=(a*a)%mod; b/=2; } return ans; } static int[] find(int n, int start, int diff) { int a[] = new int[n]; a[0] = start; for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff; return a; } static void swap(int a, int b) { int c = a; a = b; b = c; } static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } static boolean sorted(int a[]) { int n = a.length; boolean flag = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) flag = false; } if (flag) return true; else return false; } public static int findlog(long n) { if (n == 0) return 0; if (n == 1) return 0; if (n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if (den == 0) return 0; return (int) (num / den); } public static long gcd(long a, long b) { if (b % a == 0) return a; return gcd(b % a, a); } public static int gcdInt(int a, int b) { if (b % a == 0) return a; return gcdInt(b % a, a); } static void sortReverse(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); // Collections.sort.(l); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
da7d75f2e00f385457ecf4b4f4ca1427
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; import java.math.*; import java.math.BigInteger; //import javafx.util.*; public final class B { static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); static ArrayList<ArrayList<Integer>> g; static long mod=1000000007; static int D1[],D2[],par[]; static boolean set[]; static long INF=Long.MAX_VALUE; public static void main(String args[])throws IOException { int N=i(); ArrayList<Integer> A=new ArrayList<>(); A.add(1); long p=1; for(long i=2; i<N; i++) { if(GCD(i,N)==1) { p=((p%N)*(i))%N; A.add((int)i); } } if(p!=1)A.remove(A.size()-1); System.out.println(A.size()); for(int a:A)ans.append(a+" "); System.out.println(ans); } static long fact(long N) { long num=1L; while(N>=1) { num=((num%mod)*(N%mod))%mod; N--; } return num; } static boolean reverse(long A[],int l,int r) { while(l<r) { long t=A[l]; A[l]=A[r]; A[r]=t; l++; r--; } if(isSorted(A))return true; else return false; } static boolean isSorted(long A[]) { for(int i=1; i<A.length; i++)if(A[i]<A[i-1])return false; return true; } static boolean isPalindrome(char X[],int l,int r) { while(l<r) { if(X[l]!=X[r])return false; l++; r--; } return true; } static long min(long a,long b,long c) { return Math.min(a, Math.min(c, b)); } static void print(int a) { System.out.println("! "+a); } static int ask(int a,int b) { System.out.println("? "+a+" "+b); return i(); } static int find(int a) { if(par[a]<0)return a; return par[a]=find(par[a]); } static void union(int a,int b) { a=find(a); b=find(b); if(a!=b) { par[a]+=par[b]; //transfers the size par[b]=a; //changes the parent } } static void swap(char A[],int a,int b) { char ch=A[a]; A[a]=A[b]; A[b]=ch; } static void sort(long[] a) //check for long { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void setGraph(int N) { g=new ArrayList<ArrayList<Integer>>(); for(int i=0; i<=N; i++) { g.add(new ArrayList<Integer>()); } } static long pow(long a,long b) { long mod=1000000007; long pow=1; long x=a; while(b!=0) { if((b&1)!=0)pow=(pow*x)%mod; x=(x*x)%mod; b/=2; } return pow; } static long toggleBits(long x)//one's complement || Toggle bits { int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1; return ((1<<n)-1)^x; } static int countBits(long a) { return (int)(Math.log(a)/Math.log(2)+1); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static boolean isPrime(long N) { if (N<=1) return false; if (N<=3) return true; if (N%2 == 0 || N%3 == 0) return false; for (int i=5; i*i<=N; i=i+6) if (N%i == 0 || N%(i+2) == 0) return false; return true; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } //Debugging Functions Starts static void print(char A[]) { for(char c:A)System.out.print(c+" "); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Integer> A) { for(int a:A)System.out.print(a+" "); System.out.println(); } //Debugging Functions END //---------------------- //IO FUNCTIONS STARTS static HashMap<Integer,Integer> Hash(int A[]) { HashMap<Integer,Integer> mp=new HashMap<>(); for(int a:A) { int f=mp.getOrDefault(a,0)+1; mp.put(a, f); } return mp; } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } //IO FUNCTIONS END } //class pair implements Comparable<pair>{ // int index; long a; // pair(long a,int index) // { // this.a=a; // this.index=index; // } // public int compareTo(pair X) // { // if(this.a>X.a)return 1; // if(this.a==X.a)return this.index-X.index; // return -1; // } //} //Code For FastReader //Code For FastReader //Code For FastReader //Code For FastReader class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st==null || !st.hasMoreElements()) { try { st=new StringTokenizer(br.readLine()); } catch(IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } //gey double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str=""; try { str=br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
4bb96d01e6e8c6caac311c0c40af8d81
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));} String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());} catch (IOException e){e.printStackTrace();}}return st.nextToken();} int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());} String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; } } static FastReader sc = new FastReader(); static long mod = (long) (1e9+7); public static void main (String[] args){ PrintWriter out = new PrintWriter(System.out); int t = 1; // t = sc.nextInt(); z :while(t-->0){ int n = sc.nextInt(); List<Integer> ans = new ArrayList<>(); long p = 1; for(int i=1;i<n;i++) { if(gcd(i,n) == 1) { ans.add(i); p = p*i%n; } } List<Integer> res = new ArrayList<>(); if(p!=1) { for(int x:ans) { if(x != p) res.add(x); } ans = res; } out.write(ans.size()+"\n"); for(int val : ans) out.write(val+" "); } out.close(); } private static int gcd(int a, int b) { if(b == 0) return a; return gcd(b,a%b); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
5c5d223c84dc7d03ae977dea7c6c3343
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Stack1 { 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(); } } static Reader sc = new Reader(); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws IOException { /* * For integer input: int n=inputInt(); * For long input: long n=inputLong(); * For double input: double n=inputDouble(); * For String input: String s=inputString(); * Logic goes here * For printing without space: print(a+""); where a is a variable of any datatype * For printing with space: printSp(a+""); where a is a variable of any datatype * For printing with new line: println(a+""); where a is a variable of any datatype */ Scanner sc = new Scanner(System.in); int n=sc.nextInt(); long mul=1; long count=0; long lastindex=0; List<Integer> list=new ArrayList<>(); for(int j=1;j<=n;j++){ long gcd=gcd(j,n); if(gcd==1){ mul=mul%n*j%n; // System.out.println(mul%n); if(mul%n==1){ count++; list.add(j); lastindex=count; }else { count++; list.add(j); } } } System.out.println(lastindex); for(int j=0;j<lastindex;j++){ System.out.println(list.get(j)); } bw.flush(); bw.close(); } public static void dfs1(List<List<Integer>> g, boolean[] visited, Stack<Integer> stack, int num) { visited[num] = true; for (Integer integer : g.get(num)) { if (!visited[integer]) { dfs1(g, visited, stack, integer); } } stack.push(num); } public static void dfs2(List<List<Integer>> g, boolean[] visited, List<Integer> list, int num, int c, int[] raj) { visited[num] = true; for (Integer integer : g.get(num)) { if (!visited[integer]) { dfs2(g, visited, list, integer, c, raj); } } raj[num] = c; list.add(num); } public static int inputInt() throws IOException { return sc.nextInt(); } public static long inputLong() throws IOException { return sc.nextLong(); } public static double inputDouble() throws IOException { return sc.nextDouble(); } public static String inputString() throws IOException { return sc.readLine(); } public static void print(String a) throws IOException { bw.write(a); } public static void printSp(String a) throws IOException { bw.write(a + " "); } public static void println(String a) throws IOException { bw.write(a + "\n"); } public static long getAns(int[] ar, int c, long[][] dp, int i, int sign) { if (i < 0) { return 1; } if (c <= 0) { return 1; } dp[i][c] = Math.max(dp[i][c], Math.max(ar[i] * getAns(ar, c - 1, dp, i - 1, sign), getAns(ar, c, dp, i - 1, 1))); return dp[i][c]; } public static long power(long a, long b, long c) { long ans = 1; while (b != 0) { if (b % 2 == 1) { ans = ans * a; ans %= c; } a = a * a; a %= c; b /= 2; } return ans; } public static long power1(long a, long b, long c) { long ans = 1; while (b != 0) { if (b % 2 == 1) { ans = multiply(ans, a, c); } a = multiply(a, a, c); b /= 2; } return ans; } public static long multiply(long a, long b, long c) { long res = 0; a %= c; while (b > 0) { if (b % 2 == 1) { res = (res + a) % c; } a = (a + a) % c; b /= 2; } return res % c; } public static long totient(long n) { long result = n; for (long i = 2; i * i <= n; i++) { if (n % i == 0) { //sum=sum+2*i; while (n % i == 0) { n /= i; // sum=sum+n; } result -= result / i; } } if (n > 1) { result -= result / n; } return result; } public static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } public static boolean[] primes(int n) { boolean[] p = new boolean[n + 1]; p[0] = false; p[1] = false; for (int i = 2; i <= n; i++) { p[i] = true; } for (int i = 2; i * i <= n; i++) { if (p[i]) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
09241bf01f1e60e86c1a8df86a4d6bb7
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.util.*; import java.io.*; //import java.math.*; public class Task{ // ..............code begins here.............. static long mod=(long)1e9+7,inf=(long)1e17; static int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { int q = a / m; int t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0){x += m0;} return x; } static void solve() throws IOException { int n=int_v(read()); boolean[] b=new boolean[n]; List<Integer> l=new ArrayList<>(); l.add(1);b[1]=true; for(int i=2;i<n;i++){ if(b[i]) continue; if(gcd(i,n)==1){ int inv=modInverse(i,n); if(inv<n&&!b[inv]&&i!=inv){ l.add(i);l.add(inv); b[i]=b[inv]=true; } } else b[i]=true; } List<Integer> l1=new ArrayList<>(); for(int i=2;i<n;i++){ if(!b[i]){ l1.add(i); //out.write(i+", "); } } if(l1.size()>1)for(int z:l1)l.add(z); Collections.sort(l); out.write(l.size()+"\n"); for(int z:l)out.write(z+" "); } // public static void main(String[] args) throws IOException{ assign(); int t=1;//int_v(read()),cn=1; while(t--!=0){ //out.write("Case #"+cn+": "); solve(); //cn++; } out.flush(); } // taking inputs static BufferedReader s1; static BufferedWriter out; static String read() throws IOException{String line="";while(line.length()==0){line=s1.readLine();continue;}return line;} static int int_v (String s1){return Integer.parseInt(s1);} static long long_v(String s1){return Long.parseLong(s1);} static void sort(int[] a){List<Integer> l=new ArrayList<>();for(int x:a){l.add(x);}Collections.sort(l);for(int i=0;i<a.length;i++){a[i]=l.get(i);}} static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;} static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;} static void assign(){s1=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));} //static String setpreciosion(double d,int k){BigDecimal d1 = new BigDecimal(Double.toString(d));return d1.setScale(k,RoundingMode.HALF_UP).toString();}//UP DOWN HALF_UP static int add(int a,int b){int z=a+b;if(z>=mod)z-=mod;return z;} static long gcd(long a,long b){if(b==0){return a;}return gcd(b,a%b);} static long Modpow(long a,long p,long m){long res=1;while(p>0){if((p&1)!=0){res=(res*a)%m;}p >>=1;a=(a*a)%m;}return res%m;} static long Modmul(long a,long b,long m){return ((a%m)*(b%m))%m;} static long ModInv(long a,long m){return Modpow(a,m-2,m);} //static long nck(int n,int r,long m){if(r>n){return 0l;}return Modmul(f[n],ModInv(Modmul(f[n-r],f[r],m),m),m);} //static long[] f; }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
d66de85448b92b6e72ed95d41e356878
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
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.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pranay2516 */ 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); CProduct1ModuloN solver = new CProduct1ModuloN(); solver.solve(1, in, out); out.close(); } static class CProduct1ModuloN { public void solve(int testNumber, FastReader in, PrintWriter out) { long n = in.nextInt(); ArrayList<Long> ans = new ArrayList<>(); long pro = 1; for (long i = 1; i < n; ++i) { if (func.gcd(i, n) == 1) { ans.add(i); pro = func.modMul(pro, i, n); } } if (pro % n != 1) { ans.remove(ans.size() - 1); } out.println(ans.size()); for (Long e : ans) { out.print(e + " "); } out.println(); } } 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class func { public static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } public static long modMul(long a, long b, long m) { a %= m; b %= m; return ((a * b) % m + m) % m; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
e5ace6060be6d804a6c0b1c70f69c731
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.math.BigInteger; import java.util.*; public class Main { private static boolean gcd(int i, int j){ int max = Math.max(i,j); int min = Math.min(i,j); while(min != 0){ int now = min; min = max%min; max = now; } return max == 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); long sum = 1; List<Integer> list = new ArrayList<>(); for(int j = 1; j < i; j++){ if(gcd(i,j)){ list.add(j); sum = sum * j % i; } } if(sum != 1){ list.remove(list.size() - 1); } System.out.println(list.size()); for(int j = 0; j < list.size(); j++){ if(j != 0){ System.out.print(" "); } System.out.print(list.get(j)); } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
8c2a9e38c2b9450ae2a77af1b6f7d895
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
//Utilities import java.io.*; import java.util.*; public class Main { static int n; static boolean[] used; static ArrayList<Integer> res, extra; public static void main(String[] args) throws IOException { n = in.iscan(); res = new ArrayList<Integer>(); extra = new ArrayList<Integer>(); res.add(1); used = new boolean[n]; for (int i = 2; i <= n-1; i++) { if (UTILITIES.gcd(i, n) != 1 || used[i]) { continue; } int need = modInverse(i, n); if (need != i) { res.add(i); res.add(need); used[i] = used[need] = true; } else { extra.add(i); used[i] = true; } } if (extra.size() > 1) { res.addAll(extra); } out.println(res.size()); Collections.sort(res); for (int x : res) { out.print(x + " "); } out.println(); out.close(); } // helper function from GeeksForGeeks static int modInverse(int a, int m) { int m0 = m; int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient int q = a / m; int t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static int gcd (int a, int b) { return b == 0 ? a : gcd (b, a % b); } public static int lcm (int a, int b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static int fast_pow (int b, int x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output
PASSED
d269163b0bc7cf1e1dc48bbbf506a6d7
train_110.jsonl
1618839300
Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$.
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Codeforces { private static final Scanner sc = new Scanner(System.in); private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static final long MOD = (long) (1e9 + 7); private static PrintWriter out = new PrintWriter(System.out); private static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); /** Optimal(Maximum,Minimum) Answers * 1. Binary search * 2. Prefix Suffix * 3. Greedy (sorting searching) * 4. DP **/ public static void solve(int T) throws IOException { int n = sc.nextInt(); boolean[] sieve = new boolean[n + 1]; Arrays.fill(sieve,true); for (int i = 2; i <= n - 1; i++) { if (n%i == 0) { sieve[i] = false; for (int j = 2 * i; j <= n - 1; j += i) { sieve[j] = false; } } } ArrayList<Integer> ans = new ArrayList<>(); for (int i = 1; i <= n - 1; i++) { if (sieve[i]) { ans.add(i); } } long prod = 1; for (int x : ans) { prod = (x * prod) % n; } if (prod != 1) { ans.remove(ans.size()-1); } System.out.println(ans.size()); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } public static void main(String[] args) throws IOException { int t = 1; // t = sc.nextInt(); while (t-- > 0){ solve(t); } System.gc(); } }
Java
["5", "8"]
1 second
["3\n1 2 3", "4\n1 3 5 7"]
NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$.
Java 8
standard input
[ "greedy", "number theory" ]
d55afdb4a83aebdfce5a62e4ec934adb
The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$).
1,600
The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any.
standard output