WA in "CHFING" of June challenge 2019, please help

QUESTION:-

SOLUTION:-
https://www.codechef.com/viewsolution/33546545
APPROACH:-
i took a arithmatic progression of first term = k-n and difference=1-n
then I found out the nth term which is greater than 0.
Then i took the sum of AP up to that term using sum=((first+last)*n)/2
PLZ HELP!!

#include <bits/stdc++.h>
 
#define int long long
#define mod 1000000007
 
using namespace std;
 
int32_t main()
{
    int t;
    cin >> t;
 
    while(t--)
    {
        int n,k;
        cin >> n >> k;
 
        int total = (k-1)/(n-1);
        int first = k-1;
        int last = first - total*(n-1);
 
        int ans = first + last;
        total++;
 
        if(ans%2 == 0)
            ans /= 2;
        else
            total /= 2;
 
        ans = ((ans%mod) * (total%mod))%mod;
 
        cout << ans << endl;
    }
} 

Please Like

1 Like