結果

問題 No.3014 多項式ハッシュに関する教育的な問題
ユーザー ShaninAndreyShaninAndrey
提出日時 2017-05-18 21:45:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 4,921 bytes
コンパイル時間 4,545 ms
コンパイル使用メモリ 83,624 KB
実行使用メモリ 50,056 KB
最終ジャッジ日時 2024-09-18 19:12:33
合計ジャッジ時間 5,072 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.List;

public class Q804 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		long mod = nl();
		long mul = nl();
		int z = 200;
		long[][] h = new long[z][];
		h[0] = new long[]{1L, z-1};
		for(int i = 1;i < z;i++){
			h[i] = new long[]{mul(h[i-1][0], mul, mod), z-1-i};
		}
		List<long[]> res = go(h);
		char[] s = new char[z];
		Arrays.fill(s, 'm');
		char[] t = new char[z];
		Arrays.fill(t, 'm');
		for(long[] u : res){
			t[(int)u[0]] += u[1];
		}
		out.println(new String(s));
		out.println(new String(t));
		
		// check
		for(char c : t){
			if('a' <= c && c <= 'z'){
			}else{
				throw new RuntimeException();
			}
		}
		
		long hs = 0, ht = 0;
		for(int i = 0;i < z;i++){
			hs = mul(hs, mul, mod) + s[i];
			if(hs >= mod)hs -= mod;
			ht = mul(ht, mul, mod) + t[i];
			if(ht >= mod)ht -= mod;
		}
//		tr(hs, ht);
		if(hs != ht)throw new RuntimeException();
	}
	
	List<long[]> go(long[][] h){
		Arrays.sort(h, new Comparator<long[]>() {
			public int compare(long[] a, long[] b) {
				return Long.compare(a[0], b[0]);
			}
		});
		if(h[0][0] == 0){
			List<long[]> list = new ArrayList<>();
			list.add(new long[]{h[0][1], 1});
			return list;
		}
		
		int n = h.length;
		if(n == 0)throw new RuntimeException();
		long[][] g = new long[n-1][];
		for(int i = 0;i < n-1;i++){
			g[i] = new long[]{h[i+1][0] - h[i][0], i};
		}
		List<long[]> res = go(g);
		List<long[]> ret = new ArrayList<>();
		for(long[] x : res){
			ret.add(new long[]{h[(int)x[0]+1][1], x[1]});
			ret.add(new long[]{h[(int)x[0]][1], -x[1]});
		}
		return ret;
	}
	
	public static long mul(long a, long b, long mod)
	{
		if(a >= mod || a < 0)a %= mod;
		if(a < 0)a += mod;
		if(b >= mod || b < 0)b %= mod;
		if(b < 0)b += mod;
		
		long ret = 0;
		int x = 63-Long.numberOfLeadingZeros(b);
		for(;x >= 0;x--){
			ret += ret;
			if(ret >= mod)ret -= mod;
			if(b<<~x<0){
				ret += a;
				if(ret >= mod)ret -= mod;
			}
		}
		return ret;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new Q804().run(); }
	
	private byte[] inbuf = new byte[1024];
	private int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0