Codeforces Round #647 (Div. 2) A - D 解题报告

题目链接


简单写一下题解

\(A. Johnny and Ancient Computer\)

暴力

code
/*
@Author: nonameless
@Date:   2020-06-05 08:12:05
@Email:  2835391726@qq.com
@Blog:   https://www.cnblogs.com/nonameless/
*/
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const double PI  = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll LNF  = 0x3f3f3f3f3f3f;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll  gcd(ll  a, ll  b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }


int main(){

    int t; cin >> t;
    while(t --){
        ll a, b;
        cin >> a >> b;
        if(a < b) swap(a, b);
        int ans = 0;
        if(a == b) puts("0");
        else if(a % b != 0) puts("-1");
        else{
            ll t = a / b;
            for(int i = 8; i >= 2; i /= 2){
                while(t % i == 0){
                    t /= i;
                    ans ++;
                }
            }
            if(t > 1) puts("-1");
            else cout << ans << endl;
        }
    }
    return 0;
}



\(B. Johnny and His Hobbies\)

集合的数和 \(k\) 去异或得到的集合不变,那么根据异或的特性,我们让集合中的数两两异或,如果有个数出现的次数大于集合里元素的个数,那么就是符合要求的数

code
/*
@Author: nonameless
@Date:   2020-06-05 08:30:50
@Email:  2835391726@qq.com
@Blog:   https://www.cnblogs.com/nonameless/
*/
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const double PI  = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll LNF  = 0x3f3f3f3f3f3f;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll  gcd(ll  a, ll  b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }

int main(){

    int t; cin >> t;
    while(t --){
        int n; cin >> n;
        vector<int> a(n + 1);
        map<int, int> mp;
        for(int i = 1; i <= n; i ++) cin >> a[i];
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++){
                if(i == j) continue;
                int k = a[i] ^ a[j];
                mp[k] ++;
            }
        int ans = -1;
        for(auto it : mp){
           if(it.second >= n){
               ans = it.first;
               break;
           }
        }
        cout << ans << endl;
    }
    return 0;
}



\(C. Johnny and Another Rating Drop\)

打表后我们可以发现规律: \(1, 2, 1, 3, 1, 2, 1, 4...\)。预处理\(2^n\) 处的前缀和,将 \(n\) 转为二进制,求和即可。

code
/*
@Author: nonameless
@Date:   2020-06-05 09:07:15
@Email:  2835391726@qq.com
@Blog:   https://www.cnblogs.com/nonameless/
*/
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const double PI  = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll LNF  = 0x3f3f3f3f3f3f;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll  gcd(ll  a, ll  b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }

const int N = 110;

ll f[N];

ll calc(ll n){
    ll res = 0;
    string s = "";
    while(n){
        s += ('0' + n % 2);
        n >>= 1;
    }
   
    for(int i = 0; i < sz(s); i ++){
        if(s[i] == '1') res += f[i];
    }
    return res;
}

int main(){

    f[0] = 1;
    for(int i = 1; i < 64; i ++){
        f[i] = f[i - 1] * 2 + 1;
    }

    int t; cin >> t;
    while(t --){
        ll n; cin >> n;
        cout << calc(n) << endl;
    }
    
    return 0;
}



\(D. Johnny and Contribution\)

建好图,按将主题小的排序,然后对每一个点进行判断即可。

code
/*
@Author: nonameless
@Date:   2020-06-05 14:02:37
@Email:  2835391726@qq.com
@Blog:   https://www.cnblogs.com/nonameless/
*/
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const double PI  = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll LNF  = 0x3f3f3f3f3f3f;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll  gcd(ll  a, ll  b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }

const int N = 5e5 + 10, M = N << 1;

int idx = 0;
int h[N], to[M], nxt[M];

int col[N];
PII t[N];

int n, m;

void add(int u, int v){
    to[ ++ idx] = v; nxt[idx] = h[u]; h[u] = idx;
}

int main(){

    cin >> n >> m;
    for(int i = 1, u, v; i <= m; i ++){
        scanf("%d%d", &u, &v);
        add(u, v); add(v, u);
    }
    for(int i = 1, x; i <= n; i ++){
        scanf("%d", &x);
        t[i] = {x, i};
        col[i] = x;
    }
    sort(t + 1, t + n + 1);

    for(int i = 1; i <= n; i ++){
      //  cout << t[i].y << " : " << t[i].x << endl;
        int u = t[i].y;
        set<int> s;
        for(int j = h[u]; j; j = nxt[j]){
            int v = to[j];
            if(col[u] == col[v]) { puts("-1"); return 0; }
            s.insert(col[v]);
        }
        int flag = 1;
        for(int j = 1; j < col[u]; j ++){
            if(s.find(j) == s.end()){
                flag = 0;
                break;
            }
        }
        if(!flag) { puts("-1"); return 0; }
    }

    for(int i = 1; i <= n; i ++)
        printf("%d ", t[i].y);

    
    return 0;
}

posted @ 2020-06-05 21:33  nonameless  阅读(97)  评论(0编辑  收藏  举报