牛客周赛 Round 101(题解的token计算, 76修地铁 ,76选数,76构造,qcjj寄快递,幂中幂plus)

A题解的token计算

要记住c++中的对数函数:

  1. log(n) 是自然对数(以e为底)ln(n

  2. log10(n) 是以10为底的对

  3. log1p(n) 是ln(1+n),提供更高的数值精

  4. log2(n) 是以2为底的对

  5. logl(n)log10l(n)long double

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int main(){
	ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    double e = 2.718281828;
    double n;
    cin >> n;
    double w = 150 / log2l(e)* log2l(n) ;
    cout << setprecision(6) << w << endl;
    return 0;
}

B 76修地铁

这题你得先理解图:黄色是普通火把;红色是红石火把;粉色是普通铁轨;灰色是动力铁

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int main(){
	ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    int n;
    cin >> n;
    int w = (n / 5) * 2;//普通火把
    int q = ((n + 5) / 10) * 1;//红石火把
    int r = (n / 20) * 3;//普通铁轨
    int t = n * 2 - r / 3 * 2;//动力铁轨
    cout << w << " " << q << " " << r << " " << t << endl;
    return 0;
}

C76选数

最大值就是在n所有二进制为都满的情况下(选数的话也是选二进制对应的十进制值)

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int main(){
	ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    ll n;
    cin >> n;
    ll w = 1;
    ll sum = 0;
    while (n > 0) {
        sum += w;
        n /= 2;
        w *= 2;
    }
    cout << sum << endl;
    return 0;
}

D76构造

把他当作二进制看:

1的gcd()=1所以m为奇数 不成立

n组成的数最大设为max_n,max_n=n的二进制位都为1;max_n<m不成立

成立的打印:

m二进制为1的位置转化成十进制当作一个区间(先省略掉1),剩下的和1组成一个大区间

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int n, m;
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> n >> m;
    int sum = 0;
    int y = m;
    int x = 1;
    vector<ll> a;
    while (y > 0) {
        if (y % 2 == 1 && x != 1) {
            a.push_back(x);
            sum++;
        }
        y /= 2;
        x *= 2;

    }
    int y0 = n;
    int x0 = 1;
    while (y0 > 0) {
        y0 /= 2;
        x0 *= 2;

    }
    if (m % 2 == 0 || x0 <= m) {
        cout << -1 << endl;
    }
    else {
        for (int i = 0; i < a.size(); i++) {
            cout << a[i] << " ";
        }
        for (int i = 1; i <= n; i++) {
            int j = 0;
            for (j = 0; j < sum; j++) {
                if (i == a[j]) {
                    break;
                }
            }
            if (j == sum) {
                cout << i << " ";
            }
        }
        cout << endl;
        cout << sum + 1 << endl;
        for (int i = 1; i <= sum; i++) {
            cout << i << " " << i << endl;
        }
        cout << sum + 1 << " " << n << endl;
    }
    return 0;
}

Eqcjj寄快递

纯纯数学题,就是求 ,然后求导,但要注意ki的最小值不能为负数

得出ki=log2l(b[i] * ln2)时,ti最小,但ki<0时不成立,ki取0

E幂中幂plus

找规律+快速幂+前缀和
m只有1e6 且每次的结果只与当前的c有关 故一定会有循环
但是不一定从第一个数开始循环

base可能很大 可以一开始先对base取一次模

前缀和下标从1开始

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
ll base, c, mod;
int q;
ll k;
ll power(ll x, ll y) {
    ll sum = 1;
    x %= mod;
    while (y > 0) {
        if (y % 2 == 1) {
            sum = sum * x % mod;
        }
        x = x * x % mod;
        y /= 2;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> base >> c >> mod;
    base %= mod;
    if (mod==1||base==0) {
        cin >> q;
        for (int i = 0; i < q; i++) {
            cin >> k;
            cout << 0 << endl;
        }
    }
    else if (base == 1) {
        cin >> q;
        for (int i = 0; i < q; i++) {
            cin >> k;
            cout << k%mod << endl;
        }
    }
    else{
        vector<ll> a;
        map<ll, bool>p;
        ll w;
        while (true) {
            w = power(base, c);
            if (p.find(w) == p.end()) {
                p.insert({ w,true });
                a.push_back(w);
            }
            else {
                break;
            }
            c = w;
        }
        int j;
        for (j = 0; j < a.size(); j++) {
            if (a[j] == w) {
                break;
            }
        }
        vector<ll> b(a.size() + 1);
        b[0] = 0;
        for (int i = 1; i <= a.size(); i++) {
            b[i] = (b[i-1] + a[i - 1]) % mod;
        }
        int z = a.size() - j;
        cin >> q;
        for (int i = 0; i < q; i++) {
            cin >> k;
            if (k > a.size()) {
                cout << ((b[a.size()] - b[j] + mod) % mod * (((k - j) / z) % mod) % mod + b[j  + (k - j) % z]) % mod << endl;
            }
            else {
                cout << b[k ]%mod << endl;
            }
        }
    }
    return 0;
}

前缀和下标从0开始:

cpp 复制代码
/*
这种容易犯错
*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
ll base, c, mod;
int q;
ll k;
ll power(ll x, ll y) {
    ll sum = 1;
    while (y > 0) {
        if (y % 2 == 1) {
            sum = sum * x % mod;
        }
        x = x * x % mod;
        y /= 2;
    }
    return sum;
}
int main() {
    ios::sync_with_stdio(false);        // 禁用同步
    cin.tie(nullptr);                   // 解除cin与cout绑定
    cin >> base >> c >> mod;
    base %= mod;
    if (mod==1||base==0) {
        cin >> q;
        for (int i = 0; i < q; i++) {
            cin >> k;
            cout << 0 << endl;
        }
    }
    else if (base == 1) {
        cin >> q;
        for (int i = 0; i < q; i++) {
            cin >> k;
            cout << k%mod << endl;
        }
    }
    else{
        vector<ll> a;
        map<ll, bool>p;
        ll w;
        while (true) {
            w = power(base, c);
            if (p.find(w) == p.end()) {
                p.insert({ w,true });
                a.push_back(w);
            }
            else {
                break;
            }
            c = w;
        }
        int j;
        for (j = 0; j < a.size(); j++) {
            if (a[j] == w) {
                break;
            }
        }
        for (int i = 1; i < a.size(); i++) {
            a[i] = (a[i] + a[i - 1]) % mod;
        }
        int z = a.size() - j;
        cin >> q;
        for (int i = 0; i < q; i++) {
            cin >> k;
            if (k > a.size()) {
                cout << ((a[a.size() - 1] - (j - 1 >= 0 ? a[j - 1] : 0)+ mod) % mod * (((k - j) / z) % mod) % mod + (j - 1 + (k - j) % z >= 0 ? a[j - 1 + (k - j) % z] : 0)) % mod << endl;
            }
            else {
                cout << a[k - 1]%mod << endl;
            }
        }
    }
    return 0;
}
相关推荐
鹅毛在路上了1 天前
C++, ffmpeg, libavcodec-RTSP拉流,opencv实时预览
c++·opencv·ffmpeg
John_ToDebug1 天前
定制 ResourceBundle 的实现与 DuiLib 思想在 Chromium 架构下的应用解析
c++·chrome·ui
蒋星熠1 天前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
小欣加油1 天前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
3Cloudream1 天前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等
王璐WL1 天前
【c++】c++第一课:命名空间
数据结构·c++·算法
aramae1 天前
C++ -- 模板
开发语言·c++·笔记·其他
空白到白1 天前
机器学习-聚类
人工智能·算法·机器学习·聚类
索迪迈科技1 天前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法
zzzsde1 天前
【数据结构】队列
数据结构·算法