fork download
  1. //coded by vishal mourya
  2. //If you use this code anywhere you need to mention my name as above
  3.  
  4. #include<bits/stdc++.h>
  5. #define ll long long int
  6. #define vec vector<ll>
  7. #define pb push_back
  8. #define f(a,b) for( ll i = a ; i < b ; i++ )
  9. #define fe(a,b) for( ll i = a ; i <= b ; i++ )
  10. #define fj(a,b) for( ll j = a ; j < b ; j++ )
  11. #define fk(a,b) for( ll k = a ; k < b ; k++ )
  12. #define fasthoja ios_base::sync_with_stdio(false); cin.tie(NULL);
  13. #define maxN 1001
  14. #define mod 1000000007
  15. using namespace std;
  16.  
  17. vec v(maxN),superNaturalWords(maxN);
  18.  
  19. ll count_SuperNatural_Words( ll n )
  20. {
  21. // Calculate the value of 2nCn
  22. ll res = 1;
  23. f(0,n) {
  24. res *= (2 * n - i);
  25. res /= (i + 1);
  26. }
  27. // Return 2nCn/(n+1)
  28. return (res / (n + 1));
  29. }
  30.  
  31. void init() {
  32. f(1,maxN) {
  33. v[i] = count_SuperNatural_Words(i);
  34. }
  35.  
  36. superNaturalWords[1] = v[1];
  37.  
  38. f(2,maxN) {
  39. superNaturalWords[i] = ( superNaturalWords[i-1] + v[i] );
  40. }
  41.  
  42. }//end of initialization function
  43.  
  44. int main(void){
  45.  
  46. fasthoja;
  47. ll t; cin>>t;
  48. init();
  49.  
  50. while(t--){
  51. ll q; cin >> q;
  52.  
  53. while(q--) {
  54. ll l,r; cin >> l >> r;
  55.  
  56. cout << superNaturalWords[r] - superNaturalWords[l-1] <<"\n";
  57. }//end of query loop
  58.  
  59. }//end of test case loop
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 4504KB
stdin
1
2
1 2
4 5
stdout
3
56