Codeforces Round #589 (Div. 2)
Posted carered
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #589 (Div. 2)相关的知识,希望对你有一定的参考价值。
链接:https://codeforces.com/contest/1228
A:签到题,暴力 从L 到 R 搜过去。
1 #include<bits/stdc++.h> 2 using namespace std; 3 bool check(int x) 4 5 int vis[10] = 0; 6 while(x) 7 if(vis[x % 10]) return false; 8 vis[x % 10]++; 9 x /= 10; 10 11 return true; 12 13 int main() 14 15 std::ios::sync_with_stdio(false); 16 int l, r; 17 cin >> l >> r; 18 for(int i = l;i <= r;i++) 19 if(check(i)) 20 cout << i << endl; 21 return 0; 22 23 24 cout << -1 << endl; 25 return 0; 26
B:每行每列可以确定 r[i]+1 和 c[i ] + 1的位置的颜色,如果冲突输出0,否则输出2^不确定颜色的块数。
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e3 + 50; 4 const int mod = 1000000007; 5 typedef long long ll; 6 int r, c; 7 int mp[maxn][maxn]; 8 ll pow_mod(ll x, ll n) 9 ll res = 1; 10 while(n) 11 if(n & 1) res = res * x % mod; 12 x = x * x % mod; 13 n >>= 1; 14 15 return res; 16 17 int main() 18 19 std::ios::sync_with_stdio(false); 20 int n, m; 21 cin >> n >> m; 22 int ans = 0; 23 bool flag = false; 24 for(int i = 0;i < n;i++) 25 cin >> r; 26 for(int j = 0;j < r;j++) 27 mp[i][j] = 1; 28 mp[i][r] = 2; 29 30 for(int i = 0;i < m;i++) 31 32 cin >> c; 33 for(int j = 0;j < c;j++) 34 if(mp[j][i] && mp[j][i] != 1)flag = true; 35 else mp[j][i] = 1; 36 37 if(mp[c][i] && mp[c][i] != 2) flag = true; 38 else mp[c][i] = 2; 39 40 if(flag) 41 cout << 0 << endl; 42 return 0; 43 44 for(int i = 0;i < n;i++) 45 for(int j = 0;j < m;j++) 46 if(!mp[i][j]) ans++; 47 cout << pow_mod(2, ans) % mod << endl; 48 return 0; 49
C:就求n!里x的素因子的出现的次幂的乘积。
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int mod = 1e9 + 7; 5 vector<ll> vec; 6 void init(ll n) 7 8 for(ll i = 2;i * i <= n;i++) 9 if(n % i == 0) 10 11 vec.push_back(i); 12 while(n % i == 0) n /= i; 13 14 15 if(n > 1) vec.push_back(n); 16 17 ll pow_mod(ll x, ll n) 18 ll res = 1; 19 while(n) 20 if(n & 1) res = res * x % mod; 21 x = x * x % mod; 22 n >>= 1; 23 24 return res % mod; 25 26 ll fact_pow(ll n, ll k) 27 ll res = 0; 28 while (n) 29 n /= k; 30 res += n; 31 32 return res; 33 34 int main() 35 36 std::ios::sync_with_stdio(false); 37 ll x, n; 38 cin >> x >> n; 39 init(x); 40 ll ans = 1; 41 for(int i = 0;i < vec.size();i++) 42 ll k = fact_pow(n, vec[i]); 43 ans = ans * pow_mod(vec[i], k) % mod; 44 45 cout << ans % mod <<endl; 46 return 0; 47
D:三分图染色,首先对1染色,然后所有与1连线的边不是2就是3,这些边连线的边就是1,同理再染色2。最后判断满不满足题目条件即可。
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 4e5+ 5; 4 vector<int> G[maxn]; 5 int du[maxn]; 6 int color[maxn]; 7 int a[maxn], b[maxn]; 8 int far[maxn]; 9 vector<int> C[4]; //存每个分图的点 10 int n, m; 11 void dfs(int v) 12 13 for(int i = 0;i < G[v].size();i++) 14 15 int u = G[v][i]; 16 if(color[u]) continue; 17 color[u] = 3; 18 C[3].push_back(u); 19 20 21 int main() 22 23 std::ios::sync_with_stdio(false); 24 cin >> n >> m; 25 //建图 26 for(int i = 1;i <= m;i++) 27 cin >> a[i] >> b[i]; 28 G[a[i]].push_back(b[i]); 29 G[b[i]].push_back(a[i]); 30 du[a[i]]++; 31 du[b[i]]++; 32 33 //染色1. 34 color[1] = 1; 35 C[1].push_back(1); 36 far[1]= 1; 37 for(int i = 0;i < G[1].size();i++) far[ G[1][i] ] = 1; 38 for(int i = 1;i <= n;i++) 39 40 if(far[i] == 0) 41 42 color[i] = 1; 43 C[1].push_back(i); 44 45 46 //染色2 47 for(int i = 1;i <= n;i++) 48 49 if(!color[i]) 50 51 color[i] = 2; 52 C[2].push_back(i); 53 54 dfs(i);//染色3 55 56 57 if(C[1].size() + C[2].size() + C[3].size() != n) 58 59 cout << -1 << endl; 60 return 0; 61 62 if(C[1].size() == 0 || C[2].size() == 0 || C[3].size() == 0) 63 64 cout << -1 << endl; 65 return 0; 66 67 for(int i = 1;i <= n;i++) 68 69 if(du[i] != n - C[color[i]].size())//检查每个点是不是对其他俩个集合的点都有边 70 71 cout << -1 << endl; 72 return 0; 73 74 75 for(int i = 0;i < C[1].size();i++) far[C[1][i] ] = 1; 76 for(int i = 0;i < C[2].size();i++) far[C[2][i] ] = 2; 77 for(int i = 0;i < C[3].size();i++) far[C[3][i] ] = 3; 78 for(int i = 1;i <= m;i++) 79 if(far[a[i]] == far[b[i]]) 80 81 cout << -1 << endl; 82 return 0; 83 84 85 for(int i = 1;i <= n;i++) 86 cout << color[i] << " "; 87 88 cout << endl; 89 return 0; 90
以上是关于Codeforces Round #589 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #589 (Div. 2)
Codeforces Round #589 (Div. 2)
Codeforces Round #589 (Div. 2) A. Distinct Digits