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 ...
随机推荐
- Python 序列化处理
序列化 文件为dump 字符串为dumps dumps()方法返回一个str,内容就是标准的JSON loads()方法将其还原 在程序运行的过程中,所有的变量都是在内存 d = dict(name= ...
- Sqoop框架基础
Sqoop框架基础 本节我们主要需要了解的是大数据的一些协作框架,也是属于Hadoop生态系统或周边的内容,比如: ** 数据转换工具:Sqoop ** 文件收集库框架:Flume ** 任务调度框架 ...
- 用jquery控制表格奇偶行及活动行颜色
虽然jquery流行已经很多年了,一直都感觉很难,也没有花时间去学习它,只是偶尔哪天心血来潮了去看一点点,时隔多日又会忘得一干二净.最近用到表格奇偶行不同色,不得不去再看jquery,虽然感觉还是难, ...
- PostgreSQL Replication之第四章 设置异步复制(8)
4.8 处理时间线 时间线是一个您必须要知道的一个重要的概念,尤其是当您规划一个大型的设置的时候. 那么,什么是时间线呢?事实上,它是XLOG的一个分支.正常情况下,刚设置的一个数据库实例使用的时间线 ...
- AIX系统谨慎使用reboot命令
在客户一次停机维护中,发现了这个问题. 环境是ORACLE 10G RAC for AIX6,使用了HACMP管理共享磁盘. 在停机维护时间段内需要重启主机,当关闭了数据库和CLUSTER后,节点1使 ...
- 01背包-第k优解
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- Redis批量执行(如list批量添加)命令工具 —— pipeline管道应用
前言 Redis使用的是客户端-服务器(CS)模型和请求/响应协议的TCP服务器.这意味着通常情况下一个请求会遵循以下步骤: 使用Redis管道提升性能 (1)客户端向服务端发送一个查询请求,并监听S ...
- vue14 自定义过滤器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php数组增加元素
php数组增加元素 截图 代码 <HTML> <HEAD> <TITLE>给数组增加元素</TITLE> </HEAD> <? $Ci ...
- 【Linux】JDK+Eclipse 搭建C/C++开发环境
注:本文所提供的参考示例是在CentOS Linux环境下的安装,不保证适用于其他版本的Linux系统. · 安装前的注意事项 编译源代码是需要对应的代码编译工具的,本文中安装的Eclipse只 ...