Alfi asked Roy to go for shopping with her. Witty Roy came up with a condition. He said, for each product of MRP (Maximum Retail Price) R, she'll have to pay minimum of all the prime factors of R and he himself will pay rest of the amount. Without giving it a second thought Alfi agreed.
Example:
Input: r = 10
Output: 8
Approach
C++
#include <bits/stdc++.h>using namespace std;#define MAXN 1000001int spf[MAXN];void sieve(){spf[1] = 1;for (int i = 2; i < MAXN; i++)spf[i] = i;for (int i = 4; i < MAXN; i += 2)spf[i] = 2;for (int i = 3; i * i < MAXN; i++){if (spf[i] == i){for (int j = i * i; j < MAXN; j += i)if (spf[j] == j)spf[j] = i;}}}vector<int> getFactorization(int x){vector<int> ret;while (x != 1){ret.push_back(spf[x]);x = x / spf[x];}return ret;}int main(){sieve();int r = 10;vector<int> res = getFactorization(r);cout << r - res[0] << "\n";}
No comments:
Post a Comment