結果

問題 No.856 増える演算
ユーザー claw88claw88
提出日時 2019-08-15 17:24:56
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 16,044 bytes
コンパイル時間 2,189 ms
コンパイル使用メモリ 117,376 KB
実行使用メモリ 57,272 KB
最終ジャッジ日時 2023-10-19 19:11:55
合計ジャッジ時間 53,001 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
25,516 KB
testcase_01 AC 37 ms
25,516 KB
testcase_02 AC 32 ms
25,516 KB
testcase_03 AC 32 ms
25,516 KB
testcase_04 AC 31 ms
25,516 KB
testcase_05 AC 32 ms
25,516 KB
testcase_06 AC 32 ms
25,516 KB
testcase_07 AC 32 ms
25,516 KB
testcase_08 AC 32 ms
25,516 KB
testcase_09 AC 32 ms
25,516 KB
testcase_10 AC 32 ms
25,516 KB
testcase_11 AC 31 ms
25,516 KB
testcase_12 AC 32 ms
25,516 KB
testcase_13 AC 39 ms
25,516 KB
testcase_14 AC 39 ms
25,516 KB
testcase_15 AC 39 ms
25,516 KB
testcase_16 AC 39 ms
25,516 KB
testcase_17 AC 39 ms
25,516 KB
testcase_18 AC 38 ms
25,516 KB
testcase_19 AC 39 ms
25,516 KB
testcase_20 AC 38 ms
25,516 KB
testcase_21 AC 38 ms
25,516 KB
testcase_22 AC 39 ms
25,516 KB
testcase_23 AC 43 ms
26,632 KB
testcase_24 AC 44 ms
26,876 KB
testcase_25 AC 38 ms
25,916 KB
testcase_26 AC 37 ms
25,848 KB
testcase_27 AC 36 ms
25,844 KB
testcase_28 AC 42 ms
26,628 KB
testcase_29 AC 42 ms
26,524 KB
testcase_30 AC 41 ms
26,436 KB
testcase_31 AC 38 ms
26,184 KB
testcase_32 AC 42 ms
26,716 KB
testcase_33 AC 59 ms
26,876 KB
testcase_34 AC 77 ms
28,324 KB
testcase_35 AC 64 ms
27,148 KB
testcase_36 AC 75 ms
28,032 KB
testcase_37 AC 70 ms
27,692 KB
testcase_38 AC 46 ms
26,384 KB
testcase_39 AC 48 ms
26,660 KB
testcase_40 AC 56 ms
26,876 KB
testcase_41 AC 56 ms
26,876 KB
testcase_42 AC 80 ms
28,576 KB
testcase_43 AC 56 ms
26,876 KB
testcase_44 AC 43 ms
25,844 KB
testcase_45 AC 48 ms
26,636 KB
testcase_46 AC 56 ms
26,872 KB
testcase_47 AC 53 ms
26,872 KB
testcase_48 AC 66 ms
27,276 KB
testcase_49 AC 59 ms
26,876 KB
testcase_50 AC 68 ms
27,460 KB
testcase_51 AC 76 ms
28,136 KB
testcase_52 AC 78 ms
28,448 KB
testcase_53 AC 1,560 ms
50,820 KB
testcase_54 AC 1,489 ms
46,700 KB
testcase_55 AC 1,550 ms
50,568 KB
testcase_56 AC 1,471 ms
46,136 KB
testcase_57 AC 1,563 ms
51,236 KB
testcase_58 AC 1,525 ms
48,184 KB
testcase_59 AC 1,644 ms
55,580 KB
testcase_60 AC 1,498 ms
47,784 KB
testcase_61 AC 1,644 ms
55,828 KB
testcase_62 AC 1,590 ms
55,612 KB
testcase_63 AC 1,356 ms
45,000 KB
testcase_64 AC 1,582 ms
54,272 KB
testcase_65 AC 1,457 ms
46,984 KB
testcase_66 AC 1,500 ms
47,668 KB
testcase_67 AC 1,535 ms
48,888 KB
testcase_68 AC 1,587 ms
55,456 KB
testcase_69 AC 1,585 ms
55,088 KB
testcase_70 AC 1,655 ms
56,924 KB
testcase_71 AC 1,597 ms
55,736 KB
testcase_72 AC 1,572 ms
51,568 KB
testcase_73 AC 1,665 ms
57,200 KB
testcase_74 AC 1,666 ms
57,200 KB
testcase_75 AC 1,663 ms
57,272 KB
testcase_76 AC 1,672 ms
57,200 KB
testcase_77 AC 1,666 ms
57,200 KB
testcase_78 AC 1,664 ms
57,272 KB
testcase_79 AC 1,663 ms
57,200 KB
testcase_80 AC 1,664 ms
57,200 KB
testcase_81 AC 1,666 ms
57,208 KB
testcase_82 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Threading.Tasks;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    void solve()
    {
        int N = cin.nextint;
        var A = cin.scanint;
        int M = A.Max();
        var rmq = new SparseTable<int>(A, M);
        var rsq = new CumulativeSum(A);

        var min = new pair(M, M);
        var cnt = new ModInt[M + 1];

        ModInt ans = 1;
        for (int i = 0; i < A.Length; i++)
        {
            ans *= ModInt.Pow(A[i], rsq[i + 1, N]);
            cnt[A[i]] += 1;
            update(ref min, new pair(A[i], rmq[i + 1, N]));
        }

        ModInt.Mod = mod - 1;
        var num = new NumberTheoreticTransform().ConvoluteWithInt32Mod(cnt, cnt);
        ModInt.Mod = mod;

        for (int i = 0; i < A.Length; i++)
        {
            num[A[i] * 2] -= 1;
        }
        var half = ModInt.Inverse(2);
        for (int i = 0; i < num.Length; i++)
        {
            num[i] *= half;
        }

        for (int i = 0; i < num.Length; i++)
        {
            ans *= ModInt.Pow(i, num[i].num);
        }

        var z = (min.f + min.s) * ModInt.Pow(min.f, min.s);

        //WriteLine(num.Sum() + " " + ModInt.Inverse(z) + " " + ans);
        ans *= ModInt.Inverse(z);

        WriteLine(ans);
    }
    //struct P : IComparable<P>
    //{
    //    public int a, b;
    //    public P(int a, int b)
    //    {
    //        this.a = a;
    //        this.b = b;
    //    }
    //    public int CompareTo(P p)
    //    {

    //    }
    //}
    void update(ref pair a, pair b)
    {

        var p = Log(a.f + a.s) + a.s * Log(a.f);
        var q = Log(b.f + b.s) + b.s * Log(b.f);
        if (q < p) a = new pair(b.f, b.s);
    }

}

class CumulativeSum
{
    readonly long[] dat;
    public CumulativeSum(long[] a)
    {
        dat = new long[a.Length + 1];
        for (int i = 0; i < a.Length; i++)
        {
            dat[i] += dat[i - 1] + a[i];
        }
    }
    public CumulativeSum(int[] a)
    {
        dat = new long[a.Length + 1];
        for (int i = 0; i < a.Length; i++)
        {
            dat[i + 1] += dat[i] + a[i];
        }
    }
    /// <summary>
    /// [s, t)の区間Sum
    /// </summary>
    /// <param name="s"></param>
    /// <param name="t"></param>
    /// <returns></returns>
    public long this[int s, int t] => s < t ? dat[t] - dat[s] : 0;
}
class SparseTable<T> where T : IComparable<T>
{
    const int K = 18;
    T[][] st = new T[K][];
    readonly T ex;
    public SparseTable(T[] a, T ex)
    {
        this.ex = ex;
        int n = a.Length;
        for (int i = 0; i < st.Length; i++)
        {
            st[i] = new T[n];
        }
        Array.Copy(a, st[0], n);
        for (int k = 1; k < K; k++)
        {
            for (int i = 0; i + (1 << k) <= n; i++)
            {
                st[k][i] = st[k - 1][i].CompareTo(st[k - 1][i + (1 << (k - 1))]) < 0 ? st[k - 1][i] : st[k - 1][i + (1 << (k - 1))];
                //st[k][i] = Math.Min(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
            }
        }
    }
    /// <summary>
    /// [s, t)の区間Min
    /// </summary>
    /// <param name="s"></param>
    /// <param name="t"></param>
    /// <returns></returns>
    public T this[int s, int t]
    {
        get
        {
            if (!(s < t)) return ex;
            int k = 31 - builtin_clz((uint)(t - s));
            return st[k][s].CompareTo(st[k][t - (1 << k)]) < 0 ? st[k][s] : st[k][t - (1 << k)];
            //return Math.Min(st[k][a], st[k][b - (1 << k)]);
        }

    }
    int builtin_clz(uint x)
    {
        int i = 0;
        while ((x >> (31 - i) & 1) == 0)
        {
            i++;
        }
        return i;
    }
}

class NumberTheoreticTransform
{
    readonly long mod, primitive_root;
    public NumberTheoreticTransform() { }
    public NumberTheoreticTransform(long mod, int primitive_root)
    {
        this.mod = mod;
        this.primitive_root = primitive_root;
    }
    public ModInt[] ConvoluteWithInt32Mod(ModInt[] A, ModInt[] B)
    {
        long today_mod = ModInt.Mod;
        long m1 = 167772161, m2 = 469762049, m3 = 1224736769;
        var NTT_1 = new NumberTheoreticTransform(m1, 3);
        var NTT_2 = new NumberTheoreticTransform(m2, 3);
        var NTT_3 = new NumberTheoreticTransform(m3, 3);
        ModInt[] x = NTT_1.Convolute(A, B);
        ModInt[] y = NTT_2.Convolute(A, B);
        ModInt[] z = NTT_3.Convolute(A, B);

        // garner
        ModInt.Mod = m2;
        ModInt m1_inv_m2 = ModInt.Inverse(m1);
        ModInt.Mod = m3;
        ModInt m12_inv_m3 = ModInt.Inverse(m1 * m2);
        ModInt.Mod = today_mod;
        ModInt m12_mod = m1 * m2;

        int N = x.Length;
        ModInt[] ret = new ModInt[N];
        for (int i = 0; i < N; i++)
        {
            ModInt.Mod = m2;
            ModInt v1 = (y[i] - x[i]) * m1_inv_m2;
            ModInt.Mod = m3;
            ModInt v2 = (z[i] - (x[i] + m1 * v1)) * m12_inv_m3;
            ModInt.Mod = today_mod;
            ret[i] = x[i] + m1 * v1 + m12_mod * v2;
        }
        return ret;
    }
    public ModInt[] Convolute(ModInt[] A, ModInt[] B)
    {
        ModInt.Mod = mod;
        int sz = A.Length + B.Length - 1;
        int N = 1; while (N < sz) N <<= 1;

        var G = new ModInt[N];
        Array.Copy(A, G, A.Length);
        var H = new ModInt[N];
        Array.Copy(B, H, B.Length);

        NTT(G); NTT(H);
        for (int i = 0; i < N; i++) G[i] *= H[i];
        NTT(G, -1);

        Array.Resize(ref G, sz);
        return G;
    }
    void NTT(ModInt[] F, int rev = 1)
    {
        int N = F.Length;

        ModInt h = ModInt.Pow(primitive_root, (mod - 1) / N);
        if (rev == -1) h = ModInt.Inverse(h);
        for (int i = 0, j = 1; j < N - 1; j++)
        {
            for (int k = N >> 1; k > (i ^= k); k >>= 1) ;
            if (j < i) swap(ref F[i], ref F[j]);
        }

        for (int i = 2; i <= N; i <<= 1)
        {
            int m = i >> 1;
            // zeta = exp(rev * PI / m * i)
            ModInt zeta = ModInt.Pow(h, N / i);

            for (int j = 0; j < N; j += i)
            {
                ModInt zeta_pow = new ModInt(1);
                for (int u = j, v = j + m; v < j + i; u++, v++)
                {
                    ModInt vl = F[u], vr = zeta_pow * F[v];
                    F[u] = vl + vr;
                    F[v] = vl - vr;
                    zeta_pow = zeta_pow * zeta;
                }
            }
        }

        if (rev == -1)
        {
            ModInt n_inv = ModInt.Inverse(N);
            for (int i = 0; i < F.Length; i++)
            {
                F[i] *= n_inv;
            }
        }
    }

    // 2^23より大きく,primitive rootに3を持つもの
    // int[] mods= { 1224736769, 469762049, 167772161, 595591169, 645922817, 897581057, 998244353 };
    void swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
}

class FastFourierTransform
{
    public FastFourierTransform() { }

    public Complex[] Convolute(Complex[] G, Complex[] H)
    {
        int sz = G.Length + H.Length - 1;
        int N = 1; while (N < sz) N <<= 1;
        Array.Resize(ref G, N);
        Array.Resize(ref H, N);

        FFT(G); FFT(H);
        Complex[] F = new Complex[N];
        for (int i = 0; i < N; i++)
        {
            F[i] = G[i] * H[i];
        }
        FFT(F, -1);

        return F;
    }
    public long[] Convolute(long[] P, long[] Q)
    {
        int sz = P.Length + Q.Length - 1;
        int N = 1; while (N < sz) N <<= 1;

        Complex[] G = Array.ConvertAll(P, i => new Complex(i, 0));
        Complex[] H = Array.ConvertAll(Q, i => new Complex(i, 0));
        Array.Resize(ref G, N);
        Array.Resize(ref H, N);

        FFT(G); FFT(H);
        for (int i = 0; i < N; i++) G[i] *= H[i];
        FFT(G, -1);

        Array.Resize(ref G, sz);
        return Array.ConvertAll(G, i => (long)Round(i.Real));
    }
    void FFT(Complex[] F, int rev = 1)
    {
        for (int i = 0, j = 1; j < F.Length - 1; j++)
        {
            for (int k = F.Length >> 1; k > (i ^= k); k >>= 1) ;
            if (j < i) swap(ref F[i], ref F[j]);
        }

        for (int i = 2; i <= F.Length; i <<= 1)
        {
            int m = i >> 1;
            // zeta = exp(rev * PI / m * i)
            Complex zeta = new Complex(Cos(PI / m), Sin(PI / m) * rev);

            for (int j = 0; j < F.Length; j += i)
            {
                Complex zeta_pow = Complex.One;
                for (int u = j, v = j + m; v < j + i; u++, v++)
                {
                    Complex vl = F[u], vr = zeta_pow * F[v];
                    F[u] = vl + vr;
                    F[v] = vl - vr;
                    zeta_pow = zeta_pow * zeta;
                }
            }
        }

        if (rev == -1)
        {
            for (int i = 0; i < F.Length; i++)
            {
                F[i] /= F.Length;
            }
        }
    }

    void swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
}

/// <summary>
/// [0,<see cref="Mod"/>) までの値を取るような数
/// </summary>
/// <OriginalAuthor>camypaper</OriginalAuthor>
struct ModInt
{
    /// <summary>
    /// 剰余を取る値.
    /// </summary>
    public static long Mod = (int)1e9 + 7;

    /// <summary>
    /// 実際の数値.
    /// </summary>
    public long num;
    /// <summary>
    /// 値が <paramref name="n"/> であるようなインスタンスを構築します.
    /// </summary>
    /// <param name="n">インスタンスが持つ値</param>
    /// <remarks>パフォーマンスの問題上,コンストラクタ内では剰余を取りません.そのため,<paramref name="n"/> ∈ [0,<see cref="Mod"/>) を満たすような <paramref name="n"/> を渡してください.このコンストラクタは O(1) で実行されます.</remarks>
    public ModInt(long n) { num = n; }
    /// <summary>
    /// このインスタンスの数値を文字列に変換します.
    /// </summary>
    /// <returns>[0,<see cref="Mod"/>) の範囲内の整数を 10 進表記したもの.</returns>
    public override string ToString() { return num.ToString(); }
    public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; }
    public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; }
    public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); }
    public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); }

    /// <summary>
    /// 与えられた 2 つの数値からべき剰余を計算します.
    /// </summary>
    /// <param name="v">べき乗の底</param>
    /// <param name="k">べき指数</param>
    /// <returns>繰り返し二乗法により O(N log N) で実行されます.</returns>
    public static ModInt Pow(ModInt v, long k) => Pow(v.num, k);

    /// <summary>
    /// 与えられた 2 つの数値からべき剰余を計算します.
    /// </summary>
    /// <param name="v">べき乗の底</param>
    /// <param name="k">べき指数</param>
    /// <returns>繰り返し二乗法により O(N log N) で実行されます.</returns>
    public static ModInt Pow(long v, long k)
    {
        long ret = 1;
        for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod)
            if ((k & 1) == 1) ret = ret * v % Mod;
        return new ModInt(ret);
    }
    /// <summary>
    /// 与えられた数の逆元を計算します.
    /// </summary>
    /// <param name="v">逆元を取る対象となる数</param>
    /// <returns>逆元となるような値</returns>
    /// <remarks>法が素数であることを仮定して,フェルマーの小定理に従って逆元を O(log N) で計算します.</remarks>
    public static ModInt Inverse(ModInt v) => Pow(v, Mod - 2);
}

class BinomialCoefficient
{
    public ModInt[] fact, ifact;
    /// <summary>
    /// <paramref name="n"/>は <paramref name="Mod"/>未満でお願いします。
    /// </summary>
    /// <param name="n"></param>
    public BinomialCoefficient(ModInt _n)
    {
        int n = (int)_n.num;
        fact = new ModInt[n + 1];
        ifact = new ModInt[n + 1];
        fact[0] = 1;
        for (int i = 1; i <= n; i++)
            fact[i] = fact[i - 1] * i;
        ifact[n] = ModInt.Inverse(fact[n]);
        for (int i = n - 1; i >= 0; i--)
            ifact[i] = ifact[i + 1] * (i + 1);
        ifact[0] = ifact[1];
    }
    public ModInt this[int n, int r]
    {
        get
        {
            if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0;
            return fact[n] * ifact[n - r] * ifact[r];
        }
    }
    public ModInt RepeatedCombination(int n, int k)
    {
        if (k == 0) return 1;
        return this[n + k - 1, k];
    }
}

static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}
0