結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー hatoohatoo
提出日時 2018-08-06 12:54:16
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,839 ms / 2,000 ms
コード長 5,285 bytes
コンパイル時間 962 ms
コンパイル使用メモリ 185,528 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 22:09:00
合計ジャッジ時間 11,725 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,079 ms
4,348 KB
testcase_01 AC 1,839 ms
4,348 KB
testcase_02 AC 119 ms
4,348 KB
testcase_03 AC 264 ms
4,348 KB
testcase_04 AC 411 ms
4,348 KB
testcase_05 AC 1,471 ms
4,348 KB
testcase_06 AC 1,634 ms
4,348 KB
testcase_07 AC 698 ms
4,348 KB
testcase_08 AC 628 ms
4,348 KB
testcase_09 AC 543 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 76 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `new` is never used
  --> Main.rs:83:8
   |
82 | impl Fib {
   | -------- associated function in this implementation
83 |     fn new() -> Fib {
   |        ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

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() {
    let table = [
        (0, Fib { a: 0, b: 1 }),
        (
            999999734,
            Fib {
                a: 21,
                b: 999999994,
            },
        ),
        (
            999397937,
            Fib {
                a: 999999020,
                b: 610,
            },
        ),
        (
            671232238,
            Fib {
                a: 46368,
                b: 999971350,
            },
        ),
        (
            410141410,
            Fib {
                a: 997821698,
                b: 1346269,
            },
        ),
        (
            510853745,
            Fib {
                a: 102334155,
                b: 936754021,
            },
        ),
        (
            44066357,
            Fib {
                a: 192473059,
                b: 971215059,
            },
        ),
        (
            743595923,
            Fib {
                a: 851432142,
                b: 416138535,
            },
        ),
        (
            72124658,
            Fib {
                a: 790216554,
                b: 470273943,
            },
        ),
        (
            435523618,
            Fib {
                a: 8390086,
                b: 480986305,
            },
        ),
        (
            128493982,
            Fib {
                a: 815449418,
                b: 923369890,
            },
        ),
    ];

    let n = get!(u64);

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

    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 % 1000000000 == 0 {
            table.push((ans, fib.clone()));
        }

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

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