結果

問題 No.907 Continuous Kadomatu
ユーザー LayCurseLayCurse
提出日時 2019-11-02 11:21:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 12,605 bytes
コンパイル時間 3,048 ms
コンパイル使用メモリ 224,768 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-14 23:03:03
合計ジャッジ時間 4,271 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 6 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 14 ms
5,376 KB
testcase_14 AC 15 ms
5,376 KB
testcase_15 AC 15 ms
5,376 KB
testcase_16 AC 16 ms
5,376 KB
testcase_17 AC 17 ms
5,376 KB
testcase_18 AC 15 ms
5,376 KB
testcase_19 AC 16 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 5 ms
5,376 KB
testcase_23 AC 58 ms
5,376 KB
testcase_24 AC 58 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'T Comb<T>::ifac(int) [with T = Modint]',
    inlined from 'int main()' at main.cpp:543:34:
main.cpp:307:19: warning: 'comb.Comb<Modint>::ifactri' may be used uninitialized [-Wmaybe-uninitialized]
  307 |     return ifactri[k];
      |            ~~~~~~~^
main.cpp: In function 'int main()':
main.cpp:518:16: note: 'comb.Comb<Modint>::ifactri' was declared here
  518 |   Comb<Modint> comb;
      |                ^~~~

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define MD (1000000007U)
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
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 S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
template<class T> struct Comb{
  int mem_fact;
  T *factri;
  T *ifactri;
  Comb(){
    mem_fact = 0;
  }
  inline void expand_fact(int k){
    if(k <= mem_fact){
      return;
    }
    chmax(k, 2* mem_fact);
    if(mem_fact == 0){
      int i;
      factri = (T*)malloc(k * sizeof(T));
      ifactri = (T*)malloc(k * sizeof(T));
      factri[0] = 1;
      for(i=(1);i<(k);i++){
        factri[i] = i * factri[i-1];
      }
      ifactri[k-1] = 1 / factri[k-1];
      for(i=(k-1)-1;i>=(0);i--){
        ifactri[i] = (i+1) * ifactri[i+1];
      }
    }
    else{
      int i;
      factri = (T*)realloc(factri, k * sizeof(T));
      ifactri = (T*)realloc(ifactri, k * sizeof(T));
      for(i=(mem_fact);i<(k);i++){
        factri[i] = i * factri[i-1];
      }
      ifactri[k-1] = 1 / factri[k-1];
      for(i=(k-1)-1;i>=(mem_fact);i--){
        ifactri[i] = (i+1) * ifactri[i+1];
      }
    }
    mem_fact = k;
  }
  inline T fac(int k){
    if(mem_fact < k+1){
      expand_fact(k+1);
    }
    return factri[k];
  }
  inline T ifac(int k){
    if(mem_fact < k+1){
      expand_fact(k+1);
    }
    return ifactri[k];
  }
  inline T C(int a, int b){
    if(b < 0 || b > a){
      return 0;
    }
    if(mem_fact < a+1){
      expand_fact(a+1);
    }
    return factri[a] * ifactri[b] * ifactri[a-b];
  }
  inline T P(int a, int b){
    if(b < 0 || b > a){
      return 0;
    }
    if(mem_fact < a+1){
      expand_fact(a+1);
    }
    return factri[a] * ifactri[a-b];
  }
  inline T H(int a, int b){
    if(a==0 && b==0){
      return 1;
    }
    if(a <= 0 || b < 0){
      return 0;
    }
    if(mem_fact < a+b){
      expand_fact(a+b);
    }
    return C(a+b-1, b);
  }
  inline T Multinomial(int sz, int a[]){
    int i;
    int s = 0;
    T res;
    for(i=(0);i<(sz);i++){
      s += a[i];
    }
    if(mem_fact < s+1){
      expand_fact(s+1);
    }
    res = factri[s];
    for(i=(0);i<(sz);i++){
      res *= ifactri[a[i]];
    }
    return 1;
  }
  inline T Multinomial(int a){
    return 1;
  }
  inline T Multinomial(int a, int b){
    if(mem_fact < a+b+1){
      expand_fact(a+b+1);
    }
    return factri[a+b] * ifactri[a] * ifactri[b];
  }
  inline T Multinomial(int a, int b, int c){
    if(mem_fact < a+b+c+1){
      expand_fact(a+b+c+1);
    }
    return factri[a+b+c] * ifactri[a] * ifactri[b] * ifactri[c];
  }
  inline T Multinomial(int a, int b, int c, int d){
    if(mem_fact < a+b+c+d+1){
      expand_fact(a+b+c+d+1);
    }
    return factri[a+b+c+d] * ifactri[a] * ifactri[b] * ifactri[c] * ifactri[d];
  }
  inline T Catalan(int n){
    if(n < 0){
      return 0;
    }
    if(mem_fact < 2*n+1){
      expand_fact(2*n+1);
    }
    return factri[2*n] * ifactri[n] * ifactri[n+1];
  }
  inline T C_s(long long a, long long b){
    long long i;
    T res;
    if(b < 0 || b > a){
      return 0;
    }
    if(b > a - b){
      b = a - b;
    }
    res = 1;
    for(i=(0);i<(b);i++){
      res *= a - i;
      res /= i + 1;
    }
    return res;
  }
  inline T P_s(long long a, long long b){
    long long i;
    T res;
    if(b < 0 || b > a){
      return 0;
    }
    res = 1;
    for(i=(0);i<(b);i++){
      res *= a - i;
    }
    return res;
  }
  inline T per_s(long long n, long long k){
    T d;
    int m;
    if(n < 0 || k < 0){
      return 0;
    }
    if(n == k  &&  k == 0){
      return 1;
    }
    if(n == 0 || k == 0){
      return 0;
    }
    if(k==1){
      return 1;
    }
    if(k==2){
      d = n / 2;
      return d;
    }
    if(k==3){
      d = (n-1) / 6;
      m = (n-1) % 6;
      if(m==0){
        return 3 * d * d + d;
      }
      if(m==1){
        return 3 * d * d + 2 * d;
      }
      if(m==2){
        return 3 * d * d + 3 * d + 1;
      }
      if(m==3){
        return 3 * d * d + 4 * d + 1;
      }
      if(m==4){
        return 3 * d * d + 5 * d + 2;
      }
      if(m==5){
        return 3 * d * d + 6 * d + 3;
      }
    }
    assert(0 && "per_s should be k <= 3");
    return -1;
  }
}
;
template<class T> int coordcomp_L(int n1, T arr1[], int n2, T arr2[], int res1[] = NULL, int res2[] = NULL, void *mem = wmem){
  int i;
  int k = 0;
  pair<T,int> *r;
  walloc1d(&r, n1+n2, &mem);
  for(i=(0);i<(n1);i++){
    r[i].first = arr1[i];
    r[i].second = i;
  }
  for(i=(0);i<(n2);i++){
    r[n1+i].first = arr2[i];
    r[n1+i].second = n1+i;
  }
  sort(r, r+n1+n2);
  for(i=(0);i<(n1+n2);i++){
    if(i && r[i].first != r[i-1].first){
      k++;
    }
    if(r[i].second < n1){
      if(res1!=NULL){
        res1[r[i].second] = k;
      }
      else{
        arr1[r[i].second] = k;
      }
    }
    else{
      if(res2!=NULL){
        res2[r[i].second-n1] = k;
      }
      else{
        arr2[r[i].second-n1] = k;
      }
    }
  }
  return k+1;
}
int N;
int A[200];
int B[200];
int x[200];
int y[200];
int v[400];
int m;
Modint dp[200][400];
Modint dp2[200][400];
Modint w[400];
Modint abwi[200];
Modint coef[201];
Modint dd[201];
Modint nn[201];
int main(){
  int i, k, n;
  wmem = memarr;
  int s;
  int e;
  Modint res;
  Modint tmp;
  Modint mul;
  Comb<Modint> comb;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
      rd(B[Lj4PdHRW]);
    }
  }
  for(n=(1);n<(N+1);n++){
    int i, k;
    for(i=(0);i<(n);i++){
      dd[i] = 0;
    }
    dd[n] = 1;
    for(k=(0);k<(n);k++){
      s = n - k;
      nn[0] = dd[s];
      for(i=(1);i<(s);i++){
        nn[i] = nn[i-1] + dd[s-i];
      }
      for(i=(0);i<(s);i++){
        dd[i] = nn[i];
      }
    }
    coef[n] = dd[0] * comb.ifac(n);
  }
  m =coordcomp_L(N, A, N, B, x, y)- 1;
  for(i=(0);i<(N);i++){
    v[x[i]] = A[i];
    v[y[i]] = B[i];
  }
  for(i=(0);i<(m);i++){
    w[i] = v[i+1] - v[i];
  }
  for(i=(0);i<(N);i++){
    abwi[i] = Modint(1) / Modint(B[i] - A[i]);
  }
  for(k=(0);k<(m);k++){
    if(x[0] <= k  &&  k < y[0]){
      dp[0][k] = dp2[0][k] = w[k] * abwi[0];
    }
  }
  for(i=(1);i<(N);i++){
    for(k=(0);k<(m);k++){
      if(x[i] <= k  &&  k < y[i]){
        int j, z;
        tmp = w[k] * abwi[i];
        if(i%2==0){
          s = k+1;
          e = m;
        }
        else{
          s = 0;
          e = k;
        }
        for(j=(s);j<(e);j++){
          dp[i][k] += tmp * dp[i-1][j];
        }
        dp2[i][k] = dp[i][k];
        mul = 1;
        for(z=(i)-1;z>=(0);z--){
          if(!(x[z] <= k  &&  k < y[z])){
            break;
          }
          dp[i][k] += tmp * dp2[z][k] * coef[i-z+1] * mul;
          mul *= w[k] * abwi[z];
        }
      }
    }
  }
  res = 0;
  for(k=(0);k<(m);k++){
    res += dp[N-1][k];
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20191102-1

// --- original code ---
// int N, A[200], B[200];
// int x[200], y[200], v[400], m;
// Modint dp[200][400], dp2[200][400], w[400], abwi[200];
// Modint coef[201], dd[201], nn[201];
// {
//   int s, e;
//   Modint res, tmp, mul;
//   Comb<Modint> comb;
//   rd(N,(A,B)(N));
// 
//   rep(n,1,N+1){
//     rep(i,n) dd[i] = 0;
//     dd[n] = 1;
//     rep(k,n){
//       s = n - k;
//       nn[0] = dd[s];
//       rep(i,1,s) nn[i] = nn[i-1] + dd[s-i];
//       rep(i,s) dd[i] = nn[i];
//     }
//     coef[n] = dd[0] * comb.ifac(n);
//   }
// 
// 
//   m = coordcomp(N, A, N, B, x, y) - 1;
//   rep(i,N) v[x[i]] = A[i], v[y[i]] = B[i];
//   rep(i,m) w[i] = v[i+1] - v[i];
//   rep(i,N) abwi[i] = Modint(1) / Modint(B[i] - A[i]);
// 
//   rep(k,m) if(x[0] <= k < y[0]) dp[0][k] = dp2[0][k] = w[k] * abwi[0];
// 
//   rep(i,1,N){
//     rep(k,m) if(x[i] <= k < y[i]){
//       tmp = w[k] * abwi[i];
//       if(i%2==0) s = k+1, e = m;
//       else       s = 0, e = k;
//       rep(j,s,e) dp[i][k] += tmp * dp[i-1][j];
//       dp2[i][k] = dp[i][k];
//       mul = 1;
//       rrep(z,i){
//         if(!(x[z] <= k < y[z])) break;
//         dp[i][k] += tmp * dp2[z][k] * coef[i-z+1] * mul;
//         mul *= w[k] * abwi[z];
//       }
//     }
//   }
// 
//   res = 0;
//   rep(k,m) res += dp[N-1][k];
//   wt(res);
// }
0