結果

問題 No.1561 connect x connect
ユーザー uwiuwi
提出日時 2020-11-24 00:19:21
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 3,550 bytes
コンパイル時間 5,820 ms
コンパイル使用メモリ 76,448 KB
実行使用メモリ 58,072 KB
最終ジャッジ日時 2023-09-07 13:49:08
合計ジャッジ時間 11,232 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class P5518 {
	static Scanner in;
	static PrintWriter out;
	static String INPUT = "";

	static final int mod = 1000000007;

	static void solve()
	{
		int n = ni(), m = ni();
		if(!(1 <= n && n <= 9))throw new IllegalArgumentException("n");
		if(!(1 <= m && m <= 100))throw new IllegalArgumentException("m");
		if(!(n <= m))throw new IllegalArgumentException("n<=m");
		int[] a = new int[n];
		Arrays.fill(a, -1);
		State ini = new State(a, 1L, 0);
		Map<Long, State> dp = new HashMap<>();
		dp.put(ini.h(), ini);

		//     7654
		// 3210N
		for(int j = 0;j < m+1;j++){
			for(int i = 0;i < n;i++){
				Map<Long, State> ndp = new HashMap<>();
				for(State s : dp.values()){
					// 黒いセルを追加
					{
						int[] nclus = new int[n];
						for(int k = 0;k < n-1;k++)nclus[k+1] = s.clus[k];
						nclus[0] = n;
						if(i > 0 && s.clus[0] != -1){
							for(int k = 1;k < n;k++)if(nclus[k] == s.clus[0])nclus[k] = n;
						}
						if(s.clus[n-1] != -1){
							for(int k = 1;k < n;k++)if(nclus[k] == s.clus[n-1])nclus[k] = n;
						}
						normalize(nclus);
						State ns = new State(nclus, s.val, s.hidden);
						add(ns, ndp);
					}
					// 白いセルを追加
					{
						int[] nclus = new int[n];
						for(int k = 0;k < n-1;k++)nclus[k+1] = s.clus[k];
						nclus[0] = -1;
						int nhidden = s.hidden;
						if(s.clus[n-1] != -1) {
							nhidden++;
							for (int k = 0; k < n - 1; k++) {
								if (s.clus[k] == s.clus[n - 1]) {
									nhidden--;
									break;
								}
							}
						}

						if(nhidden <= 1) {
							normalize(nclus);
							State ns = new State(nclus, s.val, nhidden);
							add(ns, ndp);
						}
					}
				}
				dp = ndp;
			}
		}

		long ans = 0;
		outer:
		for(State s : dp.values()){
			if(s.hidden != 1)continue;
			for(int i = 0;i < n;i++){
				if(s.clus[i] != -1)continue outer;
			}
			ans += s.val;
		}
		out.println(ans%mod);
	}

	static int[] normalize(int[] a)
	{
		int[] label = new int[11];
		Arrays.fill(label, -1);
		int p = 0;
		for(int i = 0;i < a.length;i++){
			if(a[i] == -1)continue;
			if(label[a[i]] == -1)label[a[i]] = p++;
			a[i] = label[a[i]];
		}
		return a;
	}

	static void add(State s, Map<Long, State> dp)
	{
//		dp.merge(s.h(), s, (x, y) -> {
//			y.val += x.val;
//			if(y.val >= mod)y.val -= mod;
//			return y;
//		});
		long key = s.h();
		if(dp.containsKey(key)){
			dp.put(key, s);
		}else{
			State tar = dp.get(key);
			tar.val += s.val;
			tar.val %= mod;
		}
	}

	static class State
	{
		int[] clus;
		long val;
		int hidden; // clusに現れないが、すでに現れた連結成分の個数

		long h()
		{
			long h = 0;
			for(int v : clus){
				h = h * 1000000009 + v;
			}
			h = h * 1000000009 + hidden;
			return h;
		}

		public State(int[] clus, long val, int hidden) {
			this.clus = clus;
			this.val = val;
			this.hidden = hidden;
		}
	}
	
	public static void main(String[] args) throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);

		long S = System.currentTimeMillis();
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S);
	}
	
	static int ni() { return Integer.parseInt(in.next()); }
	static long nl() { return Long.parseLong(in.next()); }
	static double nd() { return Double.parseDouble(in.next()); }
	static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
0