Doubt regarding long/unsigned long ( May Long- CHANDF)

I changed the type from unsigned long long to long long and got AC for 2nd task 40 marks, please tell why unsigned long long ddn’t work ?

codes:
https://www.codechef.com/viewsolution/32719220
https://www.codechef.com/viewsolution/32747103

Same thing happened to me

I was stuck for 2 days in debugging this :stuck_out_tongue:

Apparently the same thing happened to me, though I didn’t know until you posted this question. It’s pretty frustrating to know that was the mistake though…

@kevin1499 I still can’t understand why it is a mistake

Happened to me also. Wasn’t so sure of approach so didn’t experimented syntax.
Got stuck at it and hence was not able to build approach for 100pts.

hey this occured to me as well but i after adding these code block
just expermenting i got AC for both subtasks without unsigned
i guess its able to prevent overflow for some case
https://www.codechef.com/viewsolution/32983172

 if((x&c)==0)
        {
            // printf("adasdasdasdda\n") ;
            for(i=0; i<=63; i++)
            {
                if(a[i]==1)
                {
                    for(j=i+1; j<=63; j++)
                    {
                        if(r[j]==1)
                        {
                            z1=1 ;
                            r[j]=0 ;
                            for(int k=j-1; k>=0; k--)
                            {
                                r[k]=1 ;
                            }
                            break ;
                        }
                    }
                    break ;
                }
            }
        }
        if((z1==0)&&((x&c)==0))
            return 0 ;     // maybe differnt for l!=0
        z1=0 ;
        c=0 ;
        for(i=0; i<=63; i++)
        {
            if(r[i]==1)
                c+=p2[i] ;
        }

        if((y&c)==0)
        {
            // printf("adasdasdasdda\n") ;
            for(i=0; i<=63; i++)
            {
                if(b[i]==1)
                {
                    for(j=i+1; j<=63; j++)
                    {
                        if(r[j]==1)
                        {
                            z1=1 ;
                            r[j]=0 ;
                            for(int k=j-1; k>=0; k--)
                            {
                                r[k]=1 ;
                            }
                            break ;
                        }
                    }
                    break ;
                }
            }
        }
        if((z1==0)&&((y&c)==0))
            return 0 ;   

So I don’t really know why it happens but apparently there’s an overflow involved. In the editorial’s post the editoralist gave the example X=Y= 10^{11} and L=0,R=X-1 this caused an overflow and the answers were different. The editoralist said he will try to find a reasonable explanation for why this happens!

1 Like

But isn’t the range of unsigned should be larger then there signed one’s??

the function value was constrained. to 2^62 which is less than range of long long int.

No my point was if long long int can accommodate some positive value then definitely unsigned long long int can accommodate to as it has bigger range then it.

1 Like

but max value of signed long long int> max value of function
so why unsigned led to AC and not signed

I guess you have misread something. In above given question and his code accepted sol. was with signed one only. And WA on unsigned.

1 Like

O my bad i read only the title and what happened to me was i was getting ac on unsigned and not on signed . and it is her