Bro, can you tell me any corner case which I am missing?
https://www.codechef.com/viewsolution/52370165
why my code is not working?
#include
#include<stdio.h>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int evenpoints(int n,int a)
{
int b,s=0;
while (n%2==0)
{
s+=a;
n=n/2;
}
return s;
}
int oddpoint(int n,int b)
{
int i,a,s=0;
for(i=3;i<=n;i+=2)
{
while (n%i==0)
{
s+=b;
n=n/i;
}
}
return s;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int pe,po,n,a,b;
cin>>n>>a>>b;
if (a<0 and b<0)
{
if(n%2==0)
cout<<a;
else
cout<<b;
}
else if(a>=0 and b<0)
{ pe=evenpoints(n,a);
if(n%2==0)
cout<<pe;
else
cout<<po;
}
else if(a<0 and b>=0)
{ po=oddpoint(n,b);
if(n%2==0)
cout<<a+po;
else
cout<<po;
}
else if(a>=0 and b>=0)
{
pe=evenpoints(n,a);
po=oddpoint(n,b);
cout<<pe+po;
}
cout<<endl;
}
}
can you share the code which gives correct output for
above testcase.thnx
#include
#include
#include
using namespace std;
int main()
{int T;
cin>>T;
while(T–)
{
int n,A,B;
cin>>n>>A>>B;
vectorvt;
int sum=0;
while(n%2==0)
{
vt.push_back(2);
n=n/2;
}
for(int i=3;i<=sqrt(n);i+=2)
{
while(n%i==0)
{
vt.push_back(i);
n=n/i;
}
}
if(n>2)
vt.push_back(n);
if(B<0)
cout<<B<<endl;
else if (A<0)
cout<<A<<endl;
else
{
for(auto i: vt)
{
if(i%2==0)
sum+=A;
else
sum+=B;
}
cout<<sum<<endl;
}
}
return 0;
}
it is giving correct output but showing wrong answer
yeah i am also having same problem
When there is negative score for odd factors the logic is:
→ To club all the odd factors together to form 1 odd number.
→ Then again convert that ODD number to even, if there is atleast one 2 present as factor. Hence making number of ODD divisors as 0.
→ If there are no 2 as factor, so the multiplication of all ODD factors will be that number itself. i.e 1 ODD number
The answer to this testcase is supposed to be 0
1
10 -1 1
Thanks brother, i have corrected it and now it’s accepted🤗
This is not expected and so annoying that n was the range 1e9 and testcases too 1e3 , and then the solution has o(sqrtn) time complexity , i figured out the solution as soon as i saw the problem , but just because of the fact that n was large , i thought if there existed some better optimization but turns out that that main solution had sqrtn
**PLEASE HELP ME.I DON’T UNDERSTAND WHY IT IS GIVING WRONG ANSWER. **
I AM A BEGINNER AND TRYING TO SOLVE THIS QUESTION BUT IT IS GIVING WRONG ANSWER.
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
long long int div(long long int n, long long int a, long long int b)
{
if (n == 1)
{
return 0;
}
if (a == 0 && b == 0)
{
return 0;
}
long long int divisor = 0;
if (a >= 0 && b >= 0)
{
if (n % 2 == 0) //FOR EVEN
{
return a + div(n / 2, a, b); //CAN GO TO ODD AS WELL
}
else //FOR ODD
{
if (b == 0)
{
return 0;
}
// FIND THE FIRST ODD DIVISOR OF N
for (long long int i = 3; i <= sqrt(n); i += 2)
{
if (n % i == 0)
{
divisor = i;
break;
}
}
if (!divisor) //MEANS CAN'T FIND ANY DIVISOR SO PRIME NO.
{
return b;
}
else
{
return b + div(n / divisor, a, b);
}
}
}
if (a >= 0 && b < 0)
{
if (n % 2 == 0)
{
if (a == 0)
{
return 0;
}
long long int x = n, sum = 0;
while ((x != 1) && x % 2 == 0) //DON'T GO TO ODD NO.
{
sum += a;
x /= 2;
}
return sum;
}
else
{
return b; //WE CAN'T GET EVEN NO. AFTER DIVIDING ODD BY ODD
}
}
if (a < 0 && b >= 0)
{
if (n % 2 == 0)
{
if (b == 0)
{
return a;
}
for (long long int i = 2; i <= sqrt(n); i += 2)
{
if (n % i == 0 && ((n / i) % 2) == 1) //WE CAN GO IN ODD NO. ONLY
{
divisor = i;
break;
}
}
if (!divisor)
{
return a;
}
else
{
return a + div(n / divisor, a, b);
}
}
else
{
for (long long int i = 3; i <= sqrt(n); i += 2) //FIND THE FIRST ODD DIVISOR
{
if (n % i == 0)
{
divisor = i;
break;
}
}
if (!divisor)
{
return b;
}
else
{
return b + div(n / divisor, a, b);
}
}
}
if (b < 0 && a < 0)
{
if (n % 2 == 0)
{
return a;
}
else
{
return b;
}
}
return -1;
}
int main()
{
int t;
cin >> t;
while (t--)
{
long long int n, a, b;
cin >> n >> a >> b;
cout << div(n, a, b) << endl;
}
return 0;
}
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while(tc–)
{
int n, a, b;
cin >> n >> a >> b;
int eve = 0, odd = 0;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
while (n % i == 0)
{
n /= i;
if(i%2 == 0)
eve++;
else
odd++;
}
}
}
if (n != 1)
odd++;
int res = 0;
if (a >= 0 && b >= 0)
{
res = eve * a + odd * b;
}
else if (a <= 0 && b <= 0)
{
if (eve)
res = a;
else
res = b;
}
else if (a >= 0 && b <= 0)
{
if (eve)
res = eve * a;
else
res = b;
}
else if (a <= 0 && b >= 0)
{
if (eve)
res = a + odd * b;
else
res = odd * b;
}
cout << res << endl;
}
}
why it gives error in one subtask