Can anyone find what’s wrong in this code I tried for every possible test case but after submission it gives WA for large numbers
#define ll long long
using namespace std;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,k;
ll mod=1000000007;
cin>>n>>k;
ll a=(ll)floor((float)k/2);
ll b=(ll)ceil((float)k/2);
ll ans=(((n*n)%mod+(2*a*n)%mod)%mod+((b-1)*b)%mod)%mod;
if(n==0)
ans=(k*(k-1))%mod;
cout<<ans<<endl;
}
}
Type casting a 64 bit integer into a float can cause problems. Use
a=k/2;b=(k+1)/2;
Generally, never use floating point arithmetic when integer answers are expected.
Integer division of a/b in C++ is equivalent to floor(a/b)
Thanks …
now it’s correct
https://www.codechef.com/viewsolution/32363169
can you please help me with this code
it got wa.
I don’t know about the rest, but you Cannot divide by 2 directly when you are doing modular arithmetic. This line ans1/=2
.
thank you .
should i use modular multiplicative inverse?
Probably. If you still get wrong answer I’ll check the rest of the code.
still getting wrong answer!
n is an int
up to 10^9 so ans=(n*n)%p;
will overflow. Remember when you multiply 2 int
s the answer is still an int, even if you assign it to a long long
or take mod p afterwards. I would recommend rewriting the code and please use something like const int p= 1000000007
and take mod p. Your code is very ugly, and it’s hard to debug ugly codes.
thank you very much .
changed int to long long and it got accepted.