CF1962F 题解

· · 题解

题意

给定 n 个数,问能否选定 a_i,a_j,a_k 三个数使得三个数相加的和的末位是 3。

思路

由于

(x \bmod {10})+(y \bmod {10})\equiv (x+y)\pmod{10}

所以我们只需要存储每个数个位上的数字就可以了。

之后,我们可以开一个桶 s,其中 s_i 表示原数组中个位是 i 的数字的个数。

最后,我们只需进行三层循环枚举所有可能性即可。

AC Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Mod1=998244353;
const int Mod2=1000000007;
int gcd(int a,int b){return __gcd(a,b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
const int N=200005;
int a[N],s[N];
void Solve()
{
    //coding here...
    int n;
    memset(s,0,sizeof(s));
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        s[a[i]%10]++;//存下个位上的数出现的次数
    }
    for(int i=0;i<=9;i++)
    {
        for(int j=0;j<=9;j++)
        {
            for(int k=0;k<=9;k++)
            {
                s[i]--;
                s[j]--;
                s[k]--;//这里需要注意 i,j,k 不能相同
                if(s[i]>=0&&s[j]>=0&&s[k]>=0&&(i+j+k)%10==3)
                {
                    cout<<"YES"<<endl;
                    return;
                }
                s[i]++;//回溯
                s[j]++;
                s[k]++;
            }
        }
    }
    cout<<"NO"<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    int _=1;
    cin>>_;
    while(_--)
    {
        Solve();
    }
    return 0;
}