結果

問題 No.498 ワープクリスタル (給料日編)
ユーザー btkbtk
提出日時 2017-03-27 20:37:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 215 ms / 2,000 ms
コード長 4,050 bytes
コンパイル時間 4,675 ms
コンパイル使用メモリ 74,756 KB
実行使用メモリ 68,484 KB
最終ジャッジ日時 2023-09-20 17:20:35
合計ジャッジ時間 6,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
66,180 KB
testcase_01 AC 65 ms
66,448 KB
testcase_02 AC 65 ms
66,456 KB
testcase_03 AC 65 ms
66,468 KB
testcase_04 AC 208 ms
67,316 KB
testcase_05 AC 64 ms
66,492 KB
testcase_06 AC 191 ms
68,024 KB
testcase_07 AC 177 ms
67,444 KB
testcase_08 AC 204 ms
67,976 KB
testcase_09 AC 176 ms
67,704 KB
testcase_10 AC 63 ms
66,736 KB
testcase_11 AC 86 ms
67,720 KB
testcase_12 AC 63 ms
66,404 KB
testcase_13 AC 79 ms
67,204 KB
testcase_14 AC 65 ms
67,144 KB
testcase_15 AC 67 ms
66,488 KB
testcase_16 AC 63 ms
66,616 KB
testcase_17 AC 79 ms
66,800 KB
testcase_18 AC 201 ms
68,024 KB
testcase_19 AC 206 ms
68,284 KB
testcase_20 AC 210 ms
67,556 KB
testcase_21 AC 215 ms
66,400 KB
testcase_22 AC 191 ms
68,484 KB
testcase_23 AC 207 ms
67,340 KB
testcase_24 AC 213 ms
68,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

/**
 * Created by btk on 2017/03/16.
 */
public class Main {

    static FastScanner in;
    static PrintWriter out;

    static int Gr,Gc;
    static int K;
    static int[] R,C,N;


    static void init(){
        Gr = in.nextInt();
        Gc = in.nextInt();
        K = in.nextInt();
        R = new int[5];
        C = new int[5];
        N = new int[5];
        for (int i = 0; i < 5; i++) {
            N[i] = 0;
        }
        for (int i = 0; i < K; i++) {
            R[i] = in.nextInt();
            C[i] = in.nextInt();
            N[i] = in.nextInt();
        }
    }
    final static int mod = (int)1e9+7;
    final static int M = 1 << 20;
    static long[] dp;
    static int[] r,c;
    static long res = 0;
    static int pos(int[] num) {
        int ret=0;
        for (int i = 4; i >=0; i--) {
            ret<<=4;
            ret+=num[i];
        }
        return ret;
    }
    static void rec(int now){
        if(dp[now]!=-1)return;
        //out.println(now);
        int lb = 1;
        int mask = 15;
        dp[now] = 0;
        for (int i = 0; i < 5; i++,mask<<=4,lb<<=4) {
            if((now&mask)>0) {
                int nxt = now -lb;
                rec(nxt);
                r[now] = r[nxt] + R[i];
                c[now] = c[nxt] + C[i];
                dp[now] += dp[nxt];
            }
        }
        dp[now] %= mod;
        if(r[now] == Gr &&c[now]==Gc)
            res+=dp[now];
    }
    static void solve(){
        dp = new long[M];
        r = new int[M];
        c = new int[M];
        for (int i = 0; i < M; i++) {
            dp[i] = -1 ;
        }
        dp[0] = 1;
        r[0] = 0;
        c[0] = 0;
        if(Gr==0&&Gc==0)res++;
        rec(pos(N));
        out.println(res%mod);
    }
    public static void main(String[] args){
        in = new FastScanner();
        out = new PrintWriter(System.out);
        init();
        solve();
        out.close();
    }
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}
0