Not so easy question

can u detect the error
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
int arr[]={1,3,5,7,11,13,17,19},i,j;
while(t–)
{
long long int ans=0;
long long int n;
cin>>n;
long long int ss=(1<<8)-1;

         for(i=0;i<=ss;i++)
         {
            long long int denom=1;
            long long int sb=__builtin_popcount(i);

            for(j=0;j<=7;j++)
            {
                if(i&(1<<j))
                {
                denom=denom*arr[j];
                }
            }
            if(sb&1)
            {
                ans+=n/denom;
            }
            else
            {
                ans-=n/denom;
            }
         }

cout<<ans<<endl;
}

return 0;
}

sir 1 is coprime
2 is prime
remove 1 from array
insert 2 in primes array

i wrote that wrong and corrected it but still i am getting run time error…

i tried but still this is giving runtime error

@saiKDr Can you send me your updated code and tell me what problem you are facing .

@amankumarkeshu
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
int arr[]={2,3,5,7,11,13,17,19},i,j;
while(t–)
{
long long int ans=0;
long long int n;
cin>>n;
long long int ss=(1<<8)-1;

         for(i=0;i<=ss;i++)
         {
            long long int denom=1;
            long long int sb=__builtin_popcount(i);

            for(j=0;j<=7;j++)
            {
                if(i&(1<<j))
                {
                denom=denom*arr[i];
                }
            }
            if(sb&1)
            {
                ans+=n/denom;
            }
            else
            {
                ans-=n/denom;
            }
         }

cout<<ans<<endl;
}

return 0;
}

i changed it
denom=denom*arr[j];
but still its giving me runtime error

@chhavibansal It should be denom =denom* arr[j] . You have used i.

@amankumarkeshu i said i corrected it and checked but still it was not working.
it is giving run time error.

@chhavibansal the code which u have sent is working for 1 test case only for another test case it is giving wrong answer.

@saiKDr

@chhavibansal It is not the right answer. You are supposed to use bit masking in this question.

yes sir now i know that is why i have provided the video where this q is getting solved

@chhavibansal Okay no problem. I didn’t knew that you are a TA .

thank u both of u i got the issue as i was starting my outer loop with 0 instead of one .

1 Like

@saiKDr okay great .