AtCoder Beginner Contest 156

B

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,k,cnt = 0;
    cin >> n >> k;
    while(n) {
        n /= k;
        cnt ++;
    }
    cout << cnt ;
    return 0;
}

C

暴力

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 200;
int a[N];
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,ans = 0x3f3f3f3f;
    cin >> n;
    for(int i = 0;i < n ; ++i) {
        cin >> a[i];
    }
    for(int i = 1;i <= 100; ++i) {
        int t = 0;
        for(int j = 0;j < n; ++j) {
            t += (a[j] - i) * (a[j] - i);
        }
        ans = min(ans,t);
    }
    cout << ans << endl;
    return 0;
}

整数三分

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef double db;
typedef long long LL;
const db EPS = 1e-9;
const int N = 2e5 + 100,INF = 1 << 31 - 1;
int a[N],n;
int f(int mid) {
    int res = 0;
    for(int i = 0;i < n; ++i) {
        res += (a[i] - mid) * (a[i] - mid);
    }
    return res;
}
int main() {
    ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
    int lans,rans;
    cin >> n;
    for(int i = 0;i < n; ++i) cin >> a[i];
    int l = 1,r = 100;
    while(l < r) {
        int lmid = l + (r - l) / 3;
        int rmid = r - (r - l) / 3;
        lans = f(lmid),rans = f(rmid);
        if(lans <= rans) r = rmid - 1;
        else l = lmid + 1;
    }
    cout << min(lans,rans) << endl;
    return 0;
}

D

组合数取模,快速幂求逆元
\(ans = 2^n - 1 - C_{n}^{a} - C_{n}^{b}\)

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int mod = 1e9 + 7,N = 2e5 + 10;
typedef long long LL;
int qmi(int a,int b) {
    int res = 1 % mod;
    while(b) {
        if(b & 1) res = (LL)res * a % mod;
        a = (LL)a * a % mod;
        b >>= 1;
    }
    return res;
}
void addmod(int &x,int y) {
    x += y;
    if(x >= mod) x -= mod;
    if(x < 0) x += mod;
}
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,a,b,ans,s = 1;
    cin >> n >> a >> b;
    ans = qmi(2,n);
    addmod(ans, -1);
    for(int i = 1;i <= b; ++i) {
        s = (LL)s * (n - i + 1) % mod;
        s = (LL)s * qmi(i,mod - 2) % mod;
        if(i == a) addmod(ans,-s);
        if(i == b) addmod(ans,-s);
    }
    cout << ans << endl;
    return 0;
}
posted @ 2020-03-22 15:47  南风--  阅读(219)  评论(0编辑  收藏  举报