Dang_Quan_10_Tin

DREAM

Aug 1st, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <unordered_map>
  6. #define task "DREAM"
  7. using namespace std;
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. const int N = 1e2 + 10;
  12. const ll mod = 1e9 + 7;
  13. int sz;
  14. struct Matrix{
  15.     ll a[N][N];
  16.     Matrix() {memset(a, 0, sizeof a);}
  17.     Matrix operator * (const Matrix &m) {
  18.         Matrix ans;
  19.         for(int i = 1; i < sz; ++i)
  20.             for(int j = 1; j < sz; ++j)
  21.                 for(int t = 1; t < sz; ++t)
  22.                     ans.a[i][j] = (ans.a[i][j] + a[i][t] * m.a[t][j] % mod) % mod;
  23.         return ans;
  24.     }
  25. };
  26.  
  27. Matrix Pow(Matrix a, ll b){
  28.     Matrix tmp;
  29.     for(int i = 1; i < sz; ++i)
  30.         tmp.a[i][i] = 1;
  31.     for(; b > 0; b >>= 1){
  32.         if(b & 1){
  33.  
  34.             tmp = tmp * a;
  35.             /*for(int i = 1; i < sz; ++i)
  36.                 for(int j = 1; j < sz; ++j)
  37.                     cout << tmp.a[i][j] << (j == sz - 1 ? "\n" : " ");
  38.             cout << "\n";*/
  39.         }
  40.         a = a * a;
  41.     }
  42.     return tmp;
  43. }
  44.  
  45. int n;
  46. ll k;
  47. ll dp[100002], ans;
  48. unordered_map<int, ll> s;
  49.  
  50. template<class T>
  51. void read(T &x){
  52.     register int c;
  53.     x = 0;
  54.     while((c = getchar()) && c < '0' || c > '9');
  55.     for(; c >= '0' && c <= '9'; c = getchar())
  56.         x = x * 10 + c - '0';
  57. }
  58.  
  59. void Read(){
  60.     read(n); read(k);
  61.     for(int i = 1; i <= n; ++i){
  62.         int d; read(d);
  63.         ++s[d];
  64.         sz = max((int)sz, d + 3);
  65.     }
  66. }
  67.  
  68. void Forces(){
  69.     dp[0] = 1;
  70.     for(ll i = 1; i <= k; ++i){
  71.         for(auto j : s)
  72.             if(i >= j.first){
  73.                 dp[i] = (dp[i] + j.second * dp[i - j.first] % mod ) % mod;
  74.                 if(i - j.first > 0)
  75.                     dp[i] = (dp[i] - j.second * dp[i - j.first - 1]) % mod;
  76.             }
  77.         dp[i] = (dp[i] + dp[i - 1]) % mod;
  78.         ans = (ans + dp[i]) % mod;
  79.     }
  80.     cout << (dp[k] + mod) % mod;
  81. }
  82.  
  83. void Solve(){
  84.     Matrix ans, cur;
  85.     memset(dp, 0, sizeof dp);
  86.     dp[0] = 1;
  87.     for(ll i = 1; i < sz; ++i){
  88.         for(auto j : s)
  89.             if(i >= j.first){
  90.                 dp[i] = (dp[i] + j.second * dp[i - j.first] % mod ) % mod;
  91.                 if(i - j.first > 0)
  92.                     dp[i] = (dp[i] - j.second * dp[i - j.first - 1]) % mod;
  93.             }
  94.         dp[i] = (dp[i] + dp[i - 1]) % mod;
  95.     }
  96.     for(int i = 1; i < sz; ++i)
  97.         ans.a[i][1] = dp[i],
  98.         cur.a[i][i + 1] = 1;
  99.     for(auto j : s){
  100.         cur.a[sz - 1][sz - j.first] = (cur.a[sz - 1][sz - j.first] + j.second) % mod;
  101.         cur.a[sz - 1][sz - j.first - 1] = (cur.a[sz - 1][sz - j.first - 1] - j.second) % mod;
  102.     }
  103.     ++cur.a[sz - 1][sz - 1];
  104.     cur = Pow(cur, k - 1ll);
  105.     cur = cur * ans;
  106.     cout << (cur.a[1][1] + mod) % mod;
  107. }
  108.  
  109. signed main(){
  110.     ios::sync_with_stdio(0);
  111.     cin.tie(0);
  112.     cout.tie(0); if(fopen(task".INP", "r"))
  113.     freopen(task".INP", "r", stdin),
  114.     freopen(task".OUT", "w", stdout);
  115.     Read();
  116.     if(k <= 1e3)
  117.         Forces();
  118.     //  cout << "\n";
  119.     else
  120.         Solve();
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment