結果

問題 No.2330 Eat Slime
ユーザー kakel-sankakel-san
提出日時 2023-11-18 20:51:20
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 11,608 bytes
コンパイル時間 18,593 ms
コンパイル使用メモリ 168,980 KB
実行使用メモリ 282,204 KB
最終ジャッジ日時 2024-09-26 05:56:19
合計ジャッジ時間 55,242 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
30,080 KB
testcase_01 AC 67 ms
30,464 KB
testcase_02 AC 67 ms
30,464 KB
testcase_03 AC 65 ms
30,336 KB
testcase_04 AC 67 ms
30,336 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 1,936 ms
111,488 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 300 ms
61,180 KB
testcase_12 AC 1,945 ms
109,572 KB
testcase_13 AC 1,892 ms
111,360 KB
testcase_14 AC 485 ms
72,356 KB
testcase_15 AC 1,084 ms
95,608 KB
testcase_16 AC 1,901 ms
107,904 KB
testcase_17 WA -
testcase_18 AC 2,031 ms
123,392 KB
testcase_19 WA -
testcase_20 AC 2,016 ms
123,596 KB
testcase_21 WA -
testcase_22 AC 2,006 ms
123,344 KB
testcase_23 AC 2,033 ms
123,336 KB
testcase_24 AC 1,997 ms
123,352 KB
testcase_25 AC 1,991 ms
123,480 KB
testcase_26 AC 1,995 ms
123,352 KB
testcase_27 AC 2,002 ms
123,264 KB
testcase_28 AC 2,036 ms
123,344 KB
testcase_29 AC 2,018 ms
123,216 KB
testcase_30 AC 2,068 ms
123,200 KB
testcase_31 AC 2,070 ms
282,204 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (150 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
        // Test();
    }
    static void Test()
    {
        var r = new Random();
        var count = 0;
        while (true)
        {
            var n = 16;
            var m = 16;
            var x = r.Next(100);
            var a = new int[n];
            for (var i = 0; i < n; ++i) a[i] = 1;
            var map = new int[m][];
            for (var i = 0; i < m; ++i) map[i] = new int[] { i + 1, 1, 100 };
            // 愚直解
            var s1 = All(n, m, x, a, map);
            // 作成解
            var s2 = Slimes(n, m, x, a, map);
            if (s1 != s2)
            {
                WriteLine(n + " " + m + " " + x);
                WriteLine(string.Join(" ", a));
                WriteLine(string.Join("\n", map.Select(m => string.Join(" ", m))));
                WriteLine(s1);
                WriteLine(s2);
                return;
            }
            ++count;
            if (count % 1000 == 0) WriteLine(count);
        }
    }
    static int All(int n, int m, int x, int[] a, int[][] map)
    {
        var ans = 0;
        for (var i = 0; i < n; ++i)
        {
            var sub = x * i;
            for (var j = 0; j < map.Length; ++j)
            {
                var pos = map[j][0] + i - 1;
                if (pos < n && a[pos] == map[j][1]) sub += map[j][2];
            }
            ans = Math.Max(ans, sub);
        }
        return ans;
    }
    static void Solve()
    {
        var c = NList;
        var (n, m, x) = (c[0], c[1], c[2]);
        var a = NList;
        var map = NArr(m);
        WriteLine(Slimes(n, m, x, a, map));
    }
    static long Slimes(int n, int m, int x, int[] a, int[][] map)
    {
        var colors = new long[5][];
        for (var i = 0; i < colors.Length; ++i) colors[i] = new long[n];
        for (var i = 0; i < n; ++i) ++colors[a[i] - 1][i];
        var points = new long[5][];
        for (var i = 0; i < points.Length; ++i) points[i] = new long[n];
        for (var i = 0; i < map.Length; ++i) points[map[i][1] - 1][map[i][0] - 1] += map[i][2];
        for (var i = 0; i < points.Length; ++i) Array.Reverse(points[i]);

        var cmb = new ConvolutionCalculator(998_244_353);
        var combo = new int[5][];
        for (var i = 0; i < combo.Length; ++i) combo[i] = cmb.Calc(colors[i], points[i]);
        var ans = 0;
        for (var j = n - 1; j < combo[0].Length; ++j)
        {
            var sum = (j - n + 1) * x;
            for (var i = 0; i < combo.Length; ++i) sum += combo[i][j];
            ans = Math.Max(ans, sum);
        }
        return ans;
    }
    // 畳み込みを mod で計算する
    class ConvolutionCalculator
    {
        int mod;
        public ConvolutionCalculator(int mod)
        {
            this.mod = mod;
        }
        public int[] Calc(long[] a, long[] b)
        {
            var _a = new int[a.Length];
            var _b = new int[b.Length];
            for (var i = 0; i < a.Length; ++i) _a[i] = (int)(a[i] % mod);
            for (var i = 0; i < b.Length; ++i) _b[i] = (int)(b[i] % mod);
            return Calc(_a, _b);
        }
        public int[] Calc(int[] a, int[] b)
        {
            var n = a.Length;
            var m = b.Length;
            if (n == 0 || m == 0)
            {
                return new int[0];
            }
            if (Math.Min(n, m) <= 100)
            {
                return ShortComvolution(a, b);
            }
            var z = 1 << CeilPow(n + m - 1);
            var za = new int[z];
            var zb = new int[z];
            Array.Copy(a, za, a.Length);
            Array.Copy(b, zb, b.Length);
            InitButterfly();
            Butterfly(za);
            Butterfly(zb);
            for (var i = 0; i < z; ++i)
            {
                za[i] = (int)((long)za[i] * zb[i] % mod);
            }
            ButterflyInv(za);
            var iz = PowMod(z, mod - 2, mod);
            var ans = new int[n + m - 1];
            for (var i = 0; i < ans.Length; ++i)
            {
                ans[i] = (int)((long)za[i] * iz % mod);
            }
            return ans;
        }
        int[] ShortComvolution(int[] a, int[] b)
        {
            var ans = new int[a.Length + b.Length - 1];
            for (var i = 0; i < a.Length; ++i)
            {
                for (var j = 0; j < b.Length; ++j)
                {
                    ans[i + j] = (int)((ans[i + j] + (long)a[i] * b[j] % mod) % mod);
                }
            }
            return ans;
        }
        int[] sume = new int[30];
        int[] sumie = new int[30];
        void InitButterfly()
        {
            var g = PrimitiveRoot(mod);
            var es = new int[30];
            var ies = new int[30];
            var cnt2 = BSF(mod - 1);
            var e = PowMod(g, mod - 1 >> cnt2, mod);
            var ie = PowMod(e, mod - 2, mod);
            for (var i = cnt2; i >= 2; --i)
            {
                es[i - 2] = e;
                ies[i - 2] = ie;
                e = (int)((long)e * e % mod);
                ie = (int)((long)ie * ie % mod);
            }
            var now = 1L;
            var inow = 1L;
            for (var i = 0; i < cnt2 - 2; ++i)
            {
                sume[i] = (int)(es[i] * now % mod);
                now = now * ies[i] % mod;
                sumie[i] = (int)(ies[i] * inow % mod);
                inow = inow * es[i] % mod;
            }
        }

        void Butterfly(int[] a)
        {
            var h = CeilPow(a.Length);

            for (var ph = 1; ph <= h; ++ph)
            {
                var w = 1 << (ph - 1);
                var p = 1 << (h - ph);
                var now = 1L;
                for (var s = 0; s < w; ++s)
                {
                    var offset = s << (h - ph + 1);
                    for (var i = 0; i < p; ++i)
                    {
                        var l = a[i + offset];
                        var r = (int)(a[i + offset + p] * now % mod);
                        a[i + offset] = (l + r) % mod;
                        a[i + offset + p] = (int)((long)mod + l - r) % mod;
                    }
                    now = now * sume[BSF(~(uint)s)] % mod;
                }
            }
        }
        void ButterflyInv(int[] a)
        {
            var h = CeilPow(a.Length);

            for (var ph = h; ph >= 1; --ph)
            {
                var w = 1 << (ph - 1);
                var p = 1 << (h - ph);
                var inow = 1L;
                for (var s = 0; s < w; ++s)
                {
                    var offset = s << (h - ph + 1);
                    for (var i = 0; i < p; ++i)
                    {
                        var l = a[i + offset];
                        var r = a[i + offset + p];
                        a[i + offset] = (l + r) % mod;
                        a[i + offset + p] = (int)(((long)mod + l - r) * inow % mod);
                    }
                    inow = inow * sumie[BSF(~(uint)s)] % mod;
                }
            }
        }
        static int PowMod(int n, int p, int mod)
        {
            long _n = n % mod;
            var _p = p;
            var result = 1L;
            if ((_p & 1) == 1)
            {
                result *= _n;
            }
            while (_p > 0)
            {
                _n = _n * _n % mod;
                _p >>= 1;
                if ((_p & 1) == 1)
                {
                    result = (result * _n) % mod;
                }
            }
            return (int)result;
        }
        static int PrimitiveRoot(int m)
        {
            if (m == 2) return 1;
            if (m == 167772161) return 3;
            if (m == 469762049) return 3;
            if (m == 754974721) return 11;
            if (m == 998244353) return 3;
            if (m == 1000000007) return 5;
            var divs = new int[20];
            divs[0] = 2;
            var cnt = 1;
            var x = (m - 1) / 2;
            while (x % 2 == 0) x /= 2;
            for (var i = 3; (long)i * i <= x; i += 2)
            {
                if (x % i == 0)
                {
                    divs[cnt++] = i;
                    while (x % i == 0) x /= i;
                }
            }
            if (x > 1) divs[cnt++] = x;
            for (var g = 2;; ++g)
            {
                var ok = true;
                for (var i = 0; i < cnt; ++i)
                {
                    if (PowMod(g, (m - 1) / divs[i], m) == 1)
                    {
                        ok = false;
                        break;
                    }
                }
                if (ok) return g;
            }
        }
        static int CeilPow(int n)
        {
            var x = 0;
            while ((1U << x) < (uint)n) x++;
            return x;
        }
        static int BSF(int n)
        {
            if (n == 0) return 0;
            var count = 0;
            while ((n & 1) == 0)
            {
                ++count;
                n >>= 1;
            }
            return count;
        }
        static int BSF(uint n)
        {
            if (n == 0) return 0;
            var count = 0;
            while ((n & 1) == 0)
            {
                ++count;
                n >>= 1;
            }
            return count;
        }
        public long[] ConvolutionLL(long[] a, long[] b)
        {
            if (a.Length == 0 && b.Length == 0) return new long[0];
            const ulong MOD1 = 754_974_721;
            const ulong MOD2 = 167_772_161;
            const ulong MOD3 = 469_762_049;
            const ulong M2M3 = 78_812_994_116_517_889;
            const ulong M1M3 = 354_658_471_880_163_329;
            const ulong M1M2 = 126_663_740_442_542_081;
            const ulong M1M2M3 = 560_135_205_046_714_369;
            const ulong i1 = 190_329_765;
            const ulong i2 = 58_587_104;
            const ulong i3 = 187_290_749;
            ulong[] offset = new ulong[] { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3 };
            var c1 = new ConvolutionCalculator((int)MOD1).Calc(a, b);
            var c2 = new ConvolutionCalculator((int)MOD2).Calc(a, b);
            var c3 = new ConvolutionCalculator((int)MOD3).Calc(a, b);
            var ans = new long[a.Length + b.Length - 1];
            for (var i = 0; i < ans.Length; ++i)
            {
                ulong x = 0;
                x += (ulong)c1[i] * i1 % MOD1 * M2M3;
                x += (ulong)c2[i] * i2 % MOD2 * M1M3;
                x += (ulong)c3[i] * i3 % MOD3 * M1M2;
                var diff = c1[i] - SafeMod((long)x, (long)MOD1);
                if (diff < 0) diff += (long)MOD1;
                x -= offset[diff % 5];
                ans[i] = (long)x;
            }
            return ans;
        }
        static long SafeMod(long a, long b)
        {
            a %= b;
            if (a < 0) a += b;
            return a;
        }
    }
}
0