Guest User

gdsub-sol

a guest
May 17th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define mxf 1010
  5. // 'mxf' is the maximum number of distinct primes
  6. const ll mod = 1e9+7;
  7.  
  8. using namespace std;
  9.  
  10. int pf[mxf] = {};   //pf array to store frequency of all primes present
  11.  
  12. int main(){
  13.     // ios_base::sync_with_stdio(false);
  14.     // cin.tie(NULL);
  15.  
  16.     #ifndef ONLINE_JUDGE    
  17.         freopen("input.txt", "r", stdin);
  18.         freopen("output.txt", "w", stdout);
  19.     #endif
  20.  
  21.     int n, k;
  22.     cin>>n>>k;  // n->number of primes, k->max length of subsequence permitted
  23.     for(int i=0; i<n; i++){
  24.         int a;
  25.         cin>>a;
  26.         pf[a] += 1; //increasing frequnency of prime 'a'
  27.     }
  28.     vector <ll> f;  //to further filter pf array
  29.     // for example: pf = {1, 0, 0, 3, 4, 0, 7}
  30.     // then f = {1, 3, 4, 7}
  31.  
  32.     for(int i=0; i<mxf; i++){
  33.         if(pf[i])
  34.             f.push_back(pf[i]);
  35.     }
  36.    
  37.     int m = f.size();   // m = size of 'f' array
  38.     int dp[m+1][k+1] = {};  // dp table created initialising all elements to 0
  39.  
  40.     // for(int i=0; i<k+1; i++){
  41.     //     dp[0][i] = 0;
  42.     // }
  43.  
  44.     for(int i=0; i<m+1; i++){
  45.         dp[i][0] = 1;
  46.     }
  47.    
  48.     for(int i=1; i<m+1; i++){
  49.         for(int j=1; j<k+1; j++){
  50.             dp[i][j] += ( ( f[i-1]*dp[i-1][j-1] ) % mod + dp[i-1][j] ) % mod;
  51.             dp[i][j] %= mod;
  52.         }
  53.     }
  54.  
  55.     ll res = 0; // to stroe the final result
  56.     for(int i=0; i<k+1; i++){
  57.         res += dp[m][i] % mod;
  58.         res %= mod;
  59.     }
  60.     cout<<res<<endl;
  61.     return 0;
  62. }
Add Comment
Please, Sign In to add comment