hdu 5312 Sequence(数学推导+线性探查(两数相加版))
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, =+++=+++.
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).
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.
对于这种题,首先一开始就要对给出的公式进行研究,从这里入手是正道。
分析公式 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(数学推导+线性探查(两数相加版))的更多相关文章
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- LeetCode(2):Add Two Numbers 两数相加
Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...
- 【LeetCode题解】2_两数相加
目录 [LeetCode题解]2_两数相加 描述 方法一:小学数学 思路 Java 代码(非递归写法) Java 代码(递归写法) Python 代码(非递归写法) [LeetCode题解]2_两数相 ...
- leetcode刷题2:两数相加add_two_numbers
题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...
- LeetCode题解002:两数相加
两数相加 题目 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字 如果,我们将这两个数相加起来,则会返回一个新的链表 ...
- LeetCode 2——两数相加(JAVA)
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- ✡ 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 ...
- [Swift]LeetCode2. 两数相加 | Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
随机推荐
- Js获取元素样式值(getComputedStyle¤tStyle)兼容性解决方案
因为:style(document.getElementById(id).style.XXX)只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. 一般js获取内部样式和外部样式使用 ...
- POJ 2392 Space Elevator 背包题解
多重背包.本题不须要二分优化.相对简单点.由于反复数十分小,小于10. 而添加一个限制每种材料的高度做法.假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了. 还有就是注意要排序,以限制高度 ...
- mongodb.open失效导致访问地址404
今天做编辑文章功能的时候发现一个问题,编辑并保存完成后再次跳转到当前文章所在的地址,结果报404,打断点发现查询数据库的时候mongodb.open方法失效.百度后找到了原因: 编辑保存的时候打开了数 ...
- MySQL指令记录(Wampserve环境)
1.MySQL在Wampserve中的默认用户名为'root',默认密码为空: 2.显示所有数据库 show databases; 3.切换数据库 use DATABASE_NAME; 4.列出所有表 ...
- android 开发中判断网络是否连接的代码
在android的开发中,尤其是与访问网络有关的开发,都要判断一下手机是否连接上了网络,下面是一个判断是否连接网络的嗲吗片段: package cn.com.karl.util; import com ...
- 我的django之旅(三)数据库和模型
我的django之旅(三)模型和数据库 标签(空格分隔):模型 数据库 ORM 1.django ORM django内置了一套完整的解决方案,其中就包括他自己的ORM.可惜没有使用SQLAlchem ...
- Java 执行终端命令实现,调用执行另外一个Java文件
Test.java package com.journaldev.files; public class Test { public static void main(String[] args) { ...
- 16-js-缓冲运动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP原始的数据库操作
<?php //这是一个工具类;作用是完成对数据库的操作; class SqlHelper{ public $conn; public $dbname=& ...
- Makefile的几个赋值运算符(转:笔记)
http://www.cnblogs.com/pengdonglin137/p/3801060.html 在Makefile中我们经常看到 = := ?= +=这几个赋值运算符,那么他们有什么区别呢? ...