码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #589 (Div. 2)

时间:2019-11-05 21:56:30      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:alc   cout   ati   prime   ons   math   lin   codeforce   als   

Codeforces Round #589 (Div. 2)

A. Distinct Digits

  • 思路:水题

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

bool calc(int x){
    bool f[10] = {0};
    while (x > 0){
        if (f[x % 10])
            return false;
        else
            f[x % 10] = true;
        x /= 10;
    }
    return true;
}

int l, r;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> l >> r;
    for (int i = l; i <= r; i ++ )
        if (calc(i)){
            cout << i << "\n";
            return 0;
        }
    cout << "-1\n";
    return 0;
}

B. Filling the Grid

  • 思路:模拟

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e3 + 10;
const int mod = 1e9 + 7;

int h, w, x, ans;
int mp[N][N];

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> h >> w;
    for (int i = 1; i <= h; i ++ ){
        cin >> x;
        for (int j = 1; j <= x; j ++ )
            mp[i][j] = 1;
        mp[i][x + 1] = -1;
    }
    for (int i = 1; i <= w; i ++ ){
        cin >> x;
        for (int j = 1; j <= x; j ++ ){
            if (mp[j][i] == -1){
                cout << "0\n";
                return 0;
            }
            mp[j][i] = 1;
        }
        if (mp[x + 1][i] == 1){
            cout << "0\n";
            return 0;
        }
        mp[x + 1][i] = 1;
    }
    for (int i = 1; i <= h; i ++ )
        for (int j = 1; j <= w; j ++ )
            if (!mp[i][j])
                ans ++ ;
    cout << pow_mod(2, ans, mod) << "\n";
    return 0;
}

C. Primes and Multiplication

  • 思路:分解质因子搞搞就行

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const ll mod = 1e9 + 7;

ll x, n, ans;
vector<ll> p;

void divide(ll x){
    for (int i = 2; i * i <= x; i ++ ){
        if (x % i == 0){
            p.push_back(i);
            while (x % i == 0)
                x /= i;
        }
    }
    if (x > 1)
        p.push_back(x);
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ans = 1;
    cin >> x >> n;
    divide(x);
    for (int i = 0; i < p.size(); i ++ ){
        ll tmp = n;
        while (tmp >= p[i]){
            ans = (ans * pow_mod(p[i], tmp / p[i], mod)) % mod;
            tmp /= p[i];
        }
        ans %= mod;
    }
    cout << ans << "\n";
    return 0;
}

D. Complete Tripartite

  • 思路:用三个set搞搞

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e5 + 10;

int n, m, a, b, cnt1, cnt2, cnt3, tot;
int col[N];
bool flag[N];
vector<set<int> > vec(N);
set<int> v1, v2, v3;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    tot = 1;
    cin >> n >> m;
    for (int i = 1; i <= m; i ++ ){
        cin >> a >> b;
        vec[a].insert(b);
        vec[b].insert(a);
    }
    for (int i = 1; i <= n; i ++ ){
        if (!col[i]){
            memset(flag, true, sizeof(flag));
            col[i] = tot;
            for (auto j: vec[i])
                flag[j] = false;
            for (int j = 1; j <= n; j ++ )
                if (flag[j])
                    col[j] = tot;
            tot ++ ;
        }
    }
    for (int i = 1; i <= n; i ++ ){
        if (col[i] == 1){
            cnt1 ++ ;
            v1.insert(i);
        }
        else if (col[i] == 2){
            cnt2 ++ ;
            v2.insert(i);
        }
        else{
            cnt3 ++ ;
            v3.insert(i);
        }
    }
    if (!cnt1 || !cnt2 || !cnt3 || m != cnt1 * cnt2 + cnt1 * cnt3 + cnt2 * cnt3){
        cout << "-1\n";
        return 0;
    }
    for (auto u: v1){
        for (auto v: v2){
            if (vec[u].find(v) == vec[u].end()){
                cout << "-1\n";
                return 0;
            }
        }
    }
    for (auto u: v3){
        for (auto v: v2){
            if (vec[u].find(v) == vec[u].end()){
                cout << "-1\n";
                return 0;
            }
        }
    }
    for (auto u: v1){
        for (auto v: v3){
            if (vec[u].find(v) == vec[u].end()){
                cout << "-1\n";
                return 0;
            }
        }
    }
    for (int i = 1; i < n; i ++ )
        cout << col[i] << " ";
    cout << col[n] << "\n";
    return 0;
}

Codeforces Round #589 (Div. 2)

标签:alc   cout   ati   prime   ons   math   lin   codeforce   als   

原文地址:https://www.cnblogs.com/Misuchii/p/11801518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!