CF594D REQ [离线+树状数组,欧拉函数]


\[x = \prod_{i=1}^{cnt} p_i^{k_i} [p_i\in prime]\]

那么显然
\[\varphi(x) = x*\frac{1} {\prod_{i=1}^{cnt}p_i}\]

因为每个质数只会出现一次,所以当成数颜色,同 [HH的项链],这题就没了

// powered by c++11
// by Isaunoya
#include 
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define int long long
using pii = pair;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp void cmax(T& x, const T& y) {
  if (x < y) x = y;
}
Tp void cmin(T& x, const T& y) {
  if (x > y) x = y;
}
// sort , unique , reverse
Tp void sort(ve& v) { sort(all(v)); }
Tp void unique(ve& v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
Tp void reverse(ve& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
  ~FILEIN() {}
  char qwq[SZ], *S = qwq, *T = qwq, ch;
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
  FILEIN& operator>>(char& c) {
    while (isspace(c = GETC()))
      ;
    return *this;
  }
  FILEIN& operator>>(string& s) {
    while (isspace(ch = GETC()))
      ;
    s = ch;
    while (!isspace(ch = GETC())) s += ch;
    return *this;
  }
  Tp void read(T& x) {
    bool sign = 1;
    while ((ch = GETC()) < 0x30)
      if (ch == 0x2d) sign = 0;
    x = (ch ^ 0x30);
    while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
    x = sign ? x : -x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  FILEIN& operator>>(signed& x) { return read(x), *this; }
  FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 0x114514;
  char quq[SZ], ST[0x114];
  signed sz, O;
  ~FILEOUT() { flush(); }
  void flush() {
    fwrite(quq, 1, O, stdout);
    fflush(stdout);
    O = 0;
  }
  FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
  FILEOUT& operator<<(string str) {
    if (O > LIMIT) flush();
    for (char c : str) quq[O++] = c;
    return *this;
  }
  Tp void write(T x) {
    if (O > LIMIT) flush();
    if (x < 0) {
      quq[O++] = 0x2d;
      x = -x;
    }
    do {
      ST[++sz] = x % 0xa ^ 0x30;
      x /= 0xa;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
    return;
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  FILEOUT& operator<<(signed x) { return write(x), *this; }
  FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out;

int n, q;
const int maxn = 2e5 + 52;
const int maxp = 1e6 + 61;
const int mod = 1e9 + 7;
int a[maxn], las[maxp];
inline int qpow(int x, int y) {
  int res = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1) res = res * x % mod;
  return res;
}
inline int inv(int x) { return qpow(x, mod - 2); }
struct BIT {
  int c[maxn];
  inline int low(int x) { return x & -x; }
  inline void upd(int x, int y) {
    for (; x <= n; x += low(x)) c[x] = c[x] * y % mod;
  }
  inline int qry(int x) {
    int ans = 1;
    for (; x; x ^= low(x)) ans = ans * c[x] % mod;
    return ans;
  }
} bit;
vector prime[maxp];
vector v[maxn];
void add(int pos) {
  for (auto x : prime[a[pos]]) {
    if (las[x]) bit.upd(las[x], x), bit.upd(las[x], inv(x - 1));
    bit.upd(pos, inv(x)), bit.upd(pos, x - 1);
    las[x] = pos;
  }
}
int pre[maxn];
int ans[maxn];
signed main() {
#ifdef _WIN64
  freopen("testdata.in", "r", stdin);
#else
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
#endif
  // code begin.
  in >> n;
  int mx = 0;
  rep(i, 1, n) in >> a[i], cmax(mx, a[i]);
  pre[0] = 1;
  rep(i, 1, n) pre[i] = pre[i - 1] * a[i] % mod;
  vector vis(mx + 1, 0);
  rep(i, 2, mx) if (!vis[i]) {
    prime[i].push_back(i);
    for (int j = (i << 1); j <= mx; j += i) prime[j].push_back(i), vis[j] = 1;
  }
  in >> q;
  rep(i, 1, q) {
    int l, r;
    in >> l >> r;
    v[r].push_back({ l, i });
  }
  rep(i, 0, n) bit.c[i] = 1;
  rep(i, 1, n) {
    add(i);
    for (auto x : v[i])
      ans[x.second] =
          pre[i] * inv(pre[x.first - 1]) % mod * bit.qry(i) % mod * inv(bit.qry(x.first - 1)) % mod;
  }
  rep(i, 1, q) out << ans[i] << '\n';
  return 0;
  // code end.
}

你可能感兴趣的:(CF594D REQ [离线+树状数组,欧拉函数])