# CHFING - Subtast 2- Task 2 is not working

``````#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define f first
#define s second
ll N = 1000000007;
ll power(ll x, ll  y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;

y = y>>1;
x = (x*x) % p;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
cin >> t;
while(t--)
{
ll n , k;
cin >> n >> k;
ll ans ;
if(n==2)
{
k--;
if(k%2==0)
{
ll temp = k/2;
ll temp1 = (k%N + 1)%N;
ans = ((temp%N)*(temp1%N))%N;
}
else
{
ll temp = (k+1)/2;
ll temp1 = (k%N);
ans = ((temp%N)*(temp1%N))%N;
}
cout << ans << endl;
}
else
{
if(k<=n)
cout << k-1 << endl;
else
{
ll ans;
ll first = k-1;
ll diff = n-1;
ll last = (k-1)%(n-1);
if(last==0)
last = n-1;

ll t = (first - last)/diff + 1;
ll sum = (first%N + last%N)%N;
ans = (sum%N)*(t%N)%N;
ans = (power(2,N-2,N)*(ans%N))%N;

cout << ans << endl;
}
}
}
}``````

Now, What did I do?

• Removed some unnecessary cases.
• Added a function named `modmult(a,b)`. This basically helps to find multiplication of larger numbers using MOD in the background.
• Used `modmult(a,b)` in all the calculations.

And its an AC.

1 Like

Thanks buddy…but what was my mistake…I figured out the formula and all but then also I struggled…

The values were overflowing in C++ without modmult(a,b) (added by @nuttela). If you used the same thing in python this might work without modmult(a,b).

1 Like

I had taken care of that @ayushomi007 previously. Please see that code…

https://www.codechef.com/viewsolution/24790923

I have used a similar approach and gotten AC. Refer please.
There was no need to use that power function.