D. Array Differentiation- 图论 三进制 建模

Problem - 1552D - Codeforces

D. Array Differentiation

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a sequence of nn integers a1,a2,…,ana1,a2,…,an.

Does there exist a sequence of nn integers b1,b2,…,bnb1,b2,…,bn such that the following property holds?

  • For each 1≤i≤n1≤i≤n, there exist two (not necessarily distinct) indices jj and kk (1≤j,k≤n1≤j,k≤n) such that ai=bj−bkai=bj−bk.

Input

The first line contains a single integer tt (1≤t≤201≤t≤20) — the number of test cases. Then tt test cases follow.

The first line of each test case contains one integer nn (1≤n≤101≤n≤10).

The second line of each test case contains the nn integers a1,…,ana1,…,an (−105≤ai≤105−105≤ai≤105).

Output

For each test case, output a line containing YES if a sequence b1,…,bnb1,…,bn satisfying the required property exists, and NO otherwise.

Example

input

Copy

5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258

output

Copy

YES
YES
NO
YES
YES

Note

In the first test case, the sequence b=[−9,2,1,3,−2]b=[−9,2,1,3,−2] satisfies the property. Indeed, the following holds:

  • a1=4=2−(−2)=b2−b5a1=4=2−(−2)=b2−b5;
  • a2=−7=−9−(−2)=b1−b5a2=−7=−9−(−2)=b1−b5;
  • a3=−1=1−2=b3−b2a3=−1=1−2=b3−b2;
  • a4=5=3−(−2)=b4−b5a4=5=3−(−2)=b4−b5;
  • a5=10=1−(−9)=b3−b1a5=10=1−(−9)=b3−b1.

In the second test case, it is sufficient to choose b=[0]b=[0], since a1=0=0−0=b1−b1a1=0=0−0=b1−b1.

In the third test case, it is possible to show that no sequence bb of length 33 satisfies the property.

---------------------------------------------------------------------------------------------------------------------------------

这是一道抽象模型题目,应该将题目中的减法结果为ai的关系设置为连边,b设置为节点,那么现在问题转化为了n个节点具有n条边。显然,必定有环。那么有环的情况,如下

这里的有环,指的是无向图意义下的有环。那么我们就可以发现,如果其方向相同,比如顺时针,其边权和一定是0的。虽然我们这个环并非规则的顺时针,但是我们可以利用其顺时针时和为0来判断环的存在,一旦有环,边权是可以任意设置的,其效果仅仅是多出来一条边罢了。

我们考虑三进制枚举,枚举每个ai的正负,0的情况,ai就是边,0代表其不在这一个环上面,1代表其为负数,-1代表为正数,一旦能满足是和为0,那么就一定可以组成一个无向图环,具体方向不必考虑。

# include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

int a[100];
int main ()
{

   int t;
   cin>>t;

   while(t--)
   {
       int n;
       cin>>n;

       for(int i=0;i<n;i++)
       {
           cin>>a[i];
       }
       int temp=1;
       for(int i=1;i<=n;i++)
       {
           temp*=3;
       }

       int ans=0;
       for(int i=1;i<temp;i++)
       {
           int now=i;
           int nowpos=0;
           ll sum1=0,sum2=0;
           while(now)
           {
               if(now%3==0)
               {
                   now/=3;
                   nowpos++;
               }
               else if(now%3==1)
               {
                   now/=3;
                   sum1+=a[nowpos];
                   nowpos++;

               }
               else
               {
                   now/=3;
                   sum2-=a[nowpos];
                   nowpos++;
               }
           }

           if(sum1+sum2==0)
           {
               ans=1;
               break;
           }
       }
       if(ans)
       {
           cout<<"YES"<<endl;
       }
       else
       {
           cout<<"No"<<endl;
       }
   }


    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值