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 ...
随机推荐
- Codeforces GYM 100741A . Queries
time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input output stan ...
- ldap_modify: No such object (32) matched DN: cn=config
centos 6.9 部署 kerbors ldap 报错 [root@hadoop data]# ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ld ...
- Android(java)学习笔记155:中文乱码的问题处理(qq登录案例)
1. 我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed& ...
- Linux-RedHat7.2 使用CentOS源
-- 查看yum rpm -qa |grep yum --卸载yum rpm -qa | grep yum | xargs rpm -e --nodeps --拷贝centos系统yum工具安装程序到 ...
- (转)关于treap的板子理解
关于treap的板子理解: 关于结构体的定义:(一般平衡树无法理解的变量名):v:节点的值:size:子节点的个数(包括自己):cnt:相同的值的副本数:l:左儿子:r:右儿子: 右旋:父亲变成左儿子 ...
- Microsoft Project 2010基础使用方法
5.1 项目管理与Microsoft Project2010 Microsoft Project2010深受广大项目管理工程师的青睐. 5.1.1 项目管理的概念 项目管理是项目管理者在有限的资源约束 ...
- tree iview treeData json数据 添加 selected 数据 要进行vue.set 进行响应式添加
tree iview treeData json数据 添加 selected 数据 要进行vue.set 进行响应式添加
- centOS7安装 mysql-community-release-el7-5.noarch.rpm 包
一.rpm包 1.wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm(下载rpm) 2.rpm -ivh mysql ...
- Spring框架 aop操作的注解方法 基于aspectj的自动注解aop方法 抽取相同的value="execution(public void cn.itcast.f_aspect.CRUD.*())"
首先是在xml配置文件中配置好对象,然后开启aop的注解方法——即<aop:aspectj-autoproxy></aop:aspectj-autoproxy> xml代码如下 ...
- Hibernate的持久化对象配置
定义Pojo对象和**.hbm.xml文件 -1 对于每一个需要持久化的对象都需要创建一个Pojo类定义,Hibernate要求POJO类定义中必须有一个no-argument的构造方法,便于Hibe ...