• 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 '
    '
    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 '
    '
    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 '
    '
    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;
    }
    
  • 相关阅读:
    关于pipe管道的读写端关闭问题
    线性表的链式存储——C语言实现
    关于无法解析的外部符号 _main
    Tomcat域名与服务器多对多配置
    JavaScript基础
    Vue.js入门
    SpringBoot注解大全,收藏一波!!!
    数据库连接错误
    SpringBoot入门
    MyBatis插入并返回id技巧
  • 原文地址:https://www.cnblogs.com/lukelmouse/p/12546331.html
Copyright © 2020-2023  润新知