結果
問題 | No.1666 累乗数 |
ユーザー | akakimidori |
提出日時 | 2021-09-03 21:41:18 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 180 ms / 2,000 ms |
コード長 | 3,295 bytes |
コンパイル時間 | 15,470 ms |
コンパイル使用メモリ | 387,844 KB |
実行使用メモリ | 9,964 KB |
最終ジャッジ日時 | 2024-12-15 11:18:51 |
合計ジャッジ時間 | 20,056 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 172 ms
9,876 KB |
testcase_01 | AC | 173 ms
9,964 KB |
testcase_02 | AC | 172 ms
9,824 KB |
testcase_03 | AC | 172 ms
9,864 KB |
testcase_04 | AC | 171 ms
9,952 KB |
testcase_05 | AC | 180 ms
9,800 KB |
testcase_06 | AC | 173 ms
9,952 KB |
testcase_07 | AC | 179 ms
9,796 KB |
testcase_08 | AC | 174 ms
9,860 KB |
testcase_09 | AC | 178 ms
9,668 KB |
testcase_10 | AC | 174 ms
9,856 KB |
testcase_11 | AC | 168 ms
9,868 KB |
testcase_12 | AC | 173 ms
9,940 KB |
testcase_13 | AC | 175 ms
9,844 KB |
testcase_14 | AC | 176 ms
9,804 KB |
testcase_15 | AC | 173 ms
9,864 KB |
testcase_16 | AC | 175 ms
9,724 KB |
testcase_17 | AC | 175 ms
9,912 KB |
testcase_18 | AC | 178 ms
9,792 KB |
testcase_19 | AC | 178 ms
9,836 KB |
コンパイルメッセージ
warning: unused import: `std::io::Write` --> src/main.rs:91:5 | 91 | use std::io::Write; | ^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: type alias `Map` is never used --> src/main.rs:94:6 | 94 | type Map<K, V> = BTreeMap<K, V>; | ^^^ | = note: `#[warn(dead_code)]` on by default warning: type alias `Set` is never used --> src/main.rs:95:6 | 95 | type Set<T> = BTreeSet<T>; | ^^^ warning: type alias `Deque` is never used --> src/main.rs:96:6 | 96 | type Deque<T> = VecDeque<T>; | ^^^^^
ソースコード
// floor(a ^ (1 / k)) fn kth_root(a: u64, k: u64) -> u64 { assert!(k > 0); if a == 0 { return 0; } if k >= 64 { return 1; } if k == 1 { return a; } let valid = |x: u64| -> bool { let mut t = x; for _ in 1..k { let (val, ok) = t.overflowing_mul(x); if !(!ok && val <= a) { return false; } t = val; } true }; let mut ok = 1; let mut ng = 2; while valid(ng) { ok = ng; ng *= 2; } while ng - ok > 1 { let mid = ok + (ng - ok) / 2; if valid(mid) { ok = mid; } else { ng = mid; } } ok } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 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_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_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 ---------- use std::io::Write; use std::collections::*; type Map<K, V> = BTreeMap<K, V>; type Set<T> = BTreeSet<T>; type Deque<T> = VecDeque<T>; fn run() { let mut val = vec![]; let inf = 10u64.pow(18); for b in 3..=60 { for a in 2.. { let mut v = 1u64; for _ in 0..b { v = v.saturating_mul(a); } if v <= inf { let r = kth_root(v, 2); if r * r != v { val.push(v); } } else { break; } } } val.sort(); val.dedup(); let a = val; input! { t: usize, ask: [usize; t], } for k in ask { let mut l = 0; let mut r = inf + 1; while r - l > 1 { let mid = (l + r) / 2; let x = a.binary_search_by_key(&(mid, 1), |p| (*p, 0)).unwrap_err(); let sq = kth_root(mid, 2) as usize; if sq + x >= k { r = mid; } else { l = mid; } } println!("{}", r); } } fn main() { run(); }