Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d49777e

Browse files
committed
Codeforces Round #309
1 parent 92cab84 commit d49777e

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

‎Codeforces/Codeforces Round #309/a.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
typedef long long int64;
6+
7+
const int64 MOD = 1000000007;
8+
9+
const int
10+
MAXN = 1 << 11,
11+
MAX_SUM = 1 << 20;
12+
13+
int K;
14+
int a[MAXN];
15+
16+
int64 fact[MAX_SUM];
17+
int64 inv[MAX_SUM];
18+
int64 invFact[MAX_SUM];
19+
int64 sum;
20+
int64 ans;
21+
22+
int64 ways(int n, int k) {
23+
return fact[n + k - 1] * invFact[n] % MOD * invFact[k - 1] % MOD;
24+
}
25+
26+
int main() {
27+
ios_base::sync_with_stdio(0);
28+
29+
30+
inv[1] = 1;
31+
for (int i = 2; i < MAX_SUM; ++i)
32+
inv[i] = (MOD - (MOD/i) * inv[MOD%i] % MOD) % MOD;
33+
34+
fact[0] = invFact[0] = 1;
35+
36+
for (int i = 1; i < MAX_SUM; ++i) {
37+
fact[i] = fact[i - 1] * i % MOD;
38+
invFact[i] = invFact[i - 1] * inv[i] % MOD;
39+
}
40+
41+
cin >> K;
42+
ans = 1;
43+
for (int i = 1; i <= K; ++i) {
44+
cin >> a[i];
45+
ans = ans * ways(a[i] - 1, sum + 1) % MOD;
46+
sum += a[i];
47+
}
48+
49+
cout << ans << endl;
50+
51+
return 0;
52+
}

‎Codeforces/Codeforces Round #309/b.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long int64;
5+
6+
int N;
7+
int64 K;
8+
int64 fib[60];
9+
10+
int main() {
11+
ios_base::sync_with_stdio(0);
12+
13+
fib[0] = 1;
14+
fib[1] = 1;
15+
fib[2] = 2;
16+
for (int i = 3; i < 60; ++i)
17+
fib[i] = fib[i - 1] + fib[i - 2];
18+
19+
cin >> N >> K;
20+
21+
int curFib = N;
22+
for (int i = 1; i <= N;)
23+
if (K <= fib[curFib - 1]) {
24+
cout << i << " ";
25+
i++;
26+
curFib--;
27+
} else {
28+
K -= fib[curFib - 1];
29+
cout << i+1 << " " << i << " ";
30+
i += 2 ;
31+
curFib -= 2;
32+
}
33+
34+
return 0;
35+
}

‎Codeforces/Codeforces Round #309/c.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long int64;
5+
6+
const int64 MOD = 1000000007;
7+
8+
const int
9+
MAXV = 1 << 17;
10+
11+
int V, E;
12+
int data[MAXV];
13+
int components;
14+
vector<int> hate[MAXV];
15+
vector<int> love[MAXV];
16+
17+
int findSet(int x) {
18+
return data[x] < 0 ? x : data[x] = findSet(data[x]);
19+
}
20+
21+
int getSize(int x) {
22+
return -data[findSet(x)];
23+
}
24+
25+
void join(int u, int v) {
26+
u = findSet(u);
27+
v = findSet(v);
28+
if (u == v)
29+
return ;
30+
31+
32+
components--;
33+
34+
if (getSize(u) > getSize(v))
35+
swap(u, v);
36+
37+
data[u] += data[v];
38+
data[v] = u;
39+
}
40+
41+
int mark[MAXV][2];
42+
int curMark;
43+
44+
void dfs(int u, int x) {
45+
if (mark[u][1 - x] == curMark) {
46+
cout << 0 << endl;
47+
exit(0);
48+
}
49+
if (mark[u][x] == curMark)
50+
return ;
51+
mark[u][x] = curMark;
52+
for (int v : love[u]) dfs(v, x);
53+
for (int v : hate[u]) dfs(v, 1 - x);
54+
}
55+
56+
int main() {
57+
ios_base::sync_with_stdio(0);
58+
59+
memset(data, -1, sizeof(data));
60+
61+
cin >> V >> E;
62+
63+
components = V;
64+
65+
for (int i = 0; i < E; ++i) {
66+
int u, v, w;
67+
cin >> u >> v >> w;
68+
--u;
69+
--v;
70+
if (w == 0) {
71+
hate[u].push_back(v);
72+
hate[v].push_back(u);
73+
} else {
74+
love[u].push_back(v);
75+
love[v].push_back(u);
76+
}
77+
join(u, v);
78+
}
79+
80+
// search for cycles with odd number of hate relations
81+
for (int i = 0; i < V; ++i) if (data[i] < 0) {
82+
curMark++;
83+
dfs(i, 0);
84+
}
85+
86+
int64 result = 1;
87+
for (int i = 0; i < components - 1; ++i)
88+
result = 2 * result % MOD;
89+
90+
cout << result << endl;
91+
92+
return 0;
93+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /