Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. //Daria Magiera - 1
  2. import java.util.Arrays;
  3.  
  4. class NSum {
  5.  
  6.  
  7.     static class Stack {
  8.  
  9.         public int[] stackArr;
  10.         private int maxSize;
  11.         public int topOfStack;
  12.  
  13. //        public Stack(int max) {
  14. //            maxSize = max;
  15. //            stackArr = new int[maxSize];
  16. //            topOfStack = 0;
  17. //        }
  18.  
  19.         public boolean isEmpty() {
  20.             return (topOfStack == 0);
  21.         }
  22.  
  23.         public int top() {
  24.             return stackArr[topOfStack - 1];
  25.         }
  26.  
  27.         public void push(int c) {
  28.             stackArr[topOfStack] = c;
  29.             topOfStack++;
  30.         }
  31.  
  32.         public int pop() {
  33.             int c = stackArr[topOfStack - 1];
  34.             topOfStack--;
  35.             return c;
  36.         }
  37.  
  38.         public int size() {
  39.             return topOfStack;
  40.         }
  41.  
  42.         public Stack() {
  43.             maxSize = 20;
  44.             stackArr = new int[maxSize];
  45.             topOfStack = 0;
  46.         }
  47.     }
  48.  
  49.     public static int[] popEverything(int i, Stack stack, int[] subArr, int top) {
  50.  
  51.         if (i < top) {
  52.             subArr[i] = stack.pop();
  53.             return popEverything(++i, stack, subArr, top);
  54.         }
  55.         else {
  56.             return subArr;
  57.         }
  58.     }
  59.  
  60.     public static int[] findNComponentsRecAux(int last, int a, int n, Stack stack) {
  61.  
  62.         if (last < 0) {
  63.             int[] arr = new int[0];
  64.             return arr;
  65.         }
  66.  
  67.         stack.push(a);
  68.         int z = last - (int)Math.pow(a, n);
  69.         if (z == 0) {
  70.             int subArr[] = new int[stack.topOfStack];
  71.             int top = stack.topOfStack;
  72.             return popEverything(0, stack, subArr, top);
  73.         }
  74.         int c = (int)Math.pow( z, 1. / n);
  75.  
  76.         if (c >= a && c != 1) {
  77.             c = a - 1;
  78.         }
  79.  
  80.         if (z != 1 && c == 1) {
  81.             int sciagam = stack.pop();
  82.             if (stack.isEmpty()) {
  83.                 int[] arr = new int[0];
  84.                 return arr;
  85.             }
  86.  
  87.             return findNComponentsRecAux((int)Math.pow(sciagam, n) + z, sciagam - 1, n, stack);
  88.  
  89.         }
  90.         if (z > c * (int)Math.pow(c, n)) {
  91.  
  92.             stack.pop();
  93.             if (stack.isEmpty()) {
  94.                 int[] arr = new int[0];
  95.                 return arr;
  96.             }
  97.             int sciagam = stack.pop();
  98.             return findNComponentsRecAux((int)Math.pow(sciagam, n) + last, sciagam - 1, n, stack);
  99.         }
  100.  
  101. //--------------
  102.         return findNComponentsRecAux(z, c, n, stack);
  103.  
  104.     }
  105.  
  106.     public static int[] findNComponentsRec(int x, int n) {
  107.  
  108.         Stack stack = new Stack();
  109.         int a = (int)Math.pow(x, 1. / n);
  110.         return findNComponentsRecAux(x, a, n, stack);
  111.  
  112.     }
  113.  
  114.     public static int[] findNComponentsIter(int x, int n) {
  115.  
  116.         Stack stack = new Stack();
  117.         int a = (int)Math.pow( x, 1. / n);
  118.         stack.push(a);
  119.         int z = x - (int)Math.pow(a, n);
  120.  
  121.         if (z == 0) {
  122.  
  123.             int subArr[] = new int[stack.topOfStack];
  124.             int top = stack.topOfStack;
  125.             for (int i = 0; i < top; i++) {
  126.                 subArr[i] = stack.pop();
  127.             }
  128.             return subArr;
  129.  
  130.         } // jesli to po prostu potega jednej liczby ^^^
  131.  
  132.         stack.pop();
  133.  
  134.         while (z != 0) {
  135.  
  136.             stack.push(a);
  137.             z = x - (int) Math.pow(a, n);
  138.  
  139.             int c = (int)Math.pow( z, 1. / n);
  140.  
  141.             if (c >= a && c != 1) {
  142.                 c = a - 1;
  143.             }
  144.  
  145.             if (z != 1 && c == 1) {
  146.                 int sciagam = stack.pop();
  147.                 if (stack.isEmpty()) {
  148.                     int[] arr = new int[0];
  149.                     return arr;
  150.                 }
  151.  
  152.                 x = (int) Math.pow(sciagam, n) + z;
  153.                 a = sciagam - 1;
  154.  
  155.                 continue;
  156.             }
  157.  
  158.             if (z > c * (int) Math.pow(c, n)) {
  159.  
  160.                 stack.pop();
  161.                 if (stack.isEmpty()) {
  162.                     int[] arr = new int[0];
  163.                     return arr;
  164.                 }
  165.                 int sciagam = stack.pop();
  166.                 x = (int) Math.pow(sciagam, n) + x;
  167.                 a = sciagam - 1;
  168.  
  169.                 continue;
  170.             }
  171.  
  172.             x = z;
  173.             a = c;
  174.  
  175.             continue;
  176.  
  177.         } //-----------------------------------koniec while
  178.  
  179.         int subArr[] = new int[stack.topOfStack];
  180.         int top = stack.topOfStack;
  181.         for (int i = 0; i < top; i++) {
  182.             subArr[i] = stack.pop();
  183.         }
  184.         return subArr;
  185.  
  186.  
  187.     } //koniec funkcji
  188.  
  189. }
  190.  
  191. public class Source {
  192.  
  193.     public static void main(String[] args) {
  194.         //152512 nie dziala
  195.         System.out.println(Arrays.toString(NSum.findNComponentsRec(10020001, 3)));
  196.  
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement