CF 1714E - Add Modulo 10

Add Modulo 10

You are given an array of n n n integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an
You can apply the following operation an arbitrary number of times:

  • select an index i ( 1 ≤ i ≤ n ) i (1≤i≤n) i(1in) and replace the value of the element a i a_i ai with the value a i + ( a i m o d 10 ) a_i+(a_i mod 10) ai+(aimod10), where a i m o d 10 a_i mod 10 aimod10 is the remainder of the integer dividing a i a_i ai by 10 10 10.

For a single index (value i i i), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of a i a_i ai is taken into account each time. For example, if a i = 47 a_i=47 ai=47 then after the first operation we get a i = 47 + 7 = 54 a_i=47+7=54 ai=47+7=54, and after the second operation we get a i = 54 + 4 = 58 a_i=54+4=58 ai=54+4=58.
Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations.
 
For example, you have an array [ 6 , 11 ] [6,11] [6,11].

  • Let’s apply this operation to the first element of the array. Let’s replace a 1 = 6 a_1=6 a1=6 with a 1 + ( a 1 m o d 10 ) = 6 + ( 6 m o d 10 ) = 6 + 6 = 12 a_1+(a_1 mod 10)=6+(6mod10)=6+6=12 a1+(a1mod10)=6+(6mod10)=6+6=12. We get the array [ 12 , 11 ] [12,11] [12,11].
  • Then apply this operation to the second element of the array. Let’s replace a 2 = 11 a_2=11 a2=11 with a 2 + ( a 2 m o d 10 ) = 11 + ( 11 m o d 10 ) = 11 + 1 = 12 a_2+(a_2 mod10)=11+(11mod10)=11+1=12 a2+(a2mod10)=11+(11mod10)=11+1=12. We get the array [ 12 , 12 ] [12,12] [12,12].

Thus, by applying 2 2 2 operations, you can make all elements of an array equal.
 
Input
The first line contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) — the number of test cases. What follows is a description of each test case.
The first line of each test case contains one integer n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n (1≤n≤2⋅10^5) n(1n2105) — the size of the array.
The second line of each test case contains n n n integers a i ( 0 ≤ a i ≤ 1 0 9 ) a_i (0≤a_i≤10^9) ai(0ai109) — array elements.
It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2105.
 
Output
For each test case print:

  • YES if it is possible to make all array elements equal;
  • NO otherwise.
    You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer) .

Example
input

10
2
6 11
3
2 18 22
5
5 10 5 10 5
4
1 2 4 8
2
4 5
3
93 96 102
2
40 6
2
50 30
2
22 44
2
1 5

output

Yes
No
Yes
Yes
No
Yes
No
No
Yes
No

Note
The first test case is clarified above.
In the second test case, it is impossible to make all array elements equal.
In the third test case, you need to apply this operation once to all elements equal to 5 5 5.
In the fourth test case, you need to apply this operation to all elements until they become equal to 8 8 8.
In the fifth test case, it is impossible to make all array elements equal.
In the sixth test case, you need to apply this operation to all elements until they become equal to 102 102 102.

题目大意
给你 n n n 个数,每一个数都可以进行不断加个位上的数操作,最终能否使得数组中 n n n 个数都相同。
解题思路
我们从 1 1 1 开始加找规律, 1 + 1 = 2 , 2 + 2 = 4 , 4 + 4 = 8 , 8 + 8 = 16 , 16 + 6 = 22 , 22 + 2 = 24 … … 1+1=2,2+2=4,4+4=8,8+8=16,16+6=22,22+2=24…… 1+1=22+2=44+4=88+8=1616+6=2222+2=24……,可以看到,个位数的变化是有规律的( 2 , 4 , 6 , 8 2,4,6,8 2,4,6,8 一个循环),然后我们可以把每个数都统一成一个相同的个位数,然后 m o d 20 mod 20 mod20 (循环一次加 20 20 20 ),得到每个数开始加的起点(一个偶数),判断起点相不相同就行了。
还有个位数是奇数的情况了,特殊的其实是 0 0 0 5 5 5 ,因为其它奇数一加就变成偶数了。如果个位数为 0 0 0 ,那么这个数不会变,起点就是它本身,如果个位数是 5 5 5 ,那么把它加 5 5 5 ,就变成个位数为 0 0 0 的情况。

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

int main()
{
    int t; cin>>t;
    while(t--)
    {
        int n; cin>>n;
        vector<int> a(n);
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
            if(a[i]%5 != 0)
            {
                while(a[i]%10 != 2) //统一个位数,最后起点要么是这个个位数,要么是这个个位数+10
                    a[i] += a[i]%10;
                a[i] %= 20; //统一起点 (2, 12 其中一个
            }
            else a[i] += a[i]%10; // 0 或 5 统一终点
        }
        bool flag = true;
        for(int i=1; i<n; i++)
            if(a[i] != a[i-1]) flag = false;
        cout<<(flag? "YES\n": "NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

花生ono

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

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

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

打赏作者

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

抵扣说明:

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

余额充值