Positive solution of arithmetic sequence of the 10th Blue Bridge Cup

Time limit: 1.0s Memory limit: 256.0MB Total points for this question: 20 points

【Problem Description】

The math teacher gave Xiao Ming a problem to sum arithmetic series. But the careless Xiao Ming forgot part of the sequence, and only remembered the N integers.
Now given these N integers, Xiaoming wants to know how many terms are there in the shortest arithmetic sequence containing these N integers?

【Input format】

The first line of input contains an integer N.
The second row contains N integers A 1 , A 2 , . . . , AN . (Note that A 1 ∼ AN is not necessarily given in the order in the arithmetic sequence)

【Output format】

Output an integer representing the answer.

【Sample input】

5
2 6 4 10 20

【Example output】

10

【Example description】

The shortest arithmetic progression that contains 2, 6, 4, 10, 20 is 2, 4, 6, 8, 10, 12, 14, 16, 18, 20.

[Scale and conventions of evaluation cases]
For all evaluation cases, 2 ≤ N ≤ 100000, 0 ≤ A i ≤ 109 10^910 9.

The reason why I wrote this problem solution is because I saw the same problem solution on the Internet and entered a misunderstanding, that is, "the minimum value of the difference between the two is the tolerance"

Then there is the following code

#include <bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;cin>>n;
    int a[n+10];
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    int x = 99999999999;
    for(int i=1;i<n;i++){
    
    
        x = min(a[i]-a[i-1],x);
    }
    if(x==0) cout<<n<<endl;
    else {
    
    
        int ans = (a[n-1]-a[0])/x+1;
        cout<<ans<<endl;
    }
    return 0;
}

Because of this, I have been depressed for a long time. I really can't think of the principle of this code. This code is still AC! ! !
Suppose a sequence of 1 3 8 is given, so the tolerance is 2? Obviously not

The H question of the Blue Bridge Cup will not be so simple

correct answer:

To understand that for a sequence of
2 6 4 10 20 given by the title,
first sort the sequence: 2, 4, 6, 10, 20 (there should be no doubt about this)
to make the sequence the shortest, a[0]=2, a[n ]=20;
By the arithmetic sequence, the general term: an=a1+(n-1)*dn=(an-a1)/d+1
At this time, we only need to find the value of the tolerance d.
The following paragraph needs to be detailed! ! !
d={greatest common divisor of the differences of all adjacent integers}
All integer differences of an ordered sequence must be a multiple of the tolerance to form an arithmetic sequence, that is, it (a[n]-a[n-1])/d==0must be established for an ordered sequence. To make the sequence the shortest, the tolerance as large as possible, so just ask for the greatest common divisor of all differences

#include<bits/stdc++.h>
using namespace std;
int N,a[100005];
int gcd(int a,int b){
    
    
	return b==0?a:gcd(b,a%b);
}
int main(){
    
    
	cin>>N;
	for(int i=0;i<N;i++){
    
    
		cin>>a[i];
	}
	sort(a,a+N);
	//求差值最大公约数 
	int d=a[1]-a[0];
	for(int i=2;i<N;i++){
    
    
		 d=gcd(d,a[i]-a[i-1]);
	}
	if(d==0)printf("%d\n",N);
	else{
    
    
		printf("%d ",(a[N-1]-a[0])/d+1);
	}
	return 0;
	
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936908&siteId=291194637