题目链接
Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from 0 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 stone 0, 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.

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 integer n and m - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109).

The second line contains n integers a1,a2,⋯,an, where ai denotes step length of the i-th frog (1≤ai≤109).

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
3
2 12
9 10
3 60
22 33 66
9 96
81 40 48 32 64 16 96 42 72

Sample Output
Case #1: 42
Case #2: 1170
Case #3: 1872

Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e4+1;
int num[maxn];
vector<int>v;
int main()
{int T,n,m,t;scanf("%d",&T);for(int s=1;s<=T;++s){v.clear();int flag=0;scanf("%d %d",&n,&m);for(int i=0;i<maxn;++i) num[i]=0;for(int i=1;i*i<=m;++i)//存m的因子 if(m%i==0){v.push_back(i);if(i!=m/i) v.push_back(m/i);}sort(v.begin(),v.end());for(int i=1;i<=n;++i){scanf("%d",&t);t=__gcd(t,m);if(t==1)flag=1;for(int j=0;j<v.size();++j)if(v[j]%t==0) num[j]=1;} printf("Case #%d: ",s);if(flag){printf("%lld\n",1LL*m*(m-1)/2);continue;}ll ans=0;for(int i=0;i<v.size();++i){if(num[i]==0) continue;ans+=1LL*(m/v[i]-1)*m/2*num[i];//算每个因子的贡献 for(int j=i+1;j<v.size();++j)//如果m的因子当中有g【i】的倍数的话要把它删去 if(v[j]%v[i]==0) num[j]-=num[i];}printf("%lld\n",ans);}
}

hdu5514 Frogs(容斥原理)相关推荐

  1. HDU5514 Frogs

    HDU5514 Frogs 题意:将\([0,m)\)所有符合\(a[i]*t ~mod~ m\)的值求和 做法: \(a[i]*t ~mod~ m\) 会在 \(gcd(a[i],m)\) 的倍数出 ...

  2. HDU-5514 Frogs

    题目大意是给定一些(n)青蛙及其跳跃的步数和方法,给定一些石头,编号从1~m,问所有曾经被青蛙跳过的石头的编号的和. 思路 容斥定理 从题目中很容易得出每一个青蛙的跳过的石头编号是k * gcd(m, ...

  3. HDU 5514 Frogs (容斥原理+因子分解)

    题目链接 题意:有n只青蛙,m个石头(围成圆圈).第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少? 题解:暴力肯定会超时,首先分解出m的因子,自己本身不用分,因为石头编号是0 ...

  4. HDU - 5514 Frogs(容斥原理)

    题目链接:点击查看 题目大意:给出 0 ~ m - 1 共 m 个石头首尾相连呈现出一个环状,初始时在第 0 个石头上有 n 只青蛙,n 只青蛙相互独立,每一只青蛙每次都会向前跳 a[ i ] 步,问 ...

  5. HDU 5514 Frogs (容斥原理)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意 : 有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过a[i] ...

  6. 信息学竞赛中的数学知识 --- 容斥原理

    C++基础数论-----容斥原理 C++基础数论-----容斥原理_C2020lax的博客-CSDN博客_容斥原理c++ C++数论容斥原理----无关的元素 C++数论容斥原理----无关的元素 - ...

  7. Frogs - HDU5514

    Frogs - HDU5514 icpc沈阳2015的题目. 容斥原理和数论的应用. 题目大意为给你一个数组 a[i]a[i]a[i](a[i]a[i]a[i] 即为第 iii 只青蛙的步长) 和若干 ...

  8. HDU 5514 Frogs(欧拉函数+数论YY)

    传送门 Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  9. HDU 4135 Co-prime(容斥原理)

    Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...

  10. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3028  Solved: 1460 [Submit][S ...

最新文章

  1. linux配置java环境变量(详细)
  2. [转] 大连理工大学部分有效FTP列表1.0
  3. 史上最详细的Android Studio系列教程四--Gradle基础
  4. Surf特征提取分析
  5. 男女薪酬差异扩大 2018年女性薪酬不及男性8成
  6. 两届诺贝尔文学奖得主将同时公布
  7. inteliJ maven 打包时把依赖的包一起打
  8. aspose.word使用简单方法
  9. Linux磁盘管理实战
  10. 创建JSON集合使用JSONArray.fromObject 转化后得到的jsonArray集为空?
  11. 物联网单片机毕业设计实现
  12. Ubuntu下Gnome修改键盘映射
  13. java saxreader 字符串_Java SAXReader.read方法代碼示例
  14. 【Git】675- 让你的 commit 更有价值
  15. 国内汽车车载电源DCDC首家,硬件原理图,软件源码
  16. 计算机数值数据编码(原码,反码,补码,移码)
  17. 教你一招快速清理DNS缓存
  18. jsf<h:outpytText>实现换行
  19. 后缀自动机1003 HDU 4416
  20. solar2lunar 实现农历、天干地支

热门文章

  1. 怎样让计算机加快速度,六大招教你把旧电脑恢复如新,速度提升N倍!-怎么让电脑速度变快...
  2. PS图片上传图片 同时生成微缩图
  3. Easy Unpack
  4. android日期时间的获取与时差计算
  5. 算法-“许三多”方法
  6. 随笔—醒悟篇之考研调剂
  7. HTML的relative与absolute区别
  8. linux gem安装软件,安装gem报错
  9. 前端大牛工程师Nicholas C. Zakas:我得到的最佳职业生涯建议
  10. 关于使用火车采集器采集分页URL不变化网站