2021.5.7 Codeforces Round #720 Div.2
A. Nastia and Nearly Good Numbers
题目
Nastia 有两个正整数A和B,她定义:
1.当一个整数被A*B整除时,它是好数;
2.否则,当这个整数被A整除时,它是 近似好数;
给出A和B,要求给出三个不同的正整数x、y、z满足x+y=z且其中一个数是好数,另外两个是 近似好数。
Input:
The first line contains a single integer t (1≤t≤10000) — the number of test cases.
The first line of each test case contains two integers A and B (1≤A≤106, 1≤B≤106) — numbers that Nastia has.
Output:
For each test case print:
“YES” and 3 different positive integers x, y, and z (1≤x,y,z≤1018)
such that exactly one of them is good
and the other 2 are nearly good, and x+y=z.
“NO” if no answer exists.
(输入输出懒得翻译了)
分析题意
这道题要求对于给出的A和B,给出三个整数,满足上述条件。
1.当B为1时,若一个数被A整除,那么一定被A*B整除,则不存在近似好数。
2.当B不为1时:
A被A整除,且因为A<AB,A不会被AB整除,
故A为一个符合题意的近似好数;
AB被A整除,又被A整除,故AB为符合题意的好数;
由x+y=z,不妨假设x为A,z为AB,则y为A(B-1),
我们可以判断出此时y被A整除。
又由于A*(B-1)=AB(1-1/B),
容易看出y不会被A*B整除,所以y为符合题意的近似好数;
因为x,y,z两两不相等,所以当B大于2时,上述y成立;
当B=2时,设x为A 近似好数,z为2AB=4A 好数,则y为3A 近似好数;
上代码
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long long a,b;
cin>>a>>b;
if(b==1) cout<<"NO"<<endl;
else if(b==2) cout<<"YES"<<endl<<a<<" "<<3*a<<" "<<4*a<<endl;
else cout<<"YES"<<endl<<a<<" "<<a*(b-1)<<" "<<a*b<<endl;
}
return 0;
}
反思
做题的时候,没有理解清楚题意,将能被AB整除理解为了既能被A整除又能被B整除。
B. Nastia and a Good Array
题目
Nastia 收到一个由n个正整数组成的数组.
她把这样的数组称为 好数组: 数组的相邻两项的最大公约数为1;
你可以进对一个数组行这样的操作:
选择两个不同的下标i和j (1≤i,j≤n, i≠j)
以及两个整数x和y (1≤x,y≤2⋅10^9),
且满足min(ai,aj)=min(x,y).
然后把ai改为x,aj改为y.
The girl asks you to make the array good using at most n operations.
It can be proven that this is always possible.
Input
The first line contains a single integer t (1≤t≤10000) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤105) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the array which Nastia has received as a gift.
It’s guaranteed that the sum of n in one test doesn’t exceed 2⋅105.
Output
For each of t test cases print a single integer k (0≤k≤n) — the number of operations. You don’t need to minimize this number.
In each of the next k lines print 4 integers i, j, x, y (1≤i≠j≤n, 1≤x,y≤2⋅109) so that min(ai,aj)=min(x,y) — in this manner you replace ai with x and aj with y.
If there are multiple answers, print any.
(输入输出懒得翻译了)
分析题意
我们可以证明x与x+1的最大公约数为1:
设x与x+1的最大公约数为p,
则x/p为整数,(x+1)/p也为整数,
那么x/p+1/p为整数,
那么p必须为1;
那么这道题可以考虑这么解决:
1.遍历数组找到数组中最小的那个数的位置;
2.利用置换规则,将与这个数距离为k的位置上的数值变成这个数+k;
这样一来,只要执行n-1次,就可以保证数组成为好数组。
上代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> arr;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int x;
for(int i=0;i<n;i++)
{
cin>>x;
arr.push_back(x);
}
int k=arr[0];
int p=0;
for(int i=0;i<n;i++)
{
k=min(k,arr[i]);
if(k==arr[i]) p=i;
}
cout<<n-1<<endl;
for(int i=0;i<n;i++)
{
if(p!=i)
{
arr[i]=k+abs(p-i);
cout<<p+1<<" "<<i+1<<" "<<k<<" "<<arr[i]<<endl;
}
}
arr.clear();
}
return 0;
}
反思
题中的数组下标从1开始,一开始没注意,造成了错误。