結果

問題 No.719 Coprime
ユーザー startcppstartcpp
提出日時 2016-11-09 13:15:40
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 4,632 bytes
コンパイル時間 2,471 ms
コンパイル使用メモリ 105,120 KB
実行使用メモリ 89,144 KB
最終ジャッジ日時 2023-08-16 15:45:22
合計ジャッジ時間 9,632 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
84,884 KB
testcase_01 AC 70 ms
87,000 KB
testcase_02 AC 69 ms
84,868 KB
testcase_03 AC 68 ms
84,948 KB
testcase_04 AC 70 ms
84,992 KB
testcase_05 AC 70 ms
85,092 KB
testcase_06 AC 69 ms
84,864 KB
testcase_07 AC 70 ms
84,948 KB
testcase_08 AC 69 ms
84,892 KB
testcase_09 AC 70 ms
84,872 KB
testcase_10 AC 71 ms
86,948 KB
testcase_11 AC 70 ms
86,860 KB
testcase_12 AC 69 ms
84,964 KB
testcase_13 AC 69 ms
84,960 KB
testcase_14 AC 70 ms
84,960 KB
testcase_15 AC 70 ms
86,992 KB
testcase_16 AC 70 ms
82,828 KB
testcase_17 AC 70 ms
85,068 KB
testcase_18 AC 70 ms
84,800 KB
testcase_19 AC 70 ms
84,852 KB
testcase_20 AC 70 ms
84,884 KB
testcase_21 AC 70 ms
86,912 KB
testcase_22 AC 70 ms
84,880 KB
testcase_23 AC 69 ms
84,884 KB
testcase_24 AC 70 ms
86,980 KB
testcase_25 AC 71 ms
86,980 KB
testcase_26 AC 69 ms
82,892 KB
testcase_27 AC 69 ms
84,916 KB
testcase_28 AC 69 ms
84,980 KB
testcase_29 AC 70 ms
87,052 KB
testcase_30 AC 70 ms
84,824 KB
testcase_31 AC 70 ms
82,760 KB
testcase_32 AC 70 ms
85,040 KB
testcase_33 AC 71 ms
86,924 KB
testcase_34 AC 70 ms
84,932 KB
testcase_35 AC 71 ms
84,960 KB
testcase_36 AC 71 ms
84,888 KB
testcase_37 AC 70 ms
87,148 KB
testcase_38 AC 69 ms
87,008 KB
testcase_39 AC 69 ms
82,928 KB
testcase_40 AC 70 ms
85,020 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 System.Threading.Tasks;

namespace Yukicoder
{
    class Coprime
    {
        static int[,] dp = new int[500, 1 << 15];

        public static void Main()
        {
            int n;

            n = int.Parse(Console.ReadLine().ToString());

            List<int> primes = get_primes(n);

            List<List<int>> numbers = get_numbers(primes, n);   //numbers[i] = 最大素因数がprimes[i]である数の集合

            for (int i = 0; i < 500; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    dp[i, j] = -1;   //undefined
                }
            }

            int answer = dfs(n, primes, numbers, 0, 0);

            Console.WriteLine(answer);
        }

        //n…(カードに書かれた)値の上限
        //primes[i]…i(>=0)番目に小さい素数
        //numbers[i]…最大素因数がprimes[i]の数の集合
        //prime_id…最大素因数がprime_id以上の数を今から選ぶ
        //usedの下位iビット目…primes[i]で割り切れる数を既に取ったなら1, 取ってないなら0 (ただし, primes[i] * primes[i] > Nとなるiについては未定義)
        //返り値:これから選ぶ値の和の最大値

        public static int dfs(int n, List<int> primes, List<List<int>> numbers, int prime_id, int used)
        {
            if (prime_id >= primes.Count())
            {
                return 0;
            }
            if (dp[prime_id, used] >= 0)
            {
                return dp[prime_id, used];
            }

            int i, j;
            int max_score = 0;

            max_score = dfs(n, primes, numbers, prime_id + 1, used);

            for (i = 0; i < numbers[prime_id].Count(); i++)
            {
                bool can_take = true;
                int take_num = numbers[prime_id][i];

                for (j = 0; j < prime_id && primes[j] * primes[j] <= n; j++)
                {
                    if (take_num % primes[j] == 0 && (used >> j) % 2 == 1)
                    {
                        can_take = false;
                        break;
                    }
                }

                if (can_take)
                {
                    int new_used = used;

                    for (j = 0; j <= prime_id && primes[j] * primes[j] <= n; j++)
                    {
                        if (take_num % primes[j] == 0)
                        {
                            new_used |= (1 << j);
                        }
                    }
                    max_score = max(max_score, take_num + dfs(n, primes, numbers, prime_id + 1, new_used));
                }
            }
            return (dp[prime_id, used] = max_score);
        }

        public static List<int> get_primes(int max_value)
        {
            int i, j;
            List<int> primes = new List<int>();     //空の動的配列を作って, primesというあだ名を付ける。

            for (i = 2; i <= max_value; i++)
            {
                for (j = 2; j * j <= i; j++)
                {
                    if (i % j == 0)
                    {
                        break;
                    }
                }
                if (j * j > i)
                {
                    primes.Add(i);
                }
            }

            return primes;  //素数テーブルを返します
        }

        public static List<List<int>> get_numbers(List<int> primes, int max_value)
        {
            int i, j;

            List<List<int>> numbers = new List<List<int>>(primes.Count());    //2次元の動的配列を作って, numbersというあだ名をつける。引数は最大容量(capacity)であり, 実際に使えるサイズとは異なるので注意。
            for (i = 0; i < primes.Count(); i++)
            {
                numbers.Add(new List<int>());   //List<int>の指す「実物の配列」を作る, で, そのアドレスをnumbersに追加していく。
            }

            for (i = 2; i <= max_value; i++)
            {
                for (j = primes.Count() - 1; j >= 0; j--)
                {
                    if (i % primes[j] == 0)
                    {
                        numbers[j].Add(i);
                        break;
                    }
                }
            }

            return numbers;
        }

        public static int max(int a, int b)
        {
            if (a > b)
            {
                return a;
            }
            return b;
        }
    }
}
0