結果

問題 No.1202 お菓子の食べ方
ユーザー 👑 PCTprobabilityPCTprobability
提出日時 2020-08-28 17:04:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 3,548 bytes
コンパイル時間 2,633 ms
コンパイル使用メモリ 209,068 KB
実行使用メモリ 58,160 KB
最終ジャッジ日時 2024-04-26 12:19:54
合計ジャッジ時間 27,155 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 886 ms
57,956 KB
testcase_01 AC 902 ms
58,012 KB
testcase_02 AC 897 ms
58,152 KB
testcase_03 AC 891 ms
58,036 KB
testcase_04 AC 899 ms
57,960 KB
testcase_05 AC 895 ms
58,108 KB
testcase_06 AC 904 ms
57,984 KB
testcase_07 AC 889 ms
57,984 KB
testcase_08 AC 879 ms
58,112 KB
testcase_09 AC 896 ms
57,932 KB
testcase_10 AC 115 ms
50,688 KB
testcase_11 AC 193 ms
51,456 KB
testcase_12 AC 508 ms
54,348 KB
testcase_13 AC 122 ms
50,688 KB
testcase_14 AC 498 ms
54,016 KB
testcase_15 AC 100 ms
50,616 KB
testcase_16 AC 356 ms
52,672 KB
testcase_17 AC 212 ms
51,688 KB
testcase_18 AC 193 ms
51,584 KB
testcase_19 AC 114 ms
50,816 KB
testcase_20 AC 384 ms
53,104 KB
testcase_21 AC 390 ms
53,504 KB
testcase_22 AC 130 ms
50,864 KB
testcase_23 AC 314 ms
52,352 KB
testcase_24 AC 321 ms
52,608 KB
testcase_25 AC 121 ms
50,816 KB
testcase_26 AC 424 ms
53,612 KB
testcase_27 AC 355 ms
52,712 KB
testcase_28 AC 168 ms
51,200 KB
testcase_29 AC 223 ms
51,668 KB
testcase_30 AC 87 ms
50,560 KB
testcase_31 AC 83 ms
50,420 KB
testcase_32 AC 85 ms
50,304 KB
testcase_33 AC 87 ms
50,304 KB
testcase_34 AC 82 ms
50,432 KB
testcase_35 AC 86 ms
50,432 KB
testcase_36 AC 86 ms
50,364 KB
testcase_37 AC 86 ms
50,432 KB
testcase_38 AC 84 ms
50,304 KB
testcase_39 AC 82 ms
50,560 KB
testcase_40 AC 876 ms
57,984 KB
testcase_41 AC 881 ms
58,160 KB
testcase_42 AC 893 ms
58,092 KB
testcase_43 AC 882 ms
57,984 KB
testcase_44 AC 884 ms
57,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <random>
#include <bits/stdc++.h>
using namespace std;
using ull = __int128;
using ll = long long;
#define rep(i,a,b) for(int i=a;i<b;i++)
#include <iostream>
using namespace std;

const int MAX = 2000010;
const int mod = 1000000007;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

class mint {
    long long x;
public:
    mint(long long x=0) : x((x%mod+mod)%mod) {}
    mint operator-() const { 
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }

    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
  friend istream& operator>>(istream& lhs,mint& rhs) noexcept {
        lhs >> rhs.x;
        return lhs;
    }
};


int main() {
    // 前処理
    COMinit();
int n,m;
  cin>>n>>m;
 vector<vector<ll>> o(n, vector<ll>(m));
 assert(1<=n&&n<=1000&&1<=m&&m<=1000);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> o.at(i).at(j);
      assert(1<=o.at(i).at(j)&&o.at(i).at(j)<=1000000);
    }
  }
 mint ans=0;
  mint p=0;
  for(int i=1;i<n;i++){
      for(int j=1;j<m;j++){
          assert(o.at(i).at(j)<=o.at(i).at(j-1)&&o.at(i).at(j)<=o.at(i-1).at(j));
      }
  }
  for(int i=1;i<n;i++){
      assert(o.at(i).at(0)<=o.at(i-1).at(0));
  }
  for(int j=1;j<m;j++){
      assert(o.at(0).at(j)<=o.at(0).at(j-1));
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      p=0;
     p=p+COM(o.at(i).at(j)+i+j-1,o.at(i).at(j)-1);
      if(j!=m-1){
       p=p+(mint(((o.at(i).at(j)*COM(o.at(i).at(j)+i+j,o.at(i).at(j))-(o.at(i).at(j+1)*COM(o.at(i).at(j+1)+i+j,o.at(i).at(j+1))))))/mint((i+j+1)));
      }
      else{
        p=p+mint((COM(o.at(i).at(j)+i+j,o.at(i).at(j))*o.at(i).at(j)))/mint((i+j+1));
      }
      if(i!=n-1){
      p=p+(mint(((o.at(i).at(j)*COM(o.at(i).at(j)+i+j,o.at(i).at(j))-(o.at(i+1).at(j)*COM(o.at(i+1).at(j)+i+j,o.at(i+1).at(j))))))/mint(i+j+1));
      }
      else{
        p=p+mint((COM(o.at(i).at(j)+i+j,o.at(i).at(j))*o.at(i).at(j)))/mint(i+j+1);
      }
      ans=ans+p*COM(i+j,i);
    }
  }
                         cout<<ans<<endl;
}
0