BestCoder Round #85

算法竞赛实战技巧
本文分享了作者参加算法竞赛的经验,包括快速输入数据的方法、解决特定数学问题的策略,并提供了三个具体的编程实例,涵盖前缀和应用、优先队列操作及质数判断。

好久没打BC了,hack数据怎么输来着。。。真心手速和思维慢了好多。

A

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 1;
int a[MAXN], p[MAXN];
bool vis[5001];
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n, m; scanf("%d%d", &n, &m);
        int sum = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            sum += a[i];
            p[i] = sum % m;
        }
        memset(vis, false, sizeof(vis));
        bool flag = false;
        for(int i = 1; i <= n; i++) {
            if(p[i] == 0 || vis[p[i]]) {
                flag = true; break;
            }
            vis[p[i]] = true;
        }
        printf(flag ? "YES\n" : "NO\n");
    }
    return 0;
}

B

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <queue>
using namespace std;
typedef __int64 LL;
const int MAXN = 1e5 + 1;
int main()
{
    int t; scanf("%d", &t);
    while(t--) {
        int n, k; scanf("%d%d", &n, &k);
        priority_queue<int, vector<int>, less<int> > Q;
        for(int i = 1; i <= n - 1; i++) {
            int d; scanf("%d", &d); Q.push(d);
        }
        k--;
        while(!Q.empty() && k > 0) {
            Q.pop(); k--;
        }
        LL ans = 0;
        while(!Q.empty()) {
            ans += Q.top(); Q.pop();
        }
        printf("%I64d\n", ans + n);
    }
    return 0;
}

C

显然是y个平方数,我们直接对x开根号。只要y是由若干个单个质数相乘得到即可。
由于1e9里面素数距离不超过300,4e4里面的素数不超过4500个.
这样时间复杂度最坏O(50 * 300 * 4500),1000ms足够了。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
typedef __int64 LL;
const int MAXN = 1e5 + 1;
int p[MAXN], k;
bool judge(LL n) {
    for(int i = 0; i < k && 1LL * p[i] * p[i] <= n; i++) {
        if(n % (1LL * p[i] * p[i]) == 0) return false;
    }
    return true;
}
bool Check(int n) {
    for(int i = 2; i * i <= n; i++) {
        if(n % i == 0) return false;
    }
    return true;
}
int main()
{
    k = 0;
    for(int i = 2; i <= 40000; i++) {
        if(Check(i)) {
            p[k++] = i;
        }
    }
    //cout << k << endl;
    int t; scanf("%d", &t);
    while(t--) {
        LL x; scanf("%I64d", &x);
        LL xx = 1LL * sqrt(x * 1.0);
        LL y1, y2;
        for(y1 = xx; y1 >= 2; y1--) { // 300
            if(judge(y1)) {
                break;
            }
        }
        for(y2 = xx + 1; ; y2++) { // 300
            if(judge(y2)) {
                break;
            }
        }
        if(y1 >= 2) {
            printf("%I64d\n", min(x - y1 * y1, y2 * y2 - x));
        }
        else {
            printf("%I64d\n", y2 * y2 - x);
        }
    }
    return 0;
}

抽空补。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值