結果

問題 No.1202 お菓子の食べ方
ユーザー PCTprobabilityPCTprobability
提出日時 2020-07-28 14:42:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,120 bytes
コンパイル時間 2,075 ms
コンパイル使用メモリ 171,716 KB
実行使用メモリ 58,240 KB
最終ジャッジ日時 2024-04-25 11:36:56
合計ジャッジ時間 17,670 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 94 ms
50,432 KB
testcase_31 AC 98 ms
50,432 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 92 ms
50,432 KB
testcase_35 AC 92 ms
50,304 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 91 ms
50,304 KB
testcase_39 AC 98 ms
50,304 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

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));
 
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> o.at(i).at(j);
    }
  }
 mint ans=0;
  mint p=0;
  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+((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))))/(i+j+1));
      }
      else{
        p=p+COM(o.at(i).at(j)+i+j,o.at(i).at(j))*o.at(i).at(j)/(i+j+1);
      }
      if(i!=n-1){
      p=p+COM(i+j,i)*((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))))/(i+j+1));
      }
      else{
        p=p+COM(o.at(i).at(j)+i+j,o.at(i).at(j))*o.at(i).at(j)/(i+j+1);
      }
      ans=ans+p*COM(i+j,i);
    }
  }
                         cout<<ans<<endl;
}
0