Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using LL = long long;
  4. #define V vector
  5. #define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
  6. #define REP(i, n) FOR(i, 0, (n) - 1)
  7. template<class T> int size(T &&x) {
  8.     return int(x.size());
  9. }
  10. template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
  11.     return out << '(' << p.first << ", " << p.second << ')';
  12. }
  13. template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
  14.     out << '{';
  15.     for(auto it = x.begin(); it != x.end(); ++it)
  16.         out << *it << (it == prev(x.end()) ? "" : ", ");
  17.     return out << '}';
  18. }
  19. void dump() {}
  20. template<class T, class... Args> void dump(T &&x, Args... args) {
  21.     cerr << x << ";  ";
  22.     dump(args...);
  23. }
  24. #ifdef DEBUG
  25.   struct Nl{~Nl(){cerr << '\n';}};
  26. # define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
  27. #else
  28. # define debug(x...) 0 && cerr
  29. #endif
  30. mt19937_64 rng(0);
  31. int rd(int l, int r) {
  32.     return uniform_int_distribution<int>(l, r)(rng);
  33. }
  34.  
  35. constexpr LL Mod = 1e9+7;
  36.  
  37. LL mul(LL a, LL b, LL mod) {
  38.     return (a * b - LL((long double) a * b / mod) * mod + mod) % mod;
  39. }
  40.  
  41. LL fpow(LL a, LL n, LL mod) {
  42.     if(n == 0)
  43.         return 1;
  44.     if(n % 2 == 1)
  45.         return mul(fpow(a, n - 1, mod), a, mod);
  46.     LL ret = fpow(a, n / 2, mod);
  47.     return mul(ret, ret, mod);
  48. }
  49.  
  50. bool miller_rabin(LL n) {
  51.     if(n < 2)
  52.         return false;
  53.     int r = 0;
  54.     LL d = n - 1;
  55.     while(d % 2 == 0)
  56.         d /= 2, r++;
  57.  
  58.     for(int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}) {
  59.         if(n == a)
  60.             return true;
  61.         LL x = fpow(a, d, n);
  62.         if(x == 1 or x == n - 1)
  63.             continue;
  64.  
  65.         bool composite = true;
  66.         REP(i, r - 1) {
  67.             x = mul(x, x, n);
  68.             if(x == n - 1) {
  69.                 composite = false;
  70.                 break;
  71.             }
  72.         }
  73.         if(composite)
  74.             return false;
  75.     }
  76.     return true;
  77. }
  78.  
  79. LL dzielnikow(LL n) {
  80.     LL cp_n = n;
  81.     LL wynik = 1;
  82.  
  83.     FOR(i, 2, n) {
  84.         if(i * LL(i) * LL(i) > n)
  85.             break;
  86.         int cnt = 0;
  87.         while(cp_n % i == 0) {
  88.             ++cnt;
  89.             cp_n /= i;
  90.         }
  91.  
  92.         wynik *= (cnt + 1);
  93.     }
  94.     n = cp_n;
  95.  
  96.     auto dokoncz = [&] {
  97.         if(n == 1)
  98.             return 1;
  99.         if(miller_rabin(n))
  100.             return 2;
  101.         int sq = int(sqrt(n));
  102.         FOR(i, sq - 2, sq + 2)
  103.             if(i * LL(i) == n)
  104.                 return 3;
  105.         return 4;
  106.     };
  107.  
  108.     return wynik * dokoncz();
  109. }
  110.  
  111. LL roz(LL x, LL y) {
  112.     return ((x - y >= 0) ? (x - y) : (x - y + Mod));
  113. }
  114.  
  115. int main() {
  116.     ios_base::sync_with_stdio(0);
  117.     cin.tie(0);
  118.  
  119.     int n; cin >> n;
  120.     V<LL> v(n);
  121.     REP(i, n)
  122.         cin >> v[i];
  123.  
  124.     V<int> div(n);
  125.     REP(i, n)
  126.         div[i] = dzielnikow(v[i]);
  127.  
  128.     LL start = 1;
  129.     REP(i, n) {
  130.         start *= div[i];
  131.         start %= Mod;
  132.     }
  133.  
  134.     for(int mask = 1; mask < (1 << n); mask++) {
  135.         int bits = __builtin_popcount(mask);
  136.         if(bits > 1) {
  137.             debug(mask);
  138.             // dodawanko
  139.             LL g = 0;   // gcd
  140.             REP(i, n)
  141.                 if(mask & (1 << i))
  142.                     g = __gcd(g, v[i]);
  143.             debug(g);
  144.             LL how_many = dzielnikow(g);
  145.             REP(i, n)
  146.                 if(~mask & (1 << i))
  147.                     how_many = (how_many * div[i]) % Mod;
  148.             debug(how_many);
  149.             how_many = (how_many * (bits - 1)) % Mod;
  150.             start = ((bits & 1) ? (start + how_many) % Mod : roz(start, how_many));
  151.         }
  152.     }
  153.  
  154.     cout << start << '\n';
  155.     return 0;
  156. }
  157.  
  158.  
  159. 4
  160. 2 1 7 2
  161.  
  162.  
  163. mask:  3;  
  164. g:  1;  
  165. how_many:  4;  
  166. mask:  5;  
  167. g:  1;  
  168. how_many:  2;  
  169. mask:  6;  
  170. g:  1;  
  171. how_many:  4;  
  172. mask:  7;  
  173. g:  1;  
  174. how_many:  2;  
  175. mask:  9;  
  176. g:  2;  
  177. how_many:  4;  
  178. mask:  10;  
  179. g:  1;  
  180. how_many:  4;  
  181. mask:  11;  
  182. g:  1;  
  183. how_many:  2;  
  184. mask:  12;  
  185. g:  1;  
  186. how_many:  2;  
  187. mask:  13;  
  188. g:  1;  
  189. how_many:  1;  
  190. mask:  14;  
  191. g:  1;  
  192. how_many:  2;  
  193. mask:  15;  
  194. g:  1;  
  195. how_many:  1;  
  196. 1000000006
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement