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 ...
随机推荐
- SharePoint Search之(七)Search result- 结果源
在使用搜索引擎的时候.非常多情况下,用户希望限定一下搜索范围,以便更加easy找到想要的结果. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU1BGYXJ ...
- PhpStorm Live Template加PHP短语法Short Open Tags打造原生模板
关于Php要不要使用模板一直被大家讨论,支持的说使用模板更简洁,易与前端project师交流.反对的说Php本身就支持内嵌语法,不是必需再用个模板,减少性能. 事实上使用Php的短语法.直接嵌入也不是 ...
- poj_3371
一道模拟题,写的有点麻烦 #include<iostream> #include<cstring> #include<cstdio> #include<alg ...
- android 源码编译sdk
源码下载好以后,想要编译sdk,结果一直失败,今天终于找到正确方法来解决这个问题了. 使用make sdk 编译命令,结果一直报找不到资源.找不到bin/sqite3等,后来操作如下命令就可以了 1. ...
- java9新特性-7-语法改进:接口的私有方法
1.官方Feature 213: Milling Project Coin Support for private methods in interfaces was briefly in consi ...
- 有关 IE11
IE10/IE11 中 99% 的 CSS3 属性不需要写 -ms- 前缀: IE9/IE10/IE11 默认开启 GPU 加速,效果比同期 Chrome 版本(如 35)流畅: IE11 的字体渲染 ...
- grep常用命令讲解
grep大家应该并不陌生,但是这个命令你确定真的会用吗?ok,接下来我通过举例子的方式,带你看清grep的本质. 首先,把/etc/password的内容复制下来命令为1.txt吧,方便操作,哈哈~ ...
- Android实战简易教程-第二十六枪(基于ViewPager实现微信页面切换效果)
1.头部布局文件top.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- HMACSHA256 Class
https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx Comp ...
- BZOJ 1305 二分+网络流
思路: 建图我根本没有想到啊--. (我是不会告诉你我借鉴了一下题解的思路) 把每个人拆成喜欢的和不喜欢的点 男 喜欢 向 男 不喜欢 连 边权为k的边 如果男喜欢女 那么 男喜欢向 女喜欢 连 1 ...