首先我们考虑对这个 做根号分治,首先 比较小的部分是平凡的,容易做到 ,现在考虑大的部分。
我们发现,大的部分值域上最多只有 段,我们想要知道这些段每一位上是否有任意一个值。那么考虑 bitset,我们发现问题变为了区间提取一段 bitset,然后把提取出来的所有 bitset 做 or 就可以了。那么大的部分复杂度就是 ,平衡一下可以做到复杂度
请认准必可老师。
代码
#include <bits/stdc++.h>
#define Debug(fmt, args...) fprintf(stderr, "(func %s, line #%d): " fmt, __usr_func__, __LINE__, ##args), fflush(stderr)
#define debug(fmt, args...) fprintf(stderr, fmt, ##args), fflush(stderr)
#define ll long long
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pii pair<ll, ll>
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ld lower_bound
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define drep(i, a, b) for (int i = (a); i >= (b); i--)
#define U(i, a, b) for (int i = (a); i < (b); i++)
#define ud upper_bound
#define mem(s, k) memset(s, k, sizeof(s))
#define fi first
#define se second
#define ull unsigned long long
#define vi vector<ll>
#define fv inline void
#define fn inline static
#define sz(x) (int)(x.size())
#define bp(x) __builtin_popcount(x)
using u8 = unsigned char; using u16 = unsigned short; using u32 = unsigned; using u64 = unsigned ll;
using i16 = short; using i32 = int; using i64 = ll; using db = long double;
using namespace std;
const i32 mod = 1e9 + 7, inf = 1e9;
template <typename T> fv chkmax(T &x, T y) { x = x < y ? y : x; }
template <typename T> fv chkmin(T &x, T y) { x = x > y ? y : x; }
fv inc(i32 &x, i32 y) { x = (x + y >= mod ? x + y - mod : x + y); }
fv dec(i32 &x, i32 y) { x = (x - y < 0 ? x - y + mod : x - y); }
fv mul(i32 &x, i32 y) { x = (u64)x * y % mod; }
fn i32 madd(i32 x, i32 y) { return (x + y >= mod ? x + y - mod : x + y); }
fn i32 msub(i32 x, i32 y) { return madd(x, mod - y); }
fn i32 qkpow(i32 a, i32 b) {
i32 ans = 1;
for (; b; b >>= 1) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
class Bitset {
public:
explicit Bitset(size_t n = 0, bool value = false) : m_size(n), m_bits((n + 63) >> 6, value ? ~u64(0) : 0) {
if (value && (n & 63)) m_bits.back() &= (u64(1) << (n & 63)) - 1;
}
Bitset(const string &str) {
m_size = str.size();
m_bits.assign((m_size + 63) >> 6, 0);
drep(i, (i32)m_size - 1, 0) if (str[m_size - 1 - i] == '1') set(i); else if (str[m_size - 1 - i] != '0') abort();
}
size_t size() const { return m_size; }
void set(size_t pos, bool val = true) {
if (pos >= m_size) abort();
if (val) m_bits[pos >> 6] |= u64(1) << (pos & 63);
else reset(pos);
}
void reset(size_t pos) {
if (pos >= m_size) abort();
m_bits[pos >> 6] &= ~(u64(1) << (pos & 63));
}
void flip(size_t pos) {
if (pos >= m_size) abort();
m_bits[pos >> 6] ^= u64(1) << (pos & 63);
}
bool test(size_t pos) const {
if (pos >= m_size) abort();
return (m_bits[pos >> 6] >> (pos & 63)) & 1;
}
bool operator[](size_t pos) const { return test(pos); }
bool any() const {
U(i, 0, m_bits.size()) if (m_bits[i]) return true;
return false;
}
bool none() const { return !any(); }
bool all() const {
if (!m_size) return true;
U(i, 0, m_bits.size() - 1) if (m_bits[i] != ~u64(0)) return false;
u64 mask = (m_size & 63) ? ((u64(1) << (m_size & 63)) - 1) : ~u64(0);
return (m_bits.back() & mask) == mask;
}
size_t count() const {
size_t cnt = 0;
U(i, 0, m_bits.size()) cnt += __builtin_popcountll(m_bits[i]);
return cnt;
}
void reset_all() { fill(m_bits.begin(), m_bits.end(), 0); }
void set_all() {
fill(m_bits.begin(), m_bits.end(), ~u64(0));
if (m_size & 63) m_bits.back() &= (u64(1) << (m_size & 63)) - 1;
}
Bitset& operator&=(const Bitset &other) {
if (m_size != other.m_size) abort();
U(i, 0, m_bits.size()) m_bits[i] &= other.m_bits[i];
return *this;
}
Bitset& operator|=(const Bitset &other) {
if (m_size != other.m_size) abort();
U(i, 0, m_bits.size()) m_bits[i] |= other.m_bits[i];
return *this;
}
Bitset& operator^=(const Bitset &other) {
if (m_size != other.m_size) abort();
U(i, 0, m_bits.size()) m_bits[i] ^= other.m_bits[i];
return *this;
}
Bitset operator~() const {
Bitset res(m_size);
U(i, 0, m_bits.size()) res.m_bits[i] = ~m_bits[i];
if (m_size & 63) res.m_bits.back() &= (u64(1) << (m_size & 63)) - 1;
return res;
}
Bitset operator&(const Bitset &other) const { Bitset res = *this; res &= other; return res; }
Bitset operator|(const Bitset &other) const { Bitset res = *this; res |= other; return res; }
Bitset operator^(const Bitset &other) const { Bitset res = *this; res ^= other; return res; }
Bitset operator<<(size_t shift) const {
if (shift >= m_size) return Bitset(m_size);
Bitset res(m_size);
U(i, shift, m_size) if (test(i - shift)) res.set(i);
return res;
}
Bitset operator>>(size_t shift) const {
if (shift >= m_size) return Bitset(m_size);
Bitset res(m_size);
U(i, 0, m_size - shift) if (test(i + shift)) res.set(i);
return res;
}
Bitset& operator<<=(size_t shift) { *this = *this << shift; return *this; }
Bitset& operator>>=(size_t shift) { *this = *this >> shift; return *this; }
Bitset subbitset(size_t l, size_t r) const {
if (l > r || r >= m_size) abort();
size_t len = r - l + 1;
Bitset res(len);
size_t nb = (len + 63) >> 6;
for (size_t i = 0; i < nb; ++i) {
size_t pos = l + (i << 6);
size_t cnt = (pos + 64 <= l + len) ? 64 : (l + len - pos);
size_t b = pos >> 6;
size_t off = pos & 63;
u64 val = 0;
if (off == 0) {
val = m_bits[b];
} else {
val = m_bits[b] >> off;
if (b + 1 < m_bits.size()) val |= m_bits[b + 1] << (64 - off);
}
if (cnt < 64) val &= (cnt == 0 ? 0 : ((u64(1) << cnt) - 1));
res.m_bits[i] = val;
}
return res;
}
bool operator==(const Bitset &other) const {
if (m_size != other.m_size) return false;
return m_bits == other.m_bits;
}
bool operator!=(const Bitset &other) const { return !(*this == other); }
string to_string() const {
string s;
s.reserve(m_size);
drep(i, (i32)m_size - 1, 0) s.push_back(test(i) ? '1' : '0');
return s;
}
private:
size_t m_size;
vector<u64> m_bits;
};
const i32 N = 1e5 + 5, B = 305;
i32 n, q, m, mx, A[N], ans[N];
bool vis[N];
int main() {
IOS;
Bitset S(200005);
cin >> n >> q;
rep (i, 1, n) cin >> A[i], S.set(A[i], 1), mx = max(mx, A[i]);
rep (i, 1, B) {
rep (j, 1, n) vis[A[j] % i] = 1;
rep (j, 0, B - 1) ans[i] += vis[j];
rep (j, 1, n) vis[A[j] % i] = 0;
}
rep (i, 1, q) {
i32 x; cin >> x;
if (x <= B) cout << ans[x] << "\n";
else {
Bitset R(x);
for (i32 i = 0; i <= mx; i += x) R |= S.subbitset(i, i + x - 1);
cout << R.count() << "\n";
}
}
}