I found one mistake : If b < PD, you are returning 0. Why is that? Lets say you have 4 switches, PD = 16. If b = 15, the answer should be ON.
Other than that, there are many ways you can improve your code. 1. dont use double. It will remove accuracy in many places. 10^8 will come within 4 bytes (usually int suffices this. else use long int). 2. To calculate 2^n, you can use the left shift operator. "int PD = 1<<n" will put 2^n into PD. 3. To check if b gives reminder PD -1, you don't need to run a loop. You can do "if (b%PD == PD-1)" On 13 May 2010 10:47, Aamir Khan <[email protected]> wrote: > #include <cmath> > #include <cstdio> > #include <iostream> > using namespace std; > > #define _CRT_SECURE_NO_WARNINGS > #define For(i,a,b) for (long i(a),_b(b); i <= _b; ++i) > > char buf[1024*1024]; > > bool isEven(long a,long b) > { bool result=0; > if(b%2==0) > result=0; > else > { > double ad=0.00; > for(int j=0;j<=30;j++) > { > if(int(ad)==a) > { break;} > ad=ad+1.00; > } > long PD=long(pow(2.00,ad)); > if(b<PD) > result=0; > > else > { > for(long i=PD;i<=100000000;i+=PD) > { > if(b==i-1) > { > result=1; > break; > } > } > } > } > return result; > } > > int main() { > freopen("A-large.in", "rt", stdin); > freopen("output_Snapper_Chain.txt", "wt", stdout); > > gets(buf); > long size=atoi(buf); > > > For(test, 1, size) > { > // cout<<"No. of loops"<<test; > long N; > long K; > > scanf("%d%d", &N, &K); > // cout<<"N"<<N<<"K"<<K; > > bool res=isEven(N,K); > if(res) > printf("Case #%d: ON\n", test); > else > printf("Case #%d: OFF\n", test); > } > exit(0); > } > > > > --------------------------------------------------------------------------------------------------------------------------- > SNAPPER CHAIN: > Can anybody tell me the errors in the code.....i have done this > problem using the simple concept which i know....it is not giving > right output.... > > Brief explanation of code: > i have first converted the long taken from file into the double using > for loop..then using pow function i have found 2^N and then found > whether k==2^N-1; > > > _____ > Aamir > > -- > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<google-code%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
