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[ ...
随机推荐
- gcc static静态编译选项提示错误修正(/usr/lib/ld: cannot find -lc)
用gcc静态编译C程序时显示出: /usr/lib/ld: cannot find -lc /usr/lib/ld: cannot find -lgcc_s /usr/lib/ld: cannot f ...
- (转)找回Git中丢失的Commit
总结:更新代码前一定要先将本地修改的文件存到本地git仓库.今天脑残直接更新了远程仓库代码导入今天写的代码...... @[git|commit|reflog] 在使用Git的过程中,有时候会因为一些 ...
- [LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- js 俄罗斯方块源码,简单易懂
1.自己引入jquery <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- Summary: sorting Algorithms
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item a ...
- 树结构数据的展示和编辑-zTree树插件的简单使用
最近在项目当中遇到一个需求,需要以树结构的方式展示一些数据,并可对每一个树节点做内容的编辑以及树节点的添加和删除,刚好听说有zTree这个插件可以实现这样的需求,所以在项目的这个需求完成之后,在博客里 ...
- 转载自(http://snailz.diandian.com/post/2012-10-24/40041265730)
PHP 5.4.8 添加系统服务命令 之前没注意,PHP 5.4.8 的安装包有自带的系统服务注册文件的 打开编译安装包,换成你自己的路径 cd /mydata/soft/php-5.4.8/ cp ...
- 登录使用inode的校园网用到的url
无需使用inode客户端,直接访问下面的url,然后输入账号密码即可. 第一次访问这个url的时候可能会提示下载inode客户端,再访问一次即可. url1:http://172.20.1.1/por ...
- sql中 substring和charindex 的用法
-- 第一个参数是要截取的字符串,第二个参数是从第几个字符开始截取,第三个参数是截取的长度 --例如:select SUBSTRING('12345678',1,4) 返回 1234 -- selec ...
- 如何提取app软件的apk格式中的字体?
1.下载apk格式的指定app软件: 2.将apk格式的文件名更改为后缀名为zip格式: 3.用winrar解压软件解压,然后就找到其中的ttf格式的字体文件. 举例说明,我想找到airbnb的app ...