CSES - Putka Open 2020 – 4/5 - Results
Submission details
Task:Tekijät
Sender:Yytsi
Submission time:2020-11-08 14:11:25 +0200
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.02 s1, 2, 3details
#20.03 s1, 2, 3details
#30.04 s2, 3details
#40.05 s3details
#50.07 s3details
#6ACCEPTED0.25 s3details
#7ACCEPTED0.04 s3details

Compiler report

input/code.cpp:1:0: warning: ignoring #pragma optimize  [-Wunknown-pragmas]
 #pragma optimize ("-O3")

Code

#pragma optimize ("-O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int n;
int smallestFactor[1010101];
vector<int> primes;

vector<int> primeFac(int x) {
  if (x == 1) return {1};
  vector<int> facs;
  while (x > 1) {
    int small = smallestFactor[x];
    if (facs.empty() || facs.back() != small) facs.push_back(small);
    x /= small;
  }
  return facs;
}

int cnt[1010101]; // cnt of divisors seen so far
int occ[20];

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin>>n;

  for (int d = 2; d <= 1000000; d++) {
    if (smallestFactor[d] > 0) continue;
    for (int x = d; x <= 1000000; x += d) {
      if (smallestFactor[x] == 0) smallestFactor[x] = d;
      else smallestFactor[x] = min(smallestFactor[x], d);
    }
  }

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    int x; cin>>x;
    if (x == 1) {
      ans += i;
      continue;
    }
    vector<int> facs = primeFac(x);
    int m = facs.size();
    for (int j = 0; j < m; j++) occ[j] = 0;
    int res = 0;

    for (int S = (1 << m) - 1; S >= 1; S--) {
      int div = 1;
      int rem = 1e9;

      for (int j = 0; j < m; j++) {
        if (S & (1 << j)) {
          div *= facs[j];
          rem = min(rem, occ[j]);
        }
      }

      int beforeCnt = cnt[div];
      beforeCnt -= rem;
      //cout<<"pos "<<i<<" with div of "<<div<<" has beforecnt = "<<beforeCnt<<"\n";
      //cout<<"rem is "<<rem<<"\n";
      for (int j = 0; j < m; j++) {
        if (S & (1 << j)) occ[j] += beforeCnt;
      }

      res += beforeCnt;
      cnt[div]++;
    }

    /*
    cout<<"at pos "<<i<<" cnt to left is "<<res<<"\n";
    cout<<"occ: ";
    for (int j = 0; j < m; j++) {
      cout<<occ[j]<<" ";
    }
    cout<<"\n";
    */
    ans += ll(i - res);
  }

  cout<<ans<<"\n";
}

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
3043

user output
3031

Test 2

Group: 1, 2, 3

Verdict:

input
100
71 19 26 18 57 78 80 89 31 26 ...

correct output
3086

user output
3085

Test 3

Group: 2, 3

Verdict:

input
100000
66 87 90 67 93 89 57 29 34 4 8...

correct output
3044751906

user output
3036365941

Test 4

Group: 3

Verdict:

input
100000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
3039650753

user output
3015454928

Test 5

Group: 3

Verdict:

input
100000
238907 151373 522599 885657 37...

correct output
3031155756

user output
3006992465

Test 6

Group: 3

Verdict: ACCEPTED

input
100000
510510 510510 510510 510510 51...

correct output
0

user output
0

Test 7

Group: 3

Verdict: ACCEPTED

input
100000
999983 999983 999983 999983 99...

correct output
0

user output
0