Codeforces Round #811 (Div. 3)

比赛链接

Codeforces Round #811 (Div. 3)

E. Add Modulo 10

给出 \(n\) 个数,每次可以将其中一个数加上其对 \(10\) 的模数,问最后所有数能否相等

解题思路

思维

可以发现,如果同时出现对 \(10\) 的模数为 \(5\)\(0\) 跟其他模数的情况肯定无法相等,所以需要特判对 \(10\) 的模数为 \(5\)\(0\) 情况,其他情况由于其加上都是 \(2,4,8,6\) 一个循环,而且如果所有数能相等的话,把这些数变为对 \(10\) 的模数为 \(2\) 也不会对判断有影响,转化后判断所有数对 \(20\) 的模数是否相等即可

时间复杂度:\(O(n)\)

代码

// Problem: E. Add Modulo 10
// Contest: Codeforces - Codeforces Round #811 (Div. 3)
// URL: https://codeforces.com/contest/1714/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e5+5;
int t,n,a[N];
int main()
{
    for(cin>>t;t;t--)
    {
    	cin>>n;
    	bool fl[2]={0};
    	for(int i=1;i<=n;i++)
    	{
    		cin>>a[i];
    		if(a[i]%10==5||a[i]%10==0)
    		{
    			if(a[i]%10==5)a[i]+=5;
    			fl[0]=true;
    		}
    		else
    			fl[1]=true;
    	}
    	bool f=true;
    	if(fl[0])
    	{
    		if(fl[1])f=false;
    		else
    		{
    			for(int i=2;i<=n;i++)
    				if(a[i]!=a[i-1])f=false;
    		}
    	}
    	else
    	{
    		 for(int i=1;i<=n;i++)
    		 {
    		 	while(a[i]%10!=2)a[i]+=a[i]%10;
    		 	a[i]%=20;
    		 }
    		 for(int i=2;i<=n;i++)
    		 	if(a[i]!=a[i-1])f=false;
    	}
    	puts(f?"Yes":"No");
    }
    return 0;
}
posted @ 2022-08-11 00:17  zyy2001  阅读(25)  评论(0编辑  收藏  举报