結果

問題 No.1202 お菓子の食べ方
ユーザー LayCurseLayCurse
提出日時 2020-08-28 22:16:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 10,778 bytes
コンパイル時間 3,443 ms
コンパイル使用メモリ 232,540 KB
実行使用メモリ 27,764 KB
最終ジャッジ日時 2024-11-14 01:04:14
合計ジャッジ時間 7,786 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
19,088 KB
testcase_01 AC 83 ms
17,216 KB
testcase_02 AC 97 ms
23,920 KB
testcase_03 AC 95 ms
21,652 KB
testcase_04 AC 92 ms
20,108 KB
testcase_05 AC 94 ms
23,584 KB
testcase_06 AC 92 ms
21,832 KB
testcase_07 AC 94 ms
21,060 KB
testcase_08 AC 91 ms
20,732 KB
testcase_09 AC 96 ms
23,572 KB
testcase_10 AC 9 ms
12,260 KB
testcase_11 AC 24 ms
15,360 KB
testcase_12 AC 53 ms
16,708 KB
testcase_13 AC 15 ms
14,824 KB
testcase_14 AC 57 ms
21,300 KB
testcase_15 AC 22 ms
16,892 KB
testcase_16 AC 48 ms
22,568 KB
testcase_17 AC 32 ms
20,668 KB
testcase_18 AC 24 ms
15,428 KB
testcase_19 AC 27 ms
22,324 KB
testcase_20 AC 36 ms
13,212 KB
testcase_21 AC 52 ms
23,492 KB
testcase_22 AC 33 ms
24,824 KB
testcase_23 AC 48 ms
25,384 KB
testcase_24 AC 49 ms
25,828 KB
testcase_25 AC 10 ms
10,440 KB
testcase_26 AC 56 ms
23,988 KB
testcase_27 AC 53 ms
27,764 KB
testcase_28 AC 25 ms
17,896 KB
testcase_29 AC 40 ms
26,044 KB
testcase_30 AC 2 ms
7,620 KB
testcase_31 AC 2 ms
7,620 KB
testcase_32 AC 2 ms
6,816 KB
testcase_33 AC 2 ms
6,820 KB
testcase_34 AC 3 ms
6,820 KB
testcase_35 AC 3 ms
7,624 KB
testcase_36 AC 2 ms
7,620 KB
testcase_37 AC 3 ms
7,496 KB
testcase_38 AC 3 ms
7,620 KB
testcase_39 AC 3 ms
6,820 KB
testcase_40 AC 99 ms
25,700 KB
testcase_41 AC 99 ms
25,820 KB
testcase_42 AC 100 ms
25,988 KB
testcase_43 AC 98 ms
26,624 KB
testcase_44 AC 99 ms
26,804 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'T Comb<T>::C(int, int) [with T = Modint]',
    inlined from 'T Comb<T>::C(int, int) [with T = Modint]' at main.cpp:345:12,
    inlined from 'int main()' at main.cpp:520:16:
main.cpp:352:31: warning: 'c.Comb<Modint>::ifactri' may be used uninitialized [-Wmaybe-uninitialized]
  352 |     return factri[a] * ifactri[b] * ifactri[a-b];
      |                        ~~~~~~~^
main.cpp: In function 'int main()':
main.cpp:506:16: note: 'c.Comb<Modint>::ifactri' was declared here
  506 |   Comb<Modint> c;
      |                ^
In member function 'T Comb<T>::C(int, int) [with T = Modint]',
    inlined from 'T Comb<T>::C(int, int) [with T = Modint]' at main.cpp:345:12,
    inlined from 'int main()' at main.cpp:520:16:
main.cpp:352:22: warning: 'c.Comb<Modint>::factri' may be used uninitialized [-Wmaybe-uninitialized]
  352 |     return factri[a] * ifactri[b] * ifactri[a-b];
      |            ~~~~~~~~~~^~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:506:16: note: 'c.Comb<Modint>::factri' was declared here
  506 |   Comb<Modint> c;
      |                ^

ソースコード

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 %= (int)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 %= (int)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 int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_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){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_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;
  }
}
;
int N;
int M;
int A[1000][1000];
int main(){
  wmem = memarr;
  int i;
  int j;
  int k;
  int x;
  Modint res = 0;
  Modint tmp;
  Comb<Modint> c;
  rd(N);
  rd(M);
  {
    int Lj4PdHRW;
    int KL2GvlyY;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      for(KL2GvlyY=(0);KL2GvlyY<(M);KL2GvlyY++){
        rd(A[Lj4PdHRW][KL2GvlyY]);
      }
    }
  }
  for(i=(0);i<(N);i++){
    for(j=(0);j<(M);j++){
      tmp = c.C(i+j, i) * c.H(i+j+1, A[i][j]-1);
      res += tmp;
      if(i==N-1){
        k =0;
      }
      else{
        k =A[i+1][j];
      }
      tmp = c.C(i+j, i) * (c.H(i+j+2, A[i][j]-1) - c.H(i+j+2, k-1));
      res += tmp;
      if(j==M-1){
        k =0;
      }
      else{
        k =A[i][j+1];
      }
      tmp = c.C(i+j, i) * (c.H(i+j+2, A[i][j]-1) - c.H(i+j+2, k-1));
      res += tmp;
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200813-1 [beta]

// --- original code ---
// int N, M, A[1000][1000];
// {
//   int i, j, k, x;
//   Modint res = 0, tmp;
//   Comb<Modint> c;
// 
//   rd(N,M,A(N,M));
//   rep(i,N) rep(j,M){
//     tmp = c.C(i+j, i) * c.H(i+j+1, A[i][j]-1);
//     res += tmp;
// 
//     k = if[i==N-1, 0, A[i+1][j]];
//     tmp = c.C(i+j, i) * (c.H(i+j+2, A[i][j]-1) - c.H(i+j+2, k-1));
//     res += tmp;
// 
//     k = if[j==M-1, 0, A[i][j+1]];
//     tmp = c.C(i+j, i) * (c.H(i+j+2, A[i][j]-1) - c.H(i+j+2, k-1));
//     res += tmp;
//   }
//   wt(res);
// }
0