結果

問題 No.685 Logical Operations
ユーザー hatoohatoo
提出日時 2018-08-05 20:42:40
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 7,603 bytes
コンパイル時間 11,336 ms
コンパイル使用メモリ 401,884 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-19 18:07:25
合計ジャッジ時間 12,656 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,812 KB
testcase_01 AC 6 ms
6,816 KB
testcase_02 AC 6 ms
6,940 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 6 ms
6,940 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 6 ms
6,944 KB
testcase_07 AC 7 ms
6,940 KB
testcase_08 AC 6 ms
6,944 KB
testcase_09 AC 7 ms
6,940 KB
testcase_10 AC 8 ms
6,944 KB
testcase_11 AC 8 ms
6,944 KB
testcase_12 AC 8 ms
6,940 KB
testcase_13 AC 9 ms
6,944 KB
testcase_14 AC 8 ms
6,940 KB
testcase_15 AC 9 ms
6,940 KB
testcase_16 AC 9 ms
6,940 KB
testcase_17 AC 9 ms
6,944 KB
testcase_18 AC 9 ms
6,944 KB
testcase_19 AC 9 ms
6,940 KB
testcase_20 AC 10 ms
6,940 KB
testcase_21 AC 7 ms
6,940 KB
testcase_22 AC 6 ms
6,944 KB
testcase_23 AC 8 ms
6,940 KB
testcase_24 AC 7 ms
6,944 KB
testcase_25 AC 9 ms
6,944 KB
testcase_26 AC 8 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[doc = " https://github.com/hatoo/competitive-rust-snippets"]
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
#[allow(unused_imports)]
use std::iter::FromIterator;
mod util {
    use std::fmt::Debug;
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn chars() -> Vec<char> {
        line().chars().collect()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { eprintln ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
const BIG_STACK_SIZE: bool = true;
#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

#[allow(dead_code)]
pub fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}
#[allow(dead_code)]
pub fn lcm(a: u64, b: u64) -> u64 {
    a / gcd(a, b) * b
}
#[allow(dead_code)]
#[doc = " (gcd, x, y)"]
pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
    if b == 0 {
        (a, 1, 0)
    } else {
        let (gcd, x, y) = extgcd(b, a % b);
        (gcd, y, x - (a / b) * y)
    }
}
#[allow(dead_code)]
#[doc = " x ^ n % m"]
pub fn mod_pow(x: u64, n: u64, m: u64) -> u64 {
    let mut res = 1;
    let mut x = x % m;
    let mut n = n;
    while n > 0 {
        if n & 1 == 1 {
            res = (res * x) % m;
        }
        x = (x * x) % m;
        n >>= 1;
    }
    res
}
#[allow(dead_code)]
pub fn mod_inverse(a: u64, m: u64) -> u64 {
    let (_, x, _) = extgcd(a as i64, m as i64);
    ((m as i64 + x) as u64 % m) % m
}
#[allow(dead_code)]
pub fn fact_table(len: usize, m: u64) -> Vec<u64> {
    let mut res = vec![1; len + 1];
    for i in 1..len + 1 {
        res[i] = (i as u64 * res[i - 1]) % m;
    }
    res
}
#[allow(dead_code)]
#[doc = " Factorial and Inverse factorial table"]
pub fn fact_inv_table(size: usize, m: u64) -> (Vec<u64>, Vec<u64>) {
    let mut fact = vec![1; size];
    let mut fact_inv = vec![1; size];
    for i in 2..size {
        fact[i] = fact[i - 1] * i as u64 % m;
        fact_inv[i] = m - ((m / i as u64) * fact_inv[(m % i as u64) as usize] % m);
    }
    for i in 1..size {
        fact_inv[i] = fact_inv[i - 1] * fact_inv[i] % m;
    }
    (fact, fact_inv)
}
#[allow(dead_code)]
#[doc = "(a mod p, e when n! = a p^e)"]
pub fn mod_fact(n: u64, p: u64, fact: &[u64]) -> (u64, u64) {
    if n == 0 {
        (1, 0)
    } else {
        let (a, b) = mod_fact(n / p, p, fact);
        let pow = b + n / p;
        if n / p % 2 != 0 {
            (a * (p - fact[(n % p) as usize]) % p, pow)
        } else {
            (a * fact[(n % p) as usize] % p, pow)
        }
    }
}
#[allow(dead_code)]
#[doc = " C(n, k) % p"]
pub fn mod_comb(n: u64, k: u64, p: u64, fact: &[u64]) -> u64 {
    if n < k {
        0
    } else {
        let (a1, e1) = mod_fact(n, p, fact);
        let (a2, e2) = mod_fact(k, p, fact);
        let (a3, e3) = mod_fact(n - k, p, fact);
        if e1 > e2 + e3 {
            0
        } else {
            a1 * mod_inverse(a2 * a3 % p, p) % p
        }
    }
}
#[allow(dead_code)]
#[doc = " H(n, k) % p"]
pub fn mod_comb_repetition(n: u64, k: u64, p: u64, fact: &[u64]) -> u64 {
    mod_comb(n - 1 + k, n - 1, p, fact)
}

#[allow(dead_code)]
pub const M: u64 = 1_000_000_007;

fn solve() {
    let n = get!(usize);

    let mut dp = vec![vec![vec![[0; 2]; 65]; 65]; 65];

    let mut bits = Vec::new();
    let mut t = n;

    while t > 0 {
        bits.push(t % 2 == 1);
        t /= 2;
    }

    bits.reverse();

    dp[0][0][0][0] = 1;

    for (i, &b) in bits.iter().enumerate() {
        if b {
            for j in 0..64 {
                for k in 0..64 {
                    // 0
                    if j > 0 {
                        dp[i + 1][j][k + 1][1] += dp[i][j][k][0];
                        dp[i + 1][j][k + 1][1] += dp[i][j][k][1];

                        dp[i + 1][j][k + 1][1] %= M;
                    } else {
                        dp[i + 1][j][k][1] += dp[i][j][k][0];
                        dp[i + 1][j][k][1] += dp[i][j][k][1];

                        dp[i + 1][j][k][1] %= M;
                    }

                    // 1
                    dp[i + 1][j + 1][k][1] += dp[i][j][k][1];
                    dp[i + 1][j + 1][k][0] += dp[i][j][k][0];

                    dp[i + 1][j + 1][k][0] %= M;
                    dp[i + 1][j + 1][k][1] %= M;
                }
            }
        } else {
            for j in 0..64 {
                for k in 0..64 {
                    // 0
                    if j > 0 {
                        dp[i + 1][j][k + 1][0] += dp[i][j][k][0];
                        dp[i + 1][j][k + 1][1] += dp[i][j][k][1];

                        dp[i + 1][j][k + 1][0] %= M;
                        dp[i + 1][j][k + 1][1] %= M;
                    } else {
                        dp[i + 1][j][k][0] += dp[i][j][k][0];
                        dp[i + 1][j][k][1] += dp[i][j][k][1];
                    }

                    // 1
                    dp[i + 1][j + 1][k][1] += dp[i][j][k][1];

                    dp[i + 1][j + 1][k][1] %= M;
                }
            }
        }
    }

    let l = bits.len();

    let mut ans = 0;
    for i in 1..64 {
        for j in 0..64 {
            let s = dp[l][i][j][0] + dp[l][i][j][1];

            ans += (s * mod_pow(2, j as u64, M) % M) * (mod_pow(2, i as u64 - 1, M) + M - 1) % M;
            ans %= M;
        }
    }

    println!("{}", ans);
}
0