SIGSEV SPOJ(PRIME1) - c++

The problem I am solving is PRIME1 which says to print all the prime numbers between 2 numbers. Although the code I made runs properly on my compiler with the worst case as well, It gives me a SIGSEV error when compiling in SPOJ. Here is the code
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int t;
cin>>t;
while(t>0)
{
int a,b,n,p;
cin>>a>>b;
n = b-a;
bool prime[n+1];
memset(prime,true,sizeof(prime));
for(p=2;p*p<=b;p++)
{
if(prime[p]==true)
{
for(int i=p*2;i<=b;i+=p)
{
prime[i]=false;
}
}
}
(a==1)?p=2:p=a;
for(p;p<=b;p++)
if(prime[p])
cout<<p<<endl;
t--;
}
}

Related

I am trying to find the factors of factorial of a number

I am trying to solve the SPOJ problem DIVFACT where we need to find the factorial of a number. Though I used the correct formulae and at the same time I also checked the special case of 0 and 1,still I am getting wrong answer and it's really difficult for me to figure out what's wrong with my code. Can someone please help me figure out the problem? For reference I am providing the link to the problem:- Link of the problem
#include<iostream>
#include<vector>
#define MOD 100000007
#define MAX 50001
#define pb push_back
using namespace std;
typedef long long ll;
ll no_of_factors(int num,vector<int> primes)
{
ll result=1;
for(int i=0;i<primes.size();i++)
{
if(primes[i]>num)
break;
ll k=primes[i];
ll count=0;
while(num/k!=0){
count=(count+(num/k))%MOD;
k=k*primes[i];
}
result=(result*((count+1)%MOD))%MOD;
}
return result;
}
vector<int> seive(){
bool isPrime[MAX];
vector<int> v;
for(int i=2;i<MAX;i++)
isPrime[i]=true;
for(int i=2;i*i<MAX;i++)
{
if(isPrime[i])
{
for(int j=i*i;j< MAX;j+=i)
isPrime[j]=false;
}
}
for(int i=2;i<MAX;i++)
{
if(isPrime[i])
v.pb(i);
}
return v;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int num;
ll divisors;
cin>>num;
vector<int> v1;
v1=seive();
divisors=no_of_factors(num,v1);
cout<<divisors<<endl;
}
return 0;
}
The problem states that the answer should be in MOD 10^9+7 but you have mistakenly defined MOD as 10^8+7.

codeforces problem named Strange List the "MEMORY LIMIT EXCEEDED"

With reference to the problem named "Strange List" in codeforces following is my code
But the problem is All test case is passing but the last test case it is showing"MEMORY EXCEEDED LIMIT" and the last test case value is also huge like for n it is 100000.
Please guide me the way to tackle it
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
ll ele,sum=0,i,j,h,n,x;
cin>>n>>x;
vector <ll> vec;
for(i=0;i<n;i++)
{
cin>>ele;
vec.push_back(ele);
}
for(i=0;i>=0;i++)
{
if(vec.at(i)%x==1)
{
break;
}
else
{
h=vec.at(i)/x;
for(j=0;j<x;j++)
{
vec.push_back(h);
}
}
}
for(i=0;i<vec.size();i++)
{
sum+=vec.at(i);
}
cout<<sum<<"\n";
}
}
You can use Dynamic Programming to solve this problem
Implementation of storing results can be done by using Vector of pairs
Below is the AC code demonstrates how these kinds of problems can be done efficiently in terms of memory consumption......Happy Coding :)
CODE
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t;
cin>>t;
while(t--){
ll n,x;
cin>>n>>x;
vector<pair<ll,ll>> a(n);
for(int i=0;i<n;i++){
cin>>a[i].first;
a[i].second=a[i].first; // Storing initial results in second part of pair
}
ll s=0;
ll d=0;
ll r=0;
ll k=0;
while(++r){
for(int i=0;i<n;i++){
if(a[i].first%x==0){
s+=a[i].second;
a[i].first=a[i].first/x;
a[i].second=(a[i].first*pow(x,r)); // updating results
}
else{
s+=a[i].second;
if(d==0)
k=i;
d=1;
}
}
if(d)
break;
}
for(int i=0;i<k;i++)
s+=a[i].second; // getting desired sum
cout<<s<<"\n"; // printing answer
}
}

SPOJ - NDIV Cant find the error

I have implemented sieve approach and it runs correctly for my test cases. I used a counter rather than a flag for all numbers and the times the numbers been accessed. When i run it on SPOJ it gives wrong answer.
Here is the link
http://www.spoj.com/problems/NDIV/
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
int main(){
vector<long int> vec;
long int sum=0,n,i,j,k,a,b,c;
double root;
cin>>a>>b>>n;
root = sqrt(b);
for(i=1;i<=b;i++) vec.push_back(1);
for(i=2;i<=root;i++){
k=i;
c=0;
for(j=2;j<=b && c<=b;j++){
c=k*j;
if(c<=b){
vec[c-1]++;
}
}
}
for(i=a;i<=b;i++){
if(sqrt(i)-floor(sqrt(i))!=0){
if(vec[i-1]*2 == n) {
sum++;
}
}
else {
if(vec[i-1]*2-1 == n) {
sum++;
}
}
}
cout<<sum;
}

Declaring 2-dimensional array in "Edit distance" on spoj locally gives runtime error?

While solving http://www.spoj.com/problems/EDIST/ , when I declare the 2-d array globally: (http://ideone.com/jG3jPW)
#include <iostream>
using namespace std;
long long int s[2001][2001];
int main() {
int t;
string a,b;
long long int i,j;
for(i=0;i<2001;i++)
{
s[i][0]=i;
s[0][i]=i;
}
cin>>t;
while(t>0)
{
cin>>a>>b;
t--;
for(i=1;i<=a.length();i++)
{
for(j=1;j<=b.length();j++)
{
if(a[i-1] == b[j-1])
s[i][j]=s[i-1][j-1];
else
s[i][j] = min(min(s[i-1][j],s[i-1][j-1]),s[i][j-1]) + 1;
}
}
cout<<s[i-1][j-1]<<"\n";
}
return 0;
}
no error occurs. But when i declare the same array locally(http://ideone.com/Tyj6UU),
#include <iostream>
using namespace std;
int main() {
int t;
string a,b;
long long int i,j;
long long int s[2001][2001]; //declared locally
for(i=0;i<2001;i++)
{
s[i][0]=i;
s[0][i]=i;
}
cin>>t;
while(t>0)
{
cin>>a>>b;
t--;
for(i=1;i<=a.length();i++)
{
for(j=1;j<=b.length();j++)
{
if(a[i-1] == b[j-1])
s[i][j]=s[i-1][j-1];
else
s[i][j] = min(min(s[i-1][j],s[i-1][j-1]),s[i][j-1]) + 1;
}
}
cout<<s[i-1][j-1]<<"\n";
}
return 0;
}
runtime error occurs. Why?
It seems that you are having some kind of problem with memory allocation.
In your second approach (i.e., the one with local declaration), change:
long long int s[2001][2001];
To:
long long int ** s = new long long int * [2001];
for (i=0;i<2001;i++)
s[i] = new long long int[2001];
This will solve your problem.

finding the number of divisors don't know what is going wrong

I am coding to find the number of common divisors of two numbers n and k.
I am implementing it using the approach of finding the GCD g and then finding the number of divisors of the GCD.
However the code compiles but gives a not responding message on running :(
I have blown my head off on this.. can anyone please help in debugging..
Thanks in advance
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
vector<bool>p(1000002,true);
long int getgcd(long int a, long int b)
{ if(b == 0)
return a;
else
return getgcd(b, a % b);
}
void prime()
{ int i,j;
for(i=2;i<1000001;i++)
if(p[i])
for(j=i*i;j<1000001;j=j+i)
p[j]=false;
p[0]=false;
p[1]=false;
}
void divfind(long int a, long int b)
{
long int g;
g = getgcd(a,b);
int i,s,j=0,ans=1,num=0;
//short int fo[1000000];
s=(int) sqrt(g);
for(i=2;i<s+1;i++)
if(p[i])
{
while(g%i==0)
{g=g/i;
num++;
}
if(num)
ans*=++num;
num=0;
}
printf("%d\n",ans);
}
int main()
{
prime();
long int n;
int q;
scanf("%ld %d",&n,&q);
while(q--)
{
int t;
long int k;
scanf("%d%ld",&t,&k);
if(t==1)
divfind(n,k);
// else if(t==2)
// divi(n,k);
// else
// nodivi(n,k);
}
return 0;
}
You're running into integer overflows - i*i is only safe up to i=46340 (~2^15*sqrt(2)) with 32 bit integers, e.g:
i i*i
46339 2147302921
46340 2147395600
46341 -2147479015
46342 -2147386332
This leads to undefined behaviour.