April Cook-Off 2020 Discussion

You shouldn’t use the \verb!pow()! function because you might overflow while doing so. Instead, you should use modular exponentiation, which is discussed in this article here: Binary Exponentiation - Algorithms for Competitive Programming

2 Likes

MATBREAK - giving wrong answer. Please help anyone
long long product = a;
long long current = aa;
for(int i = 2;i <= n;i++){
long long int powerval = power(current,2
i-1,mod)%mod;
product = (product%mod+ (powerval)%mod)%mod;
current = ((powerval%mod)*(current%mod))%mod;
}
cout<<product;

Read the constraints carefully, n can only be upto 2000 in a given test, 20,000 is the maxmimum sum over all test cases, so the worst case is actually 10 tests of size 2000 which takes only 4 x 107 operations.

2 Likes

thanks a lot, makes sense.

PROBLEM CODE: LIFTME
#include<bits/stdc++.h>
#include

using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
long long int n,q,a,lastrnumber=0,sum=0;
cin>>n>>q;

    for(long long int i=0; i<q*2; i++)
    {
        cin>>a;
        sum=sum+abs(lastrnumber-a);
       lastrnumber=a;
    }
    cout<<sum;

}
return 0;

}

why it failed ?

You should take input of both the numbers(f,d) other wise they would get mixed up and you would end up taking d as f in the next iteration. And sum += abs(current-f); sum +=abs(f-d);current = d;

1 Like

This code is giving output correct but when submitted its giving wrong answer tell what’s wrong here

#include
#include

using namespace std;
int t,f,n,q,d,i,j,a,b,k;

int main()
{
cin >> t;
while(t–){
cin >> n >> q;
a=0;
b=0;
for(j=0; j<q; j++){
cin >> f >> d;
a+=abs(b-f);
a+=(abs(d-f));
b=d;
}
cout << a << endl ;
}

return 0;

}

This happened with me as well :frowning:

take floor count int as long long int it will work

link to code is Online Compiler and IDE - GeeksforGeeks
Why is it giving WA??
I have followed ur code but my opn is starting from 2 and this much is the difference

In this condition checking;
while(s[y]==p[y] && y<n)

First check for boundary condition so we can know we are checking only if we are within bounds.
while(y<n && s[y]==p[y])

Similarly do it on line 45.
Also, in your code you are not considering opn = 1 case.

I think after this it should work.

Ya Got AC.
I misunderstood that for opn=1,ans will be n always so I started from 2.
Thank u.

Okay! Got it…Thanks for Help!

Guys I have used modulation as a separate function. Tested different test cases. All are working. No overflow is there. But still judge saying WA. Please Help wrt to the problem asked in cookoff yesterday named “MATBREAK”. Having a tough time with this problem. :frowning:
@better_sleep @anon77458947 @vijju123 @sandeep9699
Here is the attached solution:
(CodeChef: Practical coding for everyone)

Still getting WA?
https://www.codechef.com/viewsolution/32123297