-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCM_UVA10680OJ.cpp
100 lines (86 loc) · 2.03 KB
/
LCM_UVA10680OJ.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
const int N = 1000000;
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
vector<bool> isPrime(N + 1, true);
isPrime[0] = false, isPrime[1] = false;
vector<long long> primes, out(N + 1, -1);
for (long long i = 2; i <= N; ++i)
{
if (isPrime[i] == true)
{
primes.push_back(i);
for (long long j = i * i; j <= N; j += i)
{
isPrime[j] = false;
}
}
}
int size0 = primes.size();
while (true)
{
long long n = 0;
cin >> n;
if (n == 0)
{
break;
}
else if (out[n] != -1)
{
cout << out[n] << '\n';
}
long long ans = 1;
for (int i = 0; i <= size0 - 1; ++i)
{
long long now = primes[i];
if (now > n)
{
break;
}
else if (now == 5)
{
long long temp = now;
while (temp <= n)
{
ans = ans * now;
temp *= now;
}
while (ans % 10 == 0)
{
ans /= 10;
}
ans = to_string(ans).back() - '0';
}
else
{
long long temp = now;
while (temp <= n)
{
ans *= now;
temp *= now;
if (now > 5)
{
ans %= 10;
}
}
}
}
out[n] = ans;
cout << ans << '\n';
}
cout.flush();
return 0;
}