I am trying to solve the question http://www.spoj.com/problems/PRIMEZUK/
#include<iostream>
#include<cstdio>
#include<math.h>
#define l long long
using namespace std;
l chk(l a)
{
for(int i=2;i<=sqrt(a);++i)
{
if(a%i==0)
{
return a/i;
}
}
return 0;
}
main()
{
// freopen("in.txt","r",stdin);
int t,n,a;
l prod=1,flag;
//t=inp();
cin>>t;
for(int j=1;j<=t;++j)
{
cin>>n;
//n=inp();
if(n==0)
prod=-1;
else
prod=1;
while(n--)
{
cin>>a;
//a=inp();
prod*=a;
}
++prod;
flag=chk(prod);
if(!flag)
printf("Case #%d: %lld\n",j,prod);
else
printf("Case #%d: %lld\n",j,flag);
}
}
i am getting right answer for the sample test case but whne i submit i am getting wrong answer...any hints???
You are getting wrong answer because "return a/i" may also return a non-prime number.So you should check whether "return a/i" is prime or not..
Try This...
#include<bits/stdc++.h>
long long int check(long long int a)//Function to check whether a number is prime or not
{
long long int i,k;
k=sqrt(a);
for(i=2;i<=k;++i)
{
if(a%i==0) // if not prime
return check(a/i); //then find greatest prime
}
return a;
}
int main()
{
int j=1,t;
long long int n,a,prod,flag;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if(n==0)
prod=-1;
else
prod=1;
while(n--)
{
scanf("%lld",&a);
prod*=a;
}
++prod;
printf("Case #%d: %lld\n",j,check(prod));
j++;
}
return 0;
}
Related
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
}
}
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;
}
Why am i getting time limit exceeded in this question? link: http://www.spoj.com/problems/ENIGMATH/
lcm is easily calculated with the help of gcd of two numbers.For calculating the gcd euclid's algorithm is used.
#include<iostream>
using namespace std;
long long int gcd(long long int a,long long int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
long long int t,a,b,lcm,i=0;
cin>>t;
while(i<t)
{
cin>>a>>b;
if(a==b)
cout<<"1 1\n";
else
{
lcm=(a*b)/gcd(a,b);
cout<<lcm/a<<" "<<lcm/b<<"\n";
}
}
return 0;
}
You are not updating i in the loop, so it will fail into an infinite loop unless you enter negative value or zero to t.
Try having it updated by changing i<t to i++<t or another manner you like.
You may try this Way... Thank You.
#include<iostream>
using namespace std;
long long int gcd(long long int a,long long int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
long long int t,a,b,lcm,i=0;
cin>>t;
while(i<t)
{
cin>>a>>b;
if(a==b)
cout<<"1 1\n";
else
{
lcm=(a*b)/gcd(a,b);
cout<<lcm/a<<" "<<lcm/b<<"\n";
}
i += 1;//Here was mistook.
}
return 0;
}
I know that the code is not an optimized one. I just wanted to practice segmented sieve of eratosthenes and finding prime numbers in a different way.
My compiler is Dev-C++ 5.11.
System specifications:
OS: Windows 8.1 Embedded Pro
Processor: Intel i5(3rd Generation)
RAM: 4Gb
I don't know if the problem is due to hardware compatibility, I haven't use any low level procedures though.
Please explain to me why the program crashes.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#define NUM 1000000
using namespace std;
bool a[NUM]={0};
struct prime10
{
vector<long long int> primes;
int max;
prime10()
{
max=NUM;
}
}p;
void preprocess();
bool ifprime(unsigned long long int n);
void primemn(int m,int n);
int main()
{
preprocess();
int t=0;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
int a,b;
scanf("%d %d",&a,&b);
primemn(a,b);
}
return 0;
}
void preprocess()
{
for (int i=2; i<=NUM; i++){
if (a[i]==0){
for (int j = i*2; j<=NUM; j+=i){
a[j]=1;
}
}
}
}
bool ifprime(unsigned long long int n)
{
if(n<NUM){
if(!a[n])
return true;
}
else if(n%2==0)
return false;
else
{
for(int i=3;i<sqrt((long double)n);i+=2)
{
if(ifprime(i))
{
if(n%i==0)
return false;
}
}
}
return true;
}
void primemn(int m,int n)
{
if(m<NUM&&n<NUM)
{
for(long long int i=m;i<=n;i++)
{
if(i==0||i==1)
{
}
else if(!a[i])
printf("%lld\n",i);
}
}
else if(m<NUM&&n>NUM)
{
if(n<=p.max)
{
for(long long int i=m;i<NUM;i++)
{
if(!a[i])
printf("%lld\n",i);
}
for(int i=0;i<p.primes.size();i++)
printf("%lld\n",p.primes[i]);
}
else if(n>p.max)
{
for(long long int i=m;i<NUM;i++)
{
if(!a[i])
printf("%lld\n",i);
}
for(int i=0;i<p.primes.size();i++)
printf("%lld\n",p.primes[i]);
for(int k=(p.max)+1;k<=n;k++)
if(ifprime(k)){
printf("%lld\n",k);
p.primes.push_back(k);
p.max=k;
sort(p.primes.begin(),p.primes.end());
}
}
}
else if(m>NUM&&n>NUM)
{
if(m<=p.max&&n<=p.max)
for(int o=0;o<p.primes.size();o++)
{
if(p.primes[o]>=m&&p.primes[o]<=n)
printf("%lld\n",o);
}
else if(m<=p.max&&n>p.max)
{
for(int o=0;o<p.primes.size();o++)
{
if(p.primes[o]>=m&&p.primes[o]<=n)
printf("%lld\n",o);
}
for(int o1=(p.max)+1;o1<=n;o1++)
if(ifprime(o1)){
printf("%lld\n",o1);
p.primes.push_back(o1);
p.max=o1;
sort(p.primes.begin(),p.primes.end());
}
}
else if(m>p.max&&n>p.max)
{
for(int o1=m;o1<=n;o1++)
if(ifprime(o1)){
printf("%lld\n",o1);
p.primes.push_back(o1);
p.max=o1;
sort(p.primes.begin(),p.primes.end());
}
}
}
}
You're writing outside the boundary of a - your preprocess loops should be < NUM.
Overstepping this boundary has undefined behaviour.
It may cause corruption of the neighbouring p, and as p's first member is a vector, a crash when the vector is destroyed is very likely.
Of course other scenarios are possible, but it sounds like this is what's happening in your case.
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.