class TrafficCongestion { public: int theMinCars(int TH) { ll ans = 1; REP(i, TH-1) ans = (2 * ans + (i%2==0 ? 1 : -1)) % 1000000007LL; return ans; } };
↓ちなみに modulo の数をコピペしてもコンパイル通る(けど 0 か 1 しか返らない)のでややこしい。。
class TrafficCongestion { public: int theMinCars(int TH) { ll ans = 1; REP(i, TH-1) ans = (2 * ans + (i%2==0 ? 1 : -1)) % 1,000,000,007LL; return ans; } };
#define MOD 1000000007LL #define N_MAX 1300 #define K_MAX 1300 int comb[N_MAX][K_MAX]; ll combf(int n, int k) { if(!(0<=n && n<N_MAX)) return 0; if(!(0<=k && k<K_MAX)) return 0; return comb[n][k]; } class LISNumber { public: int count(vector <int> CS, int K) { REP(n, N_MAX) comb[n][0] = 1; RANGE(n, 1, N_MAX) RANGE(m, 1, K_MAX) comb[n][m] = ((ll)comb[n-1][m-1] + comb[n-1][m]) % MOD; int N=CS.size(); VVI dp(N+1, VI(K+1)); // dp[n][k] ... CS[i], i in [0, n) を使って k 個の LIS を作る方法の個数 % MOD dp[0][0] = 1; ll sum = 1; RANGE(i, 1, N+1) { RANGE(k, 1, K+1) { RANGE(pk, 0, k+1) { int add = k-pk; // in [0, k] int rest = CS[i-1] - add; ll plus = dp[i-1][pk] * combf(sum-(pk-rest)-1+add, add) % MOD * combf(pk, rest) % MOD; dp[i][k] += plus; dp[i][k] %= MOD; } } sum += CS[i-1]; } return dp[N][K]; } };