結果

問題 No.2104 Multiply-Add
ユーザー 👑 kakel-sankakel-san
提出日時 2024-04-24 00:12:38
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 2,950 bytes
コンパイル時間 13,557 ms
コンパイル使用メモリ 167,108 KB
実行使用メモリ 190,888 KB
最終ジャッジ日時 2024-04-24 00:12:56
合計ジャッジ時間 14,510 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
30,464 KB
testcase_01 AC 56 ms
30,464 KB
testcase_02 AC 60 ms
30,080 KB
testcase_03 AC 58 ms
30,208 KB
testcase_04 AC 59 ms
30,080 KB
testcase_05 AC 60 ms
30,312 KB
testcase_06 AC 62 ms
30,208 KB
testcase_07 AC 58 ms
30,592 KB
testcase_08 AC 59 ms
30,208 KB
testcase_09 AC 61 ms
30,080 KB
testcase_10 AC 60 ms
30,208 KB
testcase_11 AC 60 ms
30,208 KB
testcase_12 AC 59 ms
30,336 KB
testcase_13 AC 58 ms
30,336 KB
testcase_14 AC 59 ms
30,208 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 RE -
testcase_18 AC 59 ms
29,952 KB
testcase_19 AC 60 ms
30,460 KB
testcase_20 AC 59 ms
30,336 KB
testcase_21 AC 59 ms
29,952 KB
testcase_22 RE -
testcase_23 AC 59 ms
30,208 KB
testcase_24 WA -
testcase_25 AC 58 ms
30,464 KB
testcase_26 AC 58 ms
30,592 KB
testcase_27 AC 57 ms
30,080 KB
testcase_28 AC 56 ms
30,336 KB
testcase_29 AC 56 ms
30,080 KB
testcase_30 AC 56 ms
30,464 KB
testcase_31 AC 55 ms
30,336 KB
testcase_32 AC 56 ms
30,208 KB
testcase_33 AC 56 ms
30,080 KB
testcase_34 AC 56 ms
190,888 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (102 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var p = NList;
        var (a, b, c, d) = (p[0], p[1], p[2], p[3]);
        var s = Multiply(a, b, c, d);
        WriteLine(s);
    }
    static string Multiply(int a, int b, int c, int d)
    {
        var (alist, aord) = Rem(a, b);
        var (clist, cord) = Rem(c, d);
        if (alist[^1][0] == clist[^1][0])
        {
            var sb = new System.Text.StringBuilder();
            sb.Append(aord.Count + cord.Count);
            foreach (var ai in aord)
            {
                sb.AppendLine();
                sb.Append($"{ai[0]} {ai[1]}");
            }
            cord.Reverse();
            foreach (var ci in cord)
            {
                sb.AppendLine();
                sb.Append($"{ci[0]} {-ci[1]}");
            }
            return sb.ToString();
        }
        else
        {
            return "-1";
        }
    }
    static (List<long[]> list, List<long[]> ord) Rem(long a, long b)
    {
        var list = new List<long[]>();
        var ord = new List<long[]>();
        list.Add(new long[] { a, b });
        if (a < 0)
        {
            if (b < 0)
            {
                var r = (- a - b - 1) / (- b);
                ord.Add(new long[] { 1, - r });
                a -= b * r;
            }
            else if (b > 0)
            {
                var r = (- a + b - 1) / b;
                ord.Add(new long[] { 1, r });
                a += b * r;
            }
        }
        if (b < 0)
        {
            var r = (- b + a - 1) / a;
            ord.Add(new long[] { 2, r });
            b += a * r;
        }
        while (a != b)
        {
            if (a == 0 && b == 0) break;
            if (a == 0)
            {
                ord.Add(new long[] { 1, 1 });
                a += b;
            }
            else if (b == 0)
            {
                ord.Add(new long[] { 2, 1 });
                b += a;
            }
            else if (a > b)
            {
                var r = a / b - (a % b == 0 ? 1 : 0);
                ord.Add(new long[] { 1, - r });
                a -= b * r;
            }
            else
            {
                var r = b / a - (b % a == 0 ? 1 : 0);
                ord.Add(new long[] { 2, - r });
                b -= a * r;
            }
            list.Add(new long[] { a, b });
        }
        return (list, ord);
    }
}
0