Exemple #1
0
        static void Method(string[] args)
        {
            int[] nm   = ReadInts();
            int   n    = nm[0];
            int   m    = nm[1];
            long  mask = 1000000000 + 7;

            List <int[]> factors    = PrimeFactors(Abs(n));
            var          calculator = new CaseCalculator(mask, m + 100);
            long         res        = 1;

            for (int i = 0; i < factors.Count; i++)
            {
                res *= calculator.Combination(m + factors[i][1] - 1, m - 1);
                res %= mask;
            }

            long minuses = 0;

            for (int i = n < 0 ? 1 : 0; i <= m; i += 2)
            {
                minuses += calculator.Combination(m, i);
                minuses %= mask;
            }

            WriteLine(res * minuses % mask);
        }
Exemple #2
0
        static void Method(string[] args)
        {
            long[] kn   = ReadLongs();
            long   k    = kn[0];
            long   n    = kn[1];
            long   mask = 998244353;

            CaseCalculator calculator = new CaseCalculator(mask, n + k - 1);

            long allPat = calculator.Combination(n + k - 1, n);

            long[] res = new long[2 * k - 1];

            long prevPats = 0;

            for (long val = 2; val <= k + 1; val++)
            {
                long pats = Min(val - 2, 2 * k - val) / 2 + 1;
                if (pats == prevPats)
                {
                    res[val - 2]     = res[val - 3];
                    res[2 * k - val] = res[val - 3];
                    continue;
                }

                long seqCnt = Min(pats, n / 2);
                long resTmp = allPat;
                for (long i = 1; i <= seqCnt; i++)
                {
                    long tmp = calculator.Combination(pats, i);
                    tmp = calculator.Multi(tmp,
                                           calculator.Combination(k - 1 + n - i * 2, k - 1));
                    if (i % 2 == 1)
                    {
                        resTmp += mask - tmp;
                    }
                    else
                    {
                        resTmp += tmp;
                    }
                    resTmp %= mask;
                }

                res[val - 2]     = resTmp;
                res[2 * k - val] = resTmp;
                prevPats         = pats;
            }

            for (int i = 0; i < 2 * k - 1; i++)
            {
                WriteLine(res[i]);
            }
        }
Exemple #3
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            long[] array = ReadLongs();
            Array.Sort(array);

            long mask       = 998244353;
            long res        = 0;
            var  calculator = new CaseCalculator(mask, 2 * n);

            for (int i = 0; i < 2 * n; i++)
            {
                long tmp = calculator.Combination(2 * n - 1, n);
                tmp *= array[i];
                tmp %= mask;
                if (i < n)
                {
                    res += mask - tmp;
                }
                else
                {
                    res += tmp;
                }
                res %= mask;
            }

            WriteLine(res * 2 % mask);
        }
Exemple #4
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);
            }
        }
Exemple #5
0
        static void Method(string[] args)
        {
            int    k    = ReadInt();
            string s    = Read();
            long   mask = 1000000000 + 7;

            long[] divs  = new long[1000000 + 100];
            long[] divs2 = new long[1000000 + 100];
            divs[0]  = 1;
            divs2[0] = 1;
            for (int i = 1; i < divs.Length; i++)
            {
                divs[i]   = divs[i - 1] * 25;
                divs[i]  %= mask;
                divs2[i]  = divs2[i - 1] * 26;
                divs2[i] %= mask;
            }
            var calculator = new CaseCalculator(mask, s.Length + k + 100);

            long res = 0;

            for (int i = 0; i <= k; i++)
            {
                long val = calculator.Combination(s.Length - 1 + i, i) * divs2[k - i];
                val %= mask;
                val *= divs[i];
                val %= mask;
                res += val;
                res %= mask;
            }
            WriteLine(res);
        }
Exemple #6
0
        static void Method(string[] args)
        {
            long[]         nk         = ReadLongs();
            long           n          = nk[0];
            long           k          = nk[1];
            long           mask       = 1000000000 + 7;
            CaseCalculator calculator = new CaseCalculator(mask, n * 2);
            long           res        = 1;

            for (long i = 1; i <= Min(n, k); i++)
            {
                long tmp = calculator.Combination(n, i);
                tmp  = calculator.Multi(tmp, calculator.Combination(n - 1, i));
                res += tmp;
                res %= mask;
            }
            WriteLine(res);
        }
Exemple #7
0
        static void Method(string[] args)
        {
            int[]          rcs        = ReadInts();
            long           r1         = rcs[0];
            long           c1         = rcs[1];
            long           r2         = rcs[2];
            long           c2         = rcs[3];
            long           mask       = 1000000000 + 7;
            CaseCalculator calculator = new CaseCalculator(mask, r2 + c2 + 5);
            long           res        = 0;

            for (long i = r1 + 1; i <= r2 + 1; i++)
            {
                res += calculator.Combination(i + c2, i);
                res += mask - calculator.Combination(i + c1 - 1, i);
                res %= mask;
            }
            WriteLine(res);
        }
Exemple #8
0
        static void Method(string[] args)
        {
            long[] nk = ReadLongs();
            long   n  = nk[0];
            long   k  = nk[1];

            long[] array = ReadLongs();
            Array.Sort(array);
            long mask = 1000000000 + 7;

            for (int i = 0; i < n; i++)
            {
                array[i]  = mask * 10 + array[i];
                array[i] %= mask;
            }
            if (k == 1)
            {
                WriteLine(0);
                return;
            }

            CaseCalculator calculator = new CaseCalculator(mask, n);
            long           res        = 0;

            for (long i = k - 1; i < n; i++)
            {
                long tmp = array[i] * calculator.Combination(i, k - 1);
                tmp %= mask;
                res += tmp;
                res %= mask;
            }
            for (long i = 0; i < n - k + 1; i++)
            {
                long tmp = array[i] * calculator.Combination(n - i - 1, k - 1);
                tmp %= mask;
                res += mask - tmp;
                res %= mask;
            }
            WriteLine(res);
        }
Exemple #9
0
        static void Method(string[] args)
        {
            int p = ReadInt();

            int[]          array          = ReadInts();
            CaseCalculator caseCalculator = new CaseCalculator(p, p - 1);

            long[] combinations = new long[p];
            for (int i = 0; i < p; i++)
            {
                combinations[i] = caseCalculator.Combination(p - 1, i);
            }
            long[] res = new long[p];
            for (int i = 0; i < p; i++)
            {
                if (array[i] == 0)
                {
                    continue;
                }

                res[p - 1]++;
                long tmp   = 1;
                bool minus = false;
                for (int j = 0; j < p; j++)
                {
                    long delta = (combinations[j] * tmp) % p;
                    if (minus)
                    {
                        delta *= -1;
                    }
                    res[j] += p - delta;
                    res[j] %= p;
                    tmp    *= i;
                    tmp    %= p;
                    minus   = !minus;
                }
            }

            for (int i = p - 1; i >= 0; i--)
            {
                Write(res[i]);
                if (i > 0)
                {
                    Write(" ");
                }
            }
        }
Exemple #10
0
        static void Method(string[] args)
        {
            int s = ReadInt();

            long res        = 0;
            long mask       = 1000000000 + 7;
            var  calculator = new CaseCalculator(mask, 5000);

            for (int i = 1; i * 3 <= s; i++)
            {
                long remain = s - i * 3;
                res += calculator.Combination(remain + i - 1, remain);
                res %= mask;
            }

            WriteLine(res);
        }
Exemple #11
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);
        }
Exemple #12
0
        static void Method(string[] args)
        {
            int            t          = ReadInt();
            long           mask       = 1000000000 + 7;
            CaseCalculator calculator = new CaseCalculator(mask, 1000000);

            for (int i = 0; i < t; i++)
            {
                int[] nmk   = ReadInts();
                int   n     = nmk[0];
                int   m     = nmk[1];
                int   k     = nmk[2];
                int[] array = ReadInts();
                Array.Sort(array);

                long res = 0;
                for (int j = 0; j + m - 1 < n; j++)
                {
                    if (array[j + m - 1] - array[j] > k)
                    {
                        continue;
                    }

                    int bottom = j + m - 1;
                    int top    = n;
                    while (bottom + 1 < top)
                    {
                        int mid = (bottom + top) / 2;
                        if (array[mid] - array[j] > k)
                        {
                            top = mid;
                        }
                        else
                        {
                            bottom = mid;
                        }
                    }

                    long len = bottom - j;
                    res += calculator.Combination(len, m - 1);
                    res %= mask;
                }
                WriteLine(res);
            }
        }
Exemple #13
0
        static void Method(string[] args)
        {
            long[] xy = ReadLongs();
            long   x  = xy[0];
            long   y  = xy[1];

            if (2 * y - x < 0 || (2 * y - x) % 3 != 0 ||
                2 * x - y < 0 || (2 * x - y) % 3 != 0)
            {
                WriteLine(0);
                return;
            }
            long           a          = (2 * y - x) / 3;
            long           b          = (2 * x - y) / 3;
            long           mask       = 1000000000 + 7;
            CaseCalculator calculator = new CaseCalculator(mask, a + b);

            WriteLine(calculator.Combination(a + b, a));
        }
Exemple #14
0
        static void Method(string[] args)
        {
            int[] nk = ReadInts();
            int   n  = nk[0];
            int   k  = nk[1];

            int[][] lrs = new int[n][];
            for (int i = 0; i < n; i++)
            {
                lrs[i] = ReadInts();
            }
            Array.Sort(lrs, (a, b) => a[0] - b[0]);

            long mask       = 998244353;
            var  calculator = new CaseCalculator(mask, n);
            var  que        = new PriorityQueue <bool>();
            int  now        = 0;
            long res        = 0;

            for (int i = 0; i < n; i++)
            {
                int l = lrs[i][0];
                while (que.Count > 0 && que.Top().Key < l)
                {
                    que.Dequeue();
                    now--;
                }

                now++;
                que.Enqueue(lrs[i][1], true);
                if (now == k)
                {
                    res++;
                }
                if (now > k)
                {
                    res += calculator.Combination(now - 1, k - 1);
                }
                res %= mask;
            }
            WriteLine(res);
        }
Exemple #15
0
        static void DFS2(int now, int parent, List <int>[] graph, CaseCalculator calculator,
                         long[] pats, int[] childs, long mask)
        {
            long val      = 1;
            int  childCnt = childs[now];

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

                long tmp = calculator.Combination(childCnt, childs[to] + 1);
                DFS2(to, now, graph, calculator, pats, childs, mask);
                tmp       = (tmp * pats[to]) % mask;
                childCnt -= childs[to] + 1;
                val      *= tmp;
                val      %= mask;
            }

            pats[now] = val;
        }
Exemple #16
0
        static void Method(string[] args)
        {
            long[] nmk = ReadLongs();
            long   n   = nmk[0];
            long   m   = nmk[1];
            long   k   = nmk[2];

            if (n == 1)
            {
                WriteLine(m);
                return;
            }

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

            long res = 0;

            long[] pows = new long[n];
            pows[0] = 1;
            for (int i = 1; i < n; i++)
            {
                pows[i]  = pows[i - 1] * (m - 1);
                pows[i] %= mask;
            }
            for (long i = n - 1; n - 1 - i <= k; i--)
            {
                long tmp = m * pows[i];
                tmp %= mask;
                tmp *= calculator.Combination(n - 1, i);
                tmp %= mask;
                res += tmp;
                res %= mask;
            }
            WriteLine(res);
        }
Exemple #17
0
        static void Method(string[] args)
        {
            long[] nk = ReadLongs();
            long n = nk[0];
            long k = nk[1];
            string t = Read();
            long equals = 0;
            long notEquals = 0;
            for (int i = 0; i < t.Length / 2; i++)
            {
                if (t[i] == t[(int)n - i - 1]) equals++;
                else notEquals++;
            }
            long mask = 1000000000 + 7;
            long[] equalPats = new long[equals+1];
            long[] notEqualPats = new long[notEquals+1];
            CaseCalculator calculator = new CaseCalculator(mask, n);
            long pow25 = 1;
            for(long i = 0; i <= equals; i++)
            {
                equalPats[i] = calculator.Combination(equals, i);
                equalPats[i] = calculator.Multi(equalPats[i], pow25);
                pow25 *= 25;
                pow25 %= mask;
                if (i > 0)
                {
                    equalPats[i] += equalPats[i - 1];
                    equalPats[i] %= mask;
                }
            }
            long[] twoPows = new long[notEquals + 1];
            twoPows[0] = 1;
            for(long i = 1; i <= notEquals; i++)
            {
                twoPows[i] = twoPows[i - 1] * 2;
                twoPows[i] %= mask;
            }
            long pow24 = 1;
            for(long i = 0; i <= notEquals; i++)
            {
                notEqualPats[i] = calculator.Multi(
                    calculator.Combination(notEquals, i), twoPows[notEquals - i]);
                notEqualPats[i] = calculator.Multi(notEqualPats[i], pow24);
                pow24 *= 24;
                pow24 %= mask;
            }
            /*
            long[] notEqualSums = new long[notEquals + 1];
            for (long i = 0; i <= notEquals; i++)
            {
                notEqualSums[i] = notEqualPats[i];
                if (i > 0) notEqualSums[i] += notEqualSums[i - 1];
                notEqualSums[i] %= mask;
            }
            */

            long res = 0;
            bool haveCenter = n % 2 == 1;
            for (long i = 0; i <= notEquals; i++)
            {
                long remain = k - notEquals - i;
                if (remain < 0) continue;

                if (!(notEquals == 0 && remain == 1))
                {
                    res += calculator.Multi(notEqualPats[i],
                        equalPats[Min(equals, remain / 2)]);
                }

                if (remain > 0 && haveCenter)
                {
                    long tmp = calculator.Multi(notEqualPats[i],
                        equalPats[Min(equals,(remain - 1) / 2)]);
                    tmp = calculator.Multi(tmp, 25);
                    res += tmp;
                }
                res %= mask;
            }
            WriteLine(res);
        }
Exemple #18
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);
        }