哈尔滨理工大学第12届程序设计竞赛(同步赛)非官方题解

哈尔滨理工大学第12届程序设计竞赛(同步赛)

https://ac.nowcoder.com/acm/contest/30825#question

蒟蒻第一次打牛客QAQ 我真的好菜啊。。就是签到选手,而且还做得很慢,老错。卑微补题ing

A.割韭菜

B.抓球

题目

思路

快速幂求逆元
错因:快速幂了,但幂的不彻底

代码

#include <iostream>
#include <algorithm>
#include <cmath>
#define IOS ios::sync_with_stdio (0); cin.tie(0); cout.tie(0);

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7, N = 1e5 + 5;
ll a[N];

inline ll ksm(ll x, ll y) {
    ll ans1 = 1;
    while (y) {
        if (y & 1)
            ans1 = 1ll * ans1 * x % mod;
        y >>= 1;
        x = 1ll * x * x % mod;
    }
    return ans1;
}

ll mi (ll a, ll b) {
    return (a * ksm (b, mod - 2)) % mod;
}//分数取模

int main() {
    IOS;
    int t;
    cin >> t;

    //初始化要放在往外面!!
    a[0] = 1; //don't forget init
    for (int i = 1; i <= N; i ++)
        a[i] = (a[i - 1] * i) % mod; //有规律可循的东西最好先预处理

    while (t--) {
        ll n, m, k, q;
        cin >> n >> m >> k >> q;

        if (k > n) {
            cout << 0 << endl;
            continue;
        }

        ll dx = mi (a[n], a[n - k]) % mod;
        ll dy = mi (a[n + m], a[n + m - k]) % mod;
        dx = ksm (dx, q) % mod;
        dy = ksm (dy, q) % mod;

        ll ans = mi (dx, dy) % mod;

        cout << ans % mod << endl;
    }
}

// a/b对质数p取模就是 a*b^(p-2) mod p

C.迷宫

D.gk的爬山之旅

E.gk的字符串

题目

思路

贪心,a b c...挨个往后填 不能与前后相同
注意:最后一个要单独判断,我第一次就是因为这个RE了

代码

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
const int N = 1e6 + 5;

int main () {
    int t;
    cin >> t;
    while (t --) {
        string s;
        cin >> s;
        s = ' ' + s;
        int n = s.size();
        
        for (int i = 1; i < n; i ++) {
            if (s[i] == '?') {

                for (int j = 0; j < 26; j ++)
                    if (s[i - 1] != (char)(j + 'a') && s[i + 1] != (char)(j + 'a')) {
                        s[i] = (char)(j + 'a');
                        break;
                    }
            }
                
        }
        for (int j = 0; j < 26; j ++)
            if (s[n - 1] != (char)(j + 'a')) 
                s[n] = (char)(j + 'a');
        
        cout << s.substr(1) << endl;
        // for (int i = 1; i <= n; i ++)
        //     cout << s[i];
        // cout << endl;
    }
    
}

F. gk的树

题目


思路

节点度数就是该点拥有子结点的数量
dfs
对于我来说稍稍有点理解难度的dfs,详见注释

代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;
const int N = 100000 + 5;
int n, k, ans = 0;
bool vis[N];
vector <int> v[N];

//关键要理解这里
int dfs (int u) {
    int len = v[u].size(); //初始度数
    for (auto i : v[u]) {  //遍历所连边
        if (vis[i])
            continue;
        vis[i] = true;
        len -= dfs (i);  //需要剪枝就-1,没被剪过就咔嚓剪掉
    }
    if (len <= k)
        return 0;  //不需要操作
    ans += len - k;  //多出来了多少边
    return 1;
}

int main () {
    int t;
    cin >> t;
    while (t --) {
        ans = 0;
        memset (vis, false, sizeof vis);
        cin >> n >> k;

        for (int i = 1; i < n; i ++){
            int u, x;
            cin >> u >> x;
            v[x].push_back (u), v[u].push_back (x);
        }

        vis[1] = true;
        dfs (1);
        cout << ans << endl;  
        
        for (int i = 0; i <= n; i ++)
            v[i].clear();

    }
   
}

G.gk的数字游戏

题目


思路

这个东西是一个取模!!!我没看出来QAQ

代码

//先化简
#include <iostream>
#include <algorithm>
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);

using namespace std;

int main () {
    IOS;
    int t;
    cin >> t;
    while (t --) {
        int n, m;
        cin >> n >> m;
        if (n == 0 || m == 0) {
            cout << 0 << endl;
            continue;
        }

        int cnt = 0;
        while (m && n) {
            if (n < m) {
                cnt += m / n;
                m %= n;
            }

            else {
                cnt += n / m;
                n %= m;
            }
        }
        
        cout << cnt << endl;
    }
}

H. 进来DP

I. 又AK了

题目


思路

优先做罚时少的题目,然后挨个加起来

代码

#include <iostream>
#include <algorithm>

using namespace std;

int main () {
    int t;
    cin >> t;
    while (t --) {
        int n; 
        cin >> n;
        int a[25] = {0}, b[25];
        for (int i = 1; i <= n; i ++)
            cin >> a[i];
        sort (a +  1, a + n + 1);

        for (int i = 1; i <= n; i ++)
            b[i] = a[i] - a[i - 1];
        sort (b + 1, b + n + 1);

        // for (int i = 1; i <= n; i ++)
        //     cout << b[i] << ' ';
        // cout << endl;

        for (int i = 1; i <= n; i ++)
            b[i] += b[i - 1];
        long long sum = 0;
        for (int i = 1; i <= n; i ++)
            sum += b[i];
        cout << sum << endl;
    }
}

J.大数乘法

题目


思路

想到了按位求快速幂,但是还有一步就是要先预处理,因为\(x^{10^i}\)是固定的。
公式:把y按位拆解,每一位都变成\(a[i] * 10^i\),那么每一位\(x^{a[i]*10^i}\)相乘就是所求

代码

#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>

using namespace std;
const int N = 100000 + 5;

int main () {
    int t;
    cin >> t;
    while (t --) {
        long long x, p;
        string s;
        int a[N];
        long long b[N];
        cin >> x >> s >> p;
        int n = s.size();

        long long ans = 1;
        for (int i = n - 1; i >= 0; i --) {
            long long tt = x;
            for (int j = 1; j <= 9; j ++) {
                if (j == s[i] - '0')
                    ans = (ans * x) % p;
                x = (tt * x) % p;
            }
            
        }
        
        cout << ans << endl;
    }
}

K.蹦蹦炸弹

L.NP-Hard

题目

思路

就是直接模拟进制转换。。。我以为是CF签到题那种,就一直在找规律,哭死

代码

#include <iostream>
 
using namespace std;
int n;
 
int count (int q) {
    int cnt = 0, tmp = n;
    while (tmp) {
        if (tmp % q == 1)
            cnt ++;
        tmp /= q;
    }
    return cnt;
}
 
int main () {
    int t;
    cin >> t;
    while (t --) {
        int x, y;
        cin >> n >> x >> y;
        int xx = count (x), yy = count (y);
        //cout << xx << ' ' << yy << endl;
        if (xx == yy)
            cout << '=' << endl;
        if (xx > yy)
            cout << '>' << endl;
        if (xx < yy)
            cout << '<' << endl;    
         
    }
}
posted @ 2022-04-04 12:36  Sakana~  阅读(56)  评论(1编辑  收藏  举报