File tree Expand file tree Collapse file tree 3 files changed +82
-0
lines changed
Expand file tree Collapse file tree 3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ // verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/all/NTL_1_C
2+
3+ use proconio:: input;
4+ use programming_team_code_rust:: primes:: Primes ;
5+
6+ fn is_prime_naive ( n : usize ) -> bool {
7+ if n <= 1 {
8+ return false ;
9+ }
10+ for i in 2 ..n {
11+ if i * i > n {
12+ break ;
13+ }
14+ if n % i == 0 {
15+ return false ;
16+ }
17+ }
18+ true
19+ }
20+
21+ fn main ( ) {
22+ input ! {
23+ a: [ usize ] ,
24+ }
25+
26+ let primes = Primes :: new ( 1001 ) ;
27+ // test is prime
28+ for & elem in & a {
29+ assert_eq ! ( primes. is_prime( elem) , is_prime_naive( elem) ) ;
30+ }
31+ // test factorize (via LCM)
32+ let mut lcm_exps = vec ! [ 0 ; 1001 ] ;
33+ for & elem in & a {
34+ let mut exps = vec ! [ 0 ; 1001 ] ;
35+ let mut prev_factor = 0 ;
36+ for factor in primes. factorize ( elem) {
37+ assert ! ( prev_factor <= factor) ;
38+ exps[ factor] += 1 ;
39+ lcm_exps[ factor] = lcm_exps[ factor] . max ( exps[ factor] ) ;
40+ prev_factor = factor;
41+ }
42+ }
43+ let mut lcm = 1 ;
44+ for i in 2i32 ..1001 {
45+ lcm *= i. pow ( lcm_exps[ i as usize ] ) ;
46+ }
47+ println ! ( "{}" , lcm) ;
48+ }
Original file line number Diff line number Diff line change @@ -2,4 +2,5 @@ pub mod dijk;
22pub mod dsu;
33pub mod fenwick;
44pub mod lca;
5+ pub mod primes;
56pub mod rmq;
Original file line number Diff line number Diff line change 1+ pub struct Primes {
2+ min_fact : Vec < usize > ,
3+ }
4+ impl Primes {
5+ pub fn new ( n : usize ) -> Primes {
6+ let mut min_fact = ( 0 ..n) . collect :: < Vec < _ > > ( ) ;
7+ let mut i = 2 ;
8+ while i * i < n {
9+ if min_fact[ i] == i {
10+ let mut j = i * i;
11+ while j < n {
12+ min_fact[ j] = min_fact[ j] . min ( i) ;
13+ j += i;
14+ }
15+ }
16+ i += 1 ;
17+ }
18+ Primes { min_fact }
19+ }
20+ pub fn is_prime ( & self , x : usize ) -> bool {
21+ x >= 2 && self . min_fact [ x] == x
22+ }
23+ // returns the prime factors of `x` in sorted order
24+ pub fn factorize ( & self , mut x : usize ) -> Vec < usize > {
25+ let mut facts = vec ! [ ] ;
26+ while x > 1 {
27+ let p = self . min_fact [ x] ;
28+ facts. push ( p) ;
29+ x /= p;
30+ }
31+ facts
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments