結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー eitahoeitaho
提出日時 2015-01-28 17:31:19
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 69 ms / 5,000 ms
コード長 2,652 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 107,388 KB
実行使用メモリ 23,832 KB
最終ジャッジ日時 2023-09-05 07:24:09
合計ジャッジ時間 7,119 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
23,704 KB
testcase_01 AC 67 ms
21,704 KB
testcase_02 AC 68 ms
21,856 KB
testcase_03 AC 65 ms
19,664 KB
testcase_04 AC 66 ms
21,692 KB
testcase_05 AC 67 ms
23,716 KB
testcase_06 AC 67 ms
21,708 KB
testcase_07 AC 67 ms
23,696 KB
testcase_08 AC 68 ms
23,792 KB
testcase_09 AC 66 ms
21,700 KB
testcase_10 AC 66 ms
23,740 KB
testcase_11 AC 66 ms
21,704 KB
testcase_12 AC 67 ms
21,880 KB
testcase_13 AC 66 ms
21,684 KB
testcase_14 AC 67 ms
21,716 KB
testcase_15 AC 66 ms
21,704 KB
testcase_16 AC 67 ms
21,712 KB
testcase_17 AC 67 ms
21,628 KB
testcase_18 AC 67 ms
21,756 KB
testcase_19 AC 66 ms
21,724 KB
testcase_20 AC 66 ms
21,796 KB
testcase_21 AC 66 ms
21,888 KB
testcase_22 AC 67 ms
21,696 KB
testcase_23 AC 67 ms
19,628 KB
testcase_24 AC 66 ms
21,708 KB
testcase_25 AC 67 ms
23,832 KB
testcase_26 AC 67 ms
21,644 KB
testcase_27 AC 67 ms
23,744 KB
testcase_28 AC 67 ms
23,760 KB
testcase_29 AC 68 ms
21,824 KB
testcase_30 AC 68 ms
23,708 KB
testcase_31 AC 67 ms
21,744 KB
testcase_32 AC 66 ms
21,688 KB
testcase_33 AC 69 ms
21,772 KB
testcase_34 AC 65 ms
21,704 KB
testcase_35 AC 64 ms
21,624 KB
testcase_36 AC 65 ms
23,756 KB
testcase_37 AC 65 ms
21,628 KB
testcase_38 AC 66 ms
21,820 KB
testcase_39 AC 66 ms
21,788 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using Enu = System.Linq.Enumerable;

class Program
{
    void Solve()
    {
        int N = reader.Int();
        int K = reader.Int();
        var Lines = reader.IntGrid(K);
        var after = Enu.Range(0, N).ToArray();
        foreach (var a in Lines.Reverse())
            Swap(ref after[a[0] - 1], ref after[a[1] - 1]);

        bool[] seen = new bool[N];
        int lcm = 1;
        for (int i = 0; i < N; i++)
            if (!seen[i])
            {
                int len = 0;
                for (int x = i; !seen[x]; x = after[x], len++) { seen[x] = true; }
                lcm = LCM(lcm, len);
            }

        Console.WriteLine(lcm);
    }

    static void Swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
    static int GCD(int a, int b) { return b == 0 ? a : GCD(b, a % b); }
    static int LCM(int a, int b) { return a / GCD(a, b) * b; }





    static void Main() { new Program().Solve(); }
    Reader reader = new Reader(Console.In);
    class Reader
    {
        private readonly TextReader reader;
        private readonly char[] separator = { ' ' };
        private readonly StringSplitOptions removeOp = StringSplitOptions.RemoveEmptyEntries;
        private string[] A = new string[0];
        private int i;

        public Reader(TextReader r) { reader = r; }
        public bool HasNext() { return Enqueue(); }
        public string String() { return Dequeue(); }
        public int Int() { return int.Parse(Dequeue()); }
        public long Long() { return long.Parse(Dequeue()); }
        public double Double() { return double.Parse(Dequeue()); }
        public int[] IntLine() { var s = Line(); return s == "" ? new int[0] : Array.ConvertAll(Split(s), int.Parse); }
        public int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
        public int[][] IntGrid(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
        public string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Line()).ToArray(); }
        public string Line() { return reader.ReadLine().Trim(); }
        private string[] Split(string s) { return s.Split(separator, removeOp); }
        private bool Enqueue()
        {
            if (i < A.Length) return true;
            string line = reader.ReadLine();
            if (line == null) return false;
            if (line == "") return Enqueue();
            A = Split(line);
            i = 0;
            return true;
        }
        private string Dequeue() { Enqueue(); return A[i++]; }
    }
}
0