Hey i dont know why my code is giving WA for can any one help me out plz

#include<bits/stdc++.h>
//#include
//#include
//#include
//#include
//#include
//#include
//#include

using namespace std;
#define max(a, b) (a < b ? b : a)
#define min(a, b) ((a > b) ? b : a)

#define FOR(a, c) for (int(a) = 0; (a) < ©; (a)++)
#define FORL(a, b, c) for (int(a) = (b); (a) <= ©; (a)++)
#define FORR(a, b, c) for (int(a) = (b); (a) >= ©; (a)–)
#define INF 1000000000000000003
#define ip(a,n) for(int i=0;i<n;i++) cin>>a[i]; //self
#define op(a,n) for(int i=0;i<n;i++) cout<<a[i]<<’ ';cout<<endl;
typedef long long int ll;
typedef vector vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair

#define ll long long int
const int mod = 1e9 + 7;

void solve() {
// arary rotation
ll i, j, k, l, m, n, p, x, y, z;
cin >> n;
long long int a[n];
ip(a, n);
ll ans = 0;
ans = accumulate(a, a + n, 0);
// for (int i = 0; i < n; ++i)
// {
// /* code /
// ans += a[i];
// ans = (ans + mod ) % mod;
// }
// cout << ans ;
ans %= mod;
ans += mod;
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
/
code */
cin >> x;
ans = ans * 2;
ans %= mod;
cout << ans << endl;
}
// cout << mod;

}

int main() {

#ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

// freopen("inp_corner_test_case.txt", "r", stdin);
// freopen("out_corner_test_case.txt", "w", stdout);

#endif

// int t;
// cin >> t;
// while (t--) {
solve();

// }


return 0;

}

see in array -ve values also there so instead of just sum = sum%mod , u have to this

sum = (sum + mod)%mod

2 Likes

Even in my case i posted 3 submitted three problems on codechef which had been successfully compiled on sublime text but giving wrong answer on codechef. I could have scored 400 points but could only get 100 points. My rting would fall now… :disappointed::disappointed:

haa thank you but still doubt

i am first calculating sum then doing (sum % mod) and then (sum + mod) value will make it positive na …

or sum =(sum + mod ) % mod
both will be same na

Completely forgot about negative values :frowning_face:

1 Like

Consider if sum = -1000000000
then by your method sum % MOD = 7 then sum + MOD = 1000000014. Finally sum will be 1000000014.
But if you add mod first then sum + MOD = 7 then sum % MOD will also be 7 which is right.
Please correct me if i am wrong.

thank you buddy :smiley: but
if sum = -1000000000
then by your method sum % MOD = -1e9

and before printing the sum i am taking mod again so it dosent matter na
see…
// consider ans as sum

ans = accumulate(a, a + n, 0);
ans %= mod;
ans += mod;
int q;
cin >> q;
for (int i = 0; i < q; i++)
{

cin >> x;
ans = ans * 2;
ans %= mod;
cout << ans << endl;
} // it gives WA

but if i uses for loop and doing ans = (ans + mod )% mod for calculating ans then gives AC
such as
for (int i = 0; i < n; ++i)
{

ans += a[i];
ans = (ans + mod ) % mod;
}
// gives AC but dont know why

after adding all the elements you are doing ( ans%=mod) and ans+=mod
let (ans%=mod) = 1000 , then ans+=mod is ( mod+1000) which is greater than what is expected
so you need to perform it as (ans+mod)%mod
while solving queries and printing answer you are just taking mod of ans, which may give negative values , to make it positive add with mod and then perform modulo
i.e., (ans+mod)%mod
ex: if ans=-10 and let mod =20
by directly performing mod operation (-10%20) would give -10
but we need 10, to get that we do (-10+20)%20 ==> 10%20 ==> 10

thank you :smiley: