結果

問題 No.3036 Restricted Lucas (Easy)
ユーザー nebocconebocco
提出日時 2021-03-03 21:15:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,795 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 171,428 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 12:57:35
合計ジャッジ時間 1,672 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{stdout, BufWriter, Read, Write};
use std::ops::{Mul, Div, Rem};

fn main() {
	let mut input = String::new();
	std::io::stdin().read_to_string(&mut input).unwrap();
	let mut newline = String::new();
	newline.push(input.pop().unwrap());
	let mut input = Box::leak(input.into_boxed_str()).split_ascii_whitespace();
	let mut out = BufWriter::new(Box::leak(Box::new(stdout())).lock());
	let t: usize = input.next().unwrap().parse().unwrap();
	let zero = t - t;
	for _ in zero..t {
		let n: isize = input.next().unwrap().parse().unwrap();
		out.write_all(solve(n).to_string().as_bytes()).unwrap();
		out.write_all(newline.as_bytes()).unwrap();
	}
}

fn solve(mut n: isize) -> isize {
	let one = n.div(n);
	let zero = one - one;
	let two = one + one;
	let four = two + two;
	let seven = one | two | four;
	let sfs = seven << four | seven;
	let mut modulo = sfs << (two + seven) | sfs;
	modulo ^= seven << two;
	modulo = modulo << (four + one) ^ seven ^ two;
	modulo = modulo << (seven + two) | seven;
	let mut m = vec![vec![one, one], vec![one, zero]];
	let mut res = vec![vec![one, zero], vec![zero, one]];
	n -= one;
	while n > zero {
		if n & one == one {
			res = mat_mult(&res, &m, modulo);
		}
		m = mat_mult(&m, &m, modulo);
		n >>= one;
	}
	let ans = (res[zero as usize][zero as usize] + res[zero as usize][one as usize].mul(two).rem(modulo)).rem(modulo);
	ans
}

pub fn mat_mult(a: &[Vec<isize>], b: &[Vec<isize>], modulo: isize) -> Vec<Vec<isize>> {
	let n = a.len();
	let one = n.div(n);
	let zero = one - one;
	let m = a[zero].len();
	assert_eq!(b.len(), m);
	let o = b[zero].len();
	let mut res = vec![vec![zero as isize; o]; n];
	for i in zero..n {
		for j in zero..m {
			for k in zero..o {
				res[i][k] = (res[i][k] + a[i][j].mul(b[j][k])).rem(modulo);
			}
		}
	}
	res
}
0