結果
| 問題 |
No.611 Day of the Mountain
|
| コンテスト | |
| ユーザー |
夕叢霧香(ゆうむらきりか)
|
| 提出日時 | 2017-12-11 21:49:02 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 440 ms / 2,017 ms |
| コード長 | 4,248 bytes |
| コンパイル時間 | 2,174 ms |
| コンパイル使用メモリ | 78,004 KB |
| 実行使用メモリ | 42,448 KB |
| 最終ジャッジ日時 | 2024-11-30 21:02:25 |
| 合計ジャッジ時間 | 5,467 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 9 |
ソースコード
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;
}
}
}
夕叢霧香(ゆうむらきりか)