UVA 406 - Prime Cuts Solution
Solution in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include<set>
#include<queue>
#define efor(i,a,b) for(int i=a; i<=b; i++)
#define zfor(i,n) for (int i=0; i<n; i++)
#define mall(ar, val) memset(ar, val, sizeof(ar))
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define M 1009
using namespace std;
bool sieve(int n)
{
for(int i=2; i*i<=n; i++)
{
if(n%i==0)
return false;
}
return true;
}
int main()
{
int n,c,i,r;
vector<int>prime;
efor(i,1,1000)
{
if(sieve(i)==true)
prime.pb(i);
}
while(cin>>n>>c)
{
cout<<n<<" "<<c<<":";
int cc=0,ca;
for(i=0; i<int(prime.size()); i++)
{
if(prime[i]<=n)
{
cc++;
}
else
break;
}
ca=cc;
if(ca%2==0)
{
if(2*c>ca)
{
for(i=0; i<ca; i++)
{
cout<<" "<<prime[i];
}
cout<<endl;
}
else
{
r = (ca-(2*c))/2;
for(int i=r; i<ca-r; i++)
{
cout<<" "<<prime[i];
}
cout<<endl;
}
}
else
{
if(((2*c)-1)>ca)
{
for(i=0; i<ca; i++)
{
cout<<" "<<prime[i];
}
cout<<endl;
}
else
{
r = (ca-((2*c)-1))/2;
for(int i=r; i<ca-r; i++)
{
cout<<" "<<prime[i];
}
cout<<endl;
}
}
cout<<endl;
}
}
No comments