結果
問題 | No.916 Encounter On A Tree |
ユーザー |
|
提出日時 | 2020-04-17 15:02:52 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 23 ms / 2,000 ms |
コード長 | 1,510 bytes |
コンパイル時間 | 1,500 ms |
コンパイル使用メモリ | 168,196 KB |
実行使用メモリ | 27,008 KB |
最終ジャッジ日時 | 2024-10-03 10:18:55 |
合計ジャッジ時間 | 4,420 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 56 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; using ll = long long ; using P = pair<int,int> ; const int INF = 1e9; const int MOD = 1000000007; ll fac[3000000]; void init(){ fac[0] = 1; fac[1] = 1; for(int i=2;i<3000000;i++){ fac[i] = (fac[i-1] * i)%MOD; } } int depth(int p){ int i = 0; int res = 1; while(res <= p){ res *= 2; i ++; } return i; } ll power(int n,int k){ ll res = 1; for(int i=0;i<k;i++) res = (res * n)%MOD; return res; } int main(){ init(); int d,l,r,k; cin >> d >> l >> r >> k; int ld = depth(l); int rd = depth(r); //cout << ld << " "<< rd << endl; if((k-(rd-ld))%2 == 0 && ld - (k-(rd-ld))/2 >= 1 && rd-ld <= k){ int lca_d = ld - (k-(rd-ld))/2 ; //cout << lca_d << endl; ll ans; if(lca_d == ld) ans = (power(2,ld-1) * (power(2,rd-ld)))%MOD; else ans = (((power(2,lca_d-1) * power(2,ld-lca_d-1))%MOD * power(2,rd-lca_d-1))%MOD * 2)%MOD; //cout << lca_d-1 << " " << ld-lca_d-1 << " " << rd-lca_d-1 << endl; //cout << ans << endl; ll res = 2; for(ll i=2;i<=d;i++){ if(i==ld && i==rd) ans = (ans * fac[res-2])%MOD; else if(i==ld || i==rd) ans = (ans * fac[res-1])%MOD; else ans = (ans * fac[res])%MOD; res *= 2; } cout << ans << endl; }else{ cout << 0 << endl; return 0; } return 0; }