예제 #1
0
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var s = Console.ReadLine().Trim();

        var ts         = s.Select(x => (long)x - 'a').ToArray();
        var appearance = new long[26];

        for (int i = 0; i < ts.Length; i++)
        {
            appearance[ts[i]]++;
        }

        var dp = new long[appearance.Length];

        for (int i = 0; i < appearance.Length; i++)
        {
            for (int j = 0; j < dp.Length; j++)
            {
                if (i - j < 1)
                {
                    break;
                }

                dp[i - j] += dp[i - j - 1] * appearance[i] % mod;
                dp[i - j] %= mod;
            }
            dp[0] += appearance[i];
            dp[0] %= mod;
        }
        var k = dp.Aggregate((x, y) => (x + y) % mod);

        Console.WriteLine(k);
    }
예제 #2
0
 public void CorrectnessOfBigIntegerSum(int whichTestCase)
 {
     if (whichTestCase == 0)
     {
         var arrLong = new long[] { 5, 7, 16, Int64.MaxValue, 4, 2, 3, 0, 3 };
         Assert.DoesNotThrow(() => arrLong.SumToBigIntegerFaster());
         var result = new BigInteger();
         result = arrLong.Aggregate(result, (current, i) => current + i);
         Assert.AreEqual(arrLong.SumToBigIntegerFaster(), result);
     }
 }
예제 #3
0
        public void TestPermute()
        {
            // Matrix transpose
            var shape     = new long[] { 45, 23 };
            var totalSize = shape.Aggregate((a, l) => a * l);
            var data      = Enumerable.Range(1, (int)totalSize).Select(i => (float)i).ToArray();

            var t1 = TensorUtils.Create(shape, data);
            var t2 = TensorUtils.Create(shape, data);

            t2 = t2.Permute(1, 0);

            for (var i = 0; i < shape[0]; ++i)
            {
                for (var j = 0; j < shape[1]; ++j)
                {
                    Assert.AreEqual(t1.GetElementAsFloat(i, j), t2.GetElementAsFloat(j, i));
                }
            }

            Assert.AreEqual(t1.Sizes[0], shape[0]);
            Assert.AreEqual(t1.Sizes[1], shape[1]);

            Assert.AreEqual(t2.Sizes[0], shape[1]);
            Assert.AreEqual(t2.Sizes[1], shape[0]);

            // ND transpose
            shape     = new long[] { 600, 28, 37, 3 };
            totalSize = shape.Aggregate((a, l) => a * l);
            data      = Enumerable.Range(1, (int)totalSize).Select(i => (float)i).ToArray();

            t1 = TensorUtils.Create(shape, data);
            t2 = t1.View(new long[] { shape[0], 1, shape[1], shape[2], shape[3] });
            t2 = t2.Permute(new int[] { 4, 3, 2, 1, 0 });

            for (var i = 0; i < shape[0]; ++i)
            {
                for (var j = 0; j < shape[1]; ++j)
                {
                    for (var k = 0; k < shape[2]; ++k)
                    {
                        for (var l = 0; l < shape[3]; ++l)
                        {
                            Assert.AreEqual(t1.GetElementAsFloat(i, j, k, l), t2.GetElementAsFloat(l, k, j, 0, i));
                        }
                    }
                }
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            string[] input = File.ReadAllLines("input.txt");

            long[] treeCounts = new long[]
            {
                CountTrees(1, 1, input),
                CountTrees(3, 1, input),
                CountTrees(5, 1, input),
                CountTrees(7, 1, input),
                CountTrees(1, 2, input),
            };

            Console.WriteLine(treeCounts.Aggregate((a, b) => a * b));
        }
예제 #5
0
        public override object Part2(string input)
        {
            var numbers = input.ToLines()
                          .Select(long.Parse)
                          .OrderByDescending(x => x)
                          .ToArray();

            var parts = new long[3];

            if (!SumN.Find(numbers, 2020, ref parts))
            {
                throw new Exception("Panic! Unable to solve");
            }

            return(parts.Aggregate((x, y) => x * y));
        }
예제 #6
0
        private int SizeOfObject(CUdeviceptr basePtr, IType type)
        {
            int rank;

            if (ClrArrayType.TryGetArrayRank(type, out rank))
            {
                var dims = new long[rank];
                Context.CopyToHost(dims, basePtr);

                IType elementType;
                ClrArrayType.TryGetArrayElementType(type, out elementType);
                return(sizeof(long) * rank + dims.Aggregate(SizeOf(elementType), (x, y) => x * (int)y));
            }
            else
            {
                return(SizeOf(type));
            }
        }
예제 #7
0
        static Tuple <long[], long> GetAnswer(int num)
        {
            int[]  arr  = new int[1000];
            long[] arr2 = null;

            for (int i = 0; i < BihNum.Length; i++)
            {
                arr[i] = int.Parse(BihNum[i].ToString());
            }


            List <long>   numbers = new List <long>();
            List <long[]> array   = new List <long[]>();


            for (int i = 0; i < arr.Length - num - 1; i++)
            {
                arr2 = new long[num];
                for (int j = 0; j < num; j++)
                {
                    arr2[j] = arr[i + j];
                }

                int index = ZeroIndex(arr2);
                if (index == -1)
                {
                    numbers.Add(arr2.Aggregate((x, y) => x * y));
                    array.Add(arr2);
                }
                else if (index != 0)
                {
                    i = i + index - 1;
                }
            }

            long max      = numbers.Max();
            int  MaxIndex = numbers.IndexOf(max);

            return(new Tuple <long[], long>(array[MaxIndex], max));
        }
예제 #8
0
        static void Main(string[] args)
        {
            var nums = System.IO.File.ReadLines("input/Euler008.txt")
                       .Where(line => !String.IsNullOrWhiteSpace(line))
                       .SelectMany(line => line.Select(c => long.Parse(c.ToString())))
                       .ToList();

            long biggestProduct = 0;
            var  buffer         = new long[target];

            for (var idx = 0; idx < nums.Count(); idx++)
            {
                buffer[idx % target] = nums[idx];
                long bufferSum = buffer.Aggregate((a, b) => a * b);
                if (bufferSum > biggestProduct)
                {
                    biggestProduct = bufferSum;
                }
            }

            Console.WriteLine(biggestProduct);
        }
예제 #9
0
        /// <summary>
        /// multiply the number of trees found on on the slopes
        /// </summary>
        /// <returns>Integer value</returns>
        public long multiplySlopesOfTrees()
        {
            long[] total = new long[5];
            //First Column means right position
            //Second Column means down position
            int[,] slopes =
            {
                { 1, 1 },
                { 3, 1 },
                { 5, 1 },
                { 7, 1 },
                { 1, 2 }
            };


            for (int l = 0; l < slopes.GetLength(0); l++)
            {
                total[l] = this.GetNumberOfTrees(slopes[l, 0], slopes[l, 1]);
            }


            return(total.Aggregate((a, b) => a * b));
        }
예제 #10
0
        public static void A()
        {
            int testCases = int.Parse(Console.ReadLine());

            for (int i = 0; i < testCases; i++)
            {
                var array           = Console.ReadLine().Split(new char[] { ' ' }).Select(x => int.Parse(x)).ToArray();
                var radiatorsAmount = array[0];
                var sectionsAmount  = array[1];
                var costs           = new long[radiatorsAmount > sectionsAmount ? sectionsAmount : radiatorsAmount];
                int sum             = 0;
                while (sum < sectionsAmount)
                {
                    for (int j = 0; j < costs.Length && sum < sectionsAmount; j++)
                    {
                        costs[j]++;
                        sum++;
                    }
                }
                var answer = costs.Aggregate(0L, (acc, x) => acc + x * x);
                Console.WriteLine(answer);
            }
        }
예제 #11
0
        public static long SolvePartTwo(string filePath)
        {
            string file = File.ReadAllText(filePath);

            string[] lines = file.Split("\n");

            int[]  xStep     = new int[] { 1, 3, 5, 7, 1 };
            int[]  yStep     = new int[] { 1, 1, 1, 1, 2 };
            long[] treeCount = new long[xStep.Length];

            for (int i = 0; i < xStep.Length; i++)
            {
                int posX = 0;
                int posY = 0;

                while (posY < lines.Length - 1)
                {
                    if (lines[posY][posX] == '#')
                    {
                        treeCount[i]++;
                    }

                    if (posX + xStep[i] >= lines[posY].Length)
                    {
                        posX = posX + xStep[i] - lines[posY].Length;
                    }
                    else
                    {
                        posX += xStep[i];
                    }

                    posY += yStep[i];
                }
            }

            return(treeCount.Aggregate(Convert.ToInt64(1), (acc, val) => acc * val));
        }
예제 #12
0
        public static void Benchmark(String myExampleGraph,
                                     Int32 myRuns,
                                     Int32 myInnerRuns,
                                     List <Action <IGraph, IVertex, Func <IVertex, bool> > > mySSSPAlgos,
                                     bool myStore = false,
                                     Func <IVertex, bool> myMatchingFunc = null,
                                     Action <IGraph> myBeforeRunAction   = null,
                                     Action <IGraph> myAfterRunAction    = null)
        {
            IGraph g         = null;
            var    isGraphML = false;

            #region init graph

            g = InitGraph(myExampleGraph);

            if (g == null)
            {
                return;
            }

            // hack :/
            isGraphML = myExampleGraph.EndsWith(".xml");

            Console.WriteLine("Graph successfully loaded .. |V|={0}, |E|={1}", g.VertexCount, g.EdgeCount);

            #endregion

            // store the single results
            var runs      = new long[mySSSPAlgos.Count, myRuns];
            var innerRuns = new long[myInnerRuns];

            var sw = new Stopwatch();

            IVertex source = null;

            for (int i = 0; i < myRuns; i++)
            {
                #region init source

                source = GetRandomNode(g, isGraphML);

                if (myBeforeRunAction != null)
                {
                    myBeforeRunAction(g);
                }

                Console.WriteLine("SourceID={0}", source.UUID);

                #endregion

                #region bench

                for (int j = 0; j < mySSSPAlgos.Count; j++)
                {
                    for (int k = 0; k < myInnerRuns; k++)
                    {
                        #region before run init

                        if (myBeforeRunAction != null)
                        {
                            myBeforeRunAction(g);
                        }

                        #endregion

                        sw.Start();
                        mySSSPAlgos[j](g, source, myMatchingFunc);
                        sw.Stop();
                        innerRuns[k] = sw.ElapsedMilliseconds;

                        sw.Reset();

                        #region after run action

                        if (myAfterRunAction != null)
                        {
                            myAfterRunAction(g);
                        }

                        #endregion
                    }
                    runs[j, i] = innerRuns.Aggregate((a, b) => a + b) / myInnerRuns;

                    Console.WriteLine("Algorithm {0} took {1} ms (avg of {2} runs)", j, runs[j, i], myInnerRuns);
                }

                #endregion
            }

            #region calc avg time

            var res = CalculateResult(runs, mySSSPAlgos.Count, myRuns);

            if (myStore)
            {
                StoreResults(g, res, myExampleGraph);
            }
            #endregion
        }
예제 #13
0
        public static void Benchmark(String myExampleGraph,
                                     Int32 myRuns,
                                     Int32 myInnerRuns,
                                     List <Func <IGraph, IVertex, IVertex, Func <IVertex, bool>, List <IVertex> > > myBFSAlgos,
                                     bool myStore = false,
                                     Func <IVertex, bool> myMatchingFunc = null,
                                     Action <IGraph> myBeforeRunAction   = null,
                                     Action <IGraph> myAfterRunAction    = null)
        {
            IGraph g         = null;
            var    isGraphML = false;

            #region init graph

            g = InitGraph(myExampleGraph);

            if (g == null)
            {
                return;
            }

            // hack :/
            isGraphML = myExampleGraph.EndsWith(".xml");

            Console.WriteLine("Graph successfully loaded .. |V|={0}, |E|={1}", g.VertexCount, g.EdgeCount);

            #endregion

            #region runs

            // store the single results
            var runs      = new long[myBFSAlgos.Count, myRuns];
            var innerRuns = new long[myInnerRuns];

            var sw  = new Stopwatch();
            var sum = 0L;

            IVertex        source = null;
            IVertex        target = null;
            List <IVertex> path   = null;
            List <IVertex> path2  = null;

            for (int i = 0; i < myRuns; i++)
            {
                #region init source and target

                while (path == null)
                {
                    source = GetRandomNode(g, isGraphML);
                    target = GetRandomNode(g, isGraphML);

                    if (myBeforeRunAction != null)
                    {
                        myBeforeRunAction(g);
                    }

                    path = myBFSAlgos[0](g, source, target, myMatchingFunc);

                    if (path == null)
                    {
                        Console.WriteLine("random source and target are not connected, trying new ones");
                    }
                }

                Console.WriteLine("got a working path from {0} to {1} with length {2}...starting benchmark", source.UUID, target.UUID, path.Count);

                // k, got a connected source and target


                #endregion

                #region bench

                for (int j = 0; j < myBFSAlgos.Count; j++)
                {
                    for (int k = 0; k < myInnerRuns; k++)
                    {
                        #region before run init

                        if (myBeforeRunAction != null)
                        {
                            myBeforeRunAction(g);
                        }

                        #endregion

                        sw.Start();
                        path2 = myBFSAlgos[j](g, source, target, myMatchingFunc);
                        path2.LongCount();
                        sw.Stop();
                        innerRuns[k] = sw.ElapsedMilliseconds;



                        sw.Reset();

                        #region after run action

                        if (myAfterRunAction != null)
                        {
                            myAfterRunAction(g);
                        }

                        #endregion
                    }
                    runs[j, i] = innerRuns.Aggregate((a, b) => a + b) / myInnerRuns;

                    Console.WriteLine("Algorithm {0} took {1} ms (avg of {2} runs)", j, runs[j, i], myInnerRuns);
                }

                #endregion

                path = null;
            }

            #region calc avg time

            var res = CalculateResult(runs, myBFSAlgos.Count, myRuns);

            #endregion

            #endregion
        }