A. Soldier and Bananas
- 题意:第一个香蕉要k刀,第二个2k刀,第i个要i?k刀。现有n刀,问可以买几个香蕉。
- 题解:等差数列求和,我们知道只需要找到p使得
∑i=1pi?k≤n<∑i=1p+1i?k即可,移项就可以得到公式,同时上述求和公式是单调递增的,因此也可以二分答案。
- 参考代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline ll foo(int x, int k) {
return (((1LL + x) * x) >> 1) * k;
}
ll gao(ll k, ll n, ll w) {
return max(foo(w, k) - n, 0LL);
}
int main() {
int k, n, w;
while (~scanf(" %d %d %d", &k, &n, &w)) {
printf("%I64d\n", gao(k, n, w));
}
return 0;
}
B. Soldier and Badges
- 题意:给n个数,1≤ai≤n,将一个数从a变大成b需要消耗b?a个金币,问如何把这堆数变成两两不等的,顺序任意。
- 题解:直接扫一遍,对一个数a,向上找一个之前没有占用的位置填充即可。
- 参考代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 6007;
bool vis[MAX];
int foo(int x) {
int res = 0;
while (vis[x]) {
++x;
++res;
}
vis[x] = true;
return res;
}
int main() {
int n;
while (~scanf(" %d", &n)) {
memset(vis, false, sizeof(vis));
int a, sum = 0;
for (int i = 1; i <= n; ++i) {
scanf(" %d", &a);
sum += foo(a);
}
printf("%d\n", sum);
}
return 0;
}
C. Soldier and Cards
- 题意:两堆牌,每次取最上面的两张比较,如果更大,则吃掉另一张,并将其放到底部,然后将自己这张也放到底部。一张牌都没有时为输。问游戏能不能终止,如果能,输出谁胜利以及需要进行几轮。
- 题解:考虑到总牌数并不多(k1+k2=n, 2≤n≤10),状态数最多只有A1010?C19这么多,实际上要少得多,因此直接状态压缩模拟即可。
- 参考代码:
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct State {
vector<int> left;
vector<int> right;
bool operator==(const State& B)const {
if (left.size() != B.left.size()
|| right.size() != B.right.size()) {
return false;
}
for (int i = 0; i < left.size(); ++i) {
if (left[i] != B.left[i]
|| right[i] != B.right[i]) {
return false;
}
}
return true;
}
bool operator<(const State& B)const {
if (left.size() != B.left.size()) {
return left.size() < B.left.size();
}
for (int i = 0; i < left.size(); ++i) {
if (left[i] != B.left[i]) {
return left[i] < B.left[i];
}
}
if (right.size() != B.right.size()) {
return right.size() < B.right.size();
}
for (int i = 0; i < right.size(); ++i) {
if (right[i] != B.right[i]) {
return right[i] < B.right[i];
}
}
return false;
}
void print() {
printf("{\n\t%d", left.size());
for (int i = 0; i < left.size(); ++i) {
printf(" %d", left[i]);
}
printf("\n\t%d", right.size());
for (int i = 0; i < right.size(); ++i) {
printf(" %d", right[i]);
}
puts("\n}\n");
}
};
map<State, bool> M;
pair<int, int> gao(State& thus) {
M.clear();
for (int i = 0; ; ++i) {
//puts("----------------------");
//thus.print();
//getchar();
if (thus.left.empty()) {
return make_pair(2, i);
} else if (thus.right.empty()) {
return make_pair(1, i);
} else if (M.find(thus) != M.end()) {
return make_pair(-1, i);
} else {
M.insert(make_pair(thus, true));
//generate the next thus
if (thus.left[0] > thus.right[0]) {
thus.left.push_back(thus.right[0]);
thus.left.push_back(thus.left[0]);
} else {
thus.right.push_back(thus.left[0]);
thus.right.push_back(thus.right[0]);
}
thus.left.erase(thus.left.begin());
thus.right.erase(thus.right.begin());
}
}
}
int main() {
int n;
State my;
while (~scanf(" %d", &n)) {
int m, a;
scanf(" %d", &m);
my.left.clear();
while (m--) {
scanf(" %d", &a);
my.left.push_back(a);
}
scanf(" %d", &m);
my.right.clear();
while (m--) {
scanf(" %d", &a);
my.right.push_back(a);
}
pair<int, int> res = gao(my);
if (res.first == -1) {
puts("-1");
} else {
printf("%d %d\n", res.second, res.first);
}
}
return 0;
}
D. Soldier and Number Game
- 题意:问a!b!分解质因数后又多少个质因子。如28=22?7,有3个质因子,注意,这里质因子2的指数为2,要算成2个。
- 题解:记Ω(n)为n的质因子个数,则Ω(1)=0。根据分解质因数后的形式,设:
n=x?y则显然有:
Ω(n)=Ω(x)+Ω(y)Ω(n)有这种性质,我们就称Ω(n)是加性函数。同时可以知道:
Ω(x)=Ω(n)?Ω(y)于是得解:
(1) 首先用筛法筛出质数,求出所有质数的Ω函数值为1(记为prime[x]=1),并且对任意一个合数y得出其一个质因子p,记为flag[y],即flag[y]=p。
(2) 然后对1≤x≤5000000,我们线性地扫一遍:
prime[x]=prime[x/flag[x]]+prime[flag[x]]其中prime[x]=Ω(x)。
(3) 由于
a!b!=∑ai=1Ω(i)∑bi=1Ω(i)分子和分母都是前缀和的形式,令:
dp[x]=∑j=1xΩ(j)就可以得到答案:
ans(a,b)=dp[a]?dp[b] - 参考代码:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5000007;
int flag[MAX] = {0};
int prime[MAX] = {0}; //prime[i]: i的质因数的个数
int dp[MAX] = {0}; //前缀和
void init() {
int i;
for (i = 2; i <= MAX / i; ++i) {
if (flag[i] == 0) {
flag[i] = i;
prime[i] = 1;
//printf("flag[%d] = %d\n", i, flag[i]);
//getchar();
for (int j = i * i; j < MAX; j += i) {
if (flag[j] == 0) {
flag[j] = i;
//printf("flag[%d] = %d\n", j, i);
//getchar();
}
}
}
}
for (; i < MAX; ++i) {
if (flag[i] == 0) {
flag[i] = i;
prime[i] = 1;
}
}
for (i = 2; i < MAX; ++i) {
prime[i] = prime[i / flag[i]] + prime[flag[i]];
}
for (i = 1; i < MAX; ++i) {
dp[i] = dp[i - 1] + prime[i];
}
//for (int i = 1; i < 10; ++i) {
// printf("prime[%d] = %d, dp[%d] = %d\n", i, prime[i], i, dp[i]);
//}
}
int main() {
init();
int T, a, b;
scanf(" %d", &T);
while (T--) {
scanf(" %d %d", &a, &b);
printf("%d\n", dp[a] - dp[b]);
}
return 0;
}
E. Soldier and Traveling
- 题意:有n个城市,m条道路(无向),城市里有一些士兵,问在每个士兵只能走一次的情况下,能不能把原来的各城市的士兵数(a1,a2,...an)变成(b1,b2,...,bn)。如果能给出任一个调整方案。
- 题解:网络流。建图时,增加源点source,汇点dest,每个城市作为一个点,同时拆点为i何i+n,如果有道路(x,y),那么建边x→n+y 和 y→n+x,容量分别为ax和ay。同时,源点和点i1≤i≤n之间建边,容量为ax;汇点和点in+1≤i≤2?n之间建边,容量为bi。得到source到dest的最大流flow后,判断flow是否等于所有点的士兵总和∑ni=1ai即可。当然,也应该要判断∑ni=1ai和∑ni=1bi是否相等。
- 参考代码:
#include <bits/stdc++.h>
using namespace std;
const int INF = 0xfffffff;
const int MAX = 107 << 1;
struct Edge {
int to;
int flow;
int cap;
int rev; //mapto Edge G[to][rev]
};
vector<Edge> G[MAX];
int level[MAX];
//store answer
int ans[MAX][MAX];
int n, m;
int a[MAX], b[MAX];
void addEdge(int from, int to, int cap) {
G[from].push_back((struct Edge){to, 0, cap, G[to].size()});
G[to].push_back((struct Edge){from, cap, cap, G[from].size() - 1});
}
bool bfs(int s, int t) {
memset(level, -1, sizeof(level));
level[s] = 0;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int p = Q.front();
Q.pop();
for (vector<Edge>::iterator it = G[p].begin(); it != G[p].end(); ++it) {
if (level[it->to] == -1 && it->flow < it->cap) {
Q.push(it->to);
level[it->to] = level[p] + 1;
if (it->to == t) {
return true;
}
}
}
}
//printf("No Road.\n");
return false;
}
int dfs(int s, int t, int flow) {
if (s == t) {
return flow;
}
int sum = 0, tmp;
for (vector<Edge>::iterator it = G[s].begin(); it != G[s].end(); ++it) {
if (level[it->to] == level[s] + 1 && it->flow < it->cap) {
tmp = dfs(it->to, t, min(flow, it->cap - it->flow));
//printf("gao(%d, %d), fetch %d\n", s, it->to, tmp);
it->flow += tmp;
G[it->to][it->rev].flow -= tmp;
sum += tmp;
flow -= tmp;
}
}
return sum;
}
int dinic(int s, int t) {
int sum = 0;
while (bfs(s, t)) {
sum += dfs(s, t, INF);
}
return sum;
}
inline int ex(int x) {
return (x >= 1 && x <= n) ? x : x - n;
}
int main() {
while (~scanf(" %d %d", &n, &m)) {
for (int i = 0; i < MAX; ++i) {
G[i].clear();
}
int source = 0, dest = n << 1 | 1;
int sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; ++i) {
scanf(" %d", a + i);
sum1 += a[i];
addEdge(source, i, a[i]);
addEdge(i, i + n, a[i]);
}
for (int i = 1; i <= n; ++i) {
scanf(" %d", b + i);
sum2 += b[i];
addEdge(i + n, dest, b[i]);
}
int x, y;
for (int i = 1; i <= m; ++i) {
scanf(" %d %d", &x, &y);
//match point <x, n + y>
addEdge(x, n + y, a[x]);
addEdge(y, n + x, a[y]);
}
int flow = dinic(source, dest);
//printf("flow = %d\n", flow);
if (sum1 != sum2 || flow != sum2) {
puts("NO");
} else {
puts("YES");
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; ++i) {
ans[i][i] = a[i];
}
/*
for (int i = source; i <= dest; ++i) {
for (int j = 0; j < G[i].size(); ++j) {
printf("(%d, %d) -> %d\n", i, G[i][j].to, G[i][j].flow);
}
}
puts("----");
*/
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < G[i].size(); ++j) {
Edge& e = G[i][j];
if (e.flow > 0) {
ans[i][i] -= e.flow;
ans[i][e.to - n] += e.flow;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
printf("%d%c", ans[i][j], j == n ? ‘\n‘ : ‘ ‘);
}
}
}
}
return 0;
}
时间: 2024-10-29 22:38:07