Advertisement
matheus_monteiro

The perfect base

Dec 26th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define _ << " , " <<
  6. #define ii pair<int, int>
  7. #define fi first
  8. #define se second
  9. #define SZ(a) (int)a.size()
  10. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  11.  
  12. const int ms = 1011000;
  13.  
  14. int pt;
  15. string s;
  16. int F[ms];
  17. bitset<ms> composite;
  18.  
  19. char read() {
  20.     if(pt >= SZ(s)) return '.';
  21.     return s[pt];
  22. }
  23.  
  24. int read_next_integer() {
  25.     int x = 0;
  26.     while(read() >= '0' and read() <= '9') {
  27.         int d = read() - '0';
  28.         x = 10 * x + d;
  29.         pt++;
  30.     }
  31.     return x;
  32. }
  33.  
  34. void sieve() {
  35.     for(int i = 2; i < ms; ++i)
  36.         if(!composite[i])
  37.             for(int j = i; j < ms; j += i)
  38.                 if(F[j] == 0)
  39.                     F[j] = i;
  40. }
  41.  
  42. map<int, int> factorization(int x) {
  43.     map<int, int> f;
  44.     while(x > 1) {
  45.         f[F[x]]++;
  46.         x /= F[x];
  47.     }
  48.     return f;
  49. }
  50.  
  51. void solve(){
  52.     sieve();
  53.  
  54.     cin >> s;
  55.    
  56.     map<int, int> prime_frequency;
  57.  
  58.     auto compute = [&](int x, int v) {
  59.         auto f = factorization(x);
  60.         for(auto it : f)
  61.             prime_frequency[it.fi] += v * it.se;
  62.     };
  63.  
  64.     map<int, int> factorials;
  65.     int cnt_factorials = 0;
  66.  
  67.     while(pt < SZ(s)) {
  68.         int x = read_next_integer();
  69.         if(read() == '!') {
  70.             pt++;
  71.             int e = 1;
  72.             if(read() == '^') {
  73.                 pt++;
  74.                 int a = read_next_integer();
  75.                 if(read() == '!') {
  76.                     if(a > 1) e = 0;
  77.                     pt++;
  78.                 }
  79.                 e = (e * a) % 2;
  80.             }
  81.             while(read() == '^') {
  82.                 pt++;
  83.                 int a = read_next_integer();
  84.                 if(read() == '!') pt++;
  85.             }
  86.             if(e & 1) factorials[x]++, cnt_factorials++;
  87.             pt++;
  88.         } else if(read() == '^') {
  89.             pt++;
  90.             int a = read_next_integer(), e = 1;
  91.             if(read() == '!') {
  92.                 if(a > 1) e = 0;
  93.                 pt++;
  94.             }
  95.             e = (e * a) % 2;
  96.             while(read() == '^') {
  97.                 pt++;
  98.                 int a = read_next_integer();
  99.                 if(read() == '!') pt++;
  100.             }
  101.             if(e & 1) compute(x, 1);
  102.         } else {
  103.             compute(x, 1);
  104.             pt++;
  105.         }
  106.     }
  107.        
  108.     int cnt = SZ(factorials);
  109.  
  110.     int number = 1;
  111.     for(auto it : factorials) {
  112.         int cur = it.fi;
  113.         while(number <= cur) compute(number, cnt_factorials), number++;
  114.         cnt_factorials -= it.se;
  115.     }
  116.  
  117.     bool flag = true;
  118.     for(auto it : prime_frequency)
  119.         if(it.se & 1)
  120.             flag = false;
  121.    
  122.     if(flag) cout << "S\n";
  123.     else cout << "N\n";
  124. }
  125.  
  126. int32_t main() {
  127.     fastio;
  128.     solve();
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement