zoukankan      html  css  js  c++  java
  • CF594D REQ [离线+树状数组,欧拉函数]

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

    那么显然

    [varphi(x) = x*frac{1} {prod_{i=1}^{cnt}p_i}]

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

    // powered by c++11
    // by Isaunoya
    #include <bits/stdc++.h>
    #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<int, int>;
    #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<class T> void cmax(T& x, const T& y) {
      if (x < y) x = y;
    }
    Tp<class T> void cmin(T& x, const T& y) {
      if (x > y) x = y;
    }
    // sort , unique , reverse
    Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
    Tp<class T> void unique(ve<T>& v) {
      sort(all(v));
      v.erase(unique(all(v)), v.end());
    }
    Tp<class T> void reverse(ve<T>& 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<class T> 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<class T> 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<int> prime[maxp];
    vector<pii> 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<bool> 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] << '
    ';
      return 0;
      // code end.
    }
    
  • 相关阅读:
    219. Contains Duplicate II
    189. Rotate Array
    169. Majority Element
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    119. Pascal's Triangle II
    118. Pascal's Triangle
    88. Merge Sorted Array
    53. Maximum Subarray
    CodeForces 359D Pair of Numbers (暴力)
  • 原文地址:https://www.cnblogs.com/Isaunoya/p/12309430.html
Copyright © 2011-2022 走看看