#include #include #include #include using namespace std; long long gcd(long long a, long long b) {return (b == 0) ? a : gcd(b, a % b);} class sequence { private: vector v; //holds sequence vector> factors; //set s[i] holds factors of number v[i] vector survey; long long n, k, m; long long constraintCounter; //http://stackoverflow.com/questions/20740429/project-euler-challenge-3-finding-the-largest-prime-factor-of-a-large-number long long largest_factor(long long number) { long long result = 0; for (long long i = 2; i * i <= number; ++i) { if (number % i == 0) { result = i; while (number % i == 0) number /= i; } } if (number != 1) return number; return result; } //factor each number v[i] into set of factors and put them into s[i] map getFactors(long long num) { int origNum = num; map mm; while (num>1) { long long largest=largest_factor(num); mm[largest]++; num /= largest; } return mm; } public: sequence(long long n, long long k, long long m) { this->n = n; this->k = k; this->m = m; constraintCounter = 0; v.resize(n); factors.resize(n); survey.resize(n - k + 1); //holds intermediate constraints } ~sequence() { v.clear(); factors.clear(); } void print(map mm) { for (map::iterator it = mm.begin(); it != mm.end(); it++) cout << it->first << " " << it->second << endl; cout << endl; } //returns true if constralong long fails for K consecutive integers starting at position passed to the function bool relativelyPrime(long long start) { //assemble all factors into one map map m; for (long long i = start; i < start + k; i++) { //factors[i] = getFactors(v[i]); //update factors for (map::iterator it = factors[i].begin(); it != factors[i].end(); it++) { if (m[it->first]>0) return false;//not relatively prime else m[it->first] += it->second;//not needed, just curious what it summed to for now } } //cout << "Iteration " << start << ": "<n - k) end = n - k; //do the update!! cin >> v[index]; factors[index] = getFactors(v[index]); //update factors //now we can call relativelyPrime(start) to get our counts for (long long i = start; i <= end; i++) { bool isRelativelyPrime = relativelyPrime(i); constraintCounter += (int)survey[i] - (int)isRelativelyPrime ; //records the change of constralong long of k-group at start position i survey[i] = isRelativelyPrime; } return constraintCounter; } //read the initial sequence numbers void readSequence() { for (long long i = 0; i < n; i++) cin >> v[i]; } //sum long long sum() { long long sum = 0; for (long long i = 0; i < n; i++) sum+=v[i]; return sum; } }; //keep track of K interval being "pairwise prime" or "not pairwise prime" int main() { long long n, k, m; while (cin >> n >> k >> m && n > 0) { sequence s(n, k, m); //Read initial sequence s.readSequence(); //initial computation of all the pairwise non-prime intervals of length K long long constraintCounter = s.count(); cout << constraintCounter << endl; //here we are going to read each update and "do stuff" with it, namely update count, in place for (long long i = 0; i < m; i++) { //read 1-based position long long pos; cin >> pos; long long index = pos - 1; constraintCounter = s.updateCount(index); cout << constraintCounter << endl; } cout << s.sum() << endl; } return 0; } /* 6 3 4 7 2 3 4 5 6 4 3 5 9 4 10 6 11 0 0 0 //verify long long ver = 1; for (map::iterator it = mm.begin(); it != mm.end(); it++) { ver *= pow(it->first, it->second); } if (ver != origNum) { cout << ver << " is not " << origNum << " NO GOOD AT ALL" << endl; for (map::iterator it = mm.begin(); it != mm.end(); it++) { cout << it->first << " " << it->second << endl; } exit(0); } */