1228E - Another Filling the Grid - CodeForces Solution


combinatorics dp math *2300

Please click on ads to support us..

C++ Code:

#include <iostream> 
using namespace std; 
const long long mod = 1000000007LL; 
long long f[255][255]; 
long long C[255][255]; 
long long powk[255], powk1[255]; 
int n, k; 

int main () {
  cin >> n >> k;
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    C[i][i] = 1; 
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; 
    }
  }
  powk[0] = 1LL; 
  for (int i = 1; i <= n; i++) {
    powk[i] = (powk[i - 1] * (long long) k) % mod; 
  }
  powk1[0] = 1LL; 
  for (int i = 1; i <= n; i++) {
    powk1[i] = (powk1[i - 1] * (long long) (k - 1)) % mod;
  }
  f[0][0] = 1LL; 
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (f[i][j] == 0) {
        continue; 
      }
      long long value = f[i][j]; 
      (value *= powk1[n - j]) %= mod; 
      (value *= (powk[j] - powk1[j] + mod) % mod) %= mod; 
      (f[i + 1][j] += value) %= mod; 
      for (int x = 1; x <= n - j; x++) {
        value = f[i][j];
        (value *= C[n - j][x]) %= mod;
        (value *= powk[j]) %= mod; 
        (value *= powk1[n - j - x]) %= mod; 
        (f[i + 1][j + x] += value) %= mod; 
      }
    }
  }
  cout << f[n][n]; 
  return 0; 
}


Comments

Submit
0 Comments
More Questions

1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64