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 ...
随机推荐
- 机器学习方法:回归(二):稀疏与正则约束ridge regression,Lasso
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. "机器学习方法"系列,我本着开放与共享(open and share)的精神撰写,目的是 ...
- Restore IP Addresses——边界条件判定
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- Qt笔记——QSqlLite
静态数据库,简单方便 在.pro文件里添加 +sql #ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { ...
- 如何通过 Redis 实现分布式锁
分布式锁需要解决的问题: 互斥性:任意时刻只能有一个客户端获取锁 安全性:锁只能被持有该锁的客户端删除 死锁:获取锁的客户端因为意外宕机未能释放锁,其他客户端再也无法获取到该锁导致死锁 容错:宕机后客 ...
- 洛谷P1280 尼克的任务 [DP补完计划]
题目传送门 题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每个任务由一个开始时刻与一个持续时间构成. 尼克的一个工作日为N分钟,从 ...
- python实现RabbitMQ同步跟异步消费模型
1,消息推送类 import pika # 同步消息推送类 class RabbitPublisher(object): # 传入RabbitMQ的ip,用户名,密码,实例化一个管道 def __in ...
- Vue 2.0 Application Sample
===搭建Demo=== http://blog.csdn.net/wangjiaohome/article/details/51728217 ===单页Application=== http://b ...
- qwb与学姐 (带秩并查集)
qwb与学姐 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 149 Solved: 54[Submit][Status][Web Board] Des ...
- PIE.htc的使用
文件下载:http://css3pie.com/download/ 使用: .pie_radius{ width:200px; height:200px; background-color:red; ...
- CSS 笔记——盒子模型
2. 盒子模型 在CSS眼中,任何HTML标签对象都是一个矩形,有长度,宽度以及各角的定位坐标,俗称CSS 盒子模型理论. Margin(外边距) - 清除边框外的区域,外边距是透明的. Border ...