HDU 5312 Sequence
2015-07-26 18:48
375 查看
Sequence
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 509 Accepted Submission(s): 126
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:
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 ($)
问题描述
Soda习得了一个数列, 数列的第n(n>=1)项是3n(n-1)+1. 现在他想知道对于一个给定的整数m, 是否可以表示成若干项上述数列的和. 如果可以, 那么需要的最小项数是多少? 例如, 22可以表示为7+7+7+1, 也可以表示为19+1+1+1.
输入描述
输入有多组数据. 第一行有一个整数T(1<=T<=10000), 表示测试数据组数. 然后对于每组数据: 一行包含1个整数 m(1<= m <=10^9).
输出描述
对于每组数据输出最小花费.
输入样例
10 1 2 3 4 5 6 7 8 22 10
输出样例
1 2 3 4 5 6 1 2 4 4
解题思路:3n(n-1)+1=3*2*(n(n-1)/2)+1=6*(n(n-1)/2)+1,任意一个自然数最多只需要3个三角形数即可表示。而n(n-1)/2(n>=1)正是三角形数的通项公式,因此n(n-1)/2(n>=1)一定是整数。假设m是k个题目中的数列的数的和,则m=6*(k个三角形数的和)+k。题目要求是找到最小的k(k>=1),那么只需找到满足条件(m-k)%6==0的最小k即可,但是对于k=1和k=2的特殊情况要特判处理。
代码如下:
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> #include<string> #include<vector> #include<deque> #include<list> #include<set> #include<map> #include<stack> #include<queue> #include<numeric> #include<iomanip> #include<bitset> #include<sstream> #include<fstream> #include<limits.h> #define debug "output for debug\n" #define pi (acos(-1.0)) #define eps (1e-4) #define inf (1<<28) #define sqr(x) (x) * (x) #define mod 1e9+7 using namespace std; typedef long long ll; typedef unsigned long long ULL; int n,f[100005]; void Init() { for(int i=1;;i++) { f[i]=3*i*(i-1)+1; if(f[i]>1e9) { n=i; break; } } } //一个数 /* //调用函数二分查找 int find_1(int m) { int k=lower_bound(f+1,f+1+n,m)-f; return f[k]==m; } */ /* //遍历查找 int find_1(int m) { for(int i=1;i<=n;i++) if(f[i]==m) return 1; return 0; } */ //二分查找 int find_1(int m) { int l=1,r=n; int mid=(l+r)/2; while(l<=r) { if(f[mid]==m) return 1; else if(f[mid]<m) l=mid+1; else r=mid-1; mid=(l+r)/2; } return 0; } //两个数 /* int find_2(int m) { int l=1,r=n; while(l<=r) { if(f[l]+f[r]<m) return 0; while(l<=r) { if(f[l]+f[r]<m) { r++; break; } else if(f[l]+f[r]==m) return 1; else r--; } l++; } return 0; } */ //二分查找 int find_2(int m) { int i,j; for(i=1,j=n;i<=n&&f[i]<m;i++) { while(j>0&&f[i]+f[j]>m) j--; if(j>0&&f[i]+f[j]==m) return 1; } return 0; } int main() { Init(); int i,t,m; scanf("%d",&t); while(t--) { scanf("%d",&m); if(find_1(m)) printf("1\n"); else { int flag=0; if((m-2)%6==0) { if(find_2(m)) flag=1; } if(flag) printf("2\n"); else { for(i=3;i<10;i++) { if((m-i)%6==0) { printf("%d\n",i); break; } } } } } return 0; }
相关文章推荐
- Android中多线程编程(三)Handler更新UI的方式
- ListBuffer、ArrayBuffer、Queue、Stack操作代码实战
- leetcode[96]:Unique Binary Search Trees
- 实现Guice下的单元测试中mybatis数据库操作的回滚.
- MiniGui 多窗口程序(一)
- Implement Stack using Queues && Implement Queue using Stacks (LeetCode)
- leetcode第225题:Implement Stacks using Queues
- easyui searchbox
- UESTC 250 windy数(数位DP)
- UITextField限制文本长度 , UI控件 自适应宽高 。
- ZOJ - 2548 Prerequisites?
- 用Fluentd实现收集日志到HDFS(上)
- [iOS]类似新浪微博或者人人客户端中,中间UITabBarItem只有图片,使图片居中的方法
- HDU 5312 Sequence
- Tair-淘宝自主开发的一个分布式key/value存储系统
- Number Guessing
- 6.1.Virtio 原理与Guest OS driver
- Workqueue机制的实现
- UIImage+ImageEffects
- 树莓派与arduino串行通信