結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー hatoohatoo
提出日時 2018-08-06 12:59:13
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 987 ms / 2,000 ms
コード長 6,657 bytes
コンパイル時間 11,523 ms
コンパイル使用メモリ 378,528 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-19 18:10:34
合計ジャッジ時間 18,370 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 987 ms
6,816 KB
testcase_01 AC 655 ms
6,944 KB
testcase_02 AC 111 ms
6,944 KB
testcase_03 AC 236 ms
6,940 KB
testcase_04 AC 370 ms
6,940 KB
testcase_05 AC 321 ms
6,944 KB
testcase_06 AC 462 ms
6,944 KB
testcase_07 AC 648 ms
6,940 KB
testcase_08 AC 576 ms
6,940 KB
testcase_09 AC 488 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 69 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `new` is never used
  --> src/main.rs:83:8
   |
82 | impl Fib {
   | -------- associated function in this implementation
83 |     fn new() -> Fib {
   |        ^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
mod util {
    use std::fmt::Debug;
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn chars() -> Vec<char> {
        line().chars().collect()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

#[allow(dead_code)]
pub const M: u64 = 1_000_000_007;

#[inline]
fn add(x: u64, y: u64) -> u64 {
    let s = x + y;
    if s >= M {
        s - M
    } else {
        s
    }
}

#[derive(Debug, Eq, Hash, PartialEq, Clone)]
struct Fib {
    a: u64,
    b: u64,
}

impl Fib {
    fn new() -> Fib {
        Fib { a: 0, b: 1 }
    }

    #[inline]
    fn next(&mut self) -> u64 {
        let t = self.a;
        self.a = self.b;
        self.b = add(self.b, t);
        self.a
    }
}

fn solve() {
    const D: u64 = 500000000;

    let table = [
        (0, Fib { a: 0, b: 1 }),
        (
            200000007,
            Fib {
                a: 864787550,
                b: 220121405,
            },
        ),
        (
            999999734,
            Fib {
                a: 21,
                b: 999999994,
            },
        ),
        (
            200012817,
            Fib {
                a: 219772980,
                b: 298960180,
            },
        ),
        (
            999397937,
            Fib {
                a: 999999020,
                b: 610,
            },
        ),
        (
            228284466,
            Fib {
                a: 805882474,
                b: 728750240,
            },
        ),
        (
            671232238,
            Fib {
                a: 46368,
                b: 999971350,
            },
        ),
        (
            623800565,
            Fib {
                a: 903751015,
                b: 449778785,
            },
        ),
        (
            410141410,
            Fib {
                a: 997821698,
                b: 1346269,
            },
        ),
        (
            499553298,
            Fib {
                a: 717820129,
                b: 131647019,
            },
        ),
        (
            510853745,
            Fib {
                a: 102334155,
                b: 936754021,
            },
        ),
        (
            890320855,
            Fib {
                a: 358703167,
                b: 362811371,
            },
        ),
        (
            44066357,
            Fib {
                a: 192473059,
                b: 971215059,
            },
        ),
        (
            438560380,
            Fib {
                a: 423131148,
                b: 816218670,
            },
        ),
        (
            743595923,
            Fib {
                a: 851432142,
                b: 416138535,
            },
        ),
        (
            12431477,
            Fib {
                a: 754133024,
                b: 274911412,
            },
        ),
        (
            72124658,
            Fib {
                a: 790216554,
                b: 470273943,
            },
        ),
        (
            997709618,
            Fib {
                a: 132616976,
                b: 262945064,
            },
        ),
        (
            435523618,
            Fib {
                a: 8390086,
                b: 480986305,
            },
        ),
        (
            932680483,
            Fib {
                a: 12869153,
                b: 366670671,
            },
        ),
        (
            128493982,
            Fib {
                a: 815449418,
                b: 923369890,
            },
        ),
    ];

    let n = get!(u64);

    let i = n / D;
    let j = n % D;

    let mut ans = table[i as usize].0;
    let mut fib = table[i as usize].1.clone();

    for _ in 0..j {
        let t = fib.next();
        ans = add(ans, t * t % M);
    }
    /*
    let mut ans = 0;
    let mut fib = Fib::new();
    for i in 0..n + 1 {
        if i % D == 0 {
            table.push((ans, fib.clone()));
        }

        let t = fib.next();
        ans = add(ans, t * t % M);
    }

    debug!(table);
    */
    println!("{}", ans);
}
0