Advertisement
Guest User

1497.cpp

a guest
Jun 9th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. #define pb emplace_back
  8. #define fo(i, n) for(int i = 1; i <= n; ++i)
  9.  
  10. typedef long long ll;
  11.  
  12. const int N = 1000300;
  13. const int mod = 1e9 + 7;
  14.  
  15. int lp[N];
  16. int l[200200], r[200200];
  17. int a[200200], n, q;
  18. int ans[200200];
  19. int cnt[N];
  20. int X[N];
  21. vector<int> e[200200];
  22.  
  23. inline void sieve() {
  24.     for(int i = 2; i <= 1000000; ++i) {
  25.         if(!lp[i]) {
  26.             lp[i] = i;
  27.             if(i * 1ll * i <= 1000000)
  28.                 for(int j = i * i; j <= 1000000; j += i)
  29.                     lp[j] = i;
  30.         }
  31.     }
  32. }
  33. vector<pair<int, int> > primes;
  34. vector<int> divi, pr;
  35.  
  36. inline void calc(int p = 0, int x = 1) {
  37.     if(p == (int)primes.size()) {
  38.         divi.pb(x);
  39.         return;
  40.     }
  41.     calc(p + 1, x);
  42.     for(int i = 0; i < primes[p].second; ++i) {
  43.         x *= primes[p].first;
  44.         calc(p + 1, x);
  45.     }
  46. }
  47.  
  48. inline void gen_divisors(int n)
  49. {
  50.     primes.clear();
  51.     divi.clear();
  52.     while(n > 1)
  53.     {
  54.         int x = lp[n], st = 0;
  55.         while(n % x == 0) n /= x, st++;
  56.         primes.pb(x, st);
  57.     }
  58.     calc();
  59. }
  60.  
  61. int sign, cur_id, s2;
  62.  
  63. inline void gen_masks(int p = 0, int x = 1, int c = 0) {
  64.     if(p == (int)pr.size()) {
  65.         if(c) s2 = -1;
  66.         else s2 = 1;
  67.         ans[cur_id] += cnt[x] * s2 * sign;
  68.         return ;
  69.     }
  70.     gen_masks(p + 1, x * pr[p], c ^ 1);
  71.     gen_masks(p + 1, x, c);
  72. }
  73.  
  74. // Fastest INPUT/OUTPUT:
  75. char getChar();
  76. int getInt();
  77. void putChar(char);
  78. void putInt(int);
  79.  
  80. int main() {
  81.     sieve();
  82.     n = getInt();
  83.    
  84.     fo(i, n) {
  85.         a[i] = getInt();
  86.     }
  87.     q = getInt();
  88.     fo(it, q) {
  89.         l[it] = getInt();
  90.         r[it] = getInt();
  91.         X[it] = getInt();
  92.         e[l[it]-1].pb(-it);
  93.         e[r[it]].pb(it);
  94.     }
  95.     fo(i, n) {
  96.         gen_divisors(a[i]);
  97.         for(int j : divi) cnt[j]++;
  98.         for(int t = 0; t < (int)e[i].size(); ++t) {
  99.             int id = e[i][t], x = X[abs(id)];
  100.             pr.clear();
  101.             while(x > 1) {
  102.                 int y = lp[x];
  103.                 pr.pb(y);
  104.                 while(x % y == 0) x /= y;
  105.             }
  106.             sign = (id < 0 ? -1 : 1), cur_id = abs(id);
  107.             gen_masks();
  108.         }
  109.     }
  110.     fo(it, q) {
  111.         putInt(ans[it]);
  112.         putChar('\n');
  113.     }
  114.     putChar(-1);
  115.  
  116.     return 0;
  117. }
  118.  
  119. char getChar() {
  120.     static char buf[1<<20];
  121.     static int pos = 0;
  122.     static int size = 0;
  123.     if (size == pos) {
  124.         size = fread(buf, 1, 1<<20, stdin);
  125.         pos = 0;
  126.     }
  127.     if (size == pos) {
  128.         return EOF;
  129.     }
  130.     return buf[pos++];
  131. }
  132.  
  133. void putChar(char c) {
  134.     static char buf[1<<20];
  135.     static int size = 0;
  136.     if (size == 1<<20 || c == -1) {
  137.         fwrite(buf, 1, size, stdout);
  138.         size = 0;
  139.     }
  140.     if (c != -1) {
  141.         buf[size++] = c;
  142.     }
  143. }
  144.  
  145. int getInt() {
  146.     char c = '?';
  147.     while (!(c == '-' || ('0' <= c && c <= '9'))) c = getChar();
  148.     bool positive = true;
  149.     if (c == '-') {
  150.         positive = false;
  151.         c = getChar();
  152.     }
  153.     int answ = 0;
  154.     while ('0' <= c && c <= '9') {
  155.         (answ *= 10) += (c - '0');
  156.         c = getChar();
  157.     }
  158.     return positive ? answ : -answ;
  159. }
  160.  
  161. void putInt(int number) {
  162.     if (number < 0) {
  163.         putChar('-');
  164.         number = -number;
  165.     }
  166.    
  167.     char buf[14]; int pos = 0;
  168.     do {
  169.         buf[pos++] = number % 10 + '0';
  170.         number /= 10;
  171.     } while (number > 0);
  172.    
  173.     while (pos--) {
  174.         putChar(buf[pos]);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement