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.Runtime.Intrinsics.X86; using System.Text; namespace YukiCoder { class Program { static void Main() { using var cin = new Scanner(); var (l, r) = cin.Long2(); long ans = 0; ans += Prime.CountPrimeLucy(r); ans -= Prime.CountPrimeLucy(l - 1); ans += Prime.CountPrimeLucy(2 * r); ans -= Prime.CountPrimeLucy(2 * l); Console.WriteLine(ans); } } public static class Prime { static readonly HashSet smallPrimes_ = new HashSet { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsPrime(long value) { if (value < 2) { return false; } for (long i = 2; i * i <= value; i++) { if (value % i == 0) { return false; } } return true; } static readonly long[] sprpBase1 = { 126401071349994536 }; static readonly long[] sprpBase2 = { 336781006125, 9639812373923155 }; static readonly long[] sprpBase3 = { 2, 2570940, 211991001, 3749873356 }; static readonly long[] sprpBase4 = { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 }; public static bool IsPrimeByMillerRabin(long value) { if (value < 2) { return false; } if (smallPrimes_.Contains(value)) { return true; } long d = value - 1; int count2 = 0; while (d % 2 == 0) { d /= 2; count2++; } long[] v = value <= 291831L ? sprpBase1 : value <= 1050535501L ? sprpBase2 : value <= 47636622961201 ? sprpBase3 : sprpBase4; foreach (var a in v) { if (a == value) { return true; } long temp = PowMod(a, d, value); if (temp == 1) { continue; } bool ok = true; for (int r = 0; r < count2; r++) { if (temp == value - 1) { ok = false; break; } temp = PowMod(temp, 2, value); } if (ok) { return false; } } return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static HashSet PrimeFactor(long value) { var factors = new HashSet(); for (long i = 2; i * i <= value; ++i) { if (value % i == 0) { factors.Add(i); while (value % i == 0) { value /= i; } } } if (value != 1) { factors.Add(value); } return factors; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Dictionary PrimeFactors(long value) { var factors = new Dictionary(); for (long i = 2; i * i <= value; ++i) { while (value % i == 0) { if (factors.ContainsKey(i) == false) { factors[i] = 1; } else { factors[i] += 1; } value /= i; } } if (value != 1) { factors[value] = 1; } return factors; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Dictionary PrimeFactorsByPollardsRho(long value) { static long Next(long x, long p) { return (long)(((BigInteger)x * x + 1) % p); } static long FindFactor(long n) { if (n % 2 == 0) { return 2; } if (IsPrimeByMillerRabin(n)) { return n; } int seed = 0; while (true) { ++seed; long x = seed % n; long y = Next(x, n); long d = 1; while (d == 1) { x = Next(x, n); y = Next(Next(y, n), n); d = Gcd(Math.Abs(x - y), n); } if (d == n) { continue; } return d; } } var ret = new Dictionary(); var que = new Queue(); que.Enqueue(value); while (que.Count > 0) { var target = que.Dequeue(); if (target == 1) { continue; } if (IsPrimeByMillerRabin(target)) { if (ret.ContainsKey(target)) { ret[target]++; } else { ret.Add(target, 1); } continue; } long f = FindFactor(target); que.Enqueue(f); que.Enqueue(target / f); } return ret; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long CountPrimeLucy(long n) { if (n <= 1) { return 0; } long n_sqrt = FloorSqrt(n); var larges = new long[n_sqrt + 1]; for (int i = 1; i <= n_sqrt; i++) { larges[i] = n / i - 1; } var smalls = new long[n / n_sqrt]; for (int i = 0; i < n / n_sqrt; i++) { smalls[i] = i - 1; } for (long p = 2; p <= n_sqrt; p++) { if (p < smalls.Length) { if (smalls[p] <= smalls[p - 1]) { continue; } } else { if (larges[n / p] <= smalls[p - 1]) { continue; } } long pc = smalls[p - 1]; long q = p * p; for (int i = 1; i <= n_sqrt; i++) { if (n / i < q) { break; } long ip = i * p; long cur = (larges.Length <= ip ? smalls[n / ip] : larges[ip]) - pc; if (larges.Length <= i) { smalls[n / i] -= cur; } else { larges[i] -= cur; } } for (int i = smalls.Length - 1; i >= 0; i--) { if (i < q) { break; } long cur = smalls[i / p] - pc; smalls[i] -= cur; } } return larges[1]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long FloorSqrt(long value) { if (value < 0) { return -1; } long ok = 0; long ng = 3000000000; while (ng - ok > 1) { long mid = (ng + ok) / 2; if (mid * mid <= value) { ok = mid; } else { ng = mid; } } return ok; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Mod(long x, long p) { x %= p; if (x < 0) { x += p; } return x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long PowMod(long x, long n, long p) { if (p == 1) { return 0; } if (p < int.MaxValue) { var barrett = new BarrettReduction((uint)p); uint r = 1; uint y = (uint)Mod(x, p); while (0 < n) { if ((n & 1) != 0) { r = barrett.Multilply(r, y); } y = barrett.Multilply(y, y); n >>= 1; } return r; } else { BigInteger ret = 1; BigInteger mul = x % p; while (n != 0) { if ((n & 1) == 1) { ret = ret * mul % p; } mul = mul * mul % p; n >>= 1; } return (long)ret; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static long Gcd(long a, long b) { if (b == 0) { return a; } return Gcd(b, a % b); } public class BarrettReduction { private readonly ulong im_; public uint Mod { get; private set; } public BarrettReduction(uint m) { Mod = m; im_ = unchecked((ulong)-1) / m + 1; } public uint Multilply(uint a, uint b) { ulong z = a; z *= b; if (!Bmi2.X64.IsSupported) { return (uint)(z % Mod); } var x = Bmi2.X64.MultiplyNoFlags(z, im_); var v = unchecked((uint)(z - x * Mod)); if (Mod <= v) { v += Mod; } return v; } } } public static class ModCounting { private const long p_ = ModInt.P; private static ModInt[] factorial_; private static ModInt[] inverseFactorial_; private static ModInt[] inverse_; private static ModInt[] montmort_; public static void InitializeFactorial(long max, bool withInverse = false) { if (withInverse) { factorial_ = new ModInt[max + 1]; inverseFactorial_ = new ModInt[max + 1]; inverse_ = new ModInt[max + 1]; factorial_[0] = factorial_[1] = 1; inverseFactorial_[0] = inverseFactorial_[1] = 1; inverse_[1] = 1; for (int i = 2; i <= max; i++) { factorial_[i] = factorial_[i - 1] * i; inverse_[i] = p_ - inverse_[p_ % i] * (p_ / i); inverseFactorial_[i] = inverseFactorial_[i - 1] * inverse_[i]; } } else { factorial_ = new ModInt[max + 1]; inverseFactorial_ = new ModInt[max + 1]; factorial_[0] = factorial_[1] = 1; for (int i = 2; i <= max; i++) { factorial_[i] = factorial_[i - 1] * i; } inverseFactorial_[max] = new ModInt(1) / factorial_[max]; for (long i = max - 1; i >= 0; i--) { inverseFactorial_[i] = inverseFactorial_[i + 1] * (i + 1); } } } public static void InitializeMontmort(long max) { montmort_ = new ModInt[Math.Max(3, max + 1)]; montmort_[0] = 1; montmort_[1] = 0; for (int i = 2; i < max + 1; i++) { montmort_[i] = (i - 1) * (montmort_[i - 1] + montmort_[i - 2]); } } public static ModInt Factorial(long n) { if (n < 0) { return 0; } return factorial_[n]; } public static ModInt InverseFactorial(long n) { if (n < 0) { return 0; } return inverseFactorial_[n]; } public static ModInt Inverse(long n) { if (n < 0) { return 0; } return inverse_[n]; } public static ModInt Montmort(long n) { if (n < 0) { return 0; } return montmort_[n]; } public static ModInt Permutation(long n, long k) { if (n < k || (n < 0 || k < 0)) { return 0; } return factorial_[n] * inverseFactorial_[n - k]; } public static ModInt RepeatedPermutation(long n, long k) { long ret = 1; for (k %= p_ - 1; k > 0; k >>= 1, n = n * n % p_) { if ((k & 1) == 1) { ret = ret * n % p_; } } return ret; } public static ModInt Combination(long n, long k) { if (n < k || (n < 0 || k < 0)) { return 0; } return factorial_[n] * inverseFactorial_[k] * inverseFactorial_[n - k]; } public static ModInt CombinationK(long n, long k) { ModInt ret = 1; for (int i = 0; i < k; i++) { ret *= (n - i) % p_; ret *= inverse_[i + 1]; } return ret; } public static ModInt HomogeneousProduct(long n, long k) { if (n < 0 || k < 0) { return 0; } return Combination(n + k - 1, k); } public static ModInt HomogeneousProductK(long n, long k) { if (n < 0 || k < 0) { return 0; } return CombinationK(n + k - 1, k); } } public static class Divisor { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static HashSet Divisors(long value) { var divisors = new HashSet(); for (long i = 1; i * i <= value; ++i) { if (value % i == 0) { divisors.Add(i); if (i != value / i) { divisors.Add(value / i); } } } return divisors; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Gcd(long a, long b) { if (b == 0) { return a; } return Gcd(b, a % b); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Gcd(long[] values) { if (values.Length == 1) { return values[0]; } long gcd = values[0]; for (int i = 1; i < values.Length; ++i) { if (gcd == 1) { return gcd; } gcd = Gcd(values[i], gcd); } return gcd; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Lcm(long a, long b, long limit = 1000000000000000000L) { long gcd = Gcd(a, b); if (limit / (a / gcd) < b) { return -1; } else { return a / gcd * b; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static long Lcm(long[] values, long limit) { if (values.Length == 1) { return values[0]; } long lcm = values[0]; for (int i = 1; i < values.Length; i++) { lcm = Lcm(lcm, values[i], limit); if (lcm > limit || lcm < 0) { return -1; } } return lcm; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List PrimeFactorsToDivisors( Dictionary factors, bool sorts = false) { var count = factors.Keys.Count; var divisors = new List(); if (count == 0) { return divisors; } var keys = factors.Keys.ToArray(); void Dfs(int c, long v) { if (c == count) { divisors.Add(v); return; } Dfs(c + 1, v); for (int i = 0; i < factors[keys[c]]; i++) { v *= keys[c]; Dfs(c + 1, v); } } Dfs(0, 1); if (sorts) { divisors.Sort(); } return divisors; } } public class Eratosthenes { public static (bool[] isPrime, List primes) Sift(long n) { var isPrime = new bool[n + 1]; isPrime.AsSpan().Fill(true); var primes = new List((int)Math.Sqrt(n)); for (long i = 2; i <= n; i++) { if (isPrime[i]) { primes.Add((int)i); for (long j = i * i; j <= n; j += i) { isPrime[j] = false; } } } return (isPrime, primes); } private readonly long[] primes_; private readonly long[] minPrimeFactors_; public ReadOnlySpan Primes => primes_; public Eratosthenes(long n) { minPrimeFactors_ = new long[n + 1]; minPrimeFactors_[0] = -1; minPrimeFactors_[1] = -1; var tempPrimes = new List(); for (long d = 2; d <= n; d++) { if (minPrimeFactors_[d] == 0) { tempPrimes.Add(d); minPrimeFactors_[d] = d; for (long j = d * d; j <= n; j += d) { if (minPrimeFactors_[j] == 0) { minPrimeFactors_[j] = d; } } } } primes_ = tempPrimes.ToArray(); } public bool IsPrime(long n) { return minPrimeFactors_[n] == n; } public IReadOnlyList PrimeFactorsOf(long n) { var factors = new List(); while (n > 1) { factors.Add(minPrimeFactors_[n]); n /= minPrimeFactors_[n]; } return factors; } public Dictionary PrimeFactors(long n) { var factors = new Dictionary(); var list = PrimeFactorsOf(n); foreach (long value in list) { if (factors.ContainsKey(value)) { factors[value]++; } else { factors[value] = 1; } } return factors; } public Dictionary PrimeFactorsOfLcm(ReadOnlySpan values) { var factors = new Dictionary(); foreach (long value in values) { var temp = PrimeFactors(value); foreach (long key in temp.Keys) { if (factors.ContainsKey(key)) { factors[key] = Math.Max(factors[key], temp[key]); } else { factors[key] = temp[key]; } } } return factors; } } 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; 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 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_})"; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void ForEachSubBits(Action action) { for (BitFlag sub = (flags_ - 1) & flags_; sub > 0; sub = --sub & flags_) { action(sub); } } public SubBitsEnumerator SubBits => new SubBitsEnumerator(flags_); public struct SubBitsEnumerator : IEnumerable { private readonly int flags_; public SubBitsEnumerator(int flags) { flags_ = flags; } IEnumerator IEnumerable.GetEnumerator() => new Enumerator(flags_); IEnumerator IEnumerable.GetEnumerator() => new Enumerator(flags_); public Enumerator GetEnumerator() => new Enumerator(flags_); public struct Enumerator : IEnumerator { private readonly int src_; public BitFlag Current { get; private set; } object IEnumerator.Current => Current; public Enumerator(int flags) { src_ = flags; Current = flags; } public void Dispose() { } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool MoveNext() => (Current = --Current & src_) > 0; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reset() => Current = src_; } } } public class HashMap : Dictionary { private readonly Func initialzier_; public HashMap(Func initialzier) : base() { initialzier_ = initialzier; } public HashMap(Func 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 Merge( HashMap src, Func mergeValues) { foreach (var key in src.Keys) { this[key] = mergeValues(this[key], src[key]); } return this; } } public class JagList2 where T : struct { private readonly int n_; private readonly List[] tempValues_; private T[][] values_; public int Count => n_; public List[] Raw => tempValues_; public T[][] Values => values_; public T[] this[int index] => values_[index]; public JagList2(int n) { n_ = n; tempValues_ = new List[n]; for (int i = 0; i < n; ++i) { tempValues_[i] = new List(); } } [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(ref newDistanceHeap[0]), ref Unsafe.As(ref distanceHeap_[0]), (uint)(8 * count_)); Unsafe.CopyBlock( ref Unsafe.As(ref newVertexHeap[0]), ref Unsafe.As(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 = 10; 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 << 60; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Clamp(this T value, T min, T max) where T : struct, IComparable { if (value.CompareTo(min) <= 0) { return min; } if (value.CompareTo(max) >= 0) { return max; } return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UpdateMin(this ref T target, T value) where T : struct, IComparable => target = target.CompareTo(value) > 0 ? value : target; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UpdateMin(this ref T target, T value, Action onUpdated) where T : struct, IComparable { if (target.CompareTo(value) > 0) { target = value; onUpdated(value); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UpdateMax(this ref T target, T value) where T : struct, IComparable => target = target.CompareTo(value) < 0 ? value : target; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void UpdateMax(this ref T target, T value, Action onUpdated) where T : struct, IComparable { if (target.CompareTo(value) < 0) { target = value; onUpdated(value); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Array1(int n, T initialValue) where T : struct => new T[n].Fill(initialValue); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Array1(int n, Func initializer) => Enumerable.Range(0, n).Select(x => initializer(x)).ToArray(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Fill(this T[] array, T value) where T : struct { array.AsSpan().Fill(value); return array; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[,] Array2(int n, int m, T initialValule) where T : struct => new T[n, m].Fill(initialValule); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[,] Array2(int n, int m, Func 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(this T[,] array, T initialValue) where T : struct { MemoryMarshal.CreateSpan(ref array[0, 0], array.Length).Fill(initialValue); return array; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span AsSpan(this T[,] array, int i) => MemoryMarshal.CreateSpan(ref array[i, 0], array.GetLength(1)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[,,] Array3(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(this T[,,] array, T initialValue) where T : struct { MemoryMarshal.CreateSpan(ref array[0, 0, 0], array.Length).Fill(initialValue); return array; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span AsSpan(this T[,,] array, int i, int j) => MemoryMarshal.CreateSpan(ref array[i, j, 0], array.GetLength(2)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[,,,] Array4(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(this T[,,,] array, T initialValue) where T : struct { MemoryMarshal.CreateSpan(ref array[0, 0, 0, 0], array.Length).Fill(initialValue); return array; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span AsSpan(this T[,,,] array, int i, int j, int k) => MemoryMarshal.CreateSpan(ref array[i, j, k, 0], array.GetLength(3)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T[] Merge(ReadOnlySpan first, ReadOnlySpan second) where T : IComparable { 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 void DoIn4(int i, int j, int imax, int jmax, Action action) { 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) { action(d4i, d4j); } } } private static readonly int[] delta8_ = { 1, 0, -1, 0, 1, 1, -1, -1, 1 }; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void DoIn8(int i, int j, int imax, int jmax, Action action) { 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) { action(d8i, d8j); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ForEachSubBits(int bit, Action action) { for (int sub = bit; sub >= 0; --sub) { sub &= bit; action(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 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 AsWriteableSpan(this string str) { var span = str.AsSpan(); return MemoryMarshal.CreateSpan(ref MemoryMarshal.GetReference(span), span.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string Join(this IEnumerable values, string separator = "") => string.Join(separator, values); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string JoinNL(this IEnumerable values) => string.Join(Environment.NewLine, values); } public static class Extensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span AsSpan(this List list) { return Unsafe.As>(list).Array.AsSpan(0, list.Count); } private class FakeList { public T[] Array = null; } } public class Scanner : IDisposable { private const int BUFFER_SIZE = 1024; 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 char Char() { byte b; do { b = Read(); } while (b < ASCII_CHAR_BEGIN || ASCII_CHAR_END < b); return (char)b; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public string Next() { 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] = Next(); } 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[] 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[] 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[] 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(Next(), 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[] 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(Next(), 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[] 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); } } }