mrlolthe1st

Untitled

Nov 1st, 2020
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.13 KB | None | 0 0
  1. fun factorize(b : Int) : HashMap<Int, Int> {
  2.     var i = 2
  3.     var a = b
  4.     var res = HashMap<Int, Int>()
  5.     while (i * i <= a) {
  6.         while ((a % i) == 0) {
  7.             if (!res.containsKey(i))
  8.                 res[i] = 0
  9.             res[i] = res[i]!! + 1
  10.             a /= i
  11.         }
  12.         i++
  13.     }
  14.     if (a > 1)
  15.     {
  16.         if (!res.containsKey(a))
  17.             res[a] = 0
  18.         res[a] = res[a]!! + 1
  19.     }
  20.     return res
  21. }
  22.  
  23. fun max(a : Long, b : Long) : Long {
  24.     return if (a > b) a else b
  25. }
  26.  
  27. fun main() {
  28.     val t = readLine()!!.toInt()
  29.     for (k in 0 until t) {
  30.         var (a, b) = readLine()!!.split(' ').map(String::toLong)
  31.         val res = factorize(b.toInt())
  32.         var ans : Long = 0
  33.         for (i in res) {
  34.             var pp = a
  35.             var step = 0
  36.             while ((pp % i.key) == 0L) {
  37.                 step++
  38.                 pp /= i.key
  39.             }
  40.             var j = 0
  41.             while (j < i.value - 1 && j < step)
  42.             {
  43.                 pp *= i.key
  44.                 j++
  45.             }
  46.             ans = max(ans, pp)
  47.         }
  48.         println(ans)
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment