結果

問題 No.811 約数の個数の最大化
ユーザー Tomohiko HasegawaTomohiko Hasegawa
提出日時 2019-04-13 16:23:34
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 2,899 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 112,712 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2024-09-15 11:11:08
合計ジャッジ時間 2,157 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.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