結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー cureskolcureskol
提出日時 2021-07-09 21:35:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,035 bytes
コンパイル時間 1,465 ms
コンパイル使用メモリ 164,608 KB
実行使用メモリ 81,736 KB
最終ジャッジ日時 2023-09-14 07:58:51
合計ジャッジ時間 6,201 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
81,440 KB
testcase_01 AC 73 ms
81,456 KB
testcase_02 AC 234 ms
81,716 KB
testcase_03 AC 234 ms
81,464 KB
testcase_04 AC 236 ms
81,520 KB
testcase_05 AC 235 ms
81,432 KB
testcase_06 AC 234 ms
81,588 KB
testcase_07 AC 234 ms
81,736 KB
testcase_08 AC 234 ms
81,440 KB
testcase_09 AC 232 ms
81,668 KB
testcase_10 AC 235 ms
81,480 KB
testcase_11 AC 208 ms
81,684 KB
testcase_12 AC 207 ms
81,456 KB
testcase_13 AC 208 ms
81,480 KB
testcase_14 AC 74 ms
81,552 KB
testcase_15 AC 73 ms
81,468 KB
testcase_16 AC 73 ms
81,432 KB
testcase_17 AC 75 ms
81,432 KB
testcase_18 AC 73 ms
81,472 KB
testcase_19 AC 73 ms
81,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

using ll=long long;
const ll MOD=1e9+7;

ll pw(ll n,int k){
  assert(k>=0);
  ll res=1;
  while(k){
    if(k&1)(res*=n)%=MOD;
    (n*=n)%=MOD;
    k>>=1;
  }
  return res;
}
ll Factorial[5000000],Finverse[5000000];
inline void Cinit(){
  Factorial[0]=1;
  for(int i=1;i<5e6;i++)Factorial[i]=(Factorial[i-1]*i)%MOD;
  Finverse[4999999]=pw(Factorial[4999999],MOD-2);
  for(int i=4999998;i>=0;i--)Finverse[i]=(i+1)*Finverse[i+1]%MOD;
}
ll nCk(int n,int k){
  if(n<k)return 0;if(k<0)return 0;
  if(!Factorial[0])Cinit();
  ll res=Factorial[n];
  (res*=Finverse[k])%=MOD;
  (res*=Finverse[n-k])%=MOD;
  return res;
}

int main(){
  int n,m;cin>>n>>m;
  ll ans=nCk(2*n,n)*2*n%MOD;
  while(m--){
    int t,x,y;cin>>t>>x>>y;
    if(t==1){
      ans-=nCk(x+y,x)*nCk(n-(x+1)+n-y,n-y)%MOD;
    }
    else{
      ans-=nCk(x+y,x)*nCk(n-(y+1)+n-x,n-x)%MOD;
    }
    ans+=MOD;
    if(ans>=MOD)ans-=MOD;
  }
  cout<<ans<<endl;
}
0