Skip to content

Instantly share code, notes, and snippets.

@mikebsg01
Created October 23, 2018 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebsg01/45cd7c815f1742ae967745dd5722a066 to your computer and use it in GitHub Desktop.
Save mikebsg01/45cd7c815f1742ae967745dd5722a066 to your computer and use it in GitHub Desktop.
Pascal's Triangle with Bitmasks (DP Solution)
#include <bits/stdc++.h>
#define dd(x) cout << #x << ": " << x << endl;
#define MAXN 1000003
#define MOD 1000000007
using namespace std;
typedef long long int lli;
int N;
lli dp[2][MAXN];
void pascal() {
int i, j, idx = 1;
dp[0][1] = 1;
for (i = 1; i <= N; ++i) {
for (j = 1; j <= (i + 1); ++j) {
dp[idx][j] = dp[idx ^ 1][j] + dp[idx ^ 1][j - 1];
dp[idx][j] %= MOD;
}
idx ^= 1;
}
idx ^= 1;
for (i = 1; i <= (N + 1); ++i) {
if (i > 1) {
cout << ' ';
}
cout << dp[idx][i];
}
cout << '\n';
}
int main() {
cin >> N;
pascal();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment