結果

問題 No.823 Many Shifts Easy
ユーザー Tomohiko HasegawaTomohiko Hasegawa
提出日時 2019-04-27 13:12:06
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 3,624 bytes
コンパイル時間 1,077 ms
コンパイル使用メモリ 64,108 KB
実行使用メモリ 22,748 KB
最終ジャッジ日時 2023-08-18 13:04:19
合計ジャッジ時間 2,693 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,940 KB
testcase_01 AC 56 ms
20,844 KB
testcase_02 AC 57 ms
22,748 KB
testcase_03 AC 66 ms
20,764 KB
testcase_04 AC 57 ms
20,760 KB
testcase_05 AC 63 ms
20,884 KB
testcase_06 AC 64 ms
20,668 KB
testcase_07 AC 56 ms
20,752 KB
testcase_08 AC 58 ms
20,832 KB
testcase_09 AC 57 ms
20,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

namespace AtTest.yukicoder
{
    class _823
    {
        static void Main(string[] args)
        {
            Method(args);
        }

        static void Method(string[] args)
        {
            long[] nk = ReadLongs();
            long n = nk[0];
            long k = nk[1];
            long mask = 1000000000 + 7;
            long res = n*(n+1) / 2;
            res %= mask;
            res = MultiMod(res, Permutation(n, k, mask), mask);
            if (k == 1)
            {
                res -= n*(n+1) / 2;
            }
            else
            {
                long delta = (n - 1) * n / 2;
                delta %= mask;
                delta = MultiMod(delta,
                    MultiMod(Combination(k, 2, mask),
                    Permutation(n - 2, k - 2, mask), mask)
                    + MultiMod(k,
                    Permutation(n - 2, k - 1, mask), mask),
                    mask);
                delta += MultiMod(n,
                    MultiMod(k,
                    Permutation(n - 1, k - 1, mask), mask),
                    mask);
                delta %= mask;
                res -= delta;
            }
            while (res < 0) res += mask;
            WriteLine(res);
        }

        static long Combination(long n, long m, long mask)
        {
            if (n < m) return 0;

            if (n - m < m) m = n - m;

            long val = Permutation(n, m, mask);
            long div = Permutation(m, m, mask);
            return MultiMod(val, ReverseMod(div, mask - 2, mask), mask);
        }

        static long Permutation(long n, long m, long mask)
        {
            long val = 1;
            for (long i = 0; i < m; i++)
            {
                val *= ((n - i) % mask);
                val %= mask;
            }
            return val;
        }

        static long[] AllPermutations(long n, long mask)
        {
            var perms = new long[n + 1];
            perms[0] = 1;
            for (int i = 0; i <= n; i++)
            {
                perms[i] = MultiMod(perms[i - 1], i, mask);
            }
            return perms;
        }

        static long MultiMod(long a, long b, long mask)
        {
            return ((a % mask) * (b % mask)) % mask;
        }

        static long ReverseMod(long a, long pow, long mask)
        {
            if (pow == 0) return 1;
            else if (pow == 1) return a % mask;
            else
            {
                long halfMod = ReverseMod(a, pow / 2, mask);
                long nextMod = MultiMod(halfMod, halfMod, mask);
                if (pow % 2 == 0)
                {
                    return nextMod;
                }
                else
                {
                    return MultiMod(nextMod, a, mask);
                }
            }
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}
0