-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10139 -Factovisors.cpp
80 lines (71 loc) · 1.41 KB
/
10139 -Factovisors.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
#include <cstdio>
#include <vector>
#define maxN 46341
#define int64 long long int
using namespace std;
bool is[maxN];
vector<int>primes;
vector< pair<int, int> > factors;
void sieve()
{
is[0] = is[1] = true;
for(int i = 4; i<maxN; i+=2)
is[i] = true;
for(int i = 3; i*i<maxN; i+=2)
if(!is[i])
for(int j = i*i; j<maxN; j+=2*i)
is[j] = true;
primes.push_back(2);
for(int i = 3; i<maxN; i+=2)
if(!is[i])
primes.push_back(i);
}
int get_power(int N, int P)
{
int res = 0;
for(int64 power = P; power <= N; power*=P)
res += N/power;
return res;
}
void factorize(int N)
{
factors.clear();
int num = N;
for(int i = 0; i<primes.size() && primes[i] <= num; i++)
{
if(num % primes[i] == 0)
{
int c = 0;
while(num%primes[i] == 0)
{
num /= primes[i];
c++;
}
factors.push_back(make_pair(primes[i], c));
}
}
if(num > 1) factors.push_back(make_pair(num, 1));
}
bool is_divisible(int N, int dividend)
{
if(dividend == 0) return false;
if(N >= dividend) return true;
factorize(dividend);
for(int i = 0; i<factors.size(); i++)
{
if(factors[i].first > N) return false;
if(get_power(N, factors[i].first) < factors[i].second) return false;
}
return true;
}
int main()
{
sieve();
int N, dividend;
while(scanf("%d %d", &N, ÷nd) == 2)
{
if(is_divisible(N, dividend)) printf("%d divides %d!\n", dividend, N);
else printf("%d does not divide %d!\n", dividend, N);
}
return 0;
}