Beispiel #1
0
        static void DFS3(int now, int parent,
                         List <int>[] graph, long[] res, int[] childs,
                         long[] pats, CaseCalculator calculator, long mask)
        {
            long pat = 1;

            if (parent == -1)
            {
                pat = pats[now];
            }
            else
            {
                long parentVal = (res[parent] * calculator.Inverse(
                                      calculator.Permutation(childs.Length - 1, childs[now] + 1))) % mask;
                parentVal = (parentVal * calculator.Permutation(
                                 childs[now] + 1, childs[now] + 1)) % mask;
                parentVal = (parentVal * calculator.Inverse(pats[now])) % mask;
                parentVal = (parentVal * calculator.Combination(
                                 childs.Length - 1, childs[now])) % mask;
                pat = (parentVal * pats[now]) % mask;
            }
            res[now] = pat;

            for (int i = 0; i < graph[now].Count; i++)
            {
                int to = graph[now][i];
                if (to == parent)
                {
                    continue;
                }

                DFS3(to, now, graph, res, childs, pats, calculator, mask);
            }
        }
Beispiel #2
0
        static void Method(string[] args)
        {
            int  q          = ReadInt();
            long mask       = 1000003;
            var  calculator = new CaseCalculator(mask, mask + 100);

            for (int i = 0; i < q; i++)
            {
                long[] xdn = ReadLongs();
                long   x   = xdn[0];
                long   d   = xdn[1];
                long   n   = xdn[2];

                if (d == 0)
                {
                    WriteLine(calculator.Pow(x, n));
                }
                else
                {
                    long baseVal = calculator.Multi(x, calculator.Inverse(d));
                    if (baseVal + n - 1 >= mask)
                    {
                        WriteLine(0);
                    }
                    else
                    {
                        WriteLine(calculator.Multi(
                                      calculator.Permutation(baseVal + n - 1, n),
                                      calculator.Pow(d, n)));
                    }
                }
            }
        }
Beispiel #3
0
        static void Method(string[] args)
        {
            int[] nm = ReadInts();
            long  n  = nm[0];
            long  m  = nm[1];

            long[] array = ReadLongs();
            long   sum   = array.Sum();

            if (m < sum)
            {
                WriteLine(0);
                return;
            }

            long mask       = 1000000000 + 7;
            var  calculator = new CaseCalculator(mask, sum + n);
            long res        = 1;

            for (long i = m + n; i > m + n - (sum + n); i--)
            {
                res *= i;
                res %= mask;
            }
            res *= calculator.Inverse(calculator.Permutation(sum + n, sum + n));
            res %= mask;
            WriteLine(res);
        }
Beispiel #4
0
        static void Method(string[] args)
        {
            long[] nab  = ReadLongs();
            long   n    = nab[0];
            long   a    = nab[1];
            long   b    = nab[2];
            long   mask = 1000000000 + 7;

            long[] pows = new long[40];
            pows[0] = 2;
            for (int i = 1; i < pows.Length; i++)
            {
                pows[i]  = pows[i - 1] * pows[i - 1];
                pows[i] %= mask;
            }
            long all = 1;
            long tmp = 1;

            for (int i = 0; i < 40; i++)
            {
                if ((n / tmp) % 2 == 1)
                {
                    all *= pows[i];
                    all %= mask;
                }
                tmp *= 2;
            }
            all = (all - 1 + mask) % mask;

            CaseCalculator calculator = new CaseCalculator(mask, Max(a, b));
            long           nA         = 1;
            long           nB         = 1;

            for (long i = n; i >= n - a + 1; i--)
            {
                nA *= i;
                nA %= mask;
            }
            for (long i = n; i >= n - b + 1; i--)
            {
                nB *= i;
                nB %= mask;
            }
            all = (all - (nA * calculator.Inverse(calculator.Permutation(a, a))) % mask + mask) % mask;
            all = (all - (nB * calculator.Inverse(calculator.Permutation(b, b))) % mask + mask) % mask;
            WriteLine(all);
        }
Beispiel #5
0
        static void Method(string[] args)
        {
            int[] nxp = ReadInts();
            int   n   = nxp[0];
            int   x   = nxp[1];
            int   p   = nxp[2];

            int bottom   = 0;
            int top      = n;
            int upperCnt = 0;
            int lowerCnt = 0;

            while (bottom < top)
            {
                int mid = (bottom + top) / 2;
                if (mid <= p)
                {
                    bottom = mid + 1;
                    if (mid < p)
                    {
                        lowerCnt++;
                    }
                }
                else
                {
                    top = mid;
                    upperCnt++;
                }
            }


            long mask       = 1000000000 + 7;
            var  calculator = new CaseCalculator(mask, n);
            int  lowers     = x - 1;
            int  uppers     = n - x;
            long res        = calculator.Permutation(lowers, lowerCnt);

            res *= calculator.Permutation(uppers, upperCnt);
            res %= mask;
            res *= calculator.Permutation(n - 1 - lowerCnt - upperCnt, n - 1 - lowerCnt - upperCnt);
            res %= mask;
            WriteLine(res);
        }
Beispiel #6
0
        static void Method(string[] args)
        {
            int  n          = ReadInt();
            long mask       = 1000000000 + 7;
            var  calculator = new CaseCalculator(mask, n);
            long res        = calculator.Permutation(n, n);
            long minus      = 0;

            for (int i = 1; i <= n; i++)
            {
                minus += calculator.Combination(n - 1, i - 1);
                minus %= mask;
            }
            res += mask - minus;
            res %= mask;
            WriteLine(res);
        }
Beispiel #7
0
        static void Method(string[] args)
        {
            int[] nk = ReadInts();
            int   n  = nk[0];
            int   k  = nk[1];

            int[,] vals = new int[n, n];
            for (int i = 0; i < n; i++)
            {
                int[] row = ReadInts();
                for (int j = 0; j < n; j++)
                {
                    vals[i, j] = row[j];
                }
            }

            UnionFind rowSet = new UnionFind(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    bool ok = true;
                    for (int l = 0; l < n; l++)
                    {
                        if (vals[i, l] + vals[j, l] > k)
                        {
                            ok = false;
                            break;
                        }
                    }
                    if (ok)
                    {
                        rowSet.Unite(i, j);
                    }
                }
            }

            UnionFind colSet = new UnionFind(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    bool ok = true;
                    for (int l = 0; l < n; l++)
                    {
                        if (vals[l, i] + vals[l, j] > k)
                        {
                            ok = false;
                            break;
                        }
                    }
                    if (ok)
                    {
                        colSet.Unite(i, j);
                    }
                }
            }

            long mask       = 998244353;
            var  calculator = new CaseCalculator(mask, n);
            long res        = 1;

            for (int i = 0; i < n; i++)
            {
                if (rowSet.Root(i) == i)
                {
                    int size = rowSet.GetSize(i);
                    res *= calculator.Permutation(size, size);
                    res %= mask;
                }
            }
            for (int i = 0; i < n; i++)
            {
                if (colSet.Root(i) == i)
                {
                    int size = colSet.GetSize(i);
                    res *= calculator.Permutation(size, size);
                    res %= mask;
                }
            }

            WriteLine(res);
        }
Beispiel #8
0
        static void Method(string[] args)
        {
            int[] nk = ReadInts();
            int   n  = nk[0];
            int   k  = nk[1];

            long[] array = ReadLongs();
            long   mask  = 998244353;

            var calculator = new CaseCalculator(mask, k + 5);

            long[,] pows = new long[k + 1, n];
            long[] pow2s = new long[k + 1];
            for (int i = 0; i <= k; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == 0)
                    {
                        pows[i, j] = 1;
                    }
                    else
                    {
                        pows[i, j]  = pows[i - 1, j] * array[j];
                        pows[i, j] %= mask;
                    }
                }

                if (i == 0)
                {
                    pow2s[i] = 1;
                }
                else
                {
                    pow2s[i]  = pow2s[i - 1] * 2;
                    pow2s[i] %= mask;
                }
            }

            long[] sums = new long[k + 1];
            for (int i = 0; i <= k; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    sums[i] += pows[i, j];
                    sums[i] %= mask;
                }
            }

            for (int i = 1; i <= k; i++)
            {
                long val = 0;
                for (int j = 0; j <= i; j++)
                {
                    long left  = sums[j] * calculator.Inverse(calculator.Permutation(j, j));
                    long right = sums[i - j] * calculator.Inverse(calculator.Permutation(i - j, i - j));
                    left  %= mask;
                    right %= mask;
                    val   += left * right;
                    val   %= mask;
                }
                val *= calculator.Permutation(i, i);
                val %= mask;
                long sames = pow2s[i] * sums[i];
                sames %= mask;
                val   += mask - sames;
                val   %= mask;
                val   *= calculator.Inverse(2);
                val   %= mask;

                WriteLine(val);
            }
        }
Beispiel #9
0
        static void Method(string[] args)
        {
            long[] nm = ReadLongs();
            long   n  = nm[0];
            long   m  = nm[1];

            long mask = 1000000000 + 7;

            var calculator = new CaseCalculator(mask, m + 100);

            long[] evenRevSums = new long[n + 10];
            long[] oddRevSums  = new long[n + 10];
            for (int i = 0; i < n + 10; i++)
            {
                if (i % 2 == 0)
                {
                    evenRevSums[i] = calculator.allInverses[i];
                }
                else
                {
                    oddRevSums[i] = calculator.allInverses[i];
                }

                if (i > 0)
                {
                    evenRevSums[i] += evenRevSums[i - 1];
                    evenRevSums[i] %= mask;
                    oddRevSums[i]  += oddRevSums[i - 1];
                    oddRevSums[i]  %= mask;
                }
            }

            long res = 0;

            for (int i = 0; i <= n; i++)
            {
                if (i % 2 == 0)
                {
                    res += (calculator.Combination(n, i) * calculator.Permutation(m - i, n - i)) % mask;
                    res %= mask;
                }
                else
                {
                    res += mask - (calculator.Combination(n, i) * calculator.Permutation(m - i, n - i)) % mask;
                    res %= mask;
                }
            }
            res *= calculator.Permutation(m, n);
            res %= mask;

            /*
             * for(long i = n - (m - n); i <= n; i++)
             * {
             *  long tmp = calculator.Permutation(n, n - i);
             *  long selects = calculator.allPermutations[i];
             *  if (i % 2 == 0)
             *  {
             *      selects *= (mask + evenRevSums[i] - oddRevSums[i]) % mask;
             *
             *  }
             *  else
             *  {
             *      selects *= (mask + oddRevSums[i] - evenRevSums[i]) % mask;
             *  }
             *  selects %= mask;
             *  tmp *= selects;
             *  tmp %= mask;
             *  res += tmp;
             * }
             *
             * res *= calculator.Permutation(m, n);
             * res %= mask;
             */
            WriteLine(res);
        }