HDU5312 Sequence
题意: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的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [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 ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- Pygame-依葫芦画瓢之兔獾大战
Pygame-依葫芦画瓢之兔獾大战 前几天看到国外一个12岁的孩子写的兔獾大战游戏,心生敬佩,想当年我还是12岁的时候还不知电脑为何物,连小霸王都未曾玩过.自己也未曾想去搞游戏开发,纯属自娱自乐.在此 ...
- 我一直记不住的vim用法
一.多行编辑进入visual block模式一般模式下Crtl+v组合键以块的形式选中待编辑的文本 进入visual line模式一般模式下大写V以行的形式选中待编辑的文本 上述两种模式的复制用y,删 ...
- 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- Oracle基础了解
数据库: 关系型数据库 select * from 表名 非关系型数据库(做不到复杂查询) 以对象的形式进行存储 {"aaa":"ccc"}---键值对 ora ...
- 循序渐进PYTHON3(十三) --8-- DJANGO之ADMIN
admin简单使用: 1.urls.py 2.settings.py 3.models.py from django.db import models classUserInfo(models ...
- Windows命令远程执行工具Winexe
Windows命令远程执行工具Winexe 在对Windows系统执行渗透测试中,通过各种方式可以获取目标主机的用户名和密码.这时,只要对方主机开启文件共享服务,就可以借助Winexe工具远程执行 ...
- 关于 CommonJS AMD CMD UMD
1. CommonJS CommonJS 原来叫 ServerJS, 是服务器端模块的规范,Node.js采用了这个规范. 根据CommonJS规范,一个单独的文件就是一个模块.加载模块使用requi ...
- 【POJ 3784】 Running Median (对顶堆)
Running Median Description For this problem, you will write a program that reads in a sequence of 32 ...
- 调用sort段错误问题
问题:sort的比较函数实现有问题导致进程调用sort时core了. 结论:特别要注意,sort的比较函数必须遵循严格弱排序(strict weak ordering)的规则. 这是最近在工作中遇 ...
- 【二分查找】POJ2456-Aggressive cows
[题目大意] 有N间牛舍和M头牛,告诉你每个牛舍的位置,求出两头牛之间最小距离的最大值. [思路] 二分判断两头牛之间的最小距离d,通过贪心法进行验证. #include<iostream> ...