Wrong answer on SPOJ (PRIME1) - c++

Here is the link to the problem -> PRIME1
It asks us to print all the prime numbers between two numbers m and n
I used segmented sieve to solve the problem. Stored all the primes till sqrt(10^9) and used them to get primes in a specific range. Please help me out.
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
vector<bool> prime(32022);
void precompute()
{
int n=32000;
prime[0]=prime[1]=true;
for(int i=2;i*i<=32000;i++)
{
for(int j=i*i;j<=32000;j+=i)
prime[j]=true;
}
for(int i=0;i<=32000;i++)
if(!prime[i]) v.push_back(i);
}
int main() {
precompute();
int t; scanf("%d",&t);
while(t--)
{
int m,n; scanf("%d%d",&m,&n);
if(n<=32000)
{
for(int i:v)
{
if(i>n) break;
if(i>=m && i<=n)
printf("%d\n",i);
}
}
else
{
vector<bool> prime(n-m+1,true);
for(int i:v)
{
int st=(m/i)*i;
if(st<m) st+=i;
while(st<=n)
{
prime[st-m]=false;
st+=i;
}
}
for(int i=0;i<n-m+1;i++)
{
if(prime[i]) printf("%d\n",i+m);
}
}
printf("\n");
}
}

Related

What does "-nan" mean and what causes it?

I know that nan means "not a number",but this code using the Gaussian elimination algorithm output "-nan" :
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=55;
const double eps=1e-7;
int n;
struct mat {
int l,c;
double p[N][N];
double* operator [] (int i) {return p[i];}
}a;
bool flo0(double &x) {return (fabs(x)<=eps)?1:0;}
void REF() {
for(int i=1;i<=n;i++) {
int flag=i;
for(int j=i;j<=n;j++) {
if(fabs(a[j][i])>fabs(a[flag][i])) flag=j;
}
for(int j=1;j<=n+1;j++) swap(a[flag][j],a[i][j]);
if(flo0(a[i][i])) continue;
for(int j=i+1;j<=n;j++) {
double t=a[j][i]/a[i][i];
for(int k=i;k<=n+1;k++) {
a[j][k]-=t*a[i][k];
}
}
}
}
void RREF() {
for(int i=n;i>=1;i--) {
if(flo0(a[i][i])) continue;
for(int j=n+1;j>=i;j--) a[i][j]/=a[i][i];
for(int j=i-1;j>=1;j--) {
double t=a[j][i];
for(int k=n+1;k>=1;k--) {
a[j][k]-=t*a[i][k];
}
}
}
bool no=false,many=false;
for(int i=1;i<=n;i++) {
int t=0;
for(int j=1;j<=n;j++) if(flo0(a[i][j])==false) ++t;
if(t==0 && flo0(a[i][n+1])==false) no=true;
if(t==0 && flo0(a[i][n+1])) many=true;
}
if(no) printf("-1");
else if(many) printf("0");
else for(int i=1;i<=n;i++) {if(flo0(a[i][n+1])) a[i][n+1]=0.0;printf("x%d=%.2lf\n",i,a[i][n+1]);}
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
for(int j=1;j<=n+1;j++) {
cin>>a[i][j];
}
}
REF();
RREF();
return 0;
}
I can't get the input,but the output is
x1=-nan
x2=-nan
x3=-nan
x4=-nan
x5=-nan
x6=-nan
x7=-nan
x8=-nan
x9=-nan
x10=-nan
x11=-nan
x12=-nan
x13=-nan
x14=-nan
x15=-nan
x1...
I initially thought the reason is that I didn't use "eps" when I determining if a double equals to 0,but after I added,the error still existed.
I can't get any information about "-nan" from google.
It's a not-a-number value with a representation with negative in the sign bit, that your runtime has decided to show as "negative nan".
When the program working, the value in p[i][j] overflows and it gets inf. And inf/inf causes nan.

What is wrong with my code in finding the length of Maximum Bitonic Subarray

Logic used is simple if a triplet x,y,z occurs such that x>y<z then give the length of the array to m and continue and it gives right output in all the test cases I can think of
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int l=0;
int m=0;
for(int i=0;i<n;i++)
{
l++;
if(i>0&&i<n-1)
{
if(a[i]<a[i-1]&&a[i]<a[i+1])
{
l=1;
}
}
if(m<l)
m=l;
}
if(m)
cout<<m<<endl;
else
cout<<l<<endl;
}
return 0;
}

RunTime Error on online compilers when using string in c++

I am trying to run this code on online compilers and it shows Runtime errors while it runs perfectly locally. The parts of code that are not being used have been commented out.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
// your code goes here
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int x,y;
cin>>x>>y;
string ans;
int noOfApple=0,noOfBanana=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='a')
{
noOfApple++;
}
else
{
noOfBanana++;
}
}
int noOfABlock=ceil(noOfApple/x);
//int noOfBBlock=ceil(noOfBanana/y);
int length=floor(noOfBanana/(noOfABlock-1));
int remainderB=noOfBanana%(noOfABlock-1);
int counter=0;
//condition for '*'
if(noOfBanana<(noOfABlock-1))
{
int q=noOfBanana;
for(int i=0;i<(noOfABlock-1);i++)
{
for(int j=0;j<x;j++)
{
ans[counter++]='a';
}
if(q>0)
{
ans[counter++]='b';
q--;
}
else
{
ans[counter++]='*';
}
}
for(int i=0;i<x;i++)
{
ans[counter++]='a';
}
}
else{
for(int i =0;i<(noOfABlock-1);i++)
{
for(int j=0;j<x;j++)
{
ans[counter++]='a';
}
for(int j=0;j<length;j++)
{
ans[counter++]='b';
}
while(remainderB>0)
{
remainderB--;
ans[counter++]='b';
}
}
for(int i=0;i<x;i++)
{
ans[counter++]='a';
}
}
cout<<ans<<endl;
}
return 0;
}
Input for this program:-
6
ab
1 1
aab
1 1
aabb
1 2
aaaaab
2 1
aaaa
1 3
aaaaa
2 2
When Arrays are used we have to initialize the size of the arrays as they are static but as string are dynamic we do not need to initialize with a size.

Intersection of 2 bidimensional arrays

I want to determine the intersection of 2 bi-dimensional arrays.
Here is my code:
#include < iostream >
using namespace std;
void type(int mat[][10],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
}
void inter(int a[][10], int n,int b[][10],int m)
{
int k1=0,p, x,ok,j, c[50],i;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
p=0; x=0;
while(p<m)
{
ok=0;
while(x<m)
{
if(a[i][j]==b[p][x]) ok=1;
if(ok) c[k1++]=a[i][j];
x++;
}
p++;
}
for(i=0;i<n;i++)
{
cout<<c[i]<<" ";
cout<<endl;}
}
}
}
} // !!! added in edit!!!
int main()
{
int n,m,mat[10][10],c[30],mat2[10][10];
cout<<"n= ";cin>>n;
type(mat,n);
cout<<"m= "; cin>>m;
type(mat2,m);
inter(mat,n,mat2,m);
return 0;
}
It doesn't show me the expected answer. It shoes me numbers like these : 1
4758024
1
4758024
Can somebody help me?

Median program through uva

This is my first time using online judge, I tried a simple program to become familiar with the environment.
Here is the question.
I solved it as the following, but get a wrong answer!
#include<stdio.h>
#include<math.h>
#include<iostream>
int main()
{
int t;
int n;
int num[10];
int i,j,temp;
int s;
int fmid;
std::cin >>t;
int iter=0;
while (iter<t)
{
std::cin>>n;
if (n!=-1){
for(i=0;i<n;i++)
std::cin>>num[i];
for( i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(num[i]>num[j])
{temp=num[i];
num[i]=num[j];
num[j]=temp;
}
s=0;
if (n%2 ==0)
{
int mid=n/2-1;
int midd=mid+1;
s=(num[mid]+num[midd])/2;
fmid=s;
}
else
{s=ceil(n/2);
fmid=num[s];}
std::cout<<fmid;
}
iter++;
}
return 0;
}
Any suggestion is much appreciated.
Thanks
I would read all the numbers, store them in an array, then sort the array using std::sort in <algorithm>
Please find my code below:
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
int arr[10];
int main()
{
int N;
while ((std::cin >> N) && (N!=-1)){
for(int i=0;i<N;i++) {
std::cin >> arr[i];
}
std::sort(arr,arr+N);
if(N%2 == 1){
std::cout << arr[N/2] << std::endl;
}
else {
double ans = ((double)arr[N/2] + (double)arr[(N/2)-1]) / 2.0;
std::cout << ans << std::endl;
}
}
return 0;
}
I hope it works for you.
May I know the problem number? I will try to submit there and give you an AC solution.