結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー 14番14番
提出日時 2016-04-12 01:55:53
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 265 ms / 5,000 ms
コード長 5,722 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 115,976 KB
実行使用メモリ 56,588 KB
最終ジャッジ日時 2024-04-15 00:32:17
合計ジャッジ時間 4,515 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
24,760 KB
testcase_01 AC 263 ms
56,588 KB
testcase_02 AC 265 ms
54,504 KB
testcase_03 AC 42 ms
25,784 KB
testcase_04 AC 42 ms
27,428 KB
testcase_05 AC 77 ms
27,776 KB
testcase_06 AC 43 ms
25,532 KB
testcase_07 AC 61 ms
32,004 KB
testcase_08 AC 58 ms
29,672 KB
testcase_09 AC 48 ms
27,812 KB
testcase_10 AC 47 ms
25,512 KB
testcase_11 AC 47 ms
25,584 KB
testcase_12 AC 54 ms
27,776 KB
testcase_13 AC 57 ms
27,632 KB
testcase_14 AC 47 ms
25,660 KB
testcase_15 AC 49 ms
25,788 KB
testcase_16 AC 55 ms
27,752 KB
testcase_17 AC 53 ms
27,628 KB
testcase_18 AC 47 ms
25,664 KB
testcase_19 AC 50 ms
25,532 KB
testcase_20 AC 47 ms
27,428 KB
testcase_21 AC 53 ms
29,824 KB
testcase_22 AC 48 ms
23,604 KB
testcase_23 AC 59 ms
27,896 KB
testcase_24 AC 45 ms
25,532 KB
testcase_25 AC 56 ms
29,804 KB
testcase_26 AC 58 ms
27,512 KB
testcase_27 AC 57 ms
25,716 KB
testcase_28 AC 46 ms
25,664 KB
testcase_29 AC 58 ms
29,696 KB
testcase_30 AC 53 ms
27,768 KB
testcase_31 AC 56 ms
27,776 KB
testcase_32 AC 51 ms
25,388 KB
testcase_33 AC 58 ms
27,892 KB
testcase_34 AC 41 ms
25,272 KB
testcase_35 AC 43 ms
25,648 KB
testcase_36 AC 39 ms
25,520 KB
testcase_37 AC 42 ms
25,788 KB
testcase_38 AC 42 ms
25,656 KB
testcase_39 AC 42 ms
25,660 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.Collections.Generic;
using System.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int lineCount = int.Parse(Reader.ReadLine());
        int inputCount = int.Parse(Reader.ReadLine());

        int[] dic = new int[lineCount];

        List<int> numList = new List<int>();
        int ans = lineCount;
        for (int i = 1; i <= lineCount; i++)
        {
            numList.Add(i-1);
        }

        List<AmidaLine> amidaList = new List<AmidaLine>();
        for (int i = 0; i < inputCount; i++)
        {
            int[] inpt = Reader.GetInt();
            AmidaLine al = new AmidaLine(inpt[0]-1, inpt[1]-1, i);
            amidaList.Add(al);
        }

        for (int i = 0; i < lineCount; i++)
        {
            int pos = -1;
            int idx = i;
            for (int j = 0; j < inputCount; j++)
            {
                List<AmidaLine> select = new List<AmidaLine>(amidaList.FindAll(a => (a.Line1 == idx || a.Line2 == idx) && a.Pos > pos).OrderBy(b => b.Pos));
                if (select.Count == 0)
                {
                    break;
                }
                pos = select[0].Pos;
                idx = select[0].Goto(idx);
            }
            dic[i] = idx;
        }

        int[] target = numList.ToArray();
        int[] conv = new int[target.Length];
        int[] syuki = new int[target.Length];
        for (int i = 0; i < int.MaxValue; i++)
        {
            conv = new int[target.Length];
            bool isMatch = true;
            for (int j = 0; j < target.Length; j++)
            {
                conv[dic[j]] = target[j];
                if (dic[j] != target[j])
                {
                    isMatch = false;
                }
                else if(syuki[dic[j]] == 0)
                {
                    syuki[dic[j]] = i + 1;
                    if (syuki.Count((a) => { return a == 0; }) == 0)
                    {
                        Console.WriteLine(this.GetKoubai(syuki));
                        return;
                    } 
                }
            }
            target = conv;
            if (isMatch)
            {
                ans = i + 1;
                break;
            }

        }
        Console.WriteLine(ans);

    }


    private long GetKoubai(int[] list)
    {
        List<int> target = new List<int>(list.Distinct());

        Dictionary<int, int> nums = new Dictionary<int, int>();
        target.ForEach((a) =>
        {
            Dictionary<int, int> ret = this.GetSoin(a);
            foreach (int key in ret.Keys)
            {
                if (!nums.ContainsKey(key))
                {
                    nums.Add(key, ret[key]);
                }
                else
                {
                    nums[key] = Math.Max(nums[key], ret[key]);
                }
            }

        });

        long ans = 1;
        foreach (int key in nums.Keys)
        {
            for (int i = 1; i <= nums[key]; i++)
            {
                ans *= key;
            }
        }
        return ans;
    }

    private Dictionary<int, int> GetSoin(int src)
    {
        int num = src;
        Dictionary<int, int> ans = new Dictionary<int, int>();
        while (num > 1)
        {
            bool isProc = false;
            for (int i = 2; i <= Math.Sqrt(num); i++)
            {
                if (num % i == 0)
                {
                    isProc = true;
                    if (!ans.ContainsKey(i))
                    {
                        ans.Add(i, 1);
                    }
                    else
                    {
                        ans[i]++;
                    }
                    num = num / i;
                    break;
                }
            }
            if (!isProc)
            {
                if (!ans.ContainsKey(num))
                {
                    ans.Add(num, 1);
                }
                else
                {
                    ans[num]++;
                }
                break;
            }
        }
        return ans;
    }

    public class AmidaLine
    {
        public int Line1;
        public int Line2;
        public int Pos;
        public int Goto(int from)
        {
            if (from == Line1)
            {
                return Line2;
            }
            return Line1;
        }
        public AmidaLine(int l1, int l2, int p)
        {
            this.Line2 = l2;
            this.Line1 = l1;
            this.Pos = p;
        }
    }


    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"






 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0