【Codeforces Round #457 (Div. 2) C】Jamie and Interesting Graph

本文介绍了一种算法,用于找到大于n-1的最小素数,并构造满足特定条件的边长序列。通过验证每个数是否为素数来寻找符合条件的最小素数x,之后根据x构造出满足题目要求的边长。

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


在这里输入题意

【题解】


找比n-1大的最小的素数x
1-2,2-3..(n-2)-(n-1)长度都为1
然后(n-1)-n长度为(x-(n-2))
然后其他边长度都设为x+1就好了。
这样就能满足题意了。

【代码】

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

int n,m,ma;

bool is(int x){
    if (x<2) return false;
    int len = sqrt(x);
    for (int i = 2;i <= len;i++){
        if (x%i==0)
            return false;
    }
    return true;
}

int main(){
    #ifdef LOCAL_DEFINE
        freopen("rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n >> m;
    for (int i = n-1;;i++)
        if (is(i)){
            ma = i;
            break;
        }
    cout <<ma<<' '<<ma<<endl;
    for (int i = 1;i <= n-2;i++){
        cout <<i<<' '<<i+1<<" 1"<<endl;
    }
    cout <<n-1<<" "<<n<<" "<<ma-(n-2)<<endl;
    int now = n-1;
    for (int i = 1;i <= n &&now<m;i++)
        for (int j = i+1;j <= n && now <m;j++){
            if (j != (i+1)){
                cout <<i<<" "<<j<<" "<<ma+1<<endl;
                now++;
            }
        }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/8319855.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值