how do you do that
Thanks a lot Crap.
This is a part of your code
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
before printing add :
if ( ans < 0 ) {
ans += MOD;
}
because mod can give negative values in C++ unlike python
But it still couldn’t get ac. Now I’m just more curious what could the mistake be.Thanks though
We cannot use int for n,k cuz both of them <=10^9. int cannot store such large values and it will result in an overflow.
First, ans= (ans-abs(n-x)), can result in ans being negative, mod of which will give you a negative answer.
if(ans<0)
ans+=MOD;
else
ans%=MOD;
cout<<ans<<endl;
Secondly, declare ans as a ll and not unsigned ll.
What is the mistake in my code? getting WA … somebody help.
I have found the solution like … difference between two 0 appears at an AP of common difference 2 starting from 1 like 1,3,5…
or we can say the position of 0 to be 1,1+3,1+3+5…
and k element appears for the first time before k+1th 0…
and after that, it appears twice between two 0…
I wrote the solution based on this fact… first I calculated the 0 positions then subtract or add the k depend upon which side it will lie
Here is my code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define m 1000000007
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;cin>>t;
while(t–)
{
ll a,b;cin>>a>>b;a=a%m;b=b%m;
ll pos=a%m+1;
if(a==0)
{
ll a2=b%m-1;
ll h=((a2)(a2+1))%m;
cout<<h%m;
}
else
{
pos+=(b%m-1)/2;ll a1=pos%m-1;
pos=((a1)(a1+1))%m;
if((b-1)%2==0)
cout<<((pos)-a)%m;
else
cout<<((pos)+a)%m;
}
cout<<"\n";
}
}
Please try to provide link to the solution, then it would be easier for everybody to provide some help!
but ‘int’ can store values in the range 0 to 10^9. That is why i have the doubt.
Range of int is from -2,147,483,648 to 2,147,483,647
My bad , i thought the range of int was -32768 to +32767. But when i tried declaring n,k as int I’m getting WA. Maybe it’s because of the multiplications involved like ans=(n*n)+… which is causing overflow.
I guess this can help you.
@aquib_zaman you’re right.
When you do
long long ans = (n*n)%mod
n*n
is done first. Since n is an int
it overflows. You need to either explicitly typecast n into a long long
or declare n as a long long
Here is your solution with an AC, I modified the part were it would become negative so just added a check for that and that was it.
PS: If you still have any doubt codechef community is always there
Got it…
Thanks for the help…
can somebody help with my code. can’t find where i am wrong
https://www.codechef.com/viewsolution/32448895
such a wonderful question and editorial. sir your questions inspire many!
i spend a lot of time on this question and derived a formula and here is my code
//Waqt Ha Badal Jayega//
#include <bits/stdc++.h>
#include <stdint.h>
#define mod 1000000007
#define ull unsigned ll
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define pi 3.1415926535897932
using namespace std;
/-------------------------------------------------------------/
//for (a*b)%mod
uint64_t mulmod(uint64_t a, uint64_t b, uint64_t m)
{
long double x;
uint64_t c;
int64_t r;
if (a >= m) a %= m;
if (b >= m) b %= m;
x = a;
c = x * b / m;
r = (int64_t)(a * b - c * m) % (int64_t)m;
return r < 0 ? r + m : r;
}
/-------------------------------------------------------------/
ull binpowmod(ull a, ull b, ull m)
{
ull res = 1;
while (b > 0)
{
if (b & 1)
{
res = ((res % m) * (a % m)) % m;
}
a = ((a % m) * (a % m)) % m;
b >>= 1;
}
return res;
}
/-------------------------------------------------------------/
ll countsetbits(ll n)
{
ll count = 0;
while (n > 0)
{
n = n & (n - 1);
count++;
}
return count;
}
/-------------------------------------------------------------/
ll binpow(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
{
res *= a;
}
a *= a;
b >>= 1;
}
return res;
}
/-------------------------------------------------------------/
ll binarysearch(ll l, ll r, ll a[], ll x)
{
while (l <= r)
{
ll mid = l + ((r - l) / 2);
if (a[mid] == x )
{
return mid ;
}
if (a[mid] < x)
{
l = mid + 1;
}
else
{
r = mid - 1;
}
}
return -1;
}
/--------------------------------------------------------------/
//Adjacency list
/*vectoradj[100005];
ll vis[100005]; //how many i-th node have been visited
void dfs(ll node)
{
vis[node] = 1;
for (ll child : adj[node])
{
if (!vis[child])
{
dfs(child);
}
}
}
/
/--------------------------------------------------------------/
/ Upper bound of x gives numbers less than or equal to x */
// Bad day ! Luck matters > Stop Judging
/-------------------------------------------------------------/
int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll t;
cin >> t;
while (t--)
{
ll n,k;
cin >> n>>k;
if(n==0)
{
cout<<k*k-k<<"\n";
}
else
{
cout<<n*n+(k/2)*(2*n)+(k-(k/2)-1)*(k-(k/2))<<"\n";
}
}
return 0;
}
i dont know why its giving WA and then i changed it a little i got AC but why is this wrong .Can you please answer
@rishup_nitdgp
bro u have to analyse the pattern ; it took hours for me to find the correct pattern.