@all plz point out wat's wrong with this code. works for small numbers but fails for large number's
#include<conio.h> #include<iostream> using namespace std; unsigned int find_nCp(unsigned int n,unsigned int p) { unsigned int j=p; unsigned int num=1,den=1; for(;j;j--) num*=n--; for(;p;p--) den*=p; return (num/den); } unsigned int num(unsigned int n,unsigned int k) { if(n==1 || k==0) return 1; return find_nCp(n,k) + num(n-2,(n-1)/2); } main() { uint64_t n,p,test=1; unsigned int t,i,sum,j; scanf("%u",&t); while(t--) { n=test<<t; scanf("%llu",&n); if(n<4) cout<<0<<endl; else { for(p=1,i=0;p<=n;p=p<<1,i++); i=i-2; if(i%2==0) { i--; } cout<<num(i,i/2)<<endl; getch(); } } return 0; } On Tue, Dec 27, 2011 at 10:33 AM, kumar rajat <thebossku...@gmail.com>wrote: > *I am not able to follow.. > > On Tue, Dec 27, 2011 at 11:47 PM, kumar rajat <thebossku...@gmail.com> > wrote: > > @Lucifier: > > Thanks a lot.. > > But,I am able to follow the code that u posted for Non-Decreasing Digits. > > Can u jus explain ur algo instead of givin the code directly? > > Thanks once again. > > > > On Tue, Dec 27, 2011 at 10:44 PM, Lucifer <sourabhd2...@gmail.com> > wrote: > >> @ Special Nos.. > >> Well the actual logic would be : > >> int count = 0;for ( int i = 2; i <= LOGbase2(N) ; i+=2) count+= > >> [ (i-1) C (i/2) ] ; // here xCy is nothing but the no. of ways y items > >> can be selected from a collection of x items. > >> Hence, the working code: > >> int totalCount = 0; > >> int interCnt = 1; > >> > >> if ( LOGbase2(N) > 1) > >> { > >> totalCount = 1; // for LOGbase2(N) = 2... > >> > >> for ( int i = 4; i <= LOGbase2(N) ; i+=2) > >> { > >> interCnt = (i-1)*(i-2) * interCnt / ( i/2 * (i/2 -1)); > >> totalCount += interCnt; > >> > >> } > >> printf("%d", totalCount); > >> } > >> else > >> printf("%d", 0); > >> On Dec 27, 7:38 pm, Tasvinder Singh <tasvinde...@gmail.com> wrote: > >>> I think the first problem involves some mathematics... > >>> In this we fix the first bit and if the remaining no. of bits are odd > then > >>> we calculate the no. as follows > >>> > >>> If we have 2^4=16 then total bits 5 so we do not include this. > >>> Total no. of bits in one less than the given no. (in this eg. 15) is 4. > >>> Fix first bit, no. of bits remaining = 3 > >>> Now let 2 bits are 0 and one bit 1. We have total 3!/(2!*1!) = 3 > >>> combinations. > >>> > >>> Now go for next even no which is 2 in this case again do the same > >>> Fix first bit, no. of bits remaining = 1 > >>> Now let 1 bit is 0. We have total 1!/(0!*1!) = 1 combinations. > >>> > >>> Next even 0. stop here. > >>> You can go for this by starting from 2 till no. is less than given N > >>> > >>> > >>> > >>> > >>> > >>> > >>> > >>> > >>> > >>> On Tue, Dec 27, 2011 at 7:28 PM, kumar rajat <thebossku...@gmail.com> > wrote: > >>> > Hi > >>> > I have jus started to learn DP and I have coded the standard > >>> > algorithms(LCS,etc). > >>> > I have been trying these problems in SPOJ: > >>> > >>> >http://www.spoj.pl/problems/NOVICE63/ > >>> >http://www.spoj.pl/problems/NY10E/ > >>> > >>> > I understand these may be solved elegantly using DP,but I dont get to > >>> > code the same. > >>> > Can any1 help me how to solve these types of problems using DP? > >>> > Any help regarding the same will be greatly appreciated. > >>> > >>> > -- > >>> > You received this message because you are subscribed to the Google > Groups > >>> > "Algorithm Geeks" group. > >>> > To post to this group, send email to algogeeks@googlegroups.com. > >>> > To unsubscribe from this group, send email to > >>> > algogeeks+unsubscr...@googlegroups.com. > >>> > For more options, visit this group at > >>> >http://groups.google.com/group/algogeeks?hl=en. > >>> > >>> -- > >>> Tasvinder Singh > >>> B.Tech Final Year, > >>> Department of Computer Engineering, > >>> Malaviya National Institute Of Technology, Jaipur. > >>> Contact: +91-9460627331 > >> > >> -- > >> You received this message because you are subscribed to the Google > Groups "Algorithm Geeks" group. > >> To post to this group, send email to algogeeks@googlegroups.com. > >> To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > >> For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.