codeforces 856A (思维)

Masha and Grisha like studying sets of positive integers.

One day Grisha has written a set A containing n different integers ai on a blackboard. Now he asks Masha to create a set B containing n different integers bj such that all n2 integers that can be obtained by summing up ai and bj for all possible pairs of i and j are different.

Both Masha and Grisha don’t like big numbers, so all numbers in A are from 1 to 106, and all numbers in B must also be in the same range.

Help Masha to create the set B that satisfies Grisha’s requirement.

Input
Input data contains multiple test cases. The first line contains an integer t — the number of test cases (1 ≤ t ≤ 100).

Each test case is described in the following way: the first line of the description contains one integer n — the number of elements in A (1 ≤ n ≤ 100).

The second line contains n integers ai — the elements of A (1 ≤ ai ≤ 106).

Output
For each test first print the answer:

NO, if Masha’s task is impossible to solve, there is no way to create the required set B.
YES, if there is the way to create the required set. In this case the second line must contain n different positive integers bj — elements of B (1 ≤ bj ≤ 106). If there are several possible sets, output any of them.
Example
Input
3
3
1 10 100
1
1
2
2 4
Output
YES
1 2 3
YES
1
YES
1 2

给你一个集合A,让你构造一个集合B,使得A中任何一个元素+B中任何一个元素的所有能产生的和都不相同。转化一下 ai+bj!=ak+bm; ai-ak!=bj-bm;

找出所有的差值存下,然后扫一遍所有的数,如果能成立就放进去,一开始 b[0]=1;

#include <bits/stdc++.h>
using namespace std;

int vis[1000010];
int b[330];
int a[330];

const int BUF=30000000;  
char Buf[BUF],*buf=Buf; 
inline void read(int&a){for(a=0;*buf<48;buf++);while(*buf>47)a=a*10+*buf++-48;}


int main()
{
    int t;
    fread(Buf,1,BUF,stdin);//重点  
    read(t);
    while(t--)
    {
        int n;
        read(n);
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            read(a[i]);
        int cnt=1;
        b[0]=1;
        if(n==1)
        {
            puts("YES");
            printf("%d\n",b[0] );
            continue;
        }
        sort(a+1,a+n+1);
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                vis[a[j]-a[i]]=1;
        for(int i=2;i<=1000000;i++)
        {
            int f=0;
            for(int j=0;j<cnt;j++)
            {
                if(vis[i-b[j]])
                {
                    f=1;
                    break;
                }
            }
            if(!f)
                b[cnt++]=i;
            if(cnt>=n)
                break;
        }
        if(cnt>=n)
        {
            puts("YES");
            for(int i=0;i<cnt;i++)
                printf("%d ",b[i] );
            printf("\n");
        }
        else puts("NO");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值