fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. using ll = long long;
  6.  
  7. const int MOD = 1e9+7;
  8. const ll INF = 1e18;
  9. const int MAXN = 1e6+5;
  10.  
  11. int main() {
  12. FASTIO
  13.  
  14. int t;
  15. cin >> t;
  16. while(t--) {
  17. int n, k;
  18. cin >> n >> k;
  19. vector<int> a(n+1,0), b(n+1,0), c(n+1,0);
  20. a[2] = (k+1), b[2] = 1, c[2] = (k*k)+k-1;
  21. for(int i=3; i<=n; i++) {
  22. a[i] = a[i-1] + c[i-1];
  23. b[i] = a[i-1];
  24. c[i] = (k-1)*a[i-1] + (k+1)*b[i-1];
  25. }
  26. cout << a[n]+b[n]+c[n] << "\n";
  27. }
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 4992KB
stdin
3
7 1
7 3
20 1
stdout
58
1294
55370