Advertisement
add1ctus

Расипувач на забава 2

Nov 27th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. class Dropka
  5. {
  6.     public BigInteger broitel;
  7.     public BigInteger imenitel;
  8.      
  9.     public Dropka(Dropka d)
  10.     {
  11.         imenitel = d.imenitel;
  12.         broitel = d.broitel;
  13.     }
  14.      
  15.     public Dropka(BigInteger a, BigInteger b)
  16.     {
  17.         broitel = a;
  18.         imenitel = b;
  19.     }
  20.      
  21.     public void add(Dropka rhs)
  22.     {
  23.         if(!rhs.imenitel.equals(this.imenitel))
  24.         {
  25.             if(rhs.imenitel.compareTo(this.imenitel) > 0)
  26.             {
  27.                 this.broitel = this.broitel.multiply(rhs.imenitel.divide(this.imenitel));
  28.                 this.imenitel = this.imenitel.multiply(rhs.imenitel.divide(this.imenitel));
  29.             }
  30.             else
  31.                 rhs.broitel = rhs.broitel.multiply(this.imenitel.divide(rhs.imenitel));
  32.         }
  33.         this.broitel = this.broitel.add(rhs.broitel);
  34.     }
  35.    
  36.     public void addOne()
  37.     {
  38.         broitel = broitel.add(imenitel);
  39.     }
  40.    
  41.     public void divideByTwo()
  42.     {
  43.         imenitel = imenitel.multiply(new BigInteger("2"));
  44.     }
  45.    
  46.     public void pecati()
  47.     {
  48.         System.out.println(broitel);
  49.         System.out.println(imenitel);
  50.     }
  51.      
  52.     public void skrati()
  53.     {
  54.         BigInteger dva = new BigInteger("2");
  55.         BigInteger nula = new BigInteger("0");
  56.         while(broitel.mod(dva).equals(nula) && imenitel.mod(dva).equals(nula))
  57.         {
  58.             broitel = broitel.divide(dva);
  59.             imenitel = imenitel.divide(dva);
  60.         }
  61.     }
  62.      
  63. }
  64.  
  65. public class VISChallenge {
  66.     private static Dropka[][] DP;
  67.     private static int N;
  68.     private static int K;
  69.    
  70.     private static Dropka f(int x, int y)
  71.     {
  72.         if(DP[x][y] != null)
  73.             return DP[x][y];
  74.         if(x + y == K)
  75.             DP[x][y] = new Dropka(new BigInteger("0"),new BigInteger("1"));
  76.         else if(x == y)
  77.         {
  78.             DP[x][y] = new Dropka(f(x+1,y));
  79.             DP[x][y].addOne();
  80.         }
  81.         else if(y == K-N)
  82.             DP[x][y] = new Dropka(f(x+1,y));
  83.         else if(x == N-1)
  84.             DP[x][y] = new Dropka(f(x,y+1));
  85.         else
  86.         {
  87.             DP[x][y] = new Dropka(f(x+1,y));
  88.             DP[x][y].add(f(x,y+1));
  89.             DP[x][y].divideByTwo();
  90.             DP[x][y].addOne();
  91.         }
  92.         return DP[x][y];
  93.     }
  94.    
  95.     public static void main(String[] args) {
  96.         Scanner in = new Scanner(System.in);
  97.          
  98.         N = in.nextInt();
  99.         K = in.nextInt();
  100.         DP = new Dropka[N+1][K-N+1];
  101.        
  102.         f(0,0);
  103.          
  104.         DP[0][0].skrati();
  105.         DP[0][0].pecati();
  106.          
  107.         in.close();
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement