/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Time Exceeded ≥2598ms ≥7.992 MiB
#2 Time Exceeded ≥2600ms ≥8.02 MiB

Code

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;

int const N = 1e6 + 2;
vector<ll> a(N);

class SegmentTree {
  public:
  ll n;
  vector<ll> tree;
  SegmentTree(ll n) : tree(2 * n + 10), n(n){};

  void build() {
    for (int i = 1; i <= n; i++) 
      tree[n + i] = a[i];
    for (int i = n; i > 0; i--) 
      tree[i] = max(tree[i << 1], tree[i << 1 | 1]);
  }

  ll query(int l, int r) {
    ll res = 0;
    for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) res = max(res, tree[l++]);
      if (r & 1) res = max(res, tree[--r]);
    }
    return res;
  }
};


ll gcd(ll a, ll b) {
    while (b != 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}


ll pollards_rho(long long n) {
    if (n % 2 == 0) return 2; 

    long long x = 2, y = 2, d = 1;
    long long c = rand() % (n - 1) + 1;  

    while (d == 1) {
        x = (x * x + c) % n;
        y = (y * y + c) % n;
        y = (y * y + c) % n;
        
        d = gcd(abs(x - y), n);
    }

    return d; 
}


void factorize(ll n, map<ll, ll>& factors) {
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            factors[i]++;
            n /= i;
        }
    }

    while (n > 1) {
        if (n % 2 == 0) {
            factors[2]++;
            n /= 2;
            continue;
        }

        ll divisor = pollards_rho(n);
        while (n % divisor == 0) {
            factors[divisor]++;
            n /= divisor;
        }
    }
}

ll total_divisors(const map<ll, ll>& factors) {
    ll divisors = 1;
    for (const auto& factor : factors) {
        divisors *= (factor.second + 1);
    }
    return divisors;
}


int main() {
  ll S = 0;
  for (int i = 1; i < N - 1; i++){
    S += i;
    // ll c = 0;
    // for (int j = 1; j * j <= S; j++){
    //   if (S % j == 0){
    //     c++;
    //     if (S / j != j) c++;
    //   }
    // }
    map<ll, ll> factors;
    factorize(S, factors);
    long long c = total_divisors(factors);
    a[i] = c;
  }
  SegmentTree mx(N - 1);
  mx.build();
  int t;
  cin >> t;
  while (t--){
    ll n;
    cin >> n;
    cout << mx.query(1, n) << endl;
  }
  //unsigned long long y=(N*(N+1))/2;cout<<y;
}

Information

Submit By
Type
Submission
Problem
P1180 Maximum Divisor
Contest
Brain Booster #9
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 17:18:20
Judged At
2025-04-06 17:18:20
Judged By
Score
0
Total Time
≥2600ms
Peak Memory
≥8.02 MiB