首页 技术 正文
技术 2022年11月23日
0 收藏 640 点赞 4,163 浏览 1898 个字

Frogs

Time Limit: 1000msMemory Limit: 65536KB

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

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;
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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849