//============================================================================ // Name : potyczki2016.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <math.h> #include <vector> #include <map> #include <algorithm> // std::reverse #include <numeric> // std::accumulate using namespace std; void primeFactors(long long n, vector<int> & prime_factorization) { long long sum_elements = 0; while (n%2 == 0) { prime_factorization.push_back(2); sum_elements += 2; if (sum_elements > 100) { return; } n = n/2; } for (int i = 3; i <= sqrt(n); i = i+2) { while (n%i == 0) { prime_factorization.push_back(i); sum_elements += i; if(i > 100 or sum_elements > 100) { return; } n = n/i; } } if (n > 2) prime_factorization.push_back(n); } int main() { int t; long long k; cin >> t; for(int testcase=0; testcase<t; testcase++) { cin >> k; vector<int> prime_factorization; primeFactors(k, prime_factorization); if (std::accumulate(prime_factorization.begin(), prime_factorization.end(), 0) > 100) { cout << "NIE" << endl; } else { for(int i=0; i<prime_factorization.size(); i++) { if(i>0) cout << "*"; cout << "("; for(int j=0; j<prime_factorization[i]; j++) { if(j>0) cout << "+"; cout << "1"; } cout << ")"; } cout << endl; } } return 0; }
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | //============================================================================ // Name : potyczki2016.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <math.h> #include <vector> #include <map> #include <algorithm> // std::reverse #include <numeric> // std::accumulate using namespace std; void primeFactors(long long n, vector<int> & prime_factorization) { long long sum_elements = 0; while (n%2 == 0) { prime_factorization.push_back(2); sum_elements += 2; if (sum_elements > 100) { return; } n = n/2; } for (int i = 3; i <= sqrt(n); i = i+2) { while (n%i == 0) { prime_factorization.push_back(i); sum_elements += i; if(i > 100 or sum_elements > 100) { return; } n = n/i; } } if (n > 2) prime_factorization.push_back(n); } int main() { int t; long long k; cin >> t; for(int testcase=0; testcase<t; testcase++) { cin >> k; vector<int> prime_factorization; primeFactors(k, prime_factorization); if (std::accumulate(prime_factorization.begin(), prime_factorization.end(), 0) > 100) { cout << "NIE" << endl; } else { for(int i=0; i<prime_factorization.size(); i++) { if(i>0) cout << "*"; cout << "("; for(int j=0; j<prime_factorization[i]; j++) { if(j>0) cout << "+"; cout << "1"; } cout << ")"; } cout << endl; } } return 0; } |