HDU 5668 Circle
中国剩余定理。
可以手动模拟一下每一次开始的人的编号和结束的人的编号。
每次删掉一个人,对剩下的人重新编号。
这样一次模拟下来,可以得到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的更多相关文章
- hdu 5668 Circle 中国剩余定理
Circle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- BZOJ 2976: [Poi2002]出圈游戏 HDU 5668 CRT
2976: [Poi2002]出圈游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2976 Description Input 中第一 ...
- 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形成的不 ...
- HDU 5868 Different Circle Permutation(burnside 引理)
HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...
- HDU 1221 Rectangle and Circle(判断圆和矩形是不是相交)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1221 Rectangle and Circle Time Limit: 2000/1000 MS (J ...
- HDU 5343 MZL's Circle Zhou
MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...
- HDU 4669 Mutiples on a circle 数位DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4669 考察对取模的的理解深不深刻啊,当然还有状态的设计····设d[i][j]表示以第i个数结尾,余 ...
- 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 ...
- 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 ...
随机推荐
- Android学习笔记(十九) OkHttp
一.概述 根据我的理解,OkHttp是为了方便访问网络或者获取服务器的资源,而封装出来的一个工具包.通常的使用步骤是:首先初始化一个OkHttpClient对象,然后使用builder模式构造一个Re ...
- 解决Ueditor在bootstarp 模态框中全屏问题
基本的一些配置就不说了.先说一下要注意的问题:首先是zIndex的设置.记住最好都显示设置模态框和ueditor的zIndex.理清他们的层叠关系. 特别是用到ueditor里面的图片上传功能的更要设 ...
- 获取页面URL两种方式
以请求http://localhost:8080/doctor/demo?code=1为例 一:用java代码获取 //获取URL中的请求参数.即?后的条件 code=1 String querySt ...
- qt 设置阴影 不显示黑色边框
this->setAttribute(Qt::WA_TranslucentBackground);
- apropos命令
apropos——查看配置文件功能 示例1: # apropos ifconfig 显示ifconfig配置文件的功能,类似于执行man命令时的NAME信息
- PHP一句话后门过狗姿势万千之后门构造与隐藏
第二章节主要带给大家一些后门构造思路,与安全狗文件特征检测的机制. 另外强调一下,这篇文章需要大家对于php有一定的认识. 本章节分为三大部分,第一部分针对初级,分析菜刀php代码的执行过程,较基础: ...
- java分段加载数据,循环和递归两种方式
package org.jimmy.autosearch2019.test; import java.util.ArrayList; public class Test20190328 { priva ...
- 异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext 解决方案:缺少spring-expres ...
- node.js从入门到放弃(二)
上章讲了学习node,应该去学习什么,对这些框架去进行学习现在咋们聊聊如何用原生来进行操作 主要来讲一下events-事件触发器 events是什么东西呢?他的英文翻译,活动,事件的意思,在计算机语言 ...
- Ubuntu 和 centos7 服务的启动
Ubuntu 下: /etc/init.d/nginx start | stop | reload Centos7下: service nginx start | stop | reload