文章目录

BZOJ 4917 Hash Killer IV 做题笔记

发布于

之前没见到过这种题,感觉挺有意思的。

题意

给定函数

unsigned int Hash(unsigned int v){
    unsigned int t = v;
    t = t + (t << 10);
    t = t ^ (t >> 6);
    t = t + (t << 3);
    t = t ^ (t >> 11);
    t = t + (t << 16);
    return t;
}

$Q$ 次询问,每次给出一个值 $t$,要求找出一个 $x$,使得 $\tt Hash\left(x\right) = t$。输出任何一个满足条件的 $x$。

保证存在解。
$1 \leq Q \leq 10^5, 0 \leq t < 2^{32}$

题解

对于 $1, 3, 5$ 操作:可以看做乘上某个奇数,对 $2^{32}$ 取模,他们一定互质,所以可以求出逆元。
对于 $2, 4$ 操作:最高的 $6$ 和 $11$ 位不会变,然后后面的位可以逐个确定。

代码

// Please submit with C++17! It's best to use C++20 or higher version.
// By Koicy (https://koicy.ly)
// rbtree (i@koicy.ly)
// This is my kingdom code.

// #define EFILE ""
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define _CONSOLE 0
#define _MULTI_TESTS 1

using namespace std;
using tp = long long;
constexpr tp ZERO = 0, ONE = 1, INF32 = -1u >> 2, INF = -1ull >> 2;

// :\

namespace _IO {
constexpr size_t BUF = 217217;
unsigned long long h = 5;
char ibuf[BUF], obuf[BUF];
char *li, *ri, *lo = obuf, *ro = obuf + BUF, st[100], *tp = st;
FILE *Istream = stdin, *Ostream = stdout;

char GC() {
#if _CONSOLE
  return getchar();
#endif
  if (li == ri) {
    li = ibuf; ri = li + fread(li, 1, BUF, Istream);
    if (li == ri) return '\n';
  }
  return *li++;
}

void _flush() {
  fwrite(obuf, 1, lo - obuf, Ostream);
  lo = obuf;
}

void PC(char ch) {
#if _CONSOLE
  return (void)putchar(ch);
#endif
  if (lo == ro) _flush();
  *lo++ = ch;
}

template <typename Type>
void r_int(Type& x) {
  bool neg = 0;
  char ch = GC();
  while (ch < 48 || ch > 57) { neg ^= ch == 45; ch = GC(); }
  for (x = 0; ch >= '0' && ch <= '9'; ch = GC()) x = x * 10 + (ch & 15);
  if (neg) x = -x;
}

template <typename Type>
void r_uint(Type& x) {
  char ch = GC();
  while (ch < 48 || ch > 57) ch = GC();
  for (x = 0; ch >= '0' && ch <= '9'; ch = GC()) x = x * 10 + (ch & 15);
}

template <typename Type>
void r_db(Type& x) {
  bool neg = 0;
  char ch = GC();
  while (ch < 48 || ch > 57) { neg ^= ch == 45; ch = GC(); }
  for (x = 0; ch >= '0' && ch <= '9'; ch = GC()) x = x * 10 + (ch & 15);
  if (ch == '.') {
    double p = 1;
    for (ch = GC(); ch >= '0' && ch <= '9'; ch = GC()) x = x + (p /= 10) * (ch & 15);
  }
  if (neg) x = -x;
}

template <typename Type>
void w_uint(Type x) {
  if (!x) { PC('0'); return; }
  while (x) { *tp++ = x % 10; x /= 10; }
  while (tp > st) PC(*--tp ^ 48);
}

template <typename Type>
void w_int(Type x) {
  if (x < 0) { PC('-'); w_uint(-x); }
  else w_uint(x);
}

template <typename Type>
void w_db(Type x) {
  double l = floor(abs(x));
  string s;
  if (x < 0) { PC('-'); x = -x; }
  x -= l;
  if (!l) PC(48);
  while (l) { s.push_back(l - floor(l / 10) * 10); l = floor(l / 10); }
  for (auto i = s.rbegin(); i != s.rend(); ++i) PC(*i ^ 48);
  PC(46);
  for (unsigned long long f = 0; f < h; ++f) {
    x *= 10;
    PC(floor(x - floor(x / 10) * 10) + 48);
  }
}

struct IO {
  IO& operator>>(char& x) { do { x = GC(); } while (x == 32 || x == 10 || x == 13); return *this; }
  IO& operator>>(string& x) {
    char c;
    operator>>(c);
    x = c;
    for (c = GC(); c != 32 && c != 10 && c != 13; c = GC()) x.push_back(c);
    x.shrink_to_fit();
    return *this;
  }
  IO& operator>>(short& x) { r_int(x); return *this; }
  IO& operator>>(int& x) { r_int(x); return *this; }
  IO& operator>>(long& x) { r_int(x); return *this; }
  IO& operator>>(long long& x) { r_int(x); return *this; }
  IO& operator>>(unsigned short& x) { r_uint(x); return *this; }
  IO& operator>>(unsigned int& x) { r_uint(x); return *this; }
  IO& operator>>(unsigned long& x) { r_uint(x); return *this; }
  IO& operator>>(unsigned long long& x) { r_uint(x); return *this; }
  IO& operator>>(float& x) { r_db(x); return *this; }
  IO& operator>>(double& x) { r_db(x); return *this; }

  IO& operator<<(short x) { w_int(x); return *this; }
  IO& operator<<(int x) { w_int(x); return *this; }
  IO& operator<<(long x) { w_int(x); return *this; }
  IO& operator<<(long long x) { w_int(x); return *this; }
  IO& operator<<(unsigned short x) { w_uint(x); return *this; }
  IO& operator<<(unsigned int x) { w_uint(x); return *this; }
  IO& operator<<(unsigned long x) { w_uint(x); return *this; }
  IO& operator<<(unsigned long long x) { w_uint(x); return *this; }
  IO& operator<<(char x) { PC(x); return *this; }
  IO& operator<<(const string& x) { for (auto& i : x) PC(i); return *this; }
  IO& operator<<(float& x) { w_db(x); return *this; }
  IO& operator<<(double& x) { w_db(x); return *this; }
  void flush() { _flush(); }
  void precision(unsigned long long p) { h = p; }
  
  IO() {
#ifdef EFILE
    Istream = fopen(EFILE ".in", "r");
    Ostream = fopen(EFILE ".out", "w");
#else
#ifdef _LOCAL
    Istream = fopen("input.txt", "r");
#endif
#endif
  }

  ~IO() { _flush(); }
} bin;
}
using _IO::bin;

// :/

unsigned t;

unsigned work(unsigned x, unsigned y) {
  for (tp i = 31 - y; i >= 0; --i) x ^= (x & 1u << i + y) >> y;
  return x;
}

void STRUGGLING([[maybe_unused]] tp TEST_NUMBER) {
  bin >> t;
  t *= 4294901761;
  t = work(t, 11);
  t *= 954437177;
  t = work(t, 6);
  t *= 3222273025;
  bin << t << '\n';
}

void MIST() {
}

#include <fstream>

signed main() {
  tp t = 0, _t = 1;
  MIST();
#if _MULTI_TESTS
  bin >> _t;
#endif
  while (t < _t) STRUGGLING(++t);
  return 0;
}

//*/

暂无评论

发表评论