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 ...
随机推荐
- Linux下PHP开启Oracle支持(oci8)
使用php的常见问题是:编译php时忘记加入某扩展,后来想加入扩展,可是由于安装php后又装了一些东西如PEAR等,不想删除文件夹重装,那么此时就须要自己又一次添加某模块支持了,Linux操作系统下能 ...
- POJ 2528 Mayor's posters 离散化和线段树题解
本题就是要往墙上贴海报,问最后有多少可见的海报. 事实上本题的难点并非线段树,而是离散化. 由于数据非常大,直接按原始数据计算那么就会爆内存和时间的. 故此须要把数据离散化. 比方有海报1 6 7 ...
- C&C控制服务的设计和侦测方法综述——DDoS攻击,上传从宿主机偷窃的到的信息,定时给感染机文件加密勒索等。
这篇文章总结了一些我在安全工作里见到过的千奇百怪的C&C控制服务器的设计方法以及对应的侦测方法,在每个C&C控制服务先介绍黑帽部分即针对不同目的的C&C服务器设计方法,再介绍白 ...
- Word histogram
Here is a program that reads a file and builds a histogram of the words in the file: process_file lo ...
- java9新特性-5-Java的REPL工具: jShell命令
1.官方Feature 222: jshell: The Java Shell (Read-Eval-Print Loop) 2.产生背景 像Python 和 Scala 之类的语言早就有交互式编程环 ...
- windows 下读取文件名称和类型
def getFileWithType(self,xname): # xname='E:\\python\\recievedir\\data.pkl' # xname='E:\python\test. ...
- UI Framework-1: UI Development Practices
UI Development Practices Guidelines Principles for developing for Chrome. These best practices cente ...
- bzoj 2259: [Oibh]新型计算机 最短路 建模
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...
- python2中打印列表与字典内的中文字符
在开发过程中,我们经常需要打印一些变量的值,便于调试.这个时候就会发现如果在列表与字典这些容器中,如果包含中文字符,不管是str类型,还是unicode类型,都打印不出来.如下: >>&g ...
- STM时钟
一.在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL. ①HSI是高速内部时钟,RC振荡器,频率为8MHz. ②HSE是高速外部时钟,可接石英/陶瓷谐振器,或者接外部时钟源,频率 ...