Codeforces Round #589 div.2 C,D

This is a sense of the complexity of the very metaphysics ... I may be lazy too long becomes dishes QAQ.

C

  • The meaning of problems: given \ (x, n \) , find x from a quality factor to n g (i, p) multiplicative
  • Thinking: obtaining each of the prime factors of x, n multiplied to calculate directly connected to.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> VI;
vector<int> prime;
const int MOD = 1e9+7;
void get(ll x){
    for(int i=2;i*i<=x;++i){
        if(x%i==0){
            prime.push_back(i);
            while(x%i==0)   x/=i;
        }
    }
    if(x!=1)   prime.push_back(x);
}
ll qpow(ll a,ll b){
    ll res =1;
    while(b){
        if(b&1) res = res*a%MOD;
        a = a*a%MOD;
        b>>=1;
    }
    return res;
}
const int N = 1e5+10;
int cnt[N];
int main(){
    ll x,n;
    ll ans = 1;
    cin >> x >> n;
    get(x);
    for(auto p:prime){
        ll res = 1;
        while(res<=n/p){
            res*=p;
            ans = ans * qpow(p,n/res)%MOD;  // 不是res的n/res次方 而是 p的n/res次方 这样可以避免乘过之后影响前面
        }
    }
    ll t = (ll)ans;
    cout << t << endl;
}

Have been thinking about how the inclusion-exclusion game, let p take over in the future will not affect the front, but look at other people find the code does not require the inclusion-exclusion, directly still can do at the end with p

D

  • Meaning of the questions: given a map, so you put this figure thirds (analog bipartite graph).
  • Ideas: violence points, but will spread to check the condition 1. pick a point u not dyed dyeing, if v and u no sides, and v is not dyed, then u v dyed colors.
    2. repeat 1 3. Analyzing all three points are dyed, the three colors are present and 4. Analyzing m (number of edges) == |. col1 | * | col2 | + | col1 | * | col3 | + | col2 | * | col3 |., because a different set directly for each point there is an edge to be two points 5. Analyzing whether there is an edge different sets of
#include<bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> VI;

const int N = 1e5+10;
vector<int> G[N];
int head[N],tot;
int cnt[4];
vector<int> block[4];
int color[N];
void add(int u,int v){G[u].push_back(v);}
int n,m;
int main(){
    scanf("%d%d",&n,&m);
    int u,v;
    for(int i=1;i<=m;++i){
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    for(int col = 1;col<=3;++col){
        int idx = 0;
        for(int i=1;i<=n;++i)   if(color[i]==0){idx = i;break;}
        if(idx ==0){
            color[1] = 0;   break;
        }
        color[idx] = col;
        for(auto v:G[idx]){
            if(!color[v])   color[v] = -1;
        }
        for(int i=1;i<=n;++i){
            if(color[i]==0) color[i] = col;
            if(color[i]==-1)    color[i] = 0;
        }
    }
    for(int i=1;i<=n;++i){
        cnt[color[i]]++;
        block[color[i]].push_back(i);
    }
    int sign = 0;
    if(cnt[0] || !cnt[1] || !cnt[2] || !cnt[3] || m!= cnt[1]*cnt[2] + cnt[2]*cnt[3] + cnt[1]*cnt[3]){
        sign = 1;
    }
    for(auto u:block[1]){
        sort(G[u].begin(),G[u].end());
        for(auto v:block[2]){
            auto it = lower_bound(G[u].begin(),G[u].end(),v);
            if(it == G[u].end() || *it!=v) sign = 1;
        }
        for(auto v:block[3]){
            auto it = lower_bound(G[u].begin(),G[u].end(),v);
            if(it == G[u].end() || *it!=v) sign = 1;
        }
    }
    for(auto u:block[2]){
        sort(G[u].begin(),G[u].end());
        for(auto v:block[3]){
            auto it = lower_bound(G[u].begin(),G[u].end(),v);
            if(it == G[u].end() || *it!=v) sign = 1;
        }
    }
    if(sign){
        puts("-1");
        return 0 ;
    }
    for(int i=1;i<=n;++i){
        printf("%d ",color[i]);
    }
    puts("");
    return 0;
}

4 and 5 of the sensory judgment are repeated, but does not actually time out determined 5

Guess you like

Origin www.cnblogs.com/xxrlz/p/11612329.html