Here x has only one value i.e. 8
Sorry you are right ,here answer will be 0. Thanks for pointing it out.
What is the bitwise or of 7 and 8?
7 is 0111 and 8 is 1000
Thanks @ketan_mehta . Due to missing of this condition my code was giving wrong answer on array{7,8} which was pointed out by @everule1.
Can someone help me finding an error in this?
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
inline int mul(int x, int y){
return (x * 1ll* y)%mod;
}
inline int bitcount( int a){
int count =0;
while(a){
if(a&1) count++;
a>>=1;
}
return count;
}
inline int binpow(int x, int y){
int z=1;
while(y){
if(y&1) z=mul(z,x);
x=mul(x,x);
y>>=1;
}
return z;
}
int main(){
int t; cin>>t;
for(int i=0; i<t; i++){
int n; cin>>n;
int a; int b;
cin>>a;
int ansbit=0;
bool check=1;
for (int i=1; i<n; i++){
cin>>b;
if(check){
if((a&b)!=a) {
check=0;
break;
}
ansbit+=bitcount(a);
a=b;
}
}
if(check)
cout<<binpow(2,ansbit)<<endl;
else cout<<0<<endl;
}
}
Write cin.ignore(); before the break. If you break early, you haven’t inputted some values which you’ll input in the next test case erraneously.
cin.ignore(); takes the reader to the next line.
Also instead of bitcount(a) you can use __builtin_popcount(a) which is up to 14% faster than your implementation.
I think you counting extra bits. You should count bits of ( a & b) . But you are counting bit of a.
My approach was B[0] =A[0]. We know that B[i] = B[i-1] V A[i]. If B[i-1][j]==0 then A[i][j]=b[i][j] and we have no option. If B[i-1][j]==1 then B[i][j]=1 no whatter the value of A[i][j]. Hence we have two options. using product rule we can say that A[i] can have 2^(number of set b its in B[i-1]). The only case where the array will not be defined will be if B[i-1][j]==1 and B[i][j]==0, since that is not possible using or operation. In that case output 0. else output product of number of possible A[i] for each i from 1 to n. But I am getting wrong answer, below is my code, can someone please tell me the error in my logic/code. Help will be appreciated!
#include<iostream>
#include<vector>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long int
ll MOD = 1000000007;
long long int fast_exp(long long int base, long long int exp) {
long long int res = 1;
while(exp>0) {
if(exp&1) {
res = (res*base)%MOD;
}
base = (base*base)%MOD;
exp = exp>>1;
}
return res%MOD;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
ll n, temp;
cin>>n;
vector<bitset<32>> b(n);
for(ll i=0; i<n; i++){
cin>>temp;
b[i] = temp;
}
ll count = 1;
bool flag = true;
for(ll i=1; i<n; i++){
count = (count*fast_exp(2, b[i-1].count()))%MOD;
if((b[i-1]&(~b[i]))!=0) {
cout<<0<<'\n';
flag = false;
continue;
}
}
if(flag) cout<<count<<"\n";
}
return 0;
}
You’ve written continue; instead of break;. continue; just goes to the next value in the loop, whereas break; ends the loop
Can Someone please help me out on this.
I’m not sure if my approach is wrong or i did some mistake in writing code. The Code seems clear and understandable for everyone. So please give it a look .
https://www.codechef.com/viewsolution/30665859
Thanks for the help. I feel completely dumb now.
plz tell what is wrong with my code
Why answer is 0 when array is sorted.
and why b[i] & b[i+1] != b[i]
Why ans is 0 when array is sorted
and what’s with this condition b[i] & b[i+1] != b[i]
consider b1=7 and b2=8;
binary representation of 7 is 0111
binary representation of 8 is 1000
we need an element a2 such that b1|a2=b2
b1-0 1 1 1
a2-? ? ? ?
---------------or operation
b2-1 0 0 0
--------------
if we observe the example,we can conlcude that
there is no element a2 which satisfies b1|a2=b2;
so answer must be 0;
But why do we need to check set bits at same position of a and OR(a,b).
if there is 1 in a there will surely be 1 in OR(a,b).That means we only need to check the no. of 1’s in a and for each 1 in a there will be 2 possibilities (i.e, either 0 or 1).
Hi
Can u please help me
please tell me what is wrong with my code for MYSARA problem:
https://www.codechef.com/viewsolution/30719423
Try the test case
2
3
1 4 3
2
1 3