Advertisement
kaloyan99

Untitled

Jul 6th, 2021
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7. int k = scanner.nextInt();
  8. int n = scanner.nextInt();
  9. System.out.print(calculate(k, n));
  10. }
  11.  
  12. private static int calculate(int k, int n) {
  13. if (n == 1) {
  14. return k;
  15. }
  16. if (n == 2) {
  17. return k + 1;
  18. }//5
  19. if (n == 3) {
  20. return 2 * k + 1;
  21. }
  22. if (n == 4) {
  23. return k + 2;
  24. }
  25. if (n % 3 == 2) {
  26. return calculate(k, n / 3 + 1) + 1;
  27. } else if (n % 3 == 0) {
  28. return 2 * calculate(k, n - 1) - 1;
  29. } else {
  30. return calculate(k, n / 3) + 2;
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement