結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー merlin
提出日時 2021-07-09 22:36:36
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,101 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 77,004 KB
実行使用メモリ 52,144 KB
最終ジャッジ日時 2024-07-01 17:20:54
合計ジャッジ時間 16,157 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        int i,N=500000;
        f=new long[N]; f[0]=1;
        for(i=1;i<N;i++) f[i]=f[i-1]*i%M;

        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]);
        long ans=ncr(2*n,n)*n*2%M;

        for(i=0;i<m;i++)
        {
            s=bu.readLine().split(" ");
            int t=Integer.parseInt(s[0]),x=Integer.parseInt(s[1]),y=Integer.parseInt(s[2]);
            if((t==1 && x==n) || (t==2 && y==n)) continue;
            int x2=x,y2=y;
            if(t==1) x2++;
            else y2++;
            long paths=ncr(2*n-x2-y2,n-x2)*ncr(x+y,x)%M;
            ans=(ans-paths+M)%M;
        }
        System.out.println(ans);
    }

    static long f[],M=(int)1e9+7;
    static long ncr(int n,int r)
    {
        if(r>n || n<0) return 1;
        return f[n]*power(f[r],M-2)%M*power(f[n-r],M-2)%M;
    }

    static long power(long a,long b)
    {
        long res=1;
        while(b!=0)
        {
            if(b%2==1) res=res*a%M;
            b>>=1;
            a=a*a%M;
        }
        return res;
    }
}
0