开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第31天,点击查看活动详情
Codeforces Round #838 (Div. 2)——B. Make Array Good
Problem - B - Codeforces
An array bb of mm positive integers is good if for all pairs ii and jj (1≤i,j≤m1≤i,j≤m), max(bi,bj)max(bi,bj) is divisible by min(bi,bj)min(bi,bj).
You are given an array aa of nn positive integers. You can perform the following operation:
- Select an index ii (1≤i≤n1≤i≤n) and an integer xx (0≤x≤ai0≤x≤ai) and add xx to aiai, in other words, ai:=ai+xai:=ai+x.
- After this operation, ai≤1018ai≤1018 should be satisfied.
You have to construct a sequence of at most nn operations that will make aa good. It can be proven that under the constraints of the problem, such a sequence of operations always exists.
Input
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the length of the array aa.
The second line of each test case contains nn space-separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — representing the array aa.
It is guaranteed that the sum of nn over all test cases does not exceed 105105.
Output
For each test, output a single integer pp (0≤p≤n0≤p≤n) — denoting the number of operations in your solution.
In each of the following pp lines, output two space-separated integers — ii and xx.
You do not need to minimize the number of operations. It can be proven that a solution always exists.
Example
input
4
4
2 3 5 5
2
4 8
5
3 4 343 5 6
3
31 5 17
output
4
1 2
1 1
2 2
3 0
0
5
1 3
1 4
2 1
5 4
3 7
3
1 29
2 5
3 3
问题解析
题目是说给你n个数,你每次可以给一个数ai加上不大于aj的值,最多能进行n次操作,现在让你经过操作后让数组满足:选择任意两个位置i和j,满足i到j中的最大值可以整除最小值。
我们对数组排序,遍历数组,如果当前数直接是上一个数的倍数,我们不操作,如果当前数不是上一个数的倍数,那么很显然我们可以通过给当前数ai加上(ai-a(i-1))%a(i-1)来把ai变成上一个数的倍数。这样每个数最多只会操作一次。
AC代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define endl '\n'
//#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 1e6 + 50, MOD = 1e9 + 7;
int a[N];
void solve()
{
int n, mn = 1e9 + 10;;
cin >> n;
vector<PII>v(n);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
v[i - 1].first = a[i], v[i - 1].second = i;
mn = min(mn, a[i]);
}
sort(v.begin(), v.end());
cout << n << endl;
for (int i = 0; i < n; i++)
{
if (v[i].first % mn == 0)
{
cout << v[i].second << " " << 0 << endl;
mn = v[i].first;
}
else
{
cout << v[i].second << " " << mn - v[i].first % mn << endl;
mn = v[i].first + (mn - v[i].first % mn);
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}