結果

問題 No.696 square1001 and Permutation 5
ユーザー claw88claw88
提出日時 2018-06-09 00:18:51
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,463 bytes
コンパイル時間 1,769 ms
コンパイル使用メモリ 61,628 KB
実行使用メモリ 59,684 KB
最終ジャッジ日時 2023-09-13 00:56:16
合計ジャッジ時間 24,167 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Numerics;

//https://beta.atcoder.jp/contests/chokudai_S001/submissions/1607681
class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }

    Scanner cin;

    void calc()
    {
        cin = new Scanner();
        int N = cin.nextInt();
        int[] ar = cin.ArrayInt(N);
        //setFact(N + 3);
        //Array.Reverse(ar);
        BIT bit = new BIT(N + 3);

        BigInteger ans = 1;

        BigInteger fact = 1;
        //for (int i = 1; i < N; i++)
        //{
        //    fact *= i;
        //}
        for (int i = N - 1; i >= 0; i--)
        {
            ans += fact * (bit.bitCal(ar[i]));
            //ans %= mod;
            bit.bitUpdate(ar[i], 1);
            fact *= (N - i);
        }
        Console.WriteLine(ans);
        //Console.WriteLine(fact - ans);
    }

    //long mod = 1000000007;


    //BigInteger[] fact; //階乗
    //void setFact(int N)
    //{
    //    fact = new BigInteger[N];
    //    fact[0] = 1;
    //    for (int i = 1; i < N; i++)
    //    {
    //        fact[i] = fact[i - 1] * i;
    //        //fact[i] %= mod;
    //    }
    //}
    
    class BIT
    {
        int bitA;
        int[] bit;
        const int INF = int.MaxValue - 10;

        public BIT(int maxa)
        {
            bitA = maxa;
            bit = new int[bitA + 1];
            for (int i = 0; i < bitA; i++)
            {
                bit[i] = 0;
            }
        }

        public void bitUpdate(int a, int val)
        {
            for (int i = a; i <= bitA; i += i & -i)
            {
                bit[i] += val;
            }
        }

        public int bitCal(int max, int min)
        {
            return bitCal(max) - bitCal(min - 1);
        }

        public int bitCal(int a)
        {
            int ret = 0;
            for (int i = a; i > 0; i &= i - 1)
            {
                ret += bit[i];
            }
            return ret;
        }
    }



}

class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}
0