結果

問題 No.320 眠れない夜に
ユーザー 14番14番
提出日時 2016-05-18 22:50:29
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 2,981 bytes
コンパイル時間 1,007 ms
コンパイル使用メモリ 109,568 KB
実行使用メモリ 19,968 KB
最終ジャッジ日時 2024-04-15 22:59:22
合計ジャッジ時間 3,340 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
19,712 KB
testcase_01 AC 38 ms
19,584 KB
testcase_02 AC 38 ms
19,840 KB
testcase_03 AC 38 ms
19,584 KB
testcase_04 AC 38 ms
19,584 KB
testcase_05 AC 39 ms
19,328 KB
testcase_06 AC 38 ms
19,584 KB
testcase_07 AC 38 ms
19,840 KB
testcase_08 AC 38 ms
19,712 KB
testcase_09 AC 35 ms
19,328 KB
testcase_10 AC 39 ms
19,840 KB
testcase_11 AC 37 ms
19,968 KB
testcase_12 AC 38 ms
19,968 KB
testcase_13 AC 38 ms
19,584 KB
testcase_14 AC 37 ms
19,712 KB
testcase_15 AC 38 ms
19,712 KB
testcase_16 AC 38 ms
19,456 KB
testcase_17 AC 38 ms
19,712 KB
testcase_18 AC 36 ms
19,584 KB
testcase_19 AC 37 ms
19,584 KB
testcase_20 AC 37 ms
19,712 KB
testcase_21 AC 38 ms
19,584 KB
testcase_22 AC 36 ms
19,584 KB
testcase_23 AC 37 ms
19,584 KB
testcase_24 AC 35 ms
19,328 KB
testcase_25 AC 37 ms
19,712 KB
testcase_26 AC 38 ms
19,584 KB
testcase_27 AC 37 ms
19,584 KB
testcase_28 AC 34 ms
19,456 KB
testcase_29 AC 35 ms
19,584 KB
testcase_30 AC 38 ms
19,328 KB
testcase_31 AC 38 ms
19,584 KB
testcase_32 AC 34 ms
19,328 KB
testcase_33 AC 38 ms
19,712 KB
testcase_34 AC 37 ms
19,712 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        long[] inpt = Reader.ReadLine().Split(' ').Select(a=>long.Parse(a)).ToArray();
        
        long times = inpt[0];
        long count = inpt[1];
        
        long[] list = new long[times + 1];
        list[1] = 1;
        list[2] = 1;
        
        for(long i = 3; i<= times; i++) {
            int idx = (int)i;
            list[idx] = list[idx-1] + list[idx - 2];
        }
        long sa = list[times] - count;
        this.NumList = list.Reverse().Skip(2).ToArray();
        int ans = this.GetAns(0, sa);
        Console.WriteLine(ans);
    }
    
    Dictionary<int, Dictionary<long, int>> dic = new Dictionary<int, Dictionary<long, int>>();
    private int GetAns(int idx, long remain) {
        if(!this.dic.ContainsKey(idx)) {
            this.dic.Add(idx, new Dictionary<long, int>());
        }
        if(dic[idx].ContainsKey(remain)) {
            return dic[idx][remain];
        }
        if(remain == 0) {
            return 0;
        }
        if(idx == this.NumList.Length - 1) {
            if(remain == this.NumList.Last()) {
                return 1;
            } else
            {
                return -1;
            }
        }
        int ans = -1;
        if(this.NumList.ToList().IndexOf(remain) >= idx) {
            ans = 1;
        } else if(this.NumList.Skip(idx).Sum(a=>a) < remain) {
            ans = -1;
        } else {
            int ret = this.GetAns(idx+1, remain);
            if(ret >= 0) {
                if(ans < 0) {
                    ans = ret;
                } else
                {
                    ans = Math.Min(ans, ret); 
                }
            }
            if(remain >= this.NumList[idx]) {
                ret = this.GetAns(idx+1, remain - this.NumList[idx]);
                if(ret >= 0) {
                    ret++;
                    if(ans < 0) {
                        ans = ret;
                    } else
                    {
                        ans = Math.Min(ans, ret);
                    }
                }
            }
        }
        this.dic[idx].Add(remain, ans);
        return ans;
    }
    
    private long[] NumList;
    
    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


80 1




 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0