id
stringlengths
22
25
content
stringlengths
327
628k
max_stars_repo_path
stringlengths
49
49
condefects-java_data_1
import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; public class Main { public static void main(String[] args) { Main o = new Main(); o.solve(); } public void solve() { FastScanner sc = new FastScanner(System.in); int N = sc.nextInt(); int[] P = new int[N]; for (int i = 0; i < N; i++) { P[i] = sc.nextInt(); } int cnt = 0; int ei = 0; int oi = 1; StringBuilder ans = new StringBuilder(); while ( ei < N && oi < N ) { while ( ei < N && P[ei] % 2 != 0 ) ei += 2; while ( oi < N && P[oi] % 2 == 0 ) oi += 2; if ( ei >= N || oi >= N ) break; int i0 = 0; if ( ei < oi ) { for (int i = ei; i <= oi - 3; i += 2) { ans.append("B "); ans.append(i + 1); ans.append("\n"); swap(P, i, i + 2); cnt++; } i0 = oi - 1; } else { for (int i = oi; i <= ei - 3; i += 2) { ans.append("B "); ans.append(i + 1); ans.append("\n"); swap(P, i, i + 2); cnt++; } i0 = ei - 1; } ans.append("A "); ans.append(i0 + 1); ans.append("\n"); swap(P, i0, i0 + 1); cnt++; } for (int p = 0; p < 2; p++) { for (int i = p; i < N; i += 2) { for (int j = p + 2; j < N - i; j += 2) { if ( P[j - 2] > P[j] ) { ans.append("B "); ans.append(j - 1); ans.append("\n"); swap(P, j - 2, j); cnt++; } } } } System.out.println(cnt); System.out.print(ans.toString()); } void swap(int[] P, int i, int j) { int tmp = P[i]; P[i] = P[j]; P[j] = tmp; } class Number implements Comparable<Number> { int val = 0; int ord = 0; Number(int val) { this.val = val; } @Override public int compareTo(Main.Number o) { return val < o.val ? -1 : 1; } } class FastScanner { private final InputStream in; private final byte[] buf = new byte[1024]; private int ptr = 0; private int buflen = 0; FastScanner( InputStream source ) { this.in = source; } private boolean hasNextByte() { if ( ptr < buflen ) return true; else { ptr = 0; try { buflen = in.read(buf); } catch (IOException e) { e.printStackTrace(); } if ( buflen <= 0 ) return false; } return true; } private int readByte() { if ( hasNextByte() ) return buf[ptr++]; else return -1; } private boolean isPrintableChar( int c ) { return 33 <= c && c <= 126; } private boolean isNumeric( int c ) { return '0' <= c && c <= '9'; } private void skipToNextPrintableChar() { while ( hasNextByte() && !isPrintableChar(buf[ptr]) ) ptr++; } public boolean hasNext() { skipToNextPrintableChar(); return hasNextByte(); } public String next() { if ( !hasNext() ) throw new NoSuchElementException(); StringBuilder ret = new StringBuilder(); int b = readByte(); while ( isPrintableChar(b) ) { ret.appendCodePoint(b); b = readByte(); } return ret.toString(); } public long nextLong() { if ( !hasNext() ) throw new NoSuchElementException(); long ret = 0; int b = readByte(); boolean negative = false; if ( b == '-' ) { negative = true; if ( hasNextByte() ) b = readByte(); } if ( !isNumeric(b) ) throw new NumberFormatException(); while ( true ) { if ( isNumeric(b) ) ret = ret * 10 + b - '0'; else if ( b == -1 || !isPrintableChar(b) ) return negative ? -ret : ret; else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { return (int)nextLong(); } public double nextDouble() { return Double.parseDouble(next()); } } } import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; public class Main { public static void main(String[] args) { Main o = new Main(); o.solve(); } public void solve() { FastScanner sc = new FastScanner(System.in); int N = sc.nextInt(); int[] P = new int[N]; for (int i = 0; i < N; i++) { P[i] = sc.nextInt(); } int cnt = 0; int ei = 0; int oi = 1; StringBuilder ans = new StringBuilder(); while ( ei < N && oi < N ) { while ( ei < N && P[ei] % 2 != 0 ) ei += 2; while ( oi < N && P[oi] % 2 == 0 ) oi += 2; if ( ei >= N || oi >= N ) break; int i0 = 0; if ( ei < oi ) { for (int i = ei; i <= oi - 3; i += 2) { ans.append("B "); ans.append(i + 1); ans.append("\n"); swap(P, i, i + 2); cnt++; } i0 = oi - 1; } else { for (int i = oi; i <= ei - 3; i += 2) { ans.append("B "); ans.append(i + 1); ans.append("\n"); swap(P, i, i + 2); cnt++; } i0 = ei - 1; } ans.append("A "); ans.append(i0 + 1); ans.append("\n"); swap(P, i0, i0 + 1); cnt++; } for (int p = 0; p < 2; p++) { for (int i = p; i < N; i += 2) { for (int j = p + 2; j < N - (i - p); j += 2) { if ( P[j - 2] > P[j] ) { ans.append("B "); ans.append(j - 1); ans.append("\n"); swap(P, j - 2, j); cnt++; } } } } System.out.println(cnt); System.out.print(ans.toString()); } void swap(int[] P, int i, int j) { int tmp = P[i]; P[i] = P[j]; P[j] = tmp; } class Number implements Comparable<Number> { int val = 0; int ord = 0; Number(int val) { this.val = val; } @Override public int compareTo(Main.Number o) { return val < o.val ? -1 : 1; } } class FastScanner { private final InputStream in; private final byte[] buf = new byte[1024]; private int ptr = 0; private int buflen = 0; FastScanner( InputStream source ) { this.in = source; } private boolean hasNextByte() { if ( ptr < buflen ) return true; else { ptr = 0; try { buflen = in.read(buf); } catch (IOException e) { e.printStackTrace(); } if ( buflen <= 0 ) return false; } return true; } private int readByte() { if ( hasNextByte() ) return buf[ptr++]; else return -1; } private boolean isPrintableChar( int c ) { return 33 <= c && c <= 126; } private boolean isNumeric( int c ) { return '0' <= c && c <= '9'; } private void skipToNextPrintableChar() { while ( hasNextByte() && !isPrintableChar(buf[ptr]) ) ptr++; } public boolean hasNext() { skipToNextPrintableChar(); return hasNextByte(); } public String next() { if ( !hasNext() ) throw new NoSuchElementException(); StringBuilder ret = new StringBuilder(); int b = readByte(); while ( isPrintableChar(b) ) { ret.appendCodePoint(b); b = readByte(); } return ret.toString(); } public long nextLong() { if ( !hasNext() ) throw new NoSuchElementException(); long ret = 0; int b = readByte(); boolean negative = false; if ( b == '-' ) { negative = true; if ( hasNextByte() ) b = readByte(); } if ( !isNumeric(b) ) throw new NumberFormatException(); while ( true ) { if ( isNumeric(b) ) ret = ret * 10 + b - '0'; else if ( b == -1 || !isPrintableChar(b) ) return negative ? -ret : ret; else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { return (int)nextLong(); } public double nextDouble() { return Double.parseDouble(next()); } } }
ConDefects/ConDefects/Code/arc147_b/Java/34774631
condefects-java_data_2
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); final int n = Integer.parseInt(br.readLine()); final StringTokenizer st_a = new StringTokenizer(br.readLine()); br.close(); final long[] array_a = new long[n]; for (int i = 0; i < n; i++) { array_a[i] = Long.parseLong(st_a.nextToken()); } long g = 0; for (int i = 0; i < n - 1; i++) { g = gcd(array_a[i + 1] - array_a[i], g); } System.out.println(g != 1 ? 1 : 2); } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); final int n = Integer.parseInt(br.readLine()); final StringTokenizer st_a = new StringTokenizer(br.readLine()); br.close(); final long[] array_a = new long[n]; for (int i = 0; i < n; i++) { array_a[i] = Long.parseLong(st_a.nextToken()); } long g = 0; for (int i = 0; i < n - 1; i++) { g = gcd(Math.abs(array_a[i] - array_a[i + 1]), g); } System.out.println(g != 1 ? 1 : 2); } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } }
ConDefects/ConDefects/Code/arc148_a/Java/35625720
condefects-java_data_3
import java.util.*; import java.lang.*; import java.io.*; class Main{ public static int gcd(int a, int b){ if(b == 0){ return a; } return gcd(b, a % b); } public static void main (String[] args) throws java.lang.Exception{ 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 g = arr[1] - arr[0]; for(int i = 2; i < n; i++){ g = gcd(g, Math.abs(arr[i] - arr[i - 1])); } if(g == 1){ System.out.println(2); } else{ System.out.println(1); } } } import java.util.*; import java.lang.*; import java.io.*; class Main{ public static int gcd(int a, int b){ if(b == 0){ return a; } return gcd(b, a % b); } public static void main (String[] args) throws java.lang.Exception{ 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 g = Math.abs(arr[1] - arr[0]); for(int i = 2; i < n; i++){ g = gcd(g, Math.abs(arr[i] - arr[i - 1])); } if(g == 1){ System.out.println(2); } else{ System.out.println(1); } } }
ConDefects/ConDefects/Code/arc148_a/Java/34806342
condefects-java_data_4
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { static int MOD = (int) (1e9 + 7); public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; Reader in = new Reader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } static int add_self(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } static int sub_self(int i, int i1) { i -= i1; return i >= 0 ? i : i + MOD; } static int[][] dp; private static void solve(Reader in, PrintWriter out) throws IOException { int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int gcd = 0; for (int i = 0; i < n - 1; i++) { gcd = calcGCD(gcd, Math.abs(arr[i + 1] - arr[i])); } if(gcd== 0 || gcd > 1) { out.print(1); } else { out.print(2); } out.close(); } static int calcGCD(int a, int b) { if(a==0) return b; return calcGCD(b % a, b); } public static int[][] parents3(int[][] g, int root) { int n = g.length; int[] par = new int[n]; Arrays.fill(par, -1); int[] depth = new int[n]; int[] q = new int[n]; q[0] = root; for (int p = 0, r = 1; p < r; p++) { int cur = q[p]; for (int nex : g[cur]) { if (par[cur] != nex) { q[r++] = nex; par[nex] = cur; depth[nex] = depth[cur] + 1; } } } return new int[][]{par, q, depth}; } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int i : from) { p[i]++; } for (int i : to) { p[i]++; } for (int i = 0; i < n; i++) { g[i] = new int[p[i]]; } for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } static class Reader { BufferedReader br; StringTokenizer st; public Reader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair<T> { T a; T b; public Pair(T a, T b) { this.a = a; this.b = b; } T first() { return a; } T second() { return b; } } public static class SegmentTreeRMQL { public int M, H, N; public long[] st; public SegmentTreeRMQL(int n) { N = n; M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2; H = M >>> 1; st = new long[M]; Arrays.fill(st, 0, M, Long.MAX_VALUE); } public SegmentTreeRMQL(long[] a) { N = a.length; M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2; H = M >>> 1; st = new long[M]; for (int i = 0; i < N; i++) { st[H + i] = a[i]; } Arrays.fill(st, H + N, M, Long.MAX_VALUE); for (int i = H - 1; i >= 1; i--) propagate(i); } public void update(int pos, long x) { st[H + pos] = x; for (int i = (H + pos) >>> 1; i >= 1; i >>>= 1) propagate(i); } private void propagate(int i) { st[i] = Math.min(st[2 * i], st[2 * i + 1]); } public long minx(int l, int r) { long min = Long.MAX_VALUE / 2; if (l >= r) return min; while (l != 0) { int f = l & -l; if (l + f > r) break; long v = st[(H + l) / f]; if (v < min) min = v; l += f; } while (l < r) { int f = r & -r; long v = st[(H + r) / f - 1]; if (v < min) min = v; r -= f; } return min; } public long min(int l, int r) { return l >= r ? 0 : min(l, r, 0, H, 1); } private long min(int l, int r, int cl, int cr, int cur) { if (l <= cl && cr <= r) { return st[cur]; } else { int mid = cl + cr >>> 1; long ret = Long.MAX_VALUE; if (cl < r && l < mid) { ret = Math.min(ret, min(l, r, cl, mid, 2 * cur)); } if (mid < r && l < cr) { ret = Math.min(ret, min(l, r, mid, cr, 2 * cur + 1)); } return ret; } } public int firstle(int l, long v) { int cur = H + l; while (true) { if (st[cur] <= v) { if (cur < H) { cur = 2 * cur; } else { return cur - H; } } else { cur++; if ((cur & cur - 1) == 0) return -1; if ((cur & 1) == 0) cur >>>= 1; } } } public int lastle(int l, long v) { int cur = H + l; while (true) { if (st[cur] <= v) { if (cur < H) { cur = 2 * cur + 1; } else { return cur - H; } } else { if ((cur & cur - 1) == 0) return -1; cur--; if ((cur & 1) == 1) cur >>>= 1; } } } } } import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; public class Main { static int MOD = (int) (1e9 + 7); public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; Reader in = new Reader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(in, out); out.close(); } static int add_self(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } static int sub_self(int i, int i1) { i -= i1; return i >= 0 ? i : i + MOD; } static int[][] dp; private static void solve(Reader in, PrintWriter out) throws IOException { int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int gcd = 0; for (int i = 0; i < n - 1; i++) { gcd = calcGCD(gcd, Math.abs(arr[i + 1] - arr[i])); } if(gcd== 0 || gcd > 1) { out.print(1); } else { out.print(2); } out.close(); } static int calcGCD(int a, int b) { if(a==0) return b; return calcGCD(b % a, a); } public static int[][] parents3(int[][] g, int root) { int n = g.length; int[] par = new int[n]; Arrays.fill(par, -1); int[] depth = new int[n]; int[] q = new int[n]; q[0] = root; for (int p = 0, r = 1; p < r; p++) { int cur = q[p]; for (int nex : g[cur]) { if (par[cur] != nex) { q[r++] = nex; par[nex] = cur; depth[nex] = depth[cur] + 1; } } } return new int[][]{par, q, depth}; } static int[][] packU(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int i : from) { p[i]++; } for (int i : to) { p[i]++; } for (int i = 0; i < n; i++) { g[i] = new int[p[i]]; } for (int i = 0; i < from.length; i++) { g[from[i]][--p[from[i]]] = to[i]; g[to[i]][--p[to[i]]] = from[i]; } return g; } static class Reader { BufferedReader br; StringTokenizer st; public Reader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class Pair<T> { T a; T b; public Pair(T a, T b) { this.a = a; this.b = b; } T first() { return a; } T second() { return b; } } public static class SegmentTreeRMQL { public int M, H, N; public long[] st; public SegmentTreeRMQL(int n) { N = n; M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2; H = M >>> 1; st = new long[M]; Arrays.fill(st, 0, M, Long.MAX_VALUE); } public SegmentTreeRMQL(long[] a) { N = a.length; M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2; H = M >>> 1; st = new long[M]; for (int i = 0; i < N; i++) { st[H + i] = a[i]; } Arrays.fill(st, H + N, M, Long.MAX_VALUE); for (int i = H - 1; i >= 1; i--) propagate(i); } public void update(int pos, long x) { st[H + pos] = x; for (int i = (H + pos) >>> 1; i >= 1; i >>>= 1) propagate(i); } private void propagate(int i) { st[i] = Math.min(st[2 * i], st[2 * i + 1]); } public long minx(int l, int r) { long min = Long.MAX_VALUE / 2; if (l >= r) return min; while (l != 0) { int f = l & -l; if (l + f > r) break; long v = st[(H + l) / f]; if (v < min) min = v; l += f; } while (l < r) { int f = r & -r; long v = st[(H + r) / f - 1]; if (v < min) min = v; r -= f; } return min; } public long min(int l, int r) { return l >= r ? 0 : min(l, r, 0, H, 1); } private long min(int l, int r, int cl, int cr, int cur) { if (l <= cl && cr <= r) { return st[cur]; } else { int mid = cl + cr >>> 1; long ret = Long.MAX_VALUE; if (cl < r && l < mid) { ret = Math.min(ret, min(l, r, cl, mid, 2 * cur)); } if (mid < r && l < cr) { ret = Math.min(ret, min(l, r, mid, cr, 2 * cur + 1)); } return ret; } } public int firstle(int l, long v) { int cur = H + l; while (true) { if (st[cur] <= v) { if (cur < H) { cur = 2 * cur; } else { return cur - H; } } else { cur++; if ((cur & cur - 1) == 0) return -1; if ((cur & 1) == 0) cur >>>= 1; } } } public int lastle(int l, long v) { int cur = H + l; while (true) { if (st[cur] <= v) { if (cur < H) { cur = 2 * cur + 1; } else { return cur - H; } } else { if ((cur & cur - 1) == 0) return -1; cur--; if ((cur & 1) == 1) cur >>>= 1; } } } } }
ConDefects/ConDefects/Code/arc148_a/Java/34851302
condefects-java_data_5
import java.io.*; import java.util.*; import java.math.*; class Main{ static Library System = new Library(java.lang.System.in,java.lang.System.out); public static void main(String[] args)throws IOException{ int N = System.in.nextInt(); int[] A = System.in.nextInt(N); long gcd = Math.abs(A[1]-A[0]); for(int i=2;i<N;i++){ gcd = System.gcd(gcd,Math.abs(A[i]-A[i-1])); } System.out.println(gcd==1?1:2); System.out.close(); } } /*//////////////////////////////////////////////// * My Library * *///////////////////////////////////////////////// class Library{ SimpleScanner in = null; PrintWriter out = null; private long[] Factorials,InFactorials; public long mod = 0; public Library(InputStream in,OutputStream out){ this.in = new SimpleScanner(in); this.out = new PrintWriter(out); } public Library(InputStream in,OutputStream out,int fac,long mod){ this(in,out); Factorials=new long[fac+1]; Factorials[1]=1; for(int i=2;i<=fac;i++)Factorials[i]=Factorials[i-1]*i%mod; InFactorials=new long[fac+1]; InFactorials[fac]=modPow(Factorials[fac],mod-2,mod); for(int i=fac;i>1;i--)InFactorials[i-1]=InFactorials[i]*i%mod; this.mod = mod; } /** * コンストラクタで渡された値までの * num!、aCbをmodで割ったあまりを返します。 */ public long getFact(int num){ return Factorials[num]; } public long getCombi(int a,int b){ return (Factorials[a]*InFactorials[a-b]%mod)*InFactorials[b]%mod; } /** * ○N! * factではa!を、 * modFactでは引数のmodかコンストラクタで渡されたmodで * 割ったあまりを返します。 */ public static long fact(long a){ long ans=1; for(long i=2;i<=a;i++)ans*=i; return ans; } public static long modFact(long a,long mod){ long ans=1; for(long i=2;i<=a;i++){ ans*=i%mod; ans%=mod; } return ans; } public long modFact(long a){ if(mod==0){ System.err.println("\u001b[00;31m"+"#mod is not defined#"); System.exit(1); } long ans=1; for(long i=2;i<=a;i++){ ans*=i%mod; ans%=mod; } return ans; } /** * ○nCr * CombiではaCb、 * modCombiでは引数のmodかコンストラクタで渡されたmodで * 割ったあまりを返します。 */ public static long combi(long a,long b){ long ans=1; if(b==0||b==a)return 1; if(b<0||b>a)return 0; b=Math.min(a-b,b); for(long i=1;i<=b;i++){ ans*=a--; ans/=i; } return ans; } public static long modCombi(long a,long b,long mod){ long ans=1; if(b==0||b==a)return 1; if(b<0||b>a)return 0; b=Math.min(a-b,b); for(long i=1;i<=b;i++){ ans*=a--; ans%=mod; ans*=modPow(i,mod-2,mod); ans%=mod; } return ans; } public long modCombi(long a,long b){ if(mod==0){ System.err.println("\u001b[00;31m"+"#mod is not defined#"); System.exit(1); } long ans=1; if(b==0||b==a)return 1; if(b<0||b>a)return 0; b=Math.min(a-b,b); for(long i=1;i<=b;i++){ ans*=a--; ans%=mod; ans*=modPow(i,mod-2,mod); ans%=mod; } return ans; } /** * ○ヒープソート * int、long型配列をソートします。 * 二次元配列の場合は[i][0]と[i][1]の大きさで * 昇順に並べ替えます。 */ public static void sort(int[] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&list[(j-1)/2]<list[j]){ int temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ int temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&list[j]<list[2*j+1])||(2*j+2<i-1&&list[j]<list[2*j+2])){ if(2*j+2==i-1||list[2*j+1]>list[2*j+2]){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } public static void sort(int[][] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&(list[(j-1)/2][0]<list[j][0]||(list[(j-1)/2][0]==list[j][0]&&list[(j-1)/2][1]<list[j][1]))){ int[] temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ int[] temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&(list[j][0]<list[2*j+1][0]||(list[j][0]==list[2*j+1][0]&&list[j][1]<list[2*j+1][1])))|| (2*j+2<i-1&&(list[j][0]<list[2*j+2][0]||(list[j][0]==list[2*j+2][0]&&list[j][1]<list[2*j+2][1])))){ if(2*j+2==i-1||(list[2*j+1][0]>list[2*j+2][0]||(list[2*j+1][0]==list[2*j+2][0]&&list[2*j+1][1]>list[2*j+2][1]))){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } public static void sort(long[] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&list[(j-1)/2]<list[j]){ long temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ long temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&list[j]<list[2*j+1])||(2*j+2<i-1&&list[j]<list[2*j+2])){ if(2*j+2==i-1||list[2*j+1]>list[2*j+2]){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } public static void sort(long[][] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&(list[(j-1)/2][0]<list[j][0]||(list[(j-1)/2][0]==list[j][0]&&list[(j-1)/2][1]<list[j][1]))){ long[] temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ long[] temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&(list[j][0]<list[2*j+1][0]||(list[j][0]==list[2*j+1][0]&&list[j][1]<list[2*j+1][1])))|| (2*j+2<i-1&&(list[j][0]<list[2*j+2][0]||(list[j][0]==list[2*j+2][0]&&list[j][1]<list[2*j+2][1])))){ if(2*j+2==i-1||(list[2*j+1][0]>list[2*j+2][0]||(list[2*j+1][0]==list[2*j+2][0]&&list[2*j+1][1]>list[2*j+2][1]))){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } /** * ○カウントソート * int型配列をソートします。 * 最大値が小さい場合は有効です。 */ public static void countSort(int[] nums,int limit){ int[] list=new int[limit+1]; for(int i=0;i<nums.length;i++)list[nums[i]]++; int temp=0; for(int i=0;i<list.length;i++)for(int j=0;j<list[i];j++)nums[temp++]=i; } /** * ○ユークリッドの互除法 * gcdは最大公約数を、 * lcmは最小公倍数を返します。 * lcmはオーバーフローの可能性があるのでご注意を。 * 戻り値はlong型です。 */ public static long gcd(long a,long b){ if(a==0||b==0) return Math.max(a,b); long temp; while((temp=a%b)!=0){ a=b; b=temp; } return b; } public static long lcm(long a,long b){ long mult=a*b,temp; while((temp=a%b)!=0){ a=b; b=temp; } return mult/b; } /** * ○ミラーラビン素数判定法 * BigIntegerクラスのものを使用します。 * 素数である確率が高いならtrue、 * 確実に合成数ならfalseを返します。 * 誤判定の確率は1/2^20以下です。 */ public static boolean isPrime(long num){ return BigInteger.valueOf(num).isProbablePrime(20); } /** * ○エラトステネスの篩 * 引数まで(引数含む)の素数を返します。 * 2^30以上の素数列挙はできないのでご注意を。 */ public static int[] prime(int num){ BitSet nums=new BitSet(num+1); nums.set(2,num+1); for(int i=2;i<=Math.sqrt(num);i++)if(nums.get(i))for(int j=i*i;j<=num;j+=i)nums.clear(j); int[] answer=new int[nums.cardinality()]; int i=2,index=0; while(true){ i=nums.nextSetBit(i); answer[index++]=i++; if(index==answer.length)break; } return answer; } /** * ○繰り返し二乗法 * powではそのままの結果を、 * modPowでは引数のmodかコンストラクタで渡されたmodで * 割ったあまりを返します。 */ public static long pow(long a,long b){ long ans=1; while(b>0){ if((b&1)==1)ans*=a; a*=a; b>>=1; } return ans; } public static long modPow(long a,long b,long mod){ long ans=1; a%=mod; while(b>0){ if((b&1)==1)ans*=a; ans%=mod; a*=a; a%=mod; b>>=1; } return ans; } public long modPow(long a,long b){ if(mod==0){ System.err.println("\u001b[00;31m"+"#mod is not defined#"); System.exit(1); } long ans=1; a%=mod; while(b>0){ if((b&1)==1)ans*=a; ans%=mod; a*=a; a%=mod; b>>=1; } return ans; } /** * ○文字列→整数変換 * Stringをint、longに変換します。 * 数値以外の文字であっても無理矢理数値変換してしまうので * 使用の際はご注意を。 */ public static int parseInt(String str){ char[] nums=str.toCharArray(); int ans=0; boolean plus=true; if(nums[0]=='-'){ plus=!plus; nums[0]='0'; } for(int i=0;i<nums.length;i++)ans=ans*10+nums[i]-'0'; return plus?ans:-ans; } public static long parseLong(String str){ char[] nums=str.toCharArray(); long ans=0; boolean plus=true; if(nums[0]=='-'){ plus=!plus; nums[0]='0'; } for(int i=0;i<nums.length;i++)ans=ans*10+nums[i]-'0'; return plus?ans:-ans; } /** * ○二分探索 * downsearchではnum以下を、 * upsearchではnum以上を探します。 * 引数の配列(List)のインデックスを返します。 */ public static int downsearch(int[] nums,int num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(int[] nums,int num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(long[] nums,long num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(long[] nums,long num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(Integer[] nums,int num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(Integer[] nums,int num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(Long[] nums,long num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(Long[] nums,long num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(ArrayList<Integer> nums,int num){ int a=0,b=nums.size()-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(ArrayList<Integer> nums,int num){ int a=0,b=nums.size()-1,ans=nums.size(),c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(ArrayList<Long> nums,long num){ int a=0,b=nums.size()-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(ArrayList<Long> nums,long num){ int a=0,b=nums.size()-1,ans=nums.size(),c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>=num)b=(ans=c)-1; else a=c+1; } return ans; } public void exit(int num){ try{ in.close(); out.close(); }catch(Exception e){ }finally{ System.exit(num); } } @Override public String toString(){ StringBuilder answer=new StringBuilder(); if(Factorials!=null)answer.append("Factorials are defined. (You can use 0~"+(Factorials.length-1)+" for index)"); else answer.append("Factorial is not defined."); answer.append("\n"); if(InFactorials!=null)answer.append("InFactorials are defined. (You can use 0~"+(InFactorials.length-1)+" for index)"); else answer.append("InFactorial is not defined."); answer.append("\n"); if(mod!=0)answer.append("mod is "+mod); else answer.append("mod is not defined."); return answer.toString(); } } /*//////////////////////////////////////////////// * 入力用 * *///////////////////////////////////////////////// class SimpleScanner{ final private int buff_size = 1<<12; private InputStream is; private byte[] buff; private int point,length; public SimpleScanner(InputStream is){ this.is = is; buff = new byte[buff_size]; point = length = 0; } private void reload()throws IOException{ do{ length = is.read(buff,point = 0,buff_size); }while(length==-1); } private byte read()throws IOException{ if(point==length)reload(); return buff[point++]; } public byte nextByte()throws IOException{ byte c = read(); while(c<=' ')c = read(); return c; } public int nextInt()throws IOException{ int ans = 0; byte c = read(); while(c<=' ')c = read(); boolean negate = c == '-'; if(c=='-')c = read(); while('0'<=c&&c<='9'){ ans = ans*10+c-'0'; c = read(); } return negate ? -ans : ans; } public long nextLong()throws IOException{ long ans = 0; byte c = read(); while(c<=' ')c = read(); boolean negate = c == '-'; if(c=='-')c = read(); while('0'<=c&&c<='9'){ ans = ans*10+c-'0'; c = read(); } return negate ? -ans : ans; } public char nextChar()throws IOException{ byte c = read(); while(c<=' ')c = read(); return (char)c; } public String next()throws IOException{ StringBuilder ans = new StringBuilder(); byte c = read(); while(c<=' ')c = read(); while(c>' '){ ans.append((char)c); c = read(); } return ans.toString(); } public byte[] nextByte(int n)throws IOException{ byte[] ans = new byte[n]; for(int i=0;i<n;i++){ ans[i] = nextByte(); } return ans; } public int[] nextInt(int n)throws IOException{ int[] ans = new int[n]; for(int i=0;i<n;i++){ ans[i] = nextInt(); } return ans; } public long[] nextLong(int n)throws IOException{ long[] ans = new long[n]; for(int i=0;i<n;i++){ ans[i] = nextLong(); } return ans; } public String[] next(int n)throws IOException{ String[] ans = new String[n]; for(int i=0;i<n;i++){ ans[i] = next(); } return ans; } public char[] nextCharArray()throws IOException{ return next().toCharArray(); } public void close()throws IOException{ is.close(); } } import java.io.*; import java.util.*; import java.math.*; class Main{ static Library System = new Library(java.lang.System.in,java.lang.System.out); public static void main(String[] args)throws IOException{ int N = System.in.nextInt(); int[] A = System.in.nextInt(N); long gcd = Math.abs(A[1]-A[0]); for(int i=2;i<N;i++){ gcd = System.gcd(gcd,Math.abs(A[i]-A[i-1])); } System.out.println(gcd==1?2:1); System.out.close(); } } /*//////////////////////////////////////////////// * My Library * *///////////////////////////////////////////////// class Library{ SimpleScanner in = null; PrintWriter out = null; private long[] Factorials,InFactorials; public long mod = 0; public Library(InputStream in,OutputStream out){ this.in = new SimpleScanner(in); this.out = new PrintWriter(out); } public Library(InputStream in,OutputStream out,int fac,long mod){ this(in,out); Factorials=new long[fac+1]; Factorials[1]=1; for(int i=2;i<=fac;i++)Factorials[i]=Factorials[i-1]*i%mod; InFactorials=new long[fac+1]; InFactorials[fac]=modPow(Factorials[fac],mod-2,mod); for(int i=fac;i>1;i--)InFactorials[i-1]=InFactorials[i]*i%mod; this.mod = mod; } /** * コンストラクタで渡された値までの * num!、aCbをmodで割ったあまりを返します。 */ public long getFact(int num){ return Factorials[num]; } public long getCombi(int a,int b){ return (Factorials[a]*InFactorials[a-b]%mod)*InFactorials[b]%mod; } /** * ○N! * factではa!を、 * modFactでは引数のmodかコンストラクタで渡されたmodで * 割ったあまりを返します。 */ public static long fact(long a){ long ans=1; for(long i=2;i<=a;i++)ans*=i; return ans; } public static long modFact(long a,long mod){ long ans=1; for(long i=2;i<=a;i++){ ans*=i%mod; ans%=mod; } return ans; } public long modFact(long a){ if(mod==0){ System.err.println("\u001b[00;31m"+"#mod is not defined#"); System.exit(1); } long ans=1; for(long i=2;i<=a;i++){ ans*=i%mod; ans%=mod; } return ans; } /** * ○nCr * CombiではaCb、 * modCombiでは引数のmodかコンストラクタで渡されたmodで * 割ったあまりを返します。 */ public static long combi(long a,long b){ long ans=1; if(b==0||b==a)return 1; if(b<0||b>a)return 0; b=Math.min(a-b,b); for(long i=1;i<=b;i++){ ans*=a--; ans/=i; } return ans; } public static long modCombi(long a,long b,long mod){ long ans=1; if(b==0||b==a)return 1; if(b<0||b>a)return 0; b=Math.min(a-b,b); for(long i=1;i<=b;i++){ ans*=a--; ans%=mod; ans*=modPow(i,mod-2,mod); ans%=mod; } return ans; } public long modCombi(long a,long b){ if(mod==0){ System.err.println("\u001b[00;31m"+"#mod is not defined#"); System.exit(1); } long ans=1; if(b==0||b==a)return 1; if(b<0||b>a)return 0; b=Math.min(a-b,b); for(long i=1;i<=b;i++){ ans*=a--; ans%=mod; ans*=modPow(i,mod-2,mod); ans%=mod; } return ans; } /** * ○ヒープソート * int、long型配列をソートします。 * 二次元配列の場合は[i][0]と[i][1]の大きさで * 昇順に並べ替えます。 */ public static void sort(int[] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&list[(j-1)/2]<list[j]){ int temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ int temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&list[j]<list[2*j+1])||(2*j+2<i-1&&list[j]<list[2*j+2])){ if(2*j+2==i-1||list[2*j+1]>list[2*j+2]){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } public static void sort(int[][] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&(list[(j-1)/2][0]<list[j][0]||(list[(j-1)/2][0]==list[j][0]&&list[(j-1)/2][1]<list[j][1]))){ int[] temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ int[] temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&(list[j][0]<list[2*j+1][0]||(list[j][0]==list[2*j+1][0]&&list[j][1]<list[2*j+1][1])))|| (2*j+2<i-1&&(list[j][0]<list[2*j+2][0]||(list[j][0]==list[2*j+2][0]&&list[j][1]<list[2*j+2][1])))){ if(2*j+2==i-1||(list[2*j+1][0]>list[2*j+2][0]||(list[2*j+1][0]==list[2*j+2][0]&&list[2*j+1][1]>list[2*j+2][1]))){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } public static void sort(long[] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&list[(j-1)/2]<list[j]){ long temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ long temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&list[j]<list[2*j+1])||(2*j+2<i-1&&list[j]<list[2*j+2])){ if(2*j+2==i-1||list[2*j+1]>list[2*j+2]){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } public static void sort(long[][] list){ for(int i=0;i<list.length;i++){ int j=i; while(j>0&&(list[(j-1)/2][0]<list[j][0]||(list[(j-1)/2][0]==list[j][0]&&list[(j-1)/2][1]<list[j][1]))){ long[] temp=list[(j-1)/2]; list[(j-1)/2]=list[j]; list[j]=temp; j=(j-1)/2; } } for(int i=list.length;i>0;i--){ long[] temp=list[i-1]; list[i-1]=list[0]; list[0]=temp; int j=0; while((2*j+1<i-1&&(list[j][0]<list[2*j+1][0]||(list[j][0]==list[2*j+1][0]&&list[j][1]<list[2*j+1][1])))|| (2*j+2<i-1&&(list[j][0]<list[2*j+2][0]||(list[j][0]==list[2*j+2][0]&&list[j][1]<list[2*j+2][1])))){ if(2*j+2==i-1||(list[2*j+1][0]>list[2*j+2][0]||(list[2*j+1][0]==list[2*j+2][0]&&list[2*j+1][1]>list[2*j+2][1]))){ temp=list[2*j+1]; list[2*j+1]=list[j]; list[j]=temp; j<<=1; j++; } else{ temp=list[2*j+2]; list[2*j+2]=list[j]; list[j]=temp; j<<=1; j+=2; } } } } /** * ○カウントソート * int型配列をソートします。 * 最大値が小さい場合は有効です。 */ public static void countSort(int[] nums,int limit){ int[] list=new int[limit+1]; for(int i=0;i<nums.length;i++)list[nums[i]]++; int temp=0; for(int i=0;i<list.length;i++)for(int j=0;j<list[i];j++)nums[temp++]=i; } /** * ○ユークリッドの互除法 * gcdは最大公約数を、 * lcmは最小公倍数を返します。 * lcmはオーバーフローの可能性があるのでご注意を。 * 戻り値はlong型です。 */ public static long gcd(long a,long b){ if(a==0||b==0) return Math.max(a,b); long temp; while((temp=a%b)!=0){ a=b; b=temp; } return b; } public static long lcm(long a,long b){ long mult=a*b,temp; while((temp=a%b)!=0){ a=b; b=temp; } return mult/b; } /** * ○ミラーラビン素数判定法 * BigIntegerクラスのものを使用します。 * 素数である確率が高いならtrue、 * 確実に合成数ならfalseを返します。 * 誤判定の確率は1/2^20以下です。 */ public static boolean isPrime(long num){ return BigInteger.valueOf(num).isProbablePrime(20); } /** * ○エラトステネスの篩 * 引数まで(引数含む)の素数を返します。 * 2^30以上の素数列挙はできないのでご注意を。 */ public static int[] prime(int num){ BitSet nums=new BitSet(num+1); nums.set(2,num+1); for(int i=2;i<=Math.sqrt(num);i++)if(nums.get(i))for(int j=i*i;j<=num;j+=i)nums.clear(j); int[] answer=new int[nums.cardinality()]; int i=2,index=0; while(true){ i=nums.nextSetBit(i); answer[index++]=i++; if(index==answer.length)break; } return answer; } /** * ○繰り返し二乗法 * powではそのままの結果を、 * modPowでは引数のmodかコンストラクタで渡されたmodで * 割ったあまりを返します。 */ public static long pow(long a,long b){ long ans=1; while(b>0){ if((b&1)==1)ans*=a; a*=a; b>>=1; } return ans; } public static long modPow(long a,long b,long mod){ long ans=1; a%=mod; while(b>0){ if((b&1)==1)ans*=a; ans%=mod; a*=a; a%=mod; b>>=1; } return ans; } public long modPow(long a,long b){ if(mod==0){ System.err.println("\u001b[00;31m"+"#mod is not defined#"); System.exit(1); } long ans=1; a%=mod; while(b>0){ if((b&1)==1)ans*=a; ans%=mod; a*=a; a%=mod; b>>=1; } return ans; } /** * ○文字列→整数変換 * Stringをint、longに変換します。 * 数値以外の文字であっても無理矢理数値変換してしまうので * 使用の際はご注意を。 */ public static int parseInt(String str){ char[] nums=str.toCharArray(); int ans=0; boolean plus=true; if(nums[0]=='-'){ plus=!plus; nums[0]='0'; } for(int i=0;i<nums.length;i++)ans=ans*10+nums[i]-'0'; return plus?ans:-ans; } public static long parseLong(String str){ char[] nums=str.toCharArray(); long ans=0; boolean plus=true; if(nums[0]=='-'){ plus=!plus; nums[0]='0'; } for(int i=0;i<nums.length;i++)ans=ans*10+nums[i]-'0'; return plus?ans:-ans; } /** * ○二分探索 * downsearchではnum以下を、 * upsearchではnum以上を探します。 * 引数の配列(List)のインデックスを返します。 */ public static int downsearch(int[] nums,int num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(int[] nums,int num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(long[] nums,long num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(long[] nums,long num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(Integer[] nums,int num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(Integer[] nums,int num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(Long[] nums,long num){ int a=0,b=nums.length-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(Long[] nums,long num){ int a=0,b=nums.length-1,ans=nums.length,c=-1; while(a-b<1){ c=(a+b)/2; if(nums[c]>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(ArrayList<Integer> nums,int num){ int a=0,b=nums.size()-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(ArrayList<Integer> nums,int num){ int a=0,b=nums.size()-1,ans=nums.size(),c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>=num)b=(ans=c)-1; else a=c+1; } return ans; } public static int downsearch(ArrayList<Long> nums,long num){ int a=0,b=nums.size()-1,ans=-1,c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>num)b=c-1; else a=(ans=c)+1; } return ans; } public static int upsearch(ArrayList<Long> nums,long num){ int a=0,b=nums.size()-1,ans=nums.size(),c=-1; while(a-b<1){ c=(a+b)/2; if(nums.get(c)>=num)b=(ans=c)-1; else a=c+1; } return ans; } public void exit(int num){ try{ in.close(); out.close(); }catch(Exception e){ }finally{ System.exit(num); } } @Override public String toString(){ StringBuilder answer=new StringBuilder(); if(Factorials!=null)answer.append("Factorials are defined. (You can use 0~"+(Factorials.length-1)+" for index)"); else answer.append("Factorial is not defined."); answer.append("\n"); if(InFactorials!=null)answer.append("InFactorials are defined. (You can use 0~"+(InFactorials.length-1)+" for index)"); else answer.append("InFactorial is not defined."); answer.append("\n"); if(mod!=0)answer.append("mod is "+mod); else answer.append("mod is not defined."); return answer.toString(); } } /*//////////////////////////////////////////////// * 入力用 * *///////////////////////////////////////////////// class SimpleScanner{ final private int buff_size = 1<<12; private InputStream is; private byte[] buff; private int point,length; public SimpleScanner(InputStream is){ this.is = is; buff = new byte[buff_size]; point = length = 0; } private void reload()throws IOException{ do{ length = is.read(buff,point = 0,buff_size); }while(length==-1); } private byte read()throws IOException{ if(point==length)reload(); return buff[point++]; } public byte nextByte()throws IOException{ byte c = read(); while(c<=' ')c = read(); return c; } public int nextInt()throws IOException{ int ans = 0; byte c = read(); while(c<=' ')c = read(); boolean negate = c == '-'; if(c=='-')c = read(); while('0'<=c&&c<='9'){ ans = ans*10+c-'0'; c = read(); } return negate ? -ans : ans; } public long nextLong()throws IOException{ long ans = 0; byte c = read(); while(c<=' ')c = read(); boolean negate = c == '-'; if(c=='-')c = read(); while('0'<=c&&c<='9'){ ans = ans*10+c-'0'; c = read(); } return negate ? -ans : ans; } public char nextChar()throws IOException{ byte c = read(); while(c<=' ')c = read(); return (char)c; } public String next()throws IOException{ StringBuilder ans = new StringBuilder(); byte c = read(); while(c<=' ')c = read(); while(c>' '){ ans.append((char)c); c = read(); } return ans.toString(); } public byte[] nextByte(int n)throws IOException{ byte[] ans = new byte[n]; for(int i=0;i<n;i++){ ans[i] = nextByte(); } return ans; } public int[] nextInt(int n)throws IOException{ int[] ans = new int[n]; for(int i=0;i<n;i++){ ans[i] = nextInt(); } return ans; } public long[] nextLong(int n)throws IOException{ long[] ans = new long[n]; for(int i=0;i<n;i++){ ans[i] = nextLong(); } return ans; } public String[] next(int n)throws IOException{ String[] ans = new String[n]; for(int i=0;i<n;i++){ ans[i] = next(); } return ans; } public char[] nextCharArray()throws IOException{ return next().toCharArray(); } public void close()throws IOException{ is.close(); } }
ConDefects/ConDefects/Code/arc148_a/Java/34827248
condefects-java_data_6
import java.util.Scanner; public class Main { public static void main( String[] args) { Scanner scn = new Scanner( System.in); int N = scn.nextInt(); int[] array = new int[N]; for ( int i = 0; i < N; i++) { array[i] = scn.nextInt(); } int[] difArray = new int[N - 1]; for ( int j = 0; j < N - 1; j++) { difArray[j] = array[j + 1] - array[j]; } int firstDif = difArray[0]; for ( int dif : difArray) { while ( firstDif > 1) { if ( dif % firstDif == 0) { break; } int tmp = dif % firstDif; dif = firstDif; firstDif = tmp; } if ( firstDif == 1) { System.out.println( 2); return; } } System.out.println( 1); } } import java.util.Arrays; import java.util.Scanner; public class Main { public static void main( String[] args) { Scanner scn = new Scanner( System.in); int N = scn.nextInt(); int[] array = new int[N]; for ( int i = 0; i < N; i++) { array[i] = scn.nextInt(); } Arrays.parallelSort( array); int[] difArray = new int[N - 1]; for ( int j = 0; j < N - 1; j++) { difArray[j] = array[j + 1] - array[j]; } int firstDif = difArray[0]; for ( int dif : difArray) { while ( firstDif > 1) { if ( dif % firstDif == 0) { break; } int tmp = dif % firstDif; dif = firstDif; firstDif = tmp; } if ( firstDif == 1) { System.out.println( 2); return; } } System.out.println( 1); } }
ConDefects/ConDefects/Code/arc148_a/Java/40885914
condefects-java_data_7
import java.io.*; import java.util.*; import java.util.function.*; class Solver { private static final long INF = Long.MAX_VALUE; private final int n; private final long m; private final long[] ds; private ModCalculator mc; private long[] ghTable; private long[] cache; private long gcd(long a, long b) { return a % b == 0 ? b : gcd(b, a % b); } private long lcm(long a, long b) { if (a == INF || b == INF) { return INF; } long g = gcd(a, b); a /= g; double ad = a; double bd = b; if (ad * bd > 2 * m) { return INF; } long candidate = a * b; if (candidate > m) { return INF; } return candidate; } private long gh(int bitset) { long lcmValue = 1; for (int i = 0; i < n; i++) { if (((bitset >> i) & 1) == 1) { lcmValue = lcm(lcmValue, ds[i]); } } long v = Integer.bitCount(bitset) % 2 == 1 ? 1 : mc.norm(-1); v = mc.mul(v, mc.getF(Integer.bitCount(bitset) - 1)); v = mc.mul(v, m / lcmValue); return v; } private long solve(int bitset) { if (bitset == 0) { return 1; } if (cache[bitset] >= 0) { return cache[bitset]; } cache[bitset] = 0; for (int subBitset = bitset; subBitset > 0; subBitset = (subBitset - 1) & bitset) { if ((subBitset & Integer.lowestOneBit(bitset)) != 0) { long v = mc.mul(ghTable[subBitset], solve(bitset - subBitset)); cache[bitset] = mc.add(cache[bitset], v); } } return cache[bitset]; } public Solver(int n, long m, long[] ds) { this.n = n; this.m = m; this.ds = ds; } public long solve() { mc = new ModCalculator(998244353); ghTable = new long[1 << n]; for (int bitset = 1; bitset < (1 << n); bitset++) { ghTable[bitset] = gh(bitset); } cache = new long[1 << n]; Arrays.fill(cache, -1); return solve((1 << n) - 1); } }; class ModCalculator { private final long mod; private final ModInverseCache modInverseCache; private final ModCombinationCache modCombinationCache; ModCalculator(long mod) { this.mod = mod; this.modInverseCache = new ModInverseCache(); this.modCombinationCache = new ModCombinationCache(); } public long norm(long v) { long nogmalized = v % mod; if (nogmalized < 0) { nogmalized += mod; } return nogmalized; } public long add(long a, long b) { return norm(a + b); } public long sub(long a, long b) { return norm(a - b + mod); } public long mul(long a, long b) { return norm(a * b); } public long pow(long a, long b) { if (b == 0) { return 1; } long v = pow(mul(a, a), b / 2); if (b % 2 == 1) { return mul(v, a); } else { return v; } } public long inverse(long a) { return pow(a, mod - 2); } public long inverseFromCache(int a) { return modInverseCache.get(a); } public long div(long a, long b) { return mul(a, inverse(b)); } // Verify ARC 042 D // https://atcoder.jp/contests/arc042/tasks/arc042_d // a^x mod p === b // return -1 there is no such positive x public long log(long a, long b) { Map<Long, Long> map = new HashMap<>(); long powA = 1; long rootP = 0; while (true) { if (powA == b && rootP != 0) { return rootP; } if (map.containsKey(powA)) { return -1; } map.put(powA, rootP); powA = mul(powA, a); rootP++; if (rootP * rootP > mod) { break; } } long inversePowA = inverse(powA); for (int i = 1; i <= rootP; i++) { b = mul(b, inversePowA); Long value = map.get(b); if (value != null && value + rootP * i > 0) { return value + rootP * i; } } return -1; } public long getF(int n) { return modCombinationCache.getF(n); } public long getP(int n, int r) { return modCombinationCache.getP(n, r); } public long getC(int n, int k) { return modCombinationCache.getC(n, k); } // Verify ttpc2019 J // https://atcoder.jp/contests/ttpc2019/tasks/ttpc2019_j class PrimitiveLongList { long[] values; int size; public PrimitiveLongList() { values = new long[10]; } private void resize() { long[] newValues = new long[values.length * 2]; System.arraycopy(values, 0, newValues, 0, values.length); values = newValues; } public void add(long value) { if (size >= values.length) { resize(); } values[size] = value; size++; } private void validateIndex(int index) { if (index < 0 || size <= index) { throw new IndexOutOfBoundsException( String.format("size: %d, index: %d", size, index)); } } public long get(int index) { validateIndex(index); return values[index]; } public void set(int index, long value) { validateIndex(index); values[index] = value; } public int size() { return size; } } // Verify AGC 040 C // https://atcoder.jp/contests/agc040/tasks/agc040_c class ModInverseCache { private final PrimitiveLongList inverseCache; public ModInverseCache() { inverseCache = new PrimitiveLongList(); inverseCache.add(0L); inverseCache.add(1L); } private void resize(int n) { for (int i = inverseCache.size(); i <= n; i++) { long k = mod / i; int r = (int)(mod % i); long inverse = mul(-k, inverseCache.get(r)); inverseCache.add(inverse); } } long get(int n) { resize(n); return inverseCache.get(n); } } class ModCombinationCache { private final PrimitiveLongList factorialCache; private final PrimitiveLongList factorialInverseCache; public ModCombinationCache() { factorialCache = new PrimitiveLongList(); factorialCache.add(1L); factorialInverseCache = new PrimitiveLongList(); factorialInverseCache.add(1L); } private void resize(int n) { for (int i = factorialCache.size() - 1; i < n; i++) { factorialCache.add(mul(factorialCache.get(i), i + 1)); factorialInverseCache.add( mul(factorialInverseCache.get(i), modInverseCache.get(i + 1))); } } long getF(int n) { resize(n); return factorialCache.get(n); } long getP(int n, int r) { resize(n); return mul(factorialCache.get(n), factorialInverseCache.get(n - r)); } long getC(int n, int k) { resize(n); return mul(factorialCache.get(n), mul(factorialInverseCache.get(k), factorialInverseCache.get(n - k))); } } } public class Main { private static void execute(ContestReader reader, ContestWriter out) { int n = reader.nextInt(); long m = reader.nextLong(); long[] ds = reader.nextLong(n); out.println(new Solver(n, m, ds).solve()); } public static void main(String[] args) { ContestReader reader = new ContestReader(System.in); ContestWriter out = new ContestWriter(System.out); execute(reader, out); out.flush(); } } class ContestWriter extends PrintWriter { ContestWriter(PrintStream printStream) { super(printStream); } public void printList(List<? extends Object> list) { for (Object object : list) { println(object); } } public void printListOneLine(List<? extends Object> list) { List<String> stringList = new ArrayList<>(); for (Object object : list) { stringList.add(object.toString()); } println(String.join(" ", stringList)); } } class ContestReader { private static final int BUFFER_SIZE = 1024; private final InputStream stream; private final byte[] buffer; private int pointer; private int bufferLength; ContestReader(InputStream stream) { this.stream = stream; this.buffer = new byte[BUFFER_SIZE]; this.pointer = 0; this.bufferLength = 0; } private boolean hasNextByte() { if (pointer < bufferLength) { return true; } pointer = 0; try { bufferLength = stream.read(buffer); } catch (IOException e) { throw new RuntimeException(e); } return bufferLength > 0; } private int readByte() { if (hasNextByte()) { return buffer[pointer++]; } else { return -1; } } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[pointer])) { pointer++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); while(true) { int b = readByte(); if (!isPrintableChar(b)) { break; } sb.appendCodePoint(b); } return sb.toString(); } public String nextLine() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); while(true) { int b = readByte(); if (!isPrintableChar(b) && b != 0x20) { break; } sb.appendCodePoint(b); } return sb.toString(); } public char nextChar() { return next().charAt(0); } public int nextInt() { if (!hasNext()) { throw new NoSuchElementException(); } int n = 0; boolean minus = false; { int b = readByte(); if (b == '-') { minus = true; } else if ('0' <= b && b <= '9') { n = b - '0'; } else { throw new NumberFormatException(); } } while(true){ int b = readByte(); if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } } } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; { int b = readByte(); if (b == '-') { minus = true; } else if ('0' <= b && b <= '9') { n = b - '0'; } else { throw new NumberFormatException(); } } while(true){ int b = readByte(); if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } } } public double nextDouble() { return Double.parseDouble(next()); } public String[] next(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } return array; } public String[] nextLine(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = nextLine(); } return array; } public char[] nextChar(int n) { char[] array = new char[n]; for (int i = 0; i < n; i++) { array[i] = nextChar(); } return array; } public int[] nextInt(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextLong(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public double[] nextDouble(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } public char[] nextCharArray() { return next().toCharArray(); } public String[][] next(int n, int m) { String[][] matrix = new String[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = next(); } } return matrix; } public int[][] nextInt(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextInt(); } } return matrix; } public char[][] nextChar(int n, int m) { char[][] matrix = new char[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextChar(); } } return matrix; } public long[][] nextLong(int n, int m) { long[][] matrix = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextLong(); } } return matrix; } public double[][] nextDouble(int n, int m) { double[][] matrix = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextDouble(); } } return matrix; } public char[][] nextCharArray(int n) { char[][] matrix = new char[n][]; for (int i = 0; i < n; i++) { matrix[i] = next().toCharArray(); } return matrix; } } class MyAssert { public static void myAssert(boolean flag, String message) { if (!flag) { throw new RuntimeException(message); } } public static void myAssert(boolean flag) { myAssert(flag, ""); } } import java.io.*; import java.util.*; import java.util.function.*; class Solver { private static final long INF = Long.MAX_VALUE; private final int n; private final long m; private final long[] ds; private ModCalculator mc; private long[] ghTable; private long[] cache; private long gcd(long a, long b) { return a % b == 0 ? b : gcd(b, a % b); } private long lcm(long a, long b) { if (a == INF || b == INF) { return INF; } long g = gcd(a, b); a /= g; double ad = a; double bd = b; if (ad * bd > 2 * m) { return INF; } long candidate = a * b; if (candidate > m) { return INF; } return candidate; } private long gh(int bitset) { long lcmValue = 1; for (int i = 0; i < n; i++) { if (((bitset >> i) & 1) == 1) { lcmValue = lcm(lcmValue, ds[i]); } } long v = Integer.bitCount(bitset) % 2 == 1 ? 1 : mc.norm(-1); v = mc.mul(v, mc.getF(Integer.bitCount(bitset) - 1)); v = mc.mul(v, mc.norm(m / lcmValue)); return v; } private long solve(int bitset) { if (bitset == 0) { return 1; } if (cache[bitset] >= 0) { return cache[bitset]; } cache[bitset] = 0; for (int subBitset = bitset; subBitset > 0; subBitset = (subBitset - 1) & bitset) { if ((subBitset & Integer.lowestOneBit(bitset)) != 0) { long v = mc.mul(ghTable[subBitset], solve(bitset - subBitset)); cache[bitset] = mc.add(cache[bitset], v); } } return cache[bitset]; } public Solver(int n, long m, long[] ds) { this.n = n; this.m = m; this.ds = ds; } public long solve() { mc = new ModCalculator(998244353); ghTable = new long[1 << n]; for (int bitset = 1; bitset < (1 << n); bitset++) { ghTable[bitset] = gh(bitset); } cache = new long[1 << n]; Arrays.fill(cache, -1); return solve((1 << n) - 1); } }; class ModCalculator { private final long mod; private final ModInverseCache modInverseCache; private final ModCombinationCache modCombinationCache; ModCalculator(long mod) { this.mod = mod; this.modInverseCache = new ModInverseCache(); this.modCombinationCache = new ModCombinationCache(); } public long norm(long v) { long nogmalized = v % mod; if (nogmalized < 0) { nogmalized += mod; } return nogmalized; } public long add(long a, long b) { return norm(a + b); } public long sub(long a, long b) { return norm(a - b + mod); } public long mul(long a, long b) { return norm(a * b); } public long pow(long a, long b) { if (b == 0) { return 1; } long v = pow(mul(a, a), b / 2); if (b % 2 == 1) { return mul(v, a); } else { return v; } } public long inverse(long a) { return pow(a, mod - 2); } public long inverseFromCache(int a) { return modInverseCache.get(a); } public long div(long a, long b) { return mul(a, inverse(b)); } // Verify ARC 042 D // https://atcoder.jp/contests/arc042/tasks/arc042_d // a^x mod p === b // return -1 there is no such positive x public long log(long a, long b) { Map<Long, Long> map = new HashMap<>(); long powA = 1; long rootP = 0; while (true) { if (powA == b && rootP != 0) { return rootP; } if (map.containsKey(powA)) { return -1; } map.put(powA, rootP); powA = mul(powA, a); rootP++; if (rootP * rootP > mod) { break; } } long inversePowA = inverse(powA); for (int i = 1; i <= rootP; i++) { b = mul(b, inversePowA); Long value = map.get(b); if (value != null && value + rootP * i > 0) { return value + rootP * i; } } return -1; } public long getF(int n) { return modCombinationCache.getF(n); } public long getP(int n, int r) { return modCombinationCache.getP(n, r); } public long getC(int n, int k) { return modCombinationCache.getC(n, k); } // Verify ttpc2019 J // https://atcoder.jp/contests/ttpc2019/tasks/ttpc2019_j class PrimitiveLongList { long[] values; int size; public PrimitiveLongList() { values = new long[10]; } private void resize() { long[] newValues = new long[values.length * 2]; System.arraycopy(values, 0, newValues, 0, values.length); values = newValues; } public void add(long value) { if (size >= values.length) { resize(); } values[size] = value; size++; } private void validateIndex(int index) { if (index < 0 || size <= index) { throw new IndexOutOfBoundsException( String.format("size: %d, index: %d", size, index)); } } public long get(int index) { validateIndex(index); return values[index]; } public void set(int index, long value) { validateIndex(index); values[index] = value; } public int size() { return size; } } // Verify AGC 040 C // https://atcoder.jp/contests/agc040/tasks/agc040_c class ModInverseCache { private final PrimitiveLongList inverseCache; public ModInverseCache() { inverseCache = new PrimitiveLongList(); inverseCache.add(0L); inverseCache.add(1L); } private void resize(int n) { for (int i = inverseCache.size(); i <= n; i++) { long k = mod / i; int r = (int)(mod % i); long inverse = mul(-k, inverseCache.get(r)); inverseCache.add(inverse); } } long get(int n) { resize(n); return inverseCache.get(n); } } class ModCombinationCache { private final PrimitiveLongList factorialCache; private final PrimitiveLongList factorialInverseCache; public ModCombinationCache() { factorialCache = new PrimitiveLongList(); factorialCache.add(1L); factorialInverseCache = new PrimitiveLongList(); factorialInverseCache.add(1L); } private void resize(int n) { for (int i = factorialCache.size() - 1; i < n; i++) { factorialCache.add(mul(factorialCache.get(i), i + 1)); factorialInverseCache.add( mul(factorialInverseCache.get(i), modInverseCache.get(i + 1))); } } long getF(int n) { resize(n); return factorialCache.get(n); } long getP(int n, int r) { resize(n); return mul(factorialCache.get(n), factorialInverseCache.get(n - r)); } long getC(int n, int k) { resize(n); return mul(factorialCache.get(n), mul(factorialInverseCache.get(k), factorialInverseCache.get(n - k))); } } } public class Main { private static void execute(ContestReader reader, ContestWriter out) { int n = reader.nextInt(); long m = reader.nextLong(); long[] ds = reader.nextLong(n); out.println(new Solver(n, m, ds).solve()); } public static void main(String[] args) { ContestReader reader = new ContestReader(System.in); ContestWriter out = new ContestWriter(System.out); execute(reader, out); out.flush(); } } class ContestWriter extends PrintWriter { ContestWriter(PrintStream printStream) { super(printStream); } public void printList(List<? extends Object> list) { for (Object object : list) { println(object); } } public void printListOneLine(List<? extends Object> list) { List<String> stringList = new ArrayList<>(); for (Object object : list) { stringList.add(object.toString()); } println(String.join(" ", stringList)); } } class ContestReader { private static final int BUFFER_SIZE = 1024; private final InputStream stream; private final byte[] buffer; private int pointer; private int bufferLength; ContestReader(InputStream stream) { this.stream = stream; this.buffer = new byte[BUFFER_SIZE]; this.pointer = 0; this.bufferLength = 0; } private boolean hasNextByte() { if (pointer < bufferLength) { return true; } pointer = 0; try { bufferLength = stream.read(buffer); } catch (IOException e) { throw new RuntimeException(e); } return bufferLength > 0; } private int readByte() { if (hasNextByte()) { return buffer[pointer++]; } else { return -1; } } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[pointer])) { pointer++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); while(true) { int b = readByte(); if (!isPrintableChar(b)) { break; } sb.appendCodePoint(b); } return sb.toString(); } public String nextLine() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); while(true) { int b = readByte(); if (!isPrintableChar(b) && b != 0x20) { break; } sb.appendCodePoint(b); } return sb.toString(); } public char nextChar() { return next().charAt(0); } public int nextInt() { if (!hasNext()) { throw new NoSuchElementException(); } int n = 0; boolean minus = false; { int b = readByte(); if (b == '-') { minus = true; } else if ('0' <= b && b <= '9') { n = b - '0'; } else { throw new NumberFormatException(); } } while(true){ int b = readByte(); if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } } } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; { int b = readByte(); if (b == '-') { minus = true; } else if ('0' <= b && b <= '9') { n = b - '0'; } else { throw new NumberFormatException(); } } while(true){ int b = readByte(); if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } } } public double nextDouble() { return Double.parseDouble(next()); } public String[] next(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } return array; } public String[] nextLine(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = nextLine(); } return array; } public char[] nextChar(int n) { char[] array = new char[n]; for (int i = 0; i < n; i++) { array[i] = nextChar(); } return array; } public int[] nextInt(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextLong(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public double[] nextDouble(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } public char[] nextCharArray() { return next().toCharArray(); } public String[][] next(int n, int m) { String[][] matrix = new String[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = next(); } } return matrix; } public int[][] nextInt(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextInt(); } } return matrix; } public char[][] nextChar(int n, int m) { char[][] matrix = new char[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextChar(); } } return matrix; } public long[][] nextLong(int n, int m) { long[][] matrix = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextLong(); } } return matrix; } public double[][] nextDouble(int n, int m) { double[][] matrix = new double[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = nextDouble(); } } return matrix; } public char[][] nextCharArray(int n) { char[][] matrix = new char[n][]; for (int i = 0; i < n; i++) { matrix[i] = next().toCharArray(); } return matrix; } } class MyAssert { public static void myAssert(boolean flag, String message) { if (!flag) { throw new RuntimeException(message); } } public static void myAssert(boolean flag) { myAssert(flag, ""); } }
ConDefects/ConDefects/Code/abc236_h/Java/29799526
condefects-java_data_8
// OM NAMAH SHIVAY // Stay strong. Be brave. Always belief. import com.sun.source.tree.Tree; import javax.swing.*; import java.math.BigInteger; import java.rmi.server.RMIServerSocketFactory; import java.util.*; import java.io.*; public class Main { static boolean prime[]; static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o){ if(this.y!=o.y) return this.y-o.y; else return o.x-this.x; } } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1l; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readlongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(long x) { int v = (int) (Math.log(x) / Math.log(2)); return v; } static long binomialCoeff(long n, long r) { if (r > n) return 0l; long inv[] = new long[(int) r + 1]; inv[0] = 1; if (r + 1 >= 2) inv[1] = 1; for (int i = 2; i <= r; i++) { inv[i] = mod1 - (mod1 / i) * inv[(int) (mod1 % i)] % mod1; } long ans = 1l; for (int i = 2; i <= r; i++) { ans = (int) (((ans % mod1) * (inv[i] % mod1)) % mod1); } for (int i = (int) n; i >= (n - r + 1); i--) { ans = (int) (((ans % mod1) * (i % mod1)) % mod1); } return ans; } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long[] facts = new long[3_000_00]; static BigInteger bi(String str) { return new BigInteger(str); } static FastScanner fs = null; static long mod1 = 1000000007; static int pre[]; static PrintWriter out; static long h[][]; static long w[][]; static ArrayList<Long> a1; static int fuck_edge = -1; static int dis[]; static int low[]; static int timer; //static boolean vis[]; static Pair a[]; static boolean vis[]; static long dp[][]; static boolean bip = true; static int col[]; static long mod = 998244353; static ArrayList<int[]> al[]; public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); int t = 1; outer: while (t-- > 0) { long ans = -1; int n = fs.nextInt(); int m = fs.nextInt(); long a[] = fs.readlongArray(n); long l = 0; long r = (long) 1e18; while (l <= r) { long mid = (l + r) >> 1; if (check(mid, a, m)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } out.println(ans); } out.close(); } public static boolean check(long len, long a[],int line){ long lin = 0l; long curr = len; long cur=0; for(int i=0;i<a.length;i++){ if(cur>0) curr--; if(curr<a[i]) { lin++; curr = len; cur=0; } if(lin>line || curr<a[i]) return false; curr -= a[i]; cur++; } return lin<=line; } static long ans; static class SegmentTree{ int tree[]; int ar[]; SegmentTree(int n,int arr[]){ tree = new int[4*n]; this.ar = arr; build(1,arr,0,arr.length-1); } public void build(int node, int arr[], int l, int r){ if(l==r) { tree[node] = arr[l]; ar[l]=arr[l]; } int mid = (l+r)/2; build(node*2,arr,l,mid); build(node*2 +1, arr,mid+1,r); tree[node] = Math.max(tree[2*node],tree[2*node + 1]); } public void update(int node, int pos, int val, int l , int r){ if(l==r) { tree[node] = val; ar[l]=node; } if(l>r) return; int mid = (l+r)/2; if(mid<pos){ update(2*node+1,pos,val,mid+1,r); }else{ update(2*node,pos,val,l,mid); } tree[node] = Math.max(tree[node*2],tree[2*node + 1]); } public int query(int node, int queryLeft, int queryRight, int left, int right){ if (queryLeft <= left && right <= queryRight) return tree[node]; int mid = left + (right - left) / 2; int minValue = Integer.MIN_VALUE; if (queryLeft <= mid) minValue = Math.max(minValue, query(2 * node + 1, left, mid, queryLeft, queryRight)); if (queryRight > mid) minValue = Math.max(minValue, query(2 * node + 2, mid + 1, right, queryLeft, queryRight)); return minValue; } } static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) > 0) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1; } return res; } static long mminvprime(long a, long b) { return expo(a, b - 2, b); } static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } static long mod_ml(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } static long mod_sb(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } static long mod_dv(long a, long b, long m) { a = a % m; b = b % m; return (mod_ml(a, mminvprime(b, m), m) + m) % m; } } class DisjointSetUnion{ int[] parent; private int N; public DisjointSetUnion(int n){ this.N = n; this.parent = new int[this.N]; for(int i = 0; i < this.N; i++){ this.parent[i] = i; } } public boolean areConnected(int u, int v){ return find(u) == find(v); } public void union(int u, int v){ if(u != v){ int a = find(u); int b = find(v); parent[a] = b; } } public int find(int u){ int x = u; while(x != this.parent[x]){ x = this.parent[x]; } this.parent[u] = x; return x; } } class Fenwick{ long bit[]; int n; Fenwick(int n){ bit = new long[n+1]; this.n=n; } public void update(int x, int v){ int i=x; while(i<=this.n){ bit[i] += v; i += i&(-i); } } public long sum(int x){ long sum=0; int i=x; while(i>=1){ sum += bit[i]; i -= i&(-i); } return sum; } } // Arrays.sort(one, Comparator.comparingLong(o->o[0])); //Arrays.sort(wt,(t1,t2) -> { // return (int)(t2.w - t1.w); // }); //Arrays.sort(arr, new Comparator<int[]>() { //public int compare(int[] frst, int[] scnd) { // if(frst[colmn-1] > scnd[colmn-1]) { // return 1; // } // else return -1; // } // }); //class Trie{ // Trie[] links; // public Trie(int m){ // links = new Trie[m+1]; // } // OM NAMAH SHIVAY // Stay strong. Be brave. Always belief. import com.sun.source.tree.Tree; import javax.swing.*; import java.math.BigInteger; import java.rmi.server.RMIServerSocketFactory; import java.util.*; import java.io.*; public class Main { static boolean prime[]; static class Pair implements Comparable<Pair> { int x; int y; Pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(Pair o){ if(this.y!=o.y) return this.y-o.y; else return o.x-this.x; } } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1l; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readlongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(long x) { int v = (int) (Math.log(x) / Math.log(2)); return v; } static long binomialCoeff(long n, long r) { if (r > n) return 0l; long inv[] = new long[(int) r + 1]; inv[0] = 1; if (r + 1 >= 2) inv[1] = 1; for (int i = 2; i <= r; i++) { inv[i] = mod1 - (mod1 / i) * inv[(int) (mod1 % i)] % mod1; } long ans = 1l; for (int i = 2; i <= r; i++) { ans = (int) (((ans % mod1) * (inv[i] % mod1)) % mod1); } for (int i = (int) n; i >= (n - r + 1); i--) { ans = (int) (((ans % mod1) * (i % mod1)) % mod1); } return ans; } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static long[] facts = new long[3_000_00]; static BigInteger bi(String str) { return new BigInteger(str); } static FastScanner fs = null; static long mod1 = 1000000007; static int pre[]; static PrintWriter out; static long h[][]; static long w[][]; static ArrayList<Long> a1; static int fuck_edge = -1; static int dis[]; static int low[]; static int timer; //static boolean vis[]; static Pair a[]; static boolean vis[]; static long dp[][]; static boolean bip = true; static int col[]; static long mod = 998244353; static ArrayList<int[]> al[]; public static void main(String[] args) { fs = new FastScanner(); out = new PrintWriter(System.out); int t = 1; outer: while (t-- > 0) { long ans = -1; int n = fs.nextInt(); int m = fs.nextInt(); long a[] = fs.readlongArray(n); long l = 0; long r = (long) 1e18; while (l <= r) { long mid = (l + r) >> 1; if (check(mid, a, m)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } out.println(ans); } out.close(); } public static boolean check(long len, long a[],int line){ long lin = 1l; long curr = len; long cur=0; for(int i=0;i<a.length;i++){ if(cur>0) curr--; if(curr<a[i]) { lin++; curr = len; cur=0; } if(lin>line || curr<a[i]) return false; curr -= a[i]; cur++; } return lin<=line; } static long ans; static class SegmentTree{ int tree[]; int ar[]; SegmentTree(int n,int arr[]){ tree = new int[4*n]; this.ar = arr; build(1,arr,0,arr.length-1); } public void build(int node, int arr[], int l, int r){ if(l==r) { tree[node] = arr[l]; ar[l]=arr[l]; } int mid = (l+r)/2; build(node*2,arr,l,mid); build(node*2 +1, arr,mid+1,r); tree[node] = Math.max(tree[2*node],tree[2*node + 1]); } public void update(int node, int pos, int val, int l , int r){ if(l==r) { tree[node] = val; ar[l]=node; } if(l>r) return; int mid = (l+r)/2; if(mid<pos){ update(2*node+1,pos,val,mid+1,r); }else{ update(2*node,pos,val,l,mid); } tree[node] = Math.max(tree[node*2],tree[2*node + 1]); } public int query(int node, int queryLeft, int queryRight, int left, int right){ if (queryLeft <= left && right <= queryRight) return tree[node]; int mid = left + (right - left) / 2; int minValue = Integer.MIN_VALUE; if (queryLeft <= mid) minValue = Math.max(minValue, query(2 * node + 1, left, mid, queryLeft, queryRight)); if (queryRight > mid) minValue = Math.max(minValue, query(2 * node + 2, mid + 1, right, queryLeft, queryRight)); return minValue; } } static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) > 0) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1; } return res; } static long mminvprime(long a, long b) { return expo(a, b - 2, b); } static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } static long mod_ml(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } static long mod_sb(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } static long mod_dv(long a, long b, long m) { a = a % m; b = b % m; return (mod_ml(a, mminvprime(b, m), m) + m) % m; } } class DisjointSetUnion{ int[] parent; private int N; public DisjointSetUnion(int n){ this.N = n; this.parent = new int[this.N]; for(int i = 0; i < this.N; i++){ this.parent[i] = i; } } public boolean areConnected(int u, int v){ return find(u) == find(v); } public void union(int u, int v){ if(u != v){ int a = find(u); int b = find(v); parent[a] = b; } } public int find(int u){ int x = u; while(x != this.parent[x]){ x = this.parent[x]; } this.parent[u] = x; return x; } } class Fenwick{ long bit[]; int n; Fenwick(int n){ bit = new long[n+1]; this.n=n; } public void update(int x, int v){ int i=x; while(i<=this.n){ bit[i] += v; i += i&(-i); } } public long sum(int x){ long sum=0; int i=x; while(i>=1){ sum += bit[i]; i -= i&(-i); } return sum; } } // Arrays.sort(one, Comparator.comparingLong(o->o[0])); //Arrays.sort(wt,(t1,t2) -> { // return (int)(t2.w - t1.w); // }); //Arrays.sort(arr, new Comparator<int[]>() { //public int compare(int[] frst, int[] scnd) { // if(frst[colmn-1] > scnd[colmn-1]) { // return 1; // } // else return -1; // } // }); //class Trie{ // Trie[] links; // public Trie(int m){ // links = new Trie[m+1]; // }
ConDefects/ConDefects/Code/abc319_d/Java/45440803
condefects-java_data_9
import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long m = sc.nextInt(); long[] line = new long[n]; long sum = 0; long max = 0; for(int i = 0;i < n;i++) { line[i] = Integer.parseInt(sc.next()); sum += line[i]; max = Math.max(max, line[i]); }long right = max * ((long)n - m + 1) + ((long)n - m) + 1; long left = 0; while(left < right - 1) { long mid = (left + right)/2; long nowW = 0; long nowH = 0; for(int i = 0;i < n;i++) { if(nowW + line[i] <= mid) { nowW += line[i]; nowW++; }else if(nowW + line[i] > mid) { nowW = line[i]; nowW++; nowH++; }if(nowW >= mid && i != n - 1) { nowH++; nowW = 0; } }if(nowH <= m - 1) { right = mid; }else if(nowH > m - 1){ left = mid; } //System.out.println(left + " " + right); }System.out.print(right); } } import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long m = sc.nextInt(); long[] line = new long[n]; long sum = 0; long max = 0; for(int i = 0;i < n;i++) { line[i] = Integer.parseInt(sc.next()); sum += line[i]; max = Math.max(max, line[i]); }long right = max * ((long)n - m + 1) + ((long)n - m) + 1; long left = max - 1; while(left < right - 1) { long mid = (left + right)/2; long nowW = 0; long nowH = 0; for(int i = 0;i < n;i++) { if(nowW + line[i] <= mid) { nowW += line[i]; nowW++; }else if(nowW + line[i] > mid) { nowW = line[i]; nowW++; nowH++; }if(nowW >= mid && i != n - 1) { nowH++; nowW = 0; } }if(nowH <= m - 1) { right = mid; }else if(nowH > m - 1){ left = mid; } //System.out.println(left + " " + right); }System.out.print(right); } }
ConDefects/ConDefects/Code/abc319_d/Java/45403304
condefects-java_data_10
// 18:32:21 09-09-2023 // D - Minimum Width // https://atcoder.jp/contests/abc319/tasks/abc319_d // 2000 ms import java.io.*; import java.util.*; import java.util.function.IntUnaryOperator; import java.util.function.LongUnaryOperator; import java.util.stream.Collectors; public class Main { static In in = new FastIn(); static Out out = new Out(false); static final long inf = 0x1fffffffffffffffL; static final int iinf = 0x3fffffff; static final double eps = 1e-9; static long mod = 998244353; void solve() { int n = inp() , k = inp(); long[] arr = in.nextLongArray(n); long l = -1 , r = 1_000_000_000_000_000L; while(r - l > 1){ long m = (l + r) / 2; if(check(arr , m) <= k) r = m; else l = m; } // out.println(check(arr , 26L)); out.println(r); } int check(long[] arr , long m){ int cnt = 0; long sum = 0; for(int i = 0 ; i < arr.length ; i++){ sum = sum + arr[i] + 1; if(sum - 1 == m){ sum = 0; cnt++; continue; } if(sum > m){ cnt++; sum = arr[i] + 1; } } if(sum > 0) cnt++; return cnt; } public static void main(String... args) { int ntc = 1; // ntc = in.nextInt(); for(int i = 1 ; i <= ntc ; i++) new Main().solve(); out.flush(); } int inp(){ return in.nextInt(); } } class FastIn extends In { private final BufferedInputStream reader = new BufferedInputStream(System.in); private final byte[] buffer = new byte[0x10000]; private int i = 0; private int length = 0; public int read() { if (i == length) { i = 0; try { length = reader.read(buffer); } catch (IOException ignored) { } if (length == -1) { return 0; } } if (length <= i) { throw new RuntimeException(); } return buffer[i++]; } String next() { StringBuilder builder = new StringBuilder(); int b = read(); while (b < '!' || '~' < b) { b = read(); } while ('!' <= b && b <= '~') { builder.appendCodePoint(b); b = read(); } return builder.toString(); } String nextLine() { StringBuilder builder = new StringBuilder(); int b = read(); while (b != 0 && b != '\r' && b != '\n') { builder.appendCodePoint(b); b = read(); } if (b == '\r') { read(); } return builder.toString(); } int nextInt() { long val = nextLong(); if ((int)val != val) { throw new NumberFormatException(); } return (int)val; } long nextLong() { int b = read(); while (b < '!' || '~' < b) { b = read(); } boolean neg = false; if (b == '-') { neg = true; b = read(); } long n = 0; int c = 0; while ('0' <= b && b <= '9') { n = n * 10 + b - '0'; b = read(); c++; } if (c == 0 || c >= 2 && n == 0) { throw new NumberFormatException(); } return neg ? -n : n; } } class In { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000); private StringTokenizer tokenizer; String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException ignored) { } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char[] nextCharArray() { return next().toCharArray(); } String[] nextStringArray(int n) { String[] s = new String[n]; for (int i = 0; i < n; i++) { s[i] = next(); } return s; } char[][] nextCharGrid(int n, int m) { char[][] a = new char[n][m]; for (int i = 0; i < n; i++) { a[i] = next().toCharArray(); } return a; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } int[] nextIntArray(int n, IntUnaryOperator op) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsInt(nextInt()); } return a; } int[][] nextIntMatrix(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = nextIntArray(w); } return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long[] nextLongArray(int n, LongUnaryOperator op) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsLong(nextLong()); } return a; } long[][] nextLongMatrix(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nextLongArray(w); } return a; } List<List<Integer>> nextEdges(int n, int m, boolean directed) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; res.get(u).add(v); if (!directed) { res.get(v).add(u); } } return res; } } class Out { private final PrintWriter out = new PrintWriter(System.out); private final PrintWriter err = new PrintWriter(System.err); boolean autoFlush = false; boolean enableDebug; Out(boolean enableDebug) { this.enableDebug = enableDebug; } void println(Object... args) { if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } out.println(Arrays.stream(args).map(obj -> { Class<?> clazz = obj == null ? null : obj.getClass(); return clazz == Double.class ? String.format("%.10f", obj) : clazz == byte[].class ? Arrays.toString((byte[])obj) : clazz == short[].class ? Arrays.toString((short[])obj) : clazz == int[].class ? Arrays.toString((int[])obj) : clazz == long[].class ? Arrays.toString((long[])obj) : clazz == char[].class ? Arrays.toString((char[])obj) : clazz == float[].class ? Arrays.toString((float[])obj) : clazz == double[].class ? Arrays.toString((double[])obj) : clazz == boolean[].class ? Arrays.toString((boolean[])obj) : obj instanceof Object[] ? Arrays.deepToString((Object[])obj) : String.valueOf(obj); }).collect(Collectors.joining(" "))); if (autoFlush) { out.flush(); } } void debug(Object... args) { if (!enableDebug) { return; } if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } err.println(Arrays.stream(args).map(obj -> { Class<?> clazz = obj == null ? null : obj.getClass(); return clazz == Double.class ? String.format("%.10f", obj) : clazz == byte[].class ? Arrays.toString((byte[])obj) : clazz == short[].class ? Arrays.toString((short[])obj) : clazz == int[].class ? Arrays.toString((int[])obj) : clazz == long[].class ? Arrays.toString((long[])obj) : clazz == char[].class ? Arrays.toString((char[])obj) : clazz == float[].class ? Arrays.toString((float[])obj) : clazz == double[].class ? Arrays.toString((double[])obj) : clazz == boolean[].class ? Arrays.toString((boolean[])obj) : obj instanceof Object[] ? Arrays.deepToString((Object[])obj) : String.valueOf(obj); }).collect(Collectors.joining(" "))); err.flush(); } void println(char a) { out.println(a); if (autoFlush) { out.flush(); } } void println(int a) { out.println(a); if (autoFlush) { out.flush(); } } void println(long a) { out.println(a); if (autoFlush) { out.flush(); } } void println(double a) { out.println(String.format("%.10f", a)); if (autoFlush) { out.flush(); } } void println(String s) { out.println(s); if (autoFlush) { out.flush(); } } void println(char[] s) { out.println(String.valueOf(s)); if (autoFlush) { out.flush(); } } void println(int[] a) { StringJoiner joiner = new StringJoiner(" "); for (int i : a) { joiner.add(Integer.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } void println(long[] a) { StringJoiner joiner = new StringJoiner(" "); for (long i : a) { joiner.add(Long.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } void flush() { err.flush(); out.flush(); } } // template - Yu_212 // 18:32:21 09-09-2023 // D - Minimum Width // https://atcoder.jp/contests/abc319/tasks/abc319_d // 2000 ms import java.io.*; import java.util.*; import java.util.function.IntUnaryOperator; import java.util.function.LongUnaryOperator; import java.util.stream.Collectors; public class Main { static In in = new FastIn(); static Out out = new Out(false); static final long inf = 0x1fffffffffffffffL; static final int iinf = 0x3fffffff; static final double eps = 1e-9; static long mod = 998244353; void solve() { int n = inp() , k = inp(); long[] arr = in.nextLongArray(n); long l = -1 , r = 1_000_000_000_000_000L; while(r - l > 1){ long m = (l + r) / 2; if(check(arr , m) <= k) r = m; else l = m; } // out.println(check(arr , 26L)); out.println(r); } int check(long[] arr , long m){ int cnt = 0; long sum = 0; long max = Arrays.stream(arr).max().getAsLong(); if(max > m) return iinf; for(int i = 0 ; i < arr.length ; i++){ sum = sum + arr[i] + 1; if(sum - 1 == m){ sum = 0; cnt++; continue; } if(sum > m){ cnt++; sum = arr[i] + 1; } } if(sum > 0) cnt++; return cnt; } public static void main(String... args) { int ntc = 1; // ntc = in.nextInt(); for(int i = 1 ; i <= ntc ; i++) new Main().solve(); out.flush(); } int inp(){ return in.nextInt(); } } class FastIn extends In { private final BufferedInputStream reader = new BufferedInputStream(System.in); private final byte[] buffer = new byte[0x10000]; private int i = 0; private int length = 0; public int read() { if (i == length) { i = 0; try { length = reader.read(buffer); } catch (IOException ignored) { } if (length == -1) { return 0; } } if (length <= i) { throw new RuntimeException(); } return buffer[i++]; } String next() { StringBuilder builder = new StringBuilder(); int b = read(); while (b < '!' || '~' < b) { b = read(); } while ('!' <= b && b <= '~') { builder.appendCodePoint(b); b = read(); } return builder.toString(); } String nextLine() { StringBuilder builder = new StringBuilder(); int b = read(); while (b != 0 && b != '\r' && b != '\n') { builder.appendCodePoint(b); b = read(); } if (b == '\r') { read(); } return builder.toString(); } int nextInt() { long val = nextLong(); if ((int)val != val) { throw new NumberFormatException(); } return (int)val; } long nextLong() { int b = read(); while (b < '!' || '~' < b) { b = read(); } boolean neg = false; if (b == '-') { neg = true; b = read(); } long n = 0; int c = 0; while ('0' <= b && b <= '9') { n = n * 10 + b - '0'; b = read(); c++; } if (c == 0 || c >= 2 && n == 0) { throw new NumberFormatException(); } return neg ? -n : n; } } class In { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000); private StringTokenizer tokenizer; String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException ignored) { } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } char[] nextCharArray() { return next().toCharArray(); } String[] nextStringArray(int n) { String[] s = new String[n]; for (int i = 0; i < n; i++) { s[i] = next(); } return s; } char[][] nextCharGrid(int n, int m) { char[][] a = new char[n][m]; for (int i = 0; i < n; i++) { a[i] = next().toCharArray(); } return a; } int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } int[] nextIntArray(int n, IntUnaryOperator op) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsInt(nextInt()); } return a; } int[][] nextIntMatrix(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = nextIntArray(w); } return a; } long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long[] nextLongArray(int n, LongUnaryOperator op) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsLong(nextLong()); } return a; } long[][] nextLongMatrix(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nextLongArray(w); } return a; } List<List<Integer>> nextEdges(int n, int m, boolean directed) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; res.get(u).add(v); if (!directed) { res.get(v).add(u); } } return res; } } class Out { private final PrintWriter out = new PrintWriter(System.out); private final PrintWriter err = new PrintWriter(System.err); boolean autoFlush = false; boolean enableDebug; Out(boolean enableDebug) { this.enableDebug = enableDebug; } void println(Object... args) { if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } out.println(Arrays.stream(args).map(obj -> { Class<?> clazz = obj == null ? null : obj.getClass(); return clazz == Double.class ? String.format("%.10f", obj) : clazz == byte[].class ? Arrays.toString((byte[])obj) : clazz == short[].class ? Arrays.toString((short[])obj) : clazz == int[].class ? Arrays.toString((int[])obj) : clazz == long[].class ? Arrays.toString((long[])obj) : clazz == char[].class ? Arrays.toString((char[])obj) : clazz == float[].class ? Arrays.toString((float[])obj) : clazz == double[].class ? Arrays.toString((double[])obj) : clazz == boolean[].class ? Arrays.toString((boolean[])obj) : obj instanceof Object[] ? Arrays.deepToString((Object[])obj) : String.valueOf(obj); }).collect(Collectors.joining(" "))); if (autoFlush) { out.flush(); } } void debug(Object... args) { if (!enableDebug) { return; } if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } err.println(Arrays.stream(args).map(obj -> { Class<?> clazz = obj == null ? null : obj.getClass(); return clazz == Double.class ? String.format("%.10f", obj) : clazz == byte[].class ? Arrays.toString((byte[])obj) : clazz == short[].class ? Arrays.toString((short[])obj) : clazz == int[].class ? Arrays.toString((int[])obj) : clazz == long[].class ? Arrays.toString((long[])obj) : clazz == char[].class ? Arrays.toString((char[])obj) : clazz == float[].class ? Arrays.toString((float[])obj) : clazz == double[].class ? Arrays.toString((double[])obj) : clazz == boolean[].class ? Arrays.toString((boolean[])obj) : obj instanceof Object[] ? Arrays.deepToString((Object[])obj) : String.valueOf(obj); }).collect(Collectors.joining(" "))); err.flush(); } void println(char a) { out.println(a); if (autoFlush) { out.flush(); } } void println(int a) { out.println(a); if (autoFlush) { out.flush(); } } void println(long a) { out.println(a); if (autoFlush) { out.flush(); } } void println(double a) { out.println(String.format("%.10f", a)); if (autoFlush) { out.flush(); } } void println(String s) { out.println(s); if (autoFlush) { out.flush(); } } void println(char[] s) { out.println(String.valueOf(s)); if (autoFlush) { out.flush(); } } void println(int[] a) { StringJoiner joiner = new StringJoiner(" "); for (int i : a) { joiner.add(Integer.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } void println(long[] a) { StringJoiner joiner = new StringJoiner(" "); for (long i : a) { joiner.add(Long.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } void flush() { err.flush(); out.flush(); } } // template - Yu_212
ConDefects/ConDefects/Code/abc319_d/Java/45416107
condefects-java_data_11
import java.util.Arrays; import java.util.Scanner; class D { public static void main(String... args) { final Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); final int[] L = new int[N]; Arrays.setAll(L, $ -> sc.nextInt()); long sum = 0; for (int l : L) sum += l; long lo = sum / M; long hi = sum + N - 1; while (hi - lo > 1) { final long W = (hi + lo + 1) / 2; boolean failed = L[0] > W; long w = L[0]; for (int i = 1, j = 0; i < N; i++) { if (w + L[i] + 1 <= W) { w += L[i] + 1; } else { if (++j >= M || L[i] > W) { failed = true; break; } w = L[i]; } } if (failed) lo = W; else hi = W; } System.out.println(hi); } } public class Main { public static void main(String... args) { D.main(); } } import java.util.Arrays; import java.util.Scanner; class D { public static void main(String... args) { final Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); final int[] L = new int[N]; Arrays.setAll(L, $ -> sc.nextInt()); long sum = 0; for (int l : L) sum += l; long lo = sum / M - 1; long hi = sum + N - 1; while (hi - lo > 1) { final long W = (hi + lo + 1) / 2; boolean failed = L[0] > W; long w = L[0]; for (int i = 1, j = 0; i < N; i++) { if (w + L[i] + 1 <= W) { w += L[i] + 1; } else { if (++j >= M || L[i] > W) { failed = true; break; } w = L[i]; } } if (failed) lo = W; else hi = W; } System.out.println(hi); } } public class Main { public static void main(String... args) { D.main(); } }
ConDefects/ConDefects/Code/abc319_d/Java/45408839
condefects-java_data_12
import java.util.Scanner; public class Main { static int N, M; static long[] L; static boolean isPossible(long W) { int lines = 1; long line_width = 0; for (int i = 0; i < N; i++) { if (L[i] > W) { return false; } if (line_width + L[i] <= W) { line_width += L[i] + 1; } else { lines++; line_width = L[i] + 1; } } return lines <= M; } static long binarySearch(long left, long right) { long result = -1; while (left <= right) { long mid = (left + right) / 2; if (isPossible(mid)) { result = mid; right = mid - 1; } else { left = mid + 1; } } return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); M = scanner.nextInt(); L = new long[N]; for (int i = 0; i < N; i++) { L[i] = scanner.nextLong(); } } } import java.util.Scanner; public class Main { static int N, M; static long[] L; static boolean isPossible(long W) { int lines = 1; long line_width = 0; for (int i = 0; i < N; i++) { if (L[i] > W) { return false; } if (line_width + L[i] <= W) { line_width += L[i] + 1; } else { lines++; line_width = L[i] + 1; } } return lines <= M; } static long binarySearch(long left, long right) { long result = -1; while (left <= right) { long mid = (left + right) / 2; if (isPossible(mid)) { result = mid; right = mid - 1; } else { left = mid + 1; } } return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); N = scanner.nextInt(); M = scanner.nextInt(); L = new long[N]; for (int i = 0; i < N; i++) { L[i] = scanner.nextLong(); } long min_width = binarySearch(1, (long) 1e18); System.out.println(min_width); } }
ConDefects/ConDefects/Code/abc319_d/Java/45403392
condefects-java_data_13
//make sure to make new file! import java.io.*; import java.util.*; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); long[] array = new long[n]; st = new StringTokenizer(f.readLine()); long max = 0L; for(int k = 0; k < n; k++){ array[k] = Long.parseLong(st.nextToken()); max = Math.max(max,array[k]); } long l = max; long r = 200000001000000L; long ans = -1; while(l <= r){ long mid = l + (r-l)/2L; //see if width = mid works int lines = 1; long curline = 0; for(int k = 0; k < n; k++){ if(curline + array[k] > mid){ lines++; curline = array[k]; } else { curline += array[k]+1; } } if(lines <= m){ ans = mid; r = mid-1; } else { l = mid+1; } } out.println(ans); out.close(); } } //make sure to make new file! import java.io.*; import java.util.*; public class Main{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(f.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); long[] array = new long[n]; st = new StringTokenizer(f.readLine()); long max = 0L; for(int k = 0; k < n; k++){ array[k] = Long.parseLong(st.nextToken()); max = Math.max(max,array[k]); } long l = max; long r = 200000001000000L; long ans = -1; while(l <= r){ long mid = l + (r-l)/2L; //see if width = mid works int lines = 1; long curline = 0; for(int k = 0; k < n; k++){ if(curline + array[k] > mid){ lines++; curline = array[k]+1; } else { curline += array[k]+1; } } if(lines <= m){ ans = mid; r = mid-1; } else { l = mid+1; } } out.println(ans); out.close(); } }
ConDefects/ConDefects/Code/abc319_d/Java/45430736
condefects-java_data_14
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static FastReader sc; public static void main(String[] args) throws IOException { sc = new FastReader(); int n = sc.nextInt(); int m = sc.nextInt(); long[] L = new long[n]; long total = 0; for (int i=0;i<n;i++) { L[i] = sc.nextLong(); total += L[i]; } long ans = 0; long ng = 0; // ありうる値-1 long ok = total + n; // ありうる値+1 while (Math.abs(ok - ng) > 1) { long mid = (ok + ng) / 2; if (check(L, mid, m)) { ok = mid; } else { ng = mid; } } System.out.println(ok); } static boolean check(long[] a, long mid, long m) { int cnt = 0; long tmp = 0; int i = 0; while (true) { if (a[i] > mid) { return false; } if (i > 0 && tmp + a[i] >= mid) { if (tmp + a[i] == mid) { i++; if (i > a.length -1) { cnt++; break; } } tmp = a[i]; cnt++; } else { tmp += a[i] + 1; } i++; if (i > a.length -1) { cnt++; break; } } return cnt <= m; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main { static FastReader sc; public static void main(String[] args) throws IOException { sc = new FastReader(); int n = sc.nextInt(); int m = sc.nextInt(); long[] L = new long[n]; long total = 0; for (int i=0;i<n;i++) { L[i] = sc.nextLong(); total += L[i]; } long ans = 0; long ng = 0; // ありうる値-1 long ok = total + n; // ありうる値+1 while (Math.abs(ok - ng) > 1) { long mid = (ok + ng) / 2; if (check(L, mid, m)) { ok = mid; } else { ng = mid; } } System.out.println(ok); } static boolean check(long[] a, long mid, long m) { int cnt = 0; long tmp = 0; int i = 0; while (true) { if (a[i] > mid) { return false; } if (i > 0 && tmp + a[i] >= mid) { if (tmp + a[i] == mid) { i++; if (i > a.length -1) { cnt++; break; } } tmp = a[i] + 1; cnt++; } else { tmp += a[i] + 1; } i++; if (i > a.length -1) { cnt++; break; } } return cnt <= m; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] readLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } } }
ConDefects/ConDefects/Code/abc319_d/Java/45470117
condefects-java_data_15
import java.util.*; public class Main { private static boolean possible(int[] arr, int lines, long maxWidth) { long curWidth = 1; for(int i = 0; i < arr.length && lines >= 0; i++) { if(curWidth + (arr[i] - 1) > maxWidth) { lines--; curWidth = 1; } curWidth += (arr[i] - 1); curWidth += 2; } return lines > 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] len = new int[n]; long ans = -1, l = 1, h = (long)(1e14) * 2; for(int i = 0; i < n; i++) { len[i] = sc.nextInt(); l = Math.max(l, len[i]); } while(l <= h) { long mid = (l + h) >> 1; if(possible(len, m, mid)) { ans = mid; h = mid - 1; } else { l = mid + 1; } } System.out.println(ans); } } import java.util.*; public class Main { private static boolean possible(int[] arr, int lines, long maxWidth) { long curWidth = 1; for(int i = 0; i < arr.length && lines >= 0; i++) { if(curWidth + (arr[i] - 1) > maxWidth) { lines--; curWidth = 1; } curWidth += (arr[i] - 1); curWidth += 2; } return lines > 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] len = new int[n]; long ans = -1, l = 1, h = (long)(1e15); for(int i = 0; i < n; i++) { len[i] = sc.nextInt(); l = Math.max(l, len[i]); } while(l <= h) { long mid = (l + h) >> 1; if(possible(len, m, mid)) { ans = mid; h = mid - 1; } else { l = mid + 1; } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc319_d/Java/45424477
condefects-java_data_16
import java.io.PrintWriter; import java.util.*; public class Main { private static final ContestScanner in = new ContestScanner(System.in); private static final PrintWriter pw = new PrintWriter(System.out); private static int n, m; private static int[] l; public static void main(String[] args) { n = in.nextInt(); m = in.nextInt(); l = new int[n]; for (int i = 0; i < n; i++) l[i] = in.nextInt(); solve(); } private static void solve() { long l = 1, r = (long) 1e9 * n; while (l <= r) { long mid = (l + r) / 2; if (check(mid)) r = mid - 1; else l = mid + 1; } write(l); } private static boolean check(long w) { if (l[0] > w) return false; long rows = 1, curW = l[0]; int p = 1; for (; p < n; p++) { if (l[p] > w) return false; if (curW + 1 + l[p] > w) { curW = l[p]; rows++; } else { curW += 1 + l[p]; } } return rows <= m; } private static void write(long num) { pw.println(num); pw.flush(); } private static void write(String s) { pw.println(s); pw.flush(); } private static void write(int[] arr, int begin, int end) { StringBuilder line = new StringBuilder(); for (int i = begin; i <= end; i++) { line.append(arr[i]).append(" "); } write(line.toString()); } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString((char)b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString((char)b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } } import java.io.PrintWriter; import java.util.*; public class Main { private static final ContestScanner in = new ContestScanner(System.in); private static final PrintWriter pw = new PrintWriter(System.out); private static int n, m; private static int[] l; public static void main(String[] args) { n = in.nextInt(); m = in.nextInt(); l = new int[n]; for (int i = 0; i < n; i++) l[i] = in.nextInt(); solve(); } private static void solve() { long l = 1, r = (long) 1e9 * n + n - 1; while (l <= r) { long mid = (l + r) / 2; if (check(mid)) r = mid - 1; else l = mid + 1; } write(l); } private static boolean check(long w) { if (l[0] > w) return false; long rows = 1, curW = l[0]; int p = 1; for (; p < n; p++) { if (l[p] > w) return false; if (curW + 1 + l[p] > w) { curW = l[p]; rows++; } else { curW += 1 + l[p]; } } return rows <= m; } private static void write(long num) { pw.println(num); pw.flush(); } private static void write(String s) { pw.println(s); pw.flush(); } private static void write(int[] arr, int begin, int end) { StringBuilder line = new StringBuilder(); for (int i = begin; i <= end; i++) { line.append(arr[i]).append(" "); } write(line.toString()); } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString((char)b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString((char)b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } }
ConDefects/ConDefects/Code/abc319_d/Java/45470029
condefects-java_data_17
import com.sun.source.tree.Tree; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; /** * # * * @author pttrung */ public class Main { public static long MOD = 998244353; static long[] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int m = in.nextInt(); long[]data = new long[n]; long st = 0; long ed = 0; for(int i = 0; i < n; i++){ data[i] = in.nextInt(); st = Long.max(st, data[i]); ed += data[i]; } long re = ed; while(st <= ed){ long mid = (st + ed)/2; int total = 1; long cur = 0; for(int i = 0; i < n; i++){ if(cur + data[i] > mid){ cur = data[i] + 1; total++; continue; } cur += data[i] + 1; } if(total <= m){ re = mid; ed = mid - 1; continue; } st = mid + 1; } out.println(re); out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int compareTo(Point o) { if (x != o.x) { return Integer.compare(x, o.x); } return Integer.compare(y, o.y); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } public String toString() { return x + " " + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val) % MOD; } else { return (val * ((val * a) % MOD)) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } } import com.sun.source.tree.Tree; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; /** * # * * @author pttrung */ public class Main { public static long MOD = 998244353; static long[] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int m = in.nextInt(); long[]data = new long[n]; long st = 0; long ed = n - 1; for(int i = 0; i < n; i++){ data[i] = in.nextInt(); st = Long.max(st, data[i]); ed += data[i]; } long re = ed; while(st <= ed){ long mid = (st + ed)/2; int total = 1; long cur = 0; for(int i = 0; i < n; i++){ if(cur + data[i] > mid){ cur = data[i] + 1; total++; continue; } cur += data[i] + 1; } if(total <= m){ re = mid; ed = mid - 1; continue; } st = mid + 1; } out.println(re); out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int compareTo(Point o) { if (x != o.x) { return Integer.compare(x, o.x); } return Integer.compare(y, o.y); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Point point = (Point) o; return x == point.x && y == point.y; } @Override public int hashCode() { return Objects.hash(x, y); } public String toString() { return x + " " + y; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val) % MOD; } else { return (val * ((val * a) % MOD)) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
ConDefects/ConDefects/Code/abc319_d/Java/45446792
condefects-java_data_18
import java.util.Scanner; public class Main { public static void main(final String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int N = Integer.parseInt(sc.next()); final int M = Integer.parseInt(sc.next()); final int[] L = new int[N]; long strLength = 0; long maxLetter = Integer.MIN_VALUE; for (int i = 0; i < N; i++) { L[i] = Integer.parseInt(sc.next()) + 1; strLength += L[i]; if (maxLetter < L[i] - 1) { maxLetter += L[i] - 1; } } // 二分探索 while (maxLetter + 1 < strLength) { final long middle = (maxLetter + strLength) / 2; int rows = 1; // 今の行数 long length = 0; // 先頭から何文字目か for (int i = 0; i < N; ++i) { length += L[i]; // 行の末尾に追加してみる if (length > middle) { // はみ出たら ++rows; // 改行して length = L[i]; // 先頭に追加 } } if (rows > M) { // 縦幅が足りていなければ maxLetter = middle; // 答えは middle より大きい } else { // 縦幅が足りていなければ strLength = middle; // 答えは middle 以下 } } // 答えから 1 を引いて出力 System.out.println(strLength - 1); } } } import java.util.Scanner; public class Main { public static void main(final String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int N = Integer.parseInt(sc.next()); final int M = Integer.parseInt(sc.next()); final int[] L = new int[N]; long strLength = 0; long maxLetter = Integer.MIN_VALUE; for (int i = 0; i < N; i++) { L[i] = Integer.parseInt(sc.next()) + 1; strLength += L[i]; if (maxLetter < L[i] - 1) { maxLetter = L[i] - 1; } } // 二分探索 while (maxLetter + 1 < strLength) { final long middle = (maxLetter + strLength) / 2; int rows = 1; // 今の行数 long length = 0; // 先頭から何文字目か for (int i = 0; i < N; ++i) { length += L[i]; // 行の末尾に追加してみる if (length > middle) { // はみ出たら ++rows; // 改行して length = L[i]; // 先頭に追加 } } if (rows > M) { // 縦幅が足りていなければ maxLetter = middle; // 答えは middle より大きい } else { // 縦幅が足りていなければ strLength = middle; // 答えは middle 以下 } } // 答えから 1 を引いて出力 System.out.println(strLength - 1); } } }
ConDefects/ConDefects/Code/abc319_d/Java/45539729
condefects-java_data_19
import java.io.*; import java.math.*; import java.util.*; public class Main { static class Pair { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } } static class Compare { 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.x != p2.x) { return (int) (p1.x - p2.x); } else { return (int) (p1.y - p2.y); } // return Double.compare(a[0], b[0]); } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static boolean function(int a[],long mid,int m) { long till = 0; int fill = 0; int i = 0; while(i < a.length) { if(a[i] > mid) { break; } if(a[i] + till <= mid) { till += a[i] + 1; i++; } else { fill++; till = 0; } } return (i == a.length && fill + 1 <= m); } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = 1; while (tc-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = sc.nextInt(); } long l = 1; long r = (long)(1e11); long ans = -1; while(l <= r) { long mid = l + (r - l)/2; if(function(a,mid,m)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } res.append(ans); } System.out.println(res); } } import java.io.*; import java.math.*; import java.util.*; public class Main { static class Pair { long x; int y; Pair(long x, int y) { this.x = x; this.y = y; } } static class Compare { 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.x != p2.x) { return (int) (p1.x - p2.x); } else { return (int) (p1.y - p2.y); } // return Double.compare(a[0], b[0]); } }); // for (int i = 0; i < n; i++) { // System.out.print(arr[i].x + " " + arr[i].y + " "); // } // System.out.println(); } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static boolean function(int a[],long mid,int m) { long till = 0; int fill = 0; int i = 0; while(i < a.length) { if(a[i] > mid) { break; } if(a[i] + till <= mid) { till += a[i] + 1; i++; } else { fill++; till = 0; } } return (i == a.length && fill + 1 <= m); } public static void main(String args[]) throws Exception { Scanner sc = new Scanner(); StringBuilder res = new StringBuilder(); int tc = 1; while (tc-- > 0) { int n = sc.nextInt(); int m = sc.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = sc.nextInt(); } long l = 1; long r = (long)(1e15); long ans = -1; while(l <= r) { long mid = l + (r - l)/2; if(function(a,mid,m)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } res.append(ans); } System.out.println(res); } }
ConDefects/ConDefects/Code/abc319_d/Java/45492458
condefects-java_data_20
import java.util.*; import java.io.*; public class Main { static final FastReader sc = new FastReader(); static final PrintWriter out = new PrintWriter(System.out, true); private static boolean debug = System.getProperty("ONLINE_JUDGE") == null; static void trace(Object... o) { if (debug) { System.err.println(Arrays.deepToString(o)); } } public static void main(String[] args) { int N = sc.nextInt(), M = sc.nextInt(); long[] L = new long[N]; for (int i = 0; i < N; i++) L[i] = sc.nextInt() + 1; long l = Arrays.stream(L).max().getAsLong(), r = Arrays.stream(L).sum(); while (l < r - 1) { long mid = (l + r) / 2; long curr = 0; int nRows = 1; for (int i = 0; i < N; i++) { curr += L[i]; if (curr > mid) { nRows++; curr = L[i]; } } if (nRows > M) l = mid; else r = mid; } out.println(r - 1); } 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; } } } import java.util.*; import java.io.*; public class Main { static final FastReader sc = new FastReader(); static final PrintWriter out = new PrintWriter(System.out, true); private static boolean debug = System.getProperty("ONLINE_JUDGE") == null; static void trace(Object... o) { if (debug) { System.err.println(Arrays.deepToString(o)); } } public static void main(String[] args) { int N = sc.nextInt(), M = sc.nextInt(); long[] L = new long[N]; for (int i = 0; i < N; i++) L[i] = sc.nextInt() + 1; long l = Arrays.stream(L).max().getAsLong() - 1, r = Arrays.stream(L).sum(); while (l < r - 1) { long mid = (l + r) / 2; long curr = 0; int nRows = 1; for (int i = 0; i < N; i++) { curr += L[i]; if (curr > mid) { nRows++; curr = L[i]; } } if (nRows > M) l = mid; else r = mid; } out.println(r - 1); } 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; } } }
ConDefects/ConDefects/Code/abc319_d/Java/45667550
condefects-java_data_21
import java.io.PrintWriter; import java.util.Scanner; public class Main { public static String solve(String s, int n) { if (n == 1) return s; if(s.lastIndexOf('A')>s.indexOf('B')) return "A"; else return "B"; } public static void main(String[] args) { var sc = new Scanner(System.in); var out = new PrintWriter(System.out, false); //true for Interactive try { for (int i = sc.nextInt(); i > 0; i--) { int n = sc.nextInt(); String s = sc.next(); solve(s, n); } out.flush(); } catch (Exception e) { out.flush(); throw e; } } } import java.io.PrintWriter; import java.util.Scanner; public class Main { public static String solve(String s, int n) { if (n == 1) return s; if(s.lastIndexOf('A')>s.indexOf('B')) return "A"; else return "B"; } public static void main(String[] args) { var sc = new Scanner(System.in); var out = new PrintWriter(System.out, false); //true for Interactive try { for (int i = sc.nextInt(); i > 0; i--) { int n = sc.nextInt(); String s = sc.next(); out.println(solve(s, n)); } out.flush(); } catch (Exception e) { out.flush(); throw e; } } }
ConDefects/ConDefects/Code/agc062_a/Java/41785329
condefects-java_data_22
import java.util.Scanner; public class Main { public static void main(String[] args) { var sc = new Scanner(System.in); var t = sc.nextInt(); var n = new int[t]; var s = new String[t]; for (var i = 0; i < t; i++) { n[i] = sc.nextInt(); s[i] = sc.next(); } sc.close(); var sb = new StringBuilder(); for (var i = 0; i < t; i++) { var result = calc(n[i], s[i]); sb.append(result); sb.append("\r\n"); } System.out.println(sb.toString()); } private static String calc(int n, String s) { var chars = s.toCharArray(); var count1 = 0; var count2 = 0; for (var i = 0; i < n; i++) { if (chars[i] == 'B') { break; } count1++; } for (var i = n-1; i >= 0; i--) { if (chars[i] == 'A') { break; } count2++; } if (count1 + count2 == n && count2 >= count1) { return "B"; } return "A"; } } import java.util.Scanner; public class Main { public static void main(String[] args) { var sc = new Scanner(System.in); var t = sc.nextInt(); var n = new int[t]; var s = new String[t]; for (var i = 0; i < t; i++) { n[i] = sc.nextInt(); s[i] = sc.next(); } sc.close(); var sb = new StringBuilder(); for (var i = 0; i < t; i++) { var result = calc(n[i], s[i]); sb.append(result); sb.append("\r\n"); } System.out.println(sb.toString()); } private static String calc(int n, String s) { var chars = s.toCharArray(); var count1 = 0; var count2 = 0; for (var i = 0; i < n; i++) { if (chars[i] == 'B') { break; } count1++; } for (var i = n-1; i >= 0; i--) { if (chars[i] == 'A') { break; } count2++; } if (count1 + count2 == n && count2 > 0) { return "B"; } return "A"; } }
ConDefects/ConDefects/Code/agc062_a/Java/41606513
condefects-java_data_23
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); System.out.println(t); for (int i = 0; i < t; i++) { System.out.println(i); int n = sc.nextInt(); String s = sc.next(); String newS = s.replaceAll("A", ""); if (newS.length() == 0) { System.out.println("A"); continue; } newS = s.replace("BA", ""); if (newS.length() != s.length()) { System.out.println("A"); continue; } System.out.println("B"); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); String s = sc.next(); String newS = s.replaceAll("A", ""); if (newS.length() == 0) { System.out.println("A"); continue; } newS = s.replace("BA", ""); if (newS.length() != s.length()) { System.out.println("A"); continue; } System.out.println("B"); } } }
ConDefects/ConDefects/Code/agc062_a/Java/41607744
condefects-java_data_24
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int S = scanner.nextInt(); int M = scanner.nextInt(); int L = scanner.nextInt(); int MinCost = 10000; for (int Scount = 0; Scount <= (N + 5) / 6; Scount++) { for (int Mcount = 0; Mcount <= (N - 6 * Scount + 7) / 8; Mcount++) { for (int Lcount = 0; Lcount <= (N - 6 * Scount - 8 * Mcount + 11) / 12; Lcount++){ if (6 * Scount + 8 * Mcount + 12 * Lcount >= N) { int Cost = S * Scount + M * Mcount + L * Lcount; if (Cost < MinCost) { MinCost = Cost; } } } } } System.out.println(MinCost); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int S = scanner.nextInt(); int M = scanner.nextInt(); int L = scanner.nextInt(); int MinCost = Integer.MAX_VALUE; for (int Scount = 0; Scount <= (N + 5) / 6; Scount++) { for (int Mcount = 0; Mcount <= (N - 6 * Scount + 7) / 8; Mcount++) { for (int Lcount = 0; Lcount <= (N - 6 * Scount - 8 * Mcount + 11) / 12; Lcount++){ if (6 * Scount + 8 * Mcount + 12 * Lcount >= N) { int Cost = S * Scount + M * Mcount + L * Lcount; if (Cost < MinCost) { MinCost = Cost; } } } } } System.out.println(MinCost); } }
ConDefects/ConDefects/Code/abc331_b/Java/50653853
condefects-java_data_25
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int S = sc.nextInt(); int M = sc.nextInt(); int L = sc.nextInt(); int ans = Integer.MAX_VALUE; for (int i=0; i<=18; i++) { for (int j=0; j<=18; j++) { for (int k=0; k<=18; k++) { if (6*i + 8*j + 12*k >= N) { ans = Math.max(ans, S*i + M*j + L*k); } } } } System.out.println(ans); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int S = sc.nextInt(); int M = sc.nextInt(); int L = sc.nextInt(); int ans = Integer.MAX_VALUE; for (int i=0; i<=18; i++) { for (int j=0; j<=18; j++) { for (int k=0; k<=18; k++) { if (6*i + 8*j + 12*k >= N) { ans = Math.min(ans, S*i + M*j + L*k); } } } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc331_b/Java/50650521
condefects-java_data_26
import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.lang.reflect.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; class Solver extends BaseSolver{ public Solver(MyReader in,MyWriter out,MyWriter log){ super(in,out,log); } public static boolean multi = false; public Object solve(){ int N = in.it(); long[][] P = in.lg(N,N); long[][] R = in.lg(N,N -1); long[][] D = in.lg(N -1,N); long[][] dp = new long[N][N]; long[][] nokori = new long[N][N]; for (int i = 0;i < N;i++) for (int j = 0;j < N;j++) dp[i][j] = infL; dp[0][0] = 0; for (int i = 0;i < N;i++) for (int j = 0;j < N;j++) { long[][] sub = new long[N][N]; for (var ls:sub) fill(ls,infL); sub[i][j] = 0; for (int ni = i;ni < N;ni++) for (int nj = j;nj < N;nj++) { if (ni == i && nj == j) continue; if (0 < ni) sub[ni][nj] = min(sub[ni][nj],sub[ni -1][nj] +D[ni -1][nj]); if (0 < nj) sub[ni][nj] = min(sub[ni][nj],sub[ni][nj -1] +R[ni][nj -1]); long ceil = ceil(max(0,sub[ni][nj] -nokori[i][j]),P[i][j]); long tmp = dp[i][j] +ceil +ni -i +nj -j; if (dp[ni][nj] > tmp) { dp[ni][nj] = tmp; nokori[ni][nj] = ceil *P[i][j] +nokori[i][j] -sub[ni][nj]; } } } return dp[N -1][N -1]; } <T extends BaseV> void log(AVLSegmentTree<T, ?> seg,int n){ T[] a = (T[]) new BaseV[n]; for (int i = 0;i < n;i++) a[i] = seg.get(i); log.println(a); } } class Data extends BaseV{ int v; public Data(int v){ this.v = v; } @Override public int hashCode(){ return Objects.hash(v); } @Override public boolean equals(Object obj){ if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Data other = (Data) obj; return v == other.v; } @Override public String toString(){ return "" +v; } } abstract class AVLSegmentTree<V extends BaseV, F> { Map<V, Node> map = new HashMap<>(); private V e = e(),t = e(); private Node root; public AVLSegmentTree(int n){ root = new Node(e(),n); } public int idx(Node nd){ int ret = 0; Node pre = null; while (nd != null && nd != root) { pre = nd; nd = nd.par; if (nd != null && nd.rht == pre) ret += nd.lft.sz; } return ret; } public AVLSegmentTree(){} public void build(int n,IntFunction<V> init){ root = build(0,n,init); } private Node build(int i,int n,IntFunction<V> init){ if (n < 2) { AVLSegmentTree<V, F>.Node ret = n < 1 ? null : new Node(init.apply(i),1); map.put(init.apply(i),ret); return ret; } var ret = new Node(e(),n); ret.cld(-1,build(i,n /2,init)); ret.cld(1,build(i +n /2,n -n /2,init)); return ret.merge(); } public void add(V v){ add(v,1); } public void add(V v,int k){ ins(size(),v,k); } public void ins(int i,V v){ ins(i,v,1); } public void ins(int i,V v,int k){ root = root == null ? new Node(v,k) : ins(root,i,v,k); } private Node ins(Node nd,int i,V v,int k){ if (nd.lft == null && (i == 0 || i == nd.sz)) { split(nd,i == 0 ? 1 : -1,v,k,nd.sz +k); return nd.merge(); } if (nd.lft == null) split(nd,1,ag(e(),e,nd.val),i,nd.sz); else nd.push(); if (i < nd.lft.sz) nd.cld(-1,ins(nd.lft,i,v,k)); else nd.cld(1,ins(nd.rht,i -nd.lft.sz,v,k)); return balance(nd); } public V del(int i){ var ret = e(); root = del(ret,root,i); return ret; } private Node del(V ret,Node nd,int i){ if (nd.lft == null) { nd.sz--; ag(ret,e,nd.val); return 0 < nd.sz ? nd : null; } nd.push(); int c = i < nd.lft.sz ? -1 : 1; Node del = c < 0 ? del(ret,nd.lft,i) : del(ret,nd.rht,i -nd.lft.sz); if (del == null) return nd.cld(-c); nd.cld(c,del); return balance(nd); } public void upd(int i,F f){ upd(i,i +1,f); } public void upd(int l,int r,F f){ if (l == r) return; if (size() < r) add(e(),r -size()); root = upd(root,l,r,f); } private Node upd(Node nd,int l,int r,F f){ if (l == 0 && r == nd.sz) return nd.prop(f); if (nd.lft == null) split(nd,1,ag(e(),e,nd.val),0 < l ? l : r,nd.sz); else nd.push(); if (l < nd.lft.sz) nd.cld(-1,upd(nd.lft,l,min(nd.lft.sz,r),f)); if (nd.lft.sz < r) nd.cld(1,upd(nd.rht,max(0,l -nd.lft.sz),r -nd.lft.sz,f)); return balance(nd); } public void toggle(int l,int r){ root = l < r ? toggle(root,l,r) : root; } private Node toggle(Node nd,int l,int r){ nd.push(); if (0 < l) { split(nd,l); return merge(nd.lft,nd,toggle(nd.rht,0,r -l)); } if (r < nd.sz) { split(nd,r); return merge(toggle(nd.lft,l,r),nd,nd.rht); } return nd.toggle(); } private void split(Node nd,int i){ if (nd.lft == null) split(nd,1,ag(e(),e,nd.val),i,nd.sz); else { nd.push(); if (i < nd.lft.sz) { split(nd.lft,i); var lft = nd.lft; nd.cld(-1,lft.lft); nd.cld(1,merge(lft.rht,lft,nd.rht)); } else if (nd.lft.sz < i) { split(nd.rht,i -nd.lft.sz); var rht = nd.rht; nd.cld(1,rht.rht); nd.cld(-1,merge(nd.lft,rht,rht.lft)); } } } private Node merge(Node lft,Node nd,Node rht){ if (abs(lft.rnk -rht.rnk) < 2) { nd.cld(-1,lft); nd.cld(1,rht); } else if (lft.rnk > rht.rnk) { lft.push().cld(1,merge(lft.rht,nd,rht)); nd = lft; } else if (lft.rnk < rht.rnk) { rht.push().cld(-1,merge(lft,nd,rht.lft)); nd = rht; } return balance(nd); } public V get(int i){ return get(i,i +1); } public V get(int l,int r){ V ret = e(); if (root != null) get(ret,root,l,min(r,size())); return ret; } private void get(V ret,Node nd,int l,int r){ if (l == 0 && r == nd.sz) ag(ret,ret,nd.val()); else if (nd.lft == null) ag(ret,ret,pw(nd.val,r -l)); else { nd.push(); if (l < nd.lft.sz) get(ret,nd.lft,l,min(nd.lft.sz,r)); if (nd.lft.sz < r) get(ret,nd.rht,max(0,l -nd.lft.sz),r -nd.lft.sz); } } public V all(){ return root == null ? e : root.val(); } public int size(){ return root == null ? 0 : root.sz; } protected abstract V e(); protected abstract void agg(V v,V a,V b); protected abstract void map(V v,F f); protected abstract F comp(F f,F g); protected abstract void tog(V v); private V ag(V v,V a,V b){ agg(v,a,b); v.sz = a.sz +b.sz; return v; } protected void pow(V v,V a,int n){ for (ag(t,e,a);0 < n;n >>= 1,ag(t,t,t)) if (0 < (n &1)) ag(v,v,t); } private V pw(V a,int n){ V ret = e(); pow(ret,a,n); ret.sz = n; return ret; } private void split(Node nd,int c,V vl,int i,int sz){ Node nd2 = new Node(vl,i); map.put(vl,nd2); nd.cld(-c,nd2); Node nd3 = new Node(nd.val,sz -i); map.put(nd.val,nd3); nd.cld(c,nd3); nd.val = e(); } private Node balance(Node nd){ return (1 < abs(nd.bis = nd.rht.rnk -nd.lft.rnk) ? (nd = rotate(nd)) : nd).merge(); } private Node rotate(Node u){ var v = u.cld(u.bis).push(); if (u.bis *v.bis < -1) v = rotate(v); u.cld(u.bis,v.cld(-u.bis)); v.cld(-u.bis,u); u.merge(); return v; } public class Node{ private int sz,bis,rnk,tog; private V val; private F laz; private Node lft,rht,par; private Node(V val,int sz){ this.sz = sz; this.val = val; val.sz = 1; } private Node merge(){ bis = rht.rnk -lft.rnk; rnk = max(lft.rnk,rht.rnk) +1; ag(val,lft.val(),rht.val()); sz = val.sz; return this; } private Node push(){ if (laz != null) { lft.prop(laz); rht.prop(laz); laz = null; } if (0 < tog) { lft.toggle(); rht.toggle(); tog = 0; } return this; } private Node prop(F f){ map(val,f); if (lft != null) laz = laz == null ? f : comp(laz,f); return this; } private Node toggle(){ bis *= -1; var tn = lft; lft = rht; rht = tn; tog(val); if (lft != null) tog ^= 1; return this; } private Node cld(int c){ return c < 0 ? lft : rht; } private void cld(int c,Node nd){ nd.par = this; nd = c < 0 ? (lft = nd) : (rht = nd); } private V val(){ return lft == null && 1 < sz ? pw(val,sz) : val; } } } abstract class BaseV{ public int sz; public boolean fail; } class MyStack<T> extends MyList<T>{ public T pop(){ return remove(size() -1); } public T peek(){ return get(size() -1); } } class MyList<T> implements Iterable<T>{ private T[] arr; private int sz; public MyList(){ this(16); } public MyList(int n){ arr = Util.cast(new Object[n]); } public boolean isEmpty(){ return sz == 0; } public int size(){ return sz; } public T get(int i){ return arr[i]; } public void add(T t){ (arr = sz < arr.length ? arr : copyOf(arr,sz *5 >>2))[sz++] = t; } public T remove(int i){ var ret = arr[i]; sz--; for (int j = i;j < sz;j++) arr[j] = arr[j +1]; return ret; } public T removeFast(int i){ var ret = arr[i]; arr[i] = arr[--sz]; return ret; } public void sort(){ sort(Util.cast(Comparator.naturalOrder())); } public void sort(Comparator<T> cmp){ Arrays.sort(arr,0,sz,cmp); } @Override public Iterator<T> iterator(){ return new Iterator<>(){ int i = 0; @Override public boolean hasNext(){ return i < sz; } @Override public T next(){ return arr[i++]; } }; } public <U> MyList<U> map(Function<T, U> func){ MyList<U> ret = new MyList<>(sz); forEach(t -> ret.add(func.apply(t))); return ret; } public T[] toArray(){ return copyOf(arr,sz); } public void swap(int i,int j){ var t = arr[i]; arr[i] = arr[j]; arr[j] = t; } public void set(int i,T t){ arr[i] = t; } } class BaseSolver extends Util{ public MyReader in; public MyWriter out,log; public BaseSolver(MyReader in,MyWriter out,MyWriter log){ this.in = in; this.out = out; this.log = log; } protected long inv(long x){ return pow(x,mod -2); } protected long pow(long x,long n){ return pow(x,n,Util.mod); } protected long pow(long x,long n,long mod){ long ret = 1; for (x %= mod;0 < n;x = x *x %mod,n >>= 1) if ((n &1) == 1) ret = ret *x %mod; return ret; } protected int bSearchI(int o,int n,IntPredicate judge){ if (!judge.test(o)) return o -Integer.signum(n -o); for (int m = 0;1 < abs(n -o);) m = judge.test(m = o +n >>1) ? (o = m) : (n = m); return o; } protected long bSearchL(long o,long n,LongPredicate judge){ for (long m = 0;1 < abs(n -o);) m = judge.test(m = o +n >>1) ? (o = m) : (n = m); return o; } protected double bSearchD(double o,double n,DoublePredicate judge){ for (double m,c = 0;c < 100;c++) m = judge.test(m = (o +n) /2) ? (o = m) : (n = m); return o; } protected long gcd(long a,long b){ while (0 < b) { long t = a; a = b; b = t %b; } return a; } protected long ceil(long a,long b){ return (a +b -1) /b; } } class Util{ public static String yes = "Yes",no = "No"; public static int infI = (1 <<30) -1; public static long infL = (1L <<60 |1 <<30) -1, mod = 998244353; public static Random rd = ThreadLocalRandom.current(); private long st = System.currentTimeMillis(); protected long elapsed(){ return System.currentTimeMillis() -st; } protected void reset(){ st = System.currentTimeMillis(); } public static int[] arrI(int N,IntUnaryOperator f){ int[] ret = new int[N]; setAll(ret,f); return ret; } public static long[] arrL(int N,IntToLongFunction f){ long[] ret = new long[N]; setAll(ret,f); return ret; } public static double[] arrD(int N,IntToDoubleFunction f){ double[] ret = new double[N]; setAll(ret,f); return ret; } public static <T> T[] arr(T[] arr,IntFunction<T> f){ setAll(arr,f); return arr; } public int[][] addId(int[][] T){ return arr(new int[T.length][],i -> { int[] t = copyOf(T[i],T[i].length +1); t[t.length -1] = i; return t; }); } @SuppressWarnings("unchecked") public static <T> T cast(Object obj){ return (T) obj; } } class MyReader{ private byte[] buf = new byte[1 <<16]; private int ptr,tail; private InputStream in; public MyReader(InputStream in){ this.in = in; } private byte read(){ if (ptr == tail) try { tail = in.read(buf); ptr = 0; } catch (IOException e) {} return buf[ptr++]; } private boolean isPrintable(byte c){ return 32 < c && c < 127; } private byte nextPrintable(){ byte ret = read(); while (!isPrintable(ret)) ret = read(); return ret; } public int it(){ return toIntExact(lg()); } public int[] it(int N){ return Util.arrI(N,i -> it()); } public int[][] it(int H,int W){ return Util.arr(new int[H][],i -> it(W)); } public int idx(){ return it() -1; } public int[] idx(int N){ return Util.arrI(N,i -> idx()); } public int[][] idx(int H,int W){ return Util.arr(new int[H][],i -> idx(W)); } public long lg(){ byte i = nextPrintable(); boolean negative = i == 45; long n = negative ? 0 : i -'0'; while (isPrintable(i = read())) n = 10 *n +i -'0'; return negative ? -n : n; } public long[] lg(int N){ return Util.arrL(N,i -> lg()); } public long[][] lg(int H,int W){ return Util.arr(new long[H][],i -> lg(W)); } public double dbl(){ return Double.parseDouble(str()); } public double[] dbl(int N){ return Util.arrD(N,i -> dbl()); } public double[][] dbl(int H,int W){ return Util.arr(new double[H][],i -> dbl(W)); } public char[] ch(){ return str().toCharArray(); } public char[][] ch(int H){ return Util.arr(new char[H][],i -> ch()); } public String line(){ StringBuilder sb = new StringBuilder(); for (byte c;(c = read()) != '\n';) sb.append((char) c); return sb.toString(); } public String str(){ StringBuilder sb = new StringBuilder(); sb.append((char) nextPrintable()); for (byte c;isPrintable(c = read());) sb.append((char) c); return sb.toString(); } public String[] str(int N){ return Util.arr(new String[N],i -> str()); } } class MyWriter{ private OutputStream out; private byte[] buf = new byte[1 <<16],ibuf = new byte[20]; private int tail; private boolean autoflush; public MyWriter(OutputStream out,boolean autoflush){ this.out = out; this.autoflush = autoflush; } public void flush(){ try { out.write(buf,0,tail); tail = 0; } catch (IOException e) { e.printStackTrace(); } } private void ln(){ write((byte) '\n'); if (autoflush) flush(); } private void write(byte b){ buf[tail++] = b; if (tail == buf.length) flush(); } private void write(long n){ if (n < 0) { n = -n; write((byte) '-'); } int i = ibuf.length; do { ibuf[--i] = (byte) (n %10 +'0'); n /= 10; } while (n > 0); while (i < ibuf.length) write(ibuf[i++]); } private void print(Object obj){ if (obj instanceof Boolean) print((boolean) obj ? Util.yes : Util.no); else if (obj instanceof Integer) write((int) obj); else if (obj instanceof Long) write((long) obj); else if (obj instanceof char[]) for (char b:(char[]) obj) write((byte) b); else if (obj.getClass().isArray()) { int l = Array.getLength(obj); for (int i = 0;i < l;i++) { print(Array.get(obj,i)); if (i +1 < l) write((byte) ' '); } } else print(Objects.toString(obj).toCharArray()); } public void println(Object obj){ if (obj == null) obj = "null"; if (obj instanceof Iterable<?>) for (Object e:(Iterable<?>) obj) println(e); else if (obj.getClass().isArray() && Array.getLength(obj) > 0 && Array.get(obj,0).getClass().isArray()) { int l = Array.getLength(obj); for (int i = 0;i < l;i++) println(Array.get(obj,i)); } else { print(obj); ln(); } } public void printlns(Object... o){ print(o); ln(); } } class Main{ public static void main(String[] args) throws Exception{ var in = new MyReader(System.in); var out = new MyWriter(System.out,false); var log = new MyWriter(System.err,true); int T = Solver.multi ? in.it() : 1; while (T-- > 0) Optional.ofNullable(new Solver(in,out,log) .solve()).ifPresent(out::println); out.flush(); } } import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.lang.reflect.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; class Solver extends BaseSolver{ public Solver(MyReader in,MyWriter out,MyWriter log){ super(in,out,log); } public static boolean multi = false; public Object solve(){ int N = in.it(); long[][] P = in.lg(N,N); long[][] R = in.lg(N,N -1); long[][] D = in.lg(N -1,N); long[][] dp = new long[N][N]; long[][] nokori = new long[N][N]; for (int i = 0;i < N;i++) for (int j = 0;j < N;j++) dp[i][j] = infL; dp[0][0] = 0; for (int i = 0;i < N;i++) for (int j = 0;j < N;j++) { long[][] sub = new long[N][N]; for (var ls:sub) fill(ls,infL); sub[i][j] = 0; for (int ni = i;ni < N;ni++) for (int nj = j;nj < N;nj++) { if (ni == i && nj == j) continue; if (0 < ni) sub[ni][nj] = min(sub[ni][nj],sub[ni -1][nj] +D[ni -1][nj]); if (0 < nj) sub[ni][nj] = min(sub[ni][nj],sub[ni][nj -1] +R[ni][nj -1]); long ceil = ceil(max(0,sub[ni][nj] -nokori[i][j]),P[i][j]); long tmp = dp[i][j] +ceil +ni -i +nj -j; if (dp[ni][nj] > tmp || dp[ni][nj] == tmp && nokori[ni][nj] < ceil *P[i][j] +nokori[i][j] -sub[ni][nj]) { dp[ni][nj] = tmp; nokori[ni][nj] = ceil *P[i][j] +nokori[i][j] -sub[ni][nj]; } } } return dp[N -1][N -1]; } <T extends BaseV> void log(AVLSegmentTree<T, ?> seg,int n){ T[] a = (T[]) new BaseV[n]; for (int i = 0;i < n;i++) a[i] = seg.get(i); log.println(a); } } class Data extends BaseV{ int v; public Data(int v){ this.v = v; } @Override public int hashCode(){ return Objects.hash(v); } @Override public boolean equals(Object obj){ if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Data other = (Data) obj; return v == other.v; } @Override public String toString(){ return "" +v; } } abstract class AVLSegmentTree<V extends BaseV, F> { Map<V, Node> map = new HashMap<>(); private V e = e(),t = e(); private Node root; public AVLSegmentTree(int n){ root = new Node(e(),n); } public int idx(Node nd){ int ret = 0; Node pre = null; while (nd != null && nd != root) { pre = nd; nd = nd.par; if (nd != null && nd.rht == pre) ret += nd.lft.sz; } return ret; } public AVLSegmentTree(){} public void build(int n,IntFunction<V> init){ root = build(0,n,init); } private Node build(int i,int n,IntFunction<V> init){ if (n < 2) { AVLSegmentTree<V, F>.Node ret = n < 1 ? null : new Node(init.apply(i),1); map.put(init.apply(i),ret); return ret; } var ret = new Node(e(),n); ret.cld(-1,build(i,n /2,init)); ret.cld(1,build(i +n /2,n -n /2,init)); return ret.merge(); } public void add(V v){ add(v,1); } public void add(V v,int k){ ins(size(),v,k); } public void ins(int i,V v){ ins(i,v,1); } public void ins(int i,V v,int k){ root = root == null ? new Node(v,k) : ins(root,i,v,k); } private Node ins(Node nd,int i,V v,int k){ if (nd.lft == null && (i == 0 || i == nd.sz)) { split(nd,i == 0 ? 1 : -1,v,k,nd.sz +k); return nd.merge(); } if (nd.lft == null) split(nd,1,ag(e(),e,nd.val),i,nd.sz); else nd.push(); if (i < nd.lft.sz) nd.cld(-1,ins(nd.lft,i,v,k)); else nd.cld(1,ins(nd.rht,i -nd.lft.sz,v,k)); return balance(nd); } public V del(int i){ var ret = e(); root = del(ret,root,i); return ret; } private Node del(V ret,Node nd,int i){ if (nd.lft == null) { nd.sz--; ag(ret,e,nd.val); return 0 < nd.sz ? nd : null; } nd.push(); int c = i < nd.lft.sz ? -1 : 1; Node del = c < 0 ? del(ret,nd.lft,i) : del(ret,nd.rht,i -nd.lft.sz); if (del == null) return nd.cld(-c); nd.cld(c,del); return balance(nd); } public void upd(int i,F f){ upd(i,i +1,f); } public void upd(int l,int r,F f){ if (l == r) return; if (size() < r) add(e(),r -size()); root = upd(root,l,r,f); } private Node upd(Node nd,int l,int r,F f){ if (l == 0 && r == nd.sz) return nd.prop(f); if (nd.lft == null) split(nd,1,ag(e(),e,nd.val),0 < l ? l : r,nd.sz); else nd.push(); if (l < nd.lft.sz) nd.cld(-1,upd(nd.lft,l,min(nd.lft.sz,r),f)); if (nd.lft.sz < r) nd.cld(1,upd(nd.rht,max(0,l -nd.lft.sz),r -nd.lft.sz,f)); return balance(nd); } public void toggle(int l,int r){ root = l < r ? toggle(root,l,r) : root; } private Node toggle(Node nd,int l,int r){ nd.push(); if (0 < l) { split(nd,l); return merge(nd.lft,nd,toggle(nd.rht,0,r -l)); } if (r < nd.sz) { split(nd,r); return merge(toggle(nd.lft,l,r),nd,nd.rht); } return nd.toggle(); } private void split(Node nd,int i){ if (nd.lft == null) split(nd,1,ag(e(),e,nd.val),i,nd.sz); else { nd.push(); if (i < nd.lft.sz) { split(nd.lft,i); var lft = nd.lft; nd.cld(-1,lft.lft); nd.cld(1,merge(lft.rht,lft,nd.rht)); } else if (nd.lft.sz < i) { split(nd.rht,i -nd.lft.sz); var rht = nd.rht; nd.cld(1,rht.rht); nd.cld(-1,merge(nd.lft,rht,rht.lft)); } } } private Node merge(Node lft,Node nd,Node rht){ if (abs(lft.rnk -rht.rnk) < 2) { nd.cld(-1,lft); nd.cld(1,rht); } else if (lft.rnk > rht.rnk) { lft.push().cld(1,merge(lft.rht,nd,rht)); nd = lft; } else if (lft.rnk < rht.rnk) { rht.push().cld(-1,merge(lft,nd,rht.lft)); nd = rht; } return balance(nd); } public V get(int i){ return get(i,i +1); } public V get(int l,int r){ V ret = e(); if (root != null) get(ret,root,l,min(r,size())); return ret; } private void get(V ret,Node nd,int l,int r){ if (l == 0 && r == nd.sz) ag(ret,ret,nd.val()); else if (nd.lft == null) ag(ret,ret,pw(nd.val,r -l)); else { nd.push(); if (l < nd.lft.sz) get(ret,nd.lft,l,min(nd.lft.sz,r)); if (nd.lft.sz < r) get(ret,nd.rht,max(0,l -nd.lft.sz),r -nd.lft.sz); } } public V all(){ return root == null ? e : root.val(); } public int size(){ return root == null ? 0 : root.sz; } protected abstract V e(); protected abstract void agg(V v,V a,V b); protected abstract void map(V v,F f); protected abstract F comp(F f,F g); protected abstract void tog(V v); private V ag(V v,V a,V b){ agg(v,a,b); v.sz = a.sz +b.sz; return v; } protected void pow(V v,V a,int n){ for (ag(t,e,a);0 < n;n >>= 1,ag(t,t,t)) if (0 < (n &1)) ag(v,v,t); } private V pw(V a,int n){ V ret = e(); pow(ret,a,n); ret.sz = n; return ret; } private void split(Node nd,int c,V vl,int i,int sz){ Node nd2 = new Node(vl,i); map.put(vl,nd2); nd.cld(-c,nd2); Node nd3 = new Node(nd.val,sz -i); map.put(nd.val,nd3); nd.cld(c,nd3); nd.val = e(); } private Node balance(Node nd){ return (1 < abs(nd.bis = nd.rht.rnk -nd.lft.rnk) ? (nd = rotate(nd)) : nd).merge(); } private Node rotate(Node u){ var v = u.cld(u.bis).push(); if (u.bis *v.bis < -1) v = rotate(v); u.cld(u.bis,v.cld(-u.bis)); v.cld(-u.bis,u); u.merge(); return v; } public class Node{ private int sz,bis,rnk,tog; private V val; private F laz; private Node lft,rht,par; private Node(V val,int sz){ this.sz = sz; this.val = val; val.sz = 1; } private Node merge(){ bis = rht.rnk -lft.rnk; rnk = max(lft.rnk,rht.rnk) +1; ag(val,lft.val(),rht.val()); sz = val.sz; return this; } private Node push(){ if (laz != null) { lft.prop(laz); rht.prop(laz); laz = null; } if (0 < tog) { lft.toggle(); rht.toggle(); tog = 0; } return this; } private Node prop(F f){ map(val,f); if (lft != null) laz = laz == null ? f : comp(laz,f); return this; } private Node toggle(){ bis *= -1; var tn = lft; lft = rht; rht = tn; tog(val); if (lft != null) tog ^= 1; return this; } private Node cld(int c){ return c < 0 ? lft : rht; } private void cld(int c,Node nd){ nd.par = this; nd = c < 0 ? (lft = nd) : (rht = nd); } private V val(){ return lft == null && 1 < sz ? pw(val,sz) : val; } } } abstract class BaseV{ public int sz; public boolean fail; } class MyStack<T> extends MyList<T>{ public T pop(){ return remove(size() -1); } public T peek(){ return get(size() -1); } } class MyList<T> implements Iterable<T>{ private T[] arr; private int sz; public MyList(){ this(16); } public MyList(int n){ arr = Util.cast(new Object[n]); } public boolean isEmpty(){ return sz == 0; } public int size(){ return sz; } public T get(int i){ return arr[i]; } public void add(T t){ (arr = sz < arr.length ? arr : copyOf(arr,sz *5 >>2))[sz++] = t; } public T remove(int i){ var ret = arr[i]; sz--; for (int j = i;j < sz;j++) arr[j] = arr[j +1]; return ret; } public T removeFast(int i){ var ret = arr[i]; arr[i] = arr[--sz]; return ret; } public void sort(){ sort(Util.cast(Comparator.naturalOrder())); } public void sort(Comparator<T> cmp){ Arrays.sort(arr,0,sz,cmp); } @Override public Iterator<T> iterator(){ return new Iterator<>(){ int i = 0; @Override public boolean hasNext(){ return i < sz; } @Override public T next(){ return arr[i++]; } }; } public <U> MyList<U> map(Function<T, U> func){ MyList<U> ret = new MyList<>(sz); forEach(t -> ret.add(func.apply(t))); return ret; } public T[] toArray(){ return copyOf(arr,sz); } public void swap(int i,int j){ var t = arr[i]; arr[i] = arr[j]; arr[j] = t; } public void set(int i,T t){ arr[i] = t; } } class BaseSolver extends Util{ public MyReader in; public MyWriter out,log; public BaseSolver(MyReader in,MyWriter out,MyWriter log){ this.in = in; this.out = out; this.log = log; } protected long inv(long x){ return pow(x,mod -2); } protected long pow(long x,long n){ return pow(x,n,Util.mod); } protected long pow(long x,long n,long mod){ long ret = 1; for (x %= mod;0 < n;x = x *x %mod,n >>= 1) if ((n &1) == 1) ret = ret *x %mod; return ret; } protected int bSearchI(int o,int n,IntPredicate judge){ if (!judge.test(o)) return o -Integer.signum(n -o); for (int m = 0;1 < abs(n -o);) m = judge.test(m = o +n >>1) ? (o = m) : (n = m); return o; } protected long bSearchL(long o,long n,LongPredicate judge){ for (long m = 0;1 < abs(n -o);) m = judge.test(m = o +n >>1) ? (o = m) : (n = m); return o; } protected double bSearchD(double o,double n,DoublePredicate judge){ for (double m,c = 0;c < 100;c++) m = judge.test(m = (o +n) /2) ? (o = m) : (n = m); return o; } protected long gcd(long a,long b){ while (0 < b) { long t = a; a = b; b = t %b; } return a; } protected long ceil(long a,long b){ return (a +b -1) /b; } } class Util{ public static String yes = "Yes",no = "No"; public static int infI = (1 <<30) -1; public static long infL = (1L <<60 |1 <<30) -1, mod = 998244353; public static Random rd = ThreadLocalRandom.current(); private long st = System.currentTimeMillis(); protected long elapsed(){ return System.currentTimeMillis() -st; } protected void reset(){ st = System.currentTimeMillis(); } public static int[] arrI(int N,IntUnaryOperator f){ int[] ret = new int[N]; setAll(ret,f); return ret; } public static long[] arrL(int N,IntToLongFunction f){ long[] ret = new long[N]; setAll(ret,f); return ret; } public static double[] arrD(int N,IntToDoubleFunction f){ double[] ret = new double[N]; setAll(ret,f); return ret; } public static <T> T[] arr(T[] arr,IntFunction<T> f){ setAll(arr,f); return arr; } public int[][] addId(int[][] T){ return arr(new int[T.length][],i -> { int[] t = copyOf(T[i],T[i].length +1); t[t.length -1] = i; return t; }); } @SuppressWarnings("unchecked") public static <T> T cast(Object obj){ return (T) obj; } } class MyReader{ private byte[] buf = new byte[1 <<16]; private int ptr,tail; private InputStream in; public MyReader(InputStream in){ this.in = in; } private byte read(){ if (ptr == tail) try { tail = in.read(buf); ptr = 0; } catch (IOException e) {} return buf[ptr++]; } private boolean isPrintable(byte c){ return 32 < c && c < 127; } private byte nextPrintable(){ byte ret = read(); while (!isPrintable(ret)) ret = read(); return ret; } public int it(){ return toIntExact(lg()); } public int[] it(int N){ return Util.arrI(N,i -> it()); } public int[][] it(int H,int W){ return Util.arr(new int[H][],i -> it(W)); } public int idx(){ return it() -1; } public int[] idx(int N){ return Util.arrI(N,i -> idx()); } public int[][] idx(int H,int W){ return Util.arr(new int[H][],i -> idx(W)); } public long lg(){ byte i = nextPrintable(); boolean negative = i == 45; long n = negative ? 0 : i -'0'; while (isPrintable(i = read())) n = 10 *n +i -'0'; return negative ? -n : n; } public long[] lg(int N){ return Util.arrL(N,i -> lg()); } public long[][] lg(int H,int W){ return Util.arr(new long[H][],i -> lg(W)); } public double dbl(){ return Double.parseDouble(str()); } public double[] dbl(int N){ return Util.arrD(N,i -> dbl()); } public double[][] dbl(int H,int W){ return Util.arr(new double[H][],i -> dbl(W)); } public char[] ch(){ return str().toCharArray(); } public char[][] ch(int H){ return Util.arr(new char[H][],i -> ch()); } public String line(){ StringBuilder sb = new StringBuilder(); for (byte c;(c = read()) != '\n';) sb.append((char) c); return sb.toString(); } public String str(){ StringBuilder sb = new StringBuilder(); sb.append((char) nextPrintable()); for (byte c;isPrintable(c = read());) sb.append((char) c); return sb.toString(); } public String[] str(int N){ return Util.arr(new String[N],i -> str()); } } class MyWriter{ private OutputStream out; private byte[] buf = new byte[1 <<16],ibuf = new byte[20]; private int tail; private boolean autoflush; public MyWriter(OutputStream out,boolean autoflush){ this.out = out; this.autoflush = autoflush; } public void flush(){ try { out.write(buf,0,tail); tail = 0; } catch (IOException e) { e.printStackTrace(); } } private void ln(){ write((byte) '\n'); if (autoflush) flush(); } private void write(byte b){ buf[tail++] = b; if (tail == buf.length) flush(); } private void write(long n){ if (n < 0) { n = -n; write((byte) '-'); } int i = ibuf.length; do { ibuf[--i] = (byte) (n %10 +'0'); n /= 10; } while (n > 0); while (i < ibuf.length) write(ibuf[i++]); } private void print(Object obj){ if (obj instanceof Boolean) print((boolean) obj ? Util.yes : Util.no); else if (obj instanceof Integer) write((int) obj); else if (obj instanceof Long) write((long) obj); else if (obj instanceof char[]) for (char b:(char[]) obj) write((byte) b); else if (obj.getClass().isArray()) { int l = Array.getLength(obj); for (int i = 0;i < l;i++) { print(Array.get(obj,i)); if (i +1 < l) write((byte) ' '); } } else print(Objects.toString(obj).toCharArray()); } public void println(Object obj){ if (obj == null) obj = "null"; if (obj instanceof Iterable<?>) for (Object e:(Iterable<?>) obj) println(e); else if (obj.getClass().isArray() && Array.getLength(obj) > 0 && Array.get(obj,0).getClass().isArray()) { int l = Array.getLength(obj); for (int i = 0;i < l;i++) println(Array.get(obj,i)); } else { print(obj); ln(); } } public void printlns(Object... o){ print(o); ln(); } } class Main{ public static void main(String[] args) throws Exception{ var in = new MyReader(System.in); var out = new MyWriter(System.out,false); var log = new MyWriter(System.err,true); int T = Solver.multi ? in.it() : 1; while (T-- > 0) Optional.ofNullable(new Solver(in,out,log) .solve()).ifPresent(out::println); out.flush(); } }
ConDefects/ConDefects/Code/abc344_f/Java/51083938
condefects-java_data_27
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; //ctrl + alt + s でスニペット編集 //iter itm if nibutan sout public class Main { static final long MOD1=1000000007; static final long MOD=998244353; static final int NTT_MOD1 = 998244353; static final int NTT_MOD2 = 1053818881; static final int NTT_MOD3 = 1004535809; static final int[] dx = {0, 0, 1, 1}; static final int[] dy = {0, 1, 0, 1}; static int ans = 50; static long MAX = 1000000000000000010L;//10^18 public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int n = sc.nextInt(); long[][] p = new long[n][n]; long[][] r = new long[n][n - 1]; long[][] d = new long[n - 1][n]; for (int i = 0; i < n; i++) { p[i] = sc.nextLongArray(n); } for (int i = 0; i < n; i++) { r[i] = sc.nextLongArray(n - 1); } for (int i = 0; i < n - 1; i++) { d[i] = sc.nextLongArray(n); } PairLong[][] dp = new PairLong[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(dp[i], new PairLong(MAX, MAX)); } dp[0][0] = new PairLong(0, 0); long ans = MAX; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == n - 1 && j == n - 1) { continue; } if (dp[i][j].x == MAX) { continue; } long[][] dist = new long[n][n]; for (int k = 0; k < n; k++) { Arrays.fill(dist[k], MAX); } dist[i][j] = 0; for (int k = i; k < n; k++) { for (int l = j; l < n; l++) { if (k + 1 < n) { dist[k + 1][l] = Math.min(dist[k + 1][l], dist[k][l] + d[k][l]); } if (l + 1 < n) { dist[k][l + 1] = Math.min(dist[k][l + 1], dist[k][l] + r[k][l]); } } } long x = dp[i][j].x; long y = dp[i][j].y; ans = Math.min(ans, x + ceil(Math.max(dist[n - 1][n - 1] - y, 0), p[i][j])); //System.out.println(ans + " " + i + " " + j + " " + x + " " + y + " " + dist[n - 1][n - 1] + " " + p[i][j]); for (int k = i; k < n; k++) { for (int l = j; l < n; l++) { if (k == i && l == j) { continue; } if (p[i][j] >= p[k][l]) { continue; } long nx = x + ceil(Math.max(dist[k][l] - y, 0), p[i][j]); long ny = y + (ceil(Math.max(dist[k][l] - y, 0), p[i][j]) * p[i][j]) - dist[k][l]; if (nx < dp[k][l].x || (nx == dp[k][l].x && ny < dp[k][l].y)) { dp[k][l] = new PairLong(nx, ny); } } } } } out.println(ans + 2 * n - 2); out.flush(); } static class Edge implements Comparable<Edge>{ int to; long v; public Edge(int to,long v) { this.to=to; this.v=v; } @Override public boolean equals(Object obj){ if(obj instanceof Edge) { Edge other = (Edge) obj; return other.to==this.to && other.v==this.v; } return false; }//同値の定義 @Override public int hashCode() { return Objects.hash(this.to,this.v); } @Override public int compareTo( Edge p2 ){ if (this.v>p2.v) { return 1; } else if (this.v<p2.v) { return -1; } else { return 0; } } } static class graph{ ArrayList<Edge>[] list; int size; long INF=Long.MAX_VALUE/2; int[] color; @SuppressWarnings("unchecked") public graph(int n) { size = n; list = new ArrayList[n]; color =new int[n]; for(int i=0;i<n;i++){ list[i] = new ArrayList<Edge>(); } } void addEdge(int from,int to,long w) { list[from].add(new Edge(to,w)); } void addUndirectedEdge(int from,int to,long w) { addEdge(from, to, w); addEdge(to, from, w); } boolean nibugraph(int s,int colors) { color[s]=colors; for (Edge nv: list[s]) { if (color[nv.to]==colors) { return false; } if (color[nv.to]==0&&!nibugraph(nv.to, -colors)) { return false; } } return true; } } static class BinaryTrie { private final static int K = 31; int[] l; int[] r; int[] count; int size; int node_sum = 0; public BinaryTrie(int size) { this.l = new int[(K + 1) * (size + 1)]; this.r = new int[(K + 1) * (size + 1)]; this.count = new int[(K + 1) * (size + 1)]; this.size = size; Arrays.fill(l, -1); Arrays.fill(r, -1); } public int size() { return count[0]; } public boolean isEmpty() { return size() == 0; } public int count(int x) { int node_id = 0; for (int i = K; i >= 0; i--) { if (((x >> i) & 1) == 0) { if (l[node_id] == -1) { return 0; } node_id = l[node_id]; } else { if (r[node_id] == -1) { return 0; } node_id = r[node_id]; } } return count[node_id]; } public void add(int x) { add(x, 1); } public int get(int i) { if (i < 0 || size() <= i) { throw new IndexOutOfBoundsException(); } int node_id = 0; int val = 0; for (int j = 0; j <= K; j++) { val *= 2; int lc = l[node_id] == -1 ? 0 : count[l[node_id]]; if (i < lc) { node_id = l[node_id]; } else { val++; i -= lc; node_id = r[node_id]; } } return val; } public void add(int x, int count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } int node_id = 0; this.count[node_id] += count; for (int i = K; i >= 0; i--) { if (((x >> i) & 1) == 0) { if (l[node_id] == -1) { node_sum++; l[node_id] = node_sum; } node_id = l[node_id]; this.count[node_id] += count; } else { if (r[node_id] == -1) { node_sum++; r[node_id] = node_sum; } node_id = r[node_id]; this.count[node_id] += count; } } } public boolean remove(int x) { return remove(x, 1); } public boolean remove(int x, int count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } count = Math.min(count, count(x)); if (count == 0) { return false; } int node_id = 0; this.count[node_id] -= count; for (int i = K; i >= 0; i--) { if (((x >> i) & 1) == 0) { this.count[l[node_id]] -= count; if (this.count[l[node_id]] == 0) { l[node_id] = -1; return true; } node_id = l[node_id]; } else { this.count[r[node_id]] -= count; if (this.count[r[node_id]] == 0) { r[node_id] = -1; return true; } node_id = r[node_id]; } } return true; } public long min() { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; long val = 0; for (int j = 0; j <= K; j++) { val *= 2; if (l[node_id] != -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } return val; } public int xormin(int x) { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; int val = 0; for (int i = K; i >= 0; i--) { val *= 2; if (((x >> i) & 1) == 1) { if (r[node_id] == -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } }else { if (l[node_id] != -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } } return val; } public int max() { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; int val = 0; for (int j = 0; j <= K; j++) { val *= 2; if (r[node_id] == -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } return val; } public int xormax(long x) { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; int val = 0; for (int i = K; i >= 0; i--) { val *= 2; if (((x >> i) & 1) == 0) { if (r[node_id] == -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } }else { if (l[node_id] != -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } } return val; }//未verify public int lowerBound(int x) { if (isEmpty()) { return 0; } int node_id = 0; int i = 0; for (int j = K; j >= 0; j--) { if (((x >> j) & 1) == 0) { node_id = l[node_id]; } else { i += l[node_id] == -1 ? 0 : count[l[node_id]]; node_id = r[node_id]; } if (node_id == -1) { return i; } } return i; } public int xorlowerBound(int x, int xor) { if (isEmpty()) { return 0; } int node_id = 0; int i = 0; for (int j = K; j >= 0; j--) { if (((xor >> j) & 1) == 0) { if (((x >> j) & 1) == 0) { node_id = l[node_id]; } else { i += l[node_id] == -1 ? 0 : count[l[node_id]]; node_id = r[node_id]; } }else { if (((x >> j) & 1) == 0) { node_id = r[node_id]; } else { i += r[node_id] == -1 ? 0 : count[r[node_id]]; node_id = l[node_id]; } } if (node_id == -1) { return i; } } return i; } } static class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException( String.format("Invalid range: [%d, %d)", l, r) ); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Integer.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX) ); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX) ); } } } static int ceil(int a,int b) { if (a%b==0) { return a/b; }else { return a/b+1; } } static int lower_bound(int[] a, int fromIndex, int toIndex, int val) { if (val > a[toIndex]) return toIndex + 1; if (val <= a[fromIndex]) return fromIndex; int lb = fromIndex - 1, ub = toIndex; while (ub - lb > 1) { int mid = (ub - lb)/2+lb; if (a[mid] >= val) { ub = mid; } else { lb = mid; } } return ub; } static int lower_bound(long[] a, int fromIndex, int toIndex, long val) { if (val > a[toIndex]) return toIndex + 1; if (val <= a[fromIndex]) return fromIndex; int lb = fromIndex - 1, ub = toIndex; while (ub - lb > 1) { int mid = (ub - lb)/2+lb; if (a[mid] >= val) { ub = mid; } else { lb = mid; } } return ub; } static class UnionFindTree{ int[] par; int[] rank; int[] size; long[] diff_weight; int arraysize; public UnionFindTree(int n) { this.par=new int[n]; this.rank=new int[n]; this.size=new int[n]; this.diff_weight=new long[n]; arraysize=n; for (int i = 0; i < arraysize; i++) { set(i); } } public void set(int i) { par[i]=i; rank[i]=0; size[i]=1; diff_weight[i]=0; } public void union(int x,int y,long w) { w += weight(x); w -= weight(y); int rootx=find(x); int rooty=find(y); if (rootx==rooty) { return; } if (rank[rootx]>rank[rooty]) { par[rooty]=rootx; diff_weight[rooty] = w; size[rootx]+=size[rooty]; } else if (rank[rootx]<rank[rooty]) { par[rootx]=rooty; w=-w; diff_weight[rootx] = w; size[rooty]+=size[rootx]; } else { par[rooty]=rootx; diff_weight[rooty] = w; rank[rootx]++; size[rootx]+=size[rooty]; } } public int find(int x) { if(par[x] == x) return x; int r = find(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } public long weight(int x) { find(x); return diff_weight[x]; } public int size(int i) { return size[find(i)]; } public long diff(int x, int y) { return weight(y) - weight(x); } public boolean same(int x, int y) { return find(x) == find(y); } public ArrayList<Integer>[] group() { ArrayList<Integer>[] group = new ArrayList[arraysize]; for (int i = 0; i < group.length; i++) { group[i] = new ArrayList<>(); } for (int i = 0; i < arraysize; i++) { group[find(i)].add(i); } return group; } } static class PairLong implements Comparable<PairLong>{ public long x; public long y; public PairLong(long x,long y) { this.x=x; this.y=y; } @Override public boolean equals(Object obj) { if(obj instanceof PairInt) { PairLong other = (PairLong) obj; return other.x==this.x && other.y==this.y; } return false; }//同値の定義 @Override public int hashCode() { return Objects.hash(this.x,this.y); }//これ書かないと正しく動作しない(要 勉強) @Override public int compareTo( PairLong p){ if (this.x > p.x) { return 1; } else if (this.x < p.x) { return -1; } else { return 0; } } } static class PairInt implements Comparable<PairInt>{ public int x; public int y; public PairInt(int x,int y) { this.x=x; this.y=y; } @Override public boolean equals(Object obj) { if(obj instanceof PairInt) { PairInt other = (PairInt) obj; return other.x==this.x && other.y==this.y; } return false; }//同値の定義 @Override public int hashCode() { return Objects.hash(this.x,this.y); }//これ書かないと正しく動作しない(要 勉強) @Override public int compareTo( PairInt p){ if (this.x > p.x) { return 1; } else if (this.x < p.x) { return -1; } else { return 0; } } } static class FenwickTree{ private int _n; private long[] data; public FenwickTree(int n){ this._n = n; data = new long[n]; } /** * @verified https://atcoder.jp/contests/practice2/tasks/practice2_b * @submission https://atcoder.jp/contests/practice2/submissions/16580495 */ public FenwickTree(long[] data) { this(data.length); build(data); } public void set(int p, long x){ add(p, x - get(p)); } public void add(int p, long x){ assert(0<=p && p<_n); p++; while(p<=_n){ data[p-1] += x; p += p&-p; } } public long sum(int l, int r){ assert(0<=l && l<=r && r<=_n); return sum(r)-sum(l); } public long get(int p){ return sum(p, p+1); } private long sum(int r){ long s = 0; while(r>0){ s += data[r-1]; r -= r&-r; } return s; } private void build(long[] dat) { System.arraycopy(dat, 0, data, 0, _n); for (int i=1; i<=_n; i++) { int p = i+(i&-i); if(p<=_n){ data[p-1] += data[i-1]; } } } } static HashMap<Long, Integer> compress(long[] A,boolean is_duplication){ HashMap<Long, Integer> hashMap = new HashMap<>(); long[] tmp = Arrays.copyOf(A, A.length); Arrays.sort(tmp); int now = 0; for (int i = 0; i < A.length; i++) { if (!is_duplication && i > 0 && tmp[i] == tmp[i-1]) { continue; } hashMap.put(tmp[i], now++); } return hashMap; } static HashMap<Long, Integer> compress(long[] A, long[] B, boolean is_duplication){ HashMap<Long, Integer> hashMap = new HashMap<>(); long[] tmp = new long[A.length+B.length]; for (int i = 0; i < A.length; i++) { tmp[i] = A[i]; } for (int i = 0; i < B.length; i++) { tmp[i + A.length] = B[i]; } Arrays.sort(tmp); int now = 0; for (int i = 0; i < tmp.length; i++) { if (!is_duplication && i > 0 && tmp[i] == tmp[i-1]) { continue; } hashMap.put(tmp[i], now++); } return hashMap; } static int sign(long x) { if (x > 0) { return 1; }else if (x < 0) { return -1; }else { return 0; } } static int sign(int x) { if (x > 0) { return 1; }else if (x < 0) { return -1; }else { return 0; } } static int gcd(int a,int b) { if (b==0) { return a; } return gcd(b,a%b); } static long gcd(long a,long b) { if (b==0) { return a; } return gcd(b,a%b); } static long lcm(long a,long b) { long g=gcd(a, b); return a/g*b; } static int lcm(int a,int b) { int g=gcd(a, b); return a/g*b; } static long ceil(long a,long b){ return a % b == 0 ? a / b : a / b + 1; } static class Permutation { public static boolean next(int[] a) { int n = a.length; int i = n - 1; while (i > 0 && a[i - 1] >= a[i]) i--; if (i <= 0) return false; int j = n - 1; while (a[j] <= a[i - 1]) j--; swap(a, i - 1, j); int k = n - 1; while (i < k) swap(a, i++, k--); return true; } private static void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } static long min(long[] a) { long min = Long.MAX_VALUE; for (int i = 0; i < a.length; i++) { min = Math.min(min, a[i]); } return min; } static int min(int[] a) { int min = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) { min = Math.min(min, a[i]); } return min; } static 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 double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); 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; } } } import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; //ctrl + alt + s でスニペット編集 //iter itm if nibutan sout public class Main { static final long MOD1=1000000007; static final long MOD=998244353; static final int NTT_MOD1 = 998244353; static final int NTT_MOD2 = 1053818881; static final int NTT_MOD3 = 1004535809; static final int[] dx = {0, 0, 1, 1}; static final int[] dy = {0, 1, 0, 1}; static int ans = 50; static long MAX = 1000000000000000010L;//10^18 public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int n = sc.nextInt(); long[][] p = new long[n][n]; long[][] r = new long[n][n - 1]; long[][] d = new long[n - 1][n]; for (int i = 0; i < n; i++) { p[i] = sc.nextLongArray(n); } for (int i = 0; i < n; i++) { r[i] = sc.nextLongArray(n - 1); } for (int i = 0; i < n - 1; i++) { d[i] = sc.nextLongArray(n); } PairLong[][] dp = new PairLong[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(dp[i], new PairLong(MAX, MAX)); } dp[0][0] = new PairLong(0, 0); long ans = MAX; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == n - 1 && j == n - 1) { continue; } if (dp[i][j].x == MAX) { continue; } long[][] dist = new long[n][n]; for (int k = 0; k < n; k++) { Arrays.fill(dist[k], MAX); } dist[i][j] = 0; for (int k = i; k < n; k++) { for (int l = j; l < n; l++) { if (k + 1 < n) { dist[k + 1][l] = Math.min(dist[k + 1][l], dist[k][l] + d[k][l]); } if (l + 1 < n) { dist[k][l + 1] = Math.min(dist[k][l + 1], dist[k][l] + r[k][l]); } } } long x = dp[i][j].x; long y = dp[i][j].y; ans = Math.min(ans, x + ceil(Math.max(dist[n - 1][n - 1] - y, 0), p[i][j])); //System.out.println(ans + " " + i + " " + j + " " + x + " " + y + " " + dist[n - 1][n - 1] + " " + p[i][j]); for (int k = i; k < n; k++) { for (int l = j; l < n; l++) { if (k == i && l == j) { continue; } if (p[i][j] >= p[k][l]) { continue; } long nx = x + ceil(Math.max(dist[k][l] - y, 0), p[i][j]); long ny = y + (ceil(Math.max(dist[k][l] - y, 0), p[i][j]) * p[i][j]) - dist[k][l]; if (nx < dp[k][l].x || (nx == dp[k][l].x && ny > dp[k][l].y)) { dp[k][l] = new PairLong(nx, ny); } } } } } out.println(ans + 2 * n - 2); out.flush(); } static class Edge implements Comparable<Edge>{ int to; long v; public Edge(int to,long v) { this.to=to; this.v=v; } @Override public boolean equals(Object obj){ if(obj instanceof Edge) { Edge other = (Edge) obj; return other.to==this.to && other.v==this.v; } return false; }//同値の定義 @Override public int hashCode() { return Objects.hash(this.to,this.v); } @Override public int compareTo( Edge p2 ){ if (this.v>p2.v) { return 1; } else if (this.v<p2.v) { return -1; } else { return 0; } } } static class graph{ ArrayList<Edge>[] list; int size; long INF=Long.MAX_VALUE/2; int[] color; @SuppressWarnings("unchecked") public graph(int n) { size = n; list = new ArrayList[n]; color =new int[n]; for(int i=0;i<n;i++){ list[i] = new ArrayList<Edge>(); } } void addEdge(int from,int to,long w) { list[from].add(new Edge(to,w)); } void addUndirectedEdge(int from,int to,long w) { addEdge(from, to, w); addEdge(to, from, w); } boolean nibugraph(int s,int colors) { color[s]=colors; for (Edge nv: list[s]) { if (color[nv.to]==colors) { return false; } if (color[nv.to]==0&&!nibugraph(nv.to, -colors)) { return false; } } return true; } } static class BinaryTrie { private final static int K = 31; int[] l; int[] r; int[] count; int size; int node_sum = 0; public BinaryTrie(int size) { this.l = new int[(K + 1) * (size + 1)]; this.r = new int[(K + 1) * (size + 1)]; this.count = new int[(K + 1) * (size + 1)]; this.size = size; Arrays.fill(l, -1); Arrays.fill(r, -1); } public int size() { return count[0]; } public boolean isEmpty() { return size() == 0; } public int count(int x) { int node_id = 0; for (int i = K; i >= 0; i--) { if (((x >> i) & 1) == 0) { if (l[node_id] == -1) { return 0; } node_id = l[node_id]; } else { if (r[node_id] == -1) { return 0; } node_id = r[node_id]; } } return count[node_id]; } public void add(int x) { add(x, 1); } public int get(int i) { if (i < 0 || size() <= i) { throw new IndexOutOfBoundsException(); } int node_id = 0; int val = 0; for (int j = 0; j <= K; j++) { val *= 2; int lc = l[node_id] == -1 ? 0 : count[l[node_id]]; if (i < lc) { node_id = l[node_id]; } else { val++; i -= lc; node_id = r[node_id]; } } return val; } public void add(int x, int count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } int node_id = 0; this.count[node_id] += count; for (int i = K; i >= 0; i--) { if (((x >> i) & 1) == 0) { if (l[node_id] == -1) { node_sum++; l[node_id] = node_sum; } node_id = l[node_id]; this.count[node_id] += count; } else { if (r[node_id] == -1) { node_sum++; r[node_id] = node_sum; } node_id = r[node_id]; this.count[node_id] += count; } } } public boolean remove(int x) { return remove(x, 1); } public boolean remove(int x, int count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } count = Math.min(count, count(x)); if (count == 0) { return false; } int node_id = 0; this.count[node_id] -= count; for (int i = K; i >= 0; i--) { if (((x >> i) & 1) == 0) { this.count[l[node_id]] -= count; if (this.count[l[node_id]] == 0) { l[node_id] = -1; return true; } node_id = l[node_id]; } else { this.count[r[node_id]] -= count; if (this.count[r[node_id]] == 0) { r[node_id] = -1; return true; } node_id = r[node_id]; } } return true; } public long min() { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; long val = 0; for (int j = 0; j <= K; j++) { val *= 2; if (l[node_id] != -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } return val; } public int xormin(int x) { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; int val = 0; for (int i = K; i >= 0; i--) { val *= 2; if (((x >> i) & 1) == 1) { if (r[node_id] == -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } }else { if (l[node_id] != -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } } return val; } public int max() { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; int val = 0; for (int j = 0; j <= K; j++) { val *= 2; if (r[node_id] == -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } return val; } public int xormax(long x) { if(isEmpty()) throw new ArrayIndexOutOfBoundsException(); int node_id = 0; int val = 0; for (int i = K; i >= 0; i--) { val *= 2; if (((x >> i) & 1) == 0) { if (r[node_id] == -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } }else { if (l[node_id] != -1) { node_id = l[node_id]; } else { node_id = r[node_id]; val++; } } } return val; }//未verify public int lowerBound(int x) { if (isEmpty()) { return 0; } int node_id = 0; int i = 0; for (int j = K; j >= 0; j--) { if (((x >> j) & 1) == 0) { node_id = l[node_id]; } else { i += l[node_id] == -1 ? 0 : count[l[node_id]]; node_id = r[node_id]; } if (node_id == -1) { return i; } } return i; } public int xorlowerBound(int x, int xor) { if (isEmpty()) { return 0; } int node_id = 0; int i = 0; for (int j = K; j >= 0; j--) { if (((xor >> j) & 1) == 0) { if (((x >> j) & 1) == 0) { node_id = l[node_id]; } else { i += l[node_id] == -1 ? 0 : count[l[node_id]]; node_id = r[node_id]; } }else { if (((x >> j) & 1) == 0) { node_id = r[node_id]; } else { i += r[node_id] == -1 ? 0 : count[r[node_id]]; node_id = l[node_id]; } } if (node_id == -1) { return i; } } return i; } } static class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException( String.format("Invalid range: [%d, %d)", l, r) ); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Integer.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX) ); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX) ); } } } static int ceil(int a,int b) { if (a%b==0) { return a/b; }else { return a/b+1; } } static int lower_bound(int[] a, int fromIndex, int toIndex, int val) { if (val > a[toIndex]) return toIndex + 1; if (val <= a[fromIndex]) return fromIndex; int lb = fromIndex - 1, ub = toIndex; while (ub - lb > 1) { int mid = (ub - lb)/2+lb; if (a[mid] >= val) { ub = mid; } else { lb = mid; } } return ub; } static int lower_bound(long[] a, int fromIndex, int toIndex, long val) { if (val > a[toIndex]) return toIndex + 1; if (val <= a[fromIndex]) return fromIndex; int lb = fromIndex - 1, ub = toIndex; while (ub - lb > 1) { int mid = (ub - lb)/2+lb; if (a[mid] >= val) { ub = mid; } else { lb = mid; } } return ub; } static class UnionFindTree{ int[] par; int[] rank; int[] size; long[] diff_weight; int arraysize; public UnionFindTree(int n) { this.par=new int[n]; this.rank=new int[n]; this.size=new int[n]; this.diff_weight=new long[n]; arraysize=n; for (int i = 0; i < arraysize; i++) { set(i); } } public void set(int i) { par[i]=i; rank[i]=0; size[i]=1; diff_weight[i]=0; } public void union(int x,int y,long w) { w += weight(x); w -= weight(y); int rootx=find(x); int rooty=find(y); if (rootx==rooty) { return; } if (rank[rootx]>rank[rooty]) { par[rooty]=rootx; diff_weight[rooty] = w; size[rootx]+=size[rooty]; } else if (rank[rootx]<rank[rooty]) { par[rootx]=rooty; w=-w; diff_weight[rootx] = w; size[rooty]+=size[rootx]; } else { par[rooty]=rootx; diff_weight[rooty] = w; rank[rootx]++; size[rootx]+=size[rooty]; } } public int find(int x) { if(par[x] == x) return x; int r = find(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } public long weight(int x) { find(x); return diff_weight[x]; } public int size(int i) { return size[find(i)]; } public long diff(int x, int y) { return weight(y) - weight(x); } public boolean same(int x, int y) { return find(x) == find(y); } public ArrayList<Integer>[] group() { ArrayList<Integer>[] group = new ArrayList[arraysize]; for (int i = 0; i < group.length; i++) { group[i] = new ArrayList<>(); } for (int i = 0; i < arraysize; i++) { group[find(i)].add(i); } return group; } } static class PairLong implements Comparable<PairLong>{ public long x; public long y; public PairLong(long x,long y) { this.x=x; this.y=y; } @Override public boolean equals(Object obj) { if(obj instanceof PairInt) { PairLong other = (PairLong) obj; return other.x==this.x && other.y==this.y; } return false; }//同値の定義 @Override public int hashCode() { return Objects.hash(this.x,this.y); }//これ書かないと正しく動作しない(要 勉強) @Override public int compareTo( PairLong p){ if (this.x > p.x) { return 1; } else if (this.x < p.x) { return -1; } else { return 0; } } } static class PairInt implements Comparable<PairInt>{ public int x; public int y; public PairInt(int x,int y) { this.x=x; this.y=y; } @Override public boolean equals(Object obj) { if(obj instanceof PairInt) { PairInt other = (PairInt) obj; return other.x==this.x && other.y==this.y; } return false; }//同値の定義 @Override public int hashCode() { return Objects.hash(this.x,this.y); }//これ書かないと正しく動作しない(要 勉強) @Override public int compareTo( PairInt p){ if (this.x > p.x) { return 1; } else if (this.x < p.x) { return -1; } else { return 0; } } } static class FenwickTree{ private int _n; private long[] data; public FenwickTree(int n){ this._n = n; data = new long[n]; } /** * @verified https://atcoder.jp/contests/practice2/tasks/practice2_b * @submission https://atcoder.jp/contests/practice2/submissions/16580495 */ public FenwickTree(long[] data) { this(data.length); build(data); } public void set(int p, long x){ add(p, x - get(p)); } public void add(int p, long x){ assert(0<=p && p<_n); p++; while(p<=_n){ data[p-1] += x; p += p&-p; } } public long sum(int l, int r){ assert(0<=l && l<=r && r<=_n); return sum(r)-sum(l); } public long get(int p){ return sum(p, p+1); } private long sum(int r){ long s = 0; while(r>0){ s += data[r-1]; r -= r&-r; } return s; } private void build(long[] dat) { System.arraycopy(dat, 0, data, 0, _n); for (int i=1; i<=_n; i++) { int p = i+(i&-i); if(p<=_n){ data[p-1] += data[i-1]; } } } } static HashMap<Long, Integer> compress(long[] A,boolean is_duplication){ HashMap<Long, Integer> hashMap = new HashMap<>(); long[] tmp = Arrays.copyOf(A, A.length); Arrays.sort(tmp); int now = 0; for (int i = 0; i < A.length; i++) { if (!is_duplication && i > 0 && tmp[i] == tmp[i-1]) { continue; } hashMap.put(tmp[i], now++); } return hashMap; } static HashMap<Long, Integer> compress(long[] A, long[] B, boolean is_duplication){ HashMap<Long, Integer> hashMap = new HashMap<>(); long[] tmp = new long[A.length+B.length]; for (int i = 0; i < A.length; i++) { tmp[i] = A[i]; } for (int i = 0; i < B.length; i++) { tmp[i + A.length] = B[i]; } Arrays.sort(tmp); int now = 0; for (int i = 0; i < tmp.length; i++) { if (!is_duplication && i > 0 && tmp[i] == tmp[i-1]) { continue; } hashMap.put(tmp[i], now++); } return hashMap; } static int sign(long x) { if (x > 0) { return 1; }else if (x < 0) { return -1; }else { return 0; } } static int sign(int x) { if (x > 0) { return 1; }else if (x < 0) { return -1; }else { return 0; } } static int gcd(int a,int b) { if (b==0) { return a; } return gcd(b,a%b); } static long gcd(long a,long b) { if (b==0) { return a; } return gcd(b,a%b); } static long lcm(long a,long b) { long g=gcd(a, b); return a/g*b; } static int lcm(int a,int b) { int g=gcd(a, b); return a/g*b; } static long ceil(long a,long b){ return a % b == 0 ? a / b : a / b + 1; } static class Permutation { public static boolean next(int[] a) { int n = a.length; int i = n - 1; while (i > 0 && a[i - 1] >= a[i]) i--; if (i <= 0) return false; int j = n - 1; while (a[j] <= a[i - 1]) j--; swap(a, i - 1, j); int k = n - 1; while (i < k) swap(a, i++, k--); return true; } private static void swap(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } static long min(long[] a) { long min = Long.MAX_VALUE; for (int i = 0; i < a.length; i++) { min = Math.min(min, a[i]); } return min; } static int min(int[] a) { int min = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) { min = Math.min(min, a[i]); } return min; } static 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 double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); 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; } } }
ConDefects/ConDefects/Code/abc344_f/Java/54909243
condefects-java_data_28
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int h=sc.nextInt(); int w=sc.nextInt(); String[][] Cookie=new String[h][w]; String[] Cookie0=new String[h]; int [] sunukex=new int[h]; int [] sunukey=new int[w]; int count=0; for(int i=0;i<h;i++){ Cookie0[i]=sc.next(); String split[]=Cookie0[i].split(""); for(int j=0;j<w;j++){ Cookie[i][j]=split[j]; if(Cookie[i][j].equals("#")){ count++; } } sunukex[i]=count; //System.out.println("# x:"+sunukex[i]); count=0; } int many=sunukex[0]; for(int i=1;i<h;i++){ if(many<sunukex[i]){ many=sunukex[i]; } } //System.out.println("many x:"+many); int x=0; for(int i=0;i<h;i++){ if(sunukex[i]==many-1){ x=i+1; } } //System.out.println("x:"+x); for(int i=0;i<w;i++){ for(int j=0;j<h;j++){ if(Cookie[j][i].equals("#")){ count++; } } sunukey[i]=count; //System.out.println("# y:"+sunukey[i]); count=0; } many=sunukey[0]; for(int i=1;i<w;i++){ if(many<sunukey[i]){ many=sunukey[i+1]; } } //System.out.println("many y:"+many); int y=0; for(int i=0;i<w;i++){ if(sunukey[i]==many-1){ y=i+1; } } //System.out.println("y:"+y); System.out.println(x+" "+y); sc.close(); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int h=sc.nextInt(); int w=sc.nextInt(); String[][] Cookie=new String[h][w]; String[] Cookie0=new String[h]; int [] sunukex=new int[h]; int [] sunukey=new int[w]; int count=0; for(int i=0;i<h;i++){ Cookie0[i]=sc.next(); String split[]=Cookie0[i].split(""); for(int j=0;j<w;j++){ Cookie[i][j]=split[j]; if(Cookie[i][j].equals("#")){ count++; } } sunukex[i]=count; //System.out.println("# x:"+sunukex[i]); count=0; } int many=sunukex[0]; for(int i=1;i<h;i++){ if(many<sunukex[i]){ many=sunukex[i]; } } //System.out.println("many x:"+many); int x=0; for(int i=0;i<h;i++){ if(sunukex[i]==many-1){ x=i+1; } } //System.out.println("x:"+x); for(int i=0;i<w;i++){ for(int j=0;j<h;j++){ if(Cookie[j][i].equals("#")){ count++; } } sunukey[i]=count; //System.out.println("# y:"+sunukey[i]); count=0; } many=sunukey[0]; for(int i=1;i<w;i++){ if(many<sunukey[i]){ many=sunukey[i]; } } //System.out.println("many y:"+many); int y=0; for(int i=0;i<w;i++){ if(sunukey[i]==many-1){ y=i+1; } } //System.out.println("y:"+y); System.out.println(x+" "+y); sc.close(); } }
ConDefects/ConDefects/Code/abc305_c/Java/42414008
condefects-java_data_29
import java.util.*; class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); sc.nextLine(); char[][] ch=new char[n][m]; for(int i=0;i<n;i++) { String s=sc.nextLine(); for(int j=0;j<m;j++) ch[i][j]=s.charAt(j); } int row=0, size=-1; // TreeMap<Integer, Integer> tm=new TreeMap<>(); // for(int i=0;i<n;i++) { // int l=0; // for(int j=0;j<m;j++) { // if(ch[i][j]=='#') l++; // } // if(l!=0) tm.put(l, i); // } // for(Map.Entry<Integer, Integer> ele:tm.entrySet()) { // row=ele.getValue(); // break; // } for(int i=0;i<n;i++) {//row traversal int l=0;//cookie row length of ith row for(int j=0;j<m;j++) { if(ch[i][j]=='#') l++; } if(size==-1 && l!=0) size=l; if(size>l && l!=0) {//if there exist a cookie and its strictly smaller than ongoing size row=i; size=l; } } //we have the row number where def of cookie occur int col=0; boolean check=false; for(int j=0;row-1>=0 && j<m;j++) {//checking if upper row has any cookies if(ch[row-1][j]=='#') { check=true; } } int c=0; if(check) c=row-1; else c=row+1; for(int j=0;j<m;j++) { if(ch[c][j]!=ch[row][j]) { col=j; break; } } System.out.println((row+1)+" "+(col+1)); } } import java.util.*; class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); sc.nextLine(); char[][] ch=new char[n][m]; for(int i=0;i<n;i++) { String s=sc.nextLine(); for(int j=0;j<m;j++) ch[i][j]=s.charAt(j); } int row=0, size=-1; // TreeMap<Integer, Integer> tm=new TreeMap<>(); // for(int i=0;i<n;i++) { // int l=0; // for(int j=0;j<m;j++) { // if(ch[i][j]=='#') l++; // } // if(l!=0) tm.put(l, i); // } // for(Map.Entry<Integer, Integer> ele:tm.entrySet()) { // row=ele.getValue(); // break; // } for(int i=0;i<n;i++) {//row traversal int l=0;//cookie row length of ith row for(int j=0;j<m;j++) { if(ch[i][j]=='#') l++; } if(size==-1 && l!=0) {size=l;row=i;} if(size>l && l!=0) {//if there exist a cookie and its strictly smaller than ongoing size row=i; size=l; } } //we have the row number where def of cookie occur int col=0; boolean check=false; for(int j=0;row-1>=0 && j<m;j++) {//checking if upper row has any cookies if(ch[row-1][j]=='#') { check=true; } } int c=0; if(check) c=row-1; else c=row+1; for(int j=0;j<m;j++) { if(ch[c][j]!=ch[row][j]) { col=j; break; } } System.out.println((row+1)+" "+(col+1)); } }
ConDefects/ConDefects/Code/abc305_c/Java/42712332
condefects-java_data_30
import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { private static List<Integer> ans = new ArrayList<>(); private static Set<Integer> done = new HashSet<>(); private static List<Integer>[] g; private static void solve() { int n = nextInt(); g = new List[n]; for (int i = 0; i < n; i++) { g[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { int ci = nextInt(); for (int j = 0; j < ci; j++) { int p = nextInt()-1; g[i].add(p); } } dfs(0); for (int i = 0; i < ans.size()-1; i++) { out.print(ans.get(i)+1+" "); } out.println(); out.flush(); } private static void dfs(int current) { for (Integer next : g[current]) { if (done.contains(next)) continue; done.add(next); dfs(next); } ans.add(current); } public static void main(String[] args) { new Thread(null, () -> solve(), "", 16 * 1024 * 1024).start(); } static PrintWriter out = new PrintWriter(System.out); static Scanner scanner = new Scanner(System.in); static String next() { return scanner.next(); } static int nextInt() { int res = 0; char[] chars = next().toCharArray(); boolean minus = chars[0] == '-'; int start = minus?1:0; for (int i = start; i < chars.length; i++) { res = res*10 + (chars[i]-'0'); } return minus?-res:res; } static long nextLong() { long res = 0; char[] chars = next().toCharArray(); boolean minus = chars[0] == '-'; int start = minus?1:0; for (int i = start; i < chars.length; i++) { res = res*10 + (chars[i]-'0'); } return minus?-res:res; } static double nextDouble() { return Double.parseDouble(next()); } static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } } import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { private static List<Integer> ans = new ArrayList<>(); private static Set<Integer> done = new HashSet<>(); private static List<Integer>[] g; private static void solve() { int n = nextInt(); g = new List[n]; for (int i = 0; i < n; i++) { g[i] = new ArrayList<>(); } for (int i = 0; i < n; i++) { int ci = nextInt(); for (int j = 0; j < ci; j++) { int p = nextInt()-1; g[i].add(p); } } dfs(0); for (int i = 0; i < ans.size()-1; i++) { out.print(ans.get(i)+1+" "); } out.println(); out.flush(); } private static void dfs(int current) { for (Integer next : g[current]) { if (done.contains(next)) continue; done.add(next); dfs(next); } ans.add(current); } public static void main(String[] args) { new Thread(null, () -> solve(), "", 32L * 1024 * 1024).start(); } static PrintWriter out = new PrintWriter(System.out); static Scanner scanner = new Scanner(System.in); static String next() { return scanner.next(); } static int nextInt() { int res = 0; char[] chars = next().toCharArray(); boolean minus = chars[0] == '-'; int start = minus?1:0; for (int i = start; i < chars.length; i++) { res = res*10 + (chars[i]-'0'); } return minus?-res:res; } static long nextLong() { long res = 0; char[] chars = next().toCharArray(); boolean minus = chars[0] == '-'; int start = minus?1:0; for (int i = start; i < chars.length; i++) { res = res*10 + (chars[i]-'0'); } return minus?-res:res; } static double nextDouble() { return Double.parseDouble(next()); } static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } }
ConDefects/ConDefects/Code/abc315_e/Java/45008022
condefects-java_data_31
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; /* --- ABC315E --- */ public class Main implements Runnable { //Runnableを実装する private static HashMap<Integer, ArrayList<Integer>> G; private static StringBuilder ans; private static HashSet<Integer> done; public static void main(String[] args) { new Thread(null, new Main(), "", 32L * 1024 * 1024).start(); // 32MBスタックを確保して実行 } public void run() { /* --- Input --- */ try { var br = new BufferedReader(new InputStreamReader(System.in)); var N = Integer.parseInt(br.readLine()); G = new HashMap<>(); for (var i = 0; i < N; i++) { var sa = br.readLine().split(" "); var C = Integer.parseInt(sa[0]); for(var j=0; j<C; j++){ var P = Integer.parseInt(sa[j+1]); if(!G.containsKey(i+1)) G.put(i+1, new ArrayList<>()); G.get(i+1).add(P); } } br.close(); /* --- Process --- */ ans = new StringBuilder(); done = new HashSet<>(); done.add(1); dfs(1); /* --- Output --- */ System.out.println(ans); System.out.flush(); } catch (IOException e) { throw new RuntimeException(e); } } private static void dfs(int now){ var list = G.get(now); if(list != null) for(var next : list) if(!done.add(next)) dfs(next); if(now != 1) ans.append(now).append(" "); } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; /* --- ABC315E --- */ public class Main implements Runnable { //Runnableを実装する private static HashMap<Integer, ArrayList<Integer>> G; private static StringBuilder ans; private static HashSet<Integer> done; public static void main(String[] args) { new Thread(null, new Main(), "", 32L * 1024 * 1024).start(); // 32MBスタックを確保して実行 } public void run() { /* --- Input --- */ try { var br = new BufferedReader(new InputStreamReader(System.in)); var N = Integer.parseInt(br.readLine()); G = new HashMap<>(); for (var i = 0; i < N; i++) { var sa = br.readLine().split(" "); var C = Integer.parseInt(sa[0]); for(var j=0; j<C; j++){ var P = Integer.parseInt(sa[j+1]); if(!G.containsKey(i+1)) G.put(i+1, new ArrayList<>()); G.get(i+1).add(P); } } br.close(); /* --- Process --- */ ans = new StringBuilder(); done = new HashSet<>(); done.add(1); dfs(1); /* --- Output --- */ System.out.println(ans); System.out.flush(); } catch (IOException e) { throw new RuntimeException(e); } } private static void dfs(int now){ var list = G.get(now); if(list != null) for(var next : list) if(done.add(next)) dfs(next); if(now != 1) ans.append(now).append(" "); } }
ConDefects/ConDefects/Code/abc315_e/Java/44984614
condefects-java_data_32
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 lucasr */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyScanner in = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BArrangeYourBalls solver = new BArrangeYourBalls(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class BArrangeYourBalls { public static MyScanner sc; public static PrintWriter out; public void solve(int testNumber, MyScanner sc, PrintWriter out) { BArrangeYourBalls.sc = sc; BArrangeYourBalls.out = out; int N = sc.nextInt(); int[] count = new int[N]; for (int i = 0; i < N; i++) { count[sc.nextInt() - 1]++; } IntArray inner = new IntArray(), leaves = new IntArray(); for (int i = 0; i < N; i++) { if (count[i] == 1) { leaves.add(i); } else if (count[i] > 1) { inner.add(i); } } IntArray[] adj = new IntArray[N]; for (int i = 0; i < N; i++) { adj[i] = new IntArray(); } int[] rem = count.clone(); boolean can = true; int innerIdx = 0; for (int i = 0; i < leaves.size(); i++) { int cur = leaves.get(i); if (innerIdx < inner.size()) { int other = inner.get(innerIdx); adj[cur].add(other); adj[other].add(cur); rem[other]--; rem[cur]--; if (rem[other] == 0) { if (i + 1 < leaves.size()) can = false; break; } while (rem[other] == 1 && innerIdx + 1 < inner.size()) { int tmp = inner.get(innerIdx + 1); adj[tmp].add(other); adj[other].add(tmp); rem[tmp]--; rem[other]--; other = tmp; innerIdx++; } } else { can = false; } } if (can) { for (int i = innerIdx; i + 1 < inner.size(); i++) { int a = inner.get(i); int b = inner.get(i + 1); adj[a].add(b); adj[b].add(a); rem[a]--; rem[b]--; } IntArray order = new IntArray(); int first = !leaves.isEmpty() ? leaves.first() : inner.first(); dfs(first, -1, adj, order); for (int i = 0; i < order.size(); i++) { int cur = order.get(i); out.print(" " + (cur + 1)); while (rem[cur] > 0) { out.print(" " + (cur + 1)); rem[cur]--; } } out.println(); } else { for (int i = 0; i < N; i++) { for (int j = 0; j < count[i]; j++) { out.print(" " + (i + 1)); } } out.println(); } } private void dfs(int node, int parent, IntArray[] adj, IntArray order) { order.add(node); boolean first = true; for (int i = 0; i < adj[node].size(); i++) { int next = adj[node].get(i); if (next != parent) { dfs(next, node, adj, order); if (first && parent == -1) first = false; else order.add(node); } } } } static class IntArray { int[] arr; int size; public IntArray() { arr = new int[4]; } public void add(int val) { if (size == arr.length) { arr = Arrays.copyOf(arr, 2 * arr.length); } arr[size++] = val; } public int get(int pos) { return arr[pos]; } public int first() { return arr[0]; } public int size() { return size; } public boolean isEmpty() { return size() == 0; } public int[] toArray() { return Arrays.copyOf(arr, size); } public String toString() { return "IntArray " + Arrays.toString(toArray()); } } static class MyScanner { private BufferedReader br; private StringTokenizer tokenizer; public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } } 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 lucasr */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; MyScanner in = new MyScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BArrangeYourBalls solver = new BArrangeYourBalls(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class BArrangeYourBalls { public static MyScanner sc; public static PrintWriter out; public void solve(int testNumber, MyScanner sc, PrintWriter out) { BArrangeYourBalls.sc = sc; BArrangeYourBalls.out = out; int N = sc.nextInt(); int[] count = new int[N]; for (int i = 0; i < N; i++) { count[sc.nextInt() - 1]++; } IntArray inner = new IntArray(), leaves = new IntArray(); for (int i = 0; i < N; i++) { if (count[i] == 1) { leaves.add(i); } else if (count[i] > 1) { inner.add(i); } } IntArray[] adj = new IntArray[N]; for (int i = 0; i < N; i++) { adj[i] = new IntArray(); } int[] rem = count.clone(); boolean can = leaves.size() + inner.size() > 1; int innerIdx = 0; for (int i = 0; i < leaves.size(); i++) { int cur = leaves.get(i); if (innerIdx < inner.size()) { int other = inner.get(innerIdx); adj[cur].add(other); adj[other].add(cur); rem[other]--; rem[cur]--; if (rem[other] == 0) { if (i + 1 < leaves.size()) can = false; break; } while (rem[other] == 1 && innerIdx + 1 < inner.size()) { int tmp = inner.get(innerIdx + 1); adj[tmp].add(other); adj[other].add(tmp); rem[tmp]--; rem[other]--; other = tmp; innerIdx++; } } else { can = false; } } if (can) { for (int i = innerIdx; i + 1 < inner.size(); i++) { int a = inner.get(i); int b = inner.get(i + 1); adj[a].add(b); adj[b].add(a); rem[a]--; rem[b]--; } IntArray order = new IntArray(); int first = !leaves.isEmpty() ? leaves.first() : inner.first(); dfs(first, -1, adj, order); for (int i = 0; i < order.size(); i++) { int cur = order.get(i); out.print(" " + (cur + 1)); while (rem[cur] > 0) { out.print(" " + (cur + 1)); rem[cur]--; } } out.println(); } else { for (int i = 0; i < N; i++) { for (int j = 0; j < count[i]; j++) { out.print(" " + (i + 1)); } } out.println(); } } private void dfs(int node, int parent, IntArray[] adj, IntArray order) { order.add(node); boolean first = true; for (int i = 0; i < adj[node].size(); i++) { int next = adj[node].get(i); if (next != parent) { dfs(next, node, adj, order); if (first && parent == -1) first = false; else order.add(node); } } } } static class IntArray { int[] arr; int size; public IntArray() { arr = new int[4]; } public void add(int val) { if (size == arr.length) { arr = Arrays.copyOf(arr, 2 * arr.length); } arr[size++] = val; } public int get(int pos) { return arr[pos]; } public int first() { return arr[0]; } public int size() { return size; } public boolean isEmpty() { return size() == 0; } public int[] toArray() { return Arrays.copyOf(arr, size); } public String toString() { return "IntArray " + Arrays.toString(toArray()); } } static class MyScanner { private BufferedReader br; private StringTokenizer tokenizer; public MyScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
ConDefects/ConDefects/Code/agc059_b/Java/37024389
condefects-java_data_33
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 Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(); long K = nl(); final int mod = 998244353; long ans = 0; int[][] fif = enumFIF(n, mod); long kk = (K+1) % mod; { // nz = 0 ans += kk; } long c = 1; for(int nz = 1;nz <= n;nz++){ ans += pow(2, nz, mod) * C(n, nz, mod, fif) % mod * kk % mod * (kk-nz) % mod * c % mod * invl(nz, mod) % mod * invl(nz+1, mod); c = c * ((K-nz+1) % mod) % mod; c = c * invl(nz, mod) % mod; } ans %= mod; if(ans < 0)ans += mod; out.println(ans); } public static long pow(long a, long n, long mod) { // a %= mod; long ret = 1; int x = 63 - Long.numberOfLeadingZeros(n); for (; x >= 0; x--) { ret = ret * ret % mod; if (n << 63 - x < 0) ret = ret * a % mod; } return ret; } public static int[][] enumFIF(int n, int mod) { int[] f = new int[n + 1]; int[] invf = new int[n + 1]; f[0] = 1; for (int i = 1; i <= n; i++) { f[i] = (int) ((long) f[i - 1] * i % mod); } long a = f[n]; long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } invf[n] = (int) (p < 0 ? p + mod : p); for (int i = n - 1; i >= 0; i--) { invf[i] = (int) ((long) invf[i + 1] * (i + 1) % mod); } return new int[][]{f, invf}; } public static long C(int n, int r, int mod, int[][] fif) { if (n < 0 || r < 0 || r > n) return 0; return (long) fif[0][n] * fif[1][r] % mod * fif[1][n - r] % mod; } public static long invl(long a, long mod) { long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } return p < 0 ? p + mod : p; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static 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 static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static 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 static 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 static int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private static 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 static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } } 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 Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(); long K = nl(); final int mod = 998244353; long ans = 0; int[][] fif = enumFIF(n, mod); long kk = (K+1) % mod; { // nz = 0 ans += kk; } long c = 1; for(int nz = 1;nz <= n;nz++){ ans += pow(2, nz, mod) * C(n, nz, mod, fif) % mod * kk % mod * (kk-nz) % mod * c % mod * invl(nz, mod) % mod * invl(nz+1, mod) % mod; c = c * ((K-nz+1) % mod) % mod; c = c * invl(nz, mod) % mod; } ans %= mod; if(ans < 0)ans += mod; out.println(ans); } public static long pow(long a, long n, long mod) { // a %= mod; long ret = 1; int x = 63 - Long.numberOfLeadingZeros(n); for (; x >= 0; x--) { ret = ret * ret % mod; if (n << 63 - x < 0) ret = ret * a % mod; } return ret; } public static int[][] enumFIF(int n, int mod) { int[] f = new int[n + 1]; int[] invf = new int[n + 1]; f[0] = 1; for (int i = 1; i <= n; i++) { f[i] = (int) ((long) f[i - 1] * i % mod); } long a = f[n]; long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } invf[n] = (int) (p < 0 ? p + mod : p); for (int i = n - 1; i >= 0; i--) { invf[i] = (int) ((long) invf[i + 1] * (i + 1) % mod); } return new int[][]{f, invf}; } public static long C(int n, int r, int mod, int[][] fif) { if (n < 0 || r < 0 || r > n) return 0; return (long) fif[0][n] * fif[1][r] % mod * fif[1][n - r] % mod; } public static long invl(long a, long mod) { long b = mod; long p = 1, q = 0; while (b > 0) { long c = a / b; long d; d = a; a = b; b = d % b; d = p; p = q; q = d - c * q; } return p < 0 ? p + mod : p; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static 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 static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static 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 static 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 static int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private static 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 static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
ConDefects/ConDefects/Code/arc144_d/Java/33267997
condefects-java_data_34
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.HashMap; import java.util.NoSuchElementException; import java.util.Objects; import java.util.stream.IntStream; public final class Main { private static final FastScanner sc = new FastScanner(); private static final PrintWriter pw = new PrintWriter(System.out); private static int n; private static int m; private static char[][] grid; private static final int[] dx = { 1, -1, 0, 0 }; private static final int[] dy = { 0, 0, 1, -1 }; public static final void main(String[] args) throws Exception { n = sc.nextInt(); m = sc.nextInt(); grid = new char[n][m]; for (int i = 0; i < n; ++i) { grid[i] = sc.next().toCharArray(); } var yDeque = new ArrayDeque<Integer>(n * m); var xDeque = new ArrayDeque<Integer>(n * m); yDeque.addLast(1); xDeque.addLast(1); var targets = new HashMap<String, Boolean>(n * m); targets.put(1 + " " + 1, true); while (!yDeque.isEmpty()) { final int y0 = yDeque.pollFirst(); final int x0 = xDeque.pollFirst(); for (int i = 0; i < 4; ++i) { int y = y0, x = x0; while (true) { if (grid[y + dy[i]][x + dx[i]] == '#') { if (!targets.containsKey(y + " " + x)) { yDeque.addLast(y); xDeque.addLast(x); targets.put(y + " " + x, true); } break; } grid[y][x] = 'v'; y += dy[i]; x += dx[i]; } } } int ans = 0; for (char[] row : grid) { for (char c : row) { if (c == 'v') { ++ans; } } } println(ans); pw.flush(); } static final void println(Object obj) { pw.println(Objects.toString(obj)); } static final class Permutation { private int size; private int[] init; private int[] perm; private boolean[] used; public Permutation(int size) { if (size < 1) { throw new IllegalArgumentException(); } this.size = size; init = IntStream.rangeClosed(1, size).toArray(); perm = new int[size]; used = new boolean[size]; } private void process() { } public void buildPermutation(int depth) { if (depth == size) { process(); return; } for (int i = 0; i < size; ++i) { if (used[i]) { continue; } perm[depth] = init[i]; used[i] = true; buildPermutation(depth + 1); used[i] = false; } } } } final class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } return buflen > 0; } private int readByte() { return hasNextByte() ? buffer[ptr++] : -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private static boolean isPrintableCharForLine(int c) { return 32 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public String nextLine() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableCharForLine(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) { throw new NumberFormatException(); } return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } } final class Pair<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<Pair<F, S>> { private F first; private S second; public Pair(F first, S second) { this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } public void setFirst(F first) { this.first = first; } public void setSecond(S second) { this.second = second; } @Override public int compareTo(Pair<F, S> o) { int t = this.first.compareTo(o.getFirst()); if (t != 0) { return t; } return this.second.compareTo(o.getSecond()); } @Override public boolean equals(Object obj) { if (obj == null) { return false; } Pair<F, S> another = (Pair<F, S>) obj; return Objects.equals(this.getFirst(), another.getFirst()) && Objects.equals(this.getSecond(), another.getSecond()); } @Override public int hashCode() { int h1 = first.hashCode(); int h2 = second.hashCode(); String connected = String.valueOf(h1) + ' ' + String.valueOf(h2); return connected.hashCode(); } @Override public String toString() { return Objects.toString(first) + ' ' + Objects.toString(second); } } final class UnionFind { private int[] parents; private int[] size; public UnionFind(int n) { parents = IntStream.range(0, n).toArray(); size = new int[n]; Arrays.fill(size, 1); } public int getRootOf(int x) { if (parents[x] == x) { return x; } return parents[x] = getRootOf(parents[x]); } public boolean hasSameRoot(int x, int y) { return getRootOf(x) == getRootOf(y); } public int getSizeOf(int x) { return size[getRootOf(x)]; } public void unite(int x, int y) { int rootX = getRootOf(x); int rootY = getRootOf(y); if (rootX == rootY) { return; } if (getSizeOf(rootX) < getSizeOf(rootY)) { int t = rootX; rootX = rootY; rootY = t; } parents[rootY] = rootX; size[rootX] += size[rootY]; } public boolean allConected() { return getSizeOf(0) == size.length; } } import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.HashMap; import java.util.NoSuchElementException; import java.util.Objects; import java.util.stream.IntStream; public final class Main { private static final FastScanner sc = new FastScanner(); private static final PrintWriter pw = new PrintWriter(System.out); private static int n; private static int m; private static char[][] grid; private static final int[] dx = { 1, -1, 0, 0 }; private static final int[] dy = { 0, 0, 1, -1 }; public static final void main(String[] args) throws Exception { n = sc.nextInt(); m = sc.nextInt(); grid = new char[n][m]; for (int i = 0; i < n; ++i) { grid[i] = sc.next().toCharArray(); } var yDeque = new ArrayDeque<Integer>(n * m); var xDeque = new ArrayDeque<Integer>(n * m); yDeque.addLast(1); xDeque.addLast(1); var targets = new HashMap<String, Boolean>(n * m); targets.put(1 + " " + 1, true); while (!yDeque.isEmpty()) { final int y0 = yDeque.pollFirst(); final int x0 = xDeque.pollFirst(); for (int i = 0; i < 4; ++i) { int y = y0, x = x0; while (true) { grid[y][x] = 'v'; if (grid[y + dy[i]][x + dx[i]] == '#') { if (!targets.containsKey(y + " " + x)) { yDeque.addLast(y); xDeque.addLast(x); targets.put(y + " " + x, true); } break; } y += dy[i]; x += dx[i]; } } } int ans = 0; for (char[] row : grid) { for (char c : row) { if (c == 'v') { ++ans; } } } println(ans); pw.flush(); } static final void println(Object obj) { pw.println(Objects.toString(obj)); } static final class Permutation { private int size; private int[] init; private int[] perm; private boolean[] used; public Permutation(int size) { if (size < 1) { throw new IllegalArgumentException(); } this.size = size; init = IntStream.rangeClosed(1, size).toArray(); perm = new int[size]; used = new boolean[size]; } private void process() { } public void buildPermutation(int depth) { if (depth == size) { process(); return; } for (int i = 0; i < size; ++i) { if (used[i]) { continue; } perm[depth] = init[i]; used[i] = true; buildPermutation(depth + 1); used[i] = false; } } } } final class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } return buflen > 0; } private int readByte() { return hasNextByte() ? buffer[ptr++] : -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private static boolean isPrintableCharForLine(int c) { return 32 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public String nextLine() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableCharForLine(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) { throw new NumberFormatException(); } return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } } final class Pair<F extends Comparable<F>, S extends Comparable<S>> implements Comparable<Pair<F, S>> { private F first; private S second; public Pair(F first, S second) { this.first = first; this.second = second; } public F getFirst() { return first; } public S getSecond() { return second; } public void setFirst(F first) { this.first = first; } public void setSecond(S second) { this.second = second; } @Override public int compareTo(Pair<F, S> o) { int t = this.first.compareTo(o.getFirst()); if (t != 0) { return t; } return this.second.compareTo(o.getSecond()); } @Override public boolean equals(Object obj) { if (obj == null) { return false; } Pair<F, S> another = (Pair<F, S>) obj; return Objects.equals(this.getFirst(), another.getFirst()) && Objects.equals(this.getSecond(), another.getSecond()); } @Override public int hashCode() { int h1 = first.hashCode(); int h2 = second.hashCode(); String connected = String.valueOf(h1) + ' ' + String.valueOf(h2); return connected.hashCode(); } @Override public String toString() { return Objects.toString(first) + ' ' + Objects.toString(second); } } final class UnionFind { private int[] parents; private int[] size; public UnionFind(int n) { parents = IntStream.range(0, n).toArray(); size = new int[n]; Arrays.fill(size, 1); } public int getRootOf(int x) { if (parents[x] == x) { return x; } return parents[x] = getRootOf(parents[x]); } public boolean hasSameRoot(int x, int y) { return getRootOf(x) == getRootOf(y); } public int getSizeOf(int x) { return size[getRootOf(x)]; } public void unite(int x, int y) { int rootX = getRootOf(x); int rootY = getRootOf(y); if (rootX == rootY) { return; } if (getSizeOf(rootX) < getSizeOf(rootY)) { int t = rootX; rootX = rootY; rootY = t; } parents[rootY] = rootX; size[rootX] += size[rootY]; } public boolean allConected() { return getSizeOf(0) == size.length; } }
ConDefects/ConDefects/Code/abc311_d/Java/45729759
condefects-java_data_35
// package atcoder.beginner_300_399.abc_325; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Random; public final class Main { public static void main(String[] args) throws IOException { final FastReader fs = new FastReader(); final int n = fs.nextInt(); final PriorityQueue<long[]> pq = new PriorityQueue<>(Comparator.comparingLong(a -> a[0])); final PriorityQueue<long[]> pq2 = new PriorityQueue<>(Comparator.comparingLong(a -> a[1])); for (int i = 0; i < n; i++) { pq.offer(new long[] { fs.nextLong(), fs.nextLong() }); } int res = 0; long curr = -1; while (!pq.isEmpty() || !pq2.isEmpty()) { if (pq2.isEmpty()) { curr = Math.max(curr, pq.element()[0]); } while (!pq.isEmpty() && pq.element()[0] <= curr) { pq2.offer(pq.remove()); } final long[] u = pq2.remove(); if (curr <= u[0] + u[1]) { res++; curr = Math.max(u[0] + 1, curr + 1); } // System.out.println(curr + " " + Arrays.toString(u)); } System.out.println(res); } static final class Utils { private static class Shuffler { private static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } } public static void shuffleSort(int[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } public static void shuffleSort(long[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } private Utils() {} } static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(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 { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int readSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) {} return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public int[] nextIntArray(int n) throws IOException { final int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long[] nextLongArray(int n) throws IOException { final long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } } // package atcoder.beginner_300_399.abc_325; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; import java.util.Random; public final class Main { public static void main(String[] args) throws IOException { final FastReader fs = new FastReader(); final int n = fs.nextInt(); final PriorityQueue<long[]> pq = new PriorityQueue<>(Comparator.comparingLong(a -> a[0])); final PriorityQueue<long[]> pq2 = new PriorityQueue<>(Comparator.comparingLong(a -> a[0] + a[1])); for (int i = 0; i < n; i++) { pq.offer(new long[] { fs.nextLong(), fs.nextLong() }); } int res = 0; long curr = -1; while (!pq.isEmpty() || !pq2.isEmpty()) { if (pq2.isEmpty()) { curr = Math.max(curr, pq.element()[0]); } while (!pq.isEmpty() && pq.element()[0] <= curr) { pq2.offer(pq.remove()); } final long[] u = pq2.remove(); if (curr <= u[0] + u[1]) { res++; curr = Math.max(u[0] + 1, curr + 1); } // System.out.println(curr + " " + Arrays.toString(u)); } System.out.println(res); } static final class Utils { private static class Shuffler { private static void shuffle(int[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void shuffle(long[] x) { final Random r = new Random(); for (int i = 0; i <= x.length - 2; i++) { final int j = i + r.nextInt(x.length - i); swap(x, i, j); } } private static void swap(int[] x, int i, int j) { final int t = x[i]; x[i] = x[j]; x[j] = t; } private static void swap(long[] x, int i, int j) { final long t = x[i]; x[i] = x[j]; x[j] = t; } } public static void shuffleSort(int[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } public static void shuffleSort(long[] arr) { Shuffler.shuffle(arr); Arrays.sort(arr); } private Utils() {} } static class FastReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; FastReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } FastReader(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 { final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int readSign() throws IOException { byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) {} return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public int[] nextIntArray(int n) throws IOException { final int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long[] nextLongArray(int n) throws IOException { final long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.close(); } } }
ConDefects/ConDefects/Code/abc325_d/Java/46911206
condefects-java_data_36
import java.io.*; import java.math.*; import java.security.SecureRandom; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.Map.Entry; import java.util.regex.Pattern; import java.util.stream.Collectors; class Main implements Runnable { public static void solve () { int n = nextInt(); int[] a = nextIntArray(n); //a[i]がそれぞれ何個あるかを数える int[] count = new int[200009]; for (int i=0; i<n; i++) count[a[i]] += 1; long ans = 0; for (int i=0; i<n; i++) { //a[i]を全列挙する List<Integer> divList = allDivisor(a[i]); for (int div : divList) { int jNum = count[div]; int kNum = count[i / div]; ans += 1L * jNum * kNum; } } println(ans); } public static List<Integer> allDivisor (int n) { List<Integer> res = new ArrayList<>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i == 0) { res.add(i); if (n/i != i) res.add(n/i); } } Collections.sort(res); return res; } /* * ############################################################################################ * # useful fields, useful methods, useful class * ############################################################################################## */ // fields public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; // methods public static int min (int... a) {Arrays.sort(a); return a[0];} public static int max (int... a) {Arrays.sort(a); return a[a.length-1];} public static long min (long... a) {Arrays.sort(a); return a[0];} public static long max (long... a) {Arrays.sort(a); return a[a.length-1];} public static long pow (long c, long b) { long res = 1; for (int i=0; i<b; i++) { res *= c; } return res; } // class public static class Edge implements Comparable<Edge> { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo (Edge e) { return this.cost - e.cost; } } public static class Point implements Comparable<Point> { int x, y; Point (int x, int y) { this.x = x; this.y = y; } @Override public int compareTo (Point p) { return this.y - p.y; } } /* * ############################################################################################## * # input * ############################################################################################## */ // input - fields public static final InputStream in = System.in; public static final byte[] buffer = new byte[1024]; public static int ptr = 0; public static int bufferLength = 0; // input - basic methods public static boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } public static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } public static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public static void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public static boolean hasNext() { skipUnprintable(); return hasNextByte(); } // input - single public static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public static int nextInt() { return (int) nextLong(); } public static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public static double nextDouble() { return Double.parseDouble(next()); } // input - array public static String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public static double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } // input - table public static char[][] nextCharTable(int h, int w) { char[][] array = new char[h][w]; for (int i = 0; i < h; i++) array[i] = next().toCharArray(); return array; } public static int[][] nextIntMap(int h, int w) { int[][] a = new int[h][]; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) a[i][j] = nextInt(); } return a; } /* * ############################################################################################## * # output * ############################################################################################## */ // output - fields static PrintWriter out = new PrintWriter(System.out); //output - single public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} //output - array public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } /* * ############################################################################################## * # main * ############################################################################################## */ public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } } import java.io.*; import java.math.*; import java.security.SecureRandom; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.Map.Entry; import java.util.regex.Pattern; import java.util.stream.Collectors; class Main implements Runnable { public static void solve () { int n = nextInt(); int[] a = nextIntArray(n); //a[i]がそれぞれ何個あるかを数える int[] count = new int[200009]; for (int i=0; i<n; i++) count[a[i]] += 1; long ans = 0; for (int i=0; i<n; i++) { //a[i]を全列挙する List<Integer> divList = allDivisor(a[i]); for (int div : divList) { int jNum = count[div]; int kNum = count[a[i] / div]; ans += 1L * jNum * kNum; } } println(ans); } public static List<Integer> allDivisor (int n) { List<Integer> res = new ArrayList<>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i == 0) { res.add(i); if (n/i != i) res.add(n/i); } } Collections.sort(res); return res; } /* * ############################################################################################ * # useful fields, useful methods, useful class * ############################################################################################## */ // fields public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; // methods public static int min (int... a) {Arrays.sort(a); return a[0];} public static int max (int... a) {Arrays.sort(a); return a[a.length-1];} public static long min (long... a) {Arrays.sort(a); return a[0];} public static long max (long... a) {Arrays.sort(a); return a[a.length-1];} public static long pow (long c, long b) { long res = 1; for (int i=0; i<b; i++) { res *= c; } return res; } // class public static class Edge implements Comparable<Edge> { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo (Edge e) { return this.cost - e.cost; } } public static class Point implements Comparable<Point> { int x, y; Point (int x, int y) { this.x = x; this.y = y; } @Override public int compareTo (Point p) { return this.y - p.y; } } /* * ############################################################################################## * # input * ############################################################################################## */ // input - fields public static final InputStream in = System.in; public static final byte[] buffer = new byte[1024]; public static int ptr = 0; public static int bufferLength = 0; // input - basic methods public static boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } public static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } public static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public static void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public static boolean hasNext() { skipUnprintable(); return hasNextByte(); } // input - single public static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public static int nextInt() { return (int) nextLong(); } public static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public static double nextDouble() { return Double.parseDouble(next()); } // input - array public static String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public static int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public static long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public static double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } // input - table public static char[][] nextCharTable(int h, int w) { char[][] array = new char[h][w]; for (int i = 0; i < h; i++) array[i] = next().toCharArray(); return array; } public static int[][] nextIntMap(int h, int w) { int[][] a = new int[h][]; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) a[i][j] = nextInt(); } return a; } /* * ############################################################################################## * # output * ############################################################################################## */ // output - fields static PrintWriter out = new PrintWriter(System.out); //output - single public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} //output - array public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } /* * ############################################################################################## * # main * ############################################################################################## */ public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } }
ConDefects/ConDefects/Code/abc249_d/Java/39908157
condefects-java_data_37
//package kg.my_algorithms.codechef; import java.io.*; import java.util.*; public class Main { private static final FastReader fr = new FastReader(); public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); // int testCases = fr.nextInt(); // for(int testCase=1;testCase<=testCases;testCase++) { // } long n = fr.nextLong(); boolean[] visited = new boolean[1_00_00_01]; List<Long> primes = new ArrayList<>(); List<Long> prime_square = new ArrayList<>(); for(int i=2;i<1_00_00_01;i++){ if(!visited[i]){ prime_square.add((long)i*i); primes.add((long)i); for(int j=i;j<1_00_00_01;j+=i) visited[j] = true; } } long ans = 0L; for(int i=0;i<prime_square.size();i++){ for(int j=i+1;j<primes.size();j++){ long cur = prime_square.get(i)*primes.get(j); if(cur>n || isMultiplicationOverflow(prime_square.get(i),primes.get(j))) break; int index = searchInsert(prime_square,n/cur); index -= (j+1); if(index<=0) break; ans += index; } } out.println(ans); out.flush(); } public static boolean isMultiplicationOverflow(long a, long b) { if (a == 0 || b == 0) return false; long result = a * b; return a != result / b; } public static int searchInsert(List<Long> nums, long target) { int lo=0,hi=nums.size()-1; while(lo<=hi){ int m=lo+(hi-lo)/2; if(nums.get(m) ==target) return m-1; if(nums.get(m) >target) hi=m-1; else lo=m+1; } return lo; } } 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } } //package kg.my_algorithms.codechef; import java.io.*; import java.util.*; public class Main { private static final FastReader fr = new FastReader(); public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); // int testCases = fr.nextInt(); // for(int testCase=1;testCase<=testCases;testCase++) { // } long n = fr.nextLong(); boolean[] visited = new boolean[1_00_00_01]; List<Long> primes = new ArrayList<>(); List<Long> prime_square = new ArrayList<>(); for(int i=2;i<1_00_00_01;i++){ if(!visited[i]){ prime_square.add((long)i*i); primes.add((long)i); for(int j=i;j<1_00_00_01;j+=i) visited[j] = true; } } long ans = 0L; for(int i=0;i<prime_square.size();i++){ for(int j=i+1;j<primes.size();j++){ long cur = prime_square.get(i)*primes.get(j); if(cur>n || isMultiplicationOverflow(prime_square.get(i),primes.get(j))) break; int index = searchInsert(prime_square,n/cur); index -= (j+1); if(index<=0) break; ans += index; } } out.println(ans); out.flush(); } public static boolean isMultiplicationOverflow(long a, long b) { if (a == 0 || b == 0) return false; long result = a * b; return a != result / b; } public static int searchInsert(List<Long> nums, long target) { int lo=0,hi=nums.size()-1; while(lo<=hi){ int m=lo+(hi-lo)/2; if(nums.get(m) ==target) return m+1; if(nums.get(m) >target) hi=m-1; else lo=m+1; } return lo; } } 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 { if(st.hasMoreTokens()){ str = st.nextToken("\n"); } else{ str = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } return str; } }
ConDefects/ConDefects/Code/abc300_d/Java/41697840
condefects-java_data_38
import java.util.*; public class Main { public static void main(String[] args) { IOHandler io = new IOHandler(); long n = io.nextLong(); io.close(); /* * 前提 * a,b,c <= √n, 特に ac <= √n * * 方針 * (ac)^2 = n となる、(a, c) のペアを探す * a < b < c かつ b <= n/(ac)^2 となる b を求める */ // a, c の探索範囲となる、素数の集合を求める final int z = 1000000; // 素数の集合のオーダー、√max(n) = 10^6 boolean[] isPrime = new boolean[z + 1]; int[] countPrimes = new int[z + 1]; // List<Long> primes = new ArrayList<>(); // 2以上の整数に対し、その倍数を非素数とする for (int p = 2; p <= z; p++) isPrime[p] = true; for (int p = 2; p*p <= z; p++) { if (isPrime[p]) { for (int q = p*p; q <= z; q += p) isPrime[q] = false; } } // 累計の素数の個数を求める for (int p = 2; p <= z; p++) { if (isPrime[p]) { // primes.add((long) p); countPrimes[p]++; } } for (int p = 2; p <= z; p++) countPrimes[p] += countPrimes[p - 1]; long result = 0; // a^5 < (ac)^2 * b <= n より // a^5 <= n の範囲で a を探索する for (long a = 2; a*a*a*a*a <= n; a++) { if (!isPrime[(int) a]) continue; // (ac)^2 * c < (ac)^2 * b <= n より // (ac)^2 * c <= n の範囲で c を探索する for (long c = a + 1; a*a*a*c*c <= n; c++) { if (isPrime[(int) c]) continue; // a < b < c かつ b <= n/(ac)^2 となる b の個数をカウントする // -----+--------+----------> b // bl br // bl と br の間にある素数をカウントする(bl は含まない) long br = Math.min(c - 1, n / (a*a*c*c)); long bl = a; if (bl < br) { result += countPrimes[(int) br]; result -= countPrimes[(int) bl]; } } } io.output(result); } private static class IOHandler { private Scanner sc = new Scanner(System.in); private void close() {this.sc.close();} private long nextLong() {return this.sc.nextLong();} private void output(long result) {System.out.println(result);} } } import java.util.*; public class Main { public static void main(String[] args) { IOHandler io = new IOHandler(); long n = io.nextLong(); io.close(); /* * 前提 * a,b,c <= √n, 特に ac <= √n * * 方針 * (ac)^2 = n となる、(a, c) のペアを探す * a < b < c かつ b <= n/(ac)^2 となる b を求める */ // a, c の探索範囲となる、素数の集合を求める final int z = 1000000; // 素数の集合のオーダー、√max(n) = 10^6 boolean[] isPrime = new boolean[z + 1]; int[] countPrimes = new int[z + 1]; // List<Long> primes = new ArrayList<>(); // 2以上の整数に対し、その倍数を非素数とする for (int p = 2; p <= z; p++) isPrime[p] = true; for (int p = 2; p*p <= z; p++) { if (isPrime[p]) { for (int q = p*p; q <= z; q += p) isPrime[q] = false; } } // 累計の素数の個数を求める for (int p = 2; p <= z; p++) { if (isPrime[p]) { // primes.add((long) p); countPrimes[p]++; } } for (int p = 2; p <= z; p++) countPrimes[p] += countPrimes[p - 1]; long result = 0; // a^5 < (ac)^2 * b <= n より // a^5 <= n の範囲で a を探索する for (long a = 2; a*a*a*a*a <= n; a++) { if (!isPrime[(int) a]) continue; // (ac)^2 * c < (ac)^2 * b <= n より // (ac)^2 * c <= n の範囲で c を探索する for (long c = a + 1; a*a*a*c*c <= n; c++) { if (!isPrime[(int) c]) continue; // a < b < c かつ b <= n/(ac)^2 となる b の個数をカウントする // -----+--------+----------> b // bl br // bl と br の間にある素数をカウントする(bl は含まない) long br = Math.min(c - 1, n / (a*a*c*c)); long bl = a; if (bl < br) { result += countPrimes[(int) br]; result -= countPrimes[(int) bl]; } } } io.output(result); } private static class IOHandler { private Scanner sc = new Scanner(System.in); private void close() {this.sc.close();} private long nextLong() {return this.sc.nextLong();} private void output(long result) {System.out.println(result);} } }
ConDefects/ConDefects/Code/abc300_d/Java/43783148
condefects-java_data_39
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.stream.IntStream; import java.io.ByteArrayOutputStream; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.util.InputMismatchException; import java.io.FileNotFoundException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author tauros */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; RealFastReader in = new RealFastReader(inputStream); RealFastWriter out = new RealFastWriter(outputStream); ABC256F solver = new ABC256F(); solver.solve(1, in, out); out.close(); } static class ABC256F { private final int MOD = 998244353; private int lowBit(int x) { return x & -x; } private void update(long[] bit, int index, long val) { val = (val % MOD + MOD) % MOD; for (; index < bit.length; index += lowBit(index)) { bit[index] += val; if (bit[index] >= MOD) { bit[index] -= MOD; } } } private long query(long[] bit, int index) { long ans = 0; for (; index > 0; index -= lowBit(index)) { ans += bit[index]; if (ans >= MOD) { ans -= MOD; } } return ans; } private void update(long[] sum1, long[] sum2, long[] sum3, final int pos, final long val) { long oriVal1 = query(sum1, pos) - query(sum1, pos - 1); update(sum1, pos, val - oriVal1); long oriVal2 = query(sum2, pos) - query(sum2, pos - 1); long curVal2 = (pos * val) % MOD; update(sum2, pos, curVal2 - oriVal2); long oriVal3 = query(sum3, pos) - query(sum3, pos - 1); long curVal3 = (val * pos % MOD * pos) % MOD; update(sum3, pos, curVal3 - oriVal3); } private long query(long[] sum1, long[] sum2, long[] sum3, int pos) { long fact1 = (long) (pos + 1) * (pos + 2) % MOD; long part1 = query(sum1, pos) * fact1 % MOD; long fact2 = -(3 + 2L * pos) % MOD; long part2 = query(sum2, pos) * fact2 % MOD; long part3 = query(sum3, pos); long ans = (part3 + part2 + part1) % MOD; ans = ans * Common.invExgcdLong(2, MOD) % MOD; return ans; } public void solve(int testNumber, RealFastReader in, RealFastWriter out) { int n = in.ni(), q = in.ni(); long[] sum1 = new long[n + 1]; long[] sum2 = new long[n + 1]; long[] sum3 = new long[n + 1]; for (int i = 1; i <= n; i++) { int a = in.ni(); update(sum1, sum2, sum3, i, a); } while (q-- > 0) { int op = in.ni(); if (op == 1) { int x = in.ni(), v = in.ni(); update(sum1, sum2, sum3, x, v); } else if (op == 2) { int x = in.ni(); long ans = query(sum1, sum2, sum3, x); out.println(ans); } } out.flush(); } } static class RealFastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private OutputStream out; private Writer writer; private int ptr = 0; private RealFastWriter() { out = null; } public RealFastWriter(Writer writer) { this.writer = new BufferedWriter(writer); out = new ByteArrayOutputStream(); } public RealFastWriter(OutputStream os) { this.out = os; } public RealFastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public RealFastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) { innerflush(); } return this; } public RealFastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) { innerflush(); } }); return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) { return 19; } if (l >= 100000000000000000L) { return 18; } if (l >= 10000000000000000L) { return 17; } if (l >= 1000000000000000L) { return 16; } if (l >= 100000000000000L) { return 15; } if (l >= 10000000000000L) { return 14; } if (l >= 1000000000000L) { return 13; } if (l >= 100000000000L) { return 12; } if (l >= 10000000000L) { return 11; } if (l >= 1000000000L) { return 10; } if (l >= 100000000L) { return 9; } if (l >= 10000000L) { return 8; } if (l >= 1000000L) { return 7; } if (l >= 100000L) { return 6; } if (l >= 10000L) { return 5; } if (l >= 1000L) { return 4; } if (l >= 100L) { return 3; } if (l >= 10L) { return 2; } return 1; } public RealFastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) { innerflush(); } if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public RealFastWriter writeln(long x) { return write(x).writeln(); } public RealFastWriter writeln() { return write((byte) '\n'); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { if (writer != null) { writer.write(((ByteArrayOutputStream) out).toString()); out = new ByteArrayOutputStream(); writer.flush(); } else { out.flush(); } } catch (IOException e) { throw new RuntimeException("flush"); } } public RealFastWriter println(long x) { return writeln(x); } public void close() { flush(); try { out.close(); } catch (Exception e) { } } } static final class Common { public static long[] exgcdLong(long a, long b) { // calc ax + by = gcd(a, b) if (b == 0) { // gcd, x, y return new long[]{ a, 1, 0 }; } long[] res = exgcdLong(b, a % b); // x = y0; y = x0 - (a / b) * y0; long gcd = res[0], x0 = res[1], y0 = res[2]; long x = y0, y = x0 - (a / b) * y0; return new long[]{ gcd, x, y }; } public static long invExgcdLong(long a, long p) { long[] exgcd = exgcdLong(a, p); if (exgcd[0] != 1L) { return -1L; } long inv = exgcd[1]; return (inv % p + p) % p; } } static class RealFastReader { InputStream is; private byte[] inbuf = new byte[1024]; public int lenbuf = 0; public int ptrbuf = 0; public RealFastReader(final InputStream is) { this.is = is; } public 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++]; } public int ni() { return (int) nl(); } public 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(); } } } } import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.stream.IntStream; import java.io.ByteArrayOutputStream; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.util.InputMismatchException; import java.io.FileNotFoundException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author tauros */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; RealFastReader in = new RealFastReader(inputStream); RealFastWriter out = new RealFastWriter(outputStream); ABC256F solver = new ABC256F(); solver.solve(1, in, out); out.close(); } static class ABC256F { private final int MOD = 998244353; private int lowBit(int x) { return x & -x; } private void update(long[] bit, int index, long val) { val = (val % MOD + MOD) % MOD; for (; index < bit.length; index += lowBit(index)) { bit[index] += val; if (bit[index] >= MOD) { bit[index] -= MOD; } } } private long query(long[] bit, int index) { long ans = 0; for (; index > 0; index -= lowBit(index)) { ans += bit[index]; if (ans >= MOD) { ans -= MOD; } } return ans; } private void update(long[] sum1, long[] sum2, long[] sum3, final int pos, final long val) { long oriVal1 = query(sum1, pos) - query(sum1, pos - 1); update(sum1, pos, val - oriVal1); long oriVal2 = query(sum2, pos) - query(sum2, pos - 1); long curVal2 = (pos * val) % MOD; update(sum2, pos, curVal2 - oriVal2); long oriVal3 = query(sum3, pos) - query(sum3, pos - 1); long curVal3 = (val * pos % MOD * pos) % MOD; update(sum3, pos, curVal3 - oriVal3); } private long query(long[] sum1, long[] sum2, long[] sum3, int pos) { long fact1 = (long) (pos + 1) * (pos + 2) % MOD; long part1 = query(sum1, pos) * fact1 % MOD; long fact2 = -(3 + 2L * pos) % MOD; long part2 = query(sum2, pos) * fact2 % MOD; long part3 = query(sum3, pos); long ans = (part3 + part2 + MOD + part1) % MOD; ans = ans * Common.invExgcdLong(2, MOD) % MOD; return ans; } public void solve(int testNumber, RealFastReader in, RealFastWriter out) { int n = in.ni(), q = in.ni(); long[] sum1 = new long[n + 1]; long[] sum2 = new long[n + 1]; long[] sum3 = new long[n + 1]; for (int i = 1; i <= n; i++) { int a = in.ni(); update(sum1, sum2, sum3, i, a); } while (q-- > 0) { int op = in.ni(); if (op == 1) { int x = in.ni(), v = in.ni(); update(sum1, sum2, sum3, x, v); } else if (op == 2) { int x = in.ni(); long ans = query(sum1, sum2, sum3, x); out.println(ans); } } out.flush(); } } static class RealFastWriter { private static final int BUF_SIZE = 1 << 13; private final byte[] buf = new byte[BUF_SIZE]; private OutputStream out; private Writer writer; private int ptr = 0; private RealFastWriter() { out = null; } public RealFastWriter(Writer writer) { this.writer = new BufferedWriter(writer); out = new ByteArrayOutputStream(); } public RealFastWriter(OutputStream os) { this.out = os; } public RealFastWriter(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public RealFastWriter write(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) { innerflush(); } return this; } public RealFastWriter write(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) { innerflush(); } }); return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) { return 19; } if (l >= 100000000000000000L) { return 18; } if (l >= 10000000000000000L) { return 17; } if (l >= 1000000000000000L) { return 16; } if (l >= 100000000000000L) { return 15; } if (l >= 10000000000000L) { return 14; } if (l >= 1000000000000L) { return 13; } if (l >= 100000000000L) { return 12; } if (l >= 10000000000L) { return 11; } if (l >= 1000000000L) { return 10; } if (l >= 100000000L) { return 9; } if (l >= 10000000L) { return 8; } if (l >= 1000000L) { return 7; } if (l >= 100000L) { return 6; } if (l >= 10000L) { return 5; } if (l >= 1000L) { return 4; } if (l >= 100L) { return 3; } if (l >= 10L) { return 2; } return 1; } public RealFastWriter write(long x) { if (x == Long.MIN_VALUE) { return write("" + x); } if (ptr + 21 >= BUF_SIZE) { innerflush(); } if (x < 0) { write((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public RealFastWriter writeln(long x) { return write(x).writeln(); } public RealFastWriter writeln() { return write((byte) '\n'); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { if (writer != null) { writer.write(((ByteArrayOutputStream) out).toString()); out = new ByteArrayOutputStream(); writer.flush(); } else { out.flush(); } } catch (IOException e) { throw new RuntimeException("flush"); } } public RealFastWriter println(long x) { return writeln(x); } public void close() { flush(); try { out.close(); } catch (Exception e) { } } } static final class Common { public static long[] exgcdLong(long a, long b) { // calc ax + by = gcd(a, b) if (b == 0) { // gcd, x, y return new long[]{ a, 1, 0 }; } long[] res = exgcdLong(b, a % b); // x = y0; y = x0 - (a / b) * y0; long gcd = res[0], x0 = res[1], y0 = res[2]; long x = y0, y = x0 - (a / b) * y0; return new long[]{ gcd, x, y }; } public static long invExgcdLong(long a, long p) { long[] exgcd = exgcdLong(a, p); if (exgcd[0] != 1L) { return -1L; } long inv = exgcd[1]; return (inv % p + p) % p; } } static class RealFastReader { InputStream is; private byte[] inbuf = new byte[1024]; public int lenbuf = 0; public int ptrbuf = 0; public RealFastReader(final InputStream is) { this.is = is; } public 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++]; } public int ni() { return (int) nl(); } public 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(); } } } }
ConDefects/ConDefects/Code/abc256_f/Java/32637776
condefects-java_data_40
import java.lang.*; import java.util.*; import java.io.*; import java.math.*; class Main { static int mod = 998244353; static int lowbit(int x) { return x&(-x); } static void update(int x,int v,int[] a) { int n = a.length; while(x<n) { a[x] = ((a[x]+v) % mod +mod) % mod; x += lowbit(x); } } static int getsum(int m,int[] arr) { int res = 0; while(m>0) { res += arr[m]; res %= mod; m -= lowbit(m); } return (res+mod) % mod; } public static void deal(int n,int[] a,int q,int[][] qu) { int[] b = new int[n+1]; int[] c = new int[n+1]; int[] d = new int[n+1]; for(int i=1;i<=n;i++) { update(i,a[i],b); long t = a[i]; t = t * i % mod; update(i,(int)t,c); t = t * i % mod; update(i,(int)t,d); } long inv2 = mod-mod/2*1%mod; for(int i=0;i<q;i++) { if(qu[i][0] == 1) { long diff = ((qu[i][2]-a[qu[i][1]]) % mod+mod) % mod; update(qu[i][1], (int)diff, b); diff = diff * qu[i][1] % mod; update(qu[i][1], (int)diff, c); diff = diff * qu[i][1] % mod; update(qu[i][1], (int)diff, d); a[qu[i][1]] = qu[i][2]; } else { long t1 = getsum(qu[i][1],b); long t2 = getsum(qu[i][1],c); long t3 = getsum(qu[i][1],d); long t = (t3-t2*(2*qu[i][1]+3)+t1*(qu[i][1]+1)*(qu[i][1]+2)) %mod; t *= inv2; t = (t % mod +mod) % mod; out.println(t); } } } public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); int q = sc.nextInt(); int[] a = new int[n+1]; int[][] b = new int[q][3]; for(int i=0;i<n;i++) a[i+1] = sc.nextInt(); for(int i=0;i<q;i++) { b[i][0] = sc.nextInt(); b[i][1] = sc.nextInt(); if(b[i][0] == 1) { b[i][2] = sc.nextInt(); } } deal(n,a,q,b); out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } } import java.lang.*; import java.util.*; import java.io.*; import java.math.*; class Main { static int mod = 998244353; static int lowbit(int x) { return x&(-x); } static void update(int x,int v,int[] a) { int n = a.length; while(x<n) { a[x] = ((a[x]+v) % mod +mod) % mod; x += lowbit(x); } } static int getsum(int m,int[] arr) { int res = 0; while(m>0) { res += arr[m]; res %= mod; m -= lowbit(m); } return (res+mod) % mod; } public static void deal(int n,int[] a,int q,int[][] qu) { int[] b = new int[n+1]; int[] c = new int[n+1]; int[] d = new int[n+1]; for(int i=1;i<=n;i++) { update(i,a[i],b); long t = a[i]; t = t * i % mod; update(i,(int)t,c); t = t * i % mod; update(i,(int)t,d); } long inv2 = mod-mod/2*1%mod; for(int i=0;i<q;i++) { if(qu[i][0] == 1) { long diff = ((qu[i][2]-a[qu[i][1]]) % mod+mod) % mod; update(qu[i][1], (int)diff, b); diff = diff * qu[i][1] % mod; update(qu[i][1], (int)diff, c); diff = diff * qu[i][1] % mod; update(qu[i][1], (int)diff, d); a[qu[i][1]] = qu[i][2]; } else { long t1 = getsum(qu[i][1],b); long t2 = getsum(qu[i][1],c); long t3 = getsum(qu[i][1],d); long t = (t3-t2*(2L*qu[i][1]+3) % mod +t1*(qu[i][1]+1) % mod *(qu[i][1]+2) % mod) %mod; t *= inv2; t = (t % mod +mod) % mod; out.println(t); } } } public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n = sc.nextInt(); int q = sc.nextInt(); int[] a = new int[n+1]; int[][] b = new int[q][3]; for(int i=0;i<n;i++) a[i+1] = sc.nextInt(); for(int i=0;i<q;i++) { b[i][0] = sc.nextInt(); b[i][1] = sc.nextInt(); if(b[i][0] == 1) { b[i][2] = sc.nextInt(); } } deal(n,a,q,b); out.close(); } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
ConDefects/ConDefects/Code/abc256_f/Java/32784972
condefects-java_data_41
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char[] s = sc.next().toCharArray(); long n = sc.nextLong(); long t = 1, sum = 0; for (int i = 0; i < s.length; ++i) { if (s[i] == '1') { sum += t; } t *= 2; } for (int i = 0; i < s.length; ++i) { t /= 2; if (s[i] == '?') { if (sum + t <= n) { sum += t; } } } System.out.println(sum <= n ? sum : -1); sc.close(); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char[] s = sc.next().toCharArray(); long n = sc.nextLong(); long t = 1, sum = 0; for (int i = s.length - 1; i >= 0; --i) { if (s[i] == '1') { sum += t; } t *= 2; } for (int i = 0; i < s.length; ++i) { t /= 2; if (s[i] == '?') { if (sum + t <= n) { sum += t; } } } System.out.println(sum <= n ? sum : -1); sc.close(); } }
ConDefects/ConDefects/Code/abc301_d/Java/43543519
condefects-java_data_42
import java.io.*; import java.util.StringTokenizer; /** * @author weidq * @version 1.0 * @date 2022-10-20 12:15 * D - Bitmask */ public class Main { static class FastReader { BufferedReader reader; StringTokenizer tokenizer; FastReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 1024); tokenizer = null; } // reads in the next string String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } // reads in the next int int nextInt() { return Integer.parseInt(next()); } // reads in the next long long nextLong() { return Long.parseLong(next()); } // reads in the next double double nextDouble() { return Double.parseDouble(next()); } void close() { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } private static FastReader in = new FastReader(System.in); private static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) { String s = in.next(); char[] chars = s.toCharArray(); long m = in.nextLong(); int n = chars.length; int ans = 0; for (int i = 0; i < n; i++) { if (chars[i] == '1') { long now = 1L << (n - i - 1); if (ans + now > m) { ans = -1; break; } ans += now; } } if (ans == -1) out.println(-1); else { for (int i = 0; i < n; i++) { if (chars[i] == '?') { long now = 1L << (n - i - 1); if (ans + now <= m) { ans += now; } } } out.println(ans); } out.flush(); out.close(); in.close(); } } import java.io.*; import java.util.StringTokenizer; /** * @author weidq * @version 1.0 * @date 2022-10-20 12:15 * D - Bitmask */ public class Main { static class FastReader { BufferedReader reader; StringTokenizer tokenizer; FastReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 1024); tokenizer = null; } // reads in the next string String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } // reads in the next int int nextInt() { return Integer.parseInt(next()); } // reads in the next long long nextLong() { return Long.parseLong(next()); } // reads in the next double double nextDouble() { return Double.parseDouble(next()); } void close() { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } private static FastReader in = new FastReader(System.in); private static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static void main(String[] args) { String s = in.next(); char[] chars = s.toCharArray(); long m = in.nextLong(); int n = chars.length; long ans = 0; for (int i = 0; i < n; i++) { if (chars[i] == '1') { long now = 1L << (n - i - 1); if (ans + now > m) { ans = -1; break; } ans += now; } } if (ans == -1) out.println(-1); else { for (int i = 0; i < n; i++) { if (chars[i] == '?') { long now = 1L << (n - i - 1); if (ans + now <= m) { ans += now; } } } out.println(ans); } out.flush(); out.close(); in.close(); } }
ConDefects/ConDefects/Code/abc301_d/Java/45941482
condefects-java_data_43
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 文字列 char[] s = sc.next().toCharArray(); // 最大数 long max = sc.nextLong(); // 文字列を解析 int min = 0; // 最小値 ArrayList<Long> q = new ArrayList<>(); // ?の値 long num = 1; // 各桁の値を10進数に直した数 for (int i = 0; i < s.length; i++) { if (s[s.length-1 - i] == '1') { min += num; } else if (s[s.length-1 - i] == '?') { q.add(0, num); } num *= 2; // 次の桁に行くので2倍 } // minの時点でmaxを超えてたら-1を出力 if (min > max) { System.out.println(-1); return; } // 等しかったらそのままminを答えとして出力 else if (min == max) { System.out.println(min); return; } // 上の桁から?を1にして足してみる for (long p:q) { // maxを超えなかったらminに加算 if (min + p < max) { min += p; } // maxとぴったり一緒ならそれが答え else if (min + p == max) { System.out.println(max); return; } } // 最終的なminを出力 System.out.println(min); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 文字列 char[] s = sc.next().toCharArray(); // 最大数 long max = sc.nextLong(); // 文字列を解析 long min = 0; // 最小値 ArrayList<Long> q = new ArrayList<>(); // ?の値 long num = 1; // 各桁の値を10進数に直した数 for (int i = 0; i < s.length; i++) { if (s[s.length-1 - i] == '1') { min += num; } else if (s[s.length-1 - i] == '?') { q.add(0, num); } num *= 2; // 次の桁に行くので2倍 } // minの時点でmaxを超えてたら-1を出力 if (min > max) { System.out.println(-1); return; } // 等しかったらそのままminを答えとして出力 else if (min == max) { System.out.println(min); return; } // 上の桁から?を1にして足してみる for (long p:q) { // maxを超えなかったらminに加算 if (min + p < max) { min += p; } // maxとぴったり一緒ならそれが答え else if (min + p == max) { System.out.println(max); return; } } // 最終的なminを出力 System.out.println(min); } }
ConDefects/ConDefects/Code/abc301_d/Java/45476027
condefects-java_data_44
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); char[] cs = sc.nextLine().toCharArray(); long n = sc.nextLong(); long res=0; int len=cs.length; for (int i = 0; i < cs.length; i++) { if (cs[i]=='1'){ res+=((long) 1<<(len-i-1)); } } if (res>n){ System.out.println(-1); return; } for (int i = 0; i < cs.length; i++) { if (cs[i]=='?'){ if (res+((long)1<<(len-i-1))-n<0){ res+=((long)1<<(len-i-1)); } } } System.out.println(res); } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); char[] cs = sc.nextLine().toCharArray(); long n = sc.nextLong(); long res=0; int len=cs.length; for (int i = 0; i < cs.length; i++) { if (cs[i]=='1'){ res+=((long) 1<<(len-i-1)); } } if (res>n){ System.out.println(-1); return; } for (int i = 0; i < cs.length; i++) { if (cs[i]=='?'){ if (res+((long)1<<(len-i-1))-n<=0){ res+=((long)1<<(len-i-1)); } } } System.out.println(res); } }
ConDefects/ConDefects/Code/abc301_d/Java/45726158
condefects-java_data_45
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); long n = sc.nextLong(); List<Integer> pos = new ArrayList<>(); long number = 0; for (int i=0;i<s.length();i++){ char c = s.charAt(i); if (c=='1'){ number = number|1; }else if (c=='?'){ pos.add(i); } number = number<<1; } if (number>n) System.out.println(-1); else { for (var p:pos){ p = s.length()-p-1; if ((number|(1L<<p))<=n){ number = number| (1L <<p); } } System.out.println(number); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); long n = sc.nextLong(); List<Integer> pos = new ArrayList<>(); long number = 0; for (int i=0;i<s.length();i++){ number = number<<1; char c = s.charAt(i); if (c=='1'){ number = number|1; }else if (c=='?'){ pos.add(i); } } if (number>n) System.out.println(-1); else { for (var p:pos){ p = s.length()-p-1; if ((number|(1L<<p))<=n){ number = number| (1L <<p); } } System.out.println(number); } } }
ConDefects/ConDefects/Code/abc301_d/Java/45733207
condefects-java_data_46
import java.util.Scanner; public class Main { static long min = -1L; public static void main(final String[] args) { try (Scanner sc = new Scanner(System.in)) { final String S = sc.next(); final long N = sc.nextLong(); if (Long.parseLong(S.replace('?', '0'), 2) > N) { System.out.println(min); return; } atmarkReplace(S, N); System.out.println(min); } } public static void atmarkReplace(final String S, final long N) { if (!S.contains("?")) { min = Long.parseLong(S, 2); return; } if (Long.parseLong(S.replaceFirst("\\?", "1").replace('?', '0'), 2) < N) { atmarkReplace(S.replaceFirst("\\?", "1"), N); } else { atmarkReplace(S.replaceFirst("\\?", "0"), N); } } } import java.util.Scanner; public class Main { static long min = -1L; public static void main(final String[] args) { try (Scanner sc = new Scanner(System.in)) { final String S = sc.next(); final long N = sc.nextLong(); if (Long.parseLong(S.replace('?', '0'), 2) > N) { System.out.println(min); return; } atmarkReplace(S, N); System.out.println(min); } } public static void atmarkReplace(final String S, final long N) { if (!S.contains("?")) { min = Long.parseLong(S, 2); return; } if (Long.parseLong(S.replaceFirst("\\?", "1").replace('?', '0'), 2) <= N) { atmarkReplace(S.replaceFirst("\\?", "1"), N); } else { atmarkReplace(S.replaceFirst("\\?", "0"), N); } } }
ConDefects/ConDefects/Code/abc301_d/Java/43924063
condefects-java_data_47
import java.io.*; import java.util.*; public class Main { private void tc() { String s=io.next();long t=io.nextLong(); long a=Long.parseLong(s.replaceAll("\\?", "0"),2);if(a>t){io.println(-1);return;} for(int i=0,n=s.length();i<n;i++)if(s.charAt(i)=='?'){var b=1<<(n-i-1);if(a+b<=t)a+=b;} io.println(a);return; } public static void main(String[] args) { boolean fm=false;//fm=true; try{io=fm?new Kattio(new FileInputStream(p("input.txt"))):new Kattio();}catch (FileNotFoundException e) {e.printStackTrace();} int n=fm?io.nextInt():1;for(int i=0;i<n;i++)new Main().tc();io.flush();io.close(); } private static Kattio io=null; public static File p(String a){return new File(Main.class.getResource("").getPath()+a);} public static class Kattio extends PrintWriter{ BufferedReader r;StringTokenizer st; public Kattio(){this(System.in,System.out);} public Kattio(FileInputStream f){this(f,System.out);} public Kattio(InputStream i,OutputStream o){super(o);r=new BufferedReader(new InputStreamReader(i));} public Kattio(String intput, String output)throws IOException{super(output);r=new BufferedReader(new FileReader(intput));} public String next(){try{while(st==null||!st.hasMoreTokens())st=new StringTokenizer(r.readLine());return st.nextToken();}catch(Exception e){}return null;} public int nextInt(){return Integer.parseInt(next());} public double nextDouble(){return Double.parseDouble(next());} public long nextLong(){return Long.parseLong(next());} } } import java.io.*; import java.util.*; public class Main { private void tc() { String s=io.next();long t=io.nextLong(); long a=Long.parseLong(s.replaceAll("\\?", "0"),2);if(a>t){io.println(-1);return;} for(int i=0,n=s.length();i<n;i++)if(s.charAt(i)=='?'){var b=1L<<(n-i-1);if(a+b<=t)a+=b;} io.println(a);return; } public static void main(String[] args) { boolean fm=false;//fm=true; try{io=fm?new Kattio(new FileInputStream(p("input.txt"))):new Kattio();}catch (FileNotFoundException e) {e.printStackTrace();} int n=fm?io.nextInt():1;for(int i=0;i<n;i++)new Main().tc();io.flush();io.close(); } private static Kattio io=null; public static File p(String a){return new File(Main.class.getResource("").getPath()+a);} public static class Kattio extends PrintWriter{ BufferedReader r;StringTokenizer st; public Kattio(){this(System.in,System.out);} public Kattio(FileInputStream f){this(f,System.out);} public Kattio(InputStream i,OutputStream o){super(o);r=new BufferedReader(new InputStreamReader(i));} public Kattio(String intput, String output)throws IOException{super(output);r=new BufferedReader(new FileReader(intput));} public String next(){try{while(st==null||!st.hasMoreTokens())st=new StringTokenizer(r.readLine());return st.nextToken();}catch(Exception e){}return null;} public int nextInt(){return Integer.parseInt(next());} public double nextDouble(){return Double.parseDouble(next());} public long nextLong(){return Long.parseLong(next());} } }
ConDefects/ConDefects/Code/abc301_d/Java/45718570
condefects-java_data_48
import java.util.*; import java.lang.Math.*; public class Main { //クラス名はMain public static void main(String[] args) { //コード Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int[] p = new int[n]; int[] ans = new int[n]; for(int i=0;i<n;i++){ p[i] = Integer.parseInt(sc.next()); ans[i]=99999; } for(int i=0;i<n;i++){ for(int j=1;j<=ans[i];j++){ if(i-j>=0){ ans[i]=Math.min(ans[i],j+Math.abs(p[i]-p[i-j])); } if(i+j<n){ ans[i]=Math.min(ans[i],j+Math.abs(p[i]-p[i+j])); } } } for(int i=0;i<n;i++){ if(i!=0){ System.out.print(" "); } System.out.print(ans[i]); } } } import java.util.*; import java.lang.Math.*; public class Main { //クラス名はMain public static void main(String[] args) { //コード Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int[] p = new int[n]; int[] ans = new int[n]; for(int i=0;i<n;i++){ p[i] = Integer.parseInt(sc.next()); ans[i]=999999; } for(int i=0;i<n;i++){ for(int j=1;j<=ans[i];j++){ if(i-j>=0){ ans[i]=Math.min(ans[i],j+Math.abs(p[i]-p[i-j])); } if(i+j<n){ ans[i]=Math.min(ans[i],j+Math.abs(p[i]-p[i+j])); } } } for(int i=0;i<n;i++){ if(i!=0){ System.out.print(" "); } System.out.print(ans[i]); } } }
ConDefects/ConDefects/Code/abc283_f/Java/37517593
condefects-java_data_49
import java.util.*; import java.io.*; class Main { public static void main(String[] args) { ContestScanner sc = new ContestScanner(); ContestPrinter ou = new ContestPrinter(); int A = sc.nextInt(); int B = sc.nextInt(); ou.println(Math.pow(32,A-B)); ou.flush(); } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in) { this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter { public ContestPrinter(java.io.PrintStream stream) { super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException { super(new java.io.PrintStream(file)); } public ContestPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public <T> void printArray(T[] array) { this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n - 1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) { this.printArray(array, " ", map); } } import java.util.*; import java.io.*; class Main { public static void main(String[] args) { ContestScanner sc = new ContestScanner(); ContestPrinter ou = new ContestPrinter(); int A = sc.nextInt(); int B = sc.nextInt(); ou.println((int)Math.pow(32,A-B)); ou.flush(); } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in) { this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter { public ContestPrinter(java.io.PrintStream stream) { super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException { super(new java.io.PrintStream(file)); } public ContestPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public <T> void printArray(T[] array) { this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n - 1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) { this.printArray(array, " ", map); } }
ConDefects/ConDefects/Code/abc221_a/Java/37724455
condefects-java_data_50
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.print((int)Math.pow(32,(double)a/b)); sc.close(); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.print((int)Math.pow(32,a-b)); sc.close(); } }
ConDefects/ConDefects/Code/abc221_a/Java/36706818
condefects-java_data_51
import java.io.PrintWriter; import java.math.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; class Main { static void solve (Scanner in, PrintWriter out) { int a = in.nextInt(), b = in.nextInt(); out.println(Math.pow(32, a-b)); } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); solve(in, out); in.close(); out.close(); } } import java.io.PrintWriter; import java.math.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; class Main { static void solve (Scanner in, PrintWriter out) { int a = in.nextInt(), b = in.nextInt(); out.println((int)(Math.pow(32, a-b))); } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); solve(in, out); in.close(); out.close(); } }
ConDefects/ConDefects/Code/abc221_a/Java/37447678
condefects-java_data_52
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int b=scanner.nextInt(); System.out.println(Math.pow(32,a-b)); } } import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int b=scanner.nextInt(); System.out.println((int)Math.pow(32,a-b)); } }
ConDefects/ConDefects/Code/abc221_a/Java/41497748
condefects-java_data_53
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); if(a==b) System.out.println(1); else System.out.println(Math.pow(32,Math.abs(a-b))); } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); if(a==b) System.out.println(1); else System.out.println((int)Math.pow(32,Math.abs(a-b))); } }
ConDefects/ConDefects/Code/abc221_a/Java/38271049
condefects-java_data_54
import java.util.Scanner; public class Main{ public static void main(String []args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int n = a - b; if (a > b) { System.out.println(Math.pow(32, n)); } else if (a == b) { System.out.println(1); } } } import java.util.Scanner; public class Main{ public static void main(String []args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int n = a - b; if (a > b) { System.out.println((int)Math.pow(32, n)); } else if (a == b) { System.out.println(1); } } }
ConDefects/ConDefects/Code/abc221_a/Java/38748942
condefects-java_data_55
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double b = sc.nextDouble(); double ans = Math.pow(32.0, a - b); System.out.println(ans); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double b = sc.nextDouble(); double ans = Math.pow(32.0, a - b); System.out.println((int)ans); } }
ConDefects/ConDefects/Code/abc221_a/Java/45490610
condefects-java_data_56
import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n=scan.nextInt(); int k =scan.nextInt(); System.out.println(Math.pow(32,(n-k))); } } import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n=scan.nextInt(); int k =scan.nextInt(); System.out.println((int)Math.pow(32,(n-k))); } }
ConDefects/ConDefects/Code/abc221_a/Java/38114865
condefects-java_data_57
import java.util.*; class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); HashMap<Integer,Integer> hash = new HashMap<>(); int num =sc.nextInt(); int target=sc.nextInt(); while(num-->0) { int key=sc.nextInt(); hash.put(key, 1); if(hash.containsKey(key-target) || (key == 0 &&hash.containsKey(target))) { System.out.println("Yes"); return; } } System.out.println("No"); } } import java.util.*; class Main { public static void main (String[] args) { Scanner sc = new Scanner (System.in); HashMap<Integer,Integer> hash = new HashMap<>(); int num =sc.nextInt(); int target=sc.nextInt(); while(num-->0) { int key=sc.nextInt(); hash.put(key, 1); if(hash.containsKey(key-target) || hash.containsKey(target+key)) { System.out.println("Yes"); return; } } System.out.println("No"); } }
ConDefects/ConDefects/Code/abc296_c/Java/44832882
condefects-java_data_58
import java.util.*; class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int x=sc.nextInt(); ArrayList<Integer>ar=new ArrayList<Integer>(); for(int i=0;i<n;i++){ ar.add(sc.nextInt()); } Collections.sort(ar); int j=1; int f=0; for(int i=0;i<n;i++) { while((j+1)<n && ar.get(j+1)-ar.get(i)<=x) j++; if(ar.get(j)-ar.get(i)==x) f=1; } if(f==1) System.out.println("Yes"); else System.out.println("No"); } } import java.util.*; class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int x=sc.nextInt(); ArrayList<Integer>ar=new ArrayList<Integer>(); for(int i=0;i<n;i++){ ar.add(sc.nextInt()); } Collections.sort(ar); int j=0; int f=0; for(int i=0;i<n;i++) { while((j+1)<n && ar.get(j+1)-ar.get(i)<=x) j++; if(ar.get(j)-ar.get(i)==x) f=1; } if(f==1) System.out.println("Yes"); else System.out.println("No"); } }
ConDefects/ConDefects/Code/abc296_c/Java/43516608
condefects-java_data_59
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] sc = new int[n]; int[] bon = new int[n + 1]; for (int i = 0; i < n; i++) { sc[i] = in.nextInt(); } for (int i = 0; i < m; i++) { bon[in.nextInt()] = in.nextInt(); } long[][] dp = new long[n + 1][n + 1]; long ans = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { if (j == 0){ dp[i][j] = ans; } else if (j > i) break; else{ dp[i][j] = dp[i - 1][j - 1] + sc[i - 1] + bon[j]; ans = Math.max(ans, dp[i][j]); } } } System.out.println(ans); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] sc = new int[n]; int[] bon = new int[n + 1]; for (int i = 0; i < n; i++) { sc[i] = in.nextInt(); } for (int i = 0; i < m; i++) { bon[in.nextInt()] = in.nextInt(); } long[][] dp = new long[n + 1][n + 1]; long ans = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= n; j++) { if (j == 0){ dp[i][j] = ans; } else if (j > i) break; else{ dp[i][j] = dp[i - 1][j - 1] + sc[i - 1] + bon[j]; ans = Math.max(ans, dp[i][j]); } } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc261_d/Java/37098242
condefects-java_data_60
import java.io.*; import java.math.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.Map.Entry; import java.util.regex.Pattern; import java.util.stream.Collectors; class Main implements Runnable { public static void solve () { int n = nextInt(), m = nextInt(); int[] x = new int[n+1]; for (int i=1; i<=n; i++) x[i] = nextInt(); int[] b = new int[n+1]; for (int i=1; i<=n; i++) { b[nextInt()] = nextInt(); } //x[i] i回目のコイントスで表が出た場合、貰える金額 //b[i] カウンタの数値がiになったときに貰えるボーナス金額 //dp[i][j] i回目のコイントスが終わってカウンタがjであるときの最大値 long[][] dp = new long[n+1][n+1]; for (int i=0; i<n+1; i++) Arrays.fill(dp[i], -1); dp[0][0] = 0; for (int i=1; i<=n; i++) { //表が出てカウンタを増やす場合 //カウンタの値はi以上にはならないので for (int j=1; j<=i; j++) { //x[i] 表が出た場合に貰える金額を足す //b[j] 今の数字がボーナス対象であれば、それも足す dp[i][j] = dp[i-1][j-1] + x[i] + b[j]; } //裏が出てカウンタを0にする場合 dp[i][0] = 0; for (int j=0; j<i; j++) { dp[i][0] = Math.max(dp[i][0], dp[i-1][j]); } } long ans = 0; for (int i=0; i<n+1; i++) { ans = Math.max(ans, dp[n][i]); } println(ans); } ///////////////////////////////////////////////////////////////////////////////////////////////// // useful methods, useful fields, useful static inner class ///////////////////////////////////////////////////////////////////////////////////////////////// public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; public static class Edge { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } int getCost() {return this.cost;} } ///////////////////////////////////////////////////////////////////////////////////////////////// // input ///////////////////////////////////////////////////////////////////////////////////////////////// public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 32768); public static StringTokenizer tokenizer = null; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public static String[] nextArray(int n) { String[] a = new String[n]; for (int i=0; i<n; i++) a[i] = next(); return a; } public static int nextInt() {return Integer.parseInt(next());}; public static int[] nextIntArray(int n) { int[] a = new int[n]; for (int i=0; i<n; i++) a[i] = nextInt(); return a; } public static int[][] nextIntTable(int n, int m) { int[][] a = new int[n][m]; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) a[i][j] = nextInt(); } return a; } public static long nextLong() {return Long.parseLong(next());} public static long[] nextLongArray(int n) { long[] a = new long[n]; for (int i=0; i<n; i++) a[i] = nextLong(); return a; } public static double nextDouble() {return Double.parseDouble(next());} public static char nextChar() {return next().charAt(0);} public static char[] nextCharArray() {return next().toCharArray();} public static char[][] nextCharTable(int n, int m) { char[][] a = new char[n][m]; for (int i=0; i<n; i++) { a[i] = next().toCharArray(); } return a; } ///////////////////////////////////////////////////////////////////////////////////////////////// // output ///////////////////////////////////////////////////////////////////////////////////////////////// static PrintWriter out = new PrintWriter(System.out); public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]==-infl? "# " : a[i][j]+" "); } println(""); } } ///////////////////////////////////////////////////////////////////////////////////////////////// // main method ///////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } } import java.io.*; import java.math.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.Map.Entry; import java.util.regex.Pattern; import java.util.stream.Collectors; class Main implements Runnable { public static void solve () { int n = nextInt(), m = nextInt(); int[] x = new int[n+1]; for (int i=1; i<=n; i++) x[i] = nextInt(); int[] b = new int[n+1]; for (int i=1; i<=m; i++) { b[nextInt()] = nextInt(); } //x[i] i回目のコイントスで表が出た場合、貰える金額 //b[i] カウンタの数値がiになったときに貰えるボーナス金額 //dp[i][j] i回目のコイントスが終わってカウンタがjであるときの最大値 long[][] dp = new long[n+1][n+1]; for (int i=0; i<n+1; i++) Arrays.fill(dp[i], -1); dp[0][0] = 0; for (int i=1; i<=n; i++) { //表が出てカウンタを増やす場合 //カウンタの値はi以上にはならないので for (int j=1; j<=i; j++) { //x[i] 表が出た場合に貰える金額を足す //b[j] 今の数字がボーナス対象であれば、それも足す dp[i][j] = dp[i-1][j-1] + x[i] + b[j]; } //裏が出てカウンタを0にする場合 dp[i][0] = 0; for (int j=0; j<i; j++) { dp[i][0] = Math.max(dp[i][0], dp[i-1][j]); } } long ans = 0; for (int i=0; i<n+1; i++) { ans = Math.max(ans, dp[n][i]); } println(ans); } ///////////////////////////////////////////////////////////////////////////////////////////////// // useful methods, useful fields, useful static inner class ///////////////////////////////////////////////////////////////////////////////////////////////// public static final int infi = (int)1e9; public static final long infl = (long)1e18; public static final int modi = (int)1e9 + 7; public static final long modl = (long)1e18 + 7; public static int[] dy = {-1, 0, 1, 0}; public static int[] dx = {0, 1, 0, -1}; // public static int[] dy = {-1, 0, -1, 1, 0, 1}; // public static int[] dx = {-1, -1, 0, 0, 1, 1}; // public static int[] dy = {-1, -1, -1, 0, 1, 1, 1, 0}; // public static int[] dx = {-1, 0, 1, 1, 1, 0, -1, -1}; public static class Edge { int id, from, to, cost; Edge(int to, int cost) { //基本コレ this.to = to; this.cost = cost; } Edge(int from, int to, int cost) { this.from = from; this.to = to; this.cost = cost; } Edge(int id, int from, int to, int cost) { this.id = id; this.from = from; this.to = to; this.cost = cost; } int getCost() {return this.cost;} } ///////////////////////////////////////////////////////////////////////////////////////////////// // input ///////////////////////////////////////////////////////////////////////////////////////////////// public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 32768); public static StringTokenizer tokenizer = null; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public static String[] nextArray(int n) { String[] a = new String[n]; for (int i=0; i<n; i++) a[i] = next(); return a; } public static int nextInt() {return Integer.parseInt(next());}; public static int[] nextIntArray(int n) { int[] a = new int[n]; for (int i=0; i<n; i++) a[i] = nextInt(); return a; } public static int[][] nextIntTable(int n, int m) { int[][] a = new int[n][m]; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) a[i][j] = nextInt(); } return a; } public static long nextLong() {return Long.parseLong(next());} public static long[] nextLongArray(int n) { long[] a = new long[n]; for (int i=0; i<n; i++) a[i] = nextLong(); return a; } public static double nextDouble() {return Double.parseDouble(next());} public static char nextChar() {return next().charAt(0);} public static char[] nextCharArray() {return next().toCharArray();} public static char[][] nextCharTable(int n, int m) { char[][] a = new char[n][m]; for (int i=0; i<n; i++) { a[i] = next().toCharArray(); } return a; } ///////////////////////////////////////////////////////////////////////////////////////////////// // output ///////////////////////////////////////////////////////////////////////////////////////////////// static PrintWriter out = new PrintWriter(System.out); public static void print(Object o) {out.print(o);} public static void println(Object o) {out.println(o);} public static void printStringArray(String[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printIntArray(int[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printLongArray(long[] a) { for (int i=0; i<a.length; i++) { if (i != 0) print(" "); print(a[i]); } println(""); } public static void printBooleanArray (boolean[] a) { for (int i=0; i<a.length; i++) { char c = a[i]==true? 'o' : 'x'; print(c); } println(""); } public static void printCharTable(char[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]); } println(""); } } public static void printIntTable(int[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { if (j != 0) print(" "); print(a[i][j]); } println(""); } } public static void printBooleanTable(boolean[][] b) { for (int i=0; i<b.length; i++) { for (int j=0; j<b[0].length; j++) { print(b[i][j]? "o" : "x"); } println(""); } } public static void printLongTable(long[][] a) { for (int i=0; i<a.length; i++) { for (int j=0; j<a[0].length; j++) { print(a[i][j]==-infl? "# " : a[i][j]+" "); } println(""); } } ///////////////////////////////////////////////////////////////////////////////////////////////// // main method ///////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new Thread(null, new Main(), "", 64 * 1024 * 1024).start(); } public void run() { solve(); out.close(); } }
ConDefects/ConDefects/Code/abc261_d/Java/39510892
condefects-java_data_61
import java.util.* ; public class Main{ public static void main(String[] args){ Scanner sc= new Scanner(System.in); int N= sc.nextInt(); int ans[]= new int[N+1]; Arrays.fill(ans,0); int root=0; for(int i=1;i<=N;i++){ int ps= sc.nextInt(); if(ps== -1){ root= i; System.out.print(root+" "); } else{ ans[ps]= i; } } int i=1; while(i<=N){ System.out.print(ans[root]+" "); root= ans[root]; i++ ; } System.exit(0); } } import java.util.* ; public class Main{ public static void main(String[] args){ Scanner sc= new Scanner(System.in); int N= sc.nextInt(); int ans[]= new int[N+1]; Arrays.fill(ans,0); int root=0; for(int i=1;i<=N;i++){ int ps= sc.nextInt(); if(ps== -1){ root= i; System.out.print(root+" "); } else{ ans[ps]= i; } } int i=1; while(i<N){ System.out.print(ans[root]+" "); root= ans[root]; i++ ; } System.exit(0); } }
ConDefects/ConDefects/Code/abc337_c/Java/49630141
condefects-java_data_62
//ここからimport文 import java.util.*; import java.io.*; //import mylib.* ; //ここまでimport文 public class Main{ static final int inf = Integer.MAX_VALUE / 2 ; static final int minf = inf * -1 ; static final String LF = "\n" ; // static final int mod = 1000000007 ; static final int mod = 998244353 ; static ArrayList<ArrayList<Integer>> G ; static HashMap<Long,Long> map ; public static void main(String[] args) { Scanner sc = new Scanner( System.in ) ; PrintStream ps = new PrintStream( System.out ) ; StringBuilder sb = new StringBuilder() ; long X = sc.nextLong() ; map = new HashMap<Long,Long>() ; for ( long i = 1L ; i <= 5L ; i++ ) map.put( i , i ) ; ps.println( func( X ) ) ; sc.close() ; ps.close() ; } public static long func( long x ) { if ( map.containsKey( x ) ) return map.get( x ) ; long res = ( ( func( floor( x ) ) % mod ) * ( func( ceil( x ) ) % mod ) ) % mod ; map.put( x , res ) ; return res ; } public static long floor( long x ) { return x / 2L ; } public static long ceil( long x ) { return ( x + 1L ) / 2L ; } } //ここからimport文 import java.util.*; import java.io.*; //import mylib.* ; //ここまでimport文 public class Main{ static final int inf = Integer.MAX_VALUE / 2 ; static final int minf = inf * -1 ; static final String LF = "\n" ; // static final int mod = 1000000007 ; static final int mod = 998244353 ; static ArrayList<ArrayList<Integer>> G ; static HashMap<Long,Long> map ; public static void main(String[] args) { Scanner sc = new Scanner( System.in ) ; PrintStream ps = new PrintStream( System.out ) ; StringBuilder sb = new StringBuilder() ; long X = sc.nextLong() ; map = new HashMap<Long,Long>() ; for ( long i = 1L ; i < 5L ; i++ ) map.put( i , i ) ; ps.println( func( X ) ) ; sc.close() ; ps.close() ; } public static long func( long x ) { if ( map.containsKey( x ) ) return map.get( x ) ; long res = ( ( func( floor( x ) ) % mod ) * ( func( ceil( x ) ) % mod ) ) % mod ; map.put( x , res ) ; return res ; } public static long floor( long x ) { return x / 2L ; } public static long ceil( long x ) { return ( x + 1L ) / 2L ; } }
ConDefects/ConDefects/Code/arc135_a/Java/29653861
condefects-java_data_63
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int N = scanner.nextInt(); boolean[] array = new boolean[N + 1]; for (int i = 1; i < N + 1; i++) { int current = scanner.nextInt(); if (!array[i]) { array[current] = true; } } List<Integer> result = new ArrayList<Integer>(); for (int i = 1; i < N + 1; i++) { if (!array[i]) result.add(i); } System.out.println(result.size()); System.out.println(result.toString()); } } } import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { int N = scanner.nextInt(); boolean[] array = new boolean[N + 1]; for (int i = 1; i < N + 1; i++) { int current = scanner.nextInt(); if (!array[i]) { array[current] = true; } } List<Integer> result = new ArrayList<Integer>(); for (int i = 1; i < N + 1; i++) { if (!array[i]) result.add(i); } System.out.println(result.size()); result.forEach(item -> System.out.print(item + " ")); } } }
ConDefects/ConDefects/Code/abc293_b/Java/41474768
condefects-java_data_64
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n =scanner.nextInt(); int max=0; for(int i=0;i<n;i++){ int h=scanner.nextInt(); if(max<h){ max=h; } } System.out.println(max); } } import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n =scanner.nextInt(); int max=0; for(int i=0;i<n;i++){ int h=scanner.nextInt(); if(max<h){ max=h; } else break; } System.out.println(max); } }
ConDefects/ConDefects/Code/abc235_b/Java/41602961
condefects-java_data_65
import java.io.*; import java.util.*; class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int correntHeight = 0; for (int i = 0; i < n; i++){ int height = sc.nextInt(); if (correntHeight < height){ correntHeight = height; } } System.out.println(correntHeight); } } import java.io.*; import java.util.*; class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int correntHeight = 0; for (int i = 0; i < n; i++){ int height = sc.nextInt(); if (correntHeight < height){ correntHeight = height; }else{ break; } } System.out.println(correntHeight); } }
ConDefects/ConDefects/Code/abc235_b/Java/41629306
condefects-java_data_66
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); int[] H = new int[N + 1]; for (int i = 1; i <= N; i++) { H[i] = sc.nextInt(); } int now_idx = 1; int max_height=0; int next_idx=0; while (now_idx!=N) { next_idx = now_idx + 1; if (H[now_idx] >= H[next_idx]) { break; } else { max_height = H[next_idx]; } now_idx = next_idx; } System.out.println(max_height); } } import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); int[] H = new int[N + 1]; for (int i = 1; i <= N; i++) { H[i] = sc.nextInt(); } int now_idx = 1; int max_height=H[1]; int next_idx=0; while (now_idx!=N) { next_idx = now_idx + 1; if (H[now_idx] >= H[next_idx]) { break; } else { max_height = H[next_idx]; } now_idx = next_idx; } System.out.println(max_height); } }
ConDefects/ConDefects/Code/abc235_b/Java/33554690
condefects-java_data_67
import java.util.*; import java.io.*; class Main{ public static void main(String[] args) { FastScanner str=new FastScanner(System.in); int n=str.nextInt(); int[] h=new int[n]; for(int i=0;i<n;i++)h[i]=str.nextInt(); int poshe=h[0]; for(int i=1;i<n;i++){ if(poshe<=h[i]){ poshe=h[i]; }else { break; } } System.out.println(poshe); } } class FastScanner implements Closeable { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(InputStream in) { this.in = in; } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} public void close() { try { in.close(); } catch (IOException e) { } } } import java.util.*; import java.io.*; class Main{ public static void main(String[] args) { FastScanner str=new FastScanner(System.in); int n=str.nextInt(); int[] h=new int[n]; for(int i=0;i<n;i++)h[i]=str.nextInt(); int poshe=h[0]; for(int i=1;i<n;i++){ if(poshe<h[i]){ poshe=h[i]; }else { break; } } System.out.println(poshe); } } class FastScanner implements Closeable { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(InputStream in) { this.in = in; } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} public void close() { try { in.close(); } catch (IOException e) { } } }
ConDefects/ConDefects/Code/abc235_b/Java/36335376
condefects-java_data_68
import java.util.*; public class Main { public static void main(String[] args){ //入力の読み込み Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] H = new int[N]; for(int i=0;i<N;i++){ H[i]=sc.nextInt(); } //計算 for(int i=0;i<N;i++){ if(i==(N-1)){ System.out.println(H[i]); return; }else{ if(H[i]>H[i+1]){ System.out.println(H[i]); return; } } } //回答出力 System.out.println(); } } import java.util.*; public class Main { public static void main(String[] args){ //入力の読み込み Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] H = new int[N]; for(int i=0;i<N;i++){ H[i]=sc.nextInt(); } //計算 for(int i=0;i<N;i++){ if(i==(N-1)){ System.out.println(H[i]); return; }else{ if(H[i]>=H[i+1]){ System.out.println(H[i]); return; } } } //回答出力 System.out.println(); } }
ConDefects/ConDefects/Code/abc235_b/Java/31778158
condefects-java_data_69
import java.io.*; import java.math.BigInteger; import java.util.*; class Main{ static int arr[]; public static void main(String[] args) { ac in=new ac(); int n=in.nextInt(); arr=new int[n+1]; for(int i=0;i<3*n;i++){ int tmp=in.nextInt(); arr[tmp]++; if(arr[tmp]==1)in.print(tmp+" "); } in.flush(); } } class ac extends PrintWriter { BufferedReader br; StringTokenizer st; ac() { this(System.in, System.out); } ac(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } 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()); } } import java.io.*; import java.math.BigInteger; import java.util.*; class Main{ static int arr[]; public static void main(String[] args) { ac in=new ac(); int n=in.nextInt(); arr=new int[n+1]; for(int i=0;i<3*n;i++){ int tmp=in.nextInt(); arr[tmp]++; if(arr[tmp]==2)in.print(tmp+" "); } in.flush(); } } class ac extends PrintWriter { BufferedReader br; StringTokenizer st; ac() { this(System.in, System.out); } ac(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } 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()); } }
ConDefects/ConDefects/Code/abc306_c/Java/43479115
condefects-java_data_70
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long t = scan.nextLong(); if(t == 0) { long ft = 3; long ftt = ((ft * ft) + (ft * 2) + 3) + ((ft * ft) + (ft * 2) + 3); long fttt = (ftt * ftt) + (2 * ftt) + 3;; System.out.println(fttt); } else { long ft = (t * t) + (2 * t) + 3; long ftt = ((ft * ft) + (2 * ft) + t) + ((ft + t)*(ft + t) + (2*(ft + t)) + t); long fttt = (ftt * ftt) + (2 * ftt) + 3; System.out.println(fttt); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long t = scan.nextLong(); if(t == 0) { long ft = 3; long ftt = ((ft * ft) + (ft * 2) + 3) + ((ft * ft) + (ft * 2) + 3); long fttt = (ftt * ftt) + (2 * ftt) + 3;; System.out.println(fttt); } else { long ft = (t * t) + (2 * t) + 3; long ftt = ((ft * ft) + (2 * ft) + 3) + ((ft + t)*(ft + t) + (2*(ft + t)) + 3); long fttt = (ftt * ftt) + (2 * ftt) + 3; System.out.println(fttt); } } }
ConDefects/ConDefects/Code/abc234_a/Java/35820365
condefects-java_data_71
import java.util.Scanner; public class Main { public static int F(int X) { return X^2+2*X+3; } public static void main(String[] arg){ Scanner scanner =new Scanner(System.in); int T = scanner.nextInt(); int H = F(F(F(T)+T)+F(F(T))); System.out.println(H); } } import java.util.Scanner; public class Main { public static int F(int X) { return X*X+2*X+3; } public static void main(String[] arg){ Scanner scanner =new Scanner(System.in); int T = scanner.nextInt(); int H = F(F(F(T)+T)+F(F(T))); System.out.println(H); } }
ConDefects/ConDefects/Code/abc234_a/Java/39144673
condefects-java_data_72
import java.io.*; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static final int MM = 5005, MOD = 998244353; static long[] freq = new long[26], dp[] = new long[26][MM], fact = new long[MM], inv = new long[MM]; static long ans; static long pow(long n, int t) { if (t == 0) return 1; long res = pow(n, t/2); res = res * res % MOD; if (t % 2 == 1) res = res * n % MOD; return res % MOD; } static long c(int n, int k) { if (n < k || n < 0 || k < 0) return 0; return fact[n] * inv[k] % MOD * inv[n-k] % MOD; } public static void main(String[] args) throws IOException { String s = readLine(); for (char c: s.toCharArray()) { freq[c - 'a'] ++; } fact[0] = 1; inv[0] = 1; for (int i=1; i<MM; i++) { fact[i] = i * fact[i-1] % MOD; inv[i] = pow(fact[i], MOD-2) % MOD; if (i <= freq[0]) dp[0][i] = 1; } dp[0][0] = 1; for (int i=1; i<26; i++) { for (int j=0; j<=s.length(); j++) { for (int k=0; k<=Math.min(j, freq[i]); k++) { dp[i][j] += dp[i-1][j-k] * c(j-k+1, k); dp[i][j] %= MOD; } } } for (int j=1; j<=s.length(); j++) { ans = (ans + dp[26-1][j]) % MOD; } System.out.println(ans % MOD); } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static String readLine () throws IOException { return br.readLine().trim(); } static int readInt () throws IOException { return Integer.parseInt(next()); } } import java.io.*; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static final int MM = 5005, MOD = 998244353; static long[] freq = new long[26], dp[] = new long[26][MM], fact = new long[MM], inv = new long[MM]; static long ans; static long pow(long n, int t) { if (t == 0) return 1; long res = pow(n, t/2); res = res * res % MOD; if (t % 2 == 1) res = res * n % MOD; return res % MOD; } static long c(int n, int k) { if (n < k || n < 0 || k < 0) return 0; return fact[n] * inv[k] % MOD * inv[n-k] % MOD; } public static void main(String[] args) throws IOException { String s = readLine(); for (char c: s.toCharArray()) { freq[c - 'a'] ++; } fact[0] = 1; inv[0] = 1; for (int i=1; i<MM; i++) { fact[i] = i * fact[i-1] % MOD; inv[i] = pow(fact[i], MOD-2) % MOD; if (i <= freq[0]) dp[0][i] = 1; } dp[0][0] = 1; for (int i=1; i<26; i++) { for (int j=0; j<=s.length(); j++) { for (int k=0; k<=Math.min(j, freq[i]); k++) { dp[i][j] += dp[i-1][j-k] * c(j, k); dp[i][j] %= MOD; } } } for (int j=1; j<=s.length(); j++) { ans = (ans + dp[26-1][j]) % MOD; } System.out.println(ans % MOD); } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static String readLine () throws IOException { return br.readLine().trim(); } static int readInt () throws IOException { return Integer.parseInt(next()); } }
ConDefects/ConDefects/Code/abc234_f/Java/28458577
condefects-java_data_73
import java.io.*; import java.util.*; public class Main { static MyReader in = new MyReader(); static MyWriter out = new MyWriter(); static final int MOD = 998_244_353; public static void main(String[] args) { char[] S = new char[5000]; int N = in.read(S); var cnt = new Counter<Character>(); for (int i = 0; i < N; i++) { cnt.incr(S[i]); } var c = new Combination(N, MOD); long[] dp = new long[N + 1]; dp[0] = 1; long[] dp2 = new long[N + 1]; for (int v : cnt.values()) { Arrays.fill(dp2, 0); for (int k = 1; k <= v; k++) { for (int n = k; n <= N; n++) { dp2[n] = (dp2[n] + dp[n - k] * c.comb(n, k)) % MOD; } } for (int i = 0; i <= N; i++) { dp[i] += dp2[i]; } } long ans = 0; for (int i = 1; i <= N; i++) { ans = (ans + dp[i]) % MOD; } out.println(ans); out.flush(); } } class Combination { final int MOD; long[] fact; long[] factInv; long[] inv; Combination(int n, int mod) { MOD = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i % MOD; } factInv = new long[n + 1]; factInv[n] = inv((int)fact[n]); for (int i = n - 1; i >= 0; i--) { factInv[i] = factInv[i + 1] * (i + 1) % MOD; } inv = new long[n + 1]; for (int i = 1; i <= n; i++) { inv[i] = factInv[i] * fact[i - 1] % MOD; } } int inv(int a) { int b = MOD; int s = 1; int t = 0; while (0 < b) { int q = a / b; int r = a - b * q; a = b; b = r; r = s; s = t; t = r - t * q; } return s + (-s + MOD + 1) / MOD * MOD; } long comb(int n, int r) { return n < r || r < 0 ? 0 : fact[n] * factInv[r] % MOD * factInv[n - r] % MOD; } } class Counter<K> extends HashMap<K, Integer> { @Override public Integer get(Object key) { return super.getOrDefault(key, 0); } void plus(K key, int n) { put(key, get(key) + n); } void incr(K key) { plus(key, 1); } void decr(K key) { plus(key, -1); } } class MyReader { byte[] buf = new byte[1 << 16]; int head = 0; int tail = 0; InputStream in = System.in; byte read() { if (head == tail) { try { tail = in.read(buf); } catch (IOException e) { e.printStackTrace(); } head = 0; } return buf[head++]; } char c() { char c = (char) read(); read(); return c; } int i() { int i = read() - '0'; boolean negative = i == -3; int n = negative ? 0 : i; while (0 <= (i = read() - '0') && i <= 9) { n = 10 * n + i; } return negative ? -n : n; } void read(int[] a, int from, int to) { for (int i = from; i < to; i++) { a[i] = i(); } } void read(int[] a) { read(a, 0, a.length); } int[] ii(int length, int from, int to) { int[] a = new int[length]; read(a, from, to); return a; } int[] ii(int length) { return ii(length, 0, length); } int[][] ii2(int row, int col) { int[][] a = new int[row][col]; for (int i = 0; i < row; i++) { read(a[i]); } return a; } long l() { int i = read() - '0'; boolean negative = i == -3; long n = negative ? 0 : i; while (0 <= (i = read() - '0') && i <= 9) { n = 10 * n + i; } return negative ? -n : n; } void read(long[] a, int from, int to) { for (int i = from; i < to; i++) { a[i] = l(); } } void read(long[] a) { read(a, 0, a.length); } long[] ll(int length, int from, int to) { long[] a = new long[length]; read(a, from, to); return a; } long[] ll(int length) { return ll(length, 0, length); } int read(char[] buf, int off) { int i; char c; for (i = off; (c = (char) read()) != ' ' && c != '\n'; i++) { buf[i] = c; } return i; } int read(char[] buf) { return read(buf, 0); } char[] s(int length) { char[] s = new char[length]; for (int i = 0; i < length; i++) { s[i] = (char) read(); } read(); return s; } char[][] ss(int row, int col) { char[][] s = new char[row][col]; for (int i = 0; i < row; i++) { read(s[i]); } return s; } } class MyWriter { OutputStream out = System.out; byte[] buf = new byte[1 << 16]; byte[] ibuf = new byte[20]; int tail = 0; void flush() { try { out.write(buf, 0, tail); tail = 0; } catch (IOException e) { e.printStackTrace(); } } void write(byte b) { buf[tail++] = b; if (tail == buf.length) { flush(); } } void write(byte[] b, int off, int len) { for (int i = off; i < off + len; i++) { write(b[i]); } } void space() { write((byte) ' '); } void println() { write((byte) '\n'); } void print(char c) { write((byte) c); } void println(char c) { print(c); println(); } void printsp(char c) { print(c); space(); } void print(int n) { if (n < 0) { n = -n; write((byte) '-'); } int i = ibuf.length; do { ibuf[--i] = (byte) (n % 10 + '0'); n /= 10; } while (n > 0); write(ibuf, i, ibuf.length - i); } void println(int n) { print(n); println(); } void printsp(int n) { print(n); space(); } void print(long n) { if (n < 0) { n = -n; write((byte) '-'); } int i = ibuf.length; do { ibuf[--i] = (byte) (n % 10 + '0'); n /= 10; } while (n > 0); write(ibuf, i, ibuf.length - i); } void println(long n) { print(n); println(); } void printsp(long n) { print(n); space(); } void print(String s) { byte[] b = s.getBytes(); write(b, 0, b.length); } void println(String s) { print(s); println(); } void printsp(String s) { print(s); space(); } void print(int[] a, int from, int to, char sep) { for (int i = from; i < to; i++) { print(a[i]); print(sep); } } void print(int[] a, char sep) { print(a, 0, a.length, sep); } void print(int[] a, int from, int to) { print(a, from, to, ' '); } void print(int[] a) { print(a, ' '); } void println(int[] a) { print(a); println(); } void println(int[] a, char sep) { print(a, sep); println(); } void println(int[] a, int from, int to) { print(a, from, to); println(); } void print(char[] s, int from, int to) { for (int i = from; i < to && s[i] != '\0'; i++) { print(s[i]); } } void print(char[] s) { print(s, 0, s.length); } void println(char[] s, int from, int to) { print(s, from, to); println(); } void println(char[] s) { println(s, 0, s.length); } void print(Object o) { print(o.toString()); } void println(Object o) { print(o); } } import java.io.*; import java.util.*; public class Main { static MyReader in = new MyReader(); static MyWriter out = new MyWriter(); static final int MOD = 998_244_353; public static void main(String[] args) { char[] S = new char[5000]; int N = in.read(S); var cnt = new Counter<Character>(); for (int i = 0; i < N; i++) { cnt.incr(S[i]); } var c = new Combination(N, MOD); long[] dp = new long[N + 1]; dp[0] = 1; long[] dp2 = new long[N + 1]; for (int v : cnt.values()) { Arrays.fill(dp2, 0); for (int k = 1; k <= v; k++) { for (int n = k; n <= N; n++) { dp2[n] = (dp2[n] + dp[n - k] * c.comb(n, k)) % MOD; } } for (int i = 0; i <= N; i++) { dp[i] = (dp[i] + dp2[i]) % MOD; } } long ans = 0; for (int i = 1; i <= N; i++) { ans = (ans + dp[i]) % MOD; } out.println(ans); out.flush(); } } class Combination { final int MOD; long[] fact; long[] factInv; long[] inv; Combination(int n, int mod) { MOD = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i % MOD; } factInv = new long[n + 1]; factInv[n] = inv((int)fact[n]); for (int i = n - 1; i >= 0; i--) { factInv[i] = factInv[i + 1] * (i + 1) % MOD; } inv = new long[n + 1]; for (int i = 1; i <= n; i++) { inv[i] = factInv[i] * fact[i - 1] % MOD; } } int inv(int a) { int b = MOD; int s = 1; int t = 0; while (0 < b) { int q = a / b; int r = a - b * q; a = b; b = r; r = s; s = t; t = r - t * q; } return s + (-s + MOD + 1) / MOD * MOD; } long comb(int n, int r) { return n < r || r < 0 ? 0 : fact[n] * factInv[r] % MOD * factInv[n - r] % MOD; } } class Counter<K> extends HashMap<K, Integer> { @Override public Integer get(Object key) { return super.getOrDefault(key, 0); } void plus(K key, int n) { put(key, get(key) + n); } void incr(K key) { plus(key, 1); } void decr(K key) { plus(key, -1); } } class MyReader { byte[] buf = new byte[1 << 16]; int head = 0; int tail = 0; InputStream in = System.in; byte read() { if (head == tail) { try { tail = in.read(buf); } catch (IOException e) { e.printStackTrace(); } head = 0; } return buf[head++]; } char c() { char c = (char) read(); read(); return c; } int i() { int i = read() - '0'; boolean negative = i == -3; int n = negative ? 0 : i; while (0 <= (i = read() - '0') && i <= 9) { n = 10 * n + i; } return negative ? -n : n; } void read(int[] a, int from, int to) { for (int i = from; i < to; i++) { a[i] = i(); } } void read(int[] a) { read(a, 0, a.length); } int[] ii(int length, int from, int to) { int[] a = new int[length]; read(a, from, to); return a; } int[] ii(int length) { return ii(length, 0, length); } int[][] ii2(int row, int col) { int[][] a = new int[row][col]; for (int i = 0; i < row; i++) { read(a[i]); } return a; } long l() { int i = read() - '0'; boolean negative = i == -3; long n = negative ? 0 : i; while (0 <= (i = read() - '0') && i <= 9) { n = 10 * n + i; } return negative ? -n : n; } void read(long[] a, int from, int to) { for (int i = from; i < to; i++) { a[i] = l(); } } void read(long[] a) { read(a, 0, a.length); } long[] ll(int length, int from, int to) { long[] a = new long[length]; read(a, from, to); return a; } long[] ll(int length) { return ll(length, 0, length); } int read(char[] buf, int off) { int i; char c; for (i = off; (c = (char) read()) != ' ' && c != '\n'; i++) { buf[i] = c; } return i; } int read(char[] buf) { return read(buf, 0); } char[] s(int length) { char[] s = new char[length]; for (int i = 0; i < length; i++) { s[i] = (char) read(); } read(); return s; } char[][] ss(int row, int col) { char[][] s = new char[row][col]; for (int i = 0; i < row; i++) { read(s[i]); } return s; } } class MyWriter { OutputStream out = System.out; byte[] buf = new byte[1 << 16]; byte[] ibuf = new byte[20]; int tail = 0; void flush() { try { out.write(buf, 0, tail); tail = 0; } catch (IOException e) { e.printStackTrace(); } } void write(byte b) { buf[tail++] = b; if (tail == buf.length) { flush(); } } void write(byte[] b, int off, int len) { for (int i = off; i < off + len; i++) { write(b[i]); } } void space() { write((byte) ' '); } void println() { write((byte) '\n'); } void print(char c) { write((byte) c); } void println(char c) { print(c); println(); } void printsp(char c) { print(c); space(); } void print(int n) { if (n < 0) { n = -n; write((byte) '-'); } int i = ibuf.length; do { ibuf[--i] = (byte) (n % 10 + '0'); n /= 10; } while (n > 0); write(ibuf, i, ibuf.length - i); } void println(int n) { print(n); println(); } void printsp(int n) { print(n); space(); } void print(long n) { if (n < 0) { n = -n; write((byte) '-'); } int i = ibuf.length; do { ibuf[--i] = (byte) (n % 10 + '0'); n /= 10; } while (n > 0); write(ibuf, i, ibuf.length - i); } void println(long n) { print(n); println(); } void printsp(long n) { print(n); space(); } void print(String s) { byte[] b = s.getBytes(); write(b, 0, b.length); } void println(String s) { print(s); println(); } void printsp(String s) { print(s); space(); } void print(int[] a, int from, int to, char sep) { for (int i = from; i < to; i++) { print(a[i]); print(sep); } } void print(int[] a, char sep) { print(a, 0, a.length, sep); } void print(int[] a, int from, int to) { print(a, from, to, ' '); } void print(int[] a) { print(a, ' '); } void println(int[] a) { print(a); println(); } void println(int[] a, char sep) { print(a, sep); println(); } void println(int[] a, int from, int to) { print(a, from, to); println(); } void print(char[] s, int from, int to) { for (int i = from; i < to && s[i] != '\0'; i++) { print(s[i]); } } void print(char[] s) { print(s, 0, s.length); } void println(char[] s, int from, int to) { print(s, from, to); println(); } void println(char[] s) { println(s, 0, s.length); } void print(Object o) { print(o.toString()); } void println(Object o) { print(o); } }
ConDefects/ConDefects/Code/abc234_f/Java/28424396
condefects-java_data_74
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args){ Main m = new Main(); m.run(); } long mod = 998244353L; public void run(){ char ch[] = new Scanner(System.in).next().toCharArray(); int rev[] = new int[26]; int n = ch.length; for(int i =0; i< ch.length; i++){ rev[ch[i] - 'a']++; } long ans[] = new long[n+1]; ans[0] = 1; for(int c = 0 ; c < 26; c++){ long next[] = new long[n+1]; for(int x = 0; x <= rev[c]; x++){ for(int i =0 ; i + x <= n ;i++){ next[i+x] += (ans[i] * choose(i+x,x)) % mod; next[i+x] = next[i+x] % mod; } } ans = next; // System.out.println(Arrays.toString(ans)); } long res = 0; for(int i =1 ;i <= n; i++){ res = res + ans[i]; res = res % mod; } // System.out.println(Arrays.toString(ans)); System.out.println(res); } long[][] map = new long[5001][5001]; long choose(int n,int r){ if(map[n][r] != 0){ return map[n][r]; } if(n == r) { return 1; } if(r == 0){ return 1; } long ans = choose(n, r-1) + choose(n-1, r); map[n][r] = ans % mod; return map[n][r]; } } import java.util.*; import java.io.*; public class Main{ public static void main(String[] args){ Main m = new Main(); m.run(); } long mod = 998244353L; public void run(){ char ch[] = new Scanner(System.in).next().toCharArray(); int rev[] = new int[26]; int n = ch.length; for(int i =0; i< ch.length; i++){ rev[ch[i] - 'a']++; } long ans[] = new long[n+1]; ans[0] = 1; for(int c = 0 ; c < 26; c++){ long next[] = new long[n+1]; for(int x = 0; x <= rev[c]; x++){ for(int i =0 ; i + x <= n ;i++){ next[i+x] += (ans[i] * choose(i+x,x)) % mod; next[i+x] = next[i+x] % mod; } } ans = next; // System.out.println(Arrays.toString(ans)); } long res = 0; for(int i =1 ;i <= n; i++){ res = res + ans[i]; res = res % mod; } // System.out.println(Arrays.toString(ans)); System.out.println(res); } long[][] map = new long[5001][5001]; long choose(int n,int r){ if(map[n][r] != 0){ return map[n][r]; } if(n == r) { return 1; } if(r == 0){ return 1; } long ans = choose(n-1, r-1) + choose(n-1, r); map[n][r] = ans % mod; return map[n][r]; } }
ConDefects/ConDefects/Code/abc234_f/Java/28440008
condefects-java_data_75
import java.io.*; import java.util.*; import static java.lang.Math.abs; public class Main { long pow_mod(long x, long i, long mod){ if(i == 0){ return 1; } long temp = pow_mod(x, i >> 1, mod); temp = temp * temp % mod; if((i & 1 )!= 0){ temp = temp * x % mod; } return temp; } long M = 998244353; boolean multiCase = false; long [] A = new long[5001]; long [] Ar = new long[5001]; private void preparation() { A[0] = 1; Ar[0] = 1; for(int i = 1; i <= 5000; i++){ A[i] = A[i-1] * i % M; Ar[i] = pow_mod(A[i], M-2, M); } } int [] cnt = new int[27]; long [][] dp = new long[26][5001]; long [] sufS; long dfs(int cur, int remain){ if(remain == 0){ return 1; } if(cur > 25 || remain < 0){ return 0; } if(dp[cur][remain] != -1){ return dp[cur][remain]; } // if(cur == 25){ // return Ar[min(cnt[cur], remain)]; // } long res = 0; for(int i = 0; i <= cnt[cur]; i++){ // print("cur = %d, i = %d%n", cur, i); res = res + (long)dfs(cur + 1, remain - i) * Ar[i] % M; // print("cur = %d, i = %d, res = %d %n", cur, i, res); } dp[cur][remain] = res; // print("cur = %d, r = %d res = %d%n", cur,remain, res); return res; } private void solve() throws Exception { rf(); char [] s = rs2c(); int n = s.length; for(char c : s){ cnt[c - 'a'] ++; } fill(dp, -1); sufS = suffix(cnt); long ans = 0; // addAns(n); for(int i = 1; i <= n; i++){ ans = add(ans, A[i] * dfs(0,i) % M); // addAns(ans); } addAns(ans); } private void clear() { } private void run() throws Exception { int T = 1; if (multiCase) { rf(); T = ri(); } preparation(); while (T-- > 0) { solve(); if (T != 0) { clear(); } } printAns(); } public static void main(String[] args) throws Exception { new Main().run(); } StringBuilder sb = new StringBuilder(); BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer strT; public boolean ADD_ANSWER_FUNCTION; private void addAns(int a) { sb.append(a).append("\n"); } private void addAns(long a) { sb.append(a).append("\n"); } private void addAns(String s) { sb.append(s).append("\n"); } private void addAns(int[] A, int x) { sb.append(A[0]); int t = min(A.length, x); for (int i = 1; i < t; i++) { sb.append(" ").append(A[i]); } sb.append("\n"); } private void addAns(long[] A, int x) { sb.append(A[0]); int t = min(A.length, x); for (int i = 1; i < t; i++) { sb.append(" ").append(A[i]); } sb.append("\n"); } private void addYes() { sb.append("Yes\n"); } private void addNo() { sb.append("No\n"); } private void rf() throws IOException { strT = new StringTokenizer(infile.readLine()); } private int ri() { return Integer.parseInt(strT.nextToken()); } private long rl() { return Long.parseLong(strT.nextToken()); } private char[] rs2c() { return strT.nextToken().toCharArray(); } private String rs() { return strT.nextToken(); } private int[] readArr(int N, int start_pos, int suffix_num, int suffix_value) { int[] arr = new int[N + start_pos + suffix_num]; for (int i = 0; i < N; i++) { arr[i + start_pos] = Integer.parseInt(strT.nextToken()); } for(int i = N + start_pos; i < arr.length; i++){ arr[i] = suffix_value; } return arr; } private long[] readArr2(int N, int start_pos, int suffix_num, long suffix_value) { long[] arr = new long[N + start_pos + suffix_num]; for (int i = 0; i < N; i++) { arr[i + start_pos] = Long.parseLong(strT.nextToken()); } for(int i = N + start_pos; i < arr.length; i++){ arr[i] = suffix_value; } return arr; } public boolean PRINT_FUNCTION; private void print(String format, Object... args) { System.out.printf((format), args); } // for interactive problem. flush output after print. need with tail %n if print Line Separator private void iprint(String format, Object... args) { System.out.printf((format), args); System.out.flush(); } private void print(int[] arr, int x) { //for debugging only for (int i = 0; i < min(arr.length, x); i++) { System.out.print(arr[i] + " "); } System.out.println(); } private void print(long[] arr, int x) { //for debugging only for (int i = 0; i < min(arr.length, x); i++) { System.out.print(arr[i] + " "); } System.out.println(); } private void printAns() { System.out.print(sb); } public boolean ADD_WITH_MOD_FUNCTION; private int add(int a, int b) { return (int) add((long) a, (long) b); } private int add(int a, int b, int c) { return (int) add((long) a, (long) b, (long) c); } private int add(int a, int b, int c, int d) { return (int) add((long) a, (long) b, (long) c, (long) d); } private long add(long a, long b) { return (a % M + b % M) % M; } private long add(long a, long b, long c) { return (a % M + add(b, c)) % M; } private long add(long a, long b, long c, long d) { return (a % M + add(b, c, d)) % M; } private long mod(long x) { return (x % M + M) % M; } private int mod(int x) { return (int) (((x % M) + M) % M); } public boolean ELEMENT_COUNT_TO_MAP_FUNCTION; private HashMap<Integer, Integer> hash(int[] A) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } private HashMap<Long, Integer> hash(long[] A) { HashMap<Long, Integer> map = new HashMap<Long, Integer>(); for (long i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } private TreeMap<Integer, Integer> tree(int[] A) { TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); for (int i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } private TreeMap<Long, Integer> tree(long[] A) { TreeMap<Long, Integer> map = new TreeMap<Long, Integer>(); for (long i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } public boolean SORT_FUNCTION; private int[] sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); return A; } private long[] sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); return A; } private String sort(String s) { char[] sc = s.toCharArray(); Arrays.sort(sc); return new String(sc); } private char[] sort(char[] cs) { Arrays.sort(cs); return cs; } private int[][] sort(int[][] A, Comparator<int[]> c) { Arrays.sort(A, c); return A; } public boolean REVERSE_FUNCTION; private String reverse(String s) { StringBuilder p = new StringBuilder(s); p.reverse(); return p.toString(); } private long sum(int[] A) { long sum = 0; for (int i : A) { sum += i; } return sum; } private long sum(long[] A) { long sum = 0; for (long i : A) { sum += i; } return sum; } private void reverse(long[] A) { int n = A.length; long[] B = new long[n]; for (int i = 0; i < n; i++) { B[i] = A[n - i - 1]; } System.arraycopy(B, 0, A, 0, n); } private void reverse(int[] A) { int n = A.length; int[] B = new int[n]; for (int i = 0; i < n; i++) { B[i] = A[n - i - 1]; } System.arraycopy(B, 0, A, 0, n); } private int nextPowerOf2(int n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } private int highestPowerOf2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } private long highestPowerOf2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public boolean ARRAY_MAX_MIN_FUNCTION; private int max(int[] A) { int max = Integer.MIN_VALUE; for (int j : A) { max = Math.max(max, j); } return max; } private int min(int[] A) { int min = Integer.MAX_VALUE; for (int j : A) { min = Math.min(min, j); } return min; } private long max(long[] A) { long max = Long.MIN_VALUE; for (long l : A) { max = Math.max(max, l); } return max; } private long min(long[] A) { long min = Long.MAX_VALUE; for (long l : A) { min = Math.min(min, l); } return min; } public boolean PREFIX_SUFFIX_FUNCTION; private long[] prefix(long[] A) { long[] p = new long[A.length]; p[0] = A[0]; for (int i = 1; i < A.length; i++) p[i] = p[i - 1] + A[i]; return p; } private long[] prefix(int[] A) { long[] p = new long[A.length]; p[0] = A[0]; for (int i = 1; i < A.length; i++) p[i] = p[i - 1] + A[i]; return p; } private long[] suffix(long[] A) { long[] p = new long[A.length]; p[A.length - 1] = A[A.length - 1]; for (int i = A.length - 2; i >= 0; i--) p[i] = p[i + 1] + A[i]; return p; } private long[] suffix(int[] A) { long[] p = new long[A.length]; p[A.length - 1] = A[A.length - 1]; for (int i = A.length - 2; i >= 0; i--) p[i] = p[i + 1] + A[i]; return p; } public boolean FILL_ARRAY_FUNCTION; private void fill(int[] dp, int v) { Arrays.fill(dp, v); } private void fill(int[][] dp, int v) { for (int[] ints : dp) { Arrays.fill(ints, v); } } private void fill(int[][][] dp, int v) { for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[0].length; j++) { Arrays.fill(dp[i][j], v); } } } private void fill(int[][][][] dp, int v) { for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[0].length; j++) { for (int k = 0; k < dp[0][0].length; k++) { Arrays.fill(dp[i][j][k], v); } } } } private void fill(long[] dp, long v) { Arrays.fill(dp, v); } private void fill(long[][] dp, long v) { for (long[] longs : dp) Arrays.fill(longs, v); } private void fill(long[][][] dp, long v) { for (long[][] ones : dp) { for (long[] two : ones) { Arrays.fill(two, v); } } } private void fill(long[][][][] dp, long v) { for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[0].length; j++) { for (int k = 0; k < dp[0][0].length; k++) { Arrays.fill(dp[i][j][k], v); } } } } public boolean MAX_AND_MIN_FUNCTION; // max and min for 2 ~ 4 arguments, argument Type int long float double private int min(int a, int b) { return Math.min(a, b); } private long min(long a, long b) { return Math.min(a, b); } private float min(float a, float b) { return Math.min(a, b); } private double min(double a, double b) { return Math.min(a, b); } private int min(int a, int b, int c) { return Math.min(a, Math.min(b, c)); } private float min(float a, float b, float c) { return Math.min(a, Math.min(b, c)); } private double min(double a, double b, double c) { return Math.min(a, Math.min(b, c)); } private long min(long a, long b, long c) { return Math.min(a, Math.min(b, c)); } private int min(int a, int b, int c, int d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private long min(long a, long b, long c, long d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private float min(float a, float b, float c, float d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private double min(double a, double b, double c, double d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private int max(int a, int b) { return Math.max(a, b); } private long max(long a, long b) { return Math.max(a, b); } private float max(float a, float b) { return Math.max(a, b); } private double max(double a, double b) { return Math.max(a, b); } private int max(int a, int b, int c) { return Math.max(a, Math.max(b, c)); } private float max(float a, float b, float c) { return Math.max(a, Math.max(b, c)); } private double max(double a, double b, double c) { return Math.max(a, Math.max(b, c)); } private long max(long a, long b, long c) { return Math.max(a, Math.max(b, c)); } private int max(int a, int b, int c, int d) { return Math.max(Math.max(a, b), Math.max(c, d)); } private long max(long a, long b, long c, long d) { return Math.max(Math.max(a, b), Math.max(c, d)); } private float max(float a, float b, float c, float d) { return Math.max(Math.max(a, b), Math.max(c, d)); } private double max(double a, double b, double c, double d) { return Math.max(Math.max(a, b), Math.max(c, d)); } } import java.io.*; import java.util.*; import static java.lang.Math.abs; public class Main { long pow_mod(long x, long i, long mod){ if(i == 0){ return 1; } long temp = pow_mod(x, i >> 1, mod); temp = temp * temp % mod; if((i & 1 )!= 0){ temp = temp * x % mod; } return temp; } long M = 998244353; boolean multiCase = false; long [] A = new long[5001]; long [] Ar = new long[5001]; private void preparation() { A[0] = 1; Ar[0] = 1; for(int i = 1; i <= 5000; i++){ A[i] = A[i-1] * i % M; Ar[i] = pow_mod(A[i], M-2, M); } } int [] cnt = new int[27]; long [][] dp = new long[26][5001]; long [] sufS; long dfs(int cur, int remain){ if(remain == 0){ return 1; } if(cur > 25 || remain < 0){ return 0; } if(dp[cur][remain] != -1){ return dp[cur][remain]; } // if(cur == 25){ // return Ar[min(cnt[cur], remain)]; // } long res = 0; for(int i = 0; i <= cnt[cur]; i++){ // print("cur = %d, i = %d%n", cur, i); res = (res + (long)dfs(cur + 1, remain - i) * Ar[i] % M) % M; // print("cur = %d, i = %d, res = %d %n", cur, i, res); } dp[cur][remain] = res; // print("cur = %d, r = %d res = %d%n", cur,remain, res); return res; } private void solve() throws Exception { rf(); char [] s = rs2c(); int n = s.length; for(char c : s){ cnt[c - 'a'] ++; } fill(dp, -1); sufS = suffix(cnt); long ans = 0; // addAns(n); for(int i = 1; i <= n; i++){ ans = add(ans, A[i] * dfs(0,i) % M); // addAns(ans); } addAns(ans); } private void clear() { } private void run() throws Exception { int T = 1; if (multiCase) { rf(); T = ri(); } preparation(); while (T-- > 0) { solve(); if (T != 0) { clear(); } } printAns(); } public static void main(String[] args) throws Exception { new Main().run(); } StringBuilder sb = new StringBuilder(); BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer strT; public boolean ADD_ANSWER_FUNCTION; private void addAns(int a) { sb.append(a).append("\n"); } private void addAns(long a) { sb.append(a).append("\n"); } private void addAns(String s) { sb.append(s).append("\n"); } private void addAns(int[] A, int x) { sb.append(A[0]); int t = min(A.length, x); for (int i = 1; i < t; i++) { sb.append(" ").append(A[i]); } sb.append("\n"); } private void addAns(long[] A, int x) { sb.append(A[0]); int t = min(A.length, x); for (int i = 1; i < t; i++) { sb.append(" ").append(A[i]); } sb.append("\n"); } private void addYes() { sb.append("Yes\n"); } private void addNo() { sb.append("No\n"); } private void rf() throws IOException { strT = new StringTokenizer(infile.readLine()); } private int ri() { return Integer.parseInt(strT.nextToken()); } private long rl() { return Long.parseLong(strT.nextToken()); } private char[] rs2c() { return strT.nextToken().toCharArray(); } private String rs() { return strT.nextToken(); } private int[] readArr(int N, int start_pos, int suffix_num, int suffix_value) { int[] arr = new int[N + start_pos + suffix_num]; for (int i = 0; i < N; i++) { arr[i + start_pos] = Integer.parseInt(strT.nextToken()); } for(int i = N + start_pos; i < arr.length; i++){ arr[i] = suffix_value; } return arr; } private long[] readArr2(int N, int start_pos, int suffix_num, long suffix_value) { long[] arr = new long[N + start_pos + suffix_num]; for (int i = 0; i < N; i++) { arr[i + start_pos] = Long.parseLong(strT.nextToken()); } for(int i = N + start_pos; i < arr.length; i++){ arr[i] = suffix_value; } return arr; } public boolean PRINT_FUNCTION; private void print(String format, Object... args) { System.out.printf((format), args); } // for interactive problem. flush output after print. need with tail %n if print Line Separator private void iprint(String format, Object... args) { System.out.printf((format), args); System.out.flush(); } private void print(int[] arr, int x) { //for debugging only for (int i = 0; i < min(arr.length, x); i++) { System.out.print(arr[i] + " "); } System.out.println(); } private void print(long[] arr, int x) { //for debugging only for (int i = 0; i < min(arr.length, x); i++) { System.out.print(arr[i] + " "); } System.out.println(); } private void printAns() { System.out.print(sb); } public boolean ADD_WITH_MOD_FUNCTION; private int add(int a, int b) { return (int) add((long) a, (long) b); } private int add(int a, int b, int c) { return (int) add((long) a, (long) b, (long) c); } private int add(int a, int b, int c, int d) { return (int) add((long) a, (long) b, (long) c, (long) d); } private long add(long a, long b) { return (a % M + b % M) % M; } private long add(long a, long b, long c) { return (a % M + add(b, c)) % M; } private long add(long a, long b, long c, long d) { return (a % M + add(b, c, d)) % M; } private long mod(long x) { return (x % M + M) % M; } private int mod(int x) { return (int) (((x % M) + M) % M); } public boolean ELEMENT_COUNT_TO_MAP_FUNCTION; private HashMap<Integer, Integer> hash(int[] A) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } private HashMap<Long, Integer> hash(long[] A) { HashMap<Long, Integer> map = new HashMap<Long, Integer>(); for (long i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } private TreeMap<Integer, Integer> tree(int[] A) { TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); for (int i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } private TreeMap<Long, Integer> tree(long[] A) { TreeMap<Long, Integer> map = new TreeMap<Long, Integer>(); for (long i : A) { if (map.containsKey(i)) { map.put(i, map.get(i) + 1); } else { map.put(i, 1); } } return map; } public boolean SORT_FUNCTION; private int[] sort(int[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); return A; } private long[] sort(long[] A) { int n = A.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n - i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); return A; } private String sort(String s) { char[] sc = s.toCharArray(); Arrays.sort(sc); return new String(sc); } private char[] sort(char[] cs) { Arrays.sort(cs); return cs; } private int[][] sort(int[][] A, Comparator<int[]> c) { Arrays.sort(A, c); return A; } public boolean REVERSE_FUNCTION; private String reverse(String s) { StringBuilder p = new StringBuilder(s); p.reverse(); return p.toString(); } private long sum(int[] A) { long sum = 0; for (int i : A) { sum += i; } return sum; } private long sum(long[] A) { long sum = 0; for (long i : A) { sum += i; } return sum; } private void reverse(long[] A) { int n = A.length; long[] B = new long[n]; for (int i = 0; i < n; i++) { B[i] = A[n - i - 1]; } System.arraycopy(B, 0, A, 0, n); } private void reverse(int[] A) { int n = A.length; int[] B = new int[n]; for (int i = 0; i < n; i++) { B[i] = A[n - i - 1]; } System.arraycopy(B, 0, A, 0, n); } private int nextPowerOf2(int n) { n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } private int highestPowerOf2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } private long highestPowerOf2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public boolean ARRAY_MAX_MIN_FUNCTION; private int max(int[] A) { int max = Integer.MIN_VALUE; for (int j : A) { max = Math.max(max, j); } return max; } private int min(int[] A) { int min = Integer.MAX_VALUE; for (int j : A) { min = Math.min(min, j); } return min; } private long max(long[] A) { long max = Long.MIN_VALUE; for (long l : A) { max = Math.max(max, l); } return max; } private long min(long[] A) { long min = Long.MAX_VALUE; for (long l : A) { min = Math.min(min, l); } return min; } public boolean PREFIX_SUFFIX_FUNCTION; private long[] prefix(long[] A) { long[] p = new long[A.length]; p[0] = A[0]; for (int i = 1; i < A.length; i++) p[i] = p[i - 1] + A[i]; return p; } private long[] prefix(int[] A) { long[] p = new long[A.length]; p[0] = A[0]; for (int i = 1; i < A.length; i++) p[i] = p[i - 1] + A[i]; return p; } private long[] suffix(long[] A) { long[] p = new long[A.length]; p[A.length - 1] = A[A.length - 1]; for (int i = A.length - 2; i >= 0; i--) p[i] = p[i + 1] + A[i]; return p; } private long[] suffix(int[] A) { long[] p = new long[A.length]; p[A.length - 1] = A[A.length - 1]; for (int i = A.length - 2; i >= 0; i--) p[i] = p[i + 1] + A[i]; return p; } public boolean FILL_ARRAY_FUNCTION; private void fill(int[] dp, int v) { Arrays.fill(dp, v); } private void fill(int[][] dp, int v) { for (int[] ints : dp) { Arrays.fill(ints, v); } } private void fill(int[][][] dp, int v) { for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[0].length; j++) { Arrays.fill(dp[i][j], v); } } } private void fill(int[][][][] dp, int v) { for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[0].length; j++) { for (int k = 0; k < dp[0][0].length; k++) { Arrays.fill(dp[i][j][k], v); } } } } private void fill(long[] dp, long v) { Arrays.fill(dp, v); } private void fill(long[][] dp, long v) { for (long[] longs : dp) Arrays.fill(longs, v); } private void fill(long[][][] dp, long v) { for (long[][] ones : dp) { for (long[] two : ones) { Arrays.fill(two, v); } } } private void fill(long[][][][] dp, long v) { for (int i = 0; i < dp.length; i++) { for (int j = 0; j < dp[0].length; j++) { for (int k = 0; k < dp[0][0].length; k++) { Arrays.fill(dp[i][j][k], v); } } } } public boolean MAX_AND_MIN_FUNCTION; // max and min for 2 ~ 4 arguments, argument Type int long float double private int min(int a, int b) { return Math.min(a, b); } private long min(long a, long b) { return Math.min(a, b); } private float min(float a, float b) { return Math.min(a, b); } private double min(double a, double b) { return Math.min(a, b); } private int min(int a, int b, int c) { return Math.min(a, Math.min(b, c)); } private float min(float a, float b, float c) { return Math.min(a, Math.min(b, c)); } private double min(double a, double b, double c) { return Math.min(a, Math.min(b, c)); } private long min(long a, long b, long c) { return Math.min(a, Math.min(b, c)); } private int min(int a, int b, int c, int d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private long min(long a, long b, long c, long d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private float min(float a, float b, float c, float d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private double min(double a, double b, double c, double d) { return Math.min(Math.min(a, b), Math.min(c, d)); } private int max(int a, int b) { return Math.max(a, b); } private long max(long a, long b) { return Math.max(a, b); } private float max(float a, float b) { return Math.max(a, b); } private double max(double a, double b) { return Math.max(a, b); } private int max(int a, int b, int c) { return Math.max(a, Math.max(b, c)); } private float max(float a, float b, float c) { return Math.max(a, Math.max(b, c)); } private double max(double a, double b, double c) { return Math.max(a, Math.max(b, c)); } private long max(long a, long b, long c) { return Math.max(a, Math.max(b, c)); } private int max(int a, int b, int c, int d) { return Math.max(Math.max(a, b), Math.max(c, d)); } private long max(long a, long b, long c, long d) { return Math.max(Math.max(a, b), Math.max(c, d)); } private float max(float a, float b, float c, float d) { return Math.max(Math.max(a, b), Math.max(c, d)); } private double max(double a, double b, double c, double d) { return Math.max(Math.max(a, b), Math.max(c, d)); } }
ConDefects/ConDefects/Code/abc234_f/Java/28416734
condefects-java_data_76
import java.io.*; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static final int MM = 5005; static final long MOD = 998244353; static long[] freq = new long[26], dp[] = new long[26][MM], fact = new long[MM], inv = new long[MM]; static long ans; static long pow(long n, long t) { if (t == 0) return 1; long res = pow(n, t/2); res = res * res % MOD; if (t % 2 == 1) res = res * n % MOD; return res % MOD; } static long c(int n, int k) { if (n < k || n < 0 || k < 0) return 0; return fact[n] * inv[k] % MOD * inv[n-k] % MOD; } public static void main(String[] args) throws IOException { String s = readLine(); for (char c: s.toCharArray()) { freq[c - 'a'] ++; } fact[0] = 1; inv[0] = 1; for (int i=1; i<MM; i++) { fact[i] = i * fact[i-1] % MOD; inv[i] = pow(fact[i], MOD-2) % MOD; if (i <= freq[0]) dp[0][i] = 1; } dp[0][0] = 1; for (int i=1; i<26; i++) { for (int j=0; j<=s.length(); j++) { for (int k=0; k<=Math.min(j, freq[i]); k++) { dp[i][j] += dp[i-1][j-k] * c(j-k+1, k); dp[i][j] %= MOD; } } } for (int j=1; j<=s.length(); j++) { ans = (ans + dp[26-1][j]) % MOD; } System.out.println(ans % MOD); } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static String readLine () throws IOException { return br.readLine().trim(); } static int readInt () throws IOException { return Integer.parseInt(next()); } } import java.io.*; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static final int MM = 5005; static final long MOD = 998244353; static long[] freq = new long[26], dp[] = new long[26][MM], fact = new long[MM], inv = new long[MM]; static long ans; static long pow(long n, long t) { if (t == 0) return 1; long res = pow(n, t/2); res = res * res % MOD; if (t % 2 == 1) res = res * n % MOD; return res % MOD; } static long c(int n, int k) { if (n < k || n < 0 || k < 0) return 0; return fact[n] * inv[k] % MOD * inv[n-k] % MOD; } public static void main(String[] args) throws IOException { String s = readLine(); for (char c: s.toCharArray()) { freq[c - 'a'] ++; } fact[0] = 1; inv[0] = 1; for (int i=1; i<MM; i++) { fact[i] = i * fact[i-1] % MOD; inv[i] = pow(fact[i], MOD-2) % MOD; if (i <= freq[0]) dp[0][i] = 1; } dp[0][0] = 1; for (int i=1; i<26; i++) { for (int j=0; j<=s.length(); j++) { for (int k=0; k<=Math.min(j, freq[i]); k++) { dp[i][j] += dp[i-1][j-k] * c(j, k); dp[i][j] %= MOD; } } } for (int j=1; j<=s.length(); j++) { ans = (ans + dp[26-1][j]) % MOD; } System.out.println(ans % MOD); } static String next () throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static String readLine () throws IOException { return br.readLine().trim(); } static int readInt () throws IOException { return Integer.parseInt(next()); } }
ConDefects/ConDefects/Code/abc234_f/Java/28458592
condefects-java_data_77
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import java.util.function.IntUnaryOperator; import java.util.function.LongUnaryOperator; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { static In in = new In(); static Out out = new Out(false, false); static final long inf = 0x1fffffffffffffffL; static final int iinf = 0x3fffffff; static final double eps = 1e-9; static long mod = 998244353; int n; long[] x, y; void solve() { n = in.nextInt(); x = new long[n + 2]; y = new long[n + 2]; for (int i = 0; i < n; i++) { x[i + 1] = in.nextLong(); } for (int i = 0; i < n; i++) { y[i + 1] = in.nextLong(); } x[0] = -inf; x[n + 1] = inf; // out.debug(f(1)); // out.debug(f(2)); // out.debug(f(3)); long left = 0; long right = inf; while (right - left > 1) { long mid = (left + right) / 2; if (f(mid)) { left = mid; } else { right = mid; } } out.println(left == inf ? -1 : left); } boolean f(long mid) { PriorityQueue<LongPair> queue1 = new PriorityQueue<>(); long count = 0; for (int i = 1; i <= n + 1; i++) { if (y[i - 1] < y[i]) { queue1.add(new LongPair(x[i - 1] + 1, y[i] - y[i - 1])); } else if (y[i - 1] > y[i]) { long rem = y[i - 1] - y[i]; // v <= x[i] - 1 - mid while (!queue1.isEmpty() && queue1.peek().first <= x[i] - 1 - mid) { LongPair p = queue1.remove(); count += p.second; } if (count < rem) { return false; } count -= rem; } } return true; } public static void main(String... args) { new Main().solve(); out.flush(); } } class BinaryTrie { private static final int BIT_LEN = 63; private final Node root = new Node(); public void clear() { root.count = 0; root.left = null; root.right = null; } public long size() { return root.count; } public boolean isEmpty() { return size() == 0; } public long get(long i) { if (i < 0 || size() <= i) { throw new IndexOutOfBoundsException(); } Node node = root; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; long lc = node.left == null ? 0 : node.left.count; if (i < lc) { node = node.left; } else { val++; i -= lc; node = node.right; } } return val; } public void add(long x) { add(x, 1); } public void add(long x, long count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } Node node = root; node.count += count; node.sum += x * count; for (int i = BIT_LEN - 1; i >= 0; i--) { if ((x >> i & 1) == 0) { if (node.left == null) { node.left = new Node(); } node.left.count += count; node.left.sum += x * count; node = node.left; } else { if (node.right == null) { node.right = new Node(); } node.right.count += count; node.right.sum += x * count; node = node.right; } } } public boolean remove(long x) { return remove(x, 1); } public boolean remove(long x, long count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } if (count == 0) { return false; } if (size() < count) { throw new NoSuchElementException(); } Node node = root; node.count -= count; node.sum -= x * count; for (int i = BIT_LEN - 1; i >= 0; i--) { if ((x >> i & 1) == 0) { if (node.left.count < count) { throw new NoSuchElementException(); } node.left.count -= count; node.left.sum -= x * count; if (node.left.count == 0) { node.left = null; return true; } node = node.left; } else { if (node.right.count < count) { throw new NoSuchElementException(); } node.right.count -= count; node.right.sum -= x * count; if (node.right.count == 0) { node.right = null; return true; } node = node.right; } } return true; } public long count(long x) { Node node = root; for (int i = BIT_LEN - 1; i >= 0; i--) { if ((x >> i & 1) == 0) { node = node.left; } else { node = node.right; } if (node == null) { return 0; } } return node.count; } public boolean contains(long x) { return count(x) >= 1; } public OptionalLong min() { if (isEmpty()) { return OptionalLong.empty(); } Node node = root; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; if (node.left != null) { node = node.left; } else { node = node.right; val++; } } return OptionalLong.of(val); } public OptionalLong max() { if (isEmpty()) { return OptionalLong.empty(); } Node node = root; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; if (node.right == null) { node = node.left; } else { node = node.right; val++; } } return OptionalLong.of(val); } public long lowerBound(long x) { if (isEmpty()) { return 0; } Node node = root; long i = 0; for (int j = BIT_LEN - 1; j >= 0; j--) { if ((x >> j & 1) == 0) { node = node.left; } else { i += node.left == null ? 0 : node.left.count; node = node.right; } if (node == null) { return i; } } return i; } public long upperBound(long x) { return lowerBound(x + 1); } public long sum(long l, long r) { if (0 < l) { return sum(0, r) - sum(0, l); } Node node = root; long acc = 0; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; long lc = node.left == null ? 0 : node.left.count; if (r < lc) { node = node.left; } else { acc += node.left == null ? 0 : node.left.sum; r -= lc; val++; node = node.right; } if (node == null) { return acc; } } acc += Math.min(r, node.count) * val; return acc; } public OptionalLong lower(long x) { long index = lowerBound(x) - 1; if (index < 0) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public OptionalLong higher(long x) { long index = upperBound(x); if (size() <= index) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public OptionalLong floor(long x) { long index = upperBound(x) - 1; if (index < 0) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public OptionalLong ceil(long x) { long index = lowerBound(x); if (size() <= index) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public List<Long> toList() { List<Long> list = new ArrayList<>(); root.collect(0, 0, list); return Collections.unmodifiableList(list); } @Override public String toString() { return toList().toString(); } private static class Node { private long count; private long sum; private Node left; private Node right; private void collect(int depth, long val, List<Long> list) { if (left != null) { left.collect(depth + 1, val * 2, list); } if (right != null) { right.collect(depth + 1, val * 2 + 1, list); } if (depth == BIT_LEN) { for (long i = 0; i < count; i++) { list.add(val); } } } } } class LongPair implements Comparable<LongPair> { long first; long second; LongPair(long first, long second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (!(o instanceof LongPair)) { return false; } LongPair that = (LongPair)o; return first == that.first && second == that.second; } @Override public int hashCode() { return Long.hashCode(first) * 31 + Long.hashCode(second); } @Override public int compareTo(LongPair o) { return first == o.first ? Long.compare(second, o.second) : Long.compare(first, o.first); } @Override public String toString() { return String.format("[%d, %d]", first, second); } } class In { private final BufferedInputStream reader = new BufferedInputStream(System.in); private final byte[] buffer = new byte[0x10000]; private int i = 0; private int length = 0; public int read() { if (i == length) { i = 0; try { length = reader.read(buffer); } catch (IOException ignored) { } if (length == -1) { return 0; } } if (length <= i) { throw new RuntimeException(); } return buffer[i++]; } public String next() { StringBuilder builder = new StringBuilder(); int b = read(); while (b < '!' || '~' < b) { b = read(); } while ('!' <= b && b <= '~') { builder.appendCodePoint(b); b = read(); } return builder.toString(); } public String nextLine() { StringBuilder builder = new StringBuilder(); int b = read(); while (b != 0 && b != '\r' && b != '\n') { builder.appendCodePoint(b); b = read(); } if (b == '\r') { read(); } return builder.toString(); } public int nextInt() { long val = nextLong(); if (val < Integer.MIN_VALUE || Integer.MAX_VALUE < val) { throw new NumberFormatException(); } return (int)val; } public long nextLong() { int b = read(); while (b < '!' || '~' < b) { b = read(); } boolean neg = false; if (b == '-') { neg = true; b = read(); } long n = 0; int c = 0; while ('0' <= b && b <= '9') { n = n * 10 + b - '0'; b = read(); c++; } if (c == 0 || c >= 2 && n == 0) { throw new NumberFormatException(); } return neg ? -n : n; } public double nextDouble() { return Double.parseDouble(next()); } public char[] nextCharArray() { return next().toCharArray(); } public String[] nextStringArray(int n) { String[] s = new String[n]; for (int i = 0; i < n; i++) { s[i] = next(); } return s; } public char[][] nextCharMatrix(int n, int m) { char[][] a = new char[n][m]; for (int i = 0; i < n; i++) { a[i] = next().toCharArray(); } return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public int[] nextIntArray(int n, IntUnaryOperator op) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsInt(nextInt()); } return a; } public int[][] nextIntMatrix(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = nextIntArray(w); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public long[] nextLongArray(int n, LongUnaryOperator op) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsLong(nextLong()); } return a; } public long[][] nextLongMatrix(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nextLongArray(w); } return a; } public List<List<Integer>> nextGraph(int n, int m, boolean directed) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; res.get(u).add(v); if (!directed) { res.get(v).add(u); } } return res; } } class Out { private final PrintWriter out = new PrintWriter(System.out); private final PrintWriter err = new PrintWriter(System.err); public boolean autoFlush; public boolean enableDebug; public Out(boolean autoFlush, boolean enableDebug) { this.autoFlush = autoFlush; this.enableDebug = enableDebug; } public void debug(Object... args) { if (!enableDebug) { return; } if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } err.println(Arrays.stream(args).map(obj -> format(obj, true)).collect(Collectors.joining(" "))); err.flush(); } private String format(Object obj, boolean canMultiline) { if (obj == null) return "null"; Class<?> clazz = obj.getClass(); if (clazz == Double.class) return String.format("%.10f", obj); if (clazz == int[].class) return Arrays.toString((int[])obj); if (clazz == long[].class) return Arrays.toString((long[])obj); if (clazz == char[].class) return String.valueOf((char[])obj); if (clazz == boolean[].class) return IntStream.range(0, ((boolean[])obj).length).mapToObj(i -> ((boolean[])obj)[i] ? "1" : "0").collect(Collectors.joining()); if (clazz == double[].class) return Arrays.toString(Arrays.stream((double[])obj).mapToObj(a -> format(a, false)).toArray()); if (canMultiline && clazz.isArray() && clazz.componentType().isArray()) return Arrays.stream((Object[])obj).map(a -> format(a, false)).collect(Collectors.joining("\n")); if (clazz == Object[].class) return Arrays.toString(Arrays.stream((Object[])obj).map(a -> format(a, false)).toArray()); return String.valueOf(obj); } public void println(Object... args) { if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } out.println(Arrays.stream(args) .map(obj -> obj instanceof Double ? String.format("%.10f", obj) : String.valueOf(obj)) .collect(Collectors.joining(" "))); if (autoFlush) { out.flush(); } } public void println(char a) { out.println(a); if (autoFlush) { out.flush(); } } public void println(int a) { out.println(a); if (autoFlush) { out.flush(); } } public void println(long a) { out.println(a); if (autoFlush) { out.flush(); } } public void println(double a) { out.println(String.format("%.10f", a)); if (autoFlush) { out.flush(); } } public void println(String s) { out.println(s); if (autoFlush) { out.flush(); } } public void println(char[] s) { out.println(String.valueOf(s)); if (autoFlush) { out.flush(); } } public void println(int[] a) { StringJoiner joiner = new StringJoiner(" "); for (int i : a) { joiner.add(Integer.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } public void println(long[] a) { StringJoiner joiner = new StringJoiner(" "); for (long i : a) { joiner.add(Long.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } public void flush() { err.flush(); out.flush(); } } import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import java.util.function.IntUnaryOperator; import java.util.function.LongUnaryOperator; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { static In in = new In(); static Out out = new Out(false, false); static final long inf = 0x1fffffffffffffffL; static final int iinf = 0x3fffffff; static final double eps = 1e-9; static long mod = 998244353; int n; long[] x, y; void solve() { n = in.nextInt(); x = new long[n + 2]; y = new long[n + 2]; for (int i = 0; i < n; i++) { x[i + 1] = in.nextLong(); } for (int i = 0; i < n; i++) { y[i + 1] = in.nextLong(); } x[0] = -inf; x[n + 1] = inf; // out.debug(f(1)); // out.debug(f(2)); // out.debug(f(3)); long left = 0; long right = inf; while (right - left > 1) { long mid = (left + right) / 2; if (f(mid)) { left = mid; } else { right = mid; } } out.println(left >= inf / 2 ? -1 : left); } boolean f(long mid) { PriorityQueue<LongPair> queue1 = new PriorityQueue<>(); long count = 0; for (int i = 1; i <= n + 1; i++) { if (y[i - 1] < y[i]) { queue1.add(new LongPair(x[i - 1] + 1, y[i] - y[i - 1])); } else if (y[i - 1] > y[i]) { long rem = y[i - 1] - y[i]; // v <= x[i] - 1 - mid while (!queue1.isEmpty() && queue1.peek().first <= x[i] - 1 - mid) { LongPair p = queue1.remove(); count += p.second; } if (count < rem) { return false; } count -= rem; } } return true; } public static void main(String... args) { new Main().solve(); out.flush(); } } class BinaryTrie { private static final int BIT_LEN = 63; private final Node root = new Node(); public void clear() { root.count = 0; root.left = null; root.right = null; } public long size() { return root.count; } public boolean isEmpty() { return size() == 0; } public long get(long i) { if (i < 0 || size() <= i) { throw new IndexOutOfBoundsException(); } Node node = root; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; long lc = node.left == null ? 0 : node.left.count; if (i < lc) { node = node.left; } else { val++; i -= lc; node = node.right; } } return val; } public void add(long x) { add(x, 1); } public void add(long x, long count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } Node node = root; node.count += count; node.sum += x * count; for (int i = BIT_LEN - 1; i >= 0; i--) { if ((x >> i & 1) == 0) { if (node.left == null) { node.left = new Node(); } node.left.count += count; node.left.sum += x * count; node = node.left; } else { if (node.right == null) { node.right = new Node(); } node.right.count += count; node.right.sum += x * count; node = node.right; } } } public boolean remove(long x) { return remove(x, 1); } public boolean remove(long x, long count) { if (x < 0 || count < 0) { throw new IllegalArgumentException(); } if (count == 0) { return false; } if (size() < count) { throw new NoSuchElementException(); } Node node = root; node.count -= count; node.sum -= x * count; for (int i = BIT_LEN - 1; i >= 0; i--) { if ((x >> i & 1) == 0) { if (node.left.count < count) { throw new NoSuchElementException(); } node.left.count -= count; node.left.sum -= x * count; if (node.left.count == 0) { node.left = null; return true; } node = node.left; } else { if (node.right.count < count) { throw new NoSuchElementException(); } node.right.count -= count; node.right.sum -= x * count; if (node.right.count == 0) { node.right = null; return true; } node = node.right; } } return true; } public long count(long x) { Node node = root; for (int i = BIT_LEN - 1; i >= 0; i--) { if ((x >> i & 1) == 0) { node = node.left; } else { node = node.right; } if (node == null) { return 0; } } return node.count; } public boolean contains(long x) { return count(x) >= 1; } public OptionalLong min() { if (isEmpty()) { return OptionalLong.empty(); } Node node = root; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; if (node.left != null) { node = node.left; } else { node = node.right; val++; } } return OptionalLong.of(val); } public OptionalLong max() { if (isEmpty()) { return OptionalLong.empty(); } Node node = root; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; if (node.right == null) { node = node.left; } else { node = node.right; val++; } } return OptionalLong.of(val); } public long lowerBound(long x) { if (isEmpty()) { return 0; } Node node = root; long i = 0; for (int j = BIT_LEN - 1; j >= 0; j--) { if ((x >> j & 1) == 0) { node = node.left; } else { i += node.left == null ? 0 : node.left.count; node = node.right; } if (node == null) { return i; } } return i; } public long upperBound(long x) { return lowerBound(x + 1); } public long sum(long l, long r) { if (0 < l) { return sum(0, r) - sum(0, l); } Node node = root; long acc = 0; long val = 0; for (int j = 0; j < BIT_LEN; j++) { val *= 2; long lc = node.left == null ? 0 : node.left.count; if (r < lc) { node = node.left; } else { acc += node.left == null ? 0 : node.left.sum; r -= lc; val++; node = node.right; } if (node == null) { return acc; } } acc += Math.min(r, node.count) * val; return acc; } public OptionalLong lower(long x) { long index = lowerBound(x) - 1; if (index < 0) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public OptionalLong higher(long x) { long index = upperBound(x); if (size() <= index) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public OptionalLong floor(long x) { long index = upperBound(x) - 1; if (index < 0) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public OptionalLong ceil(long x) { long index = lowerBound(x); if (size() <= index) { return OptionalLong.empty(); } return OptionalLong.of(get(index)); } public List<Long> toList() { List<Long> list = new ArrayList<>(); root.collect(0, 0, list); return Collections.unmodifiableList(list); } @Override public String toString() { return toList().toString(); } private static class Node { private long count; private long sum; private Node left; private Node right; private void collect(int depth, long val, List<Long> list) { if (left != null) { left.collect(depth + 1, val * 2, list); } if (right != null) { right.collect(depth + 1, val * 2 + 1, list); } if (depth == BIT_LEN) { for (long i = 0; i < count; i++) { list.add(val); } } } } } class LongPair implements Comparable<LongPair> { long first; long second; LongPair(long first, long second) { this.first = first; this.second = second; } @Override public boolean equals(Object o) { if (!(o instanceof LongPair)) { return false; } LongPair that = (LongPair)o; return first == that.first && second == that.second; } @Override public int hashCode() { return Long.hashCode(first) * 31 + Long.hashCode(second); } @Override public int compareTo(LongPair o) { return first == o.first ? Long.compare(second, o.second) : Long.compare(first, o.first); } @Override public String toString() { return String.format("[%d, %d]", first, second); } } class In { private final BufferedInputStream reader = new BufferedInputStream(System.in); private final byte[] buffer = new byte[0x10000]; private int i = 0; private int length = 0; public int read() { if (i == length) { i = 0; try { length = reader.read(buffer); } catch (IOException ignored) { } if (length == -1) { return 0; } } if (length <= i) { throw new RuntimeException(); } return buffer[i++]; } public String next() { StringBuilder builder = new StringBuilder(); int b = read(); while (b < '!' || '~' < b) { b = read(); } while ('!' <= b && b <= '~') { builder.appendCodePoint(b); b = read(); } return builder.toString(); } public String nextLine() { StringBuilder builder = new StringBuilder(); int b = read(); while (b != 0 && b != '\r' && b != '\n') { builder.appendCodePoint(b); b = read(); } if (b == '\r') { read(); } return builder.toString(); } public int nextInt() { long val = nextLong(); if (val < Integer.MIN_VALUE || Integer.MAX_VALUE < val) { throw new NumberFormatException(); } return (int)val; } public long nextLong() { int b = read(); while (b < '!' || '~' < b) { b = read(); } boolean neg = false; if (b == '-') { neg = true; b = read(); } long n = 0; int c = 0; while ('0' <= b && b <= '9') { n = n * 10 + b - '0'; b = read(); c++; } if (c == 0 || c >= 2 && n == 0) { throw new NumberFormatException(); } return neg ? -n : n; } public double nextDouble() { return Double.parseDouble(next()); } public char[] nextCharArray() { return next().toCharArray(); } public String[] nextStringArray(int n) { String[] s = new String[n]; for (int i = 0; i < n; i++) { s[i] = next(); } return s; } public char[][] nextCharMatrix(int n, int m) { char[][] a = new char[n][m]; for (int i = 0; i < n; i++) { a[i] = next().toCharArray(); } return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public int[] nextIntArray(int n, IntUnaryOperator op) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsInt(nextInt()); } return a; } public int[][] nextIntMatrix(int h, int w) { int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { a[i] = nextIntArray(w); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public long[] nextLongArray(int n, LongUnaryOperator op) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = op.applyAsLong(nextLong()); } return a; } public long[][] nextLongMatrix(int h, int w) { long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { a[i] = nextLongArray(w); } return a; } public List<List<Integer>> nextGraph(int n, int m, boolean directed) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; res.get(u).add(v); if (!directed) { res.get(v).add(u); } } return res; } } class Out { private final PrintWriter out = new PrintWriter(System.out); private final PrintWriter err = new PrintWriter(System.err); public boolean autoFlush; public boolean enableDebug; public Out(boolean autoFlush, boolean enableDebug) { this.autoFlush = autoFlush; this.enableDebug = enableDebug; } public void debug(Object... args) { if (!enableDebug) { return; } if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } err.println(Arrays.stream(args).map(obj -> format(obj, true)).collect(Collectors.joining(" "))); err.flush(); } private String format(Object obj, boolean canMultiline) { if (obj == null) return "null"; Class<?> clazz = obj.getClass(); if (clazz == Double.class) return String.format("%.10f", obj); if (clazz == int[].class) return Arrays.toString((int[])obj); if (clazz == long[].class) return Arrays.toString((long[])obj); if (clazz == char[].class) return String.valueOf((char[])obj); if (clazz == boolean[].class) return IntStream.range(0, ((boolean[])obj).length).mapToObj(i -> ((boolean[])obj)[i] ? "1" : "0").collect(Collectors.joining()); if (clazz == double[].class) return Arrays.toString(Arrays.stream((double[])obj).mapToObj(a -> format(a, false)).toArray()); if (canMultiline && clazz.isArray() && clazz.componentType().isArray()) return Arrays.stream((Object[])obj).map(a -> format(a, false)).collect(Collectors.joining("\n")); if (clazz == Object[].class) return Arrays.toString(Arrays.stream((Object[])obj).map(a -> format(a, false)).toArray()); return String.valueOf(obj); } public void println(Object... args) { if (args == null || args.getClass() != Object[].class) { args = new Object[] {args}; } out.println(Arrays.stream(args) .map(obj -> obj instanceof Double ? String.format("%.10f", obj) : String.valueOf(obj)) .collect(Collectors.joining(" "))); if (autoFlush) { out.flush(); } } public void println(char a) { out.println(a); if (autoFlush) { out.flush(); } } public void println(int a) { out.println(a); if (autoFlush) { out.flush(); } } public void println(long a) { out.println(a); if (autoFlush) { out.flush(); } } public void println(double a) { out.println(String.format("%.10f", a)); if (autoFlush) { out.flush(); } } public void println(String s) { out.println(s); if (autoFlush) { out.flush(); } } public void println(char[] s) { out.println(String.valueOf(s)); if (autoFlush) { out.flush(); } } public void println(int[] a) { StringJoiner joiner = new StringJoiner(" "); for (int i : a) { joiner.add(Integer.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } public void println(long[] a) { StringJoiner joiner = new StringJoiner(" "); for (long i : a) { joiner.add(Long.toString(i)); } out.println(joiner); if (autoFlush) { out.flush(); } } public void flush() { err.flush(); out.flush(); } }
ConDefects/ConDefects/Code/arc166_d/Java/46384488
condefects-java_data_78
import java.util.*; import java.io.*; class Main { private static final void solve() throws IOException { n = ni(); x = nl(n); y = nl(n); yy = new long[n]; for (int i = 0; i < n; i++) { yy[i] = y[i]; if (i != 0) yy[i] -= y[i - 1]; } long jud_y = 0L; for (long i : yy) jud_y -= Math.min(0, i); if (jud_y <= yy[0]) { ou.println(-1); return; } final long inf = (long) 1e9 + 2022; long ok = 1L, ng = inf + 1; while (ng - ok != 1) { final long vs = ok + ng >> 1; if (f(vs)) ok = vs; else ng = vs; } ou.println(ok == inf ? -1 : ok); return; } private static final class Point { final long x, y; Point(long xx, long yy) { x = xx; y = yy; } } private static int n; private static long[] x, y, yy; private static final boolean f(long K) { var t = yy.clone(); var q = new PriorityQueue<Point>((i, j) -> Long.compare(i.x, j.x)); for (int i = 1; i < n; i++) { if (t[i] == 0) continue; if (t[i] > 0) { q.add(new Point(x[i - 1] + 1 + K, t[i])); } else { while (t[i] < 0 && !q.isEmpty() && q.peek().x < x[i]) { var p = q.remove(); if (t[i] + p.y >= 0) { t[i] = 0; q.add(new Point(p.x, t[i] + p.y)); } else t[i] += p.y; } } } long jud_y = 0L; for (long i : t) jud_y -= Math.min(0, i); if (jud_y <= t[0]) return true; return false; } public static void main(String[] args) throws IOException { // for (int i = 0, t = ni(); i < t; i++) solve(); ou.flush(); } private static final int ni() throws IOException { return sc.nextInt(); } private static final int[] ni(int n) throws IOException { return sc.nextIntArray(n); } private static final int[] ni1(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni() - 1; return a; } private static final long nl() throws IOException { return sc.nextLong(); } private static final long[] nl(int n) throws IOException { return sc.nextLongArray(n); } private static final String ns() throws IOException { return sc.next(); } private static final char[] nc() throws IOException { return sc.nextCharArray(); } private static final int[] nc1() throws IOException { char[] c = nc(); int n = c.length; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = c[i] - '0'; return a; } private static final int[] nca() throws IOException { char[] c = nc(); int n = c.length; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = c[i] - 'a'; return a; } private static final double nd() throws IOException { return sc.nextDouble(); } private static final ContestInputStream sc = new ContestInputStream(); private static final ContestOutputStream ou = new ContestOutputStream(); } final class ContestInputStream extends FilterInputStream { protected final byte[] buf; protected int pos = 0; protected int lim = 0; private final char[] cbuf; public ContestInputStream() { super(System.in); this.buf = new byte[1 << 13]; this.cbuf = new char[1 << 20]; } boolean hasRemaining() throws IOException { if (pos < lim) return true; lim = in.read(buf); pos = 0; return lim > 0; } final int remaining() throws IOException { if (pos >= lim) { lim = in.read(buf); pos = 0; } return lim - pos; } @Override public final int available() throws IOException { if (pos < lim) return lim - pos + in.available(); return in.available(); } @Override public final long skip(long n) throws IOException { if (pos < lim) { int rem = lim - pos; if (n < rem) { pos += n; return n; } pos = lim; return rem; } return in.skip(n); } @Override public final int read() throws IOException { if (hasRemaining()) return buf[pos++]; return -1; } @Override public final int read(byte[] b, int off, int len) throws IOException { if (pos < lim) { int rem = Math.min(lim - pos, len); for (int i = 0; i < rem; i++) b[off + i] = buf[pos + i]; pos += rem; return rem; } return in.read(b, off, len); } public final char[] readToken() throws IOException { int cpos = 0; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } char[] arr = new char[cpos]; for (int i = 0; i < cpos; i++) arr[i] = cbuf[i]; return arr; } public final int readToken(char[] cbuf, int off) throws IOException { int cpos = off; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } return cpos - off; } public final int readToken(char[] cbuf) throws IOException { return readToken(cbuf, 0); } public final String next() throws IOException { int cpos = 0; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } return String.valueOf(cbuf, 0, cpos); } public final char[] nextCharArray() throws IOException { return readToken(); } public final int nextInt() throws IOException { if (!hasRemaining()) return 0; int value = 0; byte b = buf[pos++]; if (b == 0x2d) { while (hasRemaining() && (b = buf[pos++]) > 0x20) value = (value << 3) + (value << 1) - b + 0x30; } else { do { value = (value << 3) + (value << 1) + b - 0x30; } while (hasRemaining() && (b = buf[pos++]) > 0x20); } if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return value; } public final long nextLong() throws IOException { if (!hasRemaining()) return 0L; long value = 0L; byte b = buf[pos++]; if (b == 0x2d) { while (hasRemaining() && (b = buf[pos++]) > 0x20) value = (value << 3) + (value << 1) - b + 0x30; } else { do { value = (value << 3) + (value << 1) + b - 0x30; } while (hasRemaining() && (b = buf[pos++]) > 0x20); } if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return value; } public final char nextChar() throws IOException { if (!hasRemaining()) throw new EOFException(); final char c = (char) buf[pos++]; if (hasRemaining() && buf[pos++] == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return c; } public final float nextFloat() throws IOException { return Float.parseFloat(next()); } public final double nextDouble() throws IOException { return Double.parseDouble(next()); } public final boolean[] nextBooleanArray(char ok) throws IOException { char[] s = readToken(); int n = s.length; boolean[] t = new boolean[n]; for (int i = 0; i < n; i++) t[i] = s[i] == ok; return t; } public final boolean[][] nextBooleanMatrix(int h, int w, char ok) throws IOException { boolean[][] s = new boolean[h][]; for (int i = 0; i < h; i++) { char[] t = readToken(); int n = t.length; s[i] = new boolean[n]; for (int j = 0; j < n; j++) s[i][j] = t[j] == ok; } return s; } public final String[] nextStringArray(int len) throws IOException { String[] arr = new String[len]; for (int i = 0; i < len; i++) arr[i] = next(); return arr; } public final int[] nextIntArray(int len) throws IOException { int[] arr = new int[len]; for (int i = 0; i < len; i++) arr[i] = nextInt(); return arr; } public final int[] nextIntArray(int len, java.util.function.IntUnaryOperator map) throws IOException { int[] arr = new int[len]; for (int i = 0; i < len; i++) arr[i] = map.applyAsInt(nextInt()); return arr; } public final long[] nextLongArray(int len, java.util.function.LongUnaryOperator map) throws IOException { long[] arr = new long[len]; for (int i = 0; i < len; i++) arr[i] = map.applyAsLong(nextLong()); return arr; } public final int[][] nextIntMatrix(int h, int w) throws IOException { int[][] arr = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = nextInt(); return arr; } public final int[][] nextIntMatrix(int h, int w, java.util.function.IntUnaryOperator map) throws IOException { int[][] arr = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = map.applyAsInt(nextInt()); return arr; } public final long[] nextLongArray(int len) throws IOException { long[] arr = new long[len]; for (int i = 0; i < len; i++) arr[i] = nextLong(); return arr; } public final long[][] nextLongMatrix(int h, int w) throws IOException { long[][] arr = new long[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = nextLong(); return arr; } public final float[] nextFloatArray(int len) throws IOException { float[] arr = new float[len]; for (int i = 0; i < len; i++) arr[i] = nextFloat(); return arr; } public final double[] nextDoubleArray(int len) throws IOException { double[] arr = new double[len]; for (int i = 0; i < len; i++) arr[i] = nextDouble(); return arr; } public final char[][] nextCharMatrix(int h, int w) throws IOException { char[][] arr = new char[h][]; for (int i = 0; i < h; i++) arr[i] = readToken(); return arr; } public final void nextThrow() throws IOException { next(); return; } public final void nextThrow(int n) throws IOException { for (int i = 0; i < n; i++) nextThrow(); return; } } final class ContestOutputStream extends FilterOutputStream implements Appendable { protected final byte[] buf; protected int pos = 0; public ContestOutputStream() { super(System.out); this.buf = new byte[1 << 13]; } @Override public void flush() throws IOException { out.write(buf, 0, pos); pos = 0; out.flush(); } void put(byte b) throws IOException { if (pos >= buf.length) flush(); buf[pos++] = b; } int remaining() throws IOException { if (pos >= buf.length) flush(); return buf.length - pos; } @Override public void write(int b) throws IOException { put((byte) b); } @Override public void write(byte[] b, int off, int len) throws IOException { int o = off; int l = len; while (l > 0) { int rem = Math.min(remaining(), l); for (int i = 0; i < rem; i++) buf[pos + i] = b[o + i]; pos += rem; o += rem; l -= rem; } } @Override public ContestOutputStream append(char c) throws IOException { put((byte) c); return this; } @Override public ContestOutputStream append(CharSequence csq, int start, int end) throws IOException { int off = start; int len = end - start; while (len > 0) { int rem = Math.min(remaining(), len); for (int i = 0; i < rem; i++) buf[pos + i] = (byte) csq.charAt(off + i); pos += rem; off += rem; len -= rem; } return this; } @Override public ContestOutputStream append(CharSequence csq) throws IOException { return append(csq, 0, csq.length()); } public ContestOutputStream append(char[] arr, int off, int len) throws IOException { int o = off; int l = len; while (l > 0) { int rem = Math.min(remaining(), l); for (int i = 0; i < rem; i++) buf[pos + i] = (byte) arr[o + i]; pos += rem; o += rem; l -= rem; } return this; } public ContestOutputStream print(char[] arr) throws IOException { return append(arr, 0, arr.length).newLine(); } public ContestOutputStream print(boolean value) throws IOException { if (value) return append("o"); return append("."); } public ContestOutputStream println(boolean value) throws IOException { if (value) return append("o\n"); return append(".\n"); } public ContestOutputStream print(boolean[][] value) throws IOException { final int n = value.length, m = value[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) print(value[i][j]); newLine(); } return this; } public ContestOutputStream print(int value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(int value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(long value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(long value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(float value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(float value) throws IOException { return append(String.valueOf(value)).newLine(); } private ContestOutputStream dtos(double x, int n) throws IOException { if (x < 0) { append('-'); x = -x; } x += Math.pow(10, -n) / 2; long longx = (long) x; print(longx); append('.'); x -= longx; for (int i = 0; i < n; i++) { x *= 10; int intx = (int) x; print(intx); x -= intx; } return this; } public ContestOutputStream print(double value) throws IOException { return dtos(value, 20); } public ContestOutputStream println(double value) throws IOException { return dtos(value, 20).newLine(); } public ContestOutputStream print(char value) throws IOException { return append(value); } public ContestOutputStream println(char value) throws IOException { return append(value).newLine(); } public ContestOutputStream print(String value) throws IOException { return append(value); } public ContestOutputStream println(String value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(Object value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(Object value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream printYN(boolean yes) throws IOException { if (yes) return println("Yes"); return println("No"); } public ContestOutputStream printAB(boolean yes) throws IOException { if (yes) return println("Alice"); return println("Bob"); } public ContestOutputStream print(CharSequence[] arr) throws IOException { if (arr.length > 0) { append(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').append(arr[i]); } return this; } public ContestOutputStream print(int[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(int[] arr, int length) throws IOException { if (length > 0) print(arr[0]); for (int i = 1; i < length; i++) append('\u0020').print(arr[i]); newLine(); return this; } public ContestOutputStream println(int[] arr) throws IOException { for (int i : arr) print(i).newLine(); return this; } public ContestOutputStream println(int[] arr, int length) throws IOException { for (int i = 0; i < length; i++) println(arr[i]); return this; } public ContestOutputStream print(boolean[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(long[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(long[] arr, int length) throws IOException { if (length > 0) print(arr[0]); for (int i = 1; i < length; i++) append('\u0020').print(arr[i]); newLine(); return this; } public ContestOutputStream println(long[] arr, int length) throws IOException { for (int i = 0; i < length; i++) println(arr[i]); return this; } public ContestOutputStream println(long[] arr) throws IOException { for (long i : arr) print(i).newLine(); return this; } public ContestOutputStream print(float[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return this; } public ContestOutputStream println(float[] arr) throws IOException { for (float i : arr) print(i).newLine(); return this; } public ContestOutputStream print(double[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return newLine(); } public ContestOutputStream println(double[] arr) throws IOException { for (double i : arr) print(i).newLine(); return this; } public ContestOutputStream print(Object[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return newLine(); } public ContestOutputStream print(java.util.ArrayList<?> arr) throws IOException { if (!arr.isEmpty()) { final int n = arr.size(); print(arr.get(0)); for (int i = 1; i < n; i++) print(" ").print(arr.get(i)); } return newLine(); } public ContestOutputStream println(java.util.ArrayList<?> arr) throws IOException { final int n = arr.size(); for (int i = 0; i < n; i++) println(arr.get(i)); return this; } public ContestOutputStream newLine() throws IOException { return append(System.lineSeparator()); } public ContestOutputStream endl() throws IOException { newLine().flush(); return this; } public ContestOutputStream print(int[][] arr) throws IOException { for (int[] i : arr) print(i); return this; } public ContestOutputStream print(long[][] arr) throws IOException { for (long[] i : arr) print(i); return this; } public ContestOutputStream print(char[][] arr) throws IOException { for (char[] i : arr) print(i); return this; } public ContestOutputStream print(Object[][] arr) throws IOException { for (Object[] i : arr) print(i); return this; } public ContestOutputStream println() throws IOException { return newLine(); } public ContestOutputStream println(Object... arr) throws IOException { for (Object i : arr) print(i); return newLine(); } public ContestOutputStream printToChar(int c) throws IOException { return print((char) c); } } import java.util.*; import java.io.*; class Main { private static final void solve() throws IOException { n = ni(); x = nl(n); y = nl(n); yy = new long[n]; for (int i = 0; i < n; i++) { yy[i] = y[i]; if (i != 0) yy[i] -= y[i - 1]; } long jud_y = 0L; for (long i : yy) jud_y -= Math.min(0, i); if (jud_y <= yy[0]) { ou.println(-1); return; } final long inf = (long) 1e9 + 2022; long ok = 1L, ng = inf + 1; while (ng - ok != 1) { final long vs = ok + ng >> 1; if (f(vs)) ok = vs; else ng = vs; } ou.println(ok == inf ? -1 : ok); return; } private static final class Point { final long x, y; Point(long xx, long yy) { x = xx; y = yy; } } private static int n; private static long[] x, y, yy; private static final boolean f(long K) { var t = yy.clone(); var q = new PriorityQueue<Point>((i, j) -> Long.compare(i.x, j.x)); for (int i = 1; i < n; i++) { if (t[i] == 0) continue; if (t[i] > 0) { q.add(new Point(x[i - 1] + 1 + K, t[i])); } else { while (t[i] < 0 && !q.isEmpty() && q.peek().x < x[i]) { var p = q.remove(); if (t[i] + p.y >= 0) { q.add(new Point(p.x, t[i] + p.y)); t[i] = 0; } else t[i] += p.y; } } } long jud_y = 0L; for (long i : t) jud_y -= Math.min(0, i); if (jud_y <= t[0]) return true; return false; } public static void main(String[] args) throws IOException { // for (int i = 0, t = ni(); i < t; i++) solve(); ou.flush(); } private static final int ni() throws IOException { return sc.nextInt(); } private static final int[] ni(int n) throws IOException { return sc.nextIntArray(n); } private static final int[] ni1(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni() - 1; return a; } private static final long nl() throws IOException { return sc.nextLong(); } private static final long[] nl(int n) throws IOException { return sc.nextLongArray(n); } private static final String ns() throws IOException { return sc.next(); } private static final char[] nc() throws IOException { return sc.nextCharArray(); } private static final int[] nc1() throws IOException { char[] c = nc(); int n = c.length; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = c[i] - '0'; return a; } private static final int[] nca() throws IOException { char[] c = nc(); int n = c.length; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = c[i] - 'a'; return a; } private static final double nd() throws IOException { return sc.nextDouble(); } private static final ContestInputStream sc = new ContestInputStream(); private static final ContestOutputStream ou = new ContestOutputStream(); } final class ContestInputStream extends FilterInputStream { protected final byte[] buf; protected int pos = 0; protected int lim = 0; private final char[] cbuf; public ContestInputStream() { super(System.in); this.buf = new byte[1 << 13]; this.cbuf = new char[1 << 20]; } boolean hasRemaining() throws IOException { if (pos < lim) return true; lim = in.read(buf); pos = 0; return lim > 0; } final int remaining() throws IOException { if (pos >= lim) { lim = in.read(buf); pos = 0; } return lim - pos; } @Override public final int available() throws IOException { if (pos < lim) return lim - pos + in.available(); return in.available(); } @Override public final long skip(long n) throws IOException { if (pos < lim) { int rem = lim - pos; if (n < rem) { pos += n; return n; } pos = lim; return rem; } return in.skip(n); } @Override public final int read() throws IOException { if (hasRemaining()) return buf[pos++]; return -1; } @Override public final int read(byte[] b, int off, int len) throws IOException { if (pos < lim) { int rem = Math.min(lim - pos, len); for (int i = 0; i < rem; i++) b[off + i] = buf[pos + i]; pos += rem; return rem; } return in.read(b, off, len); } public final char[] readToken() throws IOException { int cpos = 0; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } char[] arr = new char[cpos]; for (int i = 0; i < cpos; i++) arr[i] = cbuf[i]; return arr; } public final int readToken(char[] cbuf, int off) throws IOException { int cpos = off; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } return cpos - off; } public final int readToken(char[] cbuf) throws IOException { return readToken(cbuf, 0); } public final String next() throws IOException { int cpos = 0; int rem; byte b; l: while ((rem = remaining()) > 0) { for (int i = 0; i < rem; i++) { b = buf[pos + i]; if (b <= 0x20) { pos += i + 1; cpos += i; if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; break l; } cbuf[cpos + i] = (char) b; } pos += rem; cpos += rem; } return String.valueOf(cbuf, 0, cpos); } public final char[] nextCharArray() throws IOException { return readToken(); } public final int nextInt() throws IOException { if (!hasRemaining()) return 0; int value = 0; byte b = buf[pos++]; if (b == 0x2d) { while (hasRemaining() && (b = buf[pos++]) > 0x20) value = (value << 3) + (value << 1) - b + 0x30; } else { do { value = (value << 3) + (value << 1) + b - 0x30; } while (hasRemaining() && (b = buf[pos++]) > 0x20); } if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return value; } public final long nextLong() throws IOException { if (!hasRemaining()) return 0L; long value = 0L; byte b = buf[pos++]; if (b == 0x2d) { while (hasRemaining() && (b = buf[pos++]) > 0x20) value = (value << 3) + (value << 1) - b + 0x30; } else { do { value = (value << 3) + (value << 1) + b - 0x30; } while (hasRemaining() && (b = buf[pos++]) > 0x20); } if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return value; } public final char nextChar() throws IOException { if (!hasRemaining()) throw new EOFException(); final char c = (char) buf[pos++]; if (hasRemaining() && buf[pos++] == 0x0d && hasRemaining() && buf[pos] == 0x0a) pos++; return c; } public final float nextFloat() throws IOException { return Float.parseFloat(next()); } public final double nextDouble() throws IOException { return Double.parseDouble(next()); } public final boolean[] nextBooleanArray(char ok) throws IOException { char[] s = readToken(); int n = s.length; boolean[] t = new boolean[n]; for (int i = 0; i < n; i++) t[i] = s[i] == ok; return t; } public final boolean[][] nextBooleanMatrix(int h, int w, char ok) throws IOException { boolean[][] s = new boolean[h][]; for (int i = 0; i < h; i++) { char[] t = readToken(); int n = t.length; s[i] = new boolean[n]; for (int j = 0; j < n; j++) s[i][j] = t[j] == ok; } return s; } public final String[] nextStringArray(int len) throws IOException { String[] arr = new String[len]; for (int i = 0; i < len; i++) arr[i] = next(); return arr; } public final int[] nextIntArray(int len) throws IOException { int[] arr = new int[len]; for (int i = 0; i < len; i++) arr[i] = nextInt(); return arr; } public final int[] nextIntArray(int len, java.util.function.IntUnaryOperator map) throws IOException { int[] arr = new int[len]; for (int i = 0; i < len; i++) arr[i] = map.applyAsInt(nextInt()); return arr; } public final long[] nextLongArray(int len, java.util.function.LongUnaryOperator map) throws IOException { long[] arr = new long[len]; for (int i = 0; i < len; i++) arr[i] = map.applyAsLong(nextLong()); return arr; } public final int[][] nextIntMatrix(int h, int w) throws IOException { int[][] arr = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = nextInt(); return arr; } public final int[][] nextIntMatrix(int h, int w, java.util.function.IntUnaryOperator map) throws IOException { int[][] arr = new int[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = map.applyAsInt(nextInt()); return arr; } public final long[] nextLongArray(int len) throws IOException { long[] arr = new long[len]; for (int i = 0; i < len; i++) arr[i] = nextLong(); return arr; } public final long[][] nextLongMatrix(int h, int w) throws IOException { long[][] arr = new long[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) arr[i][j] = nextLong(); return arr; } public final float[] nextFloatArray(int len) throws IOException { float[] arr = new float[len]; for (int i = 0; i < len; i++) arr[i] = nextFloat(); return arr; } public final double[] nextDoubleArray(int len) throws IOException { double[] arr = new double[len]; for (int i = 0; i < len; i++) arr[i] = nextDouble(); return arr; } public final char[][] nextCharMatrix(int h, int w) throws IOException { char[][] arr = new char[h][]; for (int i = 0; i < h; i++) arr[i] = readToken(); return arr; } public final void nextThrow() throws IOException { next(); return; } public final void nextThrow(int n) throws IOException { for (int i = 0; i < n; i++) nextThrow(); return; } } final class ContestOutputStream extends FilterOutputStream implements Appendable { protected final byte[] buf; protected int pos = 0; public ContestOutputStream() { super(System.out); this.buf = new byte[1 << 13]; } @Override public void flush() throws IOException { out.write(buf, 0, pos); pos = 0; out.flush(); } void put(byte b) throws IOException { if (pos >= buf.length) flush(); buf[pos++] = b; } int remaining() throws IOException { if (pos >= buf.length) flush(); return buf.length - pos; } @Override public void write(int b) throws IOException { put((byte) b); } @Override public void write(byte[] b, int off, int len) throws IOException { int o = off; int l = len; while (l > 0) { int rem = Math.min(remaining(), l); for (int i = 0; i < rem; i++) buf[pos + i] = b[o + i]; pos += rem; o += rem; l -= rem; } } @Override public ContestOutputStream append(char c) throws IOException { put((byte) c); return this; } @Override public ContestOutputStream append(CharSequence csq, int start, int end) throws IOException { int off = start; int len = end - start; while (len > 0) { int rem = Math.min(remaining(), len); for (int i = 0; i < rem; i++) buf[pos + i] = (byte) csq.charAt(off + i); pos += rem; off += rem; len -= rem; } return this; } @Override public ContestOutputStream append(CharSequence csq) throws IOException { return append(csq, 0, csq.length()); } public ContestOutputStream append(char[] arr, int off, int len) throws IOException { int o = off; int l = len; while (l > 0) { int rem = Math.min(remaining(), l); for (int i = 0; i < rem; i++) buf[pos + i] = (byte) arr[o + i]; pos += rem; o += rem; l -= rem; } return this; } public ContestOutputStream print(char[] arr) throws IOException { return append(arr, 0, arr.length).newLine(); } public ContestOutputStream print(boolean value) throws IOException { if (value) return append("o"); return append("."); } public ContestOutputStream println(boolean value) throws IOException { if (value) return append("o\n"); return append(".\n"); } public ContestOutputStream print(boolean[][] value) throws IOException { final int n = value.length, m = value[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) print(value[i][j]); newLine(); } return this; } public ContestOutputStream print(int value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(int value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(long value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(long value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(float value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(float value) throws IOException { return append(String.valueOf(value)).newLine(); } private ContestOutputStream dtos(double x, int n) throws IOException { if (x < 0) { append('-'); x = -x; } x += Math.pow(10, -n) / 2; long longx = (long) x; print(longx); append('.'); x -= longx; for (int i = 0; i < n; i++) { x *= 10; int intx = (int) x; print(intx); x -= intx; } return this; } public ContestOutputStream print(double value) throws IOException { return dtos(value, 20); } public ContestOutputStream println(double value) throws IOException { return dtos(value, 20).newLine(); } public ContestOutputStream print(char value) throws IOException { return append(value); } public ContestOutputStream println(char value) throws IOException { return append(value).newLine(); } public ContestOutputStream print(String value) throws IOException { return append(value); } public ContestOutputStream println(String value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream print(Object value) throws IOException { return append(String.valueOf(value)); } public ContestOutputStream println(Object value) throws IOException { return append(String.valueOf(value)).newLine(); } public ContestOutputStream printYN(boolean yes) throws IOException { if (yes) return println("Yes"); return println("No"); } public ContestOutputStream printAB(boolean yes) throws IOException { if (yes) return println("Alice"); return println("Bob"); } public ContestOutputStream print(CharSequence[] arr) throws IOException { if (arr.length > 0) { append(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').append(arr[i]); } return this; } public ContestOutputStream print(int[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(int[] arr, int length) throws IOException { if (length > 0) print(arr[0]); for (int i = 1; i < length; i++) append('\u0020').print(arr[i]); newLine(); return this; } public ContestOutputStream println(int[] arr) throws IOException { for (int i : arr) print(i).newLine(); return this; } public ContestOutputStream println(int[] arr, int length) throws IOException { for (int i = 0; i < length; i++) println(arr[i]); return this; } public ContestOutputStream print(boolean[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(long[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } newLine(); return this; } public ContestOutputStream print(long[] arr, int length) throws IOException { if (length > 0) print(arr[0]); for (int i = 1; i < length; i++) append('\u0020').print(arr[i]); newLine(); return this; } public ContestOutputStream println(long[] arr, int length) throws IOException { for (int i = 0; i < length; i++) println(arr[i]); return this; } public ContestOutputStream println(long[] arr) throws IOException { for (long i : arr) print(i).newLine(); return this; } public ContestOutputStream print(float[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return this; } public ContestOutputStream println(float[] arr) throws IOException { for (float i : arr) print(i).newLine(); return this; } public ContestOutputStream print(double[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return newLine(); } public ContestOutputStream println(double[] arr) throws IOException { for (double i : arr) print(i).newLine(); return this; } public ContestOutputStream print(Object[] arr) throws IOException { if (arr.length > 0) { print(arr[0]); for (int i = 1; i < arr.length; i++) append('\u0020').print(arr[i]); } return newLine(); } public ContestOutputStream print(java.util.ArrayList<?> arr) throws IOException { if (!arr.isEmpty()) { final int n = arr.size(); print(arr.get(0)); for (int i = 1; i < n; i++) print(" ").print(arr.get(i)); } return newLine(); } public ContestOutputStream println(java.util.ArrayList<?> arr) throws IOException { final int n = arr.size(); for (int i = 0; i < n; i++) println(arr.get(i)); return this; } public ContestOutputStream newLine() throws IOException { return append(System.lineSeparator()); } public ContestOutputStream endl() throws IOException { newLine().flush(); return this; } public ContestOutputStream print(int[][] arr) throws IOException { for (int[] i : arr) print(i); return this; } public ContestOutputStream print(long[][] arr) throws IOException { for (long[] i : arr) print(i); return this; } public ContestOutputStream print(char[][] arr) throws IOException { for (char[] i : arr) print(i); return this; } public ContestOutputStream print(Object[][] arr) throws IOException { for (Object[] i : arr) print(i); return this; } public ContestOutputStream println() throws IOException { return newLine(); } public ContestOutputStream println(Object... arr) throws IOException { for (Object i : arr) print(i); return newLine(); } public ContestOutputStream printToChar(int c) throws IOException { return print((char) c); } }
ConDefects/ConDefects/Code/arc166_d/Java/46387699
condefects-java_data_79
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { int mod = 998244353; long[] fac; public long[] existenceCounting(int n, int k, List<Integer> sequence) { fac = new long[n + 1]; fac[0] = fac[1] = 1L; for (int i = 1; i < fac.length; i++) { fac[i] = fac[i - 1] * i % mod; } long[] ans = new long[n]; List<Long> l1 = new ArrayList<>(); long[] b1 = new long[n + 1]; long[] b2 = new long[n + 2]; boolean[] visited = new boolean[n]; for (int i = 0; i < sequence.size(); i++) { int sup = sequence.get(i); visited[sup - 1] = true; l1.add((sup - 1) * a(n - i - 1, k - i - 1)); long cnt = sup - 1 - get(b1, sup - 1); long v1 = a(n - i - 1, k - i - 1) + ((cnt - 1) * (k - i - 1) % mod) * a(n - i - 2, k - i - 2); v1 %= mod; set(b2, 1, v1); set(b2, sup, -v1); long v2 = ((k - i - 1) * cnt % mod) * a(n - i - 2, k - i - 2); v2 %= mod; set(b2, sup, v2); set(b2, n + 1, -v2); ans[sup - 1] = (ans[sup - 1] + get(b2, sup)) % mod; set(b1, sup, 1); } long c = 1L; for (int i = l1.size() - 1; i >= 0; --i) { ans[sequence.get(i) - 1]= (ans[sequence.get(i) - 1] + c) % mod; c = (c + l1.get(i)) % mod; } for (int i = 0; i < n; ++i) { if (!visited[i]) ans[i] = get(b2, i + 1); } return ans; } public void set(long[] bit, int index, long v) { while (index < bit.length) { bit[index]+=v; index += low(index); } } public long get(long[] bit, int index) { long ans = 0L; while (index > 0) { ans += bit[index]; ans %= mod; index -= low(index); } return ans; } public int low(int x) { return x & (-x); } public long a(int m, int n) { if (n < 0) return 0; if (n == 0) return 1; if (n == 1) return m; long ans = fac[m]; long a = fac[m - n]; long b = mod - 2; while (b > 0) { if (b % 2 == 1) { ans *= a; ans %= mod; } b /= 2; a *= a; a %= mod; } return ans; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int K = scanner.nextInt(); List<Integer> seq = new ArrayList<>(); for (int i = 0; i < K; i++) seq.add(scanner.nextInt()); scanner.close(); for (long v : new Main().existenceCounting(N, K, seq)) { System.out.println(v); } } } import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { int mod = 998244353; long[] fac; public long[] existenceCounting(int n, int k, List<Integer> sequence) { fac = new long[n + 1]; fac[0] = fac[1] = 1L; for (int i = 1; i < fac.length; i++) { fac[i] = fac[i - 1] * i % mod; } long[] ans = new long[n]; List<Long> l1 = new ArrayList<>(); long[] b1 = new long[n + 1]; long[] b2 = new long[n + 2]; boolean[] visited = new boolean[n]; for (int i = 0; i < sequence.size(); i++) { int sup = sequence.get(i); visited[sup - 1] = true; long cnt = sup - 1 - get(b1, sup - 1); l1.add(cnt * a(n - i - 1, k - i - 1)); long v1 = a(n - i - 1, k - i - 1) + ((cnt - 1) * (k - i - 1) % mod) * a(n - i - 2, k - i - 2); v1 %= mod; set(b2, 1, v1); set(b2, sup, -v1); long v2 = ((k - i - 1) * cnt % mod) * a(n - i - 2, k - i - 2); v2 %= mod; set(b2, sup, v2); set(b2, n + 1, -v2); ans[sup - 1] = (ans[sup - 1] + get(b2, sup)) % mod; set(b1, sup, 1); } long c = 1L; for (int i = l1.size() - 1; i >= 0; --i) { ans[sequence.get(i) - 1]= (ans[sequence.get(i) - 1] + c) % mod; c = (c + l1.get(i)) % mod; } for (int i = 0; i < n; ++i) { if (!visited[i]) ans[i] = get(b2, i + 1); } return ans; } public void set(long[] bit, int index, long v) { while (index < bit.length) { bit[index]+=v; index += low(index); } } public long get(long[] bit, int index) { long ans = 0L; while (index > 0) { ans += bit[index]; ans %= mod; index -= low(index); } return ans; } public int low(int x) { return x & (-x); } public long a(int m, int n) { if (n < 0) return 0; if (n == 0) return 1; if (n == 1) return m; long ans = fac[m]; long a = fac[m - n]; long b = mod - 2; while (b > 0) { if (b % 2 == 1) { ans *= a; ans %= mod; } b /= 2; a *= a; a %= mod; } return ans; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int K = scanner.nextInt(); List<Integer> seq = new ArrayList<>(); for (int i = 0; i < K; i++) seq.add(scanner.nextInt()); scanner.close(); for (long v : new Main().existenceCounting(N, K, seq)) { System.out.println(v); } } }
ConDefects/ConDefects/Code/arc174_e/Java/52907696
condefects-java_data_80
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); HashMap<Integer,Long> map = new HashMap<Integer,Long>(); int n = sc.nextInt(); int idx=0; for(int i=0;i<n;i++){ long a =sc.nextLong(); if(a ==1){ map.put(i+1,sc.nextLong()); idx++; }else { int b = sc.nextInt(); System.out.println(map.get(idx-b+1)); } } } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); HashMap<Integer,Long> map = new HashMap<Integer,Long>(); int n = sc.nextInt(); int idx=0; for(int i=0;i<n;i++){ long a =sc.nextLong(); if(a ==1){ map.put(idx+1,sc.nextLong()); idx++; }else { int b = sc.nextInt(); System.out.println(map.get(idx-b+1)); } } } }
ConDefects/ConDefects/Code/abc340_b/Java/53235156
condefects-java_data_81
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int q = sc.nextInt(); int [][] query = new int [q][2]; ArrayList<Integer> list = new ArrayList<>(); for(int i = 0; i < q; i++) { query[i][0] = sc.nextInt(); query[i][1] = sc.nextInt(); } for(int i = 0; i < q; i++) { if(query[i][0] == 1) { list.add(query[list.size()][1]); } else { System.out.println(list.get(list.size() - query[i][1])); } } } } import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int q = sc.nextInt(); int [][] query = new int [q][2]; ArrayList<Integer> list = new ArrayList<>(); for(int i = 0; i < q; i++) { query[i][0] = sc.nextInt(); query[i][1] = sc.nextInt(); } for(int i = 0; i < q; i++) { if(query[i][0] == 1) { list.add(query[i][1]); } else { System.out.println(list.get(list.size() - query[i][1])); } } } }
ConDefects/ConDefects/Code/abc340_b/Java/50495374
condefects-java_data_82
import java.io.PrintWriter; import java.util.Arrays; import java.util.PriorityQueue; public class Main { private static final int INF = Integer.MAX_VALUE / 2; public static void main(String[] args) throws Exception { final FastScanner sc = new FastScanner(System.in); final int n = sc.nextInt(); final long k = sc.nextLong(); final int[][] dist = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dist[i][j] = sc.nextInt(); } } final PrintWriter pw = new PrintWriter(System.out); final int q = sc.nextInt(); for (int cnt = 0; cnt < q; cnt++) { long s = sc.nextLong() - 1; long t = sc.nextLong() - 1; int s_modn = (int) (s % (long) n); int t_modn = (int) (t % (long) n); int[] temp = new int[n]; Arrays.fill(temp, INF); PriorityQueue<IntPair> queue = new PriorityQueue<>(); queue.add(new IntPair(s_modn, 0)); while (!queue.isEmpty()) { IntPair p = queue.poll(); for (int j = 0; j < n; j++) { if (dist[p.second][j] == 1 && temp[j] > p.first + 1) { temp[j] = p.first + 1; queue.add(new IntPair(temp[j], j)); } } } int ans = temp[t_modn]; pw.println(ans == INF ? -1 : ans); } pw.close(); sc.close(); } static class IntPair implements Comparable<IntPair> { int first, second; public IntPair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(IntPair o) { if (first != o.first) { return Integer.compare(first, o.first); } else { return Integer.compare(second, o.second); } } } // FastScannerライブラリ static class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buf = new byte[1024]; private int ptr = 0; private int buflen = 0; FastScanner(java.io.InputStream source) { this.in = source; } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buf); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) { return buf[ptr++]; } else { return -1; } } private boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private boolean isNumeric(int c) { return '0' <= c && c <= '9'; } private void skipToNextPrintableChar() { while (hasNextByte() && !isPrintableChar(buf[ptr])) { ptr++; } } public boolean hasNext() { skipToNextPrintableChar(); return hasNextByte(); } public String next() { if (!hasNext()) { throw new java.util.NoSuchElementException(); } StringBuilder ret = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { ret.appendCodePoint(b); b = readByte(); } return ret.toString(); } public long nextLong() { if (!hasNext()) { throw new java.util.NoSuchElementException(); } long ret = 0; int b = readByte(); boolean negative = false; if (b == '-') { negative = true; if (hasNextByte()) { b = readByte(); } } if (!isNumeric(b)) { throw new NumberFormatException(); } while (true) { if (isNumeric(b)) { ret = ret * 10 + b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return negative ? -ret : ret; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { return (int) nextLong(); } public double nextDouble() { return Double.parseDouble(next()); } @Override public void close() throws Exception { in.close(); } } } import java.io.PrintWriter; import java.util.Arrays; import java.util.PriorityQueue; public class Main { private static final int INF = Integer.MAX_VALUE / 2; public static void main(String[] args) throws Exception { final FastScanner sc = new FastScanner(System.in); final int n = sc.nextInt(); final long k = sc.nextLong(); final int[][] dist = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dist[i][j] = sc.nextInt(); } } final PrintWriter pw = new PrintWriter(System.out); final int q = sc.nextInt(); for (int cnt = 0; cnt < q; cnt++) { long s = sc.nextLong() - 1; long t = sc.nextLong() - 1; int s_modn = (int) (s % (long) n); int t_modn = (int) (t % (long) n); int[] temp = new int[n]; Arrays.fill(temp, INF); PriorityQueue<IntPair> queue = new PriorityQueue<>(); queue.add(new IntPair(0, s_modn)); while (!queue.isEmpty()) { IntPair p = queue.poll(); for (int j = 0; j < n; j++) { if (dist[p.second][j] == 1 && temp[j] > p.first + 1) { temp[j] = p.first + 1; queue.add(new IntPair(temp[j], j)); } } } int ans = temp[t_modn]; pw.println(ans == INF ? -1 : ans); } pw.close(); sc.close(); } static class IntPair implements Comparable<IntPair> { int first, second; public IntPair(int first, int second) { this.first = first; this.second = second; } @Override public int compareTo(IntPair o) { if (first != o.first) { return Integer.compare(first, o.first); } else { return Integer.compare(second, o.second); } } } // FastScannerライブラリ static class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buf = new byte[1024]; private int ptr = 0; private int buflen = 0; FastScanner(java.io.InputStream source) { this.in = source; } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buf); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) { return buf[ptr++]; } else { return -1; } } private boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private boolean isNumeric(int c) { return '0' <= c && c <= '9'; } private void skipToNextPrintableChar() { while (hasNextByte() && !isPrintableChar(buf[ptr])) { ptr++; } } public boolean hasNext() { skipToNextPrintableChar(); return hasNextByte(); } public String next() { if (!hasNext()) { throw new java.util.NoSuchElementException(); } StringBuilder ret = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { ret.appendCodePoint(b); b = readByte(); } return ret.toString(); } public long nextLong() { if (!hasNext()) { throw new java.util.NoSuchElementException(); } long ret = 0; int b = readByte(); boolean negative = false; if (b == '-') { negative = true; if (hasNextByte()) { b = readByte(); } } if (!isNumeric(b)) { throw new NumberFormatException(); } while (true) { if (isNumeric(b)) { ret = ret * 10 + b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return negative ? -ret : ret; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { return (int) nextLong(); } public double nextDouble() { return Double.parseDouble(next()); } @Override public void close() throws Exception { in.close(); } } }
ConDefects/ConDefects/Code/arc159_a/Java/40415617
condefects-java_data_83
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long k = sc.nextLong(); int matrix[][] = new int[n*2][n*2]; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { matrix[i][j] = sc.nextInt() == 1 ? 1 : INF; matrix[n + i][j] = matrix[i][j]; matrix[i][n + j] = matrix[i][j]; matrix[n + i][n + j] = matrix[i][j]; } } long[][] dist = warshallFloyd(matrix); int q = sc.nextInt(); for(int i = 0; i < q; i++) { int from = (int)((sc.nextLong() - 1)%n); int to = (int)((sc.nextLong() - 1)%n); if(dist[from][to] != INF) { System.out.println(dist[from][to]); } else { System.out.println(-1); } } } static int INF = Integer.MAX_VALUE; static long[][] warshallFloyd(int[][] matrix){ int n = matrix.length; long[][] dist = new long[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(i == j){ dist[i][j] = 0; } else { dist[i][j] = matrix[i][j]; } } } for(int k = 0; k < n; k++){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(dist[i][k] == INF || dist[k][j] == INF){ continue; } dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]); } } } return dist; } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long k = sc.nextLong(); int matrix[][] = new int[n*2][n*2]; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { matrix[i][j] = sc.nextInt() == 1 ? 1 : INF; matrix[n + i][j] = matrix[i][j]; matrix[i][n + j] = matrix[i][j]; matrix[n + i][n + j] = matrix[i][j]; } } long[][] dist = warshallFloyd(matrix); int q = sc.nextInt(); for(int i = 0; i < q; i++) { int from = (int)((sc.nextLong() - 1)%n); int to = (int)((sc.nextLong() - 1)%n + n); if(dist[from][to] != INF) { System.out.println(dist[from][to]); } else { System.out.println(-1); } } } static int INF = Integer.MAX_VALUE; static long[][] warshallFloyd(int[][] matrix){ int n = matrix.length; long[][] dist = new long[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(i == j){ dist[i][j] = 0; } else { dist[i][j] = matrix[i][j]; } } } for(int k = 0; k < n; k++){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(dist[i][k] == INF || dist[k][j] == INF){ continue; } dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]); } } } return dist; } }
ConDefects/ConDefects/Code/arc159_a/Java/40530287
condefects-java_data_84
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Deque; import java.util.LinkedList; import java.util.StringTokenizer; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception{ StringTokenizer stringTokenizer = new StringTokenizer(br.readLine()); int n = Integer.valueOf(stringTokenizer.nextToken()); int k = Integer.valueOf(stringTokenizer.nextToken()); int[][] graph = new int[n][n]; for (int i = 0; i < n; i++) { stringTokenizer = new StringTokenizer(br.readLine()); for (int j = 0; j < n; j++) { graph[i][j] = Integer.valueOf(stringTokenizer.nextToken()); } } int q = Integer.valueOf(br.readLine()); for (int i = 0; i < q; i++) { stringTokenizer = new StringTokenizer(br.readLine()); long s = Long.valueOf(stringTokenizer.nextToken()); long t = Long.valueOf(stringTokenizer.nextToken()); boolean[] visited = new boolean[n]; Deque<Integer> deque = new LinkedList<>(); deque.add((int) ((s - 1) % n)); boolean ok = false; int step = 0; while (!ok && !deque.isEmpty()){ int size = deque.size(); step++; for (int j = 0; !ok && j < size; j++) { int poll = deque.poll(); if (visited[poll]){ continue; } visited[poll] = true; for (int l = 0; !ok && l < graph[0].length; l++) { if (graph[poll][l] == 1){ if (l == (t-1)%n){ ok = true; } deque.add(graph[poll][l]); } } } } if (ok) { out.println(step); }else { out.println(-1); } } out.close(); } } import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Deque; import java.util.LinkedList; import java.util.StringTokenizer; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) throws Exception{ StringTokenizer stringTokenizer = new StringTokenizer(br.readLine()); int n = Integer.valueOf(stringTokenizer.nextToken()); int k = Integer.valueOf(stringTokenizer.nextToken()); int[][] graph = new int[n][n]; for (int i = 0; i < n; i++) { stringTokenizer = new StringTokenizer(br.readLine()); for (int j = 0; j < n; j++) { graph[i][j] = Integer.valueOf(stringTokenizer.nextToken()); } } int q = Integer.valueOf(br.readLine()); for (int i = 0; i < q; i++) { stringTokenizer = new StringTokenizer(br.readLine()); long s = Long.valueOf(stringTokenizer.nextToken()); long t = Long.valueOf(stringTokenizer.nextToken()); boolean[] visited = new boolean[n]; Deque<Integer> deque = new LinkedList<>(); deque.add((int) ((s - 1) % n)); boolean ok = false; int step = 0; while (!ok && !deque.isEmpty()){ int size = deque.size(); step++; for (int j = 0; !ok && j < size; j++) { int poll = deque.poll(); if (visited[poll]){ continue; } visited[poll] = true; for (int l = 0; !ok && l < graph[0].length; l++) { if (graph[poll][l] == 1){ if (l == (t-1)%n){ ok = true; } deque.add(l); } } } } if (ok) { out.println(step); }else { out.println(-1); } } out.close(); } }
ConDefects/ConDefects/Code/arc159_a/Java/40416866
condefects-java_data_85
import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { int n = ni(); char[] s = ns(); if (n >= 3 && s[n-1] == 'A' || s[0] == 'B' && s[n-1] == 'B') { System.out.println("Yes"); } else { System.out.println("No"); } } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } } import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { int n = ni(); char[] s = ns(); if (n == 2 && s[0] == s[1] || n >= 3 && s[n-1] == 'A' || s[0] == 'B' && s[n-1] == 'B') { System.out.println("Yes"); } else { System.out.println("No"); } } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } }
ConDefects/ConDefects/Code/arc145_a/Java/33737098
condefects-java_data_86
import java.io.*; import java.util.*; public class Main { static FastIn in = new FastIn(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static final long inf = 0x1fffffffffffffffL; static final int IINF = 0x3fffffff; static final double eps = 1e-9; static int[] dx = {0, 0, 1, -1}; static int[] dy = {1, -1, 0, 0}; //---------------------------------------------------------------------------------------------------------------------------------------------------------------------- void solve() { int n = in.nextInt(); String s = in.next(); if (n <= 2) { if (s.charAt(0) != s.charAt(n - 1)) out.println("No"); else out.println("Yes"); return; } if (s.charAt(0) == 'B' || s.charAt(n - 1) == 'A') out.println("Yes"); out.println("No"); } int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } long exGcd(long a, long b, int[] x, int[] y) { if (b == 0) { x[0] = 1; y[0] = 0; return a; } else { long d = exGcd(b, Math.floorMod(a, b), y, x); y[0] -= a / b * x[0]; return d; } } static long qmi(long a, long b, long mod) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public static void main(String... args) { new Main().solve(); out.close(); } } class FastIn extends In { private final BufferedInputStream reader = new BufferedInputStream(System.in); private final byte[] buffer = new byte[0x10000]; private int i = 0; private int length = 0; public int read() { if (i == length) { i = 0; try { length = reader.read(buffer); } catch (IOException ignored) { } if (length == -1) { return 0; } } if (length <= i) { throw new RuntimeException(); } return buffer[i++]; } String next() { StringBuilder builder = new StringBuilder(); int b = read(); while (b < '!' || '~' < b) { b = read(); } while ('!' <= b && b <= '~') { builder.appendCodePoint(b); b = read(); } return builder.toString(); } String nextLine() { StringBuilder builder = new StringBuilder(); int b = read(); while (b != 0 && b != '\r' && b != '\n') { builder.appendCodePoint(b); b = read(); } if (b == '\r') { read(); } return builder.toString(); } int nextInt() { long val = nextLong(); if ((int) val != val) { throw new NumberFormatException(); } return (int) val; } long nextLong() { int b = read(); while (b < '!' || '~' < b) { b = read(); } boolean neg = false; if (b == '-') { neg = true; b = read(); } long n = 0; int c = 0; while ('0' <= b && b <= '9') { n = n * 10 + b - '0'; b = read(); c++; } if (c == 0 || c >= 2 && n == 0) { throw new NumberFormatException(); } return neg ? -n : n; } } class In { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000); private StringTokenizer tokenizer; String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException ignored) { } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } import java.io.*; import java.util.*; public class Main { static FastIn in = new FastIn(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static final long inf = 0x1fffffffffffffffL; static final int IINF = 0x3fffffff; static final double eps = 1e-9; static int[] dx = {0, 0, 1, -1}; static int[] dy = {1, -1, 0, 0}; //---------------------------------------------------------------------------------------------------------------------------------------------------------------------- void solve() { int n = in.nextInt(); String s = in.next(); if (n <= 2) { if (s.charAt(0) != s.charAt(n - 1)) out.println("No"); else out.println("Yes"); return; } if (s.charAt(0) == 'B' || s.charAt(n - 1) == 'A') out.println("Yes"); else out.println("No"); } int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } long exGcd(long a, long b, int[] x, int[] y) { if (b == 0) { x[0] = 1; y[0] = 0; return a; } else { long d = exGcd(b, Math.floorMod(a, b), y, x); y[0] -= a / b * x[0]; return d; } } static long qmi(long a, long b, long mod) { long res = 1; while (b != 0) { if ((b & 1) == 1) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public static void main(String... args) { new Main().solve(); out.close(); } } class FastIn extends In { private final BufferedInputStream reader = new BufferedInputStream(System.in); private final byte[] buffer = new byte[0x10000]; private int i = 0; private int length = 0; public int read() { if (i == length) { i = 0; try { length = reader.read(buffer); } catch (IOException ignored) { } if (length == -1) { return 0; } } if (length <= i) { throw new RuntimeException(); } return buffer[i++]; } String next() { StringBuilder builder = new StringBuilder(); int b = read(); while (b < '!' || '~' < b) { b = read(); } while ('!' <= b && b <= '~') { builder.appendCodePoint(b); b = read(); } return builder.toString(); } String nextLine() { StringBuilder builder = new StringBuilder(); int b = read(); while (b != 0 && b != '\r' && b != '\n') { builder.appendCodePoint(b); b = read(); } if (b == '\r') { read(); } return builder.toString(); } int nextInt() { long val = nextLong(); if ((int) val != val) { throw new NumberFormatException(); } return (int) val; } long nextLong() { int b = read(); while (b < '!' || '~' < b) { b = read(); } boolean neg = false; if (b == '-') { neg = true; b = read(); } long n = 0; int c = 0; while ('0' <= b && b <= '9') { n = n * 10 + b - '0'; b = read(); c++; } if (c == 0 || c >= 2 && n == 0) { throw new NumberFormatException(); } return neg ? -n : n; } } class In { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000); private StringTokenizer tokenizer; String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException ignored) { } return tokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } }
ConDefects/ConDefects/Code/arc145_a/Java/39389139
condefects-java_data_87
import java.io.*; import java.util.*; import java.math.*; public class Main{ static Kattio k = new Kattio(); public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = k.nextInt(); StringBuilder ss = new StringBuilder(k.next()); if(ss.length() <= 1) { k.println("Yes");k.close(); System.exit(0); } int q = 0,w = ss.length()-1; boolean bo = true; while(q < w) { if(ss.charAt(q) != ss.charAt(w)) { bo = false; break; } q++;w--; } if(bo) { k.println("Yes");k.close(); System.exit(0); } if(ss.charAt(0) == 'B' || (ss.charAt(0) == 'A' && ss.charAt(ss.length()-1) == 'A')) { k.println("Yes");k.close(); System.exit(0); } k.println("No"); k.close(); } } class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } public Kattio(String intput, String output) throws IOException { super(output); r = new BufferedReader(new FileReader(intput)); } public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } } import java.io.*; import java.util.*; import java.math.*; public class Main{ static Kattio k = new Kattio(); public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = k.nextInt(); StringBuilder ss = new StringBuilder(k.next()); if(ss.length() <= 1) { k.println("Yes");k.close(); System.exit(0); } int q = 0,w = ss.length()-1; boolean bo = true; while(q < w) { if(ss.charAt(q) != ss.charAt(w)) { bo = false; break; } q++;w--; } if(bo) { k.println("Yes");k.close(); System.exit(0); } if((ss.charAt(0) == 'B' && ss.length() > 2) || (ss.charAt(0) == 'A' && ss.charAt(ss.length()-1) == 'A')) { k.println("Yes");k.close(); System.exit(0); } k.println("No"); k.close(); } } class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } public Kattio(String intput, String output) throws IOException { super(output); r = new BufferedReader(new FileReader(intput)); } public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } }
ConDefects/ConDefects/Code/arc145_a/Java/38329474
condefects-java_data_88
import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String S = sc.next(); char[] S2 = S.toCharArray(); //一致するか int check = 0; if (S2[0]=='A'&&S2[N-1]=='B')check=1; if (S2[0]=='B'&&S2[1]=='A')check=1; if(check==1) { System.out.println("No"); } else { System.out.println("Yes"); } //できる操作:AAをABに変える=AをBに変える(左端以外可)orBAをABに変える=BをAに変える、AをBに変えるorBBをABに変える=BをAに変える(右は以外可) } } import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String S = sc.next(); char[] S2 = S.toCharArray(); //一致するか int check = 0; if (S2[0]=='A'&&S2[N-1]=='B')check=1; if (N==2&&S2[0]=='B'&&S2[1]=='A')check=1; if(check==1) { System.out.println("No"); } else { System.out.println("Yes"); } //できる操作:AAをABに変える=AをBに変える(左端以外可)orBAをABに変える=BをAに変える、AをBに変えるorBBをABに変える=BをAに変える(右は以外可) } }
ConDefects/ConDefects/Code/arc145_a/Java/33778122
condefects-java_data_89
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = Integer.valueOf(scan.next()); String S = scan.next(); if (N <= 2) { if (scan.equals("BA") || scan.equals("AB")) { System.out.println("No"); } else { System.out.println("Yes"); } return; } if (S.charAt(0) == 'A' && S.charAt(N - 1) == 'B') { System.out.println("No"); return; } System.out.println("Yes"); } } import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = Integer.valueOf(scan.next()); String S = scan.next(); if (N <= 2) { if (S.equals("BA") || S.equals("AB")) { System.out.println("No"); } else { System.out.println("Yes"); } return; } if (S.charAt(0) == 'A' && S.charAt(N - 1) == 'B') { System.out.println("No"); return; } System.out.println("Yes"); } }
ConDefects/ConDefects/Code/arc145_a/Java/38328966
condefects-java_data_90
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); final int n = Integer.parseInt(br.readLine()); final String s = br.readLine(); br.close(); if (s.charAt(0) == 'A' && s.charAt(n - 1) == 'B') { System.out.println("No"); } else { System.out.println("Yes"); } } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); final int n = Integer.parseInt(br.readLine()); final String s = br.readLine(); br.close(); if ((s.charAt(0) == 'A' && s.charAt(n - 1) == 'B') || (n == 2 && s.charAt(0) != s.charAt(1))) { System.out.println("No"); } else { System.out.println("Yes"); } } }
ConDefects/ConDefects/Code/arc145_a/Java/39559624
condefects-java_data_91
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int b = sc.nextInt(); int g = sc.nextInt(); if(b>g) { System.out.println("Bat"); }else { System.out.println("Globe"); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int b = sc.nextInt(); int g = sc.nextInt(); if(b>g) { System.out.println("Bat"); }else { System.out.println("Glove"); } } }
ConDefects/ConDefects/Code/abc334_a/Java/52681354
condefects-java_data_92
// javac -d build src/*.java; java -cp build Main < in.txt > out.txt import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; record Card(Integer index,Integer a, Integer b){} public class Main{ public static void main(String... args){ Scanner scanner = new Scanner(System.in); System.out.println( scanner.nextInt()<scanner.nextInt() ? "Bat":"Glove" ); } } // javac -d build src/*.java; java -cp build Main < in.txt > out.txt import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; record Card(Integer index,Integer a, Integer b){} public class Main{ public static void main(String... args){ Scanner scanner = new Scanner(System.in); System.out.println( scanner.nextInt()<scanner.nextInt() ? "Glove":"Bat" ); } }
ConDefects/ConDefects/Code/abc334_a/Java/54203367
condefects-java_data_93
import java.util.Scanner; public class Main { public static void main(String[] args){ var sc = new Scanner(System.in); var A = sc.nextInt(); var B = sc.nextInt(); System.out.println(A>B?"bat":"glove"); } } import java.util.Scanner; public class Main { public static void main(String[] args){ var sc = new Scanner(System.in); var A = sc.nextInt(); var B = sc.nextInt(); System.out.println(A>B?"Bat":"Glove"); } }
ConDefects/ConDefects/Code/abc334_a/Java/52193948
condefects-java_data_94
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; public class Main { static final long MOD1=1000000007; static final long MOD=998244353; static int[] ans; public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int t = sc.nextInt(); long[] dp = new long[100001]; long[] sum = new long[100001]; dp[1] = 1; sum[1] = 1; for (int i = 2; i < sum.length; i++) { int from = sqrt_floor(i); dp[i] = sum[from]; sum[i] = sum[i-1] + dp[i]; } for (int i = 0; i < t; i++) { long X = sc.nextLong(); if (X <= 100000) { out.println(dp[(int)X]); }else { long sq = sqrt_floor(X); long ans = 0; for (long j = 1; j <= 100000; j++) { long r = Math.min(sq, (j+1)*(j+1)-1); long l =j*j; ans += Math.max(0, (r-l+1))*sum[(int) j]; } out.println(ans); } } out.flush(); } static int sqrt_floor(int n) { int x = (int) Math.sqrt(n); int ans = 0; for (int i = -1; i <= 1; i++) { x = x+i; if(x*x<=n) ans=x; } return ans; } static long sqrt_floor(long n) { long x = (long) Math.sqrt(n); long ans = 0; for (long i = -10; i <= 10; i++) { x = x+i; if(x*x<=n) ans=x; } return ans; } static long solve(int n, long k, int[] a) { long[] ten = new long[19]; ten[0] = 1; for (int i = 1; i < ten.length; i++) { ten[i] = ten[i-1]*10; } long ans = 0; for (int i = 0; i < a.length; i++) { int next = 18; if(i+1<n) next = a[i+1]; long sum = Math.min(k, ten[next - a[i]]-1); k -= sum; ans += sum * ten[a[i]]; } return ans; } static class Binomial{ int MAX = 510000;//ほしいサイズ long[] fac=new long[MAX]; long[] finv=new long[MAX]; long[] inv=new long[MAX]; public Binomial(){ fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[(int) (MOD%i)] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; }//facがx!、finvがx!の逆元,10^7くらいまでのテーブル(MAXまで) } long nCk(int n,int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } long fac(int n) { return fac[n]; } long fac_inv(int n) { return finv[n]; } } 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 double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); 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; } } } import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; public class Main { static final long MOD1=1000000007; static final long MOD=998244353; static int[] ans; public static void main(String[] args){ PrintWriter out = new PrintWriter(System.out); InputReader sc=new InputReader(System.in); int t = sc.nextInt(); long[] dp = new long[100001]; long[] sum = new long[100001]; dp[1] = 1; sum[1] = 1; for (int i = 2; i < sum.length; i++) { int from = sqrt_floor(i); dp[i] = sum[from]; sum[i] = sum[i-1] + dp[i]; } for (int i = 0; i < t; i++) { long X = sc.nextLong(); if (X <= 100000) { out.println(dp[(int)X]); }else { long sq = sqrt_floor(X); long ans = 0; for (long j = 1; j <= 100000; j++) { long r = Math.min(sq, (j+1)*(j+1)-1); long l =j*j; ans += Math.max(0, (r-l+1))*sum[(int) j]; } out.println(ans); } } out.flush(); } static int sqrt_floor(int n) { int x = (int) Math.sqrt(n); int ans = 0; for (int i = -1; i <= 1; i++) { x = x+i; if(x*x<=n) ans=x; } return ans; } static long sqrt_floor(long n) { long x = (long) Math.sqrt(n); long ans = 0; for (long i = -1; i <= 10; i++) { x = x+i; if(x*x<=n) ans=x; } return ans; } static long solve(int n, long k, int[] a) { long[] ten = new long[19]; ten[0] = 1; for (int i = 1; i < ten.length; i++) { ten[i] = ten[i-1]*10; } long ans = 0; for (int i = 0; i < a.length; i++) { int next = 18; if(i+1<n) next = a[i+1]; long sum = Math.min(k, ten[next - a[i]]-1); k -= sum; ans += sum * ten[a[i]]; } return ans; } static class Binomial{ int MAX = 510000;//ほしいサイズ long[] fac=new long[MAX]; long[] finv=new long[MAX]; long[] inv=new long[MAX]; public Binomial(){ fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[(int) (MOD%i)] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; }//facがx!、finvがx!の逆元,10^7くらいまでのテーブル(MAXまで) } long nCk(int n,int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } long fac(int n) { return fac[n]; } long fac_inv(int n) { return finv[n]; } } 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 double[] nextDoubleArray(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); 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; } } }
ConDefects/ConDefects/Code/abc243_g/Java/30178840
condefects-java_data_95
import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { int t = ni(); for (int i = 0; i < t; i++) { long x = nl(); out.println(solve(x)); } } private static long solve(long x) { if (x == 1) { return 1; } long n1 = sqrt(x); int n = (int)sqrt(n1); int m = 6; long[][] dp = new long[m][n + 1]; // for (int i = 2; i <= n1; i++) { // for (int j = 2; j * j <= i; j ++) { // dp[0][j] ++; // } // } for (int j = 2; j <= n; j ++) { dp[0][j] = Math.max(0, n1 - j * j + 1); } long ret = n1; for (int i = 1; i < m; i ++) { for (int j = 2; j <= n; j ++) { for (long k = 1L * j * j; k <= n; k ++) { dp[i][j] += dp[i-1][(int)k]; } } } for (int i = 0; i < m; i ++) { for (int j = 0; j <= n; j ++) { ret += dp[i][j]; } } return ret; } private static long sqrt(long x) { long ok = 1; long ng = 3000000001L; while (Math.abs(ok - ng) > 1) { long k = (ok + ng) / 2; if (k * k <= x) { ok = k; } else { ng = k; } } return ok; } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } } import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { int t = ni(); for (int i = 0; i < t; i++) { long x = nl(); out.println(solve(x)); } } private static long solve(long x) { if (x == 1) { return 1; } long n1 = sqrt(x); int n = (int)sqrt(n1); int m = 6; long[][] dp = new long[m][n + 1]; // for (int i = 2; i <= n1; i++) { // for (int j = 2; j * j <= i; j ++) { // dp[0][j] ++; // } // } for (int j = 2; j <= n; j ++) { dp[0][j] = Math.max(0, n1 - 1L * j * j + 1); } long ret = n1; for (int i = 1; i < m; i ++) { for (int j = 2; j <= n; j ++) { for (long k = 1L * j * j; k <= n; k ++) { dp[i][j] += dp[i-1][(int)k]; } } } for (int i = 0; i < m; i ++) { for (int j = 0; j <= n; j ++) { ret += dp[i][j]; } } return ret; } private static long sqrt(long x) { long ok = 1; long ng = 3000000001L; while (Math.abs(ok - ng) > 1) { long k = (ok + ng) / 2; if (k * k <= x) { ok = k; } else { ng = k; } } return ok; } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } }
ConDefects/ConDefects/Code/abc243_g/Java/30105001
condefects-java_data_96
import java.util.*; import java.util.regex.*; import java.io.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); String s = sc.next(); int q = Integer.parseInt(sc.next()); String t = ""; for (int i = 0; i < 26; i++) { t += (char)(97 + i); } StringBuilder sb1 = new StringBuilder(t); for (int i = 0; i < q; i++) { char c = sc.next().charAt(0); char d = sc.next().charAt(0); for (int j = 0; j < 26; j++) { if (t.charAt(j) == c) { sb1.setCharAt(j, d); } } } t = sb1.toString(); StringBuilder sb2 = new StringBuilder(s); for (int i = 0; i < n; i++) { int j = (int)(s.charAt(i) - 'a'); sb2.setCharAt(i, t.charAt(j)); } s = sb2.toString(); PrintWriter pw = new PrintWriter(System.out); pw.println(s); pw.flush(); } } import java.util.*; import java.util.regex.*; import java.io.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); String s = sc.next(); int q = Integer.parseInt(sc.next()); String t = ""; for (int i = 0; i < 26; i++) { t += (char)(97 + i); } StringBuilder sb1 = new StringBuilder(t); for (int i = 0; i < q; i++) { char c = sc.next().charAt(0); char d = sc.next().charAt(0); for (int j = 0; j < 26; j++) { if (t.charAt(j) == c) { sb1.setCharAt(j, d); t = sb1.toString(); } } } StringBuilder sb2 = new StringBuilder(s); for (int i = 0; i < n; i++) { int j = (int)(s.charAt(i) - 'a'); sb2.setCharAt(i, t.charAt(j)); } s = sb2.toString(); PrintWriter pw = new PrintWriter(System.out); pw.println(s); pw.flush(); } }
ConDefects/ConDefects/Code/abc342_c/Java/54143244
condefects-java_data_97
import java.util.*; public class Main{ static String s,t,t1; static char c,d; static int n; static StringBuilder ans; public static void main(String[]args){ Scanner sc=new Scanner(System.in); n=sc.nextInt(); s=sc.next(); char a[]=s.toCharArray(); int q=sc.nextInt(); char change[]=new char[26]; for(int i=0;i<26;i++){ change[i]=(char)(i+'a'); } while(q-->0){ t=sc.next(); t1=sc.next(); c=t.charAt(0); d=t1.charAt(0); for(int i=0;i<26;i++){ if(c-'a'==i)change[i]=d; } } for(int i=0;i<n;i++){ System.out.print(change[a[i]-'a']); } } } import java.util.*; public class Main{ static String s,t,t1; static char c,d; static int n; static StringBuilder ans; public static void main(String[]args){ Scanner sc=new Scanner(System.in); n=sc.nextInt(); s=sc.next(); char a[]=s.toCharArray(); int q=sc.nextInt(); char change[]=new char[26]; for(int i=0;i<26;i++){ change[i]=(char)(i+'a'); } while(q-->0){ t=sc.next(); t1=sc.next(); c=t.charAt(0); d=t1.charAt(0); for(int i=0;i<26;i++){ if(change[i]==c)change[i]=d; } } for(int i=0;i<n;i++){ System.out.print(change[a[i]-'a']); } } }
ConDefects/ConDefects/Code/abc342_c/Java/51669136
condefects-java_data_98
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); if(N>M+2){ System.out.println("Yes"); System.exit(0); } int[] numA = new int[N]; int[] numB = new int[M]; int[] numC = new int[N + M]; for (int i = 0; i < N; i++) { numA[i] = sc.nextInt(); } for (int j = 0; j < M; j++) { numB[j] = sc.nextInt(); } for (int k = 0; k < N + M; k++) { if (k < N) { numC[k] = numA[k]; } else { numC[k] = numB[k - N]; } } sort(numC); boolean judgA = false; boolean judg = false; loopC: for (int i = 0; i < numC.length - 1; i++) { for (int a : numA) { if (numC[i] == a) { if (judgA == true) { judg = true; break loopC; } else { judgA = true; continue loopC; } } } judgA = false; } if (judg) { System.out.println("Yes"); } else { System.out.println("No"); } } public static void sort(int[] a) { for (int i = a.length - 1; i >= 1; i--) { for (int j = 0; j < i; j++) { if (a[j] > a[j + 1]) { int w = a[j]; a[j] = a[j + 1]; a[j + 1] = w; } } } } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int M = sc.nextInt(); if(N>M+2){ System.out.println("Yes"); System.exit(0); } int[] numA = new int[N]; int[] numB = new int[M]; int[] numC = new int[N + M]; for (int i = 0; i < N; i++) { numA[i] = sc.nextInt(); } for (int j = 0; j < M; j++) { numB[j] = sc.nextInt(); } for (int k = 0; k < N + M; k++) { if (k < N) { numC[k] = numA[k]; } else { numC[k] = numB[k - N]; } } sort(numC); boolean judgA = false; boolean judg = false; loopC: for (int i = 0; i < numC.length; i++) { for (int a : numA) { if (numC[i] == a) { if (judgA == true) { judg = true; break loopC; } else { judgA = true; continue loopC; } } } judgA = false; } if (judg) { System.out.println("Yes"); } else { System.out.println("No"); } } public static void sort(int[] a) { for (int i = a.length - 1; i >= 1; i--) { for (int j = 0; j < i; j++) { if (a[j] > a[j + 1]) { int w = a[j]; a[j] = a[j + 1]; a[j + 1] = w; } } } } }
ConDefects/ConDefects/Code/abc355_b/Java/54217895
condefects-java_data_99
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); new Main(sc); } Integer n; Integer m; Map<Integer, String> p = new TreeMap<Integer, String>(); public Main(Scanner sc) { // 入力 n = sc.nextInt(); m = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i++) { p.put(sc.nextInt(), "a"); } sc.nextLine(); for (int i = 0; i < m; i++) { p.put(sc.nextInt(), "b"); } // System.out.println(p); String pre = ""; for (String pi : p.values()) { if (pre.equals(pi)) { System.out.println("Yes"); return; } pre = pi; } System.out.println("No"); } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); new Main(sc); } Integer n; Integer m; Map<Integer, String> p = new TreeMap<Integer, String>(); public Main(Scanner sc) { // 入力 n = sc.nextInt(); m = sc.nextInt(); sc.nextLine(); for (int i = 0; i < n; i++) { p.put(sc.nextInt(), "a"); } sc.nextLine(); for (int i = 0; i < m; i++) { p.put(sc.nextInt(), "b"); } // System.out.println(p); String pre = ""; for (String pi : p.values()) { if (pre.equals("a") && pi.equals("a")) { System.out.println("Yes"); return; } pre = pi; } System.out.println("No"); } }
ConDefects/ConDefects/Code/abc355_b/Java/54379223
condefects-java_data_100
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); List<Integer> A = new ArrayList<>(); List<Integer> B = new ArrayList<>(); List<Integer> C = new ArrayList<>(); for (int i = 0; i < N; i++) A.add(sc.nextInt()); for (int i = 0; i < M; i++) B.add(sc.nextInt()); C.addAll(A); C.addAll(B); C.sort(Comparator.naturalOrder()); for (int i = 0; i < C.size() - 1; i++) { boolean b1 = A.contains(C.get(i)); boolean b2 = A.contains(C.get(i) + 1); if (b1 && b2) { System.out.println("Yes"); return; } } System.out.println("No"); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); List<Integer> A = new ArrayList<>(); List<Integer> B = new ArrayList<>(); List<Integer> C = new ArrayList<>(); for (int i = 0; i < N; i++) A.add(sc.nextInt()); for (int i = 0; i < M; i++) B.add(sc.nextInt()); C.addAll(A); C.addAll(B); C.sort(Comparator.naturalOrder()); for (int i = 0; i < C.size() - 1; i++) { boolean b1 = A.contains(C.get(i)); boolean b2 = A.contains(C.get(i + 1)); if (b1 && b2) { System.out.println("Yes"); return; } } System.out.println("No"); } }
ConDefects/ConDefects/Code/abc355_b/Java/54416801