CF变红之路_ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

本文分享了作者从 Round400 开始刷 CodeForces 的经历,包括 A、B、C 题目的解题思路及代码实现,涉及前缀和等算法技巧。

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

这套题是昨天晚上做的,也就是从昨天开始刷CF。
先说一下我的计划:
从Round400开始刷CF,div1,2,3都要做,并记录自己的刷题进度,限时刷题(像比赛一样)+ 补题。
以此记录我的CF变红?变紫?之路。
题目链接: http://codeforces.com/contest/776

A

水题

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

typedef pair<string, string> Pair;

int main()
{
    //freopen("input.txt", "r", stdin);
    string a, b;
    cin >> a >> b;
    cout << a << " " << b << endl;
    Pair p(a, b);
    int n;
    scanf("%d", &n);
    while(n--) {
        cin >> a >> b;
        if(a==p.first) {
            p.first = b;
        }
        else if(a == p.second) {
            p.second = b;
        }
        else if(b == p.first) {
            p.first = a;
        }
        else if(b == p.second) {
            p.second = a;
        }
        cout << p.first << " " << p.second << endl;
    }
    return 0;
}

B

有关质因子的规律题,很明显,顶多是1,2然后质数是1非质数是2就行了比较简单。

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

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

int main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    if(n <= 2) {
        cout << 1 << endl;
    }
    else cout << 2 << endl;
    int fir = 1;
    for(int i = 2; i <= n + 1; i++) {
        if(fir) {
            if (isprim(i)) {
                cout << 1 << endl;
            }
            else {
                cout << 2 << endl;
            }
            fir = 0;
        }
        else {
            if (isprim(i)) {
                cout << 1 << endl;
            }
            else {
                cout << 2 << endl;
            }
            fir = 0;
        }
    }
    return 0;
}

C
思维好题,前缀和的妙用,值得思考。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)1e5 + 10;
const ll inf = (ll)1e14;
ll sum[maxn];
map<ll, ll> mp;
set<ll> st;

int main() {
    //freopen("input.txt", "r", stdin);
    ll n, k;
    while(cin >> n >> k) {
        mp.clear();
        st.clear();
        sum[0] = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%lld", &sum[i]);
            sum[i] += sum[i-1];
        }
        ll tmp = 1;
        st.insert(tmp);
        for(int i = 0; i <= 64; i++) {
            tmp *= k;
            if(tmp > inf) break;
            st.insert(tmp);
        }
        ll ans = 0;
        for(int i = 0; i <= n; i++) {
            for(set<ll>::iterator it = st.begin(); it != st.end(); it++) {
                ans += mp[sum[i] - *it];
            }
            mp[sum[i]]++;
        }
        cout << ans << endl;
    }
    return 0;
}

还有几道题未补先占个坑,刚开始做CF感觉题目思维量很大,需要大量练习才行。加油~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值