You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.0 KiB

10 months ago
use std::collections::HashSet;
fn er() -> Vec<u32> {
let mut nums = Vec::new();
let size = 1000000;
for n in 0..size+1 { nums.push(true); }
let mut p = 1;
loop {
let mut found = false;
for i in p+1..size {
if nums[i] {
found = true;
p = i;
break;
}
}
if !found { break; }
for i in (2 * p .. size).step_by(p) {
nums[i] = false;
}
}
let mut res = Vec::new();
for i in 2..size {
if nums[i] { res.push(i as u32); }
}
return res;
}
fn rt(n: u32) -> u32 {
let s = n.to_string();
let mut chars = s.chars();
let f = chars.next().unwrap();
//chars.next_back();
let mut res = String::from(chars.as_str());
res.push(f);
res.parse::<u32>().unwrap()
}
fn rp(n: u32, is: u32, k: u8) -> u32 {
let s = n.to_string();
let mut hs = HashSet::new();
let mut iss = is;
while iss > 0 {
hs.insert(iss%10);
iss/=10;
}
let mut ress = 0;
let mut index = 0;
for c in s.chars() {
ress*=10;
if !hs.contains(&(index+1)) {
let cc = c.to_string().parse::<u32>().unwrap();
//println!("--{} {} {} {}", cc, c, index+1, s);
ress += cc;
} else {
ress += k as u32;
}
index+=1;
}
return ress;
}
fn replacements(n: u32, is:u32, ps: &HashSet<u32>) -> usize {
//let mut res = Vec::new();
//println!("-------");
let mut res = 0;
let mut found = false;
for i in 0..=9 {
let r=rp(n, is, i);
if r.to_string().len() < n.to_string().len() { continue; }
if r == n { found = true; }
//println!("{} {} {} {}", r, n, is, i);
if ps.contains(&r) {
//println!(":: {} {} {} {}", r, n,is,i);
//res.push(r);
res+=1;
}
}
if !found { return 0; }
return res;
}
fn rm(n: u32, _: &Vec<u32>, ps: &HashSet<u32>) -> usize {
let len = n.to_string().len();
let rms = lpp(len as u8);
let mut max_rpls = 0;
//println!("len {}", rms.len());
for r in rms.iter() {
if r.to_string().len() > len {
break;
}
//if !is_diff_tn(*r, len as u32) {
// continue;
//}
let rpsss = replacements(n, *r, ps);
//println!("{} {} {}", rpsss, n, *r);
if rpsss > max_rpls {
max_rpls = rpsss;
//println!("-------- {}", *r);
//for r in rpsss.iter() {
// println!("{}", r);
//}
}
}
return max_rpls;
}
fn b_t_is(n: u32) -> u32 {
let mut rr = n;
let mut index = 1;
let mut res = 0;
while rr > 0 {
let c = rr % 2;
if c == 1 {
res*=10;
res+=index;
}
index+=1;
rr/=2;
}
return res;
}
fn lpp(n: u8) -> Vec<u32> {
let mut res = Vec::new();
let bt = 0;
let mut tn = 1;
for _ in 0..n {
tn *= 2;
}
for i in 1..tn {
res.push(b_t_is(i));
}
return res;
}
fn main() {
//let r = b_t_is(5);
//println!("{}", r);
//return;
//let pmsss = lpp(4);
//for p in pmsss.iter() {
// println!("{}", p);
//}
//return;
let ps = er();
let mut hs = HashSet::new();
for p in ps.iter() {
hs.insert(*p);
}
//let r = rp(56003, 13, 1);
//let id = is_diff(763);
//println!("...");
let rms = lpp(6);
//for r in rs.iter() {
// println!("{}", r);
//}
//
//println!(".x.");
//let v8res = rm(120383, &rms, &hs); // 100109,
//for p in v8res.iter() {
// println!("{}", v8res);
//}
//return;
for p in ps.iter() {
let l = rm(*p, &rms, &hs);
//println!("{}", p);
if l == 8 {
//if *p == 56003 {
println!("{}", p);
//}
break;
}
}
//println!("{}", bs);
//for r in rsss {
// println!("{}", r);
//}
}