Test instructions: The nth item of a sequence is 3*n* (n-1) +1, and N>=1, which now gives a positive integer m, asks at least how many of the numbers in the sequence are composed?
Idea: First, the 1th item of the sequence is 1, so any number can be constituted. But how much should it be at least? Deformation of the formula,6* (n (n-1)/2) +1, see the number of triangles N (n-1)/2, then it should be 6* (any natural number) +x=m is right, because up to 3 triangles can be composed of any natural number.
May I try m%6? Does this try to find x? Because any natural number is composed of up to 3, if it is k, then should be x>=k, do not forget there is a +1 item. X-k that part, it can only be done by the one.
There is a premise, m%6=0 how to do? It can't be made up of 0? That should be at least 1, so (m-1)%6 can be guaranteed not to be 0. Try M=13, then x= (13-1)%6+1=1, so can it really be composed of a number in a sequence? The order is 1,7,19 ... There seems to be no ... Tragedy! Should be 7+1+1+1+1+1+1 is, those 6 1 is to fill up. Similarly x=2 also need to ensure that there are two numbers in the sequence can be composed of M, otherwise x+=6 is the answer. And the 3 and above is not necessary, why I do not know ...
1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn=10100;7unordered_map<int,int>Mapp;8vector<int>seq;9 voidpre_cal ()Ten { One intt=1; A for(intI=1; i<20000&&t<=1000000000; i++) - { -t=3*i* (I-1)+1; themapp[t]=1; - Seq.push_back (t); - } - } + - BOOLCalintm) + { A intq=0, P=seq.size ()-1; at while(q<=p) - { - intt=seq[q]+Seq[p]; - if(t==m)return 1; - if(t>m) p--; - Elseq++; in } - return 0; to } + - intMain () the { * //freopen ("Input.txt", "R", stdin); $ intT, M;Panax Notoginseng pre_cal (); -Cin>>T; the while(t--) + { Ascanf"%d",&m); the intk= (M-1)%6+1;//K is not more than 6. But the answer could be more than 6. + - if(k>=3) printf ("%d\n", k); $ Else if(k==1)//Special Award $ { - if(Mapp[m]) printf ("1\n"); - Elseprintf"%d\n", k+=6); the } - Else if(k==2)//Special AwardWuyi { the if(Cal (M)) printf ("2\n"); - Elseprintf"%d\n", k+=6); Wu } - } About return 0; $}
AC Code
HDU 5312 Sequence (law problem)