First preprocess the number of layers and rows, then three answers, pay attention to the details
#include <cstdio>#defineINF 0x3f3f3f3fConst intmaxn=2000100; typedef __int64 LL;using namespacestd;inti; LL N,s,ans1,ans2,ans3; LL SUM[MAXN+Ten]; LL A[MAXN+Ten];voidsolve () {intflag=0; LL lb=0, ub=maxn,mid= (Lb+ub) >>1; while(ub-lb>1) {Mid= (Lb+ub) >>1; if(S>a[mid]) lb=mid; Else if(S<a[mid]) ub=mid; Else if(s==A[mid]) {Flag=1; Break; } } if(s<=a[mid]&&s>=a[mid-1]&&flag) ans1=mid; Else if(s<=a[lb]&&s>=a[lb-1]) ans1=lb; Else if(s<=a[ub]&&s>=a[ub-1]) ans1=UB; S-= (a[ans1-1]); //printf ("%i64d\n", s); //printf ("%i64d\n", ans1);flag=0; LB=1, ub=ans1,mid= (Lb+mid) >>1; while(ub-lb>1) {Mid= (Lb+ub) >>1; //printf ("%d%d\n", mid,sum[3]); if(s>Sum[mid]) lb=mid; Else if(s<Sum[mid]) UB=mid; Else if(s==Sum[mid]) {Flag=1; Break; } } //printf ("%i64d%i64d%i64d%i64d\n", sum[lb],sum[lb-1],s,lb); if(s<=sum[mid]&&s>=sum[mid-1]&&flag) { //printf ("1%i64d%i64d%i64d%i64d\n", sum[mid],sum[mid-1],s,mid);Ans2=mid; } Else if(s<=sum[lb]&&s>=sum[lb-1]){ //printf ("2%i64d%i64d%i64d%i64d\n", sum[lb],sum[lb-1],s,lb);Ans2=lb; } Else if(s<=sum[ub]&&s>=sum[ub-1]){ //printf ("3%i64d%i64d%i64d%i64d\n", sum[ub],sum[ub-1],s,ub);Ans2=UB; } Flag=0; LB=1, ub=ans2,mid= (Lb+ub) >>1; //printf ("%d\n", ans2); while(ub-lb>1) {Mid= (Lb+ub) >>1; if(s>sum[ans2-1]+mid) lb=mid; Else if(s<sum[ans2-1]+mid) ub=mid; Else if(s==mid+sum[ans2-1]) {flag=1; Break; } } if(s==mid+sum[ans2-1]&&flag) ans3=mid; Else if(s==lb+sum[ans2-1]) ans3=lb; Else if(s==ub+sum[ans2-1]) ans3=UB; printf ("%i64d%i64d%i64d\n", ANS1,ANS2,ANS3);}intMain () { for(intI=1; i<=maxn;i++) Sum[i]=sum[i-1]+i; for(intI=1; i<=maxn;i++) A[i]=sum[i]+a[i-1]; scanf ("%i64d",&N); while(n--) {scanf ("%i64d",&s); Solve (); } return 0;}
View Code
Hdu2466-shell Pyramid