結果

問題 No.2846 Birthday Cake
ユーザー uwi
提出日時 2024-08-29 14:24:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,393 ms / 2,000 ms
コード長 3,090 bytes
コンパイル時間 4,555 ms
コンパイル使用メモリ 85,164 KB
実行使用メモリ 74,184 KB
最終ジャッジ日時 2024-08-29 14:25:16
合計ジャッジ時間 32,777 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

package no2xxx;
import java.io.PrintWriter;
import java.util.*;

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

	static class Datum
	{
		long[] f;
		long v;

		public Datum(long[] f, long v) {
			this.f = f;
			this.v = v;
		}
	}
	
	static void solve()
	{
		int K = ni(), n = ni();
		Map<Long, Datum> dp = new HashMap<>();
		dp.put(1L, new Datum(new long[]{0, 1}, 1L));

		for(int i = 0;i < K;i++){
			Map<Long, Datum> ndp = new HashMap<>();
			for(long key : dp.keySet()){
				Datum d = dp.get(key);
				for(int j = n;j >= 1;j--) {
					if(j == 23 || j == 19 || j == 17 || j == 13)continue;
					long[] nf = add(d.f, new long[]{1, j});
					if (nf[0] > nf[1]) break;
					if (ndp.containsKey(h(nf))) {
						ndp.get(h(nf)).v += d.v;
					}else{
						Datum add = new Datum(nf, d.v);
						ndp.put(h(nf), add);
					}
				}
			}

//			for(Datum vv : ndp.values()){
//				tr(vv.f, vv.v);
//			}
//			tr();
			dp = ndp;
		}
		Datum d = dp.get(h(new long[]{1, 1}));
		long ans = d != null ? d.v : 0;
		if(K == 13 || K == 17 || K == 19 || K == 23)ans++;
		out.println(ans);
	}

	static long h(long[] a)
	{
		long ret = 0;
		for(long v : a)ret = ret * 1000000009 + v;
		return ret;
	}

	public static long[] reduce(long[] f)
	{
		if(f[1] == 0) { // n/0
			f[0] = 1;
		}else if(f[0] == 0) { // 0/n
			f[1] = 1;
		}else {
			if(f[1] < 0) { // -a/-b ->a/b
				f[0] = -f[0];
				f[1] = -f[1];
			}
			long a = Math.abs(f[0]), b = f[1];
			while (b > 0) {
				long c = a;
				a = b;
				b = c % b;
			}
			f[0] /= a;
			f[1] /= a;
		}
		return f;
	}

	public static long[] add(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1]+a[1]*b[0], a[1]*b[1]}); }
	public static long[] sub(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1]-a[1]*b[0], a[1]*b[1]}); }
	public static long[] mul(long[] a, long[] b){ return reduce(new long[]{a[0]*b[0], a[1]*b[1]}); }
	public static long[] div(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1], a[1]*b[0]}); }

	public static int compare(long[] a, long[] b){return Long.signum(a[0] * b[1] - a[1] * b[0]);}

	public static long[] min(long[] a, long[] b){ return compare(a, b) <= 0 ? a : b; }
	public static long[] max(long[] a, long[] b){ return compare(a, b) >= 0 ? a : b; }

	public static int lowerBound(long[][] es, int p, long[] r)
	{
		int low = -1, high = p;
		while(high-low > 1){
			int h = high+low>>>1;
			if(Long.compare(r[0]*es[h][1], r[1]*es[h][0]) <= 0){
				high = h;
			}else{
				low = h;
			}
		}
		return high;
	}

	public static Comparator<long[]> comp = (a, b) -> Long.signum(a[0]*b[1]-a[1]*b[0]);


	public static void main(String[] args) throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
	}
	
	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