結果

問題 No.896 友達以上恋人未満
ユーザー 37zigen37zigen
提出日時 2018-03-01 13:11:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,255 ms / 3,500 ms
コード長 4,486 bytes
コンパイル時間 2,589 ms
コンパイル使用メモリ 78,492 KB
実行使用メモリ 187,804 KB
最終ジャッジ日時 2024-06-11 19:17:09
合計ジャッジ時間 7,555 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
36,732 KB
testcase_01 AC 52 ms
36,764 KB
testcase_02 AC 52 ms
36,620 KB
testcase_03 AC 53 ms
36,784 KB
testcase_04 AC 52 ms
36,760 KB
testcase_05 AC 413 ms
77,072 KB
testcase_06 AC 729 ms
76,436 KB
testcase_07 AC 412 ms
77,152 KB
testcase_08 AC 408 ms
77,160 KB
testcase_09 AC 668 ms
114,184 KB
testcase_10 AC 1,255 ms
187,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		FastScanner sc = new FastScanner();
		int M = (int) sc.nextLong();
		int N = (int) sc.nextLong();
		long mulX = sc.nextLong();
		long addX = sc.nextLong();
		long mulY = sc.nextLong();
		long addY = sc.nextLong();
		long MOD = sc.nextLong();
		if (!(1 <= M && M <= 1000) && (!(M <= N && N <= 10000000))
				&& (1 <= MOD && MOD <= 1 << 24))
			throw new AssertionError();
		if (!(0 <= mulX && mulX < MOD && 0 <= mulY && mulY < MOD && 0 <= addX && addX < MOD && 0 <= addY
				&& addY < MOD))
			throw new AssertionError();
		int[] X = new int[M];
		int[] Y = new int[M];
		int[] A = new int[M];
		int[] B = new int[M];
		long[] z = new long[(int) MOD];
		for (int i = 0; i < M; ++i) {
			X[i] = (int) sc.nextLong();
			if (!(0 <= X[i] && X[i] < MOD))
				throw new AssertionError();
		}
		for (int i = 0; i < M; ++i) {
			Y[i] = (int) sc.nextLong();
			if (!(0 <= Y[i] && Y[i] < MOD))
				throw new AssertionError();
		}
		for (int i = 0; i < M; ++i) {
			A[i] = (int) sc.nextLong();
			if (!(1 <= A[i] && A[i] <= MOD))
				throw new AssertionError();
		}
		for (int i = 0; i < M; ++i) {
			B[i] = (int) sc.nextLong();
			if (!(1 <= B[i] && B[i] <= MOD))
				throw new AssertionError();
		}
		for (int i = 0; i < M; ++i) {
			z[(int) X[i]] += Y[i];
		}
		{
			long X_ = X[M - 1] & (MOD - 1);
			long Y_ = Y[M - 1] & (MOD - 1);
			for (int i = M; i < N; ++i) {
				X_ = X_ * mulX + addX & MOD - 1;
				Y_ = Y_ * mulY + addY & MOD - 1;
				z[(int) X_] += Y_;
			}
		}
		boolean[] isPrime = isPrime((int) MOD);
		for (int i = 1; i < MOD; ++i) {
			if (isPrime[i]) {
				int k = (int) (MOD - 1) / i;
				for (int j = k; j >= 1; --j) {
					z[j] += z[j * i];
				}
			}
		}
		PrintWriter pw = new PrintWriter(System.out);
		long all = 0;
		for (int i = 0; i < M; ++i) {
			long v = f(A[i], z) - f(A[i] * B[i], z);
			all ^= v;
			pw.println(v);
		}
		long A_ = A[M - 1] & (MOD - 1);
		long B_ = B[M - 1] & (MOD - 1);
		for (int i = M; i < N; ++i) {
			A_ = ((A_ * mulX & MOD - 1) + addX + MOD - 1 & MOD - 1) + 1;
			B_ = ((B_ * mulY & MOD - 1) + addY + MOD - 1 & MOD - 1) + 1;
			long v = f(A_, z) - f(A_ * B_, z);
			all = all ^ v;
		}
		pw.println(all);
		pw.close();
	}

	long f(long v, long[] z) {
		if (v >= z.length)
			return 0;
		return z[(int) v];
	}

	boolean[] isPrime(int N) {
		boolean[] ret = new boolean[N + 1];
		Arrays.fill(ret, true);
		ret[0] = ret[1] = false;
		for (int i = 2; i < ret.length; ++i) {
			if (ret[i]) {
				for (int j = 2 * i; j < ret.length; j += i) {
					ret[j] = false;
				}
			}
		}
		return ret;
	}

	class FastScanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

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

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0