Codeforces Round #589 (Div. 2)
Posted misuchii
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #589 (Div. 2)相关的知识,希望对你有一定的参考价值。
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 << "
";
return 0;
}
cout << "-1
";
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
";
return 0;
}
mp[j][i] = 1;
}
if (mp[x + 1][i] == 1){
cout << "0
";
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) << "
";
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 << "
";
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
";
return 0;
}
for (auto u: v1){
for (auto v: v2){
if (vec[u].find(v) == vec[u].end()){
cout << "-1
";
return 0;
}
}
}
for (auto u: v3){
for (auto v: v2){
if (vec[u].find(v) == vec[u].end()){
cout << "-1
";
return 0;
}
}
}
for (auto u: v1){
for (auto v: v3){
if (vec[u].find(v) == vec[u].end()){
cout << "-1
";
return 0;
}
}
}
for (int i = 1; i < n; i ++ )
cout << col[i] << " ";
cout << col[n] << "
";
return 0;
}
以上是关于Codeforces Round #589 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #589 (Div. 2)
Codeforces Round #589 (Div. 2)
Codeforces Round #589 (Div. 2) A. Distinct Digits