結果

問題 No.611 Day of the Mountain
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-11 21:49:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 427 ms / 2,017 ms
コード長 4,248 bytes
コンパイル時間 1,762 ms
コンパイル使用メモリ 74,228 KB
実行使用メモリ 56,000 KB
最終ジャッジ日時 2023-08-20 17:33:53
合計ジャッジ時間 4,823 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 409 ms
55,972 KB
testcase_01 AC 378 ms
55,660 KB
testcase_02 AC 427 ms
53,636 KB
testcase_03 AC 379 ms
56,000 KB
testcase_04 AC 40 ms
49,440 KB
testcase_05 AC 40 ms
49,508 KB
testcase_06 AC 41 ms
49,504 KB
testcase_07 AC 40 ms
49,640 KB
testcase_08 AC 41 ms
49,616 KB
testcase_09 AC 38 ms
49,380 KB
testcase_10 AC 38 ms
49,624 KB
testcase_11 AC 38 ms
49,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
    static final long MOD=201712111;
    static final long I9=powerMod(9,MOD-2);
    static long[][][]dp2;
    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;
    }
    Main(int h,int w,int[][]b){
        int[][]dp=new int[h][w];
        dp[0][0]=b[0][0]==-1?1:b[0][0];
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j){
                if(i+j==0)continue;
                int c=b[i][j]==-1?1:b[i][j];
                int m=Integer.MAX_VALUE;
                if(i>0)m=Math.min(m,dp[i-1][j]+c);
                if(j>0)m=Math.min(m,dp[i][j-1]+c);
                dp[i][j]=m;
            }
        int indet=0;
        for(int[]r:b)
            for(int rr:r)
                if(rr==-1)indet++;
        long[][]dp2=new long[2][1<<w];
        dp2[0][0]=1;
        dp2[0][1]=b[0][0]==-1?I9:1;
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j){
                if(i+j==0)continue;
                int c=b[i][j]==-1?1:b[i][j];
                long factor=b[i][j]==-1?I9:1;
                int u=(i*w+j)%2;
                int pr=1-u;
                for(int s=0;s<1<<w;++s){
                    if(s%2==0){
                        dp2[u][s]=dp2[pr][s/2];
                        continue;
                    }
                    boolean up=i>0&&dp[i][j]==dp[i-1][j]+c;
                    boolean lft=j>0&&dp[i][j]==dp[i][j-1]+c;
                    int lftS=s>>1|1;
                    int upS=s>>1|1<<(w-1);
                    long ans;
                    if(lft&&up)
                        ans=(dp2[pr][lftS]+dp2[pr][upS]-dp2[pr][lftS|upS]+MOD)%MOD;
                    else if(lft)
                        ans=dp2[pr][lftS];
                    else if(up)
                        ans=dp2[pr][upS];
                    else
                        throw new Error();
                    dp2[u][s]=ans*factor%MOD;
                }
            }
        out.println(dp[h-1][w-1]);
        long ans=powerMod(9,indet);
        ans=ans*dp2[(h*w-1)%2][1]%MOD;
        out.println(ans);
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int h=sc.nextInt();
        int w=sc.nextInt();
        String[]s=new String[h];
        for(int i=0;i<h;++i)s[i]=sc.next();
        int[][]b;
        b=new int[h][w];
        for(int i=0;i<h;++i)
            for(int j=0;j<w;++j){
                char c=s[i].charAt(j);
                b[i][j]=c=='?'?-1:c-'0';
            }
        if(h<w){
            int[][]c=new int[w][h];
            for(int i=0;i<h;++i)
                for(int j=0;j<w;++j)
                    c[j][i]=b[i][j];
            b=c;
            int t=h;
            h=w;
            w=t;
        }
        new Main(h,w,b);
        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