結果
問題 | No.916 Encounter On A Tree |
ユーザー |
![]() |
提出日時 | 2019-10-25 22:04:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 18 ms / 2,000 ms |
コード長 | 2,125 bytes |
コンパイル時間 | 1,051 ms |
コンパイル使用メモリ | 112,328 KB |
実行使用メモリ | 23,624 KB |
最終ジャッジ日時 | 2024-09-13 03:43:43 |
合計ジャッジ時間 | 2,949 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 56 |
ソースコード
#include <cstdio>#include <cstring>#include <iostream>#include <string>#include <cmath>#include <bitset>#include <vector>#include <map>#include <set>#include <queue>#include <deque>#include <algorithm>#include <complex>#include <unordered_map>#include <unordered_set>#include <random>#include <cassert>#include <fstream>#include <utility>#include <functional>#include <time.h>#include <stack>#define popcount __builtin_popcountusing namespace std;typedef long long int ll;typedef pair<int, int> P;const ll MOD=1e9+7;ll powmod(ll a, ll k){ll ap=a, ans=1;while(k){if(k&1){ans*=ap;ans%=MOD;}ap=ap*ap;ap%=MOD;k>>=1;}return ans;}ll inv(ll a){return powmod(a, MOD-2);}ll f[5000001], invf[5000001];void fac(int n){f[0]=1;for(ll i=1; i<=n; i++) f[i]=f[i-1]*i%MOD;invf[n]=inv(f[n]);for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD;}int main(){int d, l, r, k;cin>>d>>l>>r>>k;int dl=0, dr=0;int l1=l, r1=r;while(l1){dl++;l1>>=1;}while(r1){dr++;r1>>=1;}dl--; dr--;if((dl+dr)%2!=k%2 || dl+dr<k){cout<<0<<endl;return 0;}int dp=(dl+dr-k)/2;ll c[22]={};for(int i=min(dl, dr); i>=dp; i--){if(dl!=dr) c[i]=(1ll<<(dl-i+dr-i))%MOD;else{ll x=(1<<(dl-i));c[i]=x*(x-1)%MOD;}(c[i]*=(1ll<<i))%=MOD;for(int j=i+1; j<=min(dl, dr); j++){c[i]+=MOD-c[j];c[i]%=MOD;}//cout<<c[i]<<" "<<i<<endl;}ll ans=c[dp];fac(1<<d);if(dl==dr){for(int i=0; i<d; i++){if(i==dl){(ans*=f[(1<<i)-2])%=MOD;}else{(ans*=f[(1<<i)])%=MOD;}}}else{for(int i=0; i<d; i++){if(i==dl || i==dr){(ans*=f[(1<<i)-1])%=MOD;}else{(ans*=f[(1<<i)])%=MOD;}}}cout<<ans<<endl;return 0;}