中国剩余定理。

可以手动模拟一下每一次开始的人的编号和结束的人的编号。

每次删掉一个人,对剩下的人重新编号。

这样一次模拟下来,可以得到n个方程

形如:(u[i]+k)%(n-i+1)=v[i]

化简一下就是:k%(n-i+1)=v[i]-u[i]%(n-i+1)

接下来就是求解最小的k,满足所有式子

中国剩余定理板子一套就AC了......

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int maxn = ;
int n, T;
int s[maxn], flag[maxn], tmp[maxn]; int u[], v[];
LL a[maxn], b[maxn]; void ls()
{
int cnt = ;
for (int i = ; i <= n; i++)
{
if (flag[i] == )
tmp[cnt++] = i;
}
} int Find(int a)
{
for (int i = a; i <= n; i++)
if (flag[i] == ) return i;
for (int i = ; i <= a; i++)
if (flag[i] == ) return i;
} int f2(int a)
{
for (int i = ;; i++)
if (tmp[i] == a) return i;
} void egcd(LL a, LL b, LL&d, LL&x, LL&y)
{
if (!b) { d = a, x = , y = ; }
else
{
egcd(b, a%b, d, y, x);
y -= x*(a / b);
}
} LL lmes() {
LL M = a[], R = b[], x, y, d;
for (int i = ; i <= n; i++) {
egcd(M, a[i], d, x, y);
if ((b[i] - R) % d) return -;
x = (b[i] - R) / d*x % (a[i] / d);
R += x*M;
M = M / d*a[i];
R %= M;
}
return (R + M) % M ? (R + M) % M : M;
} void exgcd(LL a, LL b, LL &d, LL &x, LL &y)
{
if (b == )
d = a, x = , y = ;
else
{
exgcd(b, a%b, d, y, x);
y -= x * (a / b);
}
} LL china(LL n, LL m[], LL a[])
{
LL aa = a[];
LL mm = m[];
for (int i = ; i<n; i++)
{
LL sub = (a[i] - aa);
LL d, x, y;
exgcd(mm, m[i], d, x, y);
if (sub % d) return -; LL new_m = m[i] / d;
new_m = (sub / d*x%new_m + new_m) % new_m;
aa = mm*new_m + aa;
mm = mm*m[i] / d;
}
aa = (aa + mm) % mm;
return aa ? aa : mm;
} int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
int ai; scanf("%d", &ai);
s[ai] = i;
}
memset(flag, , sizeof flag); u[] = , v[] = s[];
if (v[] == n) v[] = ; flag[s[]] = ; for (int i = ; i <= n; i++)
{
int st, en;
ls();
st = Find(s[i - ]), st = f2(st), st = st - ;
if (st == -) st = n - i;
en = f2(s[i]), u[i] = st;
v[i] = en; if (v[i] == n - i+) v[i] = ;
flag[s[i]] = ;
}
/*
for (int i = 1; i <= n; i++)
printf("%d %d\n", u[i], v[i]);
*/ //接下来就是求解最小的k,满足所有式子
//k%(n-i+1)=v[i]-u[i]%(n-i+1) for (int i = ; i <= n; i++)
{
a[i] = (LL)(n - i + );
b[i] = (LL)(v[i] - u[i] % (n - i + ));
}
LL k = china(n, a + , b + );
if (k == -) printf("Creation August is a SB!\n");
else printf("%lld\n", k);
}
return ;
}

HDU 5668 Circle的更多相关文章

  1. hdu 5668 Circle 中国剩余定理

    Circle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  2. BZOJ 2976: [Poi2002]出圈游戏 HDU 5668 CRT

    2976: [Poi2002]出圈游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2976 Description Input 中第一 ...

  3. hdu 5343 MZL's Circle Zhou SAM

    MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...

  4. HDU 5868 Different Circle Permutation(burnside 引理)

    HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...

  5. HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1221 Rectangle and Circle Time Limit: 2000/1000 MS (J ...

  6. HDU 5343 MZL's Circle Zhou

    MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...

  7. HDU 4669 Mutiples on a circle 数位DP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4669 考察对取模的的理解深不深刻啊,当然还有状态的设计····设d[i][j]表示以第i个数结尾,余 ...

  8. HDU 4669 Mutiples on a circle (2013多校7 1004题)

    Mutiples on a circle Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  9. HDU 5343 MZL's Circle Zhou 后缀自动机+DP

    MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. iOS 应用程序内部国际化,不跟随系统语言

    前言:网络上关于iOS国际化的文章很多,但基本上都是基于跟随系统语言的国际化,笔者就不赘述了-0 – 今天要讲的是不跟随系统的切换语言版本方案,即程序内部的切换语言版本方案. 一.总则: 应用内部语言 ...

  2. CentOS 7上修改主机名

                                       如何在CentOS 7上修改主机名                                           在Cent ...

  3. DROP CAST - 删除一个用户定义的类型转换

    SYNOPSIS DROP CAST (sourcetype AS targettype) [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP CAST 删除一个前面 ...

  4. params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render

    params.row[params.column.key] vue h函数 当前单元格 h函数 div 属性 值或数组 render

  5. django URL,views,html请求顺序

    进来的请求转入/hello/.   Django通过在ROOT_URLCONF配置来决定根URLconf.     Django在URLconf中的所有URL模式中,查找第一个匹配/hello/的条目 ...

  6. vue之placeholder中引用字体图标

    先说一下问题:在placeholder中想使用字体图标,结果渲染不正确,代码如图 效果如图 在网上get到了解决方法: 在VUE组件中,给placeholder添加图标,需要注意以下几点: 1.不要给 ...

  7. python数据类型常用内置函数之字符串

    1.strip, lstrip, rstrip x = ' jiahuifeng ' print(x.strip(' ')) print(x.lstrip(' ')) print(x.rstrip(' ...

  8. spring注解开发-容器创建全过程(源码)

    1.Spring容器的创建会经历refresh()方法[创建刷新](以AnnotationConfigApplicationContext为例) public AnnotationConfigAppl ...

  9. UVM入坑系列笔记(一)

    最近本人在做毕业设计,需要用到UVM搭建验证平台,故在网上查找相关资料,看了一些博客和科普,多少有些收获,记录在这里,以便以后复习查看.以下是本人根据网上学习资料整理的笔记,如果有什么不对的地方欢迎指 ...

  10. cf上分的失落->高兴->更失落

    cf昨日做出一个题居然div2打了1800多名直接上分了...我原以为垂直落地但是....我现在1399差一分就能蓝名了啊啊啊啊,以后不一定会有这个水平的啊,给个蓝名体验卡不行吗,多加一分会死啊... ...