hdu 5668 Circle 中国剩余定理
Circle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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!
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.
7
7 6 5 4 3 2 1
思路:先模拟将出队的顺序求出,再将同余方程求出,不互质的中国剩余定理;
例: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 中国剩余定理的更多相关文章
- hdu 1370 Biorthythms 中国剩余定理
Biorhythms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 3579 Hello Kiki 不互质的中国剩余定理
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- 《孙子算经》之"物不知数"题:中国剩余定理
1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数. 如果 m ...
- POJ 1006 中国剩余定理
#include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...
- [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...
- poj1006中国剩余定理
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103506 Accepted: 31995 Des ...
- (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)
前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...
- 洛谷P2480 [SDOI2010]古代猪文(费马小定理,卢卡斯定理,中国剩余定理,线性筛)
洛谷题目传送门 蒟蒻惊叹于一道小小的数论题竟能涉及这么多知识点!不过,掌握了这些知识点,拿下这道题也并非难事. 题意一行就能写下来: 给定\(N,G\),求\(G^{\sum \limits _{d| ...
- 洛谷P3868 [TJOI2009]猜数字(中国剩余定理,扩展欧几里德)
洛谷题目传送门 90分WA第二个点的看过来! 简要介绍一下中国剩余定理 中国剩余定理,就是用来求解这样的问题: 假定以下出现数都是自然数,对于一个线性同余方程组(其中\(\forall i,j\in[ ...
随机推荐
- Spark On Yarn Cluster生产环境下JVM的OOM和Stack Overflow问题
1.Spark on Yarn下JVM的OOM问题及解决方式 2.Spark中Driver的Stack Overflow的问题及解决方式 Spark on Yarn cluster mode: 此时有 ...
- Docker 后台进程参数-------更改Docker运行根目录的方法
参数 介绍 --api-enable-cors=false 远程API调用. -b, --bridge="" 桥接一个系统上的网桥设备到 Docker 容器里,当使用 none 可 ...
- Choose the best route(迪杰斯特拉)
通过做这题,发现了自己的问题很大,做题不是贴代码,而是要了解思想:这题考的是有一个起点的集合,求起点集合到一个终点的最短距离, 本来想用Floy的但一看map[1000][1000]超时,有向图,逆序 ...
- UVALive - 7261 Xiongnu's Land
思路: 先二分下界,再二分上届. #include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB ...
- linux脚本-判断进程是否存在,从而可以做预警处理..
count=`ps -ef | grep Seeyon | grep -v "grep" | wc -l` echo $count if [ $count -gt 0 ]; the ...
- 710 Random Pick with Blacklist
1. 问题 给定一个黑名单,包含[0, N)的一些数,从[0, N)之间的非黑名单数中随机采样一个值. 2. 思路 字典映射 (1)计算黑名单数的长度,记作B,因为已经排除掉了B个元素,所以最后是从N ...
- Hive四种数据导入方式介绍
问题导读 1.从本地文件系统中通过什么命令可导入数据到Hive表? 2.什么是动态分区插入? 3.该如何实现动态分区插入? 扩展: 这里可以和Hive中的三种不同的数据导出方式介绍进行对比? Hive ...
- MyEclipse 相关设置
1. MyElipse复制项目后,修改项目的发布名称的方式.右击你的项目,选择 properties -- > MyElipse -- > web,然后修改名称即可. 2. IDE查看源代 ...
- Linux服务器---网络配置
禁止ping 有些时候为了保护主机,会禁止其他机器对主机进行ping操作.Ping命令用的是ICMP协议,只要禁用ICMP协议,那么ping方法就无法检测这台主机.关于ICMP协议的配置文件是“/pr ...
- Django框架----Web框架本质
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...