Problem Description
Today, Soda has learned a sequence whose n-th (n≥) item is 3n(n−)+. 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, =+++=+++.
Input
There are multiple test cases. The first line of input contains an integer T (≤T≤), indicating the number of test cases. For each test case:

There's a line containing an integer m (1≤m≤109).
 
Output
For each test case, output − if m cannot be represented as the sum of some items of that sequence, otherwise output the minimum items needed.
 
Sample Input

 
Sample Output

 
Source
 

 对于这种题,首先一开始就要对给出的公式进行研究,从这里入手是正道。

分析公式 3n(n−1)+1 ,若给出一个数n,假设要k个3n(n−1)+1加起来得到n,即 3n(n-1)k+k=n,注意到3n(n-1)k是6的倍数,那么求的是满足(n-k)%6==0的最小的k。还有就是要注意到1、2要特判,即k要从3开始枚举

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stdlib.h>
#include<map>
using namespace std;
#define N 20000
int num[N];
int n;
void init(){
for(int i=;i<N;i++){
num[i]=*i*(i-)+;
}
}
bool check1(){
for(int i=;i<N;i++){
if(num[i]==n)
return true;
}
return false;
}
bool check2(){
int j=N-;
for(int i=;i<N;i++){
while(num[i]+num[j]>n && j>) j--;
if(num[i]+num[j]==n && j>){
return true;
}
}
return false;
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--){ scanf("%d",&n);
if(check1()){
printf("1\n");
}
else if(check2()){
printf("2\n");
}
else{
for(int i=;i<;i++){
if((n-i)%==){
printf("%d\n",i);
break;
}
}
}
}
return ;
}

hdu 5312 Sequence(数学推导+线性探查(两数相加版))的更多相关文章

  1. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  2. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  3. 【LeetCode题解】2_两数相加

    目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...

  4. leetcode刷题2:两数相加add_two_numbers

    题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...

  5. LeetCode题解002:两数相加

    两数相加 题目 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字 如果,我们将这两个数相加起来,则会返回一个新的链表 ...

  6. LeetCode 2——两数相加(JAVA)

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  7. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. [Swift]LeetCode2. 两数相加 | Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. [Swift]LeetCode445. 两数相加 II | Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

随机推荐

  1. 搭建完全分布式的hadoop[转]

    hadoop 创建用户及hdfs权限,hdfs操作等常用shell命令 sudo addgroup hadoop#添加一个hadoop组sudo usermod -a -G hadoop larry# ...

  2. Gunplot 命令大全

    在linux命令提示符下运行gnuplot命令启动,输入quit或q或exit退出. plot命令 gnuplot> plot sin(x) with line linetype 3 linew ...

  3. 多路复用I/O select()

    select(),poll(),epoll()的总结:http://www.cnblogs.com/Anker/p/3265058.html 在socket编程中,仅仅使用connect,accept ...

  4. CSS元素 之 float

    1. float 设计的初衷 Float 设计的初衷是为了文字环绕的效果 使得文字可以围绕着 图片.就像下面这样 2. float 的包裹和 破坏 A) 包裹性 和 破坏性 例如下图   我们原本是希 ...

  5. python - 类的特殊成员

    class Foo: #构造方法 def __init__(self,name,age): pass self.name = name self.age = age def __str__(self) ...

  6. compass模块----Utilities

    引入Utilities: @import "compass/utilities"; 分别引入: @import "compass/utilities/color" ...

  7. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  8. Shell脚本中单引号(‘)和双引号(“)的使用区别[转载]

    shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符` 1. 单引号 ( '' ) # grep Susan phonebook Susan Go ...

  9. C++内存对象布局

    本章主要介绍了c++类中成员变量.函数对象的在内存中布局. 当c++类中不包含virtual机制类的函数时,内部nostatic member被包含在每一个class object之中,就想c str ...

  10. C/C++与lua实现互调

    1,在lua脚本中调用C/C++代码中的函数 在C++中定义函数时必须以lua_State为参数, 以int为返回值才能被Lua所调用. /* typedef int (*lua_CFunction) ...