C. Yet Another Array Restoration Codeforces Round #667 (Div. 3)

Insert picture description here
Idea: It is to construct an arithmetic sequence with n elements in it, and ensure that there must be x and y in it, and construct an arithmetic sequence with the smallest sum.
Because the data range of this topic is very small, we should try our best to be in x and y Put some more elements in between, then directly enumerate between x and y to find the smallest tolerance, find the first element after determining the tolerance, and then output it.
I have been thinking about the 1200 question for a long time. It's too bad! !

#include <bits/stdc++.h>

using namespace std;

int main()
{
    
    
    int t; cin>>t;
    while(t--){
    
    
        int x,y,n; cin>>n>>x>>y;
        int s = y-x;
        int ans = 0;
        for(int i=0;i<=n-2;i++){
    
    
            if(s%(i+1)==0){
    
    
                ans = i;
            }
        }
        //cout<<ans<<endl;
        int step = s/(ans+1);
        //cout<<step<<endl;
        int begin1 = x;
        for(int i=n-ans-2;i>0;i--){
    
    
            if(begin1-step>0){
    
    
                begin1-=step;
            }
            else break;
        }
        for(int i=0;i<n;i++){
    
    
            cout<<begin1+step*i<<" ";
        }
        cout<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43811879/article/details/108782738