結果

問題 No.1269 I hate Fibonacci Number
ユーザー keymoonkeymoon
提出日時 2020-10-23 23:13:59
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 241 ms / 3,000 ms
コード長 4,962 bytes
コンパイル時間 4,346 ms
コンパイル使用メモリ 114,084 KB
実行使用メモリ 29,692 KB
最終ジャッジ日時 2023-09-28 18:24:49
合計ジャッジ時間 9,049 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
22,980 KB
testcase_01 AC 76 ms
23,136 KB
testcase_02 AC 85 ms
25,360 KB
testcase_03 AC 78 ms
24,988 KB
testcase_04 AC 77 ms
25,228 KB
testcase_05 AC 80 ms
23,456 KB
testcase_06 AC 78 ms
21,228 KB
testcase_07 AC 78 ms
25,276 KB
testcase_08 AC 79 ms
23,324 KB
testcase_09 AC 78 ms
23,316 KB
testcase_10 AC 77 ms
23,332 KB
testcase_11 AC 78 ms
25,412 KB
testcase_12 AC 77 ms
25,508 KB
testcase_13 AC 86 ms
23,360 KB
testcase_14 AC 225 ms
29,580 KB
testcase_15 AC 100 ms
23,276 KB
testcase_16 AC 234 ms
29,524 KB
testcase_17 AC 80 ms
25,284 KB
testcase_18 AC 241 ms
29,692 KB
testcase_19 AC 197 ms
27,496 KB
testcase_20 AC 108 ms
23,260 KB
testcase_21 AC 166 ms
29,576 KB
testcase_22 AC 168 ms
29,428 KB
testcase_23 AC 169 ms
27,412 KB
testcase_24 AC 120 ms
23,276 KB
testcase_25 AC 135 ms
25,412 KB
testcase_26 AC 82 ms
23,192 KB
testcase_27 AC 74 ms
23,348 KB
testcase_28 AC 84 ms
25,312 KB
testcase_29 AC 136 ms
25,396 KB
testcase_30 AC 96 ms
23,352 KB
testcase_31 AC 196 ms
29,428 KB
testcase_32 AC 138 ms
25,440 KB
testcase_33 AC 96 ms
21,208 KB
testcase_34 AC 74 ms
23,264 KB
testcase_35 AC 94 ms
23,288 KB
testcase_36 AC 78 ms
23,016 KB
testcase_37 AC 75 ms
24,984 KB
testcase_38 AC 85 ms
22,948 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
public static class P
{
    public static void Main()
    {
        var nlr = Console.ReadLine().Split().Select(long.Parse).ToArray();
        var n = (int)nlr[0];
        var l = nlr[1];
        var r = nlr[2];
        //上位互換の文字列がある場合にkill
        List<long> fibs = new List<long>();

        long pp = 0;
        long p = 1;
        while (true)
        {
            var next = pp + p;
            pp = p;
            p = next;
            if (l <= next && next <= r) fibs.Add(next);
            if (r < next) break;
        }
        HashSet<string> fibSet = new HashSet<string>();
        List<string> subSs = new List<string>();
        Dictionary<string, int> dic = new Dictionary<string, int>();
        subSs.Add("");
        dic[""] = 0;
        foreach (var fib in fibs)
        {
            var s = fib.ToString();
            fibSet.Add(s);
            for (int i = 0; i < s.Length; i++)
            {
                var subs = s.Substring(0, i);
                if (fibSet.Any(x => subs.Contains(x))) break;
                if (fibSet.Contains(subs) || dic.ContainsKey(subs)) continue;
                subSs.Add(subs);
                dic[subs] = dic.Count;
            }
        }
     
        int[][] dests = Enumerable.Repeat(0, dic.Count + 1).Select(_ => new int[10]).ToArray();
        dests[dic.Count] = Enumerable.Repeat(dic.Count, 10).ToArray();
        for (int i = 0; i < subSs.Count; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                var nxt = subSs[i] + j.ToString();
                while (true)
                {
                    if (fibSet.Contains(nxt))
                    {
                        dests[i][j] = dic.Count;
                        break;
                    }
                    if (dic.ContainsKey(nxt))
                    {
                        dests[i][j] = dic[nxt];
                        break;
                    }                    
                    nxt = nxt.Substring(1);
                }
            }
        }

        ModInt[] cur = new ModInt[dests.Length];
        cur[dic[""]] = 1;
        for (int i = 0; i < n; i++)
        {
            ModInt[] nxt = new ModInt[dests.Length];
            for (int j = 0; j < dests.Length; j++)
            {
                for (int k = 0; k < 10; k++)
                {
                    nxt[dests[j][k]] += cur[j];
                }
            }
            cur = nxt;
        }
        var lim = (int)Pow(10, n);
        Console.WriteLine(cur.Take(cur.Length - 1).Aggregate(new ModInt(0), (x, y) => x + y) - 1/*stands 0*/);
    }
}


struct ModInt
{
    public const int Mod = 1000000007;
    const long POSITIVIZER = ((long)Mod) << 31;
    long Data;
    public ModInt(long data) { if ((Data = data % Mod) < 0) Data += Mod; }
    public static implicit operator long(ModInt modInt) => modInt.Data;
    public static implicit operator ModInt(long val) => new ModInt(val);
    public static ModInt operator +(ModInt a, int b) => new ModInt() { Data = (a.Data + b + POSITIVIZER) % Mod };
    public static ModInt operator +(ModInt a, long b) => new ModInt(a.Data + b);
    public static ModInt operator +(ModInt a, ModInt b) { long res = a.Data + b.Data; return new ModInt() { Data = res >= Mod ? res - Mod : res }; }
    public static ModInt operator -(ModInt a, int b) => new ModInt() { Data = (a.Data - b + POSITIVIZER) % Mod };
    public static ModInt operator -(ModInt a, long b) => new ModInt(a.Data - b);
    public static ModInt operator -(ModInt a, ModInt b) { long res = a.Data - b.Data; return new ModInt() { Data = res < 0 ? res + Mod : res }; }
    public static ModInt operator *(ModInt a, int b) => new ModInt(a.Data * b);
    public static ModInt operator *(ModInt a, long b) => a * new ModInt(b);
    public static ModInt operator *(ModInt a, ModInt b) => new ModInt() { Data = a.Data * b.Data % Mod };
    public static ModInt operator /(ModInt a, ModInt b) => new ModInt() { Data = a.Data * GetInverse(b) % Mod };
    public static bool operator ==(ModInt a, ModInt b) => a.Data == b.Data;
    public static bool operator !=(ModInt a, ModInt b) => a.Data != b.Data;
    public override string ToString() => Data.ToString();
    public override bool Equals(object obj) => (ModInt)obj == this;
    public override int GetHashCode() => (int)Data;
    static long GetInverse(long a)
    {
        long div, p = Mod, x1 = 1, y1 = 0, x2 = 0, y2 = 1;
        while (true)
        {
            if (p == 1) return x2 + Mod; div = a / p; x1 -= x2 * div; y1 -= y2 * div; a %= p;
            if (a == 1) return x1 + Mod; div = p / a; x2 -= x1 * div; y2 -= y1 * div; p %= a;
        }
    }
}
0