結果
問題 | No.916 Encounter On A Tree |
ユーザー |
![]() |
提出日時 | 2019-10-25 22:28:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 32 ms / 2,000 ms |
コード長 | 7,212 bytes |
コンパイル時間 | 1,056 ms |
コンパイル使用メモリ | 106,776 KB |
実行使用メモリ | 36,304 KB |
最終ジャッジ日時 | 2024-09-13 04:38:59 |
合計ジャッジ時間 | 4,454 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 56 |
ソースコード
// #define _GLIBCXX_DEBUG // for STL debug (optional)#include <iostream>#include <iomanip>#include <cstdio>#include <string>#include <cstring>#include <deque>#include <list>#include <queue>#include <stack>#include <vector>#include <utility>#include <algorithm>#include <map>#include <set>#include <complex>#include <cmath>#include <limits>#include <cfloat>#include <climits>#include <ctime>#include <cassert>#include <numeric>#include <fstream>#include <functional>#include <bitset>using namespace std;using ll = long long int;using int64 = long long int;template<typename T> void chmax(T &a, T b) {a = max(a, b);}template<typename T> void chmin(T &a, T b) {a = min(a, b);}template<typename T> void chadd(T &a, T b) {a = a + b;}int dx[] = {0, 0, 1, -1};int dy[] = {1, -1, 0, 0};const int INF = 1LL << 29;const ll LONGINF = 1LL << 60;const ll MOD = 1000000007LL;int depth(int k) {int ans = 0;while(k > 0) ans++, k /= 2;return ans;}int get_num_of_pair(int d) {if(d == 1) return 0;return 1 << (d - 2);}// ModInt beginusing ll = long long;template<ll mod>struct ModInt {ll v;ll mod_pow(ll x, ll n) const {return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod;}ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {}ModInt operator+ ( const ModInt& b ) const {return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));}ModInt operator- () const {return ModInt(-v);}ModInt operator- ( const ModInt& b ) const {return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));}ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;}ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;}bool operator== ( const ModInt &b ) const {return v == b.v;}bool operator!= ( const ModInt &b ) const {return !(*this == b); }ModInt& operator+= ( const ModInt &b ) {v += b.v;if(v >= mod) v -= mod;return *this;}ModInt& operator-= ( const ModInt &b ) {v -= b.v;if(v < 0) v += mod;return *this;}ModInt& operator*= ( const ModInt &b ) {(v *= b.v) %= mod;return *this;}ModInt& operator/= ( const ModInt &b ) {(v *= mod_pow(b.v, mod-2)) %= mod;return *this;}ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }// operator int() const { return int(v); }// operator long long int() const { return v; }};template<ll mod>ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}template<ll mod>istream& operator>> (istream& in, ModInt<mod>& a) {in >> a.v;return in;}// ModInt end// 各種組み合わせを求めるライブラリtemplate <typename NumType>struct Combination {int LIMIT;vector<NumType> fact_, finv_;Combination() {}Combination(int LIMIT_) : LIMIT(LIMIT_), fact_(LIMIT+1), finv_(LIMIT+1) {fact_[0] = finv_[LIMIT] = NumType(1);for(int i=1; i<=LIMIT; i++) {fact_[i] = fact_[i-1] * NumType(i);}finv_[LIMIT] /= fact_[LIMIT];for(int i=LIMIT-1; i>=0; i--) {finv_[i] = finv_[i+1] * NumType(i+1);}}inline NumType fact(int k) const { return fact_[k]; }inline NumType finv(int k) const { return finv_[k]; }NumType P(int n, int r) const {if(r < 0 or n < r) return NumType(0);return fact_[n] * finv_[n-r];}NumType C(int n, int r) const {if(r < 0 or n < r) return NumType(0);return fact_[n] * finv_[n-r] * finv_[r];}// 重複組み合わせNumType H(int n, int r) const {if(n < 0 or r < 0) return NumType(0);return r == 0 ? NumType(1) : C(n + r - 1, r);}// ベル数 (区別できる n 個のボールを区別できない k 個以下の箱に分割)// B(n, n) := n 個のボールを任意個のグループに分割する場合の数NumType B(int n, int k) const {if(n == 0) return NumType(1);k = min(n, k);NumType ret(0);vector<NumType> pref(k + 1); pref[0] = NumType(1);for(int i=1; i<=k; i++) {if(i & 1) pref[i] = pref[i-1] - finv_[i];else pref[i] = pref[i-1] + finv_[i];}for(int i=1; i<=k; i++) {// 累乗が必要なので適宜書き換える?// ModInt 使うならこれでいいret += NumType(i).pow(n) * finv_[i] * pref[k-i];}return ret;}// スターリング数 (区別できる n 個のボールを区別できない k 個の箱に分割)NumType S(int n, int k) const {if(n < k) return NumType(0);NumType ans(0);for(int i=0; i<=k; i++) {NumType val = C(k, i) * NumType(i).pow(n);if((k - i) % 2) ans -= val;else ans += val;}return ans * finv_[k];}// ランダムウォーク: 左に X 回、右に Y 回進むとき、// 移動途中・終了時に座標 K を超えないものを数える// K が非負なら左側、負なら右側領域 (?) のランダムウォークNumType walk(int X, int Y, int K) {if(K < 0) return walk(Y, X, -K);if(Y <= K) return C(X+Y, X); // 引っかからないif(Y - X > K) return NumType(0); // そもそも合計が超えるint A = Y - K - 1, B = X + K + 1;return C(X+Y, X) - C(A+B, A);}};// P(n, k) := n の k 分割 (k 個の 0 以上の整数の和)template <typename NumType, int LIMIT = 3010>struct Partition {vector< vector<NumType> > dp;Partition() : dp(LIMIT, vector<NumType>(LIMIT)) {for(int k=0; k<LIMIT; k++) dp[0][k] = NumType(1);for(int i=1; i<LIMIT; i++) {for(int j=1; j<LIMIT; j++) {dp[i][j] += dp[i][j-1];if(i-j >= 0) dp[i][j] += dp[i-j][j];}}}inline NumType get(int n, int k) {if(n < 0 or k < 0) return NumType(0);return dp[n][k];}};using mint = ModInt<MOD>;int main() {int D, L, R, K; cin >> D >> L >> R >> K;int dl = depth(L), dr = depth(R);Combination<mint> comb(1 << 21);{mint base(1);for(int d=1; d<=D; d++) {int vtx = 1 << (d - 1);if(d == dl) vtx--;if(d == dr) vtx--;if(vtx < 0) base = mint(0);else base *= comb.fact(vtx);}mint sum(0);for(int d=1; d<=D; d++) {if(d > dl or d > dr) continue;int ml = dl - d, mr = dr - d;if(ml + mr != K) continue;mint pl = mint(2).pow(max(0, ml - 1));mint pr = mint(2).pow(max(0, mr - 1));mint pm = mint(2).pow(d - 1);fprintf(stderr, "ml = %d, mr = %d\n", ml, mr);fprintf(stderr, "d = %d: pl = %lld, pr = %lld, pm = %lld\n", d, pl.v, pr.v, pm.v);sum += pl * pr * pm;}base *= mint(2);cout << base * sum << endl;}return 0;}