Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2957    Accepted Submission(s): 946

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
 
  • 观察对于n和m较小的时候的样例不难发现可以到达的点都是k*gcd(ai,m)<m
  • 但是对于给出的n个a[i]我们不能保证在计算过程中对于同一个点只计算一次
  • 考虑容斥原理解题
  • gcd(ai,m)=di的结果都属于m的因数集合D(m)
  • 如果 u*ai==v*aj 并且 u*ai 属于D(m),那么对于 u*ai的倍数就重复计算了一次,我们就应该在新的计算过程中减去多加的次数
  • 对于多加的次数这个变量的维护可以通过记录当前因数被使用的次数来实现,1-被使用次数就是当前因数对应增量的倍数
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; LL gcd(LL x, LL y){
return y?gcd(y,x%y):x;
}
int T, tot;
LL n, m, a, d, k, ans;
LL b[maxn], c[maxn], e[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
for(int kase=;kase<=T;kase++){
ans=0LL;
tot=;
scanf("%lld %lld",&n,&m);
for(LL i=1LL;i<=(LL)sqrt(m);i++)
if(==m%i){
c[tot++]=i;
if(i!=m%i)
c[tot++]=m/i;
}
sort(c,c+tot);
memset(b,,sizeof(b));
memset(e,,sizeof(e));
for(int i=;i<=n;i++){
scanf("%lld",&a);
d=gcd(a,m);
b[lower_bound(c,c+tot,d)-c]=;
}
for(int i=;i<tot;i++)
if(b[i])
for(int j=i+;j<tot;j++)
if(c[j]%c[i]== && b[j]==)
b[j]=;
for(int i=;i<tot;i++)
if(b[i]){
LL cnt=b[i]-e[i];
k=(m-)/c[i];
// k=(m/c[i])-1;
ans+=cnt*k*(c[i]+k*c[i])/;
for(int j=i+;j<tot;j++)
if(c[j]%c[i]==)
e[j]+=cnt;
}
printf("Case #%d: %lld\n",kase,ans);
}
}
return ;
}

HDU_5514_Frogs的更多相关文章

随机推荐

  1. (3)FluidMoveBehavior 之模仿 Windows Phone 开始菜单的 Tile 长按后排序

    这个工程和上一篇 (2)中介绍的排序大同小异,只是比上一篇交换复杂一点,不是通过单击进行交换, 而是拖动一个 Tile 到另一个 Tile 上时,判断两个 Tile 的中心距离是否符合条件来判断是否进 ...

  2. angularjs 构建主页 内置过滤器、日期的格式化

    从构建负责管理主屏幕的 MainController 开始.在这个 MainController 控制器内,只需设置一个每秒运转一次,同时更新一个局部作用域变量的延时 angular.module(' ...

  3. jQuery select添加图标

    //下拉菜单样式 /*查找全部select的下拉菜单*/ function getElemsById(cot_val){ return document.getElementById(cot_val) ...

  4. Mysql变量声明与使用

    set @today='2017-04-25';set @ydate=DATE_SUB(@today, INTERVAL 7 day);select @today, @ydate; 待续....

  5. supervisor配置详解

    原文:http://lixcto.blog.51cto.com/4834175/1539136 有阵子没写博客了,这段时间一直在研究python django框架和前端相关的东西.楼主学通信的,对we ...

  6. 解决阿里云部署 office web apps ApplicationFailedException 报错问题

    查找这个问题,确实花费了很长时间,所以具体解析一下问题原因吧. 报错如下: 问题详情链接 New-OfficeWebAppsFarm:Office Online服务无法启动.有关详细信息,请参阅Win ...

  7. ubuntu14.04中安装jdk

    1. 下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 将下载的 .g ...

  8. 断言(assert)和程序的安全保证

    断言,用来DEBUG错误的,在DEBUG时发现然后跟踪错误! 通常 写一个程序给别人使用的,这个代码在安全性上的要求是什么呢?直觉上,我们都知道程序不应该崩.但是通常C/C++的程序如果把包含API的 ...

  9. 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛(差分序列)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1635 差分序列是个好东西啊....很多地方都用了啊,,, 线性的进行区间操作orz 有题可知 h[a ...

  10. hdu 2059:龟兔赛跑(动态规划 DP)

    龟兔赛跑 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...