結果
問題 | No.2440 Accuracy of Integer Division Approximate Functions |
ユーザー | 👑 Mizar |
提出日時 | 2023-08-24 18:15:52 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 7 ms / 2,000 ms |
コード長 | 4,382 bytes |
コンパイル時間 | 12,042 ms |
コンパイル使用メモリ | 379,048 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-02 09:59:45 |
合計ジャッジ時間 | 11,825 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 7 ms
5,248 KB |
testcase_02 | AC | 6 ms
5,376 KB |
testcase_03 | AC | 7 ms
5,376 KB |
testcase_04 | AC | 7 ms
5,376 KB |
testcase_05 | AC | 6 ms
5,376 KB |
testcase_06 | AC | 5 ms
5,376 KB |
testcase_07 | AC | 5 ms
5,376 KB |
testcase_08 | AC | 6 ms
5,376 KB |
testcase_09 | AC | 6 ms
5,376 KB |
testcase_10 | AC | 6 ms
5,376 KB |
testcase_11 | AC | 7 ms
5,376 KB |
testcase_12 | AC | 6 ms
5,376 KB |
testcase_13 | AC | 6 ms
5,376 KB |
testcase_14 | AC | 6 ms
5,376 KB |
testcase_15 | AC | 6 ms
5,376 KB |
testcase_16 | AC | 4 ms
5,376 KB |
testcase_17 | AC | 4 ms
5,376 KB |
testcase_18 | AC | 4 ms
5,376 KB |
testcase_19 | AC | 4 ms
5,376 KB |
testcase_20 | AC | 4 ms
5,376 KB |
ソースコード
// calc sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64) pub fn floor_sum_unsigned_mod64(n: u64, mut m: u128, a: u64, b: u64) -> u64 { let (mut ans, mut n, mut a, mut b) = (0u64, n as u128, a as u128, b as u128); // 2^64 <= max(n, m, a, b) < 2^128, a * n + b < 2^128, a < 2^64 while (n | m | a | b) >> 64 != 0 { if a >= m { ans = ans.wrapping_add(((n * (n - 1) >> 1).wrapping_mul(a / m)) as u64); a %= m; } if b >= m { ans = ans.wrapping_add((n.wrapping_mul(b / m)) as u64); b %= m; } let y_max = a * n + b; if y_max < m { return ans; } (n, b, m, a) = (y_max / m, y_max % m, a, m); } let (mut n, mut m, mut a, mut b) = (n as u64, m as u64, a as u64, b as u64); // 2^32 <= max(n, m, a, b) < 2^64 while (n | m | a | b) >> 32 != 0 { if a >= m { ans = ans.wrapping_add( ((n >> 1).wrapping_mul(if (n & 1) == 0 { n - 1 } else { n })).wrapping_mul(a / m), ); a %= m; } if b >= m { ans = ans.wrapping_add(n.wrapping_mul(b / m)); b %= m; } let y_max = (a as u128) * (n as u128) + (b as u128); if (y_max >> 64) == 0 { let y_max = y_max as u64; if y_max < m { return ans; } (n, b) = (y_max / m, y_max % m); } else { (n, b) = ((y_max / (m as u128)) as u64, (y_max % (m as u128)) as u64); } (m, a) = (a, m); } // max(n, m, a, b) < 2^32 loop { if a >= m { ans = ans.wrapping_add( ((n >> 1).wrapping_mul(if (n & 1) == 0 { n - 1 } else { n })).wrapping_mul(a / m), ); a %= m; } if b >= m { ans = ans.wrapping_add(n.wrapping_mul(b / m)); b %= m; } let y_max = a * n + b; if y_max < m { return ans; } (n, b, m, a) = (y_max / m, y_max % m, a, m); } } // calc min(floor(a * 2^s / b), 2^64 - 1) pub fn solve_div_helper(a: u64, b: u128, mut s: u32) -> u64 { assert!(b < (1u128 << 127)); if b == 0 { return !0u64; } let (mut ans, mut a) = (0u64, a as u128); loop { let t = s.min(a.leading_zeros()); a <<= t; if ans > 0 { if ans.leading_zeros() < t { return !0u64; } ans <<= t; } s -= t; ans = match u64::try_from(a / b).ok().and_then(|q| ans.checked_add(q)) { Some(ans) => ans, None => return !0u64, }; a %= b; if s == 0 { return ans; } } } pub fn solve(mut n: u64, d: u64, m: u64, s: u32) -> u64 { use std::cmp::Ordering::*; assert!(n < (1u64 << 60)); assert!(d < (1u64 << 60) && d > 0); assert!(m < (1u64 << 60)); assert!(s < 121); let (pow2s, dm) = (1u128 << s, (d as u128) * (m as u128)); n = n.min(solve_div_helper(d, dm.abs_diff(pow2s), s)); match pow2s.cmp(&dm) { Equal => n, Less => n .wrapping_sub(floor_sum_unsigned_mod64(n + 1, pow2s, m, 0)) .wrapping_add(floor_sum_unsigned_mod64(n + 1, d as u128, 1, 0)), Greater => n .wrapping_add(floor_sum_unsigned_mod64(n + 1, pow2s, m, 0)) .wrapping_sub(floor_sum_unsigned_mod64(n + 1, d as u128, 1, 0)), } } fn main() { use std::io::prelude::*; let tins = std::time::Instant::now(); let stdin = std::io::stdin(); let stdout = std::io::stdout(); let stdinlock = stdin.lock(); let stdoutlock = stdout.lock(); let mut bufwriter = std::io::BufWriter::new(stdoutlock); let mut lines = stdinlock.lines(); let q = lines.next().unwrap().unwrap().parse::<usize>().unwrap(); for _ in 0..q { let l = lines.next().unwrap().unwrap(); let mut token = l.split_ascii_whitespace(); let n = token.next().unwrap().parse::<u64>().unwrap(); let d = token.next().unwrap().parse::<u64>().unwrap(); let m = token.next().unwrap().parse::<u64>().unwrap(); let s = token.next().unwrap().parse::<u32>().unwrap(); writeln!(&mut bufwriter, "{}", solve(n, d, m, s)).unwrap(); } eprintln!("{}us", tins.elapsed().as_micros()); }