Runtime Error with __gcd? (floating point exception)

Joined
Nov 28, 2024
Messages
1
Reaction score
0
i don't know why my code to check if and integer make of a1*a2*... an divided for b1*b2...*bn is a repeating or finite but there is RE?
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define fi first
#define se second
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
#define pb push_back
#define EL cout<<endl;
#define fill(a,x) memset(a,x,sizeof(a))
#define pll pair<ll,ll>
const ll M = 1e9+7;
const ll MaxN = 1e5+5;
vll a,b;
bool kt(ll n)
{
if(n<1) return false;
if(n%2 != 0 && n%5 != 0) return false;
while(n%2==0 && n>1)
n /= 2;
while(n%5==0 && n>1)
n /= 5;
if(n>1) return false;
return true;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll t,n,x;
cin>>t;
while(t--)
{
cin>>n;
a.clear();
b.clear();
ll X=1, Y=1;
for(ll i=0; i<n; i++)
{
cin>>x;
a.pb(x);
}
for(ll i=0; i<n; i++)
{
cin>>x;
b.pb(x);
}
for(ll i=0; i<n; i++)
{
if(a!=0 && b!=0)
{
ll tmp1 = __gcd(a,b);
if(tmp1 != 0)
{
a = a/tmp1;
b = b/tmp1;
}
}
X = X*a;
Y = Y*b;
if(X!=0 && Y!=0)
{
ll tmp2 = __gcd(X,Y);
if(tmp2 != 0)
{
X /= tmp2;
Y /= tmp2;
}
}
}
if(X%Y==0 || X==0)
cout<<"finite" <<endl;
else
{
if(kt(Y)==true) cout<<"finite" <<endl;
else cout<<"repeating" <<endl;
}
}
}
//4
//2 1 7 5 6
//1 3 20
//1 4 1
//1 116 495
 
Joined
Sep 21, 2022
Messages
189
Reaction score
26
I'm not certain that __gcd is an official part of the language.

Its not needed anyway.
Code:
pseudocode:
LET X be the product of all a
LET Y be the product of all b
WHILE (Y % 2 == 0)
  Y /= 2
WHILE (Y % 5 == 0)
  Y /= 5
IF (X % Y) == 0
  finite
ELSE
  repeating
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,440
Members
47,101
Latest member
DoloresHol

Latest Threads

Top