• Add Modulo 10 (规律循环节,代码实现细节)


    Add Modulo 10 (规律循环节,代码实现细节)

    题目描述

    You are given an array of $ n $ integers $ a_1, a_2, \dots, a_n $

    You can apply the following operation an arbitrary number of times:

    • select an index $ i $ ( $ 1 \le i \le n $ ) and replace the value of the element $ a_i $ with the value $ a_i + (a_i \bmod 10) $ , where $ a_i \bmod 10 $ is the remainder of the integer dividing $ a_i $ by $ 10 $ .

    For a single index (value $ 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 $ is taken into account each time. For example, if $ a_i=47 $ then after the first operation we get $ a_i=47+7=54 $ , and after the second operation we get $ a_i=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] $ .

    • Let’s apply this operation to the first element of the array. Let’s replace $ a_1 = 6 $ with $ a_1 + (a_1 \bmod 10) = 6 + (6 \bmod 10) = 6 + 6 = 12 $ . We get the array $ [12, 11] $ .
    • Then apply this operation to the second element of the array. Let’s replace $ a_2 = 11 $ with $ a_2 + (a_2 \bmod 10) = 11 + (11 \bmod 10) = 11 + 1 = 12 $ . We get the array $ [12, 12] $ .

    Thus, by applying $ 2 $ operations, you can make all elements of an array equal.

    输入格式

    The first line contains one integer $ t $ ( $ 1 \le t \le 10^4 $ ) — 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 \le n \le 2 \cdot 10^5 $ ) — the size of the array.

    The second line of each test case contains $ n $ integers $ a_i $ ( $ 0 \le a_i \le 10^9 $ ) — array elements.

    It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

    输出格式

    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) .

    样例 #1

    样例输入 #1

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    样例输出 #1

    Yes
    No
    Yes
    Yes
    No
    Yes
    No
    No
    Yes
    No
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    提示

    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 $ .

    In the fourth test case, you need to apply this operation to all elements until they become equal to $ 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 $ .


    打表找循环节。

    我的:

    /*
    1 2 4 8 16
    3 6 12 14 18
    */
    void solve(){
        cin>>n;
        set<int>st;
        bool ok = 1;
        bool A = 0;// 1
        bool B = 0;// 3
        bool C = 0;// 5
        fo(i,1,n){
            int x;cin>>x;
            int t = x+x%10;
            if(t%5==0){
            	if(!C){
            		C=1;
            		st.insert(t);
            	}
            	else{
    	        	if(st.find(t)!=st.end());
    	        	else ok = 0;
            	}
            }
            t %= 20;
            if(t == 1 || t == 2 || t == 4 || t == 8 || t == 16){
            	if(!A){
            		A=1;
            		st.insert(1);
            	}
            }
            
            if(t == 3 || t == 6 || t == 12 || t == 14 || t == 18){
            	if(!B){
            		B=1;
            		st.insert(3);
            	}
            }
        }
        if(st.size()!=1){
        	ok = 0;
        }
        cout<<(ok ? "YES" : "NO")<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    #include 
    using namespace std;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const LL mod = 1e9 + 7;
    const int N = 300005;
    
    int a[N];
    int main() {
        int _;
        scanf("%d", &_);
        while (_--) {
            int n;
            scanf("%d", &n);
            vector<int> b;
            set<int> c;
            for (int i = 1; i <= n; i++) {
                scanf("%d", &a[i]);
                if (a[i] % 10 == 5)
                    b.push_back(a[i] + 5);
                else if (a[i] % 10 == 0)
                    b.push_back(a[i]);
                else {
                    int x = a[i];
                    while (x % 10 != 6) {
                        x += x % 10;
                    }
                    c.insert(x % 20);
                }
            }
            if (b.size() > 0) {
                int ok = b.size() == n;
                for (auto x : b)
                    if (x != b[0]) ok = 0;
                puts(ok ? "YES" : "NO");
            } else {
                puts(c.size() == 1 ? "YES" : "NO");
            }
        }
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    /**
     *    author:  tourist
     *    created: 01.08.2022 17:43:24       
    **/
    #include 
    
    using namespace std;
    
    #ifdef LOCAL
    #include "algo/debug.h"
    #else
    #define debug(...) 42
    #endif
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(0);
      int tt;
      cin >> tt;
      while (tt--) {
        int n;
        cin >> n;
        vector<int> a(n);
        bool any = false;
        for (int i = 0; i < n; i++) {
          cin >> a[i];
          while (a[i] % 10 != 0 && a[i] % 10 != 2) {
            a[i] += a[i] % 10;
          }
          any |= (a[i] % 10 == 0);
        }
        if (any) {
          cout << (a == vector<int>(n, a[0]) ? "Yes" : "No") << '\n';
          continue;
        }
        int val = a[0] % 20;
        bool ok = true;
        for (int i = 0; i < n; i++) {
          ok &= (a[i] % 20 == val);
        }
        cout << (ok ? "Yes" : "No") << '\n';
      }
      return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    nginx中deny和allow详解
    C++中的泛型详细讲解
    JS 使用Reg表达式匹配后直接取值
    循环神经网络(RNN)简单介绍及实现(基于表面肌电信号)
    (一)H264视频解码问题:出现部分绿屏问题的解决
    实战!如何从零搭建10万级 QPS 大流量、高并发优惠券系统--图文解析
    共赴开源路,共筑新丰碑!2022云栖大会龙蜥操作系统峰会圆满落幕!
    使用OpenCV进行简单图像分割的3个步骤
    568A和568B两种线序
    简单理解旁路电容和去耦电容
  • 原文地址:https://blog.csdn.net/hacker__man/article/details/126119025