题意: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. Word中截取部分内容并保存为jpg图片的方法

    private void button1_Click(object sender, EventArgs e) { var appWord = new Microsoft.Office.Interop. ...

  2. 比对两个Word文件内容是否一致的C#解决办法

    using System; using System.Windows.Forms; using System.Diagnostics; using Microsoft.Office.Interop.W ...

  3. vConsole ~ 移动开发调试工具

    在开发移动端项目时,有时候在PC端好好的,但是到了手机上出bug,很难调试,这时候可以用vConsole调试工具 使用方式 1.直接引入 <script src="vconsole.m ...

  4. Intellij IDEA 去掉Mapper文件中的背景

    1.在setting中输入:inspection --> SQL 2.去掉背景颜色,Apply即可

  5. 转:攻击JavaWeb应用[5]-MVC安全

    转:http://static.hx99.net/static/drops/tips-347.html 攻击JavaWeb应用[5]-MVC安全 园长 · 2013/07/25 13:31 注:这一节 ...

  6. SSH整合错误三连

    访问Action错误 ognl.MethodFailedException: Method "add" failed for object com.test3.action.Use ...

  7. 洛谷——P1629 邮递员送信

    P1629 邮递员送信 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要 ...

  8. hdu1006 Tick and Tick (数学题 借鉴了大神的博客)

    先缩短一半的时间:早上的12个小时和下午的12小时对时钟是一样的,因为时钟12小时与0小时的三针位置相同.接着就是了解到每次所有的针从有重合到再次有重合至多有一段连续的段符合三针分离度大于n.所以只要 ...

  9. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

  10. leetcode 564. Find the Closest Palindrome

    leetcode564题目地址 Given an integer n, find the closest integer (not including itself), which is a pali ...