Beispiel #1
0
        //[SolutionMethod]
        public PowerSumAnswer BruteForce(PowerSumSample sample)
        {
            Int32 N = sample.N;
            Int32 X = sample.X;

            Int32 result = 0;

            //Int32 MaxAddendumCount = (Int32) Math.Ceiling ((Math.Sqrt(8 * X + 1) - 1) / 2);

            List <Int32> Pows = new List <Int32>()
            {
                0, 1
            };
            Int32 SummPows         = 1;
            Int32 MaxAddendumCount = 2;
            Int32 i = 2;

            while (true)
            {
                Int32 pow = (Int32)Math.Pow(i, N);
                SummPows += pow;
                Pows.Add(pow);
                if (pow >= X)
                {
                    break;
                }

                if (SummPows <= X)
                {
                    MaxAddendumCount++;
                }

                i++;
            }


            for (int j = 1; j <= MaxAddendumCount; j++)
            {
                result += CountAddendums(Pows, j, X, 1);
            }

            return(new PowerSumAnswer()
            {
                ps = result
            });
        }
Beispiel #2
0
        public TAnswer DP(TSample Sample)
        {
            PowerSumSample sample = Sample as PowerSumSample;
            Int32          N      = sample.N;
            Int32          X      = sample.X;

            Int32        result = 0;
            List <Int32> Pows   = new List <Int32>()
            {
                0, 1
            };
            Int32 SummPows         = 1;
            Int32 MaxAddendumCount = 2;
            Int32 Counter          = 2;

            while (true)
            {
                Int32 pow = (Int32)Math.Pow(Counter, N);
                if (pow == X)
                {
                    result++;
                }

                SummPows += pow;
                Pows.Add(pow);

                if (pow >= X)
                {
                    break;
                }

                if (SummPows <= X)
                {
                    MaxAddendumCount++;
                }

                Counter++;
            }

            Int32[,] DP = new Int32[Pows.Count + 1, Pows[Pows.Count - 1] + 1];

            /*
             * for (int i = 0; i < Pows.Count; i++)
             * {
             *  DP[1, Pows[i]] = 1;
             * }
             */

            DP[0, Pows[0]] = 1;

            for (int i = 1; i < Pows.Count; i++)
            {
                for (int j = 0; j <= X; j++)
                {
                    if (Pows[i] <= j)
                    {
                        DP[i, j] = DP[i - 1, j] + DP[i - 1, j - Pows[i]];
                    }
                    else
                    {
                        DP[i, j] = DP[i - 1, j];
                    }
                }
            }


            result = DP[Pows.Count - 1, X];

            return(new PowerSumAnswer()
            {
                ps = result
            });
        }