HDU5514 Frogs (gcd + 容斥原理)
来源:互联网 发布:网络通信安全员证书 编辑:程序博客网 时间:2024/05/17 08:22
Frogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2946 Accepted Submission(s): 941
Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from0 to m−1 and the frogs are numbered from 1 to n . The i -th frog can jump over exactly ai stones in a single step, which means from stone j mod m to stone (j+ai) mod m (since all stones lie on a circle).
All frogs start their jump at stone0 , then each of them can jump as many steps as he wants. A frog will occupy a stone when he reach it, and he will keep jumping to occupy as much stones as possible. A stone is still considered ``occupied" after a frog jumped away.
They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.
The stones are numbered from
All frogs start their jump at stone
They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.
Input
There are multiple test cases (no more than 20 ), and the first line contains an integer t ,
meaning the total number of test cases.
For each test case, the first line contains two positive integern and m - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109) .
The second line containsn integers a1,a2,⋯,an , where ai denotes step length of the i -th frog (1≤ai≤109) .
meaning the total number of test cases.
For each test case, the first line contains two positive integer
The second line contains
Output
For each test case, you should print first the identifier of the test case and then the sum of all occupied stones' identifiers.
Sample Input
32 129 103 6022 33 669 9681 40 48 32 64 16 96 42 72
Sample Output
Case #1: 42Case #2: 1170Case #3: 1872
Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
思路:对于第i个青蛙来说,这个青蛙一定会经过k*gcd(a[i],m) < m的所有点,证明如下:设该青蛙的位置为c,走了x圈,跳了y次,则可得一个方程:ay+c=mx;
=> mx - ay = c ,则可得c的最小取值为gcd(a,m),则gcd(a,m) | c, 设c=k * gcd(a,m) => mx - ay = k * gcd(a,m) => ay % m = k * gcd (a,m) % m ; 所以可证第i个青蛙经过的点 为gcd(a[i],m)的所有倍数。又因为m较大,所以应先求出m的所有因子,第i个青蛙经过的石头一定是m的因子,最后通过容斥去掉重复即可,详细代码如下:
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define LL long longusing namespace std;const int maxn = 1e4 + 10;int t,n,m,cas=0,k;int vis[maxn],num[maxn],p[maxn];int gcd(int a, int b){ return b ? gcd(b,a%b) : a;}int main(){ scanf("%d",&t); while(t --){ scanf("%d%d",&n,&m); memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); k = 0; for(int i = 1; i <= sqrt(m); i ++){ if(m % i == 0){//计算出m的所有因子 p[k ++] = i; if(i != m/i) p[k ++] = m/i; } } int x; sort(p,p+k);//把m的因子按照从小到大排序,最后一个元素一定是m for(int i = 1; i <= n; i ++){ scanf("%d",&x); int r = gcd(x,m);//求x和m的最大公约数 for(int j = 0; j < k; j ++){ if(p[j] % r == 0) vis[j] = 1;//如果m的因子对r取模为0,则证明该点可以被走到 } } LL ans = 0; vis[k - 1] = 0;//m点不会被走到,应该点的编号是从0~m-1 for(int i = 0; i < k; i ++){ if(vis[i] != num[i]){//初始时vis[] - num[] = 1 int t = (m-1) / p[i];//计算有多少个p[i]的倍数 ans += (LL) t * (t+1) / 2 * p[i] * (vis[i] - num[i]);//t*(t+1)/2等差数列求和,后面再去掉重复的 t = vis[i] - num[i];//表示相应点的元素多算了几个 for(int j = i; j < k; j ++){ if(p[j] % p[i] == 0) num[j] += t;//后面减掉多计算的数 } } } printf("Case #%d: %lld\n",++cas,ans); } return 0;}
阅读全文
0 0
- HDU5514 Frogs (gcd + 容斥原理)
- HDU5514 Frogs 容斥原理
- hdu5514 Frogs(容斥)
- hdu5514 Frogs(容斥)
- HDU 5514 Frogs(容斥原理 gcd)
- hdu5514容斥原理和群论
- HDU5514 Frogs(GCD+欧拉函数+数论)
- HDU5514 Frogs
- hdu5514--frogs
- Frogs (hdu5514)——2015ACM/ICPC亚洲区沈阳站(容斥定理)
- HDU5514(容斥)
- 2015ACM/ICPC亚洲区沈阳站-重现赛 HUD 5514 Frogs (容斥原理+GCD)
- hdu5514 容斥原理 + dfs + 抵消减枝
- hdu 5514 -Frogs (容斥原理 )
- HDU 5514 Frogs 容斥原理
- HDU 5514 (Frogs) (容斥原理)
- HDU 5514 Frogs 【容斥原理】
- GCD 容斥原理
- 中间件的理解
- springboot+maven+springdata jpa +querydsl快速实现增删改查分页
- 手机网上商城-项目经验总结(二)-创建项目结构
- javaBean验证框架(6)—附加约束和组合约束
- 手机网上商城-项目经验总结(三)-QueryRunner
- HDU5514 Frogs (gcd + 容斥原理)
- 1.1基本算法举例(上篇)
- 2412. 【NOI2005】瑰丽华尔兹
- STL map
- 2017.10.7离线赛总结
- 手机网上商城-项目经验总结(四)-BaseServlet类
- 设计模式之享元模式(1)
- centos6.5安装mysql
- 欢迎使用Markdown编辑器写博客