結果

問題 No.612 Move on grid
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-12 02:34:27
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 4,436 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 74,728 KB
実行使用メモリ 66,192 KB
最終ジャッジ日時 2023-08-20 17:44:13
合計ジャッジ時間 7,071 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
50,828 KB
testcase_01 AC 53 ms
50,824 KB
testcase_02 AC 54 ms
50,680 KB
testcase_03 AC 619 ms
54,148 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
    static final long MOD=1000000007;
    static final int N=2000;
    static long[] fact;
    static long[] invfact;
    static void initialize() {
	fact = new long[N];
	invfact = new long[N];
	fact[0] = invfact[0] = 1;
	for (int i = 1; i < N; ++i) {
	    fact[i] = (i * fact[i - 1]) % MOD;
	    invfact[i] = powerMod(fact[i], MOD - 2);
	}
    }
    static long powerMod(long x, long exponent) {
	long prod = 1;
	for (int i = 63; i >= 0; --i) {
	    prod = (prod * prod) % MOD;
	    if ((exponent & 1L << i) != 0) {
		prod = (prod * x) % MOD;
	    }
	}
	return prod;
    }
    static long comb(int x, int y) {
	if (x < 0) {
	    return 0;
	}
	if (y < 0 || y > x) {
	    return 0;
	}
	long r= (fact[x] * powerMod((fact[x - y] * fact[y]) % MOD, MOD - 2)) % MOD;
	return r;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int t=sc.nextInt();
        int[] a=new int[3];
        for(int i=0;i<3;++i)a[i]=sc.nextInt();
        int d=sc.nextInt();
        int e=sc.nextInt();
        if(a[2]<0){
            for(int i=0;i<3;++i)a[i]=-a[i];
            int tmp=d;
            d=-e;
            e=-tmp;
        }
        initialize();
        long[][]prec=new long[t+1][2*t+2];
        for(int i=0;i<=t;++i){
            for(int z=-t;z<=t;++z){
                int az=Math.abs(z);
                if((az+i)%2!=0)continue;
                int j=(i-z)/2;
                prec[i][z+t+1]=j>=0&&j+z>=0?invfact[j]*invfact[j+z]%MOD:0;
            }
            for(int j=0;j<2*t+1;++j)
                prec[i][j+1]=(prec[i][j+1]+prec[i][j])%MOD;
        }
        long wa=0;
        for(int x=-t;x<=t;++x){
            int ax=Math.abs(x);
            int tx=t-ax;
            for(int y=-tx;y<=tx;++y){
                int ay=Math.abs(y);
                int ty=tx-ay;
                long[]d2=new long[t+1];
                for(int i=ax+ay;i<=t;++i){
                    if((i+ax+ay)%2==0){
                        d2[i]=comb(i,(i-ax-ay)/2)*comb(i,(i-ax+ay)/2)%MOD;
                        d2[i]=d2[i]*invfact[i]%MOD;
                    }
                }
                int zMin=-ty,zMax=ty;
                if(a[2]!=0){
                    zMin=Math.max(zMin,(d-a[0]*x-a[1]*y+100000*a[2]+a[2]-1)/a[2]-100000);
                    zMax=Math.min(zMax,(e-a[0]*x-a[1]*y+100000*a[2])/a[2]-100000);
                }else if(d-a[0]*x-a[1]*y>0||e-a[0]*x-a[1]*y<0)
                    continue;
                if(zMin>zMax)continue;
                //System.err.println("x = "+x+" y = "+y);
                //System.err.println("zMin = "+zMin+" zMax = "+zMax);
                //System.err.println(Arrays.toString(d2));
                long com=fact[t];
                long tmp=0,tmp2=0;
                for(int i=0;i<=t;++i){
                    long adden=prec[i][t+zMax+1]-prec[i][t+zMin]+MOD;
                    adden%=MOD;
                    adden=adden*d2[t-i]%MOD;
                    tmp=(tmp+adden)%MOD;
                }
                wa=(wa+com*tmp)%MOD;
            }
        }
        out.println(wa);
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
0