COCI Solution Explanation
Nov 8, 2012 at 4:59pm UTC
I have been doing the competitions at the COCI, and reading the solutions for them. I have come across one section of code in a solution that I do not understand. Thanks in advance :)
Link to the task:
http://www.hsin.hr/coci/contest1_tasks.pdf
(Problem: SNAGA)
Translated solution Code:
(I have a problem understanding lines 37-42)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long long A, B, solution = 0;
cin >> A >> B;
// LCM = lcm(1, 2, ..., K-1)
long long LCM = 1;
int power[100];
power[2] = 1;
for (int K = 2; K < 100; K++)
{
for (int i = 2; i < K; i++) {
if (K % i != 0) {
power[K] = power[i] + 1;
break ;
}
}
long long new_LCM = LCM;
vector<int > prime_divisors_K;
for (int p = 2, k = K; k > 1; p++) {
if (k % p == 0) {
prime_divisors_K.push_back(p);
while (k % p == 0) {
k /= p;
}
}
}
if ((int )prime_divisors_K.size() == 1) {
new_LCM *= prime_divisors_K[0];
}
solution += (power[K] + 1) * (B/LCM - (A-1)/LCM);
if (new_LCM > B || new_LCM < 0) break ;
solution -= (power[K] + 1) * (B/new_LCM - (A-1)/new_LCM);
LCM = new_LCM;
}
cout << solution << endl;
}
Topic archived. No new replies allowed.