2021 Niu Ke Winter Holiday Algorithm Basic Training Camp 2 J. Niu Niu wants to be a hacker (structure)

J. Niuniu wants to be a hacker

Title link: https://ac.nowcoder.com/acm/contest/9982/J

Title description:

In the algorithm competition, "hack" generally refers to the use of a set of test data to trigger the defect of the program, which causes the AC code that originally passed the problem to fail the test data.
In general, it is more common to use hack data to cause WA to be dropped by others. Of course, some will cause the original AC code TLE and MLE.

Niuniu encountered such a problem in some simple exercises.
Given an array a of size n (1≤ ai ≤10^9), then please judge whether the array elements can be selected from three to form a triangle.
Niuniu discovered that there is such a violent logic in the code passed by AC. The pseudo-code of the logic is as follows.
Insert picture description here

In fact, it is a triple loop enumerating the three elements of the array to check whether it is a triangle. The trick of this code is that it has a kind of "short-circuit" logic. Once a triangle is found, the program will be terminated immediately.
In this way, it is actually easy to find triangles under random data, so if the data is purely random, obviously this is a piece of AC code.

Of course Niuniu knows that this code is obviously flawed. If the data is well structured, it should be able to card TLE, but Niuniu found that he would not construct data that can hack this violent algorithm, so he asked you to help him.

We use the number of times this program calls isTriangle as the basis for calculating the time complexity. Please construct the data hack this violent program to make it TLE.

Enter a description:

Enter a positive integer n (3 ≤ n ≤ 10^5) in the first line to indicate the size of the array you need to construct.

Output description:

Output n positive integers, and the range of positive integers is between [1,10^9]. The violent program is required to call the isTriangle function no less than the number of times it is running Insert picture description here.

Example 1:

Input
3
Output
2 2 2
Description
When n=3, the question requires the program with small w to call the isTriangle function at least once, so any 3 positive integers can be output to meet the conditions.

Example 2:

Input
10
output
1 2 4 8 16 32 64 128 256 512
Description
Since any three numbers can not form a triangle, it will scan to the last group, reaching the maximum complexity, a total of 120 calls to the isTriangle function.

Problem-solving ideas:

Any three numbers cannot form a triangle, that is, the sum of any two numbers ≤ the third number. Think of the Fibonacci sequence . Let the first part of the sequence be the Fibonacci sequence, and the sum of any two numbers in the latter part ≤ the Fibonacci sequence part.

code show as below:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
    
    
	int n;
	cin >> n;
	ll a[50];
	a[1]=2;a[2]=3;
	for (int i=3;i<=min(40,n);i++){
    
    
		a[i]=a[i-1]+a[i-2];
	}
	for (int i=1;i<=min(40,n);i++){
    
    
		cout<<a[i]<<" ";
	}
	for (int i=40;i<=n;i++){
    
    
		cout<<"1"<<" ";
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/113678063