fn main() { input! { n: usize, m: usize, } if m == 1 { println!("{}", if n == 1 { 1 } else { 0 }); return; } if m == 2 { let ans = if n == 1 { 2 } else if n == 2 { 1 } else { 0 }; println!("{}", ans); return; } let mut f = (0, 1 % n); let mut s = 0; let mut dp = vec![0usize; n]; dp[s] += 1; let mut i = 1; while i < m - 1 { f = (f.1, (f.0 + f.1) % n); s = (s + f.0) % n; if f == (0, 1 % n) && m >= 10 + i { assert!(s == 0); let q = (m - 3) / i; for dp in dp.iter_mut() { *dp *= q; } i += (q - 1) * i; } dp[s] += 1; i += 1; } f = (f.1, (f.0 + f.1) % n); let mut ans = 0; for &v in dp.iter() { ans += v * (v - 1) / 2; } if f.1 == 0 { ans += 2; if n == 1 { ans += 1; } } println!("{}", ans); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ----------