hdu 5312 Sequence(数学推导——三角形数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312
Sequence
n
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.
indicating the number of test cases. For each test case:
There's a line containing an integer m
−1
if m
cannot be represented as the sum of some items of that sequence, otherwise output the minimum items needed.
10
1
2
3
4
5
6
7
8
22
10
1
2
3
4
5
6
1
2
4
4
(序列中的没一个数能够使用若干次)
即随意由k个数组成的解 都有 (m-K)%6==0;那么仅仅要找到最小的k即为所求。
详见代码 。
#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;
}
hdu 5312 Sequence(数学推导——三角形数)的更多相关文章
- HDU 5312 Sequence (规律题)
题意: 一个序列的第n项为3*n*(n-1)+1,而 n>=1,现在给一个正整数m,问其最少由多少个序列中的数组成? 思路: 首先,序列第1项是1,所以任何数都能构成了.但是最少应该是多少?对式 ...
- hdu 5312 Sequence(数学推导+线性探查(两数相加版))
Problem Description Today, Soda has learned a sequence whose n-th (n≥) item )+. Now he wants to know ...
- hdu.5211.Mutiple(数学推导 && 在logn的时间内求一个数的所有因子)
Mutiple Accepts: 476 Submissions: 1025 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 6553 ...
- HDU 5734 Acperience(数学推导)
Problem Description Deep neural networks (DNN) have shown significant improvements in several applic ...
- HDU 5984 题解 数学推导 期望
Let’s talking about something of eating a pocky. Here is a Decorer Pocky, with colorful decorative s ...
- HDU 5312(数学推导+技巧)
首先说一下.N*(N-1)/2为三角形数,随意一个自然数都最多可由三个三角形数表示. 对于,对于给定的要求值 V, 那么其一组解可表示为 V = 6*(K个三角形数的和)+K: 即随意由k个数组成的解 ...
- 关于不同进制数之间转换的数学推导【Written By KillerLegend】
关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...
- HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)
Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total S ...
- HDU 5312:Sequence
Sequence Accepts: 25 Submissions: 1442 Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 2621 ...
随机推荐
- WET Dilutes Performance Bottlenecks
WET Dilutes Performance Bottlenecks Kirk Pepperdine THE IMPORTANCE OF THE DRY PRINCIPLE (Don't Repea ...
- 34.angularJS的{{}}和ng-bind
转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...
- Dictionaries
A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dict ...
- js中作用域链的问题
为什么没有var声明的变量是全局的? 是因为,在js中,如果某个变量没有var声明,会自动到上一层作用域中去找这个变量的声明语句,如果找到,就使用,如果没有找到,继续向上查找,一直查找到全局作用域为止 ...
- PostgreSQL两种事务隔离级
PostgreSQL两种事务隔离级别: 读已提交:PostgreSQL中缺省隔离级别.当一个事务运行在这个隔离级别时,一个SELECT查询只能看到查询开始之前提交的数据而永远无法看到未提交的数据或者在 ...
- 《剑指offer》二叉树的镜像
一.题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 二.输入描述 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 三.输出描述 镜像二叉树 8 / \ 10 ...
- yii2.0缓存篇之文件缓存
文件缓存: 在 frontend/config/main.php/components数组下添加: 'cache'=>[ 'class'=>'yii\caching\FileCa ...
- [POI2008]PLA-Postering(单调栈)
题意 N个矩形,排成一排. 现在希望用尽量少的矩形海报Cover住它们. (n<=250000,wi,di<=109) 题解 这种一堆矩形,又不像数据结构的题,一般都是单调栈. 考虑一个贪 ...
- 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
- HDU 3911 线段树区间合并
北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...