题目链接
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. SAP 企业管理软件与解决方案 产品简介
  2. python做啥用-python主要用于做什么
  3. 【正一专栏】巴萨艰难收获一场平局
  4. 一个没有好好的适应本土市场的失败的案例
  5. 程序员工资高,但为什么越来越多的人都不再愿意做程序员呢?
  6. LSA类型讲解——LSA-1【1类LSA——Router LSA】详解
  7. android 截屏_图文小编福利:Apowersoft专业截屏王
  8. nginx html目录 404 盘符_nginx配置场景分析 location /
  9. 在https上面使用ws不加密_ASP.NET Core 3.1 中使用JWT认证
  10. NOIp2017 题解
  11. 异速联:解决打印样式个性化设置
  12. 第一章 DHT11温湿度传感器的使用
  13. LoadRunner教程06:录制测试脚本
  14. abaqus更改计算机名,ABAQUS 2016 安装教程
  15. 泛微OA云桥 未授权任意文件读取
  16. 从计算机网络系统组成看 计算机网络可分为,从计算机网络系统组成的角度看,计算机网络可以分为 子网和资源子网。...
  17. 阿里云ecs概念介绍
  18. AntD 的 Descriptions 组件实现“ 右对齐效果 ”
  19. java 中rest,Java中的REST调用
  20. 清北学堂noip2018集训D3

热门文章

  1. ubuntu 安装咖啡壶-chemex命令详解
  2. Edge浏览器运行卡顿怎么办 怎样让Edge浏览器速度更快
  3. 软件工程实践 第四周第二次会议讨论
  4. 并发与同步、信号量与管程、生产者消费者问题
  5. python创建sqlite3 gbk错误_请问django python用sqlite3模拟创建一个服务器时出现错误?...
  6. Latex中使用thebibliography环境时去除“参考文献”标题方法
  7. java 生成数字_Java随机生成数字的方法
  8. PS去水印的四种方式
  9. CentOS支持NTFS格式
  10. JQuery插件机制