結果

問題 No.907 Continuous Kadomatu
ユーザー LayCurseLayCurse
提出日時 2019-10-12 03:24:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 2,000 ms
コード長 8,492 bytes
コンパイル時間 2,940 ms
コンパイル使用メモリ 223,648 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-26 09:31:02
合計ジャッジ時間 5,260 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 10 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 5 ms
5,248 KB
testcase_08 AC 8 ms
5,248 KB
testcase_09 AC 5 ms
5,248 KB
testcase_10 AC 14 ms
5,248 KB
testcase_11 AC 21 ms
5,248 KB
testcase_12 AC 49 ms
5,248 KB
testcase_13 AC 56 ms
5,248 KB
testcase_14 AC 59 ms
5,248 KB
testcase_15 AC 57 ms
5,248 KB
testcase_16 AC 63 ms
5,248 KB
testcase_17 AC 66 ms
5,248 KB
testcase_18 AC 58 ms
5,248 KB
testcase_19 AC 65 ms
5,248 KB
testcase_20 AC 7 ms
5,248 KB
testcase_21 AC 5 ms
5,248 KB
testcase_22 AC 5 ms
5,248 KB
testcase_23 AC 404 ms
5,248 KB
testcase_24 AC 403 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 2 ms
5,248 KB
testcase_28 AC 2 ms
5,248 KB
testcase_29 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

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{
  static unsigned md;
  unsigned val;
  modint(){
  }
  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);
  }
  void setmod(unsigned m){
    md = m;
  }
  unsigned ord(unsigned a){
    return a%md;
  }
  unsigned ord(int a){
    a %= md;
    if(a < 0){
      a += md;
    }
    return a;
  }
  unsigned ord(unsigned long long a){
    return a%md;
  }
  unsigned ord(long long a){
    a %= md;
    if(a < 0){
      a += md;
    }
    return a;
  }
  unsigned get(){
    return val;
  }
  modint &operator+=(modint a){
    val += a.val;
    if(val >= md){
      val -= md;
    }
    return *this;
  }
  modint &operator-=(modint a){
    if(val < a.val){
      val = val + md - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  modint &operator*=(modint a){
    val = ((unsigned long long)val*a.val)%md;
    return *this;
  }
  modint &operator/=(modint a){
    return *this *= a.inverse();
  }
  modint operator+(modint a){
    return modint(*this)+=a;
  }
  modint operator-(modint a){
    return modint(*this)-=a;
  }
  modint operator*(modint a){
    return modint(*this)*=a;
  }
  modint operator/(modint a){
    return modint(*this)/=a;
  }
  modint operator+(int a){
    return modint(*this)+=modint(a);
  }
  modint operator-(int a){
    return modint(*this)-=modint(a);
  }
  modint operator*(int a){
    return modint(*this)*=modint(a);
  }
  modint operator/(int a){
    return modint(*this)/=modint(a);
  }
  modint operator+(long long a){
    return modint(*this)+=modint(a);
  }
  modint operator-(long long a){
    return modint(*this)-=modint(a);
  }
  modint operator*(long long a){
    return modint(*this)*=modint(a);
  }
  modint operator/(long long a){
    return modint(*this)/=modint(a);
  }
  modint operator-(void){
    modint res;
    if(val){
      res.val=md-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  operator bool(void){
    return val!=0;
  }
  operator int(void){
    return get();
  }
  operator long long(void){
    return get();
  }
  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;
  }
  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;
  }
  bool operator==(int a){
    return ord(a)==val;
  }
  bool operator!=(int a){
    return ord(a)!=val;
  }
}
;
unsigned modint::md;
modint operator+(int a, modint b){
  return modint(a)+=b;
}
modint operator-(int a, modint b){
  return modint(a)-=b;
}
modint operator*(int a, modint b){
  return modint(a)*=b;
}
modint operator/(int a, modint b){
  return modint(a)/=b;
}
modint operator+(long long a, modint b){
  return modint(a)+=b;
}
modint operator-(long long a, modint b){
  return modint(a)-=b;
}
modint operator*(long long a, modint b){
  return modint(a)*=b;
}
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> 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][401];
modint dp2[200][401];
modint coef[201];
modint dd[201];
modint nn[201];
int main(){
  int i, k, n;
  wmem = memarr;
  {
    modint x;
    x.setmod(MD);
  }
  int s;
  int e;
  modint res;
  modint tmp;
  modint mul;
  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];
    for(i=(1);i<(n+1);i++){
      coef[n] /= i;
    }
  }
  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(k=(0);k<(m);k++){
    if(x[0] <= k  &&  k < y[0]){
      dp[0][k] = dp2[0][k] = modint(v[k+1] - v[k]) / modint(B[0] - A[0]);
    }
  }
  for(i=(1);i<(N);i++){
    for(k=(0);k<(m);k++){
      if(x[i] <= k  &&  k < y[i]){
        int j, z;
        tmp = modint(v[k+1] - v[k]) / modint(B[i] - A[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 *= modint(v[k+1] - v[k]) / modint(B[z] - A[z]);
        }
      }
    }
  }
  res = 0;
  for(k=(0);k<(m);k++){
    res += dp[N-1][k];
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20191012-1 [beta]

// --- original code ---
// int N, A[200], B[200];
// int x[200], y[200], v[400], m;
// modint dp[200][401], dp2[200][401];
// modint coef[201], dd[201], nn[201];
// {
//   int s, e;
//   modint res, tmp, mul;
//   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];
//     rep(i,1,n+1) coef[n] /= i;
//   }
// 
// 
//   m = coordcomp(N, A, N, B, x, y) - 1;
//   rep(i,N) v[x[i]] = A[i], v[y[i]] = B[i];
// 
//   rep(k,m) if(x[0] <= k < y[0]) dp[0][k] = dp2[0][k] = modint(v[k+1] - v[k]) / modint(B[0] - A[0]);
// 
// //  rep(i,N) wt("xy",x[i],y[i]);
// 
//   rep(i,1,N){
// //    wt(i,":",dp[i-1](m));
// //    wt(i,":",dp2[i-1](m));
//     rep(k,m) if(x[i] <= k < y[i]){
//       tmp = modint(v[k+1] - v[k]) / modint(B[i] - A[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 *= modint(v[k+1] - v[k]) / modint(B[z] - A[z]);
//       }
//     }
//   }
// //    wt(i,":",dp[i-1](m));
// //    wt(i,":",dp2[i-1](m));
// 
//   res = 0;
//   rep(k,m) res += dp[N-1][k];
//   wt(res);
// }
0