Submission #316493


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using sc = Scanner;

class Program
{

    static void Main(string[] args)
    {
        int n = sc.NextInt();
        int x = sc.NextInt();
        int[] weight  = new int[n];
        int answer = 0;
        for (int i = 0; i < n; i++)
        {
            weight[i] = sc.NextInt();
        }
        if (n == 1) // これに引っかかったくやしい
        {
            Console.WriteLine(weight[0] == x ? 1 : 0);
            return;
        }

        int[] zenhanDP = new int[1 << n/2];
        int[] kohanDP = new int[1 << n - n / 2];
        Dictionary<int,int> zenhanMultiElement = new Dictionary<int,int>();//要素と重複個数を格納,要素をO(logn)で検索可能
        Dictionary<int,int> kohanMultiElement = new Dictionary<int,int>();

        zenhanDP[1] = weight[0];
        for (int i = 1; i < n/2; i++)
            for (int j = 0 ; j < 1 << i ; j++)
                zenhanDP[(1 << i) + j] = zenhanDP[j] + weight[i];

        kohanDP[1] = weight[n/2];
        for (int i = 1; i < n - n/2; i++)
            for (int j = 0; j < 1 << i; j++)
                kohanDP[(1 << i) + j] = kohanDP[j] + weight[n/2 +i];

        Array.Sort(zenhanDP);
        Array.Sort(kohanDP);

        int tmp = zenhanDP[0];
        int tmpCount = 1;
        for (int i = 1; i < zenhanDP.Length; i++)
        {

            if(tmp != zenhanDP[i])
            {
                zenhanMultiElement.Add(tmp,tmpCount);
                tmp = zenhanDP[i];
                tmpCount = 1;
            }
            else
                tmpCount++;
            if(zenhanDP[i] > x) break; //目的値のxを超えた要素は答えにならない,ソートしてあるので以降は無駄
        }
        zenhanMultiElement.Add(tmp,tmpCount);

        tmp = kohanDP[0];
        tmpCount = 1;
        for (int i = 1; i < kohanDP.Length; i++)
        {
            if (tmp != kohanDP[i])
            {
                kohanMultiElement.Add(tmp, tmpCount);
                tmp = kohanDP[i];
                tmpCount = 1;
            }
            else
                tmpCount++;
            if (kohanDP[i] > x) break; //目的値のxを超えた要素は答えにならない,ソートしてあるので以降は無駄
        }
        kohanMultiElement.Add(tmp,tmpCount);

        foreach (var item1 in zenhanMultiElement)
        {
            int count = 0;
            if (kohanMultiElement.TryGetValue(x - item1.Key, out count)) // zenhan + kohan = xとなる要素があれば zenhancount * kohancountの組み合わせがある
            {
                answer += count * item1.Value;
            }
        }
        Console.WriteLine(answer);
    }
}
   
public static class Scanner
{
    public static string NextString()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
            tmp += readData;
        }
        return tmp;
    }

    public static string[] NextStrArray()
    {
        return Console.ReadLine().Split(' ');
    }

    public static long[] NextLongArray()
    {
        string[] s = NextStrArray();
        long[] a = new long[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = long.Parse(s[i]);
        }
        return a;
    }
    public static int[] NextIntArray()
    {

        string[] s = NextStrArray();
        int[] a = new int[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = int.Parse(s[i]);
        }
        return a;
    }
    public static int NextInt()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
        
            tmp += readData;
        }
        return int.Parse(tmp);
    }
    public static double NextDouble()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
            tmp += readData;
        }
        return double.Parse(tmp);
    }
    public static long NextLong()
    {
        string tmp = "";
        while (true)
        {
            string readData = char.ConvertFromUtf32(Console.Read());
            if (readData == " " || readData == "\n")
                break;
            tmp += readData;
        }
        return long.Parse(tmp);
    }
    public static double[] NextDoubleArray()
    {
        string[] s = NextStrArray();
        double[] a = new double[s.Length];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = double.Parse(s[i]);
        }
        return a;
    }
}

Submission Info

Submission Time
Task C - 無駄なものが嫌いな人
User pekoong
Language C# (Mono 2.10.8.1)
Score 100
Code Size 5086 Byte
Status AC
Exec Time 233 ms
Memory 14540 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 24
Set Name Test Cases
All max_1.txt, max_2.txt, max_3.txt, max_4.txt, pair_1.txt, pair_2.txt, power2_1.txt, power2_2.txt, power2_3.txt, power2_4.txt, power2_5.txt, random_1.txt, random_2.txt, random_3.txt, random_4.txt, random_5.txt, random_6.txt, random_7.txt, random_8.txt, random_9.txt, small_1.txt, small_2.txt, small_3.txt, small_4.txt
Case Name Status Exec Time Memory
max_1.txt AC 190 ms 8260 KB
max_2.txt AC 193 ms 8432 KB
max_3.txt AC 192 ms 8376 KB
max_4.txt AC 190 ms 8304 KB
pair_1.txt AC 211 ms 10040 KB
pair_2.txt AC 197 ms 8376 KB
power2_1.txt AC 201 ms 9676 KB
power2_2.txt AC 195 ms 8608 KB
power2_3.txt AC 188 ms 8320 KB
power2_4.txt AC 188 ms 8316 KB
power2_5.txt AC 162 ms 8516 KB
random_1.txt AC 222 ms 14400 KB
random_2.txt AC 217 ms 14540 KB
random_3.txt AC 197 ms 9076 KB
random_4.txt AC 233 ms 14028 KB
random_5.txt AC 198 ms 8344 KB
random_6.txt AC 183 ms 8320 KB
random_7.txt AC 195 ms 8548 KB
random_8.txt AC 210 ms 10052 KB
random_9.txt AC 206 ms 10992 KB
sample_1.txt AC 160 ms 7764 KB
sample_2.txt AC 157 ms 7744 KB
sample_3.txt AC 161 ms 7800 KB
sample_4.txt AC 156 ms 7752 KB
small_1.txt AC 141 ms 7268 KB
small_2.txt AC 141 ms 7284 KB
small_3.txt AC 161 ms 7748 KB
small_4.txt AC 161 ms 7744 KB