結果

問題 No.811 約数の個数の最大化
ユーザー Tomohiko HasegawaTomohiko Hasegawa
提出日時 2019-04-13 16:23:34
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 2,899 bytes
コンパイル時間 1,846 ms
コンパイル使用メモリ 61,588 KB
実行使用メモリ 26,952 KB
最終ジャッジ日時 2023-10-13 14:24:29
合計ジャッジ時間 3,329 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
20,756 KB
testcase_01 AC 58 ms
22,820 KB
testcase_02 AC 105 ms
26,952 KB
testcase_03 AC 58 ms
20,692 KB
testcase_04 AC 59 ms
20,952 KB
testcase_05 AC 59 ms
22,832 KB
testcase_06 AC 61 ms
20,644 KB
testcase_07 AC 60 ms
22,752 KB
testcase_08 AC 79 ms
22,804 KB
testcase_09 AC 62 ms
20,800 KB
testcase_10 AC 72 ms
20,856 KB
testcase_11 AC 62 ms
20,760 KB
testcase_12 AC 69 ms
22,800 KB
testcase_13 AC 92 ms
24,868 KB
testcase_14 AC 111 ms
26,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

namespace AtTest.yukicoder
{
    class _811
    {
        static void Main(string[] args)
        {
            Method(args);
        }

        static void Method(string[] args)
        {
            int[] nk = ReadInts();
            int n = nk[0];
            int k = nk[1];
            List<int> list = PrimeFactors(n);
            list.Add(int.MaxValue);
            int baseVal = list[0];
            int maxCnt = 0;
            int maxVal = baseVal;
            for(int i = baseVal; i < n; i+=baseVal)
            {
                List<int> tmpList = PrimeFactors(i);
                int cnt = 0;

                int iter = 0;
                for(int j = 0; j < tmpList.Count; j++)
                {
                    while (list[iter] < tmpList[j]) iter++;
                    if (tmpList[j] == list[iter])
                    {
                        cnt++;
                        iter++;
                    }
                }
                if (cnt < k) continue;

                int tmp = CountDividers(tmpList);
                if (maxCnt < tmp)
                {
                    maxCnt = tmp;
                    maxVal = i;
                }
            }
            WriteLine(maxVal);
        }

        static List<int> PrimeFactors(int n)
        {
            var res = new List<int>();
            int tmp = n;

            for (int i = 2; i * i <= n; i++)
            {
                while (tmp % i == 0)
                {
                    tmp /= i;
                    res.Add(i);
                }
            }
            if (tmp != 1) res.Add(tmp);//最後の素数も返す

            return res;
        }

        static int CountDividers(List<int> primes)
        {
            int res = 1;
            int seq = 2;
            for (int i = 1; i < primes.Count; i++)
            {
                if (primes[i] == primes[i - 1]) seq++;
                else
                {
                    res *= seq;
                    seq = 2;
                }
            }
            res *= seq;
            return res;
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}
0