Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. /*
  2. Author : quickn (quickn.ga)
  3. Email : quickwshell@gmail.com
  4. */
  5.  
  6. use std::str;
  7. use std::io::{self, BufWriter, Write};
  8.  
  9. /// Same API as Scanner but nearly twice as fast, using horribly unsafe dark arts
  10. /// **REQUIRES** Rust 1.34 or higher
  11. pub struct UnsafeScanner<R> {
  12. reader: R,
  13. buf_str: Vec<u8>,
  14. buf_iter: str::SplitAsciiWhitespace<'static>,
  15. }
  16.  
  17. impl<R: io::BufRead> UnsafeScanner<R> {
  18. pub fn new(reader: R) -> Self {
  19. Self {
  20. reader,
  21. buf_str: Vec::new(),
  22. buf_iter: "".split_ascii_whitespace(),
  23. }
  24. }
  25.  
  26. /// This function should be marked unsafe, but noone has time for that in a
  27. /// programming contest. Use at your own risk!
  28. pub fn token<T: str::FromStr>(&mut self) -> T {
  29. loop {
  30. if let Some(token) = self.buf_iter.next() {
  31. return token.parse().ok().expect("Failed parse");
  32. }
  33. self.buf_str.clear();
  34. self.reader
  35. .read_until(b'\n', &mut self.buf_str)
  36. .expect("Failed read");
  37. self.buf_iter = unsafe {
  38. let slice = str::from_utf8_unchecked(&self.buf_str);
  39. std::mem::transmute(slice.split_ascii_whitespace())
  40. }
  41. }
  42. }
  43. }
  44.  
  45. use std::fmt;
  46. use std::cmp::Ordering;
  47. use std::ops::{Add, Mul, Sub};
  48. use std::mem::MaybeUninit;
  49. use std::collections::HashMap;
  50.  
  51. // Little-Endian U256
  52.  
  53. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
  54. struct U256 {
  55. vector: [u64;4]
  56. }
  57.  
  58. impl U256 {
  59. fn new(vector: [u64;4]) -> Self {
  60. Self {
  61. vector
  62. }
  63. }
  64.  
  65. fn from_u64(x: u64) -> Self {
  66. let mut data: [u64;4] = [0;4];
  67. data[0] = x;
  68. Self {
  69. vector: data
  70. }
  71. }
  72. }
  73.  
  74. impl Add for U256 {
  75. type Output = U256;
  76. fn add(self, other: Self) -> Self {
  77. let mut data: [u64;4] = [0;4];
  78. let mut ceil = 0;
  79. for i in 0..4 {
  80. let mut res = ceil;
  81. res += self.vector[i] as u128;
  82. res += other.vector[i] as u128;
  83. ceil = res >> 64;
  84. data[i] = res as u64;
  85. }
  86. //assert_eq!(ceil, 0);
  87. Self {
  88. vector: data,
  89. }
  90. }
  91. }
  92.  
  93. impl Sub for U256 {
  94. type Output = U256;
  95. fn sub(self, other: Self) -> Self {
  96. let mut data: [u64;4] = self.vector;
  97. let mut data2: [u64;4] = other.vector;
  98. for i in 0..data2.len() {
  99. data2[i] = !data2[i];
  100. }
  101. let mut ceil = 0;
  102. for i in 0..4 {
  103. let mut res = ceil;
  104. res += data2[i] as u128;
  105. if i == 0 {
  106. res += 1;
  107. }
  108. ceil = res >> 64;
  109. //dbg!((res, ceil));
  110. data2[i] = res as u64;
  111. }
  112. ceil = 0;
  113. for i in 0..4 {
  114. let mut res = ceil;
  115. res += data[i] as u128;
  116. res += data2[i] as u128;
  117. ceil = res >> 64;
  118. //dbg!((data[i], data2[i]));
  119. data[i] = res as u64;
  120. }
  121. //assert_eq!(ceil, 0);
  122. Self {
  123. vector: data,
  124. }
  125. }
  126. }
  127.  
  128. impl Mul for U256 {
  129. type Output = Self;
  130. fn mul(self, other: Self) -> Self {
  131. let mut data: [u64;4] = [0;4];
  132. let mut ceil = 0;
  133. for i in 0..4 {
  134. let mut res = ceil;
  135. for j in 0..=i {
  136. res += (self.vector[j] as u128)*(other.vector[i-j] as u128);
  137. }
  138. ceil = res >> 64;
  139. data[i] = res as u64;
  140. }
  141. //assert_eq!(ceil, 0);
  142. Self {
  143. vector: data
  144. }
  145. }
  146. }
  147.  
  148. impl PartialOrd for U256 {
  149. fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
  150. if self.vector[3] == other.vector[3] {
  151. if self.vector[2] == other.vector[2] {
  152. if self.vector[1] == other.vector[1] {
  153. if self.vector[0] == other.vector[0] {
  154. Some(Ordering::Equal)
  155. } else {
  156. Some(self.vector[0].cmp(&other.vector[0]))
  157. }
  158. } else {
  159. Some(self.vector[1].cmp(&other.vector[1]))
  160. }
  161. } else {
  162. Some(self.vector[2].cmp(&other.vector[2]))
  163. }
  164. } else {
  165. Some(self.vector[3].cmp(&other.vector[3]))
  166. }
  167. }
  168. }
  169.  
  170. impl Ord for U256 {
  171. fn cmp(&self, other: &Self) -> Ordering {
  172. if self.vector[3] == other.vector[3] {
  173. if self.vector[2] == other.vector[2] {
  174. if self.vector[1] == other.vector[1] {
  175. if self.vector[0] == other.vector[0] {
  176. Ordering::Equal
  177. } else {
  178. self.vector[0].cmp(&other.vector[0])
  179. }
  180. } else {
  181. self.vector[1].cmp(&other.vector[1])
  182. }
  183. } else {
  184. self.vector[2].cmp(&other.vector[2])
  185. }
  186. } else {
  187. self.vector[3].cmp(&other.vector[3])
  188. }
  189. }
  190. }
  191.  
  192. impl fmt::Display for U256 {
  193. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  194. let mut input = format!("{:b}{:064b}{:064b}{:064b}",self.vector[3],self.vector[2],self.vector[1],self.vector[0]);
  195. let mut input_chars: Vec<char> = input.chars().collect();
  196. while input_chars.len() > 1 && input_chars.first().unwrap() == &'0' {
  197. input_chars.remove(0);
  198. }
  199. let mut n = input_chars.len();
  200. let mut input2: Vec<u8> = vec![0];
  201. for i in 0..n {
  202. input2.push(0);
  203. if input2[0] >= 5 { input2[0] += 3; }
  204. input2[0] <<= 1;
  205. input2[0] |= (input_chars[0] as u8) - ('0' as u8);
  206. input_chars.remove(0);
  207. let mut ceil = input2[0]>>4;
  208. if ceil >= 1 {
  209. input2[0] -= ceil<<4;
  210. }
  211. for j in 1..input2.len() {
  212. if input2[j] >= 5 { input2[j] += 3; }
  213. input2[j] <<= 1;
  214. input2[j] |= ceil;
  215. ceil = input2[j]>>4;
  216. if ceil >= 1 {
  217. input2[j] -= ceil<<4;
  218. }
  219. }
  220. }
  221. while input2.len() > 1 && input2.last().unwrap() == &0 {
  222. input2.remove(input2.len()-1);
  223. }
  224. Ok(for i in 0..input2.len() {
  225. write!(f, "{}", input2[input2.len()-1-i])?
  226. })
  227. }
  228. }
  229.  
  230. fn main() {
  231. let (stdin, stdout) = (io::stdin(), io::stdout());
  232. let (mut scan, mut sout) = (UnsafeScanner::new(stdin.lock()), BufWriter::new(stdout.lock()));
  233. let (k, n): (U256, usize) = (U256::from_u64(scan.token::<u64>()), scan.token());
  234. let mut dfs = unsafe { MaybeUninit::zeroed().assume_init() };
  235. let dfs_rec = &mut dfs as *mut dyn FnMut((U256, U256, U256), u8, u8);
  236. let init_sol: (U256, U256, U256) = (U256::from_u64(0), U256::from_u64(1), k);
  237. let mut res = vec![];
  238. dfs = |(a, b, c): (U256, U256, U256), prev_visit: u8, depth: u8| {
  239. if depth < 13 {
  240. if k*(b+c) >= a && prev_visit != 1 {
  241. unsafe { (*dfs_rec)((k*(b+c)-a, b, c), 1, depth+1) };
  242. }
  243. if k*(a+c) >= b && prev_visit != 2 {
  244. unsafe { (*dfs_rec)((a, k*(a+c)-b, c), 2, depth+1) };
  245. }
  246. if k*(a+b) >= c && prev_visit != 3 {
  247. unsafe { (*dfs_rec)((a, b, k*(a+b)-c), 3, depth+1) };
  248. }
  249. }
  250. res.push((a, b, c))
  251. };
  252. //println!("{}", U256::new([0,1,0,0])*U256::new([0,1,0,0]));
  253. dfs(init_sol, 0, 1);
  254. let mut cnt = 0;
  255. let mut i = 0;
  256. let mut hash: HashMap<U256, bool> = HashMap::new();
  257. while cnt < n {
  258. let (a, b, c) = res[i];
  259. if hash.get(&a).is_none()
  260. && hash.get(&b).is_none()
  261. && hash.get(&c).is_none()
  262. && format!("{}", a).len() <= 100
  263. && format!("{}", b).len() <= 100
  264. && format!("{}", c).len() <= 100 {
  265. //println!("{}", cnt);
  266. writeln!(sout, "{} {} {}", a, b, c).ok();
  267. //assert_eq!(a*a + b*b + c*c, k*(a*b + b*c + a*c)+U256::from_u64(1));
  268. hash.insert(a, true);
  269. hash.insert(b, true);
  270. hash.insert(c, true);
  271. cnt += 1;
  272. }
  273. i += 1;
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement