RECNDNUM - Editorial

Here is my Code for RECNDNUM. It passes all the sample test cases, then where it is going wrong…

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

I tried many random test case generators and compared my code with the Setter’s solution. Outputs are identical in all cases. I don’t know what’s wrong. Any one try and find a test case?

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

Desperate af T_T

heyy geek, please have look on my code.
I used the concept of sum of AP and the first time we find n is n*n

#include
#include
#include <bits/stdc++.h>
#include
using namespace std;
#define ll long long int
int mod=1e9+7;

ll fxp(ll a,ll b,ll m)
{
if(b==2)
return (aa)%m;
if(b%2==0)
return fxp(a
a,b/2,m);
return fxp(a,b-1,m)*a;
}

void swap(ll &a,ll &b){ ll t=a; a=b; b=t;}

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
ll n,k;
cin>>n>>k;
int res=0;
res=nn;
k=k-1;
if(n==0 && k>0)
{
if(k%2==0)
res+=(k/2)
(4+(k-1)2);
else
res+=((k
(4+(k-1)*2))/2);

    }
    else
    if(k%2==0 && k!=0)
    {
        res+=(n*2)*k/2;
        ll x=k/2;
        res+=(x*(4+(x-1)*2))/2;
    }
    else if(k%2!=0 && k!=0)
    {
        res+=(n*2)*(k/2)+n*2;
        ll x=k/2;
        res+=(x/2)*(4+(x-1)*2);
    }
    cout<<(res%mod)<<endl;
}
return 0;

}

@rishup_nitdgp why this solution (in which I used long long) is giving wrong answer but in setter’s solution which used int ans = n * n which is not overflowing whereas in my solution even though I used long long and took modulo at every step then also it resulted in WA.
How come??
My same logic worked in pypy3 so my logic was correct and it was an overflow error then how come when n could be as large as 10^9 and your solution is working?

The ans may be negative when you subtract k/2.

Can anyone tell me which test case will not be executed with this code : -

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long MOD = (long long)(1e9 + 7);
ll solve(ll t)
{
ll n,k,start;cin>>n>>k;
if(n==0)
start=1;
else if(n>0)start=n;
if(k==1)cout<<(n*n)%MOD;
else
{k–;
while(k>0)
{start++;
k-=2;
start%=MOD;
if(k<=0)break;
}
if(k<0)
cout<<(((start-1)*start)+n)%MOD;
else
cout<<(((start-1)*start)+start+(start-n))%MOD;
}

if(t>0)cout<<"\n";

}

int32_t main()
{
#ifdef ONPC
freopen(“input.txt”, “r”, stdin);
#endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll t;cin>>t;while(t–)
{
solve(t);
}

return 0;

}

But it is working in pypy3.
The point is how the setter’s solution working even though it will definitely overflow for large values of n when ans = n^2.

#define int long long :neutral_face:

2 Likes

:expressionless: That’s nasty.
by the way still can you point where is my solution overflowing.

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

1 Like

Thanks for the help.
In python when doing modulo it always gives non negative value while in CPP that’s not the case.

couldn’t understand the editorial but understood your three lines very well and got a.c . Thank you! :slightly_smiling_face:

My answer was right…just did something else when N = 0, and which satisfied K = 10, but not after that. :pensive: :pensive: :pensive:

Dude, Sharing that random test case generator code would be of a great help !

from random import randint

testcases = 10

for _ in range(testcases):
    n = randint(1, 100)
    k = randint(1, 100)
    
    print(n, k)

Here you go!

Also, here’s the brute force solution which I used for testing (Only works for small values)

l = [0]

for i in range(1, 100):
    l.extend(list(range(1, i+1)) + list(range(i-1, -1, -1)))

d = {}

for i, j in enumerate(l):
    d[j] = d.get(j, []) + [i]

# No testcases, just input n and k
while True:
    n, k = map(int, input().split())

    print(d[n][k-1])

how do you do that

Thanks a lot Crap.

This is a part of your code :slight_smile:

 t = n + (k-1)//2
 ans = t*(2 + (t-1))
 ans -= n

Your code will pass when you take modulo at each and every step of your arithmetic
Try This :
on the top of the test case loop try using this
mod = int(1e9 + 7)
and use %mod whenever you do any arithmetic operation especially multiplication