傳送門
文章目錄
題意:
思路:
對于每個數的位置(i,j)(i,j)(i,j),如果將這個位置染黑,那么我們連一個i?>j+ni->j+ni?>j+n的邊,可以發現我們的操作不影響連通性。如果想要全部染黑等價于將其變成聯通圖。
所以跑一遍最小生成樹即可,用克魯斯卡爾的話需要桶排。
#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
;
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()
{
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;
}
總結
以上是生活随笔為你收集整理的2021牛客暑期多校训练营3 B Black and white 最小生成树 + 思维的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。