結果

問題 No.16 累乗の加算
ユーザー AreTrashAreTrash
提出日時 2018-03-14 23:52:22
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 65 ms / 5,000 ms
コード長 4,374 bytes
コンパイル時間 3,974 ms
コンパイル使用メモリ 111,048 KB
実行使用メモリ 23,596 KB
最終ジャッジ日時 2023-09-08 12:14:13
合計ジャッジ時間 5,734 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
21,460 KB
testcase_01 AC 64 ms
19,508 KB
testcase_02 AC 63 ms
23,596 KB
testcase_03 AC 62 ms
21,620 KB
testcase_04 AC 64 ms
23,500 KB
testcase_05 AC 64 ms
21,548 KB
testcase_06 AC 63 ms
19,516 KB
testcase_07 AC 65 ms
23,528 KB
testcase_08 AC 64 ms
19,616 KB
testcase_09 AC 65 ms
21,428 KB
testcase_10 AC 65 ms
21,480 KB
testcase_11 AC 63 ms
21,600 KB
testcase_12 AC 62 ms
19,376 KB
testcase_13 AC 64 ms
19,412 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.IO;
using System.Collections.Generic;
using System.Linq;

namespace No16
{
    public class Program
    {
        void Solve(StreamScanner ss, StreamWriter sw)
        {
            //---------------------------------
            ModInt.Mod = 1000003;
            var X = ss.Next(ModInt.Parse);
            var N = ss.Next(int.Parse);
            var A = ss.Next(int.Parse, N);
            sw.WriteLine(A.Aggregate((ModInt)0, (res, a) => res + X.Pow(a)));
            //---------------------------------
        }

        static void Main()
        {
            var ss = new StreamScanner(new StreamReader(Console.OpenStandardInput()));
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            new Program().Solve(ss, sw);
            sw.Flush();
        }
    }

    public static partial class MathX
    {
        public static long GcdEx(long a, long b, out long x, out long y)
        {
            if (b == 0)
            {
                x = 1;
                y = 0;
                return a;
            }

            long tx, ty;
            var gcd = GcdEx(b, a % b, out tx, out ty);

            x = ty;
            y = tx - ty * (a / b);
            return gcd;
        }
    }

    public struct ModInt
    {
        public static int Mod = 1000000007;

        readonly long value;
        public int Value { get { return (int)value; } }

        public ModInt Inverse { get { return GetInverse(); } }

        public ModInt(long value)
        {
            value %= Mod;
            this.value = value < 0 ? value + Mod : value;
        }

        ModInt GetInverse()
        {
            long x, y;
            if (MathX.GcdEx(value, Mod, out x, out y) != 1)
            {
                throw new InvalidOperationException("'Value' and 'Mod' must be disjoint.");
            }
            return x;
        }

        public ModInt Square()
        {
            return this * this;
        }

        public ModInt Pow(long exp)
        {
            if (exp == 0) return 1;
            if (exp % 2 == 0) return Pow(exp / 2).Square();
            return this * Pow(exp - 1);
        }

        public static ModInt Parse(string str)
        {
            return int.Parse(str);
        }

        public static implicit operator ModInt(long value)
        {
            return new ModInt(value);
        }

        public static ModInt operator +(ModInt left, ModInt right)
        {
            return left.value + right.value;
        }

        public static ModInt operator -(ModInt left, ModInt right)
        {
            return left.value - right.value;
        }

        public static ModInt operator *(ModInt left, ModInt right)
        {
            return left.value * right.value;
        }

        public static ModInt operator /(ModInt left, ModInt right)
        {
            return left * right.Inverse;
        }

        public override bool Equals(object obj)
        {
            if (!(obj is ModInt)) return false;
            return value == ((ModInt)obj).value;
        }

        public override int GetHashCode()
        {
            return value.GetHashCode();
        }

        public override string ToString()
        {
            return value.ToString();
        }
    }

    public class StreamScanner
    {
        static readonly char[] Sep = {' '};
        readonly Queue<string> buffer = new Queue<string>();
        readonly TextReader textReader;

        public StreamScanner(TextReader textReader)
        {
            this.textReader = textReader;
        }

        public T Next<T>(Func<string, T> parser)
        {
            if (buffer.Count != 0) return parser(buffer.Dequeue());
            var nextStrings = textReader.ReadLine().Split(Sep, StringSplitOptions.RemoveEmptyEntries);
            foreach (var nextString in nextStrings) buffer.Enqueue(nextString);
            return Next(parser);
        }

        public T[] Next<T>(Func<string, T> parser, int x)
        {
            var ret = new T[x];
            for (var i = 0; i < x; ++i) ret[i] = Next(parser);
            return ret;
        }

        public T[][] Next<T>(Func<string, T> parser, int x, int y)
        {
            var ret = new T[y][];
            for (var i = 0; i < y; ++i) ret[i] = Next(parser, x);
            return ret;
        }
    }
}
0