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;
}
Related
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--;
}
}
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.
I was solving http://codeforces.com/problemset/problem/552/B.
In my first attempt I came up with something like:
#include <bits/stdc++.h>
using namespace std;
int digit(long a){
int i=0;
while(a){
a/=10;
i++;
}
return i;
}
int main()
{
long n;
long long s=0;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
int dig=digit(n),i=0;
while(i<dig){
s+=(n-pow(10,i)+1);
i++;
}
cout<<s;
return 0;
}
But for input
1000000
My program outputed
5888895
I was expecting
5888896
In my second try I wrote pow function for myself:
#include <bits/stdc++.h>
using namespace std;
int digit(long a){
int i=0;
while(a){
a/=10;
i++;
}
return i;
}
long long pow1(int a){
long long s=1;
while(a--){
s*=10;
}
return s;
}
int main()
{
long n;
long long s=0;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
int dig=digit(n),i=0;
while(i<dig){
long long aux=pow1(i);
s+=(n-aux+1);
i++;
}
cout<<s;
return 0;
}
And this time it was correct.How can one explain the working behind it?
The problem with the built-in pow function is that is does not work as accurate as your function. pow calculates x to the y as exp(y*log(x)). This generic formula works with all (even non-integral) exponents, and its performance is (mostly) independent on the arguments. The problem with that formula is, that pow(10,2) might be 99.9, which gets truncated to 99 when it is converted to an integer type. Try pow(10,i) + 0.5 to perform proper rounding.
You may not need pow here. This works as expected and is faster too.
#include <iostream>
typedef unsigned long long ull;
using namespace std;
ull count(ull n) {
ull i = 0;
for (; n; ++i) n /= 10;
return i;
}
int main() {
ull n;
cin >> n;
ull digits = count(n);
ull ans = digits * (n + 1);
for (ull i = 0, j = 1; i < digits; ++i, j *= 10)
ans -= j;
cout << ans;
return 0;
}
All testcases passed on codeforces.com
You just need to multiply pow(10,i-1) with 0.1. That will do the work you needed.
#include <iostream>
#include <cmath>
using namespace std;
int digit_calc(long long num);
long long digit_counter(long long num, int digit);
int main()
{
long long num,digit,total;
cin>>num;
digit=digit_calc(num);
total=digit_counter(num, digit);
cout<<total<<endl;
return 0;
}
int digit_calc(long long num){
int digit=0;
while(num){
digit++;
num=num/10;
}
return digit;
}
long long digit_counter(long long num, int digit){
long long sup,net,total=0;
while(num){
sup=0.1*(pow(10,digit)-1);
net=num-sup;
total=total+(net*digit);
num=sup;
digit--;
}
return total;
}
It passed all test cases on codeforce.
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;
}
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.