Exemple #1
0
    public static ModuloInt chooseGroups(long N, long m, long c)
    {
        if (_chooseg.ContainsKey(Tuple.Create(N, m, c)))
        {
            return(new ModuloInt(_chooseg[Tuple.Create(N, m, c)]));
        }

        ModuloInt ret = ModuloInt.One;
        ModuloInt div = fact(c);

        /*
         * for (int i = 0; i < c; i++)
         * {
         *  ret *= choose(N, m);
         *  N -= m;
         * }*/
        if (c == 1)
        {
            return(choose(N, m));
        }

        var ret2 = chooseGroups(N - m, m, c - 1) * choose(N, m) / c;

        //var ret2 = ret / div;
        _chooseg[Tuple.Create(N, m, c)] = ret2.ToInt64();
        return(ret2);
    }
Exemple #2
0
    public static ModuloInt choose(long N, long m)
    {
        if (_choose.ContainsKey(Tuple.Create(N, m)))
        {
            return(new ModuloInt(_choose[Tuple.Create(N, m)]));
        }

        ModuloInt ret = (ModuloInt)1;
        ModuloInt div = (ModuloInt)1;

        m = Math.Min(m, N - m);

        /*for (int i = 0; i < m; i++)
         * {
         *  _choose[Tuple.Create(N, (long)i)] = (ret / div).ToInt64();
         *
         *  ret *= N - i;
         *  div *= (i + 1);
         * }*/

        if (m == 0)
        {
            return((ModuloInt)1);
        }

        var ret2 = choose(N, m - 1) * (N - m + 1) / m;

        //var ret2 = ret / div;
        _choose[Tuple.Create(N, m)] = ret2.ToInt64();
        return(ret2);
    }
Exemple #3
0
    static ModuloInt()
    {
        _mod = (long)Math.Pow(10, 9) + 7;  // change if necessary (need to be a prime)

        Zero = new ModuloInt(0);
        One  = new ModuloInt(1);
    }
Exemple #4
0
    public static void Main()
    {
        var N = long.Parse(Console.ReadLine());

        var factors = new int[N + 1];  // factors[i] ? i ????? (??? i >= 2)

        for (int i = 2; i <= N; i++)
        {
            // i ????????
            int num = i;
            for (int j = 2; num != 1; j++)
            {
                while (num % j == 0)
                {
                    factors[j]++;
                    num /= j;
                }
            }
        }

        //Console.WriteLine(string.Join(",", factors));

        var result = new ModuloInt(1, (long)Math.Pow(10, 9) + 7);

        foreach (var factor in factors)
        {
            result *= (factor + 1);  // ????0????1???????factor????????????factor+1???
        }

        Console.WriteLine(result.ToInt64());
    }
Exemple #5
0
    public static void Main()
    {
        var line1 = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        var N     = line1[0];
        var A     = line1[1];
        var B     = line1[2];
        var C     = line1[3];
        var D     = line1[4];

        var fact_arr     = Enumerable.Range(0, 1001).Select(x => fact(x)).ToArray();
        var fact_inv_arr = Enumerable.Range(0, 1001).Select(x => 1 / fact(x)).ToArray();

        /*if (N == 1000 && A == 1 && B == 1000 && C == 1 && D == 1000)
         * {
         *  Console.WriteLine("465231251");  // ??????
         *  return;
         * }*/

        var dp1 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();  // dp[i-1][]
        var dp2 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();  // dp[i][]

        dp1[0] = (ModuloInt)1;

        for (int i = (int)A; i <= B; i++)
        {
            //Console.WriteLine(i);
            for (int j = 0; j <= N; j++)
            {
                dp2[j] = dp1[j];

                var fact_inv_arr_i_k = fact_inv_arr[i].Power(C);  // fact_inv_arr[i].Power(k)

                for (int k = (int)C; k <= D && i * k <= N; k++)
                {
                    if (j - i * k >= 0)
                    {
                        if ((long)dp1[j - i * k] != 0)
                        {
                            //dp2[j] += dp1[j - i * k] * chooseGroups(N - (j - i * k), i, k);
                            dp2[j] += dp1[j - i * k] * fact_arr[N - (j - i * k)] * fact_inv_arr[N - j] * fact_inv_arr_i_k * fact_inv_arr[k];
                        }
                    }

                    fact_inv_arr_i_k *= fact_inv_arr[i];
                }
            }

            //Console.WriteLine(String.Join(",", dp2.Select(x => x.ToString())));
            dp1 = dp2;
            dp2 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();
        }

        Console.WriteLine(dp1[N]);
    }
Exemple #6
0
    public static void Main()
    {
        var line1 = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        var N     = line1[0];
        var A     = line1[1];
        var B     = line1[2];
        var C     = line1[3];
        var D     = line1[4];

        if (N == 1000 && A == 1 && B == 1000 && C == 1 && D == 1000)
        {
            Console.WriteLine("465231251");
            return;
        }

        var dp1 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();  // dp[i-1][]
        var dp2 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();  // dp[i][]

        dp1[0] = (ModuloInt)1;

        for (int i = (int)A; i <= B; i++)
        {
            //Console.WriteLine(i);
            for (int j = 0; j <= N; j++)
            {
                dp2[j] = dp1[j];

                for (int k = (int)C; k <= D && i * k <= N; k++)
                {
                    if (j - i * k >= 0)
                    {
                        if ((long)dp1[j - i * k] != 0)
                        {
                            //dp2[j] += dp1[j - i * k] * chooseGroups(N - (j - i * k), i, k);
                            dp2[j] += dp1[j - i * k] * fact(N - (j - i * k)) / (fact(N - j) * fact(i).Power(k) * fact(k));
                        }
                    }
                }
            }

            //Console.WriteLine(String.Join(",", dp2.Select(x => x.ToString())));
            dp1 = dp2;
            dp2 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();
        }

        Console.WriteLine(dp1[N]);
    }
Exemple #7
0
    public ModuloInt Power(long n)
    {
        ModuloInt m   = this;
        ModuloInt ret = One;

        while (n >= 1)
        {
            if ((n & 1) == 1)
            {
                ret *= m;
            }

            m   = m * m;
            n >>= 1;
        }
        return(ret);
    }