为啥会TLE

P1591 阶乘数码

Arabidopsis @ 2024-11-05 18:45:00

代码在本机运行正常,几十毫秒,提交后测试点全部TLE。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Issues
{
    public class FactorialDigit
    {
        public static void Main(string[] args)
        {
            var fi = new string[1001];
            fi[1] = "1";
            for (int i = 2; i < 1001; i++)
            {
                fi[i] += AMultipleB.Run(fi[i - 1], i.ToString()); 
            }

            var N = Convert.ToInt32(Console.ReadLine());

            var sb = new StringBuilder();

            for (int i = 0; i < N; i++)
            {
                var data = Console.ReadLine();
                var pos = Convert.ToInt32(data.Split(" ")[0]);
                var cnt = 0;
                foreach (var item in fi[pos])
                {
                    if (item == data.Split(" ")[1][0]) cnt++;
                }
                sb.AppendLine(cnt.ToString());
            }

            Console.WriteLine(sb.ToString());
        }
    }

    public class AMultipleB
{
    public static int[] resultList;

    public static string Run(string ai,string bi)
    {
        var a = ai;
        var b = bi;

        // Construct array
        var listA = new int[a.Length];
        var listB = new int[b.Length];

        for (int i = a.Length; i > 0; i--)
            listA[i - 1] = Convert.ToInt32(a[a.Length - i].ToString());

        for (int i = b.Length; i > 0; i--)
            listB[i - 1] = Convert.ToInt32(b[b.Length - i].ToString());

        // Sequential multiplication
        resultList = new int[4001];

        var iterList = listA.Length <= listB.Length ? listA : listB;
        var multiList = iterList == listA ? listB : listA;

        for (int i = 0; i < iterList.Length; i++)
        {
            for (int j = 0; j < multiList.Length; j++)
            {
                var num = (iterList[i] * multiList[j]).ToString();
                var k = 0;
                if ((iterList[i] * multiList[j]) % 10 == 0) k = 1;
                if ((iterList[i] * multiList[j]) % 10 == 0) k = 1;
                SequentialAddition(num, i + j + k);
            }
        }

        // Biting here
        for (int i = 0; i < resultList.Length; i++)
        {
            var pivot = i;

            if (resultList[pivot] >= 10)
            {
                resultList[pivot + 1] += (int)Math.Floor((decimal)resultList[pivot] / 10);
                resultList[pivot] %= 10;
            }
        }

        var builder = new StringBuilder();
        var zeroLength = 0;
        var flag = true;
        for (int i = resultList.Length - 1; i >= 0; i--)
        {
            builder.Append(resultList[i]);
            if (resultList[i] != 0) flag = false;
            if (flag) zeroLength++;
        }

        var positiveResult = builder.ToString();
        builder.Clear();

        for (int i = zeroLength; i < positiveResult.Length; i++)
        {
            builder.Append(positiveResult[i]);
        }

        var res = builder.ToString();
        if (string.IsNullOrEmpty(res))
        {
            res = "0";
        }

        return res;
    }

    public static void SequentialAddition(string num,int ax10SeqX)
    {
        var numString = ReverseString(num.ToString());

        var index = 0;
        var pureNumString = numString.TrimStart('0');
        for (int i = ax10SeqX; i < pureNumString.Length + ax10SeqX; i++)
        {
            resultList[i] += Convert.ToInt32(pureNumString[index].ToString());
            index++;
        }
    }

    public static string ReverseString(string input)
    {
        var charArray = input.ToCharArray();
        var length = charArray.Length;
        char temp;

        for (int i = 0; i < length / 2; i++)
        {
            temp = charArray[i];
            charArray[i] = charArray[length - i - 1];
            charArray[length - i - 1] = temp;
        }

        return new string(charArray);
    }
}

public class APlusB
{
    public static string Run(string ai,string bi)
    {
        var a = ai;
        var b = bi;

        a = a.PadLeft(b.Length, '0');
        b = b.PadLeft(a.Length, '0');

        // Construct array
        var listA = new int[a.Length];
        var listB = new int[a.Length];

        for (int i = a.Length; i > 0; i--)
            listA[i - 1] = Convert.ToInt32(a[a.Length - i].ToString());

        for (int i = b.Length; i > 0; i--)
            listB[i - 1] = Convert.ToInt32(b[b.Length - i].ToString());

        // Sequential addition
        var maxLength = Math.Max(a.Length, b.Length);
        var result = new int[maxLength + 1];
        var flag = false;

        for (int bit = 0; bit <= maxLength; bit++)
        {
            if (bit == maxLength) result[bit] = flag ? 1 : 0;
            if (bit >= a.Length)
            {
                // Fill with B
                for (int startPos = bit; startPos < maxLength; startPos++)
                {
                    result[startPos] = listB[startPos];
                }
                break;
            }
            if (bit >= b.Length)
            {
                // Fill with A
                for (int startPos = bit; startPos < maxLength; startPos++)
                {
                    result[startPos] = listA[startPos];
                }
                break;
            }

            var sum = listA[bit] + listB[bit];
            sum = flag ? sum + 1 : sum;
            flag = false;

            if (sum >= 10)
            {
                sum -= 10;
                flag = true;
            }

            result[bit] = sum;
        }

        var resOutput = new StringBuilder();
        for (int i = result.Length - 1; i >= 0; i--)
        {
            resOutput.Append(result[i]);
        }

        var oup = resOutput.ToString()[0] == '0' ? resOutput.ToString().Substring(1) : resOutput.ToString();

        return oup;
    }
}
}

by hjb13357896690 @ 2024-11-05 18:52:45

你咋有耐心敲乍多代码啊,我最多两百行(全是大括号什么的),%%%


by hjb13357896690 @ 2024-11-05 18:54:23

@Arabidopsis 已关


by Arabidopsis @ 2024-11-05 19:29:12

@hjb13357896690 C#是这样的()代码一大半是以前写的高精度乘法,直接糊上了


by hjb13357896690 @ 2024-11-05 19:54:51

@Arabidopsis 高精度 * 单精度(不过你下载那数据,看看你是运行不出来还是时间太长但出结果了)


by Arabidopsis @ 2024-11-06 05:03:15

@hjb13357896690 这题不提供数据下载好像,本地造了二十多组数据都是几十毫秒出结果的()就很懵逼


|