結果

問題 No.2390 Udon Coupon (Hard)
ユーザー 👑 MizarMizar
提出日時 2023-07-05 19:09:46
言語 Rust
(1.77.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,601 bytes
コンパイル時間 10,771 ms
コンパイル使用メモリ 397,816 KB
最終ジャッジ日時 2024-04-27 04:50:17
合計ジャッジ時間 11,363 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error[E0599]: no method named `unwrap` found for struct `String` in the current scope
  --> src/main.rs:50:32
   |
50 |     let n = lines.next().unwrap().unwrap().parse::<u64>().unwrap();
   |                   ------          ^^^^^^ method not found in `String`
   |                   |
   |                   method `unwrap` is available on `Option<String>`

error[E0599]: no method named `unwrap` found for struct `String` in the current scope
  --> src/main.rs:52:33
   |
52 |         let s = lines.next().unwrap().unwrap();
   |                       ------          ^^^^^^ method not found in `String`
   |                       |
   |                       method `unwrap` is available on `Option<String>`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `main` (bin "main") due to 2 previous errors

ソースコード

diff #

// O((A_max)^2)解法
// 入力制約: 0 <= n <= 10^13, 0 < a_i <= 10^5, 0 < b_i, b_max * floor(n / a_min) <= 2^61
fn solve(n: u64, mut ab: [(u64, u64); 3]) -> u64 {
    let (a_min, a_max, b_min, b_max) = ab.iter().fold((!0u64, 0u64, !0u64, 0u64), |(a_min, a_max, b_min, b_max), &(a, b)| (a.min(a_min), a.max(a_max), b.min(b_min), b.max(b_max)));
    assert!(n <= 10_0000_0000_0000);
    assert!(a_min > 0 && a_max <= 100000 && b_min > 0 && b_max.saturating_mul(n / a_min) <= (1u64 << 61));
    
    if ab[1].0 * ab[0].1 < ab[0].0 * ab[1].1 {
    	ab.swap(0, 1);
    }
    if ab[2].0 * ab[0].1 < ab[0].0 * ab[2].1 {
    	ab.swap(0, 2);
    }
    let [(a1, b1), (a2, b2), (a3, b3)] = ab;
    let amax = a1.max(a2).max(a3);
    // a1inv = ceil(2^63 / a1) = floor((2^63 - 1) / a1) + 1
    let a1inv = ((1u64 << 63) - 1) / a1 + 1;
    let (a1b2, a1b3) = (amax * b2, amax * b3);
    let (mut r, mut p, mut s) = (0, 0, n);
    while p < a1b2 {
    	let (mut q, mut t) = (0, s);
        while q < a1b3 {
            // 準定数を除数とする除算の定数倍高速化
            // 0 < a1 <= 10^5, 0 <= t <= n <= 10^13 < 2^63/10^5 <= 2^63 / a1 <= ceil(2^63 / a1) = a1inv < ((2^63 / a1) + 1)
            // quot = floor((n - (i * a2 + j * a3)) / a1) = floor(t / a1) = floor((t * 2) * ceil(2^63 / a1) / 2^64) - (0 または 1)
            let qsim = ((((t * 2) as u128) * (a1inv as u128)) >> 64) as u64;
            let quot = qsim - u64::from(qsim * a1 > t);
            debug_assert_eq!(t / a1, quot);
            r.chmax(quot * b1 + p + q);
			if let Some(d) = t.checked_sub(a3) {
				t = d;
				q += b3;
			} else {
				break;
			}
        }
        if let Some(d) = s.checked_sub(a2) {
        	s = d;
        	p += b2;
        } else {
        	break;
        }
    }
	r
}

fn main() {
    let mut stdinlock = std::io::stdin().lock();
    let mut lines = std::io::BufRead::lines(&mut stdinlock).map_while(Result::ok);
	let n = lines.next().unwrap().unwrap().parse::<u64>().unwrap();
	let ab = (0..3).map(|_| {
		let s = lines.next().unwrap().unwrap();
		let mut t = s.split_whitespace();
		(
            t.next().unwrap().parse::<u64>().unwrap(),
            t.next().unwrap().parse::<u64>().unwrap(),
		)
	}).collect::<Vec<_>>();
    println!("{}", solve(n, [ab[0], ab[1], ab[2]]));
}

trait Change {
    fn chmax(&mut self, x: Self);
    fn chmin(&mut self, x: Self);
}
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) {
        if *self < x {
            *self = x;
        }
    }
    fn chmin(&mut self, x: T) {
        if *self > x {
            *self = x;
        }
    }
}
0