ALEXTASK - Editorial

My python code passes all tasks except the last one for large numbers. Can somebody tell me what I can do?

from fractions import gcd
def lcm(a,b):
return (a*b)/gcd(a,b)
t=int(input() )
while t:
t=t-1
n=int(input())
l=map(int,raw_input().split(" "))
min1 =[]
for i in range(len(l)):
for j in range(i+1,len(l)):
val = lcm(l[i],l[j])
min1.append(val)
min1.sort()
print min1[0]

I am getting NZEC for the last task, what is the problem here ? Thank you.

We have to find the minimum LCM right?
Minimum LCM can be found by finding the lcm of 2 smallest numbers

Can someone please tell me why my code is not working correctly ?
https://www.codechef.com/viewsolution/15494387

@kishynivas try using


n=int(input().strip(""))

sometimes the test cases has extra white space attached to it at the beginning and at the end, so this helps in removing it.

Hi All,
I tried the same logic, could someone please point out the error in my code.
https://www.codechef.com/viewsolution/15824052

Sainath

Visual c++ says vector subscript out of range but the range is well within the given ‘n’ . Please help me identify the flaw.
#include
#include
#include

using namespace std;

long long int hcf(long long int a,long long int b) {
if (b == 0) { return a; }
else {
long long int c = a%b;
return hcf(b, c);
}
}

int main() {
int t;
cin >> t;
while (t–) {
int n;
long long int ans;
cin >> n;
vector a;
for(int i=0;i<n;i++) {
long long int temp;
cin >> temp;
a.push_back(temp);
}
ans = (a[0] * a[1] / hcf(a[0], a[1]));
for (int i = 0; i < n; i++) {
for (int j = i+1; j <n , i!=j; j++) {
min(ans, (a[i] * a[j] / hcf(a[i], a[j])));
}
}
cout << ans << endl;
}
return 0;
}`

Bonus problem

Can this problem be solved for larger constraints such as N <= 10^5 and Ai <= 10^9 ?

If yes, please discuss your approach…

#include<bits/stdc++.h>

using namespace std;

const int N = 503;
int n;
int t;
long long a[N];
long long ans;

long long gcd(long long a, long long b){
if (b == 0) return a;
else return gcd(b,a % b);
}

long long lca(long long a,long long b){
return (a * b) / gcd(a,b);
}

int main(){
//freopen(“2.in.txt”,“r”,stdin);
//freopen(“2.out.txt”,“w”,stdout);

cin >> t;

while(t --> 0){

    cin >> n;

    ans = 1e18;
    for(int i = 0; i < n; ++i)
        cin >> a[i];

    for(int i = 0; i < n; ++i){
        for(int j = i + 1; j < n; ++j){
            ans = min(ans,lca(a[i], a[j]));
        }
    }
    cout << ans << "\n";
}

return 0;

}

///the code above is setter’s solution!!! isnt there a bug in you opinion??!! look at two for loops in main
//method the j index is exceeding limit and this is a runtime error!!! i wonder who design this problem?? an //idiot??? please email me at: danimohamadnejad@gmail.com to know if i am wrong!

@srd091 don’t think it will be a problem as t declared in inner while will hide outer t in the inner while block.

1 Like

check in answers , have tried to explained this … if anything isn’t clear feel free to ask

@error_503_404 I think there is a slight issue with the test case you’re failing. More specifically, there is extra whitespace in the input file. Using strip() on each line solves the problem, see this. Or it is always safer to use the split() function without any parameters, as here. According to the Python documentation
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

Hope this solves your problem.

Thanks for helping, smsubham. It was a trivial mistake.

1 Like

@d1k5 your program is correct but you have used double type for calculating the answer. Double can store 15-17 digits of precision but the answer in the worst case may go up to 1000000000*999999999, which leads to loss of precision. A long long can store this comfortably, but try this multiplication with double types, you’ll see the problem :slight_smile:

2 Likes

hey @radek_rak , check the constraints. 1 ≤ Ai ≤ 10^9 . You would have to use “long long int” .
TC where your code is failing :
test_cases = 1
N = 2
1000000000 999999999

Also, your code has some logical issues . wrong answer on this TC too,
N = 5
array = 2 5 7 9 9

. Your ans=10 , correct ans=9.

hey @vikasj554, thanks for the help, I modified the code and it is working for all the above cases along with your cases but it is still failing. :frowning:

In short, I am maintaining two variables, min1, min2 and then calculating LCM of all the other inputs and swapping min1, min2 based on the newly read value along with the LCM.

Can anyone tell why I am getting wrong answer on sub-task 2. I am applying the same method as provided in editorial. Here is the link.

why this code giveing wrong answer
https://www.codechef.com/viewplaintext/48762744