【BZOJ2054】疯狂的馒头(并查集)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2054
水题QAQ, 并查集就可以了。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #define LL long long 5 #define MaxN 1000010 6 using namespace std; 7 int n, m, q, p, S = 0; 8 int ans[MaxN], flag[MaxN], fa[MaxN]; 9 10 int find(int x){ 11 if (!fa[x]) return x; 12 return fa[x] = find(fa[x]); 13 } 14 15 int main(){ 16 scanf("%d%d%d%d", &n, &m, &p, &q); 17 for (int i = m; i; i--){ 18 int l = (int) ((LL) i * p + q) % n + 1; 19 int r = (int) ((LL) i * q + p) % n + 1; 20 if (l > r) swap(l, r); 21 for (int j = find(l); j <= r; j = find(j)){ 22 ans[j] = i; fa[j] = j+1; 23 } 24 } 25 for (int i = 1; i <= n; i++) printf("%d\n", ans[i]); 26 return 0; 27 }