Блог пользователя AmirHoseinEsmailie

Автор AmirHoseinEsmailie, история, 18 месяцев назад, По-английски

Sometimes we need to find the divisors of a number. The first way that might come to mind is: make a reverse loop (FOR loop) and divide the number by (i) to calculate the divisors. This approach takes O(N) time.


vector<int> get_divisors_reverse(int x) { vector<int> divs; for (int i = x; i >= 1; --i) { if (x % i == 0) { divs.push_back(i); } } return divs; }

But there are faster ways to calculate divisors.

This is a function that we can use to calculate the divisors of a number. Its time complexity is O(sqrt(N)).


vector<int> get_divisors(int x) { vector<int> divs; for (int i = 1; i * i <= x; ++i) { if (x % i == 0) { divs.push_back(i); if (i * i != x) { divs.push_back(x / i); } } } sort(divs.begin(), divs.end()); // Sorting is optional, depending on the requirement. return divs; }

if you know fastest way share it or comment it .

Полный текст и комментарии »

  • Проголосовать: нравится
  • -30
  • Проголосовать: не нравится

Автор AmirHoseinEsmailie, история, 18 месяцев назад, По-английски

LIS is about subsequences, not subarrays.

If you want to solve the problem using subarrays, you can refer to this link: https://codeforces.me/problemset/problem/702/A

The LIS algorithm does not work for finding the longest increasing subarray.

The provided code implements the LIS algorithm.

This problem (https://codeforces.me/problemset/problem/264/B) is a good example of the LIS problem.


#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3,unroll-loops") using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pii = pair<int, int>; using pll = pair<ll, ll>; #define rep(i, start, end) for (int i = start; i < end; i++) #define all(x) x.begin(), x.end() //------------------------------vector----------------------------------- template <typename T> inline void INTPUT_V(vector<T> &v) { rep(i, 0, v.size()) cin >> v[i]; } // -------------------------------------------------------------------------- #define MOD 1000000007 //#include "MyDebuger.cpp" void solve() { int n; cin >> n; vi numbers(n, 0); INTPUT_V(numbers); vector<int> dp(n, 0); rep(i, 0, n) { rep(j, 0, i) { if (numbers[j] < numbers[i] && dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; } } } //debug(dp); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // cout.tie(0); int t = 1; // cin >> t; rep(i, 0, t) { solve(); } return 0; }

Полный текст и комментарии »

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится