AtCoder Beginner Contest 324 D

https://atcoder.jp/contests/abc324/tasks/abc324_d

各平方数に対し、与えられた文字列の並び替えで実現できるかを調べます。
そのとき、各数字の個数を4ビットで表せば、13桁を64ビット以内で表すことができます。

// Square Permutation
#![allow(non_snake_case)]


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}


//////////////////// process ////////////////////

fn read_input() -> String {
    let _N: usize = read();
    let S: String = read();
    S
}

fn encode(S: String) -> i64 {
    S.chars().map(|c| (c as i64) - ('0' as i64)).
                    map(|d| 1 << (4*d)).sum::<i64>()
}

fn is_representable(mut s: i64, mut n: i64, N: usize) -> bool {
    for _ in 0..N {
        let d = s % 10;
        s /= 10;
        if ((n >> (4*d)) & 15) == 0 {
            return false
        }
        n -= 1 << (4*d)
    }
    true
}

fn F(S: String) -> usize {
    let N = S.len();
    let n = encode(S);
    
    let mut counter: usize = 0;
    let upper = 10i64.pow(N as u32);
    for s in (0..).map(|m| m*m).take_while(|&s| s < upper) {
        if is_representable(s, n, N) {
            counter += 1
        }
    }
    counter
}


//////////////////// main ////////////////////

fn main() {
    let S = read_input();
    println!("{}", F(S))
}