码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5312 Sequence(数学推导+线性探查(两数相加版))

时间:2015-09-10 21:00:37      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
Today, Soda has learned a sequence whose n-th (n≥1) item is 3n(n−1)+1. Now he wants to know if an integer m can be represented as the sum of some items of that sequence. If possible, what are the minimum items needed?

For example, 22=19+1+1+1=7+7+7+1.

 

 

Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤104), indicating the number of test cases. For each test case:

Theres a line containing an integer m (1≤m≤109).

 

 
Output
For each test case, output −1 if m cannot be represented as the sum of some items of that sequence, otherwise output the minimum items needed.

 

 
Sample Input
10
1
2
3
4
5
6
7
8
22
10

 

 

 

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

 

 

 

Source
 

 对于这种题,首先一开始就要对给出的公式进行研究,从这里入手是正道。

分析公式 3n(n1)+1 ,若给出一个数n,假设要k个3n(n1)+1加起来得到n,即 3n(n-1)k+k=n,注意到3n(n-1)k是6的倍数,那么求的是满足(n-k)%6==0的最小的k。还有就是要注意到1、2要特判,即k要从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 namespace std;
10 #define N 20000
11 int num[N];
12 int n;
13 void init(){
14     for(int i=1;i<N;i++){
15         num[i]=3*i*(i-1)+1;
16     }
17 }
18 bool check1(){
19     for(int i=1;i<N;i++){
20         if(num[i]==n)
21           return true;
22     }
23     return false;
24 }
25 bool check2(){
26     int j=N-1;
27     for(int i=1;i<N;i++){
28         while(num[i]+num[j]>n && j>0) j--;
29         if(num[i]+num[j]==n && j>0){
30             return true;
31         }
32     }
33     return false;
34 }
35 int main()
36 {
37     init();
38     int t;
39     scanf("%d",&t);
40     while(t--){
41         
42         scanf("%d",&n);
43         if(check1()){
44             printf("1\n");
45         }
46         else if(check2()){
47             printf("2\n");
48         }
49         else{
50             for(int i=3;i<10;i++){
51                 if((n-i)%6==0){
52                     printf("%d\n",i);
53                     break;
54                 }
55             }
56         }
57     }
58     return 0;
59 }
View Code

 

hdu 5312 Sequence(数学推导+线性探查(两数相加版))

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4798947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!