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 ...
随机推荐
- C#中 添加 删除 查找Xml中子节点
//添加xml节点 private void AddXml(string image, string title) { XmlDocument xmlDoc = new X ...
- nodejs 快要变成爬虫界的王者
nodejs 快要变成爬虫界的王者 爬虫这东西是很多数据采集必须要的东西. 但是现在随着网页不断发展,已经出现了出单纯的网页,到 ajax 网页, 再到 spa , 再到 websocket 应用,一 ...
- kafka java api生产者
import java.util.HashMap; import java.util.List;import java.util.Map;import java.util.Properties; im ...
- CString的GetBuffer和ReleaseBuffer
GetBuffer()主要作用是将字符串的缓冲区长度锁定,releaseBuffer则是解除锁定,使得CString对象在以后的代码中继续可以实现长度自适应增长的功能. CString ::GetBu ...
- Objective-C Data Encapsulation
All Objective-C programs are composed of the following two fundamental elements: Program statements ...
- XML文件的解析和序列化
序列化: private void createXml() { XmlSerializer serializer = Xml.newSerializer();// xml文件生成器 File file ...
- 源文件名长度大于系统支持的长度,无法删除,java主方法执行方式删除
import java.io.File; /** * @author 海盗船长 * 2017年2月14日11:24:26 */ public class DeleteFiles { public st ...
- Spring MVC系列[2]——参数传递及重定向
1.目录结构 2.代码 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- XSS 攻击实验 & 防御方案
XSS 攻击&防御实验 不要觉得你的网站很安全,实际上每个网站或多或少都存在漏洞,其中xss/csrf是最常见的漏洞,也是最容易被开发者忽略的漏洞,一不小心就要被黑 下面以一个用户列表页面来演 ...
- 远程linux服务器mysql数据库定期备份和删除
网上已经有部分关于Linux下定期备份mysql的方法,但是很多步骤不够详细,不适合新手,自己琢磨了很久,终于搞定了. 1.Linux服务器一般是ssh协议,如果本地也是Linux环境,可以直接通过s ...