HDU 5668 Circle

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
  
  
1 7 7 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值