Hdu 5312 Sequence (mathematical thinking)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.