結果

問題 No.916 Encounter On A Tree
ユーザー LayCurseLayCurse
提出日時 2019-11-02 11:36:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 5,958 bytes
コンパイル時間 2,719 ms
コンパイル使用メモリ 210,772 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-10-13 01:14:21
合計ジャッジ時間 4,982 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 7 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 6 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 7 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 6 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 7 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 6 ms
4,352 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 6 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 6 ms
4,352 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 6 ms
4,352 KB
testcase_26 AC 1 ms
4,352 KB
testcase_27 AC 4 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 6 ms
4,348 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 3 ms
4,352 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 6 ms
4,352 KB
testcase_37 AC 6 ms
4,348 KB
testcase_38 AC 2 ms
4,352 KB
testcase_39 AC 2 ms
4,352 KB
testcase_40 AC 6 ms
4,476 KB
testcase_41 AC 2 ms
4,352 KB
testcase_42 AC 1 ms
4,352 KB
testcase_43 AC 1 ms
4,352 KB
testcase_44 AC 1 ms
4,348 KB
testcase_45 AC 2 ms
4,352 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 1 ms
4,352 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 1 ms
4,352 KB
testcase_51 AC 1 ms
4,348 KB
testcase_52 AC 1 ms
4,352 KB
testcase_53 AC 2 ms
4,352 KB
testcase_54 AC 1 ms
4,352 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,352 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,352 KB
testcase_60 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define MD (1000000007U)
struct Modint{
  unsigned val;
  Modint(){
    val=0;
  }
  Modint(int a){
    val = ord(a);
  }
  Modint(unsigned a){
    val = ord(a);
  }
  Modint(long long a){
    val = ord(a);
  }
  Modint(unsigned long long a){
    val = ord(a);
  }
  inline unsigned ord(unsigned a){
    return a%MD;
  }
  inline unsigned ord(int a){
    a %= MD;
    if(a < 0){
      a += MD;
    }
    return a;
  }
  inline unsigned ord(unsigned long long a){
    return a%MD;
  }
  inline unsigned ord(long long a){
    a %= MD;
    if(a < 0){
      a += MD;
    }
    return a;
  }
  inline unsigned get(){
    return val;
  }
  inline Modint &operator+=(Modint a){
    val += a.val;
    if(val >= MD){
      val -= MD;
    }
    return *this;
  }
  inline Modint &operator-=(Modint a){
    if(val < a.val){
      val = val + MD - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  inline Modint &operator*=(Modint a){
    val = ((unsigned long long)val*a.val)%MD;
    return *this;
  }
  inline Modint &operator/=(Modint a){
    return *this *= a.inverse();
  }
  inline Modint operator+(Modint a){
    return Modint(*this)+=a;
  }
  inline Modint operator-(Modint a){
    return Modint(*this)-=a;
  }
  inline Modint operator*(Modint a){
    return Modint(*this)*=a;
  }
  inline Modint operator/(Modint a){
    return Modint(*this)/=a;
  }
  inline Modint operator+(int a){
    return Modint(*this)+=Modint(a);
  }
  inline Modint operator-(int a){
    return Modint(*this)-=Modint(a);
  }
  inline Modint operator*(int a){
    return Modint(*this)*=Modint(a);
  }
  inline Modint operator/(int a){
    return Modint(*this)/=Modint(a);
  }
  inline Modint operator+(long long a){
    return Modint(*this)+=Modint(a);
  }
  inline Modint operator-(long long a){
    return Modint(*this)-=Modint(a);
  }
  inline Modint operator*(long long a){
    return Modint(*this)*=Modint(a);
  }
  inline Modint operator/(long long a){
    return Modint(*this)/=Modint(a);
  }
  inline Modint operator-(void){
    Modint res;
    if(val){
      res.val=MD-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  inline operator bool(void){
    return val!=0;
  }
  inline operator int(void){
    return get();
  }
  inline operator long long(void){
    return get();
  }
  inline Modint inverse(){
    int a = val;
    int b = MD;
    int u = 1;
    int v = 0;
    int t;
    Modint res;
    while(b){
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if(u < 0){
      u += MD;
    }
    res.val = u;
    return res;
  }
  inline Modint pw(unsigned long long b){
    Modint a(*this);
    Modint res;
    res.val = 1;
    while(b){
      if(b&1){
        res *= a;
      }
      b >>= 1;
      a *= a;
    }
    return res;
  }
  inline bool operator==(int a){
    return ord(a)==val;
  }
  inline bool operator!=(int a){
    return ord(a)!=val;
  }
}
;
inline Modint operator+(int a, Modint b){
  return Modint(a)+=b;
}
inline Modint operator-(int a, Modint b){
  return Modint(a)-=b;
}
inline Modint operator*(int a, Modint b){
  return Modint(a)*=b;
}
inline Modint operator/(int a, Modint b){
  return Modint(a)/=b;
}
inline Modint operator+(long long a, Modint b){
  return Modint(a)+=b;
}
inline Modint operator-(long long a, Modint b){
  return Modint(a)-=b;
}
inline Modint operator*(long long a, Modint b){
  return Modint(a)*=b;
}
inline Modint operator/(long long a, Modint b){
  return Modint(a)/=b;
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(Modint x){
  int i;
  i = (int)x;
  wt_L(i);
}
template<class T, class S> inline T pow_L(T a, S b){
  T res = 1;
  res = 1;
  while(b){
    if(b&1){
      res *= a;
    }
    b >>= 1;
    a *= a;
  }
  return res;
}
inline double pow_L(double a, double b){
  return pow(a,b);
}
int D;
int L;
int R;
int K;
int getdepth(int n){
  int res = 0;
  while(n >= (1<<res)){
    n -= (1<<res);
    res++;
  }
  return res;
}
int main(){
  int i;
  Modint res;
  rd(D);
  rd(L);L += (-1);
  rd(R);R += (-1);
  rd(K);
  L = getdepth(L);
  R = getdepth(R);
  if(L > R){
    swap(L, R);
  }
  ;
  res =pow_L(Modint(2),(R-L));
  K -= R - L;
  if(K < 0 || K % 2){
    wt_L(0);
    wt_L('\n');
    return 0;
  }
  K /= 2;
  if(L < K){
    wt_L(0);
    wt_L('\n');
    return 0;
  }
  res *=pow_L(Modint(2),(L - K));
  res *=pow_L(Modint(4),(K));
  if(K){
    res /= 2;
  }
  for(i=(0);i<(D);i++){
    int j;
    int k = (1<<i);
    if(L==i){
      k--;
    }
    if(R==i){
      k--;
    }
    for(j=(0);j<(k);j++){
      res *= j+1;
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20191102-1

// --- original code ---
// int D, L, R, K;
// 
// int getdepth(int n){
//   int res = 0;
//   while(n >= (1<<res)){
//     n -= (1<<res);
//     res++;
//   }
//   return res;
// }
// 
// {
//   Modint res;
//   rd(D,L--,R--,K);
//   L = getdepth(L);
//   R = getdepth(R);
//   sortE(L,R);
//   res = Modint(2) ** (R-L);
//   K -= R - L;
//   if(K < 0 || K % 2) wt(0), return 0;
//   K /= 2;
//   if(L < K) wt(0), return 0;
//   res *= Modint(2) ** (L - K);
//   res *= Modint(4) ** (K);
//   if(K) res /= 2;
//   rep(i,D){
//     int k = (1<<i);
//     if(L==i) k--;
//     if(R==i) k--;
//     rep(j,k) res *= j+1;
//   }
//   wt(res);
// }
0