Submission #644508

#TimeUsernameProblemLanguageResultExecution timeMemory
644508GusterGoose27Strongbox (POI11_sej)C++11
84 / 100
176 ms35048 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, int> pii; const ll MAXN = 1e14; const int MAXK = 25e4; const int MAXF = 2e4; ll n; int k; ll elems[MAXK]; ll gcd(ll a, ll b) { if (a > b) return gcd(b, a); if (a == 0) return b; return gcd(b%a, a); } vector<ll> primes; bitset<(int) sqrt(MAXN)+1> is_prime; vector<pii> kfactored; vector<int> factorizations[MAXK-1]; int state[MAXF]; // 0 unvisited, 2 descended from k, 1 blocked ll val[MAXF]; int hshs[MAXK]; vector<int> coeffs; int mxh = 0; ll ans = 0; void make_primes(ll mx) { is_prime.set(); for (int i = 2; i <= mx; i++) { if (!is_prime[i]) continue; primes.push_back(i); for (int j = 2*i; j <= mx; j+= i) is_prime[j] = 0; } } void factorize(ll target) { // prime for (int i = 0; i < primes.size() && target > 1; i++) { int c = 0; while (target % primes[i] == 0) { target /= primes[i]; c++; } if (c) { kfactored.push_back(pii(primes[i], c)); } } if (target != 1) { kfactored.push_back(pii(target, 1)); } } void factorize(ll target, vector<int> &factored) { // prime for (int i = 0; i < kfactored.size(); i++) { int c = 0; while (target % kfactored[i].first == 0) { target /= kfactored[i].first; c++; } factored.push_back(c); } } void make_coeffs() { int mult = 1; for (int i = 0; i < kfactored.size(); i++) { coeffs.push_back(mult); mult *= (kfactored[i].second+1); } } int hsh(vector<int> &factored) { int r = 0; for (int i = 0; i < factored.size(); i++) { r += factored[i]*coeffs[i]; } mxh = max(mxh, r); return r; } int hsh(vector<pii> &factored) { int r = 0; for (int i = 0; i < factored.size(); i++) { r += factored[i].second*coeffs[i]; } mxh = max(mxh, r); return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for (int i = 0; i < k; i++) { cin >> elems[i]; } elems[k-1] = gcd(elems[k-1], n); for (int i = 0; i < k-1; i++) elems[i] = gcd(elems[i], elems[k-1]); if (elems[k-1] == 0) elems[k-1] = n; make_primes((int) sqrt(n)); factorize(elems[k-1]); make_coeffs(); hshs[k-1] = hsh(kfactored); state[hshs[k-1]] = 2; val[hshs[k-1]] = elems[k-1]; for (int i = 0; i < k-1; i++) { factorize(elems[i], factorizations[i]); hshs[i] = hsh(factorizations[i]); state[hshs[i]] = 1; val[hshs[i]] = elems[i]; } assert(mxh < MAXF); for (int i = mxh; i >= 0; i--) { if (state[i] == 0) continue; if (state[i] == 2) ans = max(ans, n/val[i]); for (int p = 0; p < kfactored.size(); p++) { int v = i/coeffs[p]; if (v % (kfactored[p].second+1) == 0) continue; int nxt = i-coeffs[p]; if (state[nxt] == 1 || (state[nxt] == 2 && state[i] == 2)) continue; val[nxt] = val[i]/kfactored[p].first; state[nxt] = state[i]; } } assert(ans > 0); cout << ans << "\n"; }

Compilation message (stderr)

sej.cpp: In function 'void factorize(ll)':
sej.cpp:43:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |  for (int i = 0; i < primes.size() && target > 1; i++) {
      |                  ~~^~~~~~~~~~~~~~~
sej.cpp: In function 'void factorize(ll, std::vector<int>&)':
sej.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for (int i = 0; i < kfactored.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~
sej.cpp: In function 'void make_coeffs()':
sej.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i = 0; i < kfactored.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~
sej.cpp: In function 'int hsh(std::vector<int>&)':
sej.cpp:79:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |  for (int i = 0; i < factored.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~
sej.cpp: In function 'int hsh(std::vector<std::pair<long long int, int> >&)':
sej.cpp:88:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |  for (int i = 0; i < factored.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~
sej.cpp: In function 'int main()':
sej.cpp:120:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |   for (int p = 0; p < kfactored.size(); p++) {
      |                   ~~^~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...