結果

問題 No.1202 お菓子の食べ方
ユーザー PCTprobabilityPCTprobability
提出日時 2020-07-28 15:01:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 824 ms / 2,000 ms
コード長 3,161 bytes
コンパイル時間 1,846 ms
コンパイル使用メモリ 174,008 KB
実行使用メモリ 58,240 KB
最終ジャッジ日時 2024-04-25 11:37:28
合計ジャッジ時間 22,939 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 780 ms
57,984 KB
testcase_01 AC 795 ms
58,240 KB
testcase_02 AC 815 ms
57,984 KB
testcase_03 AC 787 ms
57,984 KB
testcase_04 AC 786 ms
58,112 KB
testcase_05 AC 784 ms
57,984 KB
testcase_06 AC 774 ms
58,112 KB
testcase_07 AC 774 ms
58,112 KB
testcase_08 AC 823 ms
57,984 KB
testcase_09 AC 824 ms
58,112 KB
testcase_10 AC 88 ms
50,688 KB
testcase_11 AC 170 ms
51,456 KB
testcase_12 AC 445 ms
54,272 KB
testcase_13 AC 100 ms
50,688 KB
testcase_14 AC 423 ms
54,144 KB
testcase_15 AC 81 ms
50,688 KB
testcase_16 AC 308 ms
52,736 KB
testcase_17 AC 184 ms
51,712 KB
testcase_18 AC 174 ms
51,456 KB
testcase_19 AC 93 ms
50,816 KB
testcase_20 AC 343 ms
52,992 KB
testcase_21 AC 350 ms
53,376 KB
testcase_22 AC 110 ms
50,816 KB
testcase_23 AC 274 ms
52,480 KB
testcase_24 AC 278 ms
52,736 KB
testcase_25 AC 101 ms
50,816 KB
testcase_26 AC 371 ms
53,632 KB
testcase_27 AC 307 ms
52,736 KB
testcase_28 AC 139 ms
51,328 KB
testcase_29 AC 187 ms
51,840 KB
testcase_30 AC 66 ms
50,304 KB
testcase_31 AC 70 ms
50,432 KB
testcase_32 AC 68 ms
50,304 KB
testcase_33 AC 67 ms
50,560 KB
testcase_34 AC 68 ms
50,432 KB
testcase_35 AC 69 ms
50,432 KB
testcase_36 AC 68 ms
50,432 KB
testcase_37 AC 71 ms
50,432 KB
testcase_38 AC 70 ms
50,560 KB
testcase_39 AC 70 ms
50,432 KB
testcase_40 AC 799 ms
58,112 KB
testcase_41 AC 809 ms
57,984 KB
testcase_42 AC 794 ms
58,112 KB
testcase_43 AC 771 ms
58,112 KB
testcase_44 AC 774 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));
 
  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+(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