結果

問題 No.2104 Multiply-Add
ユーザー 👑 kakel-sankakel-san
提出日時 2024-04-23 23:53:07
言語 C#
(.NET 8.0.203)
結果
RE  
実行時間 -
コード長 2,660 bytes
コンパイル時間 13,418 ms
コンパイル使用メモリ 167,036 KB
実行使用メモリ 187,416 KB
最終ジャッジ日時 2024-04-23 23:53:33
合計ジャッジ時間 12,807 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
31,360 KB
testcase_01 AC 55 ms
30,336 KB
testcase_02 AC 60 ms
31,232 KB
testcase_03 AC 59 ms
30,848 KB
testcase_04 AC 58 ms
31,104 KB
testcase_05 AC 59 ms
30,848 KB
testcase_06 AC 60 ms
31,104 KB
testcase_07 AC 60 ms
30,848 KB
testcase_08 AC 62 ms
30,976 KB
testcase_09 AC 59 ms
30,976 KB
testcase_10 AC 60 ms
30,976 KB
testcase_11 AC 60 ms
30,976 KB
testcase_12 AC 63 ms
30,976 KB
testcase_13 AC 59 ms
30,848 KB
testcase_14 AC 59 ms
31,104 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 AC 59 ms
31,104 KB
testcase_19 AC 61 ms
30,848 KB
testcase_20 AC 59 ms
31,104 KB
testcase_21 AC 61 ms
30,976 KB
testcase_22 RE -
testcase_23 AC 60 ms
31,092 KB
testcase_24 RE -
testcase_25 AC 54 ms
30,464 KB
testcase_26 AC 59 ms
30,848 KB
testcase_27 AC 53 ms
30,592 KB
testcase_28 AC 56 ms
30,332 KB
testcase_29 AC 54 ms
30,208 KB
testcase_30 AC 55 ms
30,208 KB
testcase_31 AC 55 ms
30,336 KB
testcase_32 AC 55 ms
30,336 KB
testcase_33 AC 55 ms
30,208 KB
testcase_34 AC 54 ms
187,416 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (101 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 (alist, aord) = Rem(a, b);
        var (clist, cord) = Rem(c, d);
        if (alist[^1][0] == clist[^1][0])
        {
            WriteLine(aord.Count + cord.Count);
            WriteLine(string.Join("\n", aord.Select(ai => string.Join(" ", ai))));
            cord.Reverse();
            for (var i = 0; i < cord.Count; ++i) cord[i][1] = -cord[i][1];
            WriteLine(string.Join("\n", cord.Select(ci => $"{ci[0]} {ci[1]}")));
        }
        else
        {
            WriteLine(-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
            {
                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