結果
| 問題 |
No.532 Possible or Impossible
|
| コンテスト | |
| ユーザー |
14番
|
| 提出日時 | 2017-06-23 22:51:00 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,725 bytes |
| コンパイル時間 | 2,609 ms |
| コンパイル使用メモリ | 116,576 KB |
| 実行使用メモリ | 63,840 KB |
| 最終ジャッジ日時 | 2024-10-03 03:18:44 |
| 合計ジャッジ時間 | 6,517 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 TLE * 1 -- * 5 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
public class Program
{
public void Proc()
{
int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
this.Target = inpt[1];
decimal[] items = new decimal[inpt[0]];
for (int i = 1; i <= inpt[0]; i++) {
items[i - 1] = i;
}
string ans = "Impossible";
if(CanCreate(items)) {
ans = "Possible";
}
Console.WriteLine(ans);
}
private Dictionary<string, bool> dic = new Dictionary<string, bool>();
private decimal Target = 0;
private bool CanCreate(decimal[] items) {
string key = string.Join("#", items);
if(items.Length == 1) {
return items[0] == Target;
}
if(dic.ContainsKey(key)) {
return dic[key];
}
bool ans = false;
for (int i = 0; i < items.Length-1; i++) {
for (int j = i + 1; j < items.Length; j++) {
List<decimal> subItems = new List<decimal>();
subItems.AddRange(items);
subItems.RemoveAt(j);
subItems.RemoveAt(i);
List<decimal> newItems = new List<decimal>();
newItems.Add(items[i]+items[j]);
newItems.Add(items[i]-items[j]);
newItems.Add(items[j]-items[i]);
newItems.Add(items[i]*items[j]);
if(items[i]!=0) {
newItems.Add(items[j] / items[i]);
}
if(items[j]!=0) {
newItems.Add(items[i] / items[j]);
}
foreach(decimal addItem in newItems) {
List<decimal> tmp = new List<decimal>();
tmp.AddRange(subItems);
tmp.Add(addItem);
if(CanCreate(tmp.OrderBy(a=>a).ToArray())) {
ans = true;
break;
}
}
if(ans) {
break;
}
}
if(ans) {
break;
}
}
dic[key] = ans;
return ans;
}
public class Reader
{
private static StringReader sr;
public static bool IsDebug = false;
public static string ReadLine()
{
if (IsDebug)
{
if (sr == null)
{
sr = new StringReader(InputText.Trim());
}
return sr.ReadLine();
}
else
{
return Console.ReadLine();
}
}
private static string InputText = @"
10 1
";
}
public static void Main(string[] args)
{
#if DEBUG
Reader.IsDebug = true;
#endif
Program prg = new Program();
prg.Proc();
}
}
14番