public static void GridGraphBFS()
        {
            // 与えられた高さと幅のグリッドグラフ
            var HW = ReadInts();
            var H  = HW[0];
            var W  = HW[1];
            var S  = ReadGridGraph(H, W);

            var order   = new Queue <(int, int)>();
            var reached = new bool[H, W];

            foreach (var h in MyEnumerable.Interval(0, H))
            {
                foreach (var w in MyEnumerable.Interval(0, W))
                {
                    reached[h, w] = false;
                }
            }

            // 最初の頂点を追加
            order.Enqueue((0, 0));
            reached[0, 0] = true;

            // 求めたい値
            var ans = false;

            // 実行
            while (!order.IsEmpty())
            {
                var(y0, x0) = order.Dequeue();

                // 周りのマス
                var allSides = new[] {
        public static int[,] PascalsTriangle(int nmax, int kmax, int divisor)
        {
            var comb = new int[2000 + 1, 2000 + 1];

            foreach (var n in MyEnumerable.Interval(0, 2000 + 1))
            {
                foreach (var k in MyEnumerable.Interval(0, 2000 + 1))
                {
                    if (n < k)
                    {
                        continue;
                    }

                    if (k == 0)
                    {
                        comb[n, k] = 1;
                    }
                    else
                    {
                        comb[n, k] = (int)(((long)comb[n - 1, k - 1] + comb[n - 1, k]) % divisor);
                    }
                }
            }

            return(comb);
        }
 public static void Main(string[] args)
 {
     var N        = ReadInt();
     var abc      = ReadRows(N).Select(row => row.ToInts().ToOneBasedIndexing()).ToOneBasedIndexing();
     var patterns = MyEnumerable.Interval(2, N + 1).SelectMany(i =>
                                                               MyEnumerable.Interval(1, 3 + 1).Select(k => (i, k)));
     var init = new [] {
        public static void Main(string[] args)
        {
            var NK   = ReadInts();
            var N    = NK[0];
            var K    = NK[1];
            var div  = p10_9plus7;
            var comb = new long[2000 + 1, 2000 + 1];

            foreach (var n in MyEnumerable.Interval(0, 2000 + 1))
            {
                foreach (var k in MyEnumerable.Interval(0, 2000 + 1))
                {
                    if (n < k)
                    {
                        continue;
                    }

                    if (k == 0)
                    {
                        comb[n, k] = 1;
                    }
                    else
                    {
                        comb[n, k] = (comb[n - 1, k - 1] + comb[n - 1, k]) % div;
                    }
                }
            }
            var ans = Enumerable.Range(1, K)
                      .Select(i => (comb[N - K + 1, i] * comb[K - 1, i - 1]) % div);

            PrintColomn(ans);
        }
        public static void Main(string[] args)
        {
            var N = ReadInt();
            var a = ReadInts();
            var b = new int[N];

            foreach (var i in MyEnumerable.Interval(0, N).Reverse())
            {
                var sum = Enumerable.Range(1, N / (i + 1) - 1)
                          .Select(k => b[(i + 1) * k + i])
                          .Aggregate(0, (x1, x2) => x1 + x2);
                if (sum % 2 == a[i])
                {
                    b[i] = 0;
                }
                else
                {
                    b[i] = 1;
                }
            }

            var bi = MyEnumerable.Interval(0, N).Where(i => b[i] == 1).Select(i => i + 1);

            Print(bi.Count());
            if (bi.Count() != 0)
            {
                PrintRow(bi);
            }
        }
        public static void Main(string[] args)
        {
            var NABCD = ReadInts();
            var N     = NABCD[0];
            var A     = NABCD[1];
            var A0    = A - 1;
            var B     = NABCD[2];
            var B0    = B - 1;
            var C     = NABCD[3];
            var C0    = C - 1;
            var D     = NABCD[4];
            var D0    = D - 1;
            var S     = Read();

            if (C0 < D0)
            {
                var isNo2Rock = !MyEnumerable.Interval(A0, (D0 - 1) + 1)
                                .Any(i => S[i] == '#' && S[i + 1] == '#');
                PrintIf(isNo2Rock, "Yes", "No");
            }
            else
            {
                var isNo2Rock = !MyEnumerable.Interval(A0, (C0 - 1) + 1)
                                .Any(i => S[i] == '#' && S[i + 1] == '#');
                var exist3EmptyBD = MyEnumerable.Interval(B0, (D0 + 1))
                                    .Any(i => S[i - 1] == '.' && S[i] == '.' && S[i + 1] == '.');
                PrintIf(isNo2Rock && exist3EmptyBD, "Yes", "No");
            }
        }
        public static void Main(string[] args)
        {
            var S = Read();

            var Srev = S
                       .Reverse()
                       .ToList();

            var mod = new Mods(2019);

            var pow10 = Enumerable.Range(1, S.Length)
                        .Scanl1((acm, i) => mod.Mul(acm, 10))
                        .ToList();

            var L = MyEnumerable.Interval(0, S.Length)
                    .Scanl(0, (acm, i) => (mod.Add(acm, mod.Mul(S[S.Length - 1 - i].ToString().ToInt(), pow10[i]))))
                    .ToList();

            // PrintRow(L);

            var m = Enumerable.Repeat(0, 2019)
                    .ToList();

            foreach (var l in L)
            {
                m[l] += 1;
            }

            var ans = m
                      .Select(k => k * (k - 1) / 2)
                      .Sum();

            Print(ans);
        }
Exemple #8
0
        public static void Main(string[] args)
        {
            var S  = Read();
            var N  = S.Length;
            var K  = 2;
            var M  = 2;
            var dp = new long[N + 1, K, M];

            dp[0, 0, 0] = 1;
            foreach (var i in MyEnumerable.Interval(0, N))
            {
                var D = int.Parse(S[i].ToString());
                foreach (var k in MyEnumerable.Interval(0, K))
                {
                    foreach (var m in MyEnumerable.Interval(0, M))
                    {
                        foreach (var d in MyEnumerable.Interval(0, k == 1 ? 10 : D))
                        {
                            dp[i + 1, (k == 1 || (d < D)) ? 1 : 0, (m == 1 || d == 4 || d == 9) ? 1 : 0] += dp[i, k, m];
                        }
                    }
                }
            }

            var ans = dp[N, 0, 1] + dp[N, 1, 1];

            Print(ans);
        }
Exemple #9
0
        public static void Main(string[] args)
        {
            var NK = ReadInts();
            var N  = NK[0];
            var K  = NK[1];
            var d  = new int[K];
            var A  = new List <int> [K];

            foreach (var i in MyEnumerable.Interval(0, K))
            {
                d[i] = ReadInt();
                A[i] = ReadInts();
            }

            var have = new HashSet <int>();

            foreach (var Ai in A)
            {
                foreach (var Aik in Ai)
                {
                    have.Add(Aik);
                }
            }

            var ans = N - have.Count;

            Print(ans);
        }
        public static void Main(string[] args)
        {
            var NM = ReadInts();
            var N  = NM[0];
            var M  = NM[1];
            var H  = ReadLongs();
            var AB = ReadColumns(M, 2);
            var A  = AB[0].ToInts();
            var B  = AB[1].ToInts();

            var g = MyEnumerable.Interval(0, N)
                    .Select(_ => new List <int>())
                    .ToList();

            foreach (var i in MyEnumerable.Interval(0, M))
            {
                g[A[i] - 1].Add(B[i] - 1);
                g[B[i] - 1].Add(A[i] - 1);
            }

            var ans = 0;

            foreach (var i in MyEnumerable.Interval(0, N))
            {
                var count = g[i].Where(k => H[k] >= H[i])
                            .Count();
                if (count == 0)
                {
                    ans += 1;
                }
            }

            Print(ans);
        }
        public static void Main(string[] args)
        {
            var NW = ReadInts();
            var N  = NW[0];
            var W  = NW[1];
            var wv = ReadColumns(N, 2);
            var w  = wv[0].ToInts().Prepend(0).ToList();
            var v  = wv[1].ToLongs().Prepend(0).ToList();

            var dp = new long[N + 1, W + 1];

            foreach (var i in MyEnumerable.Interval(1, N + 1))
            {
                foreach (var k in MyEnumerable.Interval(0, W + 1))
                {
                    if (k - w[i] < 0)
                    {
                        dp[i, k] = dp[i - 1, k];
                    }
                    else
                    {
                        dp[i, k] = Max(dp[i - 1, k - w[i]] + v[i], dp[i - 1, k]);
                    }
                }
            }

            var ans = MyEnumerable.Interval(0, W + 1)
                      .Max(k => dp[N, k]);

            Print(ans);
        }
        public static void Main(string[] args)
        {
            var HW   = ReadInts();
            var H    = HW[0];
            var W    = HW[1];
            var c    = MyEnumerable.Interval(0, H).Select(i => Read().ToList()).ToList();
            var seen = MyEnumerable.Interval(0, H).Select(i =>
                                                          MyEnumerable.Interval(0, W).Select(k =>
                                                                                             false
                                                                                             ).ToList()
                                                          ).ToList();

            var yxs = MyEnumerable.Interval(0, H).SelectMany(y =>
                                                             MyEnumerable.Interval(0, W).Select(x =>
                                                                                                Tuple.Create(y, x)
                                                                                                )
                                                             );

            var s = yxs.First(t => {
                var y = t.Item1;
                var x = t.Item2;
                return(c[y][x] == 's');
            });
            var g = yxs.First(t => {
                var y = t.Item1;
                var x = t.Item2;
                return(c[y][x] == 'g');
            });

            // init
            var todo = new Stack <Tuple <int, int> > ();

            todo.Push(s);
            // dfs
            while (!todo.IsEmpty())
            {
                var t = todo.Pop();
                var y = t.Item1;
                var x = t.Item2;
                if (x < 0 || W <= x || y < 0 || H <= y || c[y][x] == '#')
                {
                    continue;
                }
                if (seen[y][x])
                {
                    continue;
                }
                seen[y][x] = true;

                todo.Push(Tuple.Create(y, x + 1));
                todo.Push(Tuple.Create(y, x - 1));
                todo.Push(Tuple.Create(y + 1, x));
                todo.Push(Tuple.Create(y - 1, x));
            }

            // output
            PrintIf(seen[g.Item1][g.Item2], "Yes", "No");
            // PrintRows (seen);
        }
        public static void TreeBFS()
        {
            // 与えられた頂点数と辺
            var N  = ReadInt();
            var ab = ReadColumns(N, 2);
            var a  = ab[0].ToInts();
            var b  = ab[1].ToInts();

            // 木構造の初期化
            var tree = MyEnumerable.Interval(0, N)
                       .Select(i => new List <int>())
                       .ToList();

            foreach (var i in MyEnumerable.Interval(0, N - 1))
            {
                tree[a[i] - 1].Add(b[i] - 1);
                tree[b[i] - 1].Add(a[i] - 1);
            }

            var order = new Queue <int>();   // BFS
            // var order = new Stack<int>();   // DFS

            var reached = Enumerable.Repeat(false, N)
                          .ToArray();

            // 最初の頂点を追加
            order.Enqueue(0);
            reached[0] = true;

            // 求めたい値
            var ans = false;

            // 実行
            while (!order.IsEmpty())
            {
                var node = order.Dequeue();                         // 現在見ている頂点
                Func <int, bool> pruningCondition = child => false; // 枝刈り条件
                var nexts = tree[node]
                            .Where(child => !reached[child] && !pruningCondition(child));

                // ************** 処理 *******************

                // 現在の頂点による処理

                // 現在の頂点と、次の頂点による処理
                foreach (var next in nexts)
                {
                }

                // ***************************************

                foreach (var next in nexts)
                {
                    order.Enqueue(next);
                    reached[next] = true;
                }
            }
        }
Exemple #14
0
        public static void Main(string[] args)
        {
            var N  = ReadInt();
            var S  = Read().Select(c => c.ToString()).ToArray();
            var Rn = Enumerable.Range(0, N)
                     .Reverse()
                     .Scanl(0, (acm, i) => acm + (S[i] == "R" ? 1 : 0))
                     .Skip(1)
                     .Reverse()
                     .ToArray();
            var Gn = Enumerable.Range(0, N)
                     .Reverse()
                     .Scanl(0, (acm, i) => acm + (S[i] == "G" ? 1 : 0))
                     .Skip(1)
                     .Reverse()
                     .ToArray();
            var Bn = Enumerable.Range(0, N)
                     .Reverse()
                     .Scanl(0, (acm, i) => acm + (S[i] == "B" ? 1 : 0))
                     .Skip(1)
                     .Reverse()
                     .ToArray();

            if (N < 3)
            {
                Print(0);
                return;
            }

            var ans = 0L;

            foreach (var i in MyEnumerable.Interval(0, N - 2))
            {
                foreach (var j in MyEnumerable.Interval(i + 1, N - 1))
                {
                    if (S[i] != S[j])
                    {
                        var badIx = 2 * j - i;
                        if (S[i] == "B" && S[j] == "G" || S[i] == "G" && S[j] == "B")
                        {
                            ans += Rn[j + 1] - (badIx < N && S[badIx] == "R" ? 1 : 0);
                        }
                        else if (S[i] == "R" && S[j] == "B" || S[i] == "B" && S[j] == "R")
                        {
                            ans += Gn[j + 1] - (badIx < N && S[badIx] == "G" ? 1 : 0);
                        }
                        else
                        {
                            ans += Bn[j + 1] - (badIx < N && S[badIx] == "B" ? 1 : 0);
                        }
                    }
                }
            }

            Print(ans);
        }
Exemple #15
0
        public static void Main(string[] args)
        {
            var n   = ReadInt();
            var p   = ReadInts();
            var ans = MyEnumerable.Interval(1, n - 1).Where(i =>
                                                            p[i - 1] < p[i] && p[i] < p[i + 1] ||
                                                            p[i + 1] < p[i] && p[i] < p[i - 1]
                                                            ).Count();

            Print(ans);
        }
        /// <summary>
        /// シーケンスを1-based indexingなリスト(っぽいDictionary)に変換する
        /// </summary>
        /// <returns>1-based indexingなDictionaryを返す</returns>
        public static Dictionary <int, T> ToOneBasedIndexing <T> (this IEnumerable <T> source)
        {
            var dict = new Dictionary <int, T> ();
            var list = source.ToList();

            foreach (var i in MyEnumerable.Interval(0, list.Count))
            {
                dict.Add(i + 1, list[i]);
            }
            return(dict);
        }
        public static void Main(string[] args)
        {
            var KS = ReadInts();
            var K  = KS[0];
            var S  = KS[1];

            var ans = MyEnumerable.Interval(0, K + 1).SelectMany(X =>
                                                                 MyEnumerable.Interval(0, K + 1).Where(Y =>
                                                                                                       0 <= S - (X + Y) && S - (X + Y) <= K
                                                                                                       )).Count();

            Print(ans);
        }
        public static void Main(string[] args)
        {
            var NL   = ReadInts();
            var N    = NL[0];
            var L    = NL[1];
            var sum  = N * (N - 1) / 2 + N * L;
            var minI = MyEnumerable.Interval(1, N + 1).OrderBy(i =>
                                                               Abs(L + i - 1)
                                                               ).First();
            var ans = sum - (L + minI - 1);

            Print(ans);
        }
Exemple #19
0
        public static void Main(string[] args)
        {
            var N = ReadInt();
            var A = ReadInts();

            var bukas = Enumerable.Repeat(0, N).ToArray();

            foreach (var i in MyEnumerable.Interval(0, N - 1))
            {
                bukas[A[i] - 1] += 1;
            }

            PrintColomn(bukas);
        }
Exemple #20
0
        public static void Main(string[] args)
        {
            var NK = ReadInts();
            var N  = NK[0];
            var K  = NK[1];

            Func <long, long, long> f = (l, r) => (l + r) * (r - l + 1L) / 2L;
            var mods = new Mods(p1000000007);

            var ans = MyEnumerable.Interval(K, N + 2)
                      .Select(i => mods.Mod(f(N - i + 1, N) - f(0, i - 1) + 1))
                      .Sum(p1000000007);

            Print(ans);
        }
        public static void Main(string[] args)
        {
            var S = Read();
            var N = S.Length;
            //

            var LRi = new List <int> ();

            LRi.Add(0);
            foreach (var i in MyEnumerable.Interval(1, N))
            {
                if (S[i - 1] == 'L' && S[i] == 'R')
                {
                    LRi.Add(i);
                }
            }
            LRi.Add(N);
            var slashes = new List <IEnumerable <int> > ();

            foreach (var i in MyEnumerable.Interval(1, LRi.Count))
            {
                var l      = LRi[i - 1];
                var r      = LRi[i];
                var countR = MyEnumerable.Interval(l, r).Where(k => S[k] == 'R').Count();
                var length = r - l;
                var middle = length / 2;
                var lastR  = middle;
                var firstL = middle;
                if (IsOdd(length))
                {
                    if (IsOdd(countR))
                    {
                        lastR += 1;
                    }
                    else
                    {
                        firstL += 1;
                    }
                }

                // Print ($"{length} {countR}");
                var Rs = Enumerable.Repeat(0, countR - 1).Append(lastR);
                var Ls = Enumerable.Repeat(0, length - countR - 1).Prepend(firstL);
                slashes.Add(Rs.Concat(Ls));
            }

            PrintRow(slashes.SelectMany(x => x));
        }
        public static void Main(string[] args)
        {
            var N     = ReadInt();
            var p     = ReadInts();
            var isYes = Enumerable.Range(0, N).Any(i =>
                                                   Enumerable.Range(0, N).Any(j => {
                var p2  = new List <int> (p);
                var tmp = p2[i];
                p2[i]   = p2[j];
                p2[j]   = tmp;
                return(MyEnumerable.Interval(1, N)
                       .All(k => p2[k - 1] <= p2[k]));
            }));

            PrintIf(isYes, "YES", "NO");
        }
        public static void Main(string[] args)
        {
            var N    = ReadInt();
            var T    = ReadInts().Prepend(0).ToList();
            var M    = ReadInt();
            var PX   = ReadColumns(M, 2);
            var P    = PX[0].ToInts().Prepend(0).ToList();
            var X    = PX[1].ToInts().Prepend(0).ToList();
            var sumT = T.Sum();
            var ans  = MyEnumerable.Interval(1, M + 1)
                       .Select(i =>
                               sumT - T[P[i]] + X[i]
                               );

            PrintColomn(ans);
        }
        public static void Main(string[] args)
        {
            var N = ReadInt();
            var H = ReadLongs();

            H[0] = H[0] == 1 ? 1 : H[0] - 1;
            foreach (var i in MyEnumerable.Interval(1, N))
            {
                if (H[i - 1] < H[i])
                {
                    H[i] -= 1;
                }
            }
            var ans = H.MapAdjacent((h1, h2) => h1 <= h2).All(t => t);

            PrintIf(ans, "Yes", "No");
        }
        /// <summary>
        /// フラグから分割するindexの位置の列への変換
        /// </summary>
        /// <param name="flags">二進数によるフラグ</param>
        /// <param name="flagSize">フラグの数</param>
        /// <returns>分割するindexの位置の列</returns>
        /// <example> CutFlagToCutIndex(10110) => [0, 2, 3, 5, 6]</example>
        public static IEnumerable <int> CutFlagToIndexes(int flags)
        {
            int flagSize = (int)Log(flags, 2);
            var indexes  = new List <int> {
                0
            };

            foreach (var i in MyEnumerable.Interval(0, flagSize))
            {
                if ((flags >> i) % 2 == 1)
                {
                    indexes.Add(i + 1);
                }
            }
            indexes.Add(flagSize + 1);
            return(indexes);
        }
        public static void Main(string[] args)
        {
            var S   = Read();
            var N   = S.Length;
            var LRi = new List <int> ();

            LRi.Add(0);
            foreach (var i in MyEnumerable.Interval(1, N))
            {
                if (S[i - 1] == 'L' && S[i] == 'R')
                {
                    LRi.Add(i);
                }
            }

            LRi.Add(N);
            var Ss  = LRi.MapAdjacent((i1, i2) => S.Substring(i1, i2 - i1)).ToList();
            var S2s = new List <IEnumerable <int> > ();

            foreach (var i in MyEnumerable.Interval(0, Ss.Count))
            {
                var countR = Ss[i].Where(s => s == 'R').Count();
                var middle = Ss[i].Length / 2;
                var lastR  = middle;
                var firstL = middle;
                if (IsOdd(Ss[i].Length))
                {
                    if (IsOdd(countR))
                    {
                        lastR += 1;
                    }
                    else
                    {
                        firstL += 1;
                    }
                }

                // Print ($"{length} {countR}");
                var Rs = Enumerable.Repeat(0, countR - 1).Append(lastR);
                var Ls = Enumerable.Repeat(0, Ss[i].Length - countR - 1).Prepend(firstL);
                S2s.Add(Rs.Concat(Ls));
            }

            PrintRow(S2s.SelectMany(x => x));
        }
Exemple #27
0
        public static void Main(string[] args)
        {
            var NW   = ReadInts();
            var N    = NW[0];
            var W    = NW[1];
            var wv   = ReadColumns(N, 2);
            var w    = wv[0].ToLongs().Prepend(0L).ToList();
            var v    = wv[1].ToInts().Prepend(0).ToList();
            var V    = (int)Pow(10, 5);
            var maxW = (long)Pow(10, 13);

            var dp = new long[N + 1, V + 1];

            // init
            dp[0, 0] = 0;
            foreach (var k in MyEnumerable.Interval(1, V + 1))
            {
                dp[0, k] = maxW;
            }

            // dp
            foreach (var i in MyEnumerable.Interval(1, N + 1))
            {
                foreach (var k in MyEnumerable.Interval(0, V + 1))
                {
                    if (k - v[i] < 0)
                    {
                        dp[i, k] = dp[i - 1, k];
                    }
                    else
                    {
                        dp[i, k] = Min(dp[i - 1, k - v[i]] + w[i], dp[i - 1, k]);
                    }
                }
            }

            var ans = MyEnumerable.Interval(0, N + 1).SelectMany(i =>
                                                                 MyEnumerable.Interval(0, V + 1).Where(k =>
                                                                                                       dp[i, k] <= W
                                                                                                       )
                                                                 )
                      .Max();

            Print(ans);
        }
Exemple #28
0
        public static void Main(string[] args)
        {
            var NK   = ReadInts();
            var N    = NK[0];
            var K    = NK[1];
            var h    = ReadInts().ToOneBasedIndexing();
            var init = new Dictionary <int, int> {
                { 1, 0 }
            };
            Func <Dictionary <int, int>, int, int> rec = (dp, j) => {
                return(MyEnumerable.Interval(j - K < 1 ? 1 : j - K, j)
                       .Select(i => dp[i] + Abs(h[i] - h[j]))
                       .Min());
            };
            var pattern = MyEnumerable.Interval(1 + init.Count, N + 1);
            var ans     = DynamicProgramming(init, rec, pattern) [N];

            Print(ans);
        }
Exemple #29
0
        public static void Main(string[] args)
        {
            var LR = ReadInts();
            var L  = LR[0];
            var R  = LR[1];

            if (R - L >= 2019)
            {
                Print(0);
            }
            else
            {
                var ans = MyEnumerable.Interval(L, R + 1).SelectMany(i =>
                                                                     MyEnumerable.Interval(i + 1, R + 1).Select(j =>
                                                                                                                ((long)i * j) % 2019
                                                                                                                )
                                                                     ).Min();
                Print(ans);
            }
        }
        public static void Main(string[] args)
        {
            // Print(Pow(200, 5) - Pow(199, 5) >= Pow(10, 9));

            var X = ReadLong();

            var ans =
                MyEnumerable.Interval(-200, 200)
                .SelectMany(A =>
                            MyEnumerable.Interval(-200, 200)
                            .Select(B => (A, B)))
                .Where(t =>
            {
                var(A, B) = t;
                return((long)Pow(A, 5) - (long)Pow(B, 5) == X);
            })
                .First();

            Print($"{ans.A} {ans.B}");
        }