题目传送门:
codeforces #720(div2)
A. Nastia and Nearly Good Numbers
AC Code
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
LL a,b;
scanf("%lld%lld",&a,&b);
if(b==1) printf("NO\n");
else
{
printf("YES\n");
if(b==2) printf("%lld %lld %lld\n",a,3*a,4*a);
else printf("%lld %lld %lld\n",a,(b-1)*a,b*a);
}
}
//system("pause");
return 0;
}
B. Nastia and a Good Array
思路:
看到这个题目首先想到的就是x和x+1肯定是互质的。那么我们只需要把最小的数调到最前面,然后构造相邻的数都相差1即可。操作的次数最多为n次。
AC Code
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int idx=0,minn=1e9+10;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]<minn)
{
minn=a[i];
idx=i;
}
}
if(idx!=1)
{
printf("%d\n",n);
printf("%d %d %d %d\n",1,idx,minn,a[1]);
a[idx]=a[1];
a[1]=minn;
}
else printf("%d\n",n-1);
for(int i=2;i<=n;i++)
printf("%d %d %d %d\n",1,i,minn,minn+i-1);
}
//system("pause");
return 0;
}
C. Nastia and a Hidden Permutation
思路:
这是一个交互题,我们观察可以得到,如果你知道了1的位置,那么你使用操作2且使x为1,你就可以知道其他每一个位置的数,这里需要操作n次。那么问题就转化为如何在n/2+30次之内找到1的位置。那么我们就可以使用1操作使x为n-1,每次找两个相邻位置i和i+1,如果1在i位置上,那么答案就会返回1,那么就找到了。但是如果1在i+1位置上那么答案是2,如果2在i+1位置上答案还是2。那么我们该怎么确认这个位置到底是1还是2呢,其实只要反过来再操作一次即可。具体细节看代码。
AC Code
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int p[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int pos=n; //找到1的位置
for(int i=1;i+1<=n;i=i+2)
{
int x;
printf("? %d %d %d %d\n",2,i,i+1,1);
fflush(stdout);
scanf("%d",&x);
if(x==1)
{
p[i]=1;
pos=i;
break;
}
else if(x==2)
{
printf("? %d %d %d %d\n",2,i+1,i,1);
fflush(stdout);
scanf("%d",&x);
if(x==1)
{
p[i+1]=1;
pos=i+1;
break;
}
}
}
p[pos]=1;
for(int i=1;i<=n;i++)
{
int x;
if(i==pos) continue;
printf("? %d %d %d %d\n",1,pos,i,n-1);
fflush(stdout);
scanf("%d",&x);
p[i]=x;
}
printf("! ");
for(int i=1;i<=n;i++) printf("%d ",p[i]);
printf("\n");
fflush(stdout);
}
//system("pause");
return 0;
}