练习cf1968C. Assembly via Remainders

题目如下
C. Assembly via Remainders
time limit per test2 seconds
memory limit per test256 megabytes
You are given an array 𝑥2,𝑥3,…,𝑥𝑛. Your task is to find any array 𝑎1,…,𝑎𝑛, where:

1≤𝑎𝑖≤109 for all 1≤𝑖≤𝑛.
𝑥𝑖=𝑎𝑖mod𝑎𝑖−1 for all 2≤𝑖≤𝑛.
Here 𝑐mod𝑑 denotes the remainder of the division of the integer 𝑐 by the integer 𝑑. For example 5mod2=1, 72mod3=0, 143mod14=3.

Note that if there is more than one 𝑎 which satisfies the statement, you are allowed to find any.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases.

The first line of each test case contains a single integer 𝑛 (2≤𝑛≤500) — the number of elements in 𝑎.

The second line of each test case contains 𝑛−1 integers 𝑥2,…,𝑥𝑛 (1≤𝑥𝑖≤500) — the elements of 𝑥.

It is guaranteed that the sum of values 𝑛 over all test cases does not exceed 2⋅105.

Output
For each test case output any 𝑎1,…,𝑎𝑛 (1≤𝑎𝑖≤109) which satisfies the statement.

题目大意
现有n-1个数组成的数组,需要找到一个由n个数组成的数组,
满足
1≤𝑎𝑖≤109 for all 1≤𝑖≤𝑛.
𝑥𝑖=𝑎𝑖mod𝑎𝑖−1 for all 2≤𝑖≤𝑛.。

题目分析
核心是要满足𝑥𝑖=𝑎𝑖mod𝑎𝑖−1,那么只要让 a[i] = a[i - 1] + x[i - 1]即可,只要让a[i]正好比a[i-1]多出模的大小即可,注意同时一定要满足a[i - 1] > x[i - 1],只有在这种情况下才成立。
根据题目限制的a数组元素的大小和整个数组元素的和,只要对a[0]事先赋合适的值,并且后续数组中的元素一定是递增的,一定满足a[i - 1] > x[i - 1]即可。

点击查看代码
#include <iostream>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int x[505];
        for(int i = 0; i < n - 1; i++){
            cin >> x[i];
        }
        long long a[505];
        a[0] = 501;
        for(int i = 1; i < n; i++){
            a[i] = a[i - 1] + x[i - 1];
        }
        for(int i = 0; i < n; i++){
            printf("%lld ", a[i]);
        }
        printf("\n");
    }
    return 0;
}
posted @ 2025-07-22 20:16  sirro1uta  阅读(7)  评论(0)    收藏  举报