hdu 5312 Sequence(数学推导——三角形数)
2015-12-09 16:59
513 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312
Total Submission(s): 1336 Accepted Submission(s): 410
[align=left]Problem Description[/align]
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 integerT(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 ($)
题目大意:给出一个序列3*n*(n-1)+1,再输入一个m,求构成给定n所需的最小个数。(序列中的没一个数可以使用若干次)
解题思路:明确一下N*(N-1)/2为三角形数。性质:任意一个自然数都最多可由三个三角形数表示。 题目给的序列是3*n*(n-1)+1就可以转换为6*n*(n-1)/2+1。对于给定的值m,假如m需要k个数来表示,那么其一组解可以表示为m= 6*(K个三角形数的和)+K;
即任意由k个数组成的解 都有 (m-K)%6==0;那么只要找到最小的k即为所求。
此外,对于序列的通式,当n=1或者n=2的时候,就会没有意义,所以对于1和2的时候需要特殊判断一下。
这是一道三角形数的推导及运用题目。
详见代码 。
Sequence
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1336 Accepted Submission(s): 410
[align=left]Problem Description[/align]
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 integerT(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 ($)
题目大意:给出一个序列3*n*(n-1)+1,再输入一个m,求构成给定n所需的最小个数。(序列中的没一个数可以使用若干次)
解题思路:明确一下N*(N-1)/2为三角形数。性质:任意一个自然数都最多可由三个三角形数表示。 题目给的序列是3*n*(n-1)+1就可以转换为6*n*(n-1)/2+1。对于给定的值m,假如m需要k个数来表示,那么其一组解可以表示为m= 6*(K个三角形数的和)+K;
即任意由k个数组成的解 都有 (m-K)%6==0;那么只要找到最小的k即为所求。
此外,对于序列的通式,当n=1或者n=2的时候,就会没有意义,所以对于1和2的时候需要特殊判断一下。
这是一道三角形数的推导及运用题目。
详见代码 。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; int a[100005]; bool check1(int m) { for (int i=1; i<20010; i++) { if (a[i]==m) return 1; } return 0; } bool check2(int m) { int j; for (int i=1,j=20010-1; i<20010&&a[i]<m; i++) { while (a[i]+a[j]>m) j--; if (a[i]+a[j]==m) return 1; } return 0; } int main() { for (int i=0; i<20010; i++) { a[i]=3*i*(i-1)+1; } int t; scanf("%d",&t); while (t--) { int m; scanf("%d",&m); int flag=0; if (check1(m)) { printf ("1\n"); continue; } else if (check2(m)) { printf ("2\n"); continue; } else { for (int i=3; i<=8; i++) //循环到8的原因是因为模6的余数只有0-5六个 { if ((m-i)%6==0) { printf ("%d\n",i); flag=1; break; } } } if (flag==0) { printf ("-1\n"); } } return 0; }
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- 用批处理解决数学问题的代码第1/4页
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- 探讨C语言的那些小秘密之断言
- C语言实现BMP转换JPG的方法
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- C语言查找数组里数字重复次数的方法