Prime Matrix Codeforces Solution

Prime Matrix Codeforces Solution

In this post, the Prime Matrix Codeforces Solution is given. Question details : Codeforces Round #166 (Div. 2), problem: (B) Prime Matrix.

Prime Matrix Codeforces Solution

#include<bits/stdc++.h>
#define ll long long
#define fastio ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0);
const int MOD = 1000000007;
#define PI = acos(-1)
using namespace std ;
int a[106];
bool isprime(ll x )
{
if(x<=1)
{
return 0;
}
for(int i = 2 ; i*i <= x ; i++)
{
if(x%i==0)
{
return 0 ;
}
}
return 1 ;
}
int main()
{
fastio ;
vector<ll> v ;
for(int i = 1 ; i <= 100050 ; i++)
{
if(isprime(i))
{
v.push_back(i);
}
}
int n , m ;
cin >> n >> m ;
int a[n][m];
for(int i =0 ; i < n ; i++)
{
for(int j =0 ; j < m ; j++)
{
cin >> a[i][j];
}
}
ll ans = 1e7;
for(int i =0 ; i < n ; i++)
{
ll count1 =0;
for(int j = 0 ; j < m ; j++)
{
int idx = lower_bound(v.begin() , v.end() , a[i][j])-v.begin();
count1+= (v[idx]-a[i][j]);
}
ans= min(ans, count1);
}
for(int j =0 ; j < m ; j++)
{
ll count2=0;
for(int i =0 ; i < n ; i++)
{
int idx = lower_bound(v.begin() , v.end() , a[i][j])-v.begin();
count2 += (v[idx]-a[i][j]);
}
ans= min(ans , count2);
}
cout << ans << endl;
}
>

Prime Matrix Codeforces Solution by Vishal Vaibhab (IIT BHU). If you like TheCode11, then do follow us on Facebook, Twitter and Instagram.

Previous Post Next Post

Contact Form