Description
Satiya August is in charge of souls.
He finds souls,and lets them become a circle.He ordered them to play Joseph Games.The souls will count off from the soul .The soul who is numbered 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 .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 .
The second line has numbers,which are the sequence in the Out Ordered**(The person who is out at round was numbered )**.
The sequence input must be a permutation from to .
.
Output
For each case,If there is a eligible number ,output the smallest ,otherwise,output”Creation August is a SB!”.
Sample Input
1
7
7 6 5 4 3 2 1
Sample Output
420
先模拟求出所有的不定方程
然后直接开始跑板子
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<memory.h>
#include<time.h>
#include<string>
using namespace std;
typedef long long ll;
long long n, a[21], m[21], biaoji[21], shuru[21];
void exgcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) { d = a;x = 1;y = 0; }
else { exgcd(b, a%b, d, y, x);y -= x*(a / b); }
}
ll CRT2(int n, ll *a, ll *m) {//板子
int i, bo = 1;
ll n1 = m[1], n2, b1 = a[1], b2, bb, d, t, k, x, y;
for (i = 2;i <= n;i++) {
n2 = m[i];b2 = a[i];
bb = b2 - b1;exgcd(n1, n2, d, x, y);
if (bb%d) {
bo = 0;break;
}
k = bb / d*x;t = abs(n2 / d);
k = (k%t + t) % t;b1 = b1 + n1*k;n1 = n1 / d*n2;
}
if (!bo) return -1;
if (b1 <= 0) b1 += n1;
return b1;
}
long long gcd(long long a, long long b)//板子
{
if (b == 0) return a;
return gcd(b, a%b);
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n;
memset(biaoji, 0, sizeof(biaoji));
memset(a, 0, sizeof(a));
memset(m, 0, sizeof(m));
memset(shuru, 0, sizeof(shuru));
for (int q = 1;q <= n;q++)
{
int y;
cin >> y;
shuru[y] = q;
}
int jiji = 0;
int xianzai = 1;
long long zongshu = n;
for (int q = 1;q <= n;q++)
{
int jishu = 0;
for (;;)
{
if (!biaoji[xianzai])
{
jishu++;
if (xianzai == shuru[q])
{
a[++jiji] = jishu;
m[jiji] = zongshu;
biaoji[xianzai] = 1;
zongshu--;
break;
}
}
if (xianzai == n)xianzai = 1;
else xianzai++;
}
}
long long i = CRT2(n, a, m);
long long zzz = 1;
for (int o = 1;o <= n;o++)
{
zzz = zzz*m[o] / gcd(zzz, m[o]);
}
if (i < 0)
{
printf("Creation August is a SB!\n");
}
else if (i == 0)
{
cout << zzz + i << endl;
}
else cout << i << endl;
}
return 0;
}

本文介绍了一种解决Joseph游戏问题的算法实现,通过输入特定序列,利用中国剩余定理(CRT)来找出最小的合法起始编号。文章详细展示了算法的具体步骤及其实现代码。
435

被折叠的 条评论
为什么被折叠?



