Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
https://www.hackerrank.com/contests/projecteuler/challenges/euler236/problem[^][^]

What I have tried:

I tried solving it but getting timeout for 66% of the test cases
Posted
Updated 13-Nov-22 16:27pm
Comments
Dave Kreskowiak 13-Nov-22 13:48pm    
You're not going to get a solution coded for you. This is not a "code to order" or "do my work for me" site.

You have to show the code you wrote and describe the problem you're having with it.
adriancs 13-Nov-22 22:08pm    
If you are looking for someone else to do the research and work for you, you can go to this site: www.freelancer.com

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Hi,
This is my solution but I am getting timeout for 60% testcases

from itertools import *
import math
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
n_a=[]
n_b=[]
sa=sum(a)
sb=sum(b)
flag=0
for i in range(n):
    n_a.append(list(range(1,a[i]+1)))  
    n_b.append(list(range(1,b[i]+1)))    #same for B
    
for i in product(*n_a):      
    for j in product(*n_b):  
        count=0                
        p=sum(i)*sb           
        q=sum(j)*sa
        m=math.gcd(p,q) 
        p,q=p//m,q//m          

        if (p/q)<=1:         
            continue

        for k in range(n):  
            r=(j[k]*a[k])
            s=(i[k]*b[k])      
            
            t=math.gcd(r,s)    
            r,s=r//t,s//t

            if r!=p or s!=q :   
                break
            else:
                count+=1      
            
            if count==n:  
                flag=1
                print("{}/{}".format(r,s))
                break

            if flag==1:
                break

        if flag==1:
            break
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900