Skip to content

Commit 7ff269e

Browse files
Create Maximum Factors Problem.cpp
1 parent 10e2785 commit 7ff269e

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <map>
4+
using namespace std;
5+
6+
const int MAX_N = 1e5 + 5;
7+
vector <int> primes;
8+
void sieve()
9+
{
10+
vector <int> is_prime(MAX_N, true);
11+
is_prime[0] = is_prime[1] = false;
12+
for(int i = 2; i < MAX_N; i++)
13+
{
14+
if(is_prime[i])
15+
{
16+
primes.push_back(i);
17+
}
18+
19+
for(int j = 0; j < primes.size() && i*primes[j] < MAX_N; j++)
20+
{
21+
is_prime[i*primes[j]] = false;
22+
23+
if(i%primes[j] == 0)
24+
{
25+
break;
26+
}
27+
}
28+
}
29+
}
30+
31+
void factorize(int n, map <int, int> &exponent)
32+
{
33+
for(int i = 0; i < primes.size() && primes[i]*primes[i] <= n; i++)
34+
{
35+
while(n%primes[i] == 0)
36+
{
37+
exponent[primes[i]]++;
38+
n /= primes[i];
39+
}
40+
}
41+
42+
if(n > 1)
43+
{
44+
exponent[n] = 1;
45+
}
46+
}
47+
48+
void solve()
49+
{
50+
int n;
51+
cin >> n;
52+
map <int, int> exponent;
53+
factorize(n, exponent);
54+
55+
int prime_with_largest_exponent = 0;
56+
for(auto it = exponent.begin(); it != exponent.end(); it++)
57+
{
58+
if(it->second > exponent[prime_with_largest_exponent])
59+
{
60+
prime_with_largest_exponent = it->first;
61+
}
62+
}
63+
64+
cout << prime_with_largest_exponent << "\n";
65+
}
66+
67+
int main()
68+
{
69+
sieve();
70+
71+
int no_of_test_cases;
72+
cin >> no_of_test_cases;
73+
74+
while(no_of_test_cases--)
75+
solve();
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)