# 【BZOJ-3275&3158】Number&千钧一发 最小割

## 3275: Number

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 748  Solved: 316
[Submit][Status][Discuss]

## Description

1:存在正整数C，使a*a+b*b=c*c
2:gcd(a,b)=1

5
3 4 5 6 7

22

n<=3000。

## 3158: 千钧一发

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 984  Solved: 359
[Submit][Status][Discuss]

4
3 4 5 12
9 8 30 9

39

## HINT

1<=N<=1000,1<=Ai,Bi<=10^6

Katharon+#1

## Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int Gcd(int a,int b)
{
if (b==0) return a; else return Gcd(b,a%b);
}
bool check(long long a,long long b)
{
if (Gcd(a,b)>1) return true;
long long T=sqrt(a*a+b*b); if (T*T!=a*a+b*b) return true;
return false;
}
#define maxn 5000
#define maxm 1000010
int N,A[maxn],B[maxn],tot;
struct Edgenode{int next,to,cap;}edge[maxm];
{
cnt++;
}
int cur[maxn],dis[maxn],S,T;
bool bfs()
{
queue<int> q;
memset(dis,-1,sizeof(dis));
q.push(S); dis[S]=0;
while (!q.empty())
{
int now=q.front(); q.pop();
if (edge[i].cap && dis[edge[i].to]==-1)
dis[edge[i].to]=dis[now]+1,q.push(edge[i].to);
}
return dis[T]!=-1;
}
int dfs(int loc,int low)
{
if (loc==T) return low;
int w,used=0;
for (int i=cur[loc]; i; i=edge[i].next)
if (edge[i].cap && dis[edge[i].to]==dis[loc]+1)
{
w=dfs(edge[i].to,min(low-used,edge[i].cap));
edge[i].cap-=w; edge[i^1].cap+=w; used+=w;
if (edge[i].cap) cur[loc]=i; if (used==low) return low;
}
if (!used) dis[loc]=-1;
return used;
}
#define inf 0x7fffffff
int dinic()
{
int tmp=0;
while (bfs())
{
for (int i=S; i<=T; i++) cur[i]=head[i];
tmp+=dfs(S,inf);
}
return tmp;
}
void Build()
{
S=0,T=N+1;
for (int i=1; i<=N; i++)
if ((A[i]%2)) insert(S,i,A[i]); else insert(i,T,A[i]);
for (int i=1; i<=N; i++)
for (int j=1; j<=N; j++)
if ((A[i]%2) && !(A[j]%2) && !check(A[i],A[j]))
insert(i,j,inf);
}
int main()
{
for (int i=1; i<=N; i++) A[i]=read(),tot+=A[i];
//for (int i=1; i<=N; i++) B[i]=read(),tot+=B[i];
Build();
int maxflow=dinic();
printf("%d\n",tot-maxflow);
return 0;
}
BZOJ-3275
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int Gcd(int a,int b)
{
if (b==0) return a; else return Gcd(b,a%b);
}
bool check(long long a,long long b)
{
if (Gcd(a,b)>1) return true;
long long T=sqrt(a*a+b*b); if (T*T!=a*a+b*b) return true;
return false;
}
#define maxn 3000
#define maxm 1000010
int N,A[maxn],B[maxn],tot;
struct Edgenode{int next,to,cap;}edge[maxm];
{
cnt++;
}
int cur[maxn],dis[maxn],S,T;
bool bfs()
{
queue<int> q;
memset(dis,-1,sizeof(dis));
q.push(S); dis[S]=0;
while (!q.empty())
{
int now=q.front(); q.pop();
if (edge[i].cap && dis[edge[i].to]==-1)
dis[edge[i].to]=dis[now]+1,q.push(edge[i].to);
}
return dis[T]!=-1;
}
int dfs(int loc,int low)
{
if (loc==T) return low;
int w,used=0;
for (int i=cur[loc]; i; i=edge[i].next)
if (edge[i].cap && dis[edge[i].to]==dis[loc]+1)
{
w=dfs(edge[i].to,min(low-used,edge[i].cap));
edge[i].cap-=w; edge[i^1].cap+=w; used+=w;
if (edge[i].cap) cur[loc]=i; if (used==low) return low;
}
if (!used) dis[loc]=-1;
return used;
}
#define inf 0x7fffffff
int dinic()
{
int tmp=0;
while (bfs())
{
for (int i=S; i<=T; i++) cur[i]=head[i];
tmp+=dfs(S,inf);
}
return tmp;
}
void Build()
{
S=0,T=N+1;
for (int i=1; i<=N; i++)
if ((A[i]%2)) insert(S,i,B[i]); else insert(i,T,B[i]);
for (int i=1; i<=N; i++)
for (int j=1; j<=N; j++)
if ((A[i]%2) && !(A[j]%2) && !check(A[i],A[j]))
insert(i,j,inf);
}
int main()
{
}