ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1048. Superlong Sums

WA 4 Memory limit exceeded C#
Posted by Daniil 31 May 2022 12:02
using System;

class Progran
{
    public static void Main()
    {
        int Size = int.Parse(Console.ReadLine());
        sbyte[] FirstNumber = new sbyte[Size + 1];

        for (int i = 0; i < Size; i++)
        {
            string[] s = Console.ReadLine().Split(' ');
            FirstNumber[i + 1] = (SByte)(sbyte.Parse(s[0]) + sbyte.Parse(s[1]));
        }

        for (int i = Size; i > 0; i--)
            if (FirstNumber[i] >= 10)
            {
                FirstNumber[i - 1]++;
                FirstNumber[i] -= 10;
            }

        for (int i = 0; i < Size; i++)
                Console.Write(FirstNumber[i + 1]);

    }
}

Edited by author 31.05.2022 12:17

I ported this code to c++ and got AC ?!

Edited by author 31.05.2022 15:01
Re: WA 4 Memory limit exceeded C#
Posted by magicdranton 15 Nov 2022 17:05
I finally got accepted it on a C#!! Main memory issue here are always any Strings! Awoid using Console.ReadLine() in cycle. For some reason, .net sometimes does not use string interning. I got below accepted using only Console.Read() byte-by-byte:

class Program
    {
        static int N = 0;
        static byte[] Digs1 = null;
        static char m_Zero = '0';

        static void _ReadData()
        {
            string v_Str = Console.ReadLine();
            if (v_Str == null) return false;
            N = Int32.Parse(v_Str);
            Digs1 = new byte[N];
            int v_TmpVal = 0;

            // Fill Digs
            for (int i = 0; i < N; i++)
            {
                v_TmpVal = Console.Read();
                Digs1[i] = (byte)(Convert.ToChar(v_TmpVal) - m_Zero);
                Console.Read();
                v_TmpVal = Console.Read();
                Digs1[i] += (byte)((Convert.ToChar(v_TmpVal) - m_Zero) * 10);
                Console.Read(); //#13
                Console.Read(); //#10
            }
static void SolveProblem()
        {
            bool v_AddOne = false;
            bool v_NextAddOne = false;
            byte v_First = 0;
            byte v_Second = 0;

            for (int i = Digs1.Length - 1; i >= 0; i--)
            {
                v_First = (byte)(Digs1[i] % 10);
                v_Second = (byte)(Digs1[i] / 10);

                v_AddOne = (v_First + v_Second + (v_NextAddOne ? 1 : 0)) > 9;

                Digs1[i] = (byte)((v_First + v_Second + (v_NextAddOne ? 1 : 0)) % 10);
                v_NextAddOne = v_AddOne;
            }
        }
 static void Main(string[] args)
        { _ReadData(); SolveProblem();

            for (int i = 0; i < N; i++)
            {
                Console.Write(Digs1[i]);
            }
}
Re: WA 4 Memory limit exceeded C#
Posted by Hristo Nikolaev (B&W) 24 Nov 2022 20:45
Hi, I`m happy for you. And it`s great that you are sharing your thoughts.
However, you are not helping anyone by posting your code.
Please don`t do it, and we all will be happier.