結果

問題 No.1596 Distance Sum in 2D Plane
ユーザー merlinmerlin
提出日時 2021-07-09 22:36:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 948 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 2,094 ms
コンパイル使用メモリ 74,108 KB
実行使用メモリ 62,132 KB
最終ジャッジ日時 2023-09-14 09:56:45
合計ジャッジ時間 15,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
55,588 KB
testcase_01 AC 64 ms
55,364 KB
testcase_02 AC 946 ms
61,664 KB
testcase_03 AC 939 ms
61,712 KB
testcase_04 AC 938 ms
62,128 KB
testcase_05 AC 948 ms
61,804 KB
testcase_06 AC 943 ms
62,132 KB
testcase_07 AC 932 ms
61,820 KB
testcase_08 AC 934 ms
61,684 KB
testcase_09 AC 929 ms
61,912 KB
testcase_10 AC 936 ms
61,440 KB
testcase_11 AC 874 ms
61,964 KB
testcase_12 AC 881 ms
61,768 KB
testcase_13 AC 879 ms
61,700 KB
testcase_14 AC 63 ms
55,380 KB
testcase_15 AC 61 ms
55,120 KB
testcase_16 AC 62 ms
55,492 KB
testcase_17 AC 63 ms
55,220 KB
testcase_18 AC 62 ms
55,340 KB
testcase_19 AC 62 ms
55,220 KB
権限があれば一括ダウンロードができます

ソースコード

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