fork download
  1. #include <bits/stdc++.h>
  2. #define MP make_pair
  3. #define PB push_back
  4. #define int long long
  5. #define st first
  6. #define nd second
  7. #define rd third
  8. #define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
  9. #define RE(i, n) FOR(i, 1, n)
  10. #define FORD(i, a, b) for(int i = (a); i >= (b); --i)
  11. #define REP(i, n) for(int i = 0;i <(n); ++i)
  12. #define VAR(v, i) __typeof(i) v=(i)
  13. #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
  14. #define ALL(x) (x).begin(), (x).end()
  15. #define SZ(x) ((int)(x).size())
  16. #define __builtin_ctz __builtin_ctzll
  17. #define __builtin_clz __builtin_clzll
  18. #define __builtin_popcount __builtin_popcountll
  19. using namespace std;
  20. template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
  21. template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  22. while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
  23. }
  24. #ifdef LOCAL
  25. #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
  26. #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
  27. #else
  28. #define debug(...) (__VA_ARGS__)
  29. #define debugv(x)
  30. #define cerr if(0)cout
  31. #endif
  32. #define next ____next
  33. #define prev ____prev
  34. #define left ____left
  35. #define hash ____hash
  36. typedef long long ll;
  37. typedef long double LD;
  38. typedef pair<int, int> PII;
  39. typedef pair<ll, ll> PLL;
  40. typedef vector<int> VI;
  41. typedef vector<VI> VVI;
  42. typedef vector<ll> VLL;
  43. typedef vector<pair<int, int> > VPII;
  44. typedef vector<pair<ll, ll> > VPLL;
  45.  
  46. template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
  47. template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
  48. template<class T1, class T2>
  49. ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
  50. template<class A, class B, class C> struct Triple { A first; B second; C third;
  51. bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
  52. template<class T> void ResizeVec(T&, vector<int>) {}
  53. template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  54. vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  55. for (T& v : vec) { ResizeVec(v, sz); }
  56. }
  57. typedef Triple<int, int, int> TIII;
  58. template<class A, class B, class C>
  59. ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
  60. template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  61. template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  62. template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
  63.  
  64. const int P = 1e9 + 7;
  65. const int M = 52;
  66. struct Matrix {
  67. array<array<int, M>, M> arr;
  68. Matrix() {
  69. REP (i, M) {
  70. REP (j, M) {
  71. arr[i][j] = 0;
  72. }
  73. }
  74. }
  75. array<int, M>& operator[](int a) { return arr[a]; }
  76. Matrix operator*(Matrix Q) {
  77. Matrix R;
  78. REP (i, M) {
  79. REP (j, M) {
  80. REP (k, M) {
  81. R[i][j] = (R[i][j] + arr[i][k] * Q[k][j]) % P;
  82. }
  83. }
  84. }
  85. return R;
  86. }
  87. };
  88. Matrix Pow(Matrix A, int b) {
  89. Matrix R = A;
  90. b--;
  91. while (b) {
  92. if (b % 2) {
  93. R = R * A;
  94. }
  95. A = A * A;
  96. b /= 2;
  97. }
  98. return R;
  99. }
  100.  
  101. int spow(int a, int b) {
  102. int r = 1;
  103. while (b) {
  104. if (b % 2) {
  105. r = r * a % P;
  106. }
  107. a = a * a % P;
  108. b /= 2;
  109. }
  110. return r;
  111. }
  112. int32_t main() {
  113.  
  114. ios_base::sync_with_stdio(0);
  115. cout << fixed << setprecision(10);
  116. cerr << fixed << setprecision(10);
  117. cin.tie(0);
  118. //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  119.  
  120. int L, A;
  121. cin>>L>>A;
  122. int all = -1;
  123. VI at_most(A + 1);
  124. RE (cap, A) {
  125. //vector<int> v(M);
  126. //v[1] = A;
  127. Matrix Q;
  128. REP (i, cap + 1) {
  129. Q[0][i] = 1;
  130. Q[1][i] = (i > 0);
  131. }
  132. FOR (i, 2, cap) {
  133. Q[i][i - 1] = A - i + 1;
  134. FOR (j, i, cap) {
  135. Q[i][j] = 1;
  136. }
  137. }
  138. Q = Pow(Q, L);
  139. //res = (res + Q[0][1]) % P;
  140. at_most[cap] = Q[0][1];
  141. debug(cap, Q[0][1]);
  142. }
  143. VI exact(A + 1);
  144. RE (cap, A) {
  145. exact[cap] = (at_most[cap] - at_most[cap - 1] + P) % P;
  146. }
  147. int R = 0;
  148. RE (cap, A) {
  149. R = (R + cap * exact[cap]) % P;
  150. }
  151. cout<<R * spow(at_most[A], P - 2) % P<<endl;
  152.  
  153.  
  154. return 0;
  155. }
  156.  
Success #stdin #stdout 1.09s 5544KB
stdin
500000000 50
stdout
41761293