HDU 5312 Sequence
来源:互联网 发布:linux内核分为 编辑:程序博客网 时间:2024/05/30 22:59
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 .
For example,
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 integerm (1≤m≤109) .
There's a line containing an integer
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
10123456782210
Sample Output
1234561244
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).
输出描述
对于每组数据输出最小花费.
输入样例
10123456782210
输出样例
1234561244
解题思路: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+7using 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;}
0 0
- HDU 5312 Sequence
- HDU 5312 Sequence
- 【瞎搞】 HDU 5312 Sequence
- hdu 5312 Sequence
- hdu 5312 Sequence
- hdu 5312 Sequence
- hdu 5312 Sequence【数学推导】
- hdu 5312 Sequence 三角形数
- hdu 5312 Sequence(数学)
- HDU 5312:Sequence 三角形数
- hdu 5312 Sequence(三角形数的利用)
- HDU 5312-Sequence(三角形数+推导)
- 【HDU 5312】Sequence(数学问题)
- hdu 1711 Number Sequence
- hdu 1711 Number Sequence
- HDU 1005 Number Sequence
- hdu 1711 Number Sequence
- HDU 3397 Sequence operation
- 本地虚拟机下的centOS安装jdk1.7
- PS简单使用
- Android中的Handler机制
- MJRefresh原理分析
- 假期第一周工作周报
- HDU 5312 Sequence
- 如何让gmail不过滤垃圾邮件
- MiniGui 多窗口程序(一)
- 变步长的 MPPT
- 【Struts2框架】第三节OGNL表达式-OGNL表达式例子2
- poj 2777 线段树(区间染色)
- OC学习笔记SEL类型初步理解
- HDU 1215.七夕节【筛选法】【7月26】
- JTAG引脚