Triangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 199 Accepted Submission(s): 141
Problem Description
Mr. Frog has n sticks, whose lengths are 1,2, 3
⋯
n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides to steal some sticks! Output the minimal number of sticks he should steal so that Mr. Frog cannot form a triangle with
any three of the remaining sticks.
any three of the remaining sticks.
Input
The first line contains only one integer T (
T≤20
), which indicates the number of test cases.
For each test case, there is only one line describing the given integer n ( 1≤n≤20 ).
For each test case, there is only one line describing the given integer n ( 1≤n≤20 ).
Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of sticks Wallice should steal.
Sample Input
3 4 5 6
Sample Output
Case #1: 1 Case #2: 1 Case #3: 2
Source
题意:给出一个数字n,问1-n序列中最少去掉几个数字可使得剩下的序列中任意三个数字均不能组成三角形三边。
思路:一开始发现数据规模很小,想找规律,但发现前面样例去掉的数字不会影响后面样例去掉的数字。之后又想打表,每次暴力求出可以构成三角形的三边长度,统计出现频率最多的数字去掉之后再暴力求剩下的再统计再去掉,但这样很难处理当多个数字出现频率一样时的情况。其实中间通过1123这样的数字联想到了斐波那契数列,但没有继续想下去。最后发现真的与斐波那契数列有关,在此引用一下骆骆的证明:
充分性:斐波拉契数列的任意项li,lj,lk均不构成三角形。
由于任意三角形三边a、b、c(a < b < c)均需要满足a + b < c。而对于斐波拉契数列中任意的边lk - 2 + lk - 1 = max(li + lj, i,j < k) = lk,因此不可能存在构成三角形。
必要性:在斐波拉契数列中增加任意一项,则可以构成至少一个三角形。
假设添加边长m在li和li + 1之间,由于li + li + 1 = li + 2,因此 li + m > li + 2,因此至少构成一个三角形。
由于任意三角形三边a、b、c(a < b < c)均需要满足a + b < c。而对于斐波拉契数列中任意的边lk - 2 + lk - 1 = max(li + lj, i,j < k) = lk,因此不可能存在构成三角形。
必要性:在斐波拉契数列中增加任意一项,则可以构成至少一个三角形。
假设添加边长m在li和li + 1之间,由于li + li + 1 = li + 2,因此 li + m > li + 2,因此至少构成一个三角形。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
int a[25]={0,0,0,0,1,1,2,3,3,4,5,6,7,7,8,9,10,11,12,13,14};
int main(){
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++){
int i;
scanf("%d",&i);
printf("Case #%d: %d\n",cas,a[i]);
}
return 0;
}