Codeforces Round #589 (Div. 2)
Contest Info
[Practice Link](https://codeforces.com/contest/1228)
| Solved | A | B | C | D | E | F |
|---|---|---|---|---|---|---|
| 5/6 | O | O | O | O | Ø | - |
- O 在比赛中通过
- Ø 赛后通过
- ! 尝试了但是失败了
- - 没有尝试
Solutions
A. Distinct Digits
签到。
B. Filling the Grid
签到。
C. Primes and Multiplication
题意:
定义\(prime(x)\)为\(x\)的所有质因子构成的集合。
定义\(g(x, p)\)为最大的\(p^k\)满足\(p^k \;|\; x\),
定义\(f(x, y)\)为:
\prod\limits_{p \in prime(x)} g(y, p)
\end{eqnarray*}
\]
现在给出\(x, n\),要求计算:
\]
思路:
枚举\(x\)的每个质因子,再从高到低枚举每个质因子的幂次,考虑对于一个质因子\(p\),用\(f[i]\)表示\([1, n]\)中有多少个\(p^i\)的倍数,且不是\(p^j (j > i)\)的倍数,那么个数是\(\left\lfloor n / p^i \right\rfloor - \left\lfloor n / p^{i + 1} \right\rfloor\)
代码:
view code
#include <bits/stdc++.h>
using namespace std;
#define debug(...) { printf("# "); printf(__VA_ARGS__); puts(""); }
#define fi first
#define se second
#define endl "\n"
using ll = long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
using VI = vector <int>;
using VL = vector <ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }
template <class T> inline void out(T s) { cout << s << "\n"; }
template <class T> inline void out(vector <T> &vec) { for (auto &it : vec) cout << it << " "; cout << endl; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
constexpr int N = 1e5 + 10;
ll x, n, bit[110];
inline ll ceil(ll x, ll y) {
return (x + y - 1) / y;
}
void run() {
vector <int> fac;
for (ll i = 2; i * i <= x; ++i) {
if (x % i == 0) fac.push_back(i);
while (x % i == 0) x /= i;
}
if (x != 1) fac.push_back(x);
ll res = 1;
for (auto &it : fac) {
if (it > n) continue;
int k = 1; bit[1] = it;
for (int i = 2; ; ++i) {
if (bit[i - 1] > ceil(n, it)) {
k = i - 1;
break;
}
bit[i] = bit[i - 1] * it;
}
ll tot = 0;
for (int i = k; i >= 1; --i) {
ll p = n / bit[i];
p -= tot;
res = res * qpow(bit[i] % mod, p % (mod - 1)) % mod;
tot += p;
}
}
out(res);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cout << fixed << setprecision(20);
while (cin >> x >> n) run();
return 0;
}
D. Complete Tripartite
题意:
现在给出一张\(n\)个点\(m\)条边的无向图,没有自环和重边, 问能否将点分成三个集合,使得集合内部的点之间没有边相连,但任意两个点(他们分属不同的集合)有边相连。
如果可以,输出方案。
思路:
考虑同一点集里所有的点连出去的边都是相同的,那么根据这个\(Hash\),如果刚好有三种\(Hash\)值,那么按\(Hash\)值分类即可
代码:
view code
#include <bits/stdc++.h>
using namespace std;
#define debug(...) { printf("# "); printf(__VA_ARGS__); puts(""); }
#define fi first
#define se second
#define endl "\n"
using ll = long long;
using ull = unsigned long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
using VI = vector <int>;
using VL = vector <ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }
template <class T> inline void out(T s) { cout << s << "\n"; }
template <class T> inline void out(vector <T> &vec) { for (auto &it : vec) cout << it << " "; cout << endl; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
constexpr int N = 3e5 + 10;
int n, m, ans[N];
mt19937 rnd(time(0));
ull f[N], g[N];
map <ull, vector<int>> mp;
void run() {
for (int i = 1; i <= n; ++i) f[i] = rnd();
memset(g, 0, sizeof g);
mp.clear();
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
g[u] ^= f[v];
g[v] ^= f[u];
}
for (int i = 1; i <= n; ++i) {
mp[g[i]].push_back(i);
if (mp.size() > 3) return out(-1);
}
if (mp.size() != 3) return out(-1);
int cnt = 0;
for (auto &it : mp) {
++cnt;
for (auto &u : it.second) {
ans[u] = cnt;
}
}
for (int i = 1; i <= n; ++i)
cout << ans[i] << " \n"[i == n];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cout << fixed << setprecision(20);
while (cin >> n >> m) run();
return 0;
}
E. Another Filling the Grid
题意:
给出一个\(n \cdot n\)的矩形,每个位置可以填\([1, k]\)。
现在要求每一行至少有一个\(1\),每一列至少有一个\(1\),问填数的方案数。
思路一:
考虑\(f[i][j]\)表示考虑前\(i\)行有\(j\)列有\(1\),转移的时候注意每一行至少有一个\(1\)。
时间复杂度\(O(n^3)\)
代码一:
view code
#include <bits/stdc++.h>
using namespace std;
#define debug(...) { printf("# "); printf(__VA_ARGS__); puts(""); }
#define fi first
#define se second
#define endl "\n"
using ll = long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
using VI = vector <int>;
using VL = vector <ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }
template <class T> inline void out(T s) { cout << s << "\n"; }
template <class T> inline void out(vector <T> &vec) { for (auto &it : vec) cout << it << " "; cout << endl; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
constexpr int N = 300 + 10;
int n, K; ll f[N][N], C[N][N];
void run() {
if (n == 1 || K == 1) return out(1);
memset(f, 0, sizeof f);
for (int i = 1; i <= n; ++i) {
f[1][i] = C[n][i] * qpow(K - 1, n - i) % mod;
}
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
ll p = qpow(K, j);
for (int k = j; k <= n; ++k) {
if (k == j) chadd(f[i][k], f[i - 1][j] * (p + mod - qpow(K - 1, j)) % mod * qpow(K - 1, n - k) % mod);
else chadd(f[i][k], f[i - 1][j] * p % mod * C[n - j][k - j] % mod * qpow(K - 1, n - k) % mod);
}
}
}
out(f[n][n]);
}
int main() {
memset(C, 0, sizeof C);
C[0][0] = 1;
for (int i = 1; i < N; ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cout << fixed << setprecision(20);
while (cin >> n >> K) run();
return 0;
}
思路二:
考虑枚举有\(i\)行\(j\)列没有\(1\),然后根据\((i + j)\)的奇偶性容斥。
时间复杂度\(O(n^2)\)
代码二:
view code
#include <bits/stdc++.h>
using namespace std;
#define debug(...) { printf("# "); printf(__VA_ARGS__); puts(""); }
#define fi first
#define se second
#define endl "\n"
using ll = long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
using VI = vector <int>;
using VL = vector <ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }
template <class T> inline void out(T s) { cout << s << "\n"; }
template <class T> inline void out(vector <T> &vec) { for (auto &it : vec) cout << it << " "; cout << endl; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
constexpr int N = 300 + 10;
int n, K; ll f[N][N], C[N][N];
void run() {
if (n == 1 || K == 1) return out(1);
ll ans = 0;
//枚举有i行,j列没有1,容斥
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
ll ch = i * n + j * n - i * j;
ll ex = n * n - ch;
ll now = C[n][i] * C[n][j] % mod * qpow(K - 1, ch) % mod * qpow(K, ex) % mod;
if ((i + j) & 1) chadd(ans, mod - now);
else chadd(ans, now);
}
}
out(ans);
}
int main() {
C[0][0] = 1;
for (int i = 1; i < N; ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cout << fixed << setprecision(20);
while (cin >> n >> K) run();
return 0;
}
思路三:
考虑枚举至少有\(i\)行没有\(1\),那么保证每一列都至少有一个\(1\),那么答案就是:
\sum\limits_{i = 0}^{n - 1} (-1)^i{n \choose i}(k - 1)^{in}f^n(n - i)
\end{eqnarray*}
\]
其中\(f[i]\)表示有\(i\)个数,至少有一个\(1\)的方案数,显然有:
f[i] = k^{i} - (k - 1)^i
\end{eqnarray*}
\]
时间复杂度\(O(nlogk)\)
代码三:
view code
#include <bits/stdc++.h>
using namespace std;
#define debug(...) { printf("# "); printf(__VA_ARGS__); puts(""); }
#define fi first
#define se second
#define endl "\n"
using ll = long long;
using pII = pair <int, int>;
using pLL = pair <ll, ll>;
using VI = vector <int>;
using VL = vector <ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; while (x >= mod) x -= mod; while (x < 0) x += mod; }
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T> inline void rd(vector <T> &vec) { for (auto &it : vec) cin >> it; }
template <class T> inline void out(T s) { cout << s << "\n"; }
template <class T> inline void out(vector <T> &vec) { for (auto &it : vec) cout << it << " "; cout << endl; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
constexpr int N = 300 + 10;
int n, K; ll f[N], C[N][N];
void run() {
if (n == 1 || K == 1) return out(1);
ll ans = 0;
//枚举有i行没有1,然后保证每列至少有一个1,容斥
for (int i = 0; i < n; ++i) {
ll f = (qpow(K, n - i) - qpow(K - 1, n - i) + mod) % mod;
ll now = qpow(f, n) * C[n][i] % mod * qpow(K - 1, n * i) % mod;
if (i & 1) chadd(ans, -now);
else chadd(ans, now);
}
out(ans);
}
int main() {
C[0][0] = 1;
for (int i = 1; i < N; ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cout << fixed << setprecision(20);
while (cin >> n >> K) run();
return 0;
}
Codeforces Round #589 (Div. 2)的更多相关文章
- Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理
Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\) ...
- Codeforces Round 589 (Div. 2) 题解
Is that a kind of fetishism? No, he is objectively a god. 见识了一把 Mcdic 究竟出题有多神. (虽然感觉还是吹过头了) 开了场 Virt ...
- Codeforces Round #589 (Div. 2) E. Another Filling the Grid(DP, 组合数学)
链接: https://codeforces.com/contest/1228/problem/E 题意: You have n×n square grid and an integer k. Put ...
- Codeforces Round #589 (Div. 2) D. Complete Tripartite(染色)
链接: https://codeforces.com/contest/1228/problem/D 题意: You have a simple undirected graph consisting ...
- Codeforces Round #589 (Div. 2) C - Primes and Multiplication(数学, 质数)
链接: https://codeforces.com/contest/1228/problem/C 题意: Let's introduce some definitions that will be ...
- Codeforces Round #589 (Div. 2) B. Filling the Grid
链接: https://codeforces.com/contest/1228/problem/B 题意: Suppose there is a h×w grid consisting of empt ...
- Codeforces Round #589 (Div. 2) A. Distinct Digits
链接: https://codeforces.com/contest/1228/problem/A 题意: You have two integers l and r. Find an integer ...
- Codeforces Round #589 (Div. 2) (e、f没写)
https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每 ...
- 【Codeforces Round #589 (Div. 2) D】Complete Tripartite
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...
随机推荐
- Scratch零基础起步攻略(一)
通常,类似这样的文章开头总要阐述一大段关于编程的重要性,还有自己的专业性.权威性等等,我就都省掉了…… 简单介绍一下自己,从事计算机编程教育前前后后有近20年了,面对了不同年龄层次的学员,大部分跟着我 ...
- X64驱动:内核操作进线程/模块
注意:下面的所有案例必须使用.C结尾的文件,且必须在链接选项中加入 /INTEGRITYCHECK 选项,否则编译根本无法通过(整合修正,Win10可编译,须在测试模式下进行),内核代码相对固定,如果 ...
- 网页调试js时,如何知道某个事件对应哪段js代码?
有时候我们需要知道某个事件对应的js代码,比如点击一个div元素时,出现下拉框,我想知道这个功能对应的js代码,那就可以按下图操作: 勾选click事件,重新运行,那么就会在每个click事件那里设置 ...
- 使用VS2012编译和使用C++ STL(STLport)
使用VS2012编译和使用C++ STL(STLport) http://cstriker1407.info/blog/use-vs2012-to-compile-and-use-the-c-stl- ...
- 我们为什么要用redis
Redis的5要点: 1.为什么要选择Redis:介绍Redis的使用场景与使用Redis的原因: 2.Redis常用命令总结:包括时间复杂度总结与具体数据类型在Redis内部使用的数据结构: 3.R ...
- iOS - 外包开发常用第三方库(1)
一:第三方插件1:基于响应式编程思想的oc地址:https://github.com/ReactiveCocoa/ReactiveCocoa2:hud提示框地址:https://github.com/ ...
- jmeter学习笔记(三)配置元件之HTTP信息头管理
使用jmeter模拟发送http请求时,有些请求是需要带上HTTP请求头里面的信息.比如页面需要登录信息的,那个就需要用户登录信息authorization.这个时候是需要使用到HTTP信息头管理器. ...
- Mac下多版本JDK安装及管理
在Java项目中,经常对JDK版本有不同的要求,可是不可能为了某个项目的运行重新下载不同版本JDK进行安装,这样就涉及到对本地环境中多个JDK版本的管理. Mac的JDK都是安装到一个指定目录的:/L ...
- 说说你对kubernetes的理解(简单)
目录 整体概述 pod工作流程 k8s网络 flannel 网络策略,network proxy 几套证书理解 组件 master管理节点上组件 node节点 整体概述 k8s是一个编排工具,是谷歌的 ...
- 浅谈sqoop
1.sqoop的概述a.sqoop 是一款工具,是appche 旗下的一款工具,主要是负责 hadoop与RDBMS之间的数据迁移,即从hadoop 文件系统 导出数据到RDBMS,从RDBMS导入数 ...