結果

問題 No.1404 誕生日プレゼント
ユーザー CuriousFairy315CuriousFairy315
提出日時 2020-12-07 23:33:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 607 ms / 3,153 ms
コード長 14,344 bytes
コンパイル時間 2,823 ms
コンパイル使用メモリ 90,396 KB
実行使用メモリ 72,324 KB
最終ジャッジ日時 2023-10-17 16:43:03
合計ジャッジ時間 42,021 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
54,496 KB
testcase_01 AC 51 ms
53,568 KB
testcase_02 AC 80 ms
54,560 KB
testcase_03 AC 105 ms
54,748 KB
testcase_04 AC 121 ms
55,424 KB
testcase_05 AC 146 ms
56,236 KB
testcase_06 AC 153 ms
56,368 KB
testcase_07 AC 155 ms
56,968 KB
testcase_08 AC 176 ms
57,160 KB
testcase_09 AC 178 ms
57,372 KB
testcase_10 AC 180 ms
57,420 KB
testcase_11 AC 183 ms
57,444 KB
testcase_12 AC 185 ms
57,376 KB
testcase_13 AC 543 ms
71,432 KB
testcase_14 AC 553 ms
71,072 KB
testcase_15 AC 547 ms
72,036 KB
testcase_16 AC 590 ms
72,320 KB
testcase_17 AC 607 ms
72,160 KB
testcase_18 AC 599 ms
72,024 KB
testcase_19 AC 488 ms
71,664 KB
testcase_20 AC 571 ms
72,324 KB
testcase_21 AC 475 ms
67,964 KB
testcase_22 AC 485 ms
71,260 KB
testcase_23 AC 132 ms
57,356 KB
testcase_24 AC 135 ms
58,036 KB
testcase_25 AC 107 ms
56,660 KB
testcase_26 AC 137 ms
57,392 KB
testcase_27 AC 72 ms
55,528 KB
testcase_28 AC 593 ms
71,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder_4950;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.NoSuchElementException;

public class Main {

	public static void main(final String[] args) {
		new Main();
	}

	public Main() {
		final InputChecker ic = new InputChecker(System.in);
		final java.io.PrintWriter out = new java.io.PrintWriter(System.out);
		solve(ic, out);
		out.flush();
	}

	public void solve(final InputChecker ic, final java.io.PrintWriter out) {
		final int N = ic.nextInt(1, 100000);
		ic.nextChar(' ');
		final int P = ic.nextInt(223577, 223577);
		ic.readNewLine();
		final int[] A = new int[N], B = new int[N];
		{
			A[0] = ic.nextInt(0, P - 1);
			for (int i = 1;i < N;++ i) {
				ic.nextChar(' ');
				A[i] = ic.nextInt(0, P - 1);
			}
			ic.readNewLine();
			B[0] = ic.nextInt(0, P - 1);
			for (int i = 1;i < N;++ i) {
				ic.nextChar(' ');
				B[i] = ic.nextInt(0, P - 1);
			}
			ic.readNewLine();
		}
		ic.checkEOF();

		{ // 例外チェック
			boolean isZeroA = true, isZeroB = true;
			for (final int i : A) isZeroA &= i == 0;
			for (final int i : B) isZeroB &= i == 0;
			if (isZeroA) {
				if (isZeroB) {
					out.println(0);
					return;
				}
				out.println(-1);
				return;
			}
			if (isZeroB) {
				out.println(-1);
				return;
			}
		}
		if (N == 1) {
			int count = 0;
			for (;A[0] != B[0] && count < P;++ count) A[0] = add(A[0], A[0], P);
			if (A[0] == B[0]) {
				out.println(count + 1);
				while(count --> 0) out.println("1 1 1");
				out.println(2);
			} else {
				out.println(-1);
			}
			return;
		}
		final ArrayList<Query> query = new ArrayList<>();
		int base = 0;
		for (int i = 0;i < N;++ i) if (B[i] != 0) base = i;
		if (A[base] == 0) {
			for (int i = 0;i < N;++ i) {
				if (A[i] != 0) {
					query(base, i, P, A, query);
					break;
				}
			}
		}
		final int ADD1 = 1;
		final int ADD2 = ADD1 + 18;
		final int ADD3 = ADD2 + 512;
		final int ADD4 = ADD3 + 512;
		// 1~18: 0000,0001,0010,0100,...
		// 19~530: 0000,0010,1000,1010,...
		// 531~1042: 0000,0020,2000,2020,...
		
		if (N >= ADD2) {
			for (int i = 0;i < 18;++ i) {
				final int target = mod(1 << i, P);
				set1(add(base, i + ADD1, N), base, target, P, A, query);
			}
		}

		if (N >= ADD3) {
			for (int i = 0;i < 512;++ i) {
				final int target = mod(get4(i), P);
				set2(add(base, i + ADD2, N), base, target, P, A, query);
			}
		}
		if (N >= ADD4) {
			for (int i = 0;i < 512;++ i) {
				final int target = mul(2, get4(i), P);
				set3(add(base, i + ADD3, N), base, target, P, A, query);
			}
		}

		for (int i = N - 1;i >= ADD4;-- i) set4(add(base, i, N), base, B[add(base, i, N)], P, A, query);
		for (int i = Math.min(ADD4 - 1, N);i >= ADD3;-- i) set3(add(base, i, N), base, B[add(base, i, N)], P, A, query);
		for (int i = Math.min(ADD3 - 1, N);i >= ADD2;-- i) set2(add(base, i, N), base, B[add(base, i, N)], P, A, query);
		for (int i = Math.min(ADD2 - 1, N);i > 1;-- i) set1(add(base, i, N), base, B[add(base, i, N)], P, A, query);

		final int target = div(B[base], pow(2, 20, P), P);
		if (A[add(base, 1, N)] == 0) query(add(base, 1, N), base, P, A, query);
		set1(base, add(base, 1, N), target, P, A, query);
		set1(add(base, 1, N), base, B[add(base, 1, N)], P, A, query);
		while(A[base] != B[base]) query(base, base, P, A, query);
//		System.err.println(query.size() + 1);
		if (query.size() + 1 > P) throw new AssertionError();
		for (int i = 0;i < N;++ i) if (A[i] != B[i]) throw new AssertionError();
		out.println(query.size() + 1);
		for (final Query q : query) out.println(q);
		out.println(2);
	}

	// i番目の値を、j番目を利用してnに変える
	private static void set1(final int i, final int j, final int n, final int P, final int[] A, final ArrayList<Query> query) {
		final int diff = sub(n, A[i], P);
		int target = div(diff, A[j], P);
		while(target != 0) {
			if ((target & 1) != 0) query(i, j, P, A, query);
			query(j, j, P, A, query);
			target >>= 1;
		}
	}
	
	// 1~18を用いて変える
	private static void set2(final int i, final int base, final int n, final int P, final int[] A, final ArrayList<Query> query) {
		final int diff = sub(n, A[i], P);
		for (int j = 0;j < 18;++ j) {
			if ((diff >> j & 1) != 0) query(i, add(base, 1 + j, A.length), P, A, query);
		}
	}
	// 19~530を用いて変える
	private static void set3(final int i, final int base, final int n, final int P, final int[] A, final ArrayList<Query> query) {
		while(A[i] != n) {
			final int source = add(base + 19, get4_1(sub(n, A[i], P)), A.length);
			query(i, source, P, A, query);
		}
	}
	// 531~1042を用いて変える
	private static void set4(final int i, final int base, final int n, final int P, final int[] A, final ArrayList<Query> query) {
		final int source1 = add(base + 531, get4_2(sub(n, A[i], P)), A.length);
		query(i, source1, P, A, query);
		final int source2 = add(base + 19, get4_1(sub(n, A[i], P)), A.length);
		query(i, source2, P, A, query);
	}

	private static void query(final int i, final int j, final int P, final int[] A, final ArrayList<Query> query) {
		query.add(new Query(i, j));
		A[i] = add(A[i], A[j], P);
	}

	private static int get4(final int n) {
		int ret = 0;
		for (int i = 0;i < 20;++ i) {
			if ((n >> i & 1) != 0) ret |= 1 << 2 * i;
		}
		return ret;
	}

	private static int get4_1(final int n) {
		int ret = 0;
		for (int i = 0;i < 15;++ i) {
			if ((n >> 2 * i & 3) != 0) ret |= 1 << i;
		}
		return ret;
	}

	private static int get4_2(final int n) {
		int ret = 0;
		for (int i = 0;i < 15;++ i) {
			if ((n >> 2 * i & 3) >= 2) ret |= 1 << i;
		}
		return ret;
	}

	private static class Query {
		final int i, j;
		/** A[i] += A[j] */
		Query(final int i, final int j) {
			this.i = i;
			this.j = j;
		}
		@Override
		public String toString() {
			return "1 " + (i + 1) + " " + (j + 1);
		}
	}

	public static int mod(final int a, final int mod) {
		return a % mod;
	}

	public static int add(final int a, final int b, final int mod) {
		int ret = a + b;
		while (ret >= mod) ret -= mod;
		return ret;
	}

	public static int sub(final int a, final int b, final int mod) {
		int ret = a - b;
		while (ret < 0) ret += mod;
		return ret;
	}

	public static int mul(final int a, final int b, final int mod) {
		return (int)((long)a * b % mod);
	}

	public static int div(final int a,final int b, final int mod) {
		return mul(a, inv(b, mod), mod);
	}

	public static int inv(final int a, final int mod) {
		return pow(a, mod - 2, mod);
	}

	public static int pow(final int a, int b, final int mod) {
		int ans = 1;
		for (int mul = a;b > 0;b >>= 1, mul = mul(mul, mul, mod)) if ((b & 1) != 0) ans = mul(ans, mul, mod);
		return ans;
	}

	public static int exponent10(final int n, final int e) {
		return n * pow(10, e);
	}

	public static long exponent10L(final int n, final int e) {
		return n * pow(10L, e);
	}

	public static int pow(final int a, int b) {
		int ans = 1;
		for (int mul = a;b > 0;b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static long pow(final long a, long b) {
		long ans = 1;
		for (long mul = a;b > 0;b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static class CharSet {
		private final BitSet set = new BitSet(256);

		public void add(final char... c) {
			for (final char i : c) set.set(i);
		}

		public void add(final CharSet... s) {
			for (final CharSet i : s) set.or(i.set);
		}

		public boolean contains(final char... c) {
			for (final char i : c) if (!set.get(i)) return false;
			return true;
	 	}

		public boolean contains(final String s) {
			return contains(s.toCharArray());
	 	}

		private static final class Chars extends CharSet {
			public Chars(final char... c) {
				super.add(c);
			}
			public Chars(final CharSet... s) {
				super.add(s);
			}
			@Override
			public void add(final char... c) {
				throw new UnsupportedOperationException();
			}
			@Override
			public void add(final CharSet... s) {
				throw new UnsupportedOperationException();
			}
		}

		public static final CharSet NUMBER = new Chars('0','1','2','3','4','5','6','7','8','9');
		public static final CharSet LOWER = new Chars('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
		public static final CharSet UPPER = new Chars('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
		public static final CharSet ALPHABET = new Chars(LOWER, UPPER);

	}
	public static class InputChecker {
		private InputStream in;
		private final byte[] buffer = new byte[1024];
		private final byte[] undo = new byte[1024];
		private int undoSize = 0;
		private int read = 0;
		private int length = 0;

		public InputChecker(final InputStream in) {
			this.in = in;
		}

		public final void setInputStream(final InputStream in) {
			this.in = in;
		}

		public final void setInputStream(final File in) {
			try {
				this.in = new FileInputStream(in);
			} catch (final FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		private boolean hasNextByte() {
			if (undoSize > 0) return true;
			if (read < length) return true;
			read = 0;
			try {
				length = in.read(buffer);
			} catch (final IOException e) {
				e.printStackTrace();
			}
			return length > 0;
		}

		private byte readByte() {
			if (hasNextByte()) return undoSize > 0 ? undo[--undoSize] : buffer[read++];
			throw new NoSuchElementException();
		}

		private void undo(final byte b) {
			undo[undoSize ++] = b;
		}

		private void undo(final char c) {
			if ((c & 0xFF80) == 0) {
				undo((byte)c);
				return;
			}
			undo((byte)(c & 0x3F | 0x80));
			if ((c & 0xF800) == 0) {
				undo((byte)(c >> 6 & 0x1F | 0xC0));
			} else {
				undo((byte)(c >> 6 & 0x3F | 0x80));
				undo((byte)(c >> 12 | 0xE0));
			}
		}

		public final boolean hasNext() {
			return hasNextByte();
		}

		public final char nextChar() {
			final byte b = readByte();
			if ((b & 0x80) == 0) return (char)b;
			if ((b & 0x20) == 0) return (char)((b & 0x1F) << 6 | readByte() & 0x3F);
			return (char)((b & 0xF) << 12 | (readByte() & 0x3F) << 6 | readByte() & 0x3F);
		}

		public final char nextChar(final char estimate) {
			final char c = nextChar();
			if (estimate == c) return estimate;
			undo(c);
			throw new AssertionError();
		}

		public final char nextChar(final CharSet estimate) {
			final char c = nextChar();
			if (estimate.contains(c)) return c;
			undo(c);
			throw new AssertionError();
		}

		public final void readNewLine() {
			char c = nextChar();
			if (c == '\r') {
				c = nextChar();
				if (c != '\n') undo(c);
				return;
			} else if (c == '\n') return;
			undo(c);
			throw new AssertionError();
		}

		public final void checkEOF() {
			if (hasNextByte()) throw new AssertionError();
		}

		public final String next(final CharSet contains) {
			final StringBuilder sb = new StringBuilder();
			try {
				do {
					final char c = nextChar();
					if (!contains.contains(c)) {
						undo(c);
						return sb.toString();
					}
					sb.append(c);
				} while(true);
			} catch (final NoSuchElementException e) {
				if (sb.length() <= 0) throw new AssertionError();
				return sb.toString();
			}
		}

		public final int nextInt() {
			byte b = readByte();
			int n = 0;
			if (b == '-') {
				if (!isNumber(b = readByte())) {
					undo(b);
					throw new NumberFormatException();
				}
				try {
					if (b == '0') {
						if (!isNumber(b = readByte())) {
							undo(b);
							return 0;
						}
						throw new NumberFormatException();
					}
					do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
					undo(b);
				} catch (final NoSuchElementException e) {
				}
				return n;
			}
			if (!isNumber(b)) {
				undo(b);
				throw new NumberFormatException();
			}
			try {
				if (b == '0') {
					if (!isNumber(b = readByte())) {
						undo(b);
						return 0;
					}
					throw new NumberFormatException();
				}
				do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
				undo(b);
			} catch (final NoSuchElementException e) {
			}
			return n;
		}

		public final int nextInt(final int min, final int max) {
			final int n = nextInt();
			if (min <= n && n <= max) return n;
			throw new NumberFormatException();
		}

		private static boolean isNumber(final byte c) {
			return '0' <= c && c <= '9';
		}

		public final long nextLong() {
			byte b = readByte();
			long n = 0;
			if (b == '-') {
				if (!isNumber(b = readByte())) {
					undo(b);
					throw new NumberFormatException();
				}
				try {
					if (b == '0') {
						if (!isNumber(b = readByte())) {
							undo(b);
							return 0;
						}
						throw new NumberFormatException();
					}
					do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
					undo(b);
				} catch (final NoSuchElementException e) {
				}
				return n;
			}
			if (!isNumber(b)) {
				undo(b);
				throw new NumberFormatException();
			}
			try {
				if (b == '0') {
					if (!isNumber(b = readByte())) {
						undo(b);
						return 0;
					}
					throw new NumberFormatException();
				}
				do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
				undo(b);
			} catch (final NoSuchElementException e) {
			}
			return n;
		}

		public final long nextLong(final long min, final long max) {
			final long n = nextLong();
			if (min <= n && n <= max) return n;
			throw new NumberFormatException();
		}

		public final double nextDouble() {
			final StringBuilder sb = new StringBuilder();
			byte b = readByte();
			if (b == '-') {
				sb.append(b);
				b = readByte();
			}
			if (b == '0') {
				sb.append(b);
				b = readByte();
			} else {
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			if (b == '.') {
				sb.append(b);
				b = readByte();
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			if (b == 'e' || b == 'E') {
				sb.append(b);
				b = readByte();
				if (b == '-' || b == '+') {
					sb.append(b);
					b = readByte();
				}
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			undo(b);
			return Double.parseDouble(sb.toString());
		}
	}
}
0