Chef and walk

the following code fails for very large numbers .can anyone help me in fixing it??

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long

/*ll pow(ll a,ll b)
{
if(b==0)
return 1ll;
if(b==1)
return a;

ll ans=pow(a,b/2);
if(b%2==0)
return (ans*ans)%mod;
else
return (((ans*ans)%mod)*a)%mod;

}*/

int main()
{
ll t;cin>>t;
while(t–)
{
ll n;ll k;cin>>n>>k;
k–;
ll ans;

    if(k%2==0)
    {
        ll ele=(n+k/2)%mod;
        ans=(ele*ele)%mod+(k/2)%mod;
    }
    
    else
    {
        ll ele=(n+1+k/2)%mod;
        ans=(ele*ele)%mod-(k/2 +1)%mod;
    }
        
        cout<<ans%mod<<endl;
}
return 0;

}

see this :slight_smile:

1 Like

your code is giving wrong answer for 0 2.

add one base condition

if(n==0)
     cout<<(k*(k-1)%mod;
1 Like

Format your code.

1 Like

Right. Even i am typing the same :slight_smile: