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

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:

There‘s 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

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;
}

  

时间: 2024-12-24 08:31:39

HDU5312——数学——Sequence(未完成)的相关文章

java之数学方法--未完成

参考http://how2j.cn/k/number-string/number-string-math/319.html java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法的形式存在 四舍五入, 随机数,开方,次方,π,自然常数 package digit; public class TestNumber { public static void main(String[] args) { float f1 = 5.4f; float f2 = 5.5f; //5.4

[C++]LeetCode: 114 Permutation Sequence(返回第k个阶乘序列——寻找数学规律)

题目: The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" &q

HDU 1005 Number Sequence (数学规律)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104190    Accepted Submission(s): 25232 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

hdu 5312 Sequence(数学推导——三角形数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1336    Accepted Submission(s): 410 Problem Description Today, Soda has learned a

HDU 1005 Number Sequence 数学

Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case cont

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

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,

1005:Number Sequence(hdu,数学规律题)

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

杭电2018多校第一场(2018 Multi-University Training Contest 1) 1008.RMQ Similar Sequence (HDU6305) -笛卡尔树+数学期望

6305.RMQ Similar Sequence 这个题的意思就是对于A,B两个序列,任意的l,r,如果RMQ(A,l,r)=RMQ(B,l,r),B序列里的数为[0,1]的实数,B的重量为B的所有元素的和,否则为0.问你B的期望重量是多少. dls讲题说是笛卡尔树,笛卡尔树是一种特定的二叉树数据结构,具体的看这篇博客吧:[pushing my way]笛卡尔树 这个题就是笛卡尔树同构的问题,假设A的笛卡尔树的子树大小为sz[u],那么序列B与A同构的概率为,因为B中的数满足均匀分布(因为B中

poj 1019 Number Sequence (数学)

链接:http://poj.org/problem?id=1019 分析:就是计数的问题..可以先算出112123...m共有多少位,具体式子看代码..然后二分取一个最大的小于n的m,接下来考虑123...m有多少位,再二分一下求解就可以了..具体式子还是看代码.. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 typedef long long