Project Euler #236: Luxury Hampers

Sort by

recency

|

64 Discussions

|

  • + 0 comments
    1. Loop over A spoilage as(i) in [1;a(i)[ range;
    2. Use GCD to reduce fractions;
    3. Overall timing for n=5 should be near 0.01 s.
  • + 0 comments

    For a good 5 minutes there I thought i was going loopy haha... what the hell is this question.

  • + 0 comments

    include

    include

    include

    include

    include

    include

    using namespace std;

    void Luxury_Hampers(int n, vector a, vector b){ long sum1=0,sum2=0; int sum_of_a_till_now=0; int sum_of_b_till_now=0;

    int till_now_bad_a=0;
    int till_now_bad_b=0;
    
    int flag=0;
    vector <int> Na;
    vector <int> Nb;
    for (int i=0; i<n; i++){
        float r1,r2;
        flag=0;
        sum_of_a_till_now+=a[i];
        sum_of_b_till_now+=b[i];
        for (int j=a[i]-1; j>0; j--){
            r1=(float)j/a[i];
            for (int k=1; k<=b[i]; k++){
                r2=(float)k/b[i];
                if (r2>r1){
                    till_now_bad_a+=j;
                    till_now_bad_b+=k;
                    if (i>0){
                        float prev_a = (float)till_now_bad_a/sum_of_a_till_now;
                        float prev_b = (float)till_now_bad_b/sum_of_b_till_now;
                        if (prev_a>prev_b){
                            Na.push_back(j);
                            Nb.push_back(k);
                            flag=1;
                            break;
                        }
                        else{
                            till_now_bad_a-=j;
                            till_now_bad_b-=k;
                        }
                    }
                    else{
                        Na.push_back(j);
                        Nb.push_back(k);
                        flag=1;
                        break;
                    }
                }
            }
            if (flag==1)
            break;
        }
    }
    for (int i=0; i<n; i++){
        sum1+=a[i];
        sum2+=b[i];
    }
    long suma=0,sumb=0;
    for (int i=0; i<Na.size(); i++){
        suma+=Na[i];
        sumb+=Nb[i];
    }
    
    long num=suma*sum2;
    long den=sumb*sum1;
    
    if (num>den){
        int a=num%den;
        if (num%a==0&&den%a==0){
            num/=a;
            den/=a;
        }
        cout<<num<<"/"<<den;
    }
    else if (den>num){
        int a=den%num;
        if (den%a==0&&num%a==0){
            num/=a;
            den/=a;
        }
        cout<<num<<"/"<<den;
    }  
    

    }

    int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; cin>>n; vector a; vector b; for (int i=0; i>x; a.push_back(x); } for (int i=0; i>x; b.push_back(x); }

    Luxury_Hampers(n,a,b);
    return 0;
    

    }

  • + 0 comments

    passed 6/16 test cases. failed 10/16 due to timeout. finding a way to optimize it. any suggestion?

    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)))   """make a list of all possible values of number of bad products for each product by A"""
        n_b.append(list(range(1,b[i]+1)))    #same for B
        
    for i in product(*n_a):      """iterate through all the possible combinations of number of bad product by A"""
         for j in product(*n_b):  """iterate through all the possible combinations of number of bad product by B"""
               count=0                """counting the number of times ratio is maintained for the current combination number of bad products by A and B """
               p=sum(i)*sb            """expression you will get by putting the given condition in a equation (A-overall/B-overall) spoilage ratio"""
               q=sum(j)*sa
               m=math.gcd(p,q)  """reduce the fraction to its lowest terms"""
               p,q=p//m,q//m          
     
               if (p/q)<=1:           """less than 1 then continue to next iteration"""
                      continue
    
               for k in range(n):     """each pair of kth product check the equality of ratio """
                      r=(j[k]*a[k])
                      s=(i[k]*b[k])         """expression you will get by putting the given condition in a equation (B-kth/A-kth) per product spoilage ratio"""
                      
                      t=math.gcd(r,s)      """reduce the fraction to its lowest terms"""
                      r,s=r//t,s//t
    
                       if r!=p or s!=q :     """not equal then break the loop and go for next combination of i and j (change j)"""
                                 break
                       else:
                                  count+=1          """equal then increase count"""
                 
                       if count==n:          """the ratio is same for all the n products then print the ratio and exit all the loops"""
                                   flag=1
                                   print("{}/{}".format(r,s))
                                   break
    
               if flag==1:
                     break
    
        if flag==1:
            break
    
  • + 0 comments

    problem doesn,t contain proper data as spoilage data is not provided