Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.util.stream.Collectors.summingInt;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.StringTokenizer;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- public class Template {
- static List<Integer>[] gr;
- static long[] arr;
- static long n, m, p, q, k, ans;
- static boolean b;
- static String s, big;
- public static Map<Long, Integer> factorize(long n) {
- List<Long> factors = new ArrayList<>();
- for (long d = 2; d * d <= n; d++) {
- while (n % d == 0) {
- factors.add(d);
- n /= d;
- }
- }
- if (n > 1) {
- factors.add(n);
- }
- return factors.stream().collect(Collectors.groupingBy(Function.identity(), summingInt(v -> 1)));
- }
- static long ANS = 0;
- static Set<Long> pairs = new HashSet<>();
- static void rec(long a, long b, int idx, List<Long> set) {
- if (idx == set.size()) {
- if (a >= l && b >= l && b <= r && a <= r) {
- if (a == b) {
- if (pairs.add(a)) {
- ANS++;
- }
- } else
- ANS++;
- }
- return;
- }
- rec(a * set.get(idx), b, idx + 1, set);
- rec(a, b * set.get(idx), idx + 1, set);
- }
- static int l, r;
- public static void main(String[] args) {
- l = in.nextInt();
- r = in.nextInt();
- int x = in.nextInt();
- int y = in.nextInt();
- int xx = y / x;
- if (y % x != 0) {
- System.out.println(0);
- return;
- }
- Map<Long, Integer> map = factorize(xx);
- List<Long> list = new ArrayList<>();
- for (java.util.Map.Entry<Long, Integer> e : map.entrySet()) {
- long pow = 1;
- for (int i = 0; i < e.getValue(); i++) {
- pow *= e.getKey();
- }
- list.add(pow);
- }
- rec(1, 1, 0, list);
- System.out.println(ANS);
- out.close();
- }
- static MyScanner in = new MyScanner();
- static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
- static boolean valid(int[][] arr, int x, int y) {
- return x >= 0 && y >= 0 && x < arr.length && y < arr[0].length;
- }
- static int[] d1 = { -1, -1, -1, 1, 1, 1, 0, 0 };
- static int[] d2 = { 1, 0, -1, 0, -1, 1, -1, 1 };
- static int DIRECTED = 1, UNDIRECTED = 0;
- static void dfsTree(int root, int parent) {
- for (int child : gr[root]) {
- if (child == parent) {
- continue;
- }
- dfsTree(child, root);
- }
- }
- static List<Integer>[] readGraph(int dir, int n) {
- List<Integer>[] gr;
- gr = new ArrayList[(int) (n + 1)];
- for (int i = 1; i <= n; i++) {
- gr[i] = new ArrayList();
- }
- for (int i = 1; i < n; i++) {
- int u = in.nextInt(), v = in.nextInt();
- gr[v].add(u);
- if (dir == UNDIRECTED) {
- gr[u].add(v);
- }
- }
- return gr;
- }
- public static long gcd(long a, long b) {
- while (b != 0) {
- long t = b;
- b = a % b;
- a = t;
- }
- return Math.abs(a);
- }
- public static long lcm(long a, long b) {
- return Math.abs(a / gcd(a, b) * b);
- }
- // gcd(a, b) = x, lcm(a,b) = y
- /*
- *
- * a*b/x = y a*b = x*y
- *
- *
- */
- static class Pair implements Comparable<Pair> {
- long a, b;
- public Pair(long a, long b) {
- this.a = a;
- this.b = b;
- }
- @Override
- public int compareTo(Pair p) {
- return a == p.a ? 0 : (a > p.a ? 1 : -1);
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + (int) (a ^ (a >>> 32));
- result = prime * result + (int) (b ^ (b >>> 32));
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Pair other = (Pair) obj;
- if (a != other.a)
- return false;
- if (b != other.b)
- return false;
- return true;
- }
- }
- public static class MyScanner {
- BufferedReader br;
- StringTokenizer st;
- public MyScanner() {
- br = new BufferedReader(new InputStreamReader(System.in));
- }
- String next() {
- while (st == null || !st.hasMoreElements()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return st.nextToken();
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- String nextLine() {
- String str = "";
- try {
- str = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return str;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment