Example #1
0
        public void Run()
        {
            var comb = new FermatCombination(1000000);

            N = Input.ReadInt();

            long ret = 0;

            {
                var v = comb.Permutation(N - 1, N - 1);
                ret += (v * (N)) % MOD;
                ret %= MOD;
            }

            for (int k = 0; k < N; k++)
            {
                // C(K ? 1, N ? 1 ? K) × K! × (N ? 1 ? K)!

                if (k - 1 < N - 1 - k)
                {
                    continue;
                }
                var v1 = comb.Combination(k - 1, N - 1 - k);
                var v2 = comb.Permutation(k, k);
                var v3 = comb.Permutation(N - 1 - k, N - 1 - k);
                var v  = (((v1 * v2) % MOD) * v3) % MOD;
                ret += (MOD - v);
                ret %= MOD;
            }

            Console.WriteLine(ret);
        }
Example #2
0
        public void Run()
        {
            N = Input.ReadInt();
            A = Input.ReadLongArray();

            var comb = new FermatCombination(200000);

            long[] S = new long[N];
            var    p = comb.Permutation(N, N);

            for (int i = 0; i < N; i++)
            {
                long c = p;
                c *= comb.Inverse[i + 1];
                c %= MOD;
                c *= comb.Permutation(i, i);
                c %= MOD;
                // c /= (i + 1);

                // Console.Error.WriteLine("{0} {1}", i, c);
                S[i]  = (i == 0 ? 0: S[i - 1]) + c;
                S[i] %= MOD;
            }

            long ret = 0;

            for (int i = 0; i < N; i++)
            {
                int  r = N - i - 1; // N - 1.. 0
                int  l = i;         // 0.. N - 1;
                long c = 0;
                c += S[r];
                c %= MOD;
                c += S[l];
                c %= MOD;
                c += (MOD - S[0]);
                c %= MOD;

                ret += (c * A[i]) % MOD;
                ret %= MOD;
            }

            Console.WriteLine(ret);
        }