总结8

· · 个人记录

T701698 统计因子

#include<bits/stdc++.h>

using namespace std;

int n, a[1000005], ans, x;

int main(){
    cin >> n;
    for(int i = 1; i <= 1000000; i++){//遍历1到1000000中的所有数。
        for(int j = i; j <= 1000000; j += i){//枚举从i开始到1000000i所有的倍数。
            a[j]++;
        }
    }
    for(int i = 1; i <= n; i++){
        cin >> x;
        ans += a[x];
    }
    cout << ans;
    return 0;
}

思路:用埃氏筛的变形预处理出1到1000000中所有数的因数个数,然后再在输入时相加。

T701714 排队

#include<bits/stdc++.h>

using namespace std;

int n, op, x;
queue<int> q;
priority_queue<int, vector<int>, greater<int>> fq;

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> op;
        if(op == 1){
            cin >> x;
            q.push(x);
        }
        else if(op == 2){
            if(fq.size()){//如果排过序的人还有
                cout << fq.top() << '\n';
                fq.pop();
            }
            else{//没有人排过序
                cout << q.front() << '\n';
                q.pop();
            }
        }
        else{
            while(!q.empty()){//排序
                fq.push(q.front());
                q.pop();
            }
        }
    }
    return 0;
}

思路:用优先队列和普通队列来写。当到第三步排序时,将普通队里的之都存到优先队列里。

T701771 落落的刷题计划

#include<bits/stdc++.h>

using namespace std;

long long n, f[110], t;
bool h;
vector<long long>v, ans;

void dfs(int u, long long now){
    if(h){
        return;
    }
    if(now == 1){
        ans = v, h = 1;
        return;
    }
    for(int i = u; i >= 1; i--){
        if(f[i] > now || now % f[i]){
            continue;
        }
        v.push_back(i);
        dfs(i, now / f[i]);
        v.pop_back();
    }
}

int main(){
    cin >> t;
    f[1] = 2, f[2] = 3;
    for(int i = 3; i <= 70; i++){
        f[i] = f[i - 1] + f[i - 2];
    }
    while(t--){
        h = 0;
        cin >> n;
        dfs(70, n);
        if(h){
            for(auto u : ans){
                while(u--){
                    cout << "X";
                }
                cout << "Y";
            }
        }
        else{
            cout << "IMPOSSIBLE";
        }
        cout << '\n';
    }
    return 0;
}

思路:可以看出,这道题是一个斐波拉契数列。所以,我们就用数组存储这个数列,并用深搜枚举出这个数的情况,最后在输出。