-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem-D.cpp
106 lines (89 loc) · 1.79 KB
/
Problem-D.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
std::vector <bool> prime(100006, true);
std::vector <int> primes;
int64_t fast_power(int64_t base, int64_t exponent, int64_t modulo = LLONG_MAX)
{
int64_t result = 1;
base %= modulo;
while (exponent > 0)
{
if (exponent & 1)
{
result = (result * base) % modulo;
}
exponent >>= 1;
base = (base * base) % modulo;
}
return result;
}
void sieve(int limit = 100005)
{
limit = std::max(limit, 1);
prime[0] = prime[1] = false;
for (int64_t i = 2; i <= limit; ++i)
{
if (prime[i])
{
primes.push_back(i);
for (int64_t j = i * i; j <= limit; j += i)
{
if (prime[j])
{
prime[j] = false;
}
}
}
}
}
std::vector <std::pair <int64_t, int>> factorize(int64_t number)
{
std::vector <std::pair <int64_t, int>> factors;
int64_t times = 0;
for (int64_t factor = 2; factor < 100005; ++factor)
{
if (prime[factor])
{
if (factor * factor > number) break;
if (number % factor == 0) factors.push_back({factor, 0});
while (number % factor == 0)
{
number /= factor;
++factors.back().second;
}
}
}
if (number > 1) factors.push_back({number, 1});
return factors;
}
int32_t main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
sieve();
int tests_number;
std::cin >> tests_number;
while (tests_number--)
{
int64_t n;
std::cin >> n;
std::vector <std::pair <int64_t, int>> prime_factors = factorize(n);
int64_t factor = -1, frequent = 0;
for (auto i : prime_factors)
{
if (i.second > frequent)
{
frequent = i.second;
factor = i.first;
}
}
std::cout << frequent << '\n';
for (int64_t i = 1; i < frequent; ++i)
{
std::cout << factor << ' ';
}
std::cout << n / fast_power(factor, frequent - 1) << '\n';
}
return 0;
}