题意:
思路:
对于每个数的位置
(
i
,
j
)
(i,j)
(i,j),如果将这个位置染黑,那么我们连一个
i
−
>
j
+
n
i->j+n
i−>j+n的边,可以发现我们的操作不影响连通性。如果想要全部染黑等价于将其变成联通图。
所以跑一遍最小生成树即可,用克鲁斯卡尔的话需要桶排。
// Problem: Black and white
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/11254/B
// Memory Limit: 1048576 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=5050,INF=0x3f3f3f3f;
const double eps=1e-6;
int a,b,c,d,mod;
int n,m;
int p[N*20];
vector<PII>v[N*20];
LL cre[N*N];
int find(int x) {
return x==p[x]? x:p[x]=find(p[x]);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
cin>>n>>m>>a>>b>>c>>d>>mod;
cre[0]=a;
for(int i=1;i<=n*m;i++) {
cre[i]=(cre[i-1]*cre[i-1]%mod*b%mod+cre[i-1]*c%mod+d)%mod;
v[cre[i]].pb({(i-1)/m,(i-1)%m});
}
LL ans=0;
for(int i=0;i<=n+m+100;i++) p[i]=i;
for(int i=0;i<mod;i++) {
for(auto x:v[i]) {
int pa=find(x.X),pb=find(x.Y+n);
if(pa==pb) continue;
ans+=i; p[pa]=pb;
}
}
cout<<ans<<endl;
return 0;
}
/*
*/