您的位置:首页 > 产品设计 > UI/UE

HDU5312——数学——Sequence(未完成)

2015-07-26 20:54 465 查看
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.

[align=left]Input[/align]
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:

There's a line containing an integer m (1≤m≤109).

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

10
1
2
3
4
5
6
7
8
22
10

[align=left]Sample Output[/align]

1
2
3
4
5
6
1
2
4
4

[align=left]Source[/align]
BestCoder 1st Anniversary ($)

/*
对于(n-1)%6 + 1还没想明白,等脑子清醒了再搞。。。

*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAX = 20000;
int a[MAX];
void solve()
{
for(int i = 0; i <= MAX; i++)
a[i] = 3 * i * (i-1) + 1;
}
int solve(int n)
{
int ans = n % 6;
if(ans == 1){
for(int i = 0 ; i <= MAX; i++){
if(a[i] == n) return 1;
}
return 7;
}
if(ans == 2){
int j = MAX - 1;
for(int i = 1; i <=j; i++){
while(a[i] + a[j] > n)
j--;
if(a[i] + a[j] == n)
return 2;
}
return 8;
}
return (n - 1) % 6 + 1;
}
int main()
{
int T, n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
printf("%d\n", solve(n));
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: