HDU 5312 Sequence (规律题)
题意:
一个序列的第n项为3*n*(n-1)+1,而 n>=1,现在给一个正整数m,问其最少由多少个序列中的数组成?
思路:
首先,序列第1项是1,所以任何数都能构成了。但是最少应该是多少?对式子进行变形,6*(n*(n-1)/2)+1,看到了三角形数n*(n-1)/2,那么应该是6*(任意自然数)+x=m才对,因为最多只要3个三角形数就能组成任何自然数啦。
不妨试试m%6是多少?这样试图求x可以吗?因为任意自然数最多由3个组成,如果是k个,那么应该x>=k,别忘了还有个+1的项。x-k那部分,就只能由1来搞定了。
还有个前提,m%6=0怎么办?总不能由0个组成吧?那应该起码是1,所以(m-1)%6可以保证不为0。试试看m=13,则x=(13-1)%6+1=1,这样就真的能由1个序列中的数构成吗?序是1,7,19...好像没有。。。悲剧!应该是7+1+1+1+1+1+1才是,那6个1是补上去的。同理x=2也是需要保证序列中有两个数字能组成m才行,否则要x+=6才是答案。而3及以上就不用了,为什么我也不知道。。。
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
unordered_map<int,int> mapp;
vector<int > seq;
void pre_cal()
{
int t=;
for(int i=; i<&&t<=; i++)
{
t=*i*(i-)+;
mapp[t]=;
seq.push_back(t);
}
} bool cal(int m)
{
int q=, p=seq.size()-;
while(q<=p)
{
int t=seq[q]+seq[p];
if(t==m) return ;
if(t>m) p--;
else q++;
}
return ;
} int main()
{
//freopen("input.txt", "r", stdin);
int t, m;
pre_cal();
cin>>t;
while(t--)
{
scanf("%d",&m);
int k=(m-)%+;//k是不会超过6的。但是答案可能超过6。 if(k>=) printf("%d\n",k);
else if(k==)//特判
{
if(mapp[m]) printf("1\n");
else printf("%d\n",k+=);
}
else if(k==)//特判
{
if(cal(m)) printf("2\n");
else printf("%d\n",k+=);
}
}
return ;
}
AC代码
HDU 5312 Sequence (规律题)的更多相关文章
- hdu 5312 Sequence(数学推导——三角形数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others) ...
- 1005:Number Sequence(hdu,数学规律题)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- hdu 5312 Sequence(数学推导+线性探查(两数相加版))
Problem Description Today, Soda has learned a sequence whose n-th (n≥) item )+. Now he wants to know ...
- HDU 4279 Number-------找规律题
Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1719 Friend 规律题
解题报告:规定数字1和数字2是Friend number,然后规定,若a和b是Friend number,那么a*b+a+b也是Friend number,输入一个数,让你判断这个数是否是Friend ...
- hdoj--1005--Number Sequence(规律题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- UVa 1584 Circular Sequence --- 水题
UVa 1584 题目大意:给定一个含有n个字母的环状字符串,可从任意位置开始按顺时针读取n个字母,输出其中字典序最小的结果 解题思路:先利用模运算实现一个判定给定一个环状的串以及两个首字母位置,比较 ...
- HDU 3397 Sequence operation(线段树)
HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...
- LightOJ1010---Knights in Chessboard (规律题)
Given an m x n chessboard where you want to place chess knights. You have to find the number of maxi ...
随机推荐
- 【BZOJ】【2820】YY的GCD
莫比乌斯反演 PoPoQQQ讲义第二题. 暴力枚举每个质数,然后去更新它的倍数即可,那个g[x]看不懂就算了…… 为什么去掉了一个memset就不T了→_→…… /****************** ...
- 剑指offer--面试题19
题目:求二叉树镜像 根据作者思路,自己所写代码如下: void BinaryTreeMirror(BinaryTreeNode* pRoot) { if(pRoot == NULL) return; ...
- PHP防止SQL注入与几种正则表达式讲解
注入漏洞代码和分析 代码如下: <?php function customerror($errno, $errstr, $errfile, $errline) { echo <b& ...
- SVN库实时同步设置
为了安全起见,SVN服务器除了做定时全量备份和增量备份以外,如条件允许,可做实时备份. 这需要2台机器,一台是master server,另一台做mirror server.master做主服务,mi ...
- ReplicaManager之DelayedOperation
DelayedOperation包括两种:DelayedFetch和DelayedProduce,它们的存在是由Kafka Protocol决定的,而Kafka Protocol是由实际需求决定的…… ...
- linux源代码阅读笔记 get_free_page()代码分析
/* 34 * Get physical address of first (actually last :-) free page, and mark it 35 * used. If no fre ...
- 数据库批量插入数据的shell脚本
测试用,先来一个简单的,这个是国产神通数据库的,用isql命令: !/bin/bash == "-h" ] then echo "USAGE: $0 table_name ...
- hdu 4335 What is N?
此题用到的公式:a^b%c=a^(b%phi(c)+phi(c))%c (b>=phi(c)). 1.当n!<phi(p)时,直接暴力掉: 2.当n!>=phi(p) &&a ...
- *[topcoder]IncrementingSequence
http://community.topcoder.com/stat?c=problem_statement&pm=12107 此题想了半天,当时瞥到了Greedy,所以就想着贪心,最后的方法 ...
- python 常用数据结构使用
python 字典操作 http://www.cnblogs.com/kaituorensheng/archive/2013/01/24/2875456.html python 字典排序 http:/ ...