http://acm.hdu.edu.cn/showproblem.php?pid=5668

这题的话,假设每次报x个,那么可以模拟一遍,

假设第i个出局的是a[i],那么从第i-1个出局的人后,重新报数到他假设经过了p个人,

那么自然x = k(n-i)+p(0<= i < n)

即x = p (mod n-i)

然后显然可以得到n个这样的方程,于是就是中国剩余定理了。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; int n, s[];
bool vis[];
LL rest[], mmod[]; LL gcd(LL a, LL b)
{
LL r;
while (b != )
{
r = b;
b = a%b;
a = r;
}
return a;
} LL lcm(LL a, LL b)
{
return a/gcd(a, b)*b;
} //EXGCD
//求解方程ax+by=d,即ax=d mod(b)
//扩展可求逆元
//O(logn)
void exgcd(LL a, LL b, LL &x, LL &y, LL &d)
{
if (b == )
{
x = ;
y = ;
d = a;
}
else
{
exgcd(b, a%b, y, x, d);
y -= a/b*x;
}
} //中国剩余定理(非互质)
//其中a为除数数组,n为模数数组
LL inv(LL a, LL n)
{
LL x, y, d;
exgcd(a, n, x, y, d);
if (d != )
return -;
return (x%n + n) % n;
} bool Merge(LL a1, LL n1, LL a2, LL n2, LL &aa, LL &nn)
{
LL d = gcd(n1, n2);
LL c = a2 - a1;
if (c % d)
return false;
//(n1/d)*k1 = (c/d)(mod(n2/d))
c /= d;
n1 /= d;
n2 /= d;
//k1 = (c/d)*(n1/d)^(-1)(mod(n2/d))
c *= inv(n1, n2);
c %= n2;//k1
c *= n1 * d;//a = n1*k1+a1
c += a1;
nn = n1*n2*d;
aa = c;
return true;
} LL CRT(int len, LL *a, LL *n)
{
LL a1 = a[], n1 = n[];
LL a2, n2;
for (int i = ; i < len; i++)
{
LL aa, nn;
a2 = a[i], n2 = n[i];
if (!Merge(a1, n1, a2, n2, aa, nn))
return -;
a1 = aa;
n1 = nn;
}
return (a1%n1 + n1) % n1;
} void work()
{
LL ans, tmp = ;
memset(vis, false, sizeof(vis));
int now = , step, t = ;
for (int i = ; i < n; ++i)
{
step = ;
for (int j = t%n; j < n; j = (j+)%n)
{
if (vis[j]) continue;
step++;
if (j == s[now])
{
now++;
vis[j] = true;
rest[i] = step%(n-i);
mmod[i] = n-i;
tmp = lcm(tmp, n-i);
t = j;
break;
}
}
}
ans = CRT(n, rest, mmod);
if (ans == -)
printf("Creation August is a SB!\n");
else
{
ans = (ans%tmp+tmp)%tmp;
if (ans == ) ans = tmp;
cout << ans << endl;
}
} int main()
{
//freopen("test.in", "r", stdin);
int T, u;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
scanf("%d", &u);
s[u-] = i;
}
work();
}
return ;
}

ACM学习历程—HDU5668 Circle(数论)的更多相关文章

  1. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  2. ACM学习历程—HDU5666 Segment(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...

  3. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

  4. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

  5. ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5 这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥. ...

  6. ACM学习历程—HDU5637 Transform(数论 && 最短路)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...

  7. ACM学习历程—BZOJ2956 模积和(数论)

    Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...

  8. ACM学习历程—SNNUOJ1132 余数之和(数论)

    Description F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数.例如F(6) = 6 % 1 + 6 % ...

  9. ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)

    Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...

随机推荐

  1. 17南宁区域赛 J - Rearrangement 【规律】

    题目链接 https://nanti.jisuanke.com/t/19976 题意 给出 一个n 然后 给出 2*n 个数 可以重新排列成两行 然后 相邻的两个数 加起来 不能被三整除 可以上下相邻 ...

  2. 【转】Linux查看物理CPU个数、核数、逻辑CPU个数

    # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/cpuinfo| g ...

  3. eclipse连接SqlServer2008(被它搞得惨兮兮)

    建民大叔告诉我要考试做一个系统要求连接SqlServer2008,于是我便开始了“炼狱”,人家连接起来一路绿灯,我却一路红灯所以决定把它记录下来,给后来人提供方便. 第一个红灯: 启动服务后利用cmd ...

  4. $SVN代码版本管理工具的使用

    SVN是一种代码版本管理工具,具有可视化的操作界面,使用简便,和git的功能类似.下面总结一下SVN的基本用法: 1.安装SVN软件,和安装一般的软件的步骤差不多,这里使用的版本是TortoiseSV ...

  5. MongoDB命令语法小用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using MongoDB; ...

  6. bootstrap 模态框中弹出层 input不能获得焦点且不可编辑

    bootstrap 模态框中弹出层 input不能获得焦点且不可编辑 问题描述:bs框架支持一层model层的情况下,在模态框中弹出了自定义的弹出层.发现自定义弹出层的输入框不能获得焦点且不可编辑. ...

  7. OC_内存管理(二)对象复制、循环引用问题、自动释放池

      循环调用: 1.循环引用的问题 两个对象A.B,有可能会出现特殊情况:A中包含B的实例变量:B中也包含A的实例变量,如果这两个实例变量都是强引用(A有着B的实例变量所有权,B也有A的实例变量所有权 ...

  8. Docker 监控平台Prometheus

    Prometheus 是一个强大的监控平台,提供了监控数据搜集.存储.处理.可视化和告警一套完整的解决方案. 官方网站:https://prometheus.io

  9. 【[NOI2011]智能车比赛】(建图+spfa+坑爹精度)

    过了这题我就想说一声艹,跟这个题死磕了将近6个小时,终于是把这个题死磕出来了.首先看到这个题的第一反应,和当初做过的一个房间最短路比较相似,然后考虑像那个题那样建边,然后跑最短路.(具体建边方法请参考 ...

  10. CentOS 7 安装 maven

    下载地址 http://maven.apache.org/download.cgi 版本 apache-maven-3.3.9 -bin.tar.gz tar -xvf apache-maven-3. ...