Language: C++

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) reep((i),0,(n))

typedef vector<int> vint;


template<class V> class MaxFlow_dinic{
public:
	struct edge{int to,reve;V cap;};
	static const int MV = 1100;
	vector<edge> E[MV];
	int itr[MV],lev[MV];

	void add_edge(int x,int y,V cap,bool undir=false){
		E[x].push_back((edge){y,(int)E[y].size(),cap});
		E[y].push_back((edge){x,(int)E[x].size()-1,undir?cap:0});
	}	
	void bfs(int cur){
		memset(lev,0xff,sizeof(lev));
		queue<int> q;
		lev[cur]=0;
		q.push(cur);
		while(q.size()){
			int v=q.front();
			q.pop();
			for(__typeof(E[v].begin()) e=E[v].begin();e!=E[v].end();e++) if(e->cap>0&&lev[e->to]<0) lev[e->to]=lev[v]+1,q.push(e->to);
		}
	}

	V dfs(int from,int to,V cf){
		if(from==to){
			return cf;
		}
		for(;itr[from]<E[from].size();itr[from]++){
			edge* e=&E[from][itr[from]];
			if(e->cap>0&&lev[from]<lev[e->to]){
				V f=dfs(e->to,to,min(cf,e->cap));
				if(f>0){
					e->cap-=f;
					E[e->to][e->reve].cap += f;
					return f;
				}
			}
		}
		return 0;
	}
	V maxflow(int from,int to){
		V fl = 0,tf;
		while(1){
			bfs(from);
			if(lev[to]<0) return fl;
			memset(itr,0,sizeof(itr));
			while((tf=dfs(from,to,numeric_limits<V>::max()))>0) fl += tf;
		}
	}
};


int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		vint a(n);
		vint b(m);
		rep(i,n) cin>>a[i];
		rep(i,m) cin>>b[i];
		MaxFlow_dinic<int> mf;
		rep(i,n){
			rep(j,m){
				int t = __gcd(a[i],b[j]);
				if(t==1) continue;
				mf.add_edge(i,n+j,1);
			}
		}
		int souce = n+m;
		int sink = n+m+1;
		rep(i,n) mf.add_edge(souce,i,1);
		rep(i,m) mf.add_edge(n+i,sink,1);
		int ans = 0;
		ans = mf.maxflow(souce,sink);
		cout<<ans<<endl;
	}
}

Compiler output:
a.cc: In member function 'void MaxFlow_dinic<V>::bfs(int)':
a.cc:25:17: error: there are no arguments to 'memset' that depend on a template parameter, so a declaration of 'memset' must be available [-fpermissive]
   25 |                 memset(lev,0xff,sizeof(lev));
      |                 ^~~~~~
a.cc:25:17: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: In member function 'V MaxFlow_dinic<V>::maxflow(int, int)':
a.cc:58:25: error: there are no arguments to 'memset' that depend on a template parameter, so a declaration of 'memset' must be available [-fpermissive]
   58 |                         memset(itr,0,sizeof(itr));
      |                         ^~~~~~
a.cc:59:47: error: 'numeric_limits' was not declared in this scope
   59 |                         while((tf=dfs(from,to,numeric_limits<V>::max()))>0) fl += tf;
      |                                               ^~~~~~~~~~~~~~
a.cc:59:63: error: expected primary-expression before '>' token
   59 |                         while((tf=dfs(from,to,numeric_limits<V>::max()))>0) fl += tf;
      |                                                               ^
a.cc:59:69: error: no matching function for call to 'max()'
   59 |                         while((tf=dfs(from,to,numeric_limits<V>::max()))>0) fl += tf;
      |                                                                ~~~~~^~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 0 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 0 provided
a.cc: In function 'int main()':
a.cc:75:41: error: '__gcd' was not declared in this scope
   75 |                                 int t = __gcd(a[i],b[j]);
      |                                         ^~~~~
a.cc: In instantiation of 'V MaxFlow_dinic<V>::maxflow(int, int) [with V = int]':
a.cc:85:19:   required from here
   85 |                 ans = mf.maxflow(souce,sink);
      |                       ~~~~~~~~~~^~~~~~~~~~~~
a.cc:58:31: error: 'memset' was not declared in this scope
   58 |                         memset(itr,0,sizeof(itr));
      |                         ~~~~~~^~~~~~~~~~~~~~~~~~~
a.cc:4:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    3 | #include <queue>
  +++ |+#include <cstring>
    4 | 
a.cc: In instantiation of 'void MaxFlow_dinic<V>::bfs(int) [with V = int]':
a.cc:56:4:   required from 'V MaxFlow_dinic<V>::maxflow(int, int) [with V = int]'
   56 |                         bfs(from);
      |                         ^~~
a.cc:85:19:   required from here
   85 |                 ans = mf.maxflow(souce,sink);
      |                       ~~~~~~~~~~^~~~~~~~~~~~
a.cc:25:23: error: 'memset' was not declared in this scope
   25 |                 memset(lev,0xff,sizeof(lev));
      |                 ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:25:23: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'