Advertisement
shamiul93

LightOJ 1067 - Combinations

Feb 19th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. #define ll long long
  4. #define m 1000003
  5.  
  6. vector< int > v ;
  7.  
  8.  
  9. ll bigmod(ll b , ll p)
  10. {
  11.     if(p == 0)
  12.     {
  13.         return 1 ;
  14.     }
  15.     if(p % 2 == 1)
  16.     {
  17.         return ( ((b%m) * (bigmod(b , p-1)%m)) % m );
  18.     }
  19.     else
  20.     {
  21.         return (( (bigmod(b , p/2)%m) * (bigmod(b , p/2)%m)) % m  ) ;
  22.     }
  23. }
  24.  
  25. /***
  26. Just saving the values of factorials in the vector is the work of the
  27. vset() function...
  28. */
  29.  
  30. void vset()
  31. {
  32.     v.push_back(1%m) ;
  33.     for(ll i = 1 ; i <= 1000000 + 7 ; i++)
  34.     {
  35.         v.push_back(((i % m) * (v[i-1]%m)) % m) ;
  36.     }
  37. }
  38.  
  39. /*** Extended Euclid Algorithm could be used.
  40. But I didn't use it. why?
  41. Ans: As we can see , m == 1000003 ; is a prime number.
  42. So, gcd( a , m ) must be equal to 1
  43. Now from Fermat's little theorem we cn see-
  44. 1. a^(p-1) = 1 (mod p)
  45. 2. a^p  = a (mod p)
  46. from 2 we can see, a^p = a (mod p)
  47. dividing both side by a ; we get,
  48.    a^(p-1) = 1 (mod p)
  49. => a * ( a^(p-2)) = 1 (mod p)
  50.  
  51. So, we can easily say ,  a^(p-2) is the modular inverse of a
  52. Now , see , we could use extended Euclid formula , ax + by = gcd(a,b)
  53. where , a is the number , and b would be m and x would be modular inverse
  54. but it could get us negative
  55. number which would take some more work to get in positive.
  56. */
  57.  
  58. //void viset()
  59. //{
  60. //    ll x , y , a ;
  61. //    vi.push_back(0) ;
  62. //
  63. //    for(ll i = 1 ; i<= 1000003 ; i++)
  64. //    {
  65. //        a = bigmod(v[i] , m-2) ;
  66. //
  67. //        vi.push_back(a) ;
  68. //    }
  69. //}
  70.  
  71.  
  72. ll C(ll n , ll r)
  73. {
  74.     /// nCr = n! / ( (n-r)! * r! ) .
  75. /**
  76. Now here is something interesting.
  77. In short, (a/b) % m != (a%m / b%m) % m
  78. we have to find , c = a%m
  79. and d = (1/b) % m = b^-1 mod m = modular inverse of b (mod m)
  80. then it becomes (c*d) % m .
  81. Now we can handle it easily..
  82. */
  83.  
  84.     ll ans ;
  85.     ans = ( (v[n] % m) * (bigmod(v[r],m-2) * bigmod(v[n-r],m-2))%m ) % m ;
  86.  
  87.     return ans ;
  88. }
  89.  
  90.  
  91. int main()
  92. {
  93. //    freopen("out.txt","w",stdout);
  94.     vset() ;
  95.     ll T , t  = 0 ;
  96.     scanf("%lld",&T);
  97.  
  98.     ll ans , n , r ;
  99.  
  100.     while(T--)
  101.     {
  102.         t++;
  103.         scanf("%lld %lld",&n , &r);
  104.         ans = C(n,r);
  105.         printf("Case %lld: %lld\n", t , ans);
  106.     }
  107.     return 0 ;
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement