結果

問題 No.1561 connect x connect
ユーザー uwiuwi
提出日時 2020-11-25 02:42:43
言語 Java21
(openjdk 21)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,804 bytes
コンパイル時間 4,119 ms
コンパイル使用メモリ 76,572 KB
実行使用メモリ 73,628 KB
最終ジャッジ日時 2023-09-07 13:50:38
合計ジャッジ時間 14,858 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,756 KB
testcase_01 AC 138 ms
55,804 KB
testcase_02 AC 483 ms
69,756 KB
testcase_03 AC 118 ms
55,760 KB
testcase_04 AC 120 ms
55,800 KB
testcase_05 AC 124 ms
55,764 KB
testcase_06 AC 123 ms
55,472 KB
testcase_07 AC 130 ms
55,788 KB
testcase_08 AC 136 ms
55,720 KB
testcase_09 AC 146 ms
57,684 KB
testcase_10 AC 165 ms
58,276 KB
testcase_11 AC 188 ms
59,128 KB
testcase_12 AC 249 ms
59,540 KB
testcase_13 AC 432 ms
68,604 KB
testcase_14 AC 414 ms
66,908 KB
testcase_15 AC 1,233 ms
73,200 KB
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 AC 286 ms
59,940 KB
testcase_32 AC 534 ms
70,108 KB
testcase_33 AC 1,204 ms
73,628 KB
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_2 {
	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);
						int numclus = s.hidden;
						int x = 0;
						for(int v : nclus)if(v != -1)x = 1;
						numclus += x;
						if(numclus <= 1) {
							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;
								}
							}
						}

						int numclus = nhidden;
						int x = 0;
						for(int v : nclus)if(v != -1)x = 1;
						numclus += x;
						if(numclus <= 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)){
			State tar = dp.get(key);
			tar.val += s.val;
			tar.val %= mod;
		}else{
			dp.put(key, s);
		}
	}

	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