ACM学习历程—HDU5668 Circle(数论)
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(数论)的更多相关文章
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—HDU5666 Segment(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5666 这题的关键是q为质数,不妨设线段上点(x0, y0),则x0+y0=q. 那么直线方程则为y = y0/x ...
- ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...
- ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)
http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...
- ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)
题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5 这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥. ...
- ACM学习历程—HDU5637 Transform(数论 && 最短路)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给两种操作,然后给你一个s,一个t,求s至少需要多少次操作到t. 考虑到第一种操作是将 ...
- ACM学习历程—BZOJ2956 模积和(数论)
Description 求∑∑((n mod i)*(m mod j))其中1<=i<=n,1<=j<=m,i≠j. Input 第一行两个数n,m. Output 一个整数表 ...
- ACM学习历程—SNNUOJ1132 余数之和(数论)
Description F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数.例如F(6) = 6 % 1 + 6 % ...
- 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 ...
随机推荐
- http://www.uupoop.com/ps/
网页版PS,在线PS 基本的PS功能都有,最重要的一点是快,网页版的嘛,哼哼!
- sed实例
删除:d命令 $ sed '2d' example-----删除example文件的第二行. $ sed '2,$d' example-----删除example文件的第二行到末尾所有行. $ sed ...
- 【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very pr ...
- 【Head First Servlets and JSP】笔记11:cookie
容器如何知道客户是谁?(这并不是HTTP能实现的!IP地址不能唯一的标识用户,另外,非必要不采用HTTPS 继续mark孤傲苍狼的博客,百科全书 cookie——Header——字典——键值对—— 延 ...
- java 命令行
javac 编译 linux平台下:javac -cp ./hadoop-common-2.7.1.jar:./hadoop-mapreduce-client-core-2.7.4.jar: Word ...
- mongodb,redis简单学习
2.mongodb安装配置简单学习 配置好数据库路径就可以mongo命令执行交互操作了:先将服务器开起来:在开个cmd执行交互操作 ...
- android.intent.category.LAUNCHER和android.intent.action.MAIN
一个应用程序可以有多个Activity,每个Activity是同级别的,那么在启动程序时,最先启动哪个Activity呢? 有些程序可能需要显示在程序列表里,有些不需要.怎么定义呢? android. ...
- Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Java中interface是否继承Object类
首先我们从C++说起, c++可以多继承.也就是一个类型 --- class,可以继承自2个以上的父类型.多继承导致一个问题,很多人知道.例如,如果类型B,类型C均继承自类型A.然后类型D继承自类型B ...
- xenapi-add-support-for-vgpu
Model types 为了在vm中支持vgpu,问题在于vgpu是否应该被当作一个PCI设备, 虽然物理GPU不是一个SR-IOV设备,但vgpu却是以一个类似SR-IOV device上的VF赋给 ...