Guest User

LVADER - Luke vs. Darth Vader

a guest
Feb 25th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  * Created by bugkiller on 16/02/18.
  7.  */
  8.  
  9. // TODO: 16/02/18 http://www.spoj.com/problems/LVADER/
  10. class LVADER {
  11.     static final int MAX = 2000005;
  12.     static final long P = 1000000007;
  13.     static long factorial[] = new long[MAX];
  14.  
  15.     public static void main(String[] args) throws IOException {
  16.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17.         int t, x1, y1, x2, y2;
  18.         String[] s;
  19.         t = Integer.parseInt(br.readLine());
  20.         fillFactorial();
  21.         while (t-- > 0) {
  22.             s = br.readLine().split("\\s");
  23.             x1 = Integer.parseInt(s[0]);
  24.             y1 = Integer.parseInt(s[1]);
  25.             x2 = Integer.parseInt(s[2]);
  26.             y2 = Integer.parseInt(s[3]);
  27.             System.out.println(solve(x1, y1, x2, y2));
  28.         }
  29.         System.exit(0);
  30.     }
  31.  
  32.     private static long solve(int x1, int y1, int x2, int y2) {
  33.         return totalPaths(x2 - x1, y2 - y1);
  34.     }
  35.  
  36.     private static long totalPaths(int m, int n) {
  37.         int k = Integer.min(m, n);
  38.         long ans = 0;
  39.         for (int i = 0; i <= k; i++) {
  40.             int x = m + n - i;
  41.             long tempAns = (nCR(x, m) * nCR(m, i)) % P;
  42.             ans = (ans + tempAns) % P;
  43.         }
  44.         return ans;
  45.     }
  46.  
  47.     static long nCR(int n, int r) {
  48.         return (((factorial[n] * pow(factorial[r], P - 2)) % P) * pow(factorial[n - r], P - 2)) % P;
  49.     }
  50.  
  51.     public static long pow(long a, long n) {
  52.         if (n > 0) {
  53.             long temp = pow(a, n / 2);
  54.             temp = (temp * temp) % P;
  55.             if ((n & 1) == 1) {
  56.                 temp = (temp * a) % P;
  57.             }
  58.             return temp;
  59.         }
  60.         return 1;
  61.     }
  62.  
  63.     private static void fillFactorial() {
  64.         factorial[0] = 1;
  65.         for (int i = 1; i < MAX; i++) {
  66.             factorial[i] = (factorial[i - 1] * i) % P;
  67.         }
  68.     }
  69.  
  70.  
  71.  
  72. }
Add Comment
Please, Sign In to add comment