結果

問題 No.2979 直角三角形の個数
ユーザー akakimidoriakakimidori
提出日時 2024-12-03 21:14:09
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 2,550 ms / 4,000 ms
コード長 4,963 bytes
コンパイル時間 12,793 ms
コンパイル使用メモリ 387,656 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-12-03 21:14:33
合計ジャッジ時間 22,531 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
17,552 KB
testcase_01 AC 24 ms
17,436 KB
testcase_02 AC 23 ms
17,464 KB
testcase_03 AC 22 ms
17,548 KB
testcase_04 AC 21 ms
17,464 KB
testcase_05 AC 20 ms
17,460 KB
testcase_06 AC 19 ms
17,480 KB
testcase_07 AC 21 ms
17,452 KB
testcase_08 AC 22 ms
17,416 KB
testcase_09 AC 22 ms
17,436 KB
testcase_10 AC 21 ms
17,464 KB
testcase_11 AC 18 ms
17,536 KB
testcase_12 AC 24 ms
17,432 KB
testcase_13 AC 22 ms
17,500 KB
testcase_14 AC 26 ms
17,484 KB
testcase_15 AC 25 ms
17,500 KB
testcase_16 AC 33 ms
17,524 KB
testcase_17 AC 44 ms
17,396 KB
testcase_18 AC 88 ms
17,644 KB
testcase_19 AC 114 ms
17,472 KB
testcase_20 AC 273 ms
17,588 KB
testcase_21 AC 407 ms
17,584 KB
testcase_22 AC 2,039 ms
17,728 KB
testcase_23 AC 2,124 ms
17,676 KB
testcase_24 AC 20 ms
17,444 KB
testcase_25 AC 26 ms
17,484 KB
testcase_26 AC 21 ms
17,624 KB
testcase_27 AC 23 ms
17,404 KB
testcase_28 AC 2,550 ms
17,792 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `ans`
  --> src/main.rs:49:13
   |
49 |     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:49:9
   |
49 |     let mut ans = 0;
   |         ----^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: function `test` is never used
  --> src/main.rs:45:4
   |
45 | fn test() {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

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 n = n / (2 * m);
        let up = (m - 1).min(n - m);
        let mut l = 1;
        while l <= up {
            let q = n / (m + l);
            let r = n / q - m;
            a.push((l, r.min(up), q));
            l = r + 1;
        }
        sieve.factorize(m, &mut p);
        let mul = if m % 2 == 0 {1} else {2};
        for (i, f) in p.iter().map(|p| *p * mul).enumerate().filter(|p| p.1 <= up) {
            let mut pre = 0;
            let mut prun = f;
            let mut sum = 0;
            for &(_, r, q) in a.iter() {
                if r >= prun {
                    let u = r / f;
                    sum += q * (u - pre);
                    pre = u;
                    prun = u * f + f;
                }
            }
            if i.count_ones() % 2 == 0 {
                ans += sum;
            } else {
                ans -= sum;
            }
        }
    }
    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 ----------
0