-
Notifications
You must be signed in to change notification settings - Fork 0
/
271B_PrimeMatrix.cpp
78 lines (73 loc) · 1.69 KB
/
271B_PrimeMatrix.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
/*
* Created by ishaanjav
* github.com/ishaanjav
* Codeforces Solutions: https://github.com/ishaanjav/Codeforces-Solutions
*/
#include <iostream>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#include <string>
#include <vector>
typedef vector<int> vi;
//#include <algorithm>
//#include <set>
//#include <map>
//#include <unordered_set>
//#include <unordered_map>
//#include <cmath>
#include <cstring>
//#include <sstream>
//#include <stack>
//#include <queue>
vector<int> primes;
void sieve() {
bool prime[100009];
memset(prime, true, sizeof(prime));
prime[0] = false;
prime[1] = false;
for (int p = 2; p < 300; p++) {
if (!prime[p]) continue;
primes.pb(p);
//cout << p << endl;
for (int j = p * p; j < 100000; j += p) prime[j] = false;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int r, c;
cin >> r >> c;
sieve();
int ar[r][c];
int lastPos = 0;
int val[r][c];
ll ans = 1e12;
for (int i = 0; i < r; i++) {
ll temp = 0;
cout << "A";
for (int j = 0; i < c; j++) {
cin >> ar[i][j];
int num;
for (int a = 0; a < primes.size(); a++) {
cout << a << " ";
if (primes[a] >= ar[i][j]) {
num = primes[a];
break;
}
}
temp += num;
val[i][j] = num;
}
ans = min(temp, ans);
}
for (int j = 0; j < c; j++) {
ll temp = 0;
for (int i = 0; i < r; i++) {
temp += val[i][j];
}
ans = min(ans, temp);
}
cout << ans << "\n";
}