結果
問題 | No.2979 直角三角形の個数 |
ユーザー | akakimidori |
提出日時 | 2024-12-03 08:23:49 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 3,092 ms / 4,000 ms |
コード長 | 6,061 bytes |
コンパイル時間 | 13,888 ms |
コンパイル使用メモリ | 380,040 KB |
実行使用メモリ | 17,664 KB |
最終ジャッジ日時 | 2024-12-03 08:24:16 |
合計ジャッジ時間 | 23,435 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 26 ms
17,536 KB |
testcase_01 | AC | 25 ms
17,536 KB |
testcase_02 | AC | 24 ms
17,408 KB |
testcase_03 | AC | 24 ms
17,664 KB |
testcase_04 | AC | 23 ms
17,536 KB |
testcase_05 | AC | 24 ms
17,536 KB |
testcase_06 | AC | 25 ms
17,536 KB |
testcase_07 | AC | 24 ms
17,536 KB |
testcase_08 | AC | 25 ms
17,536 KB |
testcase_09 | AC | 32 ms
17,536 KB |
testcase_10 | AC | 25 ms
17,536 KB |
testcase_11 | AC | 24 ms
17,536 KB |
testcase_12 | AC | 24 ms
17,408 KB |
testcase_13 | AC | 25 ms
17,536 KB |
testcase_14 | AC | 28 ms
17,536 KB |
testcase_15 | AC | 29 ms
17,536 KB |
testcase_16 | AC | 34 ms
17,536 KB |
testcase_17 | AC | 53 ms
17,536 KB |
testcase_18 | AC | 107 ms
17,536 KB |
testcase_19 | AC | 134 ms
17,536 KB |
testcase_20 | AC | 330 ms
17,536 KB |
testcase_21 | AC | 484 ms
17,536 KB |
testcase_22 | AC | 2,486 ms
17,664 KB |
testcase_23 | AC | 2,565 ms
17,664 KB |
testcase_24 | AC | 27 ms
17,536 KB |
testcase_25 | AC | 26 ms
17,408 KB |
testcase_26 | AC | 35 ms
17,536 KB |
testcase_27 | AC | 26 ms
17,536 KB |
testcase_28 | AC | 3,092 ms
17,664 KB |
コンパイルメッセージ
warning: unused variable: `ans` --> src/main.rs:81:13 | 81 | let mut ans = 0; | ^^^ help: if this is intentional, prefix it with an underscore: `_ans` | = note: `#[warn(unused_variables)]` on by default warning: variable does not need to be mutable --> src/main.rs:81:9 | 81 | let mut ans = 0; | ----^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default warning: function `test` is never used --> src/main.rs:77:4 | 77 | fn test() { | ^^^^ | = note: `#[warn(dead_code)]` on by default
ソースコード
// m^2-n^2, 2mn, m^2+n^2 // m>n // m, nは互いに素かつ偶奇が違う // 2m(m+n) // // N/(2km(m+n)) // の総和 // // 1 <= n <= m-1 について商列挙っぽいことするとどうなる? // N=L の時Qとして // Q = N/(2m(m+n)) // m+n= N/(2mQ) // n=N/(2mQ)-m // 4secならギリギリ間に合うか? fn main() { input!(n: usize); let sieve = Sieve::new(2000000); let mut p = vec![]; let mut a = vec![]; let mut ans = 0; for m in 2.. { if 2 * m * (m + 1) > n { break; } a.clear(); let up = (m - 1).min(n / (2 * m) - m); let mut l = 1; while l <= up { let q = n / (2 * m * (m + l)); let r = n / (2 * m * q) - m; a.push((l as u32 - 1, r.min(up) as u32, q)); l = r + 1; } sieve.factorize(m, &mut p); if m % 2 == 0 { for (i, &f) in p.iter().enumerate() { let f = f as u32; let mut pre = 0; if i.count_ones() % 2 == 0 { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans += q * (u - pre) as usize; pre = u; } } else { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans -= q * (u - pre) as usize; pre = u; } } } } else { for (i, f) in p.iter().enumerate() { let f = *f as u32 * 2; let mut pre = 0; if i.count_ones() % 2 == 0 { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans += q * (u - pre) as usize; pre = u; } } else { for &(_, r, q) in a.iter().filter(|p| f <= p.1) { let u = r / f; ans -= q * (u - pre) as usize; pre = u; } } } } } println!("{ans}"); } fn test() { let n = 10usize.pow(12); let sieve = Sieve::new(2000000); let mut p = vec![]; let mut ans = 0; let mut sum = 0; for m in 2.. { if 2 * m * (m + 1) > n { break; } let mut l = 1; let mut c = 0; while l <= m - 1 { let q = n / (2 * m * (m + l)); let r = n / (2 * m * q); l = r + 1; c += 1; } p.clear(); sieve.factorize(m, &mut p); sum += c * p.len(); /* if m % 2 == 0 { let up = (m - 1).min(n / (2 * m) - m); for (i, f) in p.iter().enumerate() { if i.count_ones() % 2 == 0 { ans += up / *f; } else { ans -= up / *f; } } } else { let up = (m - 1).min(n / (2 * m) - m) / 2; for (i, f) in p.iter().enumerate() { if i.count_ones() % 2 == 0 { ans += up / *f; } else { ans -= up / *f; } } } */ } println!("{}", sum); } // ---------- 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::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- // --------- end sieve ---------- pub struct Sieve { size: usize, factor: Vec<usize>, } impl Sieve { pub fn new(size: usize) -> Sieve { let mut factor = (0..(size + 1)).collect::<Vec<_>>(); for i in (2..).take_while(|p| p * p <= size) { if i == factor[i] { for j in i..(size / i + 1) { factor[j * i] = i; } } } Sieve { size: size, factor: factor, } } pub fn factor(&self, n: usize) -> Option<usize> { assert!(n <= self.size); if n == 1 { None } else { Some(self.factor[n]) } } pub fn factorize(&self, mut n: usize, res: &mut Vec<usize>) { assert!(n <= self.size); res.clear(); res.push(1); while let Some(p) = self.factor(n) { let len = res.len(); while n % p == 0 { n /= p; } for _ in 0..len { let v = res[res.len() - len] * p; res.push(v); } } } } // --------- end sieve ----------