Advertisement
matheus_monteiro

1616 Baile de Formatura

May 12th, 2022
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  4. const int mod = 1e9 + 7;
  5.  
  6. int B, G;
  7. int dp[1005][1005];
  8. int seen[1005][1005];
  9. int timestamp = 1;
  10.  
  11. int ways(int n, int m) {
  12.     if(m < 0) {
  13.         return 0;
  14.     }
  15.     if(n == 0) {
  16.         if(m == 0) {
  17.             return 1;
  18.         }
  19.         return 0;
  20.     }
  21.     if(seen[n][m] == timestamp) {
  22.         return dp[n][m];
  23.     }
  24.     int ans1 = (ways(n - 1, m - 1) * 1ll * m) % mod;
  25.     int ans2 = (ways(n - 1, m) * 1ll * (G - m)) % mod;
  26.     seen[n][m] = timestamp;
  27.     return dp[n][m] = (ans1 + ans2) % mod;
  28. }
  29.  
  30. int main(){
  31.  
  32.     ios_base::sync_with_stdio(false) ; cin.tie(NULL) ;
  33.  
  34.     while(cin >> B >> G and B and G) {
  35.         ++timestamp;
  36.         cout << ways(B, G) << '\n';
  37.     }
  38.  
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement