Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7. static class Task {
  8. public final static String INPUT_FILE = "in";
  9. public final static String OUTPUT_FILE = "out";
  10.  
  11. int n, x, y;
  12.  
  13. private void readInput() {
  14. try {
  15. Scanner sc = new Scanner(new File(INPUT_FILE));
  16. n = sc.nextInt();
  17. x = sc.nextInt();
  18. y = sc.nextInt();
  19. sc.close();
  20. } catch (IOException e) {
  21. throw new RuntimeException(e);
  22. }
  23. }
  24.  
  25. private void writeOutput(int answer) {
  26. try {
  27. PrintWriter pw = new PrintWriter(new File(OUTPUT_FILE));
  28. pw.printf("%d\n", answer);
  29. pw.close();
  30. } catch (IOException e) {
  31. throw new RuntimeException(e);
  32. }
  33. }
  34.  
  35. private int getAnswer(int n, int x, int y) {
  36. // TODO: Calculati valoarea de pe pozitia (x, y) din matricea de dimensiune
  37. // 2^N * 2^N.
  38.  
  39. if (n == 0) {
  40. return 1;
  41. } else {
  42. int aux = (int) Math.pow(2, n - 1);
  43. if (x <= aux && y <= aux) {
  44. return getAnswer(--n, x, y);
  45. } else if (x <= aux && y > aux) {
  46. return aux * aux + getAnswer(--n, x, y - aux);
  47. } else if (x > aux && y <= aux) {
  48. return 2 * aux * aux + getAnswer(--n, x - aux, y);
  49. } else {
  50. return 3 * aux * aux + getAnswer(--n, x - aux, y - aux);
  51. }
  52. }
  53. }
  54.  
  55. public void solve() {
  56. readInput();
  57. writeOutput(getAnswer(n, x, y));
  58. }
  59. }
  60.  
  61. public static void main(String[] args) {
  62. new Task().solve();
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement