题目大意
有$n$个餐厅,从0~$n$-1编号,每天都要去一个餐厅吃饭,这个餐厅的编号可以计算得出。但是他不想去$\lfloor \frac{n}{2}\rfloor$天去过的餐厅,如果$\lfloor \frac{n}{2}\rfloor$天去过了,便会向右平移1个餐厅,如果$\ge n$,编号变为0,问这$n$天去哪一个餐厅。
题目分析
这题因为随机数的原因,这道题暴力都可以过。。。
我们可以用并查集保存最近的可用餐厅,超过$\lfloor \frac{n}{2}\rfloor$天重置并查集,因为要重置并查集,所以不能路径压缩。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const LL Get_Int() { LL num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=100005; int n,m,father[maxn]; LL A,B,a[maxn]; int Get_Father(int x) { if(father[x]==x)return x; return Get_Father(father[x]); } int main() { n=Get_Int(); m=Get_Int(); A=Get_Int()%n; B=Get_Int()%n; a[1]=Get_Int(); a[2]=Get_Int(); for(int i=0; i<=n; i++)father[i]=i; father[a[1]]=a[1]+1; father[a[2]]=a[2]+1; for(int i=3; i<=m; i++) { LL x=(A*a[i-1]+B*a[i-2])%n; if(i>n/2+1)father[a[i-n/2-1]]=a[i-n/2-1]; int p=Get_Father(x); if(p<n)a[i]=p; else a[i]=Get_Father(0); father[a[i]]=a[i]+1; printf("%lld ",a[i]); } return 0; }
|