HDU 5514 Frogs
Frogs
This problem will be judged on HDU. Original ID: 5514
64-bit integer IO format: %I64d Java class name: Main
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
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
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
using LL = long long;
int n,m;
LL ret,a[maxn];
void dfs(int pos,LL lcm,int cnt) {
if(pos == n) {
if(cnt) {
LL num = m/lcm,tmp = (((num - )*num)>>)*lcm;
ret += (cnt&)?tmp:-tmp;
}
return;
}
if(lcm%a[pos] == ) return;
dfs(pos + ,lcm,cnt);
LL LCM = lcm*a[pos]/__gcd(lcm,a[pos]);
if(LCM < m) dfs(pos + ,LCM,cnt + );
}
int main() {
int kase,cs = ;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
for(int i = ; i < n; ++i) {
scanf("%I64d",a + i);
a[i] = __gcd(a[i],static_cast<LL>(m));
}
sort(a, a + n);
n = unique(a,a + n) - a;
ret = ;
if(a[] == ) ret = (static_cast<LL>(m)*(m-))>>;
else dfs(,,);
printf("Case #%d: %I64d\n",cs++,ret);
}
return ;
}
HDU 5514 Frogs的更多相关文章
- hdu 5514 Frogs(容斥)
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
- HDU 5514 Frogs (容斥原理)
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意 : 有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过a[i] ...
- HDU 5514 Frogs(容斥原理)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5514 [题目大意] m个石子围成一圈,标号为0~m-1,现在有n只青蛙,每只每次跳a[i]个石子, ...
- HDU 5514 Frogs 欧拉函数
题意: 有\(m(1 \leq m \leq 10^9)\)个石子排成一圈,编号分别为\(0,1,2 \cdots m-1\). 现在在\(0\)号石头上有\(n(1 \leq n \leq 10^4 ...
- hdu 5514 Frogs 容斥思想+gcd 银牌题
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5514 Frogs (容斥原理+因子分解)
题目链接 题意:有n只青蛙,m个石头(围成圆圈).第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少? 题解:暴力肯定会超时,首先分解出m的因子,自己本身不用分,因为石头编号是0 ...
- HDU 5514 Frogs (数论容斥)
题意:有n只青蛙,m个石头(围成圆圈).第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少? 析:首先可以知道的是第 i 只青蛙可以跳到 k * gcd(ai, m),然后我就计 ...
- HDU 5514.Frogs-欧拉函数 or 容斥原理
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
随机推荐
- Android studio 断点调试
最近进行业务测试,总是被测试环境不稳定或者测试数据错误困扰,一有问题就去找开发,自己都觉得很烦,所以就自己学着调试起来,这样以后遇到问题就可以自己定位的更深入了. 1.确保连接设备且进 ...
- JS中对数组元素进行增删改移
在js中对数组元素进行增删改移,简单总结了一下方法: 方法 说明 实例 push( ); 在原来数组中的元素最后面添加元素 arr.push("再见58"); unshift( ) ...
- Java基础之入门介绍
基础知识 1.JVM.JRE和JDK的区别: JVM(Java Virtual Machine):java虚拟机,用于保证java的跨平台的特性. java ...
- Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- (十三)maven之release和snapshots
发布release 用户A将代码打包到RELEASE仓库.用户B使用时,需要在pom.xml添加jar包的依赖坐标.如果用户A将jar包版本从1.0升级到2.0,用户B使用时也需要在pom.xml中修 ...
- Tunneling cannot be enabled without the local_ip bound to an interface on the host. Please configure local_ip 192.168.30.71 on the host interface to be used for tunneling and restart the agen
按照官方文档配置linux bridge 会出现一下问题 Tunneling cannot be enabled without the local_ip bound to an interface ...
- IOS修改系统音量
#import <IOKit/IOKitLib.h> #import <IOKit/hidsystem/IOHIDLib.h> #import <IOKit/hidsys ...
- (三)VMware harbor使用http访问
参考:https://www.cnblogs.com/biglittleant/p/7283738.html harbor使用http访问 如果使用http启动harbor需要在docker中配置-- ...
- feature map计算大小公式
http://blog.csdn.net/cheese_pop/article/details/51955915 将整个分成两部分,左边部分,右边部分.右边部分每次其实都是移动stride这么大,左边 ...
- python打开.pkl的文件并显示里面的内容
pkl文件是pyhthon里面保存文件的一种格式,如果直接打开会显示一堆序列化的东西.正确的打开方式如下: import cPickle as pickle f = open('path') info ...