题目:
C. Yet Another Array Restoration
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:
- The array consists of 𝑛n distinct positive (greater than 00) integers.
- The array contains two elements 𝑥x and 𝑦y (these elements are known for you) such that 𝑥<𝑦x<y.
- If you sort the array in increasing order (such that 𝑎1<𝑎2<…<𝑎𝑛a1<a2<…<an), differences between all adjacent (consecutive) elements are equal (i.e. 𝑎2−𝑎1=𝑎3−𝑎2=…=𝑎𝑛−𝑎𝑛−1)a2−a1=a3−a2=…=an−an−1).
It can be proven that such an array always exists under the constraints given below.
Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize max(𝑎1,𝑎2,…,𝑎𝑛)max(a1,a2,…,an).
You have to answer 𝑡t independent test cases.
Input
The first line of the input contains one integer 𝑡t (1≤𝑡≤1001≤t≤100) — the number of test cases. Then 𝑡t test cases follow.
The only line of the test case contains three integers 𝑛n, 𝑥x and 𝑦y (2≤𝑛≤502≤n≤50; 1≤𝑥<𝑦≤501≤x<y≤50) — the length of the array and two elements that are present in the array, respectively.
Output
For each test case, print the answer: 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤1091≤ai≤109), where 𝑎𝑖ai is the 𝑖i-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter).
It can be proven that such an array always exists under the given constraints.
解析:
a2−𝑎1=𝑎3−𝑎2=…=𝑎𝑛−𝑎𝑛−1)a2−a1=a3−a2=…=an−an−1). ---->等差数列
给出数组的长度,以及两个元素x,y且(2≤𝑛≤50; 1≤𝑥<𝑦≤50),输出该数组最小可能的最大元素的一个数组,y和x的差值就是公差的一个公倍数,找到可以符合条件的最小的公差,得满足y-x<=(n-1)*d1,找出公差之后第一项a1也就等于y-(n-1)*d1。
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while (t--)
{
int x,y,d,n,d1;
cin>>n>>x>>y;
d=y-x;
for (int i=1; i<=d; i++)//
{
if (d%i==0)//可能的公差
{
d1=i;
if (y - (n - 1)*i <= x)//y-x<=(n-1)*i求出符合想寻找的最小的公差
{
break;
}
}
}
int start = y - (n - 1)*d1;
while (start <= 0)
{
start += d1;
}
for (int i = 0; i<n; i++)
{
cout<< start + (d1*i)<<" ";//an=a1+nd;
}
cout<<endl;
}
return 0;
}