Test instructions: The general formula for 3*i* (i-1) +1 (n>=1) in the sequence of each number can be used several times, the minimum number required to form a given n;
Ideas:
The number required to construct n is x, then n=3*1* (1-1) +3*2* (2-1) +...+3*x* (x-1) +x; At that point, there was no further work;
Then n=6* (Sigma (i* (i-1)/2) +x (i* (i-1) is even);
That is to say, n%6 is the result, when n%6=1,2 to find out whether the number of satisfied conditions can be found;
Binary search: (manual two points) (STL binary)
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intT,n,m,mark;intnum[500010];voidPre () { for(intI=1;i<20000; i++) {Num[i]=3*i* (I-1)+1; }}/*int Seek (int x) {int k=lower_bound (num,num+20000,x)-num; return num[k]==x;}*/intSeekintx) { intL=0, r=20000; while(l<=r) {intMid= (L+R)/2; if(num[mid]==x)return 1; Else if(num[mid]<x) l=mid+1; Else if(num[mid]>x) r=mid-1; } return 0;}intMain () {inti,j,k,temp; Pre (); while(SCANF ("%d", &t)! =EOF) { while(t--) {scanf ("%d",&N); Temp= (n1)%6+1; Mark=0; if(temp>2) printf ("%d\n", temp); Else{ if(temp==1){ if(Seek (n)) printf ("1\n"); Elseprintf"7\n"); Continue; } Else{ for(i=1; num[i]*2<=n;i++){ if(Seek (nNum[i])) {Mark=1; Break; } } if(Mark) printf ("2\n"); Elseprintf"8\n"); } } } } return 0;}
Map Map in STL:
#include <cstdio>#include<cstring>#include<map>#include<algorithm>using namespacestd;intT,n,m,mark;intnum[500010];map<int,int>mm;voidPre () { for(intI=1;i<20000; i++) {Num[i]=3*i* (I-1)+1; Mm[num[i]]=i; }}intMain () {inti,j,k,temp; Pre (); while(SCANF ("%d", &t)! =EOF) { while(t--) {scanf ("%d",&N); Temp= (n1)%6+1; Mark=0; if(temp>2) printf ("%d\n", temp); Else{ if(temp==1){ if(Mm.count (n)) printf ("1\n"); Elseprintf"7\n"); Continue; } Else{ for(i=1; num[i]*2<=n;i++){ if(Mm.count (nNum[i])) {Mark=1; Break; } } if(Mark) printf ("2\n"); Elseprintf"8\n"); } } } } return 0;}
Hdu 5312 Sequence (mathematical thinking)