fork download
  1. // Author : Nishanth Vijayan
  2. // Institution: IIT Ropar,India.
  3. //
  4. // Spoj : http://w...content-available-to-author-only...j.com/users/nishanth_v/
  5. // Codechef : http://w...content-available-to-author-only...f.com/users/nishanth_v
  6. // HackerEarth : http://w...content-available-to-author-only...h.com/users/nishanththegr8/
  7. // Facebook : https://w...content-available-to-author-only...k.com/NishanthTheGr8
  8. //
  9. // Motto : The Less You Give A Fuck, The Happier You'll Be. :)
  10.  
  11.  
  12. #include <iostream>
  13. #include <cstdio>
  14. #include <map>
  15. #include <set>
  16. #include <vector>
  17. #include <stack>
  18. #include <algorithm>
  19. #include <utility>
  20. #include <cmath>
  21. #include <string>
  22. #include <cstring>
  23.  
  24.  
  25. #define ABS(x) ((x)<0?-(x):(x))
  26. #define SQR(x) ((x)*(x))
  27. #define CUBE(x) ((x)*(x)*(x))
  28. #define pnl printf("\n");
  29. #define REP(i,n) for(__typeof(n) i=0;i<(n);i++)
  30. #define FOR(i,a,b) for(__typeof(b) i=(a);i<(b);++i)
  31. #define FORE(i,a,b) for(__typeof(b) i=(a);i<=(b);++i)
  32. #define FORD(i,a,b,d) for(__typeof(b) i=(a);i<(b);i+=(d))
  33. #define FORR(i,n,e) for(__typeof(n) i=(n);i>=(e);--i)
  34. #define FORRD(i,n,e,d) for(__typeof(n) i=(n);i>=(e);i-=(d))
  35. #define FOREACH(i,s) for(__typeof((s).begin()) i=(s).begin();i!=(s).end();i++)
  36. #define UNIQUE(v) sort(ALL(v)),v.erase(unique(ALL(v)),v.end())
  37. #define FILL(a,b) memset(a,b,sizeof(a))
  38. #define pi acos(-1)
  39. #define LLI long long int
  40. #define gc getchar_unlocked
  41. #define pc putchar_unlocked
  42.  
  43. using namespace std;
  44.  
  45. LLI scanint()
  46. {
  47. register int c = gc();
  48. LLI x = 0;
  49. int neg = 0;
  50. for(;((c<48 || c>57) && c != '-');c = gc());
  51. if(c=='-') {neg=1;c=gc();}
  52. for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
  53. if(neg) x=-x;
  54. return x;
  55. }
  56.  
  57. void writeint (LLI n)
  58. {
  59. LLI N = n, rev, count = 0;
  60. rev = N;
  61. if (N == 0) { pc('0'); pc('\n'); return ;}
  62. while ((rev % 10) == 0) { count++; rev /= 10;}
  63. rev = 0;
  64. while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}
  65. while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;}
  66. while (count--) pc('0');
  67. }
  68.  
  69. int n,m,A[30],B[30];
  70.  
  71.  
  72. int F(int i,int W){
  73.  
  74. if(i==1){
  75. if(W>=A[0] && W<=B[0]) return 1;
  76. else return 0;
  77. }
  78. int count =0;
  79. FORE(j,A[i-1],B[i-1]){count+=F(i-1,W-j);}
  80. return count;
  81. }
  82.  
  83.  
  84. int main(){
  85.  
  86. cin>>m>>n;
  87. while(n!=0 || m!=0){
  88. FOR(i,0,m){cin>>A[i]>>B[i];}
  89. cout<<F(m,n)<<endl;
  90. cin>>m>>n;
  91. }
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 0s 3304KB
stdin
3 5

0 1

1 3

1 4

0 0

 
stdout
6