HDU 5312 Sequence

Problem Description

Today, Soda has learned a sequence whose nn-th (n \ge 1)(n1) item is 3n(n-1)+13n(n1)+1. Now he wants to know if an integer mm 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 + 122=19+1+1+1=7+7+7+1.

Input

There are multiple test cases. The first line of input contains an integer TT (1 \le T \le 10^4)(1T104), indicating the number of test cases. For each test case:

There's a line containing an integer mm (1 \le m \le 10^9)(1m109).

Output

For each test case, output -11 if mm 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

打表找规律,对于1和2要特判

#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const ll maxn = 20005;
int T, n, m, f[maxn];
map<int, int> M;

int main()
{
    for (int i = 1; i <= 20000; i++)
    {
        f[i] = 3 * i * (i - 1) + 1;
        M[f[i]] = 1;
    }
    cin >> T;
    while (T--)
    {
        scanf("%d", &n);
        m = (n - 1) % 6 + 1;
        if (m > 2) printf("%d\n", m);
        else
        {
            if (m == 1) if (M.count(n)) printf("1\n"); else printf("7\n");
            else
            {
                int flag = 0;
                for (int i = 1; f[i] + f[i] <= n; i++)
                    if (M.count(n - f[i])) { flag = 1; break; }
                if (flag) printf("2\n");else printf("8\n");
            }
        }
    }
    return 0;
}


你可能感兴趣的:(HDU)