题意:t组数据,每组数据给个m。问m最少能由几项形如3*n*(n-1)+1的数表示

eg 7=1(n=1)+1(n=1)+1(n=1)+1(n=1)+1(n=1)+1(n=1)+1(n=1);

     7=7(n=2);

     所以7最少能由1个数表示



分析:3*n*(n-1)+1能够转换为6*(n*(n-1)/2)+1,而n*(n-1)/2是一个三角形数。设为An,

   则m能够表示为m=6*(A1+A2+…+Ak)+k(如果m最少能由k个数表示)看,由三
   角形数的性质(一个自然数最多能由三个三角形数表示)可得,当k>=3时,A1+…
   +Ak
能够表示随意自然数,此时k=(m-1)%6+1+6*n(n=0,1,2,…)(A1+A2+…Ak
  是自然 数)此时最小值k取n=0,即k=(m-1)%6+1(k>=3).另外,假设当n=0时,
  k的值为1或者2。此时须要考虑是否存在一个或者两个三角形数能表示出   该数m。假设能够,则k的最小值即为1或者2。假设不能够,则取n=1,
  k+=6。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
using namespace std;
const int maxn = 1e6+5;
map<int,int>Map;
int v[maxn];
int main()
{
int t;
for(int i=1;i<=100000;i++){ //预处理
int tmp=3*i*(i-1)+1;
if(tmp>1e9) break;
v[i]=tmp;
Map[tmp]=1; //用于后面查看此数是否可以由3*n*(n-1)+1表示
}
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int k=(n-1)%6+1; //(n-1)%6+1==n%6=0?6:n%6,两种写法都可以
if(k>=3){ //k>=3,此时一定存在自然数能由A1+…Ak的数表示
printf("%d\n",k);
}
else if(k==1){ //k==1 检验n能否由该式子表示
if(Map.count(n)) printf("1\n");
else printf("7\n");
}
else if(k==2){ //k==2,检验n能否由两个该式子的数表示
int flag=0;
for(int i=1;v[i]<=n/2;i++){
if(Map.count(n-v[i])) flag=1;
}
if(flag) printf("2\n");
else printf("8\n");
}
}
}











HDU5312 Sequence的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. es6中对象的一些操坐

    变量的赋值 key值得构建 对象的严格检测 对象的合并 1.变量的赋值: let name='宋宇',age='17岁': let obj={name,age} //快速的将变量引入到对象中去. 2. ...

  2. 【剑指offer】面试题 29. 顺时针打印矩阵

    面试题 29. 顺时针打印矩阵 题目描述 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  3. cocos2d-x addImageAsync()异步加载资源成功之后的场景跳转问题

    http://blog.csdn.net/w20175357/article/details/23546985 1.先说说addImageAsync()异步加载图片的问题 做游戏的时候现在资源的比较大 ...

  4. Js文件中调用其它Js函数的方法

    在项目开发过程中,也许你会遇这样的情况.在某一Js文件中需要完成某一功能,但这一功能的大部分代码在另外一个Js文件中已经完成了,自己只需要调用这个方法再加上几句代码就可以实现所需的功能.我们知道,在h ...

  5. 剑指offer-树中两个节点的最低公共祖先

    普通二叉树 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; ...

  6. Spring的远程调用

    Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易 四种远程调用技术: ◆ 远程方法调用(RMI) ◆ Caucho的Hessian和Burlap ...

  7. 关于oracle存储过程需要注意的问题

    在使用oracle存储过程时,有一些需要注意的地方,下面就来总结一下. 1.在oracle的存储过程中,数据表别名不能加as 也许是为了区分存储过程中的as,怕与过程中的as冲突. 如: select ...

  8. 【BZOJ 1115】【POI 2009】石子游戏Kam

    http://www.lydsy.com/JudgeOnline/problem.php?id=1115 差分后变成阶梯博弈. #include<cstdio> #include<c ...

  9. 【找规律】【DFS】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative

    假设一个数有n个质因子a1,a2,..,an,那么n'=Σ(a1*a2*...*an)/ai. 打个表出来,发现一个数x,如果x'=Kx,那么x一定由K个“基础因子”组成. 这些基础因子是2^2,3^ ...

  10. python3-开发进阶Django中序列化以及rest_framework的序列化

    一.django框架的序列化 直接上代码 1.这是app下的models.py from django.db import models # Create your models here. clas ...