結果

問題 No.5005 3-SAT
ユーザー takytanktakytank
提出日時 2022-04-29 15:24:12
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 33,989 bytes
コンパイル時間 8,192 ms
実行使用メモリ 172,204 KB
スコア 19,522
最終ジャッジ日時 2022-04-29 15:24:42
合計ジャッジ時間 19,693 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
43,244 KB
testcase_01 AC 70 ms
48,048 KB
testcase_02 AC 65 ms
39,088 KB
testcase_03 AC 62 ms
38,880 KB
testcase_04 AC 74 ms
39,512 KB
testcase_05 AC 60 ms
36,904 KB
testcase_06 AC 67 ms
40,216 KB
testcase_07 AC 63 ms
38,252 KB
testcase_08 AC 61 ms
38,628 KB
testcase_09 AC 67 ms
46,768 KB
testcase_10 AC 65 ms
43,316 KB
testcase_11 AC 63 ms
41,800 KB
testcase_12 AC 65 ms
44,800 KB
testcase_13 AC 66 ms
42,564 KB
testcase_14 AC 63 ms
40,808 KB
testcase_15 AC 64 ms
42,156 KB
testcase_16 AC 63 ms
40,920 KB
testcase_17 AC 68 ms
41,952 KB
testcase_18 AC 64 ms
35,652 KB
testcase_19 AC 79 ms
44,800 KB
testcase_20 AC 65 ms
44,028 KB
testcase_21 AC 61 ms
39,796 KB
testcase_22 AC 64 ms
41,468 KB
testcase_23 AC 64 ms
43,112 KB
testcase_24 AC 68 ms
46,336 KB
testcase_25 AC 63 ms
41,224 KB
testcase_26 AC 70 ms
45,248 KB
testcase_27 AC 69 ms
42,720 KB
testcase_28 AC 65 ms
39,736 KB
testcase_29 AC 66 ms
41,528 KB
testcase_30 AC 70 ms
43,332 KB
testcase_31 AC 68 ms
40,020 KB
testcase_32 AC 75 ms
36,988 KB
testcase_33 AC 65 ms
41,344 KB
testcase_34 AC 70 ms
45,604 KB
testcase_35 AC 69 ms
42,868 KB
testcase_36 AC 63 ms
38,916 KB
testcase_37 AC 67 ms
45,660 KB
testcase_38 AC 68 ms
41,948 KB
testcase_39 AC 67 ms
44,900 KB
testcase_40 AC 66 ms
41,924 KB
testcase_41 AC 65 ms
39,320 KB
testcase_42 AC 70 ms
46,004 KB
testcase_43 AC 66 ms
41,804 KB
testcase_44 AC 72 ms
43,076 KB
testcase_45 AC 82 ms
41,208 KB
testcase_46 AC 62 ms
38,720 KB
testcase_47 AC 69 ms
46,192 KB
testcase_48 AC 69 ms
41,284 KB
testcase_49 AC 67 ms
43,708 KB
testcase_50 AC 67 ms
42,048 KB
testcase_51 AC 68 ms
41,176 KB
testcase_52 AC 69 ms
40,004 KB
testcase_53 AC 71 ms
47,040 KB
testcase_54 AC 69 ms
42,176 KB
testcase_55 AC 67 ms
42,056 KB
testcase_56 AC 70 ms
45,996 KB
testcase_57 AC 86 ms
37,520 KB
testcase_58 AC 66 ms
43,224 KB
testcase_59 AC 66 ms
41,560 KB
testcase_60 AC 70 ms
46,528 KB
testcase_61 AC 66 ms
40,736 KB
testcase_62 AC 63 ms
38,868 KB
testcase_63 AC 64 ms
40,100 KB
testcase_64 AC 69 ms
44,016 KB
testcase_65 AC 77 ms
42,576 KB
testcase_66 AC 73 ms
41,304 KB
testcase_67 AC 65 ms
37,780 KB
testcase_68 AC 67 ms
38,084 KB
testcase_69 AC 79 ms
44,308 KB
testcase_70 AC 64 ms
40,440 KB
testcase_71 AC 63 ms
40,924 KB
testcase_72 AC 63 ms
39,932 KB
testcase_73 AC 62 ms
39,212 KB
testcase_74 AC 68 ms
44,480 KB
testcase_75 AC 68 ms
40,984 KB
testcase_76 AC 67 ms
39,172 KB
testcase_77 AC 66 ms
43,156 KB
testcase_78 AC 66 ms
41,544 KB
testcase_79 AC 63 ms
41,712 KB
testcase_80 AC 68 ms
44,624 KB
testcase_81 AC 69 ms
40,360 KB
testcase_82 AC 75 ms
41,340 KB
testcase_83 AC 75 ms
42,344 KB
testcase_84 AC 79 ms
46,572 KB
testcase_85 AC 68 ms
35,376 KB
testcase_86 AC 74 ms
45,320 KB
testcase_87 AC 73 ms
45,924 KB
testcase_88 AC 66 ms
42,196 KB
testcase_89 AC 67 ms
43,916 KB
testcase_90 AC 65 ms
41,644 KB
testcase_91 AC 67 ms
43,992 KB
testcase_92 AC 65 ms
41,908 KB
testcase_93 AC 69 ms
41,472 KB
testcase_94 AC 73 ms
39,172 KB
testcase_95 AC 65 ms
41,128 KB
testcase_96 AC 69 ms
44,552 KB
testcase_97 AC 70 ms
46,492 KB
testcase_98 AC 67 ms
40,248 KB
testcase_99 AC 65 ms
172,204 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 117 ms).
.NET 向け Microsoft (R) Build Engine バージョン 17.0.0-preview-21470-01+cb055d28f
Copyright (C) Microsoft Corporation.All rights reserved.

  プレビュー版の .NET を使用しています。https://aka.ms/dotnet-core-preview をご覧ください
  main -> /home/judge/data/code/bin/Release/net6.0/main.dll
  main -> /home/judge/data/code/bin/Release/net6.0/publish/

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace YukiCoder
{
	class Program
	{
		static void Main()
		{
			var sw = new Stopwatch();
			sw.Start();

			using var cin = new Scanner();
			int n = 2048;
			//int n = 24;
			int m = 256;
			var info = new (int a, int b, int c, int p, int q, int r)[n];
			for (int i = 0; i < n; i++) {
				var (a, b, c) = cin.Int3();
				var (p, q, r) = cin.Int3();
				info[i] = (a, b, c, p, q, r);
			}

			int Check(int[] cur)
			{
				int score = 0;
				for (int i = 0; i < n; i++) {
					var (a, b, c, p, q, r) = info[i];
					if (cur[a] != p && cur[b] != q && cur[c] != r) {
						break;
					}

					++score;
				}

				return score;
			}

			var imos = new Imos2D(n, m);
			for (int i = 0; i < n; i++) {
				var (a, b, c, _, _, _) = info[i];
				imos.Add(i, a, i + 1, a + 1, 1);
				imos.Add(i, b, i + 1, b + 1, 1);
				imos.Add(i, b, i + 1, b + 1, 1);
			}

			imos.Build();

			var ans = new int[m];
			int score = Check(ans);

			int beam = 10;
			var que = new PriorityQueue<(int[] arr, int v), int>(x => x.v);
			que.Enqueue((new int[m].Fill(-1), n * 3));
			for (int i = 0; i < n; i++) {
				if (sw.ElapsedMilliseconds >= 1900) {
					break;
				}

				var (a, b, c, p, q, r) = info[i];

				var dst = new PriorityQueue<(int[] arr, int v), int>(x => x.v, true);
				while (que.Count > 0) {
					if (sw.ElapsedMilliseconds >= 1900) {
						break;
					}

					var (cur, v) = que.Dequeue();
					int del = 0;
					foreach (var t in new int[] { a, b, c }) {
						if (cur[c] == -1) {
							++del;
						}
					}

					void Do(int d, int s)
					{
						bool ok = false;
						int nv = v - del;
						int[] next = null;
						if (cur[d] == -1) {
							ok = true;
							next = cur.ToArray();
							nv -= (int)imos.Query(i + 1, d, n, d + 1);
							next[d] = s;
						} else if (cur[d] == s) {
							ok = true;
							next = cur.ToArray();
						}

						if (ok) {
							if (dst.Count < beam) {
								dst.Enqueue((next, nv));
							} else {
								if (dst.Top.v < nv) {
									dst.Dequeue();
									dst.Enqueue((next, nv));
								}
							}
						}
					}

					Do(a, p);
					Do(b, q);
					Do(c, r);
				}

				if (dst.Count > 0) {
					if (i + 1 > score) {
						ans = dst.Top.arr.ToArray();
						score = i + 1;
					}

					que = dst;
				} else {
					break;
				}
			}

			var sb = new StringBuilder();
			for (int i = m - 1; i >= 0; i--) {
				sb.Append((ans[i] == -1 ? 0 : ans[i]).ToString());
			}

			Console.WriteLine(sb.ToString());
#if DEBUG
			Console.WriteLine(score);
#endif
		}
	}

	public class PriorityQueue<TObject, TPriority> where TPriority : IComparable<TPriority>
	{
		private readonly Func<TObject, TPriority> selector_;
		private readonly int reverseFactory_;
		private TObject[] heap_;

		public TObject Top => heap_[0];
		public int Count { get; private set; }

		public PriorityQueue(
			Func<TObject, TPriority> selector,
			bool reverses = false)
			: this(1024, selector, reverses) { }

		public PriorityQueue(
			int capacity,
			Func<TObject, TPriority> selector,
			bool reverses = false)
		{
			if (capacity == 0) {
				capacity = 1024;
			}

			heap_ = new TObject[capacity];
			selector_ = selector;
			reverseFactory_ = reverses ? -1 : 1;
		}

		public void Enqueue(TObject item)
		{
			if (heap_.Length == Count) {
				Extend(heap_.Length * 2);
			}

			var priority = selector_(item);
			heap_[Count] = item;
			++Count;

			int c = Count - 1;
			while (c > 0) {
				int p = (c - 1) >> 1;
				if (Compare(Priority(p), priority) < 0) {
					heap_[c] = heap_[p];
					c = p;
				} else {
					break;
				}
			}

			heap_[c] = item;
		}

		public TObject Dequeue()
		{
			TObject ret = heap_[0];
			int n = Count - 1;

			var item = heap_[n];
			var priority = Priority(n);
			int p = 0;
			int c = (p << 1) + 1;
			while (c < n) {
				if (c != n - 1 && Compare(Priority(c + 1), Priority(c)) > 0) {
					++c;
				}

				if (Compare(priority, Priority(c)) < 0) {
					heap_[p] = heap_[c];
					p = c;
					c = (p << 1) + 1;
				} else {
					break;
				}
			}

			heap_[p] = item;
			Count--;

			return ret;
		}

		public void Clear() => Count = 0;

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private TPriority Priority(int index)
			=> selector_(heap_[index]);

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private int Compare(TPriority x, TPriority y)
			=> x.CompareTo(y) * reverseFactory_;

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		private void Extend(int newSize)
		{
			var newHeap = new TObject[newSize];
			heap_.CopyTo(newHeap, 0);
			heap_ = newHeap;
		}
	}

	public class Imos2D
	{
		private readonly long[,] map_;
		private readonly long[,] accum_;
		private readonly int w_;
		private readonly int h_;

		public long this[int y, int x] => map_[y, x];
		public Imos2D(int h, int w)
		{
			w_ = w;
			h_ = h;
			map_ = new long[h + 1, w + 1];
			accum_ = new long[h + 1, w + 1];
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public void Add(Range row, Range column, long value)
			=> Add(row.Start.Value, column.Start.Value, row.End.Value, column.End.Value, value);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public void Add(int y1, int x1, int y2, int x2, long value)
		{
			if (x1 >= x2 || x1 >= w_ || x2 <= 0 || y1 >= y2 || y1 >= h_ || y2 <= 0) {
				return;
			}

			map_[y1, x1] += value;
			map_[y1, x2] -= value;
			map_[y2, x1] -= value;
			map_[y2, x2] += value;
		}

		public void Build()
		{
			for (int i = 0; i < h_; i++) {
				for (int j = 0; j < w_; j++) {
					map_[i, j + 1] += map_[i, j];
				}
			}

			for (int i = 0; i < h_; i++) {
				for (int j = 0; j < w_; j++) {
					map_[i + 1, j] += map_[i, j];
				}
			}

			for (int i = 0; i < h_; i++) {
				for (int j = 0; j < w_; j++) {
					accum_[i + 1, j + 1]
						+= accum_[i + 1, j] + accum_[i, j + 1] - accum_[i, j] + map_[i, j];
				}
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Query(Range row, Range column)
			=> Query(row.Start.Value, column.Start.Value, row.End.Value, column.End.Value);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Query(int y1, int x1, int y2, int x2)
		{
			if (x1 >= x2 || x1 >= w_ || x2 <= 0 || y1 >= y2 || y1 >= h_ || y2 <= 0) {
				return 0;
			}

			return accum_[y2, x2] - accum_[y1, x2] - accum_[y2, x1] + accum_[y1, x1];
		}
	}

	public struct BitFlag
	{
		public static BitFlag Begin() => 0;
		public static BitFlag End(int bitCount) => 1 << bitCount;
		public static BitFlag FromBit(int bitNumber) => 1 << bitNumber;
		public static BitFlag Fill(int count) => (1 << count) - 1;

		public static IEnumerable<BitFlag> All(int n)
		{
			for (var f = Begin(); f < End(n); ++f) {
				yield return f;
			}
		}

		private readonly int flags_;
		public int Flag => flags_;
		public bool this[int bitNumber] => (flags_ & (1 << bitNumber)) != 0;
		public BitFlag(int flags) { flags_ = flags; }

		public bool Has(BitFlag target) => (flags_ & target.flags_) == target.flags_;
		public bool Has(int target) => (flags_ & target) == target;
		public bool HasBit(int bitNumber) => (flags_ & (1 << bitNumber)) != 0;
		public BitFlag OrBit(int bitNumber) => flags_ | (1 << bitNumber);
		public BitFlag AndBit(int bitNumber) => flags_ & (1 << bitNumber);
		public BitFlag XorBit(int bitNumber) => flags_ ^ (1 << bitNumber);
		public BitFlag ComplementOf(BitFlag sub) => flags_ ^ sub.flags_;
		public int PopCount() => BitOperations.PopCount((uint)flags_);

		public static BitFlag operator ++(BitFlag src) => new BitFlag(src.flags_ + 1);
		public static BitFlag operator --(BitFlag src) => new BitFlag(src.flags_ - 1);
		public static BitFlag operator |(BitFlag lhs, BitFlag rhs)
			=> new BitFlag(lhs.flags_ | rhs.flags_);
		public static BitFlag operator |(BitFlag lhs, int rhs)
			=> new BitFlag(lhs.flags_ | rhs);
		public static BitFlag operator |(int lhs, BitFlag rhs)
			=> new BitFlag(lhs | rhs.flags_);
		public static BitFlag operator &(BitFlag lhs, BitFlag rhs)
			=> new BitFlag(lhs.flags_ & rhs.flags_);
		public static BitFlag operator &(BitFlag lhs, int rhs)
			=> new BitFlag(lhs.flags_ & rhs);
		public static BitFlag operator &(int lhs, BitFlag rhs)
			=> new BitFlag(lhs & rhs.flags_);
		public static BitFlag operator ^(BitFlag lhs, BitFlag rhs)
			=> new BitFlag(lhs.flags_ ^ rhs.flags_);
		public static BitFlag operator ^(BitFlag lhs, int rhs)
			=> new BitFlag(lhs.flags_ ^ rhs);
		public static BitFlag operator ^(int lhs, BitFlag rhs)
			=> new BitFlag(lhs ^ rhs.flags_);
		public static BitFlag operator <<(BitFlag bit, int shift) => bit.flags_ << shift;
		public static BitFlag operator >>(BitFlag bit, int shift) => bit.flags_ >> shift;

		public static bool operator <(BitFlag lhs, BitFlag rhs) => lhs.flags_ < rhs.flags_;
		public static bool operator <(BitFlag lhs, int rhs) => lhs.flags_ < rhs;
		public static bool operator <(int lhs, BitFlag rhs) => lhs < rhs.flags_;
		public static bool operator >(BitFlag lhs, BitFlag rhs) => lhs.flags_ > rhs.flags_;
		public static bool operator >(BitFlag lhs, int rhs) => lhs.flags_ > rhs;
		public static bool operator >(int lhs, BitFlag rhs) => lhs > rhs.flags_;
		public static bool operator <=(BitFlag lhs, BitFlag rhs) => lhs.flags_ <= rhs.flags_;
		public static bool operator <=(BitFlag lhs, int rhs) => lhs.flags_ <= rhs;
		public static bool operator <=(int lhs, BitFlag rhs) => lhs <= rhs.flags_;
		public static bool operator >=(BitFlag lhs, BitFlag rhs) => lhs.flags_ >= rhs.flags_;
		public static bool operator >=(BitFlag lhs, int rhs) => lhs.flags_ >= rhs;
		public static bool operator >=(int lhs, BitFlag rhs) => lhs >= rhs.flags_;

		public static implicit operator BitFlag(int t) => new BitFlag(t);
		public static implicit operator int(BitFlag t) => t.flags_;

		public override string ToString() => $"{Convert.ToString(flags_, 2).PadLeft(32, '0')} ({flags_})";

		public SubBitsEnumerator SubBits => new SubBitsEnumerator(flags_);
		public struct SubBitsEnumerator : IEnumerable<BitFlag>
		{
			private readonly int flags_;
			public SubBitsEnumerator(int flags)
			{
				flags_ = flags;
			}

			IEnumerator<BitFlag> IEnumerable<BitFlag>.GetEnumerator() => new Enumerator(flags_);
			IEnumerator IEnumerable.GetEnumerator() => new Enumerator(flags_);
			public Enumerator GetEnumerator() => new Enumerator(flags_);
			public struct Enumerator : IEnumerator<BitFlag>
			{
				private readonly int src_;
				public BitFlag Current { get; private set; }
				object IEnumerator.Current => Current;

				public Enumerator(int flags)
				{
					src_ = flags;
					Current = flags + 1;
				}

				public void Dispose() { }
				[MethodImpl(MethodImplOptions.AggressiveInlining)]
				public bool MoveNext() => (Current = --Current & src_) > 0;
				[MethodImpl(MethodImplOptions.AggressiveInlining)]
				public void Reset() => Current = src_;
			}
		}
	}

	public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
	{
		private readonly Func<TKey, TValue> initialzier_;
		public HashMap(Func<TKey, TValue> initialzier)
			: base()
		{
			initialzier_ = initialzier;
		}

		public HashMap(Func<TKey, TValue> initialzier, int capacity)
			: base(capacity)
		{
			initialzier_ = initialzier;
		}

		new public TValue this[TKey key]
		{
			get
			{
				if (TryGetValue(key, out TValue value)) {
					return value;
				} else {
					var init = initialzier_(key);
					base[key] = init;
					return init;
				}
			}

			set { base[key] = value; }
		}

		public HashMap<TKey, TValue> Merge(
			HashMap<TKey, TValue> src,
			Func<TValue, TValue, TValue> mergeValues)
		{
			foreach (var key in src.Keys) {
				this[key] = mergeValues(this[key], src[key]);
			}

			return this;
		}
	}

	public class JagList2<T> where T : struct
	{
		private readonly int n_;
		private readonly List<T>[] tempValues_;
		private T[][] values_;

		public int Count => n_;
		public List<T>[] Raw => tempValues_;
		public T[][] Values => values_;
		public T[] this[int index] => values_[index];

		public JagList2(int n)
		{
			n_ = n;
			tempValues_ = new List<T>[n];
			for (int i = 0; i < n; ++i) {
				tempValues_[i] = new List<T>();
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public void Add(int i, T value) => tempValues_[i].Add(value);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public void Build()
		{
			values_ = new T[n_][];
			for (int i = 0; i < values_.Length; ++i) {
				values_[i] = tempValues_[i].ToArray();
			}
		}
	}

	public class DijkstraQ
	{
		private int count_ = 0;
		private long[] distanceHeap_;
		private int[] vertexHeap_;

		public int Count => count_;
		public DijkstraQ()
		{
			distanceHeap_ = new long[8];
			vertexHeap_ = new int[8];
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public void Enqueue(long distance, int v)
		{
			if (distanceHeap_.Length == count_) {
				var newDistanceHeap = new long[distanceHeap_.Length << 1];
				var newVertexHeap = new int[vertexHeap_.Length << 1];
				Unsafe.CopyBlock(
					ref Unsafe.As<long, byte>(ref newDistanceHeap[0]),
					ref Unsafe.As<long, byte>(ref distanceHeap_[0]),
					(uint)(8 * count_));
				Unsafe.CopyBlock(
					ref Unsafe.As<int, byte>(ref newVertexHeap[0]),
					ref Unsafe.As<int, byte>(ref vertexHeap_[0]),
					(uint)(4 * count_));
				distanceHeap_ = newDistanceHeap;
				vertexHeap_ = newVertexHeap;
			}

			ref var dRef = ref distanceHeap_[0];
			ref var vRef = ref vertexHeap_[0];
			Unsafe.Add(ref dRef, count_) = distance;
			Unsafe.Add(ref vRef, count_) = v;
			++count_;

			int c = count_ - 1;
			while (c > 0) {
				int p = (c - 1) >> 1;
				var tempD = Unsafe.Add(ref dRef, p);
				if (tempD <= distance) {
					break;
				} else {
					Unsafe.Add(ref dRef, c) = tempD;
					Unsafe.Add(ref vRef, c) = Unsafe.Add(ref vRef, p);
					c = p;
				}
			}

			Unsafe.Add(ref dRef, c) = distance;
			Unsafe.Add(ref vRef, c) = v;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long distance, int v) Dequeue()
		{
			ref var dRef = ref distanceHeap_[0];
			ref var vRef = ref vertexHeap_[0];
			(long distance, int v) ret = (dRef, vRef);
			int n = count_ - 1;

			var distance = Unsafe.Add(ref dRef, n);
			var vertex = Unsafe.Add(ref vRef, n);
			int p = 0;
			int c = (p << 1) + 1;
			while (c < n) {
				if (c != n - 1 && Unsafe.Add(ref dRef, c + 1) < Unsafe.Add(ref dRef, c)) {
					++c;
				}

				var tempD = Unsafe.Add(ref dRef, c);
				if (distance > tempD) {
					Unsafe.Add(ref dRef, p) = tempD;
					Unsafe.Add(ref vRef, p) = Unsafe.Add(ref vRef, c);
					p = c;
					c = (p << 1) + 1;
				} else {
					break;
				}
			}

			Unsafe.Add(ref dRef, p) = distance;
			Unsafe.Add(ref vRef, p) = vertex;
			--count_;

			return ret;
		}
	}

	public struct ModInt
	{
		//public const long P = 1000000007;
		public const long P = 998244353;
		//public const long P = 2;
		public const long ROOT = 3;

		// (924844033, 5)
		// (998244353, 3)
		// (1012924417, 5)
		// (167772161, 3)
		// (469762049, 3)
		// (1224736769, 3)

		private long value_;

		public static ModInt New(long value, bool mods) => new ModInt(value, mods);
		public ModInt(long value) => value_ = value;
		public ModInt(long value, bool mods)
		{
			if (mods) {
				value %= P;
				if (value < 0) {
					value += P;
				}
			}

			value_ = value;
		}

		public static ModInt operator +(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = (lhs.value_ + rhs.value_) % P;
			return lhs;
		}
		public static ModInt operator +(long lhs, ModInt rhs)
		{
			rhs.value_ = (lhs + rhs.value_) % P;
			return rhs;
		}
		public static ModInt operator +(ModInt lhs, long rhs)
		{
			lhs.value_ = (lhs.value_ + rhs) % P;
			return lhs;
		}

		public static ModInt operator -(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = (P + lhs.value_ - rhs.value_) % P;
			return lhs;
		}
		public static ModInt operator -(long lhs, ModInt rhs)
		{
			rhs.value_ = (P + lhs - rhs.value_) % P;
			return rhs;
		}
		public static ModInt operator -(ModInt lhs, long rhs)
		{
			lhs.value_ = (P + lhs.value_ - rhs) % P;
			return lhs;
		}

		public static ModInt operator *(ModInt lhs, ModInt rhs)
		{
			lhs.value_ = lhs.value_ * rhs.value_ % P;
			return lhs;
		}
		public static ModInt operator *(long lhs, ModInt rhs)
		{
			rhs.value_ = lhs * rhs.value_ % P;
			return rhs;
		}
		public static ModInt operator *(ModInt lhs, long rhs)
		{
			lhs.value_ = lhs.value_ * rhs % P;
			return lhs;
		}

		public static ModInt operator /(ModInt lhs, ModInt rhs)
			=> lhs * Inverse(rhs);

		public static implicit operator ModInt(long n) => new ModInt(n, true);

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static ModInt Inverse(ModInt value) => Pow(value, P - 2);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static ModInt Pow(ModInt value, long k) => Pow(value.value_, k);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static ModInt Pow(long value, long k)
		{
			long ret = 1;
			while (k > 0) {
				if ((k & 1) != 0) {
					ret = ret * value % P;
				}

				value = value * value % P;
				k >>= 1;
			}

			return new ModInt(ret);
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long ToLong() => value_;
		public override string ToString() => value_.ToString();
	}

	public static class Helper
	{
		public static long INF => 1L << 50;

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T Clamp<T>(this T value, T min, T max) where T : struct, IComparable<T>
		{
			if (value.CompareTo(min) <= 0) {
				return min;
			}

			if (value.CompareTo(max) >= 0) {
				return max;
			}

			return value;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMin<T>(this ref T target, T value) where T : struct, IComparable<T>
			=> target = target.CompareTo(value) > 0 ? value : target;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMin<T>(this ref T target, T value, Action<T> onUpdated)
			where T : struct, IComparable<T>
		{
			if (target.CompareTo(value) > 0) {
				target = value;
				onUpdated(value);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMax<T>(this ref T target, T value) where T : struct, IComparable<T>
			=> target = target.CompareTo(value) < 0 ? value : target;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void UpdateMax<T>(this ref T target, T value, Action<T> onUpdated)
			where T : struct, IComparable<T>
		{
			if (target.CompareTo(value) < 0) {
				target = value;
				onUpdated(value);
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static long BinarySearchOKNG(long ok, long ng, Func<long, bool> satisfies)
		{
			while (ng - ok > 1) {
				long mid = (ok + ng) / 2;
				if (satisfies(mid)) {
					ok = mid;
				} else {
					ng = mid;
				}
			}

			return ok;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static long BinarySearchNGOK(long ng, long ok, Func<long, bool> satisfies)
		{
			while (ok - ng > 1) {
				long mid = (ok + ng) / 2;
				if (satisfies(mid)) {
					ok = mid;
				} else {
					ng = mid;
				}
			}

			return ok;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[] Array1<T>(int n, T initialValue) where T : struct
			=> new T[n].Fill(initialValue);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[] Array1<T>(int n, Func<int, T> initializer)
			=> Enumerable.Range(0, n).Select(x => initializer(x)).ToArray();
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[] Fill<T>(this T[] array, T value)
			where T : struct
		{
			array.AsSpan().Fill(value);
			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,] Array2<T>(int n, int m, T initialValule) where T : struct
			=> new T[n, m].Fill(initialValule);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,] Array2<T>(int n, int m, Func<int, int, T> initializer)
		{
			var array = new T[n, m];
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < m; ++j) {
					array[i, j] = initializer(i, j);
				}
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,] Fill<T>(this T[,] array, T initialValue)
			where T : struct
		{
			MemoryMarshal.CreateSpan<T>(ref array[0, 0], array.Length).Fill(initialValue);
			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static Span<T> AsSpan<T>(this T[,] array, int i)
			=> MemoryMarshal.CreateSpan<T>(ref array[i, 0], array.GetLength(1));

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,,] Array3<T>(int n1, int n2, int n3, T initialValue)
			where T : struct
			=> new T[n1, n2, n3].Fill(initialValue);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,,] Fill<T>(this T[,,] array, T initialValue)
			where T : struct
		{
			MemoryMarshal.CreateSpan<T>(ref array[0, 0, 0], array.Length).Fill(initialValue);
			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static Span<T> AsSpan<T>(this T[,,] array, int i, int j)
			=> MemoryMarshal.CreateSpan<T>(ref array[i, j, 0], array.GetLength(2));

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,,,] Array4<T>(int n1, int n2, int n3, int n4, T initialValue)
			where T : struct
			=> new T[n1, n2, n3, n4].Fill(initialValue);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[,,,] Fill<T>(this T[,,,] array, T initialValue)
			where T : struct
		{
			MemoryMarshal.CreateSpan<T>(ref array[0, 0, 0, 0], array.Length).Fill(initialValue);
			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static Span<T> AsSpan<T>(this T[,,,] array, int i, int j, int k)
			=> MemoryMarshal.CreateSpan<T>(ref array[i, j, k, 0], array.GetLength(3));

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static T[] Merge<T>(ReadOnlySpan<T> first, ReadOnlySpan<T> second) where T : IComparable<T>
		{
			var ret = new T[first.Length + second.Length];
			int p = 0;
			int q = 0;
			while (p < first.Length || q < second.Length) {
				if (p == first.Length) {
					ret[p + q] = second[q];
					q++;
					continue;
				}

				if (q == second.Length) {
					ret[p + q] = first[p];
					p++;
					continue;
				}

				if (first[p].CompareTo(second[q]) < 0) {
					ret[p + q] = first[p];
					p++;
				} else {
					ret[p + q] = second[q];
					q++;
				}
			}

			return ret;
		}

		private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static IEnumerable<(int i, int j)> Adjacence4(int i, int j, int imax, int jmax)
		{
			for (int dn = 0; dn < 4; ++dn) {
				int d4i = i + delta4_[dn];
				int d4j = j + delta4_[dn + 1];
				if ((uint)d4i < (uint)imax && (uint)d4j < (uint)jmax) {
					yield return (d4i, d4j);
				}
			}
		}

		private static readonly int[] delta8_ = { 1, 0, -1, 0, 1, 1, -1, -1, 1 };
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static IEnumerable<(int i, int j)> Adjacence8(int i, int j, int imax, int jmax)
		{
			for (int dn = 0; dn < 8; ++dn) {
				int d8i = i + delta8_[dn];
				int d8j = j + delta8_[dn + 1];
				if ((uint)d8i < (uint)imax && (uint)d8j < (uint)jmax) {
					yield return (d8i, d8j);
				}
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static IEnumerable<int> SubBitsOf(int bit)
		{
			for (int sub = bit; sub > 0; sub = --sub & bit) {
				yield return sub;
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static string Reverse(string src)
		{
			var chars = src.ToCharArray();
			for (int i = 0, j = chars.Length - 1; i < j; ++i, --j) {
				var tmp = chars[i];
				chars[i] = chars[j];
				chars[j] = tmp;
			}

			return new string(chars);
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static string Exchange(string src, char a, char b)
		{
			var chars = src.ToCharArray();
			for (int i = 0; i < chars.Length; i++) {
				if (chars[i] == a) {
					chars[i] = b;
				} else if (chars[i] == b) {
					chars[i] = a;
				}
			}

			return new string(chars);
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static void Swap(this string str, int i, int j)
		{
			var span = str.AsWriteableSpan();
			(span[i], span[j]) = (span[j], span[i]);
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static char Replace(this string str, int index, char c)
		{
			var span = str.AsWriteableSpan();
			char old = span[index];
			span[index] = c;
			return old;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static Span<char> AsWriteableSpan(this string str)
		{
			var span = str.AsSpan();
			return MemoryMarshal.CreateSpan(ref MemoryMarshal.GetReference(span), span.Length);
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static string Join<T>(this IEnumerable<T> values, string separator = "")
			=> string.Join(separator, values);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static string JoinNL<T>(this IEnumerable<T> values)
			=> string.Join(Environment.NewLine, values);
	}

	public static class Extensions
	{
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public static Span<T> AsSpan<T>(this List<T> list)
		{
			return Unsafe.As<FakeList<T>>(list).Array.AsSpan(0, list.Count);
		}

		private class FakeList<T>
		{
			public T[] Array = null;
		}
	}

	public class Scanner : IDisposable
	{
		private const int BUFFER_SIZE = 1024;
		private const int ASCII_SPACE = 32;
		private const int ASCII_CHAR_BEGIN = 33;
		private const int ASCII_CHAR_END = 126;
		private readonly string filePath_;
		private readonly Stream stream_;
		private readonly byte[] buf_ = new byte[BUFFER_SIZE];
		private int length_ = 0;
		private int index_ = 0;
		private bool isEof_ = false;

		public Scanner(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				stream_ = Console.OpenStandardInput();
			} else {
				filePath_ = file;
				stream_ = new FileStream(file, FileMode.Open);
			}

			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush = false
			});
		}

		public void Dispose()
		{
			Console.Out.Flush();
			stream_.Dispose();
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public string NextLine()
		{
			var sb = new StringBuilder();
			for (var b = Char(); b >= ASCII_SPACE && b <= ASCII_CHAR_END; b = (char)Read()) {
				sb.Append(b);
			}

			return sb.ToString();
		}


		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public char Char()
		{
			byte b;
			do {
				b = Read();
			} while (b < ASCII_CHAR_BEGIN || ASCII_CHAR_END < b);

			return (char)b;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public string String()
		{
			var sb = new StringBuilder();
			for (var b = Char(); b >= ASCII_CHAR_BEGIN && b <= ASCII_CHAR_END; b = (char)Read()) {
				sb.Append(b);
			}

			return sb.ToString();
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public string[] ArrayString(int length)
		{
			var array = new string[length];
			for (int i = 0; i < length; ++i) {
				array[i] = String();
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int Int() => (int)Long();
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int Int(int offset) => Int() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int) Int2(int offset = 0)
			=> (Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int, int) Int3(int offset = 0)
			=> (Int(offset), Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int, int, int) Int4(int offset = 0)
			=> (Int(offset), Int(offset), Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (int, int, int, int, int) Int5(int offset = 0)
			=> (Int(offset), Int(offset), Int(offset), Int(offset), Int(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public int[] ArrayInt(int length, int offset = 0)
		{
			var array = new int[length];
			for (int i = 0; i < length; ++i) {
				array[i] = Int(offset);
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Long()
		{
			long ret = 0;
			byte b;
			bool ng = false;
			do {
				b = Read();
			} while (b != '-' && (b < '0' || '9' < b));

			if (b == '-') {
				ng = true;
				b = Read();
			}

			for (; true; b = Read()) {
				if (b < '0' || '9' < b) {
					return ng ? -ret : ret;
				} else {
					ret = ret * 10 + b - '0';
				}
			}
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long Long(long offset) => Long() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long) Long2(long offset = 0)
			=> (Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long, long) Long3(long offset = 0)
			=> (Long(offset), Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long, long, long) Long4(long offset = 0)
			=> (Long(offset), Long(offset), Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (long, long, long, long, long) Long5(long offset = 0)
			=> (Long(offset), Long(offset), Long(offset), Long(offset), Long(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public long[] ArrayLong(int length, long offset = 0)
		{
			var array = new long[length];
			for (int i = 0; i < length; ++i) {
				array[i] = Long(offset);
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public BigInteger Big() => new BigInteger(Long());
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public BigInteger Big(long offset) => Big() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (BigInteger, BigInteger) Big2(long offset = 0)
			=> (Big(offset), Big(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (BigInteger, BigInteger, BigInteger) Big3(long offset = 0)
			=> (Big(offset), Big(offset), Big(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (BigInteger, BigInteger, BigInteger, BigInteger) Big4(long offset = 0)
			=> (Big(offset), Big(offset), Big(offset), Big(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (BigInteger, BigInteger, BigInteger, BigInteger, BigInteger) Big5(long offset = 0)
			=> (Big(offset), Big(offset), Big(offset), Big(offset), Big(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public BigInteger[] ArrayBig(int length, long offset = 0)
		{
			var array = new BigInteger[length];
			for (int i = 0; i < length; ++i) {
				array[i] = Big(offset);
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double Double() => double.Parse(String(), CultureInfo.InvariantCulture);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double Double(double offset) => Double() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double) Double2(double offset = 0)
			=> (Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double, double) Double3(double offset = 0)
			=> (Double(offset), Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double, double, double) Double4(double offset = 0)
			=> (Double(offset), Double(offset), Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (double, double, double, double, double) Double5(double offset = 0)
			=> (Double(offset), Double(offset), Double(offset), Double(offset), Double(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public double[] ArrayDouble(int length, double offset = 0)
		{
			var array = new double[length];
			for (int i = 0; i < length; ++i) {
				array[i] = Double(offset);
			}

			return array;
		}

		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public decimal Decimal() => decimal.Parse(String(), CultureInfo.InvariantCulture);
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public decimal Decimal(decimal offset) => Decimal() + offset;
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (decimal, decimal) Decimal2(decimal offset = 0)
			=> (Decimal(offset), Decimal(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (decimal, decimal, decimal) Decimal3(decimal offset = 0)
			=> (Decimal(offset), Decimal(offset), Decimal(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (decimal, decimal, decimal, decimal) Decimal4(decimal offset = 0)
			=> (Decimal(offset), Decimal(offset), Decimal(offset), Decimal(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public (decimal, decimal, decimal, decimal, decimal) Decimal5(decimal offset = 0)
			=> (Decimal(offset), Decimal(offset), Decimal(offset), Decimal(offset), Decimal(offset));
		[MethodImpl(MethodImplOptions.AggressiveInlining)]
		public decimal[] ArrayDecimal(int length, decimal offset = 0)
		{
			var array = new decimal[length];
			for (int i = 0; i < length; ++i) {
				array[i] = Decimal(offset);
			}

			return array;
		}

		private byte Read()
		{
			if (isEof_) {
				throw new EndOfStreamException();
			}

			if (index_ >= length_) {
				index_ = 0;
				if ((length_ = stream_.Read(buf_, 0, BUFFER_SIZE)) <= 0) {
					isEof_ = true;
					return 0;
				}
			}

			return buf_[index_++];
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}
0