The5threic1-1

Counting_Problem_CC

Apr 4th, 2024
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl '\n'
  3. using namespace std;
  4. #ifdef  ONLINE_JUDGE
  5.     #define eprintf(...) 42
  6. #else
  7.         #define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
  8. #endif
  9. clock_t startTime;
  10. double getCurrentTime()
  11. {
  12.     return (double)(clock() - startTime) / CLOCKS_PER_SEC;
  13. }
  14. using namespace std;
  15. const long long mod = (long long) 1e9 + 7;
  16. const int siz = 200005;
  17. long long fact[siz], inv[siz];
  18. long long po(long long x, long long y)
  19. {
  20.     long long ans = 1;
  21.     x %= mod;
  22.     while(y>0)
  23.     {
  24.         if(y&1)
  25.         {
  26.             ans = (ans*x) % mod;
  27.         }
  28.         y /= 2;
  29.         x = (x * x)% mod;
  30.     }
  31.     return ans;
  32. }
  33. long long precomp()
  34. {
  35.     fact[0] = 1;
  36.     inv[0] = 1;
  37.     for(int i=1; i<siz; i++)
  38.     {  
  39.         fact[i] = (fact[i-1]*i) % mod;
  40.         inv[i] = po(fact[i], mod-2);
  41.     }
  42.     return 0;
  43. }
  44.  
  45. long long nCr(long long n, long long r)
  46. {
  47.     if(n<0 || n<r)
  48.     {
  49.         return 0;  
  50.     }
  51.     long long ans = fact[n];
  52.     ans *= inv[r]; ans%=mod;
  53.     ans *= inv[n-r]; ans%=mod;
  54.     return ans;
  55. }
  56. int solve()
  57. {
  58.     long long n, m, k;
  59.     cin>>n>>m>>k;
  60.     long long ans = po(m, n);
  61.     for(int i=0; i<=n; i++)
  62.     {
  63.         if(i<k)
  64.         {
  65.             long long sub_ans = nCr(n, i)*po(m-k, i); sub_ans%=mod;
  66.             sub_ans *= po(k-1, n-i); sub_ans%=mod;
  67.             ans -= sub_ans; ans%=mod; ans+=mod; ans%=mod;
  68.         }
  69.                 if(i>=k)
  70.         {
  71.             long long sub_ans = nCr(n, i)*po(m-k, k); sub_ans%=mod;
  72.             sub_ans *= po(m-k+1, i-k); sub_ans%=mod;
  73.             sub_ans *= po(k-1, n-i); sub_ans%=mod;
  74.             ans -= sub_ans; ans%=mod; ans+=mod; ans%=mod;  
  75.         }
  76.     }
  77.     cout<<ans<<endl;
  78.     return 0;
  79. }
  80. int main()
  81. {
  82.     ios::sync_with_stdio(false);
  83.     cin.tie(0);
  84.     int t;
  85.     cin>>t;
  86.     startTime = clock();
  87.     precomp();
  88.     while(t--)
  89.     {
  90.         solve();
  91.     }
  92.     eprintf("time = %.5lf\n", getCurrentTime());
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment