[BJOI2012]连连看 BZOJ2661 费用流
题目描述
凡是考智商的题里面总会有这么一种消除游戏。不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏。我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y(设x>y)的平方差x^2-y^2是一个完全平方数z^2,并且y与z互质,那么就可以将x和y连起来并且将它们一起消除,同时得到x+y点分数。那么过关的要求就是,消除的数对尽可能多的前提下,得到足够的分数。快动手动笔算一算吧。
输入输出格式
输入格式:只有一行,两个整数,分别表示a,b。
输出格式:两个数,可以消去的对数,及在此基础上能得到的最大分数。
输入输出样例
说明
对于30%的数据,1<=a,b<=100
对于100%的数据,1<=a,b<=1000
将每个点分为in,out;
建边的时候要注意我们消去的是一对数字;
所以建边的时候Inx----Outy,Iny----Outx都要建边;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 100005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) #define mclr(x,a) memset((x),a,sizeof(x)) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-5 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == '-') f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ bool vis[maxn]; int n, m, s, t; int x, y, f, z; int dis[maxn], pre[maxn], last[maxn], flow[maxn]; int maxflow, mincost; struct node { int to, nxt, flow, dis; }edge[maxn << 2]; int head[maxn], cnt; queue<int>q; void addedge(int from, int to, int flow, int dis) { edge[++cnt].to = to; edge[cnt].flow = flow; edge[cnt].dis = dis; edge[cnt].nxt = head[from]; head[from] = cnt; } bool spfa(int s, int t) { memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow)); ms(vis); q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].nxt) { if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = edge[i].dis + dis[now]; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now], edge[i].flow); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1; } void mincost_maxflow() { while (spfa(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } } } bool OK(int x, int y) { if (x < y)swap(x, y); int Z = x * x - y * y; int z = (int)sqrt(Z); if (z*z == Z) { if (gcd(z, y) == 1) { return true; } else return false; } else return false; } int main() { // ios::sync_with_stdio(0); mclr(head, -1); cnt = 1; int a, b; a = rd(); b = rd(); s = 0; t = b + 1; int num = b - a + 1; for (int i = a; i <= b; i++) { addedge(s, i, 1, 0); addedge(i, s, 0, 0); addedge(i + b , t, 1, 0); addedge(t, i + b , 0, 0); } for (int i = a; i <= b; i++) { for (int j = i + 1; j <= b; j++) { if (OK(i, j)) { addedge(i + b, j, 0, i + j); addedge(j, i + b, 1, -(i + j)); addedge(j + b, i, 0, i + j); addedge(i, j + b, 1, -(i + j)); } } } mincost_maxflow(); printf("%d %d\n", maxflow/2, -mincost/2); return 0; }
EPFL - Fighting