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[ ...
随机推荐
- Ubuntu16.04安裝最新Nvidia驱动
在安装完Ubuntu之后,可能通过自带驱动无法更新,一直处于无法下载状态,那么就需要通过到Nvidia官网下载驱动,手动安装了 方法/步骤 通过度娘,打开NVIDIA官网,然后在下载驱动那里找到自己的 ...
- javascript脚本实现浏览器自动点击(阿里员工秒杀月饼)
原文地址https://blog.csdn.net/ani521smile/article/details/52575063 秒杀活动页面 <!DOCTYPE HTML> <html ...
- yii2常用的migrate命令
开发中经常会用到的方法小结: 1../yii migrate xxx_xx 在表中插入某字段 : public function up() {$this->addColumn('{{applic ...
- 为什么采用4~20mA的电流来传输模拟量?(转)
源: 为什么采用4~20mA的电流来传输模拟量?
- ELK+Kafka学习笔记之FileBeat日志合并配置输出到kafka集群
filebeat.prospectors: - type: log #日志输出类型 enabled: true ...
- Shell学习笔记之shell脚本和python脚本实现批量ping IP测试
0x00 将IP列表放到txt文件内 先建一个存放ip列表的txt文件: [root@yysslopenvpn01 ~]# cat hostip.txt 192.168.130.1 192.168.1 ...
- redis.windows.conf 参数说明
1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis默认会把pid写入/var/ru ...
- TP/TCP/UDP
这两周我继续学习CCSDS协议栈中位于传输层较低位置的SCPS-TP协议,并且复习了TCP/IP体系中的TCP协议和UDP协议,通过学习和对比两个体系的协议,加深了我对SCPS-TP协议的认识和理解. ...
- UVa 11082 Matrix Decompressing - 网络流
开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...
- Python3基础 函数 收集参数(tuple)+普通参数 的示例
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...