Circle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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.

1≤T≤10,2≤n≤20.

 
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

思路:先模拟将出队的顺序求出,再将同余方程求出,不互质的中国剩余定理;

   例:7

     6 7 5 3 1 2  4

出队顺序:5 6 4 7 3 1 2 ;

      暴力约瑟夫得到;

       5≡k mod (n);

     1≡k mod (n-1);

5≡k mod (n-2);

     1≡k mod (n-3);

3≡k mod (n-4);

     1≡k mod (n-5);

       1≡k mod (n-6);

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
ll xu[];
ll pos[];
ll a[];
ll b[];
ll flag[];
ll gcd(ll x,ll y)
{
if(x%y==)
return y;
else
return gcd(y,x%y);
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
exgcd(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
int main()
{
ll x,y,z,i,t;
scanf("%lld",&x);
while(x--)
{
memset(a,,sizeof(a));
memset(flag,,sizeof(flag));
scanf("%lld",&y);
for(i=;i<y;i++)
{
scanf("%lld",&xu[i]);
pos[xu[i]]=i+;
}
int num=y;
for(i=;i<=y;i++)
b[i]=num--;
int st=;
for(i=;i<=y;i++)
{
while()
{
if(st==pos[i])
break;
if(!flag[st])a[i]++;
st++;
if(st==y+)
st=;
}
a[i]++;
flag[st]=;
//cout<<i<<" "<<a[i]<<endl;
}
ll a1=a[],b1=b[];
ll jie=;
for(i=;i<=y;i++)
{
ll a2=a[i],b2=b[i];
ll xx,yy;
ll gys=gcd(b1,b2);
if((a2-a1)%gys)
{
jie=;
break;
}
exgcd(b1,b2,xx,yy);
xx=(xx*(a2-a1))/gys;
ll gbs=b1*b2/gys;
a1=(((xx*b1+a1)%gbs)+gbs)%gbs;
b1=gbs;
}
if(!jie)
printf("Creation August is a SB!\n");
else if(a1!=)
printf("%lld\n",a1);
else
printf("%lld\n",b1);
}
return ;
}

hdu 5668 Circle 中国剩余定理的更多相关文章

  1. hdu 1370 Biorthythms 中国剩余定理

    Biorhythms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. hdu 3579 Hello Kiki 不互质的中国剩余定理

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  3. 《孙子算经》之"物不知数"题:中国剩余定理

    1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数.  如果 m ...

  4. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  5. [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...

  6. poj1006中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103506   Accepted: 31995 Des ...

  7. (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)

    前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...

  8. 洛谷P2480 [SDOI2010]古代猪文(费马小定理,卢卡斯定理,中国剩余定理,线性筛)

    洛谷题目传送门 蒟蒻惊叹于一道小小的数论题竟能涉及这么多知识点!不过,掌握了这些知识点,拿下这道题也并非难事. 题意一行就能写下来: 给定\(N,G\),求\(G^{\sum \limits _{d| ...

  9. 洛谷P3868 [TJOI2009]猜数字(中国剩余定理,扩展欧几里德)

    洛谷题目传送门 90分WA第二个点的看过来! 简要介绍一下中国剩余定理 中国剩余定理,就是用来求解这样的问题: 假定以下出现数都是自然数,对于一个线性同余方程组(其中\(\forall i,j\in[ ...

随机推荐

  1. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  2. 让声音更清晰,用PR去掉视频中的噪音

    Premiere V7.0没有支持当前文件类型的输入源的问题 25 我是新手,把DVD影片放到电脑里,显示的是VOB文件,导入类型显示的是所有支持格式,于是我就点击了VOB文件,但是说没有支持当前文件 ...

  3. VMWARE安装centos6 http://www.centoscn.com/image-text/setup/2013/0816/1263.html

    http://www.centoscn.com/image-text/setup/2013/0816/1263.html

  4. lnmp1.4 安装php fileinfo扩展 方法

    第一步:在lnmp1.4找到php安装的版本 使用命令 tar   -jxvf   php-7.1.7.tar.bz2 解压 第二步: 在解压的php-7.1.7文件夹里找到fileinfo文件夹,然 ...

  5. 待解决:PDF header signature not found

  6. git仓库按时间、成员等维度分析统计

    git 按时间打印所有成员代码提交: git log --since ==2018-01-01 --until=2018-12-31 --format='%aN' | sort -u | while ...

  7. Struts2快速后台验证 使用

    为了和前台基于JavaScript的开源验证框架RapidValidation使用统一的验证规则, 最大限度的减少重复的后台验证代码, 使用方式简便, 扩展方便. https://blog.csdn. ...

  8. Linux查看文件大小命令

    Linux查看文件大小命令 du命令 (1)du -b filepath 参数-b表示以字节计数 du -b filepath 参数-b表示以字节计数 #示例: $ du -b ~/Downloads ...

  9. 20145101《Java程序设计》第4周学习总结

    20145101<Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 继承:避免多个类间重复定义共同行为. 把相同代码提升为父类 运用extends关键字的子类会继承扩 ...

  10. 20145309李昊 WEB基础实践

    本实验同学帮助下完成 实验问题回答 1.什么是表单 表单在网页中主要负责数据采集功能 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码框.隐藏域.多行文本框.复选框.单选框.下拉选择 ...