HDU 5668 Circle

来源:互联网 发布:windows 杀进程 编辑:程序博客网 时间:2024/06/01 23:10
Problem Description
    Satiya August is in charge of souls.

    He finds n souls,and lets them become a circle.He ordered them to play Joseph Games.The souls will count off from the soul 1.The soul who is numbered k will be taken out,and will not join in the game again.

    Now Satiya August has got the sequence in the Out Ordered,and ask you the smallest k.If you cannot give him a correct answer,he will kill you!
 

Input
    The first line has a number T,means testcase number.

    Each test,first line has a number n.

    The second line has n numbers,which are the sequence in the Out Ordered**(The person who is out at aith round was numbered i)**.

    The sequence input must be a permutation from 1 to n.

    1T10,2n20.
 

Output
    For each case,If there is a eligible number k,output the smallest k,otherwise,output”Creation August is a SB!”.
 

Sample Input
177 6 5 4 3 2 1
 

Sample Output
420

约瑟夫环问题,可以转换成中国剩余定理,然后就是套模板了。

#pragma comment(linker, "/STACK:102400000,102400000")#include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 1e5 + 10;int T, n, x[maxn], y;LL a[maxn], b[maxn], vis[maxn];void egcd(LL a, LL b, LL&d, LL&x, LL&y){if (!b) { d = a, x = 1, y = 0; }else{egcd(b, a%b, d, y, x);y -= x*(a / b);}}LL lmes() {LL M = a[1], R = b[1], x, y, d;for (int i = 2; i <= n; i++) {egcd(M, a[i], d, x, y);if ((b[i] - R) % d) return -1;x = (b[i] - R) / d*x % (a[i] / d);R += x*M;M = M / d*a[i];R %= M;}return (R + M) % M ? (R + M) % M : M;}int main(){scanf("%d", &T);while (T--){scanf("%d", &n);for (int i = 1; i <= n; i++){a[i] = n - i + 1;vis[i] = 0;scanf("%d", &y);x[y] = i;}for (int i = 1, j = 1, k; i <= n; i++){for (k = 1; j != x[i];) { if (!vis[j]) k++; j = j % n + 1; }vis[x[i]] = 1;    b[i] = k % a[i];}LL k = lmes();if (k == -1) printf("Creation August is a SB!\n");else cout << k << endl;}return 0;}


0 1