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 ...
随机推荐
- 具体解释C++引用——带你走进引用的世界
一.介绍引用 首先说引用是什么,大家能够记住,引用就是一个别名,比方小王有个绰号叫小狗.他的妈妈喊小狗回家吃饭.那就是在喊小王回家吃饭. 接下来我们用两行代码来声明一个引用(就拿小王和小狗来说吧 ...
- C#后台请求其它网站页面
/// <summary> /// 指定Post地址使用Get 方式获取全部字符串 /// </summary> /// <param name="url&qu ...
- OpenGL编程逐步深入(五)Uniform 变量
准备知识 在这个教程中我们会遇到一种新的Shader变量类型,即uniform变量.attribute(属性)变量和uniform变量的不同之处在于attribute 变量中包含顶点的具体数据,当每次 ...
- ubutun lunix 64安装neo4j 图形数据库
借鉴链接: https://my.oschina.net/zlb1992/blog/915038
- mysql每个表总的索引大小
/* 指定的数据库 每个表的索引 不包含主键索引的大小*/ ,),,),'mb') as index_size from information_schema.tables where TABLE_S ...
- caioj 1070 动态规划入门(二维一边推3:字符距离)(最长公共子序列拓展)
复制上一题总结 caioj 1069到1071 都是最长公共字序列的拓展,我总结出了一个模型,屡试不爽 (1) 字符串下标从1开始,因为0用来表示字符为空的情况,而不是第一个字符 (2) ...
- Unity 获得Android Context上下文
1.获取Context AndroidJavaObject context = new AndroidJavaClass ("com.unity3d.player.UnityPlayer&q ...
- unity C# 获取有关文件、文件夹和驱动器的信息
class FileSysInfo { static void Main() { // You can also use System.Environment.GetLogicalDrives to ...
- ArcGIS api for javascript——地图配置-
描述 本例展示了如果删除缩放等级滑动器的刻度线.通过设置esriConfig里的sliderLabel为null来实现: esriConfig.defaults.map.sliderLabel = n ...
- Css 选择器总结
选择器 .class 类对应的元素. #id 对应的id元素. * 全部元素 div{} 对应的标签 div,p{} 全部的div和p div p{} div下全部的p标签 div>p{} di ...