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 ...
随机推荐
- [转]为ReportViewer导出的PDF文档加上水印
接到一個頗富挑戰性的需求,Reporting Service或RDLC報表可匯出成Excel.PDF等檔案格式,對一般麻瓜型使用者而言,PDF唯讀,Excel則可修改,業務單位希望在拿到報表紙本時加以 ...
- 11.JAVA-Object类之finalize(),clone(),toString()等方法覆写
1.Object介绍 Object类是一个特殊的类,是所有类(包括数组,接口 )的父类,如果一个类没有用extends明确指出继承于某个类,那么它默认继承Object类,所以可以通过向上转型的方法使用 ...
- sublime text less安装踩坑图文讲解(less无法生成css)
唉,怎么感觉做个前端几乎把所有的坑都踩遍了啊,别人按照网上安装了一遍就好使,我这里就死活不行. 先说一下我的问题:网上说的能安装的都按了,可是sublime就是不给我生成css文件,后来知道了,就是l ...
- 提高VS2010运行速度的技巧+关闭拼写检查
任务管理器,CPU和内存都不高,为何?原因就是VS2010不停地读硬盘导致的; 写代码2/3的时间都耗在卡上了,太难受了; 研究发现,VS2010如果你装了VC等语言,那么它就会自动装SQL Serv ...
- HDU 4044 GeoDefense (树形DP,混合经典)
题意: 给一棵n个节点的树,点1为敌方基地,叶子结点都为我方阵地.我们可以在每个结点安放炸弹,每点至多放一个,每个结点有ki种炸弹可选,且每种炸弹有一个花费和一个攻击力(1点攻击力使敌人掉1点hp). ...
- The Django Book - 第四章 模板2
模板(相应)使用的几种方式: 1.使用HttpResponse返回字符串HTML from django.http import HttpResponse def current_datetime(r ...
- 利用python递归实现整数转换为字符串
def trans(num): if num // 10 == 0: return '%s'%num else: return trans(num//10)+'%s'%(num%10) a=trans ...
- shell脚本,计算1+2+3+....100等于多少?
第一种方法,通过for循环来计算[root@localhost wyb]# cat yibai.sh #!/bin/bash #从1+++...100的结果 i= ` do sum=$(($sum+i ...
- 【求助】NdisSend,自定义数据包发送失败?
做ndis hook的时候,自定义了一个数据包,包结构应该没有问题,填充NDIS_PACKET结构是这样的,先初始化: NdisAllocatePacketPool(&nStat ...
- BZOJ3301 P2524 UVA11525 算法解释康托展开
这三个题的代码分别对应第二个第一个第三个 在刘汝佳蓝书上我遇到了这个康托展开题. 当时去了解了一下,发现很有意思 百度上的康托展开定义 原理介绍 编辑 康托展开运算 其中, 为整数,并且 . 的意义为 ...