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[ ...
随机推荐
- 支持向量机(SVM)、支持向量回归(SVR)
1.支持向量机( SVM )是一种比较好的实现了结构风险最小化思想的方法.它的机器学习策略是结构风险最小化原则 为了最小化期望风险,应同时最小化经验风险和置信范围) 支持向量机方法的基本思想: ( 1 ...
- 让声音更清晰,用PR去掉视频中的噪音
Premiere V7.0没有支持当前文件类型的输入源的问题 25 我是新手,把DVD影片放到电脑里,显示的是VOB文件,导入类型显示的是所有支持格式,于是我就点击了VOB文件,但是说没有支持当前文件 ...
- cc150 --链表中倒数第k个节点
题目描述 输入一个链表,输出该链表中倒数第k个结点. 快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个. public class Solution { public Li ...
- Python: Flask框架简单介绍
接触Python之后我第一次听说Flask,我就根据自己搜罗的知识尽可能简洁的说出来.如果不准确的地方还请指正,谢谢. Flask是什么? Flask是基于Python编写的微 ...
- 数据仓库基础(六)数据的ETL
本文转载自:http://www.cnblogs.com/evencao/p/3140487.html ETL是数据抽取(Extract).转换(Transform).加载(Load)的简写,是构建数 ...
- linux查看文件夹大小,备份文件夹zip压缩解压
linux查看文件夹大小,备份文件夹zip压缩解压 du -sh : 查看当前目录总共占的容量.而不单独列出各子项占用的容量 du -lh --max-depth=1 : 查看当前目录下一级子文件和子 ...
- 微信分享缩略图,如何增加微信朋友圈分享链接的小图片? facebook、google+、twitter等分享的标签
微信分享缩略图 如何增加微信朋友圈分享链接的小图片?在网页的头部<head>标签内加上以下代码,图片路径自行修改.<head><div id='wx_pic' style ...
- WindowsServer-性能计数器
https://jingyan.baidu.com/article/59703552e764e48fc00740dd.html
- QAQ的LIS树 QAQ的LIS树2 题解报告
这两道题实际上考试的时候是一道题OwO 太可怕了,忙了我三个多小时,写了整整7K 这个题两个询问关联性不强,所以分开来考虑 QAQ的LIS树 考虑如何用dp求解答案 设dp(v)表示v到根的修改后的序 ...
- 20145310《网络对抗》Exp9 Web安全基础实践
基础问题回答 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,通过执行SQL语句进执行攻击者所要的操作. 如何防御?首先严格区分 ...