Codeforces Round #589 (Div. 2)

本文解析了四道使用C++编写的算法题目,涵盖了从寻找唯一数字到复杂图论问题的解决策略。通过代码示例,深入探讨了算法设计、数据结构选择及优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A:

#include <bits/stdc++.h>
using namespace std;
int cnt[20];
int main(){
    int l,r;
    scanf("%d%d",&l,&r);
    for(int i=l; i<=r; i++){
        memset(cnt,0,sizeof(cnt));
        int tmp = i,tag = 1;
        while (tmp) {
            if (cnt[tmp % 10]) tag = 0;
            cnt[tmp % 10]++;
            tmp /= 10;
        }
        if (tag) {
            cout << i << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

B:

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef unsigned long long ULL;
LL Gcd(LL a, LL b){if (b == 0) return a; return Gcd(b , a%b);}
LL Lcm(LL a, LL b){ return a/Gcd(a,b)*b;}
inline int read(){
    int f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e3 + 10;
const LL mod = 1000000007;
LL black[maxn][maxn],white[maxn][maxn];
LL r[maxn],c[maxn];
int main(){
    //freopen("/Users/chutong/ACM/data.in","r",stdin);
    //freopen("/Users/chutong/ACM/data.out","w",stdout);
    int h = read(),w = read(),tag = 1;
    for(int i=1; i<=h; i++){
        r[i] = read();
        for(int j=1; j<=r[i]; j++){
            black[i][j] = 1;
        }
        white[i][r[i]+1] = 1;
    }
    for(int i=1; i<=w; i++){
        c[i] = read();
        for(int j=1; j<=c[i]; j++){
            if (white[j][i]) tag = 0;
            black[j][i] = 1;
        }
        if (black[c[i]+1][i]) tag = 0;
        white[c[i]+1][i] = 1; 
    }
    LL ans = 1;
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            if (!black[i][j] && !white[i][j]){
                ans = ans * 2 % mod;
            }
        }
    }
    if (tag == 0){
        cout << 0 << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}

 

C:

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef unsigned long long ULL;
LL Gcd(LL a, LL b){if (b == 0) return a; return Gcd(b , a%b);}
LL Lcm(LL a, LL b){ return a/Gcd(a,b)*b;}
inline int read(){
    int f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
const LL mod = 1e9 + 7;
std::vector<LL> res;
LL qpow(LL a,LL b){
    LL base = a % mod,ans = 1;
    while(b){
        if (b & 1) ans = ans * base % mod;
        base = base * base % mod;
        b >>= 1;
    }
    return ans % mod;
}
int main(){
    //freopen("/Users/chutong/ACM/data.in","r",stdin);
    //freopen("/Users/chutong/ACM/data.out","w",stdout);
    LL x,n;
    scanf("%lld%lld",&x,&n);
    for(LL i=2; i*i<=x; i++){
        if (x % i == 0){
            res.push_back(i);
            while(x % i == 0){
                x /= i;
            }
        }
    }
    if (x > 1) res.push_back(x);
    LL ans = 1;
    for(auto now : res){
        LL tmp = now;
        for(int i=1; i<=100; i++){
            if (tmp > n) break;
            else{
                LL cnt = n/(LL)tmp;
                ans = ans * qpow(now,cnt) % mod;
                if (n / tmp < now) break;
                else tmp = tmp * now;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

 

D:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
map<vector<int>, vector<int>> mp;
vector<int> g[maxn];
int a[maxn];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for (int i=1; i<=m; i++) {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i=1; i<=n; i++){
        if (g[i].empty()) {
            puts("-1");
            return 0;
        }
        sort(g[i].begin(), g[i].end());
        mp[g[i]].push_back(i);
    }
    if (mp.size() != 3){
        puts("-1");
        return 0;
    }
    int ans = 1;
    for(auto x: mp){
        for(auto i : x.second){
            a[i] = ans;
        }
        ans++;
    }
    for(int i=1; i<=n; i++){
        printf("%d%c",a[i],"\n"[i == n]);
    }
    return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值