src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
//package cf584d12; import java.io.*; import java.util.*; import static java.lang.Math.*; public class A { public static void main(String[] args) { MyScanner sc = new MyScanner(); int n = sc.nextInt(); Integer[] a = new Integer[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); Arrays.sort(a); boolean[] b = new boolean[n]; int ans = 0; for(int i = 0; i < n; i++) if(!b[i]) { ans++; for(int j = i + 1; j < n; j++) if(a[j] % a[i] == 0) b[j] = true; } out.println(ans); out.close(); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; 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; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int []a = new int [n]; boolean []used = new boolean[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } Arrays.sort(a); int ans = 0; for (int i = 0; i < used.length; i++) { if (!used[i]){ ans++; for (int j = i; j < used.length; j++) { if (a[j]%a[i] == 0){ used[j] = true; } } } } System.out.print(ans); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class algo_2701 { public static void main(String args[]) { Scanner ex=new Scanner(System.in); int n=ex.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=ex.nextInt(); Arrays.sort(arr); int ans=0; int check[]=new int[n]; for(int i=0;i<n;i++) { if(check[i]==0) { ans++; for(int j=i;j<n;j++) { if(arr[j]%arr[i]==0) check[j]=1; } } } System.out.println(ans); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; import java.util.concurrent.ThreadLocalRandom; @SuppressWarnings("Duplicates") public class solveLOL { FastScanner in; PrintWriter out; boolean systemIO = true, multitests = false; int INF = Integer.MAX_VALUE / 2; void solve() { int n = in.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr); boolean used[] = new boolean[n]; int k = 0; for (int i = 0; i < n; i++) { if (!used[i]) { used[i] = true; for (int j = i + 1; j < n; j++) { if (!used[j] && arr[j] % arr[i] == 0) { used[j] = true; } } k++; } } System.out.println(k); } class pair implements Comparable<pair> { int a; int b; pair(int A, int B) { this.a = A; this.b = B; } public int compareTo(pair o) { return a != o.a ? Double.compare(a, o.a) : b - o.b; } } void shuffleArray(long[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } } void printArray(long[] ar) { for (long k : ar) { System.out.print(k + " "); } System.out.println(); } void reverseArray(long[] ar) { for (int i = 0, j = ar.length - 1; i < j; i++, j--) { long a = ar[i]; ar[i] = ar[j]; ar[j] = a; } } private void run() throws IOException { if (systemIO) { in = new solveLOL.FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new solveLOL.FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } for (int t = multitests ? in.nextInt() : 1; t-- > 0; ) solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] arg) throws IOException { new solveLOL().run(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.lang.reflect.Array; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] colors = new int[n]; for (int i = 0; i < n; i++) { colors[i] = scanner.nextInt(); } Arrays.sort(colors); int amountOfColors = 0; for (int i = 0; i < n; i++) { if (colors[i] != 0){ amountOfColors++; int color = colors[i]; for (int j = i; j < n; j++) { if (colors[j] % color == 0){ colors[j] = 0; } } } } System.out.println(amountOfColors); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int ans = 0; boolean[] taken = new boolean[n]; Arrays.sort(a); for (int i = 0; i < n; i++) { if (taken[i]) continue; ans++; for (int j = i; j < n; j++) if (a[j] % a[i] == 0) taken[j] = true; } out.println(ans); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { 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 String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class ques1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int b = sc.nextInt(); ArrayList<Integer> ar=new ArrayList<>(); for(int i=0;i<b;i++){ ar.add(sc.nextInt()); } Collections.sort(ar); int count=0; int i=0; while(ar.size()!=0) { int tmep=ar.get(i); int v=ar.remove(i); count++; int j=0; while(j<ar.size()){ if(ar.get(j)%tmep==0){ int a=ar.remove(j); } else j++; } } System.out.println(count); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(br.readLine()); StringTokenizer tokenizer = new StringTokenizer(br.readLine()); boolean[] arr = new boolean[101]; int[] nums = new int[n+1]; int colors = 0; for(int i = 1; i <= n; i++) { nums[i] = Integer.parseInt(tokenizer.nextToken()); arr[nums[i]] = true; } Arrays.parallelSort(nums); for(int i = 1; i <= n; i++) { boolean newColor = false; if(!arr[nums[i]]) { continue; } for(int j = nums[i]; j <= 100; j += nums[i]) { if(arr[j]) { arr[j] = false; newColor = true; } } if(newColor) { colors++; } } bw.write(String.valueOf(colors)); br.close(); bw.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.text.*; import java.math.*; import static java.lang.Integer.*; import static java.lang.Double.*; import java.lang.Math.*; public class A { public static void main(String[] args) throws Exception { new A().run(); } public void run() throws Exception { FastIO file = new FastIO(); int n = file.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = file.nextInt(); Arrays.sort(a); boolean[] used = new boolean[n]; int count = 0; for (int i = 0; i < n; i++) { if (!used[i]) { count++; for (int j = i; j < n; j++) { if (a[j] % a[i] == 0) { used[j] = true; } } } } System.out.println(count); } public static class FastIO { BufferedReader br; StringTokenizer st; public FastIO() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static long pow(long n, long p, long mod) { if (p == 0) return 1; if (p == 1) return n % mod; if (p % 2 == 0) { long temp = pow(n, p / 2, mod); return (temp * temp) % mod; } else { long temp = pow(n, (p - 1) / 2, mod); temp = (temp * temp) % mod; return (temp * n) % mod; } } public static long pow(long n, long p) { if (p == 0) return 1; if (p == 1) return n; if (p % 2 == 0) { long temp = pow(n, p / 2); return (temp * temp); } else { long temp = pow(n, (p - 1) / 2); temp = (temp * temp); return (temp * n); } } public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } public static boolean isPrime(int 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; } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
/** * ******* Created by bla on 14/9/19 6:17 PM******* */ import java.io.*; import java.util.*; public class A1209 { public static void main(String[] args) throws IOException { try (Input input = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) { int n = input.nextInt(); int[] arr = input.readIntArray(n); Arrays.sort(arr); int ans =0; boolean[] vis = new boolean[n]; for(int i=0;i<n;i++){ if(!vis[i]){ vis[i]=true; for(int j=i+1;j<n;j++){ if(!vis[j] && arr[j]%arr[i]==0){ vis[j]=true; } } ans++; } } System.out.println(ans); } } interface Input extends Closeable { String next() throws IOException; default int nextInt() throws IOException { return Integer.parseInt(next()); } default long nextLong() throws IOException { return Long.parseLong(next()); } default double nextDouble() throws IOException { return Double.parseDouble(next()); } default int[] readIntArray() throws IOException { return readIntArray(nextInt()); } default int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i = 0; i < array.length; i++) { array[i] = nextInt(); } return array; } default long[] readLongArray(int size) throws IOException { long[] array = new long[size]; for (int i = 0; i < array.length; i++) { array[i] = nextLong(); } return array; } } private static class StandardInput implements Input { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer stringTokenizer; @Override public void close() throws IOException { reader.close(); } @Override public String next() throws IOException { if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.text.*; public class Main{ //SOLUTION BEGIN //Into the Hardware Mode void pre() throws Exception{} void solve(int TC)throws Exception{ int n = ni(); int[] a = new int[n]; for(int i = 0; i< n; i++)a[i] = ni(); Arrays.sort(a); int ans = 0; for(int i = 0; i< n; i++){ if(a[i] == -1)continue; ans++; for(int j = i+1; j< n; j++)if(a[j]%a[i] == 0)a[j] = -1; } pn(ans); } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} long IINF = (long)1e18, mod = (long)1e9+7; final int INF = (int)1e9, MX = (int)2e6+5; DecimalFormat df = new DecimalFormat("0.00000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-6; static boolean multipleTC = false, memory = false, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ if(fileIO){ in = new FastReader("input.txt"); out = new PrintWriter("output.txt"); }else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = (multipleTC)?ni():1; pre(); for(int t = 1; t<= T; t++)solve(t); out.flush(); out.close(); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str = ""; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class mainA { public static PrintWriter out = new PrintWriter(System.out); public static FastScanner enter = new FastScanner(System.in); public static void main(String[] args) throws IOException { solve(); out.close(); } public static boolean[] was; private static void solve() throws IOException{ int n=enter.nextInt(); int[] arr=new int[n]; was=new boolean[n]; for (int i = 0; i <n ; i++) { arr[i]=enter.nextInt(); } Arrays.sort(arr); int ans=0; for (int i = 0; i <n ; i++) { if(was[i]) continue; find(i, arr); ans++; } out.println(ans); } public static void find (int num, int[] arr){ for (int i = num+1; i <arr.length ; i++) { if(arr[i]%arr[num]==0) was[i]=true; } } static class FastScanner { BufferedReader br; StringTokenizer stok; FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } char nextChar() throws IOException { return (char) (br.read()); } String nextLine() throws IOException { return br.readLine(); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
// package cf1209; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; import java.util.Random; public class CFA { private static final String INPUT = "8\n" + "7 6 5 4 3 2 2 3\n"; private static final int MOD = 1_000_000_007; private PrintWriter out; private FastScanner sc; public static void main(String[] args) { new CFA().run(); } public void run() { sc = new FastScanner(oj ? System.in : new ByteArrayInputStream(INPUT.getBytes())); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } static class Color { int nr; boolean used; public Color(int nr) { this.nr = nr; } } private void solve() { int n = sc.nextInt(); Color[] a = new Color[n]; for (int i = 0; i < n; i++) { a[i] = new Color(sc.nextInt()); } Arrays.sort(a, Comparator.comparingInt(c -> c.nr)); int ans = 0; for (int i = 0; i < n; i++) { if (a[i].used) continue; ans++; for (int j = i+1; j < n; j++) { if (a[j].nr % a[i].nr == 0) { a[j].used = true; } } } out.println(ans); } //******************************************************************************************** //******************************************************************************************** //******************************************************************************************** static class SolutionFailedException extends Exception { int code; public SolutionFailedException(int code) { this.code = code; } } int [] sort(int [] a) { final int SHIFT = 16, MASK = (1 << SHIFT) - 1, SIZE = (1 << SHIFT) + 1; int n = a.length, ta [] = new int [n], ai [] = new int [SIZE]; for (int i = 0; i < n; ai[(a[i] & MASK) + 1]++, i++); for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++); for (int i = 0; i < n; ta[ai[a[i] & MASK]++] = a[i], i++); int [] t = a; a = ta; ta = t; ai = new int [SIZE]; for (int i = 0; i < n; ai[(a[i] >> SHIFT) + 1]++, i++); for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++); for (int i = 0; i < n; ta[ai[a[i] >> SHIFT]++] = a[i], i++); return ta; } private static void shuffle(int[] array) { Random random = new Random(); for (int i = array.length - 1; i > 0; i--) { int index = random.nextInt(i + 1); int temp = array[index]; array[index] = array[i]; array[i] = temp; } } /** * If searched element doesn't exist, returns index of first element which is bigger than searched value.<br> * If searched element is bigger than any array element function returns first index after last element.<br> * If searched element is lower than any array element function returns index of first element.<br> * If there are many values equals searched value function returns first occurrence.<br> */ private static int lowerBound(long[] arr, long key) { int lo = 0; int hi = arr.length - 1; while (lo < hi) { int mid = (lo + hi) / 2; if (key <= arr[mid]) { hi = mid - 1; } else { lo = mid + 1; } } return arr[lo] < key ? lo + 1 : lo; } /** * Returns index of first element which is grater than searched value. * If searched element is bigger than any array element, returns first index after last element. */ private static int upperBound(long[] arr, long key) { int lo = 0; int hi = arr.length - 1; while (lo < hi) { int mid = (lo + hi) / 2; if (key >= arr[mid]) { lo = mid + 1; } else { hi = mid; } } return arr[lo] <= key ? lo + 1 : lo; } private static int ceil(double d) { int ret = (int) d; return ret == d ? ret : ret + 1; } private static int round(double d) { return (int) (d + 0.5); } private static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } private static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } private int[] readIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = sc.nextInt(); } return res; } private long[] readLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = sc.nextLong(); } return res; } @SuppressWarnings("unused") static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } Arrays.sort(a); int nc = 0; for (int i = 0; i < n; i++) { boolean divs = false; for (int j = 0; j < i; j++) { if (a[i] % a[j] == 0) { divs = true; break; } } if (!divs) { nc++; } } out.println(nc); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
/** * Date: 14 Sep, 2019 * Link: * * @author Prasad-Chaudhari * @linkedIn: https://www.linkedin.com/in/prasad-chaudhari-841655a6/ * @git: https://github.com/Prasad-Chaudhari */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; public class newProgram5 { public static void main(String[] args) throws IOException { // TODO code application logic here FastIO in = new FastIO(); int n = in.ni(); int a[] = in.gia(n); int freq[] = new int[100 + 1]; for (int i = 0; i < n; i++) { freq[a[i]]++; } int k = 0; for (int i = 1; i <= 100; i++) { if (freq[i] > 0) { for (int j = i; j <= 100; j += i) { freq[j] = 0; } k++; // System.out.println(i); } } System.out.println(k); in.bw.flush(); } static class FastIO { private final BufferedReader br; private final BufferedWriter bw; private String s[]; private int index; private final StringBuilder sb; public FastIO() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8")); s = br.readLine().split(" "); sb = new StringBuilder(); index = 0; } public int ni() throws IOException { return Integer.parseInt(nextToken()); } public double nd() throws IOException { return Double.parseDouble(nextToken()); } public long nl() throws IOException { return Long.parseLong(nextToken()); } public String next() throws IOException { return nextToken(); } public String nli() throws IOException { try { return br.readLine(); } catch (IOException ex) { } return null; } public int[] gia(int n) throws IOException { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public int[] gia(int n, int start, int end) throws IOException { validate(n, start, end); int a[] = new int[n]; for (int i = start; i < end; i++) { a[i] = ni(); } return a; } public double[] gda(int n) throws IOException { double a[] = new double[n]; for (int i = 0; i < n; i++) { a[i] = nd(); } return a; } public double[] gda(int n, int start, int end) throws IOException { validate(n, start, end); double a[] = new double[n]; for (int i = start; i < end; i++) { a[i] = nd(); } return a; } public long[] gla(int n) throws IOException { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public long[] gla(int n, int start, int end) throws IOException { validate(n, start, end); long a[] = new long[n]; for (int i = start; i < end; i++) { a[i] = nl(); } return a; } public int[][][] gwtree(int n) throws IOException { int m = n - 1; int adja[][] = new int[n + 1][]; int weight[][] = new int[n + 1][]; int from[] = new int[m]; int to[] = new int[m]; int count[] = new int[n + 1]; int cost[] = new int[n + 1]; for (int i = 0; i < m; i++) { from[i] = i + 1; to[i] = ni(); cost[i] = ni(); count[from[i]]++; count[to[i]]++; } for (int i = 0; i <= n; i++) { adja[i] = new int[count[i]]; weight[i] = new int[count[i]]; } for (int i = 0; i < m; i++) { adja[from[i]][count[from[i]] - 1] = to[i]; adja[to[i]][count[to[i]] - 1] = from[i]; weight[from[i]][count[from[i]] - 1] = cost[i]; weight[to[i]][count[to[i]] - 1] = cost[i]; count[from[i]]--; count[to[i]]--; } return new int[][][] { adja, weight }; } public int[][][] gwg(int n, int m) throws IOException { int adja[][] = new int[n + 1][]; int weight[][] = new int[n + 1][]; int from[] = new int[m]; int to[] = new int[m]; int count[] = new int[n + 1]; int cost[] = new int[n + 1]; for (int i = 0; i < m; i++) { from[i] = ni(); to[i] = ni(); cost[i] = ni(); count[from[i]]++; count[to[i]]++; } for (int i = 0; i <= n; i++) { adja[i] = new int[count[i]]; weight[i] = new int[count[i]]; } for (int i = 0; i < m; i++) { adja[from[i]][count[from[i]] - 1] = to[i]; adja[to[i]][count[to[i]] - 1] = from[i]; weight[from[i]][count[from[i]] - 1] = cost[i]; weight[to[i]][count[to[i]] - 1] = cost[i]; count[from[i]]--; count[to[i]]--; } return new int[][][] { adja, weight }; } public int[][] gtree(int n) throws IOException { int adja[][] = new int[n + 1][]; int from[] = new int[n - 1]; int to[] = new int[n - 1]; int count[] = new int[n + 1]; for (int i = 0; i < n - 1; i++) { from[i] = i + 1; to[i] = ni(); count[from[i]]++; count[to[i]]++; } for (int i = 0; i <= n; i++) { adja[i] = new int[count[i]]; } for (int i = 0; i < n - 1; i++) { adja[from[i]][--count[from[i]]] = to[i]; adja[to[i]][--count[to[i]]] = from[i]; } return adja; } public int[][] gg(int n, int m) throws IOException { int adja[][] = new int[n + 1][]; int from[] = new int[m]; int to[] = new int[m]; int count[] = new int[n + 1]; for (int i = 0; i < m; i++) { from[i] = ni(); to[i] = ni(); count[from[i]]++; count[to[i]]++; } for (int i = 0; i <= n; i++) { adja[i] = new int[count[i]]; } for (int i = 0; i < m; i++) { adja[from[i]][--count[from[i]]] = to[i]; adja[to[i]][--count[to[i]]] = from[i]; } return adja; } public void print(String s) throws IOException { bw.write(s); } public void println(String s) throws IOException { bw.write(s); bw.newLine(); } public void print(int s) throws IOException { bw.write(s + ""); } public void println(int s) throws IOException { bw.write(s + ""); bw.newLine(); } public void print(long s) throws IOException { bw.write(s + ""); } public void println(long s) throws IOException { bw.write(s + ""); bw.newLine(); } public void print(double s) throws IOException { bw.write(s + ""); } public void println(double s) throws IOException { bw.write(s + ""); bw.newLine(); } private String nextToken() throws IndexOutOfBoundsException, IOException { if (index == s.length) { s = br.readLine().split(" "); index = 0; } return s[index++]; } private void validate(int n, int start, int end) { if (start < 0 || end >= n) { throw new IllegalArgumentException(); } } } static class Data implements Comparable<Data> { int a, b; public Data(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(Data o) { if (a == o.a) { return Integer.compare(b, o.b); } return Integer.compare(a, o.a); } public static void sort(int a[]) { Data d[] = new Data[a.length]; for (int i = 0; i < a.length; i++) { d[i] = new Data(a[i], 0); } Arrays.sort(d); for (int i = 0; i < a.length; i++) { a[i] = d[i].a; } } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
/** * Created by Alyssa Herbst on 9/14/19 9:05 AM. */ import java.util.*; import java.io.*; import static java.lang.Math.*; public class B { static StringBuilder sb; static int N; static int[] A; static boolean[] B; public static void main(String[] args) { FastScanner sc = new FastScanner(); //Scanner sc = new Scanner(System.in); sb = new StringBuilder(); N = sc.nextInt(); A = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } Arrays.sort(A); B = new boolean[N]; int count = 0; for (int i = 0; i < A.length; i++) { if(B[i]) { continue; } else { count++; B[i] = true; } for (int j = i + 1; j < A.length; j++) { if(A[j] % A[i] == 0) { B[j] = true; } } } sb.append(count); System.out.println(sb); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(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 readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntBrray(int n) { int[] a = new int[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextInt(); } return a; } long[] readLongBrray(int n) { long[] a = new long[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextLong(); } return a; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; import java.util.Arrays; public class a{ public static void main(String[] args)throws IOException{ br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = nextInt(); int v[] = new int[n]; int fv[] = new int[101]; for(int i = 0; i<n;i++){ v[i] = nextInt(); } Arrays.sort(v); for(int i = 0; i<n;i++){ for(int j = i; j<n;j++){ if(v[j]%v[i]==0){ v[j] = v[i]; fv[v[j]]++; } } } int ans = 0; for(int i = 0; i<101;i++){ if(fv[i]!=0){ ans++; } } out.println(ans); out.close(); } static BufferedReader br; static StringTokenizer st = new StringTokenizer(""); static String next()throws IOException{ while(!st.hasMoreTokens()){ st = new StringTokenizer(br.readLine()); } return st.nextToken(); } static int nextInt()throws IOException{ return Integer.parseInt(next()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; import java.util.Map.Entry; public class ProblemA { public static void main (String args[]) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); String s1=br.readLine(); String[] s=s1.split(" "); int a[] = new int[n]; for(int i = 0;i<n;i++) { a[i]=Integer.parseInt(s[i]); } Arrays.sort(a); System.out.println(findColour(a,n)); } public static int findColour(int [] a , int n) { Map <Integer,Integer> mp = new HashMap<Integer,Integer>(); int f=0; for(int i = 0; i<n;i++) { f=0; for (Map.Entry<Integer,Integer> entry : mp.entrySet()) { if(a[i] % entry.getKey()==0) { f=1; break; } } if(f==0) { mp.put(a[i],1); } } return mp.size(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Scanner; public class Loader { private final static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); ArrayList<Integer> l = new ArrayList<>(); for (int i = 0; i < n; i++) { l.add(in.nextInt()); } Collections.sort(l); int k = 0; for (int i = 0; i < l.size(); i++) { if (l.get(i) == 0) continue; for (int j = i + 1; j < l.size(); j++) { if (l.get(j) == 0) continue; if (l.get(j) % l.get(i) == 0) { l.set(j, 0); } } l.set(i, 0); k++; } System.out.println(k); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; /** * https://codeforces.com/contests */ public class TaskA { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { int a = sc.nextInt(); boolean flag = false; // true = не нужно добавлять List<Integer> toRemove = new ArrayList<>(); for (int b : set) { if (a % b == 0) { flag = true; break; } else if (b % a == 0 && a < b) { toRemove.add(b); } } for (int r: toRemove) { set.remove(r); } if (!flag) { set.add(a); } } System.out.println(set.size()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*;import java.util.*; import java.math.*; public class Main{ static int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE; static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static int max(int a,int b) { return Math.max(a, b); } static int min(int a,int b) { return Math.min(a, b); } static int i()throws IOException { if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return Integer.parseInt(st.nextToken()); } static long l()throws IOException { if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return Long.parseLong(st.nextToken()); } static String s()throws IOException { if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } static double d()throws IOException { if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return Double.parseDouble(st.nextToken()); } static void p(String p) { System.out.print(p); } static void p(int p) { System.out.print(p); } static void p(double p) { System.out.print(p); } static void p(long p) { System.out.print(p); } static void p(char p) { System.out.print(p); } static void p(boolean p) { System.out.print(p); } static void pl(String pl) { System.out.println(pl); } static void pl(int pl) { System.out.println(pl); } static void pl(char pl) { System.out.println(pl); } static void pl(double pl) { System.out.println(pl); } static void pl(long pl) { System.out.println(pl); } static void pl(boolean pl) { System.out.println(pl); } static void pl() { System.out.println(); } static int[] ari(int n)throws IOException { int ar[]=new int[n]; if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++) ar[x]=Integer.parseInt(st.nextToken()); return ar; } static int[][] ari(int n,int m)throws IOException { int ar[][]=new int[n][m]; for(int x=0;x<n;x++) { st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++) ar[x][y]=Integer.parseInt(st.nextToken()); } return ar; } static long[] arl(int n)throws IOException { long ar[]=new long[n]; if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++) ar[x]=Long.parseLong(st.nextToken()); return ar; } static long[][] arl(int n,int m)throws IOException { long ar[][]=new long[n][m]; for(int x=0;x<n;x++) { st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++) ar[x][y]=Long.parseLong(st.nextToken()); } return ar; } static String[] ars(int n)throws IOException { String ar[]=new String[n]; if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++) ar[x]=st.nextToken(); return ar; } static String[][] ars(int n,int m)throws IOException { String ar[][]=new String[n][m]; for(int x=0;x<n;x++) { st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++) ar[x][y]=st.nextToken(); } return ar; } static double[] ard(int n)throws IOException { double ar[]=new double[n]; if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++) ar[x]=Double.parseDouble(st.nextToken()); return ar; } static double[][] ard(int n,int m)throws IOException { double ar[][]=new double[n][m]; for(int x=0;x<n;x++) { st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++) ar[x][y]=Double.parseDouble(st.nextToken()); } return ar; } static char[] arc(int n)throws IOException { char ar[]=new char[n]; if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); for(int x=0;x<n;x++) ar[x]=st.nextToken().charAt(0); return ar; } static char[][] arc(int n,int m)throws IOException { char ar[][]=new char[n][m]; for(int x=0;x<n;x++) { st=new StringTokenizer(br.readLine()); for(int y=0;y<m;y++) ar[x][y]=st.nextToken().charAt(0); } return ar; } static void par(int ar[]) { for(int a:ar) System.out.print(a+" "); System.out.println(); } static void par(int ar[][]) { for(int a[]:ar) { for(int aa:a) System.out.print(aa+" "); System.out.println(); } } static void par(long ar[]) { for(long a:ar) System.out.print(a+" "); System.out.println(); } static void par(long ar[][]) { for(long a[]:ar) { for(long aa:a) System.out.print(aa+" "); System.out.println(); } } static void par(String ar[]) { for(String a:ar) System.out.print(a+" "); System.out.println(); } static void par(String ar[][]) { for(String a[]:ar) { for(String aa:a) System.out.print(aa+" "); System.out.println(); } } static void par(double ar[]) { for(double a:ar) System.out.print(a+" "); System.out.println(); } static void par(double ar[][]) { for(double a[]:ar) { for(double aa:a) System.out.print(aa+" "); System.out.println(); } } static void par(char ar[]) { for(char a:ar) System.out.print(a+" "); System.out.println(); } static void par(char ar[][]) { for(char a[]:ar) { for(char aa:a) System.out.print(aa+" "); System.out.println(); } } static public void main(String[] args)throws Exception{ st=new StringTokenizer(br.readLine()); int n=i(); int ar[]=ari(n); Arrays.sort(ar); int c=0; for(int x=0;x<n;x++) { if(ar[x]!=-1) { c++; for(int y=x+1;y<n;y++) { if(ar[y]!=-1) { if(ar[y]%ar[x]==0) { ar[y]=-1; } } } ar[x]=-1; } } pl(c); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class A { public void realMain() throws Exception { BufferedReader fin = new BufferedReader(new InputStreamReader(System.in), 1000000); String in = fin.readLine(); String[] ar = in.split(" "); int n = Integer.parseInt(ar[0]); int[] a = new int[n]; for(int i = 0; i < n; i++) { int ret = 0; boolean dig = false; for (int ch = 0; (ch = fin.read()) != -1; ) { if (ch >= '0' && ch <= '9') { dig = true; ret = ret * 10 + ch - '0'; } else if (dig) break; } a[i] = ret; } int ret = 0; Arrays.sort(a); boolean[] colored = new boolean[n]; for(int i = 0; i < n; i++) { if(!colored[i]) { ret++; for(int j = i; j < n; j++) { if(a[j] % a[i] == 0) { colored[j] = true; } } } } System.out.println(ret); } public static void main(String[] args) throws Exception { A a = new A(); a.realMain(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Iterator; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.NoSuchElementException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Egor Kulikov ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] a = in.readIntArray(n); ArrayUtils.sort(a); boolean[] done = new boolean[n]; int answer = 0; for (int i = 0; i < n; i++) { if (done[i]) { continue; } answer++; for (int j = i; j < n; j++) { if (a[j] % a[i] == 0) { done[j] = true; } } } out.printLine(answer); } } static class Sorter { private static final int INSERTION_THRESHOLD = 16; private Sorter() { } public static void sort(IntList list, IntComparator comparator) { quickSort(list, 0, list.size() - 1, (Integer.bitCount(Integer.highestOneBit(list.size()) - 1) * 5) >> 1, comparator); } private static void quickSort(IntList list, int from, int to, int remaining, IntComparator comparator) { if (to - from < INSERTION_THRESHOLD) { insertionSort(list, from, to, comparator); return; } if (remaining == 0) { heapSort(list, from, to, comparator); return; } remaining--; int pivotIndex = (from + to) >> 1; int pivot = list.get(pivotIndex); list.swap(pivotIndex, to); int storeIndex = from; int equalIndex = to; for (int i = from; i < equalIndex; i++) { int value = comparator.compare(list.get(i), pivot); if (value < 0) { list.swap(storeIndex++, i); } else if (value == 0) { list.swap(--equalIndex, i--); } } quickSort(list, from, storeIndex - 1, remaining, comparator); for (int i = equalIndex; i <= to; i++) { list.swap(storeIndex++, i); } quickSort(list, storeIndex, to, remaining, comparator); } private static void heapSort(IntList list, int from, int to, IntComparator comparator) { for (int i = (to + from - 1) >> 1; i >= from; i--) { siftDown(list, i, to, comparator, from); } for (int i = to; i > from; i--) { list.swap(from, i); siftDown(list, from, i - 1, comparator, from); } } private static void siftDown(IntList list, int start, int end, IntComparator comparator, int delta) { int value = list.get(start); while (true) { int child = ((start - delta) << 1) + 1 + delta; if (child > end) { return; } int childValue = list.get(child); if (child + 1 <= end) { int otherValue = list.get(child + 1); if (comparator.compare(otherValue, childValue) > 0) { child++; childValue = otherValue; } } if (comparator.compare(value, childValue) >= 0) { return; } list.swap(start, child); start = child; } } private static void insertionSort(IntList list, int from, int to, IntComparator comparator) { for (int i = from + 1; i <= to; i++) { int value = list.get(i); for (int j = i - 1; j >= from; j--) { if (comparator.compare(list.get(j), value) <= 0) { break; } list.swap(j, j + 1); } } } } static interface IntStream extends Iterable<Integer>, Comparable<IntStream> { public IntIterator intIterator(); default public Iterator<Integer> iterator() { return new Iterator<Integer>() { private IntIterator it = intIterator(); public boolean hasNext() { return it.isValid(); } public Integer next() { int result = it.value(); it.advance(); return result; } }; } default public int compareTo(IntStream c) { IntIterator it = intIterator(); IntIterator jt = c.intIterator(); while (it.isValid() && jt.isValid()) { int i = it.value(); int j = jt.value(); if (i < j) { return -1; } else if (i > j) { return 1; } it.advance(); jt.advance(); } if (it.isValid()) { return 1; } if (jt.isValid()) { return -1; } return 0; } } static interface IntComparator { public static final IntComparator DEFAULT = (first, second) -> { if (first < second) { return -1; } if (first > second) { return 1; } return 0; }; public int compare(int first, int second); } static class IntArray extends IntAbstractStream implements IntList { private int[] data; public IntArray(int[] arr) { data = arr; } public int size() { return data.length; } public int get(int at) { return data[at]; } public void addAt(int index, int value) { throw new UnsupportedOperationException(); } public void removeAt(int index) { throw new UnsupportedOperationException(); } public void set(int index, int value) { data[index] = value; } } static interface IntIterator { public int value() throws NoSuchElementException; public boolean advance(); public boolean isValid(); } static abstract class IntAbstractStream implements IntStream { public String toString() { StringBuilder builder = new StringBuilder(); boolean first = true; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { if (first) { first = false; } else { builder.append(' '); } builder.append(it.value()); } return builder.toString(); } public boolean equals(Object o) { if (!(o instanceof IntStream)) { return false; } IntStream c = (IntStream) o; IntIterator it = intIterator(); IntIterator jt = c.intIterator(); while (it.isValid() && jt.isValid()) { if (it.value() != jt.value()) { return false; } it.advance(); jt.advance(); } return !it.isValid() && !jt.isValid(); } public int hashCode() { int result = 0; for (IntIterator it = intIterator(); it.isValid(); it.advance()) { result *= 31; result += it.value(); } return result; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static interface IntCollection extends IntStream { public int size(); default public void add(int value) { throw new UnsupportedOperationException(); } default public IntCollection addAll(IntStream values) { for (IntIterator it = values.intIterator(); it.isValid(); it.advance()) { add(it.value()); } return this; } } static interface IntReversableCollection extends IntCollection { } static class IntArrayList extends IntAbstractStream implements IntList { private int size; private int[] data; public IntArrayList() { this(3); } public IntArrayList(int capacity) { data = new int[capacity]; } public IntArrayList(IntCollection c) { this(c.size()); addAll(c); } public IntArrayList(IntStream c) { this(); if (c instanceof IntCollection) { ensureCapacity(((IntCollection) c).size()); } addAll(c); } public IntArrayList(IntArrayList c) { size = c.size(); data = c.data.clone(); } public IntArrayList(int[] arr) { size = arr.length; data = arr.clone(); } public int size() { return size; } public int get(int at) { if (at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size); } return data[at]; } private void ensureCapacity(int capacity) { if (data.length >= capacity) { return; } capacity = Math.max(2 * data.length, capacity); data = Arrays.copyOf(data, capacity); } public void addAt(int index, int value) { ensureCapacity(size + 1); if (index > size || index < 0) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } if (index != size) { System.arraycopy(data, index, data, index + 1, size - index); } data[index] = value; size++; } public void removeAt(int index) { if (index >= size || index < 0) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } if (index != size - 1) { System.arraycopy(data, index + 1, data, index, size - index - 1); } size--; } public void set(int index, int value) { if (index >= size) { throw new IndexOutOfBoundsException("at = " + index + ", size = " + size); } data[index] = value; } } static class ArrayUtils { public static int[] sort(int[] array) { return sort(array, IntComparator.DEFAULT); } public static int[] sort(int[] array, IntComparator comparator) { return sort(array, 0, array.length, comparator); } public static int[] sort(int[] array, int from, int to, IntComparator comparator) { if (from == 0 && to == array.length) { new IntArray(array).sort(comparator); } else { new IntArray(array).subList(from, to).sort(comparator); } return array; } } static interface IntList extends IntReversableCollection { public abstract int get(int index); public abstract void set(int index, int value); public abstract void addAt(int index, int value); public abstract void removeAt(int index); default public void swap(int first, int second) { if (first == second) { return; } int temp = get(first); set(first, get(second)); set(second, temp); } default public IntIterator intIterator() { return new IntIterator() { private int at; private boolean removed; public int value() { if (removed) { throw new IllegalStateException(); } return get(at); } public boolean advance() { at++; removed = false; return isValid(); } public boolean isValid() { return !removed && at < size(); } public void remove() { removeAt(at); at--; removed = true; } }; } default public void add(int value) { addAt(size(), value); } default public IntList sort(IntComparator comparator) { Sorter.sort(this, comparator); return this; } default public IntList subList(final int from, final int to) { return new IntList() { private final int shift; private final int size; { if (from < 0 || from > to || to > IntList.this.size()) { throw new IndexOutOfBoundsException("from = " + from + ", to = " + to + ", size = " + size()); } shift = from; size = to - from; } public int size() { return size; } public int get(int at) { if (at < 0 || at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size()); } return IntList.this.get(at + shift); } public void addAt(int index, int value) { throw new UnsupportedOperationException(); } public void removeAt(int index) { throw new UnsupportedOperationException(); } public void set(int at, int value) { if (at < 0 || at >= size) { throw new IndexOutOfBoundsException("at = " + at + ", size = " + size()); } IntList.this.set(at + shift, value); } public IntList compute() { return new IntArrayList(this); } }; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class PaintTheNumers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int nums = sc.nextInt(); HashSet<Integer> elements = new HashSet<Integer>(); for (int i = 0; i < nums; i++) { elements.add(sc.nextInt()); } ArrayList<Integer> sortedElements = new ArrayList<Integer>(elements); Collections.sort(sortedElements); ArrayList<Integer> lcms = new ArrayList<Integer>(); outer: for (int i = 0; i < sortedElements.size(); i++) { int ele = sortedElements.get(i); for (int j = 0; j < lcms.size(); j++) { if (ele % lcms.get(j) == 0) { continue outer; } } lcms.add(ele); } System.out.println(lcms.size()); sc.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.math.BigInteger; public class template { public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); Integer[] arr = new Integer[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } Arrays.sort(arr); int ct = 0; boolean[] ar = new boolean[n]; for(int i=0;i<n;i++) { if(!ar[i]) { ar[i]=true; ct++; int x = arr[i]; for(int j=0;j<n;j++) { if(arr[j]%x==0) { ar[j]=true; } } } } pw.println(ct); pw.close(); } } @SuppressWarnings("all") class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(BufferedReader d) { br=d; } public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Problem_A { public static void main(String[] args) { MyScanner scan = new MyScanner(); int n = scan.nextInt(); int[] elements = new int[n]; for (int i = 0; i < n; i++) elements[i] = scan.nextInt(); int x = 0; Arrays.sort(elements); while(n > 0) { x++; int[] temp = new int[n]; int j = 0; int size = n; int min = elements[0]; n--; for (int i = 1; i < size; i++) { if (elements[i]%min == 0) { n--; } else { temp[j++] = elements[i]; } } elements = temp; } out.println(x); out.close(); } public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class MyScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; 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; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.util.StringTokenizer; import java.io.Writer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Asgar Javadov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int[] a = in.readIntArray(n); ArrayUtils.radixSort(a); int answer = 0; boolean[] used = new boolean[a.length]; for (int i = 0; i < a.length; ++i) { if (used[i]) continue; used[i] = true; answer++; for (int j = i + 1; j < a.length; ++j) if (a[j] % a[i] == 0) used[j] = true; } out.println(answer); } } static class ArrayUtils { public static void radixSort(int[] array) { int[] ordered = new int[array.length]; { int[] freq = new int[0xFFFF + 2]; for (int i = 0; i < array.length; ++i) freq[(array[i] & 0xFFFF) + 1]++; for (int i = 1; i < freq.length; ++i) freq[i] += freq[i - 1]; for (int i = 0; i < array.length; ++i) ordered[freq[array[i] & 0xFFFF]++] = array[i]; for (int i = 0; i < array.length; ++i) array[i] = ordered[i]; } { int[] freq = new int[0xFFFF + 2]; for (int i = 0; i < array.length; ++i) freq[(array[i] >>> 16) + 1]++; for (int i = 1; i < freq.length; ++i) freq[i] += freq[i - 1]; for (int i = 0; i < array.length; ++i) ordered[freq[array[i] >>> 16]++] = array[i]; int indexOfFirstNegative = freq[0x7FFF]; int index = 0; for (int i = indexOfFirstNegative; i < ordered.length; ++i, ++index) array[index] = ordered[i]; for (int i = 0; i < indexOfFirstNegative; ++i, ++index) array[index] = ordered[i]; } } } static class OutputWriter extends PrintWriter { public OutputWriter(OutputStream outputStream) { super(outputStream); } public OutputWriter(Writer writer) { super(writer); } public OutputWriter(String filename) throws FileNotFoundException { super(filename); } public void close() { super.close(); } } static class InputReader extends BufferedReader { StringTokenizer tokenizer; public InputReader(InputStream inputStream) { super(new InputStreamReader(inputStream), 32768); } public InputReader(String filename) { super(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename))); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(readLine()); } catch (IOException e) { throw new RuntimeException(); } } return tokenizer.nextToken(); } public Integer nextInt() { return Integer.valueOf(next()); } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = nextInt(); return array; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.io.FileNotFoundException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); int[] a = in.nextIntArray(n); Arrays.sort(a); int count = 0; boolean[] used = new boolean[n]; for (int i = 0; i < n; i++) { if (!used[i]) { count++; for (int j = i; j < n; j++) { if (a[j] % a[i] == 0) { used[j] = true; } } } } out.println(count); } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public FastScanner(String fileName) { try { br = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (st == null || !st.hasMoreElements()) { String line = null; try { line = br.readLine(); } catch (IOException e) { } st = new StringTokenizer(line); } return st.nextToken(); } public int[] nextIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; //import javafx.util.*; import java.math.*; //import java.lang.*; public class Main { // static int n;static ArrayList<Integer> arr[]; // int ans[]; public static void main(String[] args) throws IOException { // Scanner sc=new Scanner(System.in); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); br = new BufferedReader(new InputStreamReader(System.in)); int n=nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=nextInt(); } Arrays.sort(arr); int c=0; for(int i=0;i<n;i++){ if(arr[i]!=0){ int a=arr[i]; c++; for(int j=i;j<n;j++){ // System.out.println(arr[i]+" "+arr[j]); if(arr[j]%a==0){ arr[j]=0; } } } } pw.println(c); pw.close(); } static long maxSubArraySum(long a[],int size) { long max_so_far = 0, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_ending_here < 0) max_ending_here = 0; /* Do not compare for all elements. Compare only when max_ending_here > 0 */ else if (max_so_far < max_ending_here) max_so_far = max_ending_here; } return max_so_far; } public static BufferedReader br; public static StringTokenizer st; public static String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return st.nextToken(); } public static Integer nextInt() { return Integer.parseInt(next()); } public static Long nextLong() { return Long.parseLong(next()); } public static Double nextDouble() { return Double.parseDouble(next()); } static class Pair{ int x;int y;int z; Pair(int x,int y,int z){ this.x=x; this.y=y; this.z=z; // this.i=i; } } static class sorting implements Comparator<Pair>{ public int compare(Pair a,Pair b){ //return (a.y)-(b.y); return (a.x-b.x); } } public static int[] na(int n)throws IOException{ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = nextInt(); return a; } static class query implements Comparable<query>{ int l,r,idx,block; static int len; query(int l,int r,int i){ this.l=l; this.r=r; this.idx=i; this.block=l/len; } public int compareTo(query a){ return block==a.block?r-a.r:block-a.block; } } static boolean isPrime(int 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 long modInverse(long a, long m) { long g = gcd(a, m); if (g != 1) return -1; else{ // If a and m are relatively prime, then modulo inverse // is a^(m-2) mode m return (power(a, m - 2, m)); } } // To compute x^y under modulo m static long power(long x, long y, long m){ if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } static long fast_pow(long base,long n,long M){ if(n==0) return 1; if(n==1) return base; 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,int M){ return fast_pow(n,M-2,M); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class A { public static void main(String[] args) throws IOException { /**/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); /*/ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/a.in")))); /**/ int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); Arrays.sort(a); int ans = 0; boolean[] v = new boolean[n]; for (int i = 0; i < n; i++) { if (v[i]) continue; v[i] = true; ans++; for (int j = i; j < n; j++) { if (a[j]%a[i]==0) v[j] = true; } } System.out.println(ans); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr); int cnt = 0; int pos = 0; while (true) { while (pos < n && arr[pos] == -1) pos++; if (pos == n) break; int min = arr[pos]; arr[pos] = -1; cnt++; for (int i = pos + 1; i < n; i++) { if (arr[i] % min == 0) { arr[i] = -1; } } } System.out.println(cnt); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class paintTheNumbers { public static void main (String [] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int [] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = scanner.nextInt(); } System.out.print(paint(arr)); } public static int paint(int [] arr){ Arrays.sort(arr); HashSet<Integer> set = new HashSet<>(); int num = arr[0]; set.add(num); for(int i = 1; i < arr.length; i++){ if(!divBySet(set, arr[i])){ set.add(arr[i]); } } return set.size(); } /** * * @param set * @param a * @return */ public static boolean divBySet(HashSet<Integer> set, int a){ for(int s: set){ if(a % s == 0){ return true; } } return false; } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public final class paint_and_numers { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = in.nextInt(); Arrays.sort(a); int count = 0; boolean[] c = new boolean[n]; for(int i=0;i<n;i++) { if(c[i]==false) { c[i]=true; count++; for(int j=i+1;j<n;j++) { if(a[j]%a[i]==0) { c[j] = true; } } } } System.out.println(count); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
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.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author beginner1010 */ 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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { TreeSet<Integer> set = new TreeSet<>(); int n = in.nextInt(); for (int i = 0; i < n; i++) { int x = in.nextInt(); set.add(x); } int ans = 0; while (!set.isEmpty()) { ans++; int minimal = set.first(); int cur = minimal; while (cur <= 100) { set.remove(cur); cur += minimal; } } out.println(ans); } } static class InputReader { private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputStream stream; public InputReader(InputStream stream) { this.stream = stream; } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private 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 (isWhitespace(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 (!isWhitespace(c)); return res * sgn; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; import java.util.Map.*; import java.math.*; //import java.lang.*; public class q1 { static int MOD=1000000007; 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[1000000]; // 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 boolean isPrime(int 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 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } //Functions public static void main(String[] args) throws IOException { Scanner sc=new Scanner(System.in); //int T=sc.nextInt(); int T=1; while(T-- > 0){ int N=sc.nextInt(); int a[]=new int[N]; int count=0; int ans=0; boolean flag=false; for(int i=0;i<N;++i){ a[i]=sc.nextInt(); } Arrays.sort(a); for(int i=0;i<N;++i){ if(a[i]==-1) continue; for(int j=i+1;j<N;++j){ if(a[j]%a[i]==0 && a[j]!=-1){ a[j]=-1;; } } } //int i=0; for(int i=0;i<N;++i){ if(a[i]!= -1) count++; } System.out.println(count); } // End of test cases loop }//end of main function } //end of class
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int N = in.nextInt(); Integer a[] = in.nextIntArray(N); Arrays.sort(a); int colors = 0; for (int i = 0; i < N; i++) { if (a[i] == -1) continue; colors++; for (int j = i + 1; j < N; j++) { if (a[j] % a[i] == 0) { a[j] = -1; } } } out.printLine(colors); } } static class InputReader { BufferedReader in; StringTokenizer tokenizer = null; public InputReader(InputStream inputStream) { in = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } public Integer[] nextIntArray(int size) { Integer array[] = new Integer[size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } } static class OutputWriter { 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(); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class A { public void run() throws Exception { FastScanner sc = new FastScanner(); int n = sc.nextInt(); int[] arr = new int[n]; int[] color = new int[n]; for (int i = 0; i<n; i++) { arr[i] =sc.nextInt(); } Arrays.sort(arr); int counter = 1; for (int i = 0; i<n; i++) { if (color[i]!= 0) continue; for (int j = i;j<n; j++) { if (color[j]!= 0) continue; else if (arr[j]%arr[i] == 0) color[j] = counter; } counter++; } // System.out.println(Arrays.toString(color)); int max = 0; for (int i = 0; i<n; i++) { max = Math.max(max, color[i]); } System.out.println(max); } 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 static void main (String[] args) throws Exception { new A().run(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=s.nextInt(); } Arrays.sort(arr); int[] visited=new int[n]; int ans=0; for(int i=0;i<n;i++) { if(visited[i]==0) { ans++; for(int j=i+1;j<n;j++) { if(arr[j]%arr[i]==0&&visited[j]==0) { visited[j]=1; } } } } System.out.println(ans); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class PaintColor { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String input[] = br.readLine().split(" "); int c = 0; Set<Integer> s = new HashSet<>(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(input[i]); } Arrays.sort(arr); for (int i = 0; i < n; i++) { if (!s.contains(arr[i])) { c++; for (int j = i; j < n; j++) { if (arr[j] % arr[i] == 0) { s.add(arr[j]); } } } } System.out.println(c); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] ar = new int[n]; for (int i = 0; i < n; i++) ar[i] = in.nextInt(); Arrays.sort(ar); boolean[] u = new boolean[n]; int ans = 0; for (int i = 0; i < n; i++) { if (!u[i]) { u[i] = true; ans++; for (int j = 0; j < n; j++) { if (!u[j] && ar[j] % ar[i] == 0) { u[j] = true; } } } } out.println(ans); } } 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()); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; public class Main{ public static void main (String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(), min[] = new int[n]; boolean used[] = new boolean[n]; HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { min[i] = scan.nextInt(); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (min[i] > min[j]) { if (min[i] % min[j] == 0) min[i] = min[j]; } else if (min[j] % min[i] == 0) min[j] = min[i]; } } for (int i = 0; i < n; i++) { set.add(min[i]); } System.out.print(set.size()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { static int inf = (int) 1e9 + 7; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); int n = nextInt(); int a[] = new int [n]; for(int i = 0;i < n;i++) a[i] = nextInt(); int ans = 0; boolean b[] = new boolean[n]; Arrays.sort(a); for(int i = 0;i < n;i++) { if (!b[i]) { for(int j = i;j < n;j++) { if (a[j] % a[i] == 0) b[j] = true; } ans++; } } pw.println(ans); pw.close(); } static BufferedReader br; static StringTokenizer st = new StringTokenizer(""); static PrintWriter pw; static String next() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.lang.*; import java.math.*; public class A { public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int n = Integer.parseInt(bf.readLine()); StringTokenizer st = new StringTokenizer(bf.readLine()); int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken()); int counter = 0; TreeSet<Integer> val = new TreeSet<Integer>(); for(int i : a) val.add(i); while(!val.isEmpty()) { int min = val.first(); Set<Integer> toRemove = new HashSet<Integer>(); for(int i : val) if(i % min == 0) toRemove.add(i); for(int i : toRemove) val.remove(i); counter++; } out.println(counter); // int n = Integer.parseInt(st.nextToken()); out.close(); System.exit(0); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class task1 { static void print(Object... a) { for (Object aa : a) { System.out.println(aa.toString()); } } static Map<Character, Integer> stringToCharsMap(String str) { Map<Character, Integer> charcount = new HashMap<Character, Integer>(); for (Character c : str.toCharArray()) { if (charcount.containsKey(c)) { charcount.put(c, charcount.get(c) + 1); } else { charcount.put(c, 1); } } return charcount; } static Set<Character> stringToCharsSet(String str) { Set<Character> chars = new HashSet<>(); for (Character c : str.toCharArray()) { chars.add(c); } return chars; } static int[] readArrayOfInt() { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int array[] = new int[a]; for (int i = 0; i < a; i++) { array[i] = sc.nextInt(); } return array; } static class Point { double x; double y; Point(double x, double y) { this.x = x; this.y = y; } double distance(Point p) { return Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y)); } double distance(Line l) { return Math.abs(l.a * this.x + l.b * this.y + l.c) / Math.sqrt(l.a * l.a + l.b * l.b); } } static class Line { double a; double b; double c; double EPS = 1e-6; Line(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } Line(Point f, Point s) { this.a = f.y - s.y; this.b = s.x - f.x; this.c = -this.a * f.x - this.b * f.y; } double distance(Point p) { return Math.abs(this.a * p.x + this.b * p.y + this.c) / Math.sqrt(this.a * this.a + this.b * this.b); } boolean isPointOnLine(Point p) { return p.x * this.a + p.y * this.b + this.c < EPS; } Point linesIntersection(Line a) { double x = -(this.c * a.b - a.c * this.b) / (this.a * a.b - a.a * this.b); double y = -(this.a * a.c - a.a * this.c) / (this.a * a.b - a.a * this.b); return new Point(x, y); } } static HashMap<Integer, Integer> getDels(int a) { HashMap<Integer, Integer> h = new HashMap<>(); int i = 2; while (a != 1) { if (a % i == 0) { if (h.containsKey(i)) { h.put(i, h.get(i) + 1); } else { h.put(i, 1); } a = a / i; } else { i++; } } return h; } static class Rect { Point fcorner; Point scorner; Rect(Point f, Point s) { fcorner = f; scorner = s; } double volume() { return Math.abs((fcorner.x - scorner.x) * (fcorner.y - scorner.y)); } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[] = new int[n]; int c[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } Arrays.sort(arr); int count = 1; c[0] = 1; for (int i = 1; i < arr.length; i++) { for (int j = 0; j < i; j++) { if (arr[i] % arr[j] == 0) { c[i] = c[j]; break; } } if (c[i] == 0) { c[i] = count + 1; count++; } } System.out.println(count); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; 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 Washoum */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; inputClass in = new inputClass(inputStream); PrintWriter out = new PrintWriter(outputStream); APaintTheNumbers solver = new APaintTheNumbers(); solver.solve(1, in, out); out.close(); } static class APaintTheNumbers { public void solve(int testNumber, inputClass sc, PrintWriter out) { int n = sc.nextInt(); Integer[] tab = new Integer[n]; for (int i = 0; i < n; i++) { tab[i] = sc.nextInt(); } Arrays.sort(tab); boolean[] done = new boolean[n]; int ans = 0; for (int i = 0; i < n; i++) { if (!done[i]) { ans++; done[i] = true; for (int j = i + 1; j < n; j++) { if (!done[j] && tab[j] % tab[i] == 0) { done[j] = true; } } } } out.println(ans); } } static class inputClass { BufferedReader br; StringTokenizer st; public inputClass(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class wef { public static class FastReader { BufferedReader br; StringTokenizer st; //it reads the data about the specified point and divide the data about it ,it is quite fast //than using direct public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception r) { r.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());//converts string to integer } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception r) { r.printStackTrace(); } return str; } } static ArrayList<String>list1=new ArrayList<String>(); static void combine(String instr, StringBuffer outstr, int index,int k) { if(outstr.length()==k) { list1.add(outstr.toString());return; } if(outstr.toString().length()==0) outstr.append(instr.charAt(index)); for (int i = 0; i < instr.length(); i++) { outstr.append(instr.charAt(i)); combine(instr, outstr, i + 1,k); outstr.deleteCharAt(outstr.length() - 1); } index++; } static ArrayList<ArrayList<Integer>>l=new ArrayList<>(); static void comb(int n,int k,int ind,ArrayList<Integer>list) { if(k==0) { l.add(new ArrayList<>(list)); return; } for(int i=ind;i<=n;i++) { list.add(i); comb(n,k-1,ind+1,list); list.remove(list.size()-1); } } static long sum(long n) { long sum=0; while(n!=0) { sum+=n%10; n/=10; } return sum; } static boolean check(HashMap<Integer,Integer>map) { for(int h:map.values()) if(h>1) return false; return true; } static class Pair implements Comparable<Pair>{ int x;int y; Pair(int x,int y){ this.x=x; this.y=y; // this.i=i; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return x-o.x; } } 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 long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); public static void main(String[] args) { // TODO Auto-generated method stub FastReader in=new FastReader(); HashMap<Integer,Integer>map=new HashMap<Integer,Integer>(); ArrayList<Integer>list=new ArrayList<Integer>(); TreeSet<Integer>set=new TreeSet<Integer>(); int n=in.nextInt(); for(int i=0;i<n;i++) set.add(in.nextInt()); int ans=0; while(!set.isEmpty()) { int f=set.first(); int s=f; while(!set.isEmpty()&&s<=set.last()) { if(set.contains(s)) set.remove(new Integer(s)); s+=f; } ans++; } out.println(ans); out.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.Scanner; public class first { static int max = 1000000000; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] tab = new int[n]; for(int i=0;i<n;i++) { tab[i] = sc.nextInt(); } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i!=j) if(tab[i]>=tab[j] && tab[i]%tab[j]==0) { tab[i] = max; } } } int res = 0; for(int i=0;i<n;i++) { if(tab[i]!=max) res++; } System.out.println(res); //System.out.println(4%-1); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Mainm { static int mod1 = (int) (1e9 + 7); static class Reader { final private int BUFFER_SIZE = 1 << 16; Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in))); 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 nextString() throws IOException { String str00 = scan.next(); return str00; } 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); } String next() throws IOException { int c; for (c = read(); c <= 32; c = read()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = read()) { sb.append((char) c); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } public int[] nextArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } } static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } static long power(long x, long y, long p) { int res = 1; // Initialize result return res; } static boolean primeCheck(long num0) { boolean b1 = true; if (num0 == 1) { b1 = false; } else { int num01 = (int) (Math.sqrt(num0)) + 1; me1: for (int i = 2; i < num01; i++) { if (num0 % i == 0) { b1 = false; break me1; } } } return b1; } public static int dev(long num1) { int count00=0; while (num1%2==0) { count00++; num1/=2; } HashMap<Long,Long> hashMap=new HashMap<>(); if(count00!=0) { hashMap.put(2L,(long)count00); } for (int i = 3; i <= (int)Math.sqrt(num1); i = i + 2) { // While i divides n, print i and divide n if(num1%i==0) { int count01 = 0; while (num1 % i == 0) { num1 /= i; count01++; } hashMap.put((long)i,(long)count01); } } if(num1>2) { hashMap.put((long)num1,1L); } long numOfDiv=1; for(long num00:hashMap.keySet()) { long cDiv0=hashMap.get(num00); numOfDiv*=(cDiv0+1); } return (int)(numOfDiv); } public static long solve(long[] arr1, long N) { int n=(int)N; HashMap<Long,Integer> hm=new HashMap<>(); for(int i=0;i<n;i++) { if(hm.containsKey(arr1[i])) { } else { hm.put(arr1[i],1); } } long count1=0; for(int i=0;i<n;i++) { long num1=arr1[i]*arr1[i]; if(hm.containsKey(num1)) { count1+=hm.get(num1); if(arr1[i]==1) { count1--; } } } return count1; } public static void main(String[] args) throws IOException { Reader r = new Reader(); //PrintWriter writer=new PrintWriter(System.out); //Scanner r = new Scanner(new BufferedReader(new InputStreamReader(System.in))); //Scanner r=new Scanner(System.in); OutputWriter770 out77 = new OutputWriter770(System.out); int num1=r.nextInt(); int[] arr1=r.nextArray(num1); Arrays.sort(arr1); int res1=0; for(int i=0;i<num1;i++) { if(arr1[i]!=-1) { res1++; int num2=arr1[i]; arr1[i]=-1; for(int j=i+1;j<num1;j++) { if(arr1[j]%num2==0) { arr1[j]=-1; } } } } out77.print(res1+""); r.close(); out77.close(); } } class OutputWriter770 { BufferedWriter writer; public OutputWriter770(OutputStream stream) { writer = new BufferedWriter(new OutputStreamWriter(stream)); } public void print(int i) throws IOException { writer.write(i + ""); } public void println(int i) throws IOException { writer.write(i + "\n"); } public void print(String s) throws IOException { writer.write(s + ""); } public void println(String s) throws IOException { writer.write(s + "\n"); } public void print(char[] c) throws IOException { writer.write(c); } public void close() throws IOException { writer.flush(); writer.close(); } } class node { int source,dest; long difDist; node(int source,int dest,long tDst, long hDst) { this.source=source; this.dest=dest; this.difDist=hDst-tDst; } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; import static java.lang.Math.*; public class cfs584A { static long LINF = Long.MAX_VALUE / 4; static long IING = Integer.MAX_VALUE / 4; public static void main(String[] args) { FastScanner sc = new FastScanner(); StringBuilder sb = new StringBuilder(); int N = sc.nextInt(); int[] nums = sc.readIntArray(N); ArrayList<Integer> num = new ArrayList<>(); for (int i = 0; i < N; i++) { num.add(nums[i]); } int count = 0; while (!num.isEmpty()) { count++; int size = num.size(); int min = 200; for (int j = size-1; j >=0; j--) { if (num.get(j) < min) { min = num.get(j); } } for (int j = size-1; j >=0; j--) { int div = num.get(j) / min; if ((div * min) == num.get(j)) { num.remove(j); } } } sb.append(count); System.out.print(sb); } static void Assert(boolean b) { if (!b) throw new Error("Assertion Failed"); } static class Pair implements Comparable<Pair> { int x, y; Pair(int _x, int _y) { x = _x; y = _y; } public int compareTo(Pair o) { int c1 = Integer.compare(x, o.x); return c1 != 0 ? c1 : Integer.compare(y, o.y); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(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 readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] a = new int[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextInt(); } return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int idx = 0; idx < n; idx++) { a[idx] = nextLong(); } return a; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class Codeforce { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i=0; i<n; i++) { arr[i] = sc.nextInt(); } int color = 0; Arrays.sort(arr); for(int i=0; i<n; i++) { if(arr[i]!=0) { int col = arr[i]; color++; for(int j=i; j<n; j++) { if(arr[j]%col==0) arr[j]=0; } } } System.out.println(color); sc.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jenish */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); APaintTheNumbers solver = new APaintTheNumbers(); solver.solve(1, in, out); out.close(); } static class APaintTheNumbers { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int arr[] = new int[n]; in.scanInt(arr); CodeX.sort(arr); int ans = 0; boolean vissited[] = new boolean[n]; for (int i = 0; i < n; i++) { if (!vissited[i]) { ans++; for (int j = 0; j < n; j++) { if (arr[j] % arr[i] == 0) { vissited[j] = true; } } } } out.println(ans); } } static class CodeX { public static void sort(int arr[]) { merge_sort(arr, 0, arr.length - 1); } private static void merge_sort(int A[], int start, int end) { if (start < end) { int mid = (start + end) / 2; merge_sort(A, start, mid); merge_sort(A, mid + 1, end); merge(A, start, mid, end); } } private static void merge(int A[], int start, int mid, int end) { int p = start, q = mid + 1; int Arr[] = new int[end - start + 1]; int k = 0; for (int i = start; i <= end; i++) { if (p > mid) Arr[k++] = A[q++]; else if (q > end) Arr[k++] = A[p++]; else if (A[p] < A[q]) Arr[k++] = A[p++]; else Arr[k++] = A[q++]; } for (int i = 0; i < k; i++) { A[start++] = Arr[i]; } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int INDEX; private BufferedInputStream in; private int TOTAL; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (INDEX >= TOTAL) { INDEX = 0; try { TOTAL = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (TOTAL <= 0) return -1; } return buf[INDEX++]; } public int scanInt() { int I = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { I *= 10; I += n - '0'; n = scan(); } } return neg * I; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } public void scanInt(int[] A) { for (int i = 0; i < A.length; i++) A[i] = scanInt(); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class oK{ void pre() throws Exception{} void solve(int TC) throws Exception{ int n=ni(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=ni(); } Arrays.sort(a); int b[]=new int[101]; int flag=0; int count=0; for(int i=0;i<n;i++) { flag=0; if(b[a[i]]==0) { count++; } for(int j=i;j<n;j++) { if(b[a[j]]==0&&a[j]%a[i]==0) { b[a[j]]=1; } //if(flag==1)count++; } } pn(count); } // void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} static boolean multipleTC = false, memory = false; FastReader in;PrintWriter out; void run() throws Exception{ in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC)?ni():1; pre();for(int t = 1; t<= T; t++)solve(t); out.flush(); out.close(); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new oK().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new oK().run(); } void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str = ""; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.awt.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.io.*; public class Main3 { static PrintWriter pr; static Scanner scan; static BufferedReader br; static StringTokenizer st; public static void main(String args[]) throws Exception { pr = new PrintWriter(System.out); scan = new Scanner(System.in); br = new BufferedReader(new InputStreamReader(System.in)); int n = inputInt(); //char[] c = br.readLine().toCharArray(); int[] a = new int[n]; int[] b = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0;i<n;i++){ a[i]=Integer.parseInt(st.nextToken()); //b[i]=Integer.parseInt(st.nextToken()); } Arrays.sort(a); int ans=0; for(int i=0;i<n;i++){ if(b[i]!=1){ ans++; for(int j=i;j<n;j++){ if(a[j]%a[i]==0){ b[j]=1; } } } } System.out.println(ans); } public static int inputInt() throws IOException{ return Integer.parseInt(br.readLine()); } public static long inputLong() throws IOException{ return Long.parseLong(br.readLine()); } public static String inputString() throws IOException{ return br.readLine(); } public static int[] intArray(int n) throws IOException{ int a[] = new int[n]; st = new StringTokenizer(br.readLine()); for(int i=0;i<n;i++){ a[i] = Integer.parseInt(st.nextToken()); } return a; } public static long[] longArray(int n) throws IOException{ long a[] = new long[n]; st = new StringTokenizer(br.readLine()); for(int i=0;i<n;i++){ a[i] = Long.parseLong(st.nextToken()); } return a; } public static String[] stringArray(int n) throws IOException{ String a[] = new String[n]; st = new StringTokenizer(br.readLine()); for(int i=0;i<n;i++){ a[i] = st.nextToken(); } return a; } public static long gcd(long a,long b){ if(b==0){ return a; } else{ return gcd(b,a%b); } } public long max(long a,long b,long c){ return Math.max(a,Math.max(b,c)); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.awt.*; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int arr[]=new int[n]; for (int i=0;i<n;i++){ arr[i]=sc.nextInt(); } boolean vis[]=new boolean[n]; int c=0; for (int i=0;i<n;i++){ int min=200; for (int j=0;j<n;j++){ if (!vis[j] && min>arr[j]){ min=arr[j]; } } for (int j=0;j<n;j++){ if (!vis[j]&&arr[j]%min==0){ vis[j]=true; // System.out.println(arr[j]); } } if (min!=200){ c++; // System.out.println(min+" k"); }else break; } System.out.println(c); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import static java.lang.Math.*; import static java.util.Arrays.* ; import java.math.BigInteger; import java.util.*; import java.io.*; public class D { int [][] adjList ; int dfs(int u , int p ) { int size = 1 ; for(int v : adjList[u]) if(v != p ) { int curr = dfs(v, u) ; size += curr ; } return size ; } void main() throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt() ; int [] a = new int [n] ; boolean [] vis = new boolean[n] ; int cnt = 0 ; for(int i = 0 ;i < n ; i++) a[i] = sc.nextInt() ; sort(a); for(int i = 0 ;i < n ; i ++) { if(!vis[i]) { for(int j= i ; j < n ; j++) if(a[j] % a[i] == 0) vis[j] = true ; cnt ++ ; } } out.println(cnt); out.flush(); out.close(); } class SegmentTree { int [] sTree ; int [] lazy ; int N ; SegmentTree(int n) { N = 1 << (32 - Integer.numberOfLeadingZeros(n - 1)) ; sTree = new int [N << 1] ; lazy= new int [N << 1] ; } void push(int node , int b , int e , int mid) { sTree[node << 1] += (mid - b + 1) * lazy[node] ; sTree[node << 1 | 1] += (e - mid) * lazy[node] ; lazy[node << 1] += lazy[node] ; lazy[node << 1 | 1] += lazy[node] ; lazy[node] = 0 ; } void updateRange(int node , int b , int e , int i , int j , int val) { if(i > e || j < b)return; if(i <= b && e <= j) { sTree[node] += (e - b + 1) * val ; lazy[node] += val ; return; } int mid = b + e >> 1 ; push(node , b , e , mid) ; updateRange(node << 1 , b , mid , i , j , val); updateRange(node << 1 | 1 , mid + 1 , e , i , j , val); sTree[node] = sTree[node << 1] + sTree[node << 1 | 1] ; } int query(int node , int b , int e , int i , int j) { if(i > e || j < b) return 0 ; if(i <= b && e <= j) return sTree[node] ; int mid = b + e >> 1 ; push(node , b , e , mid); return query(node << 1 , b , mid , i , j) + query(node << 1 | 1 , mid + 1 , e , i , j) ; } } class Compressor { TreeSet<Integer> set = new TreeSet<>() ; TreeMap<Integer ,Integer> map = new TreeMap<>() ; void add(int x) { set.add(x) ; } void fix() { for(int x : set) map.put(x , map.size() + 1) ; } int get(int x) { return map.get(x) ; } } class Scanner { BufferedReader br ; StringTokenizer st ; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)) ; } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()) ; return st.nextToken() ; } int nextInt() throws Exception { return Integer.parseInt(next()) ; } long nextLong() throws Exception { return Long.parseLong(next()) ; } double nextDouble() throws Exception { return Double.parseDouble(next()) ; } } public static void main (String [] args) throws Exception {(new D()).main();} }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class A { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tok; public void go() throws IOException { ntok(); int n = ipar(); ArrayList<Integer> list = new ArrayList<>(); ntok(); for (int i = 0; i < n; i++) { list.add(ipar()); } Collections.sort(list); HashSet<Integer> set = new HashSet<>(); for (int x : list) { boolean add = true; for (int y : set) { if (x % y == 0) { add = false; break; } } if (add) { set.add(x); } } out.println(set.size()); out.flush(); in.close(); } public void ntok() throws IOException { tok = new StringTokenizer(in.readLine()); } public int ipar() { return Integer.parseInt(tok.nextToken()); } public int[] iapar(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ipar(); } return arr; } public long lpar() { return Long.parseLong(tok.nextToken()); } public long[] lapar(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = lpar(); } return arr; } public double dpar() { return Double.parseDouble(tok.nextToken()); } public String spar() { return tok.nextToken(); } public static void main(String[] args) throws IOException { new A().go(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.lang.reflect.Array; import java.math.*; import java.util.*; public class icpc { public static void main(String[] args) throws IOException { Reader in = new Reader(); // BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = in.nextInt(); boolean[] A = new boolean[n]; int count = 0; int[] B = new int[n]; for (int i=0;i<n;i++) B[i] = in.nextInt(); Arrays.sort(B); for (int i=0;i<n;i++) { if (!A[i]) { int gcd = B[i]; for (int j=0;j<n;j++) { if(!A[j]) { gcd = gcd(B[j], gcd); if(gcd == B[i]) { A[j] = true; } else { gcd = B[i]; } } } count++; A[i] = true; } } System.out.println(count); } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } } class DSU { int[] parent; int[] size; //Pass number of total nodes as parameter to the constructor DSU(int n) { this.parent = new int[n]; this.size = new int[n]; Arrays.fill(parent, -1); } public void makeSet(int v) { parent[v] = v; size[v] = 1; } public int findSet(int v) { if (v == parent[v]) return v; return parent[v] = findSet(parent[v]); } public void unionSets(int a, int b) { a = findSet(a); b = findSet(b); if (a != b) { if (size[a] < size[b]) { int temp = a; a = b; b = temp; } parent[b] = a; size[a] += size[b]; } } } class FastFourierTransform { private void fft(double[] a, double[] b, boolean invert) { int count = a.length; for (int i = 1, j = 0; i < count; i++) { int bit = count >> 1; for (; j >= bit; bit >>= 1) j -= bit; j += bit; if (i < j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; temp = b[i]; b[i] = b[j]; b[j] = temp; } } for (int len = 2; len <= count; len <<= 1) { int halfLen = len >> 1; double angle = 2 * Math.PI / len; if (invert) angle = -angle; double wLenA = Math.cos(angle); double wLenB = Math.sin(angle); for (int i = 0; i < count; i += len) { double wA = 1; double wB = 0; for (int j = 0; j < halfLen; j++) { double uA = a[i + j]; double uB = b[i + j]; double vA = a[i + j + halfLen] * wA - b[i + j + halfLen] * wB; double vB = a[i + j + halfLen] * wB + b[i + j + halfLen] * wA; a[i + j] = uA + vA; b[i + j] = uB + vB; a[i + j + halfLen] = uA - vA; b[i + j + halfLen] = uB - vB; double nextWA = wA * wLenA - wB * wLenB; wB = wA * wLenB + wB * wLenA; wA = nextWA; } } } if (invert) { for (int i = 0; i < count; i++) { a[i] /= count; b[i] /= count; } } } public long[] multiply(long[] a, long[] b) { int resultSize = Integer.highestOneBit(Math.max(a.length, b.length) - 1) << 2; resultSize = Math.max(resultSize, 1); double[] aReal = new double[resultSize]; double[] aImaginary = new double[resultSize]; double[] bReal = new double[resultSize]; double[] bImaginary = new double[resultSize]; for (int i = 0; i < a.length; i++) aReal[i] = a[i]; for (int i = 0; i < b.length; i++) bReal[i] = b[i]; fft(aReal, aImaginary, false); fft(bReal, bImaginary, false); for (int i = 0; i < resultSize; i++) { double real = aReal[i] * bReal[i] - aImaginary[i] * bImaginary[i]; aImaginary[i] = aImaginary[i] * bReal[i] + bImaginary[i] * aReal[i]; aReal[i] = real; } fft(aReal, aImaginary, true); long[] result = new long[resultSize]; for (int i = 0; i < resultSize; i++) result[i] = Math.round(aReal[i]); return result; } } class NumberTheory { public boolean isPrime(long n) { if(n < 2) return false; for(long x = 2;x * x <= n;x++) { if(n % x == 0) return false; } return true; } public ArrayList<Long> primeFactorisation(long n) { ArrayList<Long> f = new ArrayList<>(); for(long x=2;x * x <= n;x++) { while(n % x == 0) { f.add(x); n /= x; } } if(n > 1) f.add(n); return f; } public int[] sieveOfEratosthenes(int n) { //Returns an array with the smallest prime factor for each number and primes marked as 0 int[] sieve = new int[n + 1]; for(int x=2;x * x <= n;x++) { if(sieve[x] != 0) continue; for(int u=x*x;u<=n;u+=x) { if(sieve[u] == 0) { sieve[u] = x; } } } return sieve; } public long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } public long phi(long n) { double result = n; for(long p=2;p*p<=n;p++) { if(n % p == 0) { while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (double)p)); } } if(n > 1) result *= (1.0 - (1.0 / (double)n)); return (long)result; } public Name extendedEuclid(long a, long b) { if(b == 0) return new Name(a, 1, 0); Name n1 = extendedEuclid(b, a % b); Name n2 = new Name(n1.d, n1.y, n1.x - (long)Math.floor((double)a / b) * n1.y); return n2; } public long modularExponentiation(long a, long b, long n) { long d = 1L; String bString = Long.toBinaryString(b); for(int i=0;i<bString.length();i++) { d = (d * d) % n; if(bString.charAt(i) == '1') d = (d * a) % n; } return d; } } class Name { long d; long x; long y; public Name(long d, long x, long y) { this.d = d; this.x = x; this.y = y; } } class SuffixArray { int ALPHABET_SZ = 256, N; int[] T, lcp, sa, sa2, rank, tmp, c; public SuffixArray(String str) { this(toIntArray(str)); } private static int[] toIntArray(String s) { int[] text = new int[s.length()]; for (int i = 0; i < s.length(); i++) text[i] = s.charAt(i); return text; } public SuffixArray(int[] text) { T = text; N = text.length; sa = new int[N]; sa2 = new int[N]; rank = new int[N]; c = new int[Math.max(ALPHABET_SZ, N)]; construct(); kasai(); } private void construct() { int i, p, r; for (i = 0; i < N; ++i) c[rank[i] = T[i]]++; for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1]; for (i = N - 1; i >= 0; --i) sa[--c[T[i]]] = i; for (p = 1; p < N; p <<= 1) { for (r = 0, i = N - p; i < N; ++i) sa2[r++] = i; for (i = 0; i < N; ++i) if (sa[i] >= p) sa2[r++] = sa[i] - p; Arrays.fill(c, 0, ALPHABET_SZ, 0); for (i = 0; i < N; ++i) c[rank[i]]++; for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1]; for (i = N - 1; i >= 0; --i) sa[--c[rank[sa2[i]]]] = sa2[i]; for (sa2[sa[0]] = r = 0, i = 1; i < N; ++i) { if (!(rank[sa[i - 1]] == rank[sa[i]] && sa[i - 1] + p < N && sa[i] + p < N && rank[sa[i - 1] + p] == rank[sa[i] + p])) r++; sa2[sa[i]] = r; } tmp = rank; rank = sa2; sa2 = tmp; if (r == N - 1) break; ALPHABET_SZ = r + 1; } } private void kasai() { lcp = new int[N]; int[] inv = new int[N]; for (int i = 0; i < N; i++) inv[sa[i]] = i; for (int i = 0, len = 0; i < N; i++) { if (inv[i] > 0) { int k = sa[inv[i] - 1]; while ((i + len < N) && (k + len < N) && T[i + len] == T[k + len]) len++; lcp[inv[i] - 1] = len; if (len > 0) len--; } } } } class ZAlgorithm { public int[] calculateZ(char input[]) { int Z[] = new int[input.length]; int left = 0; int right = 0; for(int k = 1; k < input.length; k++) { if(k > right) { left = right = k; while(right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } else { //we are operating inside box int k1 = k - left; //if value does not stretches till right bound then just copy it. if(Z[k1] < right - k + 1) { Z[k] = Z[k1]; } else { //otherwise try to see if there are more matches. left = k; while(right < input.length && input[right] == input[right - left]) { right++; } Z[k] = right - left; right--; } } } return Z; } public ArrayList<Integer> matchPattern(char text[], char pattern[]) { char newString[] = new char[text.length + pattern.length + 1]; int i = 0; for(char ch : pattern) { newString[i] = ch; i++; } newString[i] = '$'; i++; for(char ch : text) { newString[i] = ch; i++; } ArrayList<Integer> result = new ArrayList<>(); int Z[] = calculateZ(newString); for(i = 0; i < Z.length ; i++) { if(Z[i] == pattern.length) { result.add(i - pattern.length - 1); } } return result; } } class KMPAlgorithm { public int[] computeTemporalArray(char[] pattern) { int[] lps = new int[pattern.length]; int index = 0; for(int i=1;i<pattern.length;) { if(pattern[i] == pattern[index]) { lps[i] = index + 1; index++; i++; } else { if(index != 0) { index = lps[index - 1]; } else { lps[i] = 0; i++; } } } return lps; } public ArrayList<Integer> KMPMatcher(char[] text, char[] pattern) { int[] lps = computeTemporalArray(pattern); int j = 0; int i = 0; int n = text.length; int m = pattern.length; ArrayList<Integer> indices = new ArrayList<>(); while(i < n) { if(pattern[j] == text[i]) { i++; j++; } if(j == m) { indices.add(i - j); j = lps[j - 1]; } else if(i < n && pattern[j] != text[i]) { if(j != 0) j = lps[j - 1]; else i = i + 1; } } return indices; } } class Hashing { public long[] computePowers(long p, int n, long m) { long[] powers = new long[n]; powers[0] = 1; for(int i=1;i<n;i++) { powers[i] = (powers[i - 1] * p) % m; } return powers; } public long computeHash(String s) { long p = 31; long m = 1_000_000_009; long hashValue = 0L; long[] powers = computePowers(p, s.length(), m); for(int i=0;i<s.length();i++) { char ch = s.charAt(i); hashValue = (hashValue + (ch - 'a' + 1) * powers[i]) % m; } return hashValue; } } class BasicFunctions { public long min(long[] A) { long min = Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min = Math.min(min, A[i]); } return min; } public long max(long[] A) { long max = Long.MAX_VALUE; for(int i=0;i<A.length;i++) { max = Math.max(max, A[i]); } return max; } } class MergeSortInt { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] 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() 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); } } } class MergeSortLong { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] 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() 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); } } } class Node { String s; Node(String s) { this.s = s; } @Override public boolean equals(Object ob) { if(ob == null) return false; if(!(ob instanceof Node)) return false; if(ob == this) return true; Node obj = (Node)ob; if(this.s.equals(obj.s)) return true; return false; } @Override public int hashCode() { return (int)this.s.length(); } } 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(); } } class FenwickTree { public void update(long[] fenwickTree,long delta,int index) { index += 1; while(index < fenwickTree.length) { fenwickTree[index] += delta; index = index + (index & (-index)); } } public long prefixSum(long[] fenwickTree,int index) { long sum = 0L; index += 1; while(index > 0) { sum += fenwickTree[index]; index -= (index & (-index)); } return sum; } } class SegmentTree { public int nextPowerOfTwo(int num) { if(num == 0) return 1; if(num > 0 && (num & (num - 1)) == 0) return num; while((num &(num - 1)) > 0) { num = num & (num - 1); } return num << 1; } public int[] createSegmentTree(int[] input) { int np2 = nextPowerOfTwo(input.length); int[] segmentTree = new int[np2 * 2 - 1]; for(int i=0;i<segmentTree.length;i++) segmentTree[i] = Integer.MIN_VALUE; constructSegmentTree(segmentTree,input,0,input.length-1,0); return segmentTree; } private void constructSegmentTree(int[] segmentTree,int[] input,int low,int high,int pos) { if(low == high) { segmentTree[pos] = input[low]; return; } int mid = (low + high)/ 2; constructSegmentTree(segmentTree,input,low,mid,2*pos + 1); constructSegmentTree(segmentTree,input,mid+1,high,2*pos + 2); segmentTree[pos] = Math.max(segmentTree[2*pos + 1],segmentTree[2*pos + 2]); } public int rangeMinimumQuery(int []segmentTree,int qlow,int qhigh,int len) { return rangeMinimumQuery(segmentTree,0,len-1,qlow,qhigh,0); } private int rangeMinimumQuery(int segmentTree[],int low,int high,int qlow,int qhigh,int pos) { if(qlow <= low && qhigh >= high){ return segmentTree[pos]; } if(qlow > high || qhigh < low){ return Integer.MIN_VALUE; } int mid = (low+high)/2; return Math.max(rangeMinimumQuery(segmentTree, low, mid, qlow, qhigh, 2 * pos + 1), rangeMinimumQuery(segmentTree, mid + 1, high, qlow, qhigh, 2 * pos + 2)); } } class Trie { private class TrieNode { Map<Character, TrieNode> children; boolean endOfWord; public TrieNode() { children = new HashMap<>(); endOfWord = false; } } private final TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = current.children.get(ch); if (node == null) { node = new TrieNode(); current.children.put(ch, node); } current = node; } current.endOfWord = true; } public boolean search(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { char ch = word.charAt(i); TrieNode node = current.children.get(ch); if (node == null) { return false; } current = node; } return current.endOfWord; } public void delete(String word) { delete(root, word, 0); } private boolean delete(TrieNode current, String word, int index) { if (index == word.length()) { if (!current.endOfWord) { return false; } current.endOfWord = false; return current.children.size() == 0; } char ch = word.charAt(index); TrieNode node = current.children.get(ch); if (node == null) { return false; } boolean shouldDeleteCurrentNode = delete(node, word, index + 1); if (shouldDeleteCurrentNode) { current.children.remove(ch); return current.children.size() == 0; } return false; } } class SegmentTreeLazy { public int nextPowerOfTwo(int num) { if(num == 0) return 1; if(num > 0 && (num & (num - 1)) == 0) return num; while((num &(num - 1)) > 0) { num = num & (num - 1); } return num << 1; } public int[] createSegmentTree(int input[]) { int nextPowOfTwo = nextPowerOfTwo(input.length); int segmentTree[] = new int[nextPowOfTwo*2 -1]; for(int i=0; i < segmentTree.length; i++){ segmentTree[i] = Integer.MAX_VALUE; } constructMinSegmentTree(segmentTree, input, 0, input.length - 1, 0); return segmentTree; } private void constructMinSegmentTree(int segmentTree[], int input[], int low, int high,int pos) { if(low == high) { segmentTree[pos] = input[low]; return; } int mid = (low + high)/2; constructMinSegmentTree(segmentTree, input, low, mid, 2 * pos + 1); constructMinSegmentTree(segmentTree, input, mid + 1, high, 2 * pos + 2); segmentTree[pos] = Math.min(segmentTree[2*pos+1], segmentTree[2*pos+2]); } public void updateSegmentTreeRangeLazy(int input[], int segmentTree[], int lazy[], int startRange, int endRange, int delta) { updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, 0, input.length - 1, 0); } private void updateSegmentTreeRangeLazy(int segmentTree[], int lazy[], int startRange, int endRange, int delta, int low, int high, int pos) { if(low > high) { return; } if (lazy[pos] != 0) { segmentTree[pos] += lazy[pos]; if (low != high) { lazy[2 * pos + 1] += lazy[pos]; lazy[2 * pos + 2] += lazy[pos]; } lazy[pos] = 0; } if(startRange > high || endRange < low) { return; } if(startRange <= low && endRange >= high) { segmentTree[pos] += delta; if(low != high) { lazy[2*pos + 1] += delta; lazy[2*pos + 2] += delta; } return; } int mid = (low + high)/2; updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, low, mid, 2*pos+1); updateSegmentTreeRangeLazy(segmentTree, lazy, startRange, endRange, delta, mid+1, high, 2*pos+2); segmentTree[pos] = Math.min(segmentTree[2*pos + 1], segmentTree[2*pos + 2]); } public int rangeMinimumQueryLazy(int segmentTree[], int lazy[], int qlow, int qhigh, int len) { return rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, 0, len - 1, 0); } private int rangeMinimumQueryLazy(int segmentTree[], int lazy[], int qlow, int qhigh, int low, int high, int pos) { if(low > high) { return Integer.MAX_VALUE; } if (lazy[pos] != 0) { segmentTree[pos] += lazy[pos]; if (low != high) { lazy[2 * pos + 1] += lazy[pos]; lazy[2 * pos + 2] += lazy[pos]; } lazy[pos] = 0; } if(qlow > high || qhigh < low) { return Integer.MAX_VALUE; } if(qlow <= low && qhigh >= high) { return segmentTree[pos]; } int mid = (low+high)/2; return Math.min(rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, low, mid, 2 * pos + 1), rangeMinimumQueryLazy(segmentTree, lazy, qlow, qhigh, mid + 1, high, 2 * pos + 2)); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Hack{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i]=sc.nextInt(); Arrays.sort(arr); Set<Integer> set = new TreeSet<Integer>(); for(int i=0;i<n;i++){ boolean flag=false; for(Integer x:set){ if(arr[i]%x==0){ flag=true; break; } } if(!flag) set.add(arr[i]); } System.out.println(set.size()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; public class paint { static PriorityQueue<Integer> sequence; public static void main (String [] args) throws IOException { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out, true); int numSeq = Integer.parseInt(f.readLine()); sequence = new PriorityQueue<Integer>(); StringTokenizer st = new StringTokenizer(f.readLine()); for(int i = 0; i < numSeq; i++) { sequence.add(Integer.parseInt(st.nextToken())); } int numColors = 0; while(sequence.size() > 0) { numColors++; int smallest = sequence.poll(); PriorityQueue<Integer> temp = new PriorityQueue<Integer>(); for(int each: sequence) { if(each % smallest != 0) { temp.add(each); } } sequence = temp; } System.out.println(numColors); out.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.security.KeyPair; import java.util.*; public class Main { static long[][] c ; static long[] arr; static long n , m , k; static long dp[][][]; public static void main(String[] args) throws IOException { Reader.init(System.in); int n = Reader.nextInt(); int[] arr = new int[n]; int[] mark = new int[n]; for (int i = 0 ; i < n ; i++){ arr[i] = Reader.nextInt(); } Arrays.sort(arr); int[] v = new int[n]; int ans = 0; for (int i = 0 ; i < n ; i++){ if (v[i]==0){ for (int j = i ; j < n ; j++){ if (arr[j]%arr[i]==0){ v[j] = arr[i]; } } } } TreeSet<Integer> s = new TreeSet<>(); for (int i = 0 ; i < n ;i++){ s.add(v[i]); } System.out.println(s.size()); } public static void sortbyColumn_asc(int arr[][], int col) { // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); // End of function call sort(). } public static void sortbyColumn_dsc(int arr[][], int col) { // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return -1; else return 1; } }); // End of function call sort(). } static void swap(char[] arr , int i , int j){ char tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } static void swap(int[] arr , int i , int j){ int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } class Node implements Comparable<Node>{ int a , b; Node(int a , int b){ this.a = a; this.b = b; } public int compareTo(Node o) { if (this.a == o.a){ return this.b - o.b; } return this.a - o.a; } } class Edge implements Comparable<Edge>{ int x , y , w; public Edge(int x, int y, int w) { this.x = x; this.y = y; this.w = w; } @Override public int compareTo(Edge o) { return this.w - o.w; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } class MergeSort { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] 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() 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); } } /* A utility function to print array of size n */ static void printArray(int arr[]) { int n = arr.length; for (int i=0; i<n; ++i) System.out.print(arr[i] + " "); System.out.println(); } // Driver method }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class Paint { public static void main (String srgs[] ){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); TreeSet<Integer> ts=new TreeSet<>(); for(int i=0;i<n;++i){ ts.add(sc.nextInt()); } int x=0; int a[]=new int[ts.size()]; for(int y:ts){ a[x++]=y; } for(int i=0;i<ts.size()-1;++i){ for(int j=i+1;j<ts.size();++j){ if((a[i]!=-1)&&(a[j]!=-1)&&(a[j]%a[i]==0)){ a[j]=-1; } } } int c=0; for(int z:a){ if(z!=-1)++c; } System.out.print(c); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception{ BufferedReader jk = new BufferedReader(new InputStreamReader( System.in)) ; PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)) ; StringTokenizer ana = new StringTokenizer(jk.readLine()) ; int n = Integer.parseInt(ana.nextToken()) ; int t[]= new int[101] ; ArrayList<Integer> v = new ArrayList<>() ; ana = new StringTokenizer(jk.readLine()) ; for(int i=0 ; i<n ;i++) { int y = Integer.parseInt(ana.nextToken()) ; t[y]=1 ; v.add(y) ; } Collections.sort(v); int c= 0; for(int ele : v) { if(t[ele]==1) { for(int i=ele ; i<=100 ; i+=ele) { t[i]=2 ; } c++ ; } } out.println(c); out.flush(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class Main { static PrintWriter out; static InputReader ir; static void solve() { int n = ir.nextInt(); int[] a = ir.nextIntArray(n); Arrays.sort(a); boolean[] used = new boolean[n]; int ct = 0; for (int i = 0; i < n; i++) { if (used[i]) continue; for (int j = i + 1; j < n; j++) { if (a[j] % a[i] == 0) used[j] = true; } ct++; } out.println(ct); } public static void main(String[] args) { ir = new InputReader(System.in); out = new PrintWriter(System.out); solve(); out.flush(); } static class InputReader { private InputStream in; private byte[] buffer = new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) { this.in = in; this.curbuf = this.lenbuf = 0; } public boolean hasNextByte() { if (curbuf >= lenbuf) { curbuf = 0; try { lenbuf = in.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return false; } return true; } private int readByte() { if (hasNextByte()) return buffer[curbuf++]; else return -1; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private void skip() { while (hasNextByte() && isSpaceChar(buffer[curbuf])) curbuf++; } public boolean hasNext() { skip(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); int c = readByte(); while (isSpaceChar(c)) c = readByte(); boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = readByte(); } while (!isSpaceChar(c)); return (minus) ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[][] nextCharMap(int n, int m) { char[][] map = new char[n][m]; for (int i = 0; i < n; i++) map[i] = next().toCharArray(); return map; } } static void tr(Object... o) { out.println(Arrays.deepToString(o)); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Main { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); //Scanner sc = new Scanner(); Reader in = new Reader(); Main solver = new Main(); solver.solve(out, in); out.flush(); out.close(); } static int INF = (int)1e9; static int maxn = (int)2e5+5; static int mod= 998244353 ; static int n,m,k,t,q,d,cnt=2; void solve(PrintWriter out, Reader in) throws IOException{ n = in.nextInt(); Integer[] arr = new Integer[n]; for(int i=0;i<n;i++) arr[i] = in.nextInt(); boolean[] vis = new boolean[n]; Arrays.sort(arr); int cnt=0; for(int i=0;i<n;i++){ if(vis[i]) continue; cnt++; vis[i]=true; for(int j=i+1;j<n;j++){ if(!vis[j]){ if(arr[j]%arr[i]==0){ vis[j]=true; } } } } out.println(cnt); } //<> static class Reader { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public Reader() { this(System.in); } public Reader(InputStream is) { mIs = is; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.Arrays; import java.util.InputMismatchException; import java.util.*; import java.io.*; import java.math.*; public class Main6{ static class Pair { int x; int y; int z; public Pair(int x,int y,int z) { this.x= x; this.y= y; this.z=z; } @Override public int hashCode() { final int temp = 14; int ans = 1; ans =x*31+y*13; return ans; } // Equal objects must produce the same // hash code as long as they are equal @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null) { return false; } if (this.getClass() != o.getClass()) { return false; } Pair other = (Pair)o; if (this.x != other.x || this.y!=other.y) { return false; } return true; } } static class Pair1 { String x; int y; int z; } static class Compare { static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element /* Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.start>p2.start) { return 1; } else if(p1.start==p2.start) { return 0; } else { return -1; } } }); */ } } public static long pow(long a, long b) { long result=1; while(b>0) { if (b % 2 != 0) { result=(result*a)%998244353; b--; } a=(a*a)%998244353; b /= 2; } return result; } public static long fact(long num) { long value=1; int i=0; for(i=2;i<num;i++) { value=((value%mod)*i%mod)%mod; } return value; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } /* public static long lcm(long a,long b) { return a * (b / gcd(a, b)); } */ public static long sum(int h) { return (h*(h+1)/2); } public static void dfs(int parent,boolean[] visited,int[] dp) { ArrayList<Integer> arr=new ArrayList<Integer>(); arr=graph.get(parent); visited[parent]=true; for(int i=0;i<arr.size();i++) { int num=(int)arr.get(i); if(visited[num]==false) { dfs(num,visited,dp); } dp[parent]=Math.max(dp[num]+1,dp[parent]); } } // static int flag1=0; static int[] dis; static int mod=1000000007; static ArrayList<ArrayList<Integer>> graph; public static void bfs(int num,int size) { boolean[] visited=new boolean[size+1]; Queue<Integer> q=new LinkedList<>(); q.add(num); ans[num]=1; visited[num]=true; while(!q.isEmpty()) { int x=q.poll(); ArrayList<Integer> al=graph.get(x); for(int i=0;i<al.size();i++) { int y=al.get(i); if(visited[y]==false) { q.add(y); ans[y]=ans[x]+1; visited[y]=true; } } } } static int[] ans; // static int[] a; public static int[] sort(int[] a) { int n=a.length; ArrayList<Integer> ar=new ArrayList<>(); for(int i=0;i<a.length;i++) { ar.add(a[i]); } Collections.sort(ar); for(int i=0;i<n;i++) { a[i]=ar.get(i); } return a; } static public void main(String args[])throws IOException { int n=i(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=i(); } Arrays.sort(a); boolean[] flag=new boolean[n]; int ans=0; for(int i=0;i<n;i++) { if(flag[i]==false) { ans++; for(int j=0;j<n;j++) { if(a[j]%a[i]==0 && flag[j]==false) { flag[j]=true; } } } } pln(ans+""); } /**/ static InputReader in=new InputReader(System.in); static OutputWriter out=new OutputWriter(System.out); public static long l() { String s=in.String(); return Long.parseLong(s); } public static void pln(String value) { System.out.println(value); } public static int i() { return in.Int(); } public static String s() { return in.String(); } } 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); } } 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(); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.Int(); return array; } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader sc = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, sc, out); out.close(); } static class Task { public void solve(int testNumber, InputReader sc, PrintWriter out) { int n=sc.nextInt(); int[] a=new int[n]; boolean[] jud=new boolean[101]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); Arrays.sort(a); int ans=0; for(int i=0;i<n;i++) { if(jud[a[i]]) continue; ans++; for(int j=a[i];j<=100;j+=a[i]) jud[j]=true; } out.println(ans); } } 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()); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { MScanner sc=new MScanner(System.in); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt();HashSet<Integer>nums=new HashSet<Integer>(); int[]in=new int[n];for(int i=0;i<n;i++)in[i]=sc.nextInt(); Arrays.sort(in); int ans=0; boolean vis[]=new boolean[n]; for(int i=0;i<n;i++) { if(vis[i])continue; for(int j=i+1;j<n;j++) { if(in[j]%in[i]==0) { vis[j]=true; } } ans++; } pw.println(ans); pw.flush(); } static class MScanner { StringTokenizer st; BufferedReader br; public MScanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public MScanner(String file) throws Exception { 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 String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
/** * @author Finn Lidbetter */ import java.util.*; import java.io.*; import java.awt.geom.*; public class TaskA { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); int[] arr = new int[n]; for (int i=0; i<n; i++) { arr[i] = Integer.parseInt(s[i]); } Arrays.sort(arr); boolean[] vis = new boolean[n]; int nColours = 0; int nVis = 0; int index = 0; while (nVis<n) { while (index<n && nVis<n) { if (vis[index]) { index++; continue; } int val = arr[index]; nColours++; while (index<n && nVis<n) { if (vis[index]) { index++; continue; } if (arr[index]%val==0) { vis[index] = true; nVis++; } index++; } index = 0; } } System.out.println(nColours); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] nums = new int[n]; for(int i = 0; i < n; i++){ nums[i] = scan.nextInt(); } Arrays.sort(nums); boolean[] div = new boolean[n]; int count = 0; for(int i = 0; i < n; i++) { if (!div[i]) { count++; div[i] = true; for(int j = i+1; j < n; j++) { if (nums[j] % nums[i] == 0) { div[j] = true; } } } } System.out.println(count); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { private final static long mod = 1000000007; private final static int MAXN = 100001; private static long power(long x, long y, long m) { long temp; if (y == 0) return 1; temp = power(x, y / 2, m); temp = (temp * temp) % m; if (y % 2 == 0) return temp; else return ((x % m) * temp) % m; } private static long power(long x, long y) { return power(x, y, mod); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int nextPowerOf2(int a) { return 1 << nextLog2(a); } static int nextLog2(int a) { return (a == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(a - 1)); } private static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long q = a / m; long t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } private static int[] getLogArr(int n) { int arr[] = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = (int) (Math.log(i) / Math.log(2) + 1e-10); } return arr; } private static int log[] = getLogArr(MAXN); private static int getLRMax(int st[][], int L, int R) { int j = log[R - L + 1]; return Math.max(st[L][j], st[R - (1 << j) + 1][j]); } private static int[][] getSparseTable(int array[]) { int k = log[MAXN] + 1; int st[][] = new int[MAXN][k + 1]; for (int i = 0; i < array.length; i++) st[i][0] = array[i]; for (int j = 1; j <= k; j++) { for (int i = 0; i + (1 << j) <= array.length; i++) { st[i][j] = Math.max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } } return st; } static class Subset { int parent; int rank; @Override public String toString() { return "" + parent; } } static int find(Subset[] Subsets, int i) { if (Subsets[i].parent != i) Subsets[i].parent = find(Subsets, Subsets[i].parent); return Subsets[i].parent; } static void union(Subset[] Subsets, int x, int y) { int xroot = find(Subsets, x); int yroot = find(Subsets, y); if (Subsets[xroot].rank < Subsets[yroot].rank) Subsets[xroot].parent = yroot; else if (Subsets[yroot].rank < Subsets[xroot].rank) Subsets[yroot].parent = xroot; else { Subsets[xroot].parent = yroot; Subsets[yroot].rank++; } } private static int maxx(Integer... a) { return Collections.max(Arrays.asList(a)); } private static class Pair<T, U> { T a; U b; public Pair(T a, U b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a.equals(pair.a) && b.equals(pair.b); } @Override public int hashCode() { return Objects.hash(a, b); } @Override public String toString() { return "(" + a + "," + b + ')'; } } public static void main(String[] args) throws Exception { try (FastReader in = new FastReader(); FastWriter out = new FastWriter()) { int t, i, j, n, k, l, r, m, c, p, q, ti, tidx; long x, y, z; //for (t = in.nextInt(), tidx = 1; tidx <= t; tidx++) { //out.print(String.format("Case #%d: ", tidx)); n=in.nextInt(); int a[]=new int[101]; for (i=0;i<n;i++){ a[in.nextInt()]++; } m=0; for(i=1;i<101;i++){ if(a[i]>0){ m++; for(j=i;j<=100;j+=i){ a[j]=0; } } } out.println(m); } out.commit(); } } static class FastReader implements Closeable { private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer st; String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception 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[] nextIntArr(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } double[] nextDoubleArr(int n) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } long[] nextLongArr(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } String[] nextStrArr(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = next(); } return arr; } int[][] nextIntArr2(int n, int m) { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { arr[i] = nextIntArr(m); } return arr; } long[][] nextLongArr2(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { arr[i] = nextLongArr(m); } return arr; } @Override public void close() throws IOException { br.close(); } } static class FastWriter implements Closeable { BufferedWriter bw; StringBuilder sb = new StringBuilder(); List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); FastWriter() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } <T> void commit() throws IOException { bw.write(sb.toString()); bw.flush(); sb = new StringBuilder(); } <T> void print(T obj) { sb.append(obj.toString()); } void println() throws IOException { print("\n"); } <T> void println(T obj) throws IOException { print(obj.toString() + "\n"); } <T> void printArrLn(T[] arr) throws IOException { for (int i = 0; i < arr.length - 1; i++) { print(arr[i] + " "); } println(arr[arr.length - 1]); } <T> void printArr2(T[][] arr) throws IOException { for (int j = 0; j < arr.length; j++) { for (int i = 0; i < arr[j].length - 1; i++) { print(arr[j][i] + " "); } println(arr[j][arr.length - 1]); } } <T> void printColl(Collection<T> coll) throws IOException { for (T e : coll) { print(e + " "); } println(); } void printCharN(char c, int n) throws IOException { for (int i = 0; i < n; i++) { print(c); } } void printIntArr2(int[][] arr) throws IOException { for (int j = 0; j < arr.length; j++) { for (int i = 0; i < arr[j].length - 1; i++) { print(arr[j][i] + " "); } println(arr[j][arr.length - 1]); } } @Override public void close() throws IOException { bw.close(); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; 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 anand.oza */ 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); APaintTheNumbers solver = new APaintTheNumbers(); solver.solve(1, in, out); out.close(); } static class APaintTheNumbers { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = in.readIntArray(n); Arrays.sort(a); int answer = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) continue; answer++; for (int j = 0; j < n; j++) { if (j == i) continue; if (a[j] % a[i] == 0) { a[j] = 0; } } a[i] = 0; } out.println(answer); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] readIntArray(int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = nextInt(); } return x; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.ArrayList; import java.util.Scanner; public class kosyaDetka { public static void main(String[] args){ Scanner scan = new Scanner(System.in); int t = scan.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); for(int i = 0; i < t; i++){ arr.add( scan.nextInt()); } int count = 0; while (arr.size() != 0){ int min = Integer.MAX_VALUE; for(int i = 0; i < arr.size(); i++){ int temp = arr.get(i); if( temp < min){ min = temp; } } for(int i = 0; i < arr.size(); i++){ int temp = arr.get(i); if( temp % min == 0){ arr.remove(i); i--; } } count++; } System.out.println(count); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class DivRound584ProblemA { static FastReader sc=new FastReader(); public static void main(String args[]) throws IOException { int n = sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); Arrays.sort(a); int c=0; for(int i=0;i<n;i++) { if(a[i]<0) continue; c=c-1; for(int j=i+1;j<n;j++) { if(a[j]<0) continue; if(a[j]%a[i]==0) { //System.out.println(a[i]+" : "+a[j]); a[j]=c; } } //System.out.println(c); } System.out.println(Math.abs(c)); } 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; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < count; i++) { set.add(sc.nextInt()); } ArrayList<Integer> list = new ArrayList<>(set); Collections.sort(list); for (int i = 0; i < list.size(); i++) { for (int j = i + 1; j < list.size(); j++) { if (list.get(i) % list.get(j) == 0 || list.get(j) % list.get(i) == 0) { list.remove(j); j--; } } } System.out.println(list.size()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } private void run() throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); int[] arr = new int[n]; String[] line = reader.readLine().split("\\s"); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(line[i]); } Arrays.sort(arr); Set<Integer> numbers = new HashSet<>(); for (int i = 0; i < arr.length; i++) { Iterator<Integer> iter = numbers.iterator(); boolean contains = false; while (iter.hasNext()){ int elem = iter.next(); if(gcd(elem, arr[i]) == elem){ contains = true; } } if(!contains) numbers.add(arr[i]); } System.out.println(numbers.size()); } private int gcd(int a, int b){ while (a != b){ if(a > b) a -= b; else b -= a; } return a; } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class cf573 { public static void main(String[] args){ Scanner scan=new Scanner(System.in); int n=0; if(scan.hasNext()) n=scan.nextInt(); TreeSet<Integer> set=new TreeSet<>(); for(int i=0;i<n;i++){ if(scan.hasNext()) set.add(scan.nextInt()); } int[] arr=new int[set.size()]; Iterator<Integer> it=set.iterator(); int j=0; while(it.hasNext()){ arr[j++]=it.next(); } int tot=1,flag; for(int i=1;i<arr.length;i++){ flag=0; for(int k=0;k<i;k++){ if(arr[i]%arr[k]==0){ flag=1; break; } } if(flag==0){ tot++; } } System.out.println(tot); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = scn.nextInt(); scn.close(); Arrays.sort(a); ArrayList<Integer> cyka = new ArrayList<>(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (a[j] % a[i] == 0) { boolean add = true; for (int k : cyka) { if (a[i] % k == 0) { add = false; break; } } if (add) { cyka.add(a[i]); } } } } System.out.println(cyka.size()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; import java.nio.charset.Charset; import java.util.*; public class CFContest { public static void main(String[] args) throws Exception { boolean local = System.getProperty("ONLINE_JUDGE") == null; boolean async = true; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io, new Debug(local)); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; final Debug debug; int inf = (int) 1e8; long lInf = (long) 1e18; public Task(FastIO io, Debug debug) { this.io = io; this.debug = debug; } @Override public void run() { solve(); } public void solve() { int n = io.readInt(); int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = io.readInt(); } Arrays.sort(data); boolean[] paint = new boolean[n]; int cnt = 0; for (int i = 0; i < n; i++) { if (paint[i]) { continue; } cnt++; for (int j = i; j < n; j++) { if (data[j] % data[i] == 0) { paint[j] = true; } } } io.cache.append(cnt); } } public static class FastIO { public final StringBuilder cache = new StringBuilder(20 << 20); private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 8); private byte[] buf = new byte[1 << 20]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } public FastIO(InputStream is, OutputStream os) { this(is, os, Charset.forName("ascii")); } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { boolean sign = true; skipBlank(); if (next == '+' || next == '-') { sign = next == '+'; next = read(); } long val = 0; while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } if (next != '.') { return sign ? val : -val; } next = read(); long radix = 1; long point = 0; while (next >= '0' && next <= '9') { point = point * 10 + next - '0'; radix = radix * 10; next = read(); } double result = val + (double) point / radix; return sign ? result : -result; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readLine(char[] data, int offset) { int originalOffset = offset; while (next != -1 && next != '\n') { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public char readChar() { skipBlank(); char c = (char) next; next = read(); return c; } public void flush() { try { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } catch (IOException e) { throw new RuntimeException(e); } } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Debug { private boolean allowDebug; public Debug(boolean allowDebug) { this.allowDebug = allowDebug; } public void assertTrue(boolean flag) { if (!allowDebug) { return; } if (!flag) { fail(); } } public void fail() { throw new RuntimeException(); } public void assertFalse(boolean flag) { if (!allowDebug) { return; } if (flag) { fail(); } } private void outputName(String name) { System.out.print(name + " = "); } public void debug(String name, int x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, long x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, double x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, int[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, long[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, double[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, Object x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, Object... x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.deepToString(x)); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class A { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); String str[] = reader.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(str[i]); } Arrays.sort(a); int k = 0; for (int i = 0; i < n; i++) { if (a[i] == 0){ continue; } for (int j = i + 1; j < n; j++) { if (a[j] % a[i] == 0){ a[j] = 0; } } k++; } System.out.println(k); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Paint_The_Numbers { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<Integer> paint = new ArrayList<Integer>(); int num = scan.nextInt(); for(int i = 0; i < num;i++) paint.add(scan.nextInt()); Collections.sort(paint); int counter = 0; //System.out.println(paint); while(paint.size()!=0) { num = paint.remove(0); for(int i = 0; i<paint.size();i++) { if(paint.get(i)%num==0) { paint.remove(i--); } } counter++; } System.out.println(counter); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class code_1 { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=in.nextInt(); Arrays.sort(a); for(int i=0;i<n-1;i++) { if(a[i]!=-1) { for(int j=i+1;j<n;j++) { if(a[j]%a[i]==0) a[j]=-1; } } } int count=0; for(int i=0;i<n;i++) { if(a[i]!=-1) count++; } System.out.println(count); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; public class A1 { public static BufferedReader br; public static StringTokenizer st; public static String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return st.nextToken(); } public static Integer nextInt() { return Integer.parseInt(next()); } public static Long nextLong() { return Long.parseLong(next()); } public static Double nextDouble() { return Double.parseDouble(next()); } static long fast_pow(long base,long n,long M) { if(n==0) return 1; if(n==1) return base; 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 finextDoubleMMI_fermat(long n,int M) { return fast_pow(n,M-2,M); } static long nCrModPFermat(int n, int r, int p) { 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]* finextDoubleMMI_fermat(fac[r], p)% p * finextDoubleMMI_fermat(fac[n-r], p) % p) % p; } static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[] = new int [n1]; int R[] = new int [n2]; for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; int i = 0, j = 0; int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } static void sort(int arr[], int l, int r) { if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static void sort(int arr[]) { int l=0; int r=arr.length-1; if (l < r) { int m = (l+r)/2; sort(arr, l, m); sort(arr , m+1, r); merge(arr, l, m, r); } } static long gcd(long a, long b){ if(a%b==0) return b; if(b%a==0) return a; if(a>b) return gcd(a%b,b); return gcd(a,b%a); } static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String args[])throws IOException{ int i,j; br = new BufferedReader(new InputStreamReader(System.in)); int n=nextInt(); int a[]=new int[n]; for(i=0;i<n;i++) a[i]=nextInt(); Arrays.sort(a); int l=0; for(i=0;i<n;i++){ if(a[i]!=-1){ int p=a[i]; for(j=i;j<n;j++){ if(a[j]%p==0) a[j]=-1; } l++; } } pw.println(l); pw.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main2 { static long mod = 998244353; static FastScanner scanner; static Set<Long> second = new HashSet<>(); static boolean applied = false; public static void main(String[] args) { scanner = new FastScanner(); int n = scanner.nextInt(); int[] a = scanner.nextIntArray(n); int[] colors = new int[n]; ADUtils.sort(a); int color = 0; for (int i = 0; i < n; i++) { if (colors[i] != 0) continue; color++; for (int j = i; j < n; j++) { if (a[j] % a[i] == 0) colors[j] = color; } } System.out.println(color); } static class WithIdx implements Comparable<WithIdx>{ int val, idx; public WithIdx(int val, int idx) { this.val = val; this.idx = idx; } @Override public int compareTo(WithIdx o) { return Integer.compare(val, o.val); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } String[] nextStringArray(int n) { String[] res = new String[n]; for (int i = 0; i < n; i++) res[i] = nextToken(); return res; } } static class PrefixSums { long[] sums; public PrefixSums(long[] sums) { this.sums = sums; } public long sum(int fromInclusive, int toExclusive) { if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value"); return sums[toExclusive] - sums[fromInclusive]; } public static PrefixSums of(int[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } public static PrefixSums of(long[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } } static class ADUtils { static void sort(int[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } static void reverse(int[] arr) { int last = arr.length / 2; for (int i = 0; i < last; i++) { int tmp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = tmp; } } static void sort(long[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } } static class MathUtils { static long[] FIRST_PRIMES = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 , 97 , 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051}; static long[] primes(int to) { long[] all = new long[to + 1]; long[] primes = new long[to + 1]; all[1] = 1; int primesLength = 0; for (int i = 2; i <= to; i ++) { if (all[i] == 0) { primes[primesLength++] = i; all[i] = i; } for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j]; j++) { all[(int) (i * primes[j])] = primes[j]; } } return Arrays.copyOf(primes, primesLength); } static long modpow(long b, long e, long m) { long result = 1; while (e > 0) { if ((e & 1) == 1) { /* multiply in this bit's contribution while using modulus to keep * result small */ result = (result * b) % m; } b = (b * b) % m; e >>= 1; } return result; } static long submod(long x, long y, long m) { return (x - y + m) % m; } } } /* 5 6 1 4 2 3 3 4 4 5 5 2 3 5 5 8 1 2 2 3 1 3 4 3 3 4 4 1 4 5 5 1 */
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author prakhar897 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); APaintTheNumbers solver = new APaintTheNumbers(); solver.solve(1, in, out); out.close(); } static class APaintTheNumbers { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int arr[] = in.nextIntArray(n); Arrays.sort(arr); int ans = 0; for (int i = 0; i < n; i++) { if (arr[i] == -1) continue; else { //out.println(arr[i]); ans++; for (int j = i + 1; j < n; j++) { if (arr[j] % arr[i] == 0) arr[j] = -1; } arr[i] = -1; //out.println(arr); } } out.println(ans); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int 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 isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(int i) { writer.println(i); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
/*Author: Satyajeet Singh, Delhi Technological University*/ import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Main{ /*********************************************Constants******************************************/ static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static long mod=(long)1e9+7; static long mod1=998244353; static boolean sieve[]; static ArrayList<Integer> primes; static long factorial[],invFactorial[]; static HashSet<Pair> graph[]; static boolean oj = System.getProperty("ONLINE_JUDGE") != null; /****************************************Solutions Begins***************************************/ public static void main (String[] args) throws Exception { String st[]=nl(); int n=pi(st[0]); int input[]=new int[n]; st=nl(); for(int i=0;i<n;i++){ input[i]=pi(st[i]); } int ans=0; Arrays.sort(input); boolean dp[]=new boolean[n]; for(int i=0;i<n;i++){ if(!dp[i]){ ans++; for(int j=input[i];j<=200;j+=input[i]){ for(int k=i;k<n;k++){ if(input[k]==j&&!dp[k])dp[k]=true; } } } } out.println(ans); /****************************************Solutions Ends**************************************************/ out.flush(); out.close(); } /****************************************Template Begins************************************************/ static String[] nl() throws Exception{ return br.readLine().split(" "); } static String[] nls() throws Exception{ return br.readLine().split(""); } static int pi(String str) { return Integer.parseInt(str); } static long pl(String str){ return Long.parseLong(str); } static double pd(String str){ return Double.parseDouble(str); } /***************************************Precision Printing**********************************************/ static void printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.000000000000000000000"); out.println(ft.format(d)); } /**************************************Bit Manipulation**************************************************/ static void printMask(long mask){ System.out.println(Long.toBinaryString(mask)); } static int countBit(int mask){ int ans=0; while(mask!=0){ if(mask%2==1){ ans++; } mask/=2; } return ans; } /******************************************Graph*********************************************************/ static void Makegraph(int n){ graph=new HashSet[n]; for(int i=0;i<n;i++){ graph[i]=new HashSet<>(); } } static void addEdge(int a,int b){ graph[a].add(new Pair(b,1)); } static void addEdge(int a,int b,int c){ graph[a].add(new Pair(b,c)); } /*********************************************PAIR********************************************************/ static class PairComp implements Comparator<Pair>{ public int compare(Pair p1,Pair p2){ return p1.u-p2.u; } } static class Pair implements Comparable<Pair> { int u; int v; int index=-1; public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pair other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /******************************************Long Pair*******************************************************/ static class PairCompL implements Comparator<Pairl>{ public int compare(Pairl p1,Pairl p2){ if(p1.u-p2.u<0){ return -1; } else if(p1.u-p2.u>0){ return 1; } else{ if(p1.v-p2.v<0){ return -1; } else if(p1.v-p2.v>0){ return 1; } else{ return 0; } } } } static class Pairl implements Comparable<Pairl> { long u; long v; int index=-1; public Pairl(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pairl other = (Pairl) o; return u == other.u && v == other.v; } public int compareTo(Pairl other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /*****************************************DEBUG***********************************************************/ public static void debug(Object... o) { if(!oj) System.out.println(Arrays.deepToString(o)); } /************************************MODULAR EXPONENTIATION***********************************************/ static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } /********************************************GCD**********************************************************/ static long gcd(long x, long y) { if(x==0) return y; if(y==0) return x; long r=0, a, b; a = (x > y) ? x : y; // a is greater number b = (x < y) ? x : y; // b is smaller number r = b; while(a % b != 0) { r = a % b; a = b; b = r; } return r; } /******************************************SIEVE**********************************************************/ static void sieveMake(int n){ sieve=new boolean[n]; Arrays.fill(sieve,true); sieve[0]=false; sieve[1]=false; for(int i=2;i*i<n;i++){ if(sieve[i]){ for(int j=i*i;j<n;j+=i){ sieve[j]=false; } } } primes=new ArrayList<Integer>(); for(int i=0;i<n;i++){ if(sieve[i]){ primes.add(i); } } } /********************************************End***********************************************************/ }
quadratic
1209_A. Paint the Numbers
CODEFORCES
// No sorceries shall previal. // import java.util.Scanner; import java.io.PrintWriter; import java.util.*; import java.util.Arrays; public class InVoker { public static void main(String args[]) { Scanner inp = new Scanner(System.in); PrintWriter out= new PrintWriter(System.out); int n=inp.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=inp.nextInt(); Arrays.sort(a); int gg=0; for(int i=0;i<n;i++) { if(a[i]==0) continue; gg++; for(int j=i+1;j<n;j++) { if(a[j]%a[i]==0) { a[j]=0; } } } out.println(gg); out.close(); inp.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author ilyakor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); boolean[] a = new boolean[218]; for (int i = 0; i < n; ++i) { a[in.nextInt()] = true; } int res = 0; for (int i = 1; i < a.length; ++i) { if (a[i]) { ++res; for (int j = i; j < a.length; j += i) { a[j] = false; } } } out.printLine(res); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } static class InputReader { private InputStream stream; private byte[] buffer = new byte[10000]; private int cur; private int count; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isSpace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (count == -1) { throw new InputMismatchException(); } try { if (cur >= count) { cur = 0; count = stream.read(buffer); if (count <= 0) { return -1; } } } catch (IOException e) { throw new InputMismatchException(); } return buffer[cur++]; } public int readSkipSpace() { int c; do { c = read(); } while (isSpace(c)); return c; } public int nextInt() { int sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10 + c - '0'; c = read(); } while (!isSpace(c)); res *= sgn; return res; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import javax.print.DocFlavor; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Round584_a { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int n = Integer.parseInt(br.readLine()); st = new StringTokenizer(br.readLine()); int a[] = new int[n]; for(int i=0 ; i<n ; i++) { a[i] = Integer.parseInt(st.nextToken()); } Arrays.sort(a); boolean vis[] = new boolean[n]; int count = 0; for(int i=0 ; i<n ; i++) { if(!vis[i]) { for(int j=i ; j<n ; j++) { if(!vis[j]) { if(a[j]%a[i] == 0) { vis[j] = true; } } } count++; } } System.out.println(count); } } /*import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Round584_a { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; } }*/
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } Arrays.sort(a); Set<Integer> div = new HashSet<>(); boolean[] d = new boolean[n]; for (int i = 0; i < n; i++) { for (int j = 0 ; j < n; j++) { if (d[j]) { continue; } if (a[j]%a[i] == 0) { d[j] = true; div.add(a[i]); } } } System.out.println(div.size()); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; import static java.lang.Math.*; public class A { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = in.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } Arrays.sort(a); int vis[] = new int[n]; Arrays.fill(vis, -1); int c = 0; for (int i = 0; i < n; i++) { if (vis[i] != -1) continue; c++; for (int j = i; j < n; j++) { if (vis[j] == -1 && a[j] % a[i] == 0) { vis[j] = c; } } } pw.println(c); pw.close(); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String args[]) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); IntStream.range(0, 1).forEach(tc -> { new Solver(tc, in, out).solve(); out.flush(); }); out.close(); } } class InputReader { BufferedReader reader; StringTokenizer tokenizer; InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } int nextInt() { return Integer.valueOf(next()); } double nextDouble() { return Double.valueOf(next()); } String nextLine() { try { return reader.readLine(); } catch (Exception e) { throw new RuntimeException(e); } } } class Solver { private InputReader in; private PrintWriter out; private Integer tc; Solver(Integer tc, InputReader in, PrintWriter out) { this.in = in; this.out = out; this.tc = tc; } void solve() { Integer n = in.nextInt(); TreeSet<Integer> list = IntStream.range(0, n) .map(i -> in.nextInt()) .boxed() .collect(Collectors.toCollection(TreeSet::new)); Integer answer = 0; while (!list.isEmpty()) { Integer x = list.pollFirst(); list = list.stream().filter(y -> y % x != 0).collect(Collectors.toCollection(TreeSet::new)); answer++; } out.println(answer); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; public class Tt { public static void main(String[] args) throws IOException { FastScanner fs=new FastScanner(System.in); int j = fs.nextInt(); ArrayList<Integer> a =new ArrayList<Integer>(); for(int i=0;i<j;i++) { a.add(fs.nextInt()); } Collections.sort(a); Collections.reverse(a); int c=0; while(a.size()!=0) { int f=a.get(a.size()-1); c+=1; for(int q=a.size()-1;q>-1;q--) if(a.get(q)%f==0) { a.remove(q); } } System.out.println(c); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i){ br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws IOException{ if(st.hasMoreTokens()) return st.nextToken(); else st = new StringTokenizer(br.readLine()); return next(); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public String nextLine() throws IOException { if(!st.hasMoreTokens()) return br.readLine(); String ret = st.nextToken(); while(st.hasMoreTokens()) ret += " " + st.nextToken(); return ret; } }}
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class paintNumbers { public static int minIndex(List<Integer> list) { int min = list.get(0); int minIndex = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i) < min) { min = list.get(i); minIndex = i; } } return minIndex; } public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { list.add(sc.nextInt()); } int count = 0; while (list.size() > 0) { count++; int temp = list.get(minIndex(list)); // pw.println("min = " + temp); for (int j = 0; j < list.size(); j++) { if (list.get(j) % temp == 0) { // pw.println("j = " + list.get(j)); // pw.println("min = " + temp); list.remove(j); j--; } } // for (int i = 0; i < list.size(); i++) { // pw.println(list.get(i) + " "); // } } pw.println(count); pw.close(); } static class FastScanner { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastScanner(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 peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public BigInteger readBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } 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 boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] nextIntArray(int n){ int[] array=new int[n]; for(int i=0;i<n;++i)array[i]=nextInt(); return array; } public int[] nextSortedIntArray(int n){ int array[]=nextIntArray(n); PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); for(int i = 0; i < n; i++){ pq.add(array[i]); } int[] out = new int[n]; for(int i = 0; i < n; i++){ out[i] = pq.poll(); } return out; } public int[] nextSumIntArray(int n){ int[] array=new int[n]; array[0]=nextInt(); for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt(); return array; } public ArrayList<Integer>[] nextGraph(int n, int m){ ArrayList<Integer>[] adj = new ArrayList[n]; for(int i = 0; i < n; i++){ adj[i] = new ArrayList<Integer>(); } for(int i = 0; i < m; i++){ int u = nextInt(); int v = nextInt(); u--; v--; adj[u].add(v); adj[v].add(u); } return adj; } public ArrayList<Integer>[] nextTree(int n){ return nextGraph(n, n-1); } public long[] nextLongArray(int n){ long[] array=new long[n]; for(int i=0;i<n;++i)array[i]=nextLong(); return array; } public long[] nextSumLongArray(int n){ long[] array=new long[n]; array[0]=nextInt(); for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt(); return array; } public long[] nextSortedLongArray(int n){ long array[]=nextLongArray(n); Arrays.sort(array); return array; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
//package round584; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class A { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int[] a = na(n); Arrays.sort(a); boolean[] done = new boolean[n]; int ans = 0; for(int i = 0;i < n;i++){ if(!done[i]){ ans++; for(int j = i+1;j < n;j++){ if(a[j] % a[i] == 0){ done[j] = true; } } } } out.println(ans); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new A().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); APaintTheNumbers solver = new APaintTheNumbers(); solver.solve(1, in, out); out.close(); } static class APaintTheNumbers { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); int[] hash = new int[101]; boolean[] hash1 = new boolean[101]; for (int i = 0; i < n; i++) hash[in.scanInt()]++; int ans = 0; for (int i = 1; i <= 100; i++) { if (hash1[i]) continue; if (hash[i] == 0) continue; for (int j = i; j <= 100; j += i) hash1[j] = true; ans++; } out.println(ans); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= total) { index = 0; try { total = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (total <= 0) return -1; } return buf[index++]; } public int scanInt() { int 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(); } } return neg * integer; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); Arrays.sort(a); ArrayList<Integer>al=new ArrayList(); int k=a[0]; int count=0; for(int j=0;j<n;j++) {k=a[j]; if(Collections.frequency(al,a[j])==0) {for(int i=0;i<n;i++) {if(a[i]%k==0) {al.add(a[i]);}} count++;}} System.out.println(count);}}
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.*; import java.io.*; public class A { public static void main(String[] args) { FastScanner scanner = new FastScanner(); PrintWriter out = new PrintWriter(System.out, false); int n = scanner.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } Arrays.sort(arr); int[] cols = new int[n]; Arrays.fill(cols, -1); int ans = 0; for(int i = 0; i < n; i++) { if (cols[i] == -1) { cols[i] = ans++; for(int j = i + 1; j < n; j++) { if (arr[j] % arr[i] == 0) cols[j] = cols[i]; } } } out.println(ans); out.flush(); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(Reader in) { br = new BufferedReader(in); } public FastScanner() { this(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 readNextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
//package Round584; import java.util.Arrays; import java.util.Scanner; public class Problem1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int n = sc.nextInt(); int a []=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } Arrays.sort(a); // System.out.println(Arrays.toString(a)); int k=a.length; for(int i=a.length-1;i>=0;i--) { int A=a[i]; for (int j=0;j<i;j++) { if(A%a[j]==0) { k--; break; } } } System.out.println(k); sc.close(); } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); static int oo = (int)1e9; static int mod = 1000000007; public static void main(String[] args) throws IOException { int n = in.nextInt(); int[] a = in.nextIntArray(n); Arrays.sort(a); boolean[] color = new boolean[n]; int cnt = 0; for(int i = 0; i < n; ++i) { if(!color[i]) { cnt++; for(int j = i; j < n; j++) { if(a[j] % a[i] == 0) color[j] = true; } } } System.out.println(cnt); out.close(); } static class SegmentTree { int n; long[] a, seg; int DEFAULT_VALUE = 0; public SegmentTree(long[] a, int n) { super(); this.a = a; this.n = n; seg = new long[n * 4 + 1]; build(1, 0, n-1); } private long build(int node, int i, int j) { if(i == j) return seg[node] = a[i]; long first = build(node * 2, i, (i+j) / 2); long second = build(node * 2 + 1, (i+j) / 2 + 1, j); return seg[node] = combine(first, second); } long update(int k, long value) { return update(1, 0, n-1, k, value); } private long update(int node, int i, int j, int k, long value) { if(k < i || k > j) return seg[node]; if(i == j && j == k) { a[k] = value; seg[node] = value; return value; } int m = (i + j) / 2; long first = update(node * 2, i, m, k, value); long second = update(node * 2 + 1, m + 1, j, k, value); return seg[node] = combine(first, second); } long query(int l, int r) { return query(1, 0, n-1, l, r); } private long query(int node, int i, int j, int l, int r) { if(l <= i && j <= r) return seg[node]; if(j < l || i > r) return DEFAULT_VALUE; int m = (i + j) / 2; long first = query(node * 2, i, m, l, r); long second = query(node * 2 + 1, m+1, j, l, r); return combine(first, second); } private long combine(long a, long b) { return a + b; } } static class DisjointSet { int n; int[] g; int[] h; public DisjointSet(int n) { super(); this.n = n; g = new int[n]; h = new int[n]; for(int i = 0; i < n; ++i) { g[i] = i; h[i] = 1; } } int find(int x) { if(g[x] == x) return x; return g[x] = find(g[x]); } void union(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(h[x] >= h[y]) { g[y] = x; if(h[x] == h[y]) h[x]++; } else { g[x] = y; } } } static int[] getPi(char[] a) { int m = a.length; int j = 0; int[] pi = new int[m]; for(int i = 1; i < m; ++i) { while(j > 0 && a[i] != a[j]) j = pi[j-1]; if(a[i] == a[j]) { pi[i] = j + 1; j++; } } return pi; } static long lcm(long a, long b) { return a * b / gcd(a, b); } static boolean nextPermutation(int[] a) { for(int i = a.length - 2; i >= 0; --i) { if(a[i] < a[i+1]) { for(int j = a.length - 1; ; --j) { if(a[i] < a[j]) { int t = a[i]; a[i] = a[j]; a[j] = t; for(i++, j = a.length - 1; i < j; ++i, --j) { t = a[i]; a[i] = a[j]; a[j] = t; } return true; } } } } return false; } static void shuffle(int[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); int t = a[si]; a[si] = a[i]; a[i] = t; } } static void shuffle(long[] a) { Random r = new Random(); for(int i = a.length - 1; i > 0; --i) { int si = r.nextInt(i); long t = a[si]; a[si] = a[i]; a[i] = t; } } static int lower_bound(int[] a, int n, int k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int lower_bound(long[] a, int n, long k) { int s = 0; int e = n; int m; while (e - s > 0) { m = (s + e) / 2; if (a[m] < k) s = m + 1; else e = m; } return e; } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static class Pair implements Comparable<Pair> { int first, second; public Pair(int first, int second) { super(); this.first = first; this.second = second; } @Override public int compareTo(Pair o) { return this.first != o.first ? this.first - o.first : this.second - o.second; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + first; result = prime * result + second; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pair other = (Pair) obj; if (first != other.first) return false; if (second != other.second) return false; return true; } } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
quadratic
1209_A. Paint the Numbers
CODEFORCES
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class A { static int countColors(List<Integer> colors) { Collections.sort(colors); int k = 0; while (!colors.isEmpty()) { Integer c = colors.get(0); colors.remove(0); k++; List<Integer> toRemove = new ArrayList<>(); for (int i = 0; i < colors.size(); i++) { if (colors.get(i) % c == 0) toRemove.add(i); } Collections.reverse(toRemove); for (Integer integer : toRemove) { colors.remove(integer.intValue()); } } return k; } public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int n = scanner.nextInt(); scanner.nextLine(); List<Integer> integers = Arrays.stream(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList()); System.out.println(countColors(integers)); } } }
quadratic
1209_A. Paint the Numbers
CODEFORCES