Problem link
if you imagine this test case then you show that if k is even then a is positive k/2 time and b is k/2 time because if a = 4, b = 3 and k = 4 then you show that k/2 = 4/2 = 2 time a positive so total a = a*(k/2) and b = same b*(k/2) and total answer is a - b. if k is odd then example k = 5 then we see that a = a*(k/2 +1) time and b = b*(k/2) time and final answer is a - b. maybe you understand this formula... thanks :)
A solution in c++
#include<bits/stdc++.h>
using namespace std;
/// Typedef
typedef long long ll;
#define sc1(a) scanf("%lld",&a)
#define sc2(a,b) scanf("%lld %lld",&a,&b)
#define pf1(a) printf("%lld\n",a)
#define pf2(a,b) printf("%lld %lld\n",a,b)
#define mx 1000010
#define mod 10000007
#define PI acos(-1.0)
int main()
{
ll row, col = 0, tc, t = 1;
sc1(tc);
while (tc--){
ll a, b, k;
sc2(a, b);
sc1(k);
ll ans = 0;
if(k%2==1){
ll sd = (k/2)+1;
ll ans = (sd*a) - (k/2)*b;
pf1(ans);
}
else{
ll ans = ((k/2)*a) - ((k/2)*b);
pf1(ans);
}
}
return 0;
}
2 Comments
How did you got that solution I mean how did you develop that formula
ReplyDeleteif you imagine this test case then you show that if k is even then a is positive k/2 time and b is k/2 time because if a = 4, b = 3 and k = 4 then you show that k/2 = 4/2 = 2 time a positive so total a = a*(k/2) and b = same b*(k/2) and total ans is a - b. if k is odd then example k = 5 then we see that a = a*(k/2 +1) time and b = b*(k/2) time and final answer is a - b. maybe you understand this formula... thanks :)
DeleteIf you have any doubts, Please let me know