HDU5312——数学——Sequence(未完成)
2015-07-26 20:54
465 查看
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 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).
[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 ($)
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 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).
[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 ($)
/* 对于(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; }
相关文章推荐
- UI003---transform属性
- hdu 5288 - OO’s Sequence 数学
- UI002---自定义控件
- POJ2533 Longest Ordered Subsequence(最长递增子序列)
- BUILD ROOT编译记录
- web_custom_request
- UI001---UIButton
- JavaEE基础---java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
- HDU 5312 Sequence (规律题)
- Android Gradle Plugin指南(五)——Build Variants(构建变种版本)
- Number Sequence 重在找规律,48一循环
- hdu 5312 Sequence【数学推导】
- HDU 1242 Rescue 优先队列 BFS
- hdu 5306 Gorgeous Sequence(暴力线段树)(好题)
- hdoj 1005Number Sequence
- Integer Inquiry
- MiniGUI 自定义控件(一)
- 【Prism】MEF版UIComposition
- UVA 10420 List of Conquests
- Altuim designer 导入PCB,出现错误some nets were not able to be matched