Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.HashMap;
- import java.util.Scanner;
- public class Main {
- static HashMap<Long, Long> length = new HashMap<>();
- public static long divide(long num) {
- if (num == 1)
- return 0;
- if (length.containsKey(num)) {
- return length.get(num);
- }
- if (num % 2 == 0) {
- long temp = divide(num / 2);
- if (temp != -1) {
- length.put(num, 1 + temp);
- return 1 + temp;
- }
- }
- if (num % 3 == 0) {
- long temp = divide(2 * num / 3);
- if (temp != -1) {
- length.put(num, 1 + temp);
- return 1 + temp;
- }
- }
- if (num % 5 == 0) {
- long temp = divide(4 * num / 5);
- if (temp != -1) {
- length.put(num, 1 + temp);
- return 1 + temp;
- }
- }
- length.put(num, -1L);
- return -1;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int numCases = sc.nextInt();
- for (int x = 0; x < numCases; x++) {
- System.out.println(divide(sc.nextLong()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement