結果

問題 No.800 四平方定理
ユーザー sekiya9311sekiya9311
提出日時 2019-10-20 23:31:04
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,983 ms / 2,000 ms
コード長 7,063 bytes
コンパイル時間 2,760 ms
コンパイル使用メモリ 112,304 KB
実行使用メモリ 133,844 KB
最終ジャッジ日時 2023-09-15 14:50:49
合計ジャッジ時間 29,501 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
17,372 KB
testcase_01 AC 89 ms
19,032 KB
testcase_02 AC 84 ms
18,248 KB
testcase_03 AC 86 ms
18,104 KB
testcase_04 AC 82 ms
17,384 KB
testcase_05 AC 84 ms
18,228 KB
testcase_06 AC 91 ms
19,192 KB
testcase_07 AC 87 ms
19,116 KB
testcase_08 AC 92 ms
19,200 KB
testcase_09 AC 90 ms
19,076 KB
testcase_10 AC 783 ms
66,500 KB
testcase_11 AC 945 ms
77,536 KB
testcase_12 AC 871 ms
74,952 KB
testcase_13 AC 827 ms
73,644 KB
testcase_14 AC 891 ms
75,528 KB
testcase_15 AC 893 ms
75,372 KB
testcase_16 AC 908 ms
75,772 KB
testcase_17 AC 901 ms
75,840 KB
testcase_18 AC 878 ms
75,800 KB
testcase_19 AC 888 ms
75,836 KB
testcase_20 AC 70 ms
21,892 KB
testcase_21 AC 70 ms
16,008 KB
testcase_22 AC 907 ms
69,876 KB
testcase_23 AC 1,794 ms
130,152 KB
testcase_24 AC 1,768 ms
130,916 KB
testcase_25 AC 1,841 ms
133,076 KB
testcase_26 AC 72 ms
23,928 KB
testcase_27 AC 70 ms
21,996 KB
testcase_28 AC 1,983 ms
133,232 KB
testcase_29 AC 1,853 ms
133,844 KB
testcase_30 AC 1,845 ms
128,900 KB
testcase_31 AC 1,680 ms
126,872 KB
testcase_32 AC 1,913 ms
127,332 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;

namespace ProgrammingContestTemplateByDotNetCore
{

    #region "Input and Output Classes"

    class Scanner : IDisposable
    {
        private readonly Queue<string> _buffer;
        private readonly char[] _sep;
        private readonly TextReader _reader;

        public Scanner(TextReader reader = null, char[] sep = null)
        {
            _buffer = new Queue<string>();
            _sep = sep ?? new[] { ' ' };
            _reader = reader ?? Console.In;
        }

        public Scanner(string path, char[] sep = null)
            : this(new StreamReader(path), sep) { }

        private void CheckBuffer()
        {
            if (_buffer.Any() || _reader.Peek() == -1)
                return;

            string str = string.Empty;
            for (;
                string.IsNullOrWhiteSpace(str);
                str = _reader.ReadLine()) { }

            var values = str.Split(_sep).Where(s => !string.IsNullOrWhiteSpace(s));
            foreach (var item in values)
            {
                _buffer.Enqueue(item);
            }
        }

        public string Next()
        {
            CheckBuffer();
            return _buffer.Dequeue();
        }
        public string[] GetStringArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => Next())
                .ToArray();

        public int NextInt() => int.Parse(Next());
        public int[] GetIntArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextInt())
                .ToArray();

        public long NextLong() => long.Parse(Next());
        public long[] GetLongArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextLong())
                .ToArray();

        public double NextDouble() => double.Parse(Next());
        public double[] GetDoubleArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextDouble())
                .ToArray();

        public decimal NextDecimal() => decimal.Parse(Next());
        public decimal[] GetDecimalArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextDecimal())
                .ToArray();

        public BigInteger NextBigInt() => BigInteger.Parse(Next());
        public BigInteger[] GetBigIntArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextBigInt())
                .ToArray();

        public bool IsEnd
        {
            get
            {
                CheckBuffer();
                return !_buffer.Any();
            }
        }

        public void Dispose()
        {
            if (!_reader.Equals(Console.In))
                _reader.Dispose();
        }
    }

    class Writer : IDisposable
    {
        private readonly TextWriter _writer;
        private readonly StringBuilder _cache;
        private readonly bool _isReactive;

        public Writer(TextWriter writer = null, bool isReactive = false)
        {
            _writer = writer ?? Console.Out;
            _isReactive = isReactive;
            if (!_isReactive)
                _cache = new StringBuilder();
        }

        public Writer(string path)
            : this(new StreamWriter(path)) { }

        public Writer(bool isReactive)
            : this(null, isReactive) { }

        public void Write(object value)
        {
            if (_isReactive)
            {
                _writer.Write(value);
                _writer.Flush();
            }
            else
            {
                _cache.Append(value);
            }
        }

        public void WriteFormat(string format, params object[] values)
        {
            var value = string.Format(format, values);
            Write(value);
        }

        public void WriteLine(object value = null)
        {
            Write($"{value}{Environment.NewLine}");
        }

        public void WriteLine(string format, params object[] values)
        {
            WriteFormat($"{format}{Environment.NewLine}", values);
        }

        public void Dispose()
        {
            if (!_isReactive)
            {
                _writer.Write(_cache);
                _writer.Flush();
            }
            if (!_writer.Equals(Console.Out))
            {
                _writer.Dispose();
            }
        }
    }

    public static class Extensions
    {
        public static void Set<TKey, TValue>(
            this IDictionary<TKey, TValue> source,
            TKey key, TValue value)
        {
            if (source.ContainsKey(key))
                source[key] = value;
            else
                source.Add(key, value);
        }


    }

#endregion

    class MainClass : IDisposable
    {
#region "Template"

        /// <summary> 入力を受け取る </summary>
        private readonly Scanner sc;
        /// <summary> 出力を行う </summary>
        private readonly Writer wr;
        private const string _inputFile = "input.txt";
        private const string _outFile = "output.txt";

        static void Main(string[] args)
        {
            using (new MainClass()) { }
        }

        public MainClass()
        {
            wr = new Writer(_isReactive);
            // TODO: ファイルに出力したい場合はこっち → wr = new Writer(_outFile);

#if DEBUG
            // 手元では、ファイルから入力を受け取る
            sc = new Scanner(_inputFile);
#else

            // 提出時、標準入力から受け取る
            sc = new Scanner();
#endif

            Solve();
        }

        public void Dispose()
        {
            sc?.Dispose();
            wr?.Dispose();
        }

        #endregion

        void Solve()
        {
            int N = sc.NextInt();
            int D = sc.NextInt();

            Dictionary<int, int> xy = new Dictionary<int, int>();
            {
                for (int x = 1; x <= N; x++)
                {
                    for (int y = 1; y <= N; y++)
                    {
                        var key = x * x + y * y;
                        xy.Set(key, xy.GetValueOrDefault(key) + 1);
                    }
                }
            }

            Dictionary<int, int> wz = new Dictionary<int, int>();
            {
                for (int w = 1; w <= N; w++)
                {
                    for (int z = 1; z <= N; z++)
                    {
                        var key = w * w - z * z + D;
                        wz.Set(key, wz.GetValueOrDefault(key) + 1);
                    }
                }
            }

            var ans = xy
                .Select(e => 1L * e.Value * wz.GetValueOrDefault(e.Key))
                .Sum();

            wr.WriteLine(ans);
        }

        /// <summary>
        /// TODO: リアクティブ問題か否かを指定してね♪
        /// </summary>
        private const bool _isReactive = false;
    }
}
0