271B - Prime Matrix - CodeForces Solution


binary search brute force math number theory *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define el '\n'
#define dd long double
#define ff float
#define so sort(a.begin() , a.end())
#define so1 sort(a.rbegin() , a.rend())
#define pb push_back
#define be v.begin()
#define be1 v.rbegin()
#define en v.end()
#define en1 v.rend()
#define fi first
#define sc second
#define re reserve
#define re1 v.resize
#define re2 a.resize
#define in insert
#define lp(i,k,n) for(ll i=0;i<n;i++)
using namespace std;
//continue
// @author: saleh_zizo
//std::reverse(b.begin(), b.end());
const ll N = 1e6;
void fast() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<ll>primes;
vector<bool> sieve(ll n){
 	vector<bool>isPrime(n+1,1);
 	isPrime[0]=isPrime[1]=0;
 	for(ll i=2; i<=n; i++){
   		if(isPrime[i]){
            primes.pb(i);
            for(ll j=2*i; j<=n; j+=i){
                isPrime[j]=0;
            }
   		}
 	}
 	return isPrime;
}
pair<ll, ll>pi(ll h , ll m , ll cnt) {
	while (cnt--) {
		m++;
		if (m == 60) {h++; m = 0;}
		if (h == 24) {h = 0;}
	}
	return {h, m};
}
ll checked_true(ll n){
    ll c = (n*(n+1))/2;
    return c;
}
vector<ll>primef(ll n){
    vector<ll>v;
    for(ll i=2;i*i<=n;i++){
       // vector<ll>temp;
        while(n%i==0){
            v.pb(i);
            n/=i;
        }
    }
    if(n>1){
        v.pb(n);
    }
    return v;
}
int main() { // 0 1 2 [0 , 1 , 2 , '\0'];
//freopen("window.in","r",stdin);
//cout<<fixed<<setprecision(3);// 1 2 3 4 5
	fast();
	sieve(N);
	auto v = primes;
	ll n , m; cin>>n>>m;
	ll arr[n][m];
	ll cnt = 2e9;
	lp(i,0,n){
	    ll temp = 0;
	    lp(j,0,m){
	        cin>>arr[i][j];
	        ll c = arr[i][j];
	        ll up= lower_bound (v.begin(), v.end(), c)-v.begin();
	        temp+=v[up]-c;
	    }
	    cnt = min(cnt,temp);
	}
	lp(i,0,m){
	    ll temp = 0;
	    lp(j,0,n){
	        ll c = arr[j][i];
	        ll up= lower_bound (v.begin(), v.end(), c)-v.begin();
	        temp+=v[up]-c;
	    }
	    cnt = min(cnt,temp);
	}
	cout<<cnt<<el;
}


Comments

Submit
0 Comments
More Questions

714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words