标签:
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
标签:
原文地址:http://blog.csdn.net/bit_line/article/details/46346861