Hdu 5312 Sequence (mathematical deduction + linear probing (two-digit addition))

Source: Internet
Author: User

problem Description
Today, Soda have learned a sequence whose n-th (n≥1 is 3n (n−1) +1ifas the sum of some items of that sequence. If possible, what is the minimum items needed?  1+1+1=7+7+7 +1.

Input
There is multiple test cases. The first line of input contains an integer T (1≤t≤104case: there's a Lin E containing an integer m (1≤m≤109).

Output
case, output−1if as the sum of some items of that sequence, otherwise output t He minimum items needed.

Sample Input
Ten 1 2 3 4 5 6 7 8  A Ten

Sample Output
1 2 3 4 5 6 1 2 4 4

Source Bestcoder 1st Anniversary ($)

For this kind of question, first of all, we should study the formula given, and start from here is the right path.

Analysis Formula3N(N−1)+1, if you give a number n, suppose you want K3N(N−1)+1 add up to get N, i.e. 3n (n-1) k+k=n, noting < Span id= "mathjax-span-16" class= "Mi" > < Span id= "mathjax-span-21" class= "mn" >3n (n-1) k is a multiple of 6, then the smallest k satisfying (n-k)%6==0 is asked. There is also to notice that 1, 2 to be special, that is, K to enumerate from 3

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6#include <cmath>7#include <stdlib.h>8#include <map>9 using namespacestd;Ten #defineN 20000 One intNum[n]; A intN; - voidinit () { -      for(intI=1; i<n;i++){ thenum[i]=3*i* (I-1)+1; -     } - } - BOOLCheck1 () { +      for(intI=1; i<n;i++){ -         if(num[i]==N) +           return true; A     } at     return false; - } - BOOLCheck2 () { -     inti=m-1; -      for(intI=1; i<n;i++){ -          while(Num[i]+num[j]>n && j>0) j--; in         if(Num[i]+num[j]==n && j>0){ -             return true; to         } +     } -     return false; the } * intMain () $ {Panax Notoginseng init (); -     intT; thescanf"%d",&t); +      while(t--){ A          thescanf"%d",&n); +         if(Check1 ()) { -printf"1\n"); $         } $         Else if(Check2 ()) { -printf"2\n"); -         } the         Else{ -              for(intI=3;i<Ten; i++){Wuyi                 if((n-i)%6==0){ theprintf"%d\n", i); -                      Break; Wu                 } -             } About         } $     } -     return 0; -}
View Code

Hdu 5312 Sequence (mathematical deduction + linear probing (two-digit addition))

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.