【Codeforces 342A】Xenia and Divisors

【链接】 我是链接,点我呀:)
【题意】

【题解】

最后a,b,c只有以下3种情况 1,2,4 1,2,6 1,3,6 那么用cnt[8]统计每个数字出现的次数. 输出cnt[4]次1,2,4 (如果1或2不够,那么无解 紧接着 如果6的个数和1的个数不同,那么无解 如果2的次数+3的次数和6出现的次数不同,那么无解. 否则 输出cnt[2]个1,2,6 cnt[3]个1,3,6就ok了。 看看有没有输出够n/3组 不够的话就无解(说明有其他无用的数字出现

【代码】

#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
using namespace std;

const int N = 1e5;

int n,m;
int cnt[10];
vector<pair<int,pair<int,int> > > v;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++) {
        int x;
        cin >> x;
        cnt[x]++;
    }
    rep1(i,1,cnt[4]) {
        v.push_back({1,{2,4}});
        if (cnt[1]==0) return cout<<"-1"<<endl,0;
        if (cnt[2]==0) return cout<<"-1"<<endl,0;
        cnt[1]--;cnt[2]--;
        n-=3;
    }
    cnt[4] = 0;
    if (cnt[1]!=cnt[6] || (cnt[2]+cnt[3])!=(cnt[6])){
        cout<<"-1"<<endl;
        return 0;
    }
    for (int i = 1;i <= cnt[2];i++){
        v.push_back({1,{2,6}});
        n-=3;
    }
    for (int i = 1;i <= cnt[3];i++){
        v.push_back({1,{3,6}});
        n-=3;
    }
    if (n!=0) return cout<<-1<<endl,0;
    for (auto temp:v){
        cout<<temp.first<<' '<<temp.second.first<<' '<<temp.second.second<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/9742133.html