Mysterious sequence(MYSARA)

It’s my solution link during the contest
can someone tell me the mistake with my code…
Really can’t understand my mistake now also…will be thankful to you for my help with the mistake,
https://www.codechef.com/viewsolution/30682214

Firstly, pow is only used for floating point calculations. Instead we precompute the powers of 2. ans needs to be a long long int otherwise it causes an overflow. you also need to take mod everytime you multiply ans by x because ans would be somewhere around 10^{18} and multiplying it with a large number would even overflow a long long int.
Corrected code: https://www.codechef.com/viewsolution/30688436

love you man :heart_eyes: ,thanks alot :grin:

what is wrong with my code?
#include <bits/stdc++.h>

const unsigned long long M = 1000000007;

using namespace std;

long long int power(int x, unsigned int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// // for getting input from input.txt
// freopen(“input.txt”, “r”, stdin);
// // for writing output to output.txt
// freopen(“output.txt”, “w”, stdout);
// #endif

long long t;
cin >> t;
while(t--){
    long long n;
    cin >> n;
    long long arr[n];
    for(int i = 0; i < n; ++i) cin >> arr[i];
    long long count = 1;
    int flag = 0;
    for(int i = 0 ; i < n-1 ; ++i){
        int d = __builtin_popcount(arr[i] & arr[i+1]);
        count *= (power(2,d) % M);
        if(arr[i] != (arr[i]&arr[i+1])) flag = 1;
        
    } 
    if(flag) cout << "0\n" ;
   else  cout << count << "\n";
}
return 0;

}

Here’s your correct solution—added line 41 :blush:
https://www.codechef.com/viewsolution/30712649

in line ---> count *= (power(2,d) % M);
value of (power(2,d) % M) goes maximum upto M
and you’re looping through it ‘n’ times, so count gets multiplied by M ‘n’ times,
its like doing count = M^n, which is very very large no. and it overflows your long long count

2 Likes