fork download
  1. /* package codechef; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8.  
  9. //Note : If you are running this code on codechef then replace Ideone with Codechef
  10. class Ideone //Codechef
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. try{
  15. //write your code here
  16.  
  17. Scanner sc = new Scanner(System.in);
  18. int t = sc.nextInt();
  19. int n = 1001;
  20. int[] v = new int[n];
  21. for(int i = 1; i < n ; i++ ) {
  22.  
  23. int res = 1;
  24. for( int j = 0; j < i ; j++ ) {
  25. res *= (2 * i - j);
  26. res /= (j + 1);
  27. }
  28.  
  29. v[i] = (res / (i + 1));
  30.  
  31.  
  32. }//end of for loop
  33.  
  34. int[] superNaturalWords = new int[n];
  35. superNaturalWords[1] = v[1];
  36. superNaturalWords[0] = 0;
  37.  
  38. for( int i = 2 ; i < n ; i++ ) {
  39. superNaturalWords[i] = ( superNaturalWords[i-1] + v[i] );
  40. }
  41.  
  42. while(t > 0) {
  43. int q = sc.nextInt();
  44. while( q > 0) {
  45. int l = sc.nextInt();
  46. int r = sc.nextInt();
  47.  
  48. System.out.println(superNaturalWords[r] - superNaturalWords[l-1]);
  49.  
  50. q--;
  51. }//end of query loop
  52. t--;
  53. }//end of test case loop
  54. }//end of try block
  55. catch(Exception e){
  56.  
  57. }
  58. }//end of main function
  59.  
  60. }
Success #stdin #stdout 0.14s 36072KB
stdin
1
2
1 2
4 5
stdout
3
56