id
stringlengths
22
25
content
stringlengths
327
628k
max_stars_repo_path
stringlengths
49
49
condefects-java_data_1301
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String[] S= new String[N]; int[] T= new int[N]; HashSet<String> set = new HashSet<>(); int best = -1; int bestscore = -1; for(int i = 0; i < N;i++){ S[i] = sc.next(); T[i] = sc.nextInt(); } for(int i = 0; i<N;i++){ if(!set.contains(S[i])){ if(T[i] > bestscore){ best = i; bestscore = T[i]; set.add(S[i]); } } } System.out.println(best+1); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String[] S= new String[N]; int[] T= new int[N]; HashSet<String> set = new HashSet<>(); int best = -1; int bestscore = -1; for(int i = 0; i < N;i++){ S[i] = sc.next(); T[i] = sc.nextInt(); } for(int i = 0; i<N;i++){ if(!set.contains(S[i])){ if(T[i] > bestscore){ best = i; bestscore = T[i]; } set.add(S[i]); } } System.out.println(best+1); } }
ConDefects/ConDefects/Code/abc251_c/Java/32852028
condefects-java_data_1302
import java.util.*; public class Main { public static void main(String args[]) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int k = input.nextInt(); //Map<String, Integer> map = new TreeMap<String, Integer>(); TreeSet<String> set = new TreeSet<String>(); for(int i=0;i<k;i++) { String temp = input.next(); set.add(temp); } int cnt = 0; Iterator<String> iterator = set.iterator(); while(iterator.hasNext() && cnt < 3) { System.out.println(iterator.next()); cnt++; } } } import java.util.*; public class Main { public static void main(String args[]) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int k = input.nextInt(); //Map<String, Integer> map = new TreeMap<String, Integer>(); TreeSet<String> set = new TreeSet<String>(); for(int i=0;i<k;i++) { String temp = input.next(); set.add(temp); } int cnt = 0; Iterator<String> iterator = set.iterator(); while(iterator.hasNext() && cnt < k) { System.out.println(iterator.next()); cnt++; } } }
ConDefects/ConDefects/Code/abc288_b/Java/39074847
condefects-java_data_1303
//Har Har Mahadev import java.io.*; import java.util.*; public class Main { public static void main(String[]args)throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); String[]inp=buf.readLine().split(" "); int n=Integer.parseInt(inp[0]); int k=Integer.parseInt(inp[1]); PriorityQueue<String>pq=new PriorityQueue<>((a,b)->b.compareTo(a)); while(n-->0) { String s=buf.readLine(); pq.add(s); if(pq.size()>k)pq.poll(); } ArrayList<String>al=new ArrayList<>(); while(!pq.isEmpty())al.add(0,pq.poll()); for(int i=0;i<al.size();i++)System.out.println(al.get(i)); } } //Har Har Mahadev import java.io.*; import java.util.*; public class Main { public static void main(String[]args)throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); String[]inp=buf.readLine().split(" "); int n=Integer.parseInt(inp[0]); int k=Integer.parseInt(inp[1]); PriorityQueue<String>pq=new PriorityQueue<>((a,b)->b.compareTo(a)); while(n-->0) { String s=buf.readLine(); if(pq.size()<k) pq.add(s); } ArrayList<String>al=new ArrayList<>(); while(!pq.isEmpty())al.add(0,pq.poll()); for(int i=0;i<al.size();i++)System.out.println(al.get(i)); } }
ConDefects/ConDefects/Code/abc288_b/Java/39830576
condefects-java_data_1304
/** * F - Double Sum */ import java.util.*; class Main { public static void main(String[] arg) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } AVLTree tree = new AVLTree(); long s = 0; for (int i = n - 1; i >= 0; i--) { tree.add(a[i]); int ti = tree.total_right(a[i]); // a[i]より大きい要素の個数 long si = tree.sum_right(a[i]); // a[i]より大きい要素の総和 s += si - a[i] * ti; } System.out.println(s); } } class AVLTree { class Node { Node left = null; Node right = null; long key; int height = 1; int count = 1; // # of elements with the same key int total = 1; // # of elements in the subtree long sum; // sum of elements in the subtree Node(int x) { this.key = x; this.sum = x; } } static int height(Node node) { return node == null ? 0 : node.height; } static int total(Node node) { return node == null ? 0 : node.total; } static long sum(Node node) { return node == null ? 0 : node.sum; } static int max(int a, int b) { return a > b ? a : b; } Node root = null; void add(int x) { root = insert(root, new Node(x)); } private Node insert(Node pos, Node node) { if (pos == null) { return node; } pos.total += node.total; pos.sum += node.sum; if (node.key == pos.key) { pos.count += node.count; return pos; } else if (node.key < pos.key) { pos.left = insert(pos.left, node); } else { pos.right = insert(pos.right, node); } int lh = height(pos.left); int rh = height(pos.right); pos.height = 1 + max(lh, rh); int balance = lh - rh; if (balance > 1) { if (node.key > pos.left.key) { pos.left = left_rotate(pos.left); } return right_rotate(pos); } else if (balance < -1) { if (node.key < pos.right.key) { pos.right = right_rotate(pos.right); } return left_rotate(pos); } else { return pos; } } private Node left_rotate(Node pos) { Node y = pos.right; pos.right = y.left; y.left = pos; pos.height = 1 + max(height(pos.left), height(pos.right)); y.height = 1 + max(height(pos), height(y.right)); pos.total = pos.count + total(pos.left) + total(pos.right); y.total = y.count + total(pos) + total(y.right); pos.sum = pos.key * pos.count + sum(pos.left) + sum(pos.right); y.sum = y.key * y.count + sum(pos) + sum(y.right); return y; } private Node right_rotate(Node pos) { Node x = pos.left; pos.left = x.right; x.right = pos; pos.height = 1 + max(height(pos.left), height(pos.right)); x.height = 1 + max(height(pos), height(x.left)); pos.total = pos.count + total(pos.left) + total(pos.right); x.total = x.count + total(pos) + total(x.left); pos.sum = pos.key * pos.count + sum(pos.left) + sum(pos.right); x.sum = x.key * x.count + sum(pos) + sum(x.left); return x; } /** Return the number of elements > x. */ int total_right(int x) { return total_right(root, x); } private int total_right(Node pos, int x) { if (pos == null) { return 0; } else if (pos.key > x) { int t = total_right(pos.left, x); t += pos.count + total(pos.right); return t; } else { return total_right(pos.right, x); } } /** Return the sum of elements > x. */ long sum_right(int x) { return sum_right(root, x); } private long sum_right(Node pos, int x) { if (pos == null) { return 0; } else if (pos.key > x) { long s = sum_right(pos.left, x); s += pos.count * pos.key + sum(pos.right); return s; } else { return sum_right(pos.right, x); } } } /** * F - Double Sum */ import java.util.*; class Main { public static void main(String[] arg) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scanner.nextInt(); } AVLTree tree = new AVLTree(); long s = 0; for (int i = n - 1; i >= 0; i--) { tree.add(a[i]); int ti = tree.total_right(a[i]); // a[i]より大きい要素の個数 long si = tree.sum_right(a[i]); // a[i]より大きい要素の総和 s += si - (long)a[i] * ti; } System.out.println(s); } } class AVLTree { class Node { Node left = null; Node right = null; long key; int height = 1; int count = 1; // # of elements with the same key int total = 1; // # of elements in the subtree long sum; // sum of elements in the subtree Node(int x) { this.key = x; this.sum = x; } } static int height(Node node) { return node == null ? 0 : node.height; } static int total(Node node) { return node == null ? 0 : node.total; } static long sum(Node node) { return node == null ? 0 : node.sum; } static int max(int a, int b) { return a > b ? a : b; } Node root = null; void add(int x) { root = insert(root, new Node(x)); } private Node insert(Node pos, Node node) { if (pos == null) { return node; } pos.total += node.total; pos.sum += node.sum; if (node.key == pos.key) { pos.count += node.count; return pos; } else if (node.key < pos.key) { pos.left = insert(pos.left, node); } else { pos.right = insert(pos.right, node); } int lh = height(pos.left); int rh = height(pos.right); pos.height = 1 + max(lh, rh); int balance = lh - rh; if (balance > 1) { if (node.key > pos.left.key) { pos.left = left_rotate(pos.left); } return right_rotate(pos); } else if (balance < -1) { if (node.key < pos.right.key) { pos.right = right_rotate(pos.right); } return left_rotate(pos); } else { return pos; } } private Node left_rotate(Node pos) { Node y = pos.right; pos.right = y.left; y.left = pos; pos.height = 1 + max(height(pos.left), height(pos.right)); y.height = 1 + max(height(pos), height(y.right)); pos.total = pos.count + total(pos.left) + total(pos.right); y.total = y.count + total(pos) + total(y.right); pos.sum = pos.key * pos.count + sum(pos.left) + sum(pos.right); y.sum = y.key * y.count + sum(pos) + sum(y.right); return y; } private Node right_rotate(Node pos) { Node x = pos.left; pos.left = x.right; x.right = pos; pos.height = 1 + max(height(pos.left), height(pos.right)); x.height = 1 + max(height(pos), height(x.left)); pos.total = pos.count + total(pos.left) + total(pos.right); x.total = x.count + total(pos) + total(x.left); pos.sum = pos.key * pos.count + sum(pos.left) + sum(pos.right); x.sum = x.key * x.count + sum(pos) + sum(x.left); return x; } /** Return the number of elements > x. */ int total_right(int x) { return total_right(root, x); } private int total_right(Node pos, int x) { if (pos == null) { return 0; } else if (pos.key > x) { int t = total_right(pos.left, x); t += pos.count + total(pos.right); return t; } else { return total_right(pos.right, x); } } /** Return the sum of elements > x. */ long sum_right(int x) { return sum_right(root, x); } private long sum_right(Node pos, int x) { if (pos == null) { return 0; } else if (pos.key > x) { long s = sum_right(pos.left, x); s += pos.count * pos.key + sum(pos.right); return s; } else { return sum_right(pos.right, x); } } }
ConDefects/ConDefects/Code/abc351_f/Java/52940927
condefects-java_data_1305
//package atcoder.abc351; import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); //solve(in.nextInt()); solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; 6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial. If getting stuck, skip it and solve other similar level problems. Wait for 1 week then try to solve again. Only read editorial after you solved a problem. 7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already. 8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you rushed to coding! */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a thorough idea steps! 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? 5. Any chance you got WA due to integer overflow, especially if you are dealing with all subarrays. The sum can get deceptively large! If in doubt, just use long instead of int. */ static int n; static Integer[] a; static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { n = in.nextInt(); a = in.nextIntArray(n); long ans = 0; //for each a[i], get the number of index j < i, s.t a[j] < a[i], call it C //the contribution of a[i] is C * a[i] - the sum of all such a[j] //C is easy to find out, a compressed fenwick tree works //but how do you find all the sum of all such a[j] < a[i], j < i, use another //fenwick tree, except that this fenwick tree adds individual a[i] instead of cnt 1 Integer[] b = Arrays.copyOf(a, n); Map<Integer, Integer> map = compress(b); FenwickTree fenwickTree1 = new FenwickTree(n + 5), fenwickTree2 = new FenwickTree(n + 5); for(int x : a) { int idx = map.get(x); long smallerCnt = fenwickTree1.rangeSumQuery(idx - 1); long smallerSum = fenwickTree2.rangeSumQuery(idx - 1); ans += smallerCnt * x - smallerSum; fenwickTree1.adjust(idx, 1); fenwickTree2.adjust(idx, x); } out.println(ans); } out.close(); } static Map<Integer, Integer> compress(Integer[] b) { Arrays.sort(b); Map<Integer, Integer> map = new HashMap<>(); int v = 1; for(int i = 0; i < b.length; i++) { if(i == 0 || b[i] - b[i - 1] != 0) { map.put(b[i], v); v++; } } return map; } static class FenwickTree { private long[] ft; /* n is the largest integer value among all the input integers */ public FenwickTree(int n) { ft = new long[n]; } /* query the sum in range [l, r] */ public long rangeSumQuery(int l, int r) { if(l > r) return 0; return rangeSumQuery(r) - (l == 1 ? 0 : rangeSumQuery(l - 1)); } /* query the sum in range[1, r] */ private long rangeSumQuery(int r) { int sum = 0; for(; r > 0; r -= leastSignificantOne(r)) { sum += ft[r]; } return sum; } /* adjust the value of index k by diff */ public void adjust(int k, long diff) { for(; k < ft.length; k += leastSignificantOne(k)) { ft[k] += diff; } } private int leastSignificantOne(int i) { return i & (-i); } } static long addWithMod(long x, long y, long mod) { return (x + y) % mod; } static long subtractWithMod(long x, long y, long mod) { return ((x - y) % mod + mod) % mod; } static long multiplyWithMod(long x, long y, long mod) { x %= mod; y %= mod; return x * y % mod; } static long modInv(long x, long mod) { return fastPowMod(x, mod - 2, mod); } static long fastPowMod(long x, long n, long mod) { if (n == 0) return 1; long half = fastPowMod(x, n / 2, mod); if (n % 2 == 0) return half * half % mod; return half * half % mod * x % mod; } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("input.in")); out = new PrintWriter(new FileOutputStream("output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } 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; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readUnWeightedGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<int[]>[] readWeightedGraphOneIndexed(int n, int m) { List<int[]>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); int w = in.nextInt(); adj[u].add(new int[]{v, w}); adj[v].add(new int[]{u, w}); } return adj; } List<Integer>[] readUnWeightedGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } List<int[]>[] readWeightedGraphZeroIndexed(int n, int m) { List<int[]>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; int w = in.nextInt(); adj[u].add(new int[]{v, w}); adj[v].add(new int[]{u, w}); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building an undirected weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildUndirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]][0] = end2[i]; adj[end1[i]][idxForEachNode[end1[i]]][1] = weight[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]][0] = end1[i]; adj[end2[i]][idxForEachNode[end2[i]]][1] = weight[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } /* A more efficient way of building a directed weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildDirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]][0] = to[i]; adj[from[i]][idxForEachNode[from[i]]][1] = weight[i]; idxForEachNode[from[i]]++; } return adj; } } } //package atcoder.abc351; import java.io.*; import java.util.*; import static java.lang.Math.*; public class Main { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); //solve(in.nextInt()); solve(1); } /* General tips 1. It is ok to fail, but it is not ok to fail for the same mistakes over and over! 2. Train smarter, not harder! 3. If you find an answer and want to return immediately, don't forget to flush before return! */ /* Read before practice 1. Set a timer based on a problem's difficulty level: 45 minutes at your current target practice level; 2. During a problem solving session, focus! Do not switch problems or even worse switch to do something else; 3. If fail to solve within timer limit, read editorials to get as little help as possible to get yourself unblocked; 4. If after reading the entire editorial and other people's code but still can not solve, move this problem to to-do list and re-try in the future. 5. Keep a practice log about new thinking approaches, good tricks, bugs; Review regularly; 6. Also try this new approach suggested by um_nik: Solve with no intention to read editorial. If getting stuck, skip it and solve other similar level problems. Wait for 1 week then try to solve again. Only read editorial after you solved a problem. 7. Remember to also submit in the original problem link (if using gym) so that the 1 v 1 bot knows which problems I have solved already. 8. Form the habit of writing down an implementable solution idea before coding! You've taken enough hits during contests because you rushed to coding! */ /* Read before contests and lockout 1 v 1 Mistakes you've made in the past contests: 1. Tried to solve without going through given test examples -> wasting time on solving a different problem than asked; 2. Rushed to coding without getting a comprehensive sketch of your solution -> implementation bugs and WA; Write down your idea step by step, no need to rush. It is always better to have all the steps considered before hand! Think about all the past contests that you have failed because slow implementation and implementation bugs! This will be greatly reduced if you take your time to get a thorough idea steps! 3. Forgot about possible integer overflow; When stuck: 1. Understand problem statements? Walked through test examples? 2. Take a step back and think about other approaches? 3. Check rank board to see if you can skip to work on a possibly easier problem? 4. If none of the above works, take a guess? 5. Any chance you got WA due to integer overflow, especially if you are dealing with all subarrays. The sum can get deceptively large! If in doubt, just use long instead of int. */ static int n; static Integer[] a; static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { n = in.nextInt(); a = in.nextIntArray(n); long ans = 0; //for each a[i], get the number of index j < i, s.t a[j] < a[i], call it C //the contribution of a[i] is C * a[i] - the sum of all such a[j] //C is easy to find out, a compressed fenwick tree works //but how do you find all the sum of all such a[j] < a[i], j < i, use another //fenwick tree, except that this fenwick tree adds individual a[i] instead of cnt 1 Integer[] b = Arrays.copyOf(a, n); Map<Integer, Integer> map = compress(b); FenwickTree fenwickTree1 = new FenwickTree(n + 5), fenwickTree2 = new FenwickTree(n + 5); for(int x : a) { int idx = map.get(x); long smallerCnt = fenwickTree1.rangeSumQuery(idx - 1); long smallerSum = fenwickTree2.rangeSumQuery(idx - 1); ans += smallerCnt * x - smallerSum; fenwickTree1.adjust(idx, 1); fenwickTree2.adjust(idx, x); } out.println(ans); } out.close(); } static Map<Integer, Integer> compress(Integer[] b) { Arrays.sort(b); Map<Integer, Integer> map = new HashMap<>(); int v = 1; for(int i = 0; i < b.length; i++) { if(i == 0 || b[i] - b[i - 1] != 0) { map.put(b[i], v); v++; } } return map; } static class FenwickTree { private long[] ft; /* n is the largest integer value among all the input integers */ public FenwickTree(int n) { ft = new long[n]; } /* query the sum in range [l, r] */ public long rangeSumQuery(int l, int r) { if(l > r) return 0; return rangeSumQuery(r) - (l == 1 ? 0 : rangeSumQuery(l - 1)); } /* query the sum in range[1, r] */ private long rangeSumQuery(int r) { long sum = 0; for(; r > 0; r -= leastSignificantOne(r)) { sum += ft[r]; } return sum; } /* adjust the value of index k by diff */ public void adjust(int k, long diff) { for(; k < ft.length; k += leastSignificantOne(k)) { ft[k] += diff; } } private int leastSignificantOne(int i) { return i & (-i); } } static long addWithMod(long x, long y, long mod) { return (x + y) % mod; } static long subtractWithMod(long x, long y, long mod) { return ((x - y) % mod + mod) % mod; } static long multiplyWithMod(long x, long y, long mod) { x %= mod; y %= mod; return x * y % mod; } static long modInv(long x, long mod) { return fastPowMod(x, mod - 2, mod); } static long fastPowMod(long x, long n, long mod) { if (n == 0) return 1; long half = fastPowMod(x, n / 2, mod); if (n % 2 == 0) return half * half % mod; return half * half % mod * x % mod; } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("input.in")); out = new PrintWriter(new FileOutputStream("output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } 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; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } List<Integer>[] readUnWeightedGraphOneIndexed(int n, int m) { List<Integer>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); adj[u].add(v); adj[v].add(u); } return adj; } List<int[]>[] readWeightedGraphOneIndexed(int n, int m) { List<int[]>[] adj = new List[n + 1]; for (int i = 0; i <= n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt(); int v = nextInt(); int w = in.nextInt(); adj[u].add(new int[]{v, w}); adj[v].add(new int[]{u, w}); } return adj; } List<Integer>[] readUnWeightedGraphZeroIndexed(int n, int m) { List<Integer>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; adj[u].add(v); adj[v].add(u); } return adj; } List<int[]>[] readWeightedGraphZeroIndexed(int n, int m) { List<int[]>[] adj = new List[n]; for (int i = 0; i < n; i++) { adj[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; int w = in.nextInt(); adj[u].add(new int[]{v, w}); adj[v].add(new int[]{u, w}); } return adj; } /* A more efficient way of building an undirected graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildUndirectedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]] = end2[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]] = end1[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building an undirected weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildUndirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] end1 = new int[edgeCnt], end2 = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; edgeCntForEachNode[v]++; end1[i] = u; end2[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[end1[i]][idxForEachNode[end1[i]]][0] = end2[i]; adj[end1[i]][idxForEachNode[end1[i]]][1] = weight[i]; idxForEachNode[end1[i]]++; adj[end2[i]][idxForEachNode[end2[i]]][0] = end1[i]; adj[end2[i]][idxForEachNode[end2[i]]][1] = weight[i]; idxForEachNode[end2[i]]++; } return adj; } /* A more efficient way of building a directed graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][] buildDirectedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; } int[][] adj = new int[nodeCnt + 1][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]] = to[i]; idxForEachNode[from[i]]++; } return adj; } /* A more efficient way of building a directed weighted graph using int[] instead of ArrayList to store each node's neighboring nodes. 1-indexed. */ int[][][] buildDirectedWeightedGraph(int nodeCnt, int edgeCnt) { int[] from = new int[edgeCnt], to = new int[edgeCnt], weight = new int[edgeCnt]; int[] edgeCntForEachNode = new int[nodeCnt + 1], idxForEachNode = new int[nodeCnt + 1]; //from u to v: u -> v for (int i = 0; i < edgeCnt; i++) { int u = in.nextInt(), v = in.nextInt(), w = in.nextInt(); edgeCntForEachNode[u]++; from[i] = u; to[i] = v; weight[i] = w; } int[][][] adj = new int[nodeCnt + 1][][]; for (int i = 1; i <= nodeCnt; i++) { adj[i] = new int[edgeCntForEachNode[i]][2]; } for (int i = 0; i < edgeCnt; i++) { adj[from[i]][idxForEachNode[from[i]]][0] = to[i]; adj[from[i]][idxForEachNode[from[i]]][1] = weight[i]; idxForEachNode[from[i]]++; } return adj; } } }
ConDefects/ConDefects/Code/abc351_f/Java/52927223
condefects-java_data_1306
import java.io.*; import java.util.*; public class Main { static FastScanner fs = new FastScanner(); static int inf = 2100000000 ; static class Node { long sum ; int nums ; public Node(long sum, int nums) { this.sum = sum; this.nums = nums; } @Override public String toString() { return "Node [sum=" + sum + ", nums=" + nums + "]"; } } static class IdVal { int id ; int value ; public IdVal(int id, int value) { this.id = id; this.value = value; } @Override public String toString() { return "IdVal [id=" + id + ", value=" + value + "]"; } } static class SegmentTree { Node[] sum; public SegmentTree(int n) { sum = new Node[8 * n]; for ( int i = 0 ; i < 8 * n ; i ++ ) { sum[i] = new Node(0, 0); } } private void update(int id, int l, int r, int i, long value) { if ((i < l) || (i > r)) return; if (l == r) { sum[id] = new Node(value, 1); return; } int m = (l + r) / 2; update(2 * id + 1, l, m, i, value); update(2 * id + 2, m + 1, r, i, value); sum[id] = new Node( sum[2 * id + 1].sum + sum[2 * id + 2].sum , sum[2 * id + 1].nums + sum[2 * id + 2].nums ) ; } private Node getSum(int id, int l, int r, int il, int ir) { if ((il > ir) || (l > r) || (il > r) || (l > ir)) return new Node(0, 0); if ((l >= il) && (r <= ir)) return sum[id]; int m = (l + r) / 2; Node ll = getSum(2 * id + 1, l, m, il, ir) ; Node rr = getSum(2 * id + 2, m + 1, r, il, ir) ; return new Node( ll.sum + rr.sum , ll.nums + rr.nums ) ; } } static void Test_Case() { int n = fs.nextInt() ; List<IdVal> v = new ArrayList<> (); for ( int i = 0 ; i < n ; i ++ ) { int val = fs.nextInt() ; v.add(new IdVal(i , val )) ; } Collections.sort( v , ( node1 , node2 ) -> { if ( node1.value != node2.value ) { return Integer.compare(node2.value, node1.value) ; }else { return Integer.compare(node2.id , node1.id ) ; } }); long ans = 0 ; SegmentTree segTree = new SegmentTree(n) ; for ( IdVal nw : v ) { int id = nw.id ; int value = nw.value ; int l = id + 1 , r = n - 1 ; Node node = segTree.getSum ( 0 , 0 , n - 1 , l , r ) ; ans += node.sum - ( value * node.nums ) ; segTree.update(0, 0, n - 1, id , value ); } System.out.println( ans ); } public static void main(String[] useCppForCp ) { int t = 1 ; // t = fs.nextInt(); while ( t > 0 ) { Test_Case() ; t -- ; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } } import java.io.*; import java.util.*; public class Main { static FastScanner fs = new FastScanner(); static int inf = 2100000000 ; static class Node { long sum ; int nums ; public Node(long sum, int nums) { this.sum = sum; this.nums = nums; } @Override public String toString() { return "Node [sum=" + sum + ", nums=" + nums + "]"; } } static class IdVal { int id ; int value ; public IdVal(int id, int value) { this.id = id; this.value = value; } @Override public String toString() { return "IdVal [id=" + id + ", value=" + value + "]"; } } static class SegmentTree { Node[] sum; public SegmentTree(int n) { sum = new Node[8 * n]; for ( int i = 0 ; i < 8 * n ; i ++ ) { sum[i] = new Node(0, 0); } } private void update(int id, int l, int r, int i, long value) { if ((i < l) || (i > r)) return; if (l == r) { sum[id] = new Node(value, 1); return; } int m = (l + r) / 2; update(2 * id + 1, l, m, i, value); update(2 * id + 2, m + 1, r, i, value); sum[id] = new Node( sum[2 * id + 1].sum + sum[2 * id + 2].sum , sum[2 * id + 1].nums + sum[2 * id + 2].nums ) ; } private Node getSum(int id, int l, int r, int il, int ir) { if ((il > ir) || (l > r) || (il > r) || (l > ir)) return new Node(0, 0); if ((l >= il) && (r <= ir)) return sum[id]; int m = (l + r) / 2; Node ll = getSum(2 * id + 1, l, m, il, ir) ; Node rr = getSum(2 * id + 2, m + 1, r, il, ir) ; return new Node( ll.sum + rr.sum , ll.nums + rr.nums ) ; } } static void Test_Case() { int n = fs.nextInt() ; List<IdVal> v = new ArrayList<> (); for ( int i = 0 ; i < n ; i ++ ) { int val = fs.nextInt() ; v.add(new IdVal(i , val )) ; } Collections.sort( v , ( node1 , node2 ) -> { if ( node1.value != node2.value ) { return Integer.compare(node2.value, node1.value) ; }else { return Integer.compare(node2.id , node1.id ) ; } }); long ans = 0 ; SegmentTree segTree = new SegmentTree(n) ; for ( IdVal nw : v ) { int id = nw.id ; long value = nw.value ; int l = id + 1 , r = n - 1 ; Node node = segTree.getSum ( 0 , 0 , n - 1 , l , r ) ; ans += node.sum - ( value * node.nums ) ; segTree.update(0, 0, n - 1, id , value ); } System.out.println( ans ); } public static void main(String[] useCppForCp ) { int t = 1 ; // t = fs.nextInt(); while ( t > 0 ) { Test_Case() ; t -- ; } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
ConDefects/ConDefects/Code/abc351_f/Java/52879013
condefects-java_data_1307
import java.util.*; import java.lang.*; import java.io.*; // Union-Find構造 class UnionFind { private int[] _parent; private int[] _rank; // 要素の根を探索する public int find(int i){ int p = _parent[i]; if(i == p){ return i; } return _parent[i] = find(p); } // 2つの木を併合する public void union(int i, int j){ int root1 = find(i); int root2 = find(j); if(root1 == root2){ return; } if(_rank[root1] > _rank[root2]){ _parent[root2] = root1; } else if(_rank[root2] > _rank[root1]){ _parent[root1] = root2; } else{ _parent[root2] = root1; _rank[root1]++; } } // それぞれ根として初期化(コンストラクタ) public UnionFind(int max){ _parent = new int[max]; _rank = new int[max]; for(int i = 0; i < max; i++){ _parent[i] = i; } } // 根が同じ場合、trueを返す // 木の数を数えるのに使用する public boolean same(int i, int j){ int root1 = find(i); int root2 = find(j); return root1 == root2; } } public class Main { public static FastScanner sc = new FastScanner(System.in); public static void main(String[] args){ // 自動フラッシュオフ PrintWriter out = new PrintWriter(System.out); // 入力 int N = ini(); int M = ini(); int[] U = new int[M]; int[] V = new int[M]; inia(U,V); int K = ini(); int[] X = new int[K]; int[] Y = new int[K]; inia(X,Y); int Q = ini(); int[] p = new int[Q]; int[] q = new int[Q]; inia(p,q); // UnionFind構造 UnionFind uf = new UnionFind(N+1); for(int i = 0; i < M; i++){ uf.union(U[i],V[i]); } // グループ分け int[] group = new int[N+1]; for(int i = 1; i <= N; i++){ group[i] = uf.find(i); } // 結んではいけないグループのペアを算出 Set<String> nogoodSet = new HashSet<>(); for(int i = 0; i < K; i++){ nogoodSet.add(makePair(group[X[i]], group[Y[i]])); } // 回答 for(int i = 0; i < Q; i++){ String str = makePair(group[p[i]], group[q[i]]); // 出力 if(nogoodSet.contains(str)){ out.println("No"); } else{ out.println("Yes"); } } // フラッシュ out.flush(); } // グループのペアを文字列にする関数 public static String makePair(int g1, int g2){ int min = Math.min(g1, g2); int max = Math.min(g1, g2); StringBuilder sb = new StringBuilder(); sb.append(min); sb.append(","); sb.append(max); return sb.toString(); } // 関数 public static void name(){ } // インプット関数 public static int ini(){ return sc.nextInt(); } public static long inl(){ return sc.nextLong(); } public static double ind(){ return sc.nextDouble(); } public static String ins(){ return sc.next(); } public static int[] inia(int N){ int[] A = new int[N]; for(int i = 0; i < N; i++){ A[i] = ini(); } return A; } public static void inia(int[] A, int[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); } } public static void inia(int[] A, int[] B, int[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); C[i] = ini(); } } public static long[] inla(int N){ long[] A = new long[N]; for(int i = 0; i < N; i++){ A[i] = inl(); } return A; } public static void inla(long[] A, long[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); } } public static void inla(long[] A, long[] B, long[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); C[i] = inl(); } } public static double[] inda(int N){ double[] A = new double[N]; for(int i = 0; i < N; i++){ A[i] = ind(); } return A; } public static void inda(double[] A, double[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); } } public static void inda(double[] A, double[] B, double[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); C[i] = ind(); } } public static String[] insa(int N){ String[] A = new String[N]; for(int i = 0; i < N; i++){ A[i] = ins(); } return A; } public static void insa(String[] A, String[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); } } public static void insa(String[] A, String[] B, String[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); C[i] = ins(); } } public static char[] inca(){ return ins().toCharArray(); } // 高速スキャナー static class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } } import java.util.*; import java.lang.*; import java.io.*; // Union-Find構造 class UnionFind { private int[] _parent; private int[] _rank; // 要素の根を探索する public int find(int i){ int p = _parent[i]; if(i == p){ return i; } return _parent[i] = find(p); } // 2つの木を併合する public void union(int i, int j){ int root1 = find(i); int root2 = find(j); if(root1 == root2){ return; } if(_rank[root1] > _rank[root2]){ _parent[root2] = root1; } else if(_rank[root2] > _rank[root1]){ _parent[root1] = root2; } else{ _parent[root2] = root1; _rank[root1]++; } } // それぞれ根として初期化(コンストラクタ) public UnionFind(int max){ _parent = new int[max]; _rank = new int[max]; for(int i = 0; i < max; i++){ _parent[i] = i; } } // 根が同じ場合、trueを返す // 木の数を数えるのに使用する public boolean same(int i, int j){ int root1 = find(i); int root2 = find(j); return root1 == root2; } } public class Main { public static FastScanner sc = new FastScanner(System.in); public static void main(String[] args){ // 自動フラッシュオフ PrintWriter out = new PrintWriter(System.out); // 入力 int N = ini(); int M = ini(); int[] U = new int[M]; int[] V = new int[M]; inia(U,V); int K = ini(); int[] X = new int[K]; int[] Y = new int[K]; inia(X,Y); int Q = ini(); int[] p = new int[Q]; int[] q = new int[Q]; inia(p,q); // UnionFind構造 UnionFind uf = new UnionFind(N+1); for(int i = 0; i < M; i++){ uf.union(U[i],V[i]); } // グループ分け int[] group = new int[N+1]; for(int i = 1; i <= N; i++){ group[i] = uf.find(i); } // 結んではいけないグループのペアを算出 Set<String> nogoodSet = new HashSet<>(); for(int i = 0; i < K; i++){ nogoodSet.add(makePair(group[X[i]], group[Y[i]])); } // 回答 for(int i = 0; i < Q; i++){ String str = makePair(group[p[i]], group[q[i]]); // 出力 if(nogoodSet.contains(str)){ out.println("No"); } else{ out.println("Yes"); } } // フラッシュ out.flush(); } // グループのペアを文字列にする関数 public static String makePair(int g1, int g2){ int min = Math.min(g1, g2); int max = Math.max(g1, g2); StringBuilder sb = new StringBuilder(); sb.append(min); sb.append(","); sb.append(max); return sb.toString(); } // 関数 public static void name(){ } // インプット関数 public static int ini(){ return sc.nextInt(); } public static long inl(){ return sc.nextLong(); } public static double ind(){ return sc.nextDouble(); } public static String ins(){ return sc.next(); } public static int[] inia(int N){ int[] A = new int[N]; for(int i = 0; i < N; i++){ A[i] = ini(); } return A; } public static void inia(int[] A, int[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); } } public static void inia(int[] A, int[] B, int[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); C[i] = ini(); } } public static long[] inla(int N){ long[] A = new long[N]; for(int i = 0; i < N; i++){ A[i] = inl(); } return A; } public static void inla(long[] A, long[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); } } public static void inla(long[] A, long[] B, long[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); C[i] = inl(); } } public static double[] inda(int N){ double[] A = new double[N]; for(int i = 0; i < N; i++){ A[i] = ind(); } return A; } public static void inda(double[] A, double[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); } } public static void inda(double[] A, double[] B, double[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); C[i] = ind(); } } public static String[] insa(int N){ String[] A = new String[N]; for(int i = 0; i < N; i++){ A[i] = ins(); } return A; } public static void insa(String[] A, String[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); } } public static void insa(String[] A, String[] B, String[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); C[i] = ins(); } } public static char[] inca(){ return ins().toCharArray(); } // 高速スキャナー static class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } }
ConDefects/ConDefects/Code/abc304_e/Java/45040775
condefects-java_data_1308
import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static ContestPrinter pw = new ContestPrinter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { //int T = sc.nextInt(); //for(int i = 0; i < T; i++)solve(); solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int M = sc.nextInt(); DSU dsu = new DSU(N); for(int i = 0; i < M; i++){ int s = sc.nextInt()-1; int t = sc.nextInt()-1; dsu.merge(s,t); } ArrayList<ArrayList<Integer>> group = dsu.groups(); int[] indices = new int[N]; for(int i = 0; i < group.size(); i++){ for(int v : group.get(i)){ indices[v] = i; } } int K = sc.nextInt(); HashSet<Long> set = new HashSet<>(); for(int i = 0; i < K; i++){ int s = sc.nextInt()-1; int t = sc.nextInt()-1; long k = indices[s]; long k2 = indices[t]; set.add(k*(long)1e7+k2); } int Q = sc.nextInt(); for(int i = 0; i < Q; i++){ int s = sc.nextInt()-1; int t = sc.nextInt()-1; long k = indices[s]; long k2 = indices[t]; if(!set.contains(k*(long)1e7+k2)){ pw.println("Yes"); }else{ pw.println("No"); } } } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class DSU { private int n; private int[] parentOrSize; private java.util.ArrayList<java.util.ArrayList<Integer>> map; public DSU(int n) { this.n = n; this.map = new java.util.ArrayList<java.util.ArrayList<Integer>>(); for (int i = 0; i < n; i++) { this.map.add(new java.util.ArrayList<Integer>()); this.map.get(i).add(i); } this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; this.map.get(x).addAll(this.map.get(y)); return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } public int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } java.util.ArrayList<Integer> getArray(int n) { return this.map.get(n); } } /** * 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(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 PrintWriter { public ContestPrinter(PrintStream stream) { super(stream); } 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 printlnArray(String[] array) { for (String i : array) super.println(i); } public void printSpace(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) { super.print(o[i]); super.print(" "); } super.println(o[n]); } public void println(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.println(o[n]); } public void printYN(boolean o) { super.println(o ? "Yes" : "No"); } public void print(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.print(o[n]); } public void printArray(Object[] array) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(" "); } super.println(array[n]); } public void printlnArray(Object[] array) { for (Object i : array) super.println(i); } public void printArray(int[] array, String separator) { int n = array.length - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(Integer[] array) { this.printArray(array, " "); } public void printArray(Integer[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(int[] array) { for (int i : array) super.println(i); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n])); } public void printlnArray(int[] array, java.util.function.IntUnaryOperator map) { for (int i : array) super.println(map.applyAsInt(i)); } public void printlnArray(long[] array, java.util.function.LongUnaryOperator map) { for (long i : array) super.println(map.applyAsLong(i)); } 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 - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printlnArray(long[] array) { for (long i : array) super.println(i); } public void printArray(double[] array) { printArray(array, " "); } public void printArray(double[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(double[] array) { for (double i : array) super.println(i); } public void printArray(boolean[] array, String a, String b) { int n = array.length - 1; for (int i = 0; i < n; i++) super.print((array[i] ? a : b) + " "); super.println(array[n] ? a : b); } public void printArray(boolean[] array) { this.printArray(array, "Y", "N"); } public void printArray(char[] array) { for (char c : array) this.print(c); this.println(); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(ArrayList<?> array) { this.printArray(array, " "); } public void printArray(ArrayList<?> array, String separator) { int n = array.size() - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array.get(i).toString()); super.print(separator); } super.println(array.get(n).toString()); } public void printlnArray(ArrayList<?> array) { int n = array.size(); for (int i = 0; i < n; i++) super.println(array.get(i).toString()); } public void printlnArray(ArrayList<Integer> array, java.util.function.IntUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsInt(array.get(i))); } public void printlnArray(ArrayList<Long> array, java.util.function.LongUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsLong(array.get(i))); } public void printArray(int[][] array) { for (int[] a : array) this.printArray(a); } public void printArray(int[][] array, java.util.function.IntUnaryOperator map) { for (int[] a : array) this.printArray(a, map); } public void printArray(long[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] + " "); super.println(array[i][m]); } } public void printArray(long[][] array, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { super.print(map.applyAsLong(array[i][j])); super.print(" "); } super.println(map.applyAsLong(array[i][m])); } } public void printArray(boolean[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] ? "○ " : "× "); super.println(array[i][m] ? "○" : "×"); } } public void printArray(char[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j]); super.println(); } } } import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static ContestPrinter pw = new ContestPrinter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { //int T = sc.nextInt(); //for(int i = 0; i < T; i++)solve(); solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int M = sc.nextInt(); DSU dsu = new DSU(N); for(int i = 0; i < M; i++){ int s = sc.nextInt()-1; int t = sc.nextInt()-1; dsu.merge(s,t); } ArrayList<ArrayList<Integer>> group = dsu.groups(); int[] indices = new int[N]; for(int i = 0; i < group.size(); i++){ for(int v : group.get(i)){ indices[v] = i; } } int K = sc.nextInt(); HashSet<Long> set = new HashSet<>(); for(int i = 0; i < K; i++){ int s = sc.nextInt()-1; int t = sc.nextInt()-1; long k = indices[s]; long k2 = indices[t]; set.add(k*(long)1e7+k2); set.add(k2*(long)1e7+k); } //pw.println(set); int Q = sc.nextInt(); for(int i = 0; i < Q; i++){ int s = sc.nextInt()-1; int t = sc.nextInt()-1; long k = indices[s]; long k2 = indices[t]; if(!set.contains(k*(long)1e7+k2)){ pw.println("Yes"); }else{ pw.println("No"); } } } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class DSU { private int n; private int[] parentOrSize; private java.util.ArrayList<java.util.ArrayList<Integer>> map; public DSU(int n) { this.n = n; this.map = new java.util.ArrayList<java.util.ArrayList<Integer>>(); for (int i = 0; i < n; i++) { this.map.add(new java.util.ArrayList<Integer>()); this.map.get(i).add(i); } this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; this.map.get(x).addAll(this.map.get(y)); return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } public int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } java.util.ArrayList<Integer> getArray(int n) { return this.map.get(n); } } /** * 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(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 PrintWriter { public ContestPrinter(PrintStream stream) { super(stream); } 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 printlnArray(String[] array) { for (String i : array) super.println(i); } public void printSpace(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) { super.print(o[i]); super.print(" "); } super.println(o[n]); } public void println(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.println(o[n]); } public void printYN(boolean o) { super.println(o ? "Yes" : "No"); } public void print(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.print(o[n]); } public void printArray(Object[] array) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(" "); } super.println(array[n]); } public void printlnArray(Object[] array) { for (Object i : array) super.println(i); } public void printArray(int[] array, String separator) { int n = array.length - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(Integer[] array) { this.printArray(array, " "); } public void printArray(Integer[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(int[] array) { for (int i : array) super.println(i); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n])); } public void printlnArray(int[] array, java.util.function.IntUnaryOperator map) { for (int i : array) super.println(map.applyAsInt(i)); } public void printlnArray(long[] array, java.util.function.LongUnaryOperator map) { for (long i : array) super.println(map.applyAsLong(i)); } 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 - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printlnArray(long[] array) { for (long i : array) super.println(i); } public void printArray(double[] array) { printArray(array, " "); } public void printArray(double[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(double[] array) { for (double i : array) super.println(i); } public void printArray(boolean[] array, String a, String b) { int n = array.length - 1; for (int i = 0; i < n; i++) super.print((array[i] ? a : b) + " "); super.println(array[n] ? a : b); } public void printArray(boolean[] array) { this.printArray(array, "Y", "N"); } public void printArray(char[] array) { for (char c : array) this.print(c); this.println(); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(ArrayList<?> array) { this.printArray(array, " "); } public void printArray(ArrayList<?> array, String separator) { int n = array.size() - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array.get(i).toString()); super.print(separator); } super.println(array.get(n).toString()); } public void printlnArray(ArrayList<?> array) { int n = array.size(); for (int i = 0; i < n; i++) super.println(array.get(i).toString()); } public void printlnArray(ArrayList<Integer> array, java.util.function.IntUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsInt(array.get(i))); } public void printlnArray(ArrayList<Long> array, java.util.function.LongUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsLong(array.get(i))); } public void printArray(int[][] array) { for (int[] a : array) this.printArray(a); } public void printArray(int[][] array, java.util.function.IntUnaryOperator map) { for (int[] a : array) this.printArray(a, map); } public void printArray(long[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] + " "); super.println(array[i][m]); } } public void printArray(long[][] array, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { super.print(map.applyAsLong(array[i][j])); super.print(" "); } super.println(map.applyAsLong(array[i][m])); } } public void printArray(boolean[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] ? "○ " : "× "); super.println(array[i][m] ? "○" : "×"); } } public void printArray(char[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j]); super.println(); } } }
ConDefects/ConDefects/Code/abc304_e/Java/43172118
condefects-java_data_1309
import java.util.Scanner; import java.lang.Math; public class Main { int classa() { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); int dist = 0; if (y < 0 && x > 0) { dist = x; return dist; } else if (y > 0 && x < 0) { dist = Math.abs(x); return dist; } else if (y>0 && x >0) { if (x < y) { dist = x; return dist; } else if (x > y && z < y && z < 0) { dist = Math.abs(z)*2 + x; return dist; } else if (x > y && z < y && z > 0) { dist = x; } else { dist = -1; return dist; } } else if(y<0 && x < 0) { if (x > y) { dist = Math.abs(x); return dist; } else if (x < y && z > y && z > 0) { dist = z*2 + Math.abs(x); return dist; } else if (x < y && z > y && z < 0) { dist = Math.abs(x); return dist; } else { dist = -1; return dist; } } else { dist = -1; return dist; } return 0; } public static void main (String[]args){ System.out.println(new Main().classa()); } } import java.util.Scanner; import java.lang.Math; public class Main { int classa() { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); int dist = 0; if (y < 0 && x > 0) { dist = x; return dist; } else if (y > 0 && x < 0) { dist = Math.abs(x); return dist; } else if (y>0 && x >0) { if (x < y) { dist = x; return dist; } else if (x > y && z < y && z < 0) { dist = Math.abs(z)*2 + x; return dist; } else if (x > y && z < y && z > 0) { dist = x; return dist; } else { dist = -1; return dist; } } else if(y<0 && x < 0) { if (x > y) { dist = Math.abs(x); return dist; } else if (x < y && z > y && z > 0) { dist = z*2 + Math.abs(x); return dist; } else if (x < y && z > y && z < 0) { dist = Math.abs(x); return dist; } else { dist = -1; return dist; } } else { dist = -1; return dist; } } public static void main (String[]args){ System.out.println(new Main().classa()); } }
ConDefects/ConDefects/Code/abc270_b/Java/38571992
condefects-java_data_1310
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int X=sc.nextInt(); int Y=sc.nextInt(); int Z=sc.nextInt(); if((0<X&&X<Y)||(Y<0&&0<X)||(Y<X&&X<0)||(X<0&&0<Y)){ System.out.println(Math.abs(X)); }else if((0<Y&&Y<X&&Y<Z)||(Z<Y&&X<Y&&Y<0)){ System.out.println(-1); }else{ if((Z>0&&X>0)||(Z<0&&X<0)){ System.out.println(Math.abs(X)); }else if((X<0&&0<Z)||(Z<0&&0<X)){ System.out.println(2*Math.abs(Z)+X); } } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int X=sc.nextInt(); int Y=sc.nextInt(); int Z=sc.nextInt(); if((0<X&&X<Y)||(Y<0&&0<X)||(Y<X&&X<0)||(X<0&&0<Y)){ System.out.println(Math.abs(X)); }else if((0<Y&&Y<X&&Y<Z)||(Z<Y&&X<Y&&Y<0)){ System.out.println(-1); }else{ if((Z>0&&X>0)||(Z<0&&X<0)){ System.out.println(Math.abs(X)); }else if((X<0&&0<Z)||(Z<0&&0<X)){ System.out.println(2*Math.abs(Z)+Math.abs(X)); } } } }
ConDefects/ConDefects/Code/abc270_b/Java/41819913
condefects-java_data_1311
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if( y < x && z > y && y > 0){ System.out.println("-1"); return; } if( y < 0 && y > x && y > z){ System.out.println("-1"); return; } if((x > 0 && y < 0) || (x < 0 && y >0) || (y > x && x > 0) || (y < x && x < 0)){ System.out.println(Math.abs(x)); return; } if( (x > y && y > 0 && z < 0) ){ System.out.println(2 * Math.abs(z) + x ); return; } if( z > 0 && x < y && y < 0){ System.out.println(z + 2 * Math.abs(x)); return; } System.out.println(Math.abs(x)); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if( y < x && z > y && y > 0){ System.out.println("-1"); return; } if( y < 0 && y > x && y > z){ System.out.println("-1"); return; } if((x > 0 && y < 0) || (x < 0 && y >0) || (y > x && x > 0) || (y < x && x < 0)){ System.out.println(Math.abs(x)); return; } if( (x > y && y > 0 && z < 0) ){ System.out.println(2 * Math.abs(z) + x ); return; } if( z > 0 && x < y && y < 0){ System.out.println(2 * z + Math.abs(x)); return; } System.out.println(Math.abs(x)); } }
ConDefects/ConDefects/Code/abc270_b/Java/40325124
condefects-java_data_1312
import java.io.*; // 処理 final class Process { private final int X; private final int Y; private final int Z; Process(final int X, final int Y, final int Z) { this.X = X; this.Y = Y; this.Z = Z; } // 結果を出力 final void printResult(final PrintWriter printWriter) throws IOException { if(X > 0) { if(Y > X || Y < 0) { printWriter.println(X); return; } if(Z > Y) printWriter.println(-1); else if(Z > 0) printWriter.println(X); else printWriter.println(X + (-2) * Z); return; } if(Y < X || Y > 0) { printWriter.println(X); return; } if(Z < Y) printWriter.println(-1); else if(Z < 0) printWriter.println(-X); else printWriter.println(-X + 2 * Z); } } public class Main { public static void main(final String[] args) throws IOException { final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); final PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 入力 final String[] input = bufferedReader.readLine().trim().split("[ ]+"); final int X = Integer.parseInt(input[0]); final int Y = Integer.parseInt(input[1]); final int Z = Integer.parseInt(input[2]); // Process クラスで処理を行う final Process process = new Process(X, Y, Z); process.printResult(printWriter); // 各ストリームを閉じる // 出力ストリームを閉じるときに標準出力に文字を出力する bufferedReader.close(); printWriter.close(); } } import java.io.*; // 処理 final class Process { private final int X; private final int Y; private final int Z; Process(final int X, final int Y, final int Z) { this.X = X; this.Y = Y; this.Z = Z; } // 結果を出力 final void printResult(final PrintWriter printWriter) throws IOException { if(X > 0) { if(Y > X || Y < 0) { printWriter.println(X); return; } if(Z > Y) printWriter.println(-1); else if(Z > 0) printWriter.println(X); else printWriter.println(X + (-2) * Z); return; } if(Y < X || Y > 0) { printWriter.println(-X); return; } if(Z < Y) printWriter.println(-1); else if(Z < 0) printWriter.println(-X); else printWriter.println(-X + 2 * Z); } } public class Main { public static void main(final String[] args) throws IOException { final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); final PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); // 入力 final String[] input = bufferedReader.readLine().trim().split("[ ]+"); final int X = Integer.parseInt(input[0]); final int Y = Integer.parseInt(input[1]); final int Z = Integer.parseInt(input[2]); // Process クラスで処理を行う final Process process = new Process(X, Y, Z); process.printResult(printWriter); // 各ストリームを閉じる // 出力ストリームを閉じるときに標準出力に文字を出力する bufferedReader.close(); printWriter.close(); } }
ConDefects/ConDefects/Code/abc270_b/Java/39550872
condefects-java_data_1313
import java.util.*; class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if((x > y && y < z && y >0) || (x < y && y > z && z < 0) ){ System.out.print(-1); }else if((x < y && y > 0) || (x > y && y < 0) || (y > z && z > 0) || (y < z && z < 0)){ System.out.print(Math.abs(x)); }else{ System.out.print(Math.abs(x) + Math.abs(z)*2); } } } import java.util.*; class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); if((x > y && y < z && y >0) || (x < y && y > z && y < 0) ){ System.out.print(-1); }else if((x < y && y > 0) || (x > y && y < 0) || (y > z && z > 0) || (y < z && z < 0)){ System.out.print(Math.abs(x)); }else{ System.out.print(Math.abs(x) + Math.abs(z)*2); } } }
ConDefects/ConDefects/Code/abc270_b/Java/45784672
condefects-java_data_1314
import java.util.Scanner; public class Main { public static void main(final String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int X = Integer.parseInt(sc.next()); final int Y = Integer.parseInt(sc.next()); final int Z = Integer.parseInt(sc.next()); if (X * X <= Y * Y) { System.out.println(Math.abs(X)); } else if (Z * Z <= Y * Y) { if ((X ^ Z) >= 0) { // 符号が同じ System.out.println(Math.abs(X)); } else { // 符号が違う System.out.println(Math.abs(X) + Math.abs(Z) * 2); } } else { System.out.println(-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 X = Integer.parseInt(sc.next()); final int Y = Integer.parseInt(sc.next()); final int Z = Integer.parseInt(sc.next()); if (X * X <= Y * Y || (X ^ Y) < 0) { System.out.println(Math.abs(X)); } else if (Z * Z <= Y * Y) { if ((X ^ Z) >= 0) { // 符号が同じ System.out.println(Math.abs(X)); } else { // 符号が違う System.out.println(Math.abs(X) + Math.abs(Z) * 2); } } else { System.out.println(-1); } } } }
ConDefects/ConDefects/Code/abc270_b/Java/45040873
condefects-java_data_1315
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int X = sc.nextInt(); int Y = sc.nextInt(); int Z = sc.nextInt(); if((0 < Y && Y < X && Y < Z) || (Y < 0 && X < Y && Z < Y)){ System.out.println("No"); return; } if((X > Y && Y > 0 && 0 > Z) || (X < Y && Y < 0 && 0 < Z))System.out.println(Math.abs(X) + Math.abs(2 * Z)); else System.out.println(Math.abs(X)); } } import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int X = sc.nextInt(); int Y = sc.nextInt(); int Z = sc.nextInt(); if((0 < Y && Y < X && Y < Z) || (Y < 0 && X < Y && Z < Y)){ System.out.println("-1"); return; } if((X > Y && Y > 0 && 0 > Z) || (X < Y && Y < 0 && 0 < Z))System.out.println(Math.abs(X) + Math.abs(2 * Z)); else System.out.println(Math.abs(X)); } }
ConDefects/ConDefects/Code/abc270_b/Java/42054949
condefects-java_data_1316
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.StringTokenizer; /* * Solution: 1m * Coding: 4m * Time: 5m * */ public class Main { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("atcoder_abc/input.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); int z = Integer.parseInt(st.nextToken()); if(x > 0 && y > 0){ if(x < y){ System.out.println(x); } else if(z < y) { if(z < 0){ System.out.println(2 * Math.abs(z) + Math.abs(x)); } else { System.out.println(Math.abs(x)); } } else { System.out.println(-1); } } else if (x < 0 && y < 0){ if(x > y){ System.out.println(Math.abs(x)); } else if (z > y) { if(z < 0){ System.out.println(Math.abs(x)); } else { System.out.println(2 * Math.abs(z) + Math.abs(x)); } } } else if (x * y < 0){ System.out.println(Math.abs(x)); } else { System.out.println(-1); } br.close(); } } import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.StringTokenizer; /* * Solution: 1m * Coding: 4m * Time: 5m * */ public class Main { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("atcoder_abc/input.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int x = Integer.parseInt(st.nextToken()); int y = Integer.parseInt(st.nextToken()); int z = Integer.parseInt(st.nextToken()); if(x > 0 && y > 0){ if(x < y){ System.out.println(x); } else if(z < y) { if(z < 0){ System.out.println(2 * Math.abs(z) + Math.abs(x)); } else { System.out.println(Math.abs(x)); } } else { System.out.println(-1); } } else if (x < 0 && y < 0){ if(x > y){ System.out.println(Math.abs(x)); } else if (z > y) { if(z < 0){ System.out.println(Math.abs(x)); } else { System.out.println(2 * Math.abs(z) + Math.abs(x)); } } else { System.out.println(-1); } } else if (x * y < 0){ System.out.println(Math.abs(x)); } else { System.out.println(-1); } br.close(); } }
ConDefects/ConDefects/Code/abc270_b/Java/40795105
condefects-java_data_1317
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author YC * @version 1.0 */ public class Main { static int N = (int) (2e5 + 10); static long[] sum = new long[N]; static long u = 0; static int n; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { n = Integer.parseInt(br.readLine()); String[] strs = br.readLine().split(" "); for(int i = 1; i <= n ; i ++) { sum[i] = sum[i - 1] + Integer.parseInt(strs[i - 1]); if(sum[i] < 0) { u -= sum[i]; } } System.out.println(u + sum[n]); } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author YC * @version 1.0 */ public class Main { static int N = (int) (2e5 + 10); static long[] sum = new long[N]; static long u = 0; static int n; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { n = Integer.parseInt(br.readLine()); String[] strs = br.readLine().split(" "); for(int i = 1; i <= n ; i ++) { sum[i] = sum[i - 1] + Integer.parseInt(strs[i - 1]); if(sum[i] < 0) { u = Math.max(u, -sum[i]); } } System.out.println(u + sum[n]); } }
ConDefects/ConDefects/Code/abc339_c/Java/53439528
condefects-java_data_1318
import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int n = sc.nextInt(); long[] a = new long[n]; a[0] = sc.nextLong(); long min = a[0]; for (int i = 1; i < n; i++) { a[i] = sc.nextInt() + a[i - 1]; if (min > a[i]) min = a[i]; } if (min < 0) System.out.println(-min + a[n - 1]); else System.out.println(min + a[n - 1]); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { int n = sc.nextInt(); long[] a = new long[n]; a[0] = sc.nextLong(); long min = a[0]; for (int i = 1; i < n; i++) { a[i] = sc.nextInt() + a[i - 1]; if (min > a[i]) min = a[i]; } if (min < 0) System.out.println(-min + a[n - 1]); else System.out.println(a[n - 1]); } } }
ConDefects/ConDefects/Code/abc339_c/Java/53051484
condefects-java_data_1319
import java.util.*; //計算量は N public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long min = 2000000000000000L; long passengers = 0L; //停車した中で、最も最初の人数との差が大きくなる時(少ない方向に) for(int i=0; i<N; i++){ long A = sc.nextLong(); passengers += A; if(passengers<min){ min = passengers; } } //minが負の値の時は、初期の人数=min*(-1) //minが正の値または0の時は、初期の人数=0 long ans; if(min>=0){ ans = 0; }else{ ans = min*(-1)+passengers; } System.out.println(ans); } } import java.util.*; //計算量は N public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long min = 2000000000000000L; long passengers = 0L; //停車した中で、最も最初の人数との差が大きくなる時(少ない方向に) for(int i=0; i<N; i++){ long A = sc.nextLong(); passengers += A; if(passengers<min){ min = passengers; } } //minが負の値の時は、初期の人数=min*(-1) //minが正の値または0の時は、初期の人数=0 long ans; if(min>=0){ ans = passengers; }else{ ans = min*(-1)+passengers; } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc339_c/Java/54291523
condefects-java_data_1320
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.next(); int b = input.indexOf("B"); int b2 = input.indexOf("B", b + 1); int r = input.indexOf("R"); int r2 = input.indexOf("R", r + 1); int k = input.indexOf("K"); if (b < b2 && (b % 2) != (b2 % 2)) { if (r < r2 && (r % 2) != (r2 % 2)) { if (r < k && k < r2) { System.out.println("Yes"); return; } } } System.out.println("No"); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.next(); int b = input.indexOf("B"); int b2 = input.indexOf("B", b + 1); int r = input.indexOf("R"); int r2 = input.indexOf("R", r + 1); int k = input.indexOf("K"); if (b < b2 && (b % 2) != (b2 % 2)) { if (r < r2 ) { if (r < k && k < r2) { System.out.println("Yes"); return; } } } System.out.println("No"); } }
ConDefects/ConDefects/Code/abc297_b/Java/43531424
condefects-java_data_1321
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Kattio io = new Kattio(); String s = io.next(); int fb = -1, sb = -1, fr = -1, sr = -1, k = -1; boolean bb = false, tk = false; for (int i = 0; i < 8; i++) { if (fb != sb && fb != -1 && sb != -1 && ((fb + 1) % 2 != (sb + 1) % 2)) bb = true; if (s.charAt(i) == 'B' && fb == -1) fb = i; else if (s.charAt(i) == 'B') sb = i; } for (int i = 0; i < 8; i++) { if (s.charAt(i) == 'K') k = i; else if (s.charAt(i) == 'R' && fr == -1) fr = i; else if (s.charAt(i) == 'R') sr = i; if (k != -1 && fr != -1 && sr != -1 && k < sr && k > fr) tk = true; } if (bb && tk) io.println("Yes"); else io.println("No"); io.close(); } private static class Kattio extends PrintWriter { private BufferedReader br; private StringTokenizer st; private String line, token; public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public String next() { return nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = br.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } } } import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Kattio io = new Kattio(); String s = io.next(); int fb = -1, sb = -1, fr = -1, sr = -1, k = -1; boolean bb = false, tk = false; for (int i = 0; i < 8; i++) { if (s.charAt(i) == 'B' && fb == -1) fb = i; else if (s.charAt(i) == 'B') sb = i; if (fb != sb && fb != -1 && sb != -1 && ((fb + 1) % 2 != (sb + 1) % 2)) bb = true; } for (int i = 0; i < 8; i++) { if (s.charAt(i) == 'K') k = i; else if (s.charAt(i) == 'R' && fr == -1) fr = i; else if (s.charAt(i) == 'R') sr = i; if (k != -1 && fr != -1 && sr != -1 && k < sr && k > fr) tk = true; } if (bb && tk) io.println("Yes"); else io.println("No"); io.close(); } private static class Kattio extends PrintWriter { private BufferedReader br; private StringTokenizer st; private String line, token; public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public String next() { return nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = br.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } } }
ConDefects/ConDefects/Code/abc297_b/Java/43053847
condefects-java_data_1322
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); int K = -1; // int Q = -1; int R1 = -1; int R2 = -1; int B1 = -1; int B2 = -1; // int N1 = -1; // int N2 = -1; for (int i = 0; i < 8; i++) { char ich = input.charAt(i); if (ich == 'K') { K = i; // } else if (ich == 'Q') { // Q = i; } else if (ich == 'R' && R1 == -1) { R1 = i; } else if (ich == 'R') { R2 = i; } else if (ich == 'B' && B1 == -1) { B1 = i; } else if (ich == 'B') { B2 = i; // } else if (ich == 'N' && N1 == -1) { // N1 = i; // } else if (ich == 'N') { // N2 = i; } } // Q1 int res = 0; if (B1 != -1 && B2 != -1 && (B1 == 0 || (B2%B1)%2 != 0)) { res += 1; } // Q2 if (R1 < K && K < R2) { res += 1; } System.out.println(res == 2 ? "Yes" : "No"); } } import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); int K = -1; // int Q = -1; int R1 = -1; int R2 = -1; int B1 = -1; int B2 = -1; // int N1 = -1; // int N2 = -1; for (int i = 0; i < 8; i++) { char ich = input.charAt(i); if (ich == 'K') { K = i; // } else if (ich == 'Q') { // Q = i; } else if (ich == 'R' && R1 == -1) { R1 = i; } else if (ich == 'R') { R2 = i; } else if (ich == 'B' && B1 == -1) { B1 = i; } else if (ich == 'B') { B2 = i; // } else if (ich == 'N' && N1 == -1) { // N1 = i; // } else if (ich == 'N') { // N2 = i; } } // Q1 int res = 0; if ((B2-B1)%2 != 0) { res += 1; } // Q2 if (R1 < K && K < R2) { res += 1; } System.out.println(res == 2 ? "Yes" : "No"); } }
ConDefects/ConDefects/Code/abc297_b/Java/42230667
condefects-java_data_1323
import java.util.Scanner; public class Main { public static void main(final String args[]) { try (Scanner sc = new Scanner(System.in)) { final String S = sc.next(); final char[] charS = S.toCharArray(); int firstB = 0; int secondB = 0; boolean isFirstBFound = false; int RKState = 0; for (int i = 0; i < 8; i++) { if (charS[i] == 'B' && !isFirstBFound) { firstB = i; isFirstBFound = true; } if (charS[i] == 'B' && isFirstBFound) { secondB = i; } if (charS[i] == 'R' && RKState == 0) { RKState++; } if (charS[i] == 'K' && RKState == 1) { RKState++; } if (charS[i] == 'R' && RKState == 2) { RKState++; } } if ((firstB % 2 == 0 && secondB % 2 == 1) || (firstB % 2 == 1 && secondB % 2 == 0) && RKState == 3) { System.out.println("Yes"); } else { System.out.println("No"); } } } } import java.util.Scanner; public class Main { public static void main(final String args[]) { try (Scanner sc = new Scanner(System.in)) { final String S = sc.next(); final char[] charS = S.toCharArray(); int firstB = 0; int secondB = 0; boolean isFirstBFound = false; int RKState = 0; for (int i = 0; i < 8; i++) { if (charS[i] == 'B' && !isFirstBFound) { firstB = i; isFirstBFound = true; } if (charS[i] == 'B' && isFirstBFound) { secondB = i; } if (charS[i] == 'R' && RKState == 0) { RKState++; } if (charS[i] == 'K' && RKState == 1) { RKState++; } if (charS[i] == 'R' && RKState == 2) { RKState++; } } if (((firstB % 2 == 0 && secondB % 2 == 1) || (firstB % 2 == 1 && secondB % 2 == 0)) && RKState == 3) { System.out.println("Yes"); } else { System.out.println("No"); } } } }
ConDefects/ConDefects/Code/abc297_b/Java/43748945
condefects-java_data_1324
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); boolean evenOddFlag=false; boolean existR=false; if(str.indexOf("B")%2==0) { if(str.lastIndexOf("B")%2 !=0) { evenOddFlag=true; } }else { if(str.lastIndexOf("B")%2 ==0) { evenOddFlag=true; } } String rr=str.substring(str.indexOf("B"),str.lastIndexOf("B")+1); if(rr.indexOf("K")!=-1) { existR=true; } if(evenOddFlag&&existR) { System.out.println("Yes"); }else { System.out.println("No"); } sc.close(); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); boolean evenOddFlag=false; boolean existR=false; if(str.indexOf("B")%2==0) { if(str.lastIndexOf("B")%2 !=0) { evenOddFlag=true; } }else { if(str.lastIndexOf("B")%2 ==0) { evenOddFlag=true; } } String rr=str.substring(str.indexOf("R"),str.lastIndexOf("R")); if(rr.indexOf("K")!=-1) { existR=true; } if(evenOddFlag&&existR) { System.out.println("Yes"); }else { System.out.println("No"); } sc.close(); } }
ConDefects/ConDefects/Code/abc297_b/Java/42037745
condefects-java_data_1325
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { private static Scanner stdin; private static int n,m,k,s,t,x; private static int[][][] dp; private static List<Integer>[] g; public static void main(String[] args) { int i,j; read_input(); for(i=1;i<=n;i++){ if(g[i].contains(s)) if(i!=x) dp[i][2][0]=1; else dp[i][2][1]=1; for(j=3;j<=k+1;j++) { dp[i][j][0] = -1; dp[i][j][1] = -1; } } System.out.println(dfs(t,k+1,0)); } private static int dfs(int end,int len,int mode){ if(dp[end][len][mode]>=0) return dp[end][len][mode]; dp[end][len][mode]=0; for(int next:g[end]){ if(next==x) dp[end][len][mode]=(dp[end][len][mode]+dfs(next,len-1,(mode+1)&1))%998244353; else dp[end][len][mode]=(dp[end][len][mode]+dfs(next,len-1,mode))%998244353; } return dp[end][len][mode]; } private static void read_input(){ int u,v; stdin=new Scanner(System.in); n=stdin.nextInt(); m=stdin.nextInt(); k=stdin.nextInt(); s=stdin.nextInt(); t=stdin.nextInt(); x=stdin.nextInt(); dp=new int[n+1][k+2][2]; g=new List[n+1]; for(int i=1;i<=n;i++) g[i]=new ArrayList<>(); for(int i=0;i<m;i++){ u=stdin.nextInt(); v=stdin.nextInt(); g[u].add(v); g[v].add(u); } } } import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { private static Scanner stdin; private static int n,m,k,s,t,x; private static int[][][] dp; private static List<Integer>[] g; public static void main(String[] args) { int i,j; read_input(); for(i=1;i<=n;i++){ if(g[i].contains(s)) if(i!=x) dp[i][2][0]=1; else dp[i][2][1]=1; for(j=3;j<=k+1;j++) { dp[i][j][0] = -1; dp[i][j][1] = -1; } } System.out.println(dfs(t,k+1,0)); } private static int dfs(int end,int len,int mode){ if(dp[end][len][mode]>=0) return dp[end][len][mode]; dp[end][len][mode]=0; for(int next:g[end]){ if(end==x) dp[end][len][mode]=(dp[end][len][mode]+dfs(next,len-1,(mode+1)&1))%998244353; else dp[end][len][mode]=(dp[end][len][mode]+dfs(next,len-1,mode))%998244353; } return dp[end][len][mode]; } private static void read_input(){ int u,v; stdin=new Scanner(System.in); n=stdin.nextInt(); m=stdin.nextInt(); k=stdin.nextInt(); s=stdin.nextInt(); t=stdin.nextInt(); x=stdin.nextInt(); dp=new int[n+1][k+2][2]; g=new List[n+1]; for(int i=1;i<=n;i++) g[i]=new ArrayList<>(); for(int i=0;i<m;i++){ u=stdin.nextInt(); v=stdin.nextInt(); g[u].add(v); g[v].add(u); } } }
ConDefects/ConDefects/Code/abc244_e/Java/38097890
condefects-java_data_1326
import java.util.ArrayList; import java.util.List; public class Main { private static final long MOD = 998244353L; public static void main(String[] args) throws Exception { final FastScanner sc = new FastScanner(System.in); final int n = sc.nextInt(); final int m = sc.nextInt(); final int k = sc.nextInt(); final int s = sc.nextInt() - 1; final int t = sc.nextInt() - 1; final int x = sc.nextInt() - 1; final List<List<Integer>> list_edge = new ArrayList<>(); for (int i = 0; i < n; i++) { list_edge.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; list_edge.get(u).add(v); list_edge.get(v).add(u); } sc.close(); //dp[k][n][2]:=k回目の移動で、頂点nに到達し、且つxにmod2回到達した final long[][][] dp = new long[k + 1][n][2]; dp[0][s][0] = 1; for (int i = 1; i <= k; i++) { for (int prev = 0; prev < n; prev++) { if (dp[i - 1][prev][0] + dp[i - 1][prev][1] == 0) { continue; } for (int next : list_edge.get(prev)) { if (next == x) { dp[i][next][1] += dp[i - 1][prev][0]; dp[i][next][0] += dp[i - 1][prev][1]; } else { dp[i][next][0] += dp[i - 1][prev][0]; dp[i][next][1] += dp[i - 1][prev][1]; } dp[i][next][0] %= MOD; dp[i][next][0] %= MOD; } } } System.out.println(dp[k][t][0]); } // FastScannerライブラリ static class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(java.io.InputStream input) { this.in = input; } 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') { 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()); } @Override public void close() throws Exception { in.close(); } } } import java.util.ArrayList; import java.util.List; public class Main { private static final long MOD = 998244353L; public static void main(String[] args) throws Exception { final FastScanner sc = new FastScanner(System.in); final int n = sc.nextInt(); final int m = sc.nextInt(); final int k = sc.nextInt(); final int s = sc.nextInt() - 1; final int t = sc.nextInt() - 1; final int x = sc.nextInt() - 1; final List<List<Integer>> list_edge = new ArrayList<>(); for (int i = 0; i < n; i++) { list_edge.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int u = sc.nextInt() - 1; int v = sc.nextInt() - 1; list_edge.get(u).add(v); list_edge.get(v).add(u); } sc.close(); //dp[k][n][2]:=k回目の移動で、頂点nに到達し、且つxにmod2回到達した final long[][][] dp = new long[k + 1][n][2]; dp[0][s][0] = 1; for (int i = 1; i <= k; i++) { for (int prev = 0; prev < n; prev++) { if (dp[i - 1][prev][0] + dp[i - 1][prev][1] == 0) { continue; } for (int next : list_edge.get(prev)) { if (next == x) { dp[i][next][1] += dp[i - 1][prev][0]; dp[i][next][0] += dp[i - 1][prev][1]; } else { dp[i][next][0] += dp[i - 1][prev][0]; dp[i][next][1] += dp[i - 1][prev][1]; } dp[i][next][0] %= MOD; dp[i][next][1] %= MOD; } } } System.out.println(dp[k][t][0]); } // FastScannerライブラリ static class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(java.io.InputStream input) { this.in = input; } 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') { 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()); } @Override public void close() throws Exception { in.close(); } } }
ConDefects/ConDefects/Code/abc244_e/Java/35083644
condefects-java_data_1327
import java.io.*; import java.math.*; import java.time.*; import java.util.*; import java.util.Map.Entry; class Main implements Runnable { public static void solve () { n = nextInt(); m = nextInt(); k = nextInt(); s = nextInt(); t = nextInt(); x = nextInt(); int mod = 998244353; DFS dfs = new DFS(n); for (int i=0; i<m; i++) dfs.addEdge(nextInt()-1, nextInt()-1, true); long[][][] dp = new long[k+1][n][2]; dp[0][s][0] = 1; for (int i=0; i<k; i++) { for (int j=0; j<n; j++) { for (int next : dfs.g.get(j)) { dp[i+1][next][next==x? 1 : 0] += dp[i][j][0]; dp[i+1][next][next==x? 0 : 1] += dp[i][j][1]; dp[i+1][next][next==x? 1 : 0] %= mod; dp[i+1][next][next==x? 0 : 1] %= mod; } } } println(dp[k][t][0]); } public static int n, m, k, s, t, x; public static class DFS { int nodeNum; List<List<Integer>> g; DFS (int nodeNum) { this.nodeNum = nodeNum; g = new ArrayList<>(); for (int i=0; i<nodeNum; i++) g.add(new ArrayList<>()); } void addEdge (int from, int to, boolean isDirected) { g.get(from).add(to); if (isDirected == true) g.get(to).add(from); } void dfs (int now, int prev) { for (int next : g.get(now)) { if (next == prev) continue; dfs(next, now); } } } /* * ############################################################################################ * # 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; } @Override public boolean equals (Object o) { Point p = (Point)o; if (this.x == p.x && this.y == p.y) return true; return false; } @Override public int hashCode () { return y*(infi+1) + x; } } /* * ############################################################################################## * # 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[][] nextIntTable(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);} public static void debug(Object... o) { for (int i=0; i<o.length; i++) { System.out.print(o[i] + " "); } System.out.println(""); } //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.time.*; import java.util.*; import java.util.Map.Entry; class Main implements Runnable { public static void solve () { n = nextInt(); m = nextInt(); k = nextInt(); s = nextInt()-1; t = nextInt()-1; x = nextInt()-1; int mod = 998244353; DFS dfs = new DFS(n); for (int i=0; i<m; i++) dfs.addEdge(nextInt()-1, nextInt()-1, true); long[][][] dp = new long[k+1][n][2]; dp[0][s][0] = 1; for (int i=0; i<k; i++) { for (int j=0; j<n; j++) { for (int next : dfs.g.get(j)) { dp[i+1][next][next==x? 1 : 0] += dp[i][j][0]; dp[i+1][next][next==x? 0 : 1] += dp[i][j][1]; dp[i+1][next][next==x? 1 : 0] %= mod; dp[i+1][next][next==x? 0 : 1] %= mod; } } } println(dp[k][t][0]); } public static int n, m, k, s, t, x; public static class DFS { int nodeNum; List<List<Integer>> g; DFS (int nodeNum) { this.nodeNum = nodeNum; g = new ArrayList<>(); for (int i=0; i<nodeNum; i++) g.add(new ArrayList<>()); } void addEdge (int from, int to, boolean isDirected) { g.get(from).add(to); if (isDirected == true) g.get(to).add(from); } void dfs (int now, int prev) { for (int next : g.get(now)) { if (next == prev) continue; dfs(next, now); } } } /* * ############################################################################################ * # 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; } @Override public boolean equals (Object o) { Point p = (Point)o; if (this.x == p.x && this.y == p.y) return true; return false; } @Override public int hashCode () { return y*(infi+1) + x; } } /* * ############################################################################################## * # 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[][] nextIntTable(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);} public static void debug(Object... o) { for (int i=0; i<o.length; i++) { System.out.print(o[i] + " "); } System.out.println(""); } //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/abc244_e/Java/40357690
condefects-java_data_1328
import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String t = sc.nextLine(); sc.close(); List<Integer> out = new ArrayList<>(); int i = 0; for (int j=0; j < t.length(); j++) { System.out.println("比較: " + s.charAt(i) + " と " + t.charAt(j)); if (s.charAt(i) == t.charAt(j)) { System.out.println(" マッチ!: " + s.charAt(i) + " と " + t.charAt(j)+ "at i=" + i + " j=" + j); i++; out.add(j+1); if (i == s.length()) break; } } StringBuilder sb = new StringBuilder(); for (int k : out) { sb.append(String.valueOf(k)); sb.append(" "); } System.out.println(sb); } } import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String t = sc.nextLine(); sc.close(); List<Integer> out = new ArrayList<>(); int i = 0; for (int j=0; j < t.length(); j++) { if (s.charAt(i) == t.charAt(j)) { i++; out.add(j+1); if (i == s.length()) break; } } StringBuilder sb = new StringBuilder(); for (int k : out) { sb.append(String.valueOf(k)); sb.append(" "); } System.out.println(sb); } }
ConDefects/ConDefects/Code/abc352_b/Java/53753979
condefects-java_data_1329
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String S = sc.next(); for(int i = 0; i <= 1; i++){ String hikaku = S.substring(i,i+1); int count = 0; for(int j = 0; j <= 2; j++){ if(hikaku.equals(S.substring(j,j+1))){ count++; } } if(count == 1){ System.out.println(hikaku); return; } } System.out.println(-1); } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String S = sc.next(); for(int i = 0; i <= 2; i++){ String hikaku = S.substring(i,i+1); int count = 0; for(int j = 0; j <= 2; j++){ if(hikaku.equals(S.substring(j,j+1))){ count++; } } if(count == 1){ System.out.println(hikaku); return; } } System.out.println(-1); } }
ConDefects/ConDefects/Code/abc260_a/Java/37934384
condefects-java_data_1330
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); String text = scan.nextLine(); char[] work = new char[text.length()]; for(int i = 0; i < text.length(); i++){ work[i] = text.charAt(i); } if(work[0] == work[1]){ if(work[1] == work[2]){ System.out.println(-1); }else{ System.out.println(work[2]); } }else{ if(work[1] == work[2]){ System.out.println(work[0]); }else{ if(work[0] == work[2]){ System.out.println(work[1]); }else{ System.out.println(-1); } } } } } import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scan = new Scanner(System.in); String text = scan.nextLine(); char[] work = new char[text.length()]; for(int i = 0; i < text.length(); i++){ work[i] = text.charAt(i); } if(work[0] == work[1]){ if(work[1] == work[2]){ System.out.println(-1); }else{ System.out.println(work[2]); } }else{ if(work[1] == work[2]){ System.out.println(work[0]); }else{ if(work[0] == work[2]){ System.out.println(work[1]); }else{ System.out.println(work[0]); } } } } }
ConDefects/ConDefects/Code/abc260_a/Java/46204168
condefects-java_data_1331
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s =scanner.next(); if ((s.charAt(0) == s.charAt(1)) && (s.charAt(1) == s.charAt(2))){ System.out.println(-1); } else if (s.charAt(0) == s.charAt(1)){ System.out.print(s.charAt(2)); } else if (s.charAt(0) == s.charAt(2)){ System.out.print(s.charAt(1)); } else if (s.charAt(1) == s.charAt(2)){ System.out.println(s.charAt(0)); } else{ System.out.println(-1); } } } import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s =scanner.next(); if ((s.charAt(0) == s.charAt(1)) && (s.charAt(1) == s.charAt(2))){ System.out.println(-1); } else if (s.charAt(0) == s.charAt(1)){ System.out.print(s.charAt(2)); } else if (s.charAt(0) == s.charAt(2)){ System.out.print(s.charAt(1)); } else if (s.charAt(1) == s.charAt(2)){ System.out.println(s.charAt(0)); } else{ System.out.println(s.charAt(0)); } } }
ConDefects/ConDefects/Code/abc260_a/Java/41596313
condefects-java_data_1332
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner input = new Scanner(System.in); String s = input.next(); if(s.charAt(0) != s.charAt(1) && s.charAt(0) != s.charAt(2)) { System.out.println(s.charAt(0)); }else if(s.charAt(1) != s.charAt(2) && s.charAt(1) != s.charAt(2)){ System.out.println(s.charAt(1)); }else if(s.charAt(2) != s.charAt(0) && s.charAt(1) != s.charAt(2)){ System.out.println(s.charAt(2)); }else { System.out.println(-1); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner input = new Scanner(System.in); String s = input.next(); if(s.charAt(0) != s.charAt(1) && s.charAt(0) != s.charAt(2)) { System.out.println(s.charAt(0)); }else if(s.charAt(1) != s.charAt(2) && s.charAt(1) != s.charAt(0)){ System.out.println(s.charAt(1)); }else if(s.charAt(2) != s.charAt(0) && s.charAt(1) != s.charAt(2)){ System.out.println(s.charAt(2)); }else { System.out.println(-1); } } }
ConDefects/ConDefects/Code/abc260_a/Java/41077629
condefects-java_data_1333
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = Integer.parseInt(sc.next()); int m = Integer.parseInt(sc.next()); int k = Integer.parseInt(sc.next()); HashMap<Integer, Character> keySet = new HashMap<>(); for (int i = 0; i < m; i++) { int C = Integer.parseInt(sc.next()); int keys = 0; for (int j = 0; j < C; j++) { int A = Integer.parseInt(sc.next()); keys |= 1 << (A - 1); } char r = sc.next().charAt(0); if (keySet.containsKey(keys)) { if (keySet.get(keys) == r) { System.out.println(0); return; } } keySet.put(keys, r); } System.out.println(test(k, n, keySet)); } private static int test(int k, int n, HashMap<Integer, Character> keySets) { int count = 0; for (int keySet = 0; keySet < 1 << n; keySet++) { boolean flag = true; for (int keys: keySets.keySet()) { char r = keySets.get(keys); if ((r == 'o' && Integer.bitCount(keys & keySet) < k) || (r == 'x' && Integer.bitCount(keys & keySet) >= k)) { flag = false; break; } } if (flag) { count++; } } return count; } private static 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; }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;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); 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(); } } } } import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = Integer.parseInt(sc.next()); int m = Integer.parseInt(sc.next()); int k = Integer.parseInt(sc.next()); HashMap<Integer, Character> keySet = new HashMap<>(); for (int i = 0; i < m; i++) { int C = Integer.parseInt(sc.next()); int keys = 0; for (int j = 0; j < C; j++) { int A = Integer.parseInt(sc.next()); keys |= 1 << (A - 1); } char r = sc.next().charAt(0); if (keySet.containsKey(keys)) { if (keySet.get(keys) != r) { System.out.println(0); return; } } keySet.put(keys, r); } System.out.println(test(k, n, keySet)); } private static int test(int k, int n, HashMap<Integer, Character> keySets) { int count = 0; for (int keySet = 0; keySet < 1 << n; keySet++) { boolean flag = true; for (int keys: keySets.keySet()) { char r = keySets.get(keys); if ((r == 'o' && Integer.bitCount(keys & keySet) < k) || (r == 'x' && Integer.bitCount(keys & keySet) >= k)) { flag = false; break; } } if (flag) { count++; } } return count; } private static 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; }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;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); 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(); } } } }
ConDefects/ConDefects/Code/abc356_c/Java/54186571
condefects-java_data_1334
import java.io.*; import java.util.*; class Main { static boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; static void dbg(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));} public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int T = fs.nextInt(); // for (int tt=0; tt<T; tt++) { // } int n = fs.nextInt(); int m = fs.nextInt(); int k = fs.nextInt(); int[] result = new int[m]; int[][] test = new int[m][n]; for(int i=0; i<m; i++) { int c = fs.nextInt(); for(int j=0; j<c; j++) { int x = fs.nextInt(); test[i][x-1] = 1; } String res = fs.next(); if (res.equals("o")) { result[i] = 1; } else { result[i] = 0; } } int ans = 0; for(int i=0; i<(1<<n); i++) { int[] currentPerm = new int[n]; for(int j=0; j<n; j++) { if ((i & (1<<j)) > 0) { currentPerm[j] = 1; } } boolean flag = true; for(int j=0; j<m; j++) { int goodKeys = 0; for(int l=0; l<n; l++) { if (currentPerm[l] == test[j][l]) { goodKeys++; } } if(goodKeys>=k && result[j]==0) { flag = false; break; } if(goodKeys<k && result[j]==1) { flag = false; break; } } if (flag) { ans++; } } out.println(ans); out.close(); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } } import java.io.*; import java.util.*; class Main { static boolean LOCAL = System.getProperty("ONLINE_JUDGE")==null; static void dbg(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));} public static void main(String[] args) { FastScanner fs=new FastScanner(); PrintWriter out = new PrintWriter(System.out); // int T = fs.nextInt(); // for (int tt=0; tt<T; tt++) { // } int n = fs.nextInt(); int m = fs.nextInt(); int k = fs.nextInt(); int[] result = new int[m]; int[][] test = new int[m][n]; for(int i=0; i<m; i++) { int c = fs.nextInt(); for(int j=0; j<c; j++) { int x = fs.nextInt(); test[i][x-1] = 1; } String res = fs.next(); if (res.equals("o")) { result[i] = 1; } else { result[i] = 0; } } int ans = 0; for(int i=0; i<(1<<n); i++) { int[] currentPerm = new int[n]; for(int j=0; j<n; j++) { if ((i & (1<<j)) > 0) { currentPerm[j] = 1; } } boolean flag = true; for(int j=0; j<m; j++) { int goodKeys = 0; for(int l=0; l<n; l++) { if (currentPerm[l] == 1 && 1 == test[j][l]) { goodKeys++; } } if(goodKeys>=k && result[j]==0) { flag = false; break; } if(goodKeys<k && result[j]==1) { flag = false; break; } } if (flag) { ans++; } } out.println(ans); out.close(); } static final Random random=new Random(); static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long[] readArrayLong(int n) { long[] a=new long[n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; } } }
ConDefects/ConDefects/Code/abc356_c/Java/54183682
condefects-java_data_1335
import java.util.*; public class Main { public static final int MOD998 = 998244353; public static final int MOD100 = 1000000007; public static void main(String[] args) throws Exception { ContestScanner sc = new ContestScanner(); ContestPrinter cp = new ContestPrinter(); int H = sc.nextInt(); int W = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if (a == 1) { rect(1, 1, H, b - 1); rect(H, b, 2, W); rect(1, W, a, b); } else if (b == 1) { rect(1, 1, a - 1, W); rect(a, W, H, 2); rect(H, 1, a, b); } else if (H == 2) { rect(1, 1, 2, b - 1); rect(1, b, 1, W); rect(2, W, 2, b); } else if (W == 2) { rect(1, 1, a - 1, 2); rect(a, 1, H, 1); rect(H, 2, a, 2); } else if (a == H) { if (b == W) { rect(1, 1, H, W); } else { rect(1, 1, H - 2, W); rect(H - 1, W, H, b + 1); rect(H - 1, b, H - 1, 1); rect(H, 1, H, b); } } else if (b == W) { rect(1, 1, H, W - 2); rect(H, W - 1, a + 1, W); rect(a, W - 1, 1, W - 1); rect(1, W, a, W); } else { rect(1, 1, H, b - 1); rect(H, b, a + 1, W); rect(a, W, 1, b + 1); rect(1, b, a, b); } cp.close(); } static void rect(int x1, int y1, int x2, int y2) { if (x1 < x2) { if (y1 < y2) { boolean flag = true; for (int n = x1 + y1; n <= x2 + y2; n++) { if (flag) { for (int x = x1; x <= x2; x++) { if (n - x >= y1 && n - x <= y2) { System.out.println(x + " " + (n - x)); } } } else { for (int x = x2; x >= x1; x--) { if (n - x >= y1 && n - x <= y2) { System.out.println(x + " " + (n - x)); } } } flag = !flag; } } else { boolean flag = true; for (int n = x1 - y1; n <= x2 - y2; n++) { if (flag) { for (int x = x1; x <= x2; x++) { if (x - n >= y2 && x - n <= y1) { System.out.println(x + " " + (x - n)); } } } else { for (int x = x2; x >= x1; x--) { if (x - n >= y2 && x - n <= y1) { System.out.println(x + " " + (x - n)); } } } flag = !flag; } } } else if (x1 > x2) { if (y1 < y2) { boolean flag = true; for (int n = -x1 + y1; n <= -x2 + y2; n++) { if (flag) { for (int x = x2; x <= x1; x++) { if (n + x >= y1 && n + x <= y2) { System.out.println(x + " " + (n + x)); } } } else { for (int x = x1; x >= x2; x--) { if (n + x >= y1 && n + x <= y2) { System.out.println(x + " " + (n + x)); } } } flag = !flag; } } else { boolean flag = true; for (int n = -x1 - y1; n <= -x2 - y2; n++) { if (flag) { for (int x = x2; x <= x1; x++) { if (-n - x >= y2 && -n - x <= y1) { System.out.println(x + " " + (-n - x)); } } } else { for (int x = x1; x >= x2; x--) { if (-n - x >= y2 && -n - x <= y1) { System.out.println(x + " " + (-n - x)); } } } flag = !flag; } } } } ////////////////// // My Library // ////////////////// public static class SlopeTrick { private PriorityQueue<Long> lq = new PriorityQueue<>(Comparator.reverseOrder()); private PriorityQueue<Long> rq = new PriorityQueue<>(); private long lshift = 0; private long rshift = 0; private long min = 0; public long getMin() { return min; } public long get(long x) { long val = min; for (long l : lq) { if (l - x > 0) { val += l - x; } } for (long r : rq) { if (x - r > 0) { val += x - r; } } return val; } public long getMinPosLeft() { return lq.isEmpty() ? Long.MIN_VALUE : lq.peek() + lshift; } public long getMinPosRight() { return rq.isEmpty() ? Long.MAX_VALUE : rq.peek() + rshift; } public void addConst(long a) { min += a; } public void addSlopeRight(long a) { if (!lq.isEmpty() && lq.peek() + lshift > a) { min += lq.peek() + lshift - a; lq.add(a - lshift); rq.add(lq.poll() + lshift - rshift); } else { rq.add(a - rshift); } } public void addSlopeLeft(long a) { if (!rq.isEmpty() && rq.peek() < a) { min += a - rq.peek() - rshift; rq.add(a - rshift); lq.add(rq.poll() + rshift - lshift); } else { lq.add(a - lshift); } } public void addAbs(long a) { addSlopeLeft(a); addSlopeRight(a); } public void shift(long a) { lshift += a; rshift += a; } public void slideLeft(long a) { lshift += a; } public void slideRight(long a) { rshift += a; } public void clearLeft() { lq.clear(); } public void clearRight() { rq.clear(); } public void clearMin() { min = 0; } } public static int zeroOneBFS(int[][][] weighted_graph, int start, int goal) { int[] dist = new int[weighted_graph.length]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; LinkedList<Integer> queue = new LinkedList<>(); queue.add(start); while (!queue.isEmpty()) { int now = queue.poll(); if (now == goal) { return dist[goal]; } for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; if (info[1] == 0) { queue.addFirst(info[0]); } else { queue.addLast(info[0]); } } } } return -1; } public static int[] zeroOneBFSAll(int[][][] weighted_graph, int start) { int[] dist = new int[weighted_graph.length]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; LinkedList<Integer> queue = new LinkedList<>(); queue.add(start); while (!queue.isEmpty()) { int now = queue.poll(); for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; if (info[1] == 0) { queue.addFirst(info[0]); } else { queue.addLast(info[0]); } } } } return dist; } public static long dijkstra(int[][][] weighted_graph, int start, int goal) { long[] dist = new long[weighted_graph.length]; Arrays.fill(dist, 0, dist.length, Long.MAX_VALUE); dist[start] = 0; PriorityQueue<Pair<Integer, Long>> unsettled = new PriorityQueue<>((u, v) -> (int) (u.cdr - v.cdr)); unsettled.offer(new Pair<Integer, Long>(start, 0L)); while (!unsettled.isEmpty()) { Pair<Integer, Long> pair = unsettled.poll(); int now = pair.car; if (now == goal) { return dist[goal]; } if (dist[now] < pair.cdr) { continue; } for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; unsettled.offer(new Pair<Integer, Long>(info[0], dist[info[0]])); } } } return -1; } public static long[] dijkstraAll(int[][][] weighted_graph, int start) { long[] dist = new long[weighted_graph.length]; Arrays.fill(dist, 0, dist.length, Long.MAX_VALUE); dist[start] = 0; PriorityQueue<Pair<Integer, Long>> unsettled = new PriorityQueue<>((u, v) -> (int) (u.cdr - v.cdr)); unsettled.offer(new Pair<Integer, Long>(start, 0L)); while (!unsettled.isEmpty()) { Pair<Integer, Long> pair = unsettled.poll(); int now = pair.car; if (dist[now] < pair.cdr) { continue; } for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; unsettled.offer(new Pair<Integer, Long>(info[0], dist[info[0]])); } } } return dist; } public static long countLatticePoint(int[] p1, int[] p2, boolean include_end) { long difx = p2[0] - p1[0]; long dify = p2[1] - p1[1]; if (difx == 0 && dify == 0) { return include_end ? 1 : 0; } if (difx == 0 || dify == 0) { return Math.abs(difx + dify) + (include_end ? 1 : -1); } return MathLib.gcd(difx, dify) + (include_end ? 1 : -1); } public static long countLatticePoint(long[] p1, long[] p2, boolean include_end) { long difx = p2[0] - p1[0]; long dify = p2[1] - p1[1]; if (difx == 0 && dify == 0) { return include_end ? 1 : 0; } if (difx == 0 || dify == 0) { return Math.abs(difx + dify) + (include_end ? 1 : -1); } return MathLib.gcd(difx, dify) + (include_end ? 1 : -1); } // Don't contain same points! public static long countLatticePoint(int[] p1, int[] p2, int[] p3, boolean include_edge) { int[][] arr = new int[][] { p1, p2, p3 }; Arrays.sort(arr, Comparator.comparingInt(p -> ((int[]) p)[0]).thenComparingInt(p -> ((int[]) p)[1])); if ((p2[0] - p1[0]) * (long) (p3[1] - p2[1]) == (p2[1] - p1[1]) * (long) (p3[0] - p2[0])) { return countLatticePoint(arr[0], arr[2], true) - (include_edge ? 0 : 3); } long b = countLatticePoint(p1, p2, true) + countLatticePoint(p2, p3, true) + countLatticePoint(p3, p1, true) - 3; long i = (getAreaTriangle(p1, p2, p3) - b) / 2 + 1; return include_edge ? i + b : i; } public static long getAreaTriangle(int[] p1, int[] p2, int[] p3) { int x1 = p2[0] - p1[0]; int x2 = p3[0] - p2[0]; int y1 = p2[1] - p1[1]; int y2 = p3[1] - p2[1]; return Math.abs((long) x1 * y2 - (long) x2 * y1); } // Don't contain same points! public static long countLatticePointConvex(int[][] points, boolean include_edge) { if (points.length == 1) { return include_edge ? 1 : 0; } if (points.length == 2) { return countLatticePoint(points[0], points[1], include_edge); } long s = 0; for (int n = 1; n < points.length - 1; n++) { s += getAreaTriangle(points[0], points[n], points[n + 1]); } long b = countLatticePoint(points[points.length - 1], points[0], true) - points.length; for (int n = 0; n < points.length - 1; n++) { b += countLatticePoint(points[n], points[n + 1], true); } long i = (s - b) / 2 + 1; return include_edge ? i + b : i; } public static class RationalAngle implements Comparable<RationalAngle> { public long x; public long y; public static boolean include_pi_to_minus = true; public RationalAngle(long x, long y) { if (x == 0) { this.x = x; if (y == 0) { throw new UnsupportedOperationException("Angle to (0, 0) is invalid."); } else { this.y = y > 0 ? 1 : -1; } } else if (y == 0) { this.x = x > 0 ? 1 : -1; this.y = 0; } else { long gcd = MathLib.gcd(x, y); this.x = x / gcd; this.y = y / gcd; } } public RationalAngle copy() { return new RationalAngle(x, y); } public RationalAngle add(RationalAngle a) { RationalAngle res = copy(); res.addArg(a); return res; } public void addArg(RationalAngle a) { long nx = x * a.x - y * a.y; long ny = y * a.x + x * a.y; x = nx; y = ny; } public RationalAngle sub(RationalAngle a) { RationalAngle res = copy(); res.subArg(a); return res; } public void subArg(RationalAngle a) { long nx = x * a.x + y * a.y; long ny = y * a.x - x * a.y; x = nx; y = ny; } public boolean equals(RationalAngle a) { return x == a.x && y == a.y; } public boolean parallel(RationalAngle a) { return x == a.x && y == a.y || x == -a.x && y == -a.y; } public int rotDirection(RationalAngle trg) { if (parallel(trg)) { return 0; } else if (trg.sub(this).y > 0) { return 1; } else { return -1; } } public RationalAngle minus() { return new RationalAngle(x, -y); } public RationalAngle rev() { return new RationalAngle(-x, -y); } public double toRadian() { return Math.atan2(y, x); } private int toQuad() { if (x == 0) { if (y > 0) { return 2; } else { return -2; } } else if (x > 0) { if (y == 0) { return 0; } else if (y > 0) { return 1; } else { return -1; } } else { if (y == 0) { return include_pi_to_minus ? -4 : 4; } else if (y > 0) { return 3; } else { return -3; } } } @Override public int compareTo(RationalAngle ra) { if (ra == null) { throw new NullPointerException(); } int me = toQuad(); int you = ra.toQuad(); if (me > you) { return 1; } else if (me < you) { return -1; } long sub = sub(ra).y; if (sub == 0) { return 0; } else if (sub > 0) { return 1; } else { return -1; } } } public static class Pair<A, B> { public final A car; public final B cdr; public Pair(A car_, B cdr_) { car = car_; cdr = cdr_; } private static boolean eq(Object o1, Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } private static int hc(Object o) { return o == null ? 0 : o.hashCode(); } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) return false; Pair<?, ?> rhs = (Pair<?, ?>) o; return eq(car, rhs.car) && eq(cdr, rhs.cdr); } @Override public int hashCode() { return hc(car) ^ hc(cdr); } } public static class Tuple1<A> extends Pair<A, Object> { public Tuple1(A a) { super(a, null); } } public static class Tuple2<A, B> extends Pair<A, Tuple1<B>> { public Tuple2(A a, B b) { super(a, new Tuple1<>(b)); } } public static class Tuple3<A, B, C> extends Pair<A, Tuple2<B, C>> { public Tuple3(A a, B b, C c) { super(a, new Tuple2<>(b, c)); } } public static class Tuple4<A, B, C, D> extends Pair<A, Tuple3<B, C, D>> { public Tuple4(A a, B b, C c, D d) { super(a, new Tuple3<>(b, c, d)); } } public static class Tuple5<A, B, C, D, E> extends Pair<A, Tuple4<B, C, D, E>> { public Tuple5(A a, B b, C c, D d, E e) { super(a, new Tuple4<>(b, c, d, e)); } } public static class PriorityQueueLogTime<T> { private PriorityQueue<T> queue; private Multiset<T> total; private int size = 0; public PriorityQueueLogTime() { queue = new PriorityQueue<>(); total = new Multiset<>(); } public PriorityQueueLogTime(Comparator<T> c) { queue = new PriorityQueue<>(c); total = new Multiset<>(); } public void clear() { queue.clear(); total.clear(); size = 0; } public boolean contains(T e) { return total.count(e) > 0; } public boolean isEmpty() { return size == 0; } public boolean offer(T e) { total.addOne(e); size++; return queue.offer(e); } public T peek() { if (total.isEmpty()) { return null; } simplify(); return queue.peek(); } public T poll() { if (total.isEmpty()) { return null; } simplify(); size--; T res = queue.poll(); total.removeOne(res); return res; } public void remove(T e) { total.removeOne(e); size--; } public int size() { return size; } private void simplify() { while (total.count(queue.peek()) == 0) { queue.poll(); } } } static int[][] scanGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) { int[][] arr = sc.nextIntArrayMulti(edge, 2); for (int n = 0; n < edge; n++) { arr[0][n]--; arr[1][n]--; } return GraphBuilder.makeGraph(node, edge, arr[0], arr[1], undirected); } static int[][][] scanWeightedGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) { int[][] arr = sc.nextIntArrayMulti(edge, 3); for (int n = 0; n < edge; n++) { arr[0][n]--; arr[1][n]--; } return GraphBuilder.makeGraphWithWeight(node, edge, arr[0], arr[1], arr[2], undirected); } static class EdgeData { private int capacity; private int[] from, to, weight; private int p = 0; private boolean weighted; public EdgeData(boolean weighted) { this(weighted, 500000); } public EdgeData(boolean weighted, int initial_capacity) { capacity = initial_capacity; from = new int[capacity]; to = new int[capacity]; weight = new int[capacity]; this.weighted = weighted; } public void addEdge(int u, int v) { if (weighted) { System.err.println("The graph is weighted!"); return; } if (p == capacity) { int[] newfrom = new int[capacity * 2]; int[] newto = new int[capacity * 2]; System.arraycopy(from, 0, newfrom, 0, capacity); System.arraycopy(to, 0, newto, 0, capacity); capacity *= 2; from = newfrom; to = newto; } from[p] = u; to[p] = v; p++; } public void addEdge(int u, int v, int w) { if (!weighted) { System.err.println("The graph is NOT weighted!"); return; } if (p == capacity) { int[] newfrom = new int[capacity * 2]; int[] newto = new int[capacity * 2]; int[] newweight = new int[capacity * 2]; System.arraycopy(from, 0, newfrom, 0, capacity); System.arraycopy(to, 0, newto, 0, capacity); System.arraycopy(weight, 0, newweight, 0, capacity); capacity *= 2; from = newfrom; to = newto; weight = newweight; } from[p] = u; to[p] = v; weight[p] = w; p++; } public int[] getFrom() { int[] result = new int[p]; System.arraycopy(from, 0, result, 0, p); return result; } public int[] getTo() { int[] result = new int[p]; System.arraycopy(to, 0, result, 0, p); return result; } public int[] getWeight() { int[] result = new int[p]; System.arraycopy(weight, 0, result, 0, p); return result; } public int size() { return p; } } //////////////////////////////// // Atcoder Library for Java // //////////////////////////////// static class MathLib { private static long safe_mod(long x, long m) { x %= m; if (x < 0) x += m; return x; } private static long[] inv_gcd(long a, long b) { a = safe_mod(a, b); if (a == 0) return new long[] { b, 0 }; long s = b, t = a; long m0 = 0, m1 = 1; while (t > 0) { long u = s / t; s -= t * u; m0 -= m1 * u; long tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return new long[] { s, m0 }; } public static long gcd(long a, long b) { a = java.lang.Math.abs(a); b = java.lang.Math.abs(b); return inv_gcd(a, b)[0]; } public static long lcm(long a, long b) { a = java.lang.Math.abs(a); b = java.lang.Math.abs(b); return a / gcd(a, b) * b; } public static long pow_mod(long x, long n, int m) { assert n >= 0; assert m >= 1; if (m == 1) return 0L; x = safe_mod(x, m); long ans = 1L; while (n > 0) { if ((n & 1) == 1) ans = (ans * x) % m; x = (x * x) % m; n >>>= 1; } return ans; } public static long[] crt(long[] r, long[] m) { assert (r.length == m.length); int n = r.length; long r0 = 0, m0 = 1; for (int i = 0; i < n; i++) { assert (1 <= m[i]); long r1 = safe_mod(r[i], m[i]), m1 = m[i]; if (m0 < m1) { long tmp = r0; r0 = r1; r1 = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 % m1 == 0) { if (r0 % m1 != r1) return new long[] { 0, 0 }; continue; } long[] ig = inv_gcd(m0, m1); long g = ig[0], im = ig[1]; long u1 = m1 / g; if ((r1 - r0) % g != 0) return new long[] { 0, 0 }; long x = (r1 - r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if (r0 < 0) r0 += m0; // System.err.printf("%d %d\n", r0, m0); } return new long[] { r0, m0 }; } public static long floor_sum(long n, long m, long a, long b) { long ans = 0; if (a >= m) { ans += (n - 1) * n * (a / m) / 2; a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } long y_max = (a * n + b) / m; long x_max = y_max * m - b; if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans += floor_sum(y_max, a, m, (a - x_max % a) % a); return ans; } public static java.util.ArrayList<Long> divisors(long n) { java.util.ArrayList<Long> divisors = new ArrayList<>(); java.util.ArrayList<Long> large = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { divisors.add(i); if (i * i < n) large.add(n / i); } for (int p = large.size() - 1; p >= 0; p--) { divisors.add(large.get(p)); } return divisors; } } static class Multiset<T> extends java.util.TreeMap<T, Long> { public Multiset() { super(); } public Multiset(java.util.List<T> list) { super(); for (T e : list) this.addOne(e); } public long count(Object elm) { return getOrDefault(elm, 0L); } public void add(T elm, long amount) { if (!this.containsKey(elm)) put(elm, amount); else replace(elm, get(elm) + amount); if (this.count(elm) == 0) this.remove(elm); } public void addOne(T elm) { this.add(elm, 1); } public void removeOne(T elm) { this.add(elm, -1); } public void removeAll(T elm) { this.add(elm, -this.count(elm)); } public static <T> Multiset<T> merge(Multiset<T> a, Multiset<T> b) { Multiset<T> c = new Multiset<>(); for (T x : a.keySet()) c.add(x, a.count(x)); for (T y : b.keySet()) c.add(y, b.count(y)); return c; } } static class GraphBuilder { public static int[][] makeGraph(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected) { int[][] graph = new int[NumberOfNodes][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = to[i]; if (undirected) graph[to[i]][--outdegree[to[i]]] = from[i]; } return graph; } public static int[][][] makeGraphWithWeight(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, int[] weight, boolean undirected) { int[][][] graph = new int[NumberOfNodes][][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]][]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i] }; if (undirected) graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i] }; } return graph; } public static int[][][] makeGraphWithEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected) { int[][][] graph = new int[NumberOfNodes][][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]][]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], i, 0 }; if (undirected) graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], i, 1 }; } return graph; } public static int[][][] makeGraphWithWeightAndEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, int[] weight, boolean undirected) { int[][][] graph = new int[NumberOfNodes][][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]][]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i], i, 0 }; if (undirected) graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i], i, 1 }; } return graph; } } static class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } } static class ModIntFactory { private final ModArithmetic ma; private final int mod; private final boolean usesMontgomery; private final ModArithmetic.ModArithmeticMontgomery maMontgomery; private ArrayList<Integer> factorial; private ArrayList<Integer> factorial_inversion; public ModIntFactory(int mod) { this.ma = ModArithmetic.of(mod); this.usesMontgomery = ma instanceof ModArithmetic.ModArithmeticMontgomery; this.maMontgomery = usesMontgomery ? (ModArithmetic.ModArithmeticMontgomery) ma : null; this.mod = mod; this.factorial = new ArrayList<>(); this.factorial_inversion = new ArrayList<>(); } public ModInt create(long value) { if ((value %= mod) < 0) value += mod; if (usesMontgomery) { return new ModInt(maMontgomery.generate(value)); } else { return new ModInt((int) value); } } private void prepareFactorial(int max) { factorial.ensureCapacity(max + 1); if (factorial.size() == 0) factorial.add(1); for (int i = factorial.size(); i <= max; i++) { factorial.add(ma.mul(factorial.get(i - 1), i)); } } public ModInt factorial(int i) { prepareFactorial(i); return create(factorial.get(i)); } public ModInt permutation(int n, int r) { if (n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); if (factorial_inversion.size() > n) { return create(ma.mul(factorial.get(n), factorial_inversion.get(n - r))); } return create(ma.div(factorial.get(n), factorial.get(n - r))); } public ModInt combination(int n, int r) { if (n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); if (factorial_inversion.size() > n) { return create( ma.mul(factorial.get(n), ma.mul(factorial_inversion.get(n - r), factorial_inversion.get(r)))); } return create(ma.div(factorial.get(n), ma.mul(factorial.get(r), factorial.get(n - r)))); } public void prepareFactorialInv(int max) { prepareFactorial(max); factorial_inversion.ensureCapacity(max + 1); for (int i = factorial_inversion.size(); i <= max; i++) { factorial_inversion.add(ma.inv(factorial.get(i))); } } public int getMod() { return mod; } public class ModInt { private int value; private ModInt(int value) { this.value = value; } public int mod() { return mod; } public int value() { if (ma instanceof ModArithmetic.ModArithmeticMontgomery) { return ((ModArithmetic.ModArithmeticMontgomery) ma).reduce(value); } return value; } public ModInt add(ModInt mi) { return new ModInt(ma.add(value, mi.value)); } public ModInt add(ModInt mi1, ModInt mi2) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt add(ModInt mi1, ModInt... mis) { ModInt mi = add(mi1); for (ModInt m : mis) mi.addAsg(m); return mi; } public ModInt add(long mi) { return new ModInt(ma.add(value, ma.remainder(mi))); } public ModInt sub(ModInt mi) { return new ModInt(ma.sub(value, mi.value)); } public ModInt sub(long mi) { return new ModInt(ma.sub(value, ma.remainder(mi))); } public ModInt mul(ModInt mi) { return new ModInt(ma.mul(value, mi.value)); } public ModInt mul(ModInt mi1, ModInt mi2) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mul(ModInt mi1, ModInt... mis) { ModInt mi = mul(mi1); for (ModInt m : mis) mi.mulAsg(m); return mi; } public ModInt mul(long mi) { return new ModInt(ma.mul(value, ma.remainder(mi))); } public ModInt div(ModInt mi) { return new ModInt(ma.div(value, mi.value)); } public ModInt div(long mi) { return new ModInt(ma.div(value, ma.remainder(mi))); } public ModInt inv() { return new ModInt(ma.inv(value)); } public ModInt pow(long b) { return new ModInt(ma.pow(value, b)); } public ModInt addAsg(ModInt mi) { this.value = ma.add(value, mi.value); return this; } public ModInt addAsg(ModInt mi1, ModInt mi2) { return addAsg(mi1).addAsg(mi2); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return addAsg(mi1).addAsg(mi2).addAsg(mi3); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return addAsg(mi1).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt addAsg(ModInt... mis) { for (ModInt m : mis) addAsg(m); return this; } public ModInt addAsg(long mi) { this.value = ma.add(value, ma.remainder(mi)); return this; } public ModInt subAsg(ModInt mi) { this.value = ma.sub(value, mi.value); return this; } public ModInt subAsg(long mi) { this.value = ma.sub(value, ma.remainder(mi)); return this; } public ModInt mulAsg(ModInt mi) { this.value = ma.mul(value, mi.value); return this; } public ModInt mulAsg(ModInt mi1, ModInt mi2) { return mulAsg(mi1).mulAsg(mi2); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mulAsg(ModInt... mis) { for (ModInt m : mis) mulAsg(m); return this; } public ModInt mulAsg(long mi) { this.value = ma.mul(value, ma.remainder(mi)); return this; } public ModInt divAsg(ModInt mi) { this.value = ma.div(value, mi.value); return this; } public ModInt divAsg(long mi) { this.value = ma.div(value, ma.remainder(mi)); return this; } @Override public String toString() { return String.valueOf(value()); } @Override public boolean equals(Object o) { if (o instanceof ModInt) { ModInt mi = (ModInt) o; return mod() == mi.mod() && value() == mi.value(); } return false; } @Override public int hashCode() { return (1 * 37 + mod()) * 37 + value(); } } private static abstract class ModArithmetic { abstract int mod(); abstract int remainder(long value); abstract int add(int a, int b); abstract int sub(int a, int b); abstract int mul(int a, int b); int div(int a, int b) { return mul(a, inv(b)); } int inv(int a) { int b = mod(); if (b == 1) return 0; long u = 1, v = 0; while (b >= 1) { int t = a / b; a -= t * b; int tmp1 = a; a = b; b = tmp1; u -= t * v; long tmp2 = u; u = v; v = tmp2; } if (a != 1) { throw new ArithmeticException("divide by zero"); } return remainder(u); } int pow(int a, long b) { if (b < 0) throw new ArithmeticException("negative power"); int r = 1; int x = a; while (b > 0) { if ((b & 1) == 1) r = mul(r, x); x = mul(x, x); b >>= 1; } return r; } static ModArithmetic of(int mod) { if (mod <= 0) { throw new IllegalArgumentException(); } else if (mod == 1) { return new ModArithmetic1(); } else if (mod == 2) { return new ModArithmetic2(); } else if (mod == 998244353) { return new ModArithmetic998244353(); } else if (mod == 1000000007) { return new ModArithmetic1000000007(); } else if ((mod & 1) == 1) { return new ModArithmeticMontgomery(mod); } else { return new ModArithmeticBarrett(mod); } } private static final class ModArithmetic1 extends ModArithmetic { int mod() { return 1; } int remainder(long value) { return 0; } int add(int a, int b) { return 0; } int sub(int a, int b) { return 0; } int mul(int a, int b) { return 0; } int pow(int a, long b) { return 0; } } private static final class ModArithmetic2 extends ModArithmetic { int mod() { return 2; } int remainder(long value) { return (int) (value & 1); } int add(int a, int b) { return a ^ b; } int sub(int a, int b) { return a ^ b; } int mul(int a, int b) { return a & b; } } private static final class ModArithmetic998244353 extends ModArithmetic { private final int mod = 998244353; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmetic1000000007 extends ModArithmetic { private final int mod = 1000000007; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmeticMontgomery extends ModArithmeticDynamic { private final long negInv; private final long r2; private ModArithmeticMontgomery(int mod) { super(mod); long inv = 0; long s = 1, t = 0; for (int i = 0; i < 32; i++) { if ((t & 1) == 0) { t += mod; inv += s; } t >>= 1; s <<= 1; } long r = (1l << 32) % mod; this.negInv = inv; this.r2 = (r * r) % mod; } private int generate(long x) { return reduce(x * r2); } private int reduce(long x) { x = (x + ((x * negInv) & 0xffff_ffffl) * mod) >>> 32; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return generate((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } @Override int inv(int a) { return super.inv(reduce(a)); } @Override int pow(int a, long b) { return generate(super.pow(a, b)); } } private static final class ModArithmeticBarrett extends ModArithmeticDynamic { private static final long mask = 0xffff_ffffl; private final long mh; private final long ml; private ModArithmeticBarrett(int mod) { super(mod); /** * m = floor(2^64/mod) 2^64 = p*mod + q, 2^32 = a*mod + b => (a*mod + b)^2 = * p*mod + q => p = mod*a^2 + 2ab + floor(b^2/mod) */ long a = (1l << 32) / mod; long b = (1l << 32) % mod; long m = a * a * mod + 2 * a * b + (b * b) / mod; mh = m >>> 32; ml = m & mask; } private int reduce(long x) { long z = (x & mask) * ml; z = (x & mask) * mh + (x >>> 32) * ml + (z >>> 32); z = (x >>> 32) * mh + (z >>> 32); x -= z * mod; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } } private static class ModArithmeticDynamic extends ModArithmetic { final int mod; ModArithmeticDynamic(int mod) { this.mod = mod; } int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int sum = a + b; return sum >= mod ? sum - mod : sum; } int sub(int a, int b) { int sum = a - b; return sum < 0 ? sum + mod : sum; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } } } static class Convolution { /** * Find a primitive root. * * @param m A prime number. * @return Primitive root. */ private static int primitiveRoot(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int[] divs = new int[20]; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long) (i) * i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { boolean ok = true; for (int i = 0; i < cnt; i++) { if (pow(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } /** * Power. * * @param x Parameter x. * @param n Parameter n. * @param m Mod. * @return n-th power of x mod m. */ private static long pow(long x, long n, int m) { if (m == 1) return 0; long r = 1; long y = x % m; while (n > 0) { if ((n & 1) != 0) r = (r * y) % m; y = (y * y) % m; n >>= 1; } return r; } /** * Ceil of power 2. * * @param n Value. * @return Ceil of power 2. */ private static int ceilPow2(int n) { int x = 0; while ((1L << x) < n) x++; return x; } /** * Garner's algorithm. * * @param c Mod convolution results. * @param mods Mods. * @return Result. */ private static long garner(long[] c, int[] mods) { int n = c.length + 1; long[] cnst = new long[n]; long[] coef = new long[n]; java.util.Arrays.fill(coef, 1); for (int i = 0; i < n - 1; i++) { int m1 = mods[i]; long v = (c[i] - cnst[i] + m1) % m1; v = v * pow(coef[i], m1 - 2, m1) % m1; for (int j = i + 1; j < n; j++) { long m2 = mods[j]; cnst[j] = (cnst[j] + coef[j] * v) % m2; coef[j] = (coef[j] * m1) % m2; } } return cnst[n - 1]; } /** * Pre-calculation for NTT. * * @param mod NTT Prime. * @param g Primitive root of mod. * @return Pre-calculation table. */ private static long[] sumE(int mod, int g) { long[] sum_e = new long[30]; long[] es = new long[30]; long[] ies = new long[30]; int cnt2 = Integer.numberOfTrailingZeros(mod - 1); long e = pow(g, (mod - 1) >> cnt2, mod); long ie = pow(e, mod - 2, mod); for (int i = cnt2; i >= 2; i--) { es[i - 2] = e; ies[i - 2] = ie; e = e * e % mod; ie = ie * ie % mod; } long now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_e[i] = es[i] * now % mod; now = now * ies[i] % mod; } return sum_e; } /** * Pre-calculation for inverse NTT. * * @param mod Mod. * @param g Primitive root of mod. * @return Pre-calculation table. */ private static long[] sumIE(int mod, int g) { long[] sum_ie = new long[30]; long[] es = new long[30]; long[] ies = new long[30]; int cnt2 = Integer.numberOfTrailingZeros(mod - 1); long e = pow(g, (mod - 1) >> cnt2, mod); long ie = pow(e, mod - 2, mod); for (int i = cnt2; i >= 2; i--) { es[i - 2] = e; ies[i - 2] = ie; e = e * e % mod; ie = ie * ie % mod; } long now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_ie[i] = ies[i] * now % mod; now = now * es[i] % mod; } return sum_ie; } /** * Inverse NTT. * * @param a Target array. * @param sumIE Pre-calculation table. * @param mod NTT Prime. */ private static void butterflyInv(long[] a, long[] sumIE, int mod) { int n = a.length; int h = ceilPow2(n); for (int ph = h; ph >= 1; ph--) { int w = 1 << (ph - 1), p = 1 << (h - ph); long inow = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { long l = a[i + offset]; long r = a[i + offset + p]; a[i + offset] = (l + r) % mod; a[i + offset + p] = (mod + l - r) * inow % mod; } int x = Integer.numberOfTrailingZeros(~s); inow = inow * sumIE[x] % mod; } } } /** * Inverse NTT. * * @param a Target array. * @param sumE Pre-calculation table. * @param mod NTT Prime. */ private static void butterfly(long[] a, long[] sumE, int mod) { int n = a.length; int h = ceilPow2(n); for (int ph = 1; ph <= h; ph++) { int w = 1 << (ph - 1), p = 1 << (h - ph); long now = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { long l = a[i + offset]; long r = a[i + offset + p] * now % mod; a[i + offset] = (l + r) % mod; a[i + offset + p] = (l - r + mod) % mod; } int x = Integer.numberOfTrailingZeros(~s); now = now * sumE[x] % mod; } } } /** * Convolution. * * @param a Target array 1. * @param b Target array 2. * @param mod NTT Prime. * @return Answer. */ public static long[] convolution(long[] a, long[] b, int mod) { int n = a.length; int m = b.length; if (n == 0 || m == 0) return new long[0]; int z = 1 << ceilPow2(n + m - 1); { long[] na = new long[z]; long[] nb = new long[z]; System.arraycopy(a, 0, na, 0, n); System.arraycopy(b, 0, nb, 0, m); a = na; b = nb; } int g = primitiveRoot(mod); long[] sume = sumE(mod, g); long[] sumie = sumIE(mod, g); butterfly(a, sume, mod); butterfly(b, sume, mod); for (int i = 0; i < z; i++) { a[i] = a[i] * b[i] % mod; } butterflyInv(a, sumie, mod); a = java.util.Arrays.copyOf(a, n + m - 1); long iz = pow(z, mod - 2, mod); for (int i = 0; i < n + m - 1; i++) a[i] = a[i] * iz % mod; return a; } /** * Convolution. * * @param a Target array 1. * @param b Target array 2. * @param mod Any mod. * @return Answer. */ public static long[] convolutionLL(long[] a, long[] b, int mod) { int n = a.length; int m = b.length; if (n == 0 || m == 0) return new long[0]; int mod1 = 754974721; int mod2 = 167772161; int mod3 = 469762049; long[] c1 = convolution(a, b, mod1); long[] c2 = convolution(a, b, mod2); long[] c3 = convolution(a, b, mod3); int retSize = c1.length; long[] ret = new long[retSize]; int[] mods = { mod1, mod2, mod3, mod }; for (int i = 0; i < retSize; ++i) { ret[i] = garner(new long[] { c1[i], c2[i], c3[i] }, mods); } return ret; } /** * Convolution by ModInt. * * @param a Target array 1. * @param b Target array 2. * @return Answer. */ public static java.util.List<ModIntFactory.ModInt> convolution(java.util.List<ModIntFactory.ModInt> a, java.util.List<ModIntFactory.ModInt> b) { int mod = a.get(0).mod(); long[] va = a.stream().mapToLong(ModIntFactory.ModInt::value).toArray(); long[] vb = b.stream().mapToLong(ModIntFactory.ModInt::value).toArray(); long[] c = convolutionLL(va, vb, mod); ModIntFactory factory = new ModIntFactory(mod); return java.util.Arrays.stream(c).mapToObj(factory::create).collect(java.util.stream.Collectors.toList()); } /** * Naive convolution. (Complexity is O(N^2)!!) * * @param a Target array 1. * @param b Target array 2. * @param mod Mod. * @return Answer. */ public static long[] convolutionNaive(long[] a, long[] b, int mod) { int n = a.length; int m = b.length; int k = n + m - 1; long[] ret = new long[k]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[i + j] += a[i] * b[j] % mod; ret[i + j] %= mod; } } return ret; } } static class SCC { static class Edge { int from, to; public Edge(int from, int to) { this.from = from; this.to = to; } } final int n; int m; final java.util.ArrayList<Edge> unorderedEdges; final int[] start; final int[] ids; boolean hasBuilt = false; public SCC(int n) { this.n = n; this.unorderedEdges = new java.util.ArrayList<>(); this.start = new int[n + 1]; this.ids = new int[n]; } public void addEdge(int from, int to) { rangeCheck(from); rangeCheck(to); unorderedEdges.add(new Edge(from, to)); start[from + 1]++; this.m++; } public int id(int i) { if (!hasBuilt) { throw new UnsupportedOperationException("Graph hasn't been built."); } rangeCheck(i); return ids[i]; } public int[][] build() { for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } Edge[] orderedEdges = new Edge[m]; int[] count = new int[n + 1]; System.arraycopy(start, 0, count, 0, n + 1); for (Edge e : unorderedEdges) { orderedEdges[count[e.from]++] = e; } int nowOrd = 0; int groupNum = 0; int k = 0; // parent int[] par = new int[n]; int[] vis = new int[n]; int[] low = new int[n]; int[] ord = new int[n]; java.util.Arrays.fill(ord, -1); // u = lower32(stack[i]) : visiting vertex // j = upper32(stack[i]) : jth child long[] stack = new long[n]; // size of stack int ptr = 0; // non-recursional DFS for (int i = 0; i < n; i++) { if (ord[i] >= 0) continue; par[i] = -1; // vertex i, 0th child. stack[ptr++] = 0l << 32 | i; // stack is not empty while (ptr > 0) { // last element long p = stack[--ptr]; // vertex int u = (int) (p & 0xffff_ffffl); // jth child int j = (int) (p >>> 32); if (j == 0) { // first visit low[u] = ord[u] = nowOrd++; vis[k++] = u; } if (start[u] + j < count[u]) { // there are more children // jth child int to = orderedEdges[start[u] + j].to; // incr children counter stack[ptr++] += 1l << 32; if (ord[to] == -1) { // new vertex stack[ptr++] = 0l << 32 | to; par[to] = u; } else { // backward edge low[u] = Math.min(low[u], ord[to]); } } else { // no more children (leaving) while (j-- > 0) { int to = orderedEdges[start[u] + j].to; // update lowlink if (par[to] == u) low[u] = Math.min(low[u], low[to]); } if (low[u] == ord[u]) { // root of a component while (true) { // gathering verticies int v = vis[--k]; ord[v] = n; ids[v] = groupNum; if (v == u) break; } groupNum++; // incr the number of components } } } } for (int i = 0; i < n; i++) { ids[i] = groupNum - 1 - ids[i]; } int[] counts = new int[groupNum]; for (int x : ids) counts[x]++; int[][] groups = new int[groupNum][]; for (int i = 0; i < groupNum; i++) { groups[i] = new int[counts[i]]; } for (int i = 0; i < n; i++) { int cmp = ids[i]; groups[cmp][--counts[cmp]] = i; } hasBuilt = true; return groups; } private void rangeCheck(int i) { if (i < 0 || i >= n) { throw new IndexOutOfBoundsException(String.format("Index %d out of bounds for length %d", i, n)); } } } static 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() { 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[][] nextIntArrayMulti(int length, int width) { int[][] arrays = new int[width][length]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) arrays[j][i] = this.nextInt(); } return arrays; } 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; } } static class ContestPrinter extends java.io.PrintWriter { public ContestPrinter(java.io.PrintStream stream) { super(stream); } 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; 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; 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; 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; 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); } } static class Permutation implements java.util.Iterator<int[]>, Iterable<int[]> { private int[] next; public Permutation(int n) { next = java.util.stream.IntStream.range(0, n).toArray(); } @Override public boolean hasNext() { return next != null; } @Override public int[] next() { int[] r = next.clone(); next = nextPermutation(next); return r; } @Override public java.util.Iterator<int[]> iterator() { return this; } public static int[] nextPermutation(int[] a) { if (a == null || a.length < 2) return null; int p = 0; for (int i = a.length - 2; i >= 0; i--) { if (a[i] >= a[i + 1]) continue; p = i; break; } int q = 0; for (int i = a.length - 1; i > p; i--) { if (a[i] <= a[p]) continue; q = i; break; } if (p == 0 && q == 0) return null; int temp = a[p]; a[p] = a[q]; a[q] = temp; int l = p, r = a.length; while (++l < --r) { temp = a[l]; a[l] = a[r]; a[r] = temp; } return a; } } 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)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } static class LazySegTree<S, F> { final int MAX; final int N; final int Log; final java.util.function.BinaryOperator<S> Op; final S E; final java.util.function.BiFunction<F, S, S> Mapping; final java.util.function.BinaryOperator<F> Composition; final F Id; final S[] Dat; final F[] Laz; @SuppressWarnings("unchecked") public LazySegTree(int n, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping, java.util.function.BinaryOperator<F> composition, F id) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.Log = Integer.numberOfTrailingZeros(N); this.Op = op; this.E = e; this.Mapping = mapping; this.Composition = composition; this.Id = id; this.Dat = (S[]) new Object[N << 1]; this.Laz = (F[]) new Object[N]; java.util.Arrays.fill(Dat, E); java.util.Arrays.fill(Laz, Id); } public LazySegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping, java.util.function.BinaryOperator<F> composition, F id) { this(dat.length, op, e, mapping, composition, id); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, Dat, N, l); for (int i = N - 1; i > 0; i--) { Dat[i] = Op.apply(Dat[i << 1 | 0], Dat[i << 1 | 1]); } } private void push(int k) { if (Laz[k] == Id) return; int lk = k << 1 | 0, rk = k << 1 | 1; Dat[lk] = Mapping.apply(Laz[k], Dat[lk]); Dat[rk] = Mapping.apply(Laz[k], Dat[rk]); if (lk < N) Laz[lk] = Composition.apply(Laz[k], Laz[lk]); if (rk < N) Laz[rk] = Composition.apply(Laz[k], Laz[rk]); Laz[k] = Id; } private void pushTo(int k) { for (int i = Log; i > 0; i--) push(k >> i); } private void pushTo(int lk, int rk) { for (int i = Log; i > 0; i--) { if (((lk >> i) << i) != lk) push(lk >> i); if (((rk >> i) << i) != rk) push(rk >> i); } } private void updateFrom(int k) { k >>= 1; while (k > 0) { Dat[k] = Op.apply(Dat[k << 1 | 0], Dat[k << 1 | 1]); k >>= 1; } } private void updateFrom(int lk, int rk) { for (int i = 1; i <= Log; i++) { if (((lk >> i) << i) != lk) { int lki = lk >> i; Dat[lki] = Op.apply(Dat[lki << 1 | 0], Dat[lki << 1 | 1]); } if (((rk >> i) << i) != rk) { int rki = (rk - 1) >> i; Dat[rki] = Op.apply(Dat[rki << 1 | 0], Dat[rki << 1 | 1]); } } } public void set(int p, S x) { exclusiveRangeCheck(p); p += N; pushTo(p); Dat[p] = x; updateFrom(p); } public S get(int p) { exclusiveRangeCheck(p); p += N; pushTo(p); return Dat[p]; } 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); if (l == r) return E; l += N; r += N; pushTo(l, r); S sumLeft = E, sumRight = E; while (l < r) { if ((l & 1) == 1) sumLeft = Op.apply(sumLeft, Dat[l++]); if ((r & 1) == 1) sumRight = Op.apply(Dat[--r], sumRight); l >>= 1; r >>= 1; } return Op.apply(sumLeft, sumRight); } public S allProd() { return Dat[1]; } public void apply(int p, F f) { exclusiveRangeCheck(p); p += N; pushTo(p); Dat[p] = Mapping.apply(f, Dat[p]); updateFrom(p); } public void apply(int l, int r, F f) { if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); if (l == r) return; l += N; r += N; pushTo(l, r); for (int l2 = l, r2 = r; l2 < r2;) { if ((l2 & 1) == 1) { Dat[l2] = Mapping.apply(f, Dat[l2]); if (l2 < N) Laz[l2] = Composition.apply(f, Laz[l2]); l2++; } if ((r2 & 1) == 1) { r2--; Dat[r2] = Mapping.apply(f, Dat[r2]); if (r2 < N) Laz[r2] = Composition.apply(f, Laz[r2]); } l2 >>= 1; r2 >>= 1; } updateFrom(l, r); } public int maxRight(int l, java.util.function.Predicate<S> g) { inclusiveRangeCheck(l); if (!g.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; pushTo(l); S sum = E; do { l >>= Integer.numberOfTrailingZeros(l); if (!g.test(Op.apply(sum, Dat[l]))) { while (l < N) { push(l); l = l << 1; if (g.test(Op.apply(sum, Dat[l]))) { sum = Op.apply(sum, Dat[l]); l++; } } return l - N; } sum = Op.apply(sum, Dat[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> g) { inclusiveRangeCheck(r); if (!g.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; pushTo(r - 1); S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!g.test(Op.apply(Dat[r], sum))) { while (r < N) { push(r); r = r << 1 | 1; if (g.test(Op.apply(Dat[r], sum))) { sum = Op.apply(Dat[r], sum); r--; } } return r + 1 - N; } sum = Op.apply(Dat[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 is not in [%d, %d).", p, 0, MAX)); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException(String.format("Index %d is not in [%d, %d].", p, 0, MAX)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } private S[] simulatePushAll() { S[] simDat = java.util.Arrays.copyOf(Dat, 2 * N); F[] simLaz = java.util.Arrays.copyOf(Laz, 2 * N); for (int k = 1; k < N; k++) { if (simLaz[k] == Id) continue; int lk = k << 1 | 0, rk = k << 1 | 1; simDat[lk] = Mapping.apply(simLaz[k], simDat[lk]); simDat[rk] = Mapping.apply(simLaz[k], simDat[rk]); if (lk < N) simLaz[lk] = Composition.apply(simLaz[k], simLaz[lk]); if (rk < N) simLaz[rk] = Composition.apply(simLaz[k], simLaz[rk]); simLaz[k] = Id; } return simDat; } public String toDetailedString() { return toDetailedString(1, 0, simulatePushAll()); } private String toDetailedString(int k, int sp, S[] dat) { if (k >= N) return indent(sp) + dat[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent, dat); s += "\n"; s += indent(sp) + dat[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent, dat); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { S[] dat = simulatePushAll(); StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(dat[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } static class MaxFlow { private static final class InternalCapEdge { final int to; final int rev; long cap; InternalCapEdge(int to, int rev, long cap) { this.to = to; this.rev = rev; this.cap = cap; } } public static final class CapEdge { public final int from, to; public final long cap, flow; CapEdge(int from, int to, long cap, long flow) { this.from = from; this.to = to; this.cap = cap; this.flow = flow; } @Override public boolean equals(Object o) { if (o instanceof CapEdge) { CapEdge e = (CapEdge) o; return from == e.from && to == e.to && cap == e.cap && flow == e.flow; } return false; } } private static final class IntPair { final int first, second; IntPair(int first, int second) { this.first = first; this.second = second; } } static final long INF = Long.MAX_VALUE; private final int n; private final java.util.ArrayList<IntPair> pos; private final java.util.ArrayList<InternalCapEdge>[] g; @SuppressWarnings("unchecked") public MaxFlow(int n) { this.n = n; this.pos = new java.util.ArrayList<>(); this.g = new java.util.ArrayList[n]; for (int i = 0; i < n; i++) { this.g[i] = new java.util.ArrayList<>(); } } public int addEdge(int from, int to, long cap) { rangeCheck(from, 0, n); rangeCheck(to, 0, n); nonNegativeCheck(cap, "Capacity"); int m = pos.size(); pos.add(new IntPair(from, g[from].size())); int fromId = g[from].size(); int toId = g[to].size(); if (from == to) toId++; g[from].add(new InternalCapEdge(to, toId, cap)); g[to].add(new InternalCapEdge(from, fromId, 0L)); return m; } private InternalCapEdge getInternalEdge(int i) { return g[pos.get(i).first].get(pos.get(i).second); } private InternalCapEdge getInternalEdgeReversed(InternalCapEdge e) { return g[e.to].get(e.rev); } public CapEdge getEdge(int i) { int m = pos.size(); rangeCheck(i, 0, m); InternalCapEdge e = getInternalEdge(i); InternalCapEdge re = getInternalEdgeReversed(e); return new CapEdge(re.to, e.to, e.cap + re.cap, re.cap); } public CapEdge[] getEdges() { CapEdge[] res = new CapEdge[pos.size()]; java.util.Arrays.setAll(res, this::getEdge); return res; } public void changeEdge(int i, long newCap, long newFlow) { int m = pos.size(); rangeCheck(i, 0, m); nonNegativeCheck(newCap, "Capacity"); if (newFlow > newCap) { throw new IllegalArgumentException( String.format("Flow %d is greater than the capacity %d.", newCap, newFlow)); } InternalCapEdge e = getInternalEdge(i); InternalCapEdge re = getInternalEdgeReversed(e); e.cap = newCap - newFlow; re.cap = newFlow; } public long maxFlow(int s, int t) { return flow(s, t, INF); } public long flow(int s, int t, long flowLimit) { rangeCheck(s, 0, n); rangeCheck(t, 0, n); long flow = 0L; int[] level = new int[n]; int[] que = new int[n]; int[] iter = new int[n]; while (flow < flowLimit) { bfs(s, t, level, que); if (level[t] < 0) break; java.util.Arrays.fill(iter, 0); while (flow < flowLimit) { long d = dfs(t, s, flowLimit - flow, iter, level); if (d == 0) break; flow += d; } } return flow; } private void bfs(int s, int t, int[] level, int[] que) { java.util.Arrays.fill(level, -1); int hd = 0, tl = 0; que[tl++] = s; level[s] = 0; while (hd < tl) { int u = que[hd++]; for (InternalCapEdge e : g[u]) { int v = e.to; if (e.cap == 0 || level[v] >= 0) continue; level[v] = level[u] + 1; if (v == t) return; que[tl++] = v; } } } private long dfs(int cur, int s, long flowLimit, int[] iter, int[] level) { if (cur == s) return flowLimit; long res = 0; int curLevel = level[cur]; for (int itMax = g[cur].size(); iter[cur] < itMax; iter[cur]++) { int i = iter[cur]; InternalCapEdge e = g[cur].get(i); InternalCapEdge re = getInternalEdgeReversed(e); if (curLevel <= level[e.to] || re.cap == 0) continue; long d = dfs(e.to, s, Math.min(flowLimit - res, re.cap), iter, level); if (d <= 0) continue; e.cap += d; re.cap -= d; res += d; if (res == flowLimit) break; } return res; } public boolean[] minCut(int s) { rangeCheck(s, 0, n); boolean[] visited = new boolean[n]; int[] stack = new int[n]; int ptr = 0; stack[ptr++] = s; visited[s] = true; while (ptr > 0) { int u = stack[--ptr]; for (InternalCapEdge e : g[u]) { int v = e.to; if (e.cap > 0 && !visited[v]) { visited[v] = true; stack[ptr++] = v; } } } return visited; } private void rangeCheck(int i, int minInclusive, int maxExclusive) { if (i < 0 || i >= maxExclusive) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for length %d", i, maxExclusive)); } } private void nonNegativeCheck(long cap, String attribute) { if (cap < 0) { throw new IllegalArgumentException(String.format("%s %d is negative.", attribute, cap)); } } } static class StringAlgorithm { private static int[] saNaive(int[] s) { int n = s.length; int[] sa = new int[n]; for (int i = 0; i < n; i++) { sa[i] = i; } insertionsortUsingComparator(sa, (l, r) -> { while (l < n && r < n) { if (s[l] != s[r]) return s[l] - s[r]; l++; r++; } return -(l - r); }); return sa; } public static int[] saDoubling(int[] s) { int n = s.length; int[] sa = new int[n]; for (int i = 0; i < n; i++) { sa[i] = i; } int[] rnk = java.util.Arrays.copyOf(s, n); int[] tmp = new int[n]; for (int k = 1; k < n; k *= 2) { final int _k = k; final int[] _rnk = rnk; java.util.function.IntBinaryOperator cmp = (x, y) -> { if (_rnk[x] != _rnk[y]) return _rnk[x] - _rnk[y]; int rx = x + _k < n ? _rnk[x + _k] : -1; int ry = y + _k < n ? _rnk[y + _k] : -1; return rx - ry; }; mergesortUsingComparator(sa, cmp); tmp[sa[0]] = 0; for (int i = 1; i < n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (cmp.applyAsInt(sa[i - 1], sa[i]) < 0 ? 1 : 0); } int[] buf = tmp; tmp = rnk; rnk = buf; } return sa; } private static void insertionsortUsingComparator(int[] a, java.util.function.IntBinaryOperator comparator) { final int n = a.length; for (int i = 1; i < n; i++) { final int tmp = a[i]; if (comparator.applyAsInt(a[i - 1], tmp) > 0) { int j = i; do { a[j] = a[j - 1]; j--; } while (j > 0 && comparator.applyAsInt(a[j - 1], tmp) > 0); a[j] = tmp; } } } private static void mergesortUsingComparator(int[] a, java.util.function.IntBinaryOperator comparator) { final int n = a.length; final int[] work = new int[n]; for (int block = 1; block <= n; block <<= 1) { final int block2 = block << 1; for (int l = 0, max = n - block; l < max; l += block2) { int m = l + block; int r = Math.min(l + block2, n); System.arraycopy(a, l, work, 0, block); for (int i = l, wi = 0, ti = m;; i++) { if (ti == r) { System.arraycopy(work, wi, a, i, block - wi); break; } if (comparator.applyAsInt(work[wi], a[ti]) > 0) { a[i] = a[ti++]; } else { a[i] = work[wi++]; if (wi == block) break; } } } } } private static final int THRESHOLD_NAIVE = 50; // private static final int THRESHOLD_DOUBLING = 0; private static int[] sais(int[] s, int upper) { int n = s.length; if (n == 0) return new int[0]; if (n == 1) return new int[] { 0 }; if (n == 2) { if (s[0] < s[1]) { return new int[] { 0, 1 }; } else { return new int[] { 1, 0 }; } } if (n < THRESHOLD_NAIVE) { return saNaive(s); } // if (n < THRESHOLD_DOUBLING) { // return saDoubling(s); // } int[] sa = new int[n]; boolean[] ls = new boolean[n]; for (int i = n - 2; i >= 0; i--) { ls[i] = s[i] == s[i + 1] ? ls[i + 1] : s[i] < s[i + 1]; } int[] sumL = new int[upper + 1]; int[] sumS = new int[upper + 1]; for (int i = 0; i < n; i++) { if (ls[i]) { sumL[s[i] + 1]++; } else { sumS[s[i]]++; } } for (int i = 0; i <= upper; i++) { sumS[i] += sumL[i]; if (i < upper) sumL[i + 1] += sumS[i]; } java.util.function.Consumer<int[]> induce = lms -> { java.util.Arrays.fill(sa, -1); int[] buf = new int[upper + 1]; System.arraycopy(sumS, 0, buf, 0, upper + 1); for (int d : lms) { if (d == n) continue; sa[buf[s[d]]++] = d; } System.arraycopy(sumL, 0, buf, 0, upper + 1); sa[buf[s[n - 1]]++] = n - 1; for (int i = 0; i < n; i++) { int v = sa[i]; if (v >= 1 && !ls[v - 1]) { sa[buf[s[v - 1]]++] = v - 1; } } System.arraycopy(sumL, 0, buf, 0, upper + 1); for (int i = n - 1; i >= 0; i--) { int v = sa[i]; if (v >= 1 && ls[v - 1]) { sa[--buf[s[v - 1] + 1]] = v - 1; } } }; int[] lmsMap = new int[n + 1]; java.util.Arrays.fill(lmsMap, -1); int m = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lmsMap[i] = m++; } } int[] lms = new int[m]; { int p = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms[p++] = i; } } } induce.accept(lms); if (m > 0) { int[] sortedLms = new int[m]; { int p = 0; for (int v : sa) { if (lmsMap[v] != -1) { sortedLms[p++] = v; } } } int[] recS = new int[m]; int recUpper = 0; recS[lmsMap[sortedLms[0]]] = 0; for (int i = 1; i < m; i++) { int l = sortedLms[i - 1], r = sortedLms[i]; int endL = (lmsMap[l] + 1 < m) ? lms[lmsMap[l] + 1] : n; int endR = (lmsMap[r] + 1 < m) ? lms[lmsMap[r] + 1] : n; boolean same = true; if (endL - l != endR - r) { same = false; } else { while (l < endL && s[l] == s[r]) { l++; r++; } if (l == n || s[l] != s[r]) same = false; } if (!same) { recUpper++; } recS[lmsMap[sortedLms[i]]] = recUpper; } int[] recSA = sais(recS, recUpper); for (int i = 0; i < m; i++) { sortedLms[i] = lms[recSA[i]]; } induce.accept(sortedLms); } return sa; } public static int[] suffixArray(int[] s, int upper) { assert (0 <= upper); for (int d : s) { assert (0 <= d && d <= upper); } return sais(s, upper); } public static int[] suffixArray(int[] s) { int n = s.length; int[] vals = Arrays.copyOf(s, n); java.util.Arrays.sort(vals); int p = 1; for (int i = 1; i < n; i++) { if (vals[i] != vals[i - 1]) { vals[p++] = vals[i]; } } int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = java.util.Arrays.binarySearch(vals, 0, p, s[i]); } return sais(s2, p); } public static int[] suffixArray(char[] s) { int n = s.length; int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = s[i]; } return sais(s2, 255); } public static int[] suffixArray(java.lang.String s) { return suffixArray(s.toCharArray()); } public static int[] lcpArray(int[] s, int[] sa) { int n = s.length; assert (n >= 1); int[] rnk = new int[n]; for (int i = 0; i < n; i++) { rnk[sa[i]] = i; } int[] lcp = new int[n - 1]; int h = 0; for (int i = 0; i < n; i++) { if (h > 0) h--; if (rnk[i] == 0) { continue; } int j = sa[rnk[i] - 1]; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rnk[i] - 1] = h; } return lcp; } public static int[] lcpArray(char[] s, int[] sa) { int n = s.length; int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = s[i]; } return lcpArray(s2, sa); } public static int[] lcpArray(java.lang.String s, int[] sa) { return lcpArray(s.toCharArray(), sa); } public static int[] zAlgorithm(int[] s) { int n = s.length; if (n == 0) return new int[0]; int[] z = new int[n]; for (int i = 1, j = 0; i < n; i++) { int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; z[i] = k; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } public static int[] zAlgorithm(char[] s) { int n = s.length; if (n == 0) return new int[0]; int[] z = new int[n]; for (int i = 1, j = 0; i < n; i++) { int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; z[i] = k; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } public static int[] zAlgorithm(String s) { return zAlgorithm(s.toCharArray()); } } } import java.util.*; public class Main { public static final int MOD998 = 998244353; public static final int MOD100 = 1000000007; public static void main(String[] args) throws Exception { ContestScanner sc = new ContestScanner(); ContestPrinter cp = new ContestPrinter(); int H = sc.nextInt(); int W = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); if (a == 1) { rect(1, 1, H, b - 1); rect(H, b, 2, W); rect(1, W, a, b); } else if (b == 1) { rect(1, 1, a - 1, W); rect(a, W, H, 2); rect(H, 1, a, b); } else if (H == 2) { rect(1, 1, 2, b - 1); rect(1, b, 1, W); rect(2, W, 2, b); } else if (W == 2) { rect(1, 1, a - 1, 2); rect(a, 1, H, 1); rect(H, 2, a, 2); } else if (a == H) { if (b == W) { rect(1, 1, H, W); } else { rect(1, 1, H - 2, W); rect(H - 1, W, H, b + 1); rect(H - 1, b, H - 1, 1); rect(H, 1, H, b); } } else if (b == W) { rect(1, 1, H, W - 2); rect(H, W - 1, a + 1, W); rect(a, W - 1, 1, W - 1); rect(1, W, a, W); } else { rect(1, 1, H, b - 1); rect(H, b, a + 1, W); rect(a, W, 1, b + 1); rect(1, b, a, b); } cp.close(); } static void rect(int x1, int y1, int x2, int y2) { if (x1 < x2) { if (y1 < y2) { boolean flag = true; for (int n = x1 + y1; n <= x2 + y2; n++) { if (flag) { for (int x = x1; x <= x2; x++) { if (n - x >= y1 && n - x <= y2) { System.out.println(x + " " + (n - x)); } } } else { for (int x = x2; x >= x1; x--) { if (n - x >= y1 && n - x <= y2) { System.out.println(x + " " + (n - x)); } } } flag = !flag; } } else { boolean flag = true; for (int n = x1 - y1; n <= x2 - y2; n++) { if (flag) { for (int x = x1; x <= x2; x++) { if (x - n >= y2 && x - n <= y1) { System.out.println(x + " " + (x - n)); } } } else { for (int x = x2; x >= x1; x--) { if (x - n >= y2 && x - n <= y1) { System.out.println(x + " " + (x - n)); } } } flag = !flag; } } } else { if (y1 < y2) { boolean flag = true; for (int n = -x1 + y1; n <= -x2 + y2; n++) { if (flag) { for (int x = x2; x <= x1; x++) { if (n + x >= y1 && n + x <= y2) { System.out.println(x + " " + (n + x)); } } } else { for (int x = x1; x >= x2; x--) { if (n + x >= y1 && n + x <= y2) { System.out.println(x + " " + (n + x)); } } } flag = !flag; } } else { boolean flag = true; for (int n = -x1 - y1; n <= -x2 - y2; n++) { if (flag) { for (int x = x2; x <= x1; x++) { if (-n - x >= y2 && -n - x <= y1) { System.out.println(x + " " + (-n - x)); } } } else { for (int x = x1; x >= x2; x--) { if (-n - x >= y2 && -n - x <= y1) { System.out.println(x + " " + (-n - x)); } } } flag = !flag; } } } } ////////////////// // My Library // ////////////////// public static class SlopeTrick { private PriorityQueue<Long> lq = new PriorityQueue<>(Comparator.reverseOrder()); private PriorityQueue<Long> rq = new PriorityQueue<>(); private long lshift = 0; private long rshift = 0; private long min = 0; public long getMin() { return min; } public long get(long x) { long val = min; for (long l : lq) { if (l - x > 0) { val += l - x; } } for (long r : rq) { if (x - r > 0) { val += x - r; } } return val; } public long getMinPosLeft() { return lq.isEmpty() ? Long.MIN_VALUE : lq.peek() + lshift; } public long getMinPosRight() { return rq.isEmpty() ? Long.MAX_VALUE : rq.peek() + rshift; } public void addConst(long a) { min += a; } public void addSlopeRight(long a) { if (!lq.isEmpty() && lq.peek() + lshift > a) { min += lq.peek() + lshift - a; lq.add(a - lshift); rq.add(lq.poll() + lshift - rshift); } else { rq.add(a - rshift); } } public void addSlopeLeft(long a) { if (!rq.isEmpty() && rq.peek() < a) { min += a - rq.peek() - rshift; rq.add(a - rshift); lq.add(rq.poll() + rshift - lshift); } else { lq.add(a - lshift); } } public void addAbs(long a) { addSlopeLeft(a); addSlopeRight(a); } public void shift(long a) { lshift += a; rshift += a; } public void slideLeft(long a) { lshift += a; } public void slideRight(long a) { rshift += a; } public void clearLeft() { lq.clear(); } public void clearRight() { rq.clear(); } public void clearMin() { min = 0; } } public static int zeroOneBFS(int[][][] weighted_graph, int start, int goal) { int[] dist = new int[weighted_graph.length]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; LinkedList<Integer> queue = new LinkedList<>(); queue.add(start); while (!queue.isEmpty()) { int now = queue.poll(); if (now == goal) { return dist[goal]; } for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; if (info[1] == 0) { queue.addFirst(info[0]); } else { queue.addLast(info[0]); } } } } return -1; } public static int[] zeroOneBFSAll(int[][][] weighted_graph, int start) { int[] dist = new int[weighted_graph.length]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; LinkedList<Integer> queue = new LinkedList<>(); queue.add(start); while (!queue.isEmpty()) { int now = queue.poll(); for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; if (info[1] == 0) { queue.addFirst(info[0]); } else { queue.addLast(info[0]); } } } } return dist; } public static long dijkstra(int[][][] weighted_graph, int start, int goal) { long[] dist = new long[weighted_graph.length]; Arrays.fill(dist, 0, dist.length, Long.MAX_VALUE); dist[start] = 0; PriorityQueue<Pair<Integer, Long>> unsettled = new PriorityQueue<>((u, v) -> (int) (u.cdr - v.cdr)); unsettled.offer(new Pair<Integer, Long>(start, 0L)); while (!unsettled.isEmpty()) { Pair<Integer, Long> pair = unsettled.poll(); int now = pair.car; if (now == goal) { return dist[goal]; } if (dist[now] < pair.cdr) { continue; } for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; unsettled.offer(new Pair<Integer, Long>(info[0], dist[info[0]])); } } } return -1; } public static long[] dijkstraAll(int[][][] weighted_graph, int start) { long[] dist = new long[weighted_graph.length]; Arrays.fill(dist, 0, dist.length, Long.MAX_VALUE); dist[start] = 0; PriorityQueue<Pair<Integer, Long>> unsettled = new PriorityQueue<>((u, v) -> (int) (u.cdr - v.cdr)); unsettled.offer(new Pair<Integer, Long>(start, 0L)); while (!unsettled.isEmpty()) { Pair<Integer, Long> pair = unsettled.poll(); int now = pair.car; if (dist[now] < pair.cdr) { continue; } for (int[] info : weighted_graph[now]) { if (dist[info[0]] > dist[now] + info[1]) { dist[info[0]] = dist[now] + info[1]; unsettled.offer(new Pair<Integer, Long>(info[0], dist[info[0]])); } } } return dist; } public static long countLatticePoint(int[] p1, int[] p2, boolean include_end) { long difx = p2[0] - p1[0]; long dify = p2[1] - p1[1]; if (difx == 0 && dify == 0) { return include_end ? 1 : 0; } if (difx == 0 || dify == 0) { return Math.abs(difx + dify) + (include_end ? 1 : -1); } return MathLib.gcd(difx, dify) + (include_end ? 1 : -1); } public static long countLatticePoint(long[] p1, long[] p2, boolean include_end) { long difx = p2[0] - p1[0]; long dify = p2[1] - p1[1]; if (difx == 0 && dify == 0) { return include_end ? 1 : 0; } if (difx == 0 || dify == 0) { return Math.abs(difx + dify) + (include_end ? 1 : -1); } return MathLib.gcd(difx, dify) + (include_end ? 1 : -1); } // Don't contain same points! public static long countLatticePoint(int[] p1, int[] p2, int[] p3, boolean include_edge) { int[][] arr = new int[][] { p1, p2, p3 }; Arrays.sort(arr, Comparator.comparingInt(p -> ((int[]) p)[0]).thenComparingInt(p -> ((int[]) p)[1])); if ((p2[0] - p1[0]) * (long) (p3[1] - p2[1]) == (p2[1] - p1[1]) * (long) (p3[0] - p2[0])) { return countLatticePoint(arr[0], arr[2], true) - (include_edge ? 0 : 3); } long b = countLatticePoint(p1, p2, true) + countLatticePoint(p2, p3, true) + countLatticePoint(p3, p1, true) - 3; long i = (getAreaTriangle(p1, p2, p3) - b) / 2 + 1; return include_edge ? i + b : i; } public static long getAreaTriangle(int[] p1, int[] p2, int[] p3) { int x1 = p2[0] - p1[0]; int x2 = p3[0] - p2[0]; int y1 = p2[1] - p1[1]; int y2 = p3[1] - p2[1]; return Math.abs((long) x1 * y2 - (long) x2 * y1); } // Don't contain same points! public static long countLatticePointConvex(int[][] points, boolean include_edge) { if (points.length == 1) { return include_edge ? 1 : 0; } if (points.length == 2) { return countLatticePoint(points[0], points[1], include_edge); } long s = 0; for (int n = 1; n < points.length - 1; n++) { s += getAreaTriangle(points[0], points[n], points[n + 1]); } long b = countLatticePoint(points[points.length - 1], points[0], true) - points.length; for (int n = 0; n < points.length - 1; n++) { b += countLatticePoint(points[n], points[n + 1], true); } long i = (s - b) / 2 + 1; return include_edge ? i + b : i; } public static class RationalAngle implements Comparable<RationalAngle> { public long x; public long y; public static boolean include_pi_to_minus = true; public RationalAngle(long x, long y) { if (x == 0) { this.x = x; if (y == 0) { throw new UnsupportedOperationException("Angle to (0, 0) is invalid."); } else { this.y = y > 0 ? 1 : -1; } } else if (y == 0) { this.x = x > 0 ? 1 : -1; this.y = 0; } else { long gcd = MathLib.gcd(x, y); this.x = x / gcd; this.y = y / gcd; } } public RationalAngle copy() { return new RationalAngle(x, y); } public RationalAngle add(RationalAngle a) { RationalAngle res = copy(); res.addArg(a); return res; } public void addArg(RationalAngle a) { long nx = x * a.x - y * a.y; long ny = y * a.x + x * a.y; x = nx; y = ny; } public RationalAngle sub(RationalAngle a) { RationalAngle res = copy(); res.subArg(a); return res; } public void subArg(RationalAngle a) { long nx = x * a.x + y * a.y; long ny = y * a.x - x * a.y; x = nx; y = ny; } public boolean equals(RationalAngle a) { return x == a.x && y == a.y; } public boolean parallel(RationalAngle a) { return x == a.x && y == a.y || x == -a.x && y == -a.y; } public int rotDirection(RationalAngle trg) { if (parallel(trg)) { return 0; } else if (trg.sub(this).y > 0) { return 1; } else { return -1; } } public RationalAngle minus() { return new RationalAngle(x, -y); } public RationalAngle rev() { return new RationalAngle(-x, -y); } public double toRadian() { return Math.atan2(y, x); } private int toQuad() { if (x == 0) { if (y > 0) { return 2; } else { return -2; } } else if (x > 0) { if (y == 0) { return 0; } else if (y > 0) { return 1; } else { return -1; } } else { if (y == 0) { return include_pi_to_minus ? -4 : 4; } else if (y > 0) { return 3; } else { return -3; } } } @Override public int compareTo(RationalAngle ra) { if (ra == null) { throw new NullPointerException(); } int me = toQuad(); int you = ra.toQuad(); if (me > you) { return 1; } else if (me < you) { return -1; } long sub = sub(ra).y; if (sub == 0) { return 0; } else if (sub > 0) { return 1; } else { return -1; } } } public static class Pair<A, B> { public final A car; public final B cdr; public Pair(A car_, B cdr_) { car = car_; cdr = cdr_; } private static boolean eq(Object o1, Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } private static int hc(Object o) { return o == null ? 0 : o.hashCode(); } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) return false; Pair<?, ?> rhs = (Pair<?, ?>) o; return eq(car, rhs.car) && eq(cdr, rhs.cdr); } @Override public int hashCode() { return hc(car) ^ hc(cdr); } } public static class Tuple1<A> extends Pair<A, Object> { public Tuple1(A a) { super(a, null); } } public static class Tuple2<A, B> extends Pair<A, Tuple1<B>> { public Tuple2(A a, B b) { super(a, new Tuple1<>(b)); } } public static class Tuple3<A, B, C> extends Pair<A, Tuple2<B, C>> { public Tuple3(A a, B b, C c) { super(a, new Tuple2<>(b, c)); } } public static class Tuple4<A, B, C, D> extends Pair<A, Tuple3<B, C, D>> { public Tuple4(A a, B b, C c, D d) { super(a, new Tuple3<>(b, c, d)); } } public static class Tuple5<A, B, C, D, E> extends Pair<A, Tuple4<B, C, D, E>> { public Tuple5(A a, B b, C c, D d, E e) { super(a, new Tuple4<>(b, c, d, e)); } } public static class PriorityQueueLogTime<T> { private PriorityQueue<T> queue; private Multiset<T> total; private int size = 0; public PriorityQueueLogTime() { queue = new PriorityQueue<>(); total = new Multiset<>(); } public PriorityQueueLogTime(Comparator<T> c) { queue = new PriorityQueue<>(c); total = new Multiset<>(); } public void clear() { queue.clear(); total.clear(); size = 0; } public boolean contains(T e) { return total.count(e) > 0; } public boolean isEmpty() { return size == 0; } public boolean offer(T e) { total.addOne(e); size++; return queue.offer(e); } public T peek() { if (total.isEmpty()) { return null; } simplify(); return queue.peek(); } public T poll() { if (total.isEmpty()) { return null; } simplify(); size--; T res = queue.poll(); total.removeOne(res); return res; } public void remove(T e) { total.removeOne(e); size--; } public int size() { return size; } private void simplify() { while (total.count(queue.peek()) == 0) { queue.poll(); } } } static int[][] scanGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) { int[][] arr = sc.nextIntArrayMulti(edge, 2); for (int n = 0; n < edge; n++) { arr[0][n]--; arr[1][n]--; } return GraphBuilder.makeGraph(node, edge, arr[0], arr[1], undirected); } static int[][][] scanWeightedGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) { int[][] arr = sc.nextIntArrayMulti(edge, 3); for (int n = 0; n < edge; n++) { arr[0][n]--; arr[1][n]--; } return GraphBuilder.makeGraphWithWeight(node, edge, arr[0], arr[1], arr[2], undirected); } static class EdgeData { private int capacity; private int[] from, to, weight; private int p = 0; private boolean weighted; public EdgeData(boolean weighted) { this(weighted, 500000); } public EdgeData(boolean weighted, int initial_capacity) { capacity = initial_capacity; from = new int[capacity]; to = new int[capacity]; weight = new int[capacity]; this.weighted = weighted; } public void addEdge(int u, int v) { if (weighted) { System.err.println("The graph is weighted!"); return; } if (p == capacity) { int[] newfrom = new int[capacity * 2]; int[] newto = new int[capacity * 2]; System.arraycopy(from, 0, newfrom, 0, capacity); System.arraycopy(to, 0, newto, 0, capacity); capacity *= 2; from = newfrom; to = newto; } from[p] = u; to[p] = v; p++; } public void addEdge(int u, int v, int w) { if (!weighted) { System.err.println("The graph is NOT weighted!"); return; } if (p == capacity) { int[] newfrom = new int[capacity * 2]; int[] newto = new int[capacity * 2]; int[] newweight = new int[capacity * 2]; System.arraycopy(from, 0, newfrom, 0, capacity); System.arraycopy(to, 0, newto, 0, capacity); System.arraycopy(weight, 0, newweight, 0, capacity); capacity *= 2; from = newfrom; to = newto; weight = newweight; } from[p] = u; to[p] = v; weight[p] = w; p++; } public int[] getFrom() { int[] result = new int[p]; System.arraycopy(from, 0, result, 0, p); return result; } public int[] getTo() { int[] result = new int[p]; System.arraycopy(to, 0, result, 0, p); return result; } public int[] getWeight() { int[] result = new int[p]; System.arraycopy(weight, 0, result, 0, p); return result; } public int size() { return p; } } //////////////////////////////// // Atcoder Library for Java // //////////////////////////////// static class MathLib { private static long safe_mod(long x, long m) { x %= m; if (x < 0) x += m; return x; } private static long[] inv_gcd(long a, long b) { a = safe_mod(a, b); if (a == 0) return new long[] { b, 0 }; long s = b, t = a; long m0 = 0, m1 = 1; while (t > 0) { long u = s / t; s -= t * u; m0 -= m1 * u; long tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return new long[] { s, m0 }; } public static long gcd(long a, long b) { a = java.lang.Math.abs(a); b = java.lang.Math.abs(b); return inv_gcd(a, b)[0]; } public static long lcm(long a, long b) { a = java.lang.Math.abs(a); b = java.lang.Math.abs(b); return a / gcd(a, b) * b; } public static long pow_mod(long x, long n, int m) { assert n >= 0; assert m >= 1; if (m == 1) return 0L; x = safe_mod(x, m); long ans = 1L; while (n > 0) { if ((n & 1) == 1) ans = (ans * x) % m; x = (x * x) % m; n >>>= 1; } return ans; } public static long[] crt(long[] r, long[] m) { assert (r.length == m.length); int n = r.length; long r0 = 0, m0 = 1; for (int i = 0; i < n; i++) { assert (1 <= m[i]); long r1 = safe_mod(r[i], m[i]), m1 = m[i]; if (m0 < m1) { long tmp = r0; r0 = r1; r1 = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 % m1 == 0) { if (r0 % m1 != r1) return new long[] { 0, 0 }; continue; } long[] ig = inv_gcd(m0, m1); long g = ig[0], im = ig[1]; long u1 = m1 / g; if ((r1 - r0) % g != 0) return new long[] { 0, 0 }; long x = (r1 - r0) / g % u1 * im % u1; r0 += x * m0; m0 *= u1; if (r0 < 0) r0 += m0; // System.err.printf("%d %d\n", r0, m0); } return new long[] { r0, m0 }; } public static long floor_sum(long n, long m, long a, long b) { long ans = 0; if (a >= m) { ans += (n - 1) * n * (a / m) / 2; a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } long y_max = (a * n + b) / m; long x_max = y_max * m - b; if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans += floor_sum(y_max, a, m, (a - x_max % a) % a); return ans; } public static java.util.ArrayList<Long> divisors(long n) { java.util.ArrayList<Long> divisors = new ArrayList<>(); java.util.ArrayList<Long> large = new ArrayList<>(); for (long i = 1; i * i <= n; i++) if (n % i == 0) { divisors.add(i); if (i * i < n) large.add(n / i); } for (int p = large.size() - 1; p >= 0; p--) { divisors.add(large.get(p)); } return divisors; } } static class Multiset<T> extends java.util.TreeMap<T, Long> { public Multiset() { super(); } public Multiset(java.util.List<T> list) { super(); for (T e : list) this.addOne(e); } public long count(Object elm) { return getOrDefault(elm, 0L); } public void add(T elm, long amount) { if (!this.containsKey(elm)) put(elm, amount); else replace(elm, get(elm) + amount); if (this.count(elm) == 0) this.remove(elm); } public void addOne(T elm) { this.add(elm, 1); } public void removeOne(T elm) { this.add(elm, -1); } public void removeAll(T elm) { this.add(elm, -this.count(elm)); } public static <T> Multiset<T> merge(Multiset<T> a, Multiset<T> b) { Multiset<T> c = new Multiset<>(); for (T x : a.keySet()) c.add(x, a.count(x)); for (T y : b.keySet()) c.add(y, b.count(y)); return c; } } static class GraphBuilder { public static int[][] makeGraph(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected) { int[][] graph = new int[NumberOfNodes][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = to[i]; if (undirected) graph[to[i]][--outdegree[to[i]]] = from[i]; } return graph; } public static int[][][] makeGraphWithWeight(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, int[] weight, boolean undirected) { int[][][] graph = new int[NumberOfNodes][][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]][]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i] }; if (undirected) graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i] }; } return graph; } public static int[][][] makeGraphWithEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, boolean undirected) { int[][][] graph = new int[NumberOfNodes][][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]][]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], i, 0 }; if (undirected) graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], i, 1 }; } return graph; } public static int[][][] makeGraphWithWeightAndEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to, int[] weight, boolean undirected) { int[][][] graph = new int[NumberOfNodes][][]; int[] outdegree = new int[NumberOfNodes]; for (int i = 0; i < NumberOfEdges; i++) { outdegree[from[i]]++; if (undirected) outdegree[to[i]]++; } for (int i = 0; i < NumberOfNodes; i++) graph[i] = new int[outdegree[i]][]; for (int i = 0; i < NumberOfEdges; i++) { graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i], i, 0 }; if (undirected) graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i], i, 1 }; } return graph; } } static class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } java.util.ArrayList<java.util.ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new java.util.ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(java.util.ArrayList::isEmpty); return result; } } static class ModIntFactory { private final ModArithmetic ma; private final int mod; private final boolean usesMontgomery; private final ModArithmetic.ModArithmeticMontgomery maMontgomery; private ArrayList<Integer> factorial; private ArrayList<Integer> factorial_inversion; public ModIntFactory(int mod) { this.ma = ModArithmetic.of(mod); this.usesMontgomery = ma instanceof ModArithmetic.ModArithmeticMontgomery; this.maMontgomery = usesMontgomery ? (ModArithmetic.ModArithmeticMontgomery) ma : null; this.mod = mod; this.factorial = new ArrayList<>(); this.factorial_inversion = new ArrayList<>(); } public ModInt create(long value) { if ((value %= mod) < 0) value += mod; if (usesMontgomery) { return new ModInt(maMontgomery.generate(value)); } else { return new ModInt((int) value); } } private void prepareFactorial(int max) { factorial.ensureCapacity(max + 1); if (factorial.size() == 0) factorial.add(1); for (int i = factorial.size(); i <= max; i++) { factorial.add(ma.mul(factorial.get(i - 1), i)); } } public ModInt factorial(int i) { prepareFactorial(i); return create(factorial.get(i)); } public ModInt permutation(int n, int r) { if (n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); if (factorial_inversion.size() > n) { return create(ma.mul(factorial.get(n), factorial_inversion.get(n - r))); } return create(ma.div(factorial.get(n), factorial.get(n - r))); } public ModInt combination(int n, int r) { if (n < 0 || r < 0 || n < r) return create(0); prepareFactorial(n); if (factorial_inversion.size() > n) { return create( ma.mul(factorial.get(n), ma.mul(factorial_inversion.get(n - r), factorial_inversion.get(r)))); } return create(ma.div(factorial.get(n), ma.mul(factorial.get(r), factorial.get(n - r)))); } public void prepareFactorialInv(int max) { prepareFactorial(max); factorial_inversion.ensureCapacity(max + 1); for (int i = factorial_inversion.size(); i <= max; i++) { factorial_inversion.add(ma.inv(factorial.get(i))); } } public int getMod() { return mod; } public class ModInt { private int value; private ModInt(int value) { this.value = value; } public int mod() { return mod; } public int value() { if (ma instanceof ModArithmetic.ModArithmeticMontgomery) { return ((ModArithmetic.ModArithmeticMontgomery) ma).reduce(value); } return value; } public ModInt add(ModInt mi) { return new ModInt(ma.add(value, mi.value)); } public ModInt add(ModInt mi1, ModInt mi2) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3); } public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt add(ModInt mi1, ModInt... mis) { ModInt mi = add(mi1); for (ModInt m : mis) mi.addAsg(m); return mi; } public ModInt add(long mi) { return new ModInt(ma.add(value, ma.remainder(mi))); } public ModInt sub(ModInt mi) { return new ModInt(ma.sub(value, mi.value)); } public ModInt sub(long mi) { return new ModInt(ma.sub(value, ma.remainder(mi))); } public ModInt mul(ModInt mi) { return new ModInt(ma.mul(value, mi.value)); } public ModInt mul(ModInt mi1, ModInt mi2) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3); } public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mul(ModInt mi1, ModInt... mis) { ModInt mi = mul(mi1); for (ModInt m : mis) mi.mulAsg(m); return mi; } public ModInt mul(long mi) { return new ModInt(ma.mul(value, ma.remainder(mi))); } public ModInt div(ModInt mi) { return new ModInt(ma.div(value, mi.value)); } public ModInt div(long mi) { return new ModInt(ma.div(value, ma.remainder(mi))); } public ModInt inv() { return new ModInt(ma.inv(value)); } public ModInt pow(long b) { return new ModInt(ma.pow(value, b)); } public ModInt addAsg(ModInt mi) { this.value = ma.add(value, mi.value); return this; } public ModInt addAsg(ModInt mi1, ModInt mi2) { return addAsg(mi1).addAsg(mi2); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return addAsg(mi1).addAsg(mi2).addAsg(mi3); } public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return addAsg(mi1).addAsg(mi2).addAsg(mi3).addAsg(mi4); } public ModInt addAsg(ModInt... mis) { for (ModInt m : mis) addAsg(m); return this; } public ModInt addAsg(long mi) { this.value = ma.add(value, ma.remainder(mi)); return this; } public ModInt subAsg(ModInt mi) { this.value = ma.sub(value, mi.value); return this; } public ModInt subAsg(long mi) { this.value = ma.sub(value, ma.remainder(mi)); return this; } public ModInt mulAsg(ModInt mi) { this.value = ma.mul(value, mi.value); return this; } public ModInt mulAsg(ModInt mi1, ModInt mi2) { return mulAsg(mi1).mulAsg(mi2); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3); } public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) { return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4); } public ModInt mulAsg(ModInt... mis) { for (ModInt m : mis) mulAsg(m); return this; } public ModInt mulAsg(long mi) { this.value = ma.mul(value, ma.remainder(mi)); return this; } public ModInt divAsg(ModInt mi) { this.value = ma.div(value, mi.value); return this; } public ModInt divAsg(long mi) { this.value = ma.div(value, ma.remainder(mi)); return this; } @Override public String toString() { return String.valueOf(value()); } @Override public boolean equals(Object o) { if (o instanceof ModInt) { ModInt mi = (ModInt) o; return mod() == mi.mod() && value() == mi.value(); } return false; } @Override public int hashCode() { return (1 * 37 + mod()) * 37 + value(); } } private static abstract class ModArithmetic { abstract int mod(); abstract int remainder(long value); abstract int add(int a, int b); abstract int sub(int a, int b); abstract int mul(int a, int b); int div(int a, int b) { return mul(a, inv(b)); } int inv(int a) { int b = mod(); if (b == 1) return 0; long u = 1, v = 0; while (b >= 1) { int t = a / b; a -= t * b; int tmp1 = a; a = b; b = tmp1; u -= t * v; long tmp2 = u; u = v; v = tmp2; } if (a != 1) { throw new ArithmeticException("divide by zero"); } return remainder(u); } int pow(int a, long b) { if (b < 0) throw new ArithmeticException("negative power"); int r = 1; int x = a; while (b > 0) { if ((b & 1) == 1) r = mul(r, x); x = mul(x, x); b >>= 1; } return r; } static ModArithmetic of(int mod) { if (mod <= 0) { throw new IllegalArgumentException(); } else if (mod == 1) { return new ModArithmetic1(); } else if (mod == 2) { return new ModArithmetic2(); } else if (mod == 998244353) { return new ModArithmetic998244353(); } else if (mod == 1000000007) { return new ModArithmetic1000000007(); } else if ((mod & 1) == 1) { return new ModArithmeticMontgomery(mod); } else { return new ModArithmeticBarrett(mod); } } private static final class ModArithmetic1 extends ModArithmetic { int mod() { return 1; } int remainder(long value) { return 0; } int add(int a, int b) { return 0; } int sub(int a, int b) { return 0; } int mul(int a, int b) { return 0; } int pow(int a, long b) { return 0; } } private static final class ModArithmetic2 extends ModArithmetic { int mod() { return 2; } int remainder(long value) { return (int) (value & 1); } int add(int a, int b) { return a ^ b; } int sub(int a, int b) { return a ^ b; } int mul(int a, int b) { return a & b; } } private static final class ModArithmetic998244353 extends ModArithmetic { private final int mod = 998244353; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmetic1000000007 extends ModArithmetic { private final int mod = 1000000007; int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int res = a + b; return res >= mod ? res - mod : res; } int sub(int a, int b) { int res = a - b; return res < 0 ? res + mod : res; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } private static final class ModArithmeticMontgomery extends ModArithmeticDynamic { private final long negInv; private final long r2; private ModArithmeticMontgomery(int mod) { super(mod); long inv = 0; long s = 1, t = 0; for (int i = 0; i < 32; i++) { if ((t & 1) == 0) { t += mod; inv += s; } t >>= 1; s <<= 1; } long r = (1l << 32) % mod; this.negInv = inv; this.r2 = (r * r) % mod; } private int generate(long x) { return reduce(x * r2); } private int reduce(long x) { x = (x + ((x * negInv) & 0xffff_ffffl) * mod) >>> 32; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return generate((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } @Override int inv(int a) { return super.inv(reduce(a)); } @Override int pow(int a, long b) { return generate(super.pow(a, b)); } } private static final class ModArithmeticBarrett extends ModArithmeticDynamic { private static final long mask = 0xffff_ffffl; private final long mh; private final long ml; private ModArithmeticBarrett(int mod) { super(mod); /** * m = floor(2^64/mod) 2^64 = p*mod + q, 2^32 = a*mod + b => (a*mod + b)^2 = * p*mod + q => p = mod*a^2 + 2ab + floor(b^2/mod) */ long a = (1l << 32) / mod; long b = (1l << 32) % mod; long m = a * a * mod + 2 * a * b + (b * b) / mod; mh = m >>> 32; ml = m & mask; } private int reduce(long x) { long z = (x & mask) * ml; z = (x & mask) * mh + (x >>> 32) * ml + (z >>> 32); z = (x >>> 32) * mh + (z >>> 32); x -= z * mod; return (int) (x < mod ? x : x - mod); } @Override int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } @Override int mul(int a, int b) { return reduce((long) a * b); } } private static class ModArithmeticDynamic extends ModArithmetic { final int mod; ModArithmeticDynamic(int mod) { this.mod = mod; } int mod() { return mod; } int remainder(long value) { return (int) ((value %= mod) < 0 ? value + mod : value); } int add(int a, int b) { int sum = a + b; return sum >= mod ? sum - mod : sum; } int sub(int a, int b) { int sum = a - b; return sum < 0 ? sum + mod : sum; } int mul(int a, int b) { return (int) (((long) a * b) % mod); } } } } static class Convolution { /** * Find a primitive root. * * @param m A prime number. * @return Primitive root. */ private static int primitiveRoot(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int[] divs = new int[20]; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long) (i) * i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { boolean ok = true; for (int i = 0; i < cnt; i++) { if (pow(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } /** * Power. * * @param x Parameter x. * @param n Parameter n. * @param m Mod. * @return n-th power of x mod m. */ private static long pow(long x, long n, int m) { if (m == 1) return 0; long r = 1; long y = x % m; while (n > 0) { if ((n & 1) != 0) r = (r * y) % m; y = (y * y) % m; n >>= 1; } return r; } /** * Ceil of power 2. * * @param n Value. * @return Ceil of power 2. */ private static int ceilPow2(int n) { int x = 0; while ((1L << x) < n) x++; return x; } /** * Garner's algorithm. * * @param c Mod convolution results. * @param mods Mods. * @return Result. */ private static long garner(long[] c, int[] mods) { int n = c.length + 1; long[] cnst = new long[n]; long[] coef = new long[n]; java.util.Arrays.fill(coef, 1); for (int i = 0; i < n - 1; i++) { int m1 = mods[i]; long v = (c[i] - cnst[i] + m1) % m1; v = v * pow(coef[i], m1 - 2, m1) % m1; for (int j = i + 1; j < n; j++) { long m2 = mods[j]; cnst[j] = (cnst[j] + coef[j] * v) % m2; coef[j] = (coef[j] * m1) % m2; } } return cnst[n - 1]; } /** * Pre-calculation for NTT. * * @param mod NTT Prime. * @param g Primitive root of mod. * @return Pre-calculation table. */ private static long[] sumE(int mod, int g) { long[] sum_e = new long[30]; long[] es = new long[30]; long[] ies = new long[30]; int cnt2 = Integer.numberOfTrailingZeros(mod - 1); long e = pow(g, (mod - 1) >> cnt2, mod); long ie = pow(e, mod - 2, mod); for (int i = cnt2; i >= 2; i--) { es[i - 2] = e; ies[i - 2] = ie; e = e * e % mod; ie = ie * ie % mod; } long now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_e[i] = es[i] * now % mod; now = now * ies[i] % mod; } return sum_e; } /** * Pre-calculation for inverse NTT. * * @param mod Mod. * @param g Primitive root of mod. * @return Pre-calculation table. */ private static long[] sumIE(int mod, int g) { long[] sum_ie = new long[30]; long[] es = new long[30]; long[] ies = new long[30]; int cnt2 = Integer.numberOfTrailingZeros(mod - 1); long e = pow(g, (mod - 1) >> cnt2, mod); long ie = pow(e, mod - 2, mod); for (int i = cnt2; i >= 2; i--) { es[i - 2] = e; ies[i - 2] = ie; e = e * e % mod; ie = ie * ie % mod; } long now = 1; for (int i = 0; i < cnt2 - 2; i++) { sum_ie[i] = ies[i] * now % mod; now = now * es[i] % mod; } return sum_ie; } /** * Inverse NTT. * * @param a Target array. * @param sumIE Pre-calculation table. * @param mod NTT Prime. */ private static void butterflyInv(long[] a, long[] sumIE, int mod) { int n = a.length; int h = ceilPow2(n); for (int ph = h; ph >= 1; ph--) { int w = 1 << (ph - 1), p = 1 << (h - ph); long inow = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { long l = a[i + offset]; long r = a[i + offset + p]; a[i + offset] = (l + r) % mod; a[i + offset + p] = (mod + l - r) * inow % mod; } int x = Integer.numberOfTrailingZeros(~s); inow = inow * sumIE[x] % mod; } } } /** * Inverse NTT. * * @param a Target array. * @param sumE Pre-calculation table. * @param mod NTT Prime. */ private static void butterfly(long[] a, long[] sumE, int mod) { int n = a.length; int h = ceilPow2(n); for (int ph = 1; ph <= h; ph++) { int w = 1 << (ph - 1), p = 1 << (h - ph); long now = 1; for (int s = 0; s < w; s++) { int offset = s << (h - ph + 1); for (int i = 0; i < p; i++) { long l = a[i + offset]; long r = a[i + offset + p] * now % mod; a[i + offset] = (l + r) % mod; a[i + offset + p] = (l - r + mod) % mod; } int x = Integer.numberOfTrailingZeros(~s); now = now * sumE[x] % mod; } } } /** * Convolution. * * @param a Target array 1. * @param b Target array 2. * @param mod NTT Prime. * @return Answer. */ public static long[] convolution(long[] a, long[] b, int mod) { int n = a.length; int m = b.length; if (n == 0 || m == 0) return new long[0]; int z = 1 << ceilPow2(n + m - 1); { long[] na = new long[z]; long[] nb = new long[z]; System.arraycopy(a, 0, na, 0, n); System.arraycopy(b, 0, nb, 0, m); a = na; b = nb; } int g = primitiveRoot(mod); long[] sume = sumE(mod, g); long[] sumie = sumIE(mod, g); butterfly(a, sume, mod); butterfly(b, sume, mod); for (int i = 0; i < z; i++) { a[i] = a[i] * b[i] % mod; } butterflyInv(a, sumie, mod); a = java.util.Arrays.copyOf(a, n + m - 1); long iz = pow(z, mod - 2, mod); for (int i = 0; i < n + m - 1; i++) a[i] = a[i] * iz % mod; return a; } /** * Convolution. * * @param a Target array 1. * @param b Target array 2. * @param mod Any mod. * @return Answer. */ public static long[] convolutionLL(long[] a, long[] b, int mod) { int n = a.length; int m = b.length; if (n == 0 || m == 0) return new long[0]; int mod1 = 754974721; int mod2 = 167772161; int mod3 = 469762049; long[] c1 = convolution(a, b, mod1); long[] c2 = convolution(a, b, mod2); long[] c3 = convolution(a, b, mod3); int retSize = c1.length; long[] ret = new long[retSize]; int[] mods = { mod1, mod2, mod3, mod }; for (int i = 0; i < retSize; ++i) { ret[i] = garner(new long[] { c1[i], c2[i], c3[i] }, mods); } return ret; } /** * Convolution by ModInt. * * @param a Target array 1. * @param b Target array 2. * @return Answer. */ public static java.util.List<ModIntFactory.ModInt> convolution(java.util.List<ModIntFactory.ModInt> a, java.util.List<ModIntFactory.ModInt> b) { int mod = a.get(0).mod(); long[] va = a.stream().mapToLong(ModIntFactory.ModInt::value).toArray(); long[] vb = b.stream().mapToLong(ModIntFactory.ModInt::value).toArray(); long[] c = convolutionLL(va, vb, mod); ModIntFactory factory = new ModIntFactory(mod); return java.util.Arrays.stream(c).mapToObj(factory::create).collect(java.util.stream.Collectors.toList()); } /** * Naive convolution. (Complexity is O(N^2)!!) * * @param a Target array 1. * @param b Target array 2. * @param mod Mod. * @return Answer. */ public static long[] convolutionNaive(long[] a, long[] b, int mod) { int n = a.length; int m = b.length; int k = n + m - 1; long[] ret = new long[k]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[i + j] += a[i] * b[j] % mod; ret[i + j] %= mod; } } return ret; } } static class SCC { static class Edge { int from, to; public Edge(int from, int to) { this.from = from; this.to = to; } } final int n; int m; final java.util.ArrayList<Edge> unorderedEdges; final int[] start; final int[] ids; boolean hasBuilt = false; public SCC(int n) { this.n = n; this.unorderedEdges = new java.util.ArrayList<>(); this.start = new int[n + 1]; this.ids = new int[n]; } public void addEdge(int from, int to) { rangeCheck(from); rangeCheck(to); unorderedEdges.add(new Edge(from, to)); start[from + 1]++; this.m++; } public int id(int i) { if (!hasBuilt) { throw new UnsupportedOperationException("Graph hasn't been built."); } rangeCheck(i); return ids[i]; } public int[][] build() { for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } Edge[] orderedEdges = new Edge[m]; int[] count = new int[n + 1]; System.arraycopy(start, 0, count, 0, n + 1); for (Edge e : unorderedEdges) { orderedEdges[count[e.from]++] = e; } int nowOrd = 0; int groupNum = 0; int k = 0; // parent int[] par = new int[n]; int[] vis = new int[n]; int[] low = new int[n]; int[] ord = new int[n]; java.util.Arrays.fill(ord, -1); // u = lower32(stack[i]) : visiting vertex // j = upper32(stack[i]) : jth child long[] stack = new long[n]; // size of stack int ptr = 0; // non-recursional DFS for (int i = 0; i < n; i++) { if (ord[i] >= 0) continue; par[i] = -1; // vertex i, 0th child. stack[ptr++] = 0l << 32 | i; // stack is not empty while (ptr > 0) { // last element long p = stack[--ptr]; // vertex int u = (int) (p & 0xffff_ffffl); // jth child int j = (int) (p >>> 32); if (j == 0) { // first visit low[u] = ord[u] = nowOrd++; vis[k++] = u; } if (start[u] + j < count[u]) { // there are more children // jth child int to = orderedEdges[start[u] + j].to; // incr children counter stack[ptr++] += 1l << 32; if (ord[to] == -1) { // new vertex stack[ptr++] = 0l << 32 | to; par[to] = u; } else { // backward edge low[u] = Math.min(low[u], ord[to]); } } else { // no more children (leaving) while (j-- > 0) { int to = orderedEdges[start[u] + j].to; // update lowlink if (par[to] == u) low[u] = Math.min(low[u], low[to]); } if (low[u] == ord[u]) { // root of a component while (true) { // gathering verticies int v = vis[--k]; ord[v] = n; ids[v] = groupNum; if (v == u) break; } groupNum++; // incr the number of components } } } } for (int i = 0; i < n; i++) { ids[i] = groupNum - 1 - ids[i]; } int[] counts = new int[groupNum]; for (int x : ids) counts[x]++; int[][] groups = new int[groupNum][]; for (int i = 0; i < groupNum; i++) { groups[i] = new int[counts[i]]; } for (int i = 0; i < n; i++) { int cmp = ids[i]; groups[cmp][--counts[cmp]] = i; } hasBuilt = true; return groups; } private void rangeCheck(int i) { if (i < 0 || i >= n) { throw new IndexOutOfBoundsException(String.format("Index %d out of bounds for length %d", i, n)); } } } static 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() { 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[][] nextIntArrayMulti(int length, int width) { int[][] arrays = new int[width][length]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) arrays[j][i] = this.nextInt(); } return arrays; } 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; } } static class ContestPrinter extends java.io.PrintWriter { public ContestPrinter(java.io.PrintStream stream) { super(stream); } 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; 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; 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; 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; 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); } } static class Permutation implements java.util.Iterator<int[]>, Iterable<int[]> { private int[] next; public Permutation(int n) { next = java.util.stream.IntStream.range(0, n).toArray(); } @Override public boolean hasNext() { return next != null; } @Override public int[] next() { int[] r = next.clone(); next = nextPermutation(next); return r; } @Override public java.util.Iterator<int[]> iterator() { return this; } public static int[] nextPermutation(int[] a) { if (a == null || a.length < 2) return null; int p = 0; for (int i = a.length - 2; i >= 0; i--) { if (a[i] >= a[i + 1]) continue; p = i; break; } int q = 0; for (int i = a.length - 1; i > p; i--) { if (a[i] <= a[p]) continue; q = i; break; } if (p == 0 && q == 0) return null; int temp = a[p]; a[p] = a[q]; a[q] = temp; int l = p, r = a.length; while (++l < --r) { temp = a[l]; a[l] = a[r]; a[r] = temp; } return a; } } 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)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } static class LazySegTree<S, F> { final int MAX; final int N; final int Log; final java.util.function.BinaryOperator<S> Op; final S E; final java.util.function.BiFunction<F, S, S> Mapping; final java.util.function.BinaryOperator<F> Composition; final F Id; final S[] Dat; final F[] Laz; @SuppressWarnings("unchecked") public LazySegTree(int n, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping, java.util.function.BinaryOperator<F> composition, F id) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.Log = Integer.numberOfTrailingZeros(N); this.Op = op; this.E = e; this.Mapping = mapping; this.Composition = composition; this.Id = id; this.Dat = (S[]) new Object[N << 1]; this.Laz = (F[]) new Object[N]; java.util.Arrays.fill(Dat, E); java.util.Arrays.fill(Laz, Id); } public LazySegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping, java.util.function.BinaryOperator<F> composition, F id) { this(dat.length, op, e, mapping, composition, id); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, Dat, N, l); for (int i = N - 1; i > 0; i--) { Dat[i] = Op.apply(Dat[i << 1 | 0], Dat[i << 1 | 1]); } } private void push(int k) { if (Laz[k] == Id) return; int lk = k << 1 | 0, rk = k << 1 | 1; Dat[lk] = Mapping.apply(Laz[k], Dat[lk]); Dat[rk] = Mapping.apply(Laz[k], Dat[rk]); if (lk < N) Laz[lk] = Composition.apply(Laz[k], Laz[lk]); if (rk < N) Laz[rk] = Composition.apply(Laz[k], Laz[rk]); Laz[k] = Id; } private void pushTo(int k) { for (int i = Log; i > 0; i--) push(k >> i); } private void pushTo(int lk, int rk) { for (int i = Log; i > 0; i--) { if (((lk >> i) << i) != lk) push(lk >> i); if (((rk >> i) << i) != rk) push(rk >> i); } } private void updateFrom(int k) { k >>= 1; while (k > 0) { Dat[k] = Op.apply(Dat[k << 1 | 0], Dat[k << 1 | 1]); k >>= 1; } } private void updateFrom(int lk, int rk) { for (int i = 1; i <= Log; i++) { if (((lk >> i) << i) != lk) { int lki = lk >> i; Dat[lki] = Op.apply(Dat[lki << 1 | 0], Dat[lki << 1 | 1]); } if (((rk >> i) << i) != rk) { int rki = (rk - 1) >> i; Dat[rki] = Op.apply(Dat[rki << 1 | 0], Dat[rki << 1 | 1]); } } } public void set(int p, S x) { exclusiveRangeCheck(p); p += N; pushTo(p); Dat[p] = x; updateFrom(p); } public S get(int p) { exclusiveRangeCheck(p); p += N; pushTo(p); return Dat[p]; } 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); if (l == r) return E; l += N; r += N; pushTo(l, r); S sumLeft = E, sumRight = E; while (l < r) { if ((l & 1) == 1) sumLeft = Op.apply(sumLeft, Dat[l++]); if ((r & 1) == 1) sumRight = Op.apply(Dat[--r], sumRight); l >>= 1; r >>= 1; } return Op.apply(sumLeft, sumRight); } public S allProd() { return Dat[1]; } public void apply(int p, F f) { exclusiveRangeCheck(p); p += N; pushTo(p); Dat[p] = Mapping.apply(f, Dat[p]); updateFrom(p); } public void apply(int l, int r, F f) { if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); if (l == r) return; l += N; r += N; pushTo(l, r); for (int l2 = l, r2 = r; l2 < r2;) { if ((l2 & 1) == 1) { Dat[l2] = Mapping.apply(f, Dat[l2]); if (l2 < N) Laz[l2] = Composition.apply(f, Laz[l2]); l2++; } if ((r2 & 1) == 1) { r2--; Dat[r2] = Mapping.apply(f, Dat[r2]); if (r2 < N) Laz[r2] = Composition.apply(f, Laz[r2]); } l2 >>= 1; r2 >>= 1; } updateFrom(l, r); } public int maxRight(int l, java.util.function.Predicate<S> g) { inclusiveRangeCheck(l); if (!g.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; pushTo(l); S sum = E; do { l >>= Integer.numberOfTrailingZeros(l); if (!g.test(Op.apply(sum, Dat[l]))) { while (l < N) { push(l); l = l << 1; if (g.test(Op.apply(sum, Dat[l]))) { sum = Op.apply(sum, Dat[l]); l++; } } return l - N; } sum = Op.apply(sum, Dat[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> g) { inclusiveRangeCheck(r); if (!g.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; pushTo(r - 1); S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!g.test(Op.apply(Dat[r], sum))) { while (r < N) { push(r); r = r << 1 | 1; if (g.test(Op.apply(Dat[r], sum))) { sum = Op.apply(Dat[r], sum); r--; } } return r + 1 - N; } sum = Op.apply(Dat[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 is not in [%d, %d).", p, 0, MAX)); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException(String.format("Index %d is not in [%d, %d].", p, 0, MAX)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } private S[] simulatePushAll() { S[] simDat = java.util.Arrays.copyOf(Dat, 2 * N); F[] simLaz = java.util.Arrays.copyOf(Laz, 2 * N); for (int k = 1; k < N; k++) { if (simLaz[k] == Id) continue; int lk = k << 1 | 0, rk = k << 1 | 1; simDat[lk] = Mapping.apply(simLaz[k], simDat[lk]); simDat[rk] = Mapping.apply(simLaz[k], simDat[rk]); if (lk < N) simLaz[lk] = Composition.apply(simLaz[k], simLaz[lk]); if (rk < N) simLaz[rk] = Composition.apply(simLaz[k], simLaz[rk]); simLaz[k] = Id; } return simDat; } public String toDetailedString() { return toDetailedString(1, 0, simulatePushAll()); } private String toDetailedString(int k, int sp, S[] dat) { if (k >= N) return indent(sp) + dat[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent, dat); s += "\n"; s += indent(sp) + dat[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent, dat); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { S[] dat = simulatePushAll(); StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(dat[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } static class MaxFlow { private static final class InternalCapEdge { final int to; final int rev; long cap; InternalCapEdge(int to, int rev, long cap) { this.to = to; this.rev = rev; this.cap = cap; } } public static final class CapEdge { public final int from, to; public final long cap, flow; CapEdge(int from, int to, long cap, long flow) { this.from = from; this.to = to; this.cap = cap; this.flow = flow; } @Override public boolean equals(Object o) { if (o instanceof CapEdge) { CapEdge e = (CapEdge) o; return from == e.from && to == e.to && cap == e.cap && flow == e.flow; } return false; } } private static final class IntPair { final int first, second; IntPair(int first, int second) { this.first = first; this.second = second; } } static final long INF = Long.MAX_VALUE; private final int n; private final java.util.ArrayList<IntPair> pos; private final java.util.ArrayList<InternalCapEdge>[] g; @SuppressWarnings("unchecked") public MaxFlow(int n) { this.n = n; this.pos = new java.util.ArrayList<>(); this.g = new java.util.ArrayList[n]; for (int i = 0; i < n; i++) { this.g[i] = new java.util.ArrayList<>(); } } public int addEdge(int from, int to, long cap) { rangeCheck(from, 0, n); rangeCheck(to, 0, n); nonNegativeCheck(cap, "Capacity"); int m = pos.size(); pos.add(new IntPair(from, g[from].size())); int fromId = g[from].size(); int toId = g[to].size(); if (from == to) toId++; g[from].add(new InternalCapEdge(to, toId, cap)); g[to].add(new InternalCapEdge(from, fromId, 0L)); return m; } private InternalCapEdge getInternalEdge(int i) { return g[pos.get(i).first].get(pos.get(i).second); } private InternalCapEdge getInternalEdgeReversed(InternalCapEdge e) { return g[e.to].get(e.rev); } public CapEdge getEdge(int i) { int m = pos.size(); rangeCheck(i, 0, m); InternalCapEdge e = getInternalEdge(i); InternalCapEdge re = getInternalEdgeReversed(e); return new CapEdge(re.to, e.to, e.cap + re.cap, re.cap); } public CapEdge[] getEdges() { CapEdge[] res = new CapEdge[pos.size()]; java.util.Arrays.setAll(res, this::getEdge); return res; } public void changeEdge(int i, long newCap, long newFlow) { int m = pos.size(); rangeCheck(i, 0, m); nonNegativeCheck(newCap, "Capacity"); if (newFlow > newCap) { throw new IllegalArgumentException( String.format("Flow %d is greater than the capacity %d.", newCap, newFlow)); } InternalCapEdge e = getInternalEdge(i); InternalCapEdge re = getInternalEdgeReversed(e); e.cap = newCap - newFlow; re.cap = newFlow; } public long maxFlow(int s, int t) { return flow(s, t, INF); } public long flow(int s, int t, long flowLimit) { rangeCheck(s, 0, n); rangeCheck(t, 0, n); long flow = 0L; int[] level = new int[n]; int[] que = new int[n]; int[] iter = new int[n]; while (flow < flowLimit) { bfs(s, t, level, que); if (level[t] < 0) break; java.util.Arrays.fill(iter, 0); while (flow < flowLimit) { long d = dfs(t, s, flowLimit - flow, iter, level); if (d == 0) break; flow += d; } } return flow; } private void bfs(int s, int t, int[] level, int[] que) { java.util.Arrays.fill(level, -1); int hd = 0, tl = 0; que[tl++] = s; level[s] = 0; while (hd < tl) { int u = que[hd++]; for (InternalCapEdge e : g[u]) { int v = e.to; if (e.cap == 0 || level[v] >= 0) continue; level[v] = level[u] + 1; if (v == t) return; que[tl++] = v; } } } private long dfs(int cur, int s, long flowLimit, int[] iter, int[] level) { if (cur == s) return flowLimit; long res = 0; int curLevel = level[cur]; for (int itMax = g[cur].size(); iter[cur] < itMax; iter[cur]++) { int i = iter[cur]; InternalCapEdge e = g[cur].get(i); InternalCapEdge re = getInternalEdgeReversed(e); if (curLevel <= level[e.to] || re.cap == 0) continue; long d = dfs(e.to, s, Math.min(flowLimit - res, re.cap), iter, level); if (d <= 0) continue; e.cap += d; re.cap -= d; res += d; if (res == flowLimit) break; } return res; } public boolean[] minCut(int s) { rangeCheck(s, 0, n); boolean[] visited = new boolean[n]; int[] stack = new int[n]; int ptr = 0; stack[ptr++] = s; visited[s] = true; while (ptr > 0) { int u = stack[--ptr]; for (InternalCapEdge e : g[u]) { int v = e.to; if (e.cap > 0 && !visited[v]) { visited[v] = true; stack[ptr++] = v; } } } return visited; } private void rangeCheck(int i, int minInclusive, int maxExclusive) { if (i < 0 || i >= maxExclusive) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for length %d", i, maxExclusive)); } } private void nonNegativeCheck(long cap, String attribute) { if (cap < 0) { throw new IllegalArgumentException(String.format("%s %d is negative.", attribute, cap)); } } } static class StringAlgorithm { private static int[] saNaive(int[] s) { int n = s.length; int[] sa = new int[n]; for (int i = 0; i < n; i++) { sa[i] = i; } insertionsortUsingComparator(sa, (l, r) -> { while (l < n && r < n) { if (s[l] != s[r]) return s[l] - s[r]; l++; r++; } return -(l - r); }); return sa; } public static int[] saDoubling(int[] s) { int n = s.length; int[] sa = new int[n]; for (int i = 0; i < n; i++) { sa[i] = i; } int[] rnk = java.util.Arrays.copyOf(s, n); int[] tmp = new int[n]; for (int k = 1; k < n; k *= 2) { final int _k = k; final int[] _rnk = rnk; java.util.function.IntBinaryOperator cmp = (x, y) -> { if (_rnk[x] != _rnk[y]) return _rnk[x] - _rnk[y]; int rx = x + _k < n ? _rnk[x + _k] : -1; int ry = y + _k < n ? _rnk[y + _k] : -1; return rx - ry; }; mergesortUsingComparator(sa, cmp); tmp[sa[0]] = 0; for (int i = 1; i < n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (cmp.applyAsInt(sa[i - 1], sa[i]) < 0 ? 1 : 0); } int[] buf = tmp; tmp = rnk; rnk = buf; } return sa; } private static void insertionsortUsingComparator(int[] a, java.util.function.IntBinaryOperator comparator) { final int n = a.length; for (int i = 1; i < n; i++) { final int tmp = a[i]; if (comparator.applyAsInt(a[i - 1], tmp) > 0) { int j = i; do { a[j] = a[j - 1]; j--; } while (j > 0 && comparator.applyAsInt(a[j - 1], tmp) > 0); a[j] = tmp; } } } private static void mergesortUsingComparator(int[] a, java.util.function.IntBinaryOperator comparator) { final int n = a.length; final int[] work = new int[n]; for (int block = 1; block <= n; block <<= 1) { final int block2 = block << 1; for (int l = 0, max = n - block; l < max; l += block2) { int m = l + block; int r = Math.min(l + block2, n); System.arraycopy(a, l, work, 0, block); for (int i = l, wi = 0, ti = m;; i++) { if (ti == r) { System.arraycopy(work, wi, a, i, block - wi); break; } if (comparator.applyAsInt(work[wi], a[ti]) > 0) { a[i] = a[ti++]; } else { a[i] = work[wi++]; if (wi == block) break; } } } } } private static final int THRESHOLD_NAIVE = 50; // private static final int THRESHOLD_DOUBLING = 0; private static int[] sais(int[] s, int upper) { int n = s.length; if (n == 0) return new int[0]; if (n == 1) return new int[] { 0 }; if (n == 2) { if (s[0] < s[1]) { return new int[] { 0, 1 }; } else { return new int[] { 1, 0 }; } } if (n < THRESHOLD_NAIVE) { return saNaive(s); } // if (n < THRESHOLD_DOUBLING) { // return saDoubling(s); // } int[] sa = new int[n]; boolean[] ls = new boolean[n]; for (int i = n - 2; i >= 0; i--) { ls[i] = s[i] == s[i + 1] ? ls[i + 1] : s[i] < s[i + 1]; } int[] sumL = new int[upper + 1]; int[] sumS = new int[upper + 1]; for (int i = 0; i < n; i++) { if (ls[i]) { sumL[s[i] + 1]++; } else { sumS[s[i]]++; } } for (int i = 0; i <= upper; i++) { sumS[i] += sumL[i]; if (i < upper) sumL[i + 1] += sumS[i]; } java.util.function.Consumer<int[]> induce = lms -> { java.util.Arrays.fill(sa, -1); int[] buf = new int[upper + 1]; System.arraycopy(sumS, 0, buf, 0, upper + 1); for (int d : lms) { if (d == n) continue; sa[buf[s[d]]++] = d; } System.arraycopy(sumL, 0, buf, 0, upper + 1); sa[buf[s[n - 1]]++] = n - 1; for (int i = 0; i < n; i++) { int v = sa[i]; if (v >= 1 && !ls[v - 1]) { sa[buf[s[v - 1]]++] = v - 1; } } System.arraycopy(sumL, 0, buf, 0, upper + 1); for (int i = n - 1; i >= 0; i--) { int v = sa[i]; if (v >= 1 && ls[v - 1]) { sa[--buf[s[v - 1] + 1]] = v - 1; } } }; int[] lmsMap = new int[n + 1]; java.util.Arrays.fill(lmsMap, -1); int m = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lmsMap[i] = m++; } } int[] lms = new int[m]; { int p = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms[p++] = i; } } } induce.accept(lms); if (m > 0) { int[] sortedLms = new int[m]; { int p = 0; for (int v : sa) { if (lmsMap[v] != -1) { sortedLms[p++] = v; } } } int[] recS = new int[m]; int recUpper = 0; recS[lmsMap[sortedLms[0]]] = 0; for (int i = 1; i < m; i++) { int l = sortedLms[i - 1], r = sortedLms[i]; int endL = (lmsMap[l] + 1 < m) ? lms[lmsMap[l] + 1] : n; int endR = (lmsMap[r] + 1 < m) ? lms[lmsMap[r] + 1] : n; boolean same = true; if (endL - l != endR - r) { same = false; } else { while (l < endL && s[l] == s[r]) { l++; r++; } if (l == n || s[l] != s[r]) same = false; } if (!same) { recUpper++; } recS[lmsMap[sortedLms[i]]] = recUpper; } int[] recSA = sais(recS, recUpper); for (int i = 0; i < m; i++) { sortedLms[i] = lms[recSA[i]]; } induce.accept(sortedLms); } return sa; } public static int[] suffixArray(int[] s, int upper) { assert (0 <= upper); for (int d : s) { assert (0 <= d && d <= upper); } return sais(s, upper); } public static int[] suffixArray(int[] s) { int n = s.length; int[] vals = Arrays.copyOf(s, n); java.util.Arrays.sort(vals); int p = 1; for (int i = 1; i < n; i++) { if (vals[i] != vals[i - 1]) { vals[p++] = vals[i]; } } int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = java.util.Arrays.binarySearch(vals, 0, p, s[i]); } return sais(s2, p); } public static int[] suffixArray(char[] s) { int n = s.length; int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = s[i]; } return sais(s2, 255); } public static int[] suffixArray(java.lang.String s) { return suffixArray(s.toCharArray()); } public static int[] lcpArray(int[] s, int[] sa) { int n = s.length; assert (n >= 1); int[] rnk = new int[n]; for (int i = 0; i < n; i++) { rnk[sa[i]] = i; } int[] lcp = new int[n - 1]; int h = 0; for (int i = 0; i < n; i++) { if (h > 0) h--; if (rnk[i] == 0) { continue; } int j = sa[rnk[i] - 1]; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rnk[i] - 1] = h; } return lcp; } public static int[] lcpArray(char[] s, int[] sa) { int n = s.length; int[] s2 = new int[n]; for (int i = 0; i < n; i++) { s2[i] = s[i]; } return lcpArray(s2, sa); } public static int[] lcpArray(java.lang.String s, int[] sa) { return lcpArray(s.toCharArray(), sa); } public static int[] zAlgorithm(int[] s) { int n = s.length; if (n == 0) return new int[0]; int[] z = new int[n]; for (int i = 1, j = 0; i < n; i++) { int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; z[i] = k; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } public static int[] zAlgorithm(char[] s) { int n = s.length; if (n == 0) return new int[0]; int[] z = new int[n]; for (int i = 1, j = 0; i < n; i++) { int k = j + z[j] <= i ? 0 : Math.min(j + z[j] - i, z[i - j]); while (i + k < n && s[k] == s[i + k]) k++; z[i] = k; if (j + z[j] < i + z[i]) j = i; } z[0] = n; return z; } public static int[] zAlgorithm(String s) { return zAlgorithm(s.toCharArray()); } } }
ConDefects/ConDefects/Code/abc232_h/Java/28018161
condefects-java_data_1336
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str = scan.nextLine(); str = str.replace(" ", ""); System.out.println(str); int answer = str.charAt(0) - 48; // 9 for(int i = 0; i <2; i++) { System.out.println(answer); answer = str.charAt(answer) -48; } System.out.println(answer); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str = scan.nextLine(); str = str.replace(" ", ""); int answer = str.charAt(0) - 48; // 9 for(int i = 0; i <2; i++) { answer = str.charAt(answer) -48; } System.out.println(answer); } }
ConDefects/ConDefects/Code/abc241_a/Java/35814918
condefects-java_data_1337
//package com.example.practice.atcoder.sc500; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Stack; import java.util.StringTokenizer; //Multiple of 7 public class Main { final static int M = 1600; final static int[][] PAIR = new int[M*(M+1)/2][]; final static HashMap<Integer,Integer> MAP = new HashMap<>(); static { for (int i=1;i<=M;++i){ int t = i*(i+1)/2; MAP.put(t, i); for (int j=i;j<=M;++j){ int t2 = j*(j+1)/2; if (t+t2<PAIR.length && PAIR[t+t2]==null && (i%5!=2 || j%5!=2)){ if (i%5!=2){ PAIR[t+t2] = new int[]{i, j}; }else { PAIR[t+t2] = new int[]{j, i}; } } } } } public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); System.out.println(calc(Integer.parseInt(input.readLine()))); //out.close(); // close the output file } private static String calc(final int n) { for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t==n){ return getString7(i); }else if (t>n){ break; } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (MAP.containsKey(n-t)){ return getString7(i) + "1" + getString7(MAP.get(n-t)); } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t>n)break; if(PAIR[n-t] != null){ return getString7(i) + "1" + getString7(PAIR[n-t][0]) + "1" + getString7(PAIR[n-t][1]); } } return "-1" + PAIR[6][3]; } private static String getString7(int n){ StringBuilder sb = new StringBuilder(); while (n > 0){ sb.append('7'); n--; } return sb.toString(); } } //package com.example.practice.atcoder.sc500; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Stack; import java.util.StringTokenizer; //Multiple of 7 public class Main { final static int M = 1600; final static int[][] PAIR = new int[M*(M+1)/2][]; final static HashMap<Integer,Integer> MAP = new HashMap<>(); static { for (int i=1;i<=M;++i){ int t = i*(i+1)/2; MAP.put(t, i); for (int j=i;j<=M;++j){ int t2 = j*(j+1)/2; if (t+t2<PAIR.length && PAIR[t+t2]==null && (i%5!=2 || j%5!=2)){ if (i%5!=2){ PAIR[t+t2] = new int[]{i, j}; }else { PAIR[t+t2] = new int[]{j, i}; } } } } } public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); System.out.println(calc(Integer.parseInt(input.readLine()))); //out.close(); // close the output file } private static String calc(final int n) { for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t==n){ return getString7(i); }else if (t>n){ break; } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (MAP.containsKey(n-t)){ return getString7(i) + "1" + getString7(MAP.get(n-t)); } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t>n)break; if(PAIR[n-t] != null){ return getString7(i) + "1" + getString7(PAIR[n-t][0]) + "2" + getString7(PAIR[n-t][1]); } } return "-1" + PAIR[6][3]; } private static String getString7(int n){ StringBuilder sb = new StringBuilder(); while (n > 0){ sb.append('7'); n--; } return sb.toString(); } }
ConDefects/ConDefects/Code/arc129_c/Java/31163133
condefects-java_data_1338
//package com.example.practice.atcoder.sc500; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Stack; import java.util.StringTokenizer; //Multiple of 7 public class Main { final static int M = 1600; final static int[][] PAIR = new int[M*(M+1)/2][]; final static HashMap<Integer,Integer> MAP = new HashMap<>(); static { for (int i=1;i<=M;++i){ int t = i*(i+1)/2; MAP.put(t, i); for (int j=i;j<=M;++j){ int t2 = j*(j+1)/2; if (t+t2<PAIR.length && PAIR[t+t2]==null && ((i+1)%6!=3 || (j+1)%6!=3)){ if ((i+1)%6!=3){ PAIR[t+t2] = new int[]{i, j}; }else { PAIR[t+t2] = new int[]{j, i}; } } } } } public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); calc(Integer.parseInt(input.readLine())); //out.close(); // close the output file } private static String calc(final int n) { for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t==n){ return getString7(i); }else if (t>n){ break; } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (MAP.containsKey(n-t)){ return getString7(i) + "1" + getString7(MAP.get(n-t)); } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t>n)break; if(PAIR[n-t] != null){ return getString7(i) + "1" + getString7(PAIR[n-t][0]) + "1" + getString7(PAIR[n-t][1]); } } return "-1" + PAIR[6][3]; } private static String getString7(int n){ StringBuilder sb = new StringBuilder(); while (n > 0){ sb.append('7'); n--; } return sb.toString(); } } //package com.example.practice.atcoder.sc500; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Stack; import java.util.StringTokenizer; //Multiple of 7 public class Main { final static int M = 1600; final static int[][] PAIR = new int[M*(M+1)/2][]; final static HashMap<Integer,Integer> MAP = new HashMap<>(); static { for (int i=1;i<=M;++i){ int t = i*(i+1)/2; MAP.put(t, i); for (int j=i;j<=M;++j){ int t2 = j*(j+1)/2; if (t+t2<PAIR.length && PAIR[t+t2]==null && ((i+1)%6!=3 || (j+1)%6!=3)){ if ((i+1)%6!=3){ PAIR[t+t2] = new int[]{i, j}; }else { PAIR[t+t2] = new int[]{j, i}; } } } } } public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); System.out.println(calc(Integer.parseInt(input.readLine()))); //out.close(); // close the output file } private static String calc(final int n) { for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t==n){ return getString7(i); }else if (t>n){ break; } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (MAP.containsKey(n-t)){ return getString7(i) + "1" + getString7(MAP.get(n-t)); } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t>n)break; if(PAIR[n-t] != null){ return getString7(i) + "1" + getString7(PAIR[n-t][0]) + "1" + getString7(PAIR[n-t][1]); } } return "-1" + PAIR[6][3]; } private static String getString7(int n){ StringBuilder sb = new StringBuilder(); while (n > 0){ sb.append('7'); n--; } return sb.toString(); } }
ConDefects/ConDefects/Code/arc129_c/Java/31167450
condefects-java_data_1339
//package com.example.practice.atcoder.sc500; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Stack; import java.util.StringTokenizer; //Multiple of 7 public class Main { final static int M = 1600; final static int[][] PAIR = new int[M*(M+1)/2][]; final static HashMap<Integer,Integer> MAP = new HashMap<>(); static { for (int i=1;i<=M;++i){ int t = i*(i+1)/2; MAP.put(t, i); for (int j=i;j<=M;++j){ int t2 = j*(j+1)/2; if (t+t2<PAIR.length && PAIR[t+t2]==null && (i%5!=2 || j%5!=2)){ if (i%5!=2){ PAIR[t+t2] = new int[]{i, j}; }else { PAIR[t+t2] = new int[]{j, i}; } } } } } public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); System.out.println(calc(Integer.parseInt(input.readLine()))); //out.close(); // close the output file } private static String calc(final int n) { for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t==n){ return getString7(i); }else if (t>n){ break; } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (MAP.containsKey(n-t)){ return getString7(i) + "1" + getString7(MAP.get(n-t)); } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t>n)break; if(PAIR[n-t] != null){ return getString7(i) + "1" + getString7(PAIR[n-t][0]) + "1" + getString7(PAIR[n-t][1]); } } return "-1" + PAIR[6][3]; } private static String getString7(int n){ StringBuilder sb = new StringBuilder(); while (n > 0){ sb.append('7'); n--; } return sb.toString(); } } //package com.example.practice.atcoder.sc500; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Stack; import java.util.StringTokenizer; //Multiple of 7 public class Main { final static int M = 1600; final static int[][] PAIR = new int[M*(M+1)/2][]; final static HashMap<Integer,Integer> MAP = new HashMap<>(); static { for (int i=1;i<=M;++i){ int t = i*(i+1)/2; MAP.put(t, i); for (int j=i;j<=M;++j){ int t2 = j*(j+1)/2; if (t+t2<PAIR.length && PAIR[t+t2]==null && (i%5!=2 || j%5!=2)){ if (i%5!=2){ PAIR[t+t2] = new int[]{i, j}; }else { PAIR[t+t2] = new int[]{j, i}; } } } } } public static void main (String [] args) throws IOException { // Use BufferedReader rather than RandomAccessFile; it's much faster final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // input file name goes above //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out"))); System.out.println(calc(Integer.parseInt(input.readLine()))); //out.close(); // close the output file } private static String calc(final int n) { for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t==n){ return getString7(i); }else if (t>n){ break; } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (MAP.containsKey(n-t)){ return getString7(i) + "1" + getString7(MAP.get(n-t)); } } for (int i=1; i<=M; ++i){ int t = i*(i+1)/2; if (t>n)break; if(PAIR[n-t] != null){ return getString7(i) + "1" + getString7(PAIR[n-t][0]) + "2" + getString7(PAIR[n-t][1]); } } return "-1" + PAIR[6][3]; } private static String getString7(int n){ StringBuilder sb = new StringBuilder(); while (n > 0){ sb.append('7'); n--; } return sb.toString(); } }
ConDefects/ConDefects/Code/arc129_c/Java/31162643
condefects-java_data_1340
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) throws Throwable{ try(var s = new Scanner(System.in)){ var n = s.nextInt(); var Td = s.next(); var TdLen = Td.length(); var indices = new ArrayList<String>(); for(var i = 1; i <= n; i++){ var S = s.next(); var SLen = S.length(); var Si = 0; var Tdi = 0; var diff = 0; while(Tdi < TdLen && Si < SLen){ if(Td.charAt(Tdi) != S.charAt(Si)){ diff++; if(diff > 1) break; if(TdLen < SLen) Tdi--; else if(TdLen > SLen) Si--; } Si++; Tdi++; } System.out.printf("Tdi: %d, TdLen: %d, Si: %d, SLen: %d%n", Tdi, TdLen, Si, SLen); if(diff <= 1 && Math.abs(TdLen - SLen) <= 1) indices.add("" + i); } System.out.println(indices.size()); for(var j = 0; j < indices.size(); j++){ if(j > 0) System.out.print(" "); System.out.print(indices.get(j)); } System.out.println(); } } } import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) throws Throwable{ try(var s = new Scanner(System.in)){ var n = s.nextInt(); var Td = s.next(); var TdLen = Td.length(); var indices = new ArrayList<String>(); for(var i = 1; i <= n; i++){ var S = s.next(); var SLen = S.length(); var Si = 0; var Tdi = 0; var diff = 0; while(Tdi < TdLen && Si < SLen){ if(Td.charAt(Tdi) != S.charAt(Si)){ diff++; if(diff > 1) break; if(TdLen < SLen) Tdi--; else if(TdLen > SLen) Si--; } Si++; Tdi++; } if(diff <= 1 && Math.abs(TdLen - SLen) <= 1) indices.add("" + i); } System.out.println(indices.size()); for(var j = 0; j < indices.size(); j++){ if(j > 0) System.out.print(" "); System.out.print(indices.get(j)); } System.out.println(); } } }
ConDefects/ConDefects/Code/abc324_c/Java/47192817
condefects-java_data_1341
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String t = sc.next(); String[] stArr = new String[n]; ArrayList<Integer> ansArr = new ArrayList(); for(int i = 0; i<n; i++){ // stArr[i] = sc.next(); String str = sc.next(); if(check(str, t))ansArr.add(i); } System.out.println(ansArr.size()); for(int nuuu: ansArr)System.out.print(nuuu+1+" "); } public static boolean check(String str, String t){ if(str.equals(t))return true; if(str.length()-t.length()>=2)return false; int mis = 0; if(str.length() > t.length()){ for(int i = 0, j = 0; i<t.length(); i++){ if(t.charAt(i)!=str.charAt(j)){ mis++; i--; } if(mis>1)return false; j++; } return true; }else if(str.length() < t.length()){ for(int i = 0, j = 0; i<str.length(); i++){ if(str.charAt(i)!=t.charAt(j)){ mis++; i--; } if(mis>1)return false; j++; } return true; } for(int i = 0; i<str.length(); i++){ if(str.charAt(i)!=t.charAt(i))mis++; if(mis>1)break; } return mis<2; } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String t = sc.next(); String[] stArr = new String[n]; ArrayList<Integer> ansArr = new ArrayList(); for(int i = 0; i<n; i++){ // stArr[i] = sc.next(); String str = sc.next(); if(check(str, t))ansArr.add(i); } System.out.println(ansArr.size()); for(int nuuu: ansArr)System.out.print(nuuu+1+" "); } public static boolean check(String str, String t){ if(str.equals(t))return true; if(Math.abs(str.length()-t.length())>=2)return false; int mis = 0; if(str.length() > t.length()){ for(int i = 0, j = 0; i<t.length(); i++){ if(t.charAt(i)!=str.charAt(j)){ mis++; i--; } if(mis>1)return false; j++; } return true; }else if(str.length() < t.length()){ for(int i = 0, j = 0; i<str.length(); i++){ if(str.charAt(i)!=t.charAt(j)){ mis++; i--; } if(mis>1)return false; j++; } return true; } for(int i = 0; i<str.length(); i++){ if(str.charAt(i)!=t.charAt(i))mis++; if(mis>1)break; } return mis<2; } }
ConDefects/ConDefects/Code/abc324_c/Java/47389707
condefects-java_data_1342
import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.ArrayList; public final class Main { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); final int N = scanner.nextInt(); final String TDash = scanner.next(); final String[] S = new String[N]; for (int i = 0; i < N; ++i) { S[i] = scanner.next(); } scanner.close(); final int TDashLength = TDash.length(); List<Integer> answer = new ArrayList<>(); for (int i = 0; i < N; ++i) { final int SiLength = S[i].length(); boolean isLikely = true; if (Math.abs(TDashLength - SiLength) > 1) { continue; } if (TDashLength != SiLength) { final String stringLong, stringShort; if (TDashLength > SiLength) { stringLong = TDash; stringShort = S[i]; } else { stringShort = TDash; stringLong = S[i]; } int kShort = 0, kLong = 0; for (int j = 0; j < stringShort.length(); ++j) { if (stringShort.charAt(kShort) == stringLong.charAt(kLong)) { ++kShort; ++kLong; continue; } else { if (kShort == kLong) { ++kLong; continue; } else { isLikely = false; break; } } } } else { int numberOfDifferentChars = 0; for (int j = 0; j < TDashLength; ++j) { if (TDash.charAt(j) == S[i].charAt(j)) { continue; } else { if (numberOfDifferentChars == 0) { ++numberOfDifferentChars; continue; } else { isLikely = false; break; } } } } if (isLikely) { answer.add(i+1); } } System.out.println(answer.size()); for (final int element : answer) { System.out.print(element); System.out.print(' '); } System.out.println(); } } import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.ArrayList; public final class Main { public static void main(final String[] args) { Scanner scanner = new Scanner(System.in); final int N = scanner.nextInt(); final String TDash = scanner.next(); final String[] S = new String[N]; for (int i = 0; i < N; ++i) { S[i] = scanner.next(); } scanner.close(); final int TDashLength = TDash.length(); List<Integer> answer = new ArrayList<>(); for (int i = 0; i < N; ++i) { final int SiLength = S[i].length(); boolean isLikely = true; if (Math.abs(TDashLength - SiLength) > 1) { continue; } if (TDashLength != SiLength) { final String stringLong, stringShort; if (TDashLength > SiLength) { stringLong = TDash; stringShort = S[i]; } else { stringShort = TDash; stringLong = S[i]; } int kShort = 0, kLong = 0; while (kShort < stringShort.length()) { if (stringShort.charAt(kShort) == stringLong.charAt(kLong)) { ++kShort; ++kLong; continue; } else { if (kShort == kLong) { ++kLong; continue; } else { isLikely = false; break; } } } } else { int numberOfDifferentChars = 0; for (int j = 0; j < TDashLength; ++j) { if (TDash.charAt(j) == S[i].charAt(j)) { continue; } else { if (numberOfDifferentChars == 0) { ++numberOfDifferentChars; continue; } else { isLikely = false; break; } } } } if (isLikely) { answer.add(i+1); } } System.out.println(answer.size()); for (final int element : answer) { System.out.print(element); System.out.print(' '); } System.out.println(); } }
ConDefects/ConDefects/Code/abc324_c/Java/47542216
condefects-java_data_1343
import java.util.ArrayList; import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=Integer.parseInt(sc.next()); String t=sc.next(); ArrayList<Integer>ans=new ArrayList<>(); for(int i=1;i<=n;i++){ String s=sc.next(); if(Math.abs(s.length()-t.length())>1){ continue; } if(s.length()==t.length()){ if(isMatch(s,t)){ ans.add(i); } continue; } String longerStr; String shorterStr; if(s.length()>t.length()){ longerStr=s; shorterStr=t; }else{ longerStr=t; shorterStr=s; } if(isMatchLongShort(longerStr,shorterStr)){ ans.add(i); } } System.out.println(ans.size()); for(int i=0;i<ans.size();i++){ if(i>0){ System.out.print(" "); } System.out.print(ans.get(i)); } System.out.println(); } public static boolean isMatch(String s,String t){ int diffCount=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)!=t.charAt(i)){ diffCount++; } } if(diffCount<=1){ return true; }else{ return false; } } public static boolean isMatchLongShort(String longerStr,String shorterStr){ int diffCount=0; int longerStrOffset=0; for(int i=0;i<shorterStr.length();i++){ if(longerStr.charAt(i+longerStrOffset)!=shorterStr.charAt(i)){ diffCount++; if(longerStrOffset<1){ longerStrOffset++; i--; } } } if(diffCount==1){ return true; }else{ return false; } } } import java.util.ArrayList; import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=Integer.parseInt(sc.next()); String t=sc.next(); ArrayList<Integer>ans=new ArrayList<>(); for(int i=1;i<=n;i++){ String s=sc.next(); if(Math.abs(s.length()-t.length())>1){ continue; } if(s.length()==t.length()){ if(isMatch(s,t)){ ans.add(i); } continue; } String longerStr; String shorterStr; if(s.length()>t.length()){ longerStr=s; shorterStr=t; }else{ longerStr=t; shorterStr=s; } if(isMatchLongShort(longerStr,shorterStr)){ ans.add(i); } } System.out.println(ans.size()); for(int i=0;i<ans.size();i++){ if(i>0){ System.out.print(" "); } System.out.print(ans.get(i)); } System.out.println(); } public static boolean isMatch(String s,String t){ int diffCount=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)!=t.charAt(i)){ diffCount++; } } if(diffCount<=1){ return true; }else{ return false; } } public static boolean isMatchLongShort(String longerStr,String shorterStr){ int diffCount=0; int longerStrOffset=0; for(int i=0;i<shorterStr.length();i++){ if(longerStr.charAt(i+longerStrOffset)!=shorterStr.charAt(i)){ diffCount++; if(longerStrOffset<1){ longerStrOffset++; i--; } } } if(diffCount<=1){ return true; }else{ return false; } } }
ConDefects/ConDefects/Code/abc324_c/Java/50497269
condefects-java_data_1344
import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i=0; i<t; i++) { long n = in.nextLong(); System.out.println(solve(n) ? "Yes" : "No"); } } private static boolean solve(long n) { // 素因数分解する // 試し割りでも sqrt(10^9) は 10^5 には収まるので間に合いそうな予感(怪しいけどTLEしたらエラトステネスさんの技法を採用しよう) long m = n; long sumOfFactors = 0; long i = 2; while(m>1) { if(m%i > 0) { i++; if(m<i*i) { // 素数 sumOfFactors += m; break; } continue; } m = m/i; int count = 1; while(m%i == 0) { m = m/i; count++; } sumOfFactors = i * count; } return sumOfFactors < n; } } import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int i=0; i<t; i++) { long n = in.nextLong(); System.out.println(solve(n) ? "Yes" : "No"); } } private static boolean solve(long n) { // 素因数分解する // 試し割りでも sqrt(10^9) は 10^5 には収まるので間に合いそうな予感(怪しいけどTLEしたらエラトステネスさんの技法を採用しよう) long m = n; long sumOfFactors = 0; long i = 2; while(m>1) { if(m%i > 0) { i++; if(m<i*i) { // 素数 sumOfFactors += m; break; } continue; } m = m/i; int count = 1; while(m%i == 0) { m = m/i; count++; } sumOfFactors = (long)Math.pow(i, count); } return sumOfFactors < n; } }
ConDefects/ConDefects/Code/arc165_a/Java/45677096
condefects-java_data_1345
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.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class Main { public static long MOD = 998244353; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); for (int z = 0; z < T; z++) { long n = in.nextLong(); out.println(cal(n) ? "YES" : "NO"); } out.close(); } static boolean cal(long n) { HashSet<Long> set = new HashSet<>(); for (long i = 2; i * i <= n && set.size() < 2; i++) { if (n % i != 0) { continue; } set.add(i); while (n % i == 0) { n /= i; } } if(n != 1){ set.add(n); } return set.size() > 1; } static int abs(int a) { return a < 0 ? -a : a; } 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 { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } 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, long 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 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.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class Main { public static long MOD = 998244353; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int T = in.nextInt(); for (int z = 0; z < T; z++) { long n = in.nextLong(); out.println(cal(n) ? "Yes" : "No"); } out.close(); } static boolean cal(long n) { HashSet<Long> set = new HashSet<>(); for (long i = 2; i * i <= n && set.size() < 2; i++) { if (n % i != 0) { continue; } set.add(i); while (n % i == 0) { n /= i; } } if(n != 1){ set.add(n); } return set.size() > 1; } static int abs(int a) { return a < 0 ? -a : a; } 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 { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } 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, long 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/arc165_a/Java/45703558
condefects-java_data_1346
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String []args) { Scanner sc = new Scanner(System.in); int a[] = new int[5]; for (int i = 0;i < a.length - 1;i++) { a[i] = sc.nextInt(); } Arrays.sort(a); int a1 = a[0]; int count1 = 1; int a2 = 0; int count2 = 0; for (int i = 1;i < a.length;i++) { if (a[i] == a1) { count1++; } else if (a[i] != a1&& a2 == 0) { a2 = a[i]; count2++; } else if (a[i] == a2) { count2++; } } if (count1 == 3&& count2 == 2||count1 == 2&&count2==3) { System.out.println("Yes"); } else { System.out.println("No"); } } } import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String []args) { Scanner sc = new Scanner(System.in); int a[] = new int[5]; for (int i = 0;i < 5;i++) { a[i] = sc.nextInt(); } Arrays.sort(a); int a1 = a[0]; int count1 = 1; int a2 = 0; int count2 = 0; for (int i = 1;i < a.length;i++) { if (a[i] == a1) { count1++; } else if (a[i] != a1&& a2 == 0) { a2 = a[i]; count2++; } else if (a[i] == a2) { count2++; } } if (count1 == 3&& count2 == 2||count1 == 2&&count2==3) { System.out.println("Yes"); } else { System.out.println("No"); } } }
ConDefects/ConDefects/Code/abc263_a/Java/40986022
condefects-java_data_1347
import java.util.Scanner; import java.util.Arrays; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int[] nums = new int[5]; Arrays.setAll(nums,i -> sc.nextInt()); Arrays.sort(nums); System.out.println(nums[0]==nums[2]&&nums[3]==nums[4]&&nums[0]!=nums[4]?"Yes":"No"); } } import java.util.Scanner; import java.util.Arrays; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int[] nums = new int[5]; Arrays.setAll(nums,i -> sc.nextInt()); Arrays.sort(nums); System.out.println((nums[0]==nums[2]&&nums[3]==nums[4]||nums[0]==nums[1]&&nums[2]==nums[4])&&nums[0]!=nums[4]?"Yes":"No"); } }
ConDefects/ConDefects/Code/abc263_a/Java/37235062
condefects-java_data_1348
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) { ContestScanner sc = new ContestScanner(); ContestPrinter ou = new ContestPrinter(); //ここから int A[] = new int[5]; for(int i=0;i<5;i++){ A[i] = sc.nextInt(); } Arrays.sort(A); if(A[0]==A[1]&&A[1]==A[2]&&A[3]==A[4])ou.println("Yes"); else ou.println("No"); //ここ 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.*; public class Main{ public static void main(String[] args) { ContestScanner sc = new ContestScanner(); ContestPrinter ou = new ContestPrinter(); //ここから int A[] = new int[5]; for(int i=0;i<5;i++){ A[i] = sc.nextInt(); } Arrays.sort(A); if(A[0]==A[1]&&A[1]==A[2]&&A[3]==A[4]||A[4]==A[3]&&A[3]==A[2]&&A[1]==A[0])ou.println("Yes"); else ou.println("No"); //ここ 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/abc263_a/Java/37549858
condefects-java_data_1349
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.StringTokenizer; /* * Solution: 1m * Coding: 10m * Time: 11m * * 这里可以不用考虑扩展性,数据很小,怎么快怎么写。 * */ public class Main { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("atcoder_abc/input.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); List<Integer> lst = new ArrayList<>(); for(int i = 0;i < 5;i++){ lst.add(Integer.parseInt(st.nextToken())); } if((lst.get(0) == lst.get(2) && lst.get(3) == lst.get(4)) || (lst.get(0) == lst.get(1) && lst.get(2) == lst.get(4))){ System.out.println("Yes"); } else { System.out.println("No"); } // HashMap<Integer, Integer> map = new HashMap<>(); // for(int i = 0;i < 5;i++){ // int cur = Integer.parseInt(st.nextToken()); // if(!map.containsKey(cur)){ // map.put(cur, 0); // } // map.put(cur, map.get(cur) + 1); // } // if(map.size() != 2) { // System.out.println("No"); // } else { // for(int i: map.values()){ // if(i > 3){ // System.out.println("No"); // return; // } // } // System.out.println("Yes"); // } br.close(); } } import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.StringTokenizer; /* * Solution: 1m * Coding: 10m * Time: 11m * * 这里可以不用考虑扩展性,数据很小,怎么快怎么写。 * */ public class Main { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("atcoder_abc/input.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); List<Integer> lst = new ArrayList<>(); for(int i = 0;i < 5;i++){ lst.add(Integer.parseInt(st.nextToken())); } Collections.sort(lst); if((lst.get(0) == lst.get(2) && lst.get(3) == lst.get(4)) || (lst.get(0) == lst.get(1) && lst.get(2) == lst.get(4))){ System.out.println("Yes"); } else { System.out.println("No"); } // HashMap<Integer, Integer> map = new HashMap<>(); // for(int i = 0;i < 5;i++){ // int cur = Integer.parseInt(st.nextToken()); // if(!map.containsKey(cur)){ // map.put(cur, 0); // } // map.put(cur, map.get(cur) + 1); // } // if(map.size() != 2) { // System.out.println("No"); // } else { // for(int i: map.values()){ // if(i > 3){ // System.out.println("No"); // return; // } // } // System.out.println("Yes"); // } br.close(); } }
ConDefects/ConDefects/Code/abc263_a/Java/40377905
condefects-java_data_1350
import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] arr = new int[5]; for (int i = 0; i < 5; i++) { arr[i] = scn.nextInt(); } Arrays.sort(arr); if ((arr[0] == arr[1]) && arr[0] == arr[2] && arr[3] == arr[4]) { System.out.println("YES"); } else if ( arr[0] == arr[1] && (arr[2] == arr[4]) && arr[3] == arr[2] ) { System.out.println("Yes"); } else { System.out.println("No"); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] arr = new int[5]; for (int i = 0; i < 5; i++) { arr[i] = scn.nextInt(); } Arrays.sort(arr); if ((arr[0] == arr[1]) && arr[0] == arr[2] && arr[3] == arr[4]) { System.out.println("Yes"); } else if ( arr[0] == arr[1] && (arr[2] == arr[4]) && arr[3] == arr[2] ) { System.out.println("Yes"); } else { System.out.println("No"); } } }
ConDefects/ConDefects/Code/abc263_a/Java/39499285
condefects-java_data_1351
import java.io.*; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(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; } } private static void solve(FastReader reader){ int n = reader.nextInt(); int odd = 0; int even = 0; for(int i=0; i<n; i++){ int x = reader.nextInt(); if(x%2==0) { even++; } else{ odd++; } } if(n%2!=0){ System.out.println(-1); } else if(even==odd){ System.out.println(0); } else { int ans = Math.abs(odd - even)/2; System.out.println(ans); } } public static void main(String[] args) throws FileNotFoundException { FastReader reader = new FastReader(System.in); int[] arr = new int[5]; for(int i=0; i<5; i++){ arr[i] = reader.nextInt(); } Arrays.sort(arr); if(arr[0]==arr[1] && arr[1]==arr[2] && arr[2]!=arr[3] && arr[3]==arr[4]){ System.out.println("Yes"); } else { System.out.println("No"); } } } import java.io.*; import java.util.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(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; } } private static void solve(FastReader reader){ int n = reader.nextInt(); int odd = 0; int even = 0; for(int i=0; i<n; i++){ int x = reader.nextInt(); if(x%2==0) { even++; } else{ odd++; } } if(n%2!=0){ System.out.println(-1); } else if(even==odd){ System.out.println(0); } else { int ans = Math.abs(odd - even)/2; System.out.println(ans); } } public static void main(String[] args) throws FileNotFoundException { FastReader reader = new FastReader(System.in); int[] arr = new int[5]; for(int i=0; i<5; i++){ arr[i] = reader.nextInt(); } Arrays.sort(arr); if(arr[0]==arr[1] && arr[1]==arr[2] && arr[2]!=arr[3] && arr[3]==arr[4]){ System.out.println("Yes"); } else if(arr[0]==arr[1] && arr[1]!=arr[2] && arr[2]==arr[3] && arr[3]==arr[4]){ System.out.println("Yes"); } else { System.out.println("No"); } } }
ConDefects/ConDefects/Code/abc263_a/Java/45068489
condefects-java_data_1352
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); final int H = 2; final int W = 9; Integer[] sum = new Integer[2]; sum[0]= 0; sum[1] = 0; for(int i = 0 ; i < H ; i++){ if(i == 0){ for(int j = 0; j < W; j++){ sum[0] += sc.nextInt(); } }else{ for(int j = 0; j < W-1 ; j++){ sum[1] += sc.nextInt(); } } } if(sum[0] - sum[1]> 0){ System.out.println(sum[0]-sum[1]); }else if(sum[1]-sum[0] == 0){ System.out.println(1); } } } import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); final int H = 2; final int W = 9; Integer[] sum = new Integer[2]; sum[0]= 0; sum[1] = 0; for(int i = 0 ; i < H ; i++){ if(i == 0){ for(int j = 0; j < W; j++){ sum[0] += sc.nextInt(); } }else{ for(int j = 0; j < W-1 ; j++){ sum[1] += sc.nextInt(); } } } if(sum[0] - sum[1]> 0){ System.out.println(sum[0]-sum[1]+1); }else if(sum[1]-sum[0] == 0){ System.out.println(1); } } }
ConDefects/ConDefects/Code/abc351_a/Java/54541190
condefects-java_data_1353
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int sumA = 0; int sumB = 0; for(int i = 0; i < 9; i++){ int n = sc.nextInt(); sumA += n; } //System.out.println(sumA); sc.nextLine(); for(int j = 0; j < 8; j++){ int m = sc.nextInt(); sumB += m; } //System.out.println(sumB); System.out.println(sumA - sumB); } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int sumA = 0; int sumB = 0; for(int i = 0; i < 9; i++){ int n = sc.nextInt(); sumA += n; } //System.out.println(sumA); sc.nextLine(); for(int j = 0; j < 8; j++){ int m = sc.nextInt(); sumB += m; } //System.out.println(sumB); System.out.println(sumA - sumB + 1); } }
ConDefects/ConDefects/Code/abc351_a/Java/54240858
condefects-java_data_1354
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int sumA = 0; int sumB = 0; for (int i = 0; i < 9; i++) { sumA += Integer.parseInt(sc.next()); } for (int i = 0; i < 8; i++) { sumB += Integer.parseInt(sc.next()); } System.out.print((sumA)); System.out.print((sumB)); System.out.print((sumA - sumB + 1)); } } import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int sumA = 0; int sumB = 0; for (int i = 0; i < 9; i++) { sumA += Integer.parseInt(sc.next()); } for (int i = 0; i < 8; i++) { sumB += Integer.parseInt(sc.next()); } System.out.print((sumA - sumB + 1)); } }
ConDefects/ConDefects/Code/abc351_a/Java/54020176
condefects-java_data_1355
import java.util.*; class Main { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); String s=scanner.next(); int cnt=0; for(int i=0;i<n;i++){ if(s.charAt(i)==('"'))cnt++; if(s.charAt(i)!=','){ System.out.print(s.charAt(i)); } else{ if(cnt==1){ System.out.print(","); } else{ System.out.print("."); } } } } } import java.util.*; class Main { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); String s=scanner.next(); int cnt=0; for(int i=0;i<n;i++){ if(s.charAt(i)==('"'))cnt++; if(s.charAt(i)!=','){ System.out.print(s.charAt(i)); } else{ if((cnt & 1)==1){ System.out.print(","); } else{ System.out.print("."); } } } } }
ConDefects/ConDefects/Code/abc282_c/Java/41842824
condefects-java_data_1356
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { int t = 1; while (t > 0) { solve(); t--; } w.flush(); w.close(); } static int mod = (int) 1e9 + 7; public static void solve() throws IOException { int n = f.nextInt(); int m = f.nextInt(); int[] cnt = new int[n]; for (int i = 0; i < m; i++) { int x = f.nextInt() - 1; int y = f.nextInt() - 1; cnt[(x + y) % n] ++; } Vector<Integer> c = new Vector<>(); for (int i = 0; i < n; i++) { if (cnt[i] > 0) c.add(i); } for (int i = 0; i < n; i++) { if (cnt[i] == 0 && c.size() < m) c.add(i); } w.println(n * m); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int x = j; int y = (cnt[i] - j + n) % n; w.println((x + 1) + " " + (y+1)); } } } public static class Node { int x, y; public Node(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return x == node.x && y == node.y; } @Override public int hashCode() { return Objects.hash(x, y); } } static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out)); static Input f = new Input(System.in); static class Input { public BufferedReader reader; public StringTokenizer tokenizer; public Input(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public String nextLine() throws IOException { String str = null; str = reader.readLine(); return str; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public Double nextDouble() throws IOException { return Double.parseDouble(next()); } } } import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { int t = 1; while (t > 0) { solve(); t--; } w.flush(); w.close(); } static int mod = (int) 1e9 + 7; public static void solve() throws IOException { int n = f.nextInt(); int m = f.nextInt(); int[] cnt = new int[n]; for (int i = 0; i < m; i++) { int x = f.nextInt() - 1; int y = f.nextInt() - 1; cnt[(x + y) % n] ++; } Vector<Integer> c = new Vector<>(); for (int i = 0; i < n; i++) { if (cnt[i] > 0) c.add(i); } for (int i = 0; i < n; i++) { if (cnt[i] == 0 && c.size() < m) c.add(i); } w.println(n * m); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int x = j; int y = (c.get(i) - j + n) % n; w.println((x + 1) + " " + (y+1)); } } } public static class Node { int x, y; public Node(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; return x == node.x && y == node.y; } @Override public int hashCode() { return Objects.hash(x, y); } } static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out)); static Input f = new Input(System.in); static class Input { public BufferedReader reader; public StringTokenizer tokenizer; public Input(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } public String nextLine() throws IOException { String str = null; str = reader.readLine(); return str; } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public Double nextDouble() throws IOException { return Double.parseDouble(next()); } } }
ConDefects/ConDefects/Code/arc176_a/Java/52728415
condefects-java_data_1357
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); //final long mod = 1_000_000_007L; //final long mod = 998_244_353L; int n = Integer.parseInt(sc.next()); int m = Integer.parseInt(sc.next()); //long l = Long.parseLong(sc.next()); //String[] s = sc.next().split(""); HashSet<Long> s = new HashSet<>(n*m); for ( int i=0; i<n; i++ ) { for ( int j=0; j<m; j++ ) { long v = (long)i*(long)n + (long)((i+(n/m)*j)%n); s.add(v); } } HashSet<Long> used = new HashSet<>(m*5); for ( int i=0; i<m; i++ ) { int a = Integer.parseInt(sc.next())-1; int b = Integer.parseInt(sc.next())-1; long v = (long)a*(long)n +(long)b; if ( s.contains(v) ) { used.add(v); continue; } boolean f = false; for ( int j=0; j<n; j++ ) { long vc = (long)a*(long)n +(long)j; if ( !s.contains(vc) ) continue; if ( used.contains(vc) ) continue; for ( int k=0; k<n; k++ ) { long vr = (long)k*(long)n +(long)b; long vrc = (long)k*(long)n +(long)j; if ( !s.contains(vr) ) continue; if ( used.contains(vr) ) continue; if ( s.contains(vrc) ) continue; used.add(v); s.add(v); s.add(vrc); s.remove(vr); s.remove(vc); f = true; break; } if ( f ) break; } } System.out.println(n*m); StringBuilder ans = new StringBuilder(); for ( long e : s ) { long r = e/(long)n +1L; long c = e%(long)n +1L; ans.append(r+" "+c+System.lineSeparator()); } System.out.print(ans.toString()); //System.out.println(String.format("%16.12f", ans)); } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); //final long mod = 1_000_000_007L; //final long mod = 998_244_353L; int n = Integer.parseInt(sc.next()); int m = Integer.parseInt(sc.next()); //long l = Long.parseLong(sc.next()); //String[] s = sc.next().split(""); HashSet<Long> s = new HashSet<>(n*m); for ( int i=0; i<n; i++ ) { for ( int j=0; j<m; j++ ) { long v = (long)i*(long)n + (long)((i+j)%n); s.add(v); } } HashSet<Long> used = new HashSet<>(m*5); for ( int i=0; i<m; i++ ) { int a = Integer.parseInt(sc.next())-1; int b = Integer.parseInt(sc.next())-1; long v = (long)a*(long)n +(long)b; if ( s.contains(v) ) { used.add(v); continue; } boolean f = false; for ( int j=0; j<n; j++ ) { long vc = (long)a*(long)n +(long)j; if ( !s.contains(vc) ) continue; if ( used.contains(vc) ) continue; for ( int k=0; k<n; k++ ) { long vr = (long)k*(long)n +(long)b; long vrc = (long)k*(long)n +(long)j; if ( !s.contains(vr) ) continue; if ( used.contains(vr) ) continue; if ( s.contains(vrc) ) continue; used.add(v); s.add(v); s.add(vrc); s.remove(vr); s.remove(vc); f = true; break; } if ( f ) break; } } System.out.println(n*m); StringBuilder ans = new StringBuilder(); for ( long e : s ) { long r = e/(long)n +1L; long c = e%(long)n +1L; ans.append(r+" "+c+System.lineSeparator()); } System.out.print(ans.toString()); //System.out.println(String.format("%16.12f", ans)); } }
ConDefects/ConDefects/Code/arc176_a/Java/52664166
condefects-java_data_1358
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // 看过答案 // https://atcoder.jp/contests/arc176/submissions/52668226 Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[] a = new int[m]; int[] b = new int[m]; for (int i = 0; i < m; i++) { a[i] = scanner.nextInt() - 1; b[i] = scanner.nextInt() - 1; } Set<Integer> set = new HashSet<>(); for (int i = 0; i < m; i++) { set.add((b[i] - a[i] + n) % n); } for (int i = n; i >= 0; i--) { if (set.size() == m) { break; } if (!set.contains(i)) { set.add(i); } } System.out.println(n * m); for (int p : set) { for (int i = 0; i < n; i++) { System.out.println((i + 1) + " " + ((i + p) % n + 1)); } } } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { // 看过答案 // https://atcoder.jp/contests/arc176/submissions/52668226 Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[] a = new int[m]; int[] b = new int[m]; for (int i = 0; i < m; i++) { a[i] = scanner.nextInt() - 1; b[i] = scanner.nextInt() - 1; } Set<Integer> set = new HashSet<>(); for (int i = 0; i < m; i++) { set.add((b[i] - a[i] + n) % n); } for (int i = n - 1; i >= 0; i--) { if (set.size() == m) { break; } if (!set.contains(i)) { set.add(i); } } System.out.println(n * m); for (int p : set) { for (int i = 0; i < n; i++) { System.out.println((i + 1) + " " + ((i + p) % n + 1)); } } } }
ConDefects/ConDefects/Code/arc176_a/Java/52813279
condefects-java_data_1359
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int m = Integer.parseInt(sa[1]); int[] a = new int[m]; int[] b = new int[m]; for (int i = 0; i < m; i++) { sa = br.readLine().split(" "); a[i] = Integer.parseInt(sa[0]) - 1; b[i] = Integer.parseInt(sa[1]) - 1; } br.close(); Set<Integer> set = new HashSet<>(); for (int i = 0; i < m; i++) { set.add((a[i] + b[i]) % n); } for (int i = 0; i < n; i++) { if (set.size() == m) { break; } if (!set.contains(i)) { set.add(i); } } PrintWriter pw = new PrintWriter(System.out); pw.println(n * m); for (int p : set) { for (int i = 0; i < n; i++) { pw.println((i + 1) + " " + ((i + p) % n + 1)); } } pw.flush(); } } import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int m = Integer.parseInt(sa[1]); int[] a = new int[m]; int[] b = new int[m]; for (int i = 0; i < m; i++) { sa = br.readLine().split(" "); a[i] = Integer.parseInt(sa[0]) - 1; b[i] = Integer.parseInt(sa[1]) - 1; } br.close(); Set<Integer> set = new HashSet<>(); for (int i = 0; i < m; i++) { set.add((b[i] - a[i] + n) % n); } for (int i = 0; i < n; i++) { if (set.size() == m) { break; } if (!set.contains(i)) { set.add(i); } } PrintWriter pw = new PrintWriter(System.out); pw.println(n * m); for (int p : set) { for (int i = 0; i < n; i++) { pw.println((i + 1) + " " + ((i + p) % n + 1)); } } pw.flush(); } }
ConDefects/ConDefects/Code/arc176_a/Java/52667994
condefects-java_data_1360
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 { public static void solve () { //98765432 long a = 500000000; long b = 98765431; long c = 98765431; long A = nextLong(), B = nextLong(); println(a*A*B); // println((a*b+c)); // println(2*(a*b+c)); // println((long)Math.pow(10, 18)); } ///////////////////////////////////////////////////////////////////////////////////////////////// // 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 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;} } public static String yesno(boolean b) {return b ? "Yes" : "No";} public static class Grid { int h, w; Point[][] p; boolean[][] seen; Grid(int h, int w) { p = new Point[h][w]; this.h = h; this.w = w; for (int i=0; i<h; i++) { String s = next(); for (int j=0; j<w; j++) { p[i][j] = new Point(i, j, s.charAt(j)); } } } public Point getPoint(int y, int x) { return this.p[y][x]; } public List<Point> getAround4(Point p) { int y = p.y, x = p.x; List<Point> a = new ArrayList<>(); if (y >= 1) a.add(new Point(y-1, x)); if (y <= h-2) a.add(new Point(y+1, x)); if (x >= 1) a.add(new Point(y, x-1)); if (x <= w-2) a.add(new Point(y, x+1)); return a; } public List<Point> getAround8(int y, int x) { List<Point> a = new ArrayList<>(); if (y >= 1) { a.add(new Point(y-1, x)); if (x >= 1) a.add(new Point(y-1, x-1)); if (x <= w-2) a.add(new Point(y-1, x+1)); } if (y <= h-2) { a.add(new Point(y+1, x)); if (x >= 1) a.add(new Point(y+1, x-1)); if (x <= w-2) a.add(new Point(y+1, x+1)); } if (x >= 1) a.add(new Point(y, x-1)); if (x <= w-2) a.add(new Point(y, x+1)); return a; } public int countConnectedComponents() { int count = 0; seen = new boolean[h][w]; while(true) { Point start = null; //全ての連結成分を探索したかチェック boolean flag = true; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) { if (seen[i][j] == false && p[i][j].c == '#') { flag = false; start = new Point(i, j); } } } if (flag == true) break; Deque<Point> q = new ArrayDeque<>(); q.add(start); seen[start.y][start.x] = true; //BFS while(q.size() > 0) { Point now = q.removeFirst(); for (Point next : getAround4(now)) { if (seen[next.y][next.x] == false && p[next.y][next.x].c == '#') { seen[next.y][next.x] = true; q.addLast(next); } } } count++; } return count; } } public static class Point { int y, x; char c; Point (int y, int x) { this.y = y; this.x = x; } Point (int y, int x, char c) { this.y = y; this.x = x; this.c = c; } public boolean isEqual(Point p) { if (this.y == p.y && this.x == p.x) return true; return false; } public int getY() {return this.y;} public int getX() {return this.x;} public char getC() {return this.c;} } ///////////////////////////////////////////////////////////////////////////////////////////////// // 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; } public static List<List<Integer>> nextDirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); g.get(b).add(a); } return g; } public static List<List<Integer>> nextUndirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); } return g; } ///////////////////////////////////////////////////////////////////////////////////////////////// // 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) { 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 { public static void solve () { //98765432 long a = 500000000; long b = 98765431; long c = 98765431; long A = nextLong(), B = nextLong(); println(a*B+A); // println((a*b+c)); // println(2*(a*b+c)); // println((long)Math.pow(10, 18)); } ///////////////////////////////////////////////////////////////////////////////////////////////// // 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 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;} } public static String yesno(boolean b) {return b ? "Yes" : "No";} public static class Grid { int h, w; Point[][] p; boolean[][] seen; Grid(int h, int w) { p = new Point[h][w]; this.h = h; this.w = w; for (int i=0; i<h; i++) { String s = next(); for (int j=0; j<w; j++) { p[i][j] = new Point(i, j, s.charAt(j)); } } } public Point getPoint(int y, int x) { return this.p[y][x]; } public List<Point> getAround4(Point p) { int y = p.y, x = p.x; List<Point> a = new ArrayList<>(); if (y >= 1) a.add(new Point(y-1, x)); if (y <= h-2) a.add(new Point(y+1, x)); if (x >= 1) a.add(new Point(y, x-1)); if (x <= w-2) a.add(new Point(y, x+1)); return a; } public List<Point> getAround8(int y, int x) { List<Point> a = new ArrayList<>(); if (y >= 1) { a.add(new Point(y-1, x)); if (x >= 1) a.add(new Point(y-1, x-1)); if (x <= w-2) a.add(new Point(y-1, x+1)); } if (y <= h-2) { a.add(new Point(y+1, x)); if (x >= 1) a.add(new Point(y+1, x-1)); if (x <= w-2) a.add(new Point(y+1, x+1)); } if (x >= 1) a.add(new Point(y, x-1)); if (x <= w-2) a.add(new Point(y, x+1)); return a; } public int countConnectedComponents() { int count = 0; seen = new boolean[h][w]; while(true) { Point start = null; //全ての連結成分を探索したかチェック boolean flag = true; for (int i=0; i<h; i++) { for (int j=0; j<w; j++) { if (seen[i][j] == false && p[i][j].c == '#') { flag = false; start = new Point(i, j); } } } if (flag == true) break; Deque<Point> q = new ArrayDeque<>(); q.add(start); seen[start.y][start.x] = true; //BFS while(q.size() > 0) { Point now = q.removeFirst(); for (Point next : getAround4(now)) { if (seen[next.y][next.x] == false && p[next.y][next.x].c == '#') { seen[next.y][next.x] = true; q.addLast(next); } } } count++; } return count; } } public static class Point { int y, x; char c; Point (int y, int x) { this.y = y; this.x = x; } Point (int y, int x, char c) { this.y = y; this.x = x; this.c = c; } public boolean isEqual(Point p) { if (this.y == p.y && this.x == p.x) return true; return false; } public int getY() {return this.y;} public int getX() {return this.x;} public char getC() {return this.c;} } ///////////////////////////////////////////////////////////////////////////////////////////////// // 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; } public static List<List<Integer>> nextDirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); g.get(b).add(a); } return g; } public static List<List<Integer>> nextUndirectedGraph(int n, int m) { List<List<Integer>> g = new ArrayList<>(); for (int i=0; i<n; i++) { g.add(new ArrayList<>()); } for (int i=0; i<m; i++) { int a = nextInt()-1, b = nextInt()-1; g.get(a).add(b); } return g; } ///////////////////////////////////////////////////////////////////////////////////////////////// // 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) { solve(); out.close(); } }
ConDefects/ConDefects/Code/arc131_a/Java/38921588
condefects-java_data_1361
//Har Har Mahadev import java.io.*; public class Main { public static void main(String[]args)throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); long a=Long.parseLong(buf.readLine()); long b=Long.parseLong(buf.readLine()); long ans=200000000*b+a; System.out.println(ans); } } //Har Har Mahadev import java.io.*; public class Main { public static void main(String[]args)throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); long a=Long.parseLong(buf.readLine()); long b=Long.parseLong(buf.readLine()); long ans=500000000*b+a; System.out.println(ans); } }
ConDefects/ConDefects/Code/arc131_a/Java/35872451
condefects-java_data_1362
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long a = sc.nextLong(); long b = sc.nextLong(); long ans = 500000000l * a + b; System.out.println(ans); } } import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long a = sc.nextLong(); long b = sc.nextLong(); long ans = 500000000l * b + a; System.out.println(ans); } }
ConDefects/ConDefects/Code/arc131_a/Java/28262356
condefects-java_data_1363
import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int number1 = sc.nextInt(); int number2 = sc.nextInt(); long result = 50000000000L * number2 + number1; out.println(result); out.flush(); } } import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int number1 = sc.nextInt(); int number2 = sc.nextInt(); long result = 500000000L * number2 + number1; out.println(result); out.flush(); } }
ConDefects/ConDefects/Code/arc131_a/Java/28679570
condefects-java_data_1364
import java.util.*; import java.math.*; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String todayNumberStr = sc.next(); // こいつはそのまま先頭に付けるので文字列 long tomorrowNumber = Long.parseLong(sc.next() + "0"); // 2で割るので余り防止 tomorrowNumber /= 2; String superLucky = todayNumberStr + String.valueOf(tomorrowNumber); System.out.println(superLucky); } } import java.util.*; import java.math.*; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String todayNumberStr = sc.next(); // こいつはそのまま先頭に付けるので文字列 long tomorrowNumber = Long.parseLong(sc.next() + "0"); // 2で割るので余り防止 tomorrowNumber /= 2; String superLucky = todayNumberStr + "0" + String.valueOf(tomorrowNumber); // 0をねじ込み桁上がり阻止 System.out.println(superLucky); } }
ConDefects/ConDefects/Code/arc131_a/Java/28348692
condefects-java_data_1365
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ 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(); String s=a+"0"; if(b%2==0) System.out.println(s+(b/2)); else System.out.println(s+((b/10)/2)+""+(b%10)); } } /****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ 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(); String s=a+"0"; if(b%2==0) System.out.println(s+(b/2)); else System.out.println(s+(b/2)+"5"); } }
ConDefects/ConDefects/Code/arc131_a/Java/27756570
condefects-java_data_1366
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int A = Integer.parseInt(br.readLine()); int B = Integer.parseInt(br.readLine()); System.out.println(B / 2 + ((B % 2 == 0) ? "0" : "5") + A); } } import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int A = Integer.parseInt(br.readLine()); int B = Integer.parseInt(br.readLine()); System.out.println(((B / 2 == 0) ? "" : (B / 2)) + ((B % 2 == 0) ? "0" : "5") + A); } }
ConDefects/ConDefects/Code/arc131_a/Java/34335916
condefects-java_data_1367
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); Long a=sc.nextLong(); Long b=sc.nextLong(); System.out.println(a+""+(b*50)); } } import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); Long a=sc.nextLong(); Long b=sc.nextLong(); System.out.println((b*50)+""+a); } }
ConDefects/ConDefects/Code/arc131_a/Java/27756885
condefects-java_data_1368
import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int a = sc.nextInt(); int b = sc.nextInt(); out.print(50000000 *b + a ); out.flush(); } } import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int a = sc.nextInt(); int b = sc.nextInt(); out.print(500000000L *b + a ); out.flush(); } }
ConDefects/ConDefects/Code/arc131_a/Java/28679439
condefects-java_data_1369
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 = "1 4013"; static long go(int k) { if(k % 4 == 0 || k % 5 == 0)return -1; long i = 1; for(long x = 2%k;i < 30;i++){ if(x == 0){ return i; } x = (x * 10 + 2) % k; } int[][] M = { {10%k, 2%k}, {0, 1%k} }; int lam = k; int[][] f = factor(k, primes); int o = 1; for(int[] u : f){ if(u[0] == 3){ for(int j = 0;j < u[1];j++){ o *= 3; lam /= 3; } } } for (int[] u : f) { if(u[0] != 3) { lam = lam / u[0] * (u[0] - 1); } } lam *= o; for(int d = lam;d <= lam+30;d++){ int to = pow(M, new int[]{0, 1}, d, k)[0]; if(to == 0){ int offset = d-lam; int u = lam; int[][] g = factor(u, primes); for(int[] item : g){ int p = item[0]; while(u % p == 0 && pow(M, new int[]{0, 1}, u/p+offset, k)[0] == 0){ u /= p; } } return u + offset; } } throw new RuntimeException(); } static int[] primes = sieveEratosthenes(10000); public static int[][] factor(int n, int[] primes) { int[][] ret = new int[9][2]; int rp = 0; for(int p : primes){ if(p * p > n)break; int i; for(i = 0;n % p == 0;n /= p, i++); if(i > 0){ ret[rp][0] = p; ret[rp][1] = i; rp++; } } if(n != 1){ ret[rp][0] = n; ret[rp][1] = 1; rp++; } return Arrays.copyOf(ret, rp); } public static int[] sieveEratosthenes(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } public static int[] pow(int[][] A, int[] v, long e, int mod) { int[][] MUL = A; for(int i = 0;i < v.length;i++)v[i] %= mod; for(;e > 0;e>>>=1) { if((e&1)==1)v = mul(MUL, v, mod); MUL = p2(MUL, mod); } return v; } // int matrix * int vector public static int[] mul(int[][] A, int[] v, int mod) { int m = A.length; int n = v.length; int[] w = new int[m]; for(int i = 0;i < m;i++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * v[k]; sum %= mod; } w[i] = (int)sum; } return w; } // int matrix^2 (cannot ensure negative values) public static int[][] p2(int[][] A, int mod) { int n = A.length; int[][] C = new int[n][n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * A[k][j]; sum %= mod; } C[i][j] = (int)sum; } } return C; } static void solve() { for(int T = ni();T > 0;T--){ int K = ni(); out.println(go(K)); } // 4 5 8 // tr(go(99999987)); // tr(pow(10, 66666656, 99999987)); // for(int k = 100000000;k >= 1;k--){ // for(int k = 99999999;k >= 1;k-=3){ // if(k % 4 == 0 || k % 5 == 0)continue; // long i = 1; // for(long x = 2%k;x != 0;i++){//i < 100000000;i++){ // x = (x * 10 + 2) % k; //// if(x == 0){ //// tr(i); //// } // } // tr(k, i); // tr(go(k)); // assert i == go(k); // } } 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 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(); tr(G-S+"ms"); } 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[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); 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 long go(int k) { if(k % 4 == 0 || k % 5 == 0)return -1; long i = 1; for(long x = 2%k;i < 30;i++){ if(x == 0){ return i; } x = (x * 10 + 2) % k; } int[][] M = { {10%k, 2%k}, {0, 1%k} }; int lam = k; int[][] f = factor(k, primes); int o = 1; for(int[] u : f){ if(u[0] == 3){ for(int j = 0;j < u[1];j++){ o *= 3; lam /= 3; } } } for (int[] u : f) { if(u[0] != 3) { lam = lam / u[0] * (u[0] - 1); } } lam *= o; for(int d = lam;d <= lam+30;d++){ int to = pow(M, new int[]{0, 1}, d, k)[0]; if(to == 0){ int offset = d-lam; int u = lam; int[][] g = factor(u, primes); for(int[] item : g){ int p = item[0]; while(u % p == 0 && pow(M, new int[]{0, 1}, u/p+offset, k)[0] == 0){ u /= p; } } return u + offset; } } throw new RuntimeException(); } static int[] primes = sieveEratosthenes(10000); public static int[][] factor(int n, int[] primes) { int[][] ret = new int[9][2]; int rp = 0; for(int p : primes){ if(p * p > n)break; int i; for(i = 0;n % p == 0;n /= p, i++); if(i > 0){ ret[rp][0] = p; ret[rp][1] = i; rp++; } } if(n != 1){ ret[rp][0] = n; ret[rp][1] = 1; rp++; } return Arrays.copyOf(ret, rp); } public static int[] sieveEratosthenes(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } public static int[] pow(int[][] A, int[] v, long e, int mod) { int[][] MUL = A; for(int i = 0;i < v.length;i++)v[i] %= mod; for(;e > 0;e>>>=1) { if((e&1)==1)v = mul(MUL, v, mod); MUL = p2(MUL, mod); } return v; } // int matrix * int vector public static int[] mul(int[][] A, int[] v, int mod) { int m = A.length; int n = v.length; int[] w = new int[m]; for(int i = 0;i < m;i++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * v[k]; sum %= mod; } w[i] = (int)sum; } return w; } // int matrix^2 (cannot ensure negative values) public static int[][] p2(int[][] A, int mod) { int n = A.length; int[][] C = new int[n][n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * A[k][j]; sum %= mod; } C[i][j] = (int)sum; } } return C; } static void solve() { for(int T = ni();T > 0;T--){ int K = ni(); out.println(go(K)); } // 4 5 8 // tr(go(99999987)); // tr(pow(10, 66666656, 99999987)); // for(int k = 100000000;k >= 1;k--){ // for(int k = 99999999;k >= 1;k-=3){ // if(k % 4 == 0 || k % 5 == 0)continue; // long i = 1; // for(long x = 2%k;x != 0;i++){//i < 100000000;i++){ // x = (x * 10 + 2) % k; //// if(x == 0){ //// tr(i); //// } // } // tr(k, i); // tr(go(k)); // assert i == go(k); // } } 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 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(); tr(G-S+"ms"); } 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[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); 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/abc222_g/Java/26462462
condefects-java_data_1370
import java.io.*; import java.util.*; public class Main { static PrintWriter pw; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int T = Integer.parseInt(st.nextToken()); while(T-- > 0){ st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int K = Integer.parseInt(st.nextToken()); ArrayList<Integer>[] tree = new ArrayList[N + 1]; for(int i = 1; i<=N; i++) tree[i] = new ArrayList<>(); int[] pa = new int[N + 1]; st = new StringTokenizer(br.readLine()); for(int i = 2; i<=N; i++) pa[i] = Integer.parseInt(st.nextToken()); for(int i = 2; i<=N; i++){ tree[i].add(pa[i]); tree[pa[i]].add(i); } int[] a = new int[N + 1]; st = new StringTokenizer(br.readLine()); for(int i = 1; i<=N; i++) a[i] = Integer.parseInt(st.nextToken()); int[] sizes = new int[N + 1]; TreeSet<Integer>[] set = new TreeSet[N + 1]; for(int i = 1; i<=N; i++) set[i] = new TreeSet<>(); int[] not_placed = new int[N + 1]; dfs_st(1, new boolean[N + 1], tree, -1, set, not_placed, a, sizes); boolean good = false; for(int i = 1; i<=N; i++){ //if(i == 2) pw.println(set[2].size()); if(process(i, K, tree, set, not_placed)){ good = true; break; } } pw.println(good ? "Alice":"Bob"); } pw.close(); } public static void dfs_st(int node, boolean[] vis, ArrayList<Integer>[] tree, int pa, TreeSet<Integer>[] set, int[] not_placed, int[] a, int[] sizes){ vis[node] = true; if(a[node] != -1) set[node].add(a[node]); if(a[node] == -1) not_placed[node]++; for(int ch: tree[node]){ if(ch == pa) continue; if(!vis[ch]) dfs_st(ch, vis, tree, node, set,not_placed, a, sizes); sizes[node] += sizes[ch]; not_placed[node] += not_placed[ch]; for(int v: set[ch]) set[node].add(v); } } public static boolean process(int node, int K, ArrayList<Integer>[] tree, TreeSet<Integer>[] set, int[] not_placed){ //process a subtree of node if(not_placed[node] > 1 || set[node].contains(K) || set[node].size() + not_placed[node] < K) return false; int cnt = 0; for(int i = 0; i<K; i++){ if(!set[node].contains(i)) cnt++; } if(cnt == 0) return false; return cnt <= not_placed[node]; } } import java.io.*; import java.util.*; public class Main { static PrintWriter pw; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(br.readLine()); int T = Integer.parseInt(st.nextToken()); while(T-- > 0){ st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int K = Integer.parseInt(st.nextToken()); ArrayList<Integer>[] tree = new ArrayList[N + 1]; for(int i = 1; i<=N; i++) tree[i] = new ArrayList<>(); int[] pa = new int[N + 1]; st = new StringTokenizer(br.readLine()); for(int i = 2; i<=N; i++) pa[i] = Integer.parseInt(st.nextToken()); for(int i = 2; i<=N; i++){ tree[i].add(pa[i]); tree[pa[i]].add(i); } int[] a = new int[N + 1]; st = new StringTokenizer(br.readLine()); for(int i = 1; i<=N; i++) a[i] = Integer.parseInt(st.nextToken()); int[] sizes = new int[N + 1]; TreeSet<Integer>[] set = new TreeSet[N + 1]; for(int i = 1; i<=N; i++) set[i] = new TreeSet<>(); int[] not_placed = new int[N + 1]; dfs_st(1, new boolean[N + 1], tree, -1, set, not_placed, a, sizes); boolean good = false; for(int i = 1; i<=N; i++){ if(process(i, K, tree, set, not_placed)){ good = true; break; } } pw.println(good ? "Alice":"Bob"); } pw.close(); } public static void dfs_st(int node, boolean[] vis, ArrayList<Integer>[] tree, int pa, TreeSet<Integer>[] set, int[] not_placed, int[] a, int[] sizes){ vis[node] = true; if(a[node] != -1) set[node].add(a[node]); if(a[node] == -1) not_placed[node]++; for(int ch: tree[node]){ if(ch == pa) continue; if(!vis[ch]) dfs_st(ch, vis, tree, node, set,not_placed, a, sizes); sizes[node] += sizes[ch]; not_placed[node] += not_placed[ch]; for(int v: set[ch]) set[node].add(v); } } public static boolean process(int node, int K, ArrayList<Integer>[] tree, TreeSet<Integer>[] set, int[] not_placed){ //process a subtree of node if(not_placed[node] > 1 || set[node].contains(K) || set[node].size() + not_placed[node] < K) return false; int cnt = 0; for(int i = 0; i<K; i++){ if(!set[node].contains(i)) cnt++; } return cnt <= not_placed[node]; } }
ConDefects/ConDefects/Code/arc162_c/Java/44838889
condefects-java_data_1371
import java.util.*; import java.io.*; class Main { private static final void solve() throws IOException { final int n = ni(); K = ni(); final int m = n - 1; var uu = new int[m]; var vv = new int[m]; var ty_cnt = new int[n]; for (int i = 0; i < m; i++) { ty_cnt[uu[i] = ni() - 1]++; ty_cnt[vv[i] = i + 1]++; } g = new int[n][]; for (int i = 0; i < n; i++) g[i] = new int[ty_cnt[i]]; Arrays.fill(ty_cnt, 0); for (int i = 0; i < m; i++) { g[uu[i]][ty_cnt[uu[i]]++] = vv[i]; g[vv[i]][ty_cnt[vv[i]]++] = uu[i]; } a = ni(n); cnt = new int[n + 1]; ans = false; f(0, -1); ou.println(ans ? "Alice" : "Bob"); return; } private static int K; private static int[] a; private static int[][] g; private static int[] cnt; private static int zero; private static boolean ans; private static final void f(int to, int no) { Arrays.fill(cnt, 0); zero = 0; g(to, no); int jud = 0; for (int i = 0; i < K; i++) if (cnt[i] == 0) jud++; boolean ok = false; ok |= zero == 0 && jud == 0; ok |= zero == 1 && jud == 1; ok &= cnt[K] == 0; if (ok) { ans = true; return; } for (int i : g[to]) { if (i == no) continue; f(i, to); } } private static final void g(int to, int no) { if (a[to] == -1) zero++; else cnt[a[to]]++; for (int i : g[to]) { if (i == no) continue; g(i, to); } } 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 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 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 { final int n = ni(); K = ni(); final int m = n - 1; var uu = new int[m]; var vv = new int[m]; var ty_cnt = new int[n]; for (int i = 0; i < m; i++) { ty_cnt[uu[i] = ni() - 1]++; ty_cnt[vv[i] = i + 1]++; } g = new int[n][]; for (int i = 0; i < n; i++) g[i] = new int[ty_cnt[i]]; Arrays.fill(ty_cnt, 0); for (int i = 0; i < m; i++) { g[uu[i]][ty_cnt[uu[i]]++] = vv[i]; g[vv[i]][ty_cnt[vv[i]]++] = uu[i]; } a = ni(n); cnt = new int[n + 1]; ans = false; f(0, -1); ou.println(ans ? "Alice" : "Bob"); return; } private static int K; private static int[] a; private static int[][] g; private static int[] cnt; private static int zero; private static boolean ans; private static final void f(int to, int no) { Arrays.fill(cnt, 0); zero = 0; g(to, no); int jud = 0; for (int i = 0; i < K; i++) if (cnt[i] == 0) jud++; boolean ok = false; ok |= zero == 0 && jud == 0; ok |= zero == 1 && jud <= 1; ok &= cnt[K] == 0; if (ok) { ans = true; return; } for (int i : g[to]) { if (i == no) continue; f(i, to); } } private static final void g(int to, int no) { if (a[to] == -1) zero++; else cnt[a[to]]++; for (int i : g[to]) { if (i == no) continue; g(i, to); } } 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 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 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/arc162_c/Java/42721434
condefects-java_data_1372
import java.io.*; import java.util.*; public class Main { int[] p; int[] ar; List<List<Integer>> g=new ArrayList<>(); List<Set<Integer>> chv=new ArrayList<>(); int[] slot; boolean win=false; int k,n; public void solve() throws Exception { n = nextInt(); k=nextInt(); p = new int[n+1]; ar = new int[n+1]; slot = new int[n+1]; for (int i = 0; i <= n; i++) { g.add(new ArrayList<>()); chv.add(new HashSet<>()); } for (int i = 2; i <= n; i++) { p[i]=nextInt(); g.get(p[i]).add(i); } for (int i = 1; i <= n; i++) { ar[i]=nextInt(); } dfs(1); System.out.println(win?"Alice":"Bob"); } void dfs(int i) { int s=ar[i]==-1?1:0; chv.get(i).add(ar[i]); for (Integer ch : g.get(i)) { dfs(ch); s += slot[ch]; for (Integer vv : chv.get(ch)) { chv.get(i).add(vv); } } slot[i]=s; if (chv.get(i).contains(k)) { return; } int show=0; for (int j = 0; j < k; j++) { if (chv.get(i).contains(j)) { show++; } } if (show==k-1&&s==1||show==k&&s==0) { win=true; } } public static void main(String[] args) throws Exception { int t=nextInt(); for (int i = 0; i < t; i++) { new Main().solve(); } } static PrintWriter out = new PrintWriter(System.out, true); static InputReader in = new InputReader(System.in); static String next() { return in.next(); } static int nextInt() { return Integer.parseInt(in.next()); } static long nextLong() { return Long.parseLong(in.next()); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } } import java.io.*; import java.util.*; public class Main { int[] p; int[] ar; List<List<Integer>> g=new ArrayList<>(); List<Set<Integer>> chv=new ArrayList<>(); int[] slot; boolean win=false; int k,n; public void solve() throws Exception { n = nextInt(); k=nextInt(); p = new int[n+1]; ar = new int[n+1]; slot = new int[n+1]; for (int i = 0; i <= n; i++) { g.add(new ArrayList<>()); chv.add(new HashSet<>()); } for (int i = 2; i <= n; i++) { p[i]=nextInt(); g.get(p[i]).add(i); } for (int i = 1; i <= n; i++) { ar[i]=nextInt(); } dfs(1); System.out.println(win?"Alice":"Bob"); } void dfs(int i) { int s=ar[i]==-1?1:0; chv.get(i).add(ar[i]); for (Integer ch : g.get(i)) { dfs(ch); s += slot[ch]; for (Integer vv : chv.get(ch)) { chv.get(i).add(vv); } } slot[i]=s; if (chv.get(i).contains(k)) { return; } int show=0; for (int j = 0; j < k; j++) { if (chv.get(i).contains(j)) { show++; } } if (show==k-1&&s==1||show==k&&s<=1) { win=true; } } public static void main(String[] args) throws Exception { int t=nextInt(); for (int i = 0; i < t; i++) { new Main().solve(); } } static PrintWriter out = new PrintWriter(System.out, true); static InputReader in = new InputReader(System.in); static String next() { return in.next(); } static int nextInt() { return Integer.parseInt(in.next()); } static long nextLong() { return Long.parseLong(in.next()); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } }
ConDefects/ConDefects/Code/arc162_c/Java/42727079
condefects-java_data_1373
import java.io.*; import java.util.*; public class Main { static Scanner sc; static PrintWriter out; public static void main(String[] args) { sc = new Scanner(System.in); out = new PrintWriter(System.out); new Main().solve(); out.flush(); } public void solve() { int n = sc.nextInt(); for(int i=0; i<n; i++) solve2(); } void solve2() { int n = sc.nextInt(); int k = sc.nextInt(); int[] p = new int[n]; p[0] = -1; int[] sub = new int[n]; for(int i=1; i<n; i++) { p[i] = sc.nextInt()-1; sub[p[i]]++; } int[] num = new int[n]; int[] sp = new int[n]; Arrays.setAll(num, i -> Math.min(sc.nextInt(), k+1)); boolean[][] mark = new boolean[n][k+2]; for(int i=0; i<n; i++) { if(num[i]!=-1) { mark[i][num[i]] = true; } else { sp[i]++; } } Queue<Integer> q = new LinkedList<>(); for(int i=0; i<n; i++) { if(sub[i] == 0) { q.add(i); } } while(!q.isEmpty()) { int pos = q.poll(); //out.println("pos = " + pos); int cnt = 0; for(int i=0; i<k; i++) { if(mark[pos][i]) { cnt++; } } if(!mark[pos][k] && sp[pos] <= 1 && (cnt == k || cnt == k-1 && num[pos]==-1)) { out.println("Alice"); return; } if(p[pos] == -1) { continue; } for(int i=0; i<k+2; i++) { mark[p[pos]][i] |= mark[pos][i]; } sp[p[pos]] += sp[pos]; sub[p[pos]]--; if(sub[p[pos]] == 0) { q.add(p[pos]); } } out.println("Bob"); } } import java.io.*; import java.util.*; public class Main { static Scanner sc; static PrintWriter out; public static void main(String[] args) { sc = new Scanner(System.in); out = new PrintWriter(System.out); new Main().solve(); out.flush(); } public void solve() { int n = sc.nextInt(); for(int i=0; i<n; i++) solve2(); } void solve2() { int n = sc.nextInt(); int k = sc.nextInt(); int[] p = new int[n]; p[0] = -1; int[] sub = new int[n]; for(int i=1; i<n; i++) { p[i] = sc.nextInt()-1; sub[p[i]]++; } int[] num = new int[n]; int[] sp = new int[n]; Arrays.setAll(num, i -> Math.min(sc.nextInt(), k+1)); boolean[][] mark = new boolean[n][k+2]; for(int i=0; i<n; i++) { if(num[i]!=-1) { mark[i][num[i]] = true; } else { sp[i]++; } } Queue<Integer> q = new LinkedList<>(); for(int i=0; i<n; i++) { if(sub[i] == 0) { q.add(i); } } while(!q.isEmpty()) { int pos = q.poll(); //out.println("pos = " + pos); int cnt = 0; for(int i=0; i<k; i++) { if(mark[pos][i]) { cnt++; } } if(!mark[pos][k] && (sp[pos] <= 1 && cnt == k || sp[pos] == 1 && cnt == k-1)) { out.println("Alice"); return; } if(p[pos] == -1) { continue; } for(int i=0; i<k+2; i++) { mark[p[pos]][i] |= mark[pos][i]; } sp[p[pos]] += sp[pos]; sub[p[pos]]--; if(sub[p[pos]] == 0) { q.add(p[pos]); } } out.println("Bob"); } }
ConDefects/ConDefects/Code/arc162_c/Java/42727945
condefects-java_data_1374
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collections; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.TreeSet; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start(); } final long mod = 998244353; long pow(long a, long n) { if (n == 0) return 1; return pow(a * a % mod, n / 2) * (n % 2 == 1 ? a : 1) % mod; } long inv(long a) { return pow(a, mod - 2); } public void run() { FastScanner sc=new FastScanner(); PrintWriter pw=new PrintWriter(System.out); int N = sc.nextInt(); int M = sc.nextInt(); long H = sc.nextLong(); long[] A = new long[N]; int[] B = new int[N]; for (int i = 0; i < N; ++i) { A[i] = sc.nextLong(); B[i] = sc.nextInt() - 1; } long HP = H; class State implements Comparable<State> { long point; int id; public State(long point, int id) { this.point = point; this.id = id; } public int compareTo(State o) { if (point != o.point) return Long.compare(point, o.point); else return Integer.compare(id, o.id); }; } TreeSet<State> ok = new TreeSet<>(); TreeSet<State> ng = new TreeSet<>(); long[] point = new long[M]; int[] ans = new int[N]; for (int i = 0; i < N; ++i) { State state = new State(point[B[i]], B[i]); if (ok.contains(state)) { ok.remove(state); state.point += A[i]; ok.add(state); } else { if (ng.contains(state)) { ng.remove(state); } state.point += A[i]; ng.add(state); HP -= A[i]; } point[B[i]] += A[i]; while (!ng.isEmpty() && !ok.isEmpty() && ng.last().point > ok.first().point) { State s0 = ok.pollFirst(); State s1 = ng.pollLast(); HP += s1.point - s0.point; ok.add(s1); ng.add(s0); } while (!ok.isEmpty() && HP - ok.first().point > 0) { HP -= ok.first().point; ng.add(ok.pollFirst()); } while (HP < 0) { State ns = ng.pollLast(); HP += ns.point; ok.add(ns); } ans[i] = ok.size(); } int[] ans2 = new int[M + 1]; int p = 0; for (int i = 0; i <= M; ++i) { while (p < N && ans[p] <= i) ++p; ans2[i] = p; } for (int i = 0; i <= M; ++i) { pw.print(ans2[i] + (i == M? "\n" : " ")); } pw.close(); } void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));} } 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; }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;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); 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() { return (int)nextLong(); } } import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collections; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.TreeSet; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start(); } final long mod = 998244353; long pow(long a, long n) { if (n == 0) return 1; return pow(a * a % mod, n / 2) * (n % 2 == 1 ? a : 1) % mod; } long inv(long a) { return pow(a, mod - 2); } public void run() { FastScanner sc=new FastScanner(); PrintWriter pw=new PrintWriter(System.out); int N = sc.nextInt(); int M = sc.nextInt(); long H = sc.nextLong(); long[] A = new long[N]; int[] B = new int[N]; for (int i = 0; i < N; ++i) { A[i] = sc.nextLong(); B[i] = sc.nextInt() - 1; } long HP = H; class State implements Comparable<State> { long point; int id; public State(long point, int id) { this.point = point; this.id = id; } public int compareTo(State o) { if (point != o.point) return Long.compare(point, o.point); else return Integer.compare(id, o.id); }; } TreeSet<State> ok = new TreeSet<>(); TreeSet<State> ng = new TreeSet<>(); long[] point = new long[M]; int[] ans = new int[N]; for (int i = 0; i < N; ++i) { State state = new State(point[B[i]], B[i]); if (ok.contains(state)) { ok.remove(state); state.point += A[i]; ok.add(state); } else { if (ng.contains(state)) { ng.remove(state); } state.point += A[i]; ng.add(state); HP -= A[i]; } point[B[i]] += A[i]; while (!ng.isEmpty() && !ok.isEmpty() && ng.last().point > ok.first().point) { State s0 = ok.pollFirst(); State s1 = ng.pollLast(); HP += s1.point - s0.point; ok.add(s1); ng.add(s0); } while (!ok.isEmpty() && HP - ok.first().point > 0) { HP -= ok.first().point; ng.add(ok.pollFirst()); } while (HP <= 0) { State ns = ng.pollLast(); HP += ns.point; ok.add(ns); } ans[i] = ok.size(); } int[] ans2 = new int[M + 1]; int p = 0; for (int i = 0; i <= M; ++i) { while (p < N && ans[p] <= i) ++p; ans2[i] = p; } for (int i = 0; i <= M; ++i) { pw.print(ans2[i] + (i == M? "\n" : " ")); } pw.close(); } void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));} } 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; }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;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); 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() { return (int)nextLong(); } }
ConDefects/ConDefects/Code/abc314_g/Java/44539447
condefects-java_data_1375
import java.util.*; import java.io.*; import java.util.function.*; public final class Main { private static final Scanner scanner; private static final PrintWriter writer; static { scanner = new Scanner(System.in); writer = new PrintWriter(System.out); } public static final void main(String[] args) { final long N = getNextLong(); long X = Long.MAX_VALUE; for(long a = 0; a < 1000000; a++) { long l = a; long r = 1000000; if(func(a, l) > N) { break; } else if(func(a, l) == N) { r = l; } while(r - l > 1) { final long m = (r + l) / 2; if(func(a, m) < N) { l = m; } else { r = m; } } X = min(X, func(a, r)); } println(X); flush(); } private static final long func(long a, long b) { return a * a * a + a * a * b + a * b * b + b * b * b; } private static final int max(int a, int b) { return a < b ? b : a; } private static final int max(int... arg) { return Arrays.stream(arg).max().orElse(0); } private static final int min(int a, int b) { return a > b ? b : a; } private static final int min(int... arg) { return Arrays.stream(arg).min().orElse(0); } private static final int abs(int x) { return x < 0 ? -x : x; } private static final int pow(int x, int p) { return p <= 0 ? 1 : x * pow(x, p - 1); } private static final long max(long a, long b) { return a < b ? b : a; } private static final long max(long... arg) { return Arrays.stream(arg).max().orElse(0); } private static final long min(long a, long b) { return a > b ? b : a; } private static final long min(long... arg) { return Arrays.stream(arg).min().orElse(0); } private static final long abs(long x) { return x < 0L ? -x : x; } private static final long pow(long x, int p) { return p <= 0 ? 1L : x * pow(x, p - 1); } private static final String getNext() { return scanner.next(); } private static final int getNextInt() { return Integer.parseInt(scanner.next()); } private static final long getNextLong() { return Long.parseLong(scanner.next()); } private static final int[] getIntArray(int length) { int[] ret = new int[length]; for(int i = 0; i < length; i++) { ret[i] = getNextInt(); } return ret; // return IntStream.generate(()->getNextInt()).limit(length).toArray(); } private static final int[] getIntArray(int length, IntUnaryOperator mapper) { int[] ret = new int[length]; for(int i = 0; i < length; i++) { ret[i] = mapper.applyAsInt(getNextInt()); } return ret; // return IntStream.generate(()->getNextInt()).limit(length).map(mapper).toArray(); } private static final int[][] get2dIntArray(int rows, int cols) { int[][] ret = new int[rows][]; for(int i = 0; i < rows; i++) { ret[i] = getIntArray(cols); } return ret; // return Stream.generate(()->getIntArray(cols)).limit(rows).toArray(int[][]::new); } private static final int[][] get2dIntArray(int rows, int cols, IntUnaryOperator mapper) { int[][] ret = new int[rows][]; for(int i = 0; i < rows; i++) { ret[i] = getIntArray(cols, mapper); } return ret; // return Stream.generate(()->getIntArray(cols, mapper)).limit(rows).toArray(int[][]::new); } private static final void print(int[] argi) { for(int i = 0, length = argi.length; i < length; i++) { print(String.valueOf(argi[i]) + " "); } // Arrays.stream(argi).forEach(i->print(String.valueOf(i) + " ")); } private static final void print(Object obj) { writer.print(obj); } private static final void print(Object... arg) { for(int i = 0, length = arg.length; i < length; i++) { print(arg[i]); } // Arrays.stream(arg).forEach(obj->print(obj)); } private static final void println(int[] argi) { print(argi); println(); } private static final void println(Object obj) { print(obj); println(); } private static final void println(Object... arg) { print(arg); println(); } private static final void println() { writer.println(); } private static final void flush() { writer.flush(); } } import java.util.*; import java.io.*; import java.util.function.*; public final class Main { private static final Scanner scanner; private static final PrintWriter writer; static { scanner = new Scanner(System.in); writer = new PrintWriter(System.out); } public static final void main(String[] args) { final long N = getNextLong(); long X = Long.MAX_VALUE; for(long a = 0; a < 1000000; a++) { long l = max(a - 1, 0); long r = 1000000; if(func(a, l) > N) { break; } else if(func(a, l) == N) { r = l; } while(r - l > 1) { final long m = (r + l) / 2; if(func(a, m) < N) { l = m; } else { r = m; } } X = min(X, func(a, r)); } println(X); flush(); } private static final long func(long a, long b) { return a * a * a + a * a * b + a * b * b + b * b * b; } private static final int max(int a, int b) { return a < b ? b : a; } private static final int max(int... arg) { return Arrays.stream(arg).max().orElse(0); } private static final int min(int a, int b) { return a > b ? b : a; } private static final int min(int... arg) { return Arrays.stream(arg).min().orElse(0); } private static final int abs(int x) { return x < 0 ? -x : x; } private static final int pow(int x, int p) { return p <= 0 ? 1 : x * pow(x, p - 1); } private static final long max(long a, long b) { return a < b ? b : a; } private static final long max(long... arg) { return Arrays.stream(arg).max().orElse(0); } private static final long min(long a, long b) { return a > b ? b : a; } private static final long min(long... arg) { return Arrays.stream(arg).min().orElse(0); } private static final long abs(long x) { return x < 0L ? -x : x; } private static final long pow(long x, int p) { return p <= 0 ? 1L : x * pow(x, p - 1); } private static final String getNext() { return scanner.next(); } private static final int getNextInt() { return Integer.parseInt(scanner.next()); } private static final long getNextLong() { return Long.parseLong(scanner.next()); } private static final int[] getIntArray(int length) { int[] ret = new int[length]; for(int i = 0; i < length; i++) { ret[i] = getNextInt(); } return ret; // return IntStream.generate(()->getNextInt()).limit(length).toArray(); } private static final int[] getIntArray(int length, IntUnaryOperator mapper) { int[] ret = new int[length]; for(int i = 0; i < length; i++) { ret[i] = mapper.applyAsInt(getNextInt()); } return ret; // return IntStream.generate(()->getNextInt()).limit(length).map(mapper).toArray(); } private static final int[][] get2dIntArray(int rows, int cols) { int[][] ret = new int[rows][]; for(int i = 0; i < rows; i++) { ret[i] = getIntArray(cols); } return ret; // return Stream.generate(()->getIntArray(cols)).limit(rows).toArray(int[][]::new); } private static final int[][] get2dIntArray(int rows, int cols, IntUnaryOperator mapper) { int[][] ret = new int[rows][]; for(int i = 0; i < rows; i++) { ret[i] = getIntArray(cols, mapper); } return ret; // return Stream.generate(()->getIntArray(cols, mapper)).limit(rows).toArray(int[][]::new); } private static final void print(int[] argi) { for(int i = 0, length = argi.length; i < length; i++) { print(String.valueOf(argi[i]) + " "); } // Arrays.stream(argi).forEach(i->print(String.valueOf(i) + " ")); } private static final void print(Object obj) { writer.print(obj); } private static final void print(Object... arg) { for(int i = 0, length = arg.length; i < length; i++) { print(arg[i]); } // Arrays.stream(arg).forEach(obj->print(obj)); } private static final void println(int[] argi) { print(argi); println(); } private static final void println(Object obj) { print(obj); println(); } private static final void println(Object... arg) { print(arg); println(); } private static final void println() { writer.println(); } private static final void flush() { writer.flush(); } }
ConDefects/ConDefects/Code/abc246_d/Java/41630861
condefects-java_data_1376
import java.util.Scanner; public class Main { public static long f(long a, long b) { return a*a*a + a*a*b + a*b*b + b*b*b; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); long X = sc.nextLong(); long i = 0; long j = 1000000; long ans = Long.MAX_VALUE; for(; i <= j; i++) { while(f(i,j) > X) { ans = Math.min(ans, f(i,j)); j--; } } System.out.println(ans); } } import java.util.Scanner; public class Main { public static long f(long a, long b) { return a*a*a + a*a*b + a*b*b + b*b*b; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); long X = sc.nextLong(); long i = 0; long j = 1000000; long ans = Long.MAX_VALUE; for(; i <= j; i++) { while(f(i,j) >= X) { ans = Math.min(ans, f(i,j)); j--; } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc246_d/Java/44920254
condefects-java_data_1377
import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.util.*; public class Main { private static final long MAXA = 100000; void solve() { long n = scanner.nextLong(); if(n == 0){ out.println(0); return; } long res = Long.MAX_VALUE; for(long a = 0; a <= MAXA; a++){ long b = findB(n, a); res = min(res, calc(a,b)); } out.println(res); } long findB(long n, long a){ long l = 1, r = 1000001; while(l + 1 < r){ long mid = (l+r)/2; long curr = calc(a, mid); if(curr >= n){ r = mid; } else{ l = mid; } } if(calc(a, l) >= n){ return l; } return r; } long calc(long a, long b){ long res = 0; long[] tmp = {a*a*a, b*b*b, a*a*b, a*b*b}; for(long val : tmp){ res += val; } return res; } private static final boolean memory = false; private static final boolean singleTest = true; // ----- runner templates ----- // void run() { int numOfTests = singleTest? 1: scanner.nextInt(); for(int testIdx = 1; testIdx <= numOfTests; testIdx++){ solve(); } out.flush(); out.close(); } // ----- runner templates ----- // public static void main(String[] args) { if(memory) { new Thread(null, () -> new Main().run(), "go", 1 << 26).start(); } else{ new Main().run(); } } //------ input and output ------// public static FastScanner scanner = new FastScanner(System.in); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public FastScanner(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isWhitespace(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = read(); } while (!isWhitespace(c)); return negative ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { int c = read(); while (isWhitespace(c)) { c = read(); } return (char) c; } public String next() { int c = read(); while (isWhitespace(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isWhitespace(c)); return res.toString(); } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } int[] nextIntArray(int n, int base){ int[] arr = new int[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextInt();} return arr; } long[] nextLongArray(int n, int base){ long[] arr = new long[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextLong();} return arr; } int[][] nextIntGrid(int n, int m, int base){ int[][] grid = new int[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextInt();}} return grid; } char[][] nextCharGrid(int n, int m, int base){ char[][] grid = new char[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextChar();}} return grid; } double[][] nextDoubleGrid(int n, int m, int base){ double[][] grid = new double[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextDouble();}} return grid; } int[][] nextUnweightedGraph(int n, int m, int base){ int[][] g = new int[base + n][]; int[][] edges = new int[m][2]; int[] adjSize = new int[n+base]; for(int i = 0; i < m; i++){ int a = scanner.nextInt(), b = scanner.nextInt(); edges[i][0]=a; adjSize[a]++; edges[i][1]=b; adjSize[b]++; } for(int i = base; i < base + n; i++){ g[i]=new int[adjSize[i]]; adjSize[i]=0; } for(int[] e: edges){ int a = e[0], b = e[1]; g[a][adjSize[a]++]=b; g[b][adjSize[b]++]=a; } return g; } //------ debug and print functions ------// void debug(Object...os){out.println(deepToString(os));} void print(int[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(long[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(char[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(Object... o){for(int i = 0; i < o.length; i++){out.print(o[i]);out.print(i==o.length-1?'\n':' ');}} <T> void printArrayList(List<T> arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr.get(i));out.print(i==end? '\n':' ');}} //------ sort primitive type arrays ------// static void sort(int[] arr){ List<Integer> temp = new ArrayList<>(); for(int val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(long[] arr){ List<Long> temp = new ArrayList<>(); for(long val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(char[] arr) { List<Character> temp = new ArrayList<>(); for (char val : arr) {temp.add(val);} Collections.sort(temp); for (int i = 0; i < arr.length; i++) {arr[i] = temp.get(i);} } } import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.util.*; public class Main { private static final long MAXA = 1000000; void solve() { long n = scanner.nextLong(); if(n == 0){ out.println(0); return; } long res = Long.MAX_VALUE; for(long a = 0; a <= MAXA; a++){ long b = findB(n, a); res = min(res, calc(a,b)); } out.println(res); } long findB(long n, long a){ long l = 1, r = 1000001; while(l + 1 < r){ long mid = (l+r)/2; long curr = calc(a, mid); if(curr >= n){ r = mid; } else{ l = mid; } } if(calc(a, l) >= n){ return l; } return r; } long calc(long a, long b){ long res = 0; long[] tmp = {a*a*a, b*b*b, a*a*b, a*b*b}; for(long val : tmp){ res += val; } return res; } private static final boolean memory = false; private static final boolean singleTest = true; // ----- runner templates ----- // void run() { int numOfTests = singleTest? 1: scanner.nextInt(); for(int testIdx = 1; testIdx <= numOfTests; testIdx++){ solve(); } out.flush(); out.close(); } // ----- runner templates ----- // public static void main(String[] args) { if(memory) { new Thread(null, () -> new Main().run(), "go", 1 << 26).start(); } else{ new Main().run(); } } //------ input and output ------// public static FastScanner scanner = new FastScanner(System.in); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public FastScanner(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isWhitespace(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = read(); } while (!isWhitespace(c)); return negative ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { int c = read(); while (isWhitespace(c)) { c = read(); } return (char) c; } public String next() { int c = read(); while (isWhitespace(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isWhitespace(c)); return res.toString(); } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } int[] nextIntArray(int n, int base){ int[] arr = new int[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextInt();} return arr; } long[] nextLongArray(int n, int base){ long[] arr = new long[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextLong();} return arr; } int[][] nextIntGrid(int n, int m, int base){ int[][] grid = new int[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextInt();}} return grid; } char[][] nextCharGrid(int n, int m, int base){ char[][] grid = new char[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextChar();}} return grid; } double[][] nextDoubleGrid(int n, int m, int base){ double[][] grid = new double[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextDouble();}} return grid; } int[][] nextUnweightedGraph(int n, int m, int base){ int[][] g = new int[base + n][]; int[][] edges = new int[m][2]; int[] adjSize = new int[n+base]; for(int i = 0; i < m; i++){ int a = scanner.nextInt(), b = scanner.nextInt(); edges[i][0]=a; adjSize[a]++; edges[i][1]=b; adjSize[b]++; } for(int i = base; i < base + n; i++){ g[i]=new int[adjSize[i]]; adjSize[i]=0; } for(int[] e: edges){ int a = e[0], b = e[1]; g[a][adjSize[a]++]=b; g[b][adjSize[b]++]=a; } return g; } //------ debug and print functions ------// void debug(Object...os){out.println(deepToString(os));} void print(int[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(long[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(char[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(Object... o){for(int i = 0; i < o.length; i++){out.print(o[i]);out.print(i==o.length-1?'\n':' ');}} <T> void printArrayList(List<T> arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr.get(i));out.print(i==end? '\n':' ');}} //------ sort primitive type arrays ------// static void sort(int[] arr){ List<Integer> temp = new ArrayList<>(); for(int val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(long[] arr){ List<Long> temp = new ArrayList<>(); for(long val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(char[] arr) { List<Character> temp = new ArrayList<>(); for (char val : arr) {temp.add(val);} Collections.sort(temp); for (int i = 0; i < arr.length; i++) {arr[i] = temp.get(i);} } }
ConDefects/ConDefects/Code/abc246_d/Java/40574413
condefects-java_data_1378
import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] hairetu = new long[n]; int cnt = 0; for(int i = 0; i < n; i++){ hairetu[i] = sc.nextLong(); } Arrays.sort(hairetu); long sum = 0; for(long a : hairetu){ sum += a; } sum *= (n-1); //左端と右端のポインタを初期化 int right = n-1; for(int left = 0; left < n; left++){ right = Math.max(right,left); while(right > left && hairetu[left] + hairetu[right] >= 100000000){ right--; } cnt += (n-1)-right; } sum -= cnt*100000000; System.out.println(sum); } } import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] hairetu = new long[n]; long cnt = 0; for(int i = 0; i < n; i++){ hairetu[i] = sc.nextLong(); } Arrays.sort(hairetu); long sum = 0; for(long a : hairetu){ sum += a; } sum *= (n-1); //左端と右端のポインタを初期化 int right = n-1; for(int left = 0; left < n; left++){ right = Math.max(right,left); while(right > left && hairetu[left] + hairetu[right] >= 100000000){ right--; } cnt += (n-1)-right; } sum -= cnt*100000000; System.out.println(sum); } }
ConDefects/ConDefects/Code/abc353_c/Java/54053665
condefects-java_data_1379
import java.util.*; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); int[] An = new int[N]; for(int i = 0; i < N; i++){ An[i] = sc.nextInt(); } Arrays.sort(An); long sum = 0; for(int a: An){ sum += a; } sum *= (N-1); int cnt = 0; int right_index = N-1; for(int i = 0; i < N-1; i++){ right_index = Math.max(right_index, i); while(right_index > i && An[i] + An[right_index] >= 100000000){ right_index--; } cnt += (N-1) -right_index; } sum -= (cnt * 100000000); System.out.println(sum); } } import java.util.*; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); int[] An = new int[N]; for(int i = 0; i < N; i++){ An[i] = sc.nextInt(); } Arrays.sort(An); long sum = 0; for(int a: An){ sum += a; } sum *= (N-1); long cnt = 0; int right_index = N-1; for(int i = 0; i < N-1; i++){ right_index = Math.max(right_index, i); while(right_index > i && An[i] + An[right_index] >= 100000000){ right_index--; } cnt += (N-1) -right_index; } sum -= (cnt * 100000000); System.out.println(sum); } }
ConDefects/ConDefects/Code/abc353_c/Java/53987244
condefects-java_data_1380
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] nums = new int[n]; long sum = 0; for (int i = 0;i<n;i++) { nums[i] = scanner.nextInt(); sum += nums[i]; } Arrays.sort(nums); sum = sum * (n-1); // System.out.println("sum:" + sum); for (int i = 0;i<n-1;i++) { int idx = binarySearch(nums, 100000000 - nums[i], i+1); // System.out.println("cur num: " + nums[i] + " idx: " + idx); sum -= (n - idx) * 100000000; } System.out.println(sum); } private static int binarySearch(int[] nums, int target, int i) { int left = i; int right = nums.length-1; while (left < right) { int mid = (left + right) / 2; if (nums[mid] < target) { left = mid + 1; } else if (nums[mid] >= target && (mid == i || nums[mid-1] < target)) { return mid; } else { right = mid; } } return nums[left] >= target ? left : left + 1; } } import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] nums = new int[n]; long sum = 0; for (int i = 0;i<n;i++) { nums[i] = scanner.nextInt(); sum += nums[i]; } Arrays.sort(nums); sum = sum * (n-1); // System.out.println("sum:" + sum); for (int i = 0;i<n-1;i++) { int idx = binarySearch(nums, 100000000 - nums[i], i+1); // System.out.println("cur num: " + nums[i] + " idx: " + idx); sum -= (long) (n - idx) * 100000000; } System.out.println(sum); } private static int binarySearch(int[] nums, int target, int i) { int left = i; int right = nums.length-1; while (left < right) { int mid = (left + right) / 2; if (nums[mid] < target) { left = mid + 1; } else if (nums[mid] >= target && (mid == i || nums[mid-1] < target)) { return mid; } else { right = mid; } } return nums[left] >= target ? left : left + 1; } }
ConDefects/ConDefects/Code/abc353_c/Java/54745794
condefects-java_data_1381
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.close(); String s = ""; for (int i = 0; i < N; i++) { s += "" + i; } System.out.println(s); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.close(); String s = ""; for (int i = 0; i < N; i++) { s += "" + N; } System.out.println(s); } }
ConDefects/ConDefects/Code/abc333_a/Java/54274897
condefects-java_data_1382
// https://atcoder.jp/contests/abc241/tasks/abc241_b import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan =new Scanner(System.in); String s = scan.nextLine(); String n = scan.nextLine(); String m = scan.nextLine(); int count = 0; String[] N =n.split(" "); String[] M =m.split(" "); System.out.println(Arrays.toString(N)); System.out.println(Arrays.toString(M)); a: for(int i = 0; i < M.length; i++) { for(int j = 0; j < N.length; j++) { if(M[i].equals(N[j])) { N[j] = ""; count++; continue a; } } } if(M.length==count) { System.out.println("Yes"); }else if(M.length!=count) { System.out.println("No"); } } } // https://atcoder.jp/contests/abc241/tasks/abc241_b import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan =new Scanner(System.in); String s = scan.nextLine(); String n = scan.nextLine(); String m = scan.nextLine(); int count = 0; String[] N =n.split(" "); String[] M =m.split(" "); a: for(int i = 0; i < M.length; i++) { for(int j = 0; j < N.length; j++) { if(M[i].equals(N[j])) { N[j] = ""; count++; continue a; } } } if(M.length==count) { System.out.println("Yes"); }else if(M.length!=count) { System.out.println("No"); } } }
ConDefects/ConDefects/Code/abc241_b/Java/35833338
condefects-java_data_1383
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String row1 = scan.nextLine(); String row2 = scan.nextLine(); String row3 = scan.nextLine(); String n = row1.split(" ")[0]; String m = row1.split(" ")[1]; String[] arr1 = row2.split(" "); String[] arr2 = row3.split(" "); int count = 0; ok : for( int i = 0 ; i < arr2.length ; i++) { for( int j = 0 ; j<arr1.length ; j++) { if( arr2[i].equals(arr1[j])) { count++; arr1[j] = ""; break ok; } } } if(count==Integer.parseInt(m)) { System.out.println("Yes"); }else { System.out.println("No"); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String row1 = scan.nextLine(); String row2 = scan.nextLine(); String row3 = scan.nextLine(); String n = row1.split(" ")[0]; String m = row1.split(" ")[1]; String[] arr1 = row2.split(" "); String[] arr2 = row3.split(" "); int count = 0; ok : for( int i = 0 ; i < arr2.length ; i++) { for( int j = 0 ; j<arr1.length ; j++) { if( arr2[i].equals(arr1[j])) { count++; arr1[j] = ""; continue ok; } } } if(count==Integer.parseInt(m)) { System.out.println("Yes"); }else { System.out.println("No"); } } }
ConDefects/ConDefects/Code/abc241_b/Java/35830288
condefects-java_data_1384
import java.io.*; import java.util.*; class MinValSegTree { static final long INIT = 0; static final int OP_ADD = 1; static final int OP_SET = 2; static class Node { Node left; Node right; int ls, rs;//debug用 long minVal; long minFreq; int lazyType; long lazyVal; } int maxN; Node root; public MinValSegTree(int maxN) { this.maxN = maxN; this.root = createNode(0, maxN); root.ls = 0; root.rs = maxN; } Node createNode(int ls, int rs) { Node r = new Node(); r.minVal = INIT; r.minFreq = rs-ls+1; return r; } void apply(Node node, int ls, int rs, int type, long val) { node.lazyType = type; node.lazyVal += val; if (type == OP_ADD) { node.minVal += val; } else if (type==OP_SET) { throw new RuntimeException(); } } void reduce(Node node, Node left, Node right, int ls, int rs) { long m = Math.min(left.minVal,right.minVal); node.minVal = Math.min(left.minVal,right.minVal); node.minFreq = (left.minVal==m? left.minFreq : 0) + (right.minVal==m? right.minFreq : 0); } public void add(int l, int r, long val) { update(root, l, r, 0, maxN, OP_ADD, val); } public void set(int l, int r, long val) { update(root, l, r, 0, maxN, OP_SET, val); } void build(int[] vals) { build(root, vals, 0, maxN); } private void build(Node node, int[] vals, int ls, int rs) { if (ls==rs) { apply(node, ls, rs, OP_SET, ls>=vals.length ? INIT : vals[ls]); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; build(node.left,vals, ls, mid); build(node.right,vals,mid + 1, rs); reduce(node, node.left, node.right, ls, rs); } /** * 当前Node的范围: [ls,rs] */ private void update(Node node, int l, int r, int ls, int rs, int type, long val) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { apply(node, ls, rs, type, val); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; //左子树[ls,mid] //右子树[mid+1,rs] if (l <= mid) { update(node.left, l, r, ls, mid, type, val); } if (r >= mid + 1) { update(node.right, l, r, mid + 1, rs, type, val); } reduce(node, node.left, node.right, ls, rs); } //下发lazy值 void pushDown(Node node, int ls, int rs) { int mid = ls + rs >> 1; if (node.left == null) { node.left = createNode(ls, mid); node.left.ls = ls; node.left.rs = mid; } if (node.right == null) { node.right = createNode(mid + 1, rs); node.right.ls = mid + 1; node.right.rs = rs; } if (node.lazyType != 0) { apply(node.left, ls, mid, node.lazyType, node.lazyVal); apply(node.right, mid + 1, rs, node.lazyType, node.lazyVal); node.lazyType = 0; node.lazyVal = 0; } } public Node queryMinVal(int l, int r) { queryNode.minVal = Long.MAX_VALUE; queryNode.minFreq = 0; query(root, l, r, 0, maxN); return queryNode; } private final Node queryNode = new Node(); private void query(Node node, int l, int r, int ls, int rs) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { // reduce是从left和right重新计算,原root的值不保留 reduce(queryNode, node, queryNode, ls, rs); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; if (l <= mid) { query(node.left, l, r, ls, mid); } if (r >= mid + 1) { query(node.right, l, r, mid + 1, rs); } } } public class Main implements Runnable { void solve() { int n=nextInt(); int[] a=new int[n]; for (int i = 0; i < n; i++) { a[i]=nextInt(); --a[i]; } long res=0; List<List<Integer>> occ = new ArrayList<>(n); for (int i = 0; i < n; i++) { List<Integer> innerList = new ArrayList<>(); innerList.add(-1); // 将-1加入到每个内部列表中 occ.add(innerList); } MinValSegTree tree = new MinValSegTree(n); for (int i = 0; i < n; i++) { List<Integer> l1 = occ.get(a[i]); int p1=l1.get(l1.size()-1); if (l1.size()>=2){ int p2=l1.get(l1.size()-2); tree.add(p2+1,p1,-1); } l1.add(i); tree.add(p1+1,i,1); MinValSegTree.Node node = tree.queryMinVal(0, i); res+=i+1-(node.minVal==0?node.minFreq:0); } System.out.println(res); } public static void main(String[] args) throws Exception { new Thread(null, new Main(), "CustomThread", 1024 * 1024 * 100).start(); } @Override public void run() { new Main().solve(); out.flush(); } static PrintWriter out = new PrintWriter(System.out, false); static InputReader in = new InputReader(System.in); static String next() { return in.next(); } static int nextInt() { return Integer.parseInt(in.next()); } static long nextLong() { return Long.parseLong(in.next()); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } } import java.io.*; import java.util.*; class MinValSegTree { static final long INIT = 0; static final int OP_ADD = 1; static final int OP_SET = 2; static class Node { Node left; Node right; int ls, rs;//debug用 long minVal; long minFreq; int lazyType; long lazyVal; } int maxN; Node root; public MinValSegTree(int maxN) { this.maxN = maxN; this.root = createNode(0, maxN); root.ls = 0; root.rs = maxN; } Node createNode(int ls, int rs) { Node r = new Node(); r.minVal = INIT; r.minFreq = rs-ls+1; return r; } void apply(Node node, int ls, int rs, int type, long val) { node.lazyType = type; node.lazyVal += val; if (type == OP_ADD) { node.minVal += val; } else if (type==OP_SET) { throw new RuntimeException(); } } void reduce(Node node, Node left, Node right, int ls, int rs) { long m = Math.min(left.minVal,right.minVal); node.minFreq = (left.minVal==m? left.minFreq : 0) + (right.minVal==m? right.minFreq : 0); node.minVal = m; } public void add(int l, int r, long val) { update(root, l, r, 0, maxN, OP_ADD, val); } public void set(int l, int r, long val) { update(root, l, r, 0, maxN, OP_SET, val); } void build(int[] vals) { build(root, vals, 0, maxN); } private void build(Node node, int[] vals, int ls, int rs) { if (ls==rs) { apply(node, ls, rs, OP_SET, ls>=vals.length ? INIT : vals[ls]); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; build(node.left,vals, ls, mid); build(node.right,vals,mid + 1, rs); reduce(node, node.left, node.right, ls, rs); } /** * 当前Node的范围: [ls,rs] */ private void update(Node node, int l, int r, int ls, int rs, int type, long val) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { apply(node, ls, rs, type, val); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; //左子树[ls,mid] //右子树[mid+1,rs] if (l <= mid) { update(node.left, l, r, ls, mid, type, val); } if (r >= mid + 1) { update(node.right, l, r, mid + 1, rs, type, val); } reduce(node, node.left, node.right, ls, rs); } //下发lazy值 void pushDown(Node node, int ls, int rs) { int mid = ls + rs >> 1; if (node.left == null) { node.left = createNode(ls, mid); node.left.ls = ls; node.left.rs = mid; } if (node.right == null) { node.right = createNode(mid + 1, rs); node.right.ls = mid + 1; node.right.rs = rs; } if (node.lazyType != 0) { apply(node.left, ls, mid, node.lazyType, node.lazyVal); apply(node.right, mid + 1, rs, node.lazyType, node.lazyVal); node.lazyType = 0; node.lazyVal = 0; } } public Node queryMinVal(int l, int r) { queryNode.minVal = Long.MAX_VALUE; queryNode.minFreq = 0; query(root, l, r, 0, maxN); return queryNode; } private final Node queryNode = new Node(); private void query(Node node, int l, int r, int ls, int rs) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { // reduce是从left和right重新计算,原root的值不保留 reduce(queryNode, node, queryNode, ls, rs); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; if (l <= mid) { query(node.left, l, r, ls, mid); } if (r >= mid + 1) { query(node.right, l, r, mid + 1, rs); } } } public class Main implements Runnable { void solve() { int n=nextInt(); int[] a=new int[n]; for (int i = 0; i < n; i++) { a[i]=nextInt(); --a[i]; } long res=0; List<List<Integer>> occ = new ArrayList<>(n); for (int i = 0; i < n; i++) { List<Integer> innerList = new ArrayList<>(); innerList.add(-1); // 将-1加入到每个内部列表中 occ.add(innerList); } MinValSegTree tree = new MinValSegTree(n); for (int i = 0; i < n; i++) { List<Integer> l1 = occ.get(a[i]); int p1=l1.get(l1.size()-1); if (l1.size()>=2){ int p2=l1.get(l1.size()-2); tree.add(p2+1,p1,-1); } l1.add(i); tree.add(p1+1,i,1); MinValSegTree.Node node = tree.queryMinVal(0, i); res+=i+1-(node.minVal==0?node.minFreq:0); } System.out.println(res); } public static void main(String[] args) throws Exception { new Thread(null, new Main(), "CustomThread", 1024 * 1024 * 100).start(); } @Override public void run() { new Main().solve(); out.flush(); } static PrintWriter out = new PrintWriter(System.out, false); static InputReader in = new InputReader(System.in); static String next() { return in.next(); } static int nextInt() { return Integer.parseInt(in.next()); } static long nextLong() { return Long.parseLong(in.next()); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } }
ConDefects/ConDefects/Code/abc346_g/Java/51712394
condefects-java_data_1385
import java.io.*; import java.util.*; class MinValSegTree { static final long INIT = 0; static final int OP_ADD = 1; static final int OP_SET = 2; static class Node { Node left; Node right; int ls, rs;//debug用 long minVal; long minFreq; int lazyType; long lazyVal; } int maxN; Node root; public MinValSegTree(int maxN) { this.maxN = maxN; this.root = createNode(0, maxN); root.ls = 0; root.rs = maxN; } Node createNode(int ls, int rs) { Node r = new Node(); r.minVal = INIT; r.minFreq = rs-ls+1; return r; } void apply(Node node, int ls, int rs, int type, long val) { node.lazyType = type; node.lazyVal += val; if (type == OP_ADD) { node.minVal += val; } else if (type==OP_SET) { throw new RuntimeException(); } } void reduce(Node node, Node left, Node right, int ls, int rs) { node.minVal = Math.min(left.minVal,right.minVal); node.minFreq = (left.minVal==node.minVal? left.minFreq : 0) + (right.minVal==node.minVal? right.minFreq : 0); } public void add(int l, int r, long val) { update(root, l, r, 0, maxN, OP_ADD, val); } public void set(int l, int r, long val) { update(root, l, r, 0, maxN, OP_SET, val); } void build(int[] vals) { build(root, vals, 0, maxN); } private void build(Node node, int[] vals, int ls, int rs) { if (ls==rs) { apply(node, ls, rs, OP_SET, ls>=vals.length ? INIT : vals[ls]); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; build(node.left,vals, ls, mid); build(node.right,vals,mid + 1, rs); reduce(node, node.left, node.right, ls, rs); } /** * 当前Node的范围: [ls,rs] */ private void update(Node node, int l, int r, int ls, int rs, int type, long val) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { apply(node, ls, rs, type, val); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; //左子树[ls,mid] //右子树[mid+1,rs] if (l <= mid) { update(node.left, l, r, ls, mid, type, val); } if (r >= mid + 1) { update(node.right, l, r, mid + 1, rs, type, val); } reduce(node, node.left, node.right, ls, rs); } //下发lazy值 void pushDown(Node node, int ls, int rs) { int mid = ls + rs >> 1; if (node.left == null) { node.left = createNode(ls, mid); node.left.ls = ls; node.left.rs = mid; } if (node.right == null) { node.right = createNode(mid + 1, rs); node.right.ls = mid + 1; node.right.rs = rs; } if (node.lazyType != 0) { apply(node.left, ls, mid, node.lazyType, node.lazyVal); apply(node.right, mid + 1, rs, node.lazyType, node.lazyVal); reduce(node, node.left, node.right, ls, rs); node.lazyType = 0; node.lazyVal = 0; } } public Node queryMin(int l, int r) { EMPTY.minVal = Long.MAX_VALUE; EMPTY.minFreq = 0; return query(root, l, r, 0, maxN); } private final Node EMPTY = new Node(); private Node query(Node node, int l, int r, int ls, int rs) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { return node; } pushDown(node, ls, rs); int mid = ls + rs >> 1; Node ret = createNode(ls, rs), left = EMPTY, right = EMPTY; if (l <= mid) { left = query(node.left, l, r, ls, mid); } if (r >= mid + 1) { right = query(node.right, l, r, mid + 1, rs); } reduce(ret, left, right, ls, rs); return ret; } } public class Main implements Runnable { void solve() { int n=nextInt(); int[] a=new int[n]; for (int i = 0; i < n; i++) { a[i]=nextInt(); //--a[i]; } long res=0; List<List<Integer>> occ = new ArrayList<>(n); for (int i = 0; i < n; i++) { List<Integer> innerList = new ArrayList<>(); innerList.add(-1); // 将-1加入到每个内部列表中 occ.add(innerList); } MinValSegTree tree = new MinValSegTree(n); for (int i = 0; i < n; i++) { List<Integer> l1 = occ.get(a[i]); int p1=l1.get(l1.size()-1); if (l1.size()>=2){ int p2=l1.get(l1.size()-2); tree.add(p2+1,p1,-1); } l1.add(i); tree.add(p1+1,i,1); MinValSegTree.Node node = tree.queryMin(0, i); res+=i+1-(node.minVal==0?node.minFreq:0); } System.out.println(res); } public static void main(String[] args) throws Exception { new Thread(null, new Main(), "CustomThread", 1024 * 1024 * 100).start(); } @Override public void run() { new Main().solve(); out.flush(); } static PrintWriter out = new PrintWriter(System.out, false); static InputReader in = new InputReader(System.in); static String next() { return in.next(); } static int nextInt() { return Integer.parseInt(in.next()); } static long nextLong() { return Long.parseLong(in.next()); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } } import java.io.*; import java.util.*; class MinValSegTree { static final long INIT = 0; static final int OP_ADD = 1; static final int OP_SET = 2; static class Node { Node left; Node right; int ls, rs;//debug用 long minVal; long minFreq; int lazyType; long lazyVal; } int maxN; Node root; public MinValSegTree(int maxN) { this.maxN = maxN; this.root = createNode(0, maxN); root.ls = 0; root.rs = maxN; } Node createNode(int ls, int rs) { Node r = new Node(); r.minVal = INIT; r.minFreq = rs-ls+1; return r; } void apply(Node node, int ls, int rs, int type, long val) { node.lazyType = type; node.lazyVal += val; if (type == OP_ADD) { node.minVal += val; } else if (type==OP_SET) { throw new RuntimeException(); } } void reduce(Node node, Node left, Node right, int ls, int rs) { node.minVal = Math.min(left.minVal,right.minVal); node.minFreq = (left.minVal==node.minVal? left.minFreq : 0) + (right.minVal==node.minVal? right.minFreq : 0); } public void add(int l, int r, long val) { update(root, l, r, 0, maxN, OP_ADD, val); } public void set(int l, int r, long val) { update(root, l, r, 0, maxN, OP_SET, val); } void build(int[] vals) { build(root, vals, 0, maxN); } private void build(Node node, int[] vals, int ls, int rs) { if (ls==rs) { apply(node, ls, rs, OP_SET, ls>=vals.length ? INIT : vals[ls]); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; build(node.left,vals, ls, mid); build(node.right,vals,mid + 1, rs); reduce(node, node.left, node.right, ls, rs); } /** * 当前Node的范围: [ls,rs] */ private void update(Node node, int l, int r, int ls, int rs, int type, long val) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { apply(node, ls, rs, type, val); return; } pushDown(node, ls, rs); int mid = ls + rs >> 1; //左子树[ls,mid] //右子树[mid+1,rs] if (l <= mid) { update(node.left, l, r, ls, mid, type, val); } if (r >= mid + 1) { update(node.right, l, r, mid + 1, rs, type, val); } reduce(node, node.left, node.right, ls, rs); } //下发lazy值 void pushDown(Node node, int ls, int rs) { int mid = ls + rs >> 1; if (node.left == null) { node.left = createNode(ls, mid); node.left.ls = ls; node.left.rs = mid; } if (node.right == null) { node.right = createNode(mid + 1, rs); node.right.ls = mid + 1; node.right.rs = rs; } if (node.lazyType != 0) { apply(node.left, ls, mid, node.lazyType, node.lazyVal); apply(node.right, mid + 1, rs, node.lazyType, node.lazyVal); reduce(node, node.left, node.right, ls, rs); node.lazyType = 0; node.lazyVal = 0; } } public Node queryMin(int l, int r) { EMPTY.minVal = Long.MAX_VALUE; EMPTY.minFreq = 0; return query(root, l, r, 0, maxN); } private final Node EMPTY = new Node(); private Node query(Node node, int l, int r, int ls, int rs) { if (l < 0 || r > maxN) { throw new IllegalArgumentException(); } if (l <= ls && rs <= r) { return node; } pushDown(node, ls, rs); int mid = ls + rs >> 1; Node ret = createNode(ls, rs), left = EMPTY, right = EMPTY; if (l <= mid) { left = query(node.left, l, r, ls, mid); } if (r >= mid + 1) { right = query(node.right, l, r, mid + 1, rs); } reduce(ret, left, right, ls, rs); return ret; } } public class Main implements Runnable { void solve() { int n=nextInt(); int[] a=new int[n]; for (int i = 0; i < n; i++) { a[i]=nextInt(); --a[i]; } long res=0; List<List<Integer>> occ = new ArrayList<>(n); for (int i = 0; i < n; i++) { List<Integer> innerList = new ArrayList<>(); innerList.add(-1); // 将-1加入到每个内部列表中 occ.add(innerList); } MinValSegTree tree = new MinValSegTree(n); for (int i = 0; i < n; i++) { List<Integer> l1 = occ.get(a[i]); int p1=l1.get(l1.size()-1); if (l1.size()>=2){ int p2=l1.get(l1.size()-2); tree.add(p2+1,p1,-1); } l1.add(i); tree.add(p1+1,i,1); MinValSegTree.Node node = tree.queryMin(0, i); res+=i+1-(node.minVal==0?node.minFreq:0); } System.out.println(res); } public static void main(String[] args) throws Exception { new Thread(null, new Main(), "CustomThread", 1024 * 1024 * 100).start(); } @Override public void run() { new Main().solve(); out.flush(); } static PrintWriter out = new PrintWriter(System.out, false); static InputReader in = new InputReader(System.in); static String next() { return in.next(); } static int nextInt() { return Integer.parseInt(in.next()); } static long nextLong() { return Long.parseLong(in.next()); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } }
ConDefects/ConDefects/Code/abc346_g/Java/51712254
condefects-java_data_1386
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) { FastScanner str=new FastScanner(System.in); PrintWriter out=new PrintWriter(System.out); String s=str.next(); Stack<Character>stack=new Stack<>(); Set<Character>set=new HashSet<>(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='('){ stack.push('('); }else if(s.charAt(i)==')'){ while(!stack.isEmpty()){ if(stack.peek()!='('){ stack.pop(); }else { stack.pop(); break; } } }else{ if(set.contains(s.charAt(i))){ out.print("No"); out.flush(); return; }else{ stack.push(s.charAt(i)); set.add(s.charAt(i)); } } } out.print("Yes"); out.flush(); } } 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.*; public class Main{ public static void main(String[] args) { FastScanner str=new FastScanner(System.in); PrintWriter out=new PrintWriter(System.out); String s=str.next(); Stack<Character>stack=new Stack<>(); Set<Character>set=new HashSet<>(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='('){ stack.push('('); }else if(s.charAt(i)==')'){ while(!stack.isEmpty()){ if(stack.peek()!='('){ set.remove(stack.pop()); }else { stack.pop(); break; } } }else{ if(set.contains(s.charAt(i))){ out.print("No"); out.flush(); return; }else{ stack.push(s.charAt(i)); set.add(s.charAt(i)); } } } out.print("Yes"); out.flush(); } } 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/abc283_d/Java/38936478
condefects-java_data_1387
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastInput in = new FastInput(); StringBuffer res = new StringBuffer(); var hw=in.getAutoIntArray(); var n=in.getAutoIntArray()[0]; HashMap<Integer,TreeSet<Integer>> row=new HashMap<>(); HashMap<Integer,TreeSet<Integer>> col=new HashMap<>(); for (int i = 0; i < n; i++) { var mid=in.getAutoIntArray(); if(row.containsKey(mid[0])) row.get(mid[0]).add(mid[1]); else { TreeSet<Integer> set=new TreeSet<>(); set.add(mid[1]); row.put(mid[0], set); } if(col.containsKey(mid[1])) col.get(mid[1]).add(mid[0]); else { TreeSet<Integer> set=new TreeSet<>(); set.add(mid[0]); col.put(mid[1], set); } } var q=in.getAutoIntArray()[0]; int x=hw[2]; int y=hw[3]; TreeSet<Integer> D=new TreeSet<>(); for (int i = 0; i < q; i++) { var mid=in.in.readLine(); var s=Integer.valueOf(mid.split(" ")[1]); switch (mid.charAt(0)) { case 'L': var v1=row.getOrDefault(x,D).lower(y); if(v1==null) {y=Math.max(1, y-s);} else{y=Math.max(v1+1, y-s);} break; case 'R': var v2=row.getOrDefault(x,D).higher(y); if(v2==null) {y=Math.min(hw[1], y+s);} else{y=Math.min(v2-1, y+s);} break; case 'U': var v3=col.getOrDefault(y,D).lower(x); if(v3==null) {x=Math.max(1, x-s);} else{x=Math.max(v3+1, x-s);} break; case 'D': var v4=col.getOrDefault(y,D).higher(x); if(v4==null) {x=Math.min(hw[0], x+s);} else{x=Math.min(v4+1, x+s);} break; default: break; } res.append(x+" "+y); res.append("\n"); } System.out.println(res.toString().substring(0,res.length()-1)); } } class FastInput { BufferedReader in = null; public FastInput() { in = new BufferedReader(new InputStreamReader(System.in)); } public FastInput(String path) { try { in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int[][] zip(int[] x, int[] y) { int[][] res = new int[x.length][2]; for (int i = 0; i < res.length; i++) { res[i][0] = x[i]; res[i][1] = y[i]; } return res; } public int[] getAutoIntArray() throws IOException { String[] data = in.readLine().split(" "); int[] res = new int[data.length]; for (int i = 0; i < res.length; i++) { res[i] = Integer.valueOf(data[i]); } return res; } public long[] getAutoLongArray() throws IOException { String[] data = in.readLine().split(" "); long[] res = new long[data.length]; for (int i = 0; i < res.length; i++) { res[i] = Long.valueOf(data[i]); } return res; } public double[] getAutoDoubleArray() throws IOException { String[] data = in.readLine().split(" "); double[] res = new double[data.length]; for (int i = 0; i < res.length; i++) { res[i] = Double.valueOf(data[i]); } return res; } public int[] getIntArray(int len) throws IOException { int[] res = new int[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Integer.valueOf(data[i]); } return res; } public ArrayList<Integer> getIntArrayList(int len) throws IOException { ArrayList<Integer> res = new ArrayList<>(); String[] data = in.readLine().split(" "); for (int i = 0; i < len; i++) { res.add(Integer.valueOf(data[i])); } return res; } public Integer[] getIntegerArray(int len) throws IOException { Integer[] res = new Integer[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Integer.valueOf(data[i]); } return res; } public long[] getLongArray(int len) throws IOException { long[] res = new long[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Long.valueOf(data[i]); } return res; } public ArrayList<Long> getLongArrayList(int len) throws IOException { ArrayList<Long> res = new ArrayList<>(); String[] data = in.readLine().split(" "); for (int i = 0; i < len; i++) { res.add(Long.valueOf(data[i])); } return res; } public double[] getDoubleArray(int len) throws IOException { double[] res = new double[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Double.valueOf(data[i]); } return res; } public int[] getStringToIntArray(int len) throws IOException { int[] res = new int[len]; String s = in.readLine(); for (int i = 0; i < res.length; i++) { if (s.charAt(i) == '0') { res[i] = 0; } else { res[i] = 1; } } return res; } public static void printArray(int[] x) { System.out.println(getArrayString(x)); } public static void printArray(long[] x) { System.out.println(getArrayString(x)); } public static void printArray(double[] x) { System.out.println(getArrayString(x)); } public static String getArrayString(double[] x) { if (x == null) { return "null"; } StringBuffer res = new StringBuffer(); for (int i = 0; i < x.length; i++) { res.append(x[i]); if (i + 1 != x.length) res.append(" "); } return res.toString(); } public static String getArrayString(long[] x) { if (x == null) { return "null"; } StringBuffer res = new StringBuffer(); for (int i = 0; i < x.length; i++) { res.append(x[i]); if (i + 1 != x.length) res.append(" "); } return res.toString(); } public static String getArrayString(int[] x) { if (x == null) { return "null"; } StringBuffer res = new StringBuffer(); for (int i = 0; i < x.length; i++) { res.append(x[i]); if (i + 1 != x.length) res.append(" "); } return res.toString(); } } import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastInput in = new FastInput(); StringBuffer res = new StringBuffer(); var hw=in.getAutoIntArray(); var n=in.getAutoIntArray()[0]; HashMap<Integer,TreeSet<Integer>> row=new HashMap<>(); HashMap<Integer,TreeSet<Integer>> col=new HashMap<>(); for (int i = 0; i < n; i++) { var mid=in.getAutoIntArray(); if(row.containsKey(mid[0])) row.get(mid[0]).add(mid[1]); else { TreeSet<Integer> set=new TreeSet<>(); set.add(mid[1]); row.put(mid[0], set); } if(col.containsKey(mid[1])) col.get(mid[1]).add(mid[0]); else { TreeSet<Integer> set=new TreeSet<>(); set.add(mid[0]); col.put(mid[1], set); } } var q=in.getAutoIntArray()[0]; int x=hw[2]; int y=hw[3]; TreeSet<Integer> D=new TreeSet<>(); for (int i = 0; i < q; i++) { var mid=in.in.readLine(); var s=Integer.valueOf(mid.split(" ")[1]); switch (mid.charAt(0)) { case 'L': var v1=row.getOrDefault(x,D).lower(y); if(v1==null) {y=Math.max(1, y-s);} else{y=Math.max(v1+1, y-s);} break; case 'R': var v2=row.getOrDefault(x,D).higher(y); if(v2==null) {y=Math.min(hw[1], y+s);} else{y=Math.min(v2-1, y+s);} break; case 'U': var v3=col.getOrDefault(y,D).lower(x); if(v3==null) {x=Math.max(1, x-s);} else{x=Math.max(v3+1, x-s);} break; case 'D': var v4=col.getOrDefault(y,D).higher(x); if(v4==null) {x=Math.min(hw[0], x+s);} else{x=Math.min(v4-1, x+s);} break; default: break; } res.append(x+" "+y); res.append("\n"); } System.out.println(res.toString().substring(0,res.length()-1)); } } class FastInput { BufferedReader in = null; public FastInput() { in = new BufferedReader(new InputStreamReader(System.in)); } public FastInput(String path) { try { in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(path)))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int[][] zip(int[] x, int[] y) { int[][] res = new int[x.length][2]; for (int i = 0; i < res.length; i++) { res[i][0] = x[i]; res[i][1] = y[i]; } return res; } public int[] getAutoIntArray() throws IOException { String[] data = in.readLine().split(" "); int[] res = new int[data.length]; for (int i = 0; i < res.length; i++) { res[i] = Integer.valueOf(data[i]); } return res; } public long[] getAutoLongArray() throws IOException { String[] data = in.readLine().split(" "); long[] res = new long[data.length]; for (int i = 0; i < res.length; i++) { res[i] = Long.valueOf(data[i]); } return res; } public double[] getAutoDoubleArray() throws IOException { String[] data = in.readLine().split(" "); double[] res = new double[data.length]; for (int i = 0; i < res.length; i++) { res[i] = Double.valueOf(data[i]); } return res; } public int[] getIntArray(int len) throws IOException { int[] res = new int[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Integer.valueOf(data[i]); } return res; } public ArrayList<Integer> getIntArrayList(int len) throws IOException { ArrayList<Integer> res = new ArrayList<>(); String[] data = in.readLine().split(" "); for (int i = 0; i < len; i++) { res.add(Integer.valueOf(data[i])); } return res; } public Integer[] getIntegerArray(int len) throws IOException { Integer[] res = new Integer[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Integer.valueOf(data[i]); } return res; } public long[] getLongArray(int len) throws IOException { long[] res = new long[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Long.valueOf(data[i]); } return res; } public ArrayList<Long> getLongArrayList(int len) throws IOException { ArrayList<Long> res = new ArrayList<>(); String[] data = in.readLine().split(" "); for (int i = 0; i < len; i++) { res.add(Long.valueOf(data[i])); } return res; } public double[] getDoubleArray(int len) throws IOException { double[] res = new double[len]; String[] data = in.readLine().split(" "); for (int i = 0; i < res.length; i++) { res[i] = Double.valueOf(data[i]); } return res; } public int[] getStringToIntArray(int len) throws IOException { int[] res = new int[len]; String s = in.readLine(); for (int i = 0; i < res.length; i++) { if (s.charAt(i) == '0') { res[i] = 0; } else { res[i] = 1; } } return res; } public static void printArray(int[] x) { System.out.println(getArrayString(x)); } public static void printArray(long[] x) { System.out.println(getArrayString(x)); } public static void printArray(double[] x) { System.out.println(getArrayString(x)); } public static String getArrayString(double[] x) { if (x == null) { return "null"; } StringBuffer res = new StringBuffer(); for (int i = 0; i < x.length; i++) { res.append(x[i]); if (i + 1 != x.length) res.append(" "); } return res.toString(); } public static String getArrayString(long[] x) { if (x == null) { return "null"; } StringBuffer res = new StringBuffer(); for (int i = 0; i < x.length; i++) { res.append(x[i]); if (i + 1 != x.length) res.append(" "); } return res.toString(); } public static String getArrayString(int[] x) { if (x == null) { return "null"; } StringBuffer res = new StringBuffer(); for (int i = 0; i < x.length; i++) { res.append(x[i]); if (i + 1 != x.length) res.append(" "); } return res.toString(); } }
ConDefects/ConDefects/Code/abc273_d/Java/37951054
condefects-java_data_1388
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); int rs = sc.nextInt(); int cs = sc.nextInt(); int n = sc.nextInt(); Map<Integer, TreeSet<Integer>> rowWall = new HashMap<>(); Map<Integer, TreeSet<Integer>> colWall = new HashMap<>(); for (int i = 0; i < n; i++) { int r = sc.nextInt(); int c = sc.nextInt(); if (!rowWall.containsKey(r)) { rowWall.put(r, new TreeSet<>()); rowWall.get(r).add(0); rowWall.get(r).add(w+1); } if (!colWall.containsKey(c)) { colWall.put(c, new TreeSet<>()); colWall.get(c).add(0); colWall.get(c).add(h+1); } rowWall.get(r).add(c); colWall.get(c).add(r); } StringBuilder sb = new StringBuilder(); int q = sc.nextInt(); for (int i = 0; i < q; i++) { String d = sc.next(); int l = sc.nextInt(); if (d.equals("L")) { int wall = 0; if (rowWall.containsKey(rs)) wall = rowWall.get(rs).lower(cs); cs = Math.max(wall+1, cs-l); } else if (d.equals("R")) { int wall = w+1; if (rowWall.containsKey(rs)) wall = rowWall.get(rs).higher(cs); cs = Math.min(wall-1, cs+l); } else if (d.equals("U")) { int wall = 0; if (colWall.containsKey(cs)) wall = colWall.get(cs).lower(rs); rs = Math.max(wall+1, rs-l); } else if (d.equals("D")) { int wall = h+1; if (colWall.containsKey(cs)) wall = colWall.get(cs).higher(rs); rs = Math.min(wall-1, rs+l); } sb.append(rs + " " + cs); } System.out.println(sb); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); int rs = sc.nextInt(); int cs = sc.nextInt(); int n = sc.nextInt(); Map<Integer, TreeSet<Integer>> rowWall = new HashMap<>(); Map<Integer, TreeSet<Integer>> colWall = new HashMap<>(); for (int i = 0; i < n; i++) { int r = sc.nextInt(); int c = sc.nextInt(); if (!rowWall.containsKey(r)) { rowWall.put(r, new TreeSet<>()); rowWall.get(r).add(0); rowWall.get(r).add(w+1); } if (!colWall.containsKey(c)) { colWall.put(c, new TreeSet<>()); colWall.get(c).add(0); colWall.get(c).add(h+1); } rowWall.get(r).add(c); colWall.get(c).add(r); } StringBuilder sb = new StringBuilder(); int q = sc.nextInt(); for (int i = 0; i < q; i++) { String d = sc.next(); int l = sc.nextInt(); if (d.equals("L")) { int wall = 0; if (rowWall.containsKey(rs)) wall = rowWall.get(rs).lower(cs); cs = Math.max(wall+1, cs-l); } else if (d.equals("R")) { int wall = w+1; if (rowWall.containsKey(rs)) wall = rowWall.get(rs).higher(cs); cs = Math.min(wall-1, cs+l); } else if (d.equals("U")) { int wall = 0; if (colWall.containsKey(cs)) wall = colWall.get(cs).lower(rs); rs = Math.max(wall+1, rs-l); } else if (d.equals("D")) { int wall = h+1; if (colWall.containsKey(cs)) wall = colWall.get(cs).higher(rs); rs = Math.min(wall-1, rs+l); } sb.append(rs + " " + cs + "\n"); } System.out.println(sb); } }
ConDefects/ConDefects/Code/abc273_d/Java/38837193
condefects-java_data_1389
import java.util.Scanner; //import java.util.Integer; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String a = Integer.toHexString(N); if(a.length() == 1){ a= "0"+a; } System.out.println(a); } } import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int N = sc.nextInt(); String a = Integer.toHexString(N); a = a.toUpperCase(); if(a.length() == 1){ a= "0"+a; } System.out.println(a); } }
ConDefects/ConDefects/Code/abc271_a/Java/36647328
condefects-java_data_1390
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = n / 16; int b = n % 16; if(a >= 0){ System.out.print(a); } else{ henkan(a); } if(b < 10){ System.out.println(b); } else{ henkan(b); } } public static void henkan(int num){ if(num == 10){ System.out.println("A"); } else if(num == 11){ System.out.print("B"); } else if(num == 12){ System.out.print("C"); } else if(num == 13){ System.out.print("D"); } else if(num == 14){ System.out.print("E"); } else if(num == 15){ System.out.print("F"); } } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = n / 16; int b = n % 16; if(a >= 0 && a < 10){ System.out.print(a); } else{ henkan(a); } if(b < 10){ System.out.println(b); } else{ henkan(b); } } public static void henkan(int num){ if(num == 10){ System.out.println("A"); } else if(num == 11){ System.out.print("B"); } else if(num == 12){ System.out.print("C"); } else if(num == 13){ System.out.print("D"); } else if(num == 14){ System.out.print("E"); } else if(num == 15){ System.out.print("F"); } } }
ConDefects/ConDefects/Code/abc271_a/Java/43086733
condefects-java_data_1391
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String output = Integer.toHexString(n); if(output.length()==1)output="0"+output; System.out.println(output); sc.close(); } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String output = Integer.toHexString(n).toUpperCase(); if(output.length()==1)output="0"+output; System.out.println(output); sc.close(); } }
ConDefects/ConDefects/Code/abc271_a/Java/39597239
condefects-java_data_1392
import java.util.*; public class Main { static boolean next_permutation(String[] p) { for(int a = p.length - 2; a >= 0; --a) { if(p[a].compareTo(p[a+1]) < 0) { for(int b = p.length - 1;; --b) { if(p[b].compareTo(p[a]) > 0) { String t = p[a]; p[a] = p[b]; p[b] = t; for(++a, b=p.length-1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } } } } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = Integer.toHexString(n); String zero = "O"; if(s.length() == 1) { s = zero.concat(s); } System.out.println(s.toUpperCase()); } } import java.util.*; public class Main { static boolean next_permutation(String[] p) { for(int a = p.length - 2; a >= 0; --a) { if(p[a].compareTo(p[a+1]) < 0) { for(int b = p.length - 1;; --b) { if(p[b].compareTo(p[a]) > 0) { String t = p[a]; p[a] = p[b]; p[b] = t; for(++a, b=p.length-1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } } } } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = Integer.toHexString(n); String zero = "0"; if(s.length() == 1) { s = zero.concat(s); } System.out.println(s.toUpperCase()); } }
ConDefects/ConDefects/Code/abc271_a/Java/39764798
condefects-java_data_1393
import java.util.Scanner; public class Main{ public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println(Integer.toHexString(n)); } } import java.util.Scanner; public class Main{ public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.printf("%02X",n); } }
ConDefects/ConDefects/Code/abc271_a/Java/41092189
condefects-java_data_1394
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int a = scan.nextInt();//xx int b = scan.nextInt();//xy int c = scan.nextInt();//yx int d = scan.nextInt();//yyの数 //n文字にn-1個のabcdいずれかのパターンがある。 String xyString = new String(); if(!(a == 0 || b == 0) && a + b >= 2 && c + d == 0) { System.out.println("No"); }else if(b - c >= 2) { System.out.println("No"); }else if(c - b >= 2) { System.out.println("No"); }else System.out.println("Yes"); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int a = scan.nextInt();//xx int b = scan.nextInt();//xy int c = scan.nextInt();//yx int d = scan.nextInt();//yyの数 //n文字にn-1個のabcdいずれかのパターンがある。 String xyString = new String(); if(!(a == 0 || d == 0) && a + d >= 2 && b + c == 0) { System.out.println("No"); }else if(b - c >= 2) { System.out.println("No"); }else if(c - b >= 2) { System.out.println("No"); }else System.out.println("Yes"); } }
ConDefects/ConDefects/Code/arc157_a/Java/39191902
condefects-java_data_1395
import java.util.*; import java.io.*; import java.math.*; public class Main{ static FastScanner sc ; static PrintWriter pw ; static final int inf = Integer.MAX_VALUE / 2 ; static final long linf = Long.MAX_VALUE / 2L ; static final String yes = "Yes" ; static final String no = "No" ; static final int mod1 = 1000000007 ; static final int mod = 998244353 ; public static void solve() { int N = ni() ; int A = ni() ; int B = ni() ; int C = ni() ; int D = ni() ; if ( Math.abs( B - C ) > 2 ) { pr( no ) ; } else if ( B != 0 || C != 0 ) { pr( yes ) ; } else{ pr( A == 0 || D == 0 ? yes : no ) ; } } public static void main(String[] args) { sc = new FastScanner() ; pw = new PrintWriter( System.out ) ; solve() ; pw.flush() ; pw.close() ; } static 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; }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;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); 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(); } } static class UnionFind{ int[] par ,siz ; int con ; public UnionFind( int n ) { init( n ) ; } public void init( int n ) { this.par = new int[n] ; this.siz = new int[n] ; Arrays.setAll( par , i -> i ) ; Arrays.fill( siz , 1 ) ; con = n ; } public int root( int x ) { if ( x == par[x] ) return x ; return par[x] = root( par[x] ) ; } public boolean same( int x , int y ) { return root( x ) == root( y ) ; } public int size( int n ) { return siz[root(n)] ; } public int cnt() { return con ; } public void unite( int x , int y ) { if ( same( x , y ) ) return ; x = root( x ) ; y = root( y ) ; if ( siz[x] > siz[y] ) { par[y] = x ; siz[x] += siz[y] ; } else { par[x] = y ; siz[y] += siz[x] ; } con-- ; } } static class MultiSet<T extends Comparable<T>> { TreeMap<T,Long> map ; long c ; MultiSet() { this.map = new TreeMap<T,Long>() ; c = 0L ; } void add( T x ) { add( x , 1L ) ; } void add( T x , long c ) { map.put( x , map.getOrDefault( x , 0L ) + c ) ; this.c += c ; } void removeAll( T x ) { if ( !map.containsKey( x ) ) return ; remove( x , map.get( x ) ) ; } void remove( T x , long c ) { if ( !map.containsKey( x ) ) return ; this.c -= Main.min( map.get( x ) , c ) ; if ( map.get( x ) <= c ) { map.remove( x ) ; return ; } map.put( x , map.get( x ) - c ) ; } void remove( T x ) { remove( x , 1 ) ; } T max() { return map.lastKey() ; } T min() { return map.firstKey() ; } T higher( T key ) { return map.higherKey( key ) ; } T lower( T key ) { return map.lowerKey( key ) ; } T ceiling( T key ) { return map.ceilingKey( key ) ; } T floor( T key ) { return map.floorKey( key ) ; } boolean contains( T key ) { return map.containsKey( key ) ; } long count( T key ) { return map.getOrDefault( key , 0L ) ; } int size() { return map.size() ; } long amount() { return c ; } Set<T> keySet() { return map.keySet() ; } void merge( MultiSet<T> S ) { for ( T key : S.keySet() ) add( key , S.count( key ) ) ; this.c += S.amount() ; } } static class ModInt{ final int mod ; long[] fac , finv , inv ; int size = 200001 ; ModInt( int mod ) { this.mod = mod ; } int mod( long a ) { a %= mod ; return a < 0 ? (int)a + mod : (int)a ; } int add( int a , int b ) { return this.mod( (long)a + (long)b ) ; } int add( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = this.add( res , x[i] ) ; return res ; } int add( long a , long b ) { return this.mod( this.mod( a ) + this.mod( b ) ) ; } int add( long... x ) { int res = this.mod( x[0] ) ; for ( int i = 1 ; i < x.length ; i++ ) res = this.add( res , this.mod( x[i] ) ) ; return res ; } int time( int a , int b ) { return this.mod( (long)a * (long)b ) ; } int time( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = this.time( res , x[i] ) ; return res ; } int time( long a , long b ) { return this.time( this.mod( a ) , this.mod( b ) ) ; } int time( long... x ) { int res = this.mod( x[0] ) ; for ( int i = 1 ; i < x.length ; i++ ) res = this.time( res , this.mod( x[i] ) ) ; return res ; } int fact( int a ) { int res = 1 ; for ( int i = 2 ; i <= a ; i++ ) res = this.time( res , i ) ; return res ; } int inv( int a ) { return this.pow( a , mod - 2 ) ; } int pow( int a , long b ) { int res = 1 ; while ( b > 0L ) { if ( ( b & 1L ) != 0L ) res = this.time( res , a ) ; b /= 2L ; a = this.time( a , a ) ; } return res ; } int pow( long a , long b ) { return this.pow( this.mod( a ) , b ) ; } int nPk( int n , int k ) { if ( n < k || n < 0 || k < 0 ) return 0 ; int res = 1 ; for ( int i = 1 ; i <= k ; i++ ) { res = this.time( res , n + 1 - i ) ; } return res ; } int nCk( int n , int k ) { if ( n < k || n < 0 || k < 0 ) return 0 ; int res = 1 ; for ( int i = 1 ; i <= k ; i++ ) res = this.time( res , this.time( n + 1 - i , this.inv( i ) ) ) ; return res ; } int nCk2( int n , int k ) { if ( n < k || n < 0 || k < 0 ) return 0 ; comInit( n ) ; return this.time( fac[n] , ( this.time( finv[k] , finv[n-k] ) ) ) ; } int nHr( int n , int r ) { return nCk2( n + r - 1 , r ) ; } void comInit() { comInit( this.size ) ; } void comInit( int n ) { if ( fac != null && this.size > n ) return ; this.size = (int)Math.max( n , this.size ) ; fac = new long[size] ; finv = new long[size] ; inv = new long[size] ; fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1 ; for ( int i = 2 ; i < size ; i++ ) { fac[i] = this.time( fac[i-1] , i ) ; inv[i] = mod - this.time( inv[mod%i] , mod / i ) ; finv[i] = this.time( finv[i-1] , inv[i] ) ; } } } public static int ni() { return Integer.parseInt( sc.next() ) ;} public static int[] nia( int N ) { int[] res = new int[N]; Arrays.setAll( res , i -> ni() ) ; return res ; } public static int[][] nia( int N , int M ) { int[][] res = new int[N][M]; for ( int i = 0 ; i < N ; i++ ) Arrays.setAll( res[i] , j -> ni() ) ; return res ; } public static long nl() { return Long.parseLong( sc.next() ) ;} public static long[] nla( int N ) { long[] res = new long[N]; Arrays.setAll( res , i -> nl() ) ; return res ; } public static long[][] nla( int N , int M ) { long[][] res = new long[N][M]; for ( int i = 0 ; i < N ; i++ ) Arrays.setAll( res[i] , j -> nl() ) ; return res ; } public static String ns() { return sc.next() ;} public static String[] nsa( int N ) { String[] res = new String[N]; Arrays.setAll( res , i -> ns() ) ; return res ; } public static void pr( int... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( o[i] + " " ) ; pw.println( o[o.length-1] ) ; } public static void pr( long... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( o[i] + " " ) ; pw.println( o[o.length-1] ) ; } public static void pr( double... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( new BigDecimal( o[i] ).toPlainString() + " " ) ; pw.println( new BigDecimal( o[o.length-1] ).toPlainString() ) ; } public static void pr( String... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( o[i] + " " ) ; pw.println( o[o.length-1] ) ; } public static void pr( int o ) { pw.println( o ) ; } public static void pr( long o ) { pw.println( o ) ; } public static void pr( double o ) { pw.println( new BigDecimal( o ).toPlainString() ) ; } public static void pr( String o ) { pw.println( o ) ; } public static void pr( Object o ) { pw.println( o ) ; } public static void debug( int... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( o[i] + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( long... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( o[i] + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( double... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( new BigDecimal( o[i] ).toPlainString() + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( String... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( o[i] + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( int o ) { System.err.println( o ) ; } public static void debug( long o ) { System.err.println( o ) ; } public static void debug( double o ) { System.err.println( new BigDecimal( o ).toPlainString() ) ; } public static void debug( String o ) { System.err.println( o ) ; } public static void debug( Object o ) { System.err.println( o ) ; } public static int max( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.max( res , x[i] ) ; return res ; } public static int min( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.min( res , x[i] ) ; return res ; } public static long max( long... x ) { long res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.max( res , x[i] ) ; return res ; } public static long min( long... x ) { long res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.min( res , x[i] ) ; return res ; } public static long pow( int a , int b ) { long res = 1L ; for ( int i = 0 ; i < b ; i++ ) res *= (long)a ; return res ; } public static long pow( long a , long b ) { long res = 1L ; for ( long i = 0L ; i < b ; i++ ) res *= a ; return res ; } public static long[] cumsum( int[] a ) { long[] res = new long[a.length+1] ; for ( int i = 0 ; i < a.length ; i++ ) res[i+1] += res[i] + a[i] ; return res ; } public static long[] cumsum( long[] a ) { long[] res = new long[a.length+1] ; for ( int i = 0 ; i < a.length ; i++ ) res[i+1] += res[i] + a[i] ; return res ; } public static long gcd( long a, long b ) { if ( b > a ) return gcd( b, a ) ; if ( a % b != 0 ) return gcd( b, a % b ) ; return b ; } public static int gcd( int a, int b ) { return (int)gcd( (long)a, (long)b ) ; } public static int gcd( int... a ) { if ( a.length == 1 ) return a[0] ; int result = gcd( a[0], a[1] ) ; for ( int i = 2 ; i < a.length ; i++ ) result = gcd( result, a[i] ) ; return result ; } public static long gcd( long... a ) { if ( a.length == 1 ) return a[0] ; long result = gcd( a[0], a[1] ) ; for ( int i = 2 ; i < a.length ; i++ ) result = gcd( result, a[i] ) ; return result ; } public static long lcm( long a, long b ) { return ( a / gcd( a, b ) ) * b ; } public static long lcm( int a, int b ) { return ( (long)a / gcd( a, b ) ) * (long)b ; } public static String revStr( String s ) { return new StringBuilder( s ).reverse().toString() ; } public static String sortStr( String s ) { char[] c = s.toCharArray() ; Arrays.sort( c ) ; return String.valueOf( c ) ; } public static int[] prevPermutation( int[] A ) { if ( A == null ) return null ; int N = A.length ; for ( int i = 0 ; i < N ; i++ ) A[i] = A[i] * -1 ; A = nextPermutation( A ) ; if ( A != null ) for ( int i = 0 ; i < N ; i++ ) A[i] = A[i] * -1 ; return A ; } public static int[] nextPermutation( int[] A ) { if ( A == null ) return null ; for ( int i = A.length - 1 ; i > 0 ; i-- ) { if ( A[i-1] < A[i] ) { int j = find( A, A[i-1], i, A.length - 1 ) ; int tmp = A[j] ; A[j] = A[i-1] ; A[i-1] = tmp ; Arrays.sort( A, i, A.length ) ; return A ; } } return null ; } public static int find( int[] A, int dest, int f, int l ) { if ( f == l ) return f ; int m = ( f + l + 1 ) / 2 ; return ( A[m] <= dest ? find( A, dest, f, m - 1 ) : find( A, dest, m, l ) ) ; } } import java.util.*; import java.io.*; import java.math.*; public class Main{ static FastScanner sc ; static PrintWriter pw ; static final int inf = Integer.MAX_VALUE / 2 ; static final long linf = Long.MAX_VALUE / 2L ; static final String yes = "Yes" ; static final String no = "No" ; static final int mod1 = 1000000007 ; static final int mod = 998244353 ; public static void solve() { int N = ni() ; int A = ni() ; int B = ni() ; int C = ni() ; int D = ni() ; if ( Math.abs( B - C ) >= 2 ) { pr( no ) ; } else if ( B != 0 || C != 0 ) { pr( yes ) ; } else{ pr( A == 0 || D == 0 ? yes : no ) ; } } public static void main(String[] args) { sc = new FastScanner() ; pw = new PrintWriter( System.out ) ; solve() ; pw.flush() ; pw.close() ; } static 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; }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;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); 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(); } } static class UnionFind{ int[] par ,siz ; int con ; public UnionFind( int n ) { init( n ) ; } public void init( int n ) { this.par = new int[n] ; this.siz = new int[n] ; Arrays.setAll( par , i -> i ) ; Arrays.fill( siz , 1 ) ; con = n ; } public int root( int x ) { if ( x == par[x] ) return x ; return par[x] = root( par[x] ) ; } public boolean same( int x , int y ) { return root( x ) == root( y ) ; } public int size( int n ) { return siz[root(n)] ; } public int cnt() { return con ; } public void unite( int x , int y ) { if ( same( x , y ) ) return ; x = root( x ) ; y = root( y ) ; if ( siz[x] > siz[y] ) { par[y] = x ; siz[x] += siz[y] ; } else { par[x] = y ; siz[y] += siz[x] ; } con-- ; } } static class MultiSet<T extends Comparable<T>> { TreeMap<T,Long> map ; long c ; MultiSet() { this.map = new TreeMap<T,Long>() ; c = 0L ; } void add( T x ) { add( x , 1L ) ; } void add( T x , long c ) { map.put( x , map.getOrDefault( x , 0L ) + c ) ; this.c += c ; } void removeAll( T x ) { if ( !map.containsKey( x ) ) return ; remove( x , map.get( x ) ) ; } void remove( T x , long c ) { if ( !map.containsKey( x ) ) return ; this.c -= Main.min( map.get( x ) , c ) ; if ( map.get( x ) <= c ) { map.remove( x ) ; return ; } map.put( x , map.get( x ) - c ) ; } void remove( T x ) { remove( x , 1 ) ; } T max() { return map.lastKey() ; } T min() { return map.firstKey() ; } T higher( T key ) { return map.higherKey( key ) ; } T lower( T key ) { return map.lowerKey( key ) ; } T ceiling( T key ) { return map.ceilingKey( key ) ; } T floor( T key ) { return map.floorKey( key ) ; } boolean contains( T key ) { return map.containsKey( key ) ; } long count( T key ) { return map.getOrDefault( key , 0L ) ; } int size() { return map.size() ; } long amount() { return c ; } Set<T> keySet() { return map.keySet() ; } void merge( MultiSet<T> S ) { for ( T key : S.keySet() ) add( key , S.count( key ) ) ; this.c += S.amount() ; } } static class ModInt{ final int mod ; long[] fac , finv , inv ; int size = 200001 ; ModInt( int mod ) { this.mod = mod ; } int mod( long a ) { a %= mod ; return a < 0 ? (int)a + mod : (int)a ; } int add( int a , int b ) { return this.mod( (long)a + (long)b ) ; } int add( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = this.add( res , x[i] ) ; return res ; } int add( long a , long b ) { return this.mod( this.mod( a ) + this.mod( b ) ) ; } int add( long... x ) { int res = this.mod( x[0] ) ; for ( int i = 1 ; i < x.length ; i++ ) res = this.add( res , this.mod( x[i] ) ) ; return res ; } int time( int a , int b ) { return this.mod( (long)a * (long)b ) ; } int time( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = this.time( res , x[i] ) ; return res ; } int time( long a , long b ) { return this.time( this.mod( a ) , this.mod( b ) ) ; } int time( long... x ) { int res = this.mod( x[0] ) ; for ( int i = 1 ; i < x.length ; i++ ) res = this.time( res , this.mod( x[i] ) ) ; return res ; } int fact( int a ) { int res = 1 ; for ( int i = 2 ; i <= a ; i++ ) res = this.time( res , i ) ; return res ; } int inv( int a ) { return this.pow( a , mod - 2 ) ; } int pow( int a , long b ) { int res = 1 ; while ( b > 0L ) { if ( ( b & 1L ) != 0L ) res = this.time( res , a ) ; b /= 2L ; a = this.time( a , a ) ; } return res ; } int pow( long a , long b ) { return this.pow( this.mod( a ) , b ) ; } int nPk( int n , int k ) { if ( n < k || n < 0 || k < 0 ) return 0 ; int res = 1 ; for ( int i = 1 ; i <= k ; i++ ) { res = this.time( res , n + 1 - i ) ; } return res ; } int nCk( int n , int k ) { if ( n < k || n < 0 || k < 0 ) return 0 ; int res = 1 ; for ( int i = 1 ; i <= k ; i++ ) res = this.time( res , this.time( n + 1 - i , this.inv( i ) ) ) ; return res ; } int nCk2( int n , int k ) { if ( n < k || n < 0 || k < 0 ) return 0 ; comInit( n ) ; return this.time( fac[n] , ( this.time( finv[k] , finv[n-k] ) ) ) ; } int nHr( int n , int r ) { return nCk2( n + r - 1 , r ) ; } void comInit() { comInit( this.size ) ; } void comInit( int n ) { if ( fac != null && this.size > n ) return ; this.size = (int)Math.max( n , this.size ) ; fac = new long[size] ; finv = new long[size] ; inv = new long[size] ; fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1 ; for ( int i = 2 ; i < size ; i++ ) { fac[i] = this.time( fac[i-1] , i ) ; inv[i] = mod - this.time( inv[mod%i] , mod / i ) ; finv[i] = this.time( finv[i-1] , inv[i] ) ; } } } public static int ni() { return Integer.parseInt( sc.next() ) ;} public static int[] nia( int N ) { int[] res = new int[N]; Arrays.setAll( res , i -> ni() ) ; return res ; } public static int[][] nia( int N , int M ) { int[][] res = new int[N][M]; for ( int i = 0 ; i < N ; i++ ) Arrays.setAll( res[i] , j -> ni() ) ; return res ; } public static long nl() { return Long.parseLong( sc.next() ) ;} public static long[] nla( int N ) { long[] res = new long[N]; Arrays.setAll( res , i -> nl() ) ; return res ; } public static long[][] nla( int N , int M ) { long[][] res = new long[N][M]; for ( int i = 0 ; i < N ; i++ ) Arrays.setAll( res[i] , j -> nl() ) ; return res ; } public static String ns() { return sc.next() ;} public static String[] nsa( int N ) { String[] res = new String[N]; Arrays.setAll( res , i -> ns() ) ; return res ; } public static void pr( int... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( o[i] + " " ) ; pw.println( o[o.length-1] ) ; } public static void pr( long... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( o[i] + " " ) ; pw.println( o[o.length-1] ) ; } public static void pr( double... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( new BigDecimal( o[i] ).toPlainString() + " " ) ; pw.println( new BigDecimal( o[o.length-1] ).toPlainString() ) ; } public static void pr( String... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) pw.print( o[i] + " " ) ; pw.println( o[o.length-1] ) ; } public static void pr( int o ) { pw.println( o ) ; } public static void pr( long o ) { pw.println( o ) ; } public static void pr( double o ) { pw.println( new BigDecimal( o ).toPlainString() ) ; } public static void pr( String o ) { pw.println( o ) ; } public static void pr( Object o ) { pw.println( o ) ; } public static void debug( int... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( o[i] + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( long... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( o[i] + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( double... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( new BigDecimal( o[i] ).toPlainString() + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( String... o ) { for ( int i = 0 ; i < o.length - 1 ; i++ ) System.err.print( o[i] + " " ) ; System.err.println( o[o.length-1] ) ; } public static void debug( int o ) { System.err.println( o ) ; } public static void debug( long o ) { System.err.println( o ) ; } public static void debug( double o ) { System.err.println( new BigDecimal( o ).toPlainString() ) ; } public static void debug( String o ) { System.err.println( o ) ; } public static void debug( Object o ) { System.err.println( o ) ; } public static int max( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.max( res , x[i] ) ; return res ; } public static int min( int... x ) { int res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.min( res , x[i] ) ; return res ; } public static long max( long... x ) { long res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.max( res , x[i] ) ; return res ; } public static long min( long... x ) { long res = x[0] ; for ( int i = 1 ; i < x.length ; i++ ) res = Math.min( res , x[i] ) ; return res ; } public static long pow( int a , int b ) { long res = 1L ; for ( int i = 0 ; i < b ; i++ ) res *= (long)a ; return res ; } public static long pow( long a , long b ) { long res = 1L ; for ( long i = 0L ; i < b ; i++ ) res *= a ; return res ; } public static long[] cumsum( int[] a ) { long[] res = new long[a.length+1] ; for ( int i = 0 ; i < a.length ; i++ ) res[i+1] += res[i] + a[i] ; return res ; } public static long[] cumsum( long[] a ) { long[] res = new long[a.length+1] ; for ( int i = 0 ; i < a.length ; i++ ) res[i+1] += res[i] + a[i] ; return res ; } public static long gcd( long a, long b ) { if ( b > a ) return gcd( b, a ) ; if ( a % b != 0 ) return gcd( b, a % b ) ; return b ; } public static int gcd( int a, int b ) { return (int)gcd( (long)a, (long)b ) ; } public static int gcd( int... a ) { if ( a.length == 1 ) return a[0] ; int result = gcd( a[0], a[1] ) ; for ( int i = 2 ; i < a.length ; i++ ) result = gcd( result, a[i] ) ; return result ; } public static long gcd( long... a ) { if ( a.length == 1 ) return a[0] ; long result = gcd( a[0], a[1] ) ; for ( int i = 2 ; i < a.length ; i++ ) result = gcd( result, a[i] ) ; return result ; } public static long lcm( long a, long b ) { return ( a / gcd( a, b ) ) * b ; } public static long lcm( int a, int b ) { return ( (long)a / gcd( a, b ) ) * (long)b ; } public static String revStr( String s ) { return new StringBuilder( s ).reverse().toString() ; } public static String sortStr( String s ) { char[] c = s.toCharArray() ; Arrays.sort( c ) ; return String.valueOf( c ) ; } public static int[] prevPermutation( int[] A ) { if ( A == null ) return null ; int N = A.length ; for ( int i = 0 ; i < N ; i++ ) A[i] = A[i] * -1 ; A = nextPermutation( A ) ; if ( A != null ) for ( int i = 0 ; i < N ; i++ ) A[i] = A[i] * -1 ; return A ; } public static int[] nextPermutation( int[] A ) { if ( A == null ) return null ; for ( int i = A.length - 1 ; i > 0 ; i-- ) { if ( A[i-1] < A[i] ) { int j = find( A, A[i-1], i, A.length - 1 ) ; int tmp = A[j] ; A[j] = A[i-1] ; A[i-1] = tmp ; Arrays.sort( A, i, A.length ) ; return A ; } } return null ; } public static int find( int[] A, int dest, int f, int l ) { if ( f == l ) return f ; int m = ( f + l + 1 ) / 2 ; return ( A[m] <= dest ? find( A, dest, f, m - 1 ) : find( A, dest, m, l ) ) ; } }
ConDefects/ConDefects/Code/arc157_a/Java/39352195
condefects-java_data_1396
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); if(Math.abs(b - c) >= 2 || ((b == 0) && (c == 0) && (a != 0) && (b != 0))) { System.out.println("No"); } else { System.out.println("Yes"); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); if(Math.abs(b - c) >= 2 || ((b == 0) && (c == 0) && (a != 0) && (d != 0))) { System.out.println("No"); } else { System.out.println("Yes"); } } }
ConDefects/ConDefects/Code/arc157_a/Java/39307025
condefects-java_data_1397
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void printArray(int[]a) { for(int i=0;i<a.length-1;i++) { System.out.print(a[i]+" "); } System.out.println(a[a.length-1]); } public static long lmax(long a,long b) { if(a<b)return b; else return a; } public static long lmin(long a,long b) { if(a>b)return b; else return a; } public static int max(int a,int b) { if(a<b)return b; else return a; } public static int min(int a,int b) { if(a>b)return b; else return a; } public static int[] setArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=scan.nextInt(); return a; } public static int abs(int n) { if(n<0)n*=-1; return n; } public static long labs(long n) { if(n<0)n*=-1; return n; } static Scanner scan=new Scanner(System.in); public static void main(String[] args) { int n=scan.nextInt(); int a=scan.nextInt(); int b=scan.nextInt(); int c=scan.nextInt(); int d=scan.nextInt(); if(b==0&&c==0){ if(a==n-1||b==n-1) { System.out.println("Yes"); } else { System.out.println("No"); } } else if(abs(b-c)<=1) { System.out.println("Yes"); } else { System.out.println("No"); } } } import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main { public static void printArray(int[]a) { for(int i=0;i<a.length-1;i++) { System.out.print(a[i]+" "); } System.out.println(a[a.length-1]); } public static long lmax(long a,long b) { if(a<b)return b; else return a; } public static long lmin(long a,long b) { if(a>b)return b; else return a; } public static int max(int a,int b) { if(a<b)return b; else return a; } public static int min(int a,int b) { if(a>b)return b; else return a; } public static int[] setArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=scan.nextInt(); return a; } public static int abs(int n) { if(n<0)n*=-1; return n; } public static long labs(long n) { if(n<0)n*=-1; return n; } static Scanner scan=new Scanner(System.in); public static void main(String[] args) { int n=scan.nextInt(); int a=scan.nextInt(); int b=scan.nextInt(); int c=scan.nextInt(); int d=scan.nextInt(); if(b==0&&c==0){ if(a==n-1||d==n-1) { System.out.println("Yes"); } else { System.out.println("No"); } } else if(abs(b-c)<=1) { System.out.println("Yes"); } else { System.out.println("No"); } } }
ConDefects/ConDefects/Code/arc157_a/Java/40798729
condefects-java_data_1398
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.Stack; public class Main { static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static Scanner sc = new Scanner(System.in); private static int Int() { try { st.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (int) st.nval; } private static long Long() { try { st.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (long) st.nval; } private static String str() { try { st.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (String) st.sval; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static int[][] tu; static int ans,n,m,chen,jia; static int[]dp,color,bj,arr,zt; static Map<Integer, List<Integer>>map,ziyinziweiz; //static boolean p; static int v1; static List<Long>list; static long []c,p1; public static void main(String[] args) { int t =1; long mod=(long) (998244353); while (t-->0) { int n=Int(); int a=Int(); int b=Int(); int c=Int(); int d=Int(); int m=a+d+Math.max(c, b)*2; if ((b==0&&c==0)) { m+=a==0?0:1; m+=d==0?0:1; } if (m<=n&&n!=1) { out.println("Yes"); }else { out.println("No"); } } out.close(); } } import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.Stack; public class Main { static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); private static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); static Scanner sc = new Scanner(System.in); private static int Int() { try { st.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (int) st.nval; } private static long Long() { try { st.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (long) st.nval; } private static String str() { try { st.nextToken(); } catch (IOException e) { e.printStackTrace(); } return (String) st.sval; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static int[][] tu; static int ans,n,m,chen,jia; static int[]dp,color,bj,arr,zt; static Map<Integer, List<Integer>>map,ziyinziweiz; //static boolean p; static int v1; static List<Long>list; static long []c,p1; public static void main(String[] args) { int t =1; long mod=(long) (998244353); while (t-->0) { int n=Int(); int a=Int(); int b=Int(); int c=Int(); int d=Int(); int m=a+d+Math.max(c, b)*2; if ((b==0&&c==0)) { m+=a==0?0:1; m+=d==0?0:1; } if (m<=n) { out.println("Yes"); }else { out.println("No"); } } out.close(); } }
ConDefects/ConDefects/Code/arc157_a/Java/39190692
condefects-java_data_1399
import java.util.*; import java.io.*; import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(reader); Main ins = new Main(in); ins.calc(); ins.showResult(); } static class Tuple { int n; // n番目 int firstBitPos; // 最上位bitの位置 long patternNum; Tuple(int n, int firstBitPos, long patternNum) { this.n = n; this.firstBitPos = firstBitPos; this.patternNum = patternNum; } public String toString() { StringBuilder strBuilder = new StringBuilder(); strBuilder.append(this.n + " "); strBuilder.append(this.firstBitPos + " "); strBuilder.append(this.patternNum); return strBuilder.toString(); } } PrintWriter writer = new PrintWriter(System.out); BufferedReader in = null; long N, M; final long MOD = 998244353L; long[] basePatterns; int maxBitPos; Main(BufferedReader in) throws IOException { String[] tokens = in.readLine().split(" "); this.N = Long.parseLong(tokens[0]); this.M = Long.parseLong(tokens[1]); this.basePatterns = new long[62]; this.basePatterns[0] = 0L; this.basePatterns[1] = 1L; for (int i = 2; i < basePatterns.length; ++i) { basePatterns[i] = basePatterns[i - 1] * 2L; } this.maxBitPos = Long.toString(this.M, 2).length(); for (int i = 1; i < basePatterns.length; ++i) { if (i < this.maxBitPos) { this.basePatterns[i] %= MOD; // そのまま } else if (i == this.maxBitPos) { long p = this.M ^ basePatterns[i]; p++; p %= MOD; this.basePatterns[i] = p; } else { this.basePatterns[i] = 0L; } } } void calc() { if (N > 70L) { this.writer.println(0L); return; } long[] patterns = Arrays.copyOf(basePatterns, this.basePatterns.length); for (int i = 1; i < this.N; ++i) { long[] nexts = new long[patterns.length]; Arrays.fill(nexts, 0L); long sum = 0L; for (int j = i; j + 1 < patterns.length; ++j) { sum += patterns[j]; nexts[j + 1] = sum * basePatterns[j + 1] % MOD; } patterns = nexts; } long result = 0L; for (int i = 0; i < patterns.length; ++i) { result += patterns[i]; result %= MOD; } this.writer.println(result); } void showResult() { writer.flush(); } } import java.util.*; import java.io.*; import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(reader); Main ins = new Main(in); ins.calc(); ins.showResult(); } static class Tuple { int n; // n番目 int firstBitPos; // 最上位bitの位置 long patternNum; Tuple(int n, int firstBitPos, long patternNum) { this.n = n; this.firstBitPos = firstBitPos; this.patternNum = patternNum; } public String toString() { StringBuilder strBuilder = new StringBuilder(); strBuilder.append(this.n + " "); strBuilder.append(this.firstBitPos + " "); strBuilder.append(this.patternNum); return strBuilder.toString(); } } PrintWriter writer = new PrintWriter(System.out); BufferedReader in = null; long N, M; final long MOD = 998244353L; long[] basePatterns; int maxBitPos; Main(BufferedReader in) throws IOException { String[] tokens = in.readLine().split(" "); this.N = Long.parseLong(tokens[0]); this.M = Long.parseLong(tokens[1]); this.basePatterns = new long[62]; this.basePatterns[0] = 0L; this.basePatterns[1] = 1L; for (int i = 2; i < basePatterns.length; ++i) { basePatterns[i] = basePatterns[i - 1] * 2L; } this.maxBitPos = Long.toString(this.M, 2).length(); for (int i = 1; i < basePatterns.length; ++i) { if (i < this.maxBitPos) { this.basePatterns[i] %= MOD; } else if (i == this.maxBitPos) { long p = this.M ^ basePatterns[i]; p++; p %= MOD; this.basePatterns[i] = p; } else { this.basePatterns[i] = 0L; } } } void calc() { if (N > 70L) { this.writer.println(0L); return; } long[] patterns = Arrays.copyOf(basePatterns, this.basePatterns.length); for (int i = 1; i < this.N; ++i) { long[] nexts = new long[patterns.length]; Arrays.fill(nexts, 0L); long sum = 0L; for (int j = i; j + 1 < patterns.length; ++j) { sum += patterns[j]; sum %= MOD; nexts[j + 1] = sum * basePatterns[j + 1] % MOD; } patterns = nexts; } long result = 0L; for (int i = 0; i < patterns.length; ++i) { result += patterns[i]; result %= MOD; } this.writer.println(result); } void showResult() { writer.flush(); } }
ConDefects/ConDefects/Code/arc141_b/Java/32090967
condefects-java_data_1400
import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = 998244353; public static void main(String[] args) throws Exception { solve(); pw.flush(); } public static void solve() { long n = sc.nextLong(); long M = sc.nextLong(); int cnt = 0; long tmp = M; while(tmp > 0){ cnt++; tmp >>= 1; } if(cnt < n){ pw.println(0); }else{ int N = (int)n; long[][] dp = new long[N+1][cnt+2]; long now = 1; dp[1][0] = 0; for(int i = 1; i <= cnt; i++){ long next = now << 1L; dp[1][i] = next-now; now = next; dp[1][i] %= mod; } for(int i = 1; i < N; i++){ for(int j = 0; j <= cnt; j++){ dp[i+1][j+1] += (dp[i][j]*dp[1][j+1]) % mod; dp[i+1][j+1] %= mod; } for(int j = 0; j <= cnt; j++){ dp[i+1][j+1] += dp[i+1][j]*2L; dp[i+1][j+1] %= mod; } } long ans = 0; for(int j = 0; j < cnt; j++){ ans += dp[N][j]; ans %= mod; } long m1 = (1L << cnt-1); long m2 = (1L << cnt); long pow = (M-m1+1)*modinv(m2-m1,mod)%mod; ans += (dp[N][cnt]*pow)%mod; pw.println(ans % mod); } } static long modinv(long a, long mod) { long x = 1, u = 0, s = a, t = mod; long k = 0, tmp = 0; while (t > 0) { k = s / t; s -= k * t; { tmp = s; s = t; t = tmp; } x -= k * u; { tmp = x; x = u; u = tmp; } } x %= mod; if (x < 0) x += mod; return x; } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * 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(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; } } import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = 998244353; public static void main(String[] args) throws Exception { solve(); pw.flush(); } public static void solve() { long n = sc.nextLong(); long M = sc.nextLong(); int cnt = 0; long tmp = M; while(tmp > 0){ cnt++; tmp >>= 1; } if(cnt < n){ pw.println(0); }else{ int N = (int)n; long[][] dp = new long[N+1][cnt+2]; long now = 1; dp[1][0] = 0; for(int i = 1; i <= cnt; i++){ long next = now << 1L; dp[1][i] = next-now; now = next; dp[1][i] %= mod; } for(int i = 1; i < N; i++){ for(int j = 0; j <= cnt; j++){ dp[i+1][j+1] += (dp[i][j]*dp[1][j+1]) % mod; dp[i+1][j+1] %= mod; } for(int j = 0; j <= cnt; j++){ dp[i+1][j+1] += dp[i+1][j]*2L; dp[i+1][j+1] %= mod; } } long ans = 0; for(int j = 0; j < cnt; j++){ ans += dp[N][j]; ans %= mod; } long m1 = (1L << cnt-1); long m2 = (1L << cnt); long pow = (M-m1+1)%mod*modinv(m2-m1,mod)%mod; ans += (dp[N][cnt]*pow)%mod; pw.println(ans % mod); } } static long modinv(long a, long mod) { long x = 1, u = 0, s = a, t = mod; long k = 0, tmp = 0; while (t > 0) { k = s / t; s -= k * t; { tmp = s; s = t; t = tmp; } x -= k * u; { tmp = x; x = u; u = tmp; } } x %= mod; if (x < 0) x += mod; return x; } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * 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(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; } }
ConDefects/ConDefects/Code/arc141_b/Java/32092999