結果
問題 | No.1141 田グリッド |
ユーザー | terry_u16 |
提出日時 | 2020-07-31 22:47:08 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 671 ms / 2,000 ms |
コード長 | 17,854 bytes |
コンパイル時間 | 3,322 ms |
コンパイル使用メモリ | 118,144 KB |
実行使用メモリ | 36,608 KB |
最終ジャッジ日時 | 2024-07-06 20:08:00 |
合計ジャッジ時間 | 14,060 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 34 ms
19,840 KB |
testcase_01 | AC | 34 ms
19,968 KB |
testcase_02 | AC | 33 ms
19,968 KB |
testcase_03 | AC | 34 ms
20,096 KB |
testcase_04 | AC | 34 ms
19,712 KB |
testcase_05 | AC | 34 ms
20,096 KB |
testcase_06 | AC | 34 ms
19,968 KB |
testcase_07 | AC | 33 ms
19,968 KB |
testcase_08 | AC | 61 ms
20,352 KB |
testcase_09 | AC | 49 ms
20,224 KB |
testcase_10 | AC | 63 ms
20,736 KB |
testcase_11 | AC | 79 ms
20,844 KB |
testcase_12 | AC | 60 ms
20,480 KB |
testcase_13 | AC | 539 ms
36,608 KB |
testcase_14 | AC | 563 ms
28,800 KB |
testcase_15 | AC | 666 ms
25,984 KB |
testcase_16 | AC | 661 ms
25,856 KB |
testcase_17 | AC | 662 ms
25,472 KB |
testcase_18 | AC | 190 ms
25,472 KB |
testcase_19 | AC | 189 ms
26,112 KB |
testcase_20 | AC | 189 ms
25,984 KB |
testcase_21 | AC | 671 ms
25,856 KB |
testcase_22 | AC | 664 ms
25,856 KB |
testcase_23 | AC | 666 ms
25,728 KB |
testcase_24 | AC | 535 ms
26,112 KB |
testcase_25 | AC | 540 ms
34,816 KB |
testcase_26 | AC | 532 ms
25,344 KB |
testcase_27 | AC | 529 ms
25,472 KB |
testcase_28 | AC | 539 ms
34,944 KB |
testcase_29 | AC | 403 ms
27,776 KB |
testcase_30 | AC | 406 ms
25,088 KB |
testcase_31 | AC | 274 ms
28,800 KB |
testcase_32 | AC | 492 ms
26,112 KB |
testcase_33 | AC | 491 ms
25,856 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using YukicoderContest259.Extensions; using YukicoderContest259.Questions; namespace YukicoderContest259.Questions { public class QuestionC : AtCoderQuestionBase { public override IEnumerable<object> Solve(TextReader inputStream) { var (height, width) = inputStream.ReadValue<int, int>(); var a = new int[height][]; for (int i = 0; i < a.Length; i++) { a[i] = inputStream.ReadIntArray(); } var leftTopSum = new Modular[height + 2, width + 2]; var rightTopSum = new Modular[height + 2, width + 2]; var leftBottomSum = new Modular[height + 2, width + 2]; var rightBottomSum = new Modular[height + 2, width + 2]; for (int row = 0; row < height + 2; row++) { for (int column = 0; column < width + 2; column++) { leftTopSum[row, column] = 1; rightTopSum[row, column] = 1; leftBottomSum[row, column] = 1; rightBottomSum[row, column] = 1; } } for (int row = 0; row < height; row++) { for (int column = 0; column < width; column++) { leftTopSum[row + 1, column + 1] = leftTopSum[row, column + 1] * leftTopSum[row + 1, column] * a[row][column]; if (leftTopSum[row, column] != Modular.Zero) { leftTopSum[row + 1, column + 1] /= leftTopSum[row, column]; } } } for (int row = 0; row < height; row++) { for (int column = width - 1; column >= 0; column--) { rightTopSum[row + 1, column + 1] = rightTopSum[row, column + 1] * rightTopSum[row + 1, column + 2] * a[row][column]; if (rightTopSum[row, column + 2] != Modular.Zero) { rightTopSum[row + 1, column + 1] /= rightTopSum[row, column + 2]; } } } for (int row = height - 1; row >= 0; row--) { for (int column = 0; column < width; column++) { leftBottomSum[row + 1, column + 1] = leftBottomSum[row + 2, column + 1] * leftBottomSum[row + 1, column] * a[row][column]; if (leftBottomSum[row + 2, column] != Modular.Zero) { leftBottomSum[row + 1, column + 1] /= leftBottomSum[row + 2, column]; } } } for (int row = height - 1; row >= 0; row--) { for (int column = width - 1; column >= 0; column--) { rightBottomSum[row + 1, column + 1] = rightBottomSum[row + 2, column + 1] * rightBottomSum[row + 1, column + 2] * a[row][column]; if (rightBottomSum[row + 2, column + 2] != Modular.Zero) { rightBottomSum[row + 1, column + 1] /= rightBottomSum[row + 2, column + 2]; } } } var queries = inputStream.ReadInt(); for (int q = 0; q < queries; q++) { var (r, c) = inputStream.ReadValue<int, int>(); yield return leftTopSum[r - 1, c - 1] * rightTopSum[r - 1, c + 1] * leftBottomSum[r + 1, c - 1] * rightBottomSum[r + 1, c + 1]; } } public readonly struct Modular : IEquatable<Modular>, IComparable<Modular> { private const int DefaultMod = 1000000007; public int Value { get; } public static int Mod { get; set; } = DefaultMod; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Modular(long value) { if (unchecked((ulong)value) < unchecked((ulong)Mod)) { Value = (int)value; } else { Value = (int)(value % Mod); if (Value < 0) { Value += Mod; } } } private Modular(int value) => Value = value; public static Modular Zero => new Modular(0); public static Modular One => new Modular(1); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Modular operator +(Modular a, Modular b) { var result = a.Value + b.Value; if (result >= Mod) { result -= Mod; // 剰余演算を避ける } return new Modular(result); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Modular operator -(Modular a, Modular b) { var result = a.Value - b.Value; if (result < 0) { result += Mod; // 剰余演算を避ける } return new Modular(result); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Modular operator *(Modular a, Modular b) => new Modular((long)a.Value * b.Value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Modular operator /(Modular a, Modular b) => a * Pow(b.Value, Mod - 2); // 需要は不明だけど一応 public static bool operator ==(Modular left, Modular right) => left.Equals(right); public static bool operator !=(Modular left, Modular right) => !(left == right); public static bool operator <(Modular left, Modular right) => left.CompareTo(right) < 0; public static bool operator <=(Modular left, Modular right) => left.CompareTo(right) <= 0; public static bool operator >(Modular left, Modular right) => left.CompareTo(right) > 0; public static bool operator >=(Modular left, Modular right) => left.CompareTo(right) >= 0; public static implicit operator Modular(long a) => new Modular(a); public static explicit operator int(Modular a) => a.Value; public static explicit operator long(Modular a) => a.Value; public static Modular Pow(int a, int n) { switch (n) { case 0: return Modular.One; case 1: return a; case int m when m >= 0: // ジャンプテーブル化はできなくなる var p = Pow(a, m >> 1); // m / 2 return p * p * Pow(a, m & 0x01); // m % 2 default: throw new ArgumentOutOfRangeException(nameof(n), $"べき指数{nameof(n)}は0以上の整数でなければなりません。"); } } private static List<int> _factorialCache; private static List<int> FactorialCache => _factorialCache ??= new List<int>() { 1 }; private static int[] FactorialInverseCache { get; set; } const int defaultMaxFactorial = 1000000; public static Modular Factorial(int n) { if (n < 0) { throw new ArgumentOutOfRangeException(nameof(n), $"{nameof(n)}は0以上の整数でなければなりません。"); } for (int i = FactorialCache.Count; i <= n; i++) // Countが1(0!までキャッシュ済み)のとき1!~n!まで計算 { FactorialCache.Add((int)((long)FactorialCache[i - 1] * i % Mod)); } return new Modular(FactorialCache[n]); } public static Modular Permutation(int n, int r) { CheckNR(n, r); return Factorial(n) / Factorial(n - r); } public static Modular Combination(int n, int r) { CheckNR(n, r); r = Math.Min(r, n - r); try { return new Modular(FactorialCache[n]) * new Modular(FactorialInverseCache[r]) * new Modular(FactorialInverseCache[n - r]); } catch (Exception ex) when (ex is NullReferenceException || ex is ArgumentOutOfRangeException) { throw new InvalidOperationException($"{nameof(Combination)}を呼び出す前に{nameof(InitializeCombinationTable)}により前計算を行う必要があります。", ex); } } public static void InitializeCombinationTable(int max = defaultMaxFactorial) { Factorial(max); FactorialInverseCache = new int[max + 1]; var fInv = (Modular.One / Factorial(max)).Value; FactorialInverseCache[max] = fInv; for (int i = max - 1; i >= 0; i--) { fInv = (int)((long)fInv * (i + 1) % Mod); FactorialInverseCache[i] = fInv; } } public static Modular CombinationWithRepetition(int n, int r) => Combination(n + r - 1, r); private static void CheckNR(int n, int r) { if (n < 0) { throw new ArgumentOutOfRangeException(nameof(n), $"{nameof(n)}は0以上の整数でなければなりません。"); } if (r < 0) { throw new ArgumentOutOfRangeException(nameof(r), $"{nameof(r)}は0以上の整数でなければなりません。"); } if (n < r) { throw new ArgumentOutOfRangeException($"{nameof(n)},{nameof(r)}", $"{nameof(r)}は{nameof(n)}以下でなければなりません。"); } } public override string ToString() => Value.ToString(); public override bool Equals(object obj) => obj is Modular m ? Equals(m) : false; public bool Equals([System.Diagnostics.CodeAnalysis.AllowNull] Modular other) => Value == other.Value; public int CompareTo([System.Diagnostics.CodeAnalysis.AllowNull] Modular other) => Value.CompareTo(other.Value); public override int GetHashCode() => Value.GetHashCode(); } } } namespace YukicoderContest259 { class Program { static void Main(string[] args) { IAtCoderQuestion question = new QuestionC(); var answers = question.Solve(Console.In); var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(writer); foreach (var answer in answers) { Console.WriteLine(answer); } Console.Out.Flush(); } } } #region Base Class namespace YukicoderContest259.Questions { public interface IAtCoderQuestion { IEnumerable<object> Solve(string input); IEnumerable<object> Solve(TextReader inputStream); } public abstract class AtCoderQuestionBase : IAtCoderQuestion { public IEnumerable<object> Solve(string input) { var stream = new MemoryStream(Encoding.Unicode.GetBytes(input)); var reader = new StreamReader(stream, Encoding.Unicode); return Solve(reader); } public abstract IEnumerable<object> Solve(TextReader inputStream); } } #endregion #region Extensions namespace YukicoderContest259.Extensions { public static class StringExtensions { public static string Join<T>(this IEnumerable<T> source) => string.Concat(source); public static string Join<T>(this IEnumerable<T> source, char separator) => string.Join(separator, source); public static string Join<T>(this IEnumerable<T> source, string separator) => string.Join(separator, source); } public static class TextReaderExtensions { public static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader)); public static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader)); public static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader)); public static string ReadString(this TextReader reader) => reader.ReadLine(); public static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray(); public static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray(); public static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray(); public static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator); // Supports primitive type only. public static T1 ReadValue<T1>(this TextReader reader) => (T1)Convert.ChangeType(reader.ReadLine(), typeof(T1)); public static (T1, T2) ReadValue<T1, T2>(this TextReader reader, char separator = ' ') { var inputs = ReadStringArray(reader, separator); var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1)); var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2)); return (v1, v2); } public static (T1, T2, T3) ReadValue<T1, T2, T3>(this TextReader reader, char separator = ' ') { var inputs = ReadStringArray(reader, separator); var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1)); var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2)); var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3)); return (v1, v2, v3); } public static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>(this TextReader reader, char separator = ' ') { var inputs = ReadStringArray(reader, separator); var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1)); var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2)); var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3)); var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4)); return (v1, v2, v3, v4); } public static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>(this TextReader reader, char separator = ' ') { var inputs = ReadStringArray(reader, separator); var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1)); var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2)); var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3)); var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4)); var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5)); return (v1, v2, v3, v4, v5); } public static (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>(this TextReader reader, char separator = ' ') { var inputs = ReadStringArray(reader, separator); var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1)); var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2)); var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3)); var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4)); var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5)); var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6)); return (v1, v2, v3, v4, v5, v6); } public static (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>(this TextReader reader, char separator = ' ') { var inputs = ReadStringArray(reader, separator); var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1)); var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2)); var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3)); var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4)); var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5)); var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6)); var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7)); return (v1, v2, v3, v4, v5, v6, v7); } public static (T1, T2, T3, T4, T5, T6, T7, T8) ReadValue<T1, T2, T3, T4, T5, T6, T7, T8>(this TextReader reader, char separator = ' ') { var inputs = ReadStringArray(reader, separator); var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1)); var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2)); var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3)); var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4)); var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5)); var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6)); var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7)); var v8 = (T8)Convert.ChangeType(inputs[7], typeof(T8)); return (v1, v2, v3, v4, v5, v6, v7, v8); } } } #endregion